2 ***********************************************************************
4 * __ _ _ __ ___| |_ | |__ __ _ ___ ___ / /_ | || |
5 * / _` | '_ \ / _ \ __| | '_ \ / _` / __|/ _ \ '_ \| || |_
6 * | (_| | | | | __/ |_ | |_) | (_| \__ \ __/ (_) |__ _|
7 * \__, |_| |_|\___|\__| |_.__/ \__,_|___/\___|\___/ |_|
10 * created by Alfred Reibenschuh <alfredreibenschuh@gmx.net>,
11 * under the "GNU Library General Public License" (see below).
13 ***********************************************************************
15 * Copyright (C) 2003 Free Software Foundation
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Library General Public
19 * License as published by the Free Software Foundation; either
20 * version 2 of the License, or (at your option) any later version.
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Library General Public License for more details.
27 * You should have received a copy of the GNU Library General Public
28 * License along with this library; if not, write to the Free
29 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
32 ***********************************************************************
35 * Taken from gnet's sources
36 * Modified the name of the functions and some variables
48 static gchar s_encoding_Base64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
49 #define s_encoding_Pad64 '='
50 static guchar s_encoding_Base64_rank
[256] = {
51 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0x00-0x0f */
52 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0x10-0x1f */
53 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63, /* 0x20-0x2f */
54 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,255,255,255, /* 0x30-0x3f */
55 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40-0x4f */
56 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, /* 0x50-0x5f */
57 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60-0x6f */
58 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255, /* 0x70-0x7f */
59 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0x80-0x8f */
60 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0x90-0x9f */
61 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xa0-0xaf */
62 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xb0-0xbf */
63 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xc0-0xcf */
64 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xd0-0xdf */
65 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xe0-0xef */
66 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xf0-0xff */
69 /*! \brief Convert a buffer from binary to base64 representation.
70 * \par Function Description
71 * Convert a buffer from binary to base64 representation. Set
72 * <B>strict</B> to TRUE to insert a newline every 72th character. This is
73 * required by RFC 2045, but some applications don't require this.
75 * \param [in] src Source buffer.
76 * \param [in] srclen Length of source buffer.
77 * \param [out] dstlenp Length of buffer returned
78 * (including the terminating \\0).
79 * \param [in] strict Insert new lines as required by RFC 2045.
80 * \return Caller owned buffer containing base64 representation.
82 gchar
* s_encoding_base64_encode (gchar
* src
, guint srclen
,
83 guint
* dstlenp
, gboolean strict
)
93 return NULL
; /* FIX: Or return ""? */
95 /* Calculate required length of dst. 4 bytes of dst are needed for
96 every 3 bytes of src. */
97 *dstlenp
= (((srclen
+ 2) / 3) * 4)+5;
99 *dstlenp
+= (*dstlenp
/ 72); /* Handle trailing \n */
101 dst
= g_new(gchar
, *dstlenp
);
109 Convert 3 bytes of src to 4 bytes of output
111 output[0] = input[0] 7:2
112 output[1] = input[0] 1:0 input[1] 7:4
113 output[2] = input[1] 3:0 input[2] 7:6
114 output[3] = input[1] 5:0
122 output
[0] = (input
[0] >> 2);
123 output
[1] = ((input
[0] & 0x03) << 4) +
125 output
[2] = ((input
[1] & 0x0f) << 2) +
127 output
[3] = (input
[2] & 0x3f);
129 g_assert ((dstpos
+ 4) < *dstlenp
);
131 /* Map output to the Base64 alphabet */
132 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[0]];
133 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[1]];
134 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[2]];
135 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[3]];
137 /* Add a newline if strict and */
139 if ((++ocnt
% (72/4)) == 0)
140 dst
[dstpos
++] = '\n';
143 /* Now worry about padding with remaining 1 or 2 bytes */
146 input
[0] = input
[1] = input
[2] = '\0';
147 for (i
= 0; i
< srclen
; i
++)
150 output
[0] = (input
[0] >> 2);
151 output
[1] = ((input
[0] & 0x03) << 4) +
153 output
[2] = ((input
[1] & 0x0f) << 2) +
156 g_assert ((dstpos
+ 4) < *dstlenp
);
158 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[0]];
159 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[1]];
162 dst
[dstpos
++] = s_encoding_Pad64
;
164 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[2]];
166 dst
[dstpos
++] = s_encoding_Pad64
;
169 g_assert (dstpos
<= *dstlenp
);
173 *dstlenp
= dstpos
+ 1;
178 /*! \brief Convert a buffer from base64 to binary representation.
179 * \par Function Description
180 * Convert a buffer from base64 to binary representation. This
181 * function is liberal in what it will accept. It ignores non-base64
184 * \param [in] src Source buffer.
185 * \param [in] srclen Length of the source buffer.
186 * \param [out] dstlenp Pointer to length of the destination buffer
187 * \return Caller-owned buffer with binary representation.
188 * The integer pointed to by <B>dstlenp</B> is set to the length
191 gchar
*s_encoding_base64_decode (gchar
* src
, guint srclen
, guint
* dstlenp
)
195 guint dstidx
, state
, ch
= 0;
200 srclen
= strlen(src
);
205 dst
= g_new(gchar
, srclen
+1);
212 if (ch
== s_encoding_Pad64
)
214 if (s_encoding_Base64_rank
[ch
]==255) /* Skip any non-base64 anywhere */
217 pos
= s_encoding_Base64_rank
[ch
];
222 dst
[dstidx
] = (pos
<< 2);
226 dst
[dstidx
] |= (pos
>> 4);
227 res
= ((pos
& 0x0f) << 4);
232 dst
[dstidx
] = res
| (pos
>> 2);
233 res
= (pos
& 0x03) << 6;
238 dst
[dstidx
] = res
| pos
;
247 * We are done decoding Base-64 chars. Let's see if we ended
248 * on a byte boundary, and/or with erroneous trailing characters.
250 if (ch
== s_encoding_Pad64
) /* We got a pad char. */
254 case 0: /* Invalid = in first position */
255 case 1: /* Invalid = in second position */
257 case 2: /* Valid, means one byte of info */
258 /* Skip any number of spaces. */
263 if (ch
== s_encoding_Pad64
) break;
264 if (s_encoding_Base64_rank
[ch
] != 255) break;
266 /* Make sure there is another trailing = sign. */
267 if (ch
!= s_encoding_Pad64
)
274 case 3: /* Valid, means two bytes of info */
276 * We know this char is an =. Is there anything but
277 * whitespace after it?
283 if (s_encoding_Base64_rank
[ch
] != 255)
291 * Now make sure for cases 2 and 3 that the "extra"
292 * bits that slopped past the last full byte were
293 * zeros. If we don't check them, they become a
294 * subliminal channel.
308 * We ended by seeing the end of the string. Make sure we
309 * have no partial bytes lying around.