| 1 |
|
| 2 |
|
| 3 |
#include "base64.h" |
| 4 |
|
| 5 |
#define BASE64_PAD '=' |
| 6 |
#define BASE64DE_FIRST '+' |
| 7 |
#define BASE64DE_LAST 'z' |
| 8 |
|
| 9 |
|
| 10 |
static const char base64en[] = { |
| 11 |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
| 12 |
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 13 |
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 14 |
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 15 |
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
| 16 |
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 17 |
'w', 'x', 'y', 'z', '0', '1', '2', '3', |
| 18 |
'4', '5', '6', '7', '8', '9', '+', '/', |
| 19 |
}; |
| 20 |
|
| 21 |
|
| 22 |
static const unsigned char base64de[] = { |
| 23 |
|
| 24 |
255, 255, 255, 255, 255, 255, 255, 255, |
| 25 |
|
| 26 |
|
| 27 |
255, 255, 255, 255, 255, 255, 255, 255, |
| 28 |
|
| 29 |
|
| 30 |
255, 255, 255, 255, 255, 255, 255, 255, |
| 31 |
|
| 32 |
|
| 33 |
255, 255, 255, 255, 255, 255, 255, 255, |
| 34 |
|
| 35 |
|
| 36 |
255, 255, 255, 255, 255, 255, 255, 255, |
| 37 |
|
| 38 |
|
| 39 |
255, 255, 255, 62, 255, 255, 255, 63, |
| 40 |
|
| 41 |
|
| 42 |
52, 53, 54, 55, 56, 57, 58, 59, |
| 43 |
|
| 44 |
|
| 45 |
60, 61, 255, 255, 255, 255, 255, 255, |
| 46 |
|
| 47 |
|
| 48 |
255, 0, 1, 2, 3, 4, 5, 6, |
| 49 |
|
| 50 |
|
| 51 |
7, 8, 9, 10, 11, 12, 13, 14, |
| 52 |
|
| 53 |
|
| 54 |
15, 16, 17, 18, 19, 20, 21, 22, |
| 55 |
|
| 56 |
|
| 57 |
23, 24, 25, 255, 255, 255, 255, 255, |
| 58 |
|
| 59 |
|
| 60 |
255, 26, 27, 28, 29, 30, 31, 32, |
| 61 |
|
| 62 |
|
| 63 |
33, 34, 35, 36, 37, 38, 39, 40, |
| 64 |
|
| 65 |
|
| 66 |
41, 42, 43, 44, 45, 46, 47, 48, |
| 67 |
|
| 68 |
|
| 69 |
49, 50, 51, 255, 255, 255, 255, 255 |
| 70 |
}; |
| 71 |
|
| 72 |
unsigned int |
| 73 |
base64_encode(const unsigned char *in, unsigned int inlen, char *out) |
| 74 |
{ |
| 75 |
int s; |
| 76 |
unsigned int i; |
| 77 |
unsigned int j; |
| 78 |
unsigned char c; |
| 79 |
unsigned char l; |
| 80 |
|
| 81 |
s = 0; |
| 82 |
l = 0; |
| 83 |
for (i = j = 0; i < inlen; i++) { |
| 84 |
c = in[i]; |
| 85 |
|
| 86 |
switch (s) { |
| 87 |
case 0: |
| 88 |
s = 1; |
| 89 |
out[j++] = base64en[(c >> 2) & 0x3F]; |
| 90 |
break; |
| 91 |
case 1: |
| 92 |
s = 2; |
| 93 |
out[j++] = base64en[((l & 0x3) << 4) | ((c >> 4) & 0xF)]; |
| 94 |
break; |
| 95 |
case 2: |
| 96 |
s = 0; |
| 97 |
out[j++] = base64en[((l & 0xF) << 2) | ((c >> 6) & 0x3)]; |
| 98 |
out[j++] = base64en[c & 0x3F]; |
| 99 |
break; |
| 100 |
} |
| 101 |
l = c; |
| 102 |
} |
| 103 |
|
| 104 |
switch (s) { |
| 105 |
case 1: |
| 106 |
out[j++] = base64en[(l & 0x3) << 4]; |
| 107 |
out[j++] = BASE64_PAD; |
| 108 |
out[j++] = BASE64_PAD; |
| 109 |
break; |
| 110 |
case 2: |
| 111 |
out[j++] = base64en[(l & 0xF) << 2]; |
| 112 |
out[j++] = BASE64_PAD; |
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
out[j] = 0; |
| 117 |
|
| 118 |
return j; |
| 119 |
} |
| 120 |
|
| 121 |
unsigned int |
| 122 |
base64_decode(const char *in, unsigned int inlen, unsigned char *out) |
| 123 |
{ |
| 124 |
unsigned int i; |
| 125 |
unsigned int j; |
| 126 |
unsigned char c; |
| 127 |
|
| 128 |
if (inlen & 0x3) { |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
for (i = j = 0; i < inlen; i++) { |
| 133 |
if (in[i] == BASE64_PAD) { |
| 134 |
break; |
| 135 |
} |
| 136 |
if (in[i] < BASE64DE_FIRST || in[i] > BASE64DE_LAST) { |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
c = base64de[(unsigned char)in[i]]; |
| 141 |
if (c == 255) { |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
switch (i & 0x3) { |
| 146 |
case 0: |
| 147 |
out[j] = (c << 2) & 0xFF; |
| 148 |
break; |
| 149 |
case 1: |
| 150 |
out[j++] |= (c >> 4) & 0x3; |
| 151 |
out[j] = (c & 0xF) << 4; |
| 152 |
break; |
| 153 |
case 2: |
| 154 |
out[j++] |= (c >> 2) & 0xF; |
| 155 |
out[j] = (c & 0x3) << 6; |
| 156 |
break; |
| 157 |
case 3: |
| 158 |
out[j++] |= c; |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return j; |
| 164 |
} |