[PATCH] version.h doesn't depend on Makefile.am
[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
403     str_adjust (ps);
404     return 0;
405   }
406   else
407     return -1;
408 }
409
410
411 /*
412  * FGETCONV stuff for converting a file while reading it
413  * Used in sendlib.c for converting from mutt's Charset
414  */
415
416 struct fgetconv_s {
417   FILE *file;
418   iconv_t cd;
419   char bufi[512];
420   char bufo[512];
421   char *p;
422   char *ob;
423   char *ib;
424   size_t ibl;
425   const char **inrepls;
426 };
427
428 struct fgetconv_not {
429   FILE *file;
430   iconv_t cd;
431 };
432
433 FGETCONV *fgetconv_open (FILE * file, const char *from, const char *to,
434                          int flags)
435 {
436   struct fgetconv_s *fc;
437   iconv_t cd = (iconv_t) - 1;
438   static const char *repls[] = { "\357\277\275", "?", 0 };
439
440   if (from && to)
441     cd = mutt_iconv_open (to, from, flags);
442
443   if (cd != (iconv_t) - 1) {
444     fc = p_new(struct fgetconv_s, 1);
445     fc->p = fc->ob = fc->bufo;
446     fc->ib = fc->bufi;
447     fc->ibl = 0;
448     fc->inrepls = mutt_is_utf8 (to) ? repls : repls + 1;
449   }
450   else
451     fc = p_new(struct fgetconv_s, 1);
452   fc->file = file;
453   fc->cd = cd;
454   return (FGETCONV *) fc;
455 }
456
457 char *fgetconvs (char *buf, size_t l, FGETCONV * _fc)
458 {
459   int c;
460   size_t r;
461
462   for (r = 0; r + 1 < l;) {
463     if ((c = fgetconv (_fc)) == EOF)
464       break;
465     buf[r++] = (char) c;
466     if (c == '\n')
467       break;
468   }
469   buf[r] = '\0';
470
471   if (r)
472     return buf;
473   else
474     return NULL;
475 }
476
477 int fgetconv (FGETCONV * _fc)
478 {
479   struct fgetconv_s *fc = (struct fgetconv_s *) _fc;
480
481   if (!fc)
482     return EOF;
483   if (fc->cd == (iconv_t) - 1)
484     return fgetc (fc->file);
485   if (!fc->p)
486     return EOF;
487   if (fc->p < fc->ob)
488     return (unsigned char) *(fc->p)++;
489
490   /* Try to convert some more */
491   fc->p = fc->ob = fc->bufo;
492   if (fc->ibl) {
493     size_t obl = sizeof (fc->bufo);
494
495     my_iconv(fc->cd, (const char **) &fc->ib, &fc->ibl, &fc->ob, &obl);
496     if (fc->p < fc->ob)
497       return (unsigned char) *(fc->p)++;
498   }
499
500   /* If we trusted iconv a bit more, we would at this point
501    * ask why it had stopped converting ... */
502
503   /* Try to read some more */
504   if (fc->ibl == sizeof (fc->bufi) ||
505       (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof (fc->bufi))) {
506     fc->p = 0;
507     return EOF;
508   }
509   if (fc->ibl)
510     memcpy (fc->bufi, fc->ib, fc->ibl);
511   fc->ib = fc->bufi;
512   fc->ibl +=
513     fread (fc->ib + fc->ibl, 1, sizeof (fc->bufi) - fc->ibl, fc->file);
514
515   /* Try harder this time to convert some */
516   if (fc->ibl) {
517     size_t obl = sizeof (fc->bufo);
518
519     mutt_iconv (fc->cd, (const char **) &fc->ib, &fc->ibl, &fc->ob,
520                 &obl, fc->inrepls, 0);
521     if (fc->p < fc->ob)
522       return (unsigned char) *(fc->p)++;
523   }
524
525   /* Either the file has finished or one of the buffers is too small */
526   fc->p = 0;
527   return EOF;
528 }
529
530 void fgetconv_close (FGETCONV ** _fc)
531 {
532   struct fgetconv_s *fc = (struct fgetconv_s *) *_fc;
533
534   if (fc->cd != (iconv_t) - 1)
535     iconv_close (fc->cd);
536   p_delete(_fc);
537 }
538
539 const char *mutt_get_first_charset (const char *charset)
540 {
541   static char fcharset[SHORT_STRING];
542   const char *c, *c1;
543
544   c = charset;
545   if (!m_strlen(c))
546     return "us-ascii";
547   if (!(c1 = strchr (c, ':')))
548     return ((char*) charset);
549   m_strcpy(fcharset, c1 - c + 1, c);
550   return fcharset;
551 }
552
553 static size_t convert_string (const char *f, size_t flen,
554                               const char *from, const char *to,
555                               char **t, size_t * tlen)
556 {
557   iconv_t cd;
558   char *buf, *ob;
559   size_t obl;
560   ssize_t n;
561   int e;
562
563   cd = mutt_iconv_open (to, from, 0);
564   if (cd == (iconv_t) (-1))
565     return (size_t) (-1);
566   obl = 4 * flen + 1;
567   ob = buf = xmalloc(obl);
568   n = my_iconv(cd, &f, &flen, &ob, &obl);
569   if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
570     e = errno;
571     p_delete(&buf);
572     iconv_close (cd);
573     errno = e;
574     return (size_t) (-1);
575   }
576   *ob = '\0';
577
578   *tlen = ob - buf;
579
580   p_realloc(&buf, ob - buf + 1);
581   *t = buf;
582   iconv_close (cd);
583
584   return n;
585 }
586
587 int mutt_convert_nonmime_string (char **ps)
588 {
589   const char *c, *c1;
590
591   for (c = AssumedCharset; c; c = c1 ? c1 + 1 : 0) {
592     char *u = *ps;
593     char *s = NULL;
594     char *fromcode;
595     size_t m, n;
596     size_t ulen = m_strlen(*ps);
597     size_t slen;
598
599     if (!u || !*u)
600       return 0;
601
602     c1 = strchr (c, ':');
603     n = c1 ? c1 - c : m_strlen(c);
604     if (!n)
605       continue;
606     fromcode = p_dupstr(c, n);
607     m = convert_string (u, ulen, fromcode, Charset, &s, &slen);
608     p_delete(&fromcode);
609     if (m != (size_t) (-1)) {
610       p_delete(ps);
611       *ps = s;
612       return 0;
613     }
614   }
615   return -1;
616 }