108 lines
3.5 KiB
C
108 lines
3.5 KiB
C
#include "encode.h"
|
|
|
|
#define PADDING '='
|
|
|
|
static
|
|
unsigned char b64toascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
|
"abcdefghijklmnopqrstuvwxyz" \
|
|
"0123456789" \
|
|
"+/";
|
|
|
|
static
|
|
unsigned char b64urltoascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
|
"abcdefghijklmnopqrstuvwxyz" \
|
|
"0123456789" \
|
|
"-_";
|
|
|
|
static
|
|
unsigned char asciitob64[] = {
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 62, 0, 62, 0, 63, 52, 53,
|
|
54, 55, 56, 57, 58, 59, 60, 61, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 1, 2, 3, 4,
|
|
5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
|
25, 0, 0, 0, 0, 63, 0, 26, 27, 28,
|
|
29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
|
|
39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
|
|
49, 50, 51
|
|
};
|
|
|
|
|
|
int
|
|
encode(unsigned char *octbuf, int octbufsize, unsigned char *sxtbuf, int urlencoded)
|
|
{
|
|
unsigned char *table;
|
|
unsigned char *sxtbegin;
|
|
unsigned char *octleft;
|
|
int highorderbits;
|
|
|
|
table = (!urlencoded) ? b64toascii : b64urltoascii;
|
|
|
|
sxtbegin = sxtbuf;
|
|
octleft = octbuf + octbufsize - (octbufsize % 3);
|
|
while (octbuf < octleft) {
|
|
*sxtbuf++ = table[(*octbuf & ~3) >> 2];
|
|
highorderbits = (*octbuf++ & 3) << 4;
|
|
*sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)];
|
|
highorderbits = (*octbuf++ & 15) << 2;
|
|
*sxtbuf++ = table[highorderbits + ((*octbuf & 192) >> 6)];
|
|
*sxtbuf++ = table[*octbuf++ & ~192];
|
|
}
|
|
switch (octbufsize % 3) {
|
|
case 2:
|
|
*sxtbuf++ = table[(*octbuf & ~3) >> 2];
|
|
highorderbits = (*octbuf++ & 3) << 4;
|
|
*sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)];
|
|
*sxtbuf++ = table[(*octbuf & 15) << 2];
|
|
*sxtbuf++ = PADDING;
|
|
break;
|
|
case 1:
|
|
*sxtbuf++ = table[(*octbuf & ~3) >> 2];
|
|
*sxtbuf++ = table[(*octbuf & 3) << 4];
|
|
*sxtbuf++ = PADDING;
|
|
*sxtbuf++ = PADDING;
|
|
break;
|
|
}
|
|
|
|
return sxtbuf-sxtbegin;
|
|
}
|
|
|
|
int
|
|
decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf)
|
|
{
|
|
unsigned char *octbegin;
|
|
unsigned char *sxtleft;
|
|
int highorderbits;
|
|
int loworderbits;
|
|
|
|
octbegin = octbuf;
|
|
sxtleft = sxtbuf + sxtbufsize - (sxtbufsize % 4);
|
|
while (sxtbuf < sxtleft) {
|
|
highorderbits = asciitob64[*sxtbuf++] << 2;
|
|
loworderbits = asciitob64[*sxtbuf++];
|
|
*octbuf++ = highorderbits + ((loworderbits & ~15) >> 4);
|
|
highorderbits = (loworderbits & 15) << 4;
|
|
loworderbits = asciitob64[*sxtbuf++];
|
|
*octbuf++ = highorderbits + ((loworderbits & ~3) >> 2);
|
|
*octbuf++ = ((loworderbits & 3) << 6) + asciitob64[*sxtbuf++];
|
|
}
|
|
switch (sxtbufsize % 4) {
|
|
case 3:
|
|
highorderbits = asciitob64[*sxtbuf++] << 2;
|
|
loworderbits = asciitob64[*sxtbuf++];
|
|
*octbuf++ = highorderbits + ((loworderbits & ~15) >> 4);
|
|
highorderbits = (loworderbits & 15) << 4;
|
|
*octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~3) >> 2);
|
|
break;
|
|
case 2:
|
|
highorderbits = asciitob64[*sxtbuf++] << 2;
|
|
*octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~15) >> 4);
|
|
break;
|
|
}
|
|
|
|
return octbuf-octbegin;
|
|
}
|