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