Make --enable-man and --enable-gtk-doc independent
[glib.git] / glib / gbase64.c
blobd0ab0ea270d78688236ce1ec03b5de0602804718
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>
26 #include "config.h"
28 #include <string.h>
30 #include "gbase64.h"
31 #include "gtestutils.h"
32 #include "glibintl.h"
35 /**
36 * SECTION:base64
37 * @title: Base64 Encoding
38 * @short_description: encodes and decodes data in Base64 format
40 * Base64 is an encoding that allows a sequence of arbitrary bytes to be
41 * encoded as a sequence of printable ASCII characters. For the definition
42 * of Base64, see <ulink url="http://www.ietf.org/rfc/rfc1421.txt">RFC
43 * 1421</ulink> or <ulink url="http://www.ietf.org/rfc/rfc2045.txt">RFC
44 * 2045</ulink>. Base64 is most commonly used as a MIME transfer encoding
45 * for email.
47 * GLib supports incremental encoding using g_base64_encode_step() and
48 * g_base64_encode_close(). Incremental decoding can be done with
49 * g_base64_decode_step(). To encode or decode data in one go, use
50 * g_base64_encode() or g_base64_decode(). To avoid memory allocation when
51 * decoding, you can use g_base64_decode_inplace().
53 * Support for Base64 encoding has been added in GLib 2.12.
56 static const char base64_alphabet[] =
57 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
59 /**
60 * g_base64_encode_step:
61 * @in: (array length=len) (element-type guint8): the binary data to encode
62 * @len: the length of @in
63 * @break_lines: whether to break long lines
64 * @out: (out) (array) (element-type guint8): pointer to destination buffer
65 * @state: (inout): Saved state between steps, initialize to 0
66 * @save: (inout): Saved state between steps, initialize to 0
68 * Incrementally encode a sequence of binary data into its Base-64 stringified
69 * representation. By calling this function multiple times you can convert
70 * data in chunks to avoid having to have the full encoded data in memory.
72 * When all of the data has been converted you must call
73 * g_base64_encode_close() to flush the saved state.
75 * The output buffer must be large enough to fit all the data that will
76 * be written to it. Due to the way base64 encodes you will need
77 * at least: (@len / 3 + 1) * 4 + 4 bytes (+ 4 may be needed in case of
78 * non-zero state). If you enable line-breaking you will need at least:
79 * ((@len / 3 + 1) * 4 + 4) / 72 + 1 bytes of extra space.
81 * @break_lines is typically used when putting base64-encoded data in emails.
82 * It breaks the lines at 72 columns instead of putting all of the text on
83 * the same line. This avoids problems with long lines in the email system.
84 * Note however that it breaks the lines with <literal>LF</literal>
85 * characters, not <literal>CR LF</literal> sequences, so the result cannot
86 * be passed directly to SMTP or certain other protocols.
88 * Return value: The number of bytes of output that was written
90 * Since: 2.12
92 gsize
93 g_base64_encode_step (const guchar *in,
94 gsize len,
95 gboolean break_lines,
96 gchar *out,
97 gint *state,
98 gint *save)
100 char *outptr;
101 const guchar *inptr;
103 g_return_val_if_fail (in != NULL, 0);
104 g_return_val_if_fail (out != NULL, 0);
105 g_return_val_if_fail (state != NULL, 0);
106 g_return_val_if_fail (save != NULL, 0);
108 if (len <= 0)
109 return 0;
111 inptr = in;
112 outptr = out;
114 if (len + ((char *) save) [0] > 2)
116 const guchar *inend = in+len-2;
117 int c1, c2, c3;
118 int already;
120 already = *state;
122 switch (((char *) save) [0])
124 case 1:
125 c1 = ((unsigned char *) save) [1];
126 goto skip1;
127 case 2:
128 c1 = ((unsigned char *) save) [1];
129 c2 = ((unsigned char *) save) [2];
130 goto skip2;
134 * yes, we jump into the loop, no i'm not going to change it,
135 * it's beautiful!
137 while (inptr < inend)
139 c1 = *inptr++;
140 skip1:
141 c2 = *inptr++;
142 skip2:
143 c3 = *inptr++;
144 *outptr++ = base64_alphabet [ c1 >> 2 ];
145 *outptr++ = base64_alphabet [ c2 >> 4 |
146 ((c1&0x3) << 4) ];
147 *outptr++ = base64_alphabet [ ((c2 &0x0f) << 2) |
148 (c3 >> 6) ];
149 *outptr++ = base64_alphabet [ c3 & 0x3f ];
150 /* this is a bit ugly ... */
151 if (break_lines && (++already) >= 19)
153 *outptr++ = '\n';
154 already = 0;
158 ((char *)save)[0] = 0;
159 len = 2 - (inptr - inend);
160 *state = already;
163 if (len>0)
165 char *saveout;
167 /* points to the slot for the next char to save */
168 saveout = & (((char *)save)[1]) + ((char *)save)[0];
170 /* len can only be 0 1 or 2 */
171 switch(len)
173 case 2: *saveout++ = *inptr++;
174 case 1: *saveout++ = *inptr++;
176 ((char *)save)[0] += len;
179 return outptr - out;
183 * g_base64_encode_close:
184 * @break_lines: whether to break long lines
185 * @out: (out) (array) (element-type guint8): pointer to destination buffer
186 * @state: (inout): Saved state from g_base64_encode_step()
187 * @save: (inout): Saved state from g_base64_encode_step()
189 * Flush the status from a sequence of calls to g_base64_encode_step().
191 * The output buffer must be large enough to fit all the data that will
192 * be written to it. It will need up to 4 bytes, or up to 5 bytes if
193 * line-breaking is enabled.
195 * Return value: The number of bytes of output that was written
197 * Since: 2.12
199 gsize
200 g_base64_encode_close (gboolean break_lines,
201 gchar *out,
202 gint *state,
203 gint *save)
205 int c1, c2;
206 char *outptr = out;
208 g_return_val_if_fail (out != NULL, 0);
209 g_return_val_if_fail (state != NULL, 0);
210 g_return_val_if_fail (save != NULL, 0);
212 c1 = ((unsigned char *) save) [1];
213 c2 = ((unsigned char *) save) [2];
215 switch (((char *) save) [0])
217 case 2:
218 outptr [2] = base64_alphabet[ ( (c2 &0x0f) << 2 ) ];
219 g_assert (outptr [2] != 0);
220 goto skip;
221 case 1:
222 outptr[2] = '=';
223 skip:
224 outptr [0] = base64_alphabet [ c1 >> 2 ];
225 outptr [1] = base64_alphabet [ c2 >> 4 | ( (c1&0x3) << 4 )];
226 outptr [3] = '=';
227 outptr += 4;
228 break;
230 if (break_lines)
231 *outptr++ = '\n';
233 *save = 0;
234 *state = 0;
236 return outptr - out;
240 * g_base64_encode:
241 * @data: (array length=len) (element-type guint8): the binary data to encode
242 * @len: the length of @data
244 * Encode a sequence of binary data into its Base-64 stringified
245 * representation.
247 * Return value: (transfer full): a newly allocated, zero-terminated Base-64
248 * encoded string representing @data. The returned string must
249 * be freed with g_free().
251 * Since: 2.12
253 gchar *
254 g_base64_encode (const guchar *data,
255 gsize len)
257 gchar *out;
258 gint state = 0, outlen;
259 gint save = 0;
261 g_return_val_if_fail (data != NULL || len == 0, NULL);
263 /* We can use a smaller limit here, since we know the saved state is 0,
264 +1 is needed for trailing \0, also check for unlikely integer overflow */
265 if (len >= ((G_MAXSIZE - 1) / 4 - 1) * 3)
266 g_error("%s: input too large for Base64 encoding (%"G_GSIZE_FORMAT" chars)",
267 G_STRLOC, len);
269 out = g_malloc ((len / 3 + 1) * 4 + 1);
271 outlen = g_base64_encode_step (data, len, FALSE, out, &state, &save);
272 outlen += g_base64_encode_close (FALSE, out + outlen, &state, &save);
273 out[outlen] = '\0';
275 return (gchar *) out;
278 static const unsigned char mime_base64_rank[256] = {
279 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
280 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
281 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
282 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
283 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
284 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
285 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
286 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,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,
292 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
293 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
294 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
298 * g_base64_decode_step:
299 * @in: (array length=len) (element-type guint8): binary input data
300 * @len: max length of @in data to decode
301 * @out: (out) (array) (element-type guint8): output buffer
302 * @state: (inout): Saved state between steps, initialize to 0
303 * @save: (inout): Saved state between steps, initialize to 0
305 * Incrementally decode a sequence of binary data from its Base-64 stringified
306 * representation. By calling this function multiple times you can convert
307 * data in chunks to avoid having to have the full encoded data in memory.
309 * The output buffer must be large enough to fit all the data that will
310 * be written to it. Since base64 encodes 3 bytes in 4 chars you need
311 * at least: (@len / 4) * 3 + 3 bytes (+ 3 may be needed in case of non-zero
312 * state).
314 * Return value: The number of bytes of output that was written
316 * Since: 2.12
318 gsize
319 g_base64_decode_step (const gchar *in,
320 gsize len,
321 guchar *out,
322 gint *state,
323 guint *save)
325 const guchar *inptr;
326 guchar *outptr;
327 const guchar *inend;
328 guchar c, rank;
329 guchar last[2];
330 unsigned int v;
331 int i;
333 g_return_val_if_fail (in != NULL, 0);
334 g_return_val_if_fail (out != NULL, 0);
335 g_return_val_if_fail (state != NULL, 0);
336 g_return_val_if_fail (save != NULL, 0);
338 if (len <= 0)
339 return 0;
341 inend = (const guchar *)in+len;
342 outptr = out;
344 /* convert 4 base64 bytes to 3 normal bytes */
345 v=*save;
346 i=*state;
347 inptr = (const guchar *)in;
348 last[0] = last[1] = 0;
349 while (inptr < inend)
351 c = *inptr++;
352 rank = mime_base64_rank [c];
353 if (rank != 0xff)
355 last[1] = last[0];
356 last[0] = c;
357 v = (v<<6) | rank;
358 i++;
359 if (i==4)
361 *outptr++ = v>>16;
362 if (last[1] != '=')
363 *outptr++ = v>>8;
364 if (last[0] != '=')
365 *outptr++ = v;
366 i=0;
371 *save = v;
372 *state = i;
374 return outptr - out;
378 * g_base64_decode:
379 * @text: zero-terminated string with base64 text to decode
380 * @out_len: (out): The length of the decoded data is written here
382 * Decode a sequence of Base-64 encoded text into binary data
384 * Return value: (transfer full) (array length=out_len) (element-type guint8):
385 * newly allocated buffer containing the binary data
386 * that @text represents. The returned buffer must
387 * be freed with g_free().
389 * Since: 2.12
391 guchar *
392 g_base64_decode (const gchar *text,
393 gsize *out_len)
395 guchar *ret;
396 gsize input_length;
397 gint state = 0;
398 guint save = 0;
400 g_return_val_if_fail (text != NULL, NULL);
401 g_return_val_if_fail (out_len != NULL, NULL);
403 input_length = strlen (text);
405 /* We can use a smaller limit here, since we know the saved state is 0,
406 +1 used to avoid calling g_malloc0(0), and hence returning NULL */
407 ret = g_malloc0 ((input_length / 4) * 3 + 1);
409 *out_len = g_base64_decode_step (text, input_length, ret, &state, &save);
411 return ret;
415 * g_base64_decode_inplace:
416 * @text: (inout) (array length=out_len) (element-type guint8): zero-terminated
417 * string with base64 text to decode
418 * @out_len: (inout): The length of the decoded data is written here
420 * Decode a sequence of Base-64 encoded text into binary data
421 * by overwriting the input data.
423 * Return value: (transfer none): The binary data that @text responds. This pointer
424 * is the same as the input @text.
426 * Since: 2.20
428 guchar *
429 g_base64_decode_inplace (gchar *text,
430 gsize *out_len)
432 gint input_length, state = 0;
433 guint save = 0;
435 g_return_val_if_fail (text != NULL, NULL);
436 g_return_val_if_fail (out_len != NULL, NULL);
438 input_length = strlen (text);
440 g_return_val_if_fail (input_length > 1, NULL);
442 *out_len = g_base64_decode_step (text, input_length, (guchar *) text, &state, &save);
444 return (guchar *) text;