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