KItinerary

uic9183header.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "uic9183header.h"
8#include "logging.h"
9
10#include <cstdint>
11#include <cstring>
12
13using namespace KItinerary;
14
15enum {
16 PrefixSize = 14,
17 SignatureSizeV1 = 50,
18 SignatureSizeV2 = 64,
19 SuffixSize = 4,
20 ZlibHeaderSize = 2
21};
22
23static bool isZlibHeader(const QByteArray &data, int offset)
24{
25 // check for zlib headers 0x789C or 0x78DA
26 return ((uint8_t)data[offset] == 0x78 && ((uint8_t)data[offset + 1] == 0x9C || (uint8_t)data[offset + 1] == 0xDA));
27}
28
29Uic9183Header::Uic9183Header() = default;
30Uic9183Header::Uic9183Header(const QByteArray& data)
31{
32 if (data.size() < (PrefixSize + SignatureSizeV1 + SuffixSize + ZlibHeaderSize)) {
33 return;
34 }
35 if (!data.startsWith("#UT") && !data.startsWith("OTI")) {
36 return;
37 }
38
39 const auto version = Uic9183Utils::readAsciiEncodedNumber(data, 3, 2);
40 if (version != 1 && version != 2) {
41 return;
42 }
43 auto offset = PrefixSize + (version == 1 ? SignatureSizeV1 : SignatureSizeV2) + SuffixSize;
44 if (data.size() < offset + ZlibHeaderSize) {
45 return;
46 }
47
48 // compressedMessageOffset() contains workarounds for wrong version claims, which the above check doesn't do yet
49 m_data = data;
50 offset = compressedMessageOffset();
51 if (!isZlibHeader(data, offset)) {
52 qCWarning(Log) << "UIC 918-3 payload has wrong zlib header.";
53 m_data.clear();
54 return;
55 }
56
57 m_data = data;
58}
59
60bool Uic9183Header::operator==(const Uic9183Header &other) const
61{
62 return isValid() && other.isValid() && std::memcmp(m_data.constData(), other.m_data.constData(), PrefixSize) == 0;
63}
64
65bool Uic9183Header::isValid() const
66{
67 return !m_data.isEmpty();
68}
69
70int Uic9183Header::signatureSize() const
71{
72 switch (version()) {
73 case 1: return SignatureSizeV1;
74 case 2:
75 {
76 // workaround some tickets claiming version 2 but actually being version 1...
77 if (isZlibHeader(m_data, PrefixSize + SignatureSizeV2 + SuffixSize)) {
78 return SignatureSizeV2;
79 }
80 if (isZlibHeader(m_data, PrefixSize + SignatureSizeV1 + SuffixSize)) {
81 return SignatureSizeV1;
82 }
83 return SignatureSizeV2;
84 }
85 };
86 return 0;
87}
88
89int Uic9183Header::compressedMessageSize() const
90{
91 return Uic9183Utils::readAsciiEncodedNumber(m_data, PrefixSize + signatureSize(), 4);
92}
93
94int Uic9183Header::compressedMessageOffset() const
95{
96 return PrefixSize + signatureSize() + SuffixSize;
97}
98
99#include "moc_uic9183header.cpp"
Header of an UIC 918.3 ticket.
KDB_EXPORT KDbVersionInfo version()
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
void clear()
const char * constData() const const
bool isEmpty() const const
qsizetype size() const const
bool startsWith(QByteArrayView bv) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 11:54:20 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.