2 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
4 * This program is free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later
10 * This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111, USA.
29 #include <sys/types.h>
38 # define EILSEQ EINVAL
42 * The following list has been created manually from the data under:
43 * http://www.isi.edu/in-notes/iana/assignments/character-sets
44 * Last update: 2000-09-07
46 * Note that it includes only the subset of character sets for which
47 * a preferred MIME name is given.
55 PreferredMIMENames[] =
57 { "ansi_x3.4-1968", "us-ascii" },
58 { "iso-ir-6", "us-ascii" },
59 { "iso_646.irv:1991", "us-ascii" },
60 { "ascii", "us-ascii" },
61 { "iso646-us", "us-ascii" },
63 { "ibm367", "us-ascii" },
64 { "cp367", "us-ascii" },
65 { "csASCII", "us-ascii" },
67 { "csISO2022KR", "iso-2022-kr" },
68 { "csEUCKR", "euc-kr" },
69 { "csISO2022JP", "iso-2022-jp" },
70 { "csISO2022JP2", "iso-2022-jp-2" },
72 { "ISO_8859-1:1987", "iso-8859-1" },
73 { "iso-ir-100", "iso-8859-1" },
74 { "iso_8859-1", "iso-8859-1" },
75 { "latin1", "iso-8859-1" },
76 { "l1", "iso-8859-1" },
77 { "IBM819", "iso-8859-1" },
78 { "CP819", "iso-8859-1" },
79 { "csISOLatin1", "iso-8859-1" },
81 { "ISO_8859-2:1987", "iso-8859-2" },
82 { "iso-ir-101", "iso-8859-2" },
83 { "iso_8859-2", "iso-8859-2" },
84 { "latin2", "iso-8859-2" },
85 { "l2", "iso-8859-2" },
86 { "csISOLatin2", "iso-8859-2" },
88 { "ISO_8859-3:1988", "iso-8859-3" },
89 { "iso-ir-109", "iso-8859-3" },
90 { "ISO_8859-3", "iso-8859-3" },
91 { "latin3", "iso-8859-3" },
92 { "l3", "iso-8859-3" },
93 { "csISOLatin3", "iso-8859-3" },
95 { "ISO_8859-4:1988", "iso-8859-4" },
96 { "iso-ir-110", "iso-8859-4" },
97 { "ISO_8859-4", "iso-8859-4" },
98 { "latin4", "iso-8859-4" },
99 { "l4", "iso-8859-4" },
100 { "csISOLatin4", "iso-8859-4" },
102 { "ISO_8859-6:1987", "iso-8859-6" },
103 { "iso-ir-127", "iso-8859-6" },
104 { "iso_8859-6", "iso-8859-6" },
105 { "ECMA-114", "iso-8859-6" },
106 { "ASMO-708", "iso-8859-6" },
107 { "arabic", "iso-8859-6" },
108 { "csISOLatinArabic", "iso-8859-6" },
110 { "ISO_8859-7:1987", "iso-8859-7" },
111 { "iso-ir-126", "iso-8859-7" },
112 { "ISO_8859-7", "iso-8859-7" },
113 { "ELOT_928", "iso-8859-7" },
114 { "ECMA-118", "iso-8859-7" },
115 { "greek", "iso-8859-7" },
116 { "greek8", "iso-8859-7" },
117 { "csISOLatinGreek", "iso-8859-7" },
119 { "ISO_8859-8:1988", "iso-8859-8" },
120 { "iso-ir-138", "iso-8859-8" },
121 { "ISO_8859-8", "iso-8859-8" },
122 { "hebrew", "iso-8859-8" },
123 { "csISOLatinHebrew", "iso-8859-8" },
125 { "ISO_8859-5:1988", "iso-8859-5" },
126 { "iso-ir-144", "iso-8859-5" },
127 { "ISO_8859-5", "iso-8859-5" },
128 { "cyrillic", "iso-8859-5" },
129 { "csISOLatinCyrillic", "iso8859-5" },
131 { "ISO_8859-9:1989", "iso-8859-9" },
132 { "iso-ir-148", "iso-8859-9" },
133 { "ISO_8859-9", "iso-8859-9" },
134 { "latin5", "iso-8859-9" }, /* this is not a bug */
135 { "l5", "iso-8859-9" },
136 { "csISOLatin5", "iso-8859-9" },
138 { "ISO_8859-10:1992", "iso-8859-10" },
139 { "iso-ir-157", "iso-8859-10" },
140 { "latin6", "iso-8859-10" }, /* this is not a bug */
141 { "l6", "iso-8859-10" },
142 { "csISOLatin6" "iso-8859-10" },
144 { "csKOI8r", "koi8-r" },
146 { "MS_Kanji", "Shift_JIS" }, /* Note the underscore! */
147 { "csShiftJis", "Shift_JIS" },
149 { "Extended_UNIX_Code_Packed_Format_for_Japanese",
151 { "csEUCPkdFmtJapanese",
154 { "csGB2312", "gb2312" },
155 { "csbig5", "big5" },
158 * End of official brain damage. What follows has been taken
159 * from glibc's localedata files.
162 { "iso_8859-13", "iso-8859-13" },
163 { "iso-ir-179", "iso-8859-13" },
164 { "latin7", "iso-8859-13" }, /* this is not a bug */
165 { "l7", "iso-8859-13" },
167 { "iso_8859-14", "iso-8859-14" },
168 { "latin8", "iso-8859-14" }, /* this is not a bug */
169 { "l8", "iso-8859-14" },
171 { "iso_8859-15", "iso-8859-15" },
172 { "latin9", "iso-8859-15" }, /* this is not a bug */
174 /* Suggested by Ionel Mugurel Ciobica <tgakic@sg10.chem.tue.nl> */
175 { "latin0", "iso-8859-15" }, /* this is not a bug */
177 { "iso_8859-16", "iso-8859-16" },
178 { "latin10", "iso-8859-16" }, /* this is not a bug */
181 * David Champion <dgc@uchicago.edu> has observed this with
182 * nl_langinfo under SunOS 5.8.
185 { "646", "us-ascii" },
188 * http://www.sun.com/software/white-papers/wp-unicode/
191 { "eucJP", "euc-jp" },
192 { "PCK", "Shift_JIS" },
193 { "ko_KR-euc", "euc-kr" },
194 { "zh_TW-big5", "big5" },
196 /* seems to be common on some systems */
198 { "sjis", "Shift_JIS" },
202 * If you happen to encounter system-specific brain-damage with
203 * respect to character set naming, please add it above this
204 * comment, and submit a patch to <mutt-dev@mutt.org>.
207 /* End of aliases. Please keep this line last. */
212 #ifdef HAVE_LANGINFO_CODESET
213 # include <langinfo.h>
216 void mutt_set_langinfo_charset (void)
218 char buff[LONG_STRING];
219 char buff2[LONG_STRING];
221 strfcpy (buff, nl_langinfo (CODESET), sizeof (buff));
222 mutt_canonical_charset (buff2, sizeof (buff2), buff);
224 /* finally, set $charset */
225 if (!(Charset = safe_strdup (buff2)))
226 Charset = safe_strdup ("iso-8859-1");
231 void mutt_set_langinfo_charset (void)
233 Charset = safe_strdup ("iso-8859-1");
238 void mutt_canonical_charset (char *dest, size_t dlen, const char *name)
242 char scratch[LONG_STRING];
244 /* catch some common iso-8859-something misspellings */
245 if (!ascii_strncasecmp (name, "8859", 4) && name[4] != '-')
246 snprintf (scratch, sizeof (scratch), "iso-8859-%s", name +4);
247 else if (!ascii_strncasecmp (name, "8859-", 5))
248 snprintf (scratch, sizeof (scratch), "iso-8859-%s", name + 5);
249 else if (!ascii_strncasecmp (name, "iso8859", 7) && name[7] != '-')
250 snprintf (scratch, sizeof (scratch), "iso_8859-%s", name + 7);
251 else if (!ascii_strncasecmp (name, "iso8859-", 8))
252 snprintf (scratch, sizeof (scratch), "iso_8859-%s", name + 8);
254 strfcpy (scratch, NONULL(name), sizeof (scratch));
256 for (i = 0; PreferredMIMENames[i].key; i++)
257 if (!ascii_strcasecmp (scratch, PreferredMIMENames[i].key) ||
258 !mutt_strcasecmp (scratch, PreferredMIMENames[i].key))
260 strfcpy (dest, PreferredMIMENames[i].pref, dlen);
264 strfcpy (dest, scratch, dlen);
266 /* for cosmetics' sake, transform to lowercase. */
267 for (p = dest; *p; p++)
268 *p = ascii_tolower (*p);
271 int mutt_chscmp (const char *s, const char *chs)
277 mutt_canonical_charset (buffer, sizeof (buffer), s);
278 return !ascii_strcasecmp (buffer, chs);
284 iconv_t iconv_open (const char *tocode, const char *fromcode)
286 return (iconv_t)(-1);
289 size_t iconv (iconv_t cd, ICONV_CONST char **inbuf, size_t *inbytesleft,
290 char **outbuf, size_t *outbytesleft)
295 int iconv_close (iconv_t cd)
300 #endif /* !HAVE_ICONV */
304 * Like iconv_open, but canonicalises the charsets
307 iconv_t mutt_iconv_open (const char *tocode, const char *fromcode, int flags)
309 char tocode1[SHORT_STRING];
310 char fromcode1[SHORT_STRING];
311 char *tocode2, *fromcode2;
316 mutt_canonical_charset (tocode1, sizeof (tocode1), tocode);
318 #ifdef M_ICONV_HOOK_TO
320 if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook (tocode1)))
321 mutt_canonical_charset (tocode1, sizeof (tocode1), tmp);
324 mutt_canonical_charset (fromcode1, sizeof (fromcode1), fromcode);
325 if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook (fromcode1)))
326 mutt_canonical_charset (fromcode1, sizeof (fromcode1), tmp);
328 if ((cd = iconv_open (tocode1, fromcode1)) != (iconv_t) -1)
330 if ((tocode2 = mutt_iconv_hook (tocode1)) && (fromcode2 = mutt_iconv_hook (fromcode1)))
331 return iconv_open (tocode2, fromcode2);
338 * Like iconv, but keeps going even when the input is invalid
339 * If you're supplying inrepls, the source charset should be stateless;
340 * if you're supplying an outrepl, the target charset should be.
343 size_t mutt_iconv (iconv_t cd, ICONV_CONST char **inbuf, size_t *inbytesleft,
344 char **outbuf, size_t *outbytesleft,
345 ICONV_CONST char **inrepls, const char *outrepl)
347 size_t ret = 0, ret1;
348 ICONV_CONST char *ib = *inbuf;
349 size_t ibl = *inbytesleft;
351 size_t obl = *outbytesleft;
355 ret1 = iconv (cd, &ib, &ibl, &ob, &obl);
356 if (ret1 != (size_t)-1)
358 if (ibl && obl && errno == EILSEQ)
362 /* Try replacing the input */
363 ICONV_CONST char **t;
364 for (t = inrepls; *t; t++)
366 ICONV_CONST char *ib1 = *t;
367 size_t ibl1 = strlen (*t);
370 iconv (cd, &ib1, &ibl1, &ob1, &obl1);
374 ob = ob1, obl = obl1;
384 /* Try replacing the output */
385 int n = strlen (outrepl);
388 memcpy (ob, outrepl, n);
396 *inbuf = ib, *inbytesleft = ibl;
397 *outbuf = ob, *outbytesleft = obl;
405 * Used in rfc2047.c and rfc2231.c
408 int mutt_convert_string (char **ps, const char *from, const char *to, int flags)
411 ICONV_CONST char *repls[] = { "\357\277\275", "?", 0 };
417 if (to && from && (cd = mutt_iconv_open (to, from, flags)) != (iconv_t)-1)
420 ICONV_CONST char *ib;
423 ICONV_CONST char **inrepls = 0;
426 if (mutt_is_utf8 (to))
427 outrepl = "\357\277\275";
428 else if (mutt_is_utf8 (from))
434 ib = s, ibl = len + 1;
435 obl = MB_LEN_MAX * ibl;
436 ob = buf = safe_malloc (obl + 1);
438 mutt_iconv (cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
446 mutt_str_adjust (ps);
455 * FGETCONV stuff for converting a file while reading it
456 * Used in sendlib.c for converting from mutt's Charset
469 ICONV_CONST char **inrepls;
478 FGETCONV *fgetconv_open (FILE *file, const char *from, const char *to, int flags)
480 struct fgetconv_s *fc;
481 iconv_t cd = (iconv_t)-1;
482 static ICONV_CONST char *repls[] = { "\357\277\275", "?", 0 };
485 cd = mutt_iconv_open (to, from, flags);
487 if (cd != (iconv_t)-1)
489 fc = safe_malloc (sizeof (struct fgetconv_s));
490 fc->p = fc->ob = fc->bufo;
493 fc->inrepls = mutt_is_utf8 (to) ? repls : repls + 1;
496 fc = safe_malloc (sizeof (struct fgetconv_not));
499 return (FGETCONV *)fc;
502 char *fgetconvs (char *buf, size_t l, FGETCONV *_fc)
507 for (r = 0; r + 1 < l;)
509 if ((c = fgetconv (_fc)) == EOF)
523 int fgetconv (FGETCONV *_fc)
525 struct fgetconv_s *fc = (struct fgetconv_s *)_fc;
529 if (fc->cd == (iconv_t)-1)
530 return fgetc (fc->file);
534 return (unsigned char)*(fc->p)++;
536 /* Try to convert some more */
537 fc->p = fc->ob = fc->bufo;
540 size_t obl = sizeof (fc->bufo);
541 iconv (fc->cd, (ICONV_CONST char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
543 return (unsigned char)*(fc->p)++;
546 /* If we trusted iconv a bit more, we would at this point
547 * ask why it had stopped converting ... */
549 /* Try to read some more */
550 if (fc->ibl == sizeof (fc->bufi) ||
551 (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof (fc->bufi)))
557 memcpy (fc->bufi, fc->ib, fc->ibl);
559 fc->ibl += fread (fc->ib + fc->ibl, 1, sizeof (fc->bufi) - fc->ibl, fc->file);
561 /* Try harder this time to convert some */
564 size_t obl = sizeof (fc->bufo);
565 mutt_iconv (fc->cd, (ICONV_CONST char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
568 return (unsigned char)*(fc->p)++;
571 /* Either the file has finished or one of the buffers is too small */
576 void fgetconv_close (FGETCONV **_fc)
578 struct fgetconv_s *fc = (struct fgetconv_s *) *_fc;
580 if (fc->cd != (iconv_t)-1)
581 iconv_close (fc->cd);