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
14namespace stdex
15{
17 inline const char base64_enc_lookup[64] = {
18 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
19 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
20 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
21 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
22 };
23
24 inline const uint8_t base64_dec_lookup[256] = {
25 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
26 /* 0 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
27 /* 1 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
28 /* 2 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
29 /* 3 */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 64, 255, 255,
30 /* 4 */ 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
31 /* 5 */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
32 /* 6 */ 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
33 /* 7 */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255,
34 /* 8 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
35 /* 9 */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
36 /* A */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
37 /* B */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
38 /* C */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
39 /* D */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
40 /* E */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
41 /* F */ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
42 };
44
49 {
50 public:
54 base64_enc() noexcept : m_num(0)
55 {
56 m_buf[0] = 0;
57 m_buf[1] = 0;
58 m_buf[2] = 0;
59 }
60
69 template<class _Elem, class _Traits, class _Ax>
70 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)
71 {
72 _Assume_(data || !size);
73
74 // Preallocate output
75 out.reserve(out.size() + enc_size(size));
76
77 // Convert data character by character.
78 for (size_t i = 0;; i++) {
79 if (m_num >= 3) {
80 encode(out);
81 m_num = 0;
82 }
83
84 if (i >= size)
85 break;
86
87 m_buf[m_num++] = reinterpret_cast<const uint8_t*>(data)[i];
88 }
89
90 // If this is the last block, flush the buffer.
91 if (is_last && m_num) {
92 encode(out, m_num);
93 m_num = 0;
94 }
95 }
96
100 void clear() noexcept
101 {
102 m_num = 0;
103 }
104
112 size_t enc_size(_In_ size_t size) const noexcept
113 {
114 return ((m_num + size + 2)/3)*4;
115 }
116
117 protected:
121 template<class _Elem, class _Traits, class _Ax>
122 void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out)
123 {
124 out += base64_enc_lookup[ m_buf[0] >> 2 ];
125 out += base64_enc_lookup[((m_buf[0] << 4) | (m_buf[1] >> 4)) & 0x3f];
126 out += base64_enc_lookup[((m_buf[1] << 2) | (m_buf[2] >> 6)) & 0x3f];
127 out += base64_enc_lookup[ m_buf[2] & 0x3f];
128 }
129
133 template<class _Elem, class _Traits, class _Ax>
134 void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_ size_t size)
135 {
136 if (size > 0) {
137 out += base64_enc_lookup[m_buf[0] >> 2];
138 if (size > 1) {
139 out += base64_enc_lookup[((m_buf[0] << 4) | (m_buf[1] >> 4)) & 0x3f];
140 if (size > 2) {
141 out += base64_enc_lookup[((m_buf[1] << 2) | (m_buf[2] >> 6)) & 0x3f];
142 out += base64_enc_lookup[m_buf[2] & 0x3f];
143 } else {
144 out += base64_enc_lookup[(m_buf[1] << 2) & 0x3f];
145 out += '=';
146 }
147 } else {
148 out += base64_enc_lookup[(m_buf[0] << 4) & 0x3f];
149 out += '=';
150 out += '=';
151 }
152 } else {
153 out += '=';
154 out += '=';
155 out += '=';
156 out += '=';
157 }
158 }
159
160 protected:
161 uint8_t m_buf[3];
162 size_t m_num;
163 };
164
169 {
170 public:
171 base64_writer(_Inout_ stdex::stream::basic& source, _In_ size_t max_blocks = 19) :
173 m_max_blocks(max_blocks),
174 m_num_blocks(0)
175 {}
176
177 virtual ~base64_writer()
178 {
179 // Flush the buffer.
180 if (m_num) {
181 if (++m_num_blocks > m_max_blocks) {
182 *m_source << '\n';
183 m_num_blocks = 1;
184 }
185 encode(m_num);
186 }
187 }
188
189 virtual _Success_(return != 0) size_t write(
190 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
191 {
192 _Assume_(data || !length);
193 for (size_t i = 0;; i++) {
194 if (m_num >= 3) {
195 if (++m_num_blocks > m_max_blocks) {
196 *m_source << '\n';
197 m_num_blocks = 1;
198 }
199 encode();
200 if (!m_source->ok()) _Unlikely_ {
201 m_state = m_source->state();
202 return length - i;
203 }
204 m_num = 0;
205 }
206 if (i >= length) {
207 m_state = stdex::stream::state_t::ok;
208 return length;
209 }
210 m_buf[m_num++] = reinterpret_cast<const uint8_t*>(data)[i];
211 }
212 }
213
214 protected:
218 void encode()
219 {
220 char out[4];
221 out[0] = base64_enc_lookup[ m_buf[0] >> 2 ];
222 out[1] = base64_enc_lookup[((m_buf[0] << 4) | (m_buf[1] >> 4)) & 0x3f];
223 out[2] = base64_enc_lookup[((m_buf[1] << 2) | (m_buf[2] >> 6)) & 0x3f];
224 out[3] = base64_enc_lookup[ m_buf[2] & 0x3f];
225 m_source->write_array(out, sizeof(*out), _countof(out));
226 }
227
231 void encode(_In_ size_t size)
232 {
233 char out[4];
234 if (size > 0) {
235 out[0] = base64_enc_lookup[m_buf[0] >> 2];
236 if (size > 1) {
237 out[1] = base64_enc_lookup[((m_buf[0] << 4) | (m_buf[1] >> 4)) & 0x3f];
238 if (size > 2) {
239 out[2] = base64_enc_lookup[((m_buf[1] << 2) | (m_buf[2] >> 6)) & 0x3f];
240 out[3] = base64_enc_lookup[m_buf[2] & 0x3f];
241 } else {
242 out[2] = base64_enc_lookup[(m_buf[1] << 2) & 0x3f];
243 out[3] = '=';
244 }
245 } else {
246 out[1] = base64_enc_lookup[(m_buf[0] << 4) & 0x3f];
247 out[2] = '=';
248 out[3] = '=';
249 }
250 } else {
251 out[0] = '=';
252 out[1] = '=';
253 out[2] = '=';
254 out[3] = '=';
255 }
256 m_source->write_array(out, sizeof(*out), _countof(out));
257 }
258
259 protected:
260 size_t
261 m_max_blocks,
263 };
264
269 {
270 public:
274 base64_dec() noexcept : m_num(0)
275 {
276 m_buf[0] = 0;
277 m_buf[1] = 0;
278 m_buf[2] = 0;
279 m_buf[3] = 0;
280 }
281
290 template<class _Ty, class _Ax, class _Tchr>
291 void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
292 {
293 is_last = false;
294
295 // Trim data size to first terminator.
296 for (size_t k = 0; k < size; k++)
297 if (!data[k]) { size = k; break; }
298
299 // Preallocate output
300 out.reserve(out.size() + dec_size(size));
301
302 for (size_t i = 0;; i++) {
303 if (m_num >= 4) {
304 // Buffer full; decode it.
305 size_t nibbles = decode(out);
306 if (nibbles < 3) {
307 is_last = true;
308 break;
309 }
310 }
311
312 if (i >= size)
313 break;
314
315 int x = data[i];
316 if ((m_buf[m_num] = x < _countof(base64_dec_lookup) ? base64_dec_lookup[x] : 255) != 255)
317 m_num++;
318 }
319 }
320
324 void clear() noexcept
325 {
326 m_num = 0;
327 }
328
336 size_t dec_size(_In_ size_t size) const noexcept
337 {
338 return ((m_num + size + 3)/4)*3;
339 }
340
341 protected:
345 template<class _Ty, class _Ax>
346 size_t decode(_Inout_ std::vector<_Ty, _Ax> &out)
347 {
348 m_num = 0;
349 out.push_back((_Ty)(((m_buf[0] << 2) | (m_buf[1] >> 4)) & 0xff));
350 if (m_buf[2] < 64) {
351 out.push_back((_Ty)(((m_buf[1] << 4) | (m_buf[2] >> 2)) & 0xff));
352 if (m_buf[3] < 64) {
353 out.push_back((_Ty)(((m_buf[2] << 6) | m_buf[3]) & 0xff));
354 return 3;
355 } else
356 return 2;
357 } else
358 return 1;
359 }
360
361 protected:
362 uint8_t m_buf[4];
363 size_t m_num;
364 };
365
366#ifdef _MSC_VER
367#pragma warning(push)
368#pragma warning(disable: 26495)
369#endif
370
375 {
376 public:
377 base64_reader(_Inout_ stdex::stream::basic& source) :
379 m_temp_off(0),
380 m_temp_len(0)
381 {}
382
383#pragma warning(suppress: 6101) // See [1] below
384 virtual _Success_(return != 0 || length == 0) size_t read(
385 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
386 {
387 _Assume_(data || !length);
388 for (size_t to_read = length;;) {
389 if (m_temp_len >= to_read) {
390 memcpy(data, m_temp + m_temp_off, to_read);
391 m_temp_off += to_read;
392 m_temp_len -= to_read;
393 m_state = stdex::stream::state_t::ok;
394 return length;
395 }
396 if (m_temp_len) {
397 memcpy(data, m_temp + m_temp_off, m_temp_len);
398 reinterpret_cast<uint8_t*&>(data) += m_temp_len;
399 to_read -= m_temp_len;
400 m_temp_off = 0;
401 m_temp_len = 0;
402 }
403 // Read one Base64 block (4 chars)
404 while (m_num < 4) {
405 uint8_t x;
406 *m_source >> x;
407 if (!m_source->ok()) _Unlikely_ {
408 m_state = m_source->state();
409 return length - to_read; // [1] Code analysis misses `length - to_read` bytes were written to data in previous loop iterations.
410 }
411 if ((m_buf[m_num] = base64_dec_lookup[x]) != 255)
412 m_num++;
413 }
414 decode();
415 if (m_temp_len < 3 && to_read >= 3) {
416 // If Base64 indicates end of data, truncate read to hint the client, end of Base64 data has been reached.
417 memcpy(data, m_temp + m_temp_off, m_temp_len);
418 m_temp_off = 0;
419 m_temp_len = 0;
420 to_read -= m_temp_len;
421 m_state = stdex::stream::state_t::ok;
422 return length - to_read; // [1] Code analysis misses `length - to_read` bytes were written to data in previous loop iterations.
423 }
424 }
425 }
426
427 protected:
431 void decode()
432 {
433 m_num = 0;
434 m_temp_off = 0;
435 m_temp[0] = ((m_buf[0] << 2) | (m_buf[1] >> 4)) & 0xff;
436 if (m_buf[2] < 64) {
437 m_temp[1] = ((m_buf[1] << 4) | (m_buf[2] >> 2)) & 0xff;
438 if (m_buf[3] < 64) {
439 m_temp[2] = ((m_buf[2] << 6) | m_buf[3]) & 0xff;
440 m_temp_len = 3;
441 } else
442 m_temp_len = 2;
443 } else
444 m_temp_len = 1;
445 }
446
447 protected:
448 char m_temp[3];
449 size_t
452 };
453
454#ifdef _MSC_VER
455#pragma warning(pop)
456#endif
457}
Base64 decoding session.
Definition base64.hpp:269
size_t m_num
Number of bytes used in m_buf
Definition base64.hpp:363
base64_dec() noexcept
Constructs blank decoding session.
Definition base64.hpp:274
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:291
size_t dec_size(size_t size) const noexcept
Returns maximum decoded size.
Definition base64.hpp:336
size_t decode(std::vector< _Ty, _Ax > &out)
Decodes one complete internal buffer of data.
Definition base64.hpp:346
void clear() noexcept
Resets decoding session.
Definition base64.hpp:324
uint8_t m_buf[4]
Internal buffer.
Definition base64.hpp:362
Base64 encoding session.
Definition base64.hpp:49
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:70
void encode(std::basic_string< _Elem, _Traits, _Ax > &out)
Encodes one complete internal buffer of data.
Definition base64.hpp:122
size_t m_num
Number of bytes used in m_buf
Definition base64.hpp:162
uint8_t m_buf[3]
Internal buffer.
Definition base64.hpp:161
void encode(std::basic_string< _Elem, _Traits, _Ax > &out, size_t size)
Encodes partial internal buffer of data.
Definition base64.hpp:134
base64_enc() noexcept
Constructs blank encoding session.
Definition base64.hpp:54
void clear() noexcept
Resets encoding session.
Definition base64.hpp:100
size_t enc_size(size_t size) const noexcept
Returns maximum encoded size.
Definition base64.hpp:112
Converts from Base64 when reading from a stream.
Definition base64.hpp:375
void decode()
Decodes one complete internal buffer of data.
Definition base64.hpp:431
char m_temp[3]
Temporary buffer.
Definition base64.hpp:448
size_t m_temp_len
Number of bytes of data in m_temp
Definition base64.hpp:451
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:384
size_t m_temp_off
Index of data start in m_temp
Definition base64.hpp:450
Converts to Base64 when writing to a stream.
Definition base64.hpp:169
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:262
void encode()
Encodes one complete internal buffer of data.
Definition base64.hpp:218
void encode(size_t size)
Encodes partial internal buffer of data.
Definition base64.hpp:231
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:189
‍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