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.
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.
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,
17 * Copyright © 2006 Pierre Habouzit
20 * Copyright notice from original mutt:
21 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
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.
38 #include <sys/types.h>
42 #ifdef HAVE_LANGINFO_CODESET
43 # include <langinfo.h>
46 #include <lib-lib/mem.h>
47 #include <lib-lib/ascii.h>
48 #include <lib-lib/str.h>
49 #include <lib-lib/macros.h>
55 # define EILSEQ EINVAL
59 int Charset_is_utf8 = 0;
60 wchar_t CharsetReplacement = '?';
63 /****************************************************************************/
64 /* charset functions */
65 /****************************************************************************/
67 void charset_initialize(void)
69 #ifdef HAVE_LANGINFO_CODESET
70 char buff[SHORT_STRING];
71 char buff2[SHORT_STRING];
73 m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
74 charset_canonicalize(buff2, sizeof(buff2), buff);
76 /* finally, set $charset */
77 if (!m_strisempty(buff2)) {
78 m_strreplace(&Charset, buff2);
82 m_strreplace(&Charset, "iso-8859-1");
85 Charset_is_utf8 = !strcmp(Charset, "utf-8");
86 CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
88 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
89 bind_textdomain_codeset(PACKAGE, Charset);
93 #include "charset.gperf"
94 void charset_canonicalize(char *dest, ssize_t dlen, const char *name)
96 const struct cset_pair *cp;
97 char scratch[SHORT_STRING];
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);
109 cp = charset_canonicalize_aux(scratch, strlen(scratch));
111 m_strcpy(dest, dlen, cp->pref);
113 m_strcpy(dest, dlen, name);
118 /* XXX: MC: UGLY return of local static */
119 const char *charset_getfirst(const char *charset)
121 static char fcharset[SHORT_STRING];
124 if (m_strisempty(charset))
127 p = m_strchrnul(charset, ':');
128 m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
132 static int mutt_chscmp(const char *s, const char *chs)
134 char buffer[SHORT_STRING];
139 charset_canonicalize(buffer, sizeof(buffer), s);
140 return !strcmp(buffer, chs);
143 int charset_is_utf8(const char *s)
145 return mutt_chscmp(s, "utf-8");
148 int charset_is_us_ascii(const char *s)
150 return mutt_chscmp(s, "us-ascii");
154 /****************************************************************************/
155 /* iconv-line functions */
156 /****************************************************************************/
158 /* Like iconv_open, but canonicalises the charsets */
159 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
161 char tocode1[SHORT_STRING];
162 char fromcode1[SHORT_STRING];
167 if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook(tocode1))) {
168 charset_canonicalize(tocode1, sizeof(tocode1), tmp);
170 charset_canonicalize(tocode1, sizeof(tocode1), tocode);
173 if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook(fromcode1))) {
174 charset_canonicalize(fromcode1, sizeof(fromcode1), tmp);
176 charset_canonicalize(fromcode1, sizeof(fromcode1), fromcode);
179 cd = iconv_open(tocode1, fromcode1);
180 if (cd != MUTT_ICONV_ERROR)
184 const char *to = mutt_iconv_hook(tocode1);
185 const char *from = mutt_iconv_hook(fromcode1);
187 return to && from ? iconv_open(to, from) : MUTT_ICONV_ERROR;
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)
201 ssize_t ret = 0, ret1;
202 const char *ib = *inbuf;
203 ssize_t ibl = *inbytesleft;
205 ssize_t obl = *outbytesleft;
208 ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
212 if (ibl && obl && errno == EILSEQ) {
214 /* Try replacing the input */
217 for (t = inrepls; *t; t++) {
218 const char *ib1 = *t;
219 ssize_t ibl1 = m_strlen(*t);
223 my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
226 ob = ob1, obl = obl1;
234 /* Replace the output */
237 my_iconv(cd, 0, 0, &ob, &obl);
239 ssize_t n = m_strlen(outrepl);
245 memcpy(ob, outrepl, n);
249 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
253 *inbuf = ib, *inbytesleft = ibl;
254 *outbuf = ob, *outbytesleft = obl;
259 /* Convert a string */
261 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
264 const char *repls[] = { "\357\277\275", "?", 0 };
266 if (m_strisempty(*ps))
269 cd = mutt_iconv_open(to, from, flags);
270 if (cd != MUTT_ICONV_ERROR) {
271 const char **inrepls = NULL;
272 const char *outrepl = NULL;
277 if (charset_is_utf8(to))
278 outrepl = "\357\277\275";
280 if (charset_is_utf8(from))
285 ibl = m_strlen(*ps) + 1;
288 obl = MB_LEN_MAX * ibl;
289 ob = buf = p_new(char, obl + 1);
291 mutt_iconv(cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
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)
314 if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
318 ob = buf = p_new(char, obl);
319 n = my_iconv(cd, &f, &flen, &ob, &obl);
321 if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
336 int mutt_convert_nonmime_string(char **ps)
338 const char *p = AssumedCharset;
339 ssize_t ulen = m_strlen(*ps);
344 char fromcode[LONG_STRING], *s = NULL;
353 q = m_strchrnul(p, ':');
354 m_strncpy(fromcode, sizeof(fromcode), p, q - p);
357 if (convert_string(u, ulen, fromcode, Charset, &s, &slen) >= 0) {
367 /****************************************************************************/
368 /* fgetconv functions */
369 /****************************************************************************/
371 /* fgetconv_t stuff for converting a file while reading it
372 Used in sendlib.c for converting from mutt's Charset */
383 const char **inrepls;
387 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
389 static const char *repls[] = { "\357\277\275", "?", 0 };
391 struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
394 fc->cd = MUTT_ICONV_ERROR;
396 fc->cd = mutt_iconv_open(to, from, flags);
398 if (fc->cd != MUTT_ICONV_ERROR) {
399 fc->p = fc->ob = fc->bufo;
402 fc->inrepls = repls + charset_is_utf8(to);
407 void fgetconv_close(fgetconv_t **fcp)
409 struct fgetconv_t *fc = *fcp;
411 if (fc->cd != MUTT_ICONV_ERROR)
412 iconv_close (fc->cd);
417 int fgetconv(fgetconv_t *fc)
422 if (fc->cd == MUTT_ICONV_ERROR)
423 return fgetc(fc->file);
428 return (unsigned char)*(fc->p)++;
430 /* Try to convert some more */
431 fc->p = fc->ob = fc->bufo;
433 ssize_t obl = ssizeof(fc->bufo);
435 my_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
437 return (unsigned char)*(fc->p)++;
440 /* If we trusted iconv a bit more, we would at this point
441 * ask why it had stopped converting ... */
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))) {
451 memcpy(fc->bufi, fc->ib, fc->ibl);
454 fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
457 /* Try harder this time to convert some */
459 ssize_t obl = ssizeof(fc->bufo);
461 mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
463 if (fc->p < fc->ob) {
464 return (unsigned char)*(fc->p)++;
468 /* Either the file has finished or one of the buffers is too small */
473 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
477 while (pos < len - 1) {
478 int c = fgetconv(fc);
488 return pos ? buf : NULL;