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/lib-lib.h>
52 # define EILSEQ EINVAL
56 int Charset_is_utf8 = 0;
57 wchar_t CharsetReplacement = '?';
60 /****************************************************************************/
61 /* charset functions */
62 /****************************************************************************/
64 void charset_initialize(void)
66 #ifdef HAVE_LANGINFO_CODESET
67 char buff[SHORT_STRING];
68 char buff2[SHORT_STRING];
70 m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
71 charset_canonicalize(buff2, sizeof(buff2), buff);
73 /* finally, set $charset */
74 if (!m_strisempty(buff2)) {
75 m_strreplace(&Charset, buff2);
79 m_strreplace(&Charset, "iso-8859-1");
82 Charset_is_utf8 = !strcmp(Charset, "utf-8");
83 CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
85 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
86 bind_textdomain_codeset(PACKAGE, Charset);
90 #include "charset.gperf"
91 void charset_canonicalize(char *dest, ssize_t dlen, const char *name)
93 const struct cset_pair *cp;
94 char scratch[SHORT_STRING];
99 m_strcpy(dest, dlen, "us-ascii");
103 // canonize name: only keep a-z0-9 and dots, put into lowercase
104 for (p = name; *p && *p != ':' && i < ssizeof(scratch) - 1; p++) {
105 if (isalnum(*p) || *p== '.') {
106 scratch[i++] = tolower((unsigned char)*p);
111 cp = charset_canonicalize_aux(scratch, strlen(scratch));
113 m_strcpy(dest, dlen, cp->pref);
115 m_strcpy(dest, dlen, name);
120 /* XXX: MC: UGLY return of local static */
121 const char *charset_getfirst(const char *charset)
123 static char fcharset[SHORT_STRING];
126 if (m_strisempty(charset))
129 p = m_strchrnul(charset, ':');
130 m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
134 int charset_is_utf8(const char *s)
136 char buf[SHORT_STRING];
137 charset_canonicalize(buf, sizeof(buf), s);
138 return !strcmp(buf, "utf-8");
141 int charset_is_us_ascii(const char *s)
143 char buf[SHORT_STRING];
144 charset_canonicalize(buf, sizeof(buf), s);
145 return !strcmp(buf, "us-ascii");
149 /****************************************************************************/
150 /* iconv-line functions */
151 /****************************************************************************/
153 /* Like iconv_open, but canonicalises the charsets */
154 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
156 char tocode1[SHORT_STRING];
157 char fromcode1[SHORT_STRING];
162 if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook(tocode1))) {
163 charset_canonicalize(tocode1, sizeof(tocode1), tmp);
165 charset_canonicalize(tocode1, sizeof(tocode1), tocode);
168 if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook(fromcode1))) {
169 charset_canonicalize(fromcode1, sizeof(fromcode1), tmp);
171 charset_canonicalize(fromcode1, sizeof(fromcode1), fromcode);
174 cd = iconv_open(tocode1, fromcode1);
175 if (cd != MUTT_ICONV_ERROR)
179 const char *to = mutt_iconv_hook(tocode1);
180 const char *from = mutt_iconv_hook(fromcode1);
182 return to && from ? iconv_open(to, from) : MUTT_ICONV_ERROR;
187 /* Like iconv, but keeps going even when the input is invalid
188 If you're supplying inrepls, the source charset should be stateless;
189 if you're supplying an outrepl, the target charset should be. */
190 /* XXX: MC: I do not understand what it does yet */
191 ssize_t mutt_iconv(iconv_t cd,
192 const char **inbuf, ssize_t *inbytesleft,
193 char **outbuf, ssize_t *outbytesleft,
194 const char **inrepls, const char *outrepl)
196 ssize_t ret = 0, ret1;
197 const char *ib = *inbuf;
198 ssize_t ibl = *inbytesleft;
200 ssize_t obl = *outbytesleft;
203 ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
207 if (ibl && obl && errno == EILSEQ) {
209 /* Try replacing the input */
212 for (t = inrepls; *t; t++) {
213 const char *ib1 = *t;
214 ssize_t ibl1 = m_strlen(*t);
218 my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
221 ob = ob1, obl = obl1;
229 /* Replace the output */
232 my_iconv(cd, 0, 0, &ob, &obl);
234 ssize_t n = m_strlen(outrepl);
240 memcpy(ob, outrepl, n);
244 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
248 *inbuf = ib, *inbytesleft = ibl;
249 *outbuf = ob, *outbytesleft = obl;
254 /* Convert a string */
256 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
259 const char *repls[] = { "\357\277\275", "?", 0 };
261 if (m_strisempty(*ps))
264 cd = mutt_iconv_open(to, from, flags);
265 if (cd != MUTT_ICONV_ERROR) {
266 const char **inrepls = NULL;
267 const char *outrepl = NULL;
272 if (charset_is_utf8(to))
273 outrepl = "\357\277\275";
275 if (charset_is_utf8(from))
280 ibl = m_strlen(*ps) + 1;
283 obl = MB_LEN_MAX * ibl;
284 ob = buf = p_new(char, obl + 1);
286 mutt_iconv(cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
299 static ssize_t convert_string(const char *f, ssize_t flen,
300 const char *from, const char *to,
301 char **t, ssize_t * tlen)
309 if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
313 ob = buf = p_new(char, obl);
314 n = my_iconv(cd, &f, &flen, &ob, &obl);
316 if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
331 int mutt_convert_nonmime_string(char **ps)
333 const char *p = AssumedCharset;
334 ssize_t ulen = m_strlen(*ps);
339 char fromcode[LONG_STRING], *s = NULL;
348 q = m_strchrnul(p, ':');
349 m_strncpy(fromcode, sizeof(fromcode), p, q - p);
352 if (convert_string(u, ulen, fromcode, Charset, &s, &slen) >= 0) {
362 /****************************************************************************/
363 /* fgetconv functions */
364 /****************************************************************************/
366 /* fgetconv_t stuff for converting a file while reading it
367 Used in sendlib.c for converting from mutt's Charset */
378 const char **inrepls;
382 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
384 static const char *repls[] = { "\357\277\275", "?", 0 };
386 struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
389 fc->cd = MUTT_ICONV_ERROR;
391 fc->cd = mutt_iconv_open(to, from, flags);
393 if (fc->cd != MUTT_ICONV_ERROR) {
394 fc->p = fc->ob = fc->bufo;
397 fc->inrepls = repls + charset_is_utf8(to);
402 void fgetconv_close(fgetconv_t **fcp)
404 struct fgetconv_t *fc = *fcp;
406 if (fc->cd != MUTT_ICONV_ERROR)
407 iconv_close (fc->cd);
412 int fgetconv(fgetconv_t *fc)
417 if (fc->cd == MUTT_ICONV_ERROR)
418 return fgetc(fc->file);
423 return (unsigned char)*(fc->p)++;
425 /* Try to convert some more */
426 fc->p = fc->ob = fc->bufo;
428 ssize_t obl = ssizeof(fc->bufo);
430 my_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
432 return (unsigned char)*(fc->p)++;
435 /* If we trusted iconv a bit more, we would at this point
436 * ask why it had stopped converting ... */
438 /* Try to read some more */
439 if (fc->ibl == sizeof(fc->bufi)
440 || (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof(fc->bufi))) {
446 memcpy(fc->bufi, fc->ib, fc->ibl);
449 fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
452 /* Try harder this time to convert some */
454 ssize_t obl = ssizeof(fc->bufo);
456 mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
458 if (fc->p < fc->ob) {
459 return (unsigned char)*(fc->p)++;
463 /* Either the file has finished or one of the buffers is too small */
468 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
472 while (pos < len - 1) {
473 int c = fgetconv(fc);
483 return pos ? buf : NULL;