masqmail-0.2

view src/base64/base64.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children 805abb75b20c
line source
1 /* base64.c, Copyright 2000 (C) Oliver Kurth,
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
18 /*
19 send bugs to: kurth@innominate.de
20 */
22 /* see also RFC 1341 */
24 #include <glib.h>
25 #include <string.h>
26 #include "base64.h"
28 gchar*
29 base64_encode(guchar * buf, gint len)
30 {
31 guchar *outbuf, *q;
32 gchar enc[64];
33 gint i = 0, j = 0;
34 guint in0, in1, in2;
36 for (; i < 26; i++)
37 enc[i] = (gchar) ('A' + j++);
38 j = 0;
39 for (; i < 52; i++)
40 enc[i] = (gchar) ('a' + j++);
41 j = 0;
42 for (; i < 62; i++)
43 enc[i] = (gchar) ('0' + j++);
44 enc[i++] = '+';
45 enc[i++] = '/';
47 outbuf = g_malloc(((len + 3) * 8) / 6);
48 q = outbuf;
50 i = 0;
51 while (i < len - 2) {
52 in0 = buf[i++];
53 in1 = buf[i++];
54 in2 = buf[i++];
56 *(q++) = enc[(in0 >> 2) & 0x3f];
57 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
58 *(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
59 *(q++) = enc[in2 & 0x3f];
60 }
61 if ((len - i) == 1) {
62 in0 = buf[i++];
63 *(q++) = enc[(in0 >> 2) & 0x3f];
64 *(q++) = enc[(in0 << 4) & 0x3f];
65 *(q++) = '=';
66 *(q++) = '=';
67 } else if ((len - i) == 2) {
68 in0 = buf[i++];
69 in1 = buf[i++];
70 *(q++) = enc[(in0 >> 2) & 0x3f];
71 *(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
72 *(q++) = enc[(in1 << 2) & 0x3f];
73 *(q++) = '=';
74 }
75 *q = 0;
77 return outbuf;
78 }
80 gchar *base64_decode(gchar * buf, gint * size)
81 {
82 guchar *p = buf, *q;
83 guint in[4];
84 /* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */
85 gchar *out = g_malloc((strlen(buf) + 3) + 1);
87 q = out;
88 *size = 0;
90 *q = 0;
92 while (*p) {
93 int i = 0;
94 while (i < 4) {
95 if (!*p)
96 break;
97 if ((*p >= 'A') && (*p <= 'Z'))
98 in[i++] = *p - 'A';
99 else if ((*p >= 'a') && (*p <= 'z'))
100 in[i++] = (*p - 'a') + 26;
101 else if ((*p >= '0') && (*p <= '9'))
102 in[i++] = (*p - '0') + 52;
103 else if (*p == '+')
104 in[i++] = 62;
105 else if (*p == '/')
106 in[i++] = 63;
107 else if (*p == '=') {
108 in[i++] = 0;
109 p++;
110 break;
111 } else if ((*p != '\r') && (*p != '\n')) {
112 p++;
113 break;
114 }
115 p++;
116 }
117 if ((i == 4) || (p[-1] == '=')) {
118 *(q++) = ((in[0] << 2) | (in[1] >> 4));
119 *(q++) = ((in[1] << 4) | (in[2] >> 2));
120 *(q++) = ((in[2] << 6) | in[3]);
121 if (p[-1] == '=') {
122 if (i == 3) {
123 (*size)++;
124 } else if (i == 4) {
125 (*size) += 2;
126 }
127 } else {
128 *size += 3;
129 }
130 }
131 }
132 return out;
133 }