Debian Codegraph @

src/Base/Base64.cpp

raw
1 // SPDX-License-Identifier: Zlib
2
3 /*
4 base64.cpp and base64.h
5
6 Copyright (C) 2004-2008 René Nyffenegger
7
8 This source code is provided 'as-is', without any express or implied
9 warranty. In no event will the author be held liable for any damages
10 arising from the use of this software.
11
12 Permission is granted to anyone to use this software for any purpose,
13 including commercial applications, and to alter it and redistribute it
14 freely, subject to the following restrictions:
15
16 1. The origin of this source code must not be misrepresented; you must not
17 claim that you wrote the original source code. If you use this source code
18 in a product, an acknowledgment in the product documentation would be
19 appreciated but is not required.
20
21 2. Altered source versions must be plainly marked as such, and must not be
22 misrepresented as being the original source code.
23
24 3. This notice may not be removed or altered from any source distribution.
25
26 RenĂ© Nyffenegger [email protected]
27
28 NOTICE: The source code here has been altered from the original to use a provided character buffer
29 rather than returning a new string for each call.
30 These modifications are Copyright (c) 2019 Zheng Lei ([email protected])
31
32 */
33
34 #include <array>
35
36 #include "Base64.h"
37
38 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic,
39 // cppcoreguidelines-pro-bounds-constant-array-index, cppcoreguidelines-avoid-magic-numbers,
40 // readability-magic-numbers)
41
42 static const std::array<char, 65> base64_chars {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
43 "abcdefghijklmnopqrstuvwxyz"
44 "0123456789+/"};
45
46
47 std::array<const signed char, Base::base64DecodeTableSize> Base::base64_decode_table()
48 {
49 static std::array<const signed char, Base::base64DecodeTableSize> _table = {
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -2, -1, -2, -1, -1, // 0-15
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31
52 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 32-47
53 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, // 48-63
54 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
55 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 80-95
56 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
57 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 112-127
58 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128-143
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 144-159
60 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160-175
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 176-191
62 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 192-207
63 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 208-223
64 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224-239
65 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // 240-255
66 };
67 return _table;
68 }
69
70 std::size_t Base::base64_encode(char* out, void const* in, std::size_t in_len)
71 {
72 char* ret = out;
73 auto const* bytes_to_encode = reinterpret_cast<unsigned char const*>(in); // NOLINT
74 int char3 {0};
75 int char4 {};
76 std::array<unsigned char, 3> char_array_3 {};
77 std::array<unsigned char, 4> char_array_4 {};
78
79 while ((in_len--) != 0U) {
80 char_array_3[char3++] = *(bytes_to_encode++);
81 if (char3 == 3) {
82 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
83 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
84 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
85 char_array_4[3] = char_array_3[2] & 0x3f;
86
87 for (char3 = 0; (char3 < 4); char3++) {
88 *ret++ = base64_chars[char_array_4[char3]];
89 }
90 char3 = 0;
91 }
92 }
93
94 if (char3 != 0) {
95 for (char4 = char3; char4 < 3; char4++) {
96 char_array_3[char4] = '\0';
97 }
98
99 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
100 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
101 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
102 char_array_4[3] = char_array_3[2] & 0x3f;
103
104 for (char4 = 0; (char4 < char3 + 1); char4++) {
105 *ret++ = base64_chars[char_array_4[char4]];
106 }
107
108 while ((char3++ < 3)) {
109 *ret++ = '=';
110 }
111 }
112
113 return ret - out;
114 }
115
116 std::pair<std::size_t, std::size_t> Base::base64_decode(void* _out, char const* in, std::size_t in_len)
117 {
118 auto* out = reinterpret_cast<unsigned char*>(_out); // NOLINT
119 unsigned char* ret = out;
120 char const* input = in;
121 int byteCounter1 {0};
122 int byteCounter2 {};
123 std::array<unsigned char, 4> char_array_4 {};
124 std::array<unsigned char, 3> char_array_3 {};
125
126 static auto table = base64_decode_table();
127
128 while (((in_len--) != 0U) && *in != '=') {
129 const signed char lookup = table[static_cast<unsigned char>(*in++)];
130 if (lookup < 0) {
131 break;
132 }
133
134 char_array_4[byteCounter1++] = (unsigned char)lookup;
135 if (byteCounter1 == 4) {
136 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
137 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
138 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
139
140 for (byteCounter1 = 0; (byteCounter1 < 3); byteCounter1++) {
141 *ret++ = char_array_3[byteCounter1];
142 }
143 byteCounter1 = 0;
144 }
145 }
146
147 if (byteCounter1 != 0) {
148 for (byteCounter2 = byteCounter1; byteCounter2 < 4; byteCounter2++) {
149 char_array_4[byteCounter2] = 0;
150 }
151
152 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
153 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
154 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
155
156 for (byteCounter2 = 0; (byteCounter2 < byteCounter1 - 1); byteCounter2++) {
157 *ret++ = char_array_3[byteCounter2];
158 }
159 }
160
161 return std::make_pair((std::size_t)(ret - out), (std::size_t)(in - input));
162 }
163
164 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic,
165 // cppcoreguidelines-pro-bounds-constant-array-index, cppcoreguidelines-avoid-magic-numbers,
166 // readability-magic-numbers)