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];
102 m_strcpy(dest, dlen, "us-ascii");
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);
114 cp = charset_canonicalize_aux(scratch, strlen(scratch));
116 m_strcpy(dest, dlen, cp->pref);
118 m_strcpy(dest, dlen, name);
123 /* XXX: MC: UGLY return of local static */
124 const char *charset_getfirst(const char *charset)
126 static char fcharset[SHORT_STRING];
129 if (m_strisempty(charset))
132 p = m_strchrnul(charset, ':');
133 m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
137 int charset_is_utf8(const char *s)
139 char buf[SHORT_STRING];
140 charset_canonicalize(buf, sizeof(buf), s);
141 return !strcmp(buf, "utf-8");
144 int charset_is_us_ascii(const char *s)
146 char buf[SHORT_STRING];
147 charset_canonicalize(buf, sizeof(buf), s);
148 return !strcmp(buf, "us-ascii");
152 /****************************************************************************/
153 /* iconv-line functions */
154 /****************************************************************************/
156 /* Like iconv_open, but canonicalises the charsets */
157 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
159 char tocode1[SHORT_STRING];
160 char fromcode1[SHORT_STRING];
165 if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook(tocode1))) {
166 charset_canonicalize(tocode1, sizeof(tocode1), tmp);
168 charset_canonicalize(tocode1, sizeof(tocode1), tocode);
171 if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook(fromcode1))) {
172 charset_canonicalize(fromcode1, sizeof(fromcode1), tmp);
174 charset_canonicalize(fromcode1, sizeof(fromcode1), fromcode);
177 cd = iconv_open(tocode1, fromcode1);
178 if (cd != MUTT_ICONV_ERROR)
182 const char *to = mutt_iconv_hook(tocode1);
183 const char *from = mutt_iconv_hook(fromcode1);
185 return to && from ? iconv_open(to, from) : MUTT_ICONV_ERROR;
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)
199 ssize_t ret = 0, ret1;
200 const char *ib = *inbuf;
201 ssize_t ibl = *inbytesleft;
203 ssize_t obl = *outbytesleft;
206 ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
210 if (ibl && obl && errno == EILSEQ) {
212 /* Try replacing the input */
215 for (t = inrepls; *t; t++) {
216 const char *ib1 = *t;
217 ssize_t ibl1 = m_strlen(*t);
221 my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
224 ob = ob1, obl = obl1;
232 /* Replace the output */
235 my_iconv(cd, 0, 0, &ob, &obl);
237 ssize_t n = m_strlen(outrepl);
243 memcpy(ob, outrepl, n);
247 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
251 *inbuf = ib, *inbytesleft = ibl;
252 *outbuf = ob, *outbytesleft = obl;
257 /* Convert a string */
259 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
262 const char *repls[] = { "\357\277\275", "?", 0 };
264 if (m_strisempty(*ps))
267 cd = mutt_iconv_open(to, from, flags);
268 if (cd != MUTT_ICONV_ERROR) {
269 const char **inrepls = NULL;
270 const char *outrepl = NULL;
275 if (charset_is_utf8(to))
276 outrepl = "\357\277\275";
278 if (charset_is_utf8(from))
283 ibl = m_strlen(*ps) + 1;
286 obl = MB_LEN_MAX * ibl;
287 ob = buf = p_new(char, obl + 1);
289 mutt_iconv(cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
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)
312 if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
316 ob = buf = p_new(char, obl);
317 n = my_iconv(cd, &f, &flen, &ob, &obl);
319 if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
334 int mutt_convert_nonmime_string(char **ps)
336 const char *p = AssumedCharset;
337 ssize_t ulen = m_strlen(*ps);
342 char fromcode[LONG_STRING], *s = NULL;
351 q = m_strchrnul(p, ':');
352 m_strncpy(fromcode, sizeof(fromcode), p, q - p);
355 if (convert_string(u, ulen, fromcode, Charset, &s, &slen) >= 0) {
365 /****************************************************************************/
366 /* fgetconv functions */
367 /****************************************************************************/
369 /* fgetconv_t stuff for converting a file while reading it
370 Used in sendlib.c for converting from mutt's Charset */
381 const char **inrepls;
385 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
387 static const char *repls[] = { "\357\277\275", "?", 0 };
389 struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
392 fc->cd = MUTT_ICONV_ERROR;
394 fc->cd = mutt_iconv_open(to, from, flags);
396 if (fc->cd != MUTT_ICONV_ERROR) {
397 fc->p = fc->ob = fc->bufo;
400 fc->inrepls = repls + charset_is_utf8(to);
405 void fgetconv_close(fgetconv_t **fcp)
407 struct fgetconv_t *fc = *fcp;
409 if (fc->cd != MUTT_ICONV_ERROR)
410 iconv_close (fc->cd);
415 int fgetconv(fgetconv_t *fc)
420 if (fc->cd == MUTT_ICONV_ERROR)
421 return fgetc(fc->file);
426 return (unsigned char)*(fc->p)++;
428 /* Try to convert some more */
429 fc->p = fc->ob = fc->bufo;
431 ssize_t obl = ssizeof(fc->bufo);
433 my_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
435 return (unsigned char)*(fc->p)++;
438 /* If we trusted iconv a bit more, we would at this point
439 * ask why it had stopped converting ... */
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))) {
449 memcpy(fc->bufi, fc->ib, fc->ibl);
452 fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
455 /* Try harder this time to convert some */
457 ssize_t obl = ssizeof(fc->bufo);
459 mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
461 if (fc->p < fc->ob) {
462 return (unsigned char)*(fc->p)++;
466 /* Either the file has finished or one of the buffers is too small */
471 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
475 while (pos < len - 1) {
476 int c = fgetconv(fc);
486 return pos ? buf : NULL;