more sync with 0.4.99cvs2.
[claws.git] / src / codeconv.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <stdlib.h>
28
29 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
30 #  include <wchar.h>
31 #  include <wctype.h>
32 #endif
33
34 #if HAVE_LOCALE_H
35 #  include <locale.h>
36 #endif
37
38 #if HAVE_LIBJCONV
39 #  include <jconv.h>
40 #endif
41
42 #include <kcc.h>
43
44 #include "intl.h"
45 #include "codeconv.h"
46 #include "unmime.h"
47 #include "base64.h"
48 #include "utils.h"
49 #include "prefs_common.h"
50
51 #define iskanji(c) \
52         (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xfe)
53 #define iseucss(c) \
54         (((c) & 0xff) == 0x8e || ((c) & 0xff) == 0x8f)
55 #define isunprintablekanji(c) \
56         (((c) & 0xff) >= 0xa9 && ((c) & 0xff) <= 0xaf)
57
58 void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
59 {
60         KCC_filter(outbuf, "EUC", (gchar *)inbuf, "JISBB", 0, 0, 0);
61 }
62
63 void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
64 {
65         size_t inlen, len;
66
67         inlen = strlen(inbuf);
68         if (iskanji(inbuf[inlen - 1]) || iseucss(inbuf[inlen - 1])) {
69                 /* if tail end of the string is not ended with ascii,
70                    add dummy return code. */
71                 gchar *tmpin, *tmpout;
72
73                 /* length of original string + '\n' + '\0' */
74                 tmpin = alloca(inlen + 2);
75                 if (tmpin == NULL) {
76                         g_warning(_("can't allocate memory\n"));
77                         KCC_filter(outbuf, "JISBB", (gchar *)inbuf, "EUC",
78                                    0, 0, 0);
79                         return;
80                 }
81                 strcpy(tmpin, inbuf);
82                 tmpin[inlen] = '\n';
83                 tmpin[inlen + 1] = '\0';
84
85                 tmpout = alloca(outlen + 1);
86                 if (tmpout == NULL) {
87                         g_warning(_("can't allocate memory\n"));
88                         KCC_filter(outbuf, "JISBB", (gchar *)inbuf, "EUC",
89                                    0, 0, 0);
90                         return;
91                 }
92
93                 KCC_filter(tmpout, "JISBB", tmpin, "EUC", 0, 0, 0);
94                 len = strlen(tmpout);
95                 if (tmpout[len - 1] == '\n')
96                         tmpout[len - 1] = '\0';
97                 strncpy2(outbuf, tmpout, outlen);
98         } else
99                 KCC_filter(outbuf, "JISBB", (gchar *)inbuf, "EUC", 0, 0, 0);
100 }
101
102 void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
103 {
104         KCC_filter(outbuf, "EUC", (gchar *)inbuf, "SJIS", 0, 0, 0);
105 }
106
107 void conv_anytoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
108 {
109         KCC_filter(outbuf, "EUC", (gchar *)inbuf, "AUTO", 0, 0, 0);
110 }
111
112 void conv_anytojis(gchar *outbuf, gint outlen, const gchar *inbuf)
113 {
114         KCC_filter(outbuf, "JISBB", (gchar *)inbuf, "AUTO", 0, 0, 0);
115 }
116
117 #define SUBST_CHAR      '_'
118
119 void conv_unreadable_eucjp(gchar *str)
120 {
121         register guchar *p = str;
122
123         while (*p != '\0') {
124                 if (isascii(*p)) {
125                         /* convert CR+LF -> LF */
126                         if (*p == '\r' && *(p + 1) == '\n')
127                                 memmove(p, p + 1, strlen(p));
128                         /* printable 7 bit code */
129                         p++;
130                 } else if (iskanji(*p)) {
131                         if (iskanji(*(p + 1)) && !isunprintablekanji(*p))
132                                 /* printable euc-jp code */
133                                 p += 2;
134                         else {
135                                 /* substitute unprintable code */
136                                 *p++ = SUBST_CHAR;
137                                 if (*p != '\0') {
138                                         if (isascii(*p))
139                                                 p++;
140                                         else
141                                                 *p++ = SUBST_CHAR;
142                                 }
143                         }
144                 } else if (iseucss(*p)) {
145                         if ((*(p + 1) & 0x80) != 0)
146                                 /* euc-jp hankaku kana */
147                                 p += 2;
148                         else
149                                 *p++ = SUBST_CHAR;
150                 } else
151                         /* substitute unprintable 1 byte code */
152                         *p++ = SUBST_CHAR;
153         }
154 }
155
156 void conv_unreadable_8bit(gchar *str)
157 {
158         register guchar *p = str;
159
160         while (*p != '\0') {
161                 /* convert CR+LF -> LF */
162                 if (*p == '\r' && *(p + 1) == '\n')
163                         memmove(p, p + 1, strlen(p));
164                 else if (!isascii(*p)) *p = SUBST_CHAR;
165                 p++;
166         }
167 }
168
169 void conv_unreadable_latin(gchar *str)
170 {
171         register guchar *p = str;
172
173         while (*p != '\0') {
174                 /* convert CR+LF -> LF */
175                 if (*p == '\r' && *(p + 1) == '\n')
176                         memmove(p, p + 1, strlen(p));
177                 else if ((*p & 0xff) >= 0x80 && (*p & 0xff) <= 0x9f)
178                         *p = SUBST_CHAR;
179                 p++;
180         }
181 }
182
183 #define NCV     '\0'
184
185 void conv_mb_alnum(gchar *str)
186 {
187         static guchar char_tbl[] = {
188                 /* 0xa0 - 0xaf */
189                 NCV, ' ', NCV, NCV, ',', '.', NCV, ':',
190                 ';', '?', '!', NCV, NCV, NCV, NCV, NCV,
191                 /* 0xb0 - 0xbf */
192                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
193                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
194                 /* 0xc0 - 0xcf */
195                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
196                 NCV, NCV, '(', ')', NCV, NCV, '[', ']',
197                 /* 0xd0 - 0xdf */
198                 '{', '}', NCV, NCV, NCV, NCV, NCV, NCV,
199                 NCV, NCV, NCV, NCV, '+', '-', NCV, NCV,
200                 /* 0xe0 - 0xef */
201                 NCV, '=', NCV, '<', '>', NCV, NCV, NCV,
202                 NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV
203         };
204
205         register guchar *p = str;
206         register gint len;
207
208         len = strlen(str);
209
210         while (len > 1) {
211                 if (*p == 0xa3) {
212                         register guchar ch = *(p + 1);
213
214                         if (ch >= 0xb0 && ch <= 0xfa) {
215                                 /* [a-zA-Z] */
216                                 *p = ch & 0x7f;
217                                 p++;
218                                 len--;
219                                 memmove(p, p + 1, len);
220                                 len--;
221                         } else  {
222                                 p += 2;
223                                 len -= 2;
224                         }
225                 } else if (*p == 0xa1) {
226                         register guchar ch = *(p + 1);
227
228                         if (ch >= 0xa0 && ch <= 0xef &&
229                             NCV != char_tbl[ch - 0xa0]) {
230                                 *p = char_tbl[ch - 0xa0];
231                                 p++;
232                                 len--;
233                                 memmove(p, p + 1, len);
234                                 len--;
235                         } else {
236                                 p += 2;
237                                 len -= 2;
238                         }
239                 } else if (iskanji(*p)) {
240                         p += 2;
241                         len -= 2;
242                 } else {
243                         p++;
244                         len--;
245                 }
246         }
247 }
248
249 void conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
250 {
251         conv_jistoeuc(outbuf, outlen, inbuf);
252         conv_unreadable_eucjp(outbuf);
253 }
254
255 void conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
256 {
257         conv_sjistoeuc(outbuf, outlen, inbuf);
258         conv_unreadable_eucjp(outbuf);
259 }
260
261 void conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
262 {
263         strncpy2(outbuf, inbuf, outlen);
264         conv_unreadable_eucjp(outbuf);
265 }
266
267 void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
268 {
269         strncpy2(outbuf, inbuf, outlen);
270         conv_unreadable_8bit(outbuf);
271 }
272
273 void conv_latintodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
274 {
275         strncpy2(outbuf, inbuf, outlen);
276         conv_unreadable_latin(outbuf);
277 }
278
279 void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
280 {
281         strncpy2(outbuf, inbuf, outlen);
282 }
283
284 CodeConverter *conv_code_converter_new(const gchar *charset)
285 {
286         CodeConverter *conv;
287
288         conv = g_new0(CodeConverter, 1);
289 #if !HAVE_LIBJCONV
290         conv->code_conv_func = conv_get_code_conv_func(charset);
291 #endif
292         conv->charset_str = g_strdup(charset);
293         conv->charset = conv_get_charset_from_str(charset);
294
295         return conv;
296 }
297
298 void conv_code_converter_destroy(CodeConverter *conv)
299 {
300         g_free(conv->charset_str);
301         g_free(conv);
302 }
303
304 gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
305                   const gchar *inbuf)
306 {
307 #if HAVE_LIBJCONV
308         gchar *str;
309
310         str = conv_codeset_strdup(inbuf, conv->charset_str, NULL);
311         if (!str)
312                 return -1;
313         else {
314                 strncpy2(outbuf, str, outlen);
315                 g_free(str);
316         }
317 #else /* !HAVE_LIBJCONV */
318         conv->code_conv_func(outbuf, outlen, inbuf);
319 #endif
320
321         return 0;
322 }
323
324 gchar *conv_codeset_strdup(const gchar *inbuf,
325                            const gchar *src_codeset, const gchar *dest_codeset)
326 {
327         gchar *buf;
328         size_t len;
329 #if HAVE_LIBJCONV
330         gint actual_codeset;
331         const gchar *const *codesets;
332         gint n_codesets;
333 #else /* !HAVE_LIBJCONV */
334         CharSet src_charset = C_AUTO, dest_charset = C_AUTO;
335 #endif
336
337         if (!dest_codeset) {
338                 CodeConvFunc func;
339
340                 func = conv_get_code_conv_func(src_codeset);
341                 if (func != conv_noconv) {
342                         if (func == conv_jistodisp || func == conv_sjistodisp)
343                                 len = strlen(inbuf) * 2 + 1;
344                         else
345                                 len = strlen(inbuf) + 1;
346                         buf = g_malloc(len);
347                         if (!buf) return NULL;
348                         func(buf, len, inbuf);
349                         buf = g_realloc(buf, strlen(buf) + 1);
350                         return buf;
351                 }
352         }
353
354 #if HAVE_LIBJCONV
355         if (src_codeset) {
356                 codesets = &src_codeset;
357                 n_codesets = 1;
358         } else
359                 codesets = jconv_info_get_pref_codesets(&n_codesets);
360         if (!dest_codeset)
361                 dest_codeset = conv_get_current_charset_str();
362
363         if (jconv_alloc_conv(inbuf, strlen(inbuf), &buf, &len,
364                              codesets, n_codesets,
365                              &actual_codeset, dest_codeset)
366             == 0)
367                 return buf;
368         else
369                 return NULL;
370 #else /* !HAVE_LIBJCONV */
371         if (src_codeset) {
372                 if (!strcasecmp(src_codeset, CS_EUC_JP) ||
373                     !strcasecmp(src_codeset, "EUCJP"))
374                         src_charset = C_EUC_JP;
375                 else if (!strcasecmp(src_codeset, CS_SHIFT_JIS) ||
376                          !strcasecmp(src_codeset, "SHIFT-JIS") ||
377                          !strcasecmp(src_codeset, "SJIS"))
378                         src_charset = C_SHIFT_JIS;
379                 if (dest_codeset && !strcasecmp(dest_codeset, CS_ISO_2022_JP))
380                         dest_charset = C_ISO_2022_JP;
381         }
382
383         if ((src_charset == C_EUC_JP || src_charset == C_SHIFT_JIS) &&
384             dest_charset == C_ISO_2022_JP) {
385                 len = (strlen(inbuf) + 1) * 3;
386                 buf = g_malloc(len);
387                 if (buf) {
388                         if (src_charset == C_EUC_JP)
389                                 conv_euctojis(buf, len, inbuf);
390                         else
391                                 conv_anytojis(buf, len, inbuf);
392                         buf = g_realloc(buf, strlen(buf) + 1);
393                 }
394         } else
395                 buf = g_strdup(inbuf);
396
397         return buf;
398 #endif /* !HAVE_LIBJCONV */
399 }
400
401 CodeConvFunc conv_get_code_conv_func(const gchar *charset)
402 {
403         CodeConvFunc code_conv;
404
405         if (!charset) {
406                 if (conv_get_outgoing_charset() == C_ISO_2022_JP)
407                         return conv_jistodisp;
408                 else
409                         return conv_noconv;
410         }
411
412         if (!strcasecmp(charset, CS_ISO_2022_JP) ||
413             !strcasecmp(charset, CS_ISO_2022_JP_2))
414                 code_conv = conv_jistodisp;
415         else if (!strcasecmp(charset, CS_US_ASCII))
416                 code_conv = conv_ustodisp;
417         else if (!strncasecmp(charset, "ISO-8859-", 9))
418                 code_conv = conv_latintodisp;
419         else if (!strcasecmp(charset, CS_SHIFT_JIS) ||
420                  !strcasecmp(charset, "SHIFT-JIS")  ||
421                  !strcasecmp(charset, "SJIS")       ||
422                  !strcasecmp(charset, "X-SJIS"))
423                 code_conv = conv_sjistodisp;
424         else if (!strcasecmp(charset, CS_EUC_JP) ||
425                  !strcasecmp(charset, "EUCJP"))
426                 code_conv = conv_euctodisp;
427         else
428                 code_conv = conv_noconv;
429
430         return code_conv;
431 }
432
433 static const struct {
434         CharSet charset;
435         gchar *const name;
436 } charsets[] = {
437         {C_US_ASCII,            CS_US_ASCII},
438         {C_UTF_8,               CS_UTF_8},
439         {C_ISO_8859_1,          CS_ISO_8859_1},
440         {C_ISO_8859_2,          CS_ISO_8859_2},
441         {C_ISO_8859_4,          CS_ISO_8859_4},
442         {C_ISO_8859_5,          CS_ISO_8859_5},
443         {C_ISO_8859_7,          CS_ISO_8859_7},
444         {C_ISO_8859_8,          CS_ISO_8859_8},
445         {C_ISO_8859_9,          CS_ISO_8859_9},
446         {C_ISO_8859_13,         CS_ISO_8859_13},
447         {C_ISO_8859_15,         CS_ISO_8859_15},
448         {C_BALTIC,              CS_BALTIC},
449         {C_CP1251,              CS_CP1251},
450         {C_WINDOWS_1251,        CS_WINDOWS_1251},
451         {C_KOI8_R,              CS_KOI8_R},
452         {C_KOI8_U,              CS_KOI8_U},
453         {C_ISO_2022_JP,         CS_ISO_2022_JP},
454         {C_ISO_2022_JP_2,       CS_ISO_2022_JP_2},
455         {C_EUC_JP,              CS_EUC_JP},
456         {C_SHIFT_JIS,           CS_SHIFT_JIS},
457         {C_ISO_2022_KR,         CS_ISO_2022_KR},
458         {C_EUC_KR,              CS_EUC_KR},
459         {C_ISO_2022_CN,         CS_ISO_2022_CN},
460         {C_EUC_CN,              CS_EUC_CN},
461         {C_GB2312,              CS_GB2312},
462         {C_EUC_TW,              CS_EUC_TW},
463         {C_BIG5,                CS_BIG5},
464 };
465
466 #if !HAVE_LIBJCONV
467 static const struct {
468         gchar *const locale;
469         CharSet charset;
470 } locale_table[] = {
471         {"ja_JP.eucJP"  , C_EUC_JP},
472         {"ja_JP.ujis"   , C_EUC_JP},
473         {"ja_JP.EUC"    , C_EUC_JP},
474         {"ja_JP.SJIS"   , C_SHIFT_JIS},
475         {"ja_JP.JIS"    , C_ISO_2022_JP},
476         {"ja_JP"        , C_EUC_JP},
477         {"ko_KR"        , C_EUC_KR},
478         {"zh_CN.GB2312" , C_GB2312},
479         {"zh_CN"        , C_GB2312},
480         {"zh_TW.eucTW"  , C_EUC_TW},
481         {"zh_TW.Big5"   , C_BIG5},
482         {"zh_TW"        , C_BIG5},
483
484         {"ru_RU.KOI8-R" , C_KOI8_R},
485         {"ru_RU.CP1251" , C_CP1251},
486
487         {"en_US"        , C_ISO_8859_1},
488         {"ca_ES"        , C_ISO_8859_1},
489         {"da_DK"        , C_ISO_8859_1},
490         {"de_DE"        , C_ISO_8859_1},
491         {"nl_NL"        , C_ISO_8859_1},
492         {"et_EE"        , C_ISO_8859_1},
493         {"fi_FI"        , C_ISO_8859_1},
494         {"fr_FR"        , C_ISO_8859_1},
495         {"is_IS"        , C_ISO_8859_1},
496         {"it_IT"        , C_ISO_8859_1},
497         {"no_NO"        , C_ISO_8859_1},
498         {"pt_PT"        , C_ISO_8859_1},
499         {"pt_BR"        , C_ISO_8859_1},
500         {"es_ES"        , C_ISO_8859_1},
501         {"sv_SE"        , C_ISO_8859_1},
502
503         {"hr_HR"        , C_ISO_8859_2},
504         {"hu_HU"        , C_ISO_8859_2},
505         {"pl_PL"        , C_ISO_8859_2},
506         {"ro_RO"        , C_ISO_8859_2},
507         {"sk_SK"        , C_ISO_8859_2},
508         {"sl_SI"        , C_ISO_8859_2},
509         {"ru_RU"        , C_ISO_8859_5},
510         {"el_GR"        , C_ISO_8859_7},
511         {"iw_IL"        , C_ISO_8859_8},
512         {"tr_TR"        , C_ISO_8859_9},
513
514         {"lt_LT.iso88594"       , C_ISO_8859_4},
515         {"lt_LT.ISO8859-4"      , C_ISO_8859_4},
516         {"lt_LT.ISO_8859-4"     , C_ISO_8859_4},
517         {"lt_LT"                , C_ISO_8859_13},
518         {"lv_LV"                , C_ISO_8859_13},
519
520         {"C"            , C_US_ASCII},
521         {"POSIX"        , C_US_ASCII},
522 };
523 #endif /* !HAVE_LIBJCONV */
524
525 const gchar *conv_get_charset_str(CharSet charset)
526 {
527         gint i;
528
529         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
530                 if (charsets[i].charset == charset)
531                         return charsets[i].name;
532         }
533
534         return NULL;
535 }
536
537 CharSet conv_get_charset_from_str(const gchar *charset)
538 {
539         gint i;
540
541         if (!charset) return C_AUTO;
542
543         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
544                 if (!strcasecmp(charsets[i].name, charset))
545                         return charsets[i].charset;
546         }
547
548         return C_AUTO;
549 }
550
551 CharSet conv_get_current_charset(void)
552 {
553         static CharSet cur_charset = -1;
554         gint i;
555
556 #if HAVE_LIBJCONV
557         const gchar *cur_codeset;
558 #else
559         const gchar *cur_locale;
560 #endif
561
562         if (cur_charset != -1)
563                 return cur_charset;
564
565 #if HAVE_LIBJCONV
566         cur_codeset = jconv_info_get_current_codeset();
567         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
568                 if (!strcasecmp(cur_codeset, charsets[i].name)) {
569                         cur_charset = charsets[i].charset;
570                         return cur_charset;
571                 }
572         }
573 #else
574         cur_locale = g_getenv("LC_ALL");
575         if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
576         if (!cur_locale) cur_locale = g_getenv("LANG");
577         if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
578
579         debug_print("current locale: %s\n",
580                     cur_locale ? cur_locale : "(none)");
581
582         if (!cur_locale) {
583                 cur_charset = C_US_ASCII;
584                 return cur_charset;
585         }
586
587         if (strcasestr(cur_locale, "UTF-8")) {
588                 cur_charset = C_UTF_8;
589                 return cur_charset;
590         }
591
592         for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
593                 const gchar *p;
594
595                 /* "ja_JP.EUC" matches with "ja_JP.eucJP" and "ja_JP.EUC" */
596                 /* "ja_JP" matches with "ja_JP.xxxx" and "ja" */
597                 if (!strncasecmp(cur_locale, locale_table[i].locale,
598                                  strlen(locale_table[i].locale))) {
599                         cur_charset = locale_table[i].charset;
600                         return cur_charset;
601                 } else if ((p = strchr(locale_table[i].locale, '_')) &&
602                          !strchr(p + 1, '.')) {
603                         if (strlen(cur_locale) == 2 &&
604                             !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
605                                 cur_charset = locale_table[i].charset;
606                                 return cur_charset;
607                         }
608                 }
609         }
610 #endif
611
612         cur_charset = C_AUTO;
613         return cur_charset;
614 }
615
616 const gchar *conv_get_current_charset_str(void)
617 {
618 #if HAVE_LIBJCONV
619         return jconv_info_get_current_codeset();
620 #else
621         static const gchar *codeset = NULL;
622
623         if (!codeset)
624                 codeset = conv_get_charset_str(conv_get_current_charset());
625
626         return codeset ? codeset : "US-ASCII";
627 #endif
628 }
629
630 CharSet conv_get_outgoing_charset(void)
631 {
632         static CharSet out_charset = -1;
633
634 #if HAVE_LIBJCONV
635         gint i, j, n_pref_codesets;
636         const gchar *const *pref_codesets;
637 #else
638         CharSet cur_charset;
639 #endif
640
641         if (out_charset != -1)
642                 return out_charset;
643
644 #if HAVE_LIBJCONV
645         /* skip US-ASCII and UTF-8 */
646         pref_codesets = jconv_info_get_pref_codesets(&n_pref_codesets);
647         for (i = 0; i < n_pref_codesets; i++) {
648                 for (j = 2; j < sizeof(charsets) / sizeof(charsets[0]); j++) {
649                         if (!strcasecmp(pref_codesets[i], charsets[j].name)) {
650                                 out_charset = charsets[j].charset;
651                                 return out_charset;
652                         }
653                 }
654         }
655
656         for (i = 0; i < n_pref_codesets; i++) {
657                 if (!strcasecmp(pref_codesets[i], "UTF-8")) {
658                         out_charset = C_UTF_8;
659                         return out_charset;
660                 }
661         }
662
663         out_charset = C_AUTO;
664 #else
665         cur_charset = conv_get_current_charset();
666         switch (cur_charset) {
667         case C_EUC_JP:
668         case C_SHIFT_JIS:
669                 out_charset = C_ISO_2022_JP;
670                 break;
671         default:
672                 out_charset = cur_charset;
673         }
674 #endif
675
676         return out_charset;
677 }
678
679 const gchar *conv_get_outgoing_charset_str(void)
680 {
681         CharSet out_charset;
682         const gchar *str;
683
684         if (prefs_common.outgoing_charset) {
685                 if (!isalpha(prefs_common.outgoing_charset[0])) {
686                         g_free(prefs_common.outgoing_charset);
687                         prefs_common.outgoing_charset = g_strdup(CS_AUTO);
688                 } else if (strcmp(prefs_common.outgoing_charset, CS_AUTO) != 0)
689                         return prefs_common.outgoing_charset;
690         }
691
692         out_charset = conv_get_outgoing_charset();
693         str = conv_get_charset_str(out_charset);
694
695         return str ? str : "US-ASCII";
696 }
697
698 void conv_unmime_header_overwrite(gchar *str)
699 {
700         gchar *buf;
701         gint outlen;
702         CharSet cur_charset;
703
704         cur_charset = conv_get_current_charset();
705
706 #if HAVE_LIBJCONV
707         Xstrdup_a(buf, str, {return;});
708         outlen = strlen(str) + 1;
709         UnMimeHeaderConv(buf, str, outlen);
710         if (cur_charset == C_EUC_JP)
711                 conv_unreadable_eucjp(str);
712 #else
713         if (cur_charset == C_EUC_JP) {
714                 gchar *tmp;
715                 gint len;
716
717                 Xstrdup_a(buf, str, {return;});
718                 outlen = strlen(str) + 1;
719                 UnMimeHeader(buf);
720                 len = strlen(buf) * 2 + 1;
721                 Xalloca(tmp, len, {strncpy2(str, buf, outlen); return;});
722                 conv_jistodisp(tmp, len, buf);
723                 strncpy2(str, tmp, outlen);
724         } else
725                 UnMimeHeader(str);
726 #endif
727 }
728
729 void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
730                         const gchar *charset)
731 {
732         gchar *buf;
733         CharSet cur_charset;
734
735         Xstrdup_a(buf, str, {return;});
736         cur_charset = conv_get_current_charset();
737
738 #if HAVE_LIBJCONV
739         UnMimeHeaderConv(buf, outbuf, outlen);
740         if (cur_charset == C_EUC_JP)
741                 conv_unreadable_eucjp(outbuf);
742 #else
743         UnMimeHeader(buf);
744         if (cur_charset == C_EUC_JP) {
745                 gchar *tmp;
746                 gint len;
747
748                 len = strlen(buf) * 2 + 1;
749                 Xalloca(tmp, len, {strncpy2(outbuf, buf, outlen); return;});
750                 conv_jistodisp(tmp, len, buf);
751                 strncpy2(outbuf, tmp, outlen);
752         } else
753                 strncpy2(outbuf, buf, outlen);
754 #endif
755 }
756
757 #define MAX_ENCLEN      75
758 #define MAX_LINELEN     76
759
760 #define B64LEN(len)     ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
761
762 #if HAVE_LIBJCONV
763 void conv_encode_header(gchar *dest, gint len, const gchar *src,
764                         gint header_len)
765 {
766         wchar_t *wsrc;
767         wchar_t *wsrcp;
768         gchar *destp;
769         size_t line_len, mimehdr_len, mimehdr_begin_len;
770         gchar *mimehdr_init = "=?";
771         gchar *mimehdr_end = "?=";
772         gchar *mimehdr_enctype = "?B?";
773         const gchar *mimehdr_charset;
774
775         //g_print("src = %s\n", src);
776         mimehdr_charset = conv_get_outgoing_charset_str();
777
778         /* convert to wide-character string */
779         wsrcp = wsrc = strdup_mbstowcs(src);
780
781         mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
782                 strlen(mimehdr_charset) + strlen(mimehdr_enctype);
783         mimehdr_begin_len = strlen(mimehdr_init) +
784                 strlen(mimehdr_charset) + strlen(mimehdr_enctype);
785         //line_len = 1;
786         line_len = header_len;
787         destp = dest;
788         *dest = '\0';
789
790         g_return_if_fail(wsrc != NULL);
791
792         while (*wsrcp) {
793                 wchar_t *wp, *wtmp, *wtmpp;
794                 gint nspc = 0;
795
796                 /* irresponsible buffer overrun check */
797                 if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
798
799                 /* encode string including space
800                    if non-ASCII string follows */
801                 if (is_next_nonascii(wsrcp)) {
802                         wp = wsrcp;
803                         while ((wp = find_wspace(wp)) != NULL)
804                                 if (!is_next_nonascii(wp)) break;
805                 } else
806                         wp = find_wspace(wsrcp);
807
808                 if (wp != NULL) {
809                         wtmp = wcsndup(wsrcp, wp - wsrcp);
810                         wsrcp = wp + 1;
811                         while (iswspace(wsrcp[nspc])) nspc++;
812                 } else {
813                         wtmp = wcsdup(wsrcp);
814                         wsrcp += wcslen(wsrcp);
815                 }
816
817                 wtmpp = wtmp;
818
819                 do {
820                         gint tlen = 0, str_ascii = 1;
821                         gchar *tmp; /* internal codeset */
822                         gchar *raw; /* converted, but not base64 encoded */
823                         register gchar *tmpp;
824                         gint raw_len;
825
826                         tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
827                         *tmp = '\0';
828                         raw = g_strdup("");
829                         raw_len = 0;
830
831                         while (*wtmpp != (wchar_t)0) {
832                                 gint mbl;
833                                 gint dummy;
834                                 gchar *raw_new = NULL;
835                                 int raw_new_len = 0;
836                                 const gchar *src_codeset;
837
838                                 if (*wtmpp < 32 || *wtmpp >= 127)
839                                         str_ascii = 0;
840                                 mbl = wctomb(tmpp, *wtmpp);
841                                 if (mbl == -1) {
842                                         g_warning("invalid wide character\n");
843                                         wtmpp++;
844                                         continue;
845                                 }
846                                 /* g_free(raw); */
847                                 src_codeset = conv_get_current_charset_str();
848                                 //printf ("tmp = %s, tlen = %d, mbl\n",
849                                 //      tmp, tlen, mbl);
850                                 if (jconv_alloc_conv(tmp, tlen + mbl,
851                                                      &raw_new, &raw_new_len,
852                                                      &src_codeset, 1,
853                                                      &dummy, mimehdr_charset)
854                                     != 0) {
855                                         g_warning("can't convert\n");
856                                         tmpp[0] = '\0';
857                                         continue;
858                                 }
859                                 if (!str_ascii) {
860                                         gint dlen = mimehdr_len +
861                                                 B64LEN(raw_len);
862                                         if ((line_len + dlen +
863                                              (*(wtmpp + 1) ? 0 : nspc) +
864                                              (line_len > 1 ? 1 : 0))
865                                             > MAX_LINELEN) {
866                                                 g_free(raw_new);
867                                                 if (tlen == 0) {
868                                                         *destp++ = '\n';
869                                                         *destp++ = ' ';
870                                                         line_len = 1;
871                                                         str_ascii = 1;
872                                                         continue;
873                                                 } else {
874                                                         *tmpp = '\0';
875                                                         break;
876                                                 }
877                                         }
878                                 } else {
879                                         if ((line_len + tlen + mbl +
880                                              (*(wtmpp + 1) ? 0 : nspc) +
881                                              (line_len > 1 ? 1 : 0))
882                                             > MAX_LINELEN) {
883                                                 g_free(raw_new);
884                                                 if (1 + tlen + mbl +
885                                                     (*(wtmpp + 1) ? 0 : nspc)
886                                                     >= MAX_LINELEN) {
887                                                         *tmpp = '\0';
888                                                         break;
889                                                 }
890                                                 *destp++ = '\n';
891                                                 *destp++ = ' ';
892                                                 line_len = 1;
893                                                 continue;
894                                         }
895                                 }
896
897                                 tmpp += mbl;
898                                 *tmpp = '\0';
899
900                                 tlen += mbl;
901
902                                 g_free(raw);
903                                 raw = raw_new;
904                                 raw_len = raw_new_len;
905
906                                 wtmpp++;
907                         }
908                         //g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
909                         //      tmp, tlen, mb_seqlen);
910
911                         if (tlen == 0 || raw_len == 0) {
912                                 g_free(tmp);
913                                 g_free(raw);
914                                 continue;
915                         }
916
917                         if (line_len > 1 && destp > dest) {
918                                 *destp++ = ' ';
919                                 *destp = '\0';
920                                 line_len++;
921                         }
922
923                         if (!str_ascii) {
924                                 g_snprintf(destp, len - strlen(dest), "%s%s%s",
925                                            mimehdr_init, mimehdr_charset,
926                                            mimehdr_enctype);
927                                 destp += mimehdr_begin_len;
928                                 line_len += mimehdr_begin_len;
929
930                                 to64frombits(destp, raw, raw_len);
931                                 line_len += strlen(destp);
932                                 destp += strlen(destp);
933
934                                 strcpy(destp, mimehdr_end);
935                                 destp += strlen(mimehdr_end);
936                                 line_len += strlen(mimehdr_end);
937                         } else {
938                                 strcpy(destp, tmp);
939                                 line_len += strlen(destp);
940                                 destp += strlen(destp);
941                         }
942
943                         g_free(tmp);
944                         g_free(raw);
945                         //g_print("line_len = %d\n\n", line_len);
946                 } while (*wtmpp != (wchar_t)0);
947
948                 while (iswspace(*wsrcp)) {
949                         gint mbl;
950
951                         mbl = wctomb(destp, *wsrcp++);
952                         if (mbl != -1) {
953                                 destp += mbl;
954                                 line_len += mbl;
955                         }
956                 }
957                 *destp = '\0';
958
959                 g_free(wtmp);
960         }
961
962         g_free(wsrc);
963
964         //g_print("dest = %s\n", dest);
965 }
966 #else /* !HAVE_LIBJCONV */
967
968 #define JIS_SEQLEN      3
969
970 void conv_encode_header(gchar *dest, gint len, const gchar *src,
971                         gint header_len)
972 {
973         wchar_t *wsrc;
974         wchar_t *wsrcp;
975         gchar *destp;
976         size_t line_len, mimehdr_len, mimehdr_begin_len;
977         gchar *mimehdr_init = "=?";
978         gchar *mimehdr_end = "?=";
979         gchar *mimehdr_enctype = "?B?";
980         const gchar *mimehdr_charset;
981
982         //g_print("src = %s\n", src);
983         mimehdr_charset = conv_get_outgoing_charset_str();
984         if (strcmp(mimehdr_charset, "ISO-2022-JP") != 0) {
985                 /* currently only supports Japanese */
986                 strncpy2(dest, src, len);
987                 return;
988         }
989
990         /* convert to wide-character string */
991         wsrcp = wsrc = strdup_mbstowcs(src);
992
993         mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
994                       strlen(mimehdr_charset) + strlen(mimehdr_enctype);
995         mimehdr_begin_len = strlen(mimehdr_init) +
996                             strlen(mimehdr_charset) + strlen(mimehdr_enctype);
997         //line_len = 1;
998         line_len = header_len;
999         destp = dest;
1000         *dest = '\0';
1001
1002         g_return_if_fail(wsrc != NULL);
1003
1004         while (*wsrcp) {
1005                 wchar_t *wp, *wtmp, *wtmpp;
1006                 gint nspc = 0;
1007
1008                 /* irresponsible buffer overrun check */
1009                 if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
1010
1011                 /* encode string including space
1012                    if non-ASCII string follows */
1013                 if (is_next_nonascii(wsrcp)) {
1014                         wp = wsrcp;
1015                         while ((wp = find_wspace(wp)) != NULL)
1016                                 if (!is_next_nonascii(wp)) break;
1017                 } else
1018                         wp = find_wspace(wsrcp);
1019
1020                 if (wp != NULL) {
1021                         wtmp = wcsndup(wsrcp, wp - wsrcp);
1022                         wsrcp = wp + 1;
1023                         while (iswspace(wsrcp[nspc])) nspc++;
1024                 } else {
1025                         wtmp = wcsdup(wsrcp);
1026                         wsrcp += wcslen(wsrcp);
1027                 }
1028
1029                 wtmpp = wtmp;
1030
1031                 do {
1032                         gint prev_mbl = 1, tlen = 0, mb_seqlen = 0;
1033                         gchar *tmp;
1034                         register gchar *tmpp;
1035
1036                         tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
1037                         *tmp = '\0';
1038
1039                         while (*wtmpp != (wchar_t)0) {
1040                                 gint mbl;
1041
1042                                 mbl = wctomb(tmpp, *wtmpp);
1043                                 if (mbl == -1) {
1044                                         g_warning("invalid wide character\n");
1045                                         wtmpp++;
1046                                         continue;
1047                                 }
1048
1049                                 /* length of KI + KO */
1050                                 if (prev_mbl == 1 && mbl == 2)
1051                                         mb_seqlen += JIS_SEQLEN * 2;
1052
1053                                 if (mb_seqlen) {
1054                                         gint dlen = mimehdr_len +
1055                                                 B64LEN(tlen + mb_seqlen + mbl);
1056
1057                                         if ((line_len + dlen +
1058                                              (*(wtmpp + 1) ? 0 : nspc) +
1059                                              (line_len > 1 ? 1 : 0))
1060                                             > MAX_LINELEN) {
1061                                                 if (tlen == 0) {
1062                                                         *destp++ = '\n';
1063                                                         *destp++ = ' ';
1064                                                         line_len = 1;
1065                                                         mb_seqlen = 0;
1066                                                         continue;
1067                                                 } else {
1068                                                         *tmpp = '\0';
1069                                                         break;
1070                                                 }
1071                                         }
1072                                 } else {
1073                                         if ((line_len + tlen + mbl +
1074                                              (*(wtmpp + 1) ? 0 : nspc) +
1075                                              (line_len > 1 ? 1 : 0))
1076                                             > MAX_LINELEN) {
1077                                                 if (1 + tlen + mbl +
1078                                                     (*(wtmpp + 1) ? 0 : nspc)
1079                                                     >= MAX_LINELEN) {
1080                                                         *tmpp = '\0';
1081                                                         break;
1082                                                 }
1083                                                 *destp++ = '\n';
1084                                                 *destp++ = ' ';
1085                                                 line_len = 1;
1086                                                 continue;
1087                                         }
1088                                 }
1089
1090                                 tmpp += mbl;
1091                                 *tmpp = '\0';
1092
1093                                 tlen += mbl;
1094                                 prev_mbl = mbl;
1095
1096                                 wtmpp++;
1097                         }
1098                         //g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
1099                         //      tmp, tlen, mb_seqlen);
1100
1101                         if (tlen == 0) {
1102                                 g_free(tmp);
1103                                 continue;
1104                         }
1105
1106                         if (line_len > 1 && destp > dest) {
1107                                 *destp++ = ' ';
1108                                 *destp = '\0';
1109                                 line_len++;
1110                         }
1111
1112                         if (mb_seqlen) {
1113                                 gchar *tmp_jis;
1114
1115                                 tmp_jis = g_new(gchar, tlen + mb_seqlen + 1);
1116                                 conv_euctojis(tmp_jis,
1117                                               tlen + mb_seqlen + 1, tmp);
1118                                 g_snprintf(destp, len - strlen(dest), "%s%s%s",
1119                                            mimehdr_init, mimehdr_charset,
1120                                            mimehdr_enctype);
1121                                 destp += mimehdr_begin_len;
1122                                 line_len += mimehdr_begin_len;
1123
1124                                 to64frombits(destp, tmp_jis, strlen(tmp_jis));
1125                                 line_len += strlen(destp);
1126                                 destp += strlen(destp);
1127
1128                                 strcpy(destp, mimehdr_end);
1129                                 destp += strlen(mimehdr_end);
1130                                 line_len += strlen(mimehdr_end);
1131
1132                                 g_free(tmp_jis);
1133                         } else {
1134                                 strcpy(destp, tmp);
1135                                 line_len += strlen(destp);
1136                                 destp += strlen(destp);
1137                         }
1138
1139                         g_free(tmp);
1140                         //g_print("line_len = %d\n\n", line_len);
1141                 } while (*wtmpp != (wchar_t)0);
1142
1143                 while (iswspace(*wsrcp)) {
1144                         gint mbl;
1145
1146                         mbl = wctomb(destp, *wsrcp++);
1147                         if (mbl != -1) {
1148                                 destp += mbl;
1149                                 line_len += mbl;
1150                         }
1151                 }
1152                 *destp = '\0';
1153
1154                 g_free(wtmp);
1155         }
1156
1157         g_free(wsrc);
1158
1159         //g_print("dest = %s\n", dest);
1160 }
1161 #endif /* HAVE_LIBJCONV */