1 /* gbase64.c - Base64 encoding/decoding
3 * Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
4 * Copyright (C) 2000-2003 Ximian Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * This is based on code in camel, written by:
22 * Michael Zucchi <notzed@ximian.com>
23 * Jeffrey Stedfast <fejj@ximian.com>
36 static const char base64_alphabet[] =
37 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
40 * g_base64_encode_step:
41 * @in: the binary data to encode
42 * @len: the length of @in
43 * @break_lines: whether to break long lines
44 * @out: pointer to destination buffer
45 * @state: Saved state between steps, initialize to 0
46 * @save: Saved state between steps, initialize to 0
48 * Incrementally encode a sequence of binary data into it's Base-64 stringified
49 * representation. By calling this function multiple times you can convert
50 * data in chunks to avoid having to have the full encoded data in memory.
52 * When all of the data has been converted you must call
53 * g_base64_encode_close() to flush the saved state.
55 * The output buffer must be large enough to fit all the data that will
56 * be written to it. Due to the way base64 encodes you will need
57 * at least: @len * 4 / 3 + 6 bytes. If you enable line-breaking you will
58 * need at least: @len * 4 / 3 + @len * 4 / (3 * 72) + 7 bytes.
60 * @break_lines is typically used when putting base64-encoded data in emails.
61 * It breaks the lines at 72 columns instead of putting all of the text on
62 * the same line. This avoids problems with long lines in the email system.
64 * Return value: The number of bytes of output that was written
69 g_base64_encode_step (const guchar *in,
79 g_return_val_if_fail (in != NULL, 0);
80 g_return_val_if_fail (out != NULL, 0);
81 g_return_val_if_fail (state != NULL, 0);
82 g_return_val_if_fail (save != NULL, 0);
90 if (len + ((char *) save) [0] > 2)
92 const guchar *inend = in+len-2;
98 switch (((char *) save) [0])
101 c1 = ((unsigned char *) save) [1];
104 c1 = ((unsigned char *) save) [1];
105 c2 = ((unsigned char *) save) [2];
110 * yes, we jump into the loop, no i'm not going to change it,
113 while (inptr < inend)
120 *outptr++ = base64_alphabet [ c1 >> 2 ];
121 *outptr++ = base64_alphabet [ c2 >> 4 |
123 *outptr++ = base64_alphabet [ ((c2 &0x0f) << 2) |
125 *outptr++ = base64_alphabet [ c3 & 0x3f ];
126 /* this is a bit ugly ... */
127 if (break_lines && (++already) >= 19)
134 ((char *)save)[0] = 0;
135 len = 2 - (inptr - inend);
143 /* points to the slot for the next char to save */
144 saveout = & (((char *)save)[1]) + ((char *)save)[0];
146 /* len can only be 0 1 or 2 */
149 case 2: *saveout++ = *inptr++;
150 case 1: *saveout++ = *inptr++;
152 ((char *)save)[0] += len;
159 * g_base64_encode_close:
160 * @break_lines: whether to break long lines
161 * @out: pointer to destination buffer
162 * @state: Saved state from g_base64_encode_step()
163 * @save: Saved state from g_base64_encode_step()
165 * Flush the status from a sequence of calls to g_base64_encode_step().
167 * Return value: The number of bytes of output that was written
172 g_base64_encode_close (gboolean break_lines,
180 g_return_val_if_fail (out != NULL, 0);
181 g_return_val_if_fail (state != NULL, 0);
182 g_return_val_if_fail (save != NULL, 0);
184 c1 = ((unsigned char *) save) [1];
185 c2 = ((unsigned char *) save) [2];
187 switch (((char *) save) [0])
190 outptr [2] = base64_alphabet[ ( (c2 &0x0f) << 2 ) ];
191 g_assert (outptr [2] != 0);
196 outptr [0] = base64_alphabet [ c1 >> 2 ];
197 outptr [1] = base64_alphabet [ c2 >> 4 | ( (c1&0x3) << 4 )];
213 * @data: the binary data to encode
214 * @len: the length of @data
216 * Encode a sequence of binary data into its Base-64 stringified
219 * Return value: a newly allocated, zero-terminated Base-64 encoded
220 * string representing @data.
225 g_base64_encode (const guchar *data,
229 gint state = 0, outlen;
232 g_return_val_if_fail (data != NULL, NULL);
233 g_return_val_if_fail (len > 1, NULL);
235 /* We can use a smaller limit here, since we know the saved state is 0 */
236 out = g_malloc (len * 4 / 3 + 4);
237 outlen = g_base64_encode_step (data, len, FALSE, out, &state, &save);
238 outlen += g_base64_encode_close (FALSE,
243 return (gchar *) out;
246 static const unsigned char mime_base64_rank[256] = {
247 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
248 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
249 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
250 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
251 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
252 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
253 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
254 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
255 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
256 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
257 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
258 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
259 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
260 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
261 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
262 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
266 * g_base64_decode_step:
267 * @in: binary input data
268 * @len: max length of @in data to decode
269 * @out: output buffer
270 * @state: Saved state between steps, initialize to 0
271 * @save: Saved state between steps, initialize to 0
273 * Incrementally decode a sequence of binary data from its Base-64 stringified
274 * representation. By calling this function multiple times you can convert
275 * data in chunks to avoid having to have the full encoded data in memory.
277 * The output buffer must be large enough to fit all the data that will
278 * be written to it. Since base64 encodes 3 bytes in 4 chars you need
279 * at least: @len * 3 / 4 bytes.
281 * Return value: The number of bytes of output that was written
286 g_base64_decode_step (const gchar *in,
300 g_return_val_if_fail (in != NULL, 0);
301 g_return_val_if_fail (out != NULL, 0);
302 g_return_val_if_fail (state != NULL, 0);
303 g_return_val_if_fail (save != NULL, 0);
308 inend = (const guchar *)in+len;
311 /* convert 4 base64 bytes to 3 normal bytes */
314 inptr = (const guchar *)in;
315 last[0] = last[1] = 0;
316 while (inptr < inend)
319 rank = mime_base64_rank [c];
346 * @text: zero-terminated string with base64 text to decode
347 * @out_len: The length of the decoded data is written here
349 * Decode a sequence of Base-64 encoded text into binary data
351 * Return value: a newly allocated buffer containing the binary data
352 * that @text represents
357 g_base64_decode (const gchar *text,
361 gint inlen, state = 0;
364 g_return_val_if_fail (text != NULL, NULL);
365 g_return_val_if_fail (out_len != NULL, NULL);
367 inlen = strlen (text);
368 ret = g_malloc0 (inlen * 3 / 4);
370 *out_len = g_base64_decode_step (text, inlen, ret, &state, &save);
375 #define __G_BASE64_C__
376 #include "galiasdef.c"