Mercurial > dropbear
comparison base64.c @ 0:d7da3b1e1540 libtomcrypt
put back the 0.95 makefile which was inadvertently merged over
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 31 May 2004 18:21:40 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d7da3b1e1540 |
---|---|
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 /* compliant base64 code donated by Wayne Scott ([email protected]) */ | |
12 #include "mycrypt.h" | |
13 | |
14 #ifdef BASE64 | |
15 | |
16 static const char *codes = | |
17 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
18 | |
19 static const unsigned char map[256] = { | |
20 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
21 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
22 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | |
23 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, | |
24 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, | |
25 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, | |
26 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, | |
27 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, | |
28 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, | |
29 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, | |
30 49, 50, 51, 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, | |
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 }; | |
42 | |
43 int base64_encode(const unsigned char *in, unsigned long len, | |
44 unsigned char *out, unsigned long *outlen) | |
45 { | |
46 unsigned long i, len2, leven; | |
47 unsigned char *p; | |
48 | |
49 _ARGCHK(in != NULL); | |
50 _ARGCHK(out != NULL); | |
51 _ARGCHK(outlen != NULL); | |
52 | |
53 /* valid output size ? */ | |
54 len2 = 4 * ((len + 2) / 3); | |
55 if (*outlen < len2 + 1) { | |
56 return CRYPT_BUFFER_OVERFLOW; | |
57 } | |
58 p = out; | |
59 leven = 3*(len / 3); | |
60 for (i = 0; i < leven; i += 3) { | |
61 *p++ = codes[(in[0] >> 2) & 0x3F]; | |
62 *p++ = codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F]; | |
63 *p++ = codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F]; | |
64 *p++ = codes[in[2] & 0x3F]; | |
65 in += 3; | |
66 } | |
67 /* Pad it if necessary... */ | |
68 if (i < len) { | |
69 unsigned a = in[0]; | |
70 unsigned b = (i+1 < len) ? in[1] : 0; | |
71 | |
72 *p++ = codes[(a >> 2) & 0x3F]; | |
73 *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F]; | |
74 *p++ = (i+1 < len) ? codes[(((b & 0xf) << 2)) & 0x3F] : '='; | |
75 *p++ = '='; | |
76 } | |
77 | |
78 /* append a NULL byte */ | |
79 *p = '\0'; | |
80 | |
81 /* return ok */ | |
82 *outlen = p - out; | |
83 return CRYPT_OK; | |
84 } | |
85 | |
86 int base64_decode(const unsigned char *in, unsigned long len, | |
87 unsigned char *out, unsigned long *outlen) | |
88 { | |
89 unsigned long t, x, y, z; | |
90 unsigned char c; | |
91 int g; | |
92 | |
93 _ARGCHK(in != NULL); | |
94 _ARGCHK(out != NULL); | |
95 _ARGCHK(outlen != NULL); | |
96 | |
97 g = 3; | |
98 for (x = y = z = t = 0; x < len; x++) { | |
99 c = map[in[x]&0xFF]; | |
100 if (c == 255) continue; | |
101 if (c == 254) { c = 0; g--; } | |
102 t = (t<<6)|c; | |
103 if (++y == 4) { | |
104 if (z + g > *outlen) { | |
105 return CRYPT_BUFFER_OVERFLOW; | |
106 } | |
107 out[z++] = (unsigned char)((t>>16)&255); | |
108 if (g > 1) out[z++] = (unsigned char)((t>>8)&255); | |
109 if (g > 2) out[z++] = (unsigned char)(t&255); | |
110 y = t = 0; | |
111 } | |
112 } | |
113 if (y != 0) { | |
114 return CRYPT_INVALID_PACKET; | |
115 } | |
116 *outlen = z; | |
117 return CRYPT_OK; | |
118 } | |
119 | |
120 #endif | |
121 |