stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
base64.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2016-2023 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include "stream.hpp"
10#include <cstdint>
11#include <string>
12#include <vector>
13
14
15namespace stdex
16{
18#if _HAS_CXX17
19 inline
20#endif
21 const char base64_enc_lookup[64] = {
22 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
23 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
24 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
25 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
26 };
27
28#if _HAS_CXX17
29 inline
30#endif
31 const uint8_t base64_dec_lookup[256] = {
32 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
33 /* 0 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
34 /* 1 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
35 /* 2 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
36 /* 3 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 64, 255, 255,
37 /* 4 */ 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
38 /* 5 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
39 /* 6 */ 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
40 /* 7 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255,
41 /* 8 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
42 /* 9 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
43 /* A */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
44 /* B */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
45 /* C */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
46 /* D */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
47 /* E */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
48 /* F */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
49 };
51
56 {
57 public:
61 base64_enc() noexcept : m_num(0)
62 {
63 m_buf[0] = 0;
64 m_buf[1] = 0;
65 m_buf[2] = 0;
66 }
67
76 template<class _Elem, class _Traits, class _Ax>
77 void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ bool is_last = true)
78 {
79 _Assume_(data || !size);
80
81 // Preallocate output
82 out.reserve(out.size() + enc_size(size));
83
84 // Convert data character by character.
85 for (size_t i = 0;; i++) {
86 if (m_num >= 3) {
87 encode(out);
88 m_num = 0;
89 }
90
91 if (i >= size)
92 break;
93
94 m_buf[m_num++] = reinterpret_cast<const uint8_t*>(data)[i];
95 }
96
97 // If this is the last block, flush the buffer.
98 if (is_last && m_num) {
99 encode(out, m_num);
100 m_num = 0;
101 }
102 }
103
107 void clear() noexcept
108 {
109 m_num = 0;
110 }
111
119 size_t enc_size(_In_ size_t size) const noexcept
120 {
121 return ((m_num + size + 2)/3)*4;
122 }
123
124 protected:
128 template<class _Elem, class _Traits, class _Ax>
129 void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out)
130 {
131 out += base64_enc_lookup[ m_buf[0] >> 2 ];
132 out += base64_enc_lookup[((m_buf[0] << 4) | (m_buf[1] >> 4)) & 0x3f];
133 out += base64_enc_lookup[((m_buf[1] << 2) | (m_buf[2] >> 6)) & 0x3f];
134 out += base64_enc_lookup[ m_buf[2] & 0x3f];
135 }
136
140 template<class _Elem, class _Traits, class _Ax>
141 void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_ size_t size)
142 {
143 if (size > 0) {
144 out += base64_enc_lookup[m_buf[0] >> 2];
145 if (size > 1) {
146 out += base64_enc_lookup[((m_buf[0] << 4) | (m_buf[1] >> 4)) & 0x3f];
147 if (size > 2) {
148 out += base64_enc_lookup[((m_buf[1] << 2) | (m_buf[2] >> 6)) & 0x3f];
149 out += base64_enc_lookup[m_buf[2] & 0x3f];
150 } else {
151 out += base64_enc_lookup[(m_buf[1] << 2) & 0x3f];
152 out += '=';
153 }
154 } else {
155 out += base64_enc_lookup[(m_buf[0] << 4) & 0x3f];
156 out += '=';
157 out += '=';
158 }
159 } else {
160 out += '=';
161 out += '=';
162 out += '=';
163 out += '=';
164 }
165 }
166
167 protected:
168 uint8_t m_buf[3];
169 size_t m_num;
170 };
171
176 {
177 public:
178 base64_writer(_Inout_ stdex::stream::basic& source, _In_ size_t max_blocks = 19) :
180 m_max_blocks(max_blocks),
181 m_num_blocks(0)
182 {}
183
184 virtual ~base64_writer()
185 {
186 // Flush the buffer.
187 if (m_num) {
188 if (++m_num_blocks > m_max_blocks) {
189 *m_source << '\n';
190 m_num_blocks = 1;
191 }
192 encode(m_num);
193 }
194 }
195
196 virtual _Success_(return != 0) size_t write(
197 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
198 {
199 _Assume_(data || !length);
200 for (size_t i = 0;; i++) {
201 if (m_num >= 3) {
202 if (++m_num_blocks > m_max_blocks) {
203 *m_source << '\n';
204 m_num_blocks = 1;
205 }
206 encode();
207 if (!m_source->ok()) _Unlikely_ {
208 m_state = m_source->state();
209 return length - i;
210 }
211 m_num = 0;
212 }
213 if (i >= length) {
214 m_state = stdex::stream::state_t::ok;
215 return length;
216 }
217 m_buf[m_num++] = reinterpret_cast<const uint8_t*>(data)[i];
218 }
219 }
220
221 protected:
225 void encode()
226 {
227 char out[4];
228 out[0] = base64_enc_lookup[ m_buf[0] >> 2 ];
229 out[1] = base64_enc_lookup[((m_buf[0] << 4) | (m_buf[1] >> 4)) & 0x3f];
230 out[2] = base64_enc_lookup[((m_buf[1] << 2) | (m_buf[2] >> 6)) & 0x3f];
231 out[3] = base64_enc_lookup[ m_buf[2] & 0x3f];
232 m_source->write_array(out, sizeof(*out), _countof(out));
233 }
234
238 void encode(_In_ size_t size)
239 {
240 char out[4];
241 if (size > 0) {
242 out[0] = base64_enc_lookup[m_buf[0] >> 2];
243 if (size > 1) {
244 out[1] = base64_enc_lookup[((m_buf[0] << 4) | (m_buf[1] >> 4)) & 0x3f];
245 if (size > 2) {
246 out[2] = base64_enc_lookup[((m_buf[1] << 2) | (m_buf[2] >> 6)) & 0x3f];
247 out[3] = base64_enc_lookup[m_buf[2] & 0x3f];
248 } else {
249 out[2] = base64_enc_lookup[(m_buf[1] << 2) & 0x3f];
250 out[3] = '=';
251 }
252 } else {
253 out[1] = base64_enc_lookup[(m_buf[0] << 4) & 0x3f];
254 out[2] = '=';
255 out[3] = '=';
256 }
257 } else {
258 out[0] = '=';
259 out[1] = '=';
260 out[2] = '=';
261 out[3] = '=';
262 }
263 m_source->write_array(out, sizeof(*out), _countof(out));
264 }
265
266 protected:
267 size_t
268 m_max_blocks,
270 };
271
276 {
277 public:
281 base64_dec() noexcept : m_num(0)
282 {
283 m_buf[0] = 0;
284 m_buf[1] = 0;
285 m_buf[2] = 0;
286 m_buf[3] = 0;
287 }
288
297 template<class _Ty, class _Ax, class _Tchr>
298 void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
299 {
300 is_last = false;
301
302 // Trim data size to first terminator.
303 for (size_t k = 0; k < size; k++)
304 if (!data[k]) { size = k; break; }
305
306 // Preallocate output
307 out.reserve(out.size() + dec_size(size));
308
309 for (size_t i = 0;; i++) {
310 if (m_num >= 4) {
311 // Buffer full; decode it.
312 size_t nibbles = decode(out);
313 if (nibbles < 3) {
314 is_last = true;
315 break;
316 }
317 }
318
319 if (i >= size)
320 break;
321
322 int x = data[i];
323 if ((m_buf[m_num] = x < _countof(base64_dec_lookup) ? base64_dec_lookup[x] : 255) != 255)
324 m_num++;
325 }
326 }
327
331 void clear() noexcept
332 {
333 m_num = 0;
334 }
335
343 size_t dec_size(_In_ size_t size) const noexcept
344 {
345 return ((m_num + size + 3)/4)*3;
346 }
347
348 protected:
352 template<class _Ty, class _Ax>
353 size_t decode(_Inout_ std::vector<_Ty, _Ax> &out)
354 {
355 m_num = 0;
356 out.push_back((_Ty)(((m_buf[0] << 2) | (m_buf[1] >> 4)) & 0xff));
357 if (m_buf[2] < 64) {
358 out.push_back((_Ty)(((m_buf[1] << 4) | (m_buf[2] >> 2)) & 0xff));
359 if (m_buf[3] < 64) {
360 out.push_back((_Ty)(((m_buf[2] << 6) | m_buf[3]) & 0xff));
361 return 3;
362 } else
363 return 2;
364 } else
365 return 1;
366 }
367
368 protected:
369 uint8_t m_buf[4];
370 size_t m_num;
371 };
372
373#ifdef _MSC_VER
374#pragma warning(push)
375#pragma warning(disable: 26495)
376#endif
377
382 {
383 public:
384 base64_reader(_Inout_ stdex::stream::basic& source) :
386 m_temp_off(0),
387 m_temp_len(0)
388 {}
389
390#pragma warning(suppress: 6101) // See [1] below
391 virtual _Success_(return != 0 || length == 0) size_t read(
392 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
393 {
394 _Assume_(data || !length);
395 for (size_t to_read = length;;) {
396 if (m_temp_len >= to_read) {
397 memcpy(data, m_temp + m_temp_off, to_read);
398 m_temp_off += to_read;
399 m_temp_len -= to_read;
400 m_state = stdex::stream::state_t::ok;
401 return length;
402 }
403 if (m_temp_len) {
404 memcpy(data, m_temp + m_temp_off, m_temp_len);
405 reinterpret_cast<uint8_t*&>(data) += m_temp_len;
406 to_read -= m_temp_len;
407 m_temp_off = 0;
408 m_temp_len = 0;
409 }
410 // Read one Base64 block (4 chars)
411 while (m_num < 4) {
412 uint8_t x;
413 *m_source >> x;
414 if (!m_source->ok()) _Unlikely_ {
415 m_state = m_source->state();
416 return length - to_read; // [1] Code analysis misses `length - to_read` bytes were written to data in previous loop iterations.
417 }
418 if ((m_buf[m_num] = base64_dec_lookup[x]) != 255)
419 m_num++;
420 }
421 decode();
422 if (m_temp_len < 3 && to_read >= 3) {
423 // If Base64 indicates end of data, truncate read to hint the client, end of Base64 data has been reached.
424 memcpy(data, m_temp + m_temp_off, m_temp_len);
425 m_temp_off = 0;
426 m_temp_len = 0;
427 to_read -= m_temp_len;
428 m_state = stdex::stream::state_t::ok;
429 return length - to_read; // [1] Code analysis misses `length - to_read` bytes were written to data in previous loop iterations.
430 }
431 }
432 }
433
434 protected:
438 void decode()
439 {
440 m_num = 0;
441 m_temp_off = 0;
442 m_temp[0] = ((m_buf[0] << 2) | (m_buf[1] >> 4)) & 0xff;
443 if (m_buf[2] < 64) {
444 m_temp[1] = ((m_buf[1] << 4) | (m_buf[2] >> 2)) & 0xff;
445 if (m_buf[3] < 64) {
446 m_temp[2] = ((m_buf[2] << 6) | m_buf[3]) & 0xff;
447 m_temp_len = 3;
448 } else
449 m_temp_len = 2;
450 } else
451 m_temp_len = 1;
452 }
453
454 protected:
455 char m_temp[3];
456 size_t
459 };
460
461#ifdef _MSC_VER
462#pragma warning(pop)
463#endif
464}
Base64 decoding session.
Definition base64.hpp:276
size_t m_num
Number of bytes used in m_buf
Definition base64.hpp:370
base64_dec() noexcept
Constructs blank decoding session.
Definition base64.hpp:281
void decode(std::vector< _Ty, _Ax > &out, bool &is_last, const _Tchr *data, size_t size)
Decodes one block of information, and appends it to the output.
Definition base64.hpp:298
size_t dec_size(size_t size) const noexcept
Returns maximum decoded size.
Definition base64.hpp:343
size_t decode(std::vector< _Ty, _Ax > &out)
Decodes one complete internal buffer of data.
Definition base64.hpp:353
void clear() noexcept
Resets decoding session.
Definition base64.hpp:331
uint8_t m_buf[4]
Internal buffer.
Definition base64.hpp:369
Base64 encoding session.
Definition base64.hpp:56
void encode(std::basic_string< _Elem, _Traits, _Ax > &out, const void *data, size_t size, bool is_last=true)
Encodes one block of information, and appends it to the output.
Definition base64.hpp:77
void encode(std::basic_string< _Elem, _Traits, _Ax > &out)
Encodes one complete internal buffer of data.
Definition base64.hpp:129
size_t m_num
Number of bytes used in m_buf
Definition base64.hpp:169
uint8_t m_buf[3]
Internal buffer.
Definition base64.hpp:168
void encode(std::basic_string< _Elem, _Traits, _Ax > &out, size_t size)
Encodes partial internal buffer of data.
Definition base64.hpp:141
base64_enc() noexcept
Constructs blank encoding session.
Definition base64.hpp:61
void clear() noexcept
Resets encoding session.
Definition base64.hpp:107
size_t enc_size(size_t size) const noexcept
Returns maximum encoded size.
Definition base64.hpp:119
Converts from Base64 when reading from a stream.
Definition base64.hpp:382
void decode()
Decodes one complete internal buffer of data.
Definition base64.hpp:438
char m_temp[3]
Temporary buffer.
Definition base64.hpp:455
size_t m_temp_len
Number of bytes of data in m_temp
Definition base64.hpp:458
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition base64.hpp:391
size_t m_temp_off
Index of data start in m_temp
Definition base64.hpp:457
Converts to Base64 when writing to a stream.
Definition base64.hpp:176
size_t m_num_blocks
‍Maximum number of Base64 blocks (4 chars) to write without a line break (SIZE_MAX no line breaks)
Definition base64.hpp:269
void encode()
Encodes one complete internal buffer of data.
Definition base64.hpp:225
void encode(size_t size)
Encodes partial internal buffer of data.
Definition base64.hpp:238
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition base64.hpp:196
‍UTF-8 byte-order-mark
Definition stream.hpp:79
bool ok() const
Returns true if the stream state is clean i.e. previous operation was succesful.
Definition stream.hpp:175
state_t state() const
Returns stream state after last operation.
Definition stream.hpp:170
size_t write_array(_In_reads_bytes_opt_(size *count) const void *array, size_t size, size_t count)
Writes an array of data to the stream.
Definition stream.hpp:400
Modifies data on the fly when reading from/writing to a source stream. Could also be used to modify r...
Definition stream.hpp:1022