annotate base64_encode.c @ 211:f01f0400314d libtomcrypt

disapproval of revision 6a39eb8b36778460fca83b8149df2a8b6d3327fd
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jul 2005 13:23:45 +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 /* compliant base64 code donated by Wayne Scott ([email protected]) */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 #include "mycrypt.h"
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 #ifdef BASE64
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 static const char *codes =
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 int base64_encode(const unsigned char *in, unsigned long len,
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 unsigned char *out, unsigned long *outlen)
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 unsigned long i, len2, leven;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 unsigned char *p;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 _ARGCHK(in != NULL);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 _ARGCHK(out != NULL);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 _ARGCHK(outlen != NULL);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 /* valid output size ? */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 len2 = 4 * ((len + 2) / 3);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 if (*outlen < len2 + 1) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 return CRYPT_BUFFER_OVERFLOW;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 p = out;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 leven = 3*(len / 3);
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 for (i = 0; i < leven; i += 3) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 *p++ = codes[(in[0] >> 2) & 0x3F];
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 *p++ = codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 *p++ = codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 *p++ = codes[in[2] & 0x3F];
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 in += 3;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 /* Pad it if necessary... */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 if (i < len) {
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 unsigned a = in[0];
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 unsigned b = (i+1 < len) ? in[1] : 0;
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 *p++ = codes[(a >> 2) & 0x3F];
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 *p++ = (i+1 < len) ? codes[(((b & 0xf) << 2)) & 0x3F] : '=';
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 *p++ = '=';
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 }
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 /* append a NULL byte */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 *p = '\0';
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 /* return ok */
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 *outlen = p - out;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 return CRYPT_OK;
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 }
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 #endif
6362d3854bb4 0.96 release of LibTomCrypt
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63