Andreas Krennmair:
[apps/madmutt.git] / imap / utf7.c
1 /*
2  * Copyright (C) 2000 Edmund Grimley Evans <edmundo@rano.org>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #include "charset.h"
25 #include "imap_private.h"
26
27 static int Index_64[128] = {
28   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
29   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, 63, -1, -1, -1,
31   52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
32   -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
33   15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
34   -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
35   41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
36 };
37
38 static char B64Chars[64] = {
39   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
40   'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
41   'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
42   't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
43   '8', '9', '+', ','
44 };
45
46 /*
47  * Convert the data (u7,u7len) from RFC 2060's UTF-7 to UTF-8.
48  * The result is null-terminated and returned, and also stored
49  * in (*u8,*u8len) if u8 or u8len is non-zero.
50  * If input data is invalid, return 0 and don't store anything.
51  * RFC 2060 obviously intends the encoding to be unique (see
52  * point 5 in section 5.1.3), so we reject any non-canonical
53  * form, such as &ACY- (instead of &-) or &AMA-&AMA- (instead
54  * of &AMAAwA-).
55  */
56 static char *utf7_to_utf8 (const char *u7, size_t u7len, char **u8,
57                            size_t * u8len)
58 {
59   char *buf, *p;
60   int b, ch, k;
61
62   p = buf = safe_malloc (u7len + u7len / 8 + 1);
63
64   for (; u7len; u7++, u7len--) {
65     if (*u7 == '&') {
66       u7++, u7len--;
67
68       if (u7len && *u7 == '-') {
69         *p++ = '&';
70         continue;
71       }
72
73       ch = 0;
74       k = 10;
75       for (; u7len; u7++, u7len--) {
76         if ((*u7 & 0x80) || (b = Index_64[(int) *u7]) == -1)
77           break;
78         if (k > 0) {
79           ch |= b << k;
80           k -= 6;
81         }
82         else {
83           ch |= b >> (-k);
84           if (ch < 0x80) {
85             if (0x20 <= ch && ch < 0x7f)
86               /* Printable US-ASCII */
87               goto bail;
88             *p++ = ch;
89           }
90           else if (ch < 0x800) {
91             *p++ = 0xc0 | (ch >> 6);
92             *p++ = 0x80 | (ch & 0x3f);
93           }
94           else {
95             *p++ = 0xe0 | (ch >> 12);
96             *p++ = 0x80 | ((ch >> 6) & 0x3f);
97             *p++ = 0x80 | (ch & 0x3f);
98           }
99           ch = (b << (16 + k)) & 0xffff;
100           k += 10;
101         }
102       }
103       if (ch || k < 6)
104         /* Non-zero or too many extra bits */
105         goto bail;
106       if (!u7len || *u7 != '-')
107         /* BASE64 not properly terminated */
108         goto bail;
109       if (u7len > 2 && u7[1] == '&' && u7[2] != '-')
110         /* Adjacent BASE64 sections */
111         goto bail;
112     }
113     else if (*u7 < 0x20 || *u7 >= 0x7f)
114       /* Not printable US-ASCII */
115       goto bail;
116     else
117       *p++ = *u7;
118   }
119   *p++ = '\0';
120   if (u8len)
121     *u8len = p - buf;
122
123   safe_realloc (&buf, p - buf);
124   if (u8)
125     *u8 = buf;
126   return buf;
127
128 bail:
129   FREE (&buf);
130   return 0;
131 }
132
133 /*
134  * Convert the data (u8,u8len) from UTF-8 to RFC 2060's UTF-7.
135  * The result is null-terminated and returned, and also stored
136  * in (*u7,*u7len) if u7 or u7len is non-zero.
137  * Unicode characters above U+FFFF are replaced by U+FFFE.
138  * If input data is invalid, return 0 and don't store anything.
139  */
140 static char *utf8_to_utf7 (const char *u8, size_t u8len, char **u7,
141                            size_t * u7len)
142 {
143   char *buf, *p;
144   int ch;
145   int n, i, b = 0, k = 0;
146   int base64 = 0;
147
148   /*
149    * In the worst case we convert 2 chars to 7 chars. For example:
150    * "\x10&\x10&..." -> "&ABA-&-&ABA-&-...".
151    */
152   p = buf = safe_malloc ((u8len / 2) * 7 + 6);
153
154   while (u8len) {
155     unsigned char c = *u8;
156
157     if (c < 0x80)
158       ch = c, n = 0;
159     else if (c < 0xc2)
160       goto bail;
161     else if (c < 0xe0)
162       ch = c & 0x1f, n = 1;
163     else if (c < 0xf0)
164       ch = c & 0x0f, n = 2;
165     else if (c < 0xf8)
166       ch = c & 0x07, n = 3;
167     else if (c < 0xfc)
168       ch = c & 0x03, n = 4;
169     else if (c < 0xfe)
170       ch = c & 0x01, n = 5;
171     else
172       goto bail;
173
174     u8++, u8len--;
175     if (n > u8len)
176       goto bail;
177     for (i = 0; i < n; i++) {
178       if ((u8[i] & 0xc0) != 0x80)
179         goto bail;
180       ch = (ch << 6) | (u8[i] & 0x3f);
181     }
182     if (n > 1 && !(ch >> (n * 5 + 1)))
183       goto bail;
184     u8 += n, u8len -= n;
185
186     if (ch < 0x20 || ch >= 0x7f) {
187       if (!base64) {
188         *p++ = '&';
189         base64 = 1;
190         b = 0;
191         k = 10;
192       }
193       if (ch & ~0xffff)
194         ch = 0xfffe;
195       *p++ = B64Chars[b | ch >> k];
196       k -= 6;
197       for (; k >= 0; k -= 6)
198         *p++ = B64Chars[(ch >> k) & 0x3f];
199       b = (ch << (-k)) & 0x3f;
200       k += 16;
201     }
202     else {
203       if (base64) {
204         if (k > 10)
205           *p++ = B64Chars[b];
206         *p++ = '-';
207         base64 = 0;
208       }
209       *p++ = ch;
210       if (ch == '&')
211         *p++ = '-';
212     }
213   }
214
215   if (u8len) {
216     FREE (&buf);
217     return 0;
218   }
219
220   if (base64) {
221     if (k > 10)
222       *p++ = B64Chars[b];
223     *p++ = '-';
224   }
225
226   *p++ = '\0';
227   if (u7len)
228     *u7len = p - buf;
229   safe_realloc (&buf, p - buf);
230   if (u7)
231     *u7 = buf;
232   return buf;
233
234 bail:
235   FREE (&buf);
236   return 0;
237 }
238
239 void imap_utf7_encode (char **s)
240 {
241   if (Charset) {
242     char *t = safe_strdup (*s);
243
244     if (!mutt_convert_string (&t, Charset, "UTF-8", 0))
245       utf8_to_utf7 (t, strlen (t), s, 0);
246     FREE (&t);
247   }
248 }
249
250 void imap_utf7_decode (char **s)
251 {
252   if (Charset) {
253     char *t = utf7_to_utf8 (*s, strlen (*s), 0, 0);
254
255     if (t && !mutt_convert_string (&t, "UTF-8", Charset, 0)) {
256       FREE (s);
257       *s = t;
258     }
259   }
260 }