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