45#if defined(__cpp_lib_bit_cast)
48 template<
class To,
class From>
49 std::enable_if_t<
sizeof(To) ==
sizeof(From) && std::is_trivially_copyable_v<From> && std::is_trivially_copyable_v<To>, To>
bit_cast(
50 const From& src)
noexcept
52 static_assert(std::is_trivially_constructible_v<To>,
53 "This implementation additionally requires "
54 "destination type to be trivially constructible");
57 std::memcpy(&dst, &src,
sizeof(To));
63 constexpr inline uint32_t
bad_char{0x01FFFFFF};
65#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
66# if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || (defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN) || \
67 (defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN) || (defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN) || \
68 (defined(__sun) && defined(__SVR4) && defined(_BIG_ENDIAN)) || defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
69 defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__) || defined(_M_PPC)
70# define __BIG_ENDIAN__
71# elif (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \
72 (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) \
73 || (defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN) || (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN) || \
74 (defined(__sun) && defined(__SVR4) && defined(_LITTLE_ENDIAN)) || \
75 defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \
76 defined(_M_IX86) || defined(_M_X64) || defined(_M_IA64) || \
78# define __LITTLE_ENDIAN__
82#if !defined(__LITTLE_ENDIAN__) & !defined(__BIG_ENDIAN__)
83# error "UNKNOWN Platform / endianness. Configure endianness explicitly."
86#if defined(__LITTLE_ENDIAN__)
87 std::array<std::uint32_t, 256>
constexpr decode_table_0 = {0x01ffffff,
344 constexpr std::array<std::uint32_t, 256> decode_table_1 = {0x01ffffff,
601 constexpr std::array<std::uint32_t, 256> decode_table_2 = {0x01ffffff,
858 constexpr std::array<std::uint32_t, 256> decode_table_3 = {0x01ffffff,
1117 constexpr inline size_t decidx0{0};
1118 constexpr inline size_t decidx1{1};
1119 constexpr inline size_t decidx2{2};
1121#elif defined(__BIG_ENDIAN__)
1123 std::array<std::uint32_t, 256>
constexpr decode_table_0 = {0x01ffffff,
1380 constexpr std::array<std::uint32_t, 256> decode_table_1 = {0x01ffffff,
1637 constexpr std::array<std::uint32_t, 256> decode_table_2 = {0x01ffffff,
1894 constexpr std::array<std::uint32_t, 256> decode_table_3 = {0x01ffffff,
2153 constexpr inline size_t decidx0{1};
2154 constexpr inline size_t decidx1{2};
2155 constexpr inline size_t decidx2{3};
2676 inline OutputBuffer
encode_into(InputIterator begin, InputIterator end)
2678 typedef std::decay_t<
decltype(*begin)> input_value_type;
2679 static_assert(std::is_same_v<input_value_type, char> || std::is_same_v<input_value_type, signed char> ||
2680 std::is_same_v<input_value_type, unsigned char> || std::is_same_v<input_value_type, std::byte>);
2681 typedef typename OutputBuffer::value_type output_value_type;
2682 static_assert(std::is_same_v<output_value_type, char> || std::is_same_v<output_value_type, signed char> ||
2683 std::is_same_v<output_value_type, unsigned char> || std::is_same_v<output_value_type, std::byte>);
2684 size_t const binarytextsize = end - begin;
2685 size_t const encodedsize = (binarytextsize / 3 + (binarytextsize % 3 > 0)) << 2;
2688 uint8_t
const* bytes =
reinterpret_cast<uint8_t const*
>(&*begin);
2689 char* currEncoding =
reinterpret_cast<char*
>(&encoded[0]);
2691 for (
size_t i = binarytextsize / 3; i; --i)
2693 uint8_t
const t1 = *bytes++;
2694 uint8_t
const t2 = *bytes++;
2695 uint8_t
const t3 = *bytes++;
2702 switch (binarytextsize % 3)
2710 uint8_t
const t1 = bytes[0];
2719 uint8_t
const t1 = bytes[0];
2720 uint8_t
const t2 = bytes[1];
2729 throw std::runtime_error{
"Invalid base64 encoded data"};
2750 typedef typename OutputBuffer::value_type output_value_type;
2751 static_assert(std::is_same_v<output_value_type, char> || std::is_same_v<output_value_type, signed char> ||
2752 std::is_same_v<output_value_type, unsigned char> || std::is_same_v<output_value_type, std::byte>);
2753 if (base64Text.empty())
2755 return OutputBuffer();
2758 if ((base64Text.size() & 3) != 0)
2760 throw std::runtime_error{
"Invalid base64 encoded data - Size not divisible by 4"};
2763 size_t const numPadding = std::count(base64Text.rbegin(), base64Text.rbegin() + 4,
'=');
2766 throw std::runtime_error{
"Invalid base64 encoded data - Found more than 2 padding signs"};
2769 size_t const decodedsize = (base64Text.size() * 3 >> 2) - numPadding;
2770 OutputBuffer decoded(decodedsize,
'.');
2772 uint8_t
const* bytes =
reinterpret_cast<uint8_t const*
>(&base64Text[0]);
2773 char* currDecoding =
reinterpret_cast<char*
>(&decoded[0]);
2775 for (
size_t i = (base64Text.size() >> 2) - (numPadding != 0); i; --i)
2777 uint8_t
const t1 = *bytes++;
2778 uint8_t
const t2 = *bytes++;
2779 uint8_t
const t3 = *bytes++;
2780 uint8_t
const t4 = *bytes++;
2782 uint32_t
const d1 = detail::decode_table_0[t1];
2783 uint32_t
const d2 = detail::decode_table_1[t2];
2784 uint32_t
const d3 = detail::decode_table_2[t3];
2785 uint32_t
const d4 = detail::decode_table_3[t4];
2787 uint32_t
const temp = d1 | d2 | d3 | d4;
2791 throw std::runtime_error{
"Invalid base64 encoded data - Invalid character"};
2799 *currDecoding++ = tempBytes[detail::decidx0];
2800 *currDecoding++ = tempBytes[detail::decidx1];
2801 *currDecoding++ = tempBytes[detail::decidx2];
2812 uint8_t
const t1 = *bytes++;
2813 uint8_t
const t2 = *bytes++;
2814 uint8_t
const t3 = *bytes++;
2816 uint32_t
const d1 = detail::decode_table_0[t1];
2817 uint32_t
const d2 = detail::decode_table_1[t2];
2818 uint32_t
const d3 = detail::decode_table_2[t3];
2820 uint32_t
const temp = d1 | d2 | d3;
2824 throw std::runtime_error{
"Invalid base64 encoded data - Invalid character"};
2831 *currDecoding++ = tempBytes[detail::decidx0];
2832 *currDecoding++ = tempBytes[detail::decidx1];
2837 uint8_t
const t1 = *bytes++;
2838 uint8_t
const t2 = *bytes++;
2840 uint32_t
const d1 = detail::decode_table_0[t1];
2841 uint32_t
const d2 = detail::decode_table_1[t2];
2843 uint32_t
const temp = d1 | d2;
2847 throw std::runtime_error{
"Invalid base64 encoded data - Invalid character"};
2851 *currDecoding++ = tempBytes[detail::decidx0];
2856 throw std::runtime_error{
"Invalid base64 encoded data - Invalid padding number"};
2864 inline OutputBuffer
decode_into(InputIterator begin, InputIterator end)
2866 typedef std::decay_t<
decltype(*begin)> input_value_type;
2867 static_assert(std::is_same_v<input_value_type, char> || std::is_same_v<input_value_type, signed char> ||
2868 std::is_same_v<input_value_type, unsigned char> || std::is_same_v<input_value_type, std::byte>);
2869 std::string_view data(
reinterpret_cast<char const*
>(&*begin), end - begin);