/work/include/simdutf/scalar/base64.h
Line | Count | Source |
1 | | #ifndef SIMDUTF_BASE64_H |
2 | | #define SIMDUTF_BASE64_H |
3 | | |
4 | | #include <cstddef> |
5 | | #include <cstdint> |
6 | | #include <array> |
7 | | #include <cstring> |
8 | | #include <type_traits> |
9 | | |
10 | | namespace simdutf { |
11 | | namespace scalar { |
12 | | namespace { |
13 | | namespace base64 { |
14 | | |
15 | | simdutf_really_inline simdutf_constexpr23 void |
16 | 0 | copy_encode_pair(char *dst, const std::array<char, 2> &pair) { |
17 | 0 | #if SIMDUTF_CPLUSPLUS23 |
18 | 0 | if (std::is_constant_evaluated()) { |
19 | 0 | dst[0] = pair[0]; |
20 | 0 | dst[1] = pair[1]; |
21 | 0 | return; |
22 | 0 | } |
23 | 0 | #endif |
24 | 0 | std::memcpy(dst, pair.data(), pair.size()); |
25 | 0 | } |
26 | | |
27 | | // This function is not expected to be fast. Do not use in long loops. |
28 | | // In most instances you should be using is_ignorable. |
29 | | template <class char_type> bool is_ascii_white_space(char_type c) { |
30 | | return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f'; |
31 | | } |
32 | | |
33 | 0 | template <class char_type> simdutf_constexpr23 bool is_eight_byte(char_type c) { |
34 | 0 | if constexpr (sizeof(char_type) == 1) { |
35 | 0 | return true; |
36 | 0 | } |
37 | 0 | return uint8_t(c) == c; |
38 | 0 | } Unexecuted instantiation: with_replacement.cpp:bool simdutf::scalar::(anonymous namespace)::base64::is_eight_byte<char>(char) Unexecuted instantiation: with_replacement.cpp:bool simdutf::scalar::(anonymous namespace)::base64::is_eight_byte<char16_t>(char16_t) |
39 | | |
40 | | template <class char_type> |
41 | | simdutf_constexpr23 bool is_ignorable(char_type c, |
42 | 0 | simdutf::base64_options options) { |
43 | 0 | const uint8_t *to_base64 = |
44 | 0 | (options & base64_default_or_url) |
45 | 0 | ? tables::base64::to_base64_default_or_url_value |
46 | 0 | : ((options & base64_url) ? tables::base64::to_base64_url_value |
47 | 0 | : tables::base64::to_base64_value); |
48 | 0 | const bool ignore_garbage = |
49 | 0 | (options == base64_options::base64_url_accept_garbage) || |
50 | 0 | (options == base64_options::base64_default_accept_garbage) || |
51 | 0 | (options == base64_options::base64_default_or_url_accept_garbage); |
52 | 0 | uint8_t code = to_base64[uint8_t(c)]; |
53 | 0 | if (is_eight_byte(c) && code <= 63) { |
54 | 0 | return false; |
55 | 0 | } |
56 | 0 | if (is_eight_byte(c) && code == 64) { |
57 | 0 | return true; |
58 | 0 | } |
59 | 0 | return ignore_garbage; |
60 | 0 | } Unexecuted instantiation: with_replacement.cpp:bool simdutf::scalar::(anonymous namespace)::base64::is_ignorable<char>(char, simdutf::base64_options) Unexecuted instantiation: with_replacement.cpp:bool simdutf::scalar::(anonymous namespace)::base64::is_ignorable<char16_t>(char16_t, simdutf::base64_options) |
61 | | template <class char_type> |
62 | | simdutf_constexpr23 bool is_base64(char_type c, |
63 | 0 | simdutf::base64_options options) { |
64 | 0 | const uint8_t *to_base64 = |
65 | 0 | (options & base64_default_or_url) |
66 | 0 | ? tables::base64::to_base64_default_or_url_value |
67 | 0 | : ((options & base64_url) ? tables::base64::to_base64_url_value |
68 | 0 | : tables::base64::to_base64_value); |
69 | 0 | uint8_t code = to_base64[uint8_t(c)]; |
70 | 0 | if (is_eight_byte(c) && code <= 63) { |
71 | 0 | return true; |
72 | 0 | } |
73 | 0 | return false; |
74 | 0 | } Unexecuted instantiation: with_replacement.cpp:bool simdutf::scalar::(anonymous namespace)::base64::is_base64<char>(char, simdutf::base64_options) Unexecuted instantiation: with_replacement.cpp:bool simdutf::scalar::(anonymous namespace)::base64::is_base64<char16_t>(char16_t, simdutf::base64_options) |
75 | | |
76 | | template <class char_type> |
77 | | simdutf_constexpr23 bool is_base64_or_padding(char_type c, |
78 | 0 | simdutf::base64_options options) { |
79 | 0 | const uint8_t *to_base64 = |
80 | 0 | (options & base64_default_or_url) |
81 | 0 | ? tables::base64::to_base64_default_or_url_value |
82 | 0 | : ((options & base64_url) ? tables::base64::to_base64_url_value |
83 | 0 | : tables::base64::to_base64_value); |
84 | 0 | if (c == '=') { |
85 | 0 | return true; |
86 | 0 | } |
87 | 0 | uint8_t code = to_base64[uint8_t(c)]; |
88 | 0 | if (is_eight_byte(c) && code <= 63) { |
89 | 0 | return true; |
90 | 0 | } |
91 | 0 | return false; |
92 | 0 | } Unexecuted instantiation: with_replacement.cpp:bool simdutf::scalar::(anonymous namespace)::base64::is_base64_or_padding<char>(char, simdutf::base64_options) Unexecuted instantiation: with_replacement.cpp:bool simdutf::scalar::(anonymous namespace)::base64::is_base64_or_padding<char16_t>(char16_t, simdutf::base64_options) |
93 | | |
94 | | template <class char_type> |
95 | | bool is_ignorable_or_padding(char_type c, simdutf::base64_options options) { |
96 | | return is_ignorable(c, options) || c == '='; |
97 | | } |
98 | | |
99 | | struct reduced_input { |
100 | | size_t equalsigns; // number of padding characters '=', typically 0, 1, 2. |
101 | | size_t equallocation; // location of the first padding character if any |
102 | | size_t srclen; // length of the input buffer before padding |
103 | | size_t full_input_length; // length of the input buffer with padding but |
104 | | // without ignorable characters |
105 | | }; |
106 | | |
107 | | // find the end of the base64 input buffer |
108 | | // It returns the number of padding characters, the location of the first |
109 | | // padding character if any, the length of the input buffer before padding |
110 | | // and the length of the input buffer with padding. The input buffer is not |
111 | | // modified. The function assumes that there are at most two padding characters. |
112 | | template <class char_type> |
113 | | simdutf_constexpr23 reduced_input find_end(const char_type *src, size_t srclen, |
114 | | simdutf::base64_options options) { |
115 | | const uint8_t *to_base64 = |
116 | | (options & base64_default_or_url) |
117 | | ? tables::base64::to_base64_default_or_url_value |
118 | | : ((options & base64_url) ? tables::base64::to_base64_url_value |
119 | | : tables::base64::to_base64_value); |
120 | | const bool ignore_garbage = |
121 | | (options == base64_options::base64_url_accept_garbage) || |
122 | | (options == base64_options::base64_default_accept_garbage) || |
123 | | (options == base64_options::base64_default_or_url_accept_garbage); |
124 | | |
125 | | size_t equalsigns = 0; |
126 | | // We intentionally include trailing spaces in the full input length. |
127 | | // See https://github.com/simdutf/simdutf/issues/824 |
128 | | size_t full_input_length = srclen; |
129 | | // skip trailing spaces |
130 | | while (!ignore_garbage && srclen > 0 && |
131 | | scalar::base64::is_eight_byte(src[srclen - 1]) && |
132 | | to_base64[uint8_t(src[srclen - 1])] == 64) { |
133 | | srclen--; |
134 | | } |
135 | | size_t equallocation = |
136 | | srclen; // location of the first padding character if any |
137 | | if (ignore_garbage) { |
138 | | // Technically, we don't need to find the first padding character, we can |
139 | | // just change our algorithms, but it adds substantial complexity. |
140 | | auto it = simdutf::find(src, src + srclen, '='); |
141 | | if (it != src + srclen) { |
142 | | equallocation = it - src; |
143 | | equalsigns = 1; |
144 | | srclen = equallocation; |
145 | | full_input_length = equallocation + 1; |
146 | | } |
147 | | return {equalsigns, equallocation, srclen, full_input_length}; |
148 | | } |
149 | | if (!ignore_garbage && srclen > 0 && src[srclen - 1] == '=') { |
150 | | // This is the last '=' sign. |
151 | | equallocation = srclen - 1; |
152 | | srclen--; |
153 | | equalsigns = 1; |
154 | | // skip trailing spaces |
155 | | while (srclen > 0 && scalar::base64::is_eight_byte(src[srclen - 1]) && |
156 | | to_base64[uint8_t(src[srclen - 1])] == 64) { |
157 | | srclen--; |
158 | | } |
159 | | if (srclen > 0 && src[srclen - 1] == '=') { |
160 | | // This is the second '=' sign. |
161 | | equallocation = srclen - 1; |
162 | | srclen--; |
163 | | equalsigns = 2; |
164 | | } |
165 | | } |
166 | | return {equalsigns, equallocation, srclen, full_input_length}; |
167 | | } |
168 | | |
169 | | // Returns true upon success. The destination buffer must be large enough. |
170 | | // This functions assumes that the padding (=) has been removed. |
171 | | // if check_capacity is true, it will check that the destination buffer is |
172 | | // large enough. If it is not, it will return OUTPUT_BUFFER_TOO_SMALL. |
173 | | template <bool check_capacity, class char_type> |
174 | | simdutf_constexpr23 full_result base64_tail_decode_impl( |
175 | | char *dst, size_t outlen, const char_type *src, size_t length, |
176 | | size_t padding_characters, // number of padding characters |
177 | | // '=', typically 0, 1, 2. |
178 | | base64_options options, last_chunk_handling_options last_chunk_options) { |
179 | | char *dstend = dst + outlen; |
180 | | (void)dstend; |
181 | | // This looks like 10 branches, but we expect the compiler to resolve this to |
182 | | // two branches (easily predicted): |
183 | | const uint8_t *to_base64 = |
184 | | (options & base64_default_or_url) |
185 | | ? tables::base64::to_base64_default_or_url_value |
186 | | : ((options & base64_url) ? tables::base64::to_base64_url_value |
187 | | : tables::base64::to_base64_value); |
188 | | const uint32_t *d0 = |
189 | | (options & base64_default_or_url) |
190 | | ? tables::base64::base64_default_or_url::d0 |
191 | | : ((options & base64_url) ? tables::base64::base64_url::d0 |
192 | | : tables::base64::base64_default::d0); |
193 | | const uint32_t *d1 = |
194 | | (options & base64_default_or_url) |
195 | | ? tables::base64::base64_default_or_url::d1 |
196 | | : ((options & base64_url) ? tables::base64::base64_url::d1 |
197 | | : tables::base64::base64_default::d1); |
198 | | const uint32_t *d2 = |
199 | | (options & base64_default_or_url) |
200 | | ? tables::base64::base64_default_or_url::d2 |
201 | | : ((options & base64_url) ? tables::base64::base64_url::d2 |
202 | | : tables::base64::base64_default::d2); |
203 | | const uint32_t *d3 = |
204 | | (options & base64_default_or_url) |
205 | | ? tables::base64::base64_default_or_url::d3 |
206 | | : ((options & base64_url) ? tables::base64::base64_url::d3 |
207 | | : tables::base64::base64_default::d3); |
208 | | const bool ignore_garbage = |
209 | | (options == base64_options::base64_url_accept_garbage) || |
210 | | (options == base64_options::base64_default_accept_garbage) || |
211 | | (options == base64_options::base64_default_or_url_accept_garbage); |
212 | | |
213 | | const char_type *srcend = src + length; |
214 | | const char_type *srcinit = src; |
215 | | const char *dstinit = dst; |
216 | | |
217 | | uint32_t x; |
218 | | size_t idx; |
219 | | uint8_t buffer[4]; |
220 | | while (true) { |
221 | | while (srcend - src >= 12 && (!check_capacity || dstend - dst >= 9) && |
222 | | is_eight_byte(src[0]) && is_eight_byte(src[1]) && |
223 | | is_eight_byte(src[2]) && is_eight_byte(src[3]) && |
224 | | is_eight_byte(src[4]) && is_eight_byte(src[5]) && |
225 | | is_eight_byte(src[6]) && is_eight_byte(src[7]) && |
226 | | is_eight_byte(src[8]) && is_eight_byte(src[9]) && |
227 | | is_eight_byte(src[10]) && is_eight_byte(src[11])) { |
228 | | const uint32_t x0 = d0[uint8_t(src[0])] | d1[uint8_t(src[1])] | |
229 | | d2[uint8_t(src[2])] | d3[uint8_t(src[3])]; |
230 | | const uint32_t x1 = d0[uint8_t(src[4])] | d1[uint8_t(src[5])] | |
231 | | d2[uint8_t(src[6])] | d3[uint8_t(src[7])]; |
232 | | const uint32_t x2 = d0[uint8_t(src[8])] | d1[uint8_t(src[9])] | |
233 | | d2[uint8_t(src[10])] | d3[uint8_t(src[11])]; |
234 | | if ((x0 | x1 | x2) >= 0x01FFFFFF) { |
235 | | break; |
236 | | } |
237 | | #if SIMDUTF_IS_BIG_ENDIAN |
238 | | dst[0] = static_cast<char>(x0); |
239 | | dst[1] = static_cast<char>(x0 >> 8); |
240 | | dst[2] = static_cast<char>(x0 >> 16); |
241 | | dst[3] = static_cast<char>(x1); |
242 | | dst[4] = static_cast<char>(x1 >> 8); |
243 | | dst[5] = static_cast<char>(x1 >> 16); |
244 | | dst[6] = static_cast<char>(x2); |
245 | | dst[7] = static_cast<char>(x2 >> 8); |
246 | | dst[8] = static_cast<char>(x2 >> 16); |
247 | | #else |
248 | | const uint64_t word = |
249 | | uint64_t(x0) | (uint64_t(x1) << 24) | (uint64_t(x2) << 48); |
250 | | #if SIMDUTF_CPLUSPLUS23 |
251 | | if (std::is_constant_evaluated()) { |
252 | | dst[0] = static_cast<char>(x0); |
253 | | dst[1] = static_cast<char>(x0 >> 8); |
254 | | dst[2] = static_cast<char>(x0 >> 16); |
255 | | dst[3] = static_cast<char>(x1); |
256 | | dst[4] = static_cast<char>(x1 >> 8); |
257 | | dst[5] = static_cast<char>(x1 >> 16); |
258 | | dst[6] = static_cast<char>(x2); |
259 | | dst[7] = static_cast<char>(x2 >> 8); |
260 | | } else |
261 | | #endif |
262 | | { |
263 | | std::memcpy(dst, &word, sizeof(word)); |
264 | | } |
265 | | dst[8] = static_cast<char>(x2 >> 16); |
266 | | #endif |
267 | | src += 12; |
268 | | dst += 9; |
269 | | } |
270 | | while (srcend - src >= 4 && is_eight_byte(src[0]) && |
271 | | is_eight_byte(src[1]) && is_eight_byte(src[2]) && |
272 | | is_eight_byte(src[3]) && |
273 | | (x = d0[uint8_t(src[0])] | d1[uint8_t(src[1])] | |
274 | | d2[uint8_t(src[2])] | d3[uint8_t(src[3])]) < 0x01FFFFFF) { |
275 | | if (check_capacity && dstend - dst < 3) { |
276 | | return {OUTPUT_BUFFER_TOO_SMALL, size_t(src - srcinit), |
277 | | size_t(dst - dstinit)}; |
278 | | } |
279 | | *dst++ = static_cast<char>(x & 0xFF); |
280 | | *dst++ = static_cast<char>((x >> 8) & 0xFF); |
281 | | *dst++ = static_cast<char>((x >> 16) & 0xFF); |
282 | | src += 4; |
283 | | } |
284 | | const char_type *srccur = src; |
285 | | idx = 0; |
286 | | // we need at least four characters. |
287 | | #ifdef __clang__ |
288 | | // If possible, we read four characters at a time. (It is an optimization.) |
289 | | if (ignore_garbage && src + 4 <= srcend) { |
290 | | char_type c0 = src[0]; |
291 | | char_type c1 = src[1]; |
292 | | char_type c2 = src[2]; |
293 | | char_type c3 = src[3]; |
294 | | |
295 | | uint8_t code0 = to_base64[uint8_t(c0)]; |
296 | | uint8_t code1 = to_base64[uint8_t(c1)]; |
297 | | uint8_t code2 = to_base64[uint8_t(c2)]; |
298 | | uint8_t code3 = to_base64[uint8_t(c3)]; |
299 | | |
300 | | buffer[idx] = code0; |
301 | | idx += (is_eight_byte(c0) && code0 <= 63); |
302 | | buffer[idx] = code1; |
303 | | idx += (is_eight_byte(c1) && code1 <= 63); |
304 | | buffer[idx] = code2; |
305 | | idx += (is_eight_byte(c2) && code2 <= 63); |
306 | | buffer[idx] = code3; |
307 | | idx += (is_eight_byte(c3) && code3 <= 63); |
308 | | src += 4; |
309 | | } |
310 | | #endif |
311 | | while ((idx < 4) && (src < srcend)) { |
312 | | char_type c = *src; |
313 | | |
314 | | uint8_t code = to_base64[uint8_t(c)]; |
315 | | buffer[idx] = uint8_t(code); |
316 | | if (is_eight_byte(c) && code <= 63) { |
317 | | idx++; |
318 | | } else if (!ignore_garbage && |
319 | | (code > 64 || !scalar::base64::is_eight_byte(c))) { |
320 | | return {INVALID_BASE64_CHARACTER, size_t(src - srcinit), |
321 | | size_t(dst - dstinit)}; |
322 | | } else { |
323 | | // We have a space or a newline or garbage. We ignore it. |
324 | | } |
325 | | src++; |
326 | | } |
327 | | if (idx != 4) { |
328 | | simdutf_log_assert(idx < 4, "idx should be less than 4"); |
329 | | // We never should have that the number of base64 characters + the |
330 | | // number of padding characters is more than 4. |
331 | | if (!ignore_garbage && (idx + padding_characters > 4)) { |
332 | | return {INVALID_BASE64_CHARACTER, size_t(src - srcinit), |
333 | | size_t(dst - dstinit), true}; |
334 | | } |
335 | | |
336 | | // The idea here is that in loose mode, |
337 | | // if there is padding at all, it must be used |
338 | | // to form 4-wise chunk. However, in loose mode, |
339 | | // we do accept no padding at all. |
340 | | if (!ignore_garbage && |
341 | | last_chunk_options == last_chunk_handling_options::loose && |
342 | | (idx >= 2) && padding_characters > 0 && |
343 | | ((idx + padding_characters) & 3) != 0) { |
344 | | return {INVALID_BASE64_CHARACTER, size_t(src - srcinit), |
345 | | size_t(dst - dstinit), true}; |
346 | | } else |
347 | | |
348 | | // The idea here is that in strict mode, we do not want to accept |
349 | | // incomplete base64 chunks. So if the chunk was otherwise valid, we |
350 | | // return BASE64_INPUT_REMAINDER. |
351 | | if (!ignore_garbage && |
352 | | last_chunk_options == last_chunk_handling_options::strict && |
353 | | (idx >= 2) && ((idx + padding_characters) & 3) != 0) { |
354 | | // The partial chunk was at src - idx |
355 | | return {BASE64_INPUT_REMAINDER, size_t(src - srcinit), |
356 | | size_t(dst - dstinit), true}; |
357 | | } else |
358 | | // If there is a partial chunk with insufficient padding, with |
359 | | // stop_before_partial, we need to just ignore it. In "only full" |
360 | | // mode, skip the minute there are padding characters. |
361 | | if ((last_chunk_options == |
362 | | last_chunk_handling_options::stop_before_partial && |
363 | | (padding_characters + idx < 4) && (idx != 0) && |
364 | | (idx >= 2 || padding_characters == 0)) || |
365 | | (last_chunk_options == |
366 | | last_chunk_handling_options::only_full_chunks && |
367 | | (idx >= 2 || padding_characters == 0))) { |
368 | | // partial means that we are *not* going to consume the read |
369 | | // characters. We need to rewind the src pointer. |
370 | | src = srccur; |
371 | | return {SUCCESS, size_t(src - srcinit), size_t(dst - dstinit)}; |
372 | | } else { |
373 | | if (idx == 2) { |
374 | | uint32_t triple = (uint32_t(buffer[0]) << 3 * 6) + |
375 | | (uint32_t(buffer[1]) << 2 * 6); |
376 | | if (!ignore_garbage && |
377 | | (last_chunk_options == last_chunk_handling_options::strict) && |
378 | | (triple & 0xffff)) { |
379 | | return {BASE64_EXTRA_BITS, size_t(src - srcinit), |
380 | | size_t(dst - dstinit)}; |
381 | | } |
382 | | if (check_capacity && dstend - dst < 1) { |
383 | | return {OUTPUT_BUFFER_TOO_SMALL, size_t(srccur - srcinit), |
384 | | size_t(dst - dstinit)}; |
385 | | } |
386 | | *dst++ = static_cast<char>((triple >> 16) & 0xFF); |
387 | | } else if (idx == 3) { |
388 | | uint32_t triple = (uint32_t(buffer[0]) << 3 * 6) + |
389 | | (uint32_t(buffer[1]) << 2 * 6) + |
390 | | (uint32_t(buffer[2]) << 1 * 6); |
391 | | if (!ignore_garbage && |
392 | | (last_chunk_options == last_chunk_handling_options::strict) && |
393 | | (triple & 0xff)) { |
394 | | return {BASE64_EXTRA_BITS, size_t(src - srcinit), |
395 | | size_t(dst - dstinit)}; |
396 | | } |
397 | | if (check_capacity && dstend - dst < 2) { |
398 | | return {OUTPUT_BUFFER_TOO_SMALL, size_t(srccur - srcinit), |
399 | | size_t(dst - dstinit)}; |
400 | | } |
401 | | *dst++ = static_cast<char>((triple >> 16) & 0xFF); |
402 | | *dst++ = static_cast<char>((triple >> 8) & 0xFF); |
403 | | } else if (!ignore_garbage && idx == 1 && |
404 | | (!is_partial(last_chunk_options) || |
405 | | (is_partial(last_chunk_options) && |
406 | | padding_characters > 0))) { |
407 | | return {BASE64_INPUT_REMAINDER, size_t(src - srcinit), |
408 | | size_t(dst - dstinit)}; |
409 | | } else if (!ignore_garbage && idx == 0 && padding_characters > 0) { |
410 | | return {INVALID_BASE64_CHARACTER, size_t(src - srcinit), |
411 | | size_t(dst - dstinit), true}; |
412 | | } |
413 | | return {SUCCESS, size_t(src - srcinit), size_t(dst - dstinit)}; |
414 | | } |
415 | | } |
416 | | if (check_capacity && dstend - dst < 3) { |
417 | | return {OUTPUT_BUFFER_TOO_SMALL, size_t(srccur - srcinit), |
418 | | size_t(dst - dstinit)}; |
419 | | } |
420 | | uint32_t triple = |
421 | | (uint32_t(buffer[0]) << 3 * 6) + (uint32_t(buffer[1]) << 2 * 6) + |
422 | | (uint32_t(buffer[2]) << 1 * 6) + (uint32_t(buffer[3]) << 0 * 6); |
423 | | *dst++ = static_cast<char>((triple >> 16) & 0xFF); |
424 | | *dst++ = static_cast<char>((triple >> 8) & 0xFF); |
425 | | *dst++ = static_cast<char>(triple & 0xFF); |
426 | | } |
427 | | } |
428 | | |
429 | | template <class char_type> |
430 | | simdutf_constexpr23 full_result base64_tail_decode( |
431 | | char *dst, const char_type *src, size_t length, |
432 | | size_t padding_characters, // number of padding characters |
433 | | // '=', typically 0, 1, 2. |
434 | | base64_options options, last_chunk_handling_options last_chunk_options) { |
435 | | return base64_tail_decode_impl<false>(dst, 0, src, length, padding_characters, |
436 | | options, last_chunk_options); |
437 | | } |
438 | | |
439 | | // like base64_tail_decode, but it will not write past the end of the output |
440 | | // buffer. The outlen parameter is modified to reflect the number of bytes |
441 | | // written. This functions assumes that the padding (=) has been removed. |
442 | | // |
443 | | template <class char_type> |
444 | | simdutf_constexpr23 full_result base64_tail_decode_safe( |
445 | | char *dst, size_t outlen, const char_type *src, size_t length, |
446 | | size_t padding_characters, // number of padding characters |
447 | | // '=', typically 0, 1, 2. |
448 | | base64_options options, last_chunk_handling_options last_chunk_options) { |
449 | | return base64_tail_decode_impl<true>(dst, outlen, src, length, |
450 | | padding_characters, options, |
451 | | last_chunk_options); |
452 | | } |
453 | | |
454 | | inline simdutf_constexpr23 full_result |
455 | | patch_tail_result(full_result r, size_t previous_input, size_t previous_output, |
456 | | size_t equallocation, size_t full_input_length, |
457 | 0 | last_chunk_handling_options last_chunk_options) { |
458 | 0 | r.input_count += previous_input; |
459 | 0 | r.output_count += previous_output; |
460 | 0 | if (r.padding_error) { |
461 | 0 | r.input_count = equallocation; |
462 | 0 | } |
463 | 0 |
|
464 | 0 | if (r.error == error_code::SUCCESS) { |
465 | 0 | if (!is_partial(last_chunk_options)) { |
466 | 0 | // A success when we are not in stop_before_partial mode. |
467 | 0 | // means that we have consumed the whole input buffer. |
468 | 0 | r.input_count = full_input_length; |
469 | 0 | } else if (r.output_count % 3 != 0) { |
470 | 0 | r.input_count = full_input_length; |
471 | 0 | } |
472 | 0 | } |
473 | 0 | return r; |
474 | 0 | } |
475 | | |
476 | | simdutf_really_inline simdutf_constexpr23 void |
477 | | tail_encode_base64_quantum(char *dst, const char *src, |
478 | 0 | const std::array<char, 2> *pairs) { |
479 | 0 | const uint32_t value = (uint32_t(uint8_t(src[0])) << 16) | |
480 | 0 | (uint32_t(uint8_t(src[1])) << 8) | uint8_t(src[2]); |
481 | 0 | copy_encode_pair(dst, pairs[value >> 12]); |
482 | 0 | copy_encode_pair(dst + 2, pairs[value & 0xFFF]); |
483 | 0 | } |
484 | | |
485 | | simdutf_really_inline simdutf_constexpr23 size_t tail_encode_base64_short_impl( |
486 | 0 | char *dst, const char *src, size_t srclen, base64_options options) { |
487 | 0 | const auto *pairs = (options & base64_url) |
488 | 0 | ? tables::base64::base64_url::encode_pairs.data() |
489 | 0 | : tables::base64::base64_default::encode_pairs.data(); |
490 | 0 | const bool use_padding = |
491 | 0 | ((options & base64_url) == 0) ^ |
492 | 0 | ((options & base64_reverse_padding) == base64_reverse_padding); |
493 | 0 |
|
494 | 0 | #define SIMDUTF_BASE64_ENCODE_QUANTUM(index) \ |
495 | 0 | tail_encode_base64_quantum(dst + (index) * 4, src + (index) * 3, pairs) |
496 | 0 | #define SIMDUTF_BASE64_ENCODE_TAIL1(index) \ |
497 | 0 | do { \ |
498 | 0 | const uint32_t value = uint32_t(uint8_t(src[(index) * 3])) << 16; \ |
499 | 0 | copy_encode_pair(dst + (index) * 4, pairs[value >> 12]); \ |
500 | 0 | if (use_padding) { \ |
501 | 0 | dst[(index) * 4 + 2] = '='; \ |
502 | 0 | dst[(index) * 4 + 3] = '='; \ |
503 | 0 | } \ |
504 | 0 | } while (false) |
505 | 0 | #define SIMDUTF_BASE64_ENCODE_TAIL2(index) \ |
506 | 0 | do { \ |
507 | 0 | const uint32_t value = (uint32_t(uint8_t(src[(index) * 3])) << 16) | \ |
508 | 0 | (uint32_t(uint8_t(src[(index) * 3 + 1])) << 8); \ |
509 | 0 | copy_encode_pair(dst + (index) * 4, pairs[value >> 12]); \ |
510 | 0 | dst[(index) * 4 + 2] = pairs[value & 0xFFF][0]; \ |
511 | 0 | if (use_padding) { \ |
512 | 0 | dst[(index) * 4 + 3] = '='; \ |
513 | 0 | } \ |
514 | 0 | } while (false) |
515 | 0 |
|
516 | 0 | switch (srclen) { |
517 | 0 | case 11: |
518 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(0); |
519 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(1); |
520 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(2); |
521 | 0 | SIMDUTF_BASE64_ENCODE_TAIL2(3); |
522 | 0 | return 15 + size_t(use_padding); |
523 | 0 | case 10: |
524 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(0); |
525 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(1); |
526 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(2); |
527 | 0 | SIMDUTF_BASE64_ENCODE_TAIL1(3); |
528 | 0 | return 14 + 2 * size_t(use_padding); |
529 | 0 | case 9: |
530 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(0); |
531 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(1); |
532 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(2); |
533 | 0 | return 12; |
534 | 0 | case 8: |
535 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(0); |
536 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(1); |
537 | 0 | SIMDUTF_BASE64_ENCODE_TAIL2(2); |
538 | 0 | return 11 + size_t(use_padding); |
539 | 0 | case 7: |
540 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(0); |
541 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(1); |
542 | 0 | SIMDUTF_BASE64_ENCODE_TAIL1(2); |
543 | 0 | return 10 + 2 * size_t(use_padding); |
544 | 0 | case 6: |
545 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(0); |
546 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(1); |
547 | 0 | return 8; |
548 | 0 | case 5: |
549 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(0); |
550 | 0 | SIMDUTF_BASE64_ENCODE_TAIL2(1); |
551 | 0 | return 7 + size_t(use_padding); |
552 | 0 | case 4: |
553 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(0); |
554 | 0 | SIMDUTF_BASE64_ENCODE_TAIL1(1); |
555 | 0 | return 6 + 2 * size_t(use_padding); |
556 | 0 | case 3: |
557 | 0 | SIMDUTF_BASE64_ENCODE_QUANTUM(0); |
558 | 0 | return 4; |
559 | 0 | case 2: |
560 | 0 | SIMDUTF_BASE64_ENCODE_TAIL2(0); |
561 | 0 | return 3 + size_t(use_padding); |
562 | 0 | case 1: |
563 | 0 | SIMDUTF_BASE64_ENCODE_TAIL1(0); |
564 | 0 | return 2 + 2 * size_t(use_padding); |
565 | 0 | default: |
566 | 0 | return 0; |
567 | 0 | } |
568 | 0 |
|
569 | 0 | #undef SIMDUTF_BASE64_ENCODE_TAIL2 |
570 | 0 | #undef SIMDUTF_BASE64_ENCODE_TAIL1 |
571 | 0 | #undef SIMDUTF_BASE64_ENCODE_QUANTUM |
572 | 0 | } |
573 | | |
574 | | simdutf_never_inline simdutf_constexpr23 size_t tail_encode_base64_long_impl( |
575 | 0 | char *dst, const char *src, size_t srclen, base64_options options) { |
576 | 0 | const auto *pairs = (options & base64_url) |
577 | 0 | ? tables::base64::base64_url::encode_pairs.data() |
578 | 0 | : tables::base64::base64_default::encode_pairs.data(); |
579 | 0 | char *out = dst; |
580 | 0 | size_t i = 0; |
581 | 0 | for (; i + 11 < srclen; i += 12) { |
582 | 0 | const uint32_t v0 = (uint32_t(uint8_t(src[i])) << 16) | |
583 | 0 | (uint32_t(uint8_t(src[i + 1])) << 8) | |
584 | 0 | uint8_t(src[i + 2]); |
585 | 0 | const uint32_t v1 = (uint32_t(uint8_t(src[i + 3])) << 16) | |
586 | 0 | (uint32_t(uint8_t(src[i + 4])) << 8) | |
587 | 0 | uint8_t(src[i + 5]); |
588 | 0 | const uint32_t v2 = (uint32_t(uint8_t(src[i + 6])) << 16) | |
589 | 0 | (uint32_t(uint8_t(src[i + 7])) << 8) | |
590 | 0 | uint8_t(src[i + 8]); |
591 | 0 | const uint32_t v3 = (uint32_t(uint8_t(src[i + 9])) << 16) | |
592 | 0 | (uint32_t(uint8_t(src[i + 10])) << 8) | |
593 | 0 | uint8_t(src[i + 11]); |
594 | 0 | copy_encode_pair(out, pairs[v0 >> 12]); |
595 | 0 | copy_encode_pair(out + 2, pairs[v0 & 0xFFF]); |
596 | 0 | copy_encode_pair(out + 4, pairs[v1 >> 12]); |
597 | 0 | copy_encode_pair(out + 6, pairs[v1 & 0xFFF]); |
598 | 0 | copy_encode_pair(out + 8, pairs[v2 >> 12]); |
599 | 0 | copy_encode_pair(out + 10, pairs[v2 & 0xFFF]); |
600 | 0 | copy_encode_pair(out + 12, pairs[v3 >> 12]); |
601 | 0 | copy_encode_pair(out + 14, pairs[v3 & 0xFFF]); |
602 | 0 | out += 16; |
603 | 0 | } |
604 | 0 | return size_t(out - dst); |
605 | 0 | } |
606 | | |
607 | | // Returns the number of bytes written. The destination buffer must be large |
608 | | // enough. It will add padding (=) if needed. |
609 | | template <bool use_lines = false> |
610 | | simdutf_constexpr23 size_t tail_encode_base64_impl( |
611 | | char *dst, const char *src, size_t srclen, base64_options options, |
612 | 0 | size_t line_length = simdutf::default_line_length, size_t line_offset = 0) { |
613 | 0 | if constexpr (!use_lines) { |
614 | 0 | if (srclen < 12) { |
615 | 0 | return tail_encode_base64_short_impl(dst, src, srclen, options); |
616 | 0 | } |
617 | 0 | const size_t i = srclen - srclen % 12; |
618 | 0 | const size_t written = tail_encode_base64_long_impl(dst, src, i, options); |
619 | 0 | return written + tail_encode_base64_short_impl(dst + written, src + i, |
620 | 0 | srclen - i, options); |
621 | 0 | } else { |
622 | 0 | // sanitize line_length and starting_line_offset. |
623 | 0 | // line_length must be greater than 3. |
624 | 0 | if (line_length < 4) { |
625 | 0 | line_length = 4; |
626 | 0 | } |
627 | 0 | simdutf_log_assert(line_offset <= line_length, |
628 | 0 | "line_offset should be less than line_length"); |
629 | 0 | // By default, we use padding if we are not using the URL variant. |
630 | 0 | // This is check with ((options & base64_url) == 0) which returns true if we |
631 | 0 | // are not using the URL variant. However, we also allow 'inversion' of the |
632 | 0 | // convention with the base64_reverse_padding option. If the |
633 | 0 | // base64_reverse_padding option is set, we use padding if we are using the |
634 | 0 | // URL variant, and we omit it if we are not using the URL variant. This is |
635 | 0 | // checked with |
636 | 0 | // ((options & base64_reverse_padding) == base64_reverse_padding). |
637 | 0 | bool use_padding = |
638 | 0 | ((options & base64_url) == 0) ^ |
639 | 0 | ((options & base64_reverse_padding) == base64_reverse_padding); |
640 | 0 | // This looks like 3 branches, but we expect the compiler to resolve this to |
641 | 0 | // a single branch: |
642 | 0 | const char *e0 = (options & base64_url) |
643 | 0 | ? tables::base64::base64_url::e0 |
644 | 0 | : tables::base64::base64_default::e0; |
645 | 0 | const char *e1 = (options & base64_url) |
646 | 0 | ? tables::base64::base64_url::e1 |
647 | 0 | : tables::base64::base64_default::e1; |
648 | 0 | const char *e2 = (options & base64_url) |
649 | 0 | ? tables::base64::base64_url::e2 |
650 | 0 | : tables::base64::base64_default::e2; |
651 | 0 | char *out = dst; |
652 | 0 | size_t i = 0; |
653 | 0 | uint8_t t1, t2, t3; |
654 | 0 | for (; i + 2 < srclen; i += 3) { |
655 | 0 | t1 = uint8_t(src[i]); |
656 | 0 | t2 = uint8_t(src[i + 1]); |
657 | 0 | t3 = uint8_t(src[i + 2]); |
658 | 0 | if (line_offset + 3 >= line_length) { |
659 | 0 | if (line_offset == line_length) { |
660 | 0 | *out++ = '\n'; |
661 | 0 | *out++ = e0[t1]; |
662 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
663 | 0 | *out++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)]; |
664 | 0 | *out++ = e2[t3]; |
665 | 0 | line_offset = 4; |
666 | 0 | } else if (line_offset + 1 == line_length) { |
667 | 0 | *out++ = e0[t1]; |
668 | 0 | *out++ = '\n'; |
669 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
670 | 0 | *out++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)]; |
671 | 0 | *out++ = e2[t3]; |
672 | 0 | line_offset = 3; |
673 | 0 | } else if (line_offset + 2 == line_length) { |
674 | 0 | *out++ = e0[t1]; |
675 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
676 | 0 | *out++ = '\n'; |
677 | 0 | *out++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)]; |
678 | 0 | *out++ = e2[t3]; |
679 | 0 | line_offset = 2; |
680 | 0 | } else if (line_offset + 3 == line_length) { |
681 | 0 | *out++ = e0[t1]; |
682 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
683 | 0 | *out++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)]; |
684 | 0 | *out++ = '\n'; |
685 | 0 | *out++ = e2[t3]; |
686 | 0 | line_offset = 1; |
687 | 0 | } |
688 | 0 | } else { |
689 | 0 | *out++ = e0[t1]; |
690 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
691 | 0 | *out++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)]; |
692 | 0 | *out++ = e2[t3]; |
693 | 0 | line_offset += 4; |
694 | 0 | } |
695 | 0 | } |
696 | 0 | switch (srclen - i) { |
697 | 0 | case 0: |
698 | 0 | break; |
699 | 0 | case 1: |
700 | 0 | t1 = uint8_t(src[i]); |
701 | 0 | if (use_padding) { |
702 | 0 | if (line_offset + 3 >= line_length) { |
703 | 0 | if (line_offset == line_length) { |
704 | 0 | *out++ = '\n'; |
705 | 0 | *out++ = e0[t1]; |
706 | 0 | *out++ = e1[(t1 & 0x03) << 4]; |
707 | 0 | *out++ = '='; |
708 | 0 | *out++ = '='; |
709 | 0 | } else if (line_offset + 1 == line_length) { |
710 | 0 | *out++ = e0[t1]; |
711 | 0 | *out++ = '\n'; |
712 | 0 | *out++ = e1[(t1 & 0x03) << 4]; |
713 | 0 | *out++ = '='; |
714 | 0 | *out++ = '='; |
715 | 0 | } else if (line_offset + 2 == line_length) { |
716 | 0 | *out++ = e0[t1]; |
717 | 0 | *out++ = e1[(t1 & 0x03) << 4]; |
718 | 0 | *out++ = '\n'; |
719 | 0 | *out++ = '='; |
720 | 0 | *out++ = '='; |
721 | 0 | } else if (line_offset + 3 == line_length) { |
722 | 0 | *out++ = e0[t1]; |
723 | 0 | *out++ = e1[(t1 & 0x03) << 4]; |
724 | 0 | *out++ = '='; |
725 | 0 | *out++ = '\n'; |
726 | 0 | *out++ = '='; |
727 | 0 | } |
728 | 0 | } else { |
729 | 0 | *out++ = e0[t1]; |
730 | 0 | *out++ = e1[(t1 & 0x03) << 4]; |
731 | 0 | *out++ = '='; |
732 | 0 | *out++ = '='; |
733 | 0 | } |
734 | 0 | } else { |
735 | 0 | if (line_offset + 2 >= line_length) { |
736 | 0 | if (line_offset == line_length) { |
737 | 0 | *out++ = '\n'; |
738 | 0 | *out++ = e0[uint8_t(src[i])]; |
739 | 0 | *out++ = e1[(uint8_t(src[i]) & 0x03) << 4]; |
740 | 0 | } else if (line_offset + 1 == line_length) { |
741 | 0 | *out++ = e0[uint8_t(src[i])]; |
742 | 0 | *out++ = '\n'; |
743 | 0 | *out++ = e1[(uint8_t(src[i]) & 0x03) << 4]; |
744 | 0 | } else { |
745 | 0 | *out++ = e0[uint8_t(src[i])]; |
746 | 0 | *out++ = e1[(uint8_t(src[i]) & 0x03) << 4]; |
747 | 0 | // *out++ = '\n'; ==> no newline at the end of the output |
748 | 0 | } |
749 | 0 | } else { |
750 | 0 | *out++ = e0[uint8_t(src[i])]; |
751 | 0 | *out++ = e1[(uint8_t(src[i]) & 0x03) << 4]; |
752 | 0 | } |
753 | 0 | } |
754 | 0 | break; |
755 | 0 | default: /* case 2 */ |
756 | 0 | t1 = uint8_t(src[i]); |
757 | 0 | t2 = uint8_t(src[i + 1]); |
758 | 0 | if (use_padding) { |
759 | 0 | if (line_offset + 3 >= line_length) { |
760 | 0 | if (line_offset == line_length) { |
761 | 0 | *out++ = '\n'; |
762 | 0 | *out++ = e0[t1]; |
763 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
764 | 0 | *out++ = e2[(t2 & 0x0F) << 2]; |
765 | 0 | *out++ = '='; |
766 | 0 | } else if (line_offset + 1 == line_length) { |
767 | 0 | *out++ = e0[t1]; |
768 | 0 | *out++ = '\n'; |
769 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
770 | 0 | *out++ = e2[(t2 & 0x0F) << 2]; |
771 | 0 | *out++ = '='; |
772 | 0 | } else if (line_offset + 2 == line_length) { |
773 | 0 | *out++ = e0[t1]; |
774 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
775 | 0 | *out++ = '\n'; |
776 | 0 | *out++ = e2[(t2 & 0x0F) << 2]; |
777 | 0 | *out++ = '='; |
778 | 0 | } else if (line_offset + 3 == line_length) { |
779 | 0 | *out++ = e0[t1]; |
780 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
781 | 0 | *out++ = e2[(t2 & 0x0F) << 2]; |
782 | 0 | *out++ = '\n'; |
783 | 0 | *out++ = '='; |
784 | 0 | } |
785 | 0 | } else { |
786 | 0 | *out++ = e0[t1]; |
787 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
788 | 0 | *out++ = e2[(t2 & 0x0F) << 2]; |
789 | 0 | *out++ = '='; |
790 | 0 | } |
791 | 0 | } else { |
792 | 0 | if (line_offset + 3 >= line_length) { |
793 | 0 | if (line_offset == line_length) { |
794 | 0 | *out++ = '\n'; |
795 | 0 | *out++ = e0[t1]; |
796 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
797 | 0 | *out++ = e2[(t2 & 0x0F) << 2]; |
798 | 0 | } else if (line_offset + 1 == line_length) { |
799 | 0 | *out++ = e0[t1]; |
800 | 0 | *out++ = '\n'; |
801 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
802 | 0 | *out++ = e2[(t2 & 0x0F) << 2]; |
803 | 0 | } else if (line_offset + 2 == line_length) { |
804 | 0 | *out++ = e0[t1]; |
805 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
806 | 0 | *out++ = '\n'; |
807 | 0 | *out++ = e2[(t2 & 0x0F) << 2]; |
808 | 0 | } else { |
809 | 0 | *out++ = e0[t1]; |
810 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
811 | 0 | *out++ = e2[(t2 & 0x0F) << 2]; |
812 | 0 | // *out++ = '\n'; ==> no newline at the end of the output |
813 | 0 | } |
814 | 0 | } else { |
815 | 0 | *out++ = e0[t1]; |
816 | 0 | *out++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)]; |
817 | 0 | *out++ = e2[(t2 & 0x0F) << 2]; |
818 | 0 | } |
819 | 0 | } |
820 | 0 | } |
821 | 0 | return (size_t)(out - dst); |
822 | 0 | } |
823 | 0 | } |
824 | | |
825 | | // Returns the number of bytes written. The destination buffer must be large |
826 | | // enough. It will add padding (=) if needed. |
827 | | simdutf_unused inline simdutf_constexpr23 size_t tail_encode_base64( |
828 | 0 | char *dst, const char *src, size_t srclen, base64_options options) { |
829 | 0 | return tail_encode_base64_impl(dst, src, srclen, options); |
830 | 0 | } |
831 | | |
832 | | template <class InputPtr> |
833 | | simdutf_warn_unused simdutf_constexpr23 size_t |
834 | | maximal_binary_length_from_base64(InputPtr input, size_t length) noexcept { |
835 | | // We process the padding characters ('=') at the end to make sure |
836 | | // that we return an exact result when the input has no ignorable characters |
837 | | // (e.g., spaces). |
838 | | size_t padding = 0; |
839 | | if (length > 0) { |
840 | | if (input[length - 1] == '=') { |
841 | | padding++; |
842 | | if (length > 1 && input[length - 2] == '=') { |
843 | | padding++; |
844 | | } |
845 | | } |
846 | | } |
847 | | // The input is not otherwise processed for ignorable characters or |
848 | | // validation, so that the function runs in constant time (very fast). In |
849 | | // practice, base64 inputs without ignorable characters are common and the |
850 | | // common case are line separated inputs with relatively long lines (e.g., 76 |
851 | | // characters) which leads this function to a slight (1%) overestimation of |
852 | | // the output size. |
853 | | // |
854 | | // Of course, some inputs might contain an arbitrary number of spaces or |
855 | | // newlines, which would make this function return a very pessimistic output |
856 | | // size but systems that produce base64 outputs typically do not do that and |
857 | | // if they do, they do not care much about minimizing memory usage. |
858 | | // |
859 | | // In specialized applications, users may know that their input is line |
860 | | // separated, which can be checked very quickly by by iterating (e.g., over 76 |
861 | | // character chunks, looking for the linefeed characters only). We could |
862 | | // provide a specialized function for that, but it is not clear that the added |
863 | | // complexity is worth it for us. |
864 | | // |
865 | | size_t actual_length = length - padding; |
866 | | if (actual_length % 4 <= 1) { |
867 | | return actual_length / 4 * 3; |
868 | | } |
869 | | // if we have a valid input, then the remainder must be 2 or 3 adding one or |
870 | | // two extra bytes. |
871 | | return actual_length / 4 * 3 + (actual_length % 4) - 1; |
872 | | } |
873 | | |
874 | | // This function computes the binary length by iterating through the input |
875 | | // and counting non-whitespace characters (excluding padding characters). |
876 | | // We use a simple check (c > ' ') which is easy to parallelize and matches |
877 | | // SIMD behavior. Only the last few characters are checked for padding '='. |
878 | | template <class char_type> |
879 | | simdutf_warn_unused simdutf_constexpr23 size_t |
880 | | binary_length_from_base64(const char_type *input, size_t length) noexcept { |
881 | | // Count non-whitespace characters (c > ' ') with loop unrolling |
882 | | size_t count = 0; |
883 | | for (size_t i = 0; i < length; i++) { |
884 | | count += (input[i] > ' '); |
885 | | } |
886 | | |
887 | | // Check for padding '=' at the end (at most 2 padding characters) |
888 | | // Scan backwards, skipping whitespace, to find padding |
889 | | size_t padding = 0; |
890 | | size_t pos = length; |
891 | | // Skip trailing whitespace |
892 | | while (pos > 0 && padding < 2) { |
893 | | char_type c = input[--pos]; |
894 | | if (c == '=') { |
895 | | padding++; |
896 | | } else if (c > ' ') { |
897 | | break; |
898 | | } |
899 | | } |
900 | | return ((count - padding) * 3) / 4; |
901 | | } |
902 | | |
903 | | template <typename char_type> |
904 | | simdutf_warn_unused simdutf_constexpr23 full_result |
905 | | base64_to_binary_details_impl( |
906 | | const char_type *input, size_t length, char *output, base64_options options, |
907 | | last_chunk_handling_options last_chunk_options) noexcept { |
908 | | const bool ignore_garbage = |
909 | | (options == base64_options::base64_url_accept_garbage) || |
910 | | (options == base64_options::base64_default_accept_garbage) || |
911 | | (options == base64_options::base64_default_or_url_accept_garbage); |
912 | | auto ri = simdutf::scalar::base64::find_end(input, length, options); |
913 | | size_t equallocation = ri.equallocation; |
914 | | size_t equalsigns = ri.equalsigns; |
915 | | length = ri.srclen; |
916 | | size_t full_input_length = ri.full_input_length; |
917 | | if (length == 0) { |
918 | | if (!ignore_garbage && equalsigns > 0) { |
919 | | return {INVALID_BASE64_CHARACTER, equallocation, 0, true}; |
920 | | } |
921 | | return {SUCCESS, full_input_length, 0}; |
922 | | } |
923 | | full_result r = scalar::base64::base64_tail_decode( |
924 | | output, input, length, equalsigns, options, last_chunk_options); |
925 | | r = scalar::base64::patch_tail_result(r, 0, 0, equallocation, |
926 | | full_input_length, last_chunk_options); |
927 | | if (!is_partial(last_chunk_options) && r.error == error_code::SUCCESS && |
928 | | equalsigns > 0 && !ignore_garbage) { |
929 | | // additional checks |
930 | | if ((r.output_count % 3 == 0) || |
931 | | ((r.output_count % 3) + 1 + equalsigns != 4)) { |
932 | | return {INVALID_BASE64_CHARACTER, equallocation, r.output_count, true}; |
933 | | } |
934 | | } |
935 | | // When is_partial(last_chunk_options) is true, we must either end with |
936 | | // the end of the stream (beyond whitespace) or right after a non-ignorable |
937 | | // character or at the very beginning of the stream. |
938 | | // See https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64 |
939 | | if (is_partial(last_chunk_options) && r.error == error_code::SUCCESS && |
940 | | r.input_count < full_input_length) { |
941 | | // First check if we can extend the input to the end of the stream |
942 | | while (r.input_count < full_input_length && |
943 | | base64_ignorable(*(input + r.input_count), options)) { |
944 | | r.input_count++; |
945 | | } |
946 | | // If we are still not at the end of the stream, then we must backtrack |
947 | | // to the last non-ignorable character. |
948 | | if (r.input_count < full_input_length) { |
949 | | while (r.input_count > 0 && |
950 | | base64_ignorable(*(input + r.input_count - 1), options)) { |
951 | | r.input_count--; |
952 | | } |
953 | | } |
954 | | } |
955 | | return r; |
956 | | } |
957 | | |
958 | | template <typename char_type> |
959 | | simdutf_constexpr23 simdutf_warn_unused full_result |
960 | | base64_to_binary_details_safe_impl( |
961 | | const char_type *input, size_t length, char *output, size_t outlen, |
962 | | base64_options options, |
963 | | last_chunk_handling_options last_chunk_options) noexcept { |
964 | | const bool ignore_garbage = |
965 | | (options == base64_options::base64_url_accept_garbage) || |
966 | | (options == base64_options::base64_default_accept_garbage) || |
967 | | (options == base64_options::base64_default_or_url_accept_garbage); |
968 | | auto ri = simdutf::scalar::base64::find_end(input, length, options); |
969 | | size_t equallocation = ri.equallocation; |
970 | | size_t equalsigns = ri.equalsigns; |
971 | | length = ri.srclen; |
972 | | size_t full_input_length = ri.full_input_length; |
973 | | if (length == 0) { |
974 | | if (!ignore_garbage && equalsigns > 0) { |
975 | | return {INVALID_BASE64_CHARACTER, equallocation, 0}; |
976 | | } |
977 | | return {SUCCESS, full_input_length, 0}; |
978 | | } |
979 | | full_result r = scalar::base64::base64_tail_decode_safe( |
980 | | output, outlen, input, length, equalsigns, options, last_chunk_options); |
981 | | r = scalar::base64::patch_tail_result(r, 0, 0, equallocation, |
982 | | full_input_length, last_chunk_options); |
983 | | if (!is_partial(last_chunk_options) && r.error == error_code::SUCCESS && |
984 | | equalsigns > 0 && !ignore_garbage) { |
985 | | // additional checks |
986 | | if ((r.output_count % 3 == 0) || |
987 | | ((r.output_count % 3) + 1 + equalsigns != 4)) { |
988 | | return {INVALID_BASE64_CHARACTER, equallocation, r.output_count}; |
989 | | } |
990 | | } |
991 | | |
992 | | // When is_partial(last_chunk_options) is true, we must either end with |
993 | | // the end of the stream (beyond whitespace) or right after a non-ignorable |
994 | | // character or at the very beginning of the stream. |
995 | | // See https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64 |
996 | | if (is_partial(last_chunk_options) && r.error == error_code::SUCCESS && |
997 | | r.input_count < full_input_length) { |
998 | | // First check if we can extend the input to the end of the stream |
999 | | while (r.input_count < full_input_length && |
1000 | | base64_ignorable(*(input + r.input_count), options)) { |
1001 | | r.input_count++; |
1002 | | } |
1003 | | // If we are still not at the end of the stream, then we must backtrack |
1004 | | // to the last non-ignorable character. |
1005 | | if (r.input_count < full_input_length) { |
1006 | | while (r.input_count > 0 && |
1007 | | base64_ignorable(*(input + r.input_count - 1), options)) { |
1008 | | r.input_count--; |
1009 | | } |
1010 | | } |
1011 | | } |
1012 | | return r; |
1013 | | } |
1014 | | |
1015 | | simdutf_warn_unused simdutf_constexpr23 size_t |
1016 | 0 | base64_length_from_binary(size_t length, base64_options options) noexcept { |
1017 | 0 | // By default, we use padding if we are not using the URL variant. |
1018 | 0 | // This is check with ((options & base64_url) == 0) which returns true if we |
1019 | 0 | // are not using the URL variant. However, we also allow 'inversion' of the |
1020 | 0 | // convention with the base64_reverse_padding option. If the |
1021 | 0 | // base64_reverse_padding option is set, we use padding if we are using the |
1022 | 0 | // URL variant, and we omit it if we are not using the URL variant. This is |
1023 | 0 | // checked with |
1024 | 0 | // ((options & base64_reverse_padding) == base64_reverse_padding). |
1025 | 0 | bool use_padding = |
1026 | 0 | ((options & base64_url) == 0) ^ |
1027 | 0 | ((options & base64_reverse_padding) == base64_reverse_padding); |
1028 | 0 | if (!use_padding) { |
1029 | 0 | return length / 3 * 4 + ((length % 3) ? (length % 3) + 1 : 0); |
1030 | 0 | } |
1031 | 0 | return (length + 2) / 3 * |
1032 | 0 | 4; // We use padding to make the length a multiple of 4. |
1033 | 0 | } |
1034 | | |
1035 | | simdutf_warn_unused simdutf_constexpr23 size_t |
1036 | | base64_length_from_binary_with_lines(size_t length, base64_options options, |
1037 | 0 | size_t line_length) noexcept { |
1038 | 0 | if (length == 0) { |
1039 | 0 | return 0; |
1040 | 0 | } |
1041 | 0 | size_t base64_length = |
1042 | 0 | scalar::base64::base64_length_from_binary(length, options); |
1043 | 0 | if (line_length < 4) { |
1044 | 0 | line_length = 4; |
1045 | 0 | } |
1046 | 0 | // number of lines, rounded up without computing base64_length + line_length, |
1047 | 0 | // which wraps around when line_length is close to SIZE_MAX |
1048 | 0 | size_t lines = base64_length / line_length; |
1049 | 0 | if (base64_length % line_length != 0) { |
1050 | 0 | lines++; |
1051 | 0 | } |
1052 | 0 | return base64_length + lines - 1; |
1053 | 0 | } |
1054 | | |
1055 | | // Return the length of the prefix that contains count base64 characters. |
1056 | | // Thus, if count is 3, the function returns the length of the prefix |
1057 | | // that contains 3 base64 characters. |
1058 | | // The function returns (size_t)-1 if there is not enough base64 characters in |
1059 | | // the input. |
1060 | | template <typename char_type> |
1061 | | simdutf_warn_unused size_t prefix_length(size_t count, |
1062 | | simdutf::base64_options options, |
1063 | | const char_type *input, |
1064 | | size_t length) noexcept { |
1065 | | size_t i = 0; |
1066 | | while (i < length && is_ignorable(input[i], options)) { |
1067 | | i++; |
1068 | | } |
1069 | | if (count == 0) { |
1070 | | return i; // duh! |
1071 | | } |
1072 | | for (; i < length; i++) { |
1073 | | if (is_ignorable(input[i], options)) { |
1074 | | continue; |
1075 | | } |
1076 | | // We have a base64 character or a padding character. |
1077 | | count--; |
1078 | | if (count == 0) { |
1079 | | return i + 1; |
1080 | | } |
1081 | | } |
1082 | | simdutf_log_assert(false, "You never get here"); |
1083 | | |
1084 | | return -1; // should never happen |
1085 | | } |
1086 | | |
1087 | | } // namespace base64 |
1088 | | } // unnamed namespace |
1089 | | } // namespace scalar |
1090 | | } // namespace simdutf |
1091 | | |
1092 | | #endif |