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