annotate base64_decode.c @ 88:901233045998 libtomcrypt

Fix typo in the big-endian macros
author Matt Johnston <matt@ucc.asn.au>
date Thu, 02 Sep 2004 15:34:30 +0000
parents 6362d3854bb4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 *
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * LibTomCrypt is a library that provides various cryptographic
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * algorithms in a highly modular and flexible manner.
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 *
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * The library is free for all purposes without any express
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * guarantee it works.
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 *
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * Tom St Denis, [email protected], http://libtomcrypt.org
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 /* compliant base64 code donated by Wayne Scott ([email protected]) */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 #include "mycrypt.h"
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 #ifdef BASE64
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 static const unsigned char map[256] = {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 255, 255, 255, 255 };
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 int base64_decode(const unsigned char *in, unsigned long len,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 unsigned char *out, unsigned long *outlen)
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 unsigned long t, x, y, z;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 unsigned char c;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 int g;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 _ARGCHK(in != NULL);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 _ARGCHK(out != NULL);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 _ARGCHK(outlen != NULL);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 g = 3;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 for (x = y = z = t = 0; x < len; x++) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 c = map[in[x]&0xFF];
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 if (c == 255) continue;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 if (c == 254) { c = 0; g--; }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 t = (t<<6)|c;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 if (++y == 4) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 if (z + g > *outlen) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 return CRYPT_BUFFER_OVERFLOW;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 out[z++] = (unsigned char)((t>>16)&255);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 if (g > 1) out[z++] = (unsigned char)((t>>8)&255);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 if (g > 2) out[z++] = (unsigned char)(t&255);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 y = t = 0;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 if (y != 0) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 return CRYPT_INVALID_PACKET;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 *outlen = z;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 return CRYPT_OK;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 #endif
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76