1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
12
/* compliant base64 code donated by Wayne Scott (wscott@bitmover.com) */
17
static const unsigned char map[256] = {
18
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
19
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
20
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
21
255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
22
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
23
255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
24
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
25
19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
26
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
27
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
28
49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
29
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
30
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
31
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
32
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
33
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
34
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
35
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
36
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
37
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
38
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
41
int base64_decode(const unsigned char *in, unsigned long len,
42
unsigned char *out, unsigned long *outlen)
44
unsigned long t, x, y, z;
50
_ARGCHK(outlen != NULL);
53
for (x = y = z = t = 0; x < len; x++) {
55
if (c == 255) continue;
56
if (c == 254) { c = 0; g--; }
59
if (z + g > *outlen) {
60
return CRYPT_BUFFER_OVERFLOW;
62
out[z++] = (unsigned char)((t>>16)&255);
63
if (g > 1) out[z++] = (unsigned char)((t>>8)&255);
64
if (g > 2) out[z++] = (unsigned char)(t&255);
69
return CRYPT_INVALID_PACKET;