make charset_canonicalize set the answer to "us-ascii" if s is NULL.
[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     if (!name) {
102         m_strcpy(dest, dlen, "us-ascii");
103         return;
104     }
105
106     // canonize name: only keep a-z0-9 and dots, put into lowercase
107     for (p = name; *p && *p != ':' && i < ssizeof(scratch) - 1; p++) {
108         if (isalnum(*p) || *p== '.') {
109             scratch[i++] = tolower((unsigned char)*p);
110         }
111     }
112     scratch[i] = '\0';
113
114     cp = charset_canonicalize_aux(scratch, strlen(scratch));
115     if (cp) {
116         m_strcpy(dest, dlen, cp->pref);
117     } else {
118         m_strcpy(dest, dlen, name);
119         m_strtolower(dest);
120     }
121 }
122
123 /* XXX: MC: UGLY return of local static */
124 const char *charset_getfirst(const char *charset)
125 {
126     static char fcharset[SHORT_STRING];
127     const char *p;
128
129     if (m_strisempty(charset))
130         return "us-ascii";
131
132     p = m_strchrnul(charset, ':');
133     m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
134     return fcharset;
135 }
136
137 int charset_is_utf8(const char *s)
138 {
139     char buf[SHORT_STRING];
140     charset_canonicalize(buf, sizeof(buf), s);
141     return !strcmp(buf, "utf-8");
142 }
143
144 int charset_is_us_ascii(const char *s)
145 {
146     char buf[SHORT_STRING];
147     charset_canonicalize(buf, sizeof(buf), s);
148     return !strcmp(buf, "us-ascii");
149 }
150
151
152 /****************************************************************************/
153 /* iconv-line functions                                                     */
154 /****************************************************************************/
155
156 /* Like iconv_open, but canonicalises the charsets */
157 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
158 {
159     char tocode1[SHORT_STRING];
160     char fromcode1[SHORT_STRING];
161     const char *tmp;
162
163     iconv_t cd;
164
165     if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook(tocode1))) {
166         charset_canonicalize(tocode1, sizeof(tocode1), tmp);
167     } else {
168         charset_canonicalize(tocode1, sizeof(tocode1), tocode);
169     }
170
171     if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook(fromcode1))) {
172         charset_canonicalize(fromcode1, sizeof(fromcode1), tmp);
173     } else {
174         charset_canonicalize(fromcode1, sizeof(fromcode1), fromcode);
175     }
176
177     cd = iconv_open(tocode1, fromcode1);
178     if (cd != MUTT_ICONV_ERROR)
179         return cd;
180
181     {
182         const char *to = mutt_iconv_hook(tocode1);
183         const char *from = mutt_iconv_hook(fromcode1);
184
185         return to && from ? iconv_open(to, from) : MUTT_ICONV_ERROR;
186     }
187 }
188
189
190 /* Like iconv, but keeps going even when the input is invalid
191    If you're supplying inrepls, the source charset should be stateless;
192    if you're supplying an outrepl, the target charset should be.  */
193 /* XXX: MC: I do not understand what it does yet */
194 ssize_t mutt_iconv(iconv_t cd,
195                    const char **inbuf, ssize_t *inbytesleft,
196                    char **outbuf, ssize_t *outbytesleft,
197                    const char **inrepls, const char *outrepl)
198 {
199     ssize_t ret = 0, ret1;
200     const char *ib = *inbuf;
201     ssize_t ibl = *inbytesleft;
202     char *ob = *outbuf;
203     ssize_t obl = *outbytesleft;
204
205     for (;;) {
206         ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
207         if (ret1 != -1)
208             ret += ret1;
209
210         if (ibl && obl && errno == EILSEQ) {
211             if (inrepls) {
212                 /* Try replacing the input */
213                 const char **t;
214
215                 for (t = inrepls; *t; t++) {
216                     const char *ib1 = *t;
217                     ssize_t ibl1 = m_strlen(*t);
218                     char *ob1 = ob;
219                     ssize_t obl1 = obl;
220
221                     my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
222                     if (!ibl1) {
223                         ++ib, --ibl;
224                         ob = ob1, obl = obl1;
225                         ++ret;
226                         break;
227                     }
228                 }
229                 if (*t)
230                     continue;
231             }
232             /* Replace the output */
233             if (!outrepl)
234                 outrepl = "?";
235             my_iconv(cd, 0, 0, &ob, &obl);
236             if (obl) {
237                 ssize_t n = m_strlen(outrepl);
238
239                 if (n > obl) {
240                     outrepl = "?";
241                     n = 1;
242                 }
243                 memcpy(ob, outrepl, n);
244                 ++ib, --ibl;
245                 ob += n, obl -= n;
246                 ++ret;
247                 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
248                 continue;
249             }
250         }
251         *inbuf = ib, *inbytesleft = ibl;
252         *outbuf = ob, *outbytesleft = obl;
253         return ret;
254     }
255 }
256
257 /* Convert a string */
258 int
259 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
260 {
261     iconv_t cd;
262     const char *repls[] = { "\357\277\275", "?", 0 };
263
264     if (m_strisempty(*ps))
265         return 0;
266
267     cd = mutt_iconv_open(to, from, flags);
268     if (cd != MUTT_ICONV_ERROR) {
269         const char **inrepls = NULL;
270         const char *outrepl = NULL;
271         const char *ib;
272         char *buf, *ob;
273         ssize_t ibl, obl;
274
275         if (charset_is_utf8(to))
276             outrepl = "\357\277\275";
277         else
278         if (charset_is_utf8(from))
279             inrepls = repls;
280         else
281             outrepl = "?";
282
283         ibl = m_strlen(*ps) + 1;
284         ib  = *ps;
285
286         obl = MB_LEN_MAX * ibl;
287         ob  = buf = p_new(char, obl + 1);
288
289         mutt_iconv(cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
290         iconv_close(cd);
291
292         *ob = '\0';
293
294         p_delete(ps);
295         *ps = buf;
296         return 0;
297     }
298
299     return -1;
300 }
301
302 static ssize_t convert_string(const char *f, ssize_t flen,
303                               const char *from, const char *to,
304                               char **t, ssize_t * tlen)
305 {
306     iconv_t cd;
307     char *buf, *ob;
308     ssize_t obl;
309     ssize_t n;
310     int e;
311
312     if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
313         return -1;
314
315     obl = 4 * flen + 1;
316     ob  = buf = p_new(char, obl);
317     n   = my_iconv(cd, &f, &flen, &ob, &obl);
318
319     if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
320         e = errno;
321         p_delete(&buf);
322         iconv_close(cd);
323         errno = e;
324         return -1;
325     }
326
327     *ob   = '\0';
328     *tlen = ob - buf;
329     *t    = buf;
330     iconv_close(cd);
331     return n;
332 }
333
334 int mutt_convert_nonmime_string(char **ps)
335 {
336     const char *p = AssumedCharset;
337     ssize_t ulen = m_strlen(*ps);
338     char *u = *ps;
339
340     while (*p) {
341         const char *q;
342         char fromcode[LONG_STRING], *s = NULL;
343         ssize_t slen;
344
345         if (!ulen)
346             return 0;
347
348         while (*p == ':')
349             *p++;
350
351         q = m_strchrnul(p, ':');
352         m_strncpy(fromcode, sizeof(fromcode), p, q - p);
353         p = q;
354
355         if (convert_string(u, ulen, fromcode, Charset, &s, &slen) >= 0) {
356             p_delete(ps);
357             *ps = s;
358             return 0;
359         }
360     }
361
362     return -1;
363 }
364
365 /****************************************************************************/
366 /* fgetconv functions                                                       */
367 /****************************************************************************/
368
369 /* fgetconv_t stuff for converting a file while reading it
370    Used in sendlib.c for converting from mutt's Charset */
371
372 struct fgetconv_t {
373     FILE *file;
374     iconv_t cd;
375     char bufi[BUFSIZ];
376     char bufo[BUFSIZ];
377     char *p;
378     char *ob;
379     char *ib;
380     ssize_t ibl;
381     const char **inrepls;
382 };
383
384 fgetconv_t *
385 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
386 {
387     static const char *repls[] = { "\357\277\275", "?", 0 };
388
389     struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
390
391     fc->file = file;
392     fc->cd   = MUTT_ICONV_ERROR;
393     if (from && to)
394         fc->cd = mutt_iconv_open(to, from, flags);
395
396     if (fc->cd != MUTT_ICONV_ERROR) {
397         fc->p  = fc->ob = fc->bufo;
398         fc->ib = fc->bufi;
399         fc->ibl = 0;
400         fc->inrepls = repls + charset_is_utf8(to);
401     }
402     return fc;
403 }
404
405 void fgetconv_close(fgetconv_t **fcp)
406 {
407     struct fgetconv_t *fc = *fcp;
408
409     if (fc->cd != MUTT_ICONV_ERROR)
410         iconv_close (fc->cd);
411     p_delete(fcp);
412 }
413
414
415 int fgetconv(fgetconv_t *fc)
416 {
417     if (!fc)
418         return EOF;
419
420     if (fc->cd == MUTT_ICONV_ERROR)
421         return fgetc(fc->file);
422
423     if (!fc->p)
424         return EOF;
425     if (fc->p < fc->ob)
426         return (unsigned char)*(fc->p)++;
427
428     /* Try to convert some more */
429     fc->p = fc->ob = fc->bufo;
430     if (fc->ibl) {
431         ssize_t obl = ssizeof(fc->bufo);
432
433         my_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
434         if (fc->p < fc->ob)
435             return (unsigned char)*(fc->p)++;
436     }
437
438     /* If we trusted iconv a bit more, we would at this point
439      * ask why it had stopped converting ... */
440
441     /* Try to read some more */
442     if (fc->ibl == sizeof(fc->bufi)
443     || (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof(fc->bufi))) {
444         fc->p = NULL;
445         return EOF;
446     }
447
448     if (fc->ibl) {
449         memcpy(fc->bufi, fc->ib, fc->ibl);
450     }
451     fc->ib = fc->bufi;
452     fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
453                      fc->file);
454
455     /* Try harder this time to convert some */
456     if (fc->ibl) {
457         ssize_t obl = ssizeof(fc->bufo);
458
459         mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
460                    fc->inrepls, 0);
461         if (fc->p < fc->ob) {
462             return (unsigned char)*(fc->p)++;
463         }
464     }
465
466     /* Either the file has finished or one of the buffers is too small */
467     fc->p = NULL;
468     return EOF;
469 }
470
471 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
472 {
473     ssize_t pos = 0;
474
475     while (pos < len - 1) {
476         int c = fgetconv(fc);
477         if (c == EOF)
478             break;
479
480         buf[pos++] = c;
481         if (c == '\n')
482             break;
483     }
484     buf[pos] = '\0';
485
486     return pos ? buf : NULL;
487 }