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