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@gmail.com, http://libtomcrypt.org
15
Compliant base64 code donated by Wayne Scott (wscott@bitmover.com)
21
static const unsigned char map[256] = {
22
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
23
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
24
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
25
255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
26
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
27
255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
28
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
29
19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
30
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
31
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
32
49, 50, 51, 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,
39
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
40
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
41
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
42
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
46
base64 decode a block of memory
47
@param in The base64 data to decode
48
@param inlen The length of the base64 data
49
@param out [out] The destination of the binary decoded data
50
@param outlen [in/out] The max size and resulting size of the decoded data
51
@return CRYPT_OK if successful
53
int base64_decode(const unsigned char *in, unsigned long inlen,
54
unsigned char *out, unsigned long *outlen)
56
unsigned long t, x, y, z;
60
LTC_ARGCHK(in != NULL);
61
LTC_ARGCHK(out != NULL);
62
LTC_ARGCHK(outlen != NULL);
65
for (x = y = z = t = 0; x < inlen; x++) {
67
if (c == 255) continue;
68
/* the final = symbols are read and used to trim the remaining bytes */
71
/* prevent g < 0 which would potentially allow an overflow later */
73
return CRYPT_INVALID_PACKET;
76
/* we only allow = to be at the end */
77
return CRYPT_INVALID_PACKET;
83
if (z + g > *outlen) {
84
return CRYPT_BUFFER_OVERFLOW;
86
out[z++] = (unsigned char)((t>>16)&255);
87
if (g > 1) out[z++] = (unsigned char)((t>>8)&255);
88
if (g > 2) out[z++] = (unsigned char)(t&255);
93
return CRYPT_INVALID_PACKET;
102
/* $Source: /cvs/libtom/libtomcrypt/src/misc/base64/base64_decode.c,v $ */
103
/* $Revision: 1.3 $ */
104
/* $Date: 2005/05/05 14:35:59 $ */