Mercurial > dropbear
comparison src/misc/base64/base64_decode.c @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children | 39d5d58461d6 |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis | |
2 * | |
3 * LibTomCrypt is a library that provides various cryptographic | |
4 * algorithms in a highly modular and flexible manner. | |
5 * | |
6 * The library is free for all purposes without any express | |
7 * guarantee it works. | |
8 * | |
9 * Tom St Denis, [email protected], http://libtomcrypt.org | |
10 */ | |
11 #include "tomcrypt.h" | |
12 | |
13 /** | |
14 @file base64_decode.c | |
15 Compliant base64 code donated by Wayne Scott ([email protected]) | |
16 */ | |
17 | |
18 | |
19 #ifdef BASE64 | |
20 | |
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, | |
43 255, 255, 255, 255 }; | |
44 | |
45 /** | |
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 | |
52 */ | |
53 int base64_decode(const unsigned char *in, unsigned long inlen, | |
54 unsigned char *out, unsigned long *outlen) | |
55 { | |
56 unsigned long t, x, y, z; | |
57 unsigned char c; | |
58 int g; | |
59 | |
60 LTC_ARGCHK(in != NULL); | |
61 LTC_ARGCHK(out != NULL); | |
62 LTC_ARGCHK(outlen != NULL); | |
63 | |
64 g = 3; | |
65 for (x = y = z = t = 0; x < inlen; x++) { | |
66 c = map[in[x]&0xFF]; | |
67 if (c == 255) continue; | |
68 /* the final = symbols are read and used to trim the remaining bytes */ | |
69 if (c == 254) { | |
70 c = 0; | |
71 /* prevent g < 0 which would potentially allow an overflow later */ | |
72 if (--g < 0) { | |
73 return CRYPT_INVALID_PACKET; | |
74 } | |
75 } else if (g != 3) { | |
76 /* we only allow = to be at the end */ | |
77 return CRYPT_INVALID_PACKET; | |
78 } | |
79 | |
80 t = (t<<6)|c; | |
81 | |
82 if (++y == 4) { | |
83 if (z + g > *outlen) { | |
84 return CRYPT_BUFFER_OVERFLOW; | |
85 } | |
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); | |
89 y = t = 0; | |
90 } | |
91 } | |
92 if (y != 0) { | |
93 return CRYPT_INVALID_PACKET; | |
94 } | |
95 *outlen = z; | |
96 return CRYPT_OK; | |
97 } | |
98 | |
99 #endif | |
100 |