Initial import of mutt-ng.
[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
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 = safe_malloc (u7len + u7len / 8 + 1);
58
59   for (; u7len; u7++, u7len--)
60   {
61     if (*u7 == '&')
62     {
63       u7++, u7len--;
64
65       if (u7len && *u7 == '-')
66       {
67         *p++ = '&';
68         continue;
69       }
70
71       ch = 0;
72       k = 10;
73       for (; u7len; u7++, u7len--)
74       {
75         if ((*u7 & 0x80) || (b = Index_64[(int)*u7]) == -1)
76           break;
77         if (k > 0)
78         {
79           ch |= b << k;
80           k -= 6;
81         }
82         else
83         {
84           ch |= b >> (-k);
85           if (ch < 0x80)
86           {
87             if (0x20 <= ch && ch < 0x7f)
88               /* Printable US-ASCII */
89               goto bail;
90             *p++ = ch;
91           }
92           else if (ch < 0x800)
93           {
94             *p++ = 0xc0 | (ch >> 6);
95             *p++ = 0x80 | (ch & 0x3f);
96           }
97           else
98           {
99             *p++ = 0xe0 | (ch >> 12);
100             *p++ = 0x80 | ((ch >> 6) & 0x3f);
101             *p++ = 0x80 | (ch & 0x3f);
102           }
103           ch = (b << (16 + k)) & 0xffff;
104           k += 10;
105         }
106       }
107       if (ch || k < 6)
108         /* Non-zero or too many extra bits */
109         goto bail;
110       if (!u7len || *u7 != '-')
111         /* BASE64 not properly terminated */
112         goto bail;
113       if (u7len > 2 && u7[1] == '&' && u7[2] != '-')
114         /* Adjacent BASE64 sections */
115         goto bail;
116     }
117     else if (*u7 < 0x20 || *u7 >= 0x7f)
118       /* Not printable US-ASCII */
119       goto bail;
120     else
121       *p++ = *u7;
122   }
123   *p++ = '\0';
124   if (u8len)
125     *u8len = p - buf;
126
127   safe_realloc (&buf, p - buf);
128   if (u8)
129     *u8 = buf;
130   return buf;
131
132  bail:
133   FREE (&buf);
134   return 0;
135 }
136
137 /*
138  * Convert the data (u8,u8len) from UTF-8 to RFC 2060's UTF-7.
139  * The result is null-terminated and returned, and also stored
140  * in (*u7,*u7len) if u7 or u7len is non-zero.
141  * Unicode characters above U+FFFF are replaced by U+FFFE.
142  * If input data is invalid, return 0 and don't store anything.
143  */
144 static char *utf8_to_utf7 (const char *u8, size_t u8len, char **u7,
145   size_t *u7len)
146 {
147   char *buf, *p;
148   int ch;
149   int n, i, b = 0, k = 0;
150   int base64 = 0;
151
152   /*
153    * In the worst case we convert 2 chars to 7 chars. For example:
154    * "\x10&\x10&..." -> "&ABA-&-&ABA-&-...".
155    */
156   p = buf = safe_malloc ((u8len / 2) * 7 + 6);
157
158   while (u8len)
159   {
160     unsigned char c = *u8;
161
162     if (c < 0x80)
163       ch = c, n = 0;
164     else if (c < 0xc2)
165       goto bail;
166     else if (c < 0xe0)
167       ch = c & 0x1f, n = 1;
168     else if (c < 0xf0)
169       ch = c & 0x0f, n = 2;
170     else if (c < 0xf8)
171       ch = c & 0x07, n = 3;
172     else if (c < 0xfc)
173       ch = c & 0x03, n = 4;
174     else if (c < 0xfe)
175       ch = c & 0x01, n = 5;
176     else
177       goto bail;
178
179     u8++, u8len--;
180     if (n > u8len)
181       goto bail;
182     for (i = 0; i < n; i++)
183     {
184       if ((u8[i] & 0xc0) != 0x80)
185         goto bail;
186       ch = (ch << 6) | (u8[i] & 0x3f);
187     }
188     if (n > 1 && !(ch >> (n * 5 + 1)))
189       goto bail;
190     u8 += n, u8len -= n;
191
192     if (ch < 0x20 || ch >= 0x7f)
193     {
194       if (!base64)
195       {
196         *p++ = '&';
197         base64 = 1;
198         b = 0;
199         k = 10;
200       }
201       if (ch & ~0xffff)
202         ch = 0xfffe;
203       *p++ = B64Chars[b | ch >> k];
204       k -= 6;
205       for (; k >= 0; k -= 6)
206         *p++ = B64Chars[(ch >> k) & 0x3f];
207       b = (ch << (-k)) & 0x3f;
208       k += 16;
209     }
210     else
211     {
212       if (base64)
213       {
214         if (k > 10)
215           *p++ = B64Chars[b];
216         *p++ = '-';
217         base64 = 0;
218       }
219       *p++ = ch;
220       if (ch == '&')
221         *p++ = '-';
222     }
223   }
224
225   if (u8len)
226   {
227     FREE (&buf);
228     return 0;
229   }
230
231   if (base64)
232   {
233     if (k > 10)
234       *p++ = B64Chars[b];
235     *p++ = '-';
236   }
237
238   *p++ = '\0';
239   if (u7len)
240     *u7len = p - buf;
241   safe_realloc (&buf, p - buf);
242   if (u7)  *u7 = buf;
243   return buf;
244
245  bail:
246   FREE (&buf);
247   return 0;
248 }
249
250 void imap_utf7_encode (char **s)
251 {
252   if (Charset)
253   {
254     char *t = safe_strdup (*s);
255     if (!mutt_convert_string (&t, Charset, "UTF-8", 0))
256       utf8_to_utf7 (t, strlen (t), s, 0);
257     FREE (&t);
258   }
259 }
260
261 void imap_utf7_decode (char **s)
262 {
263   if (Charset)
264   {
265     char *t = utf7_to_utf8 (*s, strlen (*s), 0, 0);
266     if (t && !mutt_convert_string (&t, "UTF-8", Charset, 0))
267     {
268       FREE (s);
269       *s = t;
270     }
271   }
272 }