File indexing completed on 2023-09-24 07:59:11
0001 0002 /* 0003 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "kcodecs.h" 0009 #include "kcodecs_debug.h" 0010 0011 #include <QDebug> 0012 0013 static constexpr const char base45Table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; 0014 0015 static uint8_t base45MapFromChar(char c) 0016 { 0017 const auto it = std::find(std::begin(base45Table), std::end(base45Table), c); 0018 if (it == std::end(base45Table)) { 0019 qCWarning(KCODECS_LOG) << "invalid base45 character:" << c; 0020 return 0; 0021 } 0022 return std::distance(std::begin(base45Table), it); 0023 } 0024 0025 QByteArray KCodecs::base45Decode(const QByteArray &in) 0026 { 0027 QByteArray out; 0028 out.reserve(((in.size() / 3) + 1) * 2); 0029 0030 for (int i = 0; i + 1 < in.size(); i += 3) { 0031 uint32_t n = base45MapFromChar(in[i]) + base45MapFromChar(in[i + 1]) * 45; 0032 if (i + 2 < in.size()) { 0033 n += 45 * 45 * base45MapFromChar(in[i + 2]); 0034 out.push_back(n >> 8); 0035 } else { 0036 if (n >> 8) { 0037 out.push_back(n >> 8); 0038 } 0039 } 0040 out.push_back(n % 256); 0041 } 0042 0043 return out; 0044 }