35 Base&& base,
char output[],
const uint8_t input[],
size_t input_length,
size_t& input_consumed,
bool final_inputs) {
38 const size_t encoding_bytes_in = base.encoding_bytes_in();
39 const size_t encoding_bytes_out = base.encoding_bytes_out();
41 size_t input_remaining = input_length;
42 size_t output_produced = 0;
44 while(input_remaining >= encoding_bytes_in) {
45 base.encode(output + output_produced, input + input_consumed);
47 input_consumed += encoding_bytes_in;
48 output_produced += encoding_bytes_out;
49 input_remaining -= encoding_bytes_in;
52 if(final_inputs && input_remaining) {
53 std::vector<uint8_t> remainder(encoding_bytes_in, 0);
54 for(
size_t i = 0; i != input_remaining; ++i) {
55 remainder[i] = input[input_consumed + i];
58 base.encode(output + output_produced, remainder.data());
60 const size_t bits_consumed = base.bits_consumed();
61 const size_t remaining_bits_before_padding = base.remaining_bits_before_padding();
63 size_t empty_bits = 8 * (encoding_bytes_in - input_remaining);
64 size_t index = output_produced + encoding_bytes_out - 1;
65 while(empty_bits >= remaining_bits_before_padding) {
66 output[index--] =
'=';
67 empty_bits -= bits_consumed;
70 input_consumed += input_remaining;
71 output_produced += encoding_bytes_out;
74 return output_produced;
79 const size_t output_length = base.encode_max_output(input_length);
80 std::string output(output_length, 0);
85 if(output_length > 0) {
86 produced =
base_encode(base, &output.front(), input, input_length, consumed,
true);
116 size_t& input_consumed,
118 bool ignore_ws =
true) {
119 const size_t decoding_bytes_in = base.decoding_bytes_in();
120 const size_t decoding_bytes_out = base.decoding_bytes_out();
123 std::vector<uint8_t> decode_buf(decoding_bytes_in, 0);
124 size_t decode_buf_pos = 0;
125 size_t final_truncate = 0;
127 clear_mem(output, base.decode_max_output(input_length));
129 for(
size_t i = 0; i != input_length; ++i) {
130 const uint8_t bin = base.lookup_binary_value(input[i]);
133 if(base.check_bad_char(bin, input[i], ignore_ws)) {
134 decode_buf[decode_buf_pos] = bin;
141 if(final_inputs && (i == input_length - 1)) {
143 for(
size_t j = decode_buf_pos; j < decoding_bytes_in; ++j) {
147 final_truncate = decoding_bytes_in - decode_buf_pos;
148 decode_buf_pos = decoding_bytes_in;
152 if(decode_buf_pos == decoding_bytes_in) {
153 base.decode(
out_ptr, decode_buf.data());
157 input_consumed = i + 1;
161 while(input_consumed < input_length && base.lookup_binary_value(input[input_consumed]) == 0x80) {
165 size_t written = (
out_ptr - output) - base.bytes_to_remove(final_truncate);
171size_t base_decode_full(Base&& base, uint8_t output[],
const char input[],
size_t input_length,
bool ignore_ws) {
173 const size_t written =
base_decode(base, output, input, input_length, consumed,
true, ignore_ws);
175 if(consumed != input_length) {
176 throw Invalid_Argument(base.name() +
" decoding failed, input did not have full bytes");
size_t base_encode(Base &&base, char output[], const uint8_t input[], size_t input_length, size_t &input_consumed, bool final_inputs)
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)