comparison src/misc/base64/base64_encode.c @ 280:59400faa4b44 libtomcrypt-orig libtomcrypt-1.05

Re-import libtomcrypt 1.05 for cleaner propagating. From crypt-1.05.tar.bz2, SHA1 of 88250202bb51570dc64f7e8f1c943cda9479258f
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 12:58:00 +0000
parents
children d5faf4814ddb
comparison
equal deleted inserted replaced
-1:000000000000 280:59400faa4b44
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_encode.c
15 Compliant base64 encoder donated by Wayne Scott ([email protected])
16 */
17
18
19 #ifdef BASE64
20
21 static const char *codes =
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23
24 /**
25 base64 Encode a buffer (NUL terminated)
26 @param in The input buffer to encode
27 @param inlen The length of the input buffer
28 @param out [out] The destination of the base64 encoded data
29 @param outlen [in/out] The max size and resulting size
30 @return CRYPT_OK if successful
31 */
32 int base64_encode(const unsigned char *in, unsigned long inlen,
33 unsigned char *out, unsigned long *outlen)
34 {
35 unsigned long i, len2, leven;
36 unsigned char *p;
37
38 LTC_ARGCHK(in != NULL);
39 LTC_ARGCHK(out != NULL);
40 LTC_ARGCHK(outlen != NULL);
41
42 /* valid output size ? */
43 len2 = 4 * ((inlen + 2) / 3);
44 if (*outlen < len2 + 1) {
45 return CRYPT_BUFFER_OVERFLOW;
46 }
47 p = out;
48 leven = 3*(inlen / 3);
49 for (i = 0; i < leven; i += 3) {
50 *p++ = codes[(in[0] >> 2) & 0x3F];
51 *p++ = codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];
52 *p++ = codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];
53 *p++ = codes[in[2] & 0x3F];
54 in += 3;
55 }
56 /* Pad it if necessary... */
57 if (i < inlen) {
58 unsigned a = in[0];
59 unsigned b = (i+1 < inlen) ? in[1] : 0;
60
61 *p++ = codes[(a >> 2) & 0x3F];
62 *p++ = codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
63 *p++ = (i+1 < inlen) ? codes[(((b & 0xf) << 2)) & 0x3F] : '=';
64 *p++ = '=';
65 }
66
67 /* append a NULL byte */
68 *p = '\0';
69
70 /* return ok */
71 *outlen = p - out;
72 return CRYPT_OK;
73 }
74
75 #endif
76
77
78 /* $Source: /cvs/libtom/libtomcrypt/src/misc/base64/base64_encode.c,v $ */
79 /* $Revision: 1.3 $ */
80 /* $Date: 2005/05/05 14:35:59 $ */