KCodecs

kcodecsbase45.cpp
1 
2 /*
3  SPDX-FileCopyrightText: 2021 Volker Krause <[email protected]>
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 
13 static constexpr const char base45Table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
14 
15 static 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 (int 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(const QByteArray &in)
Decodes the given data that was encoded using the base45 codec.
void push_back(char ch)
void reserve(int size)
int size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Dec 4 2022 04:03:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.