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., 675 Mass Ave, Cambridge, MA 02139, USA.
31 ***********************************************************************
34 * Taken from gnet's sources
35 * Modified the name of the functions and some variables
47 static gchar s_encoding_Base64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
48 #define s_encoding_Pad64 '='
49 static guchar s_encoding_Base64_rank
[256] = {
50 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0x00-0x0f */
51 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0x10-0x1f */
52 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63, /* 0x20-0x2f */
53 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,255,255,255, /* 0x30-0x3f */
54 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40-0x4f */
55 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, /* 0x50-0x5f */
56 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60-0x6f */
57 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255, /* 0x70-0x7f */
58 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0x80-0x8f */
59 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0x90-0x9f */
60 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xa0-0xaf */
61 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xb0-0xbf */
62 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xc0-0xcf */
63 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xd0-0xdf */
64 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xe0-0xef */
65 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, /* 0xf0-0xff */
68 /*! \brief Convert a buffer from binary to base64 representation.
69 * \par Function Description
70 * Convert a buffer from binary to base64 representation. Set
71 * <B>strict</B> to TRUE to insert a newline every 72th character. This is
72 * required by RFC 2045, but some applications don't require this.
74 * \param [in] src Source buffer.
75 * \param [in] srclen Length of source buffer.
76 * \param [out] dstlenp Length of buffer returned
77 * (including the terminating \\0).
78 * \param [in] strict Insert new lines as required by RFC 2045.
79 * \return Caller owned buffer containing base64 representation.
81 gchar
* s_encoding_base64_encode (gchar
* src
, guint srclen
,
82 guint
* dstlenp
, gboolean strict
)
92 return NULL
; /* FIX: Or return ""? */
94 /* Calculate required length of dst. 4 bytes of dst are needed for
95 every 3 bytes of src. */
96 *dstlenp
= (((srclen
+ 2) / 3) * 4)+5;
98 *dstlenp
+= (*dstlenp
/ 72); /* Handle trailing \n */
100 dst
= g_new(gchar
, *dstlenp
);
108 Convert 3 bytes of src to 4 bytes of output
110 output[0] = input[0] 7:2
111 output[1] = input[0] 1:0 input[1] 7:4
112 output[2] = input[1] 3:0 input[2] 7:6
113 output[3] = input[1] 5:0
121 output
[0] = (input
[0] >> 2);
122 output
[1] = ((input
[0] & 0x03) << 4) +
124 output
[2] = ((input
[1] & 0x0f) << 2) +
126 output
[3] = (input
[2] & 0x3f);
128 g_assert ((dstpos
+ 4) < *dstlenp
);
130 /* Map output to the Base64 alphabet */
131 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[0]];
132 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[1]];
133 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[2]];
134 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[3]];
136 /* Add a newline if strict and */
138 if ((++ocnt
% (72/4)) == 0)
139 dst
[dstpos
++] = '\n';
142 /* Now worry about padding with remaining 1 or 2 bytes */
145 input
[0] = input
[1] = input
[2] = '\0';
146 for (i
= 0; i
< srclen
; i
++)
149 output
[0] = (input
[0] >> 2);
150 output
[1] = ((input
[0] & 0x03) << 4) +
152 output
[2] = ((input
[1] & 0x0f) << 2) +
155 g_assert ((dstpos
+ 4) < *dstlenp
);
157 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[0]];
158 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[1]];
161 dst
[dstpos
++] = s_encoding_Pad64
;
163 dst
[dstpos
++] = s_encoding_Base64
[(guint
) output
[2]];
165 dst
[dstpos
++] = s_encoding_Pad64
;
168 g_assert (dstpos
<= *dstlenp
);
172 *dstlenp
= dstpos
+ 1;
177 /*! \brief Convert a buffer from base64 to binary representation.
178 * \par Function Description
179 * Convert a buffer from base64 to binary representation. This
180 * function is liberal in what it will accept. It ignores non-base64
183 * \param [in] src Source buffer.
184 * \param [in] srclen Length of the source buffer.
185 * \param [out] dstlenp Pointer to length of the destination buffer
186 * \return Caller-owned buffer with binary representation.
187 * The integer pointed to by <B>dstlenp</B> is set to the length
190 gchar
*s_encoding_base64_decode (gchar
* src
, guint srclen
, guint
* dstlenp
)
194 guint dstidx
, state
, ch
= 0;
199 srclen
= strlen(src
);
204 dst
= g_new(gchar
, srclen
+1);
211 if (ch
== s_encoding_Pad64
)
213 if (s_encoding_Base64_rank
[ch
]==255) /* Skip any non-base64 anywhere */
216 pos
= s_encoding_Base64_rank
[ch
];
223 dst
[dstidx
] = (pos
<< 2);
230 dst
[dstidx
] |= (pos
>> 4);
231 res
= ((pos
& 0x0f) << 4);
239 dst
[dstidx
] = res
| (pos
>> 2);
240 res
= (pos
& 0x03) << 6;
248 dst
[dstidx
] = res
| pos
;
258 * We are done decoding Base-64 chars. Let's see if we ended
259 * on a byte boundary, and/or with erroneous trailing characters.
261 if (ch
== s_encoding_Pad64
) /* We got a pad char. */
265 case 0: /* Invalid = in first position */
266 case 1: /* Invalid = in second position */
268 case 2: /* Valid, means one byte of info */
269 /* Skip any number of spaces. */
274 if (ch
== s_encoding_Pad64
) break;
275 if (s_encoding_Base64_rank
[ch
] != 255) break;
277 /* Make sure there is another trailing = sign. */
278 if (ch
!= s_encoding_Pad64
)
285 case 3: /* Valid, means two bytes of info */
287 * We know this char is an =. Is there anything but
288 * whitespace after it?
294 if (s_encoding_Base64_rank
[ch
] != 255)
302 * Now make sure for cases 2 and 3 that the "extra"
303 * bits that slopped past the last full byte were
304 * zeros. If we don't check them, they become a
305 * subliminal channel.
307 if (dst
!= NULL
&& res
!= 0)
319 * We ended by seeing the end of the string. Make sure we
320 * have no partial bytes lying around.