Commit 47a28576 authored by Volker Krause's avatar Volker Krause
Browse files

Move the rest of the base64 and string decoding functions to ByteArray

Also makes all of them work with JS ArrayBuffer objects.
parent e1786ed9
......@@ -82,25 +82,9 @@ QVariant JsApi::Barcode::decodeEraSsbTicket(const QVariant &s, int versionOverri
return SSBTicketReader::read(s.toByteArray(), versionOverride);
}
QString JsApi::Barcode::toBase64(const QVariant &b) const
{
return QString::fromUtf8(b.toByteArray().toBase64());
}
QVariant JsApi::Barcode::fromBase64(const QString &s) const
{
return QByteArray::fromBase64(s.toUtf8());
}
QVariant JsApi::Barcode::toBitArray(const QVariant &b) const
{
return QVariant::fromValue(JsApi::BitArray(b.toByteArray()));
}
QString JsApi::Barcode::byteArrayToString(const QVariant &b) const
{
const auto ba = b.toByteArray();
return QString::fromLatin1(ba.constData(), ba.size());
}
#include "moc_barcode.cpp"
......@@ -58,16 +58,8 @@ public:
*/
Q_INVOKABLE QVariant decodeEraSsbTicket(const QVariant &s, int versionOverride = 0) const;
/** Converts the given QByteArray into an base64 encoded string. */
Q_INVOKABLE QString toBase64(const QVariant &b) const;
/** Converts a given Base64 encoded string to a QByteArray. */
Q_INVOKABLE QVariant fromBase64(const QString &s) const;
/** Converts the given QByteArray into a BitArray. */
Q_INVOKABLE QVariant toBitArray(const QVariant &b) const;
/** Converts the given QByteArray into a string for processing in JS.
* This only works if there isn't "too much" binary content in the byte array.
*/
Q_INVOKABLE QString byteArrayToString(const QVariant &b) const;
///@cond internal
void setDecoder(BarcodeDecoder *decoder);
......
......@@ -44,11 +44,26 @@ QJSValue JsApi::ByteArray::inflate(const QByteArray &input) const
return toArrayBuffer(output);
}
QString JsApi::ByteArray::toBase64(const QByteArray &input) const
{
return QString::fromUtf8(input.toBase64());
}
QJSValue JsApi::ByteArray::fromBase64(const QString &b64) const
{
return toArrayBuffer(QByteArray::fromBase64(b64.toUtf8()));
}
QString JsApi::ByteArray::decodeUtf8(const QByteArray &input) const
{
return QString::fromUtf8(input);
}
QString JsApi::ByteArray::decodeLatin1(const QByteArray &input) const
{
return QString::fromLatin1(input.constData(), input.size());
}
QJSValue JsApi::ByteArray::toArrayBuffer(const QByteArray &input) const
{
const auto engine = qjsEngine(this);
......
......@@ -25,8 +25,15 @@ public:
*/
Q_INVOKABLE QJSValue inflate(const QByteArray &input) const;
/** Converts the given QByteArray or JS ArrayBuffer into an base64 encoded string. */
Q_INVOKABLE QString toBase64(const QByteArray &input) const;
/** Converts a given Base64 encoded string to a JS ArrayBuffer. */
Q_INVOKABLE QJSValue fromBase64(const QString &b64) const;
/** Convert a QByteArray/ArrayBuffer to a string, assuming UTF-8 encoding. */
Q_INVOKABLE QString decodeUtf8(const QByteArray &input) const;
/** Convert a QByteArray/ArrayBuffer to a string, assuming Latin1 encoding. */
Q_INVOKABLE QString decodeLatin1(const QByteArray &input) const;
/** Convert a QByteArray to a JS ArrayBuffer.
* This is mainly a migration aid until we return ArrayBuffers everywhere.
......
......@@ -230,7 +230,7 @@ function parsePdf(pdf, node, triggerNode) {
var reservations = parseTicket(page.text, uic918ticket);
for (var i = 0; i < reservations.length; ++i) {
reservations[i].reservedTicket.ticketToken = "aztecbin:" + Barcode.toBase64(triggerNode.content.rawData);
reservations[i].reservedTicket.ticketToken = "aztecbin:" + ByteArray.toBase64(triggerNode.content.rawData);
if (triggerNode.mimeType == "internal/uic9183") {
reservations[i].reservationFor.provider.identifier = "uic:" + triggerNode.content.carrierId;
const block = triggerNode.content.block("0080BL").findSubBlock("009")
......
......@@ -20,7 +20,7 @@ function parseTicket(pdf, node, triggerNode) {
res.reservationFor.arrivalTime = JsonLd.toDateTime(trip[1] + trip[5], "yyyy.MM.ddhh:mm", "hu");
res.reservationFor.trainNumber = trip[6];
res.reservedTicket.ticketedSeat.seatingType = trip[7];
res.reservedTicket.ticketToken = "pdf417bin:" + Barcode.toBase64(triggerNode.content);
res.reservedTicket.ticketToken = "pdf417bin:" + ByteArray.toBase64(triggerNode.content);
// see https://community.kde.org/KDE_PIM/KItinerary/MAV_Barcode
const outer = ByteArray.toArrayBuffer(triggerNode.content);
......
......@@ -234,7 +234,7 @@ function parseSecutixPdf(pdf, node, triggerNode)
{
// see https://community.kde.org/KDE_PIM/KItinerary/SNCF_Barcodes#SNCF_Secutix_Tickets
var res = JsonLd.newTrainReservation();
var code = Barcode.byteArrayToString(triggerNode.content);
var code = ByteArray.decodeLatin1(triggerNode.content);
res.reservationNumber = code.substr(268, 9);
res.reservationFor.departureStation.name = code.substr(277, 5);
res.reservationFor.departureStation.identifier = "sncf:" + code.substr(277, 5);
......@@ -242,7 +242,7 @@ function parseSecutixPdf(pdf, node, triggerNode)
res.reservationFor.arrivalStation.identifier = "sncf:" + code.substr(282, 5);
res.reservationFor.departureDay = JsonLd.toDateTime(code.substr(343, 8), "ddMMyyyy", "fr");
res.reservedTicket.ticketedSeat.seatingType = code.substr(351, 1);
res.reservedTicket.ticketToken = "aztecbin:" + Barcode.toBase64(triggerNode.content);
res.reservedTicket.ticketToken = "aztecbin:" + ByteArray.toBase64(triggerNode.content);
res.underName.familyName = code.substr(376, 19);
res.underName.givenName = code.substr(395, 19);
......
......@@ -20,14 +20,14 @@ function parseSsbTicket(ssb, node) {
res.reservedTicket.ticketedSeat.seatSection = ssb.type1CoachNumber;
res.reservedTicket.ticketedSeat.seatNumber = ssb.type1SeatNumber;
res.reservationNumber = ssb.tcn;
res.reservedTicket.ticketToken = "aztecbin:" + Barcode.toBase64(ssb.rawData);
res.reservedTicket.ticketToken = "aztecbin:" + ByteArray.toBase64(ssb.rawData);
return res;
}
function parseReservation(html, node) {
var tokenElem = html.eval('//table[@class="qrcode"]//img')[0];
var token = tokenElem.attribute("src").match(/barcode\/tAZTEC\/.*\/nBinary\/v(.*)\/barcode.gif/);
var res = parseSsbTicket(Barcode.decodeEraSsbTicket(Barcode.fromBase64(token[1])), node);
var res = parseSsbTicket(Barcode.decodeEraSsbTicket(ByteArray.fromBase64(token[1])), node);
var subtitle = html.eval('//table[@class="subtitle"]');
var ref = subtitle[0].recursiveContent.match(/(\d{2}.\d{2}.\d{4})[\s\S]*([A-Z0-9]{6})/);
......
......@@ -37,7 +37,7 @@ function parsePdf(pdf) {
var offset = 0;
for (var j = 0; j < images.length; ++j) {
var barcode = Barcode.decodeAztecBinary(images[j]);
var barcodeB64 = Barcode.toBase64(barcode);
var barcodeB64 = ByteArray.toBase64(barcode);
if (!barcodeB64)
continue;
......
......@@ -7,7 +7,7 @@
function parseTicket(ticket) {
var res = JsonLd.newTrainReservation();
res.reservationNumber = ticket.pnr;
res.reservedTicket.ticketToken = "aztectBin:" + Barcode.toBase64(ticket.rawData);
res.reservedTicket.ticketToken = "aztectBin:" + ByteArray.toBase64(ticket.rawData);
res.underName.name = ticket.ticketLayout.text(0, 0, 72, 1);
res.reservationFor.departureTime = JsonLd.toDateTime(ticket.ticketLayout.text(3, 0, 72, 1).match(/([\d\.: ]+)/)[1], "dd.MM.yyyy hh:mm", "de");
res.reservationFor.departureStation.name = ticket.ticketLayout.text(7, 0, 72, 1);
......
......@@ -26,7 +26,7 @@ function parseSsbBarcode(ssb, node)
res.reservationFor.arrivalStation.identifier = "vrfi:" + ssb.arrivalStationAlpha;
res.reservationFor.provider.identifier = "uic:" + ssb.issuerCode;
res.reservedTicket.ticketToken = "aztectbin:" + Barcode.toBase64(ssb.rawData);
res.reservedTicket.ticketToken = "aztectbin:" + ByteArray.toBase64(ssb.rawData);
res.reservedTicket.ticketedSeat.seatingType = ssb.classOfTransport;
if (ssb.coachNumber > 0) {
......
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