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 static rx_t *charset_hooks = NULL;
42 static rx_t *iconv_hooks = NULL;
47 ** This variable is a colon-separated list of character encoding
48 ** schemes for messages without character encoding indication.
49 ** Header field values and message body content without character encoding
50 ** indication would be assumed that they are written in one of this list.
51 ** By default, all the header fields and message body without any charset
52 ** indication are assumed to be in \fTus-ascii\fP.
54 ** For example, Japanese users might prefer this:
56 ** \fTset assumed_charset="iso-2022-jp:euc-jp:shift_jis:utf-8"\fP
58 ** However, only the first content is valid for the message body.
59 ** This variable is valid only if $$strict_mime is unset.
61 string_t assumed_charset = m_strdup("us-ascii");
65 ** Character set your terminal uses to display and enter textual data.
67 string_t charset = NULL;
71 ** This variable is a colon-separated list of character encoding
72 ** schemes for text file attatchments.
73 ** If \fIunset\fP, $$charset value will be used instead.
74 ** For example, the following configuration would work for Japanese
77 ** \fTset file_charset="iso-2022-jp:euc-jp:shift_jis:utf-8"\fP
79 ** Note: ``\fTiso-2022-*\fP'' must be put at the head of the value as shown above
82 string_t file_charset = NULL;
86 ** A list of character sets for outgoing messages. Madmutt will use the
87 ** first character set into which the text can be converted exactly.
88 ** If your ``$$charset'' is not \fTiso-8859-1\fP and recipients may not
89 ** understand \fTUTF-8\fP, it is advisable to include in the list an
90 ** appropriate widely used standard character set (such as
91 ** \fTiso-8859-2\fP, \fTkoi8-r\fP or \fTiso-2022-jp\fP) either
92 ** instead of or after \fTiso-8859-1\fP.
94 string_t send_charset = m_strdup("us-ascii:iso-8859-1:utf-8");
96 void charset_hook(rx_t local, const string_t alias) {
97 rx_set_template(local, alias);
98 rx_list_append(&charset_hooks, local);
102 void iconv_hook(rx_t local, const string_t alias) {
103 rx_set_template(local, alias);
104 rx_list_append(&iconv_hooks, local);
109 int Charset_is_utf8 = 0;
110 wchar_t CharsetReplacement = '?';
113 /****************************************************************************/
114 /* charset functions */
115 /****************************************************************************/
117 void charset_initialize(void)
119 #ifdef HAVE_LANGINFO_CODESET
123 m_strcpy(buff, sizeof(buff), nl_langinfo(CODESET));
124 charset_canonicalize(buff2, sizeof(buff2), buff);
126 /* finally, set $charset */
127 if (!m_strisempty(buff2)) {
128 m_strreplace(&MCharset.charset, buff2);
132 m_strreplace(&MCharset.charset, "iso-8859-1");
135 Charset_is_utf8 = !m_strcmp(MCharset.charset, "utf-8");
136 CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
138 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
139 bind_textdomain_codeset(PACKAGE, MCharset.charset);
143 #include "charset.gperf"
144 void charset_canonicalize(char *dest, ssize_t dlen, const char *name)
146 const struct cset_pair *cp;
147 char scratch[STRING];
152 m_strcpy(dest, dlen, "us-ascii");
156 // canonize name: only keep a-z0-9 and dots, put into lowercase
157 for (p = name; *p && *p != ':' && i < ssizeof(scratch) - 1; p++) {
158 if (isalnum(*p) || *p== '.') {
159 scratch[i++] = tolower((unsigned char)*p);
164 cp = charset_canonicalize_aux(scratch, strlen(scratch));
166 m_strcpy(dest, dlen, cp->pref);
168 m_strcpy(dest, dlen, name);
173 /* XXX: MC: UGLY return of local static */
174 const char *charset_getfirst(const char *charset)
176 static char fcharset[STRING];
179 if (m_strisempty(charset))
182 p = m_strchrnul(charset, ':');
183 m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
187 int charset_is_utf8(const char *s)
190 charset_canonicalize(buf, sizeof(buf), s);
191 return !m_strcmp(buf, "utf-8");
194 int charset_is_us_ascii(const char *s)
197 charset_canonicalize(buf, sizeof(buf), s);
198 return !m_strcmp(buf, "us-ascii");
202 /****************************************************************************/
203 /* iconv-line functions */
204 /****************************************************************************/
206 /* Like iconv_open, but canonicalises the charsets */
207 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
214 if ((flags & M_ICONV_HOOK_TO)
215 && rx_list_match2(charset_hooks, tocode, tmp, sizeof(tmp))) {
216 charset_canonicalize(to1, sizeof(to1), tmp);
218 charset_canonicalize(to1, sizeof(to1), tocode);
221 if ((flags & M_ICONV_HOOK_FROM)
222 && rx_list_match2(charset_hooks, fromcode, tmp, sizeof(tmp))) {
223 charset_canonicalize(from1, sizeof(from1), tmp);
225 charset_canonicalize(from1, sizeof(from1), fromcode);
228 if ((cd = iconv_open(to1, from1)) != MUTT_ICONV_ERROR)
235 if (rx_list_match2(iconv_hooks, to1, to2, sizeof(to2))
236 && rx_list_match2(iconv_hooks, from1, from2, sizeof(from2)))
237 return iconv_open(to2, from2);
239 return MUTT_ICONV_ERROR;
243 /* Like iconv, but keeps going even when the input is invalid
244 If you're supplying inrepls, the source charset should be stateless;
245 if you're supplying an outrepl, the target charset should be. */
246 /* XXX: MC: I do not understand what it does yet */
247 ssize_t mutt_iconv(iconv_t cd,
248 const char **inbuf, ssize_t *inbytesleft,
249 char **outbuf, ssize_t *outbytesleft,
250 const char **inrepls, const char *outrepl)
252 ssize_t ret = 0, ret1;
253 const char *ib = *inbuf;
254 ssize_t ibl = *inbytesleft;
256 ssize_t obl = *outbytesleft;
259 ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
263 if (ibl && obl && errno == EILSEQ) {
265 /* Try replacing the input */
268 for (t = inrepls; *t; t++) {
269 const char *ib1 = *t;
270 ssize_t ibl1 = m_strlen(*t);
274 my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
277 ob = ob1, obl = obl1;
285 /* Replace the output */
288 my_iconv(cd, 0, 0, &ob, &obl);
290 ssize_t n = m_strlen(outrepl);
296 memcpy(ob, outrepl, n);
300 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
304 *inbuf = ib, *inbytesleft = ibl;
305 *outbuf = ob, *outbytesleft = obl;
310 /* Convert a string */
312 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
315 const char *repls[] = { "\357\277\275", "?", 0 };
317 if (m_strisempty(*ps))
320 cd = mutt_iconv_open(to, from, flags);
321 if (cd != MUTT_ICONV_ERROR) {
322 const char **inrepls = NULL;
323 const char *outrepl = NULL;
328 if (charset_is_utf8(to))
329 outrepl = "\357\277\275";
331 if (charset_is_utf8(from))
336 ibl = m_strlen(*ps) + 1;
339 obl = MB_LEN_MAX * ibl;
340 ob = buf = p_new(char, obl + 1);
342 mutt_iconv(cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
355 static ssize_t convert_string(const char *f, ssize_t flen,
356 const char *from, const char *to,
357 char **t, ssize_t * tlen)
365 if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
369 ob = buf = p_new(char, obl);
370 n = my_iconv(cd, &f, &flen, &ob, &obl);
372 if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
387 int mutt_convert_nonmime_string(char **ps)
389 const char *p = MCharset.assumed_charset;
390 ssize_t ulen = m_strlen(*ps);
395 char fromcode[LONG_STRING], *s = NULL;
404 q = m_strchrnul(p, ':');
405 m_strncpy(fromcode, sizeof(fromcode), p, q - p);
408 if (convert_string(u, ulen, fromcode, MCharset.charset, &s, &slen) >= 0) {
418 /****************************************************************************/
419 /* fgetconv functions */
420 /****************************************************************************/
422 /* fgetconv_t stuff for converting a file while reading it
423 Used in sendlib.c for converting from mutt's charset */
434 const char **inrepls;
438 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
440 static const char *repls[] = { "\357\277\275", "?", 0 };
442 struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
445 fc->cd = MUTT_ICONV_ERROR;
447 fc->cd = mutt_iconv_open(to, from, flags);
449 if (fc->cd != MUTT_ICONV_ERROR) {
450 fc->p = fc->ob = fc->bufo;
453 fc->inrepls = repls + charset_is_utf8(to);
458 void fgetconv_close(fgetconv_t **fcp)
460 struct fgetconv_t *fc = *fcp;
462 if (fc->cd != MUTT_ICONV_ERROR)
463 iconv_close (fc->cd);
468 int fgetconv(fgetconv_t *fc)
473 if (fc->cd == MUTT_ICONV_ERROR)
474 return fgetc(fc->file);
479 return (unsigned char)*(fc->p)++;
481 /* Try to convert some more */
482 fc->p = fc->ob = fc->bufo;
484 ssize_t obl = ssizeof(fc->bufo);
486 my_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
488 return (unsigned char)*(fc->p)++;
491 /* If we trusted iconv a bit more, we would at this point
492 * ask why it had stopped converting ... */
494 /* Try to read some more */
495 if (fc->ibl == sizeof(fc->bufi)
496 || (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof(fc->bufi))) {
502 memcpy(fc->bufi, fc->ib, fc->ibl);
505 fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
508 /* Try harder this time to convert some */
510 ssize_t obl = ssizeof(fc->bufo);
512 mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
514 if (fc->p < fc->ob) {
515 return (unsigned char)*(fc->p)++;
519 /* Either the file has finished or one of the buffers is too small */
524 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
528 while (pos < len - 1) {
529 int c = fgetconv(fc);
539 return pos ? buf : NULL;