Commit 859e6d42 authored by Volker Krause's avatar Volker Krause
Browse files

Add a class encapsulating U_HEAD decoding

This is currently done adhoc in Uic9183Parser with magic numbers spread
all over.
parent 845dff1c
......@@ -53,6 +53,7 @@ set(kitinerary_lib_srcs
uic9183/rct2ticket.cpp
uic9183/uic9183block.cpp
uic9183/uic9183head.cpp
uic9183/uic9183parser.cpp
uic9183/uic9183ticketlayout.cpp
uic9183/vendor0080block.cpp
......
......@@ -5,6 +5,7 @@
*/
#include "uic9183block.h"
#include "logging.h"
using namespace KItinerary;
......@@ -84,3 +85,27 @@ QString Uic9183Block::contentText() const
{
return QString::fromUtf8(content(), contentSize());
}
int Uic9183Block::readAsciiEncodedNumber(int offset, int length) const
{
if (isNull() || offset < 0 || length < 1 || m_offset + BlockHeaderSize + offset + length > m_data.size()) {
qCWarning(Log) << "invalid UIC 918.3 block content read:" << offset << length << m_offset << m_data.size();
return {};
}
int v = 0;
for (int i = 0; i < length; ++i) {
v *= 10;
v += (*(content() + offset + i)) - '0';
}
return v;
}
QString Uic9183Block::readUtf8String(int offset, int length) const
{
if (isNull() || offset < 0 || length < 1 || m_offset + BlockHeaderSize + offset + length > m_data.size()) {
qCWarning(Log) << "invalid UIC 918.3 block content read:" << offset << length << m_offset << m_data.size();
return {};
}
return QString::fromUtf8(content() + offset, length);
}
......@@ -50,6 +50,11 @@ public:
*/
Uic9183Block nextBlock() const;
/** Reads a number from content at offset @p offset and @p length in ASCII encoding. */
int readAsciiEncodedNumber(int offset, int length) const;
/** Reads a string from the content starting at offset @p offset with @p length, assuming UTF-8 encoding. */
QString readUtf8String(int offset, int length) const;
private:
QString contentText() const;
......
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "uic9183head.h"
using namespace KItinerary;
// U_HEAD Block (version 1, size 53)
// 4x issuing carrier id
// 6x PNR
// 20x unique ticket key
// 12x issuing date/time as ddMMyyyyHHMM, as UTC
// 1x flags
// 2x ticket language
// 2x secondary ticket language
Uic9183Head::Uic9183Head(const Uic9183Block &block)
{
if (block.version() == 1 && block.size() == 53) {
m_block = block;
}
}
bool Uic9183Head::isValid() const
{
return !m_block.isNull();
}
QDateTime Uic9183Head::issuingDateTime() const
{
return QDateTime::fromString(m_block.readUtf8String(24, 12), QStringLiteral("ddMMyyyyhhmm"));
}
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KITINERARY_UIC9183HEAD_H
#define KITINERARY_UIC9183HEAD_H
#include "kitinerary_export.h"
#include "uic9183block.h"
#include <QDateTime>
namespace KItinerary {
#define HEAD_NUM_PROPERTY(Name, Offset, Length) \
public: \
inline int Name() const { return m_block.readAsciiEncodedNumber(Offset, Length); } \
Q_PROPERTY(int Name READ Name)
#define HEAD_STR_PROPERTY(Name, Offset, Length) \
inline QString Name() const { return m_block.readUtf8String(Offset, Length); } \
Q_PROPERTY(QString Name READ Name)
/** U_HEAD block of a UIC 918.3 ticket container.
* @see ERA TAP TSI TD B.12 Digital Security Elements For Rail Passenger Ticketing - §10.3.1 Main record (U_HEAD)
*/
class KITINERARY_EXPORT Uic9183Head
{
Q_GADGET
HEAD_NUM_PROPERTY(issuerCompanyCodeNumeric, 0, 4)
HEAD_STR_PROPERTY(issuerCompanyCodeString, 0, 4)
HEAD_STR_PROPERTY(ticketKey, 4, 20)
Q_PROPERTY(QDateTime issuingDateTime READ issuingDateTime)
HEAD_NUM_PROPERTY(flags, 36, 1)
HEAD_STR_PROPERTY(primaryLanguage, 37, 2);
HEAD_STR_PROPERTY(secondaryLanguage, 39, 2);
public:
Uic9183Head(const Uic9183Block &block);
/** Returns @c true if this is a valid U_HEAD block. */
bool isValid() const;
QDateTime issuingDateTime() const;
static constexpr const char RecordId[] = "U_HEAD";
private:
Uic9183Block m_block;
};
#undef HEAD_NUM_PROPERTY
}
#endif // KITINERARY_UIC9183HEAD_H
......@@ -179,7 +179,7 @@ Uic9183TicketLayout& Uic9183TicketLayout::operator=(const Uic9183TicketLayout&)
QString Uic9183TicketLayout::type() const
{
return QString::fromUtf8(d->block.content(), 4);
return d->block.readUtf8String(0, 4);
}
bool Uic9183TicketLayout::isValid() const
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment