Bump gEDA version
[geda-gaf.git] / libgeda / src / s_encoding.c
blob7a8e681d3196f05542c5f163e30df7a747e5dfeb
1 /*! \verbatim
2 ***********************************************************************
3 * _ _ __ _ _
4 * __ _ _ __ ___| |_ | |__ __ _ ___ ___ / /_ | || |
5 * / _` | '_ \ / _ \ __| | '_ \ / _` / __|/ _ \ '_ \| || |_
6 * | (_| | | | | __/ |_ | |_) | (_| \__ \ __/ (_) |__ _|
7 * \__, |_| |_|\___|\__| |_.__/ \__,_|___/\___|\___/ |_|
8 * |___/
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,
30 * MA 02111-1301 USA.
32 ***********************************************************************
33 \endverbatim
35 * Taken from gnet's sources
36 * Modified the name of the functions and some variables
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
42 #include <ctype.h>
43 #include <glib.h>
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #endif
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)
85 gchar* dst;
86 guint dstpos;
87 guchar input[3];
88 guchar output[4];
89 guint ocnt;
90 guint i;
92 if (srclen == 0)
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;
98 if (strict)
99 *dstlenp += (*dstlenp / 72); /* Handle trailing \n */
101 dst = g_new(gchar, *dstlenp );
103 /* bulk encoding */
104 dstpos = 0;
105 ocnt = 0;
106 while (srclen >= 3)
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
117 input[0] = *src++;
118 input[1] = *src++;
119 input[2] = *src++;
120 srclen -= 3;
122 output[0] = (input[0] >> 2);
123 output[1] = ((input[0] & 0x03) << 4) +
124 (input[1] >> 4);
125 output[2] = ((input[1] & 0x0f) << 2) +
126 (input[2] >> 6);
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 */
138 if (strict)
139 if ((++ocnt % (72/4)) == 0)
140 dst[dstpos++] = '\n';
143 /* Now worry about padding with remaining 1 or 2 bytes */
144 if (srclen != 0)
146 input[0] = input[1] = input[2] = '\0';
147 for (i = 0; i < srclen; i++)
148 input[i] = *src++;
150 output[0] = (input[0] >> 2);
151 output[1] = ((input[0] & 0x03) << 4) +
152 (input[1] >> 4);
153 output[2] = ((input[1] & 0x0f) << 2) +
154 (input[2] >> 6);
156 g_assert ((dstpos + 4) < *dstlenp);
158 dst[dstpos++] = s_encoding_Base64[(guint) output[0]];
159 dst[dstpos++] = s_encoding_Base64[(guint) output[1]];
161 if (srclen == 1)
162 dst[dstpos++] = s_encoding_Pad64;
163 else
164 dst[dstpos++] = s_encoding_Base64[(guint) output[2]];
166 dst[dstpos++] = s_encoding_Pad64;
169 g_assert (dstpos <= *dstlenp);
171 dst[dstpos] = '\0';
173 *dstlenp = dstpos + 1;
175 return dst;
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
182 * symbols.
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
189 * of that buffer.
191 gchar *s_encoding_base64_decode (gchar* src, guint srclen, guint* dstlenp)
194 gchar* dst;
195 guint dstidx, state, ch = 0;
196 gchar res;
197 guchar pos;
199 if (srclen == 0)
200 srclen = strlen(src);
201 state = 0;
202 dstidx = 0;
203 res = 0;
205 dst = g_new(gchar, srclen+1);
206 *dstlenp = srclen+1;
208 while (srclen > 0)
210 srclen--;
211 ch = *src++;
212 if (ch == s_encoding_Pad64)
213 break;
214 if (s_encoding_Base64_rank[ch]==255) /* Skip any non-base64 anywhere */
215 continue;
217 pos = s_encoding_Base64_rank[ch];
219 switch (state)
221 case 0:
222 dst[dstidx] = (pos << 2);
223 state = 1;
224 break;
225 case 1:
226 dst[dstidx] |= (pos >> 4);
227 res = ((pos & 0x0f) << 4);
228 dstidx++;
229 state = 2;
230 break;
231 case 2:
232 dst[dstidx] = res | (pos >> 2);
233 res = (pos & 0x03) << 6;
234 dstidx++;
235 state = 3;
236 break;
237 case 3:
238 dst[dstidx] = res | pos;
239 dstidx++;
240 state = 0;
241 break;
242 default:
243 break;
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. */
252 switch (state)
254 case 0: /* Invalid = in first position */
255 case 1: /* Invalid = in second position */
256 return NULL;
257 case 2: /* Valid, means one byte of info */
258 /* Skip any number of spaces. */
259 while (srclen > 0)
261 srclen--;
262 ch = *src++;
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)
269 g_free(dst);
270 *dstlenp = 0;
271 return NULL;
273 /* FALLTHROUGH */
274 case 3: /* Valid, means two bytes of info */
276 * We know this char is an =. Is there anything but
277 * whitespace after it?
279 while (srclen > 0)
281 srclen--;
282 ch = *src++;
283 if (s_encoding_Base64_rank[ch] != 255)
285 g_free(dst);
286 *dstlenp = 0;
287 return NULL;
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.
296 if (res != 0)
298 g_free(dst);
299 *dstlenp = 0;
300 return NULL;
302 default:
303 break;
305 } else
308 * We ended by seeing the end of the string. Make sure we
309 * have no partial bytes lying around.
311 if (state != 0)
313 g_free(dst);
314 *dstlenp = 0;
315 return NULL;
318 dst[dstidx]=0;
319 *dstlenp = dstidx;
320 return dst;