updated pt_BR.po
[claws.git] / src / codeconv.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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, CS_ISO_8859_1, 10))
418                 code_conv = conv_latintodisp;
419 #if !HAVE_LIBJCONV
420         else if (!strncasecmp(charset, "ISO-8859-", 9))
421                 code_conv = conv_latintodisp;
422 #endif
423         else if (!strcasecmp(charset, CS_SHIFT_JIS) ||
424                  !strcasecmp(charset, "SHIFT-JIS")  ||
425                  !strcasecmp(charset, "SJIS")       ||
426                  !strcasecmp(charset, "X-SJIS"))
427                 code_conv = conv_sjistodisp;
428         else if (!strcasecmp(charset, CS_EUC_JP) ||
429                  !strcasecmp(charset, "EUCJP"))
430                 code_conv = conv_euctodisp;
431         else
432                 code_conv = conv_noconv;
433
434         return code_conv;
435 }
436
437 static const struct {
438         CharSet charset;
439         gchar *const name;
440 } charsets[] = {
441         {C_US_ASCII,            CS_US_ASCII},
442         {C_UTF_8,               CS_UTF_8},
443         {C_ISO_8859_1,          CS_ISO_8859_1},
444         {C_ISO_8859_2,          CS_ISO_8859_2},
445         {C_ISO_8859_4,          CS_ISO_8859_4},
446         {C_ISO_8859_5,          CS_ISO_8859_5},
447         {C_ISO_8859_7,          CS_ISO_8859_7},
448         {C_ISO_8859_8,          CS_ISO_8859_8},
449         {C_ISO_8859_9,          CS_ISO_8859_9},
450         {C_ISO_8859_13,         CS_ISO_8859_13},
451         {C_ISO_8859_15,         CS_ISO_8859_15},
452         {C_BALTIC,              CS_BALTIC},
453         {C_CP1251,              CS_CP1251},
454         {C_WINDOWS_1251,        CS_WINDOWS_1251},
455         {C_KOI8_R,              CS_KOI8_R},
456         {C_KOI8_U,              CS_KOI8_U},
457         {C_ISO_2022_JP,         CS_ISO_2022_JP},
458         {C_ISO_2022_JP_2,       CS_ISO_2022_JP_2},
459         {C_EUC_JP,              CS_EUC_JP},
460         {C_SHIFT_JIS,           CS_SHIFT_JIS},
461         {C_ISO_2022_KR,         CS_ISO_2022_KR},
462         {C_EUC_KR,              CS_EUC_KR},
463         {C_ISO_2022_CN,         CS_ISO_2022_CN},
464         {C_EUC_CN,              CS_EUC_CN},
465         {C_GB2312,              CS_GB2312},
466         {C_EUC_TW,              CS_EUC_TW},
467         {C_BIG5,                CS_BIG5},
468 };
469
470 #if !HAVE_LIBJCONV
471 static const struct {
472         gchar *const locale;
473         CharSet charset;
474 } locale_table[] = {
475         {"ja_JP.eucJP"  , C_EUC_JP},
476         {"ja_JP.ujis"   , C_EUC_JP},
477         {"ja_JP.EUC"    , C_EUC_JP},
478         {"ja_JP.SJIS"   , C_SHIFT_JIS},
479         {"ja_JP.JIS"    , C_ISO_2022_JP},
480         {"ja_JP"        , C_EUC_JP},
481         {"ko_KR"        , C_EUC_KR},
482         {"zh_CN.GB2312" , C_GB2312},
483         {"zh_CN"        , C_GB2312},
484         {"zh_TW.eucTW"  , C_EUC_TW},
485         {"zh_TW.Big5"   , C_BIG5},
486         {"zh_TW"        , C_BIG5},
487
488         {"ru_RU.KOI8-R" , C_KOI8_R},
489         {"ru_RU.CP1251" , C_CP1251},
490
491         {"en_US"        , C_ISO_8859_1},
492         {"ca_ES"        , C_ISO_8859_1},
493         {"da_DK"        , C_ISO_8859_1},
494         {"de_DE"        , C_ISO_8859_1},
495         {"nl_NL"        , C_ISO_8859_1},
496         {"et_EE"        , C_ISO_8859_1},
497         {"fi_FI"        , C_ISO_8859_1},
498         {"fr_FR"        , C_ISO_8859_1},
499         {"is_IS"        , C_ISO_8859_1},
500         {"it_IT"        , C_ISO_8859_1},
501         {"no_NO"        , C_ISO_8859_1},
502         {"pt_PT"        , C_ISO_8859_1},
503         {"pt_BR"        , C_ISO_8859_1},
504         {"es_ES"        , C_ISO_8859_1},
505         {"sv_SE"        , C_ISO_8859_1},
506
507         {"hr_HR"        , C_ISO_8859_2},
508         {"hu_HU"        , C_ISO_8859_2},
509         {"pl_PL"        , C_ISO_8859_2},
510         {"ro_RO"        , C_ISO_8859_2},
511         {"sk_SK"        , C_ISO_8859_2},
512         {"sl_SI"        , C_ISO_8859_2},
513         {"ru_RU"        , C_ISO_8859_5},
514         {"el_GR"        , C_ISO_8859_7},
515         {"iw_IL"        , C_ISO_8859_8},
516         {"tr_TR"        , C_ISO_8859_9},
517
518         {"lt_LT.iso88594"       , C_ISO_8859_4},
519         {"lt_LT.ISO8859-4"      , C_ISO_8859_4},
520         {"lt_LT.ISO_8859-4"     , C_ISO_8859_4},
521         {"lt_LT"                , C_ISO_8859_13},
522         {"lv_LV"                , C_ISO_8859_13},
523
524         {"C"            , C_US_ASCII},
525         {"POSIX"        , C_US_ASCII},
526 };
527 #endif /* !HAVE_LIBJCONV */
528
529 const gchar *conv_get_charset_str(CharSet charset)
530 {
531         gint i;
532
533         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
534                 if (charsets[i].charset == charset)
535                         return charsets[i].name;
536         }
537
538         return NULL;
539 }
540
541 CharSet conv_get_charset_from_str(const gchar *charset)
542 {
543         gint i;
544
545         if (!charset) return C_AUTO;
546
547         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
548                 if (!strcasecmp(charsets[i].name, charset))
549                         return charsets[i].charset;
550         }
551
552         return C_AUTO;
553 }
554
555 CharSet conv_get_current_charset(void)
556 {
557         static CharSet cur_charset = -1;
558         gint i;
559
560 #if HAVE_LIBJCONV
561         const gchar *cur_codeset;
562 #else
563         const gchar *cur_locale;
564 #endif
565
566         if (cur_charset != -1)
567                 return cur_charset;
568
569 #if HAVE_LIBJCONV
570         cur_codeset = jconv_info_get_current_codeset();
571         for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
572                 if (!strcasecmp(cur_codeset, charsets[i].name)) {
573                         cur_charset = charsets[i].charset;
574                         return cur_charset;
575                 }
576         }
577 #else
578         cur_locale = g_getenv("LC_ALL");
579         if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
580         if (!cur_locale) cur_locale = g_getenv("LANG");
581         if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
582
583         debug_print("current locale: %s\n",
584                     cur_locale ? cur_locale : "(none)");
585
586         if (!cur_locale) {
587                 cur_charset = C_US_ASCII;
588                 return cur_charset;
589         }
590
591         if (strcasestr(cur_locale, "UTF-8")) {
592                 cur_charset = C_UTF_8;
593                 return cur_charset;
594         }
595
596         for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
597                 const gchar *p;
598
599                 /* "ja_JP.EUC" matches with "ja_JP.eucJP" and "ja_JP.EUC" */
600                 /* "ja_JP" matches with "ja_JP.xxxx" and "ja" */
601                 if (!strncasecmp(cur_locale, locale_table[i].locale,
602                                  strlen(locale_table[i].locale))) {
603                         cur_charset = locale_table[i].charset;
604                         return cur_charset;
605                 } else if ((p = strchr(locale_table[i].locale, '_')) &&
606                          !strchr(p + 1, '.')) {
607                         if (strlen(cur_locale) == 2 &&
608                             !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
609                                 cur_charset = locale_table[i].charset;
610                                 return cur_charset;
611                         }
612                 }
613         }
614 #endif
615
616         cur_charset = C_AUTO;
617         return cur_charset;
618 }
619
620 const gchar *conv_get_current_charset_str(void)
621 {
622 #if HAVE_LIBJCONV
623         return jconv_info_get_current_codeset();
624 #else
625         static const gchar *codeset = NULL;
626
627         if (!codeset)
628                 codeset = conv_get_charset_str(conv_get_current_charset());
629
630         return codeset ? codeset : "US-ASCII";
631 #endif
632 }
633
634 CharSet conv_get_outgoing_charset(void)
635 {
636         static CharSet out_charset = -1;
637
638 #if HAVE_LIBJCONV
639         gint i, j, n_pref_codesets;
640         const gchar *const *pref_codesets;
641 #else
642         CharSet cur_charset;
643 #endif
644
645         if (out_charset != -1)
646                 return out_charset;
647
648 #if HAVE_LIBJCONV
649         /* skip US-ASCII and UTF-8 */
650         pref_codesets = jconv_info_get_pref_codesets(&n_pref_codesets);
651         for (i = 0; i < n_pref_codesets; i++) {
652                 for (j = 2; j < sizeof(charsets) / sizeof(charsets[0]); j++) {
653                         if (!strcasecmp(pref_codesets[i], charsets[j].name)) {
654                                 out_charset = charsets[j].charset;
655                                 return out_charset;
656                         }
657                 }
658         }
659
660         for (i = 0; i < n_pref_codesets; i++) {
661                 if (!strcasecmp(pref_codesets[i], "UTF-8")) {
662                         out_charset = C_UTF_8;
663                         return out_charset;
664                 }
665         }
666
667         out_charset = C_AUTO;
668 #else
669         cur_charset = conv_get_current_charset();
670         switch (cur_charset) {
671         case C_EUC_JP:
672         case C_SHIFT_JIS:
673                 out_charset = C_ISO_2022_JP;
674                 break;
675         default:
676                 out_charset = cur_charset;
677         }
678 #endif
679
680         return out_charset;
681 }
682
683 const gchar *conv_get_outgoing_charset_str(void)
684 {
685         CharSet out_charset;
686         const gchar *str;
687
688         if (prefs_common.outgoing_charset) {
689                 if (!isalpha(prefs_common.outgoing_charset[0])) {
690                         g_free(prefs_common.outgoing_charset);
691                         prefs_common.outgoing_charset = g_strdup(CS_AUTO);
692                 } else if (strcmp(prefs_common.outgoing_charset, CS_AUTO) != 0)
693                         return prefs_common.outgoing_charset;
694         }
695
696         out_charset = conv_get_outgoing_charset();
697         str = conv_get_charset_str(out_charset);
698
699         return str ? str : "US-ASCII";
700 }
701
702 void conv_unmime_header_overwrite(gchar *str)
703 {
704         gchar *buf;
705         gint outlen;
706         CharSet cur_charset;
707
708         cur_charset = conv_get_current_charset();
709
710 #if HAVE_LIBJCONV
711         Xstrdup_a(buf, str, return);
712         outlen = strlen(str) + 1;
713         UnMimeHeaderConv(buf, str, outlen);
714         if (cur_charset == C_EUC_JP) {
715                 gchar *tmp;
716                 gint len;
717
718                 len = strlen(str) * 2 + 1;
719                 Xalloca(tmp, len, return);
720                 conv_jistodisp(tmp, len, str);
721                 strncpy2(str, tmp, outlen);
722         }
723 #else
724         if (cur_charset == C_EUC_JP) {
725                 gchar *tmp;
726                 gint len;
727
728                 Xstrdup_a(buf, str, return);
729                 outlen = strlen(str) + 1;
730                 UnMimeHeader(buf);
731                 len = strlen(buf) * 2 + 1;
732                 Xalloca(tmp, len, {strncpy2(str, buf, outlen); return;});
733                 conv_jistodisp(tmp, len, buf);
734                 strncpy2(str, tmp, outlen);
735         } else
736                 UnMimeHeader(str);
737 #endif
738 }
739
740 void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
741                         const gchar *charset)
742 {
743         gchar *buf;
744         CharSet cur_charset;
745
746         cur_charset = conv_get_current_charset();
747         Xstrdup_a(buf, str, return);
748
749 #if HAVE_LIBJCONV
750         UnMimeHeaderConv(buf, outbuf, outlen);
751 #else
752         UnMimeHeader(buf);
753         strncpy2(outbuf, buf, outlen);
754 #endif
755         if (cur_charset == C_EUC_JP) {
756                 gint len;
757
758                 len = strlen(outbuf) * 2 + 1;
759                 Xalloca(buf, len, return);
760                 conv_jistodisp(buf, len, outbuf);
761                 strncpy2(outbuf, buf, outlen);
762         }
763 }
764
765 #define MAX_ENCLEN      75
766 #define MAX_LINELEN     76
767
768 #define B64LEN(len)     ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
769
770 #if HAVE_LIBJCONV
771 void conv_encode_header(gchar *dest, gint len, const gchar *src,
772                         gint header_len)
773 {
774         wchar_t *wsrc;
775         wchar_t *wsrcp;
776         gchar *destp;
777         size_t line_len, mimehdr_len, mimehdr_begin_len;
778         gchar *mimehdr_init = "=?";
779         gchar *mimehdr_end = "?=";
780         gchar *mimehdr_enctype = "?B?";
781         const gchar *mimehdr_charset;
782
783         /* g_print("src = %s\n", src); */
784         mimehdr_charset = conv_get_outgoing_charset_str();
785
786         /* convert to wide-character string */
787         wsrcp = wsrc = strdup_mbstowcs(src);
788
789         mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
790                 strlen(mimehdr_charset) + strlen(mimehdr_enctype);
791         mimehdr_begin_len = strlen(mimehdr_init) +
792                 strlen(mimehdr_charset) + strlen(mimehdr_enctype);
793         /* line_len = 1; */
794         line_len = header_len;
795         destp = dest;
796         *dest = '\0';
797
798         g_return_if_fail(wsrc != NULL);
799
800         while (*wsrcp) {
801                 wchar_t *wp, *wtmp, *wtmpp;
802                 gint nspc = 0;
803
804                 /* irresponsible buffer overrun check */
805                 if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
806
807                 /* encode string including space
808                    if non-ASCII string follows */
809                 if (is_next_nonascii(wsrcp)) {
810                         wp = wsrcp;
811                         while ((wp = find_wspace(wp)) != NULL)
812                                 if (!is_next_nonascii(wp)) break;
813                 } else
814                         wp = find_wspace(wsrcp);
815
816                 if (wp != NULL) {
817                         wtmp = wcsndup(wsrcp, wp - wsrcp);
818                         wsrcp = wp + 1;
819                         while (iswspace(wsrcp[nspc])) nspc++;
820                 } else {
821                         wtmp = wcsdup(wsrcp);
822                         wsrcp += wcslen(wsrcp);
823                 }
824
825                 wtmpp = wtmp;
826
827                 do {
828                         gint tlen = 0, str_ascii = 1;
829                         gchar *tmp; /* internal codeset */
830                         gchar *raw; /* converted, but not base64 encoded */
831                         register gchar *tmpp;
832                         gint raw_len;
833
834                         tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
835                         *tmp = '\0';
836                         raw = g_strdup("");
837                         raw_len = 0;
838
839                         while (*wtmpp != (wchar_t)0) {
840                                 gint mbl;
841                                 gint dummy;
842                                 gchar *raw_new = NULL;
843                                 int raw_new_len = 0;
844                                 const gchar *src_codeset;
845
846                                 if (*wtmpp < 32 || *wtmpp >= 127)
847                                         str_ascii = 0;
848                                 mbl = wctomb(tmpp, *wtmpp);
849                                 if (mbl == -1) {
850                                         g_warning("invalid wide character\n");
851                                         wtmpp++;
852                                         continue;
853                                 }
854                                 /* g_free(raw); */
855                                 src_codeset = conv_get_current_charset_str();
856                                 /* printf ("tmp = %s, tlen = %d, mbl\n", 
857                                         tmp, tlen, mbl); */
858                                 if (jconv_alloc_conv(tmp, tlen + mbl,
859                                                      &raw_new, &raw_new_len,
860                                                      &src_codeset, 1,
861                                                      &dummy, mimehdr_charset)
862                                     != 0) {
863                                         g_warning("can't convert\n");
864                                         tmpp[0] = '\0';
865                                         continue;
866                                 }
867                                 if (!str_ascii) {
868                                         gint dlen = mimehdr_len +
869                                                 B64LEN(raw_len);
870                                         if ((line_len + dlen +
871                                              (*(wtmpp + 1) ? 0 : nspc) +
872                                              (line_len > 1 ? 1 : 0))
873                                             > MAX_LINELEN) {
874                                                 g_free(raw_new);
875                                                 if (tlen == 0) {
876                                                         *destp++ = '\n';
877                                                         *destp++ = ' ';
878                                                         line_len = 1;
879                                                         str_ascii = 1;
880                                                         continue;
881                                                 } else {
882                                                         *tmpp = '\0';
883                                                         break;
884                                                 }
885                                         }
886                                 } else if ((line_len + tlen + mbl +
887                                             (*(wtmpp + 1) ? 0 : nspc) +
888                                             (line_len > 1 ? 1 : 0))
889                                            > MAX_LINELEN) {
890                                         g_free(raw_new);
891                                         if (1 + tlen + mbl +
892                                             (*(wtmpp + 1) ? 0 : nspc)
893                                             >= MAX_LINELEN) {
894                                                 *tmpp = '\0';
895                                                 break;
896                                         }
897                                         *destp++ = '\n';
898                                         *destp++ = ' ';
899                                         line_len = 1;
900                                         continue;
901                                 }
902
903                                 tmpp += mbl;
904                                 *tmpp = '\0';
905
906                                 tlen += mbl;
907
908                                 g_free(raw);
909                                 raw = raw_new;
910                                 raw_len = raw_new_len;
911
912                                 wtmpp++;
913                         }
914                         /*
915                           g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
916                                 tmp, tlen, mb_seqlen);
917                         */                              
918
919                         if (tlen == 0 || raw_len == 0) {
920                                 g_free(tmp);
921                                 g_free(raw);
922                                 continue;
923                         }
924
925                         if (line_len > 1 && destp > dest) {
926                                 *destp++ = ' ';
927                                 *destp = '\0';
928                                 line_len++;
929                         }
930
931                         if (!str_ascii) {
932                                 g_snprintf(destp, len - strlen(dest), "%s%s%s",
933                                            mimehdr_init, mimehdr_charset,
934                                            mimehdr_enctype);
935                                 destp += mimehdr_begin_len;
936                                 line_len += mimehdr_begin_len;
937
938                                 to64frombits(destp, raw, raw_len);
939                                 line_len += strlen(destp);
940                                 destp += strlen(destp);
941
942                                 strcpy(destp, mimehdr_end);
943                                 destp += strlen(mimehdr_end);
944                                 line_len += strlen(mimehdr_end);
945                         } else {
946                                 strcpy(destp, tmp);
947                                 line_len += strlen(destp);
948                                 destp += strlen(destp);
949                         }
950
951                         g_free(tmp);
952                         g_free(raw);
953                         /* g_print("line_len = %d\n\n", line_len); */
954                 } while (*wtmpp != (wchar_t)0);
955
956                 while (iswspace(*wsrcp)) {
957                         gint mbl;
958
959                         mbl = wctomb(destp, *wsrcp++);
960                         if (mbl != -1) {
961                                 destp += mbl;
962                                 line_len += mbl;
963                         }
964                 }
965                 *destp = '\0';
966
967                 g_free(wtmp);
968         }
969
970         g_free(wsrc);
971
972         /* g_print("dest = %s\n", dest);  */
973 }
974 #else /* !HAVE_LIBJCONV */
975
976 #define JIS_SEQLEN      3
977
978 void conv_encode_header(gchar *dest, gint len, const gchar *src,
979                         gint header_len)
980 {
981         wchar_t *wsrc;
982         wchar_t *wsrcp;
983         gchar *destp;
984         size_t line_len, mimehdr_len, mimehdr_begin_len;
985         gchar *mimehdr_init = "=?";
986         gchar *mimehdr_end = "?=";
987         gchar *mimehdr_enctype = "?B?";
988         const gchar *mimehdr_charset;
989
990         /* g_print("src = %s\n", src); */
991         mimehdr_charset = conv_get_outgoing_charset_str();
992         if (strcmp(mimehdr_charset, "ISO-2022-JP") != 0) {
993                 /* currently only supports Japanese */
994                 strncpy2(dest, src, len);
995                 return;
996         }
997
998         /* convert to wide-character string */
999         wsrcp = wsrc = strdup_mbstowcs(src);
1000
1001         mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
1002                       strlen(mimehdr_charset) + strlen(mimehdr_enctype);
1003         mimehdr_begin_len = strlen(mimehdr_init) +
1004                             strlen(mimehdr_charset) + strlen(mimehdr_enctype);
1005         /* line_len = 1; */
1006         line_len = header_len;
1007         destp = dest;
1008         *dest = '\0';
1009
1010         g_return_if_fail(wsrc != NULL);
1011
1012         while (*wsrcp) {
1013                 wchar_t *wp, *wtmp, *wtmpp;
1014                 gint nspc = 0;
1015
1016                 /* irresponsible buffer overrun check */
1017                 if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
1018
1019                 /* encode string including space
1020                    if non-ASCII string follows */
1021                 if (is_next_nonascii(wsrcp)) {
1022                         wp = wsrcp;
1023                         while ((wp = find_wspace(wp)) != NULL)
1024                                 if (!is_next_nonascii(wp)) break;
1025                 } else
1026                         wp = find_wspace(wsrcp);
1027
1028                 if (wp != NULL) {
1029                         wtmp = wcsndup(wsrcp, wp - wsrcp);
1030                         wsrcp = wp + 1;
1031                         while (iswspace(wsrcp[nspc])) nspc++;
1032                 } else {
1033                         wtmp = wcsdup(wsrcp);
1034                         wsrcp += wcslen(wsrcp);
1035                 }
1036
1037                 wtmpp = wtmp;
1038
1039                 do {
1040                         gint prev_mbl = 1, tlen = 0, mb_seqlen = 0;
1041                         gchar *tmp;
1042                         register gchar *tmpp;
1043
1044                         tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
1045                         *tmp = '\0';
1046
1047                         while (*wtmpp != (wchar_t)0) {
1048                                 gint mbl;
1049
1050                                 mbl = wctomb(tmpp, *wtmpp);
1051                                 if (mbl == -1) {
1052                                         g_warning("invalid wide character\n");
1053                                         wtmpp++;
1054                                         continue;
1055                                 }
1056
1057                                 /* length of KI + KO */
1058                                 if (prev_mbl == 1 && mbl == 2)
1059                                         mb_seqlen += JIS_SEQLEN * 2;
1060
1061                                 if (mb_seqlen) {
1062                                         gint dlen = mimehdr_len +
1063                                                 B64LEN(tlen + mb_seqlen + mbl);
1064
1065                                         if ((line_len + dlen +
1066                                              (*(wtmpp + 1) ? 0 : nspc) +
1067                                              (line_len > 1 ? 1 : 0))
1068                                             > MAX_LINELEN) {
1069                                                 if (tlen == 0) {
1070                                                         *destp++ = '\n';
1071                                                         *destp++ = ' ';
1072                                                         line_len = 1;
1073                                                         mb_seqlen = 0;
1074                                                         continue;
1075                                                 } else {
1076                                                         *tmpp = '\0';
1077                                                         break;
1078                                                 }
1079                                         }
1080                                 } else if ((line_len + tlen + mbl +
1081                                             (*(wtmpp + 1) ? 0 : nspc) +
1082                                             (line_len > 1 ? 1 : 0))
1083                                            > MAX_LINELEN) {
1084                                         if (1 + tlen + mbl +
1085                                             (*(wtmpp + 1) ? 0 : nspc)
1086                                             >= MAX_LINELEN) {
1087                                                 *tmpp = '\0';
1088                                                 break;
1089                                         }
1090                                         *destp++ = '\n';
1091                                         *destp++ = ' ';
1092                                         line_len = 1;
1093                                         continue;
1094                                 }
1095
1096                                 tmpp += mbl;
1097                                 *tmpp = '\0';
1098
1099                                 tlen += mbl;
1100                                 prev_mbl = mbl;
1101
1102                                 wtmpp++;
1103                         }
1104                         /* g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
1105                                 tmp, tlen, mb_seqlen);
1106                         */                              
1107
1108                         if (tlen == 0) {
1109                                 g_free(tmp);
1110                                 continue;
1111                         }
1112
1113                         if (line_len > 1 && destp > dest) {
1114                                 *destp++ = ' ';
1115                                 *destp = '\0';
1116                                 line_len++;
1117                         }
1118
1119                         if (mb_seqlen) {
1120                                 gchar *tmp_jis;
1121
1122                                 tmp_jis = g_new(gchar, tlen + mb_seqlen + 1);
1123                                 conv_euctojis(tmp_jis,
1124                                               tlen + mb_seqlen + 1, tmp);
1125                                 g_snprintf(destp, len - strlen(dest), "%s%s%s",
1126                                            mimehdr_init, mimehdr_charset,
1127                                            mimehdr_enctype);
1128                                 destp += mimehdr_begin_len;
1129                                 line_len += mimehdr_begin_len;
1130
1131                                 to64frombits(destp, tmp_jis, strlen(tmp_jis));
1132                                 line_len += strlen(destp);
1133                                 destp += strlen(destp);
1134
1135                                 strcpy(destp, mimehdr_end);
1136                                 destp += strlen(mimehdr_end);
1137                                 line_len += strlen(mimehdr_end);
1138
1139                                 g_free(tmp_jis);
1140                         } else {
1141                                 strcpy(destp, tmp);
1142                                 line_len += strlen(destp);
1143                                 destp += strlen(destp);
1144                         }
1145
1146                         g_free(tmp);
1147                         /* g_print("line_len = %d\n\n", line_len); */
1148                 } while (*wtmpp != (wchar_t)0);
1149
1150                 while (iswspace(*wsrcp)) {
1151                         gint mbl;
1152
1153                         mbl = wctomb(destp, *wsrcp++);
1154                         if (mbl != -1) {
1155                                 destp += mbl;
1156                                 line_len += mbl;
1157                         }
1158                 }
1159                 *destp = '\0';
1160
1161                 g_free(wtmp);
1162         }
1163
1164         g_free(wsrc);
1165
1166         /* g_print("dest = %s\n", dest); */
1167 }
1168 #endif /* HAVE_LIBJCONV */