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 * SECTION:base64 Base64 Encoding
37 * @short_description: encodes and decodes data in Base64 format
39 * Base64 is an encoding that allows a sequence of arbitrary bytes to be
40 * encoded as a sequence of printable ASCII characters. For the definition
41 * of Base64, see <ulink url="http://www.ietf.org/rfc/rfc1421.txt">RFC
42 * 1421</ulink> or <ulink url="http://www.ietf.org/rfc/rfc2045.txt">RFC
43 * 2045</ulink>. Base64 is most commonly used as a MIME transfer encoding
46 * GLib supports incremental encoding using g_base64_encode_step() and
47 * g_base64_encode_close(). Incremental decoding can be done with
48 * g_base64_decode_step(). To encode or decode data in one go, use
49 * g_base64_encode() or g_base64_decode(). To avoid memory allocation when
50 * decoding, you can use g_base64_decode_inplace().
52 * Support for Base64 encoding has been added in GLib 2.12.
55 static const char base64_alphabet[] =
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
59 * g_base64_encode_step:
60 * @in: the binary data to encode
61 * @len: the length of @in
62 * @break_lines: whether to break long lines
63 * @out: pointer to destination buffer
64 * @state: Saved state between steps, initialize to 0
65 * @save: Saved state between steps, initialize to 0
67 * Incrementally encode a sequence of binary data into its Base-64 stringified
68 * representation. By calling this function multiple times you can convert
69 * data in chunks to avoid having to have the full encoded data in memory.
71 * When all of the data has been converted you must call
72 * g_base64_encode_close() to flush the saved state.
74 * The output buffer must be large enough to fit all the data that will
75 * be written to it. Due to the way base64 encodes you will need
76 * at least: (@len / 3 + 1) * 4 + 4 bytes (+ 4 may be needed in case of
77 * non-zero state). If you enable line-breaking you will need at least:
78 * ((@len / 3 + 1) * 4 + 4) / 72 + 1 bytes of extra space.
80 * @break_lines is typically used when putting base64-encoded data in emails.
81 * It breaks the lines at 72 columns instead of putting all of the text on
82 * the same line. This avoids problems with long lines in the email system.
84 * Return value: The number of bytes of output that was written
89 g_base64_encode_step (const guchar *in,
99 g_return_val_if_fail (in != NULL, 0);
100 g_return_val_if_fail (out != NULL, 0);
101 g_return_val_if_fail (state != NULL, 0);
102 g_return_val_if_fail (save != NULL, 0);
110 if (len + ((char *) save) [0] > 2)
112 const guchar *inend = in+len-2;
118 switch (((char *) save) [0])
121 c1 = ((unsigned char *) save) [1];
124 c1 = ((unsigned char *) save) [1];
125 c2 = ((unsigned char *) save) [2];
130 * yes, we jump into the loop, no i'm not going to change it,
133 while (inptr < inend)
140 *outptr++ = base64_alphabet [ c1 >> 2 ];
141 *outptr++ = base64_alphabet [ c2 >> 4 |
143 *outptr++ = base64_alphabet [ ((c2 &0x0f) << 2) |
145 *outptr++ = base64_alphabet [ c3 & 0x3f ];
146 /* this is a bit ugly ... */
147 if (break_lines && (++already) >= 19)
154 ((char *)save)[0] = 0;
155 len = 2 - (inptr - inend);
163 /* points to the slot for the next char to save */
164 saveout = & (((char *)save)[1]) + ((char *)save)[0];
166 /* len can only be 0 1 or 2 */
169 case 2: *saveout++ = *inptr++;
170 case 1: *saveout++ = *inptr++;
172 ((char *)save)[0] += len;
179 * g_base64_encode_close:
180 * @break_lines: whether to break long lines
181 * @out: pointer to destination buffer
182 * @state: Saved state from g_base64_encode_step()
183 * @save: Saved state from g_base64_encode_step()
185 * Flush the status from a sequence of calls to g_base64_encode_step().
187 * The output buffer must be large enough to fit all the data that will
188 * be written to it. It will need up to 4 bytes, or up to 5 bytes if
189 * line-breaking is enabled.
191 * Return value: The number of bytes of output that was written
196 g_base64_encode_close (gboolean break_lines,
204 g_return_val_if_fail (out != NULL, 0);
205 g_return_val_if_fail (state != NULL, 0);
206 g_return_val_if_fail (save != NULL, 0);
208 c1 = ((unsigned char *) save) [1];
209 c2 = ((unsigned char *) save) [2];
211 switch (((char *) save) [0])
214 outptr [2] = base64_alphabet[ ( (c2 &0x0f) << 2 ) ];
215 g_assert (outptr [2] != 0);
220 outptr [0] = base64_alphabet [ c1 >> 2 ];
221 outptr [1] = base64_alphabet [ c2 >> 4 | ( (c1&0x3) << 4 )];
237 * @data: the binary data to encode
238 * @len: the length of @data
240 * Encode a sequence of binary data into its Base-64 stringified
243 * Return value: a newly allocated, zero-terminated Base-64 encoded
244 * string representing @data. The returned string must
245 * be freed with g_free().
250 g_base64_encode (const guchar *data,
254 gint state = 0, outlen;
257 g_return_val_if_fail (data != NULL, NULL);
258 g_return_val_if_fail (len > 0, NULL);
260 /* We can use a smaller limit here, since we know the saved state is 0,
261 +1 is needed for trailing \0, also check for unlikely integer overflow */
262 if (len >= ((G_MAXSIZE - 1) / 4 - 1) * 3)
263 g_error("%s: input too large for Base64 encoding (%"G_GSIZE_FORMAT" chars)",
266 out = g_malloc ((len / 3 + 1) * 4 + 1);
268 outlen = g_base64_encode_step (data, len, FALSE, out, &state, &save);
269 outlen += g_base64_encode_close (FALSE, out + outlen, &state, &save);
272 return (gchar *) out;
275 static const unsigned char mime_base64_rank[256] = {
276 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
277 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
278 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
279 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
280 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
281 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
282 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
283 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
284 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
285 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
286 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
287 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
288 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
289 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
290 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
291 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
295 * g_base64_decode_step:
296 * @in: binary input data
297 * @len: max length of @in data to decode
298 * @out: output buffer
299 * @state: Saved state between steps, initialize to 0
300 * @save: Saved state between steps, initialize to 0
302 * Incrementally decode a sequence of binary data from its Base-64 stringified
303 * representation. By calling this function multiple times you can convert
304 * data in chunks to avoid having to have the full encoded data in memory.
306 * The output buffer must be large enough to fit all the data that will
307 * be written to it. Since base64 encodes 3 bytes in 4 chars you need
308 * at least: (@len / 4) * 3 + 3 bytes (+ 3 may be needed in case of non-zero
311 * Return value: The number of bytes of output that was written
316 g_base64_decode_step (const gchar *in,
330 g_return_val_if_fail (in != NULL, 0);
331 g_return_val_if_fail (out != NULL, 0);
332 g_return_val_if_fail (state != NULL, 0);
333 g_return_val_if_fail (save != NULL, 0);
338 inend = (const guchar *)in+len;
341 /* convert 4 base64 bytes to 3 normal bytes */
344 inptr = (const guchar *)in;
345 last[0] = last[1] = 0;
346 while (inptr < inend)
349 rank = mime_base64_rank [c];
376 * @text: zero-terminated string with base64 text to decode
377 * @out_len: The length of the decoded data is written here
379 * Decode a sequence of Base-64 encoded text into binary data
381 * Return value: a newly allocated buffer containing the binary data
382 * that @text represents. The returned buffer must
383 * be freed with g_free().
388 g_base64_decode (const gchar *text,
396 g_return_val_if_fail (text != NULL, NULL);
397 g_return_val_if_fail (out_len != NULL, NULL);
399 input_length = strlen (text);
401 g_return_val_if_fail (input_length > 1, NULL);
403 /* We can use a smaller limit here, since we know the saved state is 0,
404 +1 used to avoid calling g_malloc0(0), and hence retruning NULL */
405 ret = g_malloc0 ((input_length / 4) * 3 + 1);
407 *out_len = g_base64_decode_step (text, input_length, ret, &state, &save);
413 * g_base64_decode_inplace:
414 * @text: zero-terminated string with base64 text to decode
415 * @out_len: The length of the decoded data is written here
417 * Decode a sequence of Base-64 encoded text into binary data
418 * by overwriting the input data.
420 * Return value: The binary data that @text responds. This pointer
421 * is the same as the input @text.
426 g_base64_decode_inplace (gchar *text,
429 gint input_length, state = 0;
432 g_return_val_if_fail (text != NULL, NULL);
433 g_return_val_if_fail (out_len != NULL, NULL);
435 input_length = strlen (text);
437 g_return_val_if_fail (input_length > 1, NULL);
439 *out_len = g_base64_decode_step (text, input_length, (guchar *) text, &state, &save);
441 return (guchar *) text;