MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
base64 Namespace Reference

Namespaces

namespace  detail

Functions

template<class OutputBuffer, class InputIterator>
OutputBuffer decode_into (InputIterator begin, InputIterator end)
template<class OutputBuffer>
OutputBuffer decode_into (std::string_view base64Text)
template<class OutputBuffer, class InputIterator>
OutputBuffer encode_into (InputIterator begin, InputIterator end)
template<class OutputBuffer>
OutputBuffer encode_into (std::string_view data)
std::string from_base64 (std::string_view data)
std::string to_base64 (std::string_view data)

Function Documentation

◆ decode_into() [1/2]

template<class OutputBuffer, class InputIterator>
OutputBuffer base64::decode_into ( InputIterator begin,
InputIterator end )
inline

Definition at line 2864 of file Base64.hpp.

2865 {
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);
2870 return decode_into<OutputBuffer>(data);
2871 }
OutputBuffer decode_into(std::string_view base64Text)
Definition Base64.hpp:2748

◆ decode_into() [2/2]

template<class OutputBuffer>
OutputBuffer base64::decode_into ( std::string_view base64Text)
inline

Definition at line 2748 of file Base64.hpp.

2749 {
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())
2754 {
2755 return OutputBuffer();
2756 }
2757
2758 if ((base64Text.size() & 3) != 0)
2759 {
2760 throw std::runtime_error{"Invalid base64 encoded data - Size not divisible by 4"};
2761 }
2762
2763 size_t const numPadding = std::count(base64Text.rbegin(), base64Text.rbegin() + 4, '=');
2764 if (numPadding > 2)
2765 {
2766 throw std::runtime_error{"Invalid base64 encoded data - Found more than 2 padding signs"};
2767 }
2768
2769 size_t const decodedsize = (base64Text.size() * 3 >> 2) - numPadding;
2770 OutputBuffer decoded(decodedsize, '.');
2771
2772 uint8_t const* bytes = reinterpret_cast<uint8_t const*>(&base64Text[0]);
2773 char* currDecoding = reinterpret_cast<char*>(&decoded[0]);
2774
2775 for (size_t i = (base64Text.size() >> 2) - (numPadding != 0); i; --i)
2776 {
2777 uint8_t const t1 = *bytes++;
2778 uint8_t const t2 = *bytes++;
2779 uint8_t const t3 = *bytes++;
2780 uint8_t const t4 = *bytes++;
2781
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];
2786
2787 uint32_t const temp = d1 | d2 | d3 | d4;
2788
2789 if (temp >= detail::bad_char)
2790 {
2791 throw std::runtime_error{"Invalid base64 encoded data - Invalid character"};
2792 }
2793
2794 // Use bit_cast instead of union and type punning to avoid
2795 // undefined behaviour risk:
2796 // https://en.wikipedia.org/wiki/Type_punning#Use_of_union
2797 std::array<char, 4> const tempBytes = detail::bit_cast<std::array<char, 4>, uint32_t>(temp);
2798
2799 *currDecoding++ = tempBytes[detail::decidx0];
2800 *currDecoding++ = tempBytes[detail::decidx1];
2801 *currDecoding++ = tempBytes[detail::decidx2];
2802 }
2803
2804 switch (numPadding)
2805 {
2806 case 0:
2807 {
2808 break;
2809 }
2810 case 1:
2811 {
2812 uint8_t const t1 = *bytes++;
2813 uint8_t const t2 = *bytes++;
2814 uint8_t const t3 = *bytes++;
2815
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];
2819
2820 uint32_t const temp = d1 | d2 | d3;
2821
2822 if (temp >= detail::bad_char)
2823 {
2824 throw std::runtime_error{"Invalid base64 encoded data - Invalid character"};
2825 }
2826
2827 // Use bit_cast instead of union and type punning to avoid
2828 // undefined behaviour risk:
2829 // https://en.wikipedia.org/wiki/Type_punning#Use_of_union
2830 std::array<char, 4> const tempBytes = detail::bit_cast<std::array<char, 4>, uint32_t>(temp);
2831 *currDecoding++ = tempBytes[detail::decidx0];
2832 *currDecoding++ = tempBytes[detail::decidx1];
2833 break;
2834 }
2835 case 2:
2836 {
2837 uint8_t const t1 = *bytes++;
2838 uint8_t const t2 = *bytes++;
2839
2840 uint32_t const d1 = detail::decode_table_0[t1];
2841 uint32_t const d2 = detail::decode_table_1[t2];
2842
2843 uint32_t const temp = d1 | d2;
2844
2845 if (temp >= detail::bad_char)
2846 {
2847 throw std::runtime_error{"Invalid base64 encoded data - Invalid character"};
2848 }
2849
2850 std::array<char, 4> const tempBytes = detail::bit_cast<std::array<char, 4>, uint32_t>(temp);
2851 *currDecoding++ = tempBytes[detail::decidx0];
2852 break;
2853 }
2854 default:
2855 {
2856 throw std::runtime_error{"Invalid base64 encoded data - Invalid padding number"};
2857 }
2858 }
2859
2860 return decoded;
2861 }
std::enable_if_t< sizeof(To)==sizeof(From) &&std::is_trivially_copyable_v< From > &&std::is_trivially_copyable_v< To >, To > bit_cast(const From &src) noexcept
Definition Base64.hpp:49
constexpr uint32_t bad_char
Definition Base64.hpp:63

◆ encode_into() [1/2]

template<class OutputBuffer, class InputIterator>
OutputBuffer base64::encode_into ( InputIterator begin,
InputIterator end )
inline

Definition at line 2676 of file Base64.hpp.

2677 {
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;
2686 OutputBuffer encoded(encodedsize, detail::padding_char);
2687
2688 uint8_t const* bytes = reinterpret_cast<uint8_t const*>(&*begin);
2689 char* currEncoding = reinterpret_cast<char*>(&encoded[0]);
2690
2691 for (size_t i = binarytextsize / 3; i; --i)
2692 {
2693 uint8_t const t1 = *bytes++;
2694 uint8_t const t2 = *bytes++;
2695 uint8_t const t3 = *bytes++;
2696 *currEncoding++ = detail::encode_table_0[t1];
2697 *currEncoding++ = detail::encode_table_1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
2698 *currEncoding++ = detail::encode_table_1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
2699 *currEncoding++ = detail::encode_table_1[t3];
2700 }
2701
2702 switch (binarytextsize % 3)
2703 {
2704 case 0:
2705 {
2706 break;
2707 }
2708 case 1:
2709 {
2710 uint8_t const t1 = bytes[0];
2711 *currEncoding++ = detail::encode_table_0[t1];
2712 *currEncoding++ = detail::encode_table_1[(t1 & 0x03) << 4];
2713 // *currEncoding++ = detail::padding_char;
2714 // *currEncoding++ = detail::padding_char;
2715 break;
2716 }
2717 case 2:
2718 {
2719 uint8_t const t1 = bytes[0];
2720 uint8_t const t2 = bytes[1];
2721 *currEncoding++ = detail::encode_table_0[t1];
2722 *currEncoding++ = detail::encode_table_1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
2723 *currEncoding++ = detail::encode_table_1[(t2 & 0x0F) << 2];
2724 // *currEncoding++ = detail::padding_char;
2725 break;
2726 }
2727 default:
2728 {
2729 throw std::runtime_error{"Invalid base64 encoded data"};
2730 }
2731 }
2732
2733 return encoded;
2734 }
constexpr char padding_char
Definition Base64.hpp:62
std::array< char, 256 > constexpr encode_table_0
Definition Base64.hpp:2159
constexpr std::array< char, 256 > encode_table_1
Definition Base64.hpp:2416

◆ encode_into() [2/2]

template<class OutputBuffer>
OutputBuffer base64::encode_into ( std::string_view data)
inline

Definition at line 2737 of file Base64.hpp.

2738 {
2739 return encode_into<OutputBuffer>(std::begin(data), std::end(data));
2740 }
OutputBuffer encode_into(InputIterator begin, InputIterator end)
Definition Base64.hpp:2676

◆ from_base64()

std::string base64::from_base64 ( std::string_view data)
inline

Definition at line 2873 of file Base64.hpp.

2874 {
2875 return decode_into<std::string>(data);
2876 }

◆ to_base64()

std::string base64::to_base64 ( std::string_view data)
inline

Definition at line 2742 of file Base64.hpp.

2743 {
2744 return encode_into<std::string>(std::begin(data), std::end(data));
2745 }