8 #include <botan/base64.h>
9 #include <botan/internal/codec_base.h>
10 #include <botan/exceptn.h>
11 #include <botan/internal/rounding.h>
20 static inline std::string
name() noexcept
25 static inline size_t encoding_bytes_in() noexcept
27 return m_encoding_bytes_in;
29 static inline size_t encoding_bytes_out() noexcept
31 return m_encoding_bytes_out;
34 static inline size_t decoding_bytes_in() noexcept
36 return m_encoding_bytes_out;
38 static inline size_t decoding_bytes_out() noexcept
40 return m_encoding_bytes_in;
43 static inline size_t bits_consumed() noexcept
45 return m_encoding_bits;
47 static inline size_t remaining_bits_before_padding() noexcept
49 return m_remaining_bits_before_padding;
52 static inline size_t encode_max_output(
size_t input_length)
54 return (
round_up(input_length, m_encoding_bytes_in) / m_encoding_bytes_in) * m_encoding_bytes_out;
56 static inline size_t decode_max_output(
size_t input_length)
58 return (
round_up(input_length, m_encoding_bytes_out) * m_encoding_bytes_in) / m_encoding_bytes_out;
61 static void encode(
char out[8],
const uint8_t in[5]) noexcept
63 out[0] = Base64::m_bin_to_base64[(in[0] & 0xFC) >> 2];
64 out[1] = Base64::m_bin_to_base64[((in[0] & 0x03) << 4) | (in[1] >> 4)];
65 out[2] = Base64::m_bin_to_base64[((in[1] & 0x0F) << 2) | (in[2] >> 6)];
66 out[3] = Base64::m_bin_to_base64[in[2] & 0x3F];
69 static inline uint8_t lookup_binary_value(
char input) noexcept
71 return Base64::m_base64_to_bin[
static_cast<uint8_t
>(input)];
74 static inline bool check_bad_char(uint8_t bin,
char input,
bool ignore_ws)
80 else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws)))
82 std::string bad_char(1, input);
85 else if(bad_char ==
"\n")
87 else if(bad_char ==
"\r")
90 throw Invalid_Argument(
91 std::string(
"base64_decode: invalid base64 character '") +
97 static void decode(uint8_t* out_ptr,
const uint8_t decode_buf[4])
99 out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4);
100 out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2);
101 out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3];
104 static inline size_t bytes_to_remove(
size_t final_truncate)
106 return final_truncate;
110 static const size_t m_encoding_bits = 6;
111 static const size_t m_remaining_bits_before_padding = 8;
114 static const size_t m_encoding_bytes_in = 3;
115 static const size_t m_encoding_bytes_out = 4;
118 static const uint8_t m_bin_to_base64[64];
119 static const uint8_t m_base64_to_bin[256];
122 const uint8_t Base64::m_bin_to_base64[64] =
124 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
125 'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
126 'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
127 'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
128 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'
135 const uint8_t Base64::m_base64_to_bin[256] =
137 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80,
138 0x80, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
139 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
140 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
141 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F, 0x34, 0x35,
142 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF,
143 0xFF, 0x81, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04,
144 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
145 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16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F
169 size_t& input_consumed,
172 return base_encode(Base64(), out, in, input_length, input_consumed, final_inputs);
184 size_t& input_consumed,
188 return base_decode(Base64(), out, in, input_length, input_consumed, final_inputs, ignore_ws);
200 const std::string& input,
203 return base64_decode(output, input.data(), input.length(), ignore_ws);
210 return base_decode_to_vec<secure_vector<uint8_t>>(Base64(), input, input_length, ignore_ws);
221 return Base64::encode_max_output(input_length);
226 return Base64::decode_max_output(input_length);
size_t base_decode(Base &&base, uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws=true)
size_t base64_decode_max_output(size_t input_length)
size_t base64_encode_max_output(size_t input_length)
int(* final)(unsigned char *, CTX *)
std::string base_encode_to_string(Base &&base, const uint8_t input[], size_t input_length)
std::vector< T, secure_allocator< T >> secure_vector
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
size_t base_decode_full(Base &&base, uint8_t output[], const char input[], size_t input_length, bool ignore_ws)
size_t round_up(size_t n, size_t align_to)
size_t base_encode(Base &&base, char output[], const uint8_t input[], size_t input_length, size_t &input_consumed, bool final_inputs)