2011-02-16 [paul] 3.7.8cvs57
[claws.git] / src / common / base64.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #include <glib.h>
21 #include <ctype.h>
22 #include <string.h>
23
24 #include "base64.h"
25 #include "utils.h"
26
27 static const gchar base64char[64] =
28         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
29
30 static const signed char base64val[128] = {
31         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
34         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
35         -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
36         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
37         -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
38         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
39 };
40
41 #define BASE64VAL(c)    (IS_ASCII(c) ? base64val[(gint) (c)] : -1)
42
43 void base64_encode(gchar *out, const guchar *in, gint inlen)
44 {
45         const guchar *inp = in;
46         gchar *outp = out;
47
48         while (inlen >= 3) {
49                 *outp++ = base64char[(inp[0] >> 2) & 0x3f];
50                 *outp++ = base64char[((inp[0] & 0x03) << 4) |
51                                      ((inp[1] >> 4) & 0x0f)];
52                 *outp++ = base64char[((inp[1] & 0x0f) << 2) |
53                                      ((inp[2] >> 6) & 0x03)];
54                 *outp++ = base64char[inp[2] & 0x3f];
55
56                 inp += 3;
57                 inlen -= 3;
58         }
59
60         if (inlen > 0) {
61                 *outp++ = base64char[(inp[0] >> 2) & 0x3f];
62                 if (inlen == 1) {
63                         *outp++ = base64char[(inp[0] & 0x03) << 4];
64                         *outp++ = '=';
65                 } else {
66                         *outp++ = base64char[((inp[0] & 0x03) << 4) |
67                                              ((inp[1] >> 4) & 0x0f)];
68                         *outp++ = base64char[((inp[1] & 0x0f) << 2)];
69                 }
70                 *outp++ = '=';
71         }
72
73         *outp = '\0';
74 }
75
76 gint base64_decode(guchar *out, const gchar *in, gint inlen)
77 {
78         const gchar *inp = in;
79         guchar *outp = out;
80         gchar buf[4];
81
82         if (inlen < 0)
83                 inlen = G_MAXINT;
84
85         while (inlen >= 4 && *inp != '\0') {
86                 buf[0] = *inp++;
87                 inlen--;
88                 if (BASE64VAL(buf[0]) == -1) break;
89
90                 buf[1] = *inp++;
91                 inlen--;
92                 if (BASE64VAL(buf[1]) == -1) break;
93
94                 buf[2] = *inp++;
95                 inlen--;
96                 if (buf[2] != '=' && BASE64VAL(buf[2]) == -1) break;
97
98                 buf[3] = *inp++;
99                 inlen--;
100                 if (buf[3] != '=' && BASE64VAL(buf[3]) == -1) break;
101
102                 *outp++ = ((BASE64VAL(buf[0]) << 2) & 0xfc) |
103                           ((BASE64VAL(buf[1]) >> 4) & 0x03);
104                 if (buf[2] != '=') {
105                         *outp++ = ((BASE64VAL(buf[1]) & 0x0f) << 4) |
106                                   ((BASE64VAL(buf[2]) >> 2) & 0x0f);
107                         if (buf[3] != '=') {
108                                 *outp++ = ((BASE64VAL(buf[2]) & 0x03) << 6) |
109                                            (BASE64VAL(buf[3]) & 0x3f);
110                         }
111                 }
112         }
113
114         return outp - out;
115 }
116
117 Base64Decoder *base64_decoder_new(void)
118 {
119         Base64Decoder *decoder;
120
121         decoder = g_new0(Base64Decoder, 1);
122         return decoder;
123 }
124
125 void base64_decoder_free(Base64Decoder *decoder)
126 {
127         g_free(decoder);
128 }
129
130 gint base64_decoder_decode(Base64Decoder *decoder,
131                            const gchar *in, guchar *out, gint inlen)
132 {
133         const gchar *in_end = in + inlen;
134         gint len, total_len = 0;
135         gboolean in_more = inlen > 0;
136         gboolean got_eq = FALSE;
137         gint buf_len;
138         gchar buf[4];
139
140         cm_return_val_if_fail(decoder != NULL, -1);
141         cm_return_val_if_fail(in != NULL, -1);
142         cm_return_val_if_fail(out != NULL, -1);
143
144         /* Start with previous saved tail */    
145         buf_len = decoder->buf_len;
146         memcpy(buf, decoder->buf, sizeof(buf));
147
148         while (in_more) {
149                 while (buf_len < 4 && in_more) {
150                         gchar c = *in;
151
152                         in++;
153                         got_eq = (c == '=');
154                         if (got_eq || BASE64VAL(c) >= 0)
155                                 buf[buf_len++] = c;
156                         in_more = (in < in_end) && !(got_eq && (buf_len == 4));
157                 }
158                 if (buf_len == 4) {
159                         len = base64_decode(out, buf, 4);
160                         out += len;
161                         total_len += len;
162                         buf_len = 0;
163                 }
164         }
165         if (buf_len < 4) { 
166                 /* Save tail for next iteration call. It wll be ignored if ends here. */
167                 decoder->buf_len = buf_len;
168                 memcpy(decoder->buf, buf, buf_len);
169         }
170         return total_len;
171 }