Mercurial > dropbear
comparison libtomcrypt/src/misc/base64/base64_encode.c @ 285:1b9e69c058d2
propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 08 Mar 2006 13:23:58 +0000 |
parents | |
children | 0cbe8f6dbf9e |
comparison
equal
deleted
inserted
replaced
281:997e6f7dc01e | 285:1b9e69c058d2 |
---|---|
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 $ */ |