39 static const char *mBase64IndexToChar =
40 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
41 "abcdefghijklmnopqrstuvwxyz"
44 size_t outLen = ((len + 2) / 3) * 4;
45 char *outData = (
char *)malloc(1 + outLen);
49 outData[outLen] = 0x00;
53 int in0 = src[0] >> 2;
54 int in1 = (src[0] & 0x3) << 4;
55 dst[0] = mBase64IndexToChar[in0];
56 dst[1] = mBase64IndexToChar[in1];
65 int in2 = (src[1] & 0xf) << 2;
66 dst[1] = mBase64IndexToChar[in1];
67 dst[2] = mBase64IndexToChar[in2];
75 int in3 = src[2] & 0x3f;
76 dst[2] = mBase64IndexToChar[in2];
77 dst[3] = mBase64IndexToChar[in3];
91 unsigned char *
base64_Decode(
const char *src,
size_t *len,
size_t srcLen)
93 static const signed char mBase64CharToIndex[256] =
110 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
111 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
112 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 63, -1, 63,
113 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
114 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
115 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
116 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
117 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
118 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
119 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
120 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
121 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
122 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
123 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
124 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
125 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
127 size_t numChars = (srcLen * 3) / 4;
128 unsigned char *outData = (
unsigned char *)malloc(numChars);
133 unsigned char *dst = outData;
134 const char *finish = src + srcLen;
137 int data0 = mBase64CharToIndex[(
unsigned char)src[0]];
138 int data1 = mBase64CharToIndex[(
unsigned char)src[1]];
139 int data2 = mBase64CharToIndex[(
unsigned char)src[2]];
140 int data3 = mBase64CharToIndex[(
unsigned char)src[3]];
141 *dst++ = (data0 << 2) | (data1 >> 4);
147 *dst++ = (data1 << 4) | (data2 >> 2);
153 *dst++ = (data2 << 6) | (data3);