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>
33 # define EILSEQ EINVAL
37 * The following list has been created manually from the data under:
38 * http://www.isi.edu/in-notes/iana/assignments/character-sets
39 * Last update: 2000-09-07
41 * Note that it includes only the subset of character sets for which
42 * a preferred MIME name is given.
48 } PreferredMIMENames[] = {
50 "ansi_x3.4-1968", "us-ascii"}, {
51 "iso-ir-6", "us-ascii"}, {
52 "iso_646.irv:1991", "us-ascii"}, {
53 "ascii", "us-ascii"}, {
54 "iso646-us", "us-ascii"}, {
56 "ibm367", "us-ascii"}, {
57 "cp367", "us-ascii"}, {
58 "csASCII", "us-ascii"}, {
59 "csISO2022KR", "iso-2022-kr"}, {
60 "csEUCKR", "euc-kr"}, {
61 "csISO2022JP", "iso-2022-jp"}, {
62 "csISO2022JP2", "iso-2022-jp-2"}, {
63 "ISO_8859-1:1987", "iso-8859-1"}, {
64 "iso-ir-100", "iso-8859-1"}, {
65 "iso_8859-1", "iso-8859-1"}, {
66 "latin1", "iso-8859-1"}, {
67 "l1", "iso-8859-1"}, {
68 "IBM819", "iso-8859-1"}, {
69 "CP819", "iso-8859-1"}, {
70 "csISOLatin1", "iso-8859-1"}, {
71 "ISO_8859-2:1987", "iso-8859-2"}, {
72 "iso-ir-101", "iso-8859-2"}, {
73 "iso_8859-2", "iso-8859-2"}, {
74 "latin2", "iso-8859-2"}, {
75 "l2", "iso-8859-2"}, {
76 "csISOLatin2", "iso-8859-2"}, {
77 "ISO_8859-3:1988", "iso-8859-3"}, {
78 "iso-ir-109", "iso-8859-3"}, {
79 "ISO_8859-3", "iso-8859-3"}, {
80 "latin3", "iso-8859-3"}, {
81 "l3", "iso-8859-3"}, {
82 "csISOLatin3", "iso-8859-3"}, {
83 "ISO_8859-4:1988", "iso-8859-4"}, {
84 "iso-ir-110", "iso-8859-4"}, {
85 "ISO_8859-4", "iso-8859-4"}, {
86 "latin4", "iso-8859-4"}, {
87 "l4", "iso-8859-4"}, {
88 "csISOLatin4", "iso-8859-4"}, {
89 "ISO_8859-6:1987", "iso-8859-6"}, {
90 "iso-ir-127", "iso-8859-6"}, {
91 "iso_8859-6", "iso-8859-6"}, {
92 "ECMA-114", "iso-8859-6"}, {
93 "ASMO-708", "iso-8859-6"}, {
94 "arabic", "iso-8859-6"}, {
95 "csISOLatinArabic", "iso-8859-6"}, {
96 "ISO_8859-7:1987", "iso-8859-7"}, {
97 "iso-ir-126", "iso-8859-7"}, {
98 "ISO_8859-7", "iso-8859-7"}, {
99 "ELOT_928", "iso-8859-7"}, {
100 "ECMA-118", "iso-8859-7"}, {
101 "greek", "iso-8859-7"}, {
102 "greek8", "iso-8859-7"}, {
103 "csISOLatinGreek", "iso-8859-7"}, {
104 "ISO_8859-8:1988", "iso-8859-8"}, {
105 "iso-ir-138", "iso-8859-8"}, {
106 "ISO_8859-8", "iso-8859-8"}, {
107 "hebrew", "iso-8859-8"}, {
108 "csISOLatinHebrew", "iso-8859-8"}, {
109 "ISO_8859-5:1988", "iso-8859-5"}, {
110 "iso-ir-144", "iso-8859-5"}, {
111 "ISO_8859-5", "iso-8859-5"}, {
112 "cyrillic", "iso-8859-5"}, {
113 "csISOLatinCyrillic", "iso8859-5"}, {
114 "ISO_8859-9:1989", "iso-8859-9"}, {
115 "iso-ir-148", "iso-8859-9"}, {
116 "ISO_8859-9", "iso-8859-9"}, {
117 "latin5", "iso-8859-9"}, /* this is not a bug */
119 "l5", "iso-8859-9"}, {
120 "csISOLatin5", "iso-8859-9"}, {
121 "ISO_8859-10:1992", "iso-8859-10"}, {
122 "iso-ir-157", "iso-8859-10"}, {
123 "latin6", "iso-8859-10"}, /* this is not a bug */
125 "l6", "iso-8859-10"}, {
126 "csISOLatin6" "iso-8859-10"}, {
127 "csKOI8r", "koi8-r"}, {
128 "MS_Kanji", "Shift_JIS"}, /* Note the underscore! */
130 "csShiftJis", "Shift_JIS"}, {
131 "Extended_UNIX_Code_Packed_Format_for_Japanese", "EUC-JP"}, {
132 "csEUCPkdFmtJapanese", "EUC-JP"}, {
133 "csGB2312", "gb2312"}, {
136 * End of official brain damage. What follows has been taken
137 * from glibc's localedata files.
140 "iso_8859-13", "iso-8859-13"}, {
141 "iso-ir-179", "iso-8859-13"}, {
142 "latin7", "iso-8859-13"}, /* this is not a bug */
144 "l7", "iso-8859-13"}, {
145 "iso_8859-14", "iso-8859-14"}, {
146 "latin8", "iso-8859-14"}, /* this is not a bug */
148 "l8", "iso-8859-14"}, {
149 "iso_8859-15", "iso-8859-15"}, {
150 "latin9", "iso-8859-15"}, /* this is not a bug */
151 /* Suggested by Ionel Mugurel Ciobica <tgakic@sg10.chem.tue.nl> */
153 "latin0", "iso-8859-15"}, /* this is not a bug */
155 "iso_8859-16", "iso-8859-16"}, {
156 "latin10", "iso-8859-16"}, /* this is not a bug */
158 * David Champion <dgc@uchicago.edu> has observed this with
159 * nl_langinfo under SunOS 5.8.
164 * http://www.sun.com/software/white-papers/wp-unicode/
167 "eucJP", "euc-jp"}, {
168 "PCK", "Shift_JIS"}, {
169 "ko_KR-euc", "euc-kr"}, {
170 "zh_TW-big5", "big5"},
171 /* seems to be common on some systems */
173 "sjis", "Shift_JIS"}, {
174 "euc-jp-ms", "eucJP-ms"},
176 * If you happen to encounter system-specific brain-damage with
177 * respect to character set naming, please add it above this
178 * comment, and submit a patch to <mutt-dev@mutt.org>.
180 /* End of aliases. Please keep this line last. */
185 #ifdef HAVE_LANGINFO_CODESET
186 # include <langinfo.h>
189 void mutt_set_langinfo_charset (void)
191 char buff[LONG_STRING];
192 char buff2[LONG_STRING];
194 strfcpy (buff, nl_langinfo (CODESET), sizeof (buff));
195 mutt_canonical_charset (buff2, sizeof (buff2), buff);
197 /* finally, set $charset */
198 if (!(Charset = safe_strdup (buff2)))
199 Charset = safe_strdup ("iso-8859-1");
204 void mutt_set_langinfo_charset (void)
206 Charset = safe_strdup ("iso-8859-1");
211 void mutt_canonical_charset (char *dest, size_t dlen, const char *name)
215 char scratch[LONG_STRING];
217 /* catch some common iso-8859-something misspellings */
218 if (!ascii_strncasecmp (name, "8859", 4) && name[4] != '-')
219 snprintf (scratch, sizeof (scratch), "iso-8859-%s", name + 4);
220 else if (!ascii_strncasecmp (name, "8859-", 5))
221 snprintf (scratch, sizeof (scratch), "iso-8859-%s", name + 5);
222 else if (!ascii_strncasecmp (name, "iso8859", 7) && name[7] != '-')
223 snprintf (scratch, sizeof (scratch), "iso_8859-%s", name + 7);
224 else if (!ascii_strncasecmp (name, "iso8859-", 8))
225 snprintf (scratch, sizeof (scratch), "iso_8859-%s", name + 8);
227 strfcpy (scratch, NONULL (name), sizeof (scratch));
229 for (i = 0; PreferredMIMENames[i].key; i++)
230 if (!ascii_strcasecmp (scratch, PreferredMIMENames[i].key) ||
231 !safe_strcasecmp (scratch, PreferredMIMENames[i].key)) {
232 strfcpy (dest, PreferredMIMENames[i].pref, dlen);
236 strfcpy (dest, scratch, dlen);
238 /* for cosmetics' sake, transform to lowercase. */
239 for (p = dest; *p; p++)
240 *p = ascii_tolower (*p);
243 int mutt_chscmp (const char *s, const char *chs)
250 mutt_canonical_charset (buffer, sizeof (buffer), s);
251 return !ascii_strcasecmp (buffer, chs);
257 iconv_t iconv_open (const char *tocode, const char *fromcode)
259 return (iconv_t) (-1);
262 size_t iconv (iconv_t cd, ICONV_CONST char **inbuf, size_t * inbytesleft,
263 char **outbuf, size_t * outbytesleft)
268 int iconv_close (iconv_t cd)
273 #endif /* !HAVE_ICONV */
277 * Like iconv_open, but canonicalises the charsets
280 iconv_t mutt_iconv_open (const char *tocode, const char *fromcode, int flags)
282 char tocode1[SHORT_STRING];
283 char fromcode1[SHORT_STRING];
284 char *tocode2, *fromcode2;
289 mutt_canonical_charset (tocode1, sizeof (tocode1), tocode);
291 #ifdef M_ICONV_HOOK_TO
293 if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook (tocode1)))
294 mutt_canonical_charset (tocode1, sizeof (tocode1), tmp);
297 mutt_canonical_charset (fromcode1, sizeof (fromcode1), fromcode);
298 if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook (fromcode1)))
299 mutt_canonical_charset (fromcode1, sizeof (fromcode1), tmp);
301 if ((cd = iconv_open (tocode1, fromcode1)) != (iconv_t) - 1)
303 if ((tocode2 = mutt_iconv_hook (tocode1))
304 && (fromcode2 = mutt_iconv_hook (fromcode1)))
305 return iconv_open (tocode2, fromcode2);
307 return (iconv_t) - 1;
312 * Like iconv, but keeps going even when the input is invalid
313 * If you're supplying inrepls, the source charset should be stateless;
314 * if you're supplying an outrepl, the target charset should be.
317 size_t mutt_iconv (iconv_t cd, ICONV_CONST char **inbuf, size_t * inbytesleft,
318 char **outbuf, size_t * outbytesleft,
319 ICONV_CONST char **inrepls, const char *outrepl)
321 size_t ret = 0, ret1;
322 ICONV_CONST char *ib = *inbuf;
323 size_t ibl = *inbytesleft;
325 size_t obl = *outbytesleft;
328 ret1 = iconv (cd, &ib, &ibl, &ob, &obl);
329 if (ret1 != (size_t) - 1)
331 if (ibl && obl && errno == EILSEQ) {
333 /* Try replacing the input */
334 ICONV_CONST char **t;
336 for (t = inrepls; *t; t++) {
337 ICONV_CONST char *ib1 = *t;
338 size_t ibl1 = mutt_strlen (*t);
342 iconv (cd, &ib1, &ibl1, &ob1, &obl1);
345 ob = ob1, obl = obl1;
353 /* Replace the output */
356 iconv (cd, 0, 0, &ob, &obl);
358 int n = mutt_strlen (outrepl);
364 memcpy (ob, outrepl, n);
368 iconv (cd, 0, 0, 0, 0); /* for good measure */
372 *inbuf = ib, *inbytesleft = ibl;
373 *outbuf = ob, *outbytesleft = obl;
381 * Used in rfc2047.c and rfc2231.c
384 int mutt_convert_string (char **ps, const char *from, const char *to,
388 ICONV_CONST char *repls[] = { "\357\277\275", "?", 0 };
394 if (to && from && (cd = mutt_iconv_open (to, from, flags)) != (iconv_t) - 1) {
396 ICONV_CONST char *ib;
399 ICONV_CONST char **inrepls = 0;
402 if (mutt_is_utf8 (to))
403 outrepl = "\357\277\275";
404 else if (mutt_is_utf8 (from))
409 len = mutt_strlen (s);
410 ib = s, ibl = len + 1;
411 obl = MB_LEN_MAX * ibl;
412 ob = buf = safe_malloc (obl + 1);
414 mutt_iconv (cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
431 * FGETCONV stuff for converting a file while reading it
432 * Used in sendlib.c for converting from mutt's Charset
444 ICONV_CONST char **inrepls;
447 struct fgetconv_not {
452 FGETCONV *fgetconv_open (FILE * file, const char *from, const char *to,
455 struct fgetconv_s *fc;
456 iconv_t cd = (iconv_t) - 1;
457 static ICONV_CONST char *repls[] = { "\357\277\275", "?", 0 };
460 cd = mutt_iconv_open (to, from, flags);
462 if (cd != (iconv_t) - 1) {
463 fc = safe_malloc (sizeof (struct fgetconv_s));
464 fc->p = fc->ob = fc->bufo;
467 fc->inrepls = mutt_is_utf8 (to) ? repls : repls + 1;
470 fc = safe_malloc (sizeof (struct fgetconv_not));
473 return (FGETCONV *) fc;
476 char *fgetconvs (char *buf, size_t l, FGETCONV * _fc)
481 for (r = 0; r + 1 < l;) {
482 if ((c = fgetconv (_fc)) == EOF)
496 int fgetconv (FGETCONV * _fc)
498 struct fgetconv_s *fc = (struct fgetconv_s *) _fc;
502 if (fc->cd == (iconv_t) - 1)
503 return fgetc (fc->file);
507 return (unsigned char) *(fc->p)++;
509 /* Try to convert some more */
510 fc->p = fc->ob = fc->bufo;
512 size_t obl = sizeof (fc->bufo);
514 iconv (fc->cd, (ICONV_CONST char **) &fc->ib, &fc->ibl, &fc->ob, &obl);
516 return (unsigned char) *(fc->p)++;
519 /* If we trusted iconv a bit more, we would at this point
520 * ask why it had stopped converting ... */
522 /* Try to read some more */
523 if (fc->ibl == sizeof (fc->bufi) ||
524 (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof (fc->bufi))) {
529 memcpy (fc->bufi, fc->ib, fc->ibl);
532 fread (fc->ib + fc->ibl, 1, sizeof (fc->bufi) - fc->ibl, fc->file);
534 /* Try harder this time to convert some */
536 size_t obl = sizeof (fc->bufo);
538 mutt_iconv (fc->cd, (ICONV_CONST char **) &fc->ib, &fc->ibl, &fc->ob,
539 &obl, fc->inrepls, 0);
541 return (unsigned char) *(fc->p)++;
544 /* Either the file has finished or one of the buffers is too small */
549 void fgetconv_close (FGETCONV ** _fc)
551 struct fgetconv_s *fc = (struct fgetconv_s *) *_fc;
553 if (fc->cd != (iconv_t) - 1)
554 iconv_close (fc->cd);
558 char *mutt_get_first_charset (const char *charset)
560 static char fcharset[SHORT_STRING];
564 if (!mutt_strlen (c))
566 if (!(c1 = strchr (c, ':')))
568 strfcpy (fcharset, c, c1 - c + 1);
572 static size_t convert_string (ICONV_CONST char *f, size_t flen,
573 const char *from, const char *to,
574 char **t, size_t * tlen)
581 cd = mutt_iconv_open (to, from, 0);
582 if (cd == (iconv_t) (-1))
583 return (size_t) (-1);
585 ob = buf = safe_malloc (obl);
586 n = iconv (cd, &f, &flen, &ob, &obl);
587 if (n == (size_t) (-1) || iconv (cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
592 return (size_t) (-1);
598 safe_realloc (&buf, ob - buf + 1);
605 int mutt_convert_nonmime_string (char **ps)
609 for (c = AssumedCharset; c; c = c1 ? c1 + 1 : 0) {
614 size_t ulen = mutt_strlen (*ps);
620 c1 = strchr (c, ':');
621 n = c1 ? c1 - c : mutt_strlen (c);
624 fromcode = safe_malloc (n + 1);
625 strfcpy (fromcode, c, n + 1);
626 m = convert_string (u, ulen, fromcode, Charset, &s, &slen);
628 if (m != (size_t) (-1)) {