-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfc4648.hpp
657 lines (559 loc) · 22.5 KB
/
rfc4648.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#pragma once
#include <algorithm>
#include <bit>
#include <climits>
#include <concepts>
#include <cstring>
#include <iterator>
#include <memory>
#include <ranges>
#include <type_traits>
namespace bizwen
{
namespace detail
{
template <typename CharType> struct rfc4648_traits;
template <> struct rfc4648_traits<char>
{
static inline constexpr auto base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static inline constexpr auto base64_url = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
static inline constexpr auto base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
static inline constexpr auto base32_lower = "abcdefghijklmnopqrstuvwxyz234567=";
static inline constexpr auto base32_hex = "0123456789ABCDEFGHIJKLMNOPQRSTUV=";
static inline constexpr auto base32_hex_lower = "0123456789abcdefghijklmnopqrstuv=";
static inline constexpr auto base32_crockford = "0123456789ABCDEFGHJKMNPQRSTVWXYZ=";
static inline constexpr auto base32_crockford_lower = "0123456789abcdefghjkmnpqrstvwxyz=";
static inline constexpr auto base16 = base32_hex;
static inline constexpr auto base16_lower = base32_hex_lower;
};
template <> struct rfc4648_traits<wchar_t>
{
static inline constexpr auto base64 = L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static inline constexpr auto base64_url = L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
static inline constexpr auto base32 = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
static inline constexpr auto base32_lower = L"abcdefghijklmnopqrstuvwxyz234567=";
static inline constexpr auto base32_hex = L"0123456789ABCDEFGHIJKLMNOPQRSTUV=";
static inline constexpr auto base32_hex_lower = L"0123456789abcdefghijklmnopqrstuv=";
static inline constexpr auto base32_crockford = L"0123456789ABCDEFGHJKMNPQRSTVWXYZ=";
static inline constexpr auto base32_crockford_lower = L"0123456789abcdefghjkmnpqrstvwxyz=";
static inline constexpr auto base16 = base32_hex;
static inline constexpr auto base16_lower = base32_hex_lower;
};
template <> struct rfc4648_traits<char8_t>
{
static inline constexpr auto base64 = u8"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static inline constexpr auto base64_url = u8"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
static inline constexpr auto base32 = u8"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
static inline constexpr auto base32_lower = u8"abcdefghijklmnopqrstuvwxyz234567=";
static inline constexpr auto base32_hex = u8"0123456789ABCDEFGHIJKLMNOPQRSTUV=";
static inline constexpr auto base32_hex_lower = u8"0123456789abcdefghijklmnopqrstuv=";
static inline constexpr auto base32_crockford = u8"0123456789ABCDEFGHJKMNPQRSTVWXYZ=";
static inline constexpr auto base32_crockford_lower = u8"0123456789abcdefghjkmnpqrstvwxyz=";
static inline constexpr auto base16 = base32_hex;
static inline constexpr auto base16_lower = base32_hex_lower;
};
template <> struct rfc4648_traits<char16_t>
{
static inline constexpr auto base64 = u"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static inline constexpr auto base64_url = u"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
static inline constexpr auto base32 = u"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
static inline constexpr auto base32_lower = u"abcdefghijklmnopqrstuvwxyz234567=";
static inline constexpr auto base32_hex = u"0123456789ABCDEFGHIJKLMNOPQRSTUV=";
static inline constexpr auto base32_hex_lower = u"0123456789abcdefghijklmnopqrstuv=";
static inline constexpr auto base32_crockford = u"0123456789ABCDEFGHJKMNPQRSTVWXYZ=";
static inline constexpr auto base32_crockford_lower = u"0123456789abcdefghjkmnpqrstvwxyz=";
static inline constexpr auto base16 = base32_hex;
static inline constexpr auto base16_lower = base32_hex_lower;
};
template <> struct rfc4648_traits<char32_t>
{
static inline constexpr auto base64 = U"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static inline constexpr auto base64_url = U"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
static inline constexpr auto base32 = U"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
static inline constexpr auto base32_lower = U"abcdefghijklmnopqrstuvwxyz234567=";
static inline constexpr auto base32_hex = U"0123456789ABCDEFGHIJKLMNOPQRSTUV=";
static inline constexpr auto base32_hex_lower = U"0123456789abcdefghijklmnopqrstuv=";
static inline constexpr auto base32_crockford = U"0123456789ABCDEFGHJKMNPQRSTVWXYZ=";
static inline constexpr auto base32_crockford_lower = U"0123456789abcdefghjkmnpqrstvwxyz=";
static inline constexpr auto base16 = base32_hex;
static inline constexpr auto base16_lower = base32_hex_lower;
};
enum class rfc4648_kind : unsigned char
{
base64,
base64_url,
base32,
base32_lower,
base32_hex,
base32_hex_lower,
base32_crockford,
base32_crockford_lower,
base16,
base16_lower,
hex = base16,
hex_lower = base16_lower
};
using buf_reference = unsigned char (&)[4];
using sig_reference = unsigned char &;
template <typename T> inline constexpr unsigned char to_uc(T t) noexcept
{
// T is char, unsigned char or std::byte
return static_cast<unsigned char>(t);
}
template <rfc4648_kind Kind, typename Traits> inline consteval auto get_alphabet() noexcept
{
if constexpr (Kind == rfc4648_kind::base64)
return Traits::base64;
if constexpr (Kind == rfc4648_kind::base64_url)
return Traits::base64_url;
if constexpr (Kind == rfc4648_kind::base32)
return Traits::base32;
if constexpr (Kind == rfc4648_kind::base32_lower)
return Traits::base32_lower;
if constexpr (Kind == rfc4648_kind::base32_hex)
return Traits::base32_hex;
if constexpr (Kind == rfc4648_kind::base32_hex_lower)
return Traits::base32_hex_lower;
if constexpr (Kind == rfc4648_kind::base32_crockford)
return Traits::base32_crockford;
if constexpr (Kind == rfc4648_kind::base32_crockford_lower)
return Traits::base32_crockford_lower;
if constexpr (Kind == rfc4648_kind::base16)
return Traits::base16;
if constexpr (Kind == rfc4648_kind::base16_lower)
return Traits::base16_lower;
}
template <rfc4648_kind Kind> inline consteval auto get_family()
{
if constexpr (Kind == rfc4648_kind::base64 || Kind == rfc4648_kind::base64_url)
return rfc4648_kind::base64;
if constexpr (Kind == rfc4648_kind::base16 || Kind == rfc4648_kind::base16_lower)
return rfc4648_kind::base16;
else
return rfc4648_kind::base32;
}
template <std::size_t Count, typename T> inline constexpr auto chars_to_int_big_endian(T begin)
{
constexpr auto size = Count <= 4 ? 4 : 8;
using int32_type = std::conditional_t<sizeof(int) == 4, unsigned int, unsigned long>;
using data_type = std::conditional_t<size == 4, int32_type, unsigned long long>;
static_assert(Count < 9);
static_assert(CHAR_BIT == 8);
static_assert(std::endian::native == std::endian::big || std::endian::native == std::endian::little);
static_assert(sizeof(int32_type) == 4);
#if defined(__cpp_if_consteval) && (__cpp_if_consteval >= 202106L)
if consteval
#else
if (::std::is_constant_evaluated())
#endif
{
unsigned char buf[size]{};
for (std::size_t i{}; i < Count; ++i, ++begin)
buf[i] = to_uc(*begin);
if constexpr (std::endian::native == std::endian::little)
return std::byteswap(std::bit_cast<data_type>(buf));
else
return std::bit_cast<data_type>(buf);
}
else
{
data_type buf{};
std::memcpy(&buf, begin, Count);
if constexpr (std::endian::native == std::endian::little)
return std::byteswap(buf);
else
return buf;
}
}
template <typename A, typename I, typename O> inline constexpr void encode_impl_b64_6(A alphabet, I begin, O &first)
{
auto data = chars_to_int_big_endian<6>(begin);
*(first++) = alphabet[(data >> 58) & 63];
*(first++) = alphabet[(data >> 52) & 63];
*(first++) = alphabet[(data >> 46) & 63];
*(first++) = alphabet[(data >> 40) & 63];
*(first++) = alphabet[(data >> 34) & 63];
*(first++) = alphabet[(data >> 28) & 63];
*(first++) = alphabet[(data >> 22) & 63];
*(first++) = alphabet[(data >> 16) & 63];
}
template <typename A, typename I, typename O> inline constexpr void encode_impl_b64_3(A alphabet, I begin, O &first)
{
auto data = chars_to_int_big_endian<3>(begin);
*(first++) = alphabet[(data >> 26) & 63];
*(first++) = alphabet[(data >> 20) & 63];
*(first++) = alphabet[(data >> 14) & 63];
*(first++) = alphabet[(data >> 8) & 63];
}
template <bool Padding, typename A, typename I, typename O>
inline constexpr void encode_impl_b64_2(A alphabet, I begin, O &first)
{
auto data = chars_to_int_big_endian<2>(begin);
*(first++) = alphabet[(data >> 26) & 63];
*(first++) = alphabet[(data >> 20) & 63];
*(first++) = alphabet[(data >> 14) & 63];
if constexpr (Padding)
*(first++) = alphabet[64];
}
template <bool Padding, typename A, typename I, typename O>
inline constexpr void encode_impl_b64_1(A alphabet, I begin, O &first)
{
auto a = to_uc(*begin);
auto b = a >> 2; // XXXXXX
auto c = (a << 4) & 63; // XX0000
*(first++) = alphabet[b];
*(first++) = alphabet[c];
if constexpr (Padding)
{
*(first++) = alphabet[64]; // pad1
*(first++) = alphabet[64]; // pad2
}
}
template <bool Padding, typename A, typename I, typename O>
inline constexpr void encode_impl_b64(A alphabet, I begin, I end, O &first)
{
if constexpr (sizeof(std::size_t) == 8)
{
for (; end - begin > 5; begin += 6)
encode_impl_b64_6(alphabet, begin, first);
}
for (; end - begin > 2; begin += 3)
encode_impl_b64_3(alphabet, begin, first);
if (end - begin == 2)
encode_impl_b64_2<Padding>(alphabet, begin, first);
else if (end - begin) // == 1
encode_impl_b64_1<Padding>(alphabet, begin, first);
// == 0 fallthrough
}
template <typename A, typename I, typename O>
inline constexpr void encode_impl_b64_ctx(A alphabet, buf_reference buf, sig_reference sig, I begin, I end, O &first)
{
if (sig == 2) // 0, 1, 2
{
if (end - begin == 0)
return;
unsigned char buf[3];
buf[0] = buf[0];
buf[1] = buf[1];
buf[2] = to_uc(*(begin++));
encode_impl_b64_3(alphabet, std::begin(buf), first);
}
else if (sig) // == 1
{
if (end - begin == 0)
{
return;
}
else if (end - begin == 1)
{
buf[1] = to_uc(*(begin++));
sig = 2;
return;
}
else // >= 2
{
unsigned char lbuf[3];
lbuf[0] = buf[0];
lbuf[1] = to_uc(*(begin++));
lbuf[2] = to_uc(*(begin++));
encode_impl_b64_3(alphabet, std::begin(lbuf), first);
}
}
if constexpr (sizeof(std::size_t) == 8)
{
for (; end - begin > 5; begin += 6)
encode_impl_b64_6(alphabet, begin, first);
}
for (; end - begin > 3; begin += 3)
encode_impl_b64_3(alphabet, begin, first);
if (end - begin == 2)
{
buf[0] = to_uc(*(begin++));
buf[1] = to_uc(*(begin));
sig = 2;
}
else if (end - begin != 0) // == 1
{
buf[1] = to_uc(*(begin));
sig = 1;
}
else // NB: clear ctx
{
sig = 0;
}
}
template <bool Padding, typename A, typename O>
inline constexpr void encode_impl_b64_ctx(A alphabet, buf_reference buf, sig_reference sig, O &first)
{
if (sig == 2)
detail::encode_impl_b64_2<Padding>(alphabet, std::begin(buf), first);
else if (sig) // == 1
detail::encode_impl_b64_1<Padding>(alphabet, std::begin(buf), first);
// == 0 fallthrough
// clear ctx
sig = 0;
}
template <typename A, typename I, typename O> inline constexpr void encode_impl_b32_5(A alphabet, I begin, O &first)
{
auto data = chars_to_int_big_endian<5>(begin);
*(first++) = alphabet[(data >> 59) & 31];
*(first++) = alphabet[(data >> 54) & 31];
*(first++) = alphabet[(data >> 49) & 31];
*(first++) = alphabet[(data >> 44) & 31];
*(first++) = alphabet[(data >> 39) & 31];
*(first++) = alphabet[(data >> 34) & 31];
*(first++) = alphabet[(data >> 29) & 31];
*(first++) = alphabet[(data >> 24) & 31];
}
template <bool Padding, typename A, typename I, typename O>
inline constexpr void encode_impl_b32_4(A alphabet, I begin, O &first)
{
auto data = chars_to_int_big_endian<4>(begin);
*(first++) = alphabet[(data >> 27) & 31];
*(first++) = alphabet[(data >> 22) & 31];
*(first++) = alphabet[(data >> 17) & 31];
*(first++) = alphabet[(data >> 12) & 31];
*(first++) = alphabet[(data >> 7) & 31];
*(first++) = alphabet[(data >> 2) & 31];
// NB: left shift
*(first++) = alphabet[(data << 3) & 31];
if constexpr (Padding)
*(first++) = alphabet[32];
}
template <bool Padding, typename A, typename I, typename O>
inline constexpr void encode_impl_b32_3(A alphabet, I begin, O &first)
{
auto data = chars_to_int_big_endian<3>(begin);
*(first++) = alphabet[(data >> 27) & 31];
*(first++) = alphabet[(data >> 22) & 31];
*(first++) = alphabet[(data >> 17) & 31];
*(first++) = alphabet[(data >> 12) & 31];
*(first++) = alphabet[(data >> 7) & 31];
if constexpr (Padding)
{
*(first++) = alphabet[32];
*(first++) = alphabet[32];
*(first++) = alphabet[32];
}
}
template <bool Padding, typename A, typename I, typename O>
inline constexpr void encode_impl_b32_2(A alphabet, I begin, O &first)
{
auto data = chars_to_int_big_endian<2>(begin);
*(first++) = alphabet[(data >> 27) & 31];
*(first++) = alphabet[(data >> 22) & 31];
*(first++) = alphabet[(data >> 17) & 31];
*(first++) = alphabet[(data >> 12) & 31];
if constexpr (Padding)
{
*(first++) = alphabet[32];
*(first++) = alphabet[32];
*(first++) = alphabet[32];
*(first++) = alphabet[32];
}
}
template <bool Padding, typename A, typename I, typename O>
inline constexpr void encode_impl_b32_1(A alphabet, I begin, O &first)
{
auto a = to_uc(*(begin));
*(first++) = alphabet[a >> 3];
*(first++) = alphabet[(a << 2) & 31];
if constexpr (Padding)
{
*(first++) = alphabet[32];
*(first++) = alphabet[32];
*(first++) = alphabet[32];
*(first++) = alphabet[32];
*(first++) = alphabet[32];
*(first++) = alphabet[32];
}
}
template <bool Padding = true, typename A, typename I, typename O>
inline constexpr void encode_impl_b32(A alphabet, I begin, I end, O &first)
{
for (; end - begin > 4; begin += 5)
encode_impl_b32_5(alphabet, begin, first);
if (end - begin == 4)
encode_impl_b32_4<Padding>(alphabet, begin, first);
else if (end - begin == 3)
encode_impl_b32_3<Padding>(alphabet, begin, first);
else if (end - begin == 2)
encode_impl_b32_2<Padding>(alphabet, begin, first);
else if (end - begin) // == 1
encode_impl_b32_1<Padding>(alphabet, begin, first);
// == 0 fallthrough
}
template <typename A, typename I, typename O>
inline constexpr void encode_impl_b32_ctx(A alphabet, buf_reference buf, sig_reference sig, I begin, I end, O &first)
{
#if __has_cpp_attribute(assume)
[[assume(sig < 5)]];
[[assume(end - begin >= 0)]];
#endif
if (end - begin + sig < 5)
{
for (; begin != end; ++begin, ++sig)
buf[sig] = to_uc(*begin);
return;
}
if (sig)
{
unsigned char lbuf[5];
std::copy(std::begin(buf), std::begin(buf) + sig, std::begin(lbuf));
std::copy(begin, begin + (5 - sig), std::begin(lbuf) + sig);
begin += (5 - sig);
encode_impl_b32_5(alphabet, std::begin(lbuf), first);
}
for (; end - begin > 4; begin += 5)
encode_impl_b32_5(alphabet, begin, first);
sig = static_cast<unsigned char>(end - begin);
for (std::size_t i{}; i < sig; ++i, ++begin)
buf[i] = to_uc(*begin);
}
template <bool Padding, typename A, typename O>
inline constexpr void encode_impl_b32_ctx(A alphabet, buf_reference buf, sig_reference sig, O &first)
{
if (sig == 1)
encode_impl_b32_1<Padding>(alphabet, std::begin(buf), first);
else if (sig == 2)
encode_impl_b32_2<Padding>(alphabet, std::begin(buf), first);
else if (sig == 3)
encode_impl_b32_3<Padding>(alphabet, std::begin(buf), first);
else if (sig == 4)
encode_impl_b32_4<Padding>(alphabet, std::begin(buf), first);
sig = 0;
}
template <typename A, typename I, typename O>
inline constexpr void encode_impl_b16(A alphabet, I begin, I end, O &first)
{
if constexpr (sizeof(size_t) == 8)
{
for (; end - begin > 7; begin += 8)
{
auto data = chars_to_int_big_endian<8>(begin);
for (std::size_t i{}; i < 16; ++i)
*(first++) = alphabet[(data >> (64 - (i + 1) * 4)) & 15];
}
}
else // 32-bit machine
{
for (; end - begin > 3; begin += 4)
{
auto data = chars_to_int_big_endian<4>(begin);
for (std::size_t i{}; i < 8; ++i)
*(first++) = alphabet[(data >> (32 - (i + 1) * 4)) & 15];
}
}
for (; begin != end; ++begin)
{
auto data = to_uc(*begin);
*(first++) = alphabet[data >> 4];
*(first++) = alphabet[data & 15];
}
}
struct rfc4648_encode_fn;
class rfc4648_ctx
{
unsigned char sig_{}; // 0、1、2 for base64; 0 - 4 for base32
unsigned char buf_[4];
friend rfc4648_encode_fn;
};
struct rfc4648_encode_fn
{
template <rfc4648_kind Kind = rfc4648_kind::base64, bool Padding = true, typename In, typename Out>
#if defined(__cpp_static_call_operator) && __cpp_static_call_operator >= 202207L
static
#endif
inline constexpr Out
operator()(In begin, In end, Out first)
#if !defined(__cpp_static_call_operator) || __cpp_static_call_operator < 202207L
const
#endif
{
using in_char = std::remove_cvref_t<decltype(*begin)>;
using out_char = std::remove_cvref_t<decltype(*first)>;
using traits = detail::rfc4648_traits<out_char>;
static_assert(std::contiguous_iterator<In>);
static_assert(std::is_same_v<in_char, char> || std::is_same_v<in_char, unsigned char> ||
std::is_same_v<in_char, std::byte>);
auto begin_ptr = std::to_address(begin);
auto end_ptr = std::to_address(end);
if constexpr (detail::get_family<Kind>() == rfc4648_kind::base64)
detail::encode_impl_b64<Padding>(detail::get_alphabet<Kind, traits>(), begin_ptr, end_ptr, first);
if constexpr (detail::get_family<Kind>() == rfc4648_kind::base32)
detail::encode_impl_b32<Padding>(detail::get_alphabet<Kind, traits>(), begin_ptr, end_ptr, first);
if constexpr (detail::get_family<Kind>() == rfc4648_kind::base16)
detail::encode_impl_b16(detail::get_alphabet<Kind, traits>(), begin_ptr, end_ptr, first);
return first;
}
template <rfc4648_kind Kind = rfc4648_kind::base64, bool Padding = true, typename R, typename Out>
#if defined(__cpp_static_call_operator) && __cpp_static_call_operator >= 202207L
static
#endif
inline constexpr Out
operator()(R &&r, Out first)
#if !defined(__cpp_static_call_operator) || __cpp_static_call_operator < 202207L
const
#endif
{
return operator()<Kind, Padding>(r.begin(), r.end(), first);
}
// NB: don't need padding
template <rfc4648_kind Kind = rfc4648_kind::base64, typename In, typename Out>
#if defined(__cpp_static_call_operator) && __cpp_static_call_operator >= 202207L
static
#endif
inline constexpr Out
operator()(rfc4648_ctx &ctx, In begin, In end, Out first)
#if !defined(__cpp_static_call_operator) || __cpp_static_call_operator < 202207L
const
#endif
{
using in_char = std::remove_cvref_t<decltype(*begin)>;
using out_char = std::remove_cvref_t<decltype(*first)>;
using traits = detail::rfc4648_traits<out_char>;
static_assert(std::contiguous_iterator<In>);
static_assert(std::is_same_v<in_char, char> || std::is_same_v<in_char, unsigned char> ||
std::is_same_v<in_char, std::byte>);
auto begin_ptr = std::to_address(begin);
auto end_ptr = std::to_address(end);
if constexpr (detail::get_family<Kind>() == rfc4648_kind::base64)
detail::encode_impl_b64_ctx(detail::get_alphabet<Kind, traits>(), ctx.buf_, ctx.sig_, begin_ptr, end_ptr,
first);
if constexpr (detail::get_family<Kind>() == rfc4648_kind::base32)
detail::encode_impl_b32_ctx(detail::get_alphabet<Kind, traits>(), ctx.buf_, ctx.sig_, begin_ptr, end_ptr,
first);
if constexpr (detail::get_family<Kind>() == rfc4648_kind::base16)
detail::encode_impl_b16(detail::get_alphabet<Kind, traits>(), begin_ptr, end_ptr, first);
return first;
}
template <rfc4648_kind Kind = rfc4648_kind::base64, typename R, typename Out>
#if defined(__cpp_static_call_operator) && __cpp_static_call_operator >= 202207L
static
#endif
inline constexpr Out
operator()(rfc4648_ctx &ctx, R &&r, Out first)
#if !defined(__cpp_static_call_operator) || __cpp_static_call_operator < 202207L
const
#endif
{
return operator()<Kind>(ctx, r.begin(), r.end(), first);
}
template <rfc4648_kind Kind = rfc4648_kind::base64, bool Padding = true, typename Out>
inline constexpr Out operator()(rfc4648_ctx &ctx, Out first) const
{
using out_char = std::remove_cvref_t<decltype(*first)>;
using traits = detail::rfc4648_traits<out_char>;
if constexpr (detail::get_family<Kind>() == rfc4648_kind::base64)
detail::encode_impl_b64_ctx<Padding>(detail::get_alphabet<Kind, traits>(), ctx.buf_, ctx.sig_, first);
if constexpr (detail::get_family<Kind>() == rfc4648_kind::base32)
detail::encode_impl_b32_ctx<Padding>(detail::get_alphabet<Kind, traits>(), ctx.buf_, ctx.sig_, first);
// no effect when family is base16 and CHAR_BIT is 8*
return first;
}
};
} // namespace detail
using detail::rfc4648_ctx;
using detail::rfc4648_kind;
inline constexpr detail::rfc4648_encode_fn rfc4648_encode;
} // namespace bizwen