-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathBase64.cpp
370 lines (303 loc) · 9.25 KB
/
Base64.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "graphqlservice/internal/Base64.h"
#include <array>
#include <stdexcept>
namespace graphql::internal {
std::uint8_t Base64::verifyFromBase64(char ch)
{
const std::uint8_t result = fromBase64(ch);
if ((result & 0xC0) != 0)
{
throw std::logic_error { "invalid character in base64 encoded string" };
}
return result;
}
std::vector<std::uint8_t> Base64::fromBase64(std::string_view encoded)
{
std::vector<std::uint8_t> result;
if (encoded.empty())
{
return result;
}
result.reserve((encoded.size() + (encoded.size() % 4)) * 3 / 4);
// First decode all of the full unpadded segments 24 bits at a time
while (encoded.size() >= 4 && encoded[3] != padding)
{
const uint32_t segment = (static_cast<uint32_t>(verifyFromBase64(encoded[0])) << 18)
| (static_cast<uint32_t>(verifyFromBase64(encoded[1])) << 12)
| (static_cast<uint32_t>(verifyFromBase64(encoded[2])) << 6)
| static_cast<uint32_t>(verifyFromBase64(encoded[3]));
result.emplace_back(static_cast<std::uint8_t>((segment & 0xFF0000) >> 16));
result.emplace_back(static_cast<std::uint8_t>((segment & 0xFF00) >> 8));
result.emplace_back(static_cast<std::uint8_t>(segment & 0xFF));
encoded = encoded.substr(4);
}
// Get any leftover partial segment with 2 or 3 non-padding characters
if (encoded.size() > 1)
{
const bool triplet = (encoded.size() > 2 && padding != encoded[2]);
const std::uint8_t tail = (triplet ? verifyFromBase64(encoded[2]) : 0);
const uint16_t segment = (static_cast<uint16_t>(verifyFromBase64(encoded[0])) << 10)
| (static_cast<uint16_t>(verifyFromBase64(encoded[1])) << 4)
| (static_cast<uint16_t>(tail) >> 2);
if (triplet)
{
if (tail & 0x3)
{
throw std::logic_error { "invalid padding at the end of a base64 encoded string" };
}
result.emplace_back(static_cast<std::uint8_t>((segment & 0xFF00) >> 8));
result.emplace_back(static_cast<std::uint8_t>(segment & 0xFF));
encoded = encoded.substr(3);
}
else
{
if (segment & 0xFF)
{
throw std::logic_error { "invalid padding at the end of a base64 encoded string" };
}
result.emplace_back(static_cast<std::uint8_t>((segment & 0xFF00) >> 8));
encoded = encoded.substr(2);
}
}
// Make sure anything that's left is 0 - 2 characters of padding
if ((encoded.size() > 0 && padding != encoded[0])
|| (encoded.size() > 1 && padding != encoded[1]) || encoded.size() > 2)
{
throw std::logic_error { "invalid padding at the end of a base64 encoded string" };
}
return result;
}
char Base64::verifyToBase64(std::uint8_t i)
{
unsigned char result = toBase64(i);
if (result == padding)
{
throw std::logic_error { "invalid 6-bit value" };
}
return result;
}
std::string Base64::toBase64(const std::vector<std::uint8_t>& bytes)
{
std::string result;
if (bytes.empty())
{
return result;
}
size_t count = bytes.size();
const std::uint8_t* data = bytes.data();
result.reserve((count + (count % 3)) * 4 / 3);
// First encode all of the full unpadded segments 24 bits at a time
while (count >= 3)
{
const uint32_t segment = (static_cast<uint32_t>(data[0]) << 16)
| (static_cast<uint32_t>(data[1]) << 8) | static_cast<uint32_t>(data[2]);
result.append({ verifyToBase64(static_cast<std::uint8_t>((segment & 0xFC0000) >> 18)),
verifyToBase64(static_cast<std::uint8_t>((segment & 0x3F000) >> 12)),
verifyToBase64(static_cast<std::uint8_t>((segment & 0xFC0) >> 6)),
verifyToBase64(static_cast<std::uint8_t>(segment & 0x3F)) });
data += 3;
count -= 3;
}
// Get any leftover partial segment with 1 or 2 bytes
if (count > 0)
{
const bool pair = (count > 1);
const uint16_t segment =
(static_cast<uint16_t>(data[0]) << 8) | (pair ? static_cast<uint16_t>(data[1]) : 0);
const std::array<char, 4> remainder { verifyToBase64(static_cast<std::uint8_t>(
(segment & 0xFC00) >> 10)),
verifyToBase64(static_cast<std::uint8_t>((segment & 0x3F0) >> 4)),
(pair ? verifyToBase64(static_cast<std::uint8_t>((segment & 0xF) << 2)) : padding),
padding };
result.append(remainder.data(), remainder.size());
}
return result;
}
Base64::Comparison Base64::compareBase64(
const std::vector<std::uint8_t>& bytes, std::string_view maybeEncoded) noexcept
{
if (bytes.empty())
{
if (maybeEncoded.empty())
{
return Comparison::EqualTo;
}
}
else if (maybeEncoded.empty())
{
return Comparison::GreaterThan;
}
auto result = Comparison::EqualTo;
auto itr = bytes.cbegin();
const auto itrEnd = bytes.cend();
// First decode and compare all of the full unpadded segments 24 bits at a time
while (maybeEncoded.size() >= 4 && maybeEncoded[3] != padding)
{
const auto a = fromBase64(maybeEncoded[0]);
const auto b = fromBase64(maybeEncoded[1]);
const auto c = fromBase64(maybeEncoded[2]);
const auto d = fromBase64(maybeEncoded[3]);
if (((a | b | c | d) & 0xC0) != 0)
{
// Invalid Base64 characters
return Comparison::InvalidBase64;
}
if (Comparison::EqualTo == result)
{
const uint32_t segment = (static_cast<uint32_t>(a) << 18)
| (static_cast<uint32_t>(b) << 12) | (static_cast<uint32_t>(c) << 6)
| static_cast<uint32_t>(d);
const std::array decoded { static_cast<std::uint8_t>((segment & 0xFF0000) >> 16),
static_cast<std::uint8_t>((segment & 0xFF00) >> 8),
static_cast<std::uint8_t>(segment & 0xFF) };
for (auto value : decoded)
{
if (itr == itrEnd)
{
result = Comparison::LessThan;
break;
}
if (*itr != value)
{
result = *itr < value ? Comparison::LessThan : Comparison::GreaterThan;
break;
}
++itr;
}
}
maybeEncoded = maybeEncoded.substr(4);
}
// Compare any leftover partial segment with 2 or 3 non-padding characters
if (maybeEncoded.size() > 1)
{
const bool triplet = (maybeEncoded.size() > 2 && padding != maybeEncoded[2]);
const auto a = fromBase64(maybeEncoded[0]);
const auto b = fromBase64(maybeEncoded[1]);
const auto c = triplet ? fromBase64(maybeEncoded[2]) : std::uint8_t {};
if (((a | b | c) & 0xC0) != 0 || (c & 0x3) != 0)
{
// Invalid Base64 characters or padding
return Comparison::InvalidBase64;
}
const uint16_t segment = (static_cast<uint16_t>(a) << 10) | (static_cast<uint16_t>(b) << 4)
| (static_cast<uint16_t>(c) >> 2);
const std::array decoded { static_cast<std::uint8_t>((segment & 0xFF00) >> 8),
static_cast<std::uint8_t>(segment & 0xFF) };
if (triplet)
{
if (Comparison::EqualTo == result)
{
for (auto value : decoded)
{
if (itr == itrEnd)
{
result = Comparison::LessThan;
break;
}
if (*itr != value)
{
result = *itr < value ? Comparison::LessThan : Comparison::GreaterThan;
break;
}
++itr;
}
}
maybeEncoded = maybeEncoded.substr(3);
}
else
{
if (decoded[1] != 0)
{
// Invalid padding
return Comparison::InvalidBase64;
}
if (Comparison::EqualTo == result)
{
if (itr == itrEnd)
{
result = Comparison::LessThan;
}
else if (*itr != decoded[0])
{
result = *itr < decoded[0] ? Comparison::LessThan : Comparison::GreaterThan;
}
++itr;
}
maybeEncoded = maybeEncoded.substr(2);
}
}
// Make sure anything that's left is 0 - 2 characters of padding
if ((maybeEncoded.size() > 0 && padding != maybeEncoded[0])
|| (maybeEncoded.size() > 1 && padding != maybeEncoded[1]) || maybeEncoded.size() > 2)
{
return Comparison::InvalidBase64;
}
if (Comparison::EqualTo == result)
{
// We should reach the end of the byte vector
if (itr != itrEnd)
{
result = Comparison::GreaterThan;
}
}
return result;
}
bool Base64::validateBase64(std::string_view maybeEncoded) noexcept
{
if (maybeEncoded.empty())
{
return true;
}
// First decode and compare all of the full unpadded segments 24 bits at a time
while (maybeEncoded.size() >= 4 && maybeEncoded[3] != padding)
{
const auto a = fromBase64(maybeEncoded[0]);
const auto b = fromBase64(maybeEncoded[1]);
const auto c = fromBase64(maybeEncoded[2]);
const auto d = fromBase64(maybeEncoded[3]);
if (((a | b | c | d) & 0xC0) != 0)
{
// Invalid Base64 characters
return false;
}
maybeEncoded = maybeEncoded.substr(4);
}
// Compare any leftover partial segment with 2 or 3 non-padding characters
if (maybeEncoded.size() > 1)
{
const bool triplet = (maybeEncoded.size() > 2 && padding != maybeEncoded[2]);
const auto a = fromBase64(maybeEncoded[0]);
const auto b = fromBase64(maybeEncoded[1]);
const auto c = triplet ? fromBase64(maybeEncoded[2]) : std::uint8_t {};
if (((a | b | c) & 0xC0) != 0 || (c & 0x3) != 0)
{
// Invalid Base64 characters or padding
return false;
}
if (triplet)
{
maybeEncoded = maybeEncoded.substr(3);
}
else
{
const uint16_t segment = (static_cast<uint16_t>(a) << 10)
| (static_cast<uint16_t>(b) << 4) | (static_cast<uint16_t>(c) >> 2);
if ((segment & 0xFF) != 0)
{
// Invalid padding
return false;
}
maybeEncoded = maybeEncoded.substr(2);
}
}
// Make sure anything that's left is 0 - 2 characters of padding
if ((maybeEncoded.size() > 0 && padding != maybeEncoded[0])
|| (maybeEncoded.size() > 1 && padding != maybeEncoded[1]) || maybeEncoded.size() > 2)
{
return false;
}
return true;
}
} // namespace graphql::internal