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.
28 #include <lib-lib/lib-lib.h>
30 #ifdef HAVE_LANGINFO_CODESET
31 # include <langinfo.h>
38 # define EILSEQ EINVAL
42 int Charset_is_utf8 = 0;
43 wchar_t CharsetReplacement = '?';
46 /****************************************************************************/
47 /* charset functions */
48 /****************************************************************************/
50 void charset_initialize(void)
52 #ifdef HAVE_LANGINFO_CODESET
53 char buff[SHORT_STRING];
54 char buff2[SHORT_STRING];
56 m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
57 charset_canonicalize(buff2, sizeof(buff2), buff);
59 /* finally, set $charset */
60 if (!m_strisempty(buff2)) {
61 m_strreplace(&Charset, buff2);
65 m_strreplace(&Charset, "iso-8859-1");
68 Charset_is_utf8 = !m_strcmp(Charset, "utf-8");
69 CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
71 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
72 bind_textdomain_codeset(PACKAGE, Charset);
76 #include "charset.gperf"
77 void charset_canonicalize(char *dest, ssize_t dlen, const char *name)
79 const struct cset_pair *cp;
80 char scratch[SHORT_STRING];
85 m_strcpy(dest, dlen, "us-ascii");
89 // canonize name: only keep a-z0-9 and dots, put into lowercase
90 for (p = name; *p && *p != ':' && i < ssizeof(scratch) - 1; p++) {
91 if (isalnum(*p) || *p== '.') {
92 scratch[i++] = tolower((unsigned char)*p);
97 cp = charset_canonicalize_aux(scratch, strlen(scratch));
99 m_strcpy(dest, dlen, cp->pref);
101 m_strcpy(dest, dlen, name);
106 /* XXX: MC: UGLY return of local static */
107 const char *charset_getfirst(const char *charset)
109 static char fcharset[SHORT_STRING];
112 if (m_strisempty(charset))
115 p = m_strchrnul(charset, ':');
116 m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
120 int charset_is_utf8(const char *s)
122 char buf[SHORT_STRING];
123 charset_canonicalize(buf, sizeof(buf), s);
124 return !m_strcmp(buf, "utf-8");
127 int charset_is_us_ascii(const char *s)
129 char buf[SHORT_STRING];
130 charset_canonicalize(buf, sizeof(buf), s);
131 return !m_strcmp(buf, "us-ascii");
135 /****************************************************************************/
136 /* iconv-line functions */
137 /****************************************************************************/
139 /* Like iconv_open, but canonicalises the charsets */
140 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
142 char tocode1[SHORT_STRING];
143 char fromcode1[SHORT_STRING];
148 if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook(tocode1))) {
149 charset_canonicalize(tocode1, sizeof(tocode1), tmp);
151 charset_canonicalize(tocode1, sizeof(tocode1), tocode);
154 if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook(fromcode1))) {
155 charset_canonicalize(fromcode1, sizeof(fromcode1), tmp);
157 charset_canonicalize(fromcode1, sizeof(fromcode1), fromcode);
160 cd = iconv_open(tocode1, fromcode1);
161 if (cd != MUTT_ICONV_ERROR)
165 const char *to = mutt_iconv_hook(tocode1);
166 const char *from = mutt_iconv_hook(fromcode1);
168 return to && from ? iconv_open(to, from) : MUTT_ICONV_ERROR;
173 /* Like iconv, but keeps going even when the input is invalid
174 If you're supplying inrepls, the source charset should be stateless;
175 if you're supplying an outrepl, the target charset should be. */
176 /* XXX: MC: I do not understand what it does yet */
177 ssize_t mutt_iconv(iconv_t cd,
178 const char **inbuf, ssize_t *inbytesleft,
179 char **outbuf, ssize_t *outbytesleft,
180 const char **inrepls, const char *outrepl)
182 ssize_t ret = 0, ret1;
183 const char *ib = *inbuf;
184 ssize_t ibl = *inbytesleft;
186 ssize_t obl = *outbytesleft;
189 ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
193 if (ibl && obl && errno == EILSEQ) {
195 /* Try replacing the input */
198 for (t = inrepls; *t; t++) {
199 const char *ib1 = *t;
200 ssize_t ibl1 = m_strlen(*t);
204 my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
207 ob = ob1, obl = obl1;
215 /* Replace the output */
218 my_iconv(cd, 0, 0, &ob, &obl);
220 ssize_t n = m_strlen(outrepl);
226 memcpy(ob, outrepl, n);
230 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
234 *inbuf = ib, *inbytesleft = ibl;
235 *outbuf = ob, *outbytesleft = obl;
240 /* Convert a string */
242 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
245 const char *repls[] = { "\357\277\275", "?", 0 };
247 if (m_strisempty(*ps))
250 cd = mutt_iconv_open(to, from, flags);
251 if (cd != MUTT_ICONV_ERROR) {
252 const char **inrepls = NULL;
253 const char *outrepl = NULL;
258 if (charset_is_utf8(to))
259 outrepl = "\357\277\275";
261 if (charset_is_utf8(from))
266 ibl = m_strlen(*ps) + 1;
269 obl = MB_LEN_MAX * ibl;
270 ob = buf = p_new(char, obl + 1);
272 mutt_iconv(cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
285 static ssize_t convert_string(const char *f, ssize_t flen,
286 const char *from, const char *to,
287 char **t, ssize_t * tlen)
295 if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
299 ob = buf = p_new(char, obl);
300 n = my_iconv(cd, &f, &flen, &ob, &obl);
302 if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
317 int mutt_convert_nonmime_string(char **ps)
319 const char *p = AssumedCharset;
320 ssize_t ulen = m_strlen(*ps);
325 char fromcode[LONG_STRING], *s = NULL;
334 q = m_strchrnul(p, ':');
335 m_strncpy(fromcode, sizeof(fromcode), p, q - p);
338 if (convert_string(u, ulen, fromcode, Charset, &s, &slen) >= 0) {
348 /****************************************************************************/
349 /* fgetconv functions */
350 /****************************************************************************/
352 /* fgetconv_t stuff for converting a file while reading it
353 Used in sendlib.c for converting from mutt's Charset */
364 const char **inrepls;
368 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
370 static const char *repls[] = { "\357\277\275", "?", 0 };
372 struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
375 fc->cd = MUTT_ICONV_ERROR;
377 fc->cd = mutt_iconv_open(to, from, flags);
379 if (fc->cd != MUTT_ICONV_ERROR) {
380 fc->p = fc->ob = fc->bufo;
383 fc->inrepls = repls + charset_is_utf8(to);
388 void fgetconv_close(fgetconv_t **fcp)
390 struct fgetconv_t *fc = *fcp;
392 if (fc->cd != MUTT_ICONV_ERROR)
393 iconv_close (fc->cd);
398 int fgetconv(fgetconv_t *fc)
403 if (fc->cd == MUTT_ICONV_ERROR)
404 return fgetc(fc->file);
409 return (unsigned char)*(fc->p)++;
411 /* Try to convert some more */
412 fc->p = fc->ob = fc->bufo;
414 ssize_t obl = ssizeof(fc->bufo);
416 my_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
418 return (unsigned char)*(fc->p)++;
421 /* If we trusted iconv a bit more, we would at this point
422 * ask why it had stopped converting ... */
424 /* Try to read some more */
425 if (fc->ibl == sizeof(fc->bufi)
426 || (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof(fc->bufi))) {
432 memcpy(fc->bufi, fc->ib, fc->ibl);
435 fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
438 /* Try harder this time to convert some */
440 ssize_t obl = ssizeof(fc->bufo);
442 mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
444 if (fc->p < fc->ob) {
445 return (unsigned char)*(fc->p)++;
449 /* Either the file has finished or one of the buffers is too small */
454 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
458 while (pos < len - 1) {
459 int c = fgetconv(fc);
469 return pos ? buf : NULL;