xrpld
Loading...
Searching...
No Matches
libxrpl/basics/base64.cpp
1// Distributed under the Boost Software License, Version 1.0. (See accompanying
2// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
3//
4
5/*
6 Portions from http://www.adp-gmbh.ch/cpp/common/base64.html
7 Copyright notice:
8
9 base64.cpp and base64.h
10
11 Copyright (C) 2004-2008 René Nyffenegger
12
13 This source code is provided 'as-is', without any express or implied
14 warranty. In no event will the author be held liable for any damages
15 arising from the use of this software.
16
17 Permission is granted to anyone to use this software for any purpose,
18 including commercial applications, and to alter it and redistribute it
19 freely, subject to the following restrictions:
20
21 1. The origin of this source code must not be misrepresented; you must not
22 claim that you wrote the original source code. If you use this source code
23 in a product, an acknowledgment in the product documentation would be
24 appreciated but is not required.
25
26 2. Altered source versions must be plainly marked as such, and must not be
27 misrepresented as being the original source code.
28
29 3. This notice may not be removed or altered from any source distribution.
30
31 René Nyffenegger rene.nyffenegger@adp-gmbh.ch
32
33*/
34
35#include <xrpl/basics/base64.h>
36
37#include <cstddef>
38#include <cstdint>
39#include <string>
40#include <string_view>
41#include <utility>
42
43namespace xrpl {
44
45namespace base64 {
46
47inline char const*
49{
50 static constexpr char kTab[] = {
51 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
52 return &kTab[0];
53}
54
55inline signed char const*
57{
58 static constexpr signed char kTab[] = {
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0-15
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 32-47
62 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 48-63
63 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
64 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 80-95
65 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
66 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 112-127
67 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128-143
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 144-159
69 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160-175
70 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 176-191
71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 192-207
72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 208-223
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224-239
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // 240-255
75 };
76 return &kTab[0];
77}
78
80constexpr std::size_t
82{
83 return 4 * ((n + 2) / 3);
84}
85
87constexpr std::size_t
89{
90 return ((n / 4) * 3) + 2;
91}
92
106encode(void* dest, void const* src, std::size_t len)
107{
108 char* out = static_cast<char*>(dest); // NOLINT(misc-const-correctness)
109 char const* in = static_cast<char const*>(src);
110 auto const tab = base64::getAlphabet();
111
112 for (auto n = len / 3; n > 0; --n)
113 {
114 *out++ = tab[(in[0] & 0xfc) >> 2];
115 *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)];
116 *out++ = tab[((in[2] & 0xc0) >> 6) + ((in[1] & 0x0f) << 2)];
117 *out++ = tab[in[2] & 0x3f];
118 in += 3;
119 }
120
121 // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
122 switch (len % 3)
123 {
124 case 2:
125 *out++ = tab[(in[0] & 0xfc) >> 2];
126 *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)];
127 *out++ = tab[(in[1] & 0x0f) << 2];
128 *out++ = '=';
129 break;
130
131 case 1:
132 *out++ = tab[(in[0] & 0xfc) >> 2];
133 *out++ = tab[((in[0] & 0x03) << 4)];
134 *out++ = '=';
135 *out++ = '=';
136 break;
137
138 case 0:
139 break;
140 }
141
142 return out - static_cast<char*>(dest);
143}
144
157decode(void* dest, char const* src, std::size_t len)
158{
159 char* out = static_cast<char*>(dest); // NOLINT(misc-const-correctness)
160 auto in = reinterpret_cast<unsigned char const*>(src);
161 unsigned char c3[3]{}, c4[4]{};
162 int i = 0;
163 int j = 0;
164
165 auto const inverse = base64::getInverse();
166
167 while (((len--) != 0u) && *in != '=')
168 {
169 auto const v = inverse[*in];
170 if (v == -1)
171 break;
172 ++in;
173 c4[i] = v;
174 if (++i; i == 4)
175 {
176 c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4);
177 c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
178 c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
179
180 for (i = 0; i < 3; i++)
181 *out++ = c3[i];
182 i = 0;
183 }
184 }
185
186 if (i != 0)
187 {
188 c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4);
189 c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
190 c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
191
192 for (j = 0; j < i - 1; j++)
193 *out++ = c3[j];
194 }
195
196 return {out - static_cast<char*>(dest), in - reinterpret_cast<unsigned char const*>(src)};
197}
198
199} // namespace base64
200
203{
204 std::string dest;
205 dest.resize(base64::encodedSize(len));
206 dest.resize(base64::encode(&dest[0], data, len));
207 return dest;
208}
209
212{
213 std::string dest;
214 dest.resize(base64::decodedSize(data.size()));
215 auto const result = base64::decode(&dest[0], data.data(), data.size());
216 dest.resize(result.first);
217 return dest;
218}
219
220} // namespace xrpl
constexpr std::size_t encodedSize(std::size_t n)
Returns max chars needed to encode a base64 string.
std::pair< std::size_t, std::size_t > decode(void *dest, char const *src, std::size_t len)
Decode a padded base64 string into a series of octets.
std::size_t encode(void *dest, void const *src, std::size_t len)
Encode a series of octets as a padded, base64 string.
signed char const * getInverse()
char const * getAlphabet()
constexpr std::size_t decodedSize(std::size_t n)
Returns max bytes needed to decode a base64 string.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string base64Decode(std::string_view data)
std::string base64Encode(std::uint8_t const *data, std::size_t len)
T resize(T... args)