rippled
Loading...
Searching...
No Matches
libxrpl/basics/base64.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2018 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20//
21// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
22//
23// Distributed under the Boost Software License, Version 1.0. (See accompanying
24// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
25//
26
27/*
28 Portions from http://www.adp-gmbh.ch/cpp/common/base64.html
29 Copyright notice:
30
31 base64.cpp and base64.h
32
33 Copyright (C) 2004-2008 René Nyffenegger
34
35 This source code is provided 'as-is', without any express or implied
36 warranty. In no event will the author be held liable for any damages
37 arising from the use of this software.
38
39 Permission is granted to anyone to use this software for any purpose,
40 including commercial applications, and to alter it and redistribute it
41 freely, subject to the following restrictions:
42
43 1. The origin of this source code must not be misrepresented; you must not
44 claim that you wrote the original source code. If you use this source code
45 in a product, an acknowledgment in the product documentation would be
46 appreciated but is not required.
47
48 2. Altered source versions must be plainly marked as such, and must not be
49 misrepresented as being the original source code.
50
51 3. This notice may not be removed or altered from any source distribution.
52
53 René Nyffenegger rene.nyffenegger@adp-gmbh.ch
54
55*/
56
57#include <xrpl/basics/base64.h>
58
59#include <cstddef>
60#include <cstdint>
61#include <string>
62#include <string_view>
63#include <utility>
64
65namespace ripple {
66
67namespace base64 {
68
69inline char const*
71{
72 static char constexpr tab[] = {
73 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
74 return &tab[0];
75}
76
77inline signed char const*
79{
80 static signed char constexpr tab[] = {
81 -1, -1, -1, -1, -1, -1, -1, -1,
82 -1, -1, -1, -1, -1, -1, -1, -1, // 0-15
83 -1, -1, -1, -1, -1, -1, -1, -1,
84 -1, -1, -1, -1, -1, -1, -1, -1, // 16-31
85 -1, -1, -1, -1, -1, -1, -1, -1,
86 -1, -1, -1, 62, -1, -1, -1, 63, // 32-47
87 52, 53, 54, 55, 56, 57, 58, 59,
88 60, 61, -1, -1, -1, -1, -1, -1, // 48-63
89 -1, 0, 1, 2, 3, 4, 5, 6,
90 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
91 15, 16, 17, 18, 19, 20, 21, 22,
92 23, 24, 25, -1, -1, -1, -1, -1, // 80-95
93 -1, 26, 27, 28, 29, 30, 31, 32,
94 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
95 41, 42, 43, 44, 45, 46, 47, 48,
96 49, 50, 51, -1, -1, -1, -1, -1, // 112-127
97 -1, -1, -1, -1, -1, -1, -1, -1,
98 -1, -1, -1, -1, -1, -1, -1, -1, // 128-143
99 -1, -1, -1, -1, -1, -1, -1, -1,
100 -1, -1, -1, -1, -1, -1, -1, -1, // 144-159
101 -1, -1, -1, -1, -1, -1, -1, -1,
102 -1, -1, -1, -1, -1, -1, -1, -1, // 160-175
103 -1, -1, -1, -1, -1, -1, -1, -1,
104 -1, -1, -1, -1, -1, -1, -1, -1, // 176-191
105 -1, -1, -1, -1, -1, -1, -1, -1,
106 -1, -1, -1, -1, -1, -1, -1, -1, // 192-207
107 -1, -1, -1, -1, -1, -1, -1, -1,
108 -1, -1, -1, -1, -1, -1, -1, -1, // 208-223
109 -1, -1, -1, -1, -1, -1, -1, -1,
110 -1, -1, -1, -1, -1, -1, -1, -1, // 224-239
111 -1, -1, -1, -1, -1, -1, -1, -1,
112 -1, -1, -1, -1, -1, -1, -1, -1 // 240-255
113 };
114 return &tab[0];
115}
116
119{
120 return 4 * ((n + 2) / 3);
121}
122
125{
126 return ((n / 4) * 3) + 2;
127}
128
142encode(void* dest, void const* src, std::size_t len)
143{
144 char* out = static_cast<char*>(dest);
145 char const* in = static_cast<char const*>(src);
146 auto const tab = base64::get_alphabet();
147
148 for (auto n = len / 3; n--;)
149 {
150 *out++ = tab[(in[0] & 0xfc) >> 2];
151 *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)];
152 *out++ = tab[((in[2] & 0xc0) >> 6) + ((in[1] & 0x0f) << 2)];
153 *out++ = tab[in[2] & 0x3f];
154 in += 3;
155 }
156
157 switch (len % 3)
158 {
159 case 2:
160 *out++ = tab[(in[0] & 0xfc) >> 2];
161 *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)];
162 *out++ = tab[(in[1] & 0x0f) << 2];
163 *out++ = '=';
164 break;
165
166 case 1:
167 *out++ = tab[(in[0] & 0xfc) >> 2];
168 *out++ = tab[((in[0] & 0x03) << 4)];
169 *out++ = '=';
170 *out++ = '=';
171 break;
172
173 case 0:
174 break;
175 }
176
177 return out - static_cast<char*>(dest);
178}
179
192decode(void* dest, char const* src, std::size_t len)
193{
194 char* out = static_cast<char*>(dest);
195 auto in = reinterpret_cast<unsigned char const*>(src);
196 unsigned char c3[3]{}, c4[4]{};
197 int i = 0;
198 int j = 0;
199
200 auto const inverse = base64::get_inverse();
201
202 while (len-- && *in != '=')
203 {
204 auto const v = inverse[*in];
205 if (v == -1)
206 break;
207 ++in;
208 c4[i] = v;
209 if (++i == 4)
210 {
211 c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4);
212 c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
213 c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
214
215 for (i = 0; i < 3; i++)
216 *out++ = c3[i];
217 i = 0;
218 }
219 }
220
221 if (i)
222 {
223 c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4);
224 c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2);
225 c3[2] = ((c4[2] & 0x3) << 6) + c4[3];
226
227 for (j = 0; j < i - 1; j++)
228 *out++ = c3[j];
229 }
230
231 return {
232 out - static_cast<char*>(dest),
233 in - reinterpret_cast<unsigned char const*>(src)};
234}
235
236} // namespace base64
237
240{
241 std::string dest;
242 dest.resize(base64::encoded_size(len));
243 dest.resize(base64::encode(&dest[0], data, len));
244 return dest;
245}
246
249{
250 std::string dest;
251 dest.resize(base64::decoded_size(data.size()));
252 auto const result = base64::decode(&dest[0], data.data(), data.size());
253 dest.resize(result.first);
254 return dest;
255}
256
257} // namespace ripple
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.
signed char const * get_inverse()
char const * get_alphabet()
std::size_t encode(void *dest, void const *src, std::size_t len)
Encode a series of octets as a padded, base64 string.
std::size_t constexpr decoded_size(std::size_t n)
Returns max bytes needed to decode a base64 string.
std::size_t constexpr encoded_size(std::size_t n)
Returns max chars needed to encode a base64 string.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string base64_decode(std::string_view data)
std::string base64_encode(std::uint8_t const *data, std::size_t len)
T resize(T... args)