@@ -457,118 +457,6 @@ def _generate_enum_to_wstring_implementation(
457
457
)
458
458
459
459
460
- def _generate_base64_encode_definition () -> Stripped :
461
- """Generate the definition of a stringification of bytes as base64 wstring."""
462
- function_name = cpp_naming .function_name (Identifier ("base64_encode" ))
463
- return Stripped (
464
- f"""\
465
- /**
466
- * Encode the \\ p bytes with base64 to a std::wstring.
467
- *
468
- * \\ param bytes to be encoded
469
- * \\ return base64-encoding of \\ p bytes
470
- */
471
- std::wstring { function_name } (
472
- { I } const std::vector<std::uint8_t>& bytes
473
- );"""
474
- )
475
-
476
-
477
- def _generate_base64_encode_implementation () -> List [Stripped ]:
478
- """Generate the implementation of a stringification of bytes as base64 wstring."""
479
- function_name = cpp_naming .function_name (Identifier ("base64_encode" ))
480
-
481
- wchar_base64_table = cpp_naming .constant_name (Identifier ("wchar_base64_table" ))
482
-
483
- return [
484
- Stripped (
485
- """\
486
- // The following encoder has been adapted from Jouni Malinen <j@w1.fi> to work with
487
- // std::wstring. The original source code is available at:
488
- // https://web.mit.edu/freebsd/head/contrib/wpa/src/utils/base64.c"""
489
- ),
490
- Stripped (
491
- f"""\
492
- static const wchar_t { wchar_base64_table } [65](
493
- { I } L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
494
- );"""
495
- ),
496
- Stripped (
497
- f"""\
498
- std::wstring { function_name } (
499
- { I } const std::vector<std::uint8_t>& bytes
500
- ) {{
501
- { I } // See: https://cplusplus.com/reference/vector/vector/data/.
502
- { I } // The data is guaranteed to be a continuous block in memory.
503
- { I } const unsigned char* src(
504
- { II } bytes.data()
505
- { I } );
506
-
507
- { I } const std::size_t len = bytes.size();
508
-
509
- { I } // 3-byte blocks to 4-byte
510
- { I } const std::size_t olen = 4 * ((len + 2) / 3);
511
-
512
- { I } // Integer overflow?
513
- { I } if (olen < len) {{
514
- { II } throw std::invalid_argument(
515
- { III } common::Concat(
516
- { IIII } "The calculation of the output length overflowed. "
517
- { IIII } "The length was: ",
518
- { IIII } std::to_string(len),
519
- { IIII } ", but the output length was calculated as: ",
520
- { IIII } std::to_string(olen)
521
- { III } )
522
- { II } );
523
- { I } }}
524
-
525
- { I } std::wstring out_wstring;
526
- { I } out_wstring.resize(olen);
527
-
528
- { I } wchar_t* out(
529
- { II } static_cast<wchar_t*>(
530
- { III } &out_wstring[0]
531
- { II } )
532
- { I } );
533
-
534
- { I } const unsigned char* end = src + len;
535
-
536
- { I } const unsigned char* in = src;
537
- { I } wchar_t* pos = out;
538
-
539
- { I } while (end - in >= 3) {{
540
- { II } *pos++ = { wchar_base64_table } [in[0] >> 2];
541
- { II } *pos++ = { wchar_base64_table } [((in[0] & 0x03) << 4) | (in[1] >> 4)];
542
- { II } *pos++ = { wchar_base64_table } [((in[1] & 0x0f) << 2) | (in[2] >> 6)];
543
- { II } *pos++ = { wchar_base64_table } [in[2] & 0x3f];
544
- { II } in += 3;
545
- { I } }}
546
-
547
- { I } if (end - in) {{
548
- { II } *pos++ = { wchar_base64_table } [in[0] >> 2];
549
-
550
- { II } if (end - in == 1) {{
551
- { III } *pos++ = { wchar_base64_table } [(in[0] & 0x03) << 4];
552
- { III } *pos++ = L'=';
553
- { II } }} else {{
554
- { III } *pos++ = { wchar_base64_table } [
555
- { IIII } ((in[0] & 0x03) << 4) | (in[1] >> 4)
556
- { III } ];
557
- { III } *pos++ = { wchar_base64_table } [(in[1] & 0x0f) << 2];
558
- { II } }}
559
- { II } *pos++ = L'=';
560
- { I } }}
561
-
562
- { I } return out_wstring;
563
- }}"""
564
- ),
565
- ]
566
-
567
-
568
- # NOTE (mristin, 2023-07-12):
569
- # The SDK does not use base64-decoding *from* wide strings, so we omit that direction
570
- # here following YAGNI.
571
-
572
460
# fmt: off
573
461
@ensure (
574
462
lambda result :
@@ -622,7 +510,6 @@ def generate_header(
622
510
623
511
blocks .extend (
624
512
[
625
- _generate_base64_encode_definition (),
626
513
Stripped (
627
514
"""\
628
515
} // namespace wstringification
@@ -682,7 +569,6 @@ def generate_implementation(
682
569
683
570
blocks .extend (
684
571
[
685
- * _generate_base64_encode_implementation (),
686
572
cpp_common .generate_namespace_closing (namespace ),
687
573
cpp_common .WARNING ,
688
574
]
0 commit comments