Repository navigation
Expand file tree
/
Copy pathbase64.cpp
More file actions
252 lines (231 loc) · 6.85 KB
/
Copy pathbase64.cpp
File metadata and controls
252 lines (231 loc) · 6.85 KB
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
/*
* Copyright (C) 2026 Shitty team
* MIT licensed
* See the file LICENSE.MIT for the full license.
*/
#include "base64.h"
#include <std/lib/buffer.h>
#include <std/str/view.h>
#if __has_include(<simdutf.h>)
#include <simdutf.h>
#define SHITTY_BASE64_SIMDUTF 1
#else
#define SHITTY_BASE64_SIMDUTF 0
#endif
using namespace stl;
namespace {
u8 decodeValue(u8 byte) noexcept {
if (byte >= u8'A' && byte <= u8'Z') {
return byte - u8'A';
}
if (byte >= u8'a' && byte <= u8'z') {
return byte - u8'a' + 26;
}
if (byte >= u8'0' && byte <= u8'9') {
return byte - u8'0' + 52;
}
if (byte == u8'+') {
return 62;
}
if (byte == u8'/') {
return 63;
}
return (u8)0xff;
}
#if SHITTY_BASE64_SIMDUTF
bool containsSpace(StringView input) noexcept {
for (const u8 byte : input) {
if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r' || byte == '\f') {
return true;
}
}
return false;
}
#else
constexpr u8 alphabet[] = u8"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#endif
}
void Base64Decoder::reset() noexcept {
count = 0;
padding = false;
complete = false;
valid = true;
}
bool Base64Decoder::push(u8 byte, Buffer& output) {
if (!valid || complete) {
valid = false;
return false;
}
if (byte == '=') {
if (count == 2 && !padding) {
padding = true;
count = 3;
return true;
}
if (count != 3) {
valid = false;
return false;
}
if (padding) {
if ((values[1] & 0x0f) != 0) {
valid = false;
return false;
}
const u8 decoded = (values[0] << 2) | (values[1] >> 4);
output.append(&decoded, 1);
} else {
if ((values[2] & 0x03) != 0) {
valid = false;
return false;
}
const u8 decoded[] = {
(u8)((values[0] << 2) | (values[1] >> 4)),
(u8)((values[1] << 4) | (values[2] >> 2)),
};
output.append(decoded, sizeof(decoded));
}
count = 0;
padding = false;
complete = true;
return true;
}
if (padding) {
valid = false;
return false;
}
const u8 value = decodeValue(byte);
if (value == (u8)0xff) {
valid = false;
return false;
}
values[count++] = value;
if (count == 3) {
return true;
}
if (count != 4) {
return true;
}
const u8 decoded[] = {
(u8)((values[0] << 2) | (values[1] >> 4)),
(u8)((values[1] << 4) | (values[2] >> 2)),
(u8)((values[2] << 6) | value),
};
output.append(decoded, sizeof(decoded));
count = 0;
return true;
}
bool Base64Decoder::finish(Buffer& output) {
if (!valid || padding) {
valid = false;
return false;
}
if (complete || count == 0) {
return valid;
}
if (count == 1) {
valid = false;
return false;
}
if (count == 2) {
if ((values[1] & 0x0f) != 0) {
valid = false;
return false;
}
const u8 decoded = (values[0] << 2) | (values[1] >> 4);
output.append(&decoded, 1);
} else {
if ((values[2] & 0x03) != 0) {
valid = false;
return false;
}
const u8 decoded[] = {
(u8)((values[0] << 2) | (values[1] >> 4)),
(u8)((values[1] << 4) | (values[2] >> 2)),
};
output.append(decoded, sizeof(decoded));
}
count = 0;
complete = true;
return true;
}
Buffer& base64Encode(StringView input, Buffer& output) {
#if SHITTY_BASE64_SIMDUTF
output.reset();
output.grow(simdutf::base64_length_from_binary(input.length()));
const size_t size = simdutf::binary_to_base64((const char*)(input.data()), input.length(), (char*)(output.mutData()));
output.seekAbsolute(size);
return output;
#else
output.reset();
const size_t outputSize = input.length() / 3 * 4 + (input.length() % 3 == 0 ? 0 : 4);
output.grow(outputSize);
auto* encoded = (u8*)output.mutData();
size_t source = 0;
size_t target = 0;
while (input.length() - source >= 3) {
const u32 bits = ((u32)input[source] << 16) | ((u32)input[source + 1] << 8) | (u32)input[source + 2];
encoded[target++] = alphabet[(bits >> 18) & 0x3f];
encoded[target++] = alphabet[(bits >> 12) & 0x3f];
encoded[target++] = alphabet[(bits >> 6) & 0x3f];
encoded[target++] = alphabet[bits & 0x3f];
source += 3;
}
const size_t remainder = input.length() - source;
if (remainder != 0) {
u32 bits = (u32)input[source] << 16;
if (remainder == 2) {
bits |= (u32)input[source + 1] << 8;
}
encoded[target++] = alphabet[(bits >> 18) & 0x3f];
encoded[target++] = alphabet[(bits >> 12) & 0x3f];
encoded[target++] = remainder == 2 ? alphabet[(bits >> 6) & 0x3f] : u8'=';
encoded[target++] = u8'=';
}
output.seekAbsolute(target);
return output;
#endif
}
Buffer& base64Decode(StringView input, Buffer& output, bool& valid) {
#if SHITTY_BASE64_SIMDUTF
output.reset();
valid = false;
if (containsSpace(input)) {
return output;
}
output.grow(simdutf::maximal_binary_length_from_base64((const char*)(input.data()), input.length()));
const size_t remainder = input.length() % 4;
const simdutf::last_chunk_handling_options handling = remainder == 2 || remainder == 3 ? simdutf::loose : simdutf::strict;
const simdutf::result result = simdutf::base64_to_binary((const char*)(input.data()), input.length(), (char*)(output.mutData()), simdutf::base64_default, handling);
if (result.error != simdutf::SUCCESS) {
return output;
}
if (handling == simdutf::loose) {
char tail[4];
for (size_t index = 0; index < remainder; ++index) {
tail[index] = (char)(input[input.length() - remainder + index]);
}
for (size_t index = remainder; index < sizeof(tail); ++index) {
tail[index] = '=';
}
char decodedTail[3];
const simdutf::result tailResult = simdutf::base64_to_binary(tail, sizeof(tail), decodedTail, simdutf::base64_default, simdutf::strict);
if (tailResult.error != simdutf::SUCCESS) {
return output;
}
}
output.seekAbsolute(result.count);
valid = true;
return output;
#else
output.reset();
Base64Decoder decoder;
for (const u8 byte : input) {
decoder.push(byte, output);
}
valid = decoder.finish(output);
if (!valid) {
output.reset();
}
return output;
#endif
}