KCodecs

kcodecsbase45.cpp
1
2/*
3 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kcodecs.h"
9#include "kcodecs_debug.h"
10
11#include <QDebug>
12
13static constexpr const char base45Table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
14
15static uint8_t base45MapFromChar(char c)
16{
17 const auto it = std::find(std::begin(base45Table), std::end(base45Table), c);
18 if (it == std::end(base45Table)) {
19 qCWarning(KCODECS_LOG) << "invalid base45 character:" << c;
20 return 0;
21 }
22 return std::distance(std::begin(base45Table), it);
23}
24
26{
27 QByteArray out;
28 out.reserve(((in.size() / 3) + 1) * 2);
29
30 for (qsizetype i = 0; i + 1 < in.size(); i += 3) {
31 uint32_t n = base45MapFromChar(in[i]) + base45MapFromChar(in[i + 1]) * 45;
32 if (i + 2 < in.size()) {
33 n += 45 * 45 * base45MapFromChar(in[i + 2]);
34 out.push_back(n >> 8);
35 } else {
36 if (n >> 8) {
37 out.push_back(n >> 8);
38 }
39 }
40 out.push_back(n % 256);
41 }
42
43 return out;
44}
KCODECS_EXPORT QByteArray base45Decode(QByteArrayView in)
Decodes the given data that was encoded using the base45 codec.
void push_back(QByteArrayView str)
void reserve(qsizetype size)
qsizetype size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.