forked from awslabs/aws-c-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.c
334 lines (257 loc) · 11.2 KB
/
encoding.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <aws/common/encoding.h>
#include <ctype.h>
#include <assert.h>
static const uint8_t *hex_chars = (const uint8_t *)"0123456789abcdef";
static const uint8_t base64_sentinal_value = 0xff;
static const uint8_t base64_encoding_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/* in this table, 0xDD is an invalid decoded value, if you have to do byte counting for any reason, there's 16 bytes
* per row. */
static uint8_t base64_decoding_table[256] = {
64, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 62, 0xDD, 0xDD, 0xDD, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xDD, 0xDD, 0xDD, 255, 0xDD, 0xDD,
0xDD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD };
int aws_hex_compute_encoded_len(size_t to_encode_len, size_t *encoded_length) {
assert(encoded_length);
size_t temp = (to_encode_len << 1) + 1;
if (AWS_UNLIKELY(temp < to_encode_len)) {
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
}
*encoded_length = temp;
return AWS_OP_SUCCESS;
}
int aws_hex_encode(const struct aws_byte_buf *AWS_RESTRICT to_encode, struct aws_byte_buf *AWS_RESTRICT output) {
assert(to_encode->buffer);
assert(output->buffer);
size_t encoded_len = 0;
if (AWS_UNLIKELY(aws_hex_compute_encoded_len(to_encode->len, &encoded_len))) {
return AWS_OP_ERR;
}
if (AWS_UNLIKELY(output->len < encoded_len)) {
return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
}
size_t written = 0;
for (size_t i = 0; i < to_encode->len; ++i) {
output->buffer[written++] = hex_chars[to_encode->buffer[i] >> 4 & 0x0f];
output->buffer[written++] = hex_chars[to_encode->buffer[i] & 0x0f];
}
output->buffer[written] = '\0';
return AWS_OP_SUCCESS;
}
static int hex_decode_char_to_int(char character, uint8_t *int_val) {
if (character >= 'a' && character <= 'f') {
*int_val = (uint8_t)(10 + (character - 'a'));
return 0;
}
else if (character >= 'A' && character <= 'F') {
*int_val = (uint8_t)(10 + (character - 'A'));
return 0;
}
else if (character >= '0' && character <= '9') {
*int_val = (uint8_t)(character - '0');
return 0;
}
return AWS_OP_ERR;
}
int aws_hex_compute_decoded_len(size_t to_decode_len, size_t *decoded_len) {
assert(decoded_len);
size_t temp = (to_decode_len + 1);
if (AWS_UNLIKELY(temp < to_decode_len)) {
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
}
*decoded_len = temp >> 1;
return AWS_OP_SUCCESS;
}
int aws_hex_decode(const struct aws_byte_buf *AWS_RESTRICT to_decode, struct aws_byte_buf *AWS_RESTRICT output) {
assert(to_decode->buffer);
assert(output->buffer);
size_t decoded_length = 0;
if (AWS_UNLIKELY(aws_hex_compute_decoded_len(to_decode->len, &decoded_length))) {
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
}
if (AWS_UNLIKELY(output->len < decoded_length)) {
return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
}
size_t written = 0;
size_t i = 0;
uint8_t high_value = 0;
uint8_t low_value = 0;
/* if the buffer isn't even, prepend a 0 to the buffer. */
if (AWS_UNLIKELY(to_decode->len & 0x01)) {
i = 1;
if (hex_decode_char_to_int(to_decode->buffer[0], &low_value)) {
return aws_raise_error(AWS_ERROR_INVALID_HEX_STR);
}
output->buffer[written++] = low_value;
}
for (; i < to_decode->len; i += 2) {
if (AWS_UNLIKELY(hex_decode_char_to_int(to_decode->buffer[i], &high_value) ||
hex_decode_char_to_int(to_decode->buffer[i + 1], &low_value))) {
return aws_raise_error(AWS_ERROR_INVALID_HEX_STR);
}
uint8_t value = high_value << 4;
value |= low_value;
output->buffer[written++] = value;
}
return AWS_OP_SUCCESS;
}
int aws_base64_compute_encoded_len(size_t to_encode_len, size_t *encoded_len) {
assert(encoded_len);
size_t tmp = to_encode_len + 2;
if (AWS_UNLIKELY(tmp < to_encode_len)) {
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
}
tmp /= 3;
size_t overflow_check = tmp;
tmp = 4 * tmp + 1; /* plus one for the NULL terminator */
if (AWS_UNLIKELY(tmp < overflow_check)) {
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
}
*encoded_len = tmp;
return AWS_OP_SUCCESS;
}
int aws_base64_compute_decoded_len(const char *input, size_t len, size_t *decoded_len) {
assert(input);
assert(decoded_len);
if (len == 0) {
*decoded_len = 0;
return AWS_OP_SUCCESS;
}
if (AWS_UNLIKELY(len & 0x03)) {
return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR);
}
size_t tmp = len * 3;
if (AWS_UNLIKELY(tmp < len)) {
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
}
size_t padding = 0;
if (len >= 2 && input[len - 1] == '=' && input[len - 2] == '=') { /*last two chars are = */
padding = 2;
}
else if (input[len - 1] == '=') { /*last char is = */
padding = 1;
}
*decoded_len = (tmp / 4 - padding);
return AWS_OP_SUCCESS;
}
int aws_base64_encode(const struct aws_byte_buf *AWS_RESTRICT to_encode, struct aws_byte_buf *AWS_RESTRICT output) {
assert(to_encode->buffer);
assert(output->buffer);
size_t encoded_length = 0;
if (AWS_UNLIKELY(aws_base64_compute_encoded_len(to_encode->len, &encoded_length))) {
return AWS_OP_ERR;
}
if (AWS_UNLIKELY(output->len< encoded_length)) {
return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
}
size_t buffer_length = to_encode->len;
size_t block_count = (buffer_length + 2) / 3;
size_t remainder_count = (buffer_length % 3);
size_t str_index = 0;
for (size_t i = 0; i < to_encode->len; i += 3 )
{
uint32_t block = to_encode->buffer[ i ];
block <<= 8;
if (AWS_LIKELY(i + 1 < buffer_length))
{
block = block | to_encode->buffer[ i + 1 ];
}
block <<= 8;
if (AWS_LIKELY(i + 2 < to_encode->len))
{
block = block | to_encode->buffer[ i + 2 ];
}
output->buffer[str_index++] = base64_encoding_table[(block >> 18) & 0x3F];
output->buffer[str_index++] = base64_encoding_table[(block >> 12) & 0x3F];
output->buffer[str_index++] = base64_encoding_table[(block >> 6) & 0x3F];
output->buffer[str_index++] = base64_encoding_table[block & 0x3F];
}
if(remainder_count > 0)
{
output->buffer[block_count * 4 - 1] = '=';
if(remainder_count == 1)
{
output->buffer[block_count * 4 - 2] = '=';
}
}
/* it's a string add the null terminator. */
output->buffer[encoded_length - 1] = 0;
return AWS_OP_SUCCESS;
}
static inline int base64_get_decoded_value(char to_decode, uint8_t *value, int8_t allow_sentinal) {
uint8_t decode_value = base64_decoding_table[(size_t)to_decode];
if (decode_value != 0xDD && (decode_value != base64_sentinal_value || allow_sentinal)) {
*value = decode_value;
return AWS_OP_SUCCESS;
}
return AWS_OP_ERR;
}
int aws_base64_decode(const struct aws_byte_buf *AWS_RESTRICT to_decode, struct aws_byte_buf *AWS_RESTRICT output) {
size_t decoded_length = 0;
if (AWS_UNLIKELY(aws_base64_compute_decoded_len((char *)to_decode->buffer, to_decode->len, &decoded_length))) {
return AWS_OP_ERR;
}
if (output->len < decoded_length) {
return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
}
int64_t block_count = (int)to_decode->len / 4;
size_t string_index = 0;
uint8_t value1 = 0, value2 = 0, value3 = 0, value4 = 0;
int64_t buffer_index = 0;
for(int32_t i = 0; i < block_count - 1; ++i)
{
if (AWS_UNLIKELY(base64_get_decoded_value(to_decode->buffer[string_index++], &value1, 0) ||
base64_get_decoded_value(to_decode->buffer[string_index++], &value2, 0) ||
base64_get_decoded_value(to_decode->buffer[string_index++], &value3, 0) ||
base64_get_decoded_value(to_decode->buffer[string_index++], &value4, 0))) {
return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR);
}
buffer_index = i * 3;
output->buffer[buffer_index++] = (uint8_t)((value1 << 2) | ((value2 >> 4) & 0x03));
output->buffer[buffer_index++] = (uint8_t)(((value2 << 4) & 0xF0) | ((value3 >> 2) & 0x0F));
output->buffer[buffer_index] = (uint8_t)((value3 & 0x03) << 6 | value4);
}
buffer_index = (block_count - 1) * 3;
if (buffer_index >= 0) {
if(base64_get_decoded_value(to_decode->buffer[string_index++], &value1, 0) ||
base64_get_decoded_value(to_decode->buffer[string_index++], &value2, 0) ||
base64_get_decoded_value(to_decode->buffer[string_index++], &value3, 1) ||
base64_get_decoded_value(to_decode->buffer[string_index], &value4, 1)) {
return aws_raise_error(AWS_ERROR_INVALID_BASE64_STR);
}
output->buffer[buffer_index++] = (uint8_t) ((value1 << 2) | ((value2 >> 4) & 0x03));
if (value3 != base64_sentinal_value) {
output->buffer[buffer_index++] = (uint8_t) (((value2 << 4) & 0xF0) | ((value3 >> 2) & 0x0F));
if (value4 != base64_sentinal_value) {
output->buffer[buffer_index] = (uint8_t) ((value3 & 0x03) << 6 | value4);
}
}
}
return AWS_OP_SUCCESS;
}