1 /***************************************************************************\
3 * BitlBee - An IRC to IM gateway *
4 * Base64 handling functions. encode_real() is mostly based on the y64 en- *
5 * coder from libyahoo2. Moving it to a new file because it's getting big. *
7 * Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net> *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
19 * You should have received a copy of the GNU General Public License along *
20 * with this program; if not, write to the Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
23 \***************************************************************************/
29 static const char real_b64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
31 char *tobase64(const char *text
)
33 return base64_encode((const unsigned char *)text
, strlen(text
));
36 char *base64_encode(const unsigned char *in
, int len
)
40 out
= g_malloc((len
+ 2) /* the == padding */
41 / 3 /* every 3-byte block */
42 * 4 /* becomes a 4-byte one */
43 + 1); /* and of course, ASCIIZ! */
45 base64_encode_real((unsigned char*) in
, len
, (unsigned char*) out
, real_b64
);
50 int base64_encode_real(const unsigned char *in
, int inlen
, unsigned char *out
, const char *b64digits
)
54 for (; inlen
>= 3; inlen
-= 3)
56 out
[outlen
++] = b64digits
[in
[0] >> 2];
57 out
[outlen
++] = b64digits
[((in
[0]<<4) & 0x30) | (in
[1]>>4)];
58 out
[outlen
++] = b64digits
[((in
[1]<<2) & 0x3c) | (in
[2]>>6)];
59 out
[outlen
++] = b64digits
[in
[2] & 0x3f];
64 out
[outlen
++] = b64digits
[in
[0] >> 2];
67 out
[outlen
++] = b64digits
[((in
[0]<<4) & 0x30) | (in
[1]>>4)];
68 out
[outlen
++] = b64digits
[((in
[1]<<2) & 0x3c)];
72 out
[outlen
++] = b64digits
[((in
[0]<<4) & 0x30)];
73 out
[outlen
++] = b64digits
[64];
75 out
[outlen
++] = b64digits
[64];
82 /* Just a simple wrapper, but usually not very convenient because of zero
84 char *frombase64(const char *in
)
88 base64_decode(in
, &out
);
93 /* FIXME: Lookup table stuff is not threadsafe! (But for now BitlBee is not threaded.) */
94 int base64_decode(const char *in
, unsigned char **out
)
96 static char b64rev
[256] = { 0 };
99 /* Create a reverse-lookup for the Base64 sequence. */
102 memset( b64rev
, 0xff, 256 );
103 for( i
= 0; i
<= 64; i
++ )
104 b64rev
[(int)real_b64
[i
]] = i
;
108 *out
= g_malloc( ( len
+ 6 ) / 4 * 3 );
109 len
= base64_decode_real( (unsigned char*) in
, *out
, b64rev
);
110 *out
= g_realloc( *out
, len
+ 1 );
111 out
[0][len
] = 0; /* Zero termination can't hurt. */
116 int base64_decode_real(const unsigned char *in
, unsigned char *out
, char *b64rev
)
120 for( i
= 0; in
[i
] && in
[i
+1] && in
[i
+2] && in
[i
+3]; i
+= 4 )
124 sx
= b64rev
[(int)in
[i
+0]];
127 out
[outlen
] = ( sx
<< 2 ) & 0xfc;
129 sx
= b64rev
[(int)in
[i
+1]];
132 out
[outlen
] |= ( sx
>> 4 ) & 0x03;
134 out
[outlen
] = ( sx
<< 4 ) & 0xf0;
136 sx
= b64rev
[(int)in
[i
+2]];
139 out
[outlen
] |= ( sx
>> 2 ) & 0x0f;
141 out
[outlen
] = ( sx
<< 6 ) & 0xc0;
143 sx
= b64rev
[(int)in
[i
+3]];
150 /* If sx > 64 the base64 string was damaged. Should we ignore this? */