use gperf for the charsets as well.
[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 #ifdef HAVE_LANGINFO_CODESET
25 #  include <langinfo.h>
26 #endif
27
28 #include <lib-lib/mem.h>
29 #include <lib-lib/ascii.h>
30 #include <lib-lib/str.h>
31 #include <lib-lib/macros.h>
32
33 #include "mutt.h"
34 #include "charset.h"
35
36 #ifndef EILSEQ
37 #  define EILSEQ EINVAL
38 #endif
39
40 char *Charset;
41 int Charset_is_utf8 = 0;
42
43 void mutt_set_langinfo_charset(void)
44 {
45 #ifdef HAVE_LANGINFO_CODESET
46     char buff[LONG_STRING];
47     char buff2[LONG_STRING];
48
49     m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
50     mutt_canonical_charset(buff2, sizeof(buff2), buff);
51
52     /* finally, set $charset */
53     if (!(Charset = m_strdup(buff2)))
54 #endif
55         Charset = m_strdup("iso-8859-1");
56 }
57
58 #include "charset.gperf"
59
60 void mutt_canonical_charset(char *dest, ssize_t dlen, const char *name)
61 {
62     const struct cset_pair *cp;
63     char scratch[LONG_STRING];
64     int i;
65
66     // canonize name: only keep a-z0-9 and dots, put into lowercase
67     for (i = 0; i < ssizeof(scratch); i++) {
68         if (isalnum(*name) || *name == '.') {
69             scratch[i++] = tolower((unsigned char)*name);
70         }
71
72         if (!*name || *name == ':') {
73             scratch[i] = '\0';
74             break;
75         }
76     }
77
78     cp = mutt_canonical_charset_aux(scratch, strlen(scratch));
79     m_strcpy(dest, dlen, cp ? cp->pref : name);
80 }
81
82 static int mutt_chscmp(const char *s, const char *chs)
83 {
84     char buffer[STRING];
85
86     if (!s)
87         return 0;
88
89     mutt_canonical_charset(buffer, sizeof(buffer), s);
90     return !strcmp(buffer, chs);
91 }
92
93 int mutt_is_utf8(const char *s)
94 {
95     return mutt_chscmp(s, "utf-8");
96 }
97
98 int mutt_is_us_ascii(const char *s)
99 {
100     return mutt_chscmp(s, "us-ascii");
101 }
102
103
104 /*
105  * Like iconv_open, but canonicalises the charsets
106  */
107
108 iconv_t mutt_iconv_open (const char *tocode, const char *fromcode, int flags)
109 {
110   char tocode1[SHORT_STRING];
111   char fromcode1[SHORT_STRING];
112   char *tocode2, *fromcode2;
113   char *tmp;
114
115   iconv_t cd;
116
117   mutt_canonical_charset (tocode1, sizeof (tocode1), tocode);
118
119 #ifdef M_ICONV_HOOK_TO
120   /* Not used. */
121   if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook (tocode1)))
122     mutt_canonical_charset (tocode1, sizeof (tocode1), tmp);
123 #endif
124
125   mutt_canonical_charset (fromcode1, sizeof (fromcode1), fromcode);
126   if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook (fromcode1)))
127     mutt_canonical_charset (fromcode1, sizeof (fromcode1), tmp);
128
129   if ((cd = iconv_open (tocode1, fromcode1)) != (iconv_t) - 1)
130     return cd;
131   if ((tocode2 = mutt_iconv_hook (tocode1))
132       && (fromcode2 = mutt_iconv_hook (fromcode1)))
133     return iconv_open (tocode2, fromcode2);
134
135   return (iconv_t) - 1;
136 }
137
138
139 /*
140  * Like iconv, but keeps going even when the input is invalid
141  * If you're supplying inrepls, the source charset should be stateless;
142  * if you're supplying an outrepl, the target charset should be.
143  */
144
145 ssize_t mutt_iconv(iconv_t cd, const char **inbuf, ssize_t *inbytesleft,
146                    char **outbuf, ssize_t *outbytesleft,
147                    const char **inrepls, const char *outrepl)
148 {
149   ssize_t ret = 0, ret1;
150   const char *ib = *inbuf;
151   ssize_t ibl = *inbytesleft;
152   char *ob = *outbuf;
153   ssize_t obl = *outbytesleft;
154
155   for (;;) {
156     ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
157     if (ret1 != -1)
158       ret += ret1;
159     if (ibl && obl && errno == EILSEQ) {
160       if (inrepls) {
161         /* Try replacing the input */
162         const char **t;
163
164         for (t = inrepls; *t; t++) {
165           const char *ib1 = *t;
166           ssize_t ibl1 = m_strlen(*t);
167           char *ob1 = ob;
168           ssize_t obl1 = obl;
169
170           my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
171           if (!ibl1) {
172             ++ib, --ibl;
173             ob = ob1, obl = obl1;
174             ++ret;
175             break;
176           }
177         }
178         if (*t)
179           continue;
180       }
181       /* Replace the output */
182       if (!outrepl)
183         outrepl = "?";
184       my_iconv(cd, 0, 0, &ob, &obl);
185       if (obl) {
186         ssize_t n = m_strlen(outrepl);
187
188         if (n > obl) {
189           outrepl = "?";
190           n = 1;
191         }
192         memcpy (ob, outrepl, n);
193         ++ib, --ibl;
194         ob += n, obl -= n;
195         ++ret;
196         my_iconv(cd, 0, 0, 0, 0); /* for good measure */
197         continue;
198       }
199     }
200     *inbuf = ib, *inbytesleft = ibl;
201     *outbuf = ob, *outbytesleft = obl;
202     return ret;
203   }
204 }
205
206
207 /*
208  * Convert a string
209  * Used in rfc2047.c and rfc2231.c
210  */
211
212 int mutt_convert_string (char **ps, const char *from, const char *to,
213                          int flags)
214 {
215   iconv_t cd;
216   const char *repls[] = { "\357\277\275", "?", 0 };
217   char *s = *ps;
218
219   if (!s || !*s)
220     return 0;
221
222   if (to && from && (cd = mutt_iconv_open (to, from, flags)) != (iconv_t) - 1) {
223     int len;
224     const char *ib;
225     char *buf, *ob;
226     ssize_t ibl, obl;
227     const char **inrepls = NULL;
228     const char *outrepl = NULL;
229
230     if (mutt_is_utf8 (to))
231       outrepl = "\357\277\275";
232     else if (mutt_is_utf8 (from))
233       inrepls = repls;
234     else
235       outrepl = "?";
236
237     len = m_strlen(s);
238     ib = s, ibl = len + 1;
239     obl = MB_LEN_MAX * ibl;
240     ob = buf = xmalloc(obl + 1);
241
242     mutt_iconv (cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
243     iconv_close (cd);
244
245     *ob = '\0';
246
247     p_delete(ps);
248     *ps = buf;
249     return 0;
250   }
251   else
252     return -1;
253 }
254
255
256 /*
257  * FGETCONV stuff for converting a file while reading it
258  * Used in sendlib.c for converting from mutt's Charset
259  */
260
261 struct fgetconv_s {
262   FILE *file;
263   iconv_t cd;
264   char bufi[512];
265   char bufo[512];
266   char *p;
267   char *ob;
268   char *ib;
269   ssize_t ibl;
270   const char **inrepls;
271 };
272
273 struct fgetconv_not {
274   FILE *file;
275   iconv_t cd;
276 };
277
278 FGETCONV *fgetconv_open (FILE * file, const char *from, const char *to,
279                          int flags)
280 {
281   struct fgetconv_s *fc;
282   iconv_t cd = (iconv_t) - 1;
283   static const char *repls[] = { "\357\277\275", "?", 0 };
284
285   if (from && to)
286     cd = mutt_iconv_open (to, from, flags);
287
288   if (cd != (iconv_t) - 1) {
289     fc = p_new(struct fgetconv_s, 1);
290     fc->p = fc->ob = fc->bufo;
291     fc->ib = fc->bufi;
292     fc->ibl = 0;
293     fc->inrepls = mutt_is_utf8 (to) ? repls : repls + 1;
294   }
295   else
296     fc = p_new(struct fgetconv_s, 1);
297   fc->file = file;
298   fc->cd = cd;
299   return (FGETCONV *) fc;
300 }
301
302 char *fgetconvs (char *buf, ssize_t l, FGETCONV * _fc)
303 {
304   int c;
305   ssize_t r;
306
307   for (r = 0; r + 1 < l;) {
308     if ((c = fgetconv (_fc)) == EOF)
309       break;
310     buf[r++] = (char) c;
311     if (c == '\n')
312       break;
313   }
314   buf[r] = '\0';
315
316   if (r)
317     return buf;
318   else
319     return NULL;
320 }
321
322 int fgetconv (FGETCONV * _fc)
323 {
324   struct fgetconv_s *fc = (struct fgetconv_s *) _fc;
325
326   if (!fc)
327     return EOF;
328   if (fc->cd == (iconv_t) - 1)
329     return fgetc (fc->file);
330   if (!fc->p)
331     return EOF;
332   if (fc->p < fc->ob)
333     return (unsigned char) *(fc->p)++;
334
335   /* Try to convert some more */
336   fc->p = fc->ob = fc->bufo;
337   if (fc->ibl) {
338     ssize_t obl = ssizeof(fc->bufo);
339
340     my_iconv(fc->cd, (const char **) &fc->ib, &fc->ibl, &fc->ob, &obl);
341     if (fc->p < fc->ob)
342       return (unsigned char) *(fc->p)++;
343   }
344
345   /* If we trusted iconv a bit more, we would at this point
346    * ask why it had stopped converting ... */
347
348   /* Try to read some more */
349   if (fc->ibl == sizeof (fc->bufi) ||
350       (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof (fc->bufi))) {
351     fc->p = 0;
352     return EOF;
353   }
354   if (fc->ibl)
355     memcpy (fc->bufi, fc->ib, fc->ibl);
356   fc->ib = fc->bufi;
357   fc->ibl +=
358     fread (fc->ib + fc->ibl, 1, sizeof (fc->bufi) - fc->ibl, fc->file);
359
360   /* Try harder this time to convert some */
361   if (fc->ibl) {
362     ssize_t obl = ssizeof(fc->bufo);
363
364     mutt_iconv (fc->cd, (const char **) &fc->ib, &fc->ibl, &fc->ob,
365                 &obl, fc->inrepls, 0);
366     if (fc->p < fc->ob)
367       return (unsigned char) *(fc->p)++;
368   }
369
370   /* Either the file has finished or one of the buffers is too small */
371   fc->p = 0;
372   return EOF;
373 }
374
375 void fgetconv_close (FGETCONV ** _fc)
376 {
377   struct fgetconv_s *fc = (struct fgetconv_s *) *_fc;
378
379   if (fc->cd != (iconv_t) - 1)
380     iconv_close (fc->cd);
381   p_delete(_fc);
382 }
383
384 const char *mutt_get_first_charset (const char *charset)
385 {
386   static char fcharset[SHORT_STRING];
387   const char *c, *c1;
388
389   c = charset;
390   if (!m_strlen(c))
391     return "us-ascii";
392   if (!(c1 = strchr (c, ':')))
393     return ((char*) charset);
394   m_strcpy(fcharset, c1 - c + 1, c);
395   return fcharset;
396 }
397
398 static ssize_t convert_string (const char *f, ssize_t flen,
399                               const char *from, const char *to,
400                               char **t, ssize_t * tlen)
401 {
402   iconv_t cd;
403   char *buf, *ob;
404   ssize_t obl;
405   ssize_t n;
406   int e;
407
408   cd = mutt_iconv_open (to, from, 0);
409   if (cd == (iconv_t) (-1))
410     return -1;
411   obl = 4 * flen + 1;
412   ob = buf = xmalloc(obl);
413   n = my_iconv(cd, &f, &flen, &ob, &obl);
414   if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
415     e = errno;
416     p_delete(&buf);
417     iconv_close (cd);
418     errno = e;
419     return -1;
420   }
421   *ob = '\0';
422
423   *tlen = ob - buf;
424
425   p_realloc(&buf, ob - buf + 1);
426   *t = buf;
427   iconv_close (cd);
428
429   return n;
430 }
431
432 int mutt_convert_nonmime_string (char **ps)
433 {
434   const char *c, *c1;
435
436   for (c = AssumedCharset; c; c = c1 ? c1 + 1 : 0) {
437     char *u = *ps;
438     char *s = NULL;
439     char *fromcode;
440     ssize_t m, n;
441     ssize_t ulen = m_strlen(*ps);
442     ssize_t slen;
443
444     if (!u || !*u)
445       return 0;
446
447     c1 = strchr (c, ':');
448     n = c1 ? c1 - c : m_strlen(c);
449     if (!n)
450       continue;
451     fromcode = p_dupstr(c, n);
452     m = convert_string (u, ulen, fromcode, Charset, &s, &slen);
453     p_delete(&fromcode);
454     if (m != -1) {
455       p_delete(ps);
456       *ps = s;
457       return 0;
458     }
459   }
460   return -1;
461 }
462
463 void mutt_set_charset (char *charset)
464 {
465     char buffer[STRING];
466
467     mutt_canonical_charset (buffer, sizeof (buffer), charset);
468     Charset_is_utf8 = !strcmp(buffer, "utf-8");
469
470 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
471     bind_textdomain_codeset (PACKAGE, buffer);
472 #endif
473 }
474
475 wchar_t replacement_char(void)
476 {
477     return Charset_is_utf8 ? 0xfffd : '?';
478 }