2 * Copyright notice from original mutt:
3 * Copyright (C) 2000 Edmund Grimley Evans <edmundo@rano.org>
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.
14 #include <lib-lib/mem.h>
18 #include "imap_private.h"
21 * Convert the data (u7,u7len) from RFC 2060's UTF-7 to UTF-8.
22 * The result is null-terminated and returned, and also stored
23 * in (*u8,*u8len) if u8 or u8len is non-zero.
24 * If input data is invalid, return 0 and don't store anything.
25 * RFC 2060 obviously intends the encoding to be unique (see
26 * point 5 in section 5.1.3), so we reject any non-canonical
27 * form, such as &ACY- (instead of &-) or &AMA-&AMA- (instead
30 static char *utf7_to_utf8 (const char *u7, size_t u7len, char **u8,
36 p = buf = p_new(char, u7len + u7len / 8 + 1);
38 for (; u7len; u7++, u7len--) {
42 if (u7len && *u7 == '-') {
49 for (; u7len; u7++, u7len--) {
50 if ((b = base64val(*u7)) < 0)
59 if (0x20 <= ch && ch < 0x7f)
60 /* Printable US-ASCII */
64 else if (ch < 0x800) {
65 *p++ = 0xc0 | (ch >> 6);
66 *p++ = 0x80 | (ch & 0x3f);
69 *p++ = 0xe0 | (ch >> 12);
70 *p++ = 0x80 | ((ch >> 6) & 0x3f);
71 *p++ = 0x80 | (ch & 0x3f);
73 ch = (b << (16 + k)) & 0xffff;
78 /* Non-zero or too many extra bits */
80 if (!u7len || *u7 != '-')
81 /* BASE64 not properly terminated */
83 if (u7len > 2 && u7[1] == '&' && u7[2] != '-')
84 /* Adjacent BASE64 sections */
87 else if (*u7 < 0x20 || *u7 >= 0x7f)
88 /* Not printable US-ASCII */
97 p_realloc(&buf, p - buf);
108 * Convert the data (u8,u8len) from UTF-8 to RFC 2060's UTF-7.
109 * The result is null-terminated and returned, and also stored
110 * in (*u7,*u7len) if u7 or u7len is non-zero.
111 * Unicode characters above U+FFFF are replaced by U+FFFE.
112 * If input data is invalid, return 0 and don't store anything.
114 static char *utf8_to_utf7 (const char *u8, ssize_t u8len, char **u7,
119 int n, i, b = 0, k = 0;
123 * In the worst case we convert 2 chars to 7 chars. For example:
124 * "\x10&\x10&..." -> "&ABA-&-&ABA-&-...".
126 p = buf = p_new(char, (u8len / 2) * 7 + 6);
129 unsigned char c = *u8;
136 ch = c & 0x1f, n = 1;
138 ch = c & 0x0f, n = 2;
140 ch = c & 0x07, n = 3;
142 ch = c & 0x03, n = 4;
144 ch = c & 0x01, n = 5;
151 for (i = 0; i < n; i++) {
152 if ((u8[i] & 0xc0) != 0x80)
154 ch = (ch << 6) | (u8[i] & 0x3f);
156 if (n > 1 && !(ch >> (n * 5 + 1)))
160 if (ch < 0x20 || ch >= 0x7f) {
169 *p++ = __m_b64chars[b | ch >> k];
171 for (; k >= 0; k -= 6)
172 *p++ = __m_b64chars[(ch >> k) & 0x3f];
173 b = (ch << (-k)) & 0x3f;
179 *p++ = __m_b64chars[b];
196 *p++ = __m_b64chars[b];
203 p_realloc(&buf, p - buf);
213 void imap_utf7_encode (char **s)
216 char *t = m_strdup(*s);
218 if (!mutt_convert_string (&t, Charset, "UTF-8", 0)) {
219 char *u7 = utf8_to_utf7 (t, strlen (t), NULL, 0);
227 void imap_utf7_decode (char **s)
230 char *t = utf7_to_utf8 (*s, m_strlen(*s), 0, 0);
232 if (t && !mutt_convert_string (&t, "UTF-8", Charset, 0)) {