KItinerary

bytearray.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 "bytearray.h"
8#include "bitarray.h"
9#include "logging.h"
10
11#include "protobuf/protobufstreamreader.h"
12#include "rsp/rsp6decoder.h"
13
14#include <QQmlEngine>
15
16#include <zlib.h>
17
18using namespace KItinerary;
19
21{
22 QByteArray output;
23 output.resize(4096);
24 z_stream stream;
25 stream.zalloc = nullptr;
26 stream.zfree = nullptr;
27 stream.opaque = nullptr;
28 stream.avail_in = input.size();
29 stream.next_in = reinterpret_cast<unsigned char*>(const_cast<char*>(input.data()));
30 stream.avail_out = output.size();
31 stream.next_out = reinterpret_cast<unsigned char*>(output.data());
32
33 inflateInit2(&stream, MAX_WBITS + 32);
34 const auto res = ::inflate(&stream, Z_NO_FLUSH);
35 switch (res) {
36 case Z_OK:
37 case Z_STREAM_END:
38 break; // all good
39 default:
40 qCWarning(Log) << "zlib decompression failed " << stream.msg << stream.avail_in;
41 return {};
42 }
43 inflateEnd(&stream);
44 output.truncate(output.size() - stream.avail_out);
45 return toArrayBuffer(output);
46}
47
49{
50 return QString::fromUtf8(input.toBase64());
51}
52
54{
55 return toArrayBuffer(QByteArray::fromBase64(b64.toUtf8()));
56}
57
59{
60 // explicitly truncate at the first null byte, Qt6 doesn't do that automatically anymore
61 const auto idx = input.indexOf('\0');
62 return QString::fromUtf8(input.constData(), idx >= 0 ? idx : input.size());
63}
64
66{
67 // explicitly truncate at the first null byte, Qt6 doesn't do that automatically anymore
68 const auto idx = input.indexOf('\0');
69 return QString::fromLatin1(input.constData(), idx >= 0 ? idx : input.size());
70}
71
76
81
83{
84 return toArrayBuffer(Rsp6Decoder::decode(text.toLatin1()));
85}
86
88{
89 const auto engine = qjsEngine(this);
90 return QJSValue(QJSManagedValue(QVariant(input), engine));
91}
92
93#include "moc_bytearray.cpp"
Bit array methods for JS extractor scripts.
Definition bitarray.h:17
Q_INVOKABLE QJSValue inflate(const QByteArray &input) const
Perform zlib decompression on the given byte array.
Definition bytearray.cpp:20
Q_INVOKABLE QJSValue fromBase64(const QString &b64) const
Converts a given Base64 encoded string to a JS ArrayBuffer.
Definition bytearray.cpp:53
Q_INVOKABLE QString decodeLatin1(const QByteArray &input) const
Convert a QByteArray/ArrayBuffer to a string, assuming Latin1 encoding.
Definition bytearray.cpp:65
Q_INVOKABLE QJSValue toArrayBuffer(const QByteArray &input) const
Convert a QByteArray to a JS ArrayBuffer.
Definition bytearray.cpp:87
Q_INVOKABLE QJSValue decodeRsp6Ticket(const QString &text) const
Decode/decrypt a UK RSP-6 ticket barcode.
Definition bytearray.cpp:82
Q_INVOKABLE QString toBase64(const QByteArray &input) const
Converts the given QByteArray or JS ArrayBuffer into an base64 encoded string.
Definition bytearray.cpp:48
Q_INVOKABLE QVariant toProtobufStreamReader(const QByteArray &input) const
Creates a Protocol Buffers stream reader for the given JS ArrayBuffer.
Definition bytearray.cpp:77
Q_INVOKABLE QString decodeUtf8(const QByteArray &input) const
Convert a QByteArray/ArrayBuffer to a string, assuming UTF-8 encoding.
Definition bytearray.cpp:58
Q_INVOKABLE QVariant toBitArray(const QByteArray &input) const
Converts the given QByteArray or JS ArrayBuffer into a BitArray.
Definition bytearray.cpp:72
Protocol Buffers stream reader.
static QByteArray decode(const QByteArray &data)
Decodes base26 transport encoding and decrypts the ticket payload.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
const char * constData() const const
char * data()
QByteArray fromBase64(const QByteArray &base64, Base64Options options)
qsizetype indexOf(QByteArrayView bv, qsizetype from) const const
void resize(qsizetype newSize, char c)
qsizetype size() const const
QByteArray toBase64(Base64Options options) const const
void truncate(qsizetype pos)
QString fromLatin1(QByteArrayView str)
QString fromUtf8(QByteArrayView str)
QByteArray toLatin1() const const
QByteArray toUtf8() 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.