move all the parameter related functions into the lib-mime.
[apps/madmutt.git] / charset.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19 /*
20  * Copyright notice from original mutt:
21  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
22  *
23  * This file is part of mutt-ng, see http://www.muttng.org/.
24  * It's licensed under the GNU General Public License,
25  * please see the file GPL in the top level source directory.
26  */
27
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #include <ctype.h>
37
38 #include <sys/types.h>
39 #include <dirent.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #ifdef HAVE_LANGINFO_CODESET
43 #  include <langinfo.h>
44 #endif
45
46 #include <lib-lib/mem.h>
47 #include <lib-lib/ascii.h>
48 #include <lib-lib/str.h>
49 #include <lib-lib/macros.h>
50
51 #include "mutt.h"
52 #include "charset.h"
53
54 #ifndef EILSEQ
55 #  define EILSEQ EINVAL
56 #endif
57
58 char *Charset;
59 int Charset_is_utf8 = 0;
60 wchar_t CharsetReplacement = '?';
61
62
63 /****************************************************************************/
64 /* charset functions                                                        */
65 /****************************************************************************/
66
67 void charset_initialize(void)
68 {
69 #ifdef HAVE_LANGINFO_CODESET
70     char buff[SHORT_STRING];
71     char buff2[SHORT_STRING];
72
73     m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
74     charset_canonicalize(buff2, sizeof(buff2), buff);
75
76     /* finally, set $charset */
77     if (!m_strisempty(buff2)) {
78         m_strreplace(&Charset, buff2);
79     } else
80 #endif
81     {
82         m_strreplace(&Charset, "iso-8859-1");
83     }
84
85     Charset_is_utf8    = !strcmp(Charset, "utf-8");
86     CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
87
88 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
89     bind_textdomain_codeset(PACKAGE, Charset);
90 #endif
91 }
92
93 #include "charset.gperf"
94 void charset_canonicalize(char *dest, ssize_t dlen, const char *name)
95 {
96     const struct cset_pair *cp;
97     char scratch[SHORT_STRING];
98     const char *p;
99     int i = 0;
100
101     // canonize name: only keep a-z0-9 and dots, put into lowercase
102     for (p = name; *p && *p != ':' && i < ssizeof(scratch) - 1; p++) {
103         if (isalnum(*p) || *p== '.') {
104             scratch[i++] = tolower((unsigned char)*p);
105         }
106     }
107     scratch[i] = '\0';
108
109     cp = charset_canonicalize_aux(scratch, strlen(scratch));
110     if (cp) {
111         m_strcpy(dest, dlen, cp->pref);
112     } else {
113         m_strcpy(dest, dlen, name);
114         m_strtolower(dest);
115     }
116 }
117
118 /* XXX: MC: UGLY return of local static */
119 const char *charset_getfirst(const char *charset)
120 {
121     static char fcharset[SHORT_STRING];
122     const char *p;
123
124     if (m_strisempty(charset))
125         return "us-ascii";
126
127     p = m_strchrnul(charset, ':');
128     m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
129     return fcharset;
130 }
131
132 static int mutt_chscmp(const char *s, const char *chs)
133 {
134     char buffer[SHORT_STRING];
135
136     if (!s)
137         return 0;
138
139     charset_canonicalize(buffer, sizeof(buffer), s);
140     return !strcmp(buffer, chs);
141 }
142
143 int charset_is_utf8(const char *s)
144 {
145     return mutt_chscmp(s, "utf-8");
146 }
147
148 int charset_is_us_ascii(const char *s)
149 {
150     return mutt_chscmp(s, "us-ascii");
151 }
152
153
154 /****************************************************************************/
155 /* iconv-line functions                                                     */
156 /****************************************************************************/
157
158 /* Like iconv_open, but canonicalises the charsets */
159 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
160 {
161     char tocode1[SHORT_STRING];
162     char fromcode1[SHORT_STRING];
163     const char *tmp;
164
165     iconv_t cd;
166
167     if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook(tocode1))) {
168         charset_canonicalize(tocode1, sizeof(tocode1), tmp);
169     } else {
170         charset_canonicalize(tocode1, sizeof(tocode1), tocode);
171     }
172
173     if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook(fromcode1))) {
174         charset_canonicalize(fromcode1, sizeof(fromcode1), tmp);
175     } else {
176         charset_canonicalize(fromcode1, sizeof(fromcode1), fromcode);
177     }
178
179     cd = iconv_open(tocode1, fromcode1);
180     if (cd != MUTT_ICONV_ERROR)
181         return cd;
182
183     {
184         const char *to = mutt_iconv_hook(tocode1);
185         const char *from = mutt_iconv_hook(fromcode1);
186
187         return to && from ? iconv_open(to, from) : MUTT_ICONV_ERROR;
188     }
189 }
190
191
192 /* Like iconv, but keeps going even when the input is invalid
193    If you're supplying inrepls, the source charset should be stateless;
194    if you're supplying an outrepl, the target charset should be.  */
195 /* XXX: MC: I do not understand what it does yet */
196 ssize_t mutt_iconv(iconv_t cd,
197                    const char **inbuf, ssize_t *inbytesleft,
198                    char **outbuf, ssize_t *outbytesleft,
199                    const char **inrepls, const char *outrepl)
200 {
201     ssize_t ret = 0, ret1;
202     const char *ib = *inbuf;
203     ssize_t ibl = *inbytesleft;
204     char *ob = *outbuf;
205     ssize_t obl = *outbytesleft;
206
207     for (;;) {
208         ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
209         if (ret1 != -1)
210             ret += ret1;
211
212         if (ibl && obl && errno == EILSEQ) {
213             if (inrepls) {
214                 /* Try replacing the input */
215                 const char **t;
216
217                 for (t = inrepls; *t; t++) {
218                     const char *ib1 = *t;
219                     ssize_t ibl1 = m_strlen(*t);
220                     char *ob1 = ob;
221                     ssize_t obl1 = obl;
222
223                     my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
224                     if (!ibl1) {
225                         ++ib, --ibl;
226                         ob = ob1, obl = obl1;
227                         ++ret;
228                         break;
229                     }
230                 }
231                 if (*t)
232                     continue;
233             }
234             /* Replace the output */
235             if (!outrepl)
236                 outrepl = "?";
237             my_iconv(cd, 0, 0, &ob, &obl);
238             if (obl) {
239                 ssize_t n = m_strlen(outrepl);
240
241                 if (n > obl) {
242                     outrepl = "?";
243                     n = 1;
244                 }
245                 memcpy(ob, outrepl, n);
246                 ++ib, --ibl;
247                 ob += n, obl -= n;
248                 ++ret;
249                 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
250                 continue;
251             }
252         }
253         *inbuf = ib, *inbytesleft = ibl;
254         *outbuf = ob, *outbytesleft = obl;
255         return ret;
256     }
257 }
258
259 /* Convert a string */
260 int
261 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
262 {
263     iconv_t cd;
264     const char *repls[] = { "\357\277\275", "?", 0 };
265
266     if (m_strisempty(*ps))
267         return 0;
268
269     cd = mutt_iconv_open(to, from, flags);
270     if (cd != MUTT_ICONV_ERROR) {
271         const char **inrepls = NULL;
272         const char *outrepl = NULL;
273         const char *ib;
274         char *buf, *ob;
275         ssize_t ibl, obl;
276
277         if (charset_is_utf8(to))
278             outrepl = "\357\277\275";
279         else
280         if (charset_is_utf8(from))
281             inrepls = repls;
282         else
283             outrepl = "?";
284
285         ibl = m_strlen(*ps) + 1;
286         ib  = *ps;
287
288         obl = MB_LEN_MAX * ibl;
289         ob  = buf = p_new(char, obl + 1);
290
291         mutt_iconv(cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
292         iconv_close(cd);
293
294         *ob = '\0';
295
296         p_delete(ps);
297         *ps = buf;
298         return 0;
299     }
300
301     return -1;
302 }
303
304 static ssize_t convert_string(const char *f, ssize_t flen,
305                               const char *from, const char *to,
306                               char **t, ssize_t * tlen)
307 {
308     iconv_t cd;
309     char *buf, *ob;
310     ssize_t obl;
311     ssize_t n;
312     int e;
313
314     if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
315         return -1;
316
317     obl = 4 * flen + 1;
318     ob  = buf = p_new(char, obl);
319     n   = my_iconv(cd, &f, &flen, &ob, &obl);
320
321     if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
322         e = errno;
323         p_delete(&buf);
324         iconv_close(cd);
325         errno = e;
326         return -1;
327     }
328
329     *ob   = '\0';
330     *tlen = ob - buf;
331     *t    = buf;
332     iconv_close(cd);
333     return n;
334 }
335
336 int mutt_convert_nonmime_string(char **ps)
337 {
338     const char *p = AssumedCharset;
339     ssize_t ulen = m_strlen(*ps);
340     char *u = *ps;
341
342     while (*p) {
343         const char *q;
344         char fromcode[LONG_STRING], *s = NULL;
345         ssize_t slen;
346
347         if (!ulen)
348             return 0;
349
350         while (*p == ':')
351             *p++;
352
353         q = m_strchrnul(p, ':');
354         m_strncpy(fromcode, sizeof(fromcode), p, q - p);
355         p = q;
356
357         if (convert_string(u, ulen, fromcode, Charset, &s, &slen) >= 0) {
358             p_delete(ps);
359             *ps = s;
360             return 0;
361         }
362     }
363
364     return -1;
365 }
366
367 /****************************************************************************/
368 /* fgetconv functions                                                       */
369 /****************************************************************************/
370
371 /* fgetconv_t stuff for converting a file while reading it
372    Used in sendlib.c for converting from mutt's Charset */
373
374 struct fgetconv_t {
375     FILE *file;
376     iconv_t cd;
377     char bufi[BUFSIZ];
378     char bufo[BUFSIZ];
379     char *p;
380     char *ob;
381     char *ib;
382     ssize_t ibl;
383     const char **inrepls;
384 };
385
386 fgetconv_t *
387 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
388 {
389     static const char *repls[] = { "\357\277\275", "?", 0 };
390
391     struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
392
393     fc->file = file;
394     fc->cd   = MUTT_ICONV_ERROR;
395     if (from && to)
396         fc->cd = mutt_iconv_open(to, from, flags);
397
398     if (fc->cd != MUTT_ICONV_ERROR) {
399         fc->p  = fc->ob = fc->bufo;
400         fc->ib = fc->bufi;
401         fc->ibl = 0;
402         fc->inrepls = repls + charset_is_utf8(to);
403     }
404     return fc;
405 }
406
407 void fgetconv_close(fgetconv_t **fcp)
408 {
409     struct fgetconv_t *fc = *fcp;
410
411     if (fc->cd != MUTT_ICONV_ERROR)
412         iconv_close (fc->cd);
413     p_delete(fcp);
414 }
415
416
417 int fgetconv(fgetconv_t *fc)
418 {
419     if (!fc)
420         return EOF;
421
422     if (fc->cd == MUTT_ICONV_ERROR)
423         return fgetc(fc->file);
424
425     if (!fc->p)
426         return EOF;
427     if (fc->p < fc->ob)
428         return (unsigned char)*(fc->p)++;
429
430     /* Try to convert some more */
431     fc->p = fc->ob = fc->bufo;
432     if (fc->ibl) {
433         ssize_t obl = ssizeof(fc->bufo);
434
435         my_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
436         if (fc->p < fc->ob)
437             return (unsigned char)*(fc->p)++;
438     }
439
440     /* If we trusted iconv a bit more, we would at this point
441      * ask why it had stopped converting ... */
442
443     /* Try to read some more */
444     if (fc->ibl == sizeof(fc->bufi)
445     || (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof(fc->bufi))) {
446         fc->p = NULL;
447         return EOF;
448     }
449
450     if (fc->ibl) {
451         memcpy(fc->bufi, fc->ib, fc->ibl);
452     }
453     fc->ib = fc->bufi;
454     fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
455                      fc->file);
456
457     /* Try harder this time to convert some */
458     if (fc->ibl) {
459         ssize_t obl = ssizeof(fc->bufo);
460
461         mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
462                    fc->inrepls, 0);
463         if (fc->p < fc->ob) {
464             return (unsigned char)*(fc->p)++;
465         }
466     }
467
468     /* Either the file has finished or one of the buffers is too small */
469     fc->p = NULL;
470     return EOF;
471 }
472
473 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
474 {
475     ssize_t pos = 0;
476
477     while (pos < len - 1) {
478         int c = fgetconv(fc);
479         if (c == EOF)
480             break;
481
482         buf[pos++] = c;
483         if (c == '\n')
484             break;
485     }
486     buf[pos] = '\0';
487
488     return pos ? buf : NULL;
489 }