2 * Copyright notice from original mutt:
3 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.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.
20 #include <sys/types.h>
25 #include <lib-lib/mem.h>
26 #include <lib-lib/ascii.h>
27 #include <lib-lib/str.h>
28 #include <lib-lib/macros.h>
35 # define EILSEQ EINVAL
39 * The following list has been created manually from the data under:
40 * http://www.isi.edu/in-notes/iana/assignments/character-sets
41 * Last update: 2000-09-07
43 * Note that it includes only the subset of character sets for which
44 * a preferred MIME name is given.
50 } PreferredMIMENames[] = {
52 "ansi_x3.4-1968", "us-ascii"}, {
53 "iso-ir-6", "us-ascii"}, {
54 "iso_646.irv:1991", "us-ascii"}, {
55 "ascii", "us-ascii"}, {
56 "iso646-us", "us-ascii"}, {
58 "ibm367", "us-ascii"}, {
59 "cp367", "us-ascii"}, {
60 "csASCII", "us-ascii"}, {
61 "csISO2022KR", "iso-2022-kr"}, {
62 "csEUCKR", "euc-kr"}, {
63 "csISO2022JP", "iso-2022-jp"}, {
64 "csISO2022JP2", "iso-2022-jp-2"}, {
65 "ISO_8859-1:1987", "iso-8859-1"}, {
66 "iso-ir-100", "iso-8859-1"}, {
67 "iso_8859-1", "iso-8859-1"}, {
68 "latin1", "iso-8859-1"}, {
69 "l1", "iso-8859-1"}, {
70 "IBM819", "iso-8859-1"}, {
71 "CP819", "iso-8859-1"}, {
72 "csISOLatin1", "iso-8859-1"}, {
73 "ISO_8859-2:1987", "iso-8859-2"}, {
74 "iso-ir-101", "iso-8859-2"}, {
75 "iso_8859-2", "iso-8859-2"}, {
76 "latin2", "iso-8859-2"}, {
77 "l2", "iso-8859-2"}, {
78 "csISOLatin2", "iso-8859-2"}, {
79 "ISO_8859-3:1988", "iso-8859-3"}, {
80 "iso-ir-109", "iso-8859-3"}, {
81 "ISO_8859-3", "iso-8859-3"}, {
82 "latin3", "iso-8859-3"}, {
83 "l3", "iso-8859-3"}, {
84 "csISOLatin3", "iso-8859-3"}, {
85 "ISO_8859-4:1988", "iso-8859-4"}, {
86 "iso-ir-110", "iso-8859-4"}, {
87 "ISO_8859-4", "iso-8859-4"}, {
88 "latin4", "iso-8859-4"}, {
89 "l4", "iso-8859-4"}, {
90 "csISOLatin4", "iso-8859-4"}, {
91 "ISO_8859-6:1987", "iso-8859-6"}, {
92 "iso-ir-127", "iso-8859-6"}, {
93 "iso_8859-6", "iso-8859-6"}, {
94 "ECMA-114", "iso-8859-6"}, {
95 "ASMO-708", "iso-8859-6"}, {
96 "arabic", "iso-8859-6"}, {
97 "csISOLatinArabic", "iso-8859-6"}, {
98 "ISO_8859-7:1987", "iso-8859-7"}, {
99 "iso-ir-126", "iso-8859-7"}, {
100 "ISO_8859-7", "iso-8859-7"}, {
101 "ELOT_928", "iso-8859-7"}, {
102 "ECMA-118", "iso-8859-7"}, {
103 "greek", "iso-8859-7"}, {
104 "greek8", "iso-8859-7"}, {
105 "csISOLatinGreek", "iso-8859-7"}, {
106 "ISO_8859-8:1988", "iso-8859-8"}, {
107 "iso-ir-138", "iso-8859-8"}, {
108 "ISO_8859-8", "iso-8859-8"}, {
109 "hebrew", "iso-8859-8"}, {
110 "csISOLatinHebrew", "iso-8859-8"}, {
111 "ISO_8859-5:1988", "iso-8859-5"}, {
112 "iso-ir-144", "iso-8859-5"}, {
113 "ISO_8859-5", "iso-8859-5"}, {
114 "cyrillic", "iso-8859-5"}, {
115 "csISOLatinCyrillic", "iso8859-5"}, {
116 "ISO_8859-9:1989", "iso-8859-9"}, {
117 "iso-ir-148", "iso-8859-9"}, {
118 "ISO_8859-9", "iso-8859-9"}, {
119 "latin5", "iso-8859-9"}, /* this is not a bug */
121 "l5", "iso-8859-9"}, {
122 "csISOLatin5", "iso-8859-9"}, {
123 "ISO_8859-10:1992", "iso-8859-10"}, {
124 "iso-ir-157", "iso-8859-10"}, {
125 "latin6", "iso-8859-10"}, /* this is not a bug */
127 "l6", "iso-8859-10"}, {
128 "csISOLatin6", "iso-8859-10"}, {
129 "csKOI8r", "koi8-r"}, {
130 "MS_Kanji", "Shift_JIS"}, /* Note the underscore! */
132 "csShiftJis", "Shift_JIS"}, {
133 "Extended_UNIX_Code_Packed_Format_for_Japanese", "EUC-JP"}, {
134 "csEUCPkdFmtJapanese", "EUC-JP"}, {
135 "csGB2312", "gb2312"}, {
138 * End of official brain damage. What follows has been taken
139 * from glibc's localedata files.
142 "iso_8859-13", "iso-8859-13"}, {
143 "iso-ir-179", "iso-8859-13"}, {
144 "latin7", "iso-8859-13"}, /* this is not a bug */
146 "l7", "iso-8859-13"}, {
147 "iso_8859-14", "iso-8859-14"}, {
148 "latin8", "iso-8859-14"}, /* this is not a bug */
150 "l8", "iso-8859-14"}, {
151 "iso_8859-15", "iso-8859-15"}, {
152 "latin9", "iso-8859-15"}, /* this is not a bug */
153 /* Suggested by Ionel Mugurel Ciobica <tgakic@sg10.chem.tue.nl> */
155 "latin0", "iso-8859-15"}, /* this is not a bug */
157 "iso_8859-16", "iso-8859-16"}, {
158 "latin10", "iso-8859-16"}, /* this is not a bug */
160 * David Champion <dgc@uchicago.edu> has observed this with
161 * nl_langinfo under SunOS 5.8.
166 * http://www.sun.com/software/white-papers/wp-unicode/
169 "eucJP", "euc-jp"}, {
170 "PCK", "Shift_JIS"}, {
171 "ko_KR-euc", "euc-kr"}, {
172 "zh_TW-big5", "big5"},
173 /* seems to be common on some systems */
175 "sjis", "Shift_JIS"}, {
176 "euc-jp-ms", "eucJP-ms"},
178 * If you happen to encounter system-specific brain-damage with
179 * respect to character set naming, please add it above this
180 * comment, and submit a patch to <mutt-dev@mutt.org>.
182 /* End of aliases. Please keep this line last. */
187 #ifdef HAVE_LANGINFO_CODESET
188 # include <langinfo.h>
191 void mutt_set_langinfo_charset (void)
193 char buff[LONG_STRING];
194 char buff2[LONG_STRING];
196 strfcpy (buff, nl_langinfo (CODESET), sizeof (buff));
197 mutt_canonical_charset (buff2, sizeof (buff2), buff);
199 /* finally, set $charset */
200 if (!(Charset = m_strdup(buff2)))
201 Charset = m_strdup("iso-8859-1");
206 void mutt_set_langinfo_charset (void)
208 Charset = m_strdup("iso-8859-1");
213 void mutt_canonical_charset (char *dest, size_t dlen, const char *name)
217 char scratch[LONG_STRING];
219 /* catch some common iso-8859-something misspellings */
220 if (!ascii_strncasecmp (name, "8859", 4) && name[4] != '-')
221 snprintf (scratch, sizeof (scratch), "iso-8859-%s", name + 4);
222 else if (!ascii_strncasecmp (name, "8859-", 5))
223 snprintf (scratch, sizeof (scratch), "iso-8859-%s", name + 5);
224 else if (!ascii_strncasecmp (name, "iso8859", 7) && name[7] != '-')
225 snprintf (scratch, sizeof (scratch), "iso_8859-%s", name + 7);
226 else if (!ascii_strncasecmp (name, "iso8859-", 8))
227 snprintf (scratch, sizeof (scratch), "iso_8859-%s", name + 8);
229 strfcpy (scratch, NONULL (name), sizeof (scratch));
231 for (i = 0; PreferredMIMENames[i].key; i++)
232 if (!ascii_strcasecmp (scratch, PreferredMIMENames[i].key) ||
233 !str_casecmp (scratch, PreferredMIMENames[i].key)) {
234 strfcpy (dest, PreferredMIMENames[i].pref, dlen);
238 strfcpy (dest, scratch, dlen);
240 /* for cosmetics' sake, transform to lowercase. */
241 for (p = dest; *p; p++)
242 *p = ascii_tolower (*p);
245 int mutt_chscmp (const char *s, const char *chs)
252 mutt_canonical_charset (buffer, sizeof (buffer), s);
253 return !ascii_strcasecmp (buffer, chs);
258 * Like iconv_open, but canonicalises the charsets
261 iconv_t mutt_iconv_open (const char *tocode, const char *fromcode, int flags)
263 char tocode1[SHORT_STRING];
264 char fromcode1[SHORT_STRING];
265 char *tocode2, *fromcode2;
270 mutt_canonical_charset (tocode1, sizeof (tocode1), tocode);
272 #ifdef M_ICONV_HOOK_TO
274 if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook (tocode1)))
275 mutt_canonical_charset (tocode1, sizeof (tocode1), tmp);
278 mutt_canonical_charset (fromcode1, sizeof (fromcode1), fromcode);
279 if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook (fromcode1)))
280 mutt_canonical_charset (fromcode1, sizeof (fromcode1), tmp);
282 if ((cd = iconv_open (tocode1, fromcode1)) != (iconv_t) - 1)
284 if ((tocode2 = mutt_iconv_hook (tocode1))
285 && (fromcode2 = mutt_iconv_hook (fromcode1)))
286 return iconv_open (tocode2, fromcode2);
288 return (iconv_t) - 1;
293 * Like iconv, but keeps going even when the input is invalid
294 * If you're supplying inrepls, the source charset should be stateless;
295 * if you're supplying an outrepl, the target charset should be.
298 size_t mutt_iconv (iconv_t cd, const char **inbuf, size_t * inbytesleft,
299 char **outbuf, size_t * outbytesleft,
300 const char **inrepls, const char *outrepl)
302 size_t ret = 0, ret1;
303 const char *ib = *inbuf;
304 size_t ibl = *inbytesleft;
306 size_t obl = *outbytesleft;
309 ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
310 if (ret1 != (size_t) - 1)
312 if (ibl && obl && errno == EILSEQ) {
314 /* Try replacing the input */
317 for (t = inrepls; *t; t++) {
318 const char *ib1 = *t;
319 size_t ibl1 = m_strlen(*t);
323 my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
326 ob = ob1, obl = obl1;
334 /* Replace the output */
337 my_iconv(cd, 0, 0, &ob, &obl);
339 int n = m_strlen(outrepl);
345 memcpy (ob, outrepl, n);
349 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
353 *inbuf = ib, *inbytesleft = ibl;
354 *outbuf = ob, *outbytesleft = obl;
362 * Used in rfc2047.c and rfc2231.c
365 int mutt_convert_string (char **ps, const char *from, const char *to,
369 const char *repls[] = { "\357\277\275", "?", 0 };
375 if (to && from && (cd = mutt_iconv_open (to, from, flags)) != (iconv_t) - 1) {
380 const char **inrepls = NULL;
381 const char *outrepl = NULL;
383 if (mutt_is_utf8 (to))
384 outrepl = "\357\277\275";
385 else if (mutt_is_utf8 (from))
391 ib = s, ibl = len + 1;
392 obl = MB_LEN_MAX * ibl;
393 ob = buf = xmalloc(obl + 1);
395 mutt_iconv (cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
412 * FGETCONV stuff for converting a file while reading it
413 * Used in sendlib.c for converting from mutt's Charset
425 const char **inrepls;
428 struct fgetconv_not {
433 FGETCONV *fgetconv_open (FILE * file, const char *from, const char *to,
436 struct fgetconv_s *fc;
437 iconv_t cd = (iconv_t) - 1;
438 static const char *repls[] = { "\357\277\275", "?", 0 };
441 cd = mutt_iconv_open (to, from, flags);
443 if (cd != (iconv_t) - 1) {
444 fc = p_new(struct fgetconv_s, 1);
445 fc->p = fc->ob = fc->bufo;
448 fc->inrepls = mutt_is_utf8 (to) ? repls : repls + 1;
451 fc = p_new(struct fgetconv_not, 1);
454 return (FGETCONV *) fc;
457 char *fgetconvs (char *buf, size_t l, FGETCONV * _fc)
462 for (r = 0; r + 1 < l;) {
463 if ((c = fgetconv (_fc)) == EOF)
477 int fgetconv (FGETCONV * _fc)
479 struct fgetconv_s *fc = (struct fgetconv_s *) _fc;
483 if (fc->cd == (iconv_t) - 1)
484 return fgetc (fc->file);
488 return (unsigned char) *(fc->p)++;
490 /* Try to convert some more */
491 fc->p = fc->ob = fc->bufo;
493 size_t obl = sizeof (fc->bufo);
495 my_iconv(fc->cd, (const char **) &fc->ib, &fc->ibl, &fc->ob, &obl);
497 return (unsigned char) *(fc->p)++;
500 /* If we trusted iconv a bit more, we would at this point
501 * ask why it had stopped converting ... */
503 /* Try to read some more */
504 if (fc->ibl == sizeof (fc->bufi) ||
505 (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof (fc->bufi))) {
510 memcpy (fc->bufi, fc->ib, fc->ibl);
513 fread (fc->ib + fc->ibl, 1, sizeof (fc->bufi) - fc->ibl, fc->file);
515 /* Try harder this time to convert some */
517 size_t obl = sizeof (fc->bufo);
519 mutt_iconv (fc->cd, (const char **) &fc->ib, &fc->ibl, &fc->ob,
520 &obl, fc->inrepls, 0);
522 return (unsigned char) *(fc->p)++;
525 /* Either the file has finished or one of the buffers is too small */
530 void fgetconv_close (FGETCONV ** _fc)
532 struct fgetconv_s *fc = (struct fgetconv_s *) *_fc;
534 if (fc->cd != (iconv_t) - 1)
535 iconv_close (fc->cd);
539 const char *mutt_get_first_charset (const char *charset)
541 static char fcharset[SHORT_STRING];
547 if (!(c1 = strchr (c, ':')))
548 return ((char*) charset);
549 strfcpy (fcharset, c, c1 - c + 1);
553 static size_t convert_string (const char *f, size_t flen,
554 const char *from, const char *to,
555 char **t, size_t * tlen)
562 cd = mutt_iconv_open (to, from, 0);
563 if (cd == (iconv_t) (-1))
564 return (size_t) (-1);
566 ob = buf = xmalloc(obl);
567 n = my_iconv(cd, &f, &flen, &ob, &obl);
568 if (n == (size_t) (-1) || my_iconv(cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
573 return (size_t) (-1);
579 p_realloc(&buf, ob - buf + 1);
586 int mutt_convert_nonmime_string (char **ps)
590 for (c = AssumedCharset; c; c = c1 ? c1 + 1 : 0) {
595 size_t ulen = m_strlen(*ps);
601 c1 = strchr (c, ':');
602 n = c1 ? c1 - c : m_strlen(c);
605 fromcode = p_dupstr(c, n);
606 m = convert_string (u, ulen, fromcode, Charset, &s, &slen);
608 if (m != (size_t) (-1)) {