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>
37 # define EILSEQ EINVAL
39 @import "lib-lua/base.cpkg"
41 int Charset_is_utf8 = 0;
42 wchar_t CharsetReplacement = '?';
44 static rx_t *charset_hooks = NULL;
45 static rx_t *iconv_hooks = NULL;
47 static char *charset_init(void)
49 const char *res = "iso-8859-1";
50 #ifdef HAVE_LANGINFO_CODESET
54 m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
55 charset_canonicalize(buff2, sizeof(buff2), buff);
57 /* finally, set $charset */
58 if (!m_strisempty(buff2)) {
62 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
63 bind_textdomain_codeset(PACKAGE, res);
68 static void charset_onchange(const char *cset)
70 Charset_is_utf8 = charset_is_utf8(cset);
71 CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
77 ** This variable is a colon-separated list of character encoding
78 ** schemes for messages without character encoding indication.
79 ** Header field values and message body content without character encoding
80 ** indication would be assumed that they are written in one of this list.
81 ** By default, all the header fields and message body without any charset
82 ** indication are assumed to be in \fTus-ascii\fP.
84 ** For example, Japanese users might prefer this:
86 ** \fTset assumed_charset="iso-2022-jp:euc-jp:shift_jis:utf-8"\fP
88 ** However, only the first content is valid for the message body.
89 ** This variable is valid only if $$strict_mime is unset.
91 string_t assumed_charset = m_strdup("us-ascii");
95 ** Character set your terminal uses to display and enter textual data.
98 .init = charset_init();
99 .onchange = charset_onchange($$);
104 ** This variable is a colon-separated list of character encoding
105 ** schemes for text file attatchments.
106 ** If \fIunset\fP, $$charset value will be used instead.
107 ** For example, the following configuration would work for Japanese
110 ** \fTset file_charset="iso-2022-jp:euc-jp:shift_jis:utf-8"\fP
112 ** Note: ``\fTiso-2022-*\fP'' must be put at the head of the value as shown above
115 string_t file_charset = NULL;
119 ** A list of character sets for outgoing messages. Madmutt will use the
120 ** first character set into which the text can be converted exactly.
121 ** If your ``$$charset'' is not \fTiso-8859-1\fP and recipients may not
122 ** understand \fTUTF-8\fP, it is advisable to include in the list an
123 ** appropriate widely used standard character set (such as
124 ** \fTiso-8859-2\fP, \fTkoi8-r\fP or \fTiso-2022-jp\fP) either
125 ** instead of or after \fTiso-8859-1\fP.
127 string_t send_charset = m_strdup("us-ascii:iso-8859-1:utf-8");
129 void charset_hook(rx_t local, const string_t alias) {
130 rx_set_template(local, alias);
131 rx_list_add2(&charset_hooks, &local);
135 void iconv_hook(rx_t local, const string_t alias) {
136 rx_set_template(local, alias);
137 rx_list_add2(&iconv_hooks, &local);
142 /****************************************************************************/
143 /* charset functions */
144 /****************************************************************************/
146 #include "charset.gperf"
147 void charset_canonicalize(char *dest, ssize_t dlen, const char *name)
149 const struct cset_pair *cp;
150 char scratch[STRING];
155 m_strcpy(dest, dlen, "us-ascii");
159 // canonize name: only keep a-z0-9 and dots, put into lowercase
160 for (p = name; *p && *p != ':' && i < ssizeof(scratch) - 1; p++) {
161 if (isalnum(*p) || *p== '.') {
162 scratch[i++] = tolower((unsigned char)*p);
167 cp = charset_canonicalize_aux(scratch, strlen(scratch));
169 m_strcpy(dest, dlen, cp->pref);
171 m_strcpy(dest, dlen, name);
176 /* XXX: MC: UGLY return of local static */
177 const char *charset_getfirst(const char *charset)
179 static char fcharset[STRING];
182 if (m_strisempty(charset))
185 p = m_strchrnul(charset, ':');
186 m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
190 int charset_is_utf8(const char *s)
193 charset_canonicalize(buf, sizeof(buf), s);
194 return !m_strcmp(buf, "utf-8");
197 int charset_is_us_ascii(const char *s)
200 charset_canonicalize(buf, sizeof(buf), s);
201 return !m_strcmp(buf, "us-ascii");
205 /****************************************************************************/
206 /* iconv-line functions */
207 /****************************************************************************/
209 /* Like iconv_open, but canonicalises the charsets */
210 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
212 char to1[STRING], to2[STRING];
213 char from1[STRING], from2[STRING];
217 if ((flags & M_ICONV_HOOK_TO)
218 && rx_list_match2(charset_hooks, tocode, tmp, sizeof(tmp))) {
219 charset_canonicalize(to1, sizeof(to1), tmp);
221 charset_canonicalize(to1, sizeof(to1), tocode);
224 if ((flags & M_ICONV_HOOK_FROM)
225 && rx_list_match2(charset_hooks, fromcode, tmp, sizeof(tmp))) {
226 charset_canonicalize(from1, sizeof(from1), tmp);
228 charset_canonicalize(from1, sizeof(from1), fromcode);
231 if ((cd = iconv_open(to1, from1)) != MUTT_ICONV_ERROR)
234 if (rx_list_match2(iconv_hooks, to1, to2, sizeof(to2))
235 && rx_list_match2(iconv_hooks, from1, from2, sizeof(from2)))
236 return iconv_open(to2, from2);
238 return MUTT_ICONV_ERROR;
242 /* Like iconv, but keeps going even when the input is invalid
243 If you're supplying inrepls, the source charset should be stateless;
244 if you're supplying an outrepl, the target charset should be. */
245 /* XXX: MC: I do not understand what it does yet */
246 ssize_t mutt_iconv(iconv_t cd,
247 const char **inbuf, ssize_t *inbytesleft,
248 char **outbuf, ssize_t *outbytesleft,
249 const char **inrepls, const char *outrepl)
251 ssize_t ret = 0, ret1;
252 const char *ib = *inbuf;
253 ssize_t ibl = *inbytesleft;
255 ssize_t obl = *outbytesleft;
258 ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
262 if (ibl && obl && errno == EILSEQ) {
264 /* Try replacing the input */
267 for (t = inrepls; *t; t++) {
268 const char *ib1 = *t;
269 ssize_t ibl1 = m_strlen(*t);
273 my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
276 ob = ob1, obl = obl1;
284 /* Replace the output */
287 my_iconv(cd, 0, 0, &ob, &obl);
289 ssize_t n = m_strlen(outrepl);
295 memcpy(ob, outrepl, n);
299 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
303 *inbuf = ib, *inbytesleft = ibl;
304 *outbuf = ob, *outbytesleft = obl;
309 /* Convert a string */
311 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
314 const char *repls[] = { "\357\277\275", "?", 0 };
316 if (m_strisempty(*ps))
319 cd = mutt_iconv_open(to, from, flags);
320 if (cd != MUTT_ICONV_ERROR) {
321 const char **inrepls = NULL;
322 const char *outrepl = NULL;
327 if (charset_is_utf8(to))
328 outrepl = "\357\277\275";
330 if (charset_is_utf8(from))
335 ibl = m_strlen(*ps) + 1;
338 obl = MB_LEN_MAX * ibl;
339 ob = buf = p_new(char, obl + 1);
341 mutt_iconv(cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
354 static ssize_t convert_string(const char *f, ssize_t flen,
355 const char *from, const char *to,
356 char **t, ssize_t * tlen)
364 if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
368 ob = buf = p_new(char, obl);
369 n = my_iconv(cd, &f, &flen, &ob, &obl);
371 if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
386 int mutt_convert_nonmime_string(char **ps)
388 const char *p = MCharset.assumed_charset;
389 ssize_t ulen = m_strlen(*ps);
394 char fromcode[LONG_STRING], *s = NULL;
403 q = m_strchrnul(p, ':');
404 m_strncpy(fromcode, sizeof(fromcode), p, q - p);
407 if (convert_string(u, ulen, fromcode, MCharset.charset, &s, &slen) >= 0) {
417 /****************************************************************************/
418 /* fgetconv functions */
419 /****************************************************************************/
421 /* fgetconv_t stuff for converting a file while reading it
422 Used in sendlib.c for converting from mutt's charset */
433 const char **inrepls;
437 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
439 static const char *repls[] = { "\357\277\275", "?", 0 };
441 struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
444 fc->cd = MUTT_ICONV_ERROR;
446 fc->cd = mutt_iconv_open(to, from, flags);
448 if (fc->cd != MUTT_ICONV_ERROR) {
449 fc->p = fc->ob = fc->bufo;
452 fc->inrepls = repls + charset_is_utf8(to);
457 void fgetconv_close(fgetconv_t **fcp)
459 struct fgetconv_t *fc = *fcp;
461 if (fc->cd != MUTT_ICONV_ERROR)
462 iconv_close (fc->cd);
467 int fgetconv(fgetconv_t *fc)
472 if (fc->cd == MUTT_ICONV_ERROR)
473 return fgetc(fc->file);
478 return (unsigned char)*(fc->p)++;
480 /* Try to convert some more */
481 fc->p = fc->ob = fc->bufo;
483 ssize_t obl = ssizeof(fc->bufo);
485 my_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
487 return (unsigned char)*(fc->p)++;
490 /* If we trusted iconv a bit more, we would at this point
491 * ask why it had stopped converting ... */
493 /* Try to read some more */
494 if (fc->ibl == sizeof(fc->bufi)
495 || (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof(fc->bufi))) {
501 memcpy(fc->bufi, fc->ib, fc->ibl);
504 fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
507 /* Try harder this time to convert some */
509 ssize_t obl = ssizeof(fc->bufo);
511 mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
513 if (fc->p < fc->ob) {
514 return (unsigned char)*(fc->p)++;
518 /* Either the file has finished or one of the buffers is too small */
523 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
527 while (pos < len - 1) {
528 int c = fgetconv(fc);
538 return pos ? buf : NULL;