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>
34 # define EILSEQ EINVAL
38 * The following list has been created manually from the data under:
39 * http://www.isi.edu/in-notes/iana/assignments/character-sets
40 * Last update: 2000-09-07
42 * Note that it includes only the subset of character sets for which
43 * a preferred MIME name is given.
49 } PreferredMIMENames[] = {
51 "ansi_x3.4-1968", "us-ascii"}, {
52 "iso-ir-6", "us-ascii"}, {
53 "iso_646.irv:1991", "us-ascii"}, {
54 "ascii", "us-ascii"}, {
55 "iso646-us", "us-ascii"}, {
57 "ibm367", "us-ascii"}, {
58 "cp367", "us-ascii"}, {
59 "csASCII", "us-ascii"}, {
60 "csISO2022KR", "iso-2022-kr"}, {
61 "csEUCKR", "euc-kr"}, {
62 "csISO2022JP", "iso-2022-jp"}, {
63 "csISO2022JP2", "iso-2022-jp-2"}, {
64 "ISO_8859-1:1987", "iso-8859-1"}, {
65 "iso-ir-100", "iso-8859-1"}, {
66 "iso_8859-1", "iso-8859-1"}, {
67 "latin1", "iso-8859-1"}, {
68 "l1", "iso-8859-1"}, {
69 "IBM819", "iso-8859-1"}, {
70 "CP819", "iso-8859-1"}, {
71 "csISOLatin1", "iso-8859-1"}, {
72 "ISO_8859-2:1987", "iso-8859-2"}, {
73 "iso-ir-101", "iso-8859-2"}, {
74 "iso_8859-2", "iso-8859-2"}, {
75 "latin2", "iso-8859-2"}, {
76 "l2", "iso-8859-2"}, {
77 "csISOLatin2", "iso-8859-2"}, {
78 "ISO_8859-3:1988", "iso-8859-3"}, {
79 "iso-ir-109", "iso-8859-3"}, {
80 "ISO_8859-3", "iso-8859-3"}, {
81 "latin3", "iso-8859-3"}, {
82 "l3", "iso-8859-3"}, {
83 "csISOLatin3", "iso-8859-3"}, {
84 "ISO_8859-4:1988", "iso-8859-4"}, {
85 "iso-ir-110", "iso-8859-4"}, {
86 "ISO_8859-4", "iso-8859-4"}, {
87 "latin4", "iso-8859-4"}, {
88 "l4", "iso-8859-4"}, {
89 "csISOLatin4", "iso-8859-4"}, {
90 "ISO_8859-6:1987", "iso-8859-6"}, {
91 "iso-ir-127", "iso-8859-6"}, {
92 "iso_8859-6", "iso-8859-6"}, {
93 "ECMA-114", "iso-8859-6"}, {
94 "ASMO-708", "iso-8859-6"}, {
95 "arabic", "iso-8859-6"}, {
96 "csISOLatinArabic", "iso-8859-6"}, {
97 "ISO_8859-7:1987", "iso-8859-7"}, {
98 "iso-ir-126", "iso-8859-7"}, {
99 "ISO_8859-7", "iso-8859-7"}, {
100 "ELOT_928", "iso-8859-7"}, {
101 "ECMA-118", "iso-8859-7"}, {
102 "greek", "iso-8859-7"}, {
103 "greek8", "iso-8859-7"}, {
104 "csISOLatinGreek", "iso-8859-7"}, {
105 "ISO_8859-8:1988", "iso-8859-8"}, {
106 "iso-ir-138", "iso-8859-8"}, {
107 "ISO_8859-8", "iso-8859-8"}, {
108 "hebrew", "iso-8859-8"}, {
109 "csISOLatinHebrew", "iso-8859-8"}, {
110 "ISO_8859-5:1988", "iso-8859-5"}, {
111 "iso-ir-144", "iso-8859-5"}, {
112 "ISO_8859-5", "iso-8859-5"}, {
113 "cyrillic", "iso-8859-5"}, {
114 "csISOLatinCyrillic", "iso8859-5"}, {
115 "ISO_8859-9:1989", "iso-8859-9"}, {
116 "iso-ir-148", "iso-8859-9"}, {
117 "ISO_8859-9", "iso-8859-9"}, {
118 "latin5", "iso-8859-9"}, /* this is not a bug */
120 "l5", "iso-8859-9"}, {
121 "csISOLatin5", "iso-8859-9"}, {
122 "ISO_8859-10:1992", "iso-8859-10"}, {
123 "iso-ir-157", "iso-8859-10"}, {
124 "latin6", "iso-8859-10"}, /* this is not a bug */
126 "l6", "iso-8859-10"}, {
127 "csISOLatin6" "iso-8859-10"}, {
128 "csKOI8r", "koi8-r"}, {
129 "MS_Kanji", "Shift_JIS"}, /* Note the underscore! */
131 "csShiftJis", "Shift_JIS"}, {
132 "Extended_UNIX_Code_Packed_Format_for_Japanese", "EUC-JP"}, {
133 "csEUCPkdFmtJapanese", "EUC-JP"}, {
134 "csGB2312", "gb2312"}, {
137 * End of official brain damage. What follows has been taken
138 * from glibc's localedata files.
141 "iso_8859-13", "iso-8859-13"}, {
142 "iso-ir-179", "iso-8859-13"}, {
143 "latin7", "iso-8859-13"}, /* this is not a bug */
145 "l7", "iso-8859-13"}, {
146 "iso_8859-14", "iso-8859-14"}, {
147 "latin8", "iso-8859-14"}, /* this is not a bug */
149 "l8", "iso-8859-14"}, {
150 "iso_8859-15", "iso-8859-15"}, {
151 "latin9", "iso-8859-15"}, /* this is not a bug */
152 /* Suggested by Ionel Mugurel Ciobica <tgakic@sg10.chem.tue.nl> */
154 "latin0", "iso-8859-15"}, /* this is not a bug */
156 "iso_8859-16", "iso-8859-16"}, {
157 "latin10", "iso-8859-16"}, /* this is not a bug */
159 * David Champion <dgc@uchicago.edu> has observed this with
160 * nl_langinfo under SunOS 5.8.
165 * http://www.sun.com/software/white-papers/wp-unicode/
168 "eucJP", "euc-jp"}, {
169 "PCK", "Shift_JIS"}, {
170 "ko_KR-euc", "euc-kr"}, {
171 "zh_TW-big5", "big5"},
172 /* seems to be common on some systems */
174 "sjis", "Shift_JIS"}, {
175 "euc-jp-ms", "eucJP-ms"},
177 * If you happen to encounter system-specific brain-damage with
178 * respect to character set naming, please add it above this
179 * comment, and submit a patch to <mutt-dev@mutt.org>.
181 /* End of aliases. Please keep this line last. */
186 #ifdef HAVE_LANGINFO_CODESET
187 # include <langinfo.h>
190 void mutt_set_langinfo_charset (void)
192 char buff[LONG_STRING];
193 char buff2[LONG_STRING];
195 strfcpy (buff, nl_langinfo (CODESET), sizeof (buff));
196 mutt_canonical_charset (buff2, sizeof (buff2), buff);
198 /* finally, set $charset */
199 if (!(Charset = str_dup (buff2)))
200 Charset = str_dup ("iso-8859-1");
205 void mutt_set_langinfo_charset (void)
207 Charset = str_dup ("iso-8859-1");
212 void mutt_canonical_charset (char *dest, size_t dlen, const char *name)
216 char scratch[LONG_STRING];
218 /* catch some common iso-8859-something misspellings */
219 if (!ascii_strncasecmp (name, "8859", 4) && name[4] != '-')
220 snprintf (scratch, sizeof (scratch), "iso-8859-%s", name + 4);
221 else if (!ascii_strncasecmp (name, "8859-", 5))
222 snprintf (scratch, sizeof (scratch), "iso-8859-%s", name + 5);
223 else if (!ascii_strncasecmp (name, "iso8859", 7) && name[7] != '-')
224 snprintf (scratch, sizeof (scratch), "iso_8859-%s", name + 7);
225 else if (!ascii_strncasecmp (name, "iso8859-", 8))
226 snprintf (scratch, sizeof (scratch), "iso_8859-%s", name + 8);
228 strfcpy (scratch, NONULL (name), sizeof (scratch));
230 for (i = 0; PreferredMIMENames[i].key; i++)
231 if (!ascii_strcasecmp (scratch, PreferredMIMENames[i].key) ||
232 !str_casecmp (scratch, PreferredMIMENames[i].key)) {
233 strfcpy (dest, PreferredMIMENames[i].pref, dlen);
237 strfcpy (dest, scratch, dlen);
239 /* for cosmetics' sake, transform to lowercase. */
240 for (p = dest; *p; p++)
241 *p = ascii_tolower (*p);
244 int mutt_chscmp (const char *s, const char *chs)
251 mutt_canonical_charset (buffer, sizeof (buffer), s);
252 return !ascii_strcasecmp (buffer, chs);
258 iconv_t iconv_open (const char *tocode, const char *fromcode)
260 return (iconv_t) (-1);
263 size_t iconv (iconv_t cd, ICONV_CONST char **inbuf, size_t * inbytesleft,
264 char **outbuf, size_t * outbytesleft)
269 int iconv_close (iconv_t cd)
274 #endif /* !HAVE_ICONV */
278 * Like iconv_open, but canonicalises the charsets
281 iconv_t mutt_iconv_open (const char *tocode, const char *fromcode, int flags)
283 char tocode1[SHORT_STRING];
284 char fromcode1[SHORT_STRING];
285 char *tocode2, *fromcode2;
290 mutt_canonical_charset (tocode1, sizeof (tocode1), tocode);
292 #ifdef M_ICONV_HOOK_TO
294 if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook (tocode1)))
295 mutt_canonical_charset (tocode1, sizeof (tocode1), tmp);
298 mutt_canonical_charset (fromcode1, sizeof (fromcode1), fromcode);
299 if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook (fromcode1)))
300 mutt_canonical_charset (fromcode1, sizeof (fromcode1), tmp);
302 if ((cd = iconv_open (tocode1, fromcode1)) != (iconv_t) - 1)
304 if ((tocode2 = mutt_iconv_hook (tocode1))
305 && (fromcode2 = mutt_iconv_hook (fromcode1)))
306 return iconv_open (tocode2, fromcode2);
308 return (iconv_t) - 1;
313 * Like iconv, but keeps going even when the input is invalid
314 * If you're supplying inrepls, the source charset should be stateless;
315 * if you're supplying an outrepl, the target charset should be.
318 size_t mutt_iconv (iconv_t cd, ICONV_CONST char **inbuf, size_t * inbytesleft,
319 char **outbuf, size_t * outbytesleft,
320 ICONV_CONST char **inrepls, const char *outrepl)
322 size_t ret = 0, ret1;
323 ICONV_CONST char *ib = *inbuf;
324 size_t ibl = *inbytesleft;
326 size_t obl = *outbytesleft;
329 ret1 = iconv (cd, &ib, &ibl, &ob, &obl);
330 if (ret1 != (size_t) - 1)
332 if (ibl && obl && errno == EILSEQ) {
334 /* Try replacing the input */
335 ICONV_CONST char **t;
337 for (t = inrepls; *t; t++) {
338 ICONV_CONST char *ib1 = *t;
339 size_t ibl1 = str_len (*t);
343 iconv (cd, &ib1, &ibl1, &ob1, &obl1);
346 ob = ob1, obl = obl1;
354 /* Replace the output */
357 iconv (cd, 0, 0, &ob, &obl);
359 int n = str_len (outrepl);
365 memcpy (ob, outrepl, n);
369 iconv (cd, 0, 0, 0, 0); /* for good measure */
373 *inbuf = ib, *inbytesleft = ibl;
374 *outbuf = ob, *outbytesleft = obl;
382 * Used in rfc2047.c and rfc2231.c
385 int mutt_convert_string (char **ps, const char *from, const char *to,
389 ICONV_CONST char *repls[] = { "\357\277\275", "?", 0 };
395 if (to && from && (cd = mutt_iconv_open (to, from, flags)) != (iconv_t) - 1) {
397 ICONV_CONST char *ib;
400 ICONV_CONST char **inrepls = 0;
403 if (mutt_is_utf8 (to))
404 outrepl = "\357\277\275";
405 else if (mutt_is_utf8 (from))
411 ib = s, ibl = len + 1;
412 obl = MB_LEN_MAX * ibl;
413 ob = buf = mem_malloc (obl + 1);
415 mutt_iconv (cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
432 * FGETCONV stuff for converting a file while reading it
433 * Used in sendlib.c for converting from mutt's Charset
445 ICONV_CONST char **inrepls;
448 struct fgetconv_not {
453 FGETCONV *fgetconv_open (FILE * file, const char *from, const char *to,
456 struct fgetconv_s *fc;
457 iconv_t cd = (iconv_t) - 1;
458 static ICONV_CONST char *repls[] = { "\357\277\275", "?", 0 };
461 cd = mutt_iconv_open (to, from, flags);
463 if (cd != (iconv_t) - 1) {
464 fc = mem_malloc (sizeof (struct fgetconv_s));
465 fc->p = fc->ob = fc->bufo;
468 fc->inrepls = mutt_is_utf8 (to) ? repls : repls + 1;
471 fc = mem_malloc (sizeof (struct fgetconv_not));
474 return (FGETCONV *) fc;
477 char *fgetconvs (char *buf, size_t l, FGETCONV * _fc)
482 for (r = 0; r + 1 < l;) {
483 if ((c = fgetconv (_fc)) == EOF)
497 int fgetconv (FGETCONV * _fc)
499 struct fgetconv_s *fc = (struct fgetconv_s *) _fc;
503 if (fc->cd == (iconv_t) - 1)
504 return fgetc (fc->file);
508 return (unsigned char) *(fc->p)++;
510 /* Try to convert some more */
511 fc->p = fc->ob = fc->bufo;
513 size_t obl = sizeof (fc->bufo);
515 iconv (fc->cd, (ICONV_CONST char **) &fc->ib, &fc->ibl, &fc->ob, &obl);
517 return (unsigned char) *(fc->p)++;
520 /* If we trusted iconv a bit more, we would at this point
521 * ask why it had stopped converting ... */
523 /* Try to read some more */
524 if (fc->ibl == sizeof (fc->bufi) ||
525 (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof (fc->bufi))) {
530 memcpy (fc->bufi, fc->ib, fc->ibl);
533 fread (fc->ib + fc->ibl, 1, sizeof (fc->bufi) - fc->ibl, fc->file);
535 /* Try harder this time to convert some */
537 size_t obl = sizeof (fc->bufo);
539 mutt_iconv (fc->cd, (ICONV_CONST char **) &fc->ib, &fc->ibl, &fc->ob,
540 &obl, fc->inrepls, 0);
542 return (unsigned char) *(fc->p)++;
545 /* Either the file has finished or one of the buffers is too small */
550 void fgetconv_close (FGETCONV ** _fc)
552 struct fgetconv_s *fc = (struct fgetconv_s *) *_fc;
554 if (fc->cd != (iconv_t) - 1)
555 iconv_close (fc->cd);
559 char *mutt_get_first_charset (const char *charset)
561 static char fcharset[SHORT_STRING];
567 if (!(c1 = strchr (c, ':')))
568 return ((char*) charset);
569 strfcpy (fcharset, c, c1 - c + 1);
573 static size_t convert_string (ICONV_CONST char *f, size_t flen,
574 const char *from, const char *to,
575 char **t, size_t * tlen)
582 cd = mutt_iconv_open (to, from, 0);
583 if (cd == (iconv_t) (-1))
584 return (size_t) (-1);
586 ob = buf = mem_malloc (obl);
587 n = iconv (cd, &f, &flen, &ob, &obl);
588 if (n == (size_t) (-1) || iconv (cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
593 return (size_t) (-1);
599 mem_realloc (&buf, ob - buf + 1);
606 int mutt_convert_nonmime_string (char **ps)
610 for (c = AssumedCharset; c; c = c1 ? c1 + 1 : 0) {
615 size_t ulen = str_len (*ps);
621 c1 = strchr (c, ':');
622 n = c1 ? c1 - c : str_len (c);
625 fromcode = mem_malloc (n + 1);
626 strfcpy (fromcode, c, n + 1);
627 m = convert_string (u, ulen, fromcode, Charset, &s, &slen);
628 mem_free (&fromcode);
629 if (m != (size_t) (-1)) {