20 #define PADDING_CHAR '=' 23 #define QUANTUM_SIZE 4 24 #define INPUT_GROUP_SIZE 3 27 #define UPPER_COUNT_PADDING 3 30 static const char base64_encode_table[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
34 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
35 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
36 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F,
37 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
38 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
39 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
40 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
41 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
42 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
43 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF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, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
73 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
84 DEBUG_ENTRY(
"const string_t *encoded_data = <%p>, mcl_uint8_t **decoded_data = <%p>, mcl_size_t *decoded_data_size = <%p>", encoded_data, decoded_data, decoded_data_size)
94 DEBUG_ENTRY(
"const string_t *encoded_data = <%p>, mcl_uint8_t **decoded_data = <%p>, mcl_size_t *decoded_data_size = <%p>", encoded_data, decoded_data, decoded_data_size)
104 DEBUG_ENTRY(
"const mcl_uint8_t *data = <%p>, mcl_size_t data_size = <%u>, string_t **encoded_data = <%p>", data, data_size, encoded_data)
114 DEBUG_ENTRY(
"const mcl_uint8_t *data = <%p>, mcl_size_t data_size = <%u>, string_t **encoded_data = <%p>", data, data_size, encoded_data)
124 DEBUG_ENTRY(
"const mcl_uint8_t *table = <%p>, const string_t *encoded_data = <%p>, mcl_uint8_t **decoded_data = <%p>, mcl_size_t *decoded_data_size = <%p>", table,
125 encoded_data, decoded_data, decoded_data_size)
127 char *encoded_buffer;
137 "Length of encoded data must be positive and multiples of 4");
140 *decoded_data_size = 0;
143 encoded_buffer = encoded_data->
buffer;
148 MCL_DEBUG(
"Found first padding symbol '=' at index = <%u>", index);
159 MCL_DEBUG(
"Number of padding chars = <%u>", padding);
166 MCL_DEBUG(
"Number of quantums (4 Byte groups) = <%u>", number_of_quantums);
170 MCL_DEBUG(
"Length of resulting string will be = <%u>", raw_length);
176 position = decode_buffer;
179 for (index = 0; index < number_of_quantums; index++)
181 MCL_DEBUG(
"Decoding quantum = <%u> at input data = <%s>", index, encoded_buffer);
183 "Decode of quantum failed!");
193 *decoded_data = decode_buffer;
194 *decoded_data_size = raw_length;
196 MCL_DEBUG(
"decode_buffer = <%p>", decode_buffer);
197 MCL_DEBUG(
"decoded_data_size = <%u>", *decoded_data_size);
205 DEBUG_ENTRY(
"const mcl_uint8_t *table = <%p>, const char *source = <%s>, mcl_uint8_t *destination = <%p>, mcl_size_t *padding = <%p>", table, source, destination,
209 const char *current_char = source;
212 for (index = 0; index <
QUANTUM_SIZE; index++, current_char++)
218 MCL_DEBUG(
"Padding char found. Current number of paddings = <%u>", *padding);
224 MCL_DEBUG(
"Look up current char = <%c> in table.", *current_char);
229 MCL_DEBUG(
"Current char = <%c> in table found at index = <%u>", *current_char, table_index);
230 data = (data << 6) + table_index;
236 destination[2] = (char)(data & 0xFFUL);
242 destination[1] = (char)(data & 0xFFUL);
246 destination[0] = (char)(data & 0xFFUL);
255 DEBUG_ENTRY(
"const char *table = <%s>, const mcl_uint8_t *data = <%p>, mcl_size_t data_size = <%u>, string_t **encoded_data = <%p>", table, data, data_size, encoded_data)
268 output_start = output;
270 while (data_size > 0)
282 input_buffer[index] = *data;
288 input_buffer[index] = 0;
291 MCL_DEBUG(
"data_size = <%u> left to encode.", data_size);
294 output_buffer[0] = (
mcl_uint8_t)((input_buffer[0] & 0xFC) >> 2);
295 output_buffer[1] = (
mcl_uint8_t)(((input_buffer[0] & 0x03) << 4) | ((input_buffer[1] & 0xF0) >> 4));
296 output_buffer[2] = (
mcl_uint8_t)(((input_buffer[1] & 0x0F) << 2) | ((input_buffer[2] & 0xC0) >> 6));
297 output_buffer[3] = (
mcl_uint8_t)(input_buffer[2] & 0x3F);
300 output[0] = table[output_buffer[0]];
301 output[1] = table[output_buffer[1]];
310 output[2] = table[output_buffer[2]];
315 output[2] = table[output_buffer[2]];
316 output[3] = table[output_buffer[3]];
323 MCL_DEBUG(
"4 bytes written to output = <%u>", output - output_start);
332 MCL_DEBUG(
"End of encode reached. encoded_data = <%s>, length encoded_data = <%u>.", (*encoded_data)->buffer, (*encoded_data)->length);
Memory module header file.
E_MCL_ERROR_CODE base64_url_encode(const mcl_uint8_t *data, mcl_size_t data_size, string_t **encoded_data)
static const mcl_uint8_t base64_decode_table[]
char * buffer
Buffer of string handle.
static const char base64_url_encode_table[]
static E_MCL_ERROR_CODE _encode_with_table(const char *table, const mcl_uint8_t *data, mcl_size_t data_size, string_t **encoded_data)
#define UPPER_COUNT_PADDING
#define MCL_MALLOC(bytes)
Log utility module header file.
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
static const char base64_encode_table[]
#define ASSERT_STATEMENT_CODE_MESSAGE(condition, statement, return_code,...)
If given content for Base64 encoding is bad.
static E_MCL_ERROR_CODE _decode_with_table(const mcl_uint8_t *table, const string_t *encoded_data, mcl_uint8_t **decoded_data, mcl_size_t *decoded_data_size)
#define ASSERT_CODE_MESSAGE(condition, return_code,...)
E_MCL_ERROR_CODE base64_url_decode(const string_t *encoded_data, mcl_uint8_t **decoded_data, mcl_size_t *decoded_data_size)
Definitions module header file.
E_MCL_ERROR_CODE string_initialize_dynamic(const char *value, mcl_size_t value_length, string_t **string)
Initializes a dynamic string_t object with the given value and length.
mcl_size_t length
Length of buffer.
E_MCL_ERROR_CODE base64_encode(const mcl_uint8_t *data, mcl_size_t data_size, string_t **encoded_data)
Base64 module header file.
static E_MCL_ERROR_CODE _decode_quantum(const mcl_uint8_t *table, const char *source, mcl_uint8_t *destination, mcl_size_t *padding)
E_MCL_ERROR_CODE base64_decode(const string_t *encoded_data, mcl_uint8_t **decoded_data, mcl_size_t *decoded_data_size)
static const mcl_uint8_t base64_url_decode_table[]