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 #include <sys/types.h>
31 #ifdef HAVE_LANGINFO_CODESET
32 # include <langinfo.h>
39 # define EILSEQ EINVAL
43 int Charset_is_utf8 = 0;
44 wchar_t CharsetReplacement = '?';
47 /****************************************************************************/
48 /* charset functions */
49 /****************************************************************************/
51 void charset_initialize(void)
53 #ifdef HAVE_LANGINFO_CODESET
54 char buff[SHORT_STRING];
55 char buff2[SHORT_STRING];
57 m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
58 charset_canonicalize(buff2, sizeof(buff2), buff);
60 /* finally, set $charset */
61 if (!m_strisempty(buff2)) {
62 m_strreplace(&Charset, buff2);
66 m_strreplace(&Charset, "iso-8859-1");
69 Charset_is_utf8 = !strcmp(Charset, "utf-8");
70 CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
72 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
73 bind_textdomain_codeset(PACKAGE, Charset);
77 #include "charset.gperf"
78 void charset_canonicalize(char *dest, ssize_t dlen, const char *name)
80 const struct cset_pair *cp;
81 char scratch[SHORT_STRING];
86 m_strcpy(dest, dlen, "us-ascii");
90 // canonize name: only keep a-z0-9 and dots, put into lowercase
91 for (p = name; *p && *p != ':' && i < ssizeof(scratch) - 1; p++) {
92 if (isalnum(*p) || *p== '.') {
93 scratch[i++] = tolower((unsigned char)*p);
98 cp = charset_canonicalize_aux(scratch, strlen(scratch));
100 m_strcpy(dest, dlen, cp->pref);
102 m_strcpy(dest, dlen, name);
107 /* XXX: MC: UGLY return of local static */
108 const char *charset_getfirst(const char *charset)
110 static char fcharset[SHORT_STRING];
113 if (m_strisempty(charset))
116 p = m_strchrnul(charset, ':');
117 m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
121 int charset_is_utf8(const char *s)
123 char buf[SHORT_STRING];
124 charset_canonicalize(buf, sizeof(buf), s);
125 return !strcmp(buf, "utf-8");
128 int charset_is_us_ascii(const char *s)
130 char buf[SHORT_STRING];
131 charset_canonicalize(buf, sizeof(buf), s);
132 return !strcmp(buf, "us-ascii");
136 /****************************************************************************/
137 /* iconv-line functions */
138 /****************************************************************************/
140 /* Like iconv_open, but canonicalises the charsets */
141 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
143 char tocode1[SHORT_STRING];
144 char fromcode1[SHORT_STRING];
149 if ((flags & M_ICONV_HOOK_TO) && (tmp = mutt_charset_hook(tocode1))) {
150 charset_canonicalize(tocode1, sizeof(tocode1), tmp);
152 charset_canonicalize(tocode1, sizeof(tocode1), tocode);
155 if ((flags & M_ICONV_HOOK_FROM) && (tmp = mutt_charset_hook(fromcode1))) {
156 charset_canonicalize(fromcode1, sizeof(fromcode1), tmp);
158 charset_canonicalize(fromcode1, sizeof(fromcode1), fromcode);
161 cd = iconv_open(tocode1, fromcode1);
162 if (cd != MUTT_ICONV_ERROR)
166 const char *to = mutt_iconv_hook(tocode1);
167 const char *from = mutt_iconv_hook(fromcode1);
169 return to && from ? iconv_open(to, from) : MUTT_ICONV_ERROR;
174 /* Like iconv, but keeps going even when the input is invalid
175 If you're supplying inrepls, the source charset should be stateless;
176 if you're supplying an outrepl, the target charset should be. */
177 /* XXX: MC: I do not understand what it does yet */
178 ssize_t mutt_iconv(iconv_t cd,
179 const char **inbuf, ssize_t *inbytesleft,
180 char **outbuf, ssize_t *outbytesleft,
181 const char **inrepls, const char *outrepl)
183 ssize_t ret = 0, ret1;
184 const char *ib = *inbuf;
185 ssize_t ibl = *inbytesleft;
187 ssize_t obl = *outbytesleft;
190 ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
194 if (ibl && obl && errno == EILSEQ) {
196 /* Try replacing the input */
199 for (t = inrepls; *t; t++) {
200 const char *ib1 = *t;
201 ssize_t ibl1 = m_strlen(*t);
205 my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
208 ob = ob1, obl = obl1;
216 /* Replace the output */
219 my_iconv(cd, 0, 0, &ob, &obl);
221 ssize_t n = m_strlen(outrepl);
227 memcpy(ob, outrepl, n);
231 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
235 *inbuf = ib, *inbytesleft = ibl;
236 *outbuf = ob, *outbytesleft = obl;
241 /* Convert a string */
243 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
246 const char *repls[] = { "\357\277\275", "?", 0 };
248 if (m_strisempty(*ps))
251 cd = mutt_iconv_open(to, from, flags);
252 if (cd != MUTT_ICONV_ERROR) {
253 const char **inrepls = NULL;
254 const char *outrepl = NULL;
259 if (charset_is_utf8(to))
260 outrepl = "\357\277\275";
262 if (charset_is_utf8(from))
267 ibl = m_strlen(*ps) + 1;
270 obl = MB_LEN_MAX * ibl;
271 ob = buf = p_new(char, obl + 1);
273 mutt_iconv(cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
286 static ssize_t convert_string(const char *f, ssize_t flen,
287 const char *from, const char *to,
288 char **t, ssize_t * tlen)
296 if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
300 ob = buf = p_new(char, obl);
301 n = my_iconv(cd, &f, &flen, &ob, &obl);
303 if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
318 int mutt_convert_nonmime_string(char **ps)
320 const char *p = AssumedCharset;
321 ssize_t ulen = m_strlen(*ps);
326 char fromcode[LONG_STRING], *s = NULL;
335 q = m_strchrnul(p, ':');
336 m_strncpy(fromcode, sizeof(fromcode), p, q - p);
339 if (convert_string(u, ulen, fromcode, Charset, &s, &slen) >= 0) {
349 /****************************************************************************/
350 /* fgetconv functions */
351 /****************************************************************************/
353 /* fgetconv_t stuff for converting a file while reading it
354 Used in sendlib.c for converting from mutt's Charset */
365 const char **inrepls;
369 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
371 static const char *repls[] = { "\357\277\275", "?", 0 };
373 struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
376 fc->cd = MUTT_ICONV_ERROR;
378 fc->cd = mutt_iconv_open(to, from, flags);
380 if (fc->cd != MUTT_ICONV_ERROR) {
381 fc->p = fc->ob = fc->bufo;
384 fc->inrepls = repls + charset_is_utf8(to);
389 void fgetconv_close(fgetconv_t **fcp)
391 struct fgetconv_t *fc = *fcp;
393 if (fc->cd != MUTT_ICONV_ERROR)
394 iconv_close (fc->cd);
399 int fgetconv(fgetconv_t *fc)
404 if (fc->cd == MUTT_ICONV_ERROR)
405 return fgetc(fc->file);
410 return (unsigned char)*(fc->p)++;
412 /* Try to convert some more */
413 fc->p = fc->ob = fc->bufo;
415 ssize_t obl = ssizeof(fc->bufo);
417 my_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
419 return (unsigned char)*(fc->p)++;
422 /* If we trusted iconv a bit more, we would at this point
423 * ask why it had stopped converting ... */
425 /* Try to read some more */
426 if (fc->ibl == sizeof(fc->bufi)
427 || (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof(fc->bufi))) {
433 memcpy(fc->bufi, fc->ib, fc->ibl);
436 fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
439 /* Try harder this time to convert some */
441 ssize_t obl = ssizeof(fc->bufo);
443 mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
445 if (fc->p < fc->ob) {
446 return (unsigned char)*(fc->p)++;
450 /* Either the file has finished or one of the buffers is too small */
455 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
459 while (pos < len - 1) {
460 int c = fgetconv(fc);
470 return pos ? buf : NULL;