stdex Additional custom or not Standard C++ covered algorithms

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