1 #include <twr_base64.h>
3 const char twr_b64_alphabet[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
5 static uint8_t twr_base64_lookup(
char c);
7 bool twr_base64_encode(
char *output,
size_t *output_length, uint8_t *input,
size_t input_length)
10 size_t encode_length = 0;
14 for (; input_length != 0; input_length--)
19 a4[0] = (uint8_t) ((a3[0] & 0xfc) >> 2);
20 a4[1] = (uint8_t) (((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4));
21 a4[2] = (uint8_t) (((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6));
22 a4[3] = (uint8_t) (a3[2] & 0x3f);
24 for (i = 0; i < 4; i++)
26 output[encode_length++] = twr_b64_alphabet[a4[i]];
27 if (encode_length > *output_length)
39 for (j = i; j < 3; j++)
44 a4[0] = (uint8_t) ((a3[0] & 0xfc) >> 2);
45 a4[1] = (uint8_t) (((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4));
46 a4[2] = (uint8_t) (((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6));
47 a4[3] = (uint8_t) (a3[2] & 0x3f);
49 for (j = 0; j < (i + 1); j++)
51 output[encode_length++] = twr_b64_alphabet[a4[j]];
52 if (encode_length > *output_length)
61 output[encode_length++] =
'=';
62 if (encode_length > *output_length)
69 output[encode_length] = 0x00;
71 *output_length = encode_length;
76 bool twr_base64_decode(uint8_t *output,
size_t *output_length,
char *input,
size_t input_length)
79 size_t decode_length = 0;
83 for (; input_length != 0; input_length--)
90 a4[i++] = (uint8_t) *(input++);
93 for (i = 0; i < 4; i++)
95 a4[i] = twr_base64_lookup(a4[i]);
98 a3[0] = (uint8_t) ((a4[0] << 2) + ((a4[1] & 0x30) >> 4));
99 a3[1] = (uint8_t) (((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2));
100 a3[2] = (uint8_t) (((a4[2] & 0x3) << 6) + a4[3]);
102 for (i = 0; i < 3; i++)
104 output[decode_length++] = a3[i];
105 if (decode_length > *output_length)
117 for (j = i; j < 4; j++)
122 for (j = 0; j < 4; j++)
124 a4[j] = twr_base64_lookup(a4[j]);
127 a3[0] = (uint8_t) ((a4[0] << 2) + ((a4[1] & 0x30) >> 4));
128 a3[1] = (uint8_t) (((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2));
129 a3[2] = (uint8_t) (((a4[2] & 0x3) << 6) + a4[3]);
131 for (j = 0; j < (i - 1); j++)
133 output[decode_length++] = a3[j];
134 if (decode_length > *output_length)
141 output[decode_length] = 0x00;
143 *output_length = decode_length;
150 size_t n = (int) length;
151 return (n + 2 - ((n + 2) % 3)) / 3 * 4;
159 for (i = length - 1; input[i] ==
'='; i--)
164 return ((6 * length) / 8) - num_eq;
167 static uint8_t twr_base64_lookup(
char c)
169 if (c >=
'A' && c <=
'Z')
171 return (uint8_t) (c -
'A');
173 if (c >=
'a' && c <=
'z')
175 return (uint8_t) (c - 71);
177 if (c >=
'0' && c <=
'9')
179 return (uint8_t) (c + 4);
size_t twr_base64_calculate_decode_length(char *input, size_t length)
BASE64 Calculate decode length.
bool twr_base64_encode(char *output, size_t *output_length, uint8_t *input, size_t input_length)
BASE64 encode.
size_t twr_base64_calculate_encode_length(size_t length)
BASE64 Calculate encode length.
bool twr_base64_decode(uint8_t *output, size_t *output_length, char *input, size_t input_length)
BASE64 decode.