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