2005-06-17 [colin] 1.9.11cvs80
[claws.git] / src / codeconv.c
index f0c072256f1701ee39ac1f3c27e3a23614e11e9d..d2d636a58d8fc156bc385dffca7791407ccf7021 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2005 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #  include "config.h"
 #endif
 
+#include "defs.h"
+
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
-
-#if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
-#  include <wchar.h>
-#  include <wctype.h>
-#endif
+#include <errno.h>
 
 #if HAVE_LOCALE_H
 #  include <locale.h>
 #endif
 
-#if HAVE_LIBJCONV
-#  include <jconv.h>
-#endif
+#include <iconv.h>
 
-#include "intl.h"
 #include "codeconv.h"
 #include "unmime.h"
 #include "base64.h"
+#include "quoted-printable.h"
 #include "utils.h"
 #include "prefs_common.h"
 
@@ -54,7 +51,7 @@ typedef enum
        JIS_AUXKANJI
 } JISState;
 
-#define SUBST_CHAR     '_'
+#define SUBST_CHAR     0x5f;
 #define ESC            '\033'
 
 #define iseuckanji(c) \
@@ -65,8 +62,6 @@ typedef enum
        (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
 #define iseucaux(c) \
        (((c) & 0xff) == 0x8f)
-#define isunprintableeuckanji(c) \
-       (((c) & 0xff) >= 0xa9 && ((c) & 0xff) <= 0xaf)
 #define issjiskanji1(c) \
        ((((c) & 0xff) >= 0x81 && ((c) & 0xff) <= 0x9f) || \
         (((c) & 0xff) >= 0xe0 && ((c) & 0xff) <= 0xfc))
@@ -109,7 +104,29 @@ typedef enum
                state = JIS_AUXKANJI;   \
        }
 
-void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
+static void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
+static void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf);
+static void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static void conv_jistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+static void conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+static void conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+static void conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static void conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
+static void conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static void conv_unreadable_8bit(gchar *str);
+
+static void conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static void conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static void conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
        guchar *out = outbuf;
@@ -178,14 +195,80 @@ void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        *out = '\0';
 }
 
-void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
+#define JIS_HWDAKUTEN          0x5e
+#define JIS_HWHANDAKUTEN       0x5f
+
+static gint conv_jis_hantozen(guchar *outbuf, guchar jis_code, guchar sound_sym)
+{
+       static guint16 h2z_tbl[] = {
+               /* 0x20 - 0x2f */
+               0x0000, 0x2123, 0x2156, 0x2157, 0x2122, 0x2126, 0x2572, 0x2521,
+               0x2523, 0x2525, 0x2527, 0x2529, 0x2563, 0x2565, 0x2567, 0x2543,
+               /* 0x30 - 0x3f */
+               0x213c, 0x2522, 0x2524, 0x2526, 0x2528, 0x252a, 0x252b, 0x252d,
+               0x252f, 0x2531, 0x2533, 0x2535, 0x2537, 0x2539, 0x253b, 0x253d,
+               /* 0x40 - 0x4f */
+               0x253f, 0x2541, 0x2544, 0x2546, 0x2548, 0x254a, 0x254b, 0x254c,
+               0x254d, 0x254e, 0x254f, 0x2552, 0x2555, 0x2558, 0x255b, 0x255e,
+               /* 0x50 - 0x5f */
+               0x255f, 0x2560, 0x2561, 0x2562, 0x2564, 0x2566, 0x2568, 0x2569,
+               0x256a, 0x256b, 0x256c, 0x256d, 0x256f, 0x2573, 0x212b, 0x212c
+       };
+
+       static guint16 dakuten_tbl[] = {
+               /* 0x30 - 0x3f */
+               0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x252c, 0x252e,
+               0x2530, 0x2532, 0x2534, 0x2536, 0x2538, 0x253a, 0x253c, 0x253e,
+               /* 0x40 - 0x4f */
+               0x2540, 0x2542, 0x2545, 0x2547, 0x2549, 0x0000, 0x0000, 0x0000,
+               0x0000, 0x0000, 0x2550, 0x2553, 0x2556, 0x2559, 0x255c, 0x0000
+       };
+
+       static guint16 handakuten_tbl[] = {
+               /* 0x4a - 0x4e */
+               0x2551, 0x2554, 0x2557, 0x255a, 0x255d
+       };
+
+       guint16 out_code;
+
+       jis_code &= 0x7f;
+       sound_sym &= 0x7f;
+
+       if (jis_code < 0x21 || jis_code > 0x5f)
+               return 0;
+
+       if (sound_sym == JIS_HWDAKUTEN &&
+           jis_code >= 0x36 && jis_code <= 0x4e) {
+               out_code = dakuten_tbl[jis_code - 0x30];
+               if (out_code != 0) {
+                       *outbuf = out_code >> 8;
+                       *(outbuf + 1) = out_code & 0xff;
+                       return 2;
+               }
+       }
+
+       if (sound_sym == JIS_HWHANDAKUTEN &&
+           jis_code >= 0x4a && jis_code <= 0x4e) {
+               out_code = handakuten_tbl[jis_code - 0x4a];
+               *outbuf = out_code >> 8;
+               *(outbuf + 1) = out_code & 0xff;
+               return 2;
+       }
+
+       out_code = h2z_tbl[jis_code - 0x20];
+       *outbuf = out_code >> 8;
+       *(outbuf + 1) = out_code & 0xff;
+       return 1;
+}
+
+static void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
        guchar *out = outbuf;
        JISState state = JIS_ASCII;
 
        while (*in != '\0') {
-               if (isascii(*in)) {
+               if (IS_ASCII(*in)) {
                        K_OUT();
                        *out++ = *in++;
                } else if (iseuckanji(*in)) {
@@ -197,19 +280,43 @@ void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
                                K_OUT();
                                *out++ = SUBST_CHAR;
                                in++;
-                               if (*in != '\0' && !isascii(*in)) {
+                               if (*in != '\0' && !IS_ASCII(*in)) {
                                        *out++ = SUBST_CHAR;
                                        in++;
                                }
                        }
                } else if (iseuchwkana1(*in)) {
-                       in++;
-                       if (iseuchwkana2(*in)) {
-                               HW_IN();
-                               *out++ = *in++ & 0x7f;
+                       if (iseuchwkana2(*(in + 1))) {
+                               if (prefs_common.allow_jisx0201_kana) {
+                                       HW_IN();
+                                       in++;
+                                       *out++ = *in++ & 0x7f;
+                               } else {
+                                       guchar jis_ch[2];
+                                       gint len;
+
+                                       if (iseuchwkana1(*(in + 2)) &&
+                                           iseuchwkana2(*(in + 3)))
+                                               len = conv_jis_hantozen
+                                                       (jis_ch,
+                                                        *(in + 1), *(in + 3));
+                                       else
+                                               len = conv_jis_hantozen
+                                                       (jis_ch,
+                                                        *(in + 1), '\0');
+                                       if (len == 0)
+                                               in += 2;
+                                       else {
+                                               K_IN();
+                                               in += len * 2;
+                                               *out++ = jis_ch[0];
+                                               *out++ = jis_ch[1];
+                                       }
+                               }
                        } else {
                                K_OUT();
-                               if (*in != '\0' && !isascii(*in)) {
+                               in++;
+                               if (*in != '\0' && !IS_ASCII(*in)) {
                                        *out++ = SUBST_CHAR;
                                        in++;
                                }
@@ -222,10 +329,10 @@ void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
                                *out++ = *in++ & 0x7f;
                        } else {
                                K_OUT();
-                               if (*in != '\0' && !isascii(*in)) {
+                               if (*in != '\0' && !IS_ASCII(*in)) {
                                        *out++ = SUBST_CHAR;
                                        in++;
-                                       if (*in != '\0' && !isascii(*in)) {
+                                       if (*in != '\0' && !IS_ASCII(*in)) {
                                                *out++ = SUBST_CHAR;
                                                in++;
                                        }
@@ -242,13 +349,13 @@ void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
        *out = '\0';
 }
 
-void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
+static void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
        guchar *out = outbuf;
 
        while (*in != '\0') {
-               if (isascii(*in)) {
+               if (IS_ASCII(*in)) {
                        *out++ = *in++;
                } else if (issjiskanji1(*in)) {
                        if (issjiskanji2(*(in + 1))) {
@@ -271,7 +378,7 @@ void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
                        } else {
                                *out++ = SUBST_CHAR;
                                in++;
-                               if (*in != '\0' && !isascii(*in)) {
+                               if (*in != '\0' && !IS_ASCII(*in)) {
                                        *out++ = SUBST_CHAR;
                                        in++;
                                }
@@ -288,26 +395,71 @@ void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        *out = '\0';
 }
 
-void conv_anytoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
+static void conv_jistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       switch (conv_guess_encoding(inbuf)) {
-       case C_ISO_2022_JP:
-               conv_jistoeuc(outbuf, outlen, inbuf);
-               break;
-       case C_SHIFT_JIS:
-               conv_sjistoeuc(outbuf, outlen, inbuf);
-               break;
-       default:
+       gchar *eucstr;
+
+       Xalloca(eucstr, outlen, return);
+
+       conv_jistoeuc(eucstr, outlen, inbuf);
+       conv_euctoutf8(outbuf, outlen, eucstr);
+}
+
+static void conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
+{
+       gchar *tmpstr;
+
+       tmpstr = conv_iconv_strdup(inbuf, CS_SHIFT_JIS, CS_UTF_8);
+       if (tmpstr) {
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+       } else
                strncpy2(outbuf, inbuf, outlen);
-               break;
+}
+
+static void conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
+{
+       static iconv_t cd = (iconv_t)-1;
+       static gboolean iconv_ok = TRUE;
+       gchar *tmpstr;
+
+       if (cd == (iconv_t)-1) {
+               if (!iconv_ok) {
+                       strncpy2(outbuf, inbuf, outlen);
+                       return;
+               }
+               cd = iconv_open(CS_UTF_8, CS_EUC_JP_MS);
+               if (cd == (iconv_t)-1) {
+                       cd = iconv_open(CS_UTF_8, CS_EUC_JP);
+                       if (cd == (iconv_t)-1) {
+                               g_warning("conv_euctoutf8(): %s\n",
+                                         g_strerror(errno));
+                               iconv_ok = FALSE;
+                               strncpy2(outbuf, inbuf, outlen);
+                               return;
+                       }
+               }
        }
+
+       tmpstr = conv_iconv_strdup_with_cd(inbuf, cd);
+       if (tmpstr) {
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+       } else
+               strncpy2(outbuf, inbuf, outlen);
 }
 
-void conv_anytojis(gchar *outbuf, gint outlen, const gchar *inbuf)
+static void conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       switch (conv_guess_encoding(inbuf)) {
+       switch (conv_guess_ja_encoding(inbuf)) {
+       case C_ISO_2022_JP:
+               conv_jistoutf8(outbuf, outlen, inbuf);
+               break;
+       case C_SHIFT_JIS:
+               conv_sjistoutf8(outbuf, outlen, inbuf);
+               break;
        case C_EUC_JP:
-               conv_euctojis(outbuf, outlen, inbuf);
+               conv_euctoutf8(outbuf, outlen, inbuf);
                break;
        default:
                strncpy2(outbuf, inbuf, outlen);
@@ -315,63 +467,49 @@ void conv_anytojis(gchar *outbuf, gint outlen, const gchar *inbuf)
        }
 }
 
-void conv_unreadable_eucjp(gchar *str)
+static void conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       register guchar *p = str;
-
-       while (*p != '\0') {
-               if (isascii(*p)) {
-                       /* convert CR+LF -> LF */
-                       if (*p == '\r' && *(p + 1) == '\n')
-                               memmove(p, p + 1, strlen(p));
-                       /* printable 7 bit code */
-                       p++;
-               } else if (iseuckanji(*p)) {
-                       if (iseuckanji(*(p + 1)) && !isunprintableeuckanji(*p))
-                               /* printable euc-jp code */
-                               p += 2;
-                       else {
-                               /* substitute unprintable code */
-                               *p++ = SUBST_CHAR;
-                               if (*p != '\0') {
-                                       if (isascii(*p))
-                                               p++;
-                                       else
-                                               *p++ = SUBST_CHAR;
-                               }
+       static iconv_t cd = (iconv_t)-1;
+       static gboolean iconv_ok = TRUE;
+       gchar *tmpstr;
+
+       if (cd == (iconv_t)-1) {
+               if (!iconv_ok) {
+                       strncpy2(outbuf, inbuf, outlen);
+                       return;
+               }
+               cd = iconv_open(CS_EUC_JP_MS, CS_UTF_8);
+               if (cd == (iconv_t)-1) {
+                       cd = iconv_open(CS_EUC_JP, CS_UTF_8);
+                       if (cd == (iconv_t)-1) {
+                               g_warning("conv_utf8toeuc(): %s\n",
+                                         g_strerror(errno));
+                               iconv_ok = FALSE;
+                               strncpy2(outbuf, inbuf, outlen);
+                               return;
                        }
-               } else if (iseuchwkana1(*p)) {
-                       if (iseuchwkana2(*(p + 1)))
-                               /* euc-jp hankaku kana */
-                               p += 2;
-                       else
-                               *p++ = SUBST_CHAR;
-               } else if (iseucaux(*p)) {
-                       if (iseuckanji(*(p + 1)) && iseuckanji(*(p + 2))) {
-                               /* auxiliary kanji */
-                               p += 3;
-                       } else
-                               *p++ = SUBST_CHAR;
-               } else
-                       /* substitute unprintable 1 byte code */
-                       *p++ = SUBST_CHAR;
+               }
        }
+
+       tmpstr = conv_iconv_strdup_with_cd(inbuf, cd);
+       if (tmpstr) {
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+       } else
+               strncpy2(outbuf, inbuf, outlen);
 }
 
-void conv_unreadable_8bit(gchar *str)
+static void conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       register guchar *p = str;
+       gchar *eucstr;
 
-       while (*p != '\0') {
-               /* convert CR+LF -> LF */
-               if (*p == '\r' && *(p + 1) == '\n')
-                       memmove(p, p + 1, strlen(p));
-               else if (!isascii(*p)) *p = SUBST_CHAR;
-               p++;
-       }
+       Xalloca(eucstr, outlen, return);
+
+       conv_utf8toeuc(eucstr, outlen, inbuf);
+       conv_euctojis(outbuf, outlen, eucstr);
 }
 
-void conv_unreadable_latin(gchar *str)
+static void conv_unreadable_8bit(gchar *str)
 {
        register guchar *p = str;
 
@@ -379,8 +517,7 @@ void conv_unreadable_latin(gchar *str)
                /* convert CR+LF -> LF */
                if (*p == '\r' && *(p + 1) == '\n')
                        memmove(p, p + 1, strlen(p));
-               else if ((*p & 0xff) >= 0x80 && (*p & 0xff) <= 0x9f)
-                       *p = SUBST_CHAR;
+               else if (!IS_ASCII(*p)) *p = SUBST_CHAR;
                p++;
        }
 }
@@ -451,7 +588,7 @@ void conv_mb_alnum(gchar *str)
        }
 }
 
-CharSet conv_guess_encoding(const gchar *str)
+CharSet conv_guess_ja_encoding(const gchar *str)
 {
        const guchar *p = str;
        CharSet guessed = C_US_ASCII;
@@ -461,7 +598,7 @@ CharSet conv_guess_encoding(const gchar *str)
                        if (guessed == C_US_ASCII)
                                return C_ISO_2022_JP;
                        p += 2;
-               } else if (isascii(*p)) {
+               } else if (IS_ASCII(*p)) {
                        p++;
                } else if (iseuckanji(*p) && iseuckanji(*(p + 1))) {
                        if (*p >= 0xfd && *p <= 0xfe)
@@ -493,57 +630,82 @@ CharSet conv_guess_encoding(const gchar *str)
        return guessed;
 }
 
-void conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static void conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       conv_jistoeuc(outbuf, outlen, inbuf);
-       conv_unreadable_eucjp(outbuf);
+       conv_jistoutf8(outbuf, outlen, inbuf);
 }
 
-void conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static void conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       conv_sjistoeuc(outbuf, outlen, inbuf);
-       conv_unreadable_eucjp(outbuf);
+       conv_sjistoutf8(outbuf, outlen, inbuf);
 }
 
-void conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static void conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       strncpy2(outbuf, inbuf, outlen);
-       conv_unreadable_eucjp(outbuf);
+       conv_euctoutf8(outbuf, outlen, inbuf);
 }
 
-void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+void conv_utf8todisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       conv_anytoeuc(outbuf, outlen, inbuf);
-       conv_unreadable_eucjp(outbuf);
+       if (g_utf8_validate(inbuf, -1, NULL) == TRUE)
+               strncpy2(outbuf, inbuf, outlen);
+       else
+               conv_ustodisp(outbuf, outlen, inbuf);
 }
 
-void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+{
+       conv_anytoutf8(outbuf, outlen, inbuf);
+       if (g_utf8_validate(outbuf, -1, NULL) != TRUE)
+               conv_unreadable_8bit(outbuf);
+}
+
+static void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        strncpy2(outbuf, inbuf, outlen);
        conv_unreadable_8bit(outbuf);
 }
 
-void conv_latintodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       strncpy2(outbuf, inbuf, outlen);
-       conv_unreadable_latin(outbuf);
+       gchar *tmpstr;
+
+       tmpstr = conv_iconv_strdup(inbuf, conv_get_locale_charset_str(),
+                                  CS_INTERNAL);
+       if (tmpstr) {
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+       } else
+               conv_utf8todisp(outbuf, outlen, inbuf);
 }
 
-void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
+static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        strncpy2(outbuf, inbuf, outlen);
 }
 
-CodeConverter *conv_code_converter_new(const gchar *charset)
+static const gchar *
+conv_get_fallback_for_private_encoding(const gchar *encoding)
+{
+       if (encoding && (encoding[0] == 'X' || encoding[0] == 'x') &&
+           encoding[1] == '-') {
+               if (!g_ascii_strcasecmp(encoding, CS_X_GBK))
+                       return CS_GBK;
+       }
+
+       return encoding;
+}
+
+CodeConverter *conv_code_converter_new(const gchar *src_charset)
 {
        CodeConverter *conv;
 
+       src_charset = conv_get_fallback_for_private_encoding(src_charset);
+
        conv = g_new0(CodeConverter, 1);
-#if !HAVE_LIBJCONV
-       conv->code_conv_func = conv_get_code_conv_func(charset);
-#endif
-       conv->charset_str = g_strdup(charset);
-       conv->charset = conv_get_charset_from_str(charset);
+       conv->code_conv_func = conv_get_code_conv_func(src_charset, NULL);
+       conv->charset_str = g_strdup(src_charset);
+       conv->charset = conv_get_charset_from_str(src_charset);
 
        return conv;
 }
@@ -557,153 +719,228 @@ void conv_code_converter_destroy(CodeConverter *conv)
 gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
                  const gchar *inbuf)
 {
-#if HAVE_LIBJCONV
-       gchar *str;
-
-       str = conv_codeset_strdup(inbuf, conv->charset_str, NULL);
-       if (!str)
-               return -1;
+       if (conv->code_conv_func != conv_noconv)
+               conv->code_conv_func(outbuf, outlen, inbuf);
        else {
-               strncpy2(outbuf, str, outlen);
-               g_free(str);
+               gchar *str;
+
+               str = conv_iconv_strdup(inbuf, conv->charset_str, NULL);
+               if (!str)
+                       return -1;
+               else {
+                       strncpy2(outbuf, str, outlen);
+                       g_free(str);
+               }
        }
-#else /* !HAVE_LIBJCONV */
-       conv->code_conv_func(outbuf, outlen, inbuf);
-#endif
 
        return 0;
 }
 
 gchar *conv_codeset_strdup(const gchar *inbuf,
-                          const gchar *src_codeset, const gchar *dest_codeset)
+                          const gchar *src_code, const gchar *dest_code)
 {
        gchar *buf;
        size_t len;
-#if HAVE_LIBJCONV
-       gint actual_codeset;
-       const gchar *const *codesets;
-       gint n_codesets;
-#else /* !HAVE_LIBJCONV */
-       CharSet src_charset = C_AUTO, dest_charset = C_AUTO;
-#endif
+       CodeConvFunc conv_func;
 
-       if (!dest_codeset) {
-               CodeConvFunc func;
+       src_code = conv_get_fallback_for_private_encoding(src_code);
+       conv_func = conv_get_code_conv_func(src_code, dest_code);
+       if (conv_func != conv_noconv) {
+               len = (strlen(inbuf) + 1) * 3;
+               buf = g_malloc(len);
+               if (!buf) return NULL;
 
-               func = conv_get_code_conv_func(src_codeset);
-               if (func != conv_noconv) {
-                       if (func == conv_jistodisp ||
-                           func == conv_sjistodisp ||
-                           func == conv_anytodisp)
-                               len = strlen(inbuf) * 2 + 1;
-                       else
-                               len = strlen(inbuf) + 1;
-                       buf = g_malloc(len);
-                       if (!buf) return NULL;
-                       func(buf, len, inbuf);
-                       buf = g_realloc(buf, strlen(buf) + 1);
-                       return buf;
-               }
+               conv_func(buf, len, inbuf);
+               return g_realloc(buf, strlen(buf) + 1);
+       }
+
+       return conv_iconv_strdup(inbuf, src_code, dest_code);
+}
+
+CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
+                                    const gchar *dest_charset_str)
+{
+       CodeConvFunc code_conv = conv_noconv;
+       CharSet src_charset;
+       CharSet dest_charset;
+
+       if (!src_charset_str)
+               src_charset = conv_get_locale_charset();
+       else
+               src_charset = conv_get_charset_from_str(src_charset_str);
+
+       /* auto detection mode */
+       if (!src_charset_str && !dest_charset_str) {
+               if (conv_is_ja_locale())
+                       return conv_anytodisp;
+               else
+                       return conv_noconv;
+       }
+
+       dest_charset = conv_get_charset_from_str(dest_charset_str);
+
+       if (dest_charset == C_US_ASCII)
+               return conv_ustodisp;
+
+       switch (src_charset) {
+       case C_US_ASCII:
+       case C_ISO_8859_1:
+       case C_ISO_8859_2:
+       case C_ISO_8859_3:
+       case C_ISO_8859_4:
+       case C_ISO_8859_5:
+       case C_ISO_8859_6:
+       case C_ISO_8859_7:
+       case C_ISO_8859_8:
+       case C_ISO_8859_9:
+       case C_ISO_8859_10:
+       case C_ISO_8859_11:
+       case C_ISO_8859_13:
+       case C_ISO_8859_14:
+       case C_ISO_8859_15:
+               break;
+       case C_ISO_2022_JP:
+       case C_ISO_2022_JP_2:
+       case C_ISO_2022_JP_3:
+               if (dest_charset == C_AUTO)
+                       code_conv = conv_jistodisp;
+               else if (dest_charset == C_EUC_JP)
+                       code_conv = conv_jistoeuc;
+               else if (dest_charset == C_UTF_8)
+                       code_conv = conv_jistoutf8;
+               break;
+       case C_SHIFT_JIS:
+               if (dest_charset == C_AUTO)
+                       code_conv = conv_sjistodisp;
+               else if (dest_charset == C_EUC_JP)
+                       code_conv = conv_sjistoeuc;
+               else if (dest_charset == C_UTF_8)
+                       code_conv = conv_sjistoutf8;
+               break;
+       case C_EUC_JP:
+               if (dest_charset == C_AUTO)
+                       code_conv = conv_euctodisp;
+               else if (dest_charset == C_ISO_2022_JP   ||
+                        dest_charset == C_ISO_2022_JP_2 ||
+                        dest_charset == C_ISO_2022_JP_3)
+                       code_conv = conv_euctojis;
+               else if (dest_charset == C_UTF_8)
+                       code_conv = conv_euctoutf8;
+               break;
+       case C_UTF_8:
+               if (dest_charset == C_EUC_JP)
+                       code_conv = conv_utf8toeuc;
+               else if (dest_charset == C_ISO_2022_JP   ||
+                        dest_charset == C_ISO_2022_JP_2 ||
+                        dest_charset == C_ISO_2022_JP_3)
+                       code_conv = conv_utf8tojis;
+               break;
+       default:
+               break;
        }
 
+       return code_conv;
+}
+
+gchar *conv_iconv_strdup(const gchar *inbuf,
+                        const gchar *src_code, const gchar *dest_code)
+{
+       iconv_t cd;
+       gchar *outbuf;
+
+       if (!src_code)
+               src_code = conv_get_outgoing_charset_str();
+       if (!dest_code)
+               dest_code = CS_INTERNAL;
+
        /* don't convert if src and dest codeset are identical */
-       if (src_codeset && dest_codeset &&
-           !strcasecmp(src_codeset, dest_codeset))
+       if (!strcasecmp(src_code, dest_code))
                return g_strdup(inbuf);
 
-#if HAVE_LIBJCONV
-       if (src_codeset) {
-               codesets = &src_codeset;
-               n_codesets = 1;
-       } else
-               codesets = jconv_info_get_pref_codesets(&n_codesets);
-       if (!dest_codeset) {
-               dest_codeset = conv_get_current_charset_str();
-               /* don't convert if current codeset is US-ASCII */
-               if (!strcasecmp(dest_codeset, CS_US_ASCII))
-                       return g_strdup(inbuf);
-       }
+       /* don't convert if current codeset is US-ASCII */
+       if (!strcasecmp(dest_code, CS_US_ASCII))
+               return g_strdup(inbuf);
 
-       if (jconv_alloc_conv(inbuf, strlen(inbuf), &buf, &len,
-                            codesets, n_codesets,
-                            &actual_codeset, dest_codeset)
-           == 0)
-               return buf;
-       else {
-#if 0
-               g_warning("code conversion from %s to %s failed\n",
-                         codesets && codesets[0] ? codesets[0] : "(unknown)",
-                         dest_codeset);
-#endif /* 0 */
+       cd = iconv_open(dest_code, src_code);
+       if (cd == (iconv_t)-1)
                return NULL;
-       }
-#else /* !HAVE_LIBJCONV */
-       if (src_codeset) {
-               if (!strcasecmp(src_codeset, CS_EUC_JP) ||
-                   !strcasecmp(src_codeset, CS_EUCJP))
-                       src_charset = C_EUC_JP;
-               else if (!strcasecmp(src_codeset, CS_SHIFT_JIS) ||
-                        !strcasecmp(src_codeset, "SHIFT-JIS") ||
-                        !strcasecmp(src_codeset, "SJIS"))
-                       src_charset = C_SHIFT_JIS;
-               if (dest_codeset && !strcasecmp(dest_codeset, CS_ISO_2022_JP))
-                       dest_charset = C_ISO_2022_JP;
-       }
 
-       if ((src_charset == C_EUC_JP || src_charset == C_SHIFT_JIS) &&
-           dest_charset == C_ISO_2022_JP) {
-               len = (strlen(inbuf) + 1) * 3;
-               buf = g_malloc(len);
-               if (buf) {
-                       if (src_charset == C_EUC_JP)
-                               conv_euctojis(buf, len, inbuf);
-                       else
-                               conv_anytojis(buf, len, inbuf);
-                       buf = g_realloc(buf, strlen(buf) + 1);
-               }
-       } else
-               buf = g_strdup(inbuf);
+       outbuf = conv_iconv_strdup_with_cd(inbuf, cd);
 
-       return buf;
-#endif /* !HAVE_LIBJCONV */
+       iconv_close(cd);
+
+       return outbuf;
 }
 
-CodeConvFunc conv_get_code_conv_func(const gchar *charset)
+gchar *conv_iconv_strdup_with_cd(const gchar *inbuf, iconv_t cd)
 {
-       CodeConvFunc code_conv;
-       CharSet cur_charset;
+       const gchar *inbuf_p;
+       gchar *outbuf;
+       gchar *outbuf_p;
+       size_t in_size;
+       size_t in_left;
+       size_t out_size;
+       size_t out_left;
+       size_t n_conv;
+       size_t len;
 
-       if (!charset) {
-               cur_charset = conv_get_current_charset();
-               if (cur_charset == C_EUC_JP || cur_charset == C_SHIFT_JIS)
-                       return conv_anytodisp;
-               else
-                       return conv_noconv;
+       inbuf_p = inbuf;
+       in_size = strlen(inbuf);
+       in_left = in_size;
+       out_size = (in_size + 1) * 2;
+       outbuf = g_malloc(out_size);
+       outbuf_p = outbuf;
+       out_left = out_size;
+
+#define EXPAND_BUF()                           \
+{                                              \
+       len = outbuf_p - outbuf;                \
+       out_size *= 2;                          \
+       outbuf = g_realloc(outbuf, out_size);   \
+       outbuf_p = outbuf + len;                \
+       out_left = out_size - len;              \
+}
+
+       while ((n_conv = iconv(cd, (ICONV_CONST gchar **)&inbuf_p, &in_left,
+                              &outbuf_p, &out_left)) == (size_t)-1) {
+               if (EILSEQ == errno) {
+                       //g_print("iconv(): at %d: %s\n", in_size - in_left, g_strerror(errno));
+                       inbuf_p++;
+                       in_left--;
+                       if (out_left == 0) {
+                               EXPAND_BUF();
+                       }
+                       *outbuf_p++ = SUBST_CHAR;
+                       out_left--;
+               } else if (EINVAL == errno) {
+                       break;
+               } else if (E2BIG == errno) {
+                       EXPAND_BUF();
+               } else {
+                       g_warning("conv_iconv_strdup(): %s\n",
+                                 g_strerror(errno));
+                       break;
+               }
        }
 
-       if (!strcasecmp(charset, CS_ISO_2022_JP) ||
-           !strcasecmp(charset, CS_ISO_2022_JP_2))
-               code_conv = conv_jistodisp;
-       else if (!strcasecmp(charset, CS_US_ASCII))
-               code_conv = conv_ustodisp;
-       else if (!strncasecmp(charset, CS_ISO_8859_1, 10))
-               code_conv = conv_latintodisp;
-#if !HAVE_LIBJCONV
-       else if (!strncasecmp(charset, "ISO-8859-", 9))
-               code_conv = conv_latintodisp;
-#endif
-       else if (!strcasecmp(charset, CS_SHIFT_JIS) ||
-                !strcasecmp(charset, "SHIFT-JIS")  ||
-                !strcasecmp(charset, "SJIS")       ||
-                !strcasecmp(charset, "X-SJIS"))
-               code_conv = conv_sjistodisp;
-       else if (!strcasecmp(charset, CS_EUC_JP) ||
-                !strcasecmp(charset, CS_EUCJP))
-               code_conv = conv_euctodisp;
-       else
-               code_conv = conv_noconv;
+       while ((n_conv = iconv(cd, NULL, NULL, &outbuf_p, &out_left)) ==
+              (size_t)-1) {
+               if (E2BIG == errno) {
+                       EXPAND_BUF();
+               } else {
+                       g_warning("conv_iconv_strdup(): %s\n",
+                                 g_strerror(errno));
+                       break;
+               }
+       }
 
-       return code_conv;
+#undef EXPAND_BUF
+
+       len = outbuf_p - outbuf;
+       outbuf = g_realloc(outbuf, len + 1);
+       outbuf[len] = '\0';
+
+       return outbuf;
 }
 
 static const struct {
@@ -713,160 +950,316 @@ static const struct {
        {C_US_ASCII,            CS_US_ASCII},
        {C_US_ASCII,            CS_ANSI_X3_4_1968},
        {C_UTF_8,               CS_UTF_8},
+       {C_UTF_7,               CS_UTF_7},
        {C_ISO_8859_1,          CS_ISO_8859_1},
        {C_ISO_8859_2,          CS_ISO_8859_2},
+       {C_ISO_8859_3,          CS_ISO_8859_3},
        {C_ISO_8859_4,          CS_ISO_8859_4},
        {C_ISO_8859_5,          CS_ISO_8859_5},
+       {C_ISO_8859_6,          CS_ISO_8859_6},
        {C_ISO_8859_7,          CS_ISO_8859_7},
        {C_ISO_8859_8,          CS_ISO_8859_8},
        {C_ISO_8859_9,          CS_ISO_8859_9},
+       {C_ISO_8859_10,         CS_ISO_8859_10},
        {C_ISO_8859_11,         CS_ISO_8859_11},
        {C_ISO_8859_13,         CS_ISO_8859_13},
+       {C_ISO_8859_14,         CS_ISO_8859_14},
        {C_ISO_8859_15,         CS_ISO_8859_15},
        {C_BALTIC,              CS_BALTIC},
+       {C_CP1250,              CS_CP1250},
        {C_CP1251,              CS_CP1251},
+       {C_CP1252,              CS_CP1252},
+       {C_CP1253,              CS_CP1253},
+       {C_CP1254,              CS_CP1254},
+       {C_CP1255,              CS_CP1255},
+       {C_CP1256,              CS_CP1256},
+       {C_CP1257,              CS_CP1257},
+       {C_CP1258,              CS_CP1258},
+       {C_WINDOWS_1250,        CS_WINDOWS_1250},
        {C_WINDOWS_1251,        CS_WINDOWS_1251},
+       {C_WINDOWS_1252,        CS_WINDOWS_1252},
+       {C_WINDOWS_1253,        CS_WINDOWS_1253},
+       {C_WINDOWS_1254,        CS_WINDOWS_1254},
+       {C_WINDOWS_1255,        CS_WINDOWS_1255},
+       {C_WINDOWS_1256,        CS_WINDOWS_1256},
+       {C_WINDOWS_1257,        CS_WINDOWS_1257},
+       {C_WINDOWS_1258,        CS_WINDOWS_1258},
        {C_KOI8_R,              CS_KOI8_R},
+       {C_KOI8_T,              CS_KOI8_T},
        {C_KOI8_U,              CS_KOI8_U},
        {C_ISO_2022_JP,         CS_ISO_2022_JP},
        {C_ISO_2022_JP_2,       CS_ISO_2022_JP_2},
+       {C_ISO_2022_JP_3,       CS_ISO_2022_JP_3},
        {C_EUC_JP,              CS_EUC_JP},
        {C_EUC_JP,              CS_EUCJP},
+       {C_EUC_JP_MS,           CS_EUC_JP_MS},
        {C_SHIFT_JIS,           CS_SHIFT_JIS},
+       {C_SHIFT_JIS,           CS_SHIFT__JIS},
+       {C_SHIFT_JIS,           CS_SJIS},
        {C_ISO_2022_KR,         CS_ISO_2022_KR},
        {C_EUC_KR,              CS_EUC_KR},
        {C_ISO_2022_CN,         CS_ISO_2022_CN},
        {C_EUC_CN,              CS_EUC_CN},
        {C_GB2312,              CS_GB2312},
+       {C_GBK,                 CS_GBK},
        {C_EUC_TW,              CS_EUC_TW},
        {C_BIG5,                CS_BIG5},
+       {C_BIG5_HKSCS,          CS_BIG5_HKSCS},
        {C_TIS_620,             CS_TIS_620},
        {C_WINDOWS_874,         CS_WINDOWS_874},
+       {C_GEORGIAN_PS,         CS_GEORGIAN_PS},
+       {C_TCVN5712_1,          CS_TCVN5712_1},
 };
 
-#if !HAVE_LIBJCONV
 static const struct {
        gchar *const locale;
        CharSet charset;
+       CharSet out_charset;
 } locale_table[] = {
-       {"ja_JP.eucJP"  , C_EUC_JP},
-       {"ja_JP.ujis"   , C_EUC_JP},
-       {"ja_JP.EUC"    , C_EUC_JP},
-       {"ja_JP.SJIS"   , C_SHIFT_JIS},
-       {"ja_JP.JIS"    , C_ISO_2022_JP},
-       {"ja_JP"        , C_EUC_JP},
-       {"ko_KR"        , C_EUC_KR},
-       {"zh_CN.GB2312" , C_GB2312},
-       {"zh_CN"        , C_GB2312},
-       {"zh_TW.eucTW"  , C_EUC_TW},
-       {"zh_TW.Big5"   , C_BIG5},
-       {"zh_TW"        , C_BIG5},
-
-       {"ru_RU.KOI8-R" , C_KOI8_R},
-       {"ru_RU.CP1251" , C_WINDOWS_1251},
-
-       {"bg_BG"        , C_WINDOWS_1251},
-
-       {"en_US"        , C_ISO_8859_1},
-       {"ca_ES"        , C_ISO_8859_1},
-       {"da_DK"        , C_ISO_8859_1},
-       {"de_DE"        , C_ISO_8859_1},
-       {"nl_NL"        , C_ISO_8859_1},
-       {"et_EE"        , C_ISO_8859_1},
-       {"fi_FI"        , C_ISO_8859_1},
-       {"fr_FR"        , C_ISO_8859_1},
-       {"is_IS"        , C_ISO_8859_1},
-       {"it_IT"        , C_ISO_8859_1},
-       {"no_NO"        , C_ISO_8859_1},
-       {"pt_PT"        , C_ISO_8859_1},
-       {"pt_BR"        , C_ISO_8859_1},
-       {"es_ES"        , C_ISO_8859_1},
-       {"sv_SE"        , C_ISO_8859_1},
-
-       {"hr_HR"        , C_ISO_8859_2},
-       {"hu_HU"        , C_ISO_8859_2},
-       {"pl_PL"        , C_ISO_8859_2},
-       {"ro_RO"        , C_ISO_8859_2},
-       {"sk_SK"        , C_ISO_8859_2},
-       {"sl_SI"        , C_ISO_8859_2},
-       {"ru_RU"        , C_ISO_8859_5},
-       {"el_GR"        , C_ISO_8859_7},
-       {"iw_IL"        , C_ISO_8859_8},
-       {"tr_TR"        , C_ISO_8859_9},
-
-       {"th_TH"        , C_TIS_620},
+       {"ja_JP.eucJP"  , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP.EUC-JP" , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP.EUC"    , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP.ujis"   , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP.SJIS"   , C_SHIFT_JIS   , C_ISO_2022_JP},
+       {"ja_JP.JIS"    , C_ISO_2022_JP , C_ISO_2022_JP},
+       {"ja_JP"        , C_EUC_JP      , C_ISO_2022_JP},
+       {"ko_KR.EUC-KR" , C_EUC_KR      , C_EUC_KR},
+       {"ko_KR"        , C_EUC_KR      , C_EUC_KR},
+       {"zh_CN.GB2312" , C_GB2312      , C_GB2312},
+       {"zh_CN.GBK"    , C_GBK         , C_GBK},
+       {"zh_CN"        , C_GB2312      , C_GB2312},
+       {"zh_HK"        , C_BIG5_HKSCS  , C_BIG5_HKSCS},
+       {"zh_TW.eucTW"  , C_EUC_TW      , C_BIG5},
+       {"zh_TW.EUC-TW" , C_EUC_TW      , C_BIG5},
+       {"zh_TW.Big5"   , C_BIG5        , C_BIG5},
+       {"zh_TW"        , C_BIG5        , C_BIG5},
+
+       {"ru_RU.KOI8-R" , C_KOI8_R      , C_KOI8_R},
+       {"ru_RU.KOI8R"  , C_KOI8_R      , C_KOI8_R},
+       {"ru_RU.CP1251" , C_WINDOWS_1251, C_KOI8_R},
+       {"ru_RU"        , C_ISO_8859_5  , C_KOI8_R},
+       {"tg_TJ"        , C_KOI8_T      , C_KOI8_T},
+       {"ru_UA"        , C_KOI8_U      , C_KOI8_U},
+       {"uk_UA.CP1251" , C_WINDOWS_1251, C_KOI8_U},
+       {"uk_UA"        , C_KOI8_U      , C_KOI8_U},
+
+       {"be_BY"        , C_WINDOWS_1251, C_WINDOWS_1251},
+       {"bg_BG"        , C_WINDOWS_1251, C_WINDOWS_1251},
+
+       {"yi_US"        , C_WINDOWS_1255, C_WINDOWS_1255},
+
+       {"af_ZA"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"br_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"ca_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"da_DK"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_AT"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_CH"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_DE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_LU"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_AU"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_BW"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_CA"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_DK"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_GB"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_HK"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_IE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_NZ"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_PH"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_SG"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_US"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_ZA"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_ZW"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_AR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_BO"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_CL"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_CO"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_CR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_DO"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_EC"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_GT"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_HN"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_MX"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_NI"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PA"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PY"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_SV"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_US"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_UY"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_VE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"et_EE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"eu_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fi_FI"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fo_FO"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_CA"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_CH"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_LU"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"ga_IE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"gl_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"gv_GB"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"id_ID"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"is_IS"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"it_CH"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"it_IT"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"kl_GL"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"kw_GB"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"ms_MY"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"nl_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"nl_NL"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"nn_NO"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"no_NO"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"oc_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"pt_BR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"pt_PT"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"sq_AL"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"sv_FI"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"sv_SE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"tl_PH"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"uz_UZ"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"wa_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
+
+       {"bs_BA"        , C_ISO_8859_2  , C_ISO_8859_2},
+       {"cs_CZ"        , C_ISO_8859_2  , C_ISO_8859_2},
+       {"hr_HR"        , C_ISO_8859_2  , C_ISO_8859_2},
+       {"hu_HU"        , C_ISO_8859_2  , C_ISO_8859_2},
+       {"pl_PL"        , C_ISO_8859_2  , C_ISO_8859_2},
+       {"ro_RO"        , C_ISO_8859_2  , C_ISO_8859_2},
+       {"sk_SK"        , C_ISO_8859_2  , C_ISO_8859_2},
+       {"sl_SI"        , C_ISO_8859_2  , C_ISO_8859_2},
+
+       {"sr_YU@cyrillic"       , C_ISO_8859_5  , C_ISO_8859_5},
+       {"sr_YU"                , C_ISO_8859_2  , C_ISO_8859_2},
+
+       {"mt_MT"                , C_ISO_8859_3  , C_ISO_8859_3},
+
+       {"lt_LT.iso88594"       , C_ISO_8859_4  , C_ISO_8859_4},
+       {"lt_LT.ISO8859-4"      , C_ISO_8859_4  , C_ISO_8859_4},
+       {"lt_LT.ISO_8859-4"     , C_ISO_8859_4  , C_ISO_8859_4},
+       {"lt_LT"                , C_ISO_8859_13 , C_ISO_8859_13},
+
+       {"mk_MK"        , C_ISO_8859_5  , C_ISO_8859_5},
+
+       {"ar_AE"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_BH"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_DZ"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_EG"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_IQ"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_JO"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_KW"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_LB"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_LY"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_MA"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_OM"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_QA"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_SA"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_SD"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_SY"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_TN"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_YE"        , C_ISO_8859_6  , C_ISO_8859_6},
+
+       {"el_GR"        , C_ISO_8859_7  , C_ISO_8859_7},
+       {"he_IL"        , C_ISO_8859_8  , C_ISO_8859_8},
+       {"iw_IL"        , C_ISO_8859_8  , C_ISO_8859_8},
+       {"tr_TR"        , C_ISO_8859_9  , C_ISO_8859_9},
+
+       {"lv_LV"        , C_ISO_8859_13 , C_ISO_8859_13},
+       {"mi_NZ"        , C_ISO_8859_13 , C_ISO_8859_13},
+
+       {"cy_GB"        , C_ISO_8859_14 , C_ISO_8859_14},
+
+       {"ar_IN"        , C_UTF_8       , C_UTF_8},
+       {"en_IN"        , C_UTF_8       , C_UTF_8},
+       {"se_NO"        , C_UTF_8       , C_UTF_8},
+       {"ta_IN"        , C_UTF_8       , C_UTF_8},
+       {"te_IN"        , C_UTF_8       , C_UTF_8},
+       {"ur_PK"        , C_UTF_8       , C_UTF_8},
+
+       {"th_TH"        , C_TIS_620     , C_TIS_620},
        /* {"th_TH"     , C_WINDOWS_874}, */
        /* {"th_TH"     , C_ISO_8859_11}, */
 
-       {"lt_LT.iso88594"       , C_ISO_8859_4},
-       {"lt_LT.ISO8859-4"      , C_ISO_8859_4},
-       {"lt_LT.ISO_8859-4"     , C_ISO_8859_4},
-       {"lt_LT"                , C_ISO_8859_13},
-       {"lv_LV"                , C_ISO_8859_13},
+       {"ka_GE"        , C_GEORGIAN_PS , C_GEORGIAN_PS},
+       {"vi_VN.TCVN"   , C_TCVN5712_1  , C_TCVN5712_1},
 
-       {"C"                    , C_US_ASCII},
-       {"POSIX"                , C_US_ASCII},
-       {"ANSI_X3.4-1968"       , C_US_ASCII},
+       {"C"                    , C_US_ASCII    , C_US_ASCII},
+       {"POSIX"                , C_US_ASCII    , C_US_ASCII},
+       {"ANSI_X3.4-1968"       , C_US_ASCII    , C_US_ASCII},
 };
-#endif /* !HAVE_LIBJCONV */
 
-const gchar *conv_get_charset_str(CharSet charset)
+static GHashTable *conv_get_charset_to_str_table(void)
 {
+       static GHashTable *table;
        gint i;
 
+       if (table)
+               return table;
+
+       table = g_hash_table_new(NULL, g_direct_equal);
+
        for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
-               if (charsets[i].charset == charset)
-                       return charsets[i].name;
+               if (g_hash_table_lookup(table, GUINT_TO_POINTER(charsets[i].charset))
+                   == NULL) {
+                       g_hash_table_insert
+                               (table, GUINT_TO_POINTER(charsets[i].charset),
+                                charsets[i].name);
+               }
        }
 
-       return NULL;
+       return table;
 }
 
-CharSet conv_get_charset_from_str(const gchar *charset)
+static GHashTable *conv_get_charset_from_str_table(void)
 {
+       static GHashTable *table;
        gint i;
 
-       if (!charset) return C_AUTO;
+       if (table)
+               return table;
+
+       table = g_hash_table_new(str_case_hash, str_case_equal);
 
        for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
-               if (!strcasecmp(charsets[i].name, charset))
-                       return charsets[i].charset;
+               g_hash_table_insert(table, charsets[i].name,
+                                   GUINT_TO_POINTER(charsets[i].charset));
        }
 
-       return C_AUTO;
+       return table;
 }
 
-CharSet conv_get_current_charset(void)
+const gchar *conv_get_charset_str(CharSet charset)
 {
-       static CharSet cur_charset = -1;
-       gint i;
+       GHashTable *table;
 
-#if HAVE_LIBJCONV
-       const gchar *cur_codeset;
-#else
+       table = conv_get_charset_to_str_table();
+       return g_hash_table_lookup(table, GUINT_TO_POINTER(charset));
+}
+
+CharSet conv_get_charset_from_str(const gchar *charset)
+{
+       GHashTable *table;
+
+       if (!charset) return C_AUTO;
+
+       table = conv_get_charset_from_str_table();
+       return GPOINTER_TO_UINT(g_hash_table_lookup(table, charset));
+}
+
+CharSet conv_get_locale_charset(void)
+{
+       static CharSet cur_charset = -1;
        const gchar *cur_locale;
-#endif
+       const gchar *p;
+       gint i;
 
        if (cur_charset != -1)
                return cur_charset;
 
-#if HAVE_LIBJCONV
-       cur_codeset = jconv_info_get_current_codeset();
-       for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
-               if (!strcasecmp(cur_codeset, charsets[i].name)) {
-                       cur_charset = charsets[i].charset;
-                       return cur_charset;
-               }
-       }
-#else
-       cur_locale = g_getenv("LC_ALL");
-       if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
-       if (!cur_locale) cur_locale = g_getenv("LANG");
-       if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
-
-       debug_print("current locale: %s\n",
-                   cur_locale ? cur_locale : "(none)");
-
+       cur_locale = conv_get_current_locale();
        if (!cur_locale) {
                cur_charset = C_US_ASCII;
                return cur_charset;
@@ -877,85 +1270,96 @@ CharSet conv_get_current_charset(void)
                return cur_charset;
        }
 
+       if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
+               cur_charset = C_ISO_8859_15;
+               return cur_charset;
+       }
+
        for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
                const gchar *p;
 
-               /* "ja_JP.EUC" matches with "ja_JP.eucJP" and "ja_JP.EUC" */
-               /* "ja_JP" matches with "ja_JP.xxxx" and "ja" */
-               if (!strncasecmp(cur_locale, locale_table[i].locale,
+               /* "ja_JP.EUC" matches with "ja_JP.eucJP", "ja_JP.EUC" and
+                  "ja_JP". "ja_JP" matches with "ja_JP.xxxx" and "ja" */
+               if (!g_ascii_strncasecmp(cur_locale, locale_table[i].locale,
                                 strlen(locale_table[i].locale))) {
                        cur_charset = locale_table[i].charset;
                        return cur_charset;
                } else if ((p = strchr(locale_table[i].locale, '_')) &&
                         !strchr(p + 1, '.')) {
                        if (strlen(cur_locale) == 2 &&
-                           !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
+                           !g_ascii_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
                                cur_charset = locale_table[i].charset;
                                return cur_charset;
                        }
                }
        }
-#endif
 
        cur_charset = C_AUTO;
        return cur_charset;
 }
 
-const gchar *conv_get_current_charset_str(void)
+const gchar *conv_get_locale_charset_str(void)
 {
        static const gchar *codeset = NULL;
 
        if (!codeset)
-               codeset = conv_get_charset_str(conv_get_current_charset());
+               codeset = conv_get_charset_str(conv_get_locale_charset());
+
+       return codeset ? codeset : CS_INTERNAL;
+}
 
-       return codeset ? codeset : "US-ASCII";
+CharSet conv_get_internal_charset(void)
+{
+       return C_INTERNAL;
+}
+
+const gchar *conv_get_internal_charset_str(void)
+{
+       return CS_INTERNAL;
 }
 
 CharSet conv_get_outgoing_charset(void)
 {
        static CharSet out_charset = -1;
-
-#if HAVE_LIBJCONV
-       gint i, j, n_pref_codesets;
-       const gchar *const *pref_codesets;
-#else
-       CharSet cur_charset;
-#endif
+       const gchar *cur_locale;
+       const gchar *p;
+       gint i;
 
        if (out_charset != -1)
                return out_charset;
 
-#if HAVE_LIBJCONV
-       /* skip US-ASCII and UTF-8 */
-       pref_codesets = jconv_info_get_pref_codesets(&n_pref_codesets);
-       for (i = 0; i < n_pref_codesets; i++) {
-               for (j = 3; j < sizeof(charsets) / sizeof(charsets[0]); j++) {
-                       if (!strcasecmp(pref_codesets[i], charsets[j].name)) {
-                               out_charset = charsets[j].charset;
-                               return out_charset;
-                       }
-               }
+       cur_locale = conv_get_current_locale();
+       if (!cur_locale) {
+               out_charset = C_AUTO;
+               return out_charset;
        }
 
-       for (i = 0; i < n_pref_codesets; i++) {
-               if (!strcasecmp(pref_codesets[i], "UTF-8")) {
-                       out_charset = C_UTF_8;
-                       return out_charset;
-               }
+       if (strcasestr(cur_locale, "UTF-8")) {
+               out_charset = C_UTF_8;
+               return out_charset;
        }
 
-       out_charset = C_AUTO;
-#else
-       cur_charset = conv_get_current_charset();
-       switch (cur_charset) {
-       case C_EUC_JP:
-       case C_SHIFT_JIS:
-               out_charset = C_ISO_2022_JP;
-               break;
-       default:
-               out_charset = cur_charset;
+       if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
+               out_charset = C_ISO_8859_15;
+               return out_charset;
+       }
+
+       for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
+               const gchar *p;
+
+               if (!g_ascii_strncasecmp(cur_locale, locale_table[i].locale,
+                                strlen(locale_table[i].locale))) {
+                       out_charset = locale_table[i].out_charset;
+                       break;
+               } else if ((p = strchr(locale_table[i].locale, '_')) &&
+                        !strchr(p + 1, '.')) {
+                       if (strlen(cur_locale) == 2 &&
+                           !g_ascii_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
+                               out_charset = locale_table[i].out_charset;
+                               break;
+                       }
+               }
        }
-#endif
 
        return out_charset;
 }
@@ -965,25 +1369,43 @@ const gchar *conv_get_outgoing_charset_str(void)
        CharSet out_charset;
        const gchar *str;
 
-       if (prefs_common.outgoing_charset) {
-               if (!isalpha(prefs_common.outgoing_charset[0])) {
-                       g_free(prefs_common.outgoing_charset);
-                       prefs_common.outgoing_charset = g_strdup(CS_AUTO);
-               } else if (strcmp(prefs_common.outgoing_charset, CS_AUTO) != 0)
-                       return prefs_common.outgoing_charset;
-       }
-
        out_charset = conv_get_outgoing_charset();
        str = conv_get_charset_str(out_charset);
 
-       return str ? str : "US-ASCII";
+       return str ? str : CS_UTF_8;
+}
+
+gboolean conv_is_multibyte_encoding(CharSet encoding)
+{
+       switch (encoding) {
+       case C_EUC_JP:
+       case C_EUC_JP_MS:
+       case C_EUC_KR:
+       case C_EUC_TW:
+       case C_EUC_CN:
+       case C_ISO_2022_JP:
+       case C_ISO_2022_JP_2:
+       case C_ISO_2022_JP_3:
+       case C_ISO_2022_KR:
+       case C_ISO_2022_CN:
+       case C_SHIFT_JIS:
+       case C_GB2312:
+       case C_GBK:
+       case C_BIG5:
+       case C_UTF_8:
+       case C_UTF_7:
+               return TRUE;
+       default:
+               return FALSE;
+       }
 }
 
 const gchar *conv_get_current_locale(void)
 {
-       gchar *cur_locale;
+       const gchar *cur_locale;
 
        cur_locale = g_getenv("LC_ALL");
+       if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
        if (!cur_locale) cur_locale = g_getenv("LANG");
        if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
 
@@ -993,448 +1415,289 @@ const gchar *conv_get_current_locale(void)
        return cur_locale;
 }
 
-void conv_unmime_header_overwrite(gchar *str)
+gboolean conv_is_ja_locale(void)
 {
-       gchar *buf;
-       gint buflen;
-       CharSet cur_charset;
+       static gint is_ja_locale = -1;
+       const gchar *cur_locale;
 
-       cur_charset = conv_get_current_charset();
+       if (is_ja_locale != -1)
+               return is_ja_locale != 0;
 
-       if (cur_charset == C_EUC_JP) {
-               buflen = strlen(str) * 2 + 1;
-               Xalloca(buf, buflen, return);
-               conv_anytodisp(buf, buflen, str);
-               unmime_header(str, buf);
-       } else {
-               buflen = strlen(str) + 1;
-               Xalloca(buf, buflen, return);
-               unmime_header(buf, str);
-               strncpy2(str, buf, buflen);
+       is_ja_locale = 0;
+       cur_locale = conv_get_current_locale();
+       if (cur_locale) {
+               if (g_ascii_strncasecmp(cur_locale, "ja", 2) == 0)
+                       is_ja_locale = 1;
        }
+
+       return is_ja_locale != 0;
 }
 
-void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
-                       const gchar *charset)
+gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
 {
-       CharSet cur_charset;
-
-       cur_charset = conv_get_current_charset();
-
-       if (cur_charset == C_EUC_JP) {
-               gchar *buf;
-               gint buflen;
+       gchar buf[BUFFSIZE];
 
-               buflen = strlen(str) * 2 + 1;
-               Xalloca(buf, buflen, return);
-               conv_anytodisp(buf, buflen, str);
-               unmime_header(outbuf, buf);
-       } else
-               unmime_header(outbuf, str);
-}
+       if (is_ascii_str(str))
+               return unmime_header(str);
 
-#define MAX_ENCLEN     75
-#define MAX_LINELEN    76
+       if (default_encoding) {
+               gchar *utf8_buf;
 
-#define B64LEN(len)    ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
+               utf8_buf = conv_codeset_strdup
+                       (str, default_encoding, CS_INTERNAL);
+               if (utf8_buf) {
+                       gchar *decoded_str;
 
-#if HAVE_LIBJCONV
-void conv_encode_header(gchar *dest, gint len, const gchar *src,
-                       gint header_len)
-{
-       wchar_t *wsrc;
-       wchar_t *wsrcp;
-       gchar *destp;
-       size_t line_len, mimehdr_len, mimehdr_begin_len;
-       gchar *mimehdr_init = "=?";
-       gchar *mimehdr_end = "?=";
-       gchar *mimehdr_enctype = "?B?";
-       const gchar *mimehdr_charset;
-
-       /* g_print("src = %s\n", src); */
-       mimehdr_charset = conv_get_outgoing_charset_str();
-
-       /* convert to wide-character string */
-       wsrcp = wsrc = strdup_mbstowcs(src);
-
-       mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
-               strlen(mimehdr_charset) + strlen(mimehdr_enctype);
-       mimehdr_begin_len = strlen(mimehdr_init) +
-               strlen(mimehdr_charset) + strlen(mimehdr_enctype);
-       line_len = header_len;
-       destp = dest;
-       *dest = '\0';
-
-       g_return_if_fail(wsrc != NULL);
-
-       while (*wsrcp) {
-               wchar_t *wp, *wtmp, *wtmpp;
-               gint nspc = 0;
-               gboolean str_is_non_ascii;
-
-               /* irresponsible buffer overrun check */
-               if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
-
-               /* encode string including space
-                  if non-ASCII string follows */
-               if (is_next_nonascii(wsrcp)) {
-                       wp = wsrcp;
-                       while ((wp = find_wspace(wp)) != NULL)
-                               if (!is_next_nonascii(wp)) break;
-                       str_is_non_ascii = TRUE;
-               } else {
-                       wp = find_wspace(wsrcp);
-                       str_is_non_ascii = FALSE;
-               }
-
-               if (wp != NULL) {
-                       wtmp = wcsndup(wsrcp, wp - wsrcp);
-                       wsrcp = wp + 1;
-                       while (iswspace(wsrcp[nspc])) nspc++;
-               } else {
-                       wtmp = wcsdup(wsrcp);
-                       wsrcp += wcslen(wsrcp);
+                       decoded_str = unmime_header(utf8_buf);
+                       g_free(utf8_buf);
+                       return decoded_str;
                }
+       }
 
-               wtmpp = wtmp;
-
-               do {
-                       gint tlen = 0;
-                       gchar *tmp; /* internal codeset */
-                       gchar *raw; /* converted, but not base64 encoded */
-                       register gchar *tmpp;
-                       gint raw_len;
-
-                       tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
-                       *tmp = '\0';
-                       raw = g_strdup("");
-                       raw_len = 0;
-
-                       while (*wtmpp != (wchar_t)0) {
-                               gint mbl;
-                               gint dummy;
-                               gchar *raw_new = NULL;
-                               int raw_new_len = 0;
-                               const gchar *src_codeset;
-
-                               mbl = wctomb(tmpp, *wtmpp);
-                               if (mbl == -1) {
-                                       g_warning("invalid wide character\n");
-                                       wtmpp++;
-                                       continue;
-                               }
-                               /* g_free(raw); */
-                               src_codeset = conv_get_current_charset_str();
-                               /* printf ("tmp = %s, tlen = %d, mbl\n",
-                                       tmp, tlen, mbl); */
-                               if (jconv_alloc_conv(tmp, tlen + mbl,
-                                                    &raw_new, &raw_new_len,
-                                                    &src_codeset, 1,
-                                                    &dummy, mimehdr_charset)
-                                   != 0) {
-                                       g_warning("can't convert\n");
-                                       tmpp[0] = '\0';
-                                       wtmpp++;
-                                       continue;
-                               }
-                               if (str_is_non_ascii) {
-                                       gint dlen = mimehdr_len +
-                                               B64LEN(raw_len);
-                                       if ((line_len + dlen +
-                                            (*(wtmpp + 1) ? 0 : nspc) +
-                                            (line_len > 1 ? 1 : 0))
-                                           > MAX_LINELEN) {
-                                               g_free(raw_new);
-                                               if (tlen == 0) {
-                                                       *destp++ = '\n';
-                                                       *destp++ = ' ';
-                                                       line_len = 1;
-                                                       continue;
-                                               } else {
-                                                       *tmpp = '\0';
-                                                       break;
-                                               }
-                                       }
-                               } else if ((line_len + tlen + mbl +
-                                           (*(wtmpp + 1) ? 0 : nspc) +
-                                           (line_len > 1 ? 1 : 0))
-                                          > MAX_LINELEN) {
-                                       g_free(raw_new);
-                                       if (1 + tlen + mbl +
-                                           (*(wtmpp + 1) ? 0 : nspc)
-                                           >= MAX_LINELEN) {
-                                               *tmpp = '\0';
-                                               break;
-                                       }
-                                       *destp++ = '\n';
-                                       *destp++ = ' ';
-                                       line_len = 1;
-                                       continue;
-                               }
-
-                               tmpp += mbl;
-                               *tmpp = '\0';
-
-                               tlen += mbl;
+       if (conv_is_ja_locale())
+               conv_anytodisp(buf, sizeof(buf), str);
+       else
+               conv_localetodisp(buf, sizeof(buf), str);
 
-                               g_free(raw);
-                               raw = raw_new;
-                               raw_len = raw_new_len;
+       return unmime_header(buf);
+}
 
-                               wtmpp++;
-                       }
-                       /* g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
-                               tmp, tlen, mb_seqlen); */
+#define MAX_LINELEN            76
+#define MAX_HARD_LINELEN       996
+#define MIMESEP_BEGIN          "=?"
+#define MIMESEP_END            "?="
+
+#define LBREAK_IF_REQUIRED(cond, is_plain_text)                                \
+{                                                                      \
+       if (len - (destp - (guchar *)dest) < MAX_LINELEN + 2) {         \
+               *destp = '\0';                                          \
+               return;                                                 \
+       }                                                               \
+                                                                       \
+       if ((cond) && *srcp) {                                          \
+               if (destp > (guchar *)dest && left < MAX_LINELEN - 1) { \
+                       if (isspace(*(destp - 1)))                      \
+                               destp--;                                \
+                       else if (is_plain_text && isspace(*srcp))       \
+                               srcp++;                                 \
+                       if (*srcp) {                                    \
+                               *destp++ = '\n';                        \
+                               *destp++ = ' ';                         \
+                               left = MAX_LINELEN - 1;                 \
+                       }                                               \
+               }                                                       \
+       }                                                               \
+}
 
-                       if (tlen == 0 || raw_len == 0) {
-                               g_free(tmp);
-                               g_free(raw);
-                               continue;
-                       }
+void conv_encode_header_full(gchar *dest, gint len, const gchar *src,
+                       gint header_len, gboolean addr_field,
+                       const gchar *out_encoding_)
+{
+       const gchar *cur_encoding;
+       const gchar *out_encoding;
+       gint mimestr_len;
+       gchar *mimesep_enc;
+       gint left;
+       const guchar *srcp = src;
+       guchar *destp = dest;
+       gboolean use_base64;
+
+       g_return_if_fail(g_utf8_validate(src, -1, NULL) == TRUE);
+
+       if (MB_CUR_MAX > 1) {
+               use_base64 = TRUE;
+               mimesep_enc = "?B?";
+       } else {
+               use_base64 = FALSE;
+               mimesep_enc = "?Q?";
+       }
 
-                       if (line_len > 1 && destp > dest) {
-                               *destp++ = ' ';
-                               *destp = '\0';
-                               line_len++;
-                       }
+       cur_encoding = CS_INTERNAL;
 
-                       if (str_is_non_ascii) {
-                               g_snprintf(destp, len - strlen(dest), "%s%s%s",
-                                          mimehdr_init, mimehdr_charset,
-                                          mimehdr_enctype);
-                               destp += mimehdr_begin_len;
-                               line_len += mimehdr_begin_len;
+       if (out_encoding_)
+               out_encoding = out_encoding_;
+       else
+               out_encoding = conv_get_outgoing_charset_str();
 
-                               base64_encode(destp, raw, raw_len);
-                               line_len += strlen(destp);
-                               destp += strlen(destp);
+       if (!strcmp(out_encoding, CS_US_ASCII))
+               out_encoding = CS_ISO_8859_1;
 
-                               strcpy(destp, mimehdr_end);
-                               destp += strlen(mimehdr_end);
-                               line_len += strlen(mimehdr_end);
-                       } else {
-                               strcpy(destp, tmp);
-                               line_len += strlen(destp);
-                               destp += strlen(destp);
-                       }
+       mimestr_len = strlen(MIMESEP_BEGIN) + strlen(out_encoding) +
+               strlen(mimesep_enc) + strlen(MIMESEP_END);
 
-                       g_free(tmp);
-                       g_free(raw);
-                       /* g_print("line_len = %d\n\n", line_len); */
-               } while (*wtmpp != (wchar_t)0);
+       left = MAX_LINELEN - header_len;
 
-               while (iswspace(*wsrcp)) {
-                       gint mbl;
+       while (*srcp) {
+               LBREAK_IF_REQUIRED(left <= 0, TRUE);
 
-                       mbl = wctomb(destp, *wsrcp++);
-                       if (mbl != -1) {
-                               destp += mbl;
-                               line_len += mbl;
-                       }
+               while (isspace(*srcp)) {
+                       *destp++ = *srcp++;
+                       left--;
+                       LBREAK_IF_REQUIRED(left <= 0, TRUE);
                }
-               *destp = '\0';
 
-               g_free(wtmp);
-       }
-
-       g_free(wsrc);
-
-       /* g_print("dest = %s\n", dest); */
-}
-#else /* !HAVE_LIBJCONV */
-
-#define JIS_SEQLEN     3
-
-void conv_encode_header(gchar *dest, gint len, const gchar *src,
-                       gint header_len)
-{
-       wchar_t *wsrc;
-       wchar_t *wsrcp;
-       gchar *destp;
-       size_t line_len, mimehdr_len, mimehdr_begin_len;
-       gchar *mimehdr_init = "=?";
-       gchar *mimehdr_end = "?=";
-       gchar *mimehdr_enctype = "?B?";
-       const gchar *mimehdr_charset;
-
-       /* g_print("src = %s\n", src); */
-       mimehdr_charset = conv_get_outgoing_charset_str();
-       if (strcmp(mimehdr_charset, "ISO-2022-JP") != 0) {
-               /* currently only supports Japanese */
-               strncpy2(dest, src, len);
-               return;
-       }
+               /* output as it is if the next word is ASCII string */
+               if (!is_next_nonascii(srcp)) {
+                       gint word_len;
+
+                       word_len = get_next_word_len(srcp);
+                       LBREAK_IF_REQUIRED(left < word_len, TRUE);
+                       while (word_len > 0) {
+                               LBREAK_IF_REQUIRED(left + (MAX_HARD_LINELEN - MAX_LINELEN) <= 0, TRUE)
+                               *destp++ = *srcp++;
+                               left--;
+                               word_len--;
+                       }
 
-       /* convert to wide-character string */
-       wsrcp = wsrc = strdup_mbstowcs(src);
-
-       mimehdr_len = strlen(mimehdr_init) + strlen(mimehdr_end) +
-                     strlen(mimehdr_charset) + strlen(mimehdr_enctype);
-       mimehdr_begin_len = strlen(mimehdr_init) +
-                           strlen(mimehdr_charset) + strlen(mimehdr_enctype);
-       line_len = header_len;
-       destp = dest;
-       *dest = '\0';
-
-       g_return_if_fail(wsrc != NULL);
-
-       while (*wsrcp) {
-               wchar_t *wp, *wtmp, *wtmpp;
-               gint nspc = 0;
-               gboolean str_is_non_ascii;
-
-               /* irresponsible buffer overrun check */
-               if ((len - (destp - dest)) < (MAX_LINELEN + 1) * 2) break;
-
-               /* encode string including space
-                  if non-ASCII string follows */
-               if (is_next_nonascii(wsrcp)) {
-                       wp = wsrcp;
-                       while ((wp = find_wspace(wp)) != NULL)
-                               if (!is_next_nonascii(wp)) break;
-                       str_is_non_ascii = TRUE;
-               } else {
-                       wp = find_wspace(wsrcp);
-                       str_is_non_ascii = FALSE;
+                       continue;
                }
 
-               if (wp != NULL) {
-                       wtmp = wcsndup(wsrcp, wp - wsrcp);
-                       wsrcp = wp + 1;
-                       while (iswspace(wsrcp[nspc])) nspc++;
-               } else {
-                       wtmp = wcsdup(wsrcp);
-                       wsrcp += wcslen(wsrcp);
+               /* don't include parentheses in encoded strings */
+               if (addr_field && (*srcp == '(' || *srcp == ')')) {
+                       LBREAK_IF_REQUIRED(left < 2, FALSE);
+                       *destp++ = *srcp++;
+                       left--;
                }
 
-               wtmpp = wtmp;
-
-               do {
-                       gint prev_mbl = 1, tlen = 0, mb_seqlen = 0;
-                       gchar *tmp;
-                       register gchar *tmpp;
+               while (1) {
+                       gint mb_len = 0;
+                       gint cur_len = 0;
+                       gchar *part_str;
+                       gchar *out_str;
+                       gchar *enc_str;
+                       const guchar *p = srcp;
+                       gint out_str_len;
+                       gint out_enc_str_len;
+                       gint mime_block_len;
+                       gboolean cont = FALSE;
+
+                       while (*p != '\0') {
+                               if (isspace(*p) && !is_next_nonascii(p + 1))
+                                       break;
+                               /* don't include parentheses in encoded
+                                  strings */
+                               if (addr_field && (*p == '(' || *p == ')'))
+                                       break;
+
+                               mb_len = g_utf8_skip[*p];
+
+                               Xstrndup_a(part_str, srcp, cur_len + mb_len, );
+                               out_str = conv_codeset_strdup
+                                       (part_str, cur_encoding, out_encoding);
+                               if (!out_str) {
+                                       g_warning("conv_encode_header(): code conversion failed\n");
+                                       conv_unreadable_8bit(part_str);
+                                       out_str = g_strdup(part_str);
+                               }
+                               out_str_len = strlen(out_str);
 
-                       tmpp = tmp = g_malloc(wcslen(wtmpp) * MB_CUR_MAX + 1);
-                       *tmp = '\0';
+                               if (use_base64)
+                                       out_enc_str_len = B64LEN(out_str_len);
+                               else
+                                       out_enc_str_len =
+                                               qp_get_q_encoding_len(out_str);
 
-                       while (*wtmpp != (wchar_t)0) {
-                               gint mbl;
+                               g_free(out_str);
 
-                               mbl = wctomb(tmpp, *wtmpp);
-                               if (mbl == -1) {
-                                       g_warning("invalid wide character\n");
-                                       wtmpp++;
+                               if (mimestr_len + out_enc_str_len <= left) {
+                                       cur_len += mb_len;
+                                       p += mb_len;
+                               } else if (cur_len == 0) {
+                                       LBREAK_IF_REQUIRED(1, FALSE);
                                        continue;
+                               } else {
+                                       cont = TRUE;
+                                       break;
                                }
+                       }
 
-                               /* length of KI + KO */
-                               if (prev_mbl == 1 && mbl == 2)
-                                       mb_seqlen += JIS_SEQLEN * 2;
-
-                               if (str_is_non_ascii) {
-                                       gint dlen = mimehdr_len +
-                                               B64LEN(tlen + mb_seqlen + mbl);
-
-                                       if ((line_len + dlen +
-                                            (*(wtmpp + 1) ? 0 : nspc) +
-                                            (line_len > 1 ? 1 : 0))
-                                           > MAX_LINELEN) {
-                                               if (tlen == 0) {
-                                                       *destp++ = '\n';
-                                                       *destp++ = ' ';
-                                                       line_len = 1;
-                                                       mb_seqlen = 0;
-                                                       continue;
-                                               } else {
-                                                       *tmpp = '\0';
-                                                       break;
-                                               }
-                                       }
-                               } else if ((line_len + tlen + mbl +
-                                           (*(wtmpp + 1) ? 0 : nspc) +
-                                           (line_len > 1 ? 1 : 0))
-                                          > MAX_LINELEN) {
-                                       if (1 + tlen + mbl +
-                                           (*(wtmpp + 1) ? 0 : nspc)
-                                           >= MAX_LINELEN) {
-                                               *tmpp = '\0';
-                                               break;
-                                       }
-                                       *destp++ = '\n';
-                                       *destp++ = ' ';
-                                       line_len = 1;
-                                       continue;
+                       if (cur_len > 0) {
+                               Xstrndup_a(part_str, srcp, cur_len, );
+                               out_str = conv_codeset_strdup
+                                       (part_str, cur_encoding, out_encoding);
+                               if (!out_str) {
+                                       g_warning("conv_encode_header(): code conversion failed\n");
+                                       conv_unreadable_8bit(part_str);
+                                       out_str = g_strdup(part_str);
                                }
+                               out_str_len = strlen(out_str);
 
-                               tmpp += mbl;
-                               *tmpp = '\0';
+                               if (use_base64)
+                                       out_enc_str_len = B64LEN(out_str_len);
+                               else
+                                       out_enc_str_len =
+                                               qp_get_q_encoding_len(out_str);
 
-                               tlen += mbl;
-                               prev_mbl = mbl;
+                               Xalloca(enc_str, out_enc_str_len + 1, );
+                               if (use_base64)
+                                       base64_encode(enc_str, out_str, out_str_len);
+                               else
+                                       qp_q_encode(enc_str, out_str);
 
-                               wtmpp++;
-                       }
-                       /* g_print("tmp = %s, tlen = %d, mb_seqlen = %d\n",
-                               tmp, tlen, mb_seqlen); */
+                               g_free(out_str);
 
-                       if (tlen == 0) {
-                               g_free(tmp);
-                               continue;
-                       }
+                               /* output MIME-encoded string block */
+                               mime_block_len = mimestr_len + strlen(enc_str);
+                               g_snprintf(destp, mime_block_len + 1,
+                                          MIMESEP_BEGIN "%s%s%s" MIMESEP_END,
+                                          out_encoding, mimesep_enc, enc_str);
+                               destp += mime_block_len;
+                               srcp += cur_len;
 
-                       if (line_len > 1 && destp > dest) {
-                               *destp++ = ' ';
-                               *destp = '\0';
-                               line_len++;
+                               left -= mime_block_len;
                        }
 
-                       if (str_is_non_ascii) {
-                               gchar *tmp_jis;
-
-                               tmp_jis = g_new(gchar, tlen + mb_seqlen + 1);
-                               conv_euctojis(tmp_jis,
-                                             tlen + mb_seqlen + 1, tmp);
-                               g_snprintf(destp, len - strlen(dest), "%s%s%s",
-                                          mimehdr_init, mimehdr_charset,
-                                          mimehdr_enctype);
-                               destp += mimehdr_begin_len;
-                               line_len += mimehdr_begin_len;
+                       LBREAK_IF_REQUIRED(cont, FALSE);
 
-                               base64_encode(destp, tmp_jis, strlen(tmp_jis));
-                               line_len += strlen(destp);
-                               destp += strlen(destp);
-
-                               strcpy(destp, mimehdr_end);
-                               destp += strlen(mimehdr_end);
-                               line_len += strlen(mimehdr_end);
+                       if (cur_len == 0)
+                               break;
+               }
+       }
 
-                               g_free(tmp_jis);
-                       } else {
-                               strcpy(destp, tmp);
-                               line_len += strlen(destp);
-                               destp += strlen(destp);
-                       }
+       *destp = '\0';
+}
 
-                       g_free(tmp);
-                       /* g_print("line_len = %d\n\n", line_len); */
-               } while (*wtmpp != (wchar_t)0);
+void conv_encode_header(gchar *dest, gint len, const gchar *src,
+                       gint header_len, gboolean addr_field)
+{
+       conv_encode_header_full(dest,len,src,header_len,addr_field,NULL);
+}
 
-               while (iswspace(*wsrcp)) {
-                       gint mbl;
+#undef LBREAK_IF_REQUIRED
+gchar *conv_filename_from_utf8(const gchar *utf8_file)
+{
+       gchar *fs_file;
+       GError *error = NULL;
+
+       fs_file = g_filename_from_utf8(utf8_file, -1, NULL, NULL, &error);
+       if (error) {
+               g_warning("failed to convert encoding of file name: %s\n",
+                         error->message);
+               g_error_free(error);
+       }
+       if (!fs_file)
+               fs_file = g_strdup(utf8_file);
 
-                       mbl = wctomb(destp, *wsrcp++);
-                       if (mbl != -1) {
-                               destp += mbl;
-                               line_len += mbl;
-                       }
-               }
-               *destp = '\0';
+       return fs_file;
+}
 
-               g_free(wtmp);
+gchar *conv_filename_to_utf8(const gchar *fs_file)
+{
+       gchar *utf8_file = NULL;
+       GError *error = NULL;
+
+       utf8_file = g_filename_to_utf8(fs_file, -1, NULL, NULL, &error);
+       if (error) {
+               g_warning("failed to convert encoding of file name: %s\n",
+                         error->message);
+               g_error_free(error);
        }
 
-       g_free(wsrc);
+       if (!utf8_file || !g_utf8_validate(utf8_file, -1, NULL)) {
+               g_free(utf8_file);
+               utf8_file = g_strdup(fs_file);
+               conv_unreadable_8bit(utf8_file);
+       }
 
-       /* g_print("dest = %s\n", dest); */
+       return utf8_file;
 }
-#endif /* HAVE_LIBJCONV */