jaulib v1.3.0
Jau Support Library (C++, Java, ..)
base_codec.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef JAU_BASE_CODEC_HPP_
26#define JAU_BASE_CODEC_HPP_
27
28#include <string>
29#include <vector>
30#include <type_traits>
31
32#include <jau/int_types.hpp>
33
34/**
35 * Base codecs, i.e. changing the decimal or binary values' base for a different representation.
36 */
38
39 /** @defgroup Codec Codec
40 * Data Stream Encoder and Decoder
41 *
42 * Supported codecs:
43 * - jau::codec::base::alphabet enables variable integer base encode() and fixed binary base-64 encode64().
44 *
45 * @{
46 */
47
48 /**
49 * Base Alphabet Specification providing the alphabet for encode() and decode().
50 *
51 * Implementation delegates static code_point() function.
52 *
53 * @see encode()
54 * @see decode()
55 */
56 class alphabet {
57 public:
58 typedef int (*code_point_func)(const char c) noexcept;
59
60 private:
61 std::string name_;
62 int base_;
63 std::string_view symbols_;
64 char padding64_;
66
67 public:
68 alphabet(std::string _name, int _base, std::string_view _symbols, char _padding64, code_point_func _cpf) noexcept
69 : name_(std::move(_name)), base_(_base), symbols_(_symbols), padding64_(_padding64), cpf(_cpf) {}
70
71 /** Human readable name for this alphabet instance. */
72 constexpr const std::string& name() const noexcept { return name_; }
73
74 /** The fixed base used for this alphabet. */
75 constexpr int base() const noexcept { return base_; }
76
77 /** The string of symbols of this alphabet. */
78 constexpr const std::string_view& symbols() const noexcept { return symbols_; }
79
80 /** Padding symbol for base <= 64 and block encoding only. May return zero for no padding. */
81 constexpr char padding64() const noexcept { return padding64_; }
82
83 /** Returns the code-point of the given character or -1 if not element of this alphabet. */
84 constexpr int code_point(const char c) const noexcept { return cpf(c); }
85
86 /** Retrieve the character at given code-point of this alphabet. */
87 constexpr char operator[]( size_t cp ) const noexcept { return symbols_[cp]; }
88
89 std::string to_string() const noexcept {
90 std::string res("alphabet[");
91 res.append(name());
92 res.append(", base <= "+std::to_string(base())+"]");
93 return res;
94 }
95 };
96
97 inline std::string to_string(const alphabet& v) noexcept { return v.to_string(); }
98
99 inline bool operator!=(const alphabet& lhs, const alphabet& rhs ) noexcept {
100 return lhs.base() != rhs.base() || lhs.name() != rhs.name() || lhs.symbols() != rhs.symbols();
101 }
102
103 inline bool operator==(const alphabet& lhs, const alphabet& rhs ) noexcept {
104 return !( lhs != rhs );
105 }
106
107 /**
108 * Safe canonical `base64` alphabet, without ASCII code-point sorting order.
109 *
110 * Representing the canonical `base64` [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648.html) *Base 64 Alphabet*
111 * including its code-point order `A` < `a` < `0` < `/`.
112 *
113 * - Value: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`
114 * - Padding: `=`
115 *
116 * ### Properties
117 * - Base 64
118 * - 7-bit ASCII
119 * - Code page 437 compatible
120 * - [`base64` alphabet](https://www.rfc-editor.org/rfc/rfc4648.html), identical order
121 * - Excludes quoting chars: "'$ and space
122 * - Not supporting ASCII code-point sorting.
123 * - Order: `A` < `a` < `0` < `/`
124 */
125 class base64_alphabet : public alphabet {
126 private:
127 static inline constexpr const std::string_view data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
128
129 static int s_code_point(const char c) noexcept {
130 if ('A' <= c && c <= 'Z') {
131 return c - 'A';
132 } else if ('a' <= c && c <= 'z') {
133 return c - 'a' + 26;
134 } else if ('0' <= c && c <= '9') {
135 return c - '0' + 52;
136 } else if ('+' == c) {
137 return 62;
138 } else if ('/' == c) {
139 return 63;
140 } else {
141 return -1;
142 }
143 }
144
145 public:
147 : alphabet("base64", 64, data, '=', s_code_point) {}
148 };
149
150 /**
151 * Safe canonical `base64url` alphabet, without ASCII code-point sorting order.
152 *
153 * Representing the canonical `base64url` [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648.html) `URL and Filename safe` *Base 64 Alphabet*
154 * including its code-point order `A` < `a` < `0` < `_`.
155 *
156 * - Value: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_`
157 * - Padding: `=`
158 *
159 * ### Properties
160 * - Base 64
161 * - 7-bit ASCII
162 * - Code page 437 compatible
163 * - [`base64url` alphabet](https://www.rfc-editor.org/rfc/rfc4648.html), identical order
164 * - Safe URL and filename use
165 * - Excludes forbidden [v]fat chars: `<>:"/\|?*`
166 * - Excludes quoting chars: "'$ and space
167 * - Not supporting ASCII code-point sorting.
168 * - Order: `A` < `a` < `0` < `_`
169 */
171 private:
172 static inline constexpr const std::string_view data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
173
174 static int s_code_point(const char c) noexcept {
175 if ('A' <= c && c <= 'Z') {
176 return c - 'A';
177 } else if ('a' <= c && c <= 'z') {
178 return c - 'a' + 26;
179 } else if ('0' <= c && c <= '9') {
180 return c - '0' + 52;
181 } else if ('-' == c) {
182 return 62;
183 } else if ('_' == c) {
184 return 63;
185 } else {
186 return -1;
187 }
188 }
189
190 public:
192 : alphabet("base64url", 64, data, '=', s_code_point) {}
193 };
194
195 /**
196 * Safe natural base 64 alphabet, both without ASCII code-point sorting order.
197 *
198 * Order is considered a natural extension of decimal symbols, i.e. `0` < `a` < `A` < `_`.
199 *
200 * - Value: `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_`
201 * - Padding: `=`
202 *
203 * ### Properties
204 * - Base 64
205 * - 7-bit ASCII
206 * - Code page 437 compatible
207 * - [`base64url` alphabet](https://www.rfc-editor.org/rfc/rfc4648.html), but different order
208 * - Safe URL and filename use
209 * - Excludes forbidden [v]fat chars: `<>:"/\|?*`
210 * - Excludes quoting chars: "'$ and space
211 * - Not supporting ASCII code-point sorting.
212 * - Order: `0` < `a` < `A` < `_`
213 */
215 private:
216 static inline constexpr const std::string_view data = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
217
218 static int s_code_point(const char c) noexcept {
219 if ('0' <= c && c <= '9') {
220 return c - '0';
221 } else if ('a' <= c && c <= 'z') {
222 return c - 'a' + 10;
223 } else if ('A' <= c && c <= 'Z') {
224 return c - 'A' + 36;
225 } else if ('-' == c) {
226 return 62;
227 } else if ('_' == c) {
228 return 63;
229 } else {
230 return -1;
231 }
232 }
233
234 public:
236 : alphabet("natural64", 64, data, '=', s_code_point) {}
237 };
238
239 /**
240 * Natural base 86 alphabet, without ASCII code-point sorting order.
241 *
242 * Order is considered a natural extension of decimal symbols, i.e. `0` < `a` < `A` < `_` < `~`
243 *
244 * - Value: `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_!#%&()+,/:;<=>?@[]^{}~`
245 * - Padding: none
246 *
247 * ### Properties
248 * - Base 86
249 * - 7-bit ASCII
250 * - Code page 437 compatible
251 * - Excludes quoting chars: "'$ and space
252 * - Not supporting ASCII code-point sorting.
253 * - Order: `0` < `a` < `A` < `_` < `~`
254 */
256 private:
257 static inline constexpr const std::string_view data = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_!#%&()+,/:;<=>?@[]^{}~";
258
259 static int s_code_point(const char c) noexcept {
260 if ('0' <= c && c <= '9') {
261 return c - '0';
262 } else if ('a' <= c && c <= 'z') {
263 return c - 'a' + 10;
264 } else if ('A' <= c && c <= 'Z') {
265 return c - 'A' + 36;
266 } else {
267 switch( c ) {
268 case '-': return 62;
269 case '_': return 63;
270 case '!': return 64;
271 case '#': return 65;
272 case '%': return 66;
273 case '&': return 67;
274 case '(': return 68;
275 case ')': return 69;
276 case '+': return 70;
277 case ',': return 71;
278 case '/': return 72;
279 case ':': return 73;
280 case ';': return 74;
281 case '<': return 75;
282 case '=': return 76;
283 case '>': return 77;
284 case '?': return 78;
285 case '@': return 79;
286 case '[': return 80;
287 case ']': return 81;
288 case '^': return 82;
289 case '{': return 83;
290 case '}': return 84;
291 case '~': return 85;
292 default: return -1;
293 }
294 }
295 }
296
297 public:
299 : alphabet("natural86", 86, data, 0, s_code_point) {}
300 };
301
302 /**
303 * Safe base 38 alphabet with ASCII code-point sorting order.
304 *
305 * - Value: `-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_`
306 * - Padding: `=`
307 *
308 * ### Properties
309 * - Base 38
310 * - 7-bit ASCII
311 * - Code page 437 compatible
312 * - Safe URL and filename use
313 * - Excludes forbidden [v]fat chars: `<>:"/\|?*`
314 * - Only using upper-case letters for unique filename under vfat
315 * - Excludes quoting chars: "'$ and space
316 * - Supporting ASCII code-point sorting.
317 * - Order: `-` < `0` < `A` < `a` < `z`
318 */
319 class ascii38_alphabet : public alphabet {
320 private:
321 static inline constexpr const std::string_view data = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
322
323 static int s_code_point(const char c) noexcept {
324 if ('0' <= c && c <= '9') {
325 return c - '0' + 1;
326 } else if ('A' <= c && c <= 'Z') {
327 return c - 'A' + 11;
328 } else if ('-' == c) {
329 return 0;
330 } else if ('_' == c) {
331 return 37;
332 } else {
333 return -1;
334 }
335 }
336
337 public:
339 : alphabet("ascii38", 38, data, '=', s_code_point) {}
340 };
341
342 /**
343 * Safe base 64 alphabet with ASCII code-point sorting order.
344 *
345 * - Value: `-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz`
346 * - Padding: `=`
347 *
348 * ### Properties
349 * - Base 64
350 * - 7-bit ASCII
351 * - Code page 437 compatible
352 * - [`base64url` alphabet](https://www.rfc-editor.org/rfc/rfc4648.html), but different order
353 * - Safe URL and filename use
354 * - Excludes forbidden [v]fat chars: `<>:"/\|?*`
355 * - Excludes quoting chars: "'$ and space
356 * - Supporting ASCII code-point sorting.
357 * - Order: `-` < `0` < `A` < `a` < `z`
358 */
359 class ascii64_alphabet : public alphabet {
360 private:
361 static inline constexpr const std::string_view data = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
362
363 static int s_code_point(const char c) noexcept {
364 if ('0' <= c && c <= '9') {
365 return c - '0' + 1;
366 } else if ('A' <= c && c <= 'Z') {
367 return c - 'A' + 11;
368 } else if ('a' <= c && c <= 'z') {
369 return c - 'a' + 38;
370 } else if ('-' == c) {
371 return 0;
372 } else if ('_' == c) {
373 return 37;
374 } else {
375 return -1;
376 }
377 }
378
379 public:
381 : alphabet("ascii64", 64, data, '=', s_code_point) {}
382 };
383
384 /**
385 * Base 86 alphabet with ASCII code-point sorting order.
386 *
387 * - Value: `!#%&()+,-/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{}~`
388 * - Padding: None
389 *
390 * ### Properties
391 * - Base 86
392 * - 7-bit ASCII
393 * - Code page 437 compatible
394 * - Excludes quoting chars: "'$ and space
395 * - Supporting ASCII code-point sorting.
396 * - Order: `!` < `0` < `:` < `A` < `[` < `a` < `{` < `~`
397 */
398 class ascii86_alphabet : public alphabet {
399 private:
400 static inline constexpr const std::string_view data = "!#%&()+,-/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{}~";
401
402 static int s_code_point(const char c) noexcept {
403 if ('0' <= c && c <= '9') {
404 return c - '0' + 10;
405 } else if ('A' <= c && c <= 'Z') {
406 return c - 'A' + 27;
407 } else if ('a' <= c && c <= 'z') {
408 return c - 'a' + 57;
409 } else {
410 switch( c ) {
411 case '!': return 0;
412 case '#': return 1;
413 case '%': return 2;
414 case '&': return 3;
415 case '(': return 4;
416 case ')': return 5;
417 case '+': return 6;
418 case ',': return 7;
419 case '-': return 8;
420 case '/': return 9;
421
422 case ':': return 20;
423 case ';': return 21;
424 case '<': return 22;
425 case '=': return 23;
426 case '>': return 24;
427 case '?': return 25;
428 case '@': return 26;
429
430 case '[': return 53;
431 case ']': return 54;
432 case '^': return 55;
433 case '_': return 56;
434
435 case '{': return 83;
436 case '}': return 84;
437 case '~': return 85;
438 default: return -1;
439 }
440 }
441 }
442
443 public:
445 : alphabet("ascii86", 86, data, 0, s_code_point) {}
446 };
447
448 /**
449 * Encodes a given positive decimal number to a symbolic string representing a given alphabet and its base.
450 *
451 * Besides using a custom alphabet, the following build-in alphabets are provided
452 * - jau::codec::base::base64_alphabet
453 * - jau::codec::base::base64url_alphabet
454 * - jau::codec::base::natural86_alphabet
455 * - jau::codec::base::ascii64_alphabet
456 * - jau::codec::base::ascii86_alphabet
457 *
458 * @param num a positive decimal number
459 * @param aspec the used alphabet specification
460 * @param min_width minimum width of the encoded string, encoded zero is used for padding
461 * @return the encoded string or an empty string if base exceeds alphabet::max_base() or invalid arguments
462 *
463 * @see encodeBase()
464 * @see decodeBase()
465 */
466 std::string encode(int num, const alphabet& aspec, const unsigned int min_width=0) noexcept;
467
468 /**
469 * Encodes a given positive decimal number to a symbolic string representing a given alphabet and its base.
470 *
471 * Besides using a custom alphabet, the following build-in alphabets are provided
472 * - jau::codec::base::base64_alphabet
473 * - jau::codec::base::base64url_alphabet
474 * - jau::codec::base::natural86_alphabet
475 * - jau::codec::base::ascii64_alphabet
476 * - jau::codec::base::ascii86_alphabet
477 *
478 * @param num a positive decimal number
479 * @param aspec the used alphabet specification
480 * @param min_width minimum width of the encoded string, encoded zero is used for padding
481 * @return the encoded string or an empty string if base exceeds alphabet::max_base() or invalid arguments
482 *
483 * @see encodeBase()
484 * @see decodeBase()
485 */
486 std::string encode(int64_t num, const alphabet& aspec, const unsigned int min_width=0) noexcept;
487
488 /**
489 * Decodes a given symbolic string representing a given alphabet and its base to a positive decimal number.
490 *
491 * Besides using a custom alphabet, the following build-in alphabets are provided
492 * - jau::codec::base::base64_alphabet
493 * - jau::codec::base::base64url_alphabet
494 * - jau::codec::base::natural86_alphabet
495 * - jau::codec::base::ascii64_alphabet
496 * - jau::codec::base::ascii86_alphabet
497 *
498 * @param str an encoded string
499 * @param aspec the used alphabet specification
500 * @return the decoded decimal value or -1 if base exceeds alphabet::max_base(), unknown code-point or invalid arguments
501 *
502 * @see encodeBase()
503 */
504 int64_t decode(const std::string_view& str, const alphabet& aspec) noexcept;
505
506 /**
507 * Encodes given octets using the given alphabet and fixed base 64 encoding
508 * according to `base64` [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648.html).
509 *
510 * An error only occurs if in_len > 0 and resulting encoded string is empty.
511 *
512 * @param in_octets pointer to octets start
513 * @param in_len length of octets in bytes
514 * @param aspec the used base 64 alphabet specification
515 * @return the encoded string, empty if base exceeds alphabet::max_base() or invalid arguments
516 */
517 std::string encode64(const void* in_octets, size_t in_len, const alphabet& aspec) noexcept;
518
519 /**
520 * Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets
521 * according to `base64` [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648.html).
522 *
523 * An error only occurs if the encoded string length > 0 and resulting decoded octets size is empty.
524 *
525 * @param str encoded string
526 * @param aspec the used base 64 alphabet specification
527 * @return the decoded octets, empty if base exceeds alphabet::max_base(), unknown code-point or invalid arguments
528 */
529 std::vector<uint8_t> decode64(const std::string_view& str, const alphabet& aspec) noexcept;
530
531 /**
532 * Inserts a line feed (LF) character `\n` (ASCII 0x0a) after every period of characters.
533 *
534 * @param str the input string of characters, which will be mutated.
535 * @param period period of characters after which one LF will be inserted.
536 * @return count of inserted LF characters
537 */
538 size_t insert_lf(std::string& str, const size_t period) noexcept;
539
540 /**
541 * Removes line feed character from str.
542 *
543 * @param str the input string of characters, which will be mutated.
544 * @return count of removed LF characters
545 */
546 size_t remove_lf(std::string& str) noexcept;
547
548 /**
549 * Encodes given octets using the given alphabet and fixed base 64 encoding
550 * according to `base64` [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648.html)
551 * and adds line-feeds every 64 characters as required for PEM.
552 *
553 * An error only occurs if in_len > 0 and resulting encoded string is empty.
554 *
555 * @param in_octets pointer to octets start
556 * @param in_len length of octets in bytes
557 * @param aspec the used base 64 alphabet specification
558 * @return the encoded string, empty if base exceeds alphabet::max_base() or invalid arguments
559 */
560 inline std::string encode64_pem(const void* in_octets, size_t in_len, const alphabet& aspec) noexcept {
561 std::string e = encode64(in_octets, in_len, aspec);
562 (void)insert_lf(e, 64);
563 return e;
564 }
565
566 /**
567 * Encodes given octets using the given alphabet and fixed base 64 encoding
568 * according to `base64` [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648.html)
569 * and adds line-feeds every 76 characters as required for MIME.
570 *
571 * An error only occurs if in_len > 0 and resulting encoded string is empty.
572 *
573 * @param in_octets pointer to octets start
574 * @param in_len length of octets in bytes
575 * @param aspec the used base 64 alphabet specification
576 * @return the encoded string, empty if base exceeds alphabet::max_base() or invalid arguments
577 */
578 inline std::string encode64_mime(const void* in_octets, size_t in_len, const alphabet& aspec) noexcept {
579 std::string e = encode64(in_octets, in_len, aspec);
580 (void)insert_lf(e, 76);
581 return e;
582 }
583
584 /**
585 * Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets
586 * according to `base64` [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648.html)
587 * and removes all linefeeds before decoding as required for PEM and MIME.
588 *
589 * An error only occurs if the encoded string length > 0 and resulting decoded octets size is empty.
590 *
591 * @param str and encoded string, will be copied
592 * @param aspec the used base 64 alphabet specification
593 * @return the decoded octets, empty if base exceeds alphabet::max_base(), unknown code-point or invalid arguments
594 */
595 inline std::vector<uint8_t> decode64_lf(const std::string_view& str, const alphabet& aspec) noexcept {
596 std::string e(str); // costly copy
597 (void)remove_lf(e);
598 return decode64(e, aspec);
599 }
600
601 /**
602 * Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets
603 * according to `base64` [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648.html)
604 * and removes all linefeeds before decoding as required for PEM and MIME.
605 *
606 * An error only occurs if the encoded string length > 0 and resulting decoded octets size is empty.
607 *
608 * @param str and encoded string, no copy, will be mutated
609 * @param aspec the used base 64 alphabet specification
610 * @return the decoded octets, empty if base exceeds alphabet::max_base(), unknown code-point or invalid arguments
611 */
612 inline std::vector<uint8_t> decode64_lf(std::string& str, const alphabet& aspec) noexcept {
613 (void)remove_lf(str);
614 return decode64(str, aspec);
615 }
616
617 /**@}*/
618
619} // namespace jau::codec::base
620
621/** \example test_intdecstring01.cpp
622 * This C++ unit test validates the jau::to_decstring implementation
623 */
624
625#endif /* JAU_BASE_CODEC_HPP_ */
Base Alphabet Specification providing the alphabet for encode() and decode().
Definition: base_codec.hpp:56
constexpr int base() const noexcept
The fixed base used for this alphabet.
Definition: base_codec.hpp:75
std::string to_string() const noexcept
Definition: base_codec.hpp:89
constexpr int code_point(const char c) const noexcept
Returns the code-point of the given character or -1 if not element of this alphabet.
Definition: base_codec.hpp:84
constexpr char operator[](size_t cp) const noexcept
Retrieve the character at given code-point of this alphabet.
Definition: base_codec.hpp:87
constexpr const std::string_view & symbols() const noexcept
The string of symbols of this alphabet.
Definition: base_codec.hpp:78
alphabet(std::string _name, int _base, std::string_view _symbols, char _padding64, code_point_func _cpf) noexcept
Definition: base_codec.hpp:68
constexpr char padding64() const noexcept
Padding symbol for base <= 64 and block encoding only.
Definition: base_codec.hpp:81
int(* code_point_func)(const char c) noexcept
Definition: base_codec.hpp:58
constexpr const std::string & name() const noexcept
Human readable name for this alphabet instance.
Definition: base_codec.hpp:72
Safe base 38 alphabet with ASCII code-point sorting order.
Definition: base_codec.hpp:319
Safe base 64 alphabet with ASCII code-point sorting order.
Definition: base_codec.hpp:359
Base 86 alphabet with ASCII code-point sorting order.
Definition: base_codec.hpp:398
Safe canonical base64 alphabet, without ASCII code-point sorting order.
Definition: base_codec.hpp:125
Safe canonical base64url alphabet, without ASCII code-point sorting order.
Definition: base_codec.hpp:170
Safe natural base 64 alphabet, both without ASCII code-point sorting order.
Definition: base_codec.hpp:214
Natural base 86 alphabet, without ASCII code-point sorting order.
Definition: base_codec.hpp:255
size_t insert_lf(std::string &str, const size_t period) noexcept
Inserts a line feed (LF) character \n (ASCII 0x0a) after every period of characters.
Definition: base_codec.cpp:212
std::string encode(int num, const alphabet &aspec, const unsigned int min_width=0) noexcept
Encodes a given positive decimal number to a symbolic string representing a given alphabet and its ba...
Definition: base_codec.cpp:33
std::string encode64_mime(const void *in_octets, size_t in_len, const alphabet &aspec) noexcept
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
Definition: base_codec.hpp:578
std::string encode64(const void *in_octets, size_t in_len, const alphabet &aspec) noexcept
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
Definition: base_codec.cpp:90
std::vector< uint8_t > decode64(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets accordi...
Definition: base_codec.cpp:141
std::string encode64_pem(const void *in_octets, size_t in_len, const alphabet &aspec) noexcept
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
Definition: base_codec.hpp:560
std::vector< uint8_t > decode64_lf(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets accordi...
Definition: base_codec.hpp:595
size_t remove_lf(std::string &str) noexcept
Removes line feed character from str.
Definition: base_codec.cpp:224
int64_t decode(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing a given alphabet and its base to a positive decimal numb...
Definition: base_codec.cpp:72
bool operator!=(const alphabet &lhs, const alphabet &rhs) noexcept
Definition: base_codec.hpp:99
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
bool operator==(const alphabet &lhs, const alphabet &rhs) noexcept
Definition: base_codec.hpp:103
Base codecs, i.e.
Definition: base_codec.hpp:37
STL namespace.