KItinerary

ssbticketreader.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 "ssbticketreader.h"
8#include "ssbv1ticket.h"
9#include "ssbv2ticket.h"
10#include "ssbv3ticket.h"
11
12#include <QVariant>
13
14using namespace KItinerary;
15
16[[nodiscard]] static bool isBase64Char(char c)
17{
18 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '=' || c == '/' || c == '+';
19}
20
21[[nodiscard]] static bool maybeBase64(QByteArrayView data)
22{
23 return std::all_of(data.begin(), data.end(), isBase64Char);
24}
25
27{
29 return true;
30 }
31
32 // base64 encoded tickets are found in the wild, although that isn't specified anywhere AFAIK
33 if (data.size() > 168 || !maybeBase64(data)) { // 4/3 * largest SSB ticket size
34 return false;
35 }
36
37 const auto decoded = QByteArray::fromBase64(data);
38 return SSBv1Ticket::maybeSSB(decoded) || SSBv2Ticket::maybeSSB(decoded) || SSBv3Ticket::maybeSSB(decoded);
39}
40
41[[nodiscard]] static QVariant readInternal(QByteArray ticketData, int versionOverride)
42{
43 auto version = ticketData[0] >> 4;
44 if (versionOverride > 0 && version != versionOverride) {
45 ticketData[0] = (ticketData[0] & 0x0f) | (versionOverride << 4);
46 version = versionOverride;
47 }
48
49 switch (version) {
50 case 1:
51 {
52 SSBv1Ticket ticket(ticketData);
53 return ticket.isValid() ? QVariant::fromValue(ticket) : QVariant();
54 }
55 case 2:
56 {
57 SSBv2Ticket ticket(ticketData);
58 return ticket.isValid() ? QVariant::fromValue(ticket) : QVariant();
59 }
60 case 3:
61 {
62 SSBv3Ticket ticket(ticketData);
63 return ticket.isValid() ? QVariant::fromValue(ticket) : QVariant();
64 }
65 }
66 return {};
67}
68
69QVariant SSBTicketReader::read(const QByteArray& data, int versionOverride)
70{
71 if (data.isEmpty()) {
72 return {};
73 }
74
75 auto ticket = readInternal(data, versionOverride);
76 if (ticket.isValid() || data.size() > 168 || !maybeBase64(data)) {
77 return ticket;
78 }
79
80 // try base64, see above
81 return readInternal(QByteArray::fromBase64(data), versionOverride);
82}
ERA SSB ticket barcode (version 1).
Definition ssbv1ticket.h:21
static bool maybeSSB(const QByteArray &data)
Returns true if data might be an ERA SSB ticket.
ERA SSB ticket barcode (version 2).
Definition ssbv2ticket.h:21
static bool maybeSSB(const QByteArray &data)
Returns true if data might be an ERA SSB ticket.
ERA SSB ticket barcode (version 3).
Definition ssbv3ticket.h:20
static bool maybeSSB(const QByteArray &data)
Returns true if data might be an ERA SSB ticket.
QVariant read(const QByteArray &data, int versionOverride=0)
Attempt to read an SSB ticket.
bool maybeSSB(const QByteArray &data)
Returns whether data could be an ERA SSB ticket.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QByteArray fromBase64(const QByteArray &base64, Base64Options options)
bool isEmpty() const const
qsizetype size() const const
const_iterator begin() const const
const_iterator end() const const
QVariant fromValue(T &&value)
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.