KItinerary

uic9183block.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "uic9183block.h"
8#include "uic9183utils.h"
9#include "logging.h"
10
11#include <cstring>
12
13using namespace KItinerary;
14
15enum {
16 BlockHeaderSize = 12,
17 BlockVersionOffset = 6,
18 BlockVersionSize = 2,
19 BlockSizeOffset = 8,
20 BlockSizeSize = 4,
21};
22
23Uic9183Block::Uic9183Block() = default;
24Uic9183Block::Uic9183Block(const Uic9183Block&) = default;
25Uic9183Block::Uic9183Block(Uic9183Block&&) = default;
26Uic9183Block& Uic9183Block::operator=(const Uic9183Block&) = default;
27Uic9183Block& Uic9183Block::operator=(Uic9183Block&&) = default;
28
29Uic9183Block::Uic9183Block(const QByteArray &data, int offset)
30 : m_offset(offset)
31{
32 if (data.size() < offset + BlockHeaderSize) {
33 return;
34 }
35
36 const auto blockSize = Uic9183Utils::readAsciiEncodedNumber(data, offset + BlockSizeOffset, BlockSizeSize);
37 if (data.size() < (blockSize + offset) || blockSize < BlockHeaderSize) {
38 return;
39 }
40
41 m_data = data;
42}
43
44bool Uic9183Block::operator==(const Uic9183Block &other) const
45{
46 return size() == other.size() && std::memcmp(m_data.constData() + m_offset, other.m_data.constData() + other.m_offset, size()) == 0;
47}
48
49
50// 6x header name
51// 2x block version
52// 4x block size as string, including the header
53// followed by block payload (as 12 byte offset from header start)
54
55const char* Uic9183Block::name() const
56{
57 if (isNull()) {
58 return nullptr;
59 }
60 return m_data.constData() + m_offset;
61}
62
63bool Uic9183Block::isA(const char recordId[6]) const
64{
65 return std::strncmp(name(), recordId, 6) == 0;
66}
67
68const char* Uic9183Block::content() const
69{
70 if (isNull()) {
71 return nullptr;
72 }
73 return m_data.constData() + m_offset + BlockHeaderSize;
74}
75
77{
78 return Uic9183Utils::readAsciiEncodedNumber(m_data, m_offset + BlockSizeOffset, BlockSizeSize);
79}
80
82{
83 return std::max(0, size() - BlockHeaderSize);
84}
85
87{
88 return Uic9183Utils::readAsciiEncodedNumber(m_data, m_offset + BlockVersionOffset, BlockVersionSize);
89}
90
92{
93 return m_data.isEmpty();
94}
95
97{
98 return Uic9183Block(m_data, m_offset + size());
99}
100
102{
103 return Uic9183Utils::readUtf8String(m_data, m_offset + BlockHeaderSize, contentSize());
104}
105
106#include "moc_uic9183block.cpp"
A data block from a UIC 918.3 ticket.
const char * name() const
Returns the block name (6 characters).
int contentSize() const
Returns the size of the content data.
bool isA() const
Checks if a block is of type.
bool isNull() const
Checks if the block is valid or empty/default constructed.
Uic9183Block nextBlock() const
Returns the next block in the ticket.
int version() const
Returns the version number of this block.
int size() const
Returns the size of the entire block data.
QString contentText
Content as string, for use in JS.
const char * content() const
Returns the payload data (not including the block header).
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
const char * constData() const const
bool isEmpty() const const
qsizetype size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.