Botan 3.8.1
Crypto and TLS for C&
codec_base.h
Go to the documentation of this file.
1/*
2* Base Encoding and Decoding
3* (C) 2018 Erwan Chaussy
4* (C) 2018 Jack Lloyd
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_BASE_CODEC_H_
10#define BOTAN_BASE_CODEC_H_
11
12#include <botan/exceptn.h>
13#include <botan/mem_ops.h>
14#include <array>
15#include <string>
16#include <type_traits>
17
18namespace Botan {
19
20/**
21* Perform encoding using the base provided
22* @param base object giving access to the encodings specifications
23* @param output an array of at least base.encode_max_output bytes
24* @param input is some binary data
25* @param input_length length of input in bytes
26* @param input_consumed is an output parameter which says how many
27* bytes of input were actually consumed. If less than
28* input_length, then the range input[consumed:length]
29* should be passed in later along with more input.
30* @param final_inputs true iff this is the last input, in which case
31 padding chars will be applied if needed
32* @return number of bytes written to output
33*/
34template <class Base>
36 Base&& base, char output[], const uint8_t input[], size_t input_length, size_t& input_consumed, bool final_inputs) {
37 input_consumed = 0;
38
39 // TODO(Botan4) Check if we can use just base. or Base:: here instead
40 constexpr size_t encoding_bytes_in = std::remove_reference_t<Base>::encoding_bytes_in();
41 constexpr size_t encoding_bytes_out = std::remove_reference_t<Base>::encoding_bytes_out();
42
43 size_t input_remaining = input_length;
44 size_t output_produced = 0;
45
46 while(input_remaining >= encoding_bytes_in) {
47 base.encode(output + output_produced, input + input_consumed);
48
49 input_consumed += encoding_bytes_in;
50 output_produced += encoding_bytes_out;
51 input_remaining -= encoding_bytes_in;
52 }
53
54 if(final_inputs && input_remaining) {
55 std::array<uint8_t, encoding_bytes_in> remainder{};
56 for(size_t i = 0; i != input_remaining; ++i) {
57 remainder[i] = input[input_consumed + i];
58 }
59
60 base.encode(output + output_produced, remainder.data());
61
62 const size_t bits_consumed = base.bits_consumed();
63 const size_t remaining_bits_before_padding = base.remaining_bits_before_padding();
64
65 size_t empty_bits = 8 * (encoding_bytes_in - input_remaining);
66 size_t index = output_produced + encoding_bytes_out - 1;
67 while(empty_bits >= remaining_bits_before_padding) {
68 output[index--] = '=';
69 empty_bits -= bits_consumed;
70 }
71
72 input_consumed += input_remaining;
73 output_produced += encoding_bytes_out;
74 }
75
76 return output_produced;
77}
78
79template <typename Base>
80std::string base_encode_to_string(Base&& base, const uint8_t input[], size_t input_length) {
81 const size_t output_length = base.encode_max_output(input_length);
82 std::string output(output_length, 0);
83
84 size_t consumed = 0;
85 size_t produced = 0;
86
87 if(output_length > 0) {
88 produced = base_encode(base, &output.front(), input, input_length, consumed, true);
89 }
90
91 BOTAN_ASSERT_EQUAL(consumed, input_length, "Consumed the entire input");
92 BOTAN_ASSERT_EQUAL(produced, output.size(), "Produced expected size");
93
94 return output;
95}
96
97/**
98* Perform decoding using the base provided
99* @param base object giving access to the encodings specifications
100* @param output an array of at least Base::decode_max_output bytes
101* @param input some base input
102* @param input_length length of input in bytes
103* @param input_consumed is an output parameter which says how many
104* bytes of input were actually consumed. If less than
105* input_length, then the range input[consumed:length]
106* should be passed in later along with more input.
107* @param final_inputs true iff this is the last input, in which case
108 padding is allowed
109* @param ignore_ws ignore whitespace on input; if false, throw an
110 exception if whitespace is encountered
111* @return number of bytes written to output
112*/
113template <typename Base>
114size_t base_decode(Base&& base,
115 uint8_t output[],
116 const char input[],
117 size_t input_length,
118 size_t& input_consumed,
119 bool final_inputs,
120 bool ignore_ws = true) {
121 // TODO(Botan4) Check if we can use just base. or Base:: here instead
122 constexpr size_t decoding_bytes_in = std::remove_reference_t<Base>::decoding_bytes_in();
123 constexpr size_t decoding_bytes_out = std::remove_reference_t<Base>::decoding_bytes_out();
124
125 uint8_t* out_ptr = output;
126 std::array<uint8_t, decoding_bytes_in> decode_buf{};
127 size_t decode_buf_pos = 0;
128 size_t final_truncate = 0;
129
130 clear_mem(output, base.decode_max_output(input_length));
131
132 for(size_t i = 0; i != input_length; ++i) {
133 const uint8_t bin = base.lookup_binary_value(input[i]);
134
135 // This call might throw Invalid_Argument
136 if(base.check_bad_char(bin, input[i], ignore_ws)) {
137 decode_buf[decode_buf_pos] = bin;
138 ++decode_buf_pos;
139 }
140
141 /*
142 * If we're at the end of the input, pad with 0s and truncate
143 */
144 if(final_inputs && (i == input_length - 1)) {
145 if(decode_buf_pos) {
146 for(size_t j = decode_buf_pos; j < decoding_bytes_in; ++j) {
147 decode_buf[j] = 0;
148 }
149
150 final_truncate = decoding_bytes_in - decode_buf_pos;
151 decode_buf_pos = decoding_bytes_in;
152 }
153 }
154
155 if(decode_buf_pos == decoding_bytes_in) {
156 base.decode(out_ptr, decode_buf.data());
157
158 out_ptr += decoding_bytes_out;
159 decode_buf_pos = 0;
160 input_consumed = i + 1;
161 }
162 }
163
164 while(input_consumed < input_length && base.lookup_binary_value(input[input_consumed]) == 0x80) {
165 ++input_consumed;
166 }
167
168 size_t written = (out_ptr - output) - base.bytes_to_remove(final_truncate);
169
170 return written;
171}
172
173template <typename Base>
174size_t base_decode_full(Base&& base, uint8_t output[], const char input[], size_t input_length, bool ignore_ws) {
175 size_t consumed = 0;
176 const size_t written = base_decode(base, output, input, input_length, consumed, true, ignore_ws);
177
178 if(consumed != input_length) {
179 throw Invalid_Argument(base.name() + " decoding failed, input did not have full bytes");
180 }
181
182 return written;
183}
184
185template <typename Vector, typename Base>
186Vector base_decode_to_vec(Base&& base, const char input[], size_t input_length, bool ignore_ws) {
187 const size_t output_length = base.decode_max_output(input_length);
188 Vector bin(output_length);
189
190 const size_t written = base_decode_full(base, bin.data(), input, input_length, ignore_ws);
191
192 bin.resize(written);
193 return bin;
194}
195
196} // namespace Botan
197
198#endif
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition assert.h:70
constexpr auto out_ptr(T &outptr) noexcept
Definition stl_util.h:421
Vector base_decode_to_vec(Base &&base, const char input[], size_t input_length, bool ignore_ws)
Definition codec_base.h:186
size_t base_encode(Base &&base, char output[], const uint8_t input[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition codec_base.h:35
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)
Definition codec_base.h:114
std::string base_encode_to_string(Base &&base, const uint8_t input[], size_t input_length)
Definition codec_base.h:80
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:123
size_t base_decode_full(Base &&base, uint8_t output[], const char input[], size_t input_length, bool ignore_ws)
Definition codec_base.h:174