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