sync 099claws
[claws.git] / src / common / base64.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include <ctype.h>
22 #include <string.h>
23
24 #include "base64.h"
25
26 static const gchar base64char[64] =
27         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
28
29 static const gchar base64val[128] = {
30         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, 62, -1, -1, -1, 63,
33         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
34         -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
35         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
36         -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
37         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
38 };
39
40 #define BASE64VAL(c)    (isascii((guchar)c) ? base64val[(gint)(c)] : -1)
41
42 void base64_encode(gchar *out, const guchar *in, gint inlen)
43 {
44         const guchar *inp = in;
45         gchar *outp = out;
46
47         while (inlen >= 3) {
48                 *outp++ = base64char[(inp[0] >> 2) & 0x3f];
49                 *outp++ = base64char[((inp[0] & 0x03) << 4) |
50                                      ((inp[1] >> 4) & 0x0f)];
51                 *outp++ = base64char[((inp[1] & 0x0f) << 2) |
52                                      ((inp[2] >> 6) & 0x03)];
53                 *outp++ = base64char[inp[2] & 0x3f];
54
55                 inp += 3;
56                 inlen -= 3;
57         }
58
59         if (inlen > 0) {
60                 *outp++ = base64char[(inp[0] >> 2) & 0x3f];
61                 if (inlen == 1) {
62                         *outp++ = base64char[(inp[0] & 0x03) << 4];
63                         *outp++ = '=';
64                 } else {
65                         *outp++ = base64char[((inp[0] & 0x03) << 4) |
66                                              ((inp[1] >> 4) & 0x0f)];
67                         *outp++ = base64char[((inp[1] & 0x0f) << 2)];
68                 }
69                 *outp++ = '=';
70         }
71
72         *outp = '\0';
73 }
74
75 gint base64_decode(guchar *out, const gchar *in, gint inlen)
76 {
77         const gchar *inp = in;
78         guchar *outp = out;
79         gchar buf[4];
80
81         if (inlen < 0)
82                 inlen = G_MAXINT;
83
84         while (inlen >= 4 && *inp != '\0') {
85                 buf[0] = *inp++;
86                 inlen--;
87                 if (BASE64VAL(buf[0]) == -1) break;
88
89                 buf[1] = *inp++;
90                 inlen--;
91                 if (BASE64VAL(buf[1]) == -1) break;
92
93                 buf[2] = *inp++;
94                 inlen--;
95                 if (buf[2] != '=' && BASE64VAL(buf[2]) == -1) break;
96
97                 buf[3] = *inp++;
98                 inlen--;
99                 if (buf[3] != '=' && BASE64VAL(buf[3]) == -1) break;
100
101                 *outp++ = ((BASE64VAL(buf[0]) << 2) & 0xfc) |
102                           ((BASE64VAL(buf[1]) >> 4) & 0x03);
103                 if (buf[2] != '=') {
104                         *outp++ = ((BASE64VAL(buf[1]) & 0x0f) << 4) |
105                                   ((BASE64VAL(buf[2]) >> 2) & 0x0f);
106                         if (buf[3] != '=') {
107                                 *outp++ = ((BASE64VAL(buf[2]) & 0x03) << 6) |
108                                            (BASE64VAL(buf[3]) & 0x3f);
109                         }
110                 }
111         }
112
113         return outp - out;
114 }
115
116 Base64Decoder *base64_decoder_new(void)
117 {
118         Base64Decoder *decoder;
119
120         decoder = g_new0(Base64Decoder, 1);
121         return decoder;
122 }
123
124 void base64_decoder_free(Base64Decoder *decoder)
125 {
126         g_free(decoder);
127 }
128
129 gint base64_decoder_decode(Base64Decoder *decoder,
130                            const gchar *in, guchar *out)
131 {
132         gint len, total_len = 0;
133         gint buf_len;
134         gchar buf[4];
135
136         g_return_val_if_fail(decoder != NULL, -1);
137         g_return_val_if_fail(in != NULL, -1);
138         g_return_val_if_fail(out != NULL, -1);
139
140         buf_len = decoder->buf_len;
141         memcpy(buf, decoder->buf, sizeof(buf));
142
143         for (;;) {
144                 while (buf_len < 4) {
145                         gchar c = *in;
146
147                         in++;
148                         if (c == '\0') break;
149                         if (c == '\r' || c == '\n') continue;
150                         if (c != '=' && BASE64VAL(c) == -1)
151                                 return -1;
152                         buf[buf_len++] = c;
153                 }
154                 if (buf_len < 4 || buf[0] == '=' || buf[1] == '=') {
155                         decoder->buf_len = buf_len;
156                         memcpy(decoder->buf, buf, sizeof(buf));
157                         return total_len;
158                 }
159                 len = base64_decode(out, buf, 4);
160                 out += len;
161                 total_len += len;
162                 buf_len = 0;
163                 if (len < 3) {
164                         decoder->buf_len = 0;
165                         return total_len;
166                 }
167         }
168 }