comparison base64_decode.c @ 15:6362d3854bb4 libtomcrypt-orig

0.96 release of LibTomCrypt
author Matt Johnston <matt@ucc.asn.au>
date Tue, 15 Jun 2004 14:07:21 +0000
parents
children
comparison
equal deleted inserted replaced
3:7faae8f46238 15:6362d3854bb4
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
12 /* compliant base64 code donated by Wayne Scott ([email protected]) */
13 #include "mycrypt.h"
14
15 #ifdef BASE64
16
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,
39 255, 255, 255, 255 };
40
41 int base64_decode(const unsigned char *in, unsigned long len,
42 unsigned char *out, unsigned long *outlen)
43 {
44 unsigned long t, x, y, z;
45 unsigned char c;
46 int g;
47
48 _ARGCHK(in != NULL);
49 _ARGCHK(out != NULL);
50 _ARGCHK(outlen != NULL);
51
52 g = 3;
53 for (x = y = z = t = 0; x < len; x++) {
54 c = map[in[x]&0xFF];
55 if (c == 255) continue;
56 if (c == 254) { c = 0; g--; }
57 t = (t<<6)|c;
58 if (++y == 4) {
59 if (z + g > *outlen) {
60 return CRYPT_BUFFER_OVERFLOW;
61 }
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);
65 y = t = 0;
66 }
67 }
68 if (y != 0) {
69 return CRYPT_INVALID_PACKET;
70 }
71 *outlen = z;
72 return CRYPT_OK;
73 }
74
75 #endif
76