KItinerary

uic9183header.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
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 
12 using namespace KItinerary;
13 
14 enum {
15  PrefixSize = 14,
16  SignatureSizeV1 = 50,
17  SignatureSizeV2 = 64,
18  SuffixSize = 4,
19  ZlibHeaderSize = 2
20 };
21 
22 Uic9183Header::Uic9183Header() = default;
23 Uic9183Header::Uic9183Header(const QByteArray& data)
24 {
25  if (data.size() < (PrefixSize + SignatureSizeV1 + SuffixSize + ZlibHeaderSize)) {
26  return;
27  }
28  if (!data.startsWith("#UT") && !data.startsWith("OTI")) {
29  return;
30  }
31 
32  const auto version = Uic9183Utils::readAsciiEncodedNumber(data, 3, 2);
33  if (version != 1 && version != 2) {
34  return;
35  }
36  const auto offset = PrefixSize + (version == 1 ? SignatureSizeV1 : SignatureSizeV2) + SuffixSize;
37  if (data.size() < offset + ZlibHeaderSize) {
38  return;
39  }
40 
41  // check for zlib headers 0x789C or 0x78DA
42  if ((uint8_t)data[offset] != 0x78 || ((uint8_t)data[offset + 1] != 0x9C && (uint8_t)data[offset + 1] != 0xDA)) {
43  qCWarning(Log) << "UIC 918-3 payload has wrong zlib header.";
44  return;
45  }
46 
47  m_data = data;
48 }
49 
50 bool Uic9183Header::isValid() const
51 {
52  return !m_data.isEmpty();
53 }
54 
55 int Uic9183Header::signatureSize() const
56 {
57  switch (version()) {
58  case 1: return 50;
59  case 2: return 64;
60  };
61  return 0;
62 }
63 
64 int Uic9183Header::compressedMessageSize() const
65 {
66  return Uic9183Utils::readAsciiEncodedNumber(m_data, 14 + signatureSize(), 4);
67 }
68 
69 int Uic9183Header::compressedMessageOffset() const
70 {
71  return PrefixSize + (version() == 1 ? SignatureSizeV1 : SignatureSizeV2) + SuffixSize;
72 }
Classes for reservation/travel data models, data extraction and data augmentation.
bool isEmpty() const const
bool startsWith(const QByteArray &ba) const const
int size() const const
KDB_EXPORT KDbVersionInfo version()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jan 20 2022 23:06:12 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.