63 #include <sys/types.h>
73 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
140 void *dest,
size_t targsize)
142 const unsigned char *src = _src;
144 size_t datalength = 0;
145 u_char input[3] = {0};
149 assert(dest && targsize > 0);
151 while (2 < srclength) {
157 output[0] = input[0] >> 2;
158 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
159 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
160 output[3] = input[2] & 0x3f;
162 if (datalength + 4 > targsize)
164 target[datalength++] =
Base64[output[0]];
165 target[datalength++] =
Base64[output[1]];
166 target[datalength++] =
Base64[output[2]];
167 target[datalength++] =
Base64[output[3]];
171 if (0 != srclength) {
173 input[0] = input[1] = input[2] =
'\0';
174 for (i = 0; i < srclength; i++)
177 output[0] = input[0] >> 2;
178 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
179 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
181 if (datalength + 4 > targsize)
183 target[datalength++] =
Base64[output[0]];
184 target[datalength++] =
Base64[output[1]];
186 target[datalength++] =
Pad64;
188 target[datalength++] =
Base64[output[2]];
189 target[datalength++] =
Pad64;
191 if (datalength >= targsize)
193 target[datalength] =
'\0';
203 int b64_decode(
const void *_src,
void *dest,
size_t targsize)
205 const char *src = _src;
206 unsigned char *target = dest;
215 assert(dest && targsize > 0);
217 while ((ch = (
unsigned char)*src++) !=
'\0') {
231 if (tarindex >= targsize)
233 target[tarindex] = (pos -
Base64) << 2;
239 if (tarindex >= targsize)
241 target[tarindex] |= (pos -
Base64) >> 4;
242 nextbyte = ((pos -
Base64) & 0x0f) << 4;
243 if (tarindex + 1 < targsize)
244 target[tarindex+1] = nextbyte;
253 if (tarindex >= targsize)
255 target[tarindex] |= (pos -
Base64) >> 2;
256 nextbyte = ((pos -
Base64) & 0x03) << 6;
257 if (tarindex + 1 < targsize)
258 target[tarindex+1] = nextbyte;
267 if (tarindex >= targsize)
269 target[tarindex] |= (pos -
Base64);
283 ch = (
unsigned char)*src++;
291 for (; ch !=
'\0'; ch = (
unsigned char)*src++)
297 ch = (
unsigned char)*src++;
306 for (; ch !=
'\0'; ch = (
unsigned char)*src++)
316 if (target && tarindex < targsize &&
317 target[tarindex] != 0)
330 if (tarindex < targsize)
331 target[tarindex] = 0;
int b64_encode(const void *_src, size_t srclength, void *dest, size_t targsize)
static const char Base64[]
int b64_decode(const void *_src, void *dest, size_t targsize)