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