1
/* base64.h - encode and decode base64 encoding according to RFC 2045
3
* Copyright (C) 2005, Raphaël Quinet <raphael@gimp.org>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the
17
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
* Boston, MA 02111-1307, USA.
21
/* Yet another implementation of base64 encoding and decoding. I
22
* ended up writing this because most of the implementations that I
23
* found required a null-terminated buffer, some of the others did not
24
* ignore whitespace (especially those written for HTTP usage) and the
25
* rest were not compatible with the LGPL (some were GPL, not LGPL).
26
* Or at least I haven't been able to find LGPL implementations.
27
* Writing this according to RFC 2045 did not take long anyway.
34
# include "libgimp/stdplugins-intl.h"
38
# define _(String) (String)
39
# define N_(String) (String)
49
static const gchar base64_code[] =
50
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
51
static const gint base64_6bits[256] =
53
-2, -3, -3, -3, -3, -3, -3, -3, -3, -1, -1, -1, -1, -1, -3, -3,
54
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
55
-1, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, 62, -3, -3, -3, 63,
56
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -3, -3, -3, -2, -3, -3,
57
-3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
58
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -3, -3, -3, -3, -3,
59
-3, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
60
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -3, -3, -3, -3, -3,
61
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
62
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
63
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
64
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
65
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
66
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
67
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
68
-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3
69
}; /* -1: skip whitespace, -2: end of input, -3: error */
73
* @src_b64: input buffer containing base64-encoded data
74
* @src_size: input buffer size (in bytes) or -1 if @src_b64 is nul-terminated
75
* @dest: buffer in which the decoded data should be stored
76
* @dest_size: size of the destination buffer
77
* @ignore_errors: if #TRUE, skip all invalid characters (no data validation)
79
* Read base64-encoded data from the input buffer @src_b64 and write
80
* the decoded data into @dest.
82
* The base64 encoding uses 4 bytes for every 3 bytes of input, so
83
* @dest_size should be at least 3/4 of @src_size (or less if the
84
* input contains whitespace characters). The base64 encoding has no
85
* reliable EOF marker, so this can cause additional data following
86
* the base64-encoded block to be misinterpreted if @src_size is not
87
* specified correctly. The decoder will stop at the first nul byte
88
* or at the first '=' (padding byte) so you should ensure that one of
89
* these is present if you supply -1 for @src_size. For more details
90
* about the base64 encoding, see RFC 2045, chapter 6.8.
92
* Returns: the number of bytes stored in @dest, or -1 if invalid data was found.
95
base64_decode (const gchar *src_b64,
99
gboolean ignore_errors)
106
g_return_val_if_fail (src_b64 != NULL, -1);
107
g_return_val_if_fail (dest != NULL, -1);
111
for (i = 0; (src_size != 0) && (i + 3 <= dest_size); src_b64++, src_size--)
113
bits = base64_6bits[(int) *src_b64 & 0xff];
118
else if ((bits == -3) && !ignore_errors)
127
/* we have decoded 4 source chars => 24 bits of output (3 chars) */
128
dest[i++] = decoded >> 16;
129
dest[i++] = (decoded >> 8) & 0xff;
130
dest[i++] = decoded & 0xff;
135
if ((n == 3) && (i + 2 <= dest_size))
137
/* 3 source chars (+ 1 padding "=") => 16 bits of output (2 chars) */
138
dest[i++] = decoded >> 10;
139
dest[i++] = (decoded >> 2) & 0xff;
141
else if ((n == 2) && (i + 1 <= dest_size))
143
/* 2 source chars (+ 2 padding "=") => 8 bits of output (1 char) */
144
dest[i++] = decoded >> 4;
154
* @src_size: input buffer size (in bytes) or -1 if @src is nul-terminated
155
* @dest_b64: buffer in which the base64 encoded data should be stored
156
* @dest_size: size of the destination buffer
157
* @columns: if > 0, add line breaks in the output after this many columns
159
* Read binary data from the input buffer @src and write
160
* base64-encoded data into @dest_b64.
162
* Since the base64 encoding uses 4 bytes for every 3 bytes of input,
163
* @dest_size should be at least 4/3 of @src_size, plus optional line
164
* breaks if @columns > 0 and up to two padding bytes at the end. For
165
* more details about the base64 encoding, see RFC 2045, chapter 6.8.
166
* Note that RFC 2045 recommends setting @columns to 76.
168
* Returns: the number of bytes stored in @dest.
171
base64_encode (const gchar *src,
182
g_return_val_if_fail (src != NULL, -1);
183
g_return_val_if_fail (dest_b64 != NULL, -1);
188
for (i = 0; (src_size != 0) && (i + 4 <= dest_size); src++, src_size--)
190
bits += *(guchar *)src;
193
dest_b64[i++] = base64_code[(bits >> 18) & 0x3f];
194
dest_b64[i++] = base64_code[(bits >> 12) & 0x3f];
195
dest_b64[i++] = base64_code[(bits >> 6) & 0x3f];
196
dest_b64[i++] = base64_code[bits & 0x3f];
202
if ((c >= columns) && (i < dest_size))
204
dest_b64[i++] = '\n';
214
if ((n != 0) && (i + 4 <= dest_size))
218
dest_b64[i++] = base64_code[(bits >> 10) & 0x3f];
219
dest_b64[i++] = base64_code[(bits >> 4) & 0x3f];
225
dest_b64[i++] = base64_code[(bits >> 18) & 0x3f];
226
dest_b64[i++] = base64_code[(bits >> 12) & 0x3f];
227
dest_b64[i++] = base64_code[(bits >> 6) & 0x3f];
231
if ((columns > 0) && ((c != 0) || (n != 0)) && (i + 1 < dest_size))
232
dest_b64[i++] = '\n';