38 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
45 #if (BASE64_SUPPORT == ENABLED)
48 static const char_t base64EncTable[64] =
50 'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
51 'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
52 'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
53 'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'
57 static const uint8_t base64DecTable[128] =
59 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
60 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
61 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F,
62 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
63 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
64 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
65 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
66 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
80 size_t *outputLen,
size_t lineWidth)
91 if(
length > 0 && lineWidth > 0)
102 if(
n != 0 && input != NULL && output != NULL)
112 while(i > 0 && j > 0)
115 output[--j] = output[--i];
118 if(j >= 2 && (i % lineWidth) == 0)
127 if(outputLen != NULL)
153 p = (
const uint8_t *) input;
159 if(inputLen == (
n * 3 + 1))
162 if(input != NULL && output != NULL)
165 a = (
p[
n * 3] & 0xFC) >> 2;
166 b = (
p[
n * 3] & 0x03) << 4;
170 output[
n * 4] = base64EncTable[
a];
171 output[
n * 4 + 1] = base64EncTable[
b];
172 output[
n * 4 + 2] =
'=';
173 output[
n * 4 + 3] =
'=';
174 output[
n * 4 + 4] =
'\0';
178 if(outputLen != NULL)
180 *outputLen =
n * 4 + 4;
183 else if(inputLen == (
n * 3 + 2))
186 if(input != NULL && output != NULL)
189 a = (
p[
n * 3] & 0xFC) >> 2;
190 b = ((
p[
n * 3] & 0x03) << 4) | ((
p[
n * 3 + 1] & 0xF0) >> 4);
191 c = (
p[
n * 3 + 1] & 0x0F) << 2;
195 output[
n * 4] = base64EncTable[
a];
196 output[
n * 4 + 1] = base64EncTable[
b];
197 output[
n * 4 + 2] = base64EncTable[
c];
198 output[
n * 4 + 3] =
'=';
199 output[
n * 4 + 4] =
'\0';
203 if(outputLen != NULL)
205 *outputLen =
n * 4 + 4;
215 output[
n * 4] =
'\0';
219 if(outputLen != NULL)
227 if(input != NULL && output != NULL)
233 a = (
p[
n * 3] & 0xFC) >> 2;
234 b = ((
p[
n * 3] & 0x03) << 4) | ((
p[
n * 3 + 1] & 0xF0) >> 4);
235 c = ((
p[
n * 3 + 1] & 0x0F) << 2) | ((
p[
n * 3 + 2] & 0xC0) >> 6);
236 d =
p[
n * 3 + 2] & 0x3F;
240 output[
n * 4] = base64EncTable[
a];
241 output[
n * 4 + 1] = base64EncTable[
b];
242 output[
n * 4 + 2] = base64EncTable[
c];
243 output[
n * 4 + 3] = base64EncTable[d];
271 if(input == NULL && inputLen != 0)
273 if(outputLen == NULL)
280 p = (uint8_t *) output;
289 for(i = 0; i < inputLen && !error; i++)
295 if(
c ==
'\r' ||
c ==
'\n')
304 else if(
c < 128 && base64DecTable[
c] < 64 && padLen == 0)
316 p[
n + 1] = (
value >> 8) & 0xFF;
341 if(padLen == 0 && j == 0)
345 else if(padLen == 1 && j == 3)
352 p[
n + 1] = (
value >> 2) & 0xFF;
358 else if(padLen == 2 && j == 2)