Move o_redraw_single() from libgeda to gschem
[geda-gaf/peter-b.git] / libgeda / src / s_encoding.c
blobc1df554c3be3c6288b5af490d6d11c953d20198e
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., 675 Mass Ave, Cambridge, MA 02139, USA.
31 ***********************************************************************
32 \endverbatim
34 * Taken from gnet's sources
35 * Modified the name of the functions and some variables
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
41 #include <ctype.h>
42 #include <glib.h>
43 #ifdef HAVE_STRING_H
44 #include <string.h>
45 #endif
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)
84 gchar* dst;
85 guint dstpos;
86 guchar input[3];
87 guchar output[4];
88 guint ocnt;
89 guint i;
91 if (srclen == 0)
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;
97 if (strict)
98 *dstlenp += (*dstlenp / 72); /* Handle trailing \n */
100 dst = g_new(gchar, *dstlenp );
102 /* bulk encoding */
103 dstpos = 0;
104 ocnt = 0;
105 while (srclen >= 3)
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
116 input[0] = *src++;
117 input[1] = *src++;
118 input[2] = *src++;
119 srclen -= 3;
121 output[0] = (input[0] >> 2);
122 output[1] = ((input[0] & 0x03) << 4) +
123 (input[1] >> 4);
124 output[2] = ((input[1] & 0x0f) << 2) +
125 (input[2] >> 6);
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 */
137 if (strict)
138 if ((++ocnt % (72/4)) == 0)
139 dst[dstpos++] = '\n';
142 /* Now worry about padding with remaining 1 or 2 bytes */
143 if (srclen != 0)
145 input[0] = input[1] = input[2] = '\0';
146 for (i = 0; i < srclen; i++)
147 input[i] = *src++;
149 output[0] = (input[0] >> 2);
150 output[1] = ((input[0] & 0x03) << 4) +
151 (input[1] >> 4);
152 output[2] = ((input[1] & 0x0f) << 2) +
153 (input[2] >> 6);
155 g_assert ((dstpos + 4) < *dstlenp);
157 dst[dstpos++] = s_encoding_Base64[(guint) output[0]];
158 dst[dstpos++] = s_encoding_Base64[(guint) output[1]];
160 if (srclen == 1)
161 dst[dstpos++] = s_encoding_Pad64;
162 else
163 dst[dstpos++] = s_encoding_Base64[(guint) output[2]];
165 dst[dstpos++] = s_encoding_Pad64;
168 g_assert (dstpos <= *dstlenp);
170 dst[dstpos] = '\0';
172 *dstlenp = dstpos + 1;
174 return dst;
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
181 * symbols.
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
188 * of that buffer.
190 gchar *s_encoding_base64_decode (gchar* src, guint srclen, guint* dstlenp)
193 gchar* dst;
194 guint dstidx, state, ch = 0;
195 gchar res;
196 guchar pos;
198 if (srclen == 0)
199 srclen = strlen(src);
200 state = 0;
201 dstidx = 0;
202 res = 0;
204 dst = g_new(gchar, srclen+1);
205 *dstlenp = srclen+1;
207 while (srclen > 0)
209 srclen--;
210 ch = *src++;
211 if (ch == s_encoding_Pad64)
212 break;
213 if (s_encoding_Base64_rank[ch]==255) /* Skip any non-base64 anywhere */
214 continue;
216 pos = s_encoding_Base64_rank[ch];
218 switch (state)
220 case 0:
221 if (dst != NULL)
223 dst[dstidx] = (pos << 2);
225 state = 1;
226 break;
227 case 1:
228 if (dst != NULL)
230 dst[dstidx] |= (pos >> 4);
231 res = ((pos & 0x0f) << 4);
233 dstidx++;
234 state = 2;
235 break;
236 case 2:
237 if (dst != NULL)
239 dst[dstidx] = res | (pos >> 2);
240 res = (pos & 0x03) << 6;
242 dstidx++;
243 state = 3;
244 break;
245 case 3:
246 if (dst != NULL)
248 dst[dstidx] = res | pos;
250 dstidx++;
251 state = 0;
252 break;
253 default:
254 break;
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. */
263 switch (state)
265 case 0: /* Invalid = in first position */
266 case 1: /* Invalid = in second position */
267 return NULL;
268 case 2: /* Valid, means one byte of info */
269 /* Skip any number of spaces. */
270 while (srclen > 0)
272 srclen--;
273 ch = *src++;
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)
280 g_free(dst);
281 *dstlenp = 0;
282 return NULL;
284 /* FALLTHROUGH */
285 case 3: /* Valid, means two bytes of info */
287 * We know this char is an =. Is there anything but
288 * whitespace after it?
290 while (srclen > 0)
292 srclen--;
293 ch = *src++;
294 if (s_encoding_Base64_rank[ch] != 255)
296 g_free(dst);
297 *dstlenp = 0;
298 return NULL;
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)
309 g_free(dst);
310 *dstlenp = 0;
311 return NULL;
313 default:
314 break;
316 } else
319 * We ended by seeing the end of the string. Make sure we
320 * have no partial bytes lying around.
322 if (state != 0)
324 g_free(dst);
325 *dstlenp = 0;
326 return NULL;
329 dst[dstidx]=0;
330 *dstlenp = dstidx;
331 return dst;