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.
10 #include <lib-lib/lib-lib.h>
14 #include "imap_private.h"
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
26 static char *utf7_to_utf8 (const char *u7, size_t u7len, char **u8,
32 p = buf = p_new(char, u7len + u7len / 8 + 1);
34 for (; u7len; u7++, u7len--) {
38 if (u7len && *u7 == '-') {
45 for (; u7len; u7++, u7len--) {
46 if ((b = base64val(*u7)) < 0)
55 if (0x20 <= ch && ch < 0x7f)
56 /* Printable US-ASCII */
60 else if (ch < 0x800) {
61 *p++ = 0xc0 | (ch >> 6);
62 *p++ = 0x80 | (ch & 0x3f);
65 *p++ = 0xe0 | (ch >> 12);
66 *p++ = 0x80 | ((ch >> 6) & 0x3f);
67 *p++ = 0x80 | (ch & 0x3f);
69 ch = (b << (16 + k)) & 0xffff;
74 /* Non-zero or too many extra bits */
76 if (!u7len || *u7 != '-')
77 /* BASE64 not properly terminated */
79 if (u7len > 2 && u7[1] == '&' && u7[2] != '-')
80 /* Adjacent BASE64 sections */
83 else if (*u7 < 0x20 || *u7 >= 0x7f)
84 /* Not printable US-ASCII */
93 p_realloc(&buf, p - buf);
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.
110 static char *utf8_to_utf7 (const char *u8, ssize_t u8len, char **u7,
115 int n, i, b = 0, k = 0;
119 * In the worst case we convert 2 chars to 7 chars. For example:
120 * "\x10&\x10&..." -> "&ABA-&-&ABA-&-...".
122 p = buf = p_new(char, (u8len / 2) * 7 + 6);
125 unsigned char c = *u8;
132 ch = c & 0x1f, n = 1;
134 ch = c & 0x0f, n = 2;
136 ch = c & 0x07, n = 3;
138 ch = c & 0x03, n = 4;
140 ch = c & 0x01, n = 5;
147 for (i = 0; i < n; i++) {
148 if ((u8[i] & 0xc0) != 0x80)
150 ch = (ch << 6) | (u8[i] & 0x3f);
152 if (n > 1 && !(ch >> (n * 5 + 1)))
156 if (ch < 0x20 || ch >= 0x7f) {
165 *p++ = __m_b64chars[b | ch >> k];
167 for (; k >= 0; k -= 6)
168 *p++ = __m_b64chars[(ch >> k) & 0x3f];
169 b = (ch << (-k)) & 0x3f;
175 *p++ = __m_b64chars[b];
192 *p++ = __m_b64chars[b];
199 p_realloc(&buf, p - buf);
209 void imap_utf7_encode (char **s)
211 if (mod_cset.charset) {
212 char *t = m_strdup(*s);
214 if (!mutt_convert_string (&t, mod_cset.charset, "utf-8", 0)) {
215 char *u7 = utf8_to_utf7 (t, strlen (t), NULL, 0);
223 void imap_utf7_decode (char **s)
225 if (mod_cset.charset) {
226 char *t = utf7_to_utf8 (*s, m_strlen(*s), 0, 0);
228 if (t && !mutt_convert_string (&t, "utf-8", mod_cset.charset, 0)) {