Rocco Rutte:
[apps/madmutt.git] / charset.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
4  *
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.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #include <ctype.h>
19
20 #include <sys/types.h>
21 #include <dirent.h>
22 #include <unistd.h>
23 #include <errno.h>
24
25 #include "mutt.h"
26 #include "charset.h"
27 #include "ascii.h"
28
29 #include "lib/mem.h"
30 #include "lib/intl.h"
31 #include "lib/str.h"
32
33 #ifndef EILSEQ
34 # define EILSEQ EINVAL
35 #endif
36
37 /* 
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
41  *
42  * Note that it includes only the subset of character sets for which
43  * a preferred MIME name is given.
44  */
45
46 static struct {
47   char *key;
48   char *pref;
49 } PreferredMIMENames[] = {
50   {
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"}, {
56   "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 */
119   {
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 */
125   {
126   "l6", "iso-8859-10"}, {
127   "csISOLatin6" "iso-8859-10"}, {
128   "csKOI8r", "koi8-r"}, {
129   "MS_Kanji", "Shift_JIS"},     /* Note the underscore! */
130   {
131   "csShiftJis", "Shift_JIS"}, {
132   "Extended_UNIX_Code_Packed_Format_for_Japanese", "EUC-JP"}, {
133   "csEUCPkdFmtJapanese", "EUC-JP"}, {
134   "csGB2312", "gb2312"}, {
135   "csbig5", "big5"},
136     /* 
137      * End of official brain damage.  What follows has been taken
138      * from glibc's localedata files. 
139      */
140   {
141   "iso_8859-13", "iso-8859-13"}, {
142   "iso-ir-179", "iso-8859-13"}, {
143   "latin7", "iso-8859-13"},     /* this is not a bug */
144   {
145   "l7", "iso-8859-13"}, {
146   "iso_8859-14", "iso-8859-14"}, {
147   "latin8", "iso-8859-14"},     /* this is not a bug */
148   {
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> */
153   {
154   "latin0", "iso-8859-15"},     /* this is not a bug */
155   {
156   "iso_8859-16", "iso-8859-16"}, {
157   "latin10", "iso-8859-16"},    /* this is not a bug */
158     /* 
159      * David Champion <dgc@uchicago.edu> has observed this with
160      * nl_langinfo under SunOS 5.8. 
161      */
162   {
163   "646", "us-ascii"},
164     /* 
165      * http://www.sun.com/software/white-papers/wp-unicode/
166      */
167   {
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 */
173   {
174   "sjis", "Shift_JIS"}, {
175   "euc-jp-ms", "eucJP-ms"},
176     /*
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>. 
180      */
181     /* End of aliases.  Please keep this line last. */
182   {
183   NULL, NULL}
184 };
185
186 #ifdef HAVE_LANGINFO_CODESET
187 # include <langinfo.h>
188
189
190 void mutt_set_langinfo_charset (void)
191 {
192   char buff[LONG_STRING];
193   char buff2[LONG_STRING];
194
195   strfcpy (buff, nl_langinfo (CODESET), sizeof (buff));
196   mutt_canonical_charset (buff2, sizeof (buff2), buff);
197
198   /* finally, set $charset */
199   if (!(Charset = str_dup (buff2)))
200     Charset = str_dup ("iso-8859-1");
201 }
202
203 #else
204
205 void mutt_set_langinfo_charset (void)
206 {
207   Charset = str_dup ("iso-8859-1");
208 }
209
210 #endif
211
212 void mutt_canonical_charset (char *dest, size_t dlen, const char *name)
213 {
214   size_t i;
215   char *p;
216   char scratch[LONG_STRING];
217
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);
227   else
228     strfcpy (scratch, NONULL (name), sizeof (scratch));
229
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);
234       return;
235     }
236
237   strfcpy (dest, scratch, dlen);
238
239   /* for cosmetics' sake, transform to lowercase. */
240   for (p = dest; *p; p++)
241     *p = ascii_tolower (*p);
242 }
243
244 int mutt_chscmp (const char *s, const char *chs)
245 {
246   char buffer[STRING];
247
248   if (!s)
249     return 0;
250
251   mutt_canonical_charset (buffer, sizeof (buffer), s);
252   return !ascii_strcasecmp (buffer, chs);
253 }
254
255
256 #ifndef HAVE_ICONV
257
258 iconv_t iconv_open (const char *tocode, const char *fromcode)
259 {
260   return (iconv_t) (-1);
261 }
262
263 size_t iconv (iconv_t cd, ICONV_CONST char **inbuf, size_t * inbytesleft,
264               char **outbuf, size_t * outbytesleft)
265 {
266   return 0;
267 }
268
269 int iconv_close (iconv_t cd)
270 {
271   return 0;
272 }
273
274 #endif /* !HAVE_ICONV */
275
276
277 /*
278  * Like iconv_open, but canonicalises the charsets
279  */
280
281 iconv_t mutt_iconv_open (const char *tocode, const char *fromcode, int flags)
282 {
283   char tocode1[SHORT_STRING];
284   char fromcode1[SHORT_STRING];
285   char *tocode2, *fromcode2;
286   char *tmp;
287
288   iconv_t cd;
289
290   mutt_canonical_charset (tocode1, sizeof (tocode1), tocode);
291
292 #ifdef M_ICONV_HOOK_TO
293   /* Not used. */
294   if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook (tocode1)))
295     mutt_canonical_charset (tocode1, sizeof (tocode1), tmp);
296 #endif
297
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);
301
302   if ((cd = iconv_open (tocode1, fromcode1)) != (iconv_t) - 1)
303     return cd;
304   if ((tocode2 = mutt_iconv_hook (tocode1))
305       && (fromcode2 = mutt_iconv_hook (fromcode1)))
306     return iconv_open (tocode2, fromcode2);
307
308   return (iconv_t) - 1;
309 }
310
311
312 /*
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.
316  */
317
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)
321 {
322   size_t ret = 0, ret1;
323   ICONV_CONST char *ib = *inbuf;
324   size_t ibl = *inbytesleft;
325   char *ob = *outbuf;
326   size_t obl = *outbytesleft;
327
328   for (;;) {
329     ret1 = iconv (cd, &ib, &ibl, &ob, &obl);
330     if (ret1 != (size_t) - 1)
331       ret += ret1;
332     if (ibl && obl && errno == EILSEQ) {
333       if (inrepls) {
334         /* Try replacing the input */
335         ICONV_CONST char **t;
336
337         for (t = inrepls; *t; t++) {
338           ICONV_CONST char *ib1 = *t;
339           size_t ibl1 = str_len (*t);
340           char *ob1 = ob;
341           size_t obl1 = obl;
342
343           iconv (cd, &ib1, &ibl1, &ob1, &obl1);
344           if (!ibl1) {
345             ++ib, --ibl;
346             ob = ob1, obl = obl1;
347             ++ret;
348             break;
349           }
350         }
351         if (*t)
352           continue;
353       }
354       /* Replace the output */
355       if (!outrepl)
356         outrepl = "?";
357       iconv (cd, 0, 0, &ob, &obl);
358       if (obl) {
359         int n = str_len (outrepl);
360
361         if (n > obl) {
362           outrepl = "?";
363           n = 1;
364         }
365         memcpy (ob, outrepl, n);
366         ++ib, --ibl;
367         ob += n, obl -= n;
368         ++ret;
369         iconv (cd, 0, 0, 0, 0); /* for good measure */
370         continue;
371       }
372     }
373     *inbuf = ib, *inbytesleft = ibl;
374     *outbuf = ob, *outbytesleft = obl;
375     return ret;
376   }
377 }
378
379
380 /*
381  * Convert a string
382  * Used in rfc2047.c and rfc2231.c
383  */
384
385 int mutt_convert_string (char **ps, const char *from, const char *to,
386                          int flags)
387 {
388   iconv_t cd;
389   ICONV_CONST char *repls[] = { "\357\277\275", "?", 0 };
390   char *s = *ps;
391
392   if (!s || !*s)
393     return 0;
394
395   if (to && from && (cd = mutt_iconv_open (to, from, flags)) != (iconv_t) - 1) {
396     int len;
397     ICONV_CONST char *ib;
398     char *buf, *ob;
399     size_t ibl, obl;
400     ICONV_CONST char **inrepls = 0;
401     char *outrepl = 0;
402
403     if (mutt_is_utf8 (to))
404       outrepl = "\357\277\275";
405     else if (mutt_is_utf8 (from))
406       inrepls = repls;
407     else
408       outrepl = "?";
409
410     len = str_len (s);
411     ib = s, ibl = len + 1;
412     obl = MB_LEN_MAX * ibl;
413     ob = buf = mem_malloc (obl + 1);
414
415     mutt_iconv (cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
416     iconv_close (cd);
417
418     *ob = '\0';
419
420     mem_free (ps);
421     *ps = buf;
422
423     str_adjust (ps);
424     return 0;
425   }
426   else
427     return -1;
428 }
429
430
431 /*
432  * FGETCONV stuff for converting a file while reading it
433  * Used in sendlib.c for converting from mutt's Charset
434  */
435
436 struct fgetconv_s {
437   FILE *file;
438   iconv_t cd;
439   char bufi[512];
440   char bufo[512];
441   char *p;
442   char *ob;
443   char *ib;
444   size_t ibl;
445   ICONV_CONST char **inrepls;
446 };
447
448 struct fgetconv_not {
449   FILE *file;
450   iconv_t cd;
451 };
452
453 FGETCONV *fgetconv_open (FILE * file, const char *from, const char *to,
454                          int flags)
455 {
456   struct fgetconv_s *fc;
457   iconv_t cd = (iconv_t) - 1;
458   static ICONV_CONST char *repls[] = { "\357\277\275", "?", 0 };
459
460   if (from && to)
461     cd = mutt_iconv_open (to, from, flags);
462
463   if (cd != (iconv_t) - 1) {
464     fc = mem_malloc (sizeof (struct fgetconv_s));
465     fc->p = fc->ob = fc->bufo;
466     fc->ib = fc->bufi;
467     fc->ibl = 0;
468     fc->inrepls = mutt_is_utf8 (to) ? repls : repls + 1;
469   }
470   else
471     fc = mem_malloc (sizeof (struct fgetconv_not));
472   fc->file = file;
473   fc->cd = cd;
474   return (FGETCONV *) fc;
475 }
476
477 char *fgetconvs (char *buf, size_t l, FGETCONV * _fc)
478 {
479   int c;
480   size_t r;
481
482   for (r = 0; r + 1 < l;) {
483     if ((c = fgetconv (_fc)) == EOF)
484       break;
485     buf[r++] = (char) c;
486     if (c == '\n')
487       break;
488   }
489   buf[r] = '\0';
490
491   if (r)
492     return buf;
493   else
494     return NULL;
495 }
496
497 int fgetconv (FGETCONV * _fc)
498 {
499   struct fgetconv_s *fc = (struct fgetconv_s *) _fc;
500
501   if (!fc)
502     return EOF;
503   if (fc->cd == (iconv_t) - 1)
504     return fgetc (fc->file);
505   if (!fc->p)
506     return EOF;
507   if (fc->p < fc->ob)
508     return (unsigned char) *(fc->p)++;
509
510   /* Try to convert some more */
511   fc->p = fc->ob = fc->bufo;
512   if (fc->ibl) {
513     size_t obl = sizeof (fc->bufo);
514
515     iconv (fc->cd, (ICONV_CONST char **) &fc->ib, &fc->ibl, &fc->ob, &obl);
516     if (fc->p < fc->ob)
517       return (unsigned char) *(fc->p)++;
518   }
519
520   /* If we trusted iconv a bit more, we would at this point
521    * ask why it had stopped converting ... */
522
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))) {
526     fc->p = 0;
527     return EOF;
528   }
529   if (fc->ibl)
530     memcpy (fc->bufi, fc->ib, fc->ibl);
531   fc->ib = fc->bufi;
532   fc->ibl +=
533     fread (fc->ib + fc->ibl, 1, sizeof (fc->bufi) - fc->ibl, fc->file);
534
535   /* Try harder this time to convert some */
536   if (fc->ibl) {
537     size_t obl = sizeof (fc->bufo);
538
539     mutt_iconv (fc->cd, (ICONV_CONST char **) &fc->ib, &fc->ibl, &fc->ob,
540                 &obl, fc->inrepls, 0);
541     if (fc->p < fc->ob)
542       return (unsigned char) *(fc->p)++;
543   }
544
545   /* Either the file has finished or one of the buffers is too small */
546   fc->p = 0;
547   return EOF;
548 }
549
550 void fgetconv_close (FGETCONV ** _fc)
551 {
552   struct fgetconv_s *fc = (struct fgetconv_s *) *_fc;
553
554   if (fc->cd != (iconv_t) - 1)
555     iconv_close (fc->cd);
556   mem_free (_fc);
557 }
558
559 char *mutt_get_first_charset (const char *charset)
560 {
561   static char fcharset[SHORT_STRING];
562   const char *c, *c1;
563
564   c = charset;
565   if (!str_len (c))
566     return "us-ascii";
567   if (!(c1 = strchr (c, ':')))
568     return ((char*) charset);
569   strfcpy (fcharset, c, c1 - c + 1);
570   return fcharset;
571 }
572
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)
576 {
577   iconv_t cd;
578   char *buf, *ob;
579   size_t obl, n;
580   int e;
581
582   cd = mutt_iconv_open (to, from, 0);
583   if (cd == (iconv_t) (-1))
584     return (size_t) (-1);
585   obl = 4 * flen + 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)) {
589     e = errno;
590     mem_free (&buf);
591     iconv_close (cd);
592     errno = e;
593     return (size_t) (-1);
594   }
595   *ob = '\0';
596
597   *tlen = ob - buf;
598
599   mem_realloc (&buf, ob - buf + 1);
600   *t = buf;
601   iconv_close (cd);
602
603   return n;
604 }
605
606 int mutt_convert_nonmime_string (char **ps)
607 {
608   const char *c, *c1;
609
610   for (c = AssumedCharset; c; c = c1 ? c1 + 1 : 0) {
611     char *u = *ps;
612     char *s = NULL;
613     char *fromcode;
614     size_t m, n;
615     size_t ulen = str_len (*ps);
616     size_t slen;
617
618     if (!u || !*u)
619       return 0;
620
621     c1 = strchr (c, ':');
622     n = c1 ? c1 - c : str_len (c);
623     if (!n)
624       continue;
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)) {
630       mem_free (ps);
631       *ps = s;
632       return 0;
633     }
634   }
635   return -1;
636 }