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_H
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_H
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 bind_textdomain_codeset(PACKAGE, res);
66 static void charset_onchange(const char *cset)
68 Charset_is_utf8 = charset_is_utf8(cset);
69 CharsetReplacement = Charset_is_utf8 ? 0xfffd : '?';
75 ** This variable is a colon-separated list of character encoding
76 ** schemes for messages without character encoding indication.
77 ** Header field values and message body content without character encoding
78 ** indication would be assumed that they are written in one of this list.
79 ** By default, all the header fields and message body without any charset
80 ** indication are assumed to be in \fTus-ascii\fP.
82 ** For example, Japanese users might prefer this:
84 ** \fTset assumed_charset="iso-2022-jp:euc-jp:shift_jis:utf-8"\fP
86 ** However, only the first content is valid for the message body.
87 ** This variable is valid only if $$strict_mime is unset.
89 string_t assumed_charset = m_strdup("us-ascii");
93 ** Character set your terminal uses to display and enter textual data.
96 .init = charset_init();
97 .onchange = charset_onchange($$);
102 ** This variable is a colon-separated list of character encoding
103 ** schemes for text file attatchments.
104 ** If \fIunset\fP, $$charset value will be used instead.
105 ** For example, the following configuration would work for Japanese
108 ** \fTset file_charset="iso-2022-jp:euc-jp:shift_jis:utf-8"\fP
110 ** Note: ``\fTiso-2022-*\fP'' must be put at the head of the value as shown above
113 string_t file_charset = NULL;
117 ** A list of character sets for outgoing messages. Madmutt will use the
118 ** first character set into which the text can be converted exactly.
119 ** If your ``$$charset'' is not \fTiso-8859-1\fP and recipients may not
120 ** understand \fTUTF-8\fP, it is advisable to include in the list an
121 ** appropriate widely used standard character set (such as
122 ** \fTiso-8859-2\fP, \fTkoi8-r\fP or \fTiso-2022-jp\fP) either
123 ** instead of or after \fTiso-8859-1\fP.
125 string_t send_charset = m_strdup("us-ascii:iso-8859-1:utf-8");
127 void charset_hook(rx_t local, const string_t alias) {
128 rx_set_template(local, alias);
129 rx_list_add2(&charset_hooks, &local);
133 void iconv_hook(rx_t local, const string_t alias) {
134 rx_set_template(local, alias);
135 rx_list_add2(&iconv_hooks, &local);
140 /****************************************************************************/
141 /* charset functions */
142 /****************************************************************************/
144 #include "charset.gperf"
145 void charset_canonicalize(char *dest, ssize_t dlen, const char *name)
147 const struct cset_pair *cp;
148 char scratch[STRING];
153 m_strcpy(dest, dlen, "us-ascii");
157 // canonize name: only keep a-z0-9 and dots, put into lowercase
158 for (p = name; *p && *p != ':' && i < ssizeof(scratch) - 1; p++) {
159 if (isalnum(*p) || *p== '.') {
160 scratch[i++] = tolower((unsigned char)*p);
165 cp = charset_canonicalize_aux(scratch, strlen(scratch));
167 m_strcpy(dest, dlen, cp->pref);
169 m_strcpy(dest, dlen, name);
174 /* XXX: MC: UGLY return of local static */
175 const char *charset_getfirst(const char *charset)
177 static char fcharset[STRING];
180 if (m_strisempty(charset))
183 p = m_strchrnul(charset, ':');
184 m_strncpy(fcharset, sizeof(fcharset), charset, p - charset);
188 int charset_is_utf8(const char *s)
191 charset_canonicalize(buf, sizeof(buf), s);
192 return !m_strcmp(buf, "utf-8");
195 int charset_is_us_ascii(const char *s)
198 charset_canonicalize(buf, sizeof(buf), s);
199 return !m_strcmp(buf, "us-ascii");
203 /****************************************************************************/
204 /* iconv-line functions */
205 /****************************************************************************/
207 /* Like iconv_open, but canonicalises the charsets */
208 iconv_t mutt_iconv_open(const char *tocode, const char *fromcode, int flags)
210 char to1[STRING], to2[STRING];
211 char from1[STRING], from2[STRING];
215 if ((flags & M_ICONV_HOOK_TO)
216 && rx_list_match2(charset_hooks, tocode, tmp, sizeof(tmp))) {
217 charset_canonicalize(to1, sizeof(to1), tmp);
219 charset_canonicalize(to1, sizeof(to1), tocode);
222 if ((flags & M_ICONV_HOOK_FROM)
223 && rx_list_match2(charset_hooks, fromcode, tmp, sizeof(tmp))) {
224 charset_canonicalize(from1, sizeof(from1), tmp);
226 charset_canonicalize(from1, sizeof(from1), fromcode);
229 if ((cd = iconv_open(to1, from1)) != MUTT_ICONV_ERROR)
232 if (rx_list_match2(iconv_hooks, to1, to2, sizeof(to2))
233 && rx_list_match2(iconv_hooks, from1, from2, sizeof(from2)))
234 return iconv_open(to2, from2);
236 return MUTT_ICONV_ERROR;
240 /* Like iconv, but keeps going even when the input is invalid
241 If you're supplying inrepls, the source charset should be stateless;
242 if you're supplying an outrepl, the target charset should be. */
243 /* XXX: MC: I do not understand what it does yet */
244 ssize_t mutt_iconv(iconv_t cd,
245 const char **inbuf, ssize_t *inbytesleft,
246 char **outbuf, ssize_t *outbytesleft,
247 const char **inrepls, const char *outrepl)
249 ssize_t ret = 0, ret1;
250 const char *ib = *inbuf;
251 ssize_t ibl = *inbytesleft;
253 ssize_t obl = *outbytesleft;
256 ret1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
260 if (ibl && obl && errno == EILSEQ) {
262 /* Try replacing the input */
265 for (t = inrepls; *t; t++) {
266 const char *ib1 = *t;
267 ssize_t ibl1 = m_strlen(*t);
271 my_iconv(cd, &ib1, &ibl1, &ob1, &obl1);
274 ob = ob1, obl = obl1;
282 /* Replace the output */
285 my_iconv(cd, 0, 0, &ob, &obl);
287 ssize_t n = m_strlen(outrepl);
293 memcpy(ob, outrepl, n);
297 my_iconv(cd, 0, 0, 0, 0); /* for good measure */
301 *inbuf = ib, *inbytesleft = ibl;
302 *outbuf = ob, *outbytesleft = obl;
307 /* Convert a string */
309 mutt_convert_string(char **ps, const char *from, const char *to, int flags)
312 const char *repls[] = { "\357\277\275", "?", 0 };
314 if (m_strisempty(*ps))
317 cd = mutt_iconv_open(to, from, flags);
318 if (cd != MUTT_ICONV_ERROR) {
319 const char **inrepls = NULL;
320 const char *outrepl = NULL;
325 if (charset_is_utf8(to))
326 outrepl = "\357\277\275";
328 if (charset_is_utf8(from))
333 ibl = m_strlen(*ps) + 1;
336 obl = MB_LEN_MAX * ibl;
337 ob = buf = p_new(char, obl + 1);
339 mutt_iconv(cd, &ib, &ibl, &ob, &obl, inrepls, outrepl);
352 static ssize_t convert_string(const char *f, ssize_t flen,
353 const char *from, const char *to,
354 char **t, ssize_t * tlen)
362 if ((cd = mutt_iconv_open(to, from, 0)) == MUTT_ICONV_ERROR)
366 ob = buf = p_new(char, obl);
367 n = my_iconv(cd, &f, &flen, &ob, &obl);
369 if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
384 int mutt_convert_nonmime_string(char **ps)
386 const char *p = mod_cset.assumed_charset;
387 ssize_t ulen = m_strlen(*ps);
392 char fromcode[LONG_STRING], *s = NULL;
401 q = m_strchrnul(p, ':');
402 m_strncpy(fromcode, sizeof(fromcode), p, q - p);
405 if (convert_string(u, ulen, fromcode, mod_cset.charset, &s, &slen) >= 0) {
415 /****************************************************************************/
416 /* fgetconv functions */
417 /****************************************************************************/
419 /* fgetconv_t stuff for converting a file while reading it
420 Used in sendlib.c for converting from mutt's charset */
431 const char **inrepls;
435 fgetconv_open(FILE *file, const char *from, const char *to, int flags)
437 static const char *repls[] = { "\357\277\275", "?", 0 };
439 struct fgetconv_t *fc = p_new(struct fgetconv_t, 1);
442 fc->cd = MUTT_ICONV_ERROR;
444 fc->cd = mutt_iconv_open(to, from, flags);
446 if (fc->cd != MUTT_ICONV_ERROR) {
447 fc->p = fc->ob = fc->bufo;
450 fc->inrepls = repls + charset_is_utf8(to);
455 void fgetconv_close(fgetconv_t **fcp)
457 struct fgetconv_t *fc = *fcp;
459 if (fc->cd != MUTT_ICONV_ERROR)
460 iconv_close (fc->cd);
465 int fgetconv(fgetconv_t *fc)
470 if (fc->cd == MUTT_ICONV_ERROR)
471 return fgetc(fc->file);
476 return (unsigned char)*(fc->p)++;
478 /* Try to convert some more */
479 fc->p = fc->ob = fc->bufo;
481 ssize_t obl = ssizeof(fc->bufo);
483 my_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl);
485 return (unsigned char)*(fc->p)++;
488 /* If we trusted iconv a bit more, we would at this point
489 * ask why it had stopped converting ... */
491 /* Try to read some more */
492 if (fc->ibl == sizeof(fc->bufi)
493 || (fc->ibl && fc->ib + fc->ibl < fc->bufi + sizeof(fc->bufi))) {
499 memcpy(fc->bufi, fc->ib, fc->ibl);
502 fc->ibl += fread(fc->ib + fc->ibl, 1, sizeof(fc->bufi) - fc->ibl,
505 /* Try harder this time to convert some */
507 ssize_t obl = ssizeof(fc->bufo);
509 mutt_iconv(fc->cd, (const char **)&fc->ib, &fc->ibl, &fc->ob, &obl,
511 if (fc->p < fc->ob) {
512 return (unsigned char)*(fc->p)++;
516 /* Either the file has finished or one of the buffers is too small */
521 char *fgetconvs(char *buf, ssize_t len, fgetconv_t *fc)
525 while (pos < len - 1) {
526 int c = fgetconv(fc);
536 return pos ? buf : NULL;