2005-09-27 [colin] 1.9.14cvs56
[claws.git] / src / codeconv.c
index 50990d8cf282046e8fd3170ad17f2a87f4337d9c..7827f042fb6026e9fae9b68e09615185b3720500 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 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
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
 #endif
 
+#include "defs.h"
+
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
 #  include <locale.h>
 #endif
 
-#if HAVE_ICONV
-#  include <iconv.h>
-#endif
+#include <iconv.h>
 
-#include "intl.h"
 #include "codeconv.h"
 #include "unmime.h"
 #include "base64.h"
@@ -51,7 +51,7 @@ typedef enum
        JIS_AUXKANJI
 } JISState;
 
-#define SUBST_CHAR     '_'
+#define SUBST_CHAR     0x5f;
 #define ESC            '\033'
 
 #define iseuckanji(c) \
@@ -104,7 +104,36 @@ 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 gboolean strict_mode = FALSE;
+
+void codeconv_set_strict(gboolean mode)
+{
+       strict_mode = mode;
+}
+
+static void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
        guchar *out = outbuf;
@@ -173,14 +202,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)) {
@@ -192,19 +287,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++;
                                }
@@ -217,10 +336,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++;
                                        }
@@ -237,13 +356,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))) {
@@ -266,7 +385,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++;
                                }
@@ -283,26 +402,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_ja_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_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);
@@ -310,145 +474,49 @@ void conv_anytojis(gchar *outbuf, gint outlen, const gchar *inbuf)
        }
 }
 
-static gchar valid_eucjp_tbl[][96] = {
-       /* 0xa2a0 - 0xa2ff */
-       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 0,
-         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 1, 1, 1, 1, 1, 1,
-         1, 1, 0, 0, 0, 0, 0, 0,  0, 0, 1, 1, 1, 1, 1, 1,
-         1, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0,
-         0, 0, 1, 1, 1, 1, 1, 1,  1, 1, 0, 0, 0, 0, 1, 0 },
-
-       /* 0xa3a0 - 0xa3ff */
-       { 0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 0, 0, 0, 0, 0, 0,
-         0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0,
-         0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 0, 0, 0, 0, 0 },
-
-       /* 0xa4a0 - 0xa4ff */
-       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 },
-
-       /* 0xa5a0 - 0xa5ff */
-       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 0,  0, 0, 0, 0, 0, 0, 0, 0 },
-
-       /* 0xa6a0 - 0xa6ff */
-       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 0, 0, 0, 0, 0, 0, 0,
-         0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 0, 0, 0, 0, 0, 0, 0,
-         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
-         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 },
-
-       /* 0xa7a0 - 0xa7ff */
-       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
-         0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 },
-
-       /* 0xa8a0 - 0xa8ff */
-       { 0, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1,
-         1, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
-         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
-         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0,
-         0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }
-};
-
-static gboolean isprintableeuckanji(guchar c1, guchar c2)
+static void conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       if (c1 <= 0xa0 || c1 >= 0xf5)
-               return FALSE;
-       if (c2 <= 0xa0 || c2 == 0xff)
-               return FALSE;
-
-       if (c1 >= 0xa9 && c1 <= 0xaf)
-               return FALSE;
-
-       if (c1 >= 0xa2 && c1 <= 0xa8)
-               return (gboolean)valid_eucjp_tbl[c1 - 0xa2][c2 - 0xa0];
-
-       if (c1 == 0xcf) {
-               if (c2 >= 0xd4 && c2 <= 0xff)
-                       return FALSE;
-       } else if (c1 == 0xf4) {
-               if (c2 >= 0xa7 && c2 <= 0xff)
-                       return FALSE;
+       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;
+                       }
+               }
        }
 
-       return TRUE;
+       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_eucjp(gchar *str)
+static void conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       register guchar *p = str;
+       gchar *eucstr;
 
-       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 (isprintableeuckanji(*p, *(p + 1))) {
-                               /* printable euc-jp code */
-                               p += 2;
-                       } else {
-                               /* substitute unprintable code */
-                               *p++ = SUBST_CHAR;
-                               if (*p != '\0') {
-                                       if (isascii(*p))
-                                               p++;
-                                       else
-                                               *p++ = SUBST_CHAR;
-                               }
-                       }
-               } 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;
-       }
-}
+       Xalloca(eucstr, outlen, return);
 
-void conv_unreadable_8bit(gchar *str)
-{
-       register guchar *p = str;
-
-       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++;
-       }
+       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;
 
@@ -456,40 +524,11 @@ 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) >= 0x7f && (*p & 0xff) <= 0x9f)
-                       *p = SUBST_CHAR;
+               else if (!IS_ASCII(*p)) *p = SUBST_CHAR;
                p++;
        }
 }
 
-void conv_unreadable_locale(gchar *str)
-{
-       switch (conv_get_current_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:
-               conv_unreadable_latin(str);
-               break;
-       case C_EUC_JP:
-               conv_unreadable_eucjp(str);
-               break;
-       default:
-               break;
-       }
-}
-
 #define NCV    '\0'
 
 void conv_mb_alnum(gchar *str)
@@ -566,7 +605,7 @@ CharSet conv_guess_ja_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)
@@ -598,61 +637,85 @@ CharSet conv_guess_ja_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)
 {
-       strncpy2(outbuf, inbuf, outlen);
-       conv_unreadable_8bit(outbuf);
+       conv_anytoutf8(outbuf, outlen, inbuf);
+       if (g_utf8_validate(outbuf, -1, NULL) != TRUE)
+               conv_unreadable_8bit(outbuf);
 }
 
-void conv_latintodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        strncpy2(outbuf, inbuf, outlen);
-       conv_unreadable_latin(outbuf);
+       conv_unreadable_8bit(outbuf);
 }
 
 void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       strncpy2(outbuf, inbuf, outlen);
-       conv_unreadable_locale(outbuf);
+       gchar *tmpstr;
+
+       tmpstr = conv_iconv_strdup(inbuf, conv_get_locale_charset_str(),
+                                  CS_INTERNAL);
+       if (tmpstr && g_utf8_validate(tmpstr, -1, NULL)) {
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+       } else if (tmpstr && !g_utf8_validate(tmpstr, -1, NULL)) {
+               g_free(tmpstr);
+               conv_utf8todisp(outbuf, outlen, inbuf);
+       } 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);
-       conv->code_conv_func = conv_get_code_conv_func(charset, NULL);
-       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;
 }
@@ -666,7 +729,6 @@ void conv_code_converter_destroy(CodeConverter *conv)
 gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
                  const gchar *inbuf)
 {
-#if HAVE_ICONV
        if (conv->code_conv_func != conv_noconv)
                conv->code_conv_func(outbuf, outlen, inbuf);
        else {
@@ -680,9 +742,6 @@ gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
                        g_free(str);
                }
        }
-#else /* !HAVE_ICONV */
-       conv->code_conv_func(outbuf, outlen, inbuf);
-#endif
 
        return 0;
 }
@@ -694,6 +753,7 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        size_t len;
        CodeConvFunc conv_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;
@@ -704,11 +764,7 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
                return g_realloc(buf, strlen(buf) + 1);
        }
 
-#if HAVE_ICONV
        return conv_iconv_strdup(inbuf, src_code, dest_code);
-#else
-       return g_strdup(inbuf);
-#endif /* HAVE_ICONV */
 }
 
 CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
@@ -719,13 +775,13 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
        CharSet dest_charset;
 
        if (!src_charset_str)
-               src_charset = conv_get_current_charset();
+               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 (src_charset == C_EUC_JP || src_charset == C_SHIFT_JIS)
+               if (conv_is_ja_locale())
                        return conv_anytodisp;
                else
                        return conv_noconv;
@@ -735,23 +791,9 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
 
        if (dest_charset == C_US_ASCII)
                return conv_ustodisp;
-       else if (dest_charset == C_UTF_8 ||
-                (dest_charset == C_AUTO &&
-                 conv_get_current_charset() == C_UTF_8))
-               return conv_noconv;
 
        switch (src_charset) {
-       case C_ISO_2022_JP:
-       case C_ISO_2022_JP_2:
-               if (dest_charset == C_AUTO)
-                       code_conv = conv_jistodisp;
-               else if (dest_charset == C_EUC_JP)
-                       code_conv = conv_jistoeuc;
-               break;
        case C_US_ASCII:
-               if (dest_charset == C_AUTO)
-                       code_conv = conv_ustodisp;
-               break;
        case C_ISO_8859_1:
        case C_ISO_8859_2:
        case C_ISO_8859_3:
@@ -766,21 +808,42 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
        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_latintodisp;
+                       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)
+               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;
@@ -789,38 +852,48 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
        return code_conv;
 }
 
-#if HAVE_ICONV
 gchar *conv_iconv_strdup(const gchar *inbuf,
                         const gchar *src_code, const gchar *dest_code)
 {
        iconv_t cd;
-       const gchar *inbuf_p;
        gchar *outbuf;
-       gchar *outbuf_p;
-       gint in_size;
-       gint in_left;
-       gint out_size;
-       gint out_left;
-       gint n_conv;
-       gint len;
 
        if (!src_code)
                src_code = conv_get_outgoing_charset_str();
        if (!dest_code)
-               dest_code = conv_get_current_charset_str();
-
-       /* don't convert if current codeset is US-ASCII */
-       if (!strcasecmp(dest_code, CS_US_ASCII))
-               return g_strdup(inbuf);
+               dest_code = CS_INTERNAL;
 
        /* don't convert if src and dest codeset are identical */
        if (!strcasecmp(src_code, dest_code))
                return g_strdup(inbuf);
 
+       /* don't convert if current codeset is US-ASCII */
+       if (!strcasecmp(dest_code, CS_US_ASCII))
+               return g_strdup(inbuf);
+
        cd = iconv_open(dest_code, src_code);
        if (cd == (iconv_t)-1)
                return NULL;
 
+       outbuf = conv_iconv_strdup_with_cd(inbuf, cd);
+
+       iconv_close(cd);
+
+       return outbuf;
+}
+
+gchar *conv_iconv_strdup_with_cd(const gchar *inbuf, iconv_t cd)
+{
+       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;
+
        inbuf_p = inbuf;
        in_size = strlen(inbuf);
        in_left = in_size;
@@ -839,8 +912,13 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
 }
 
        while ((n_conv = iconv(cd, (ICONV_CONST gchar **)&inbuf_p, &in_left,
-                              &outbuf_p, &out_left)) < 0) {
+                              &outbuf_p, &out_left)) == (size_t)-1) {
                if (EILSEQ == errno) {
+                       if (strict_mode) {
+                               g_free(outbuf);
+                               return NULL;
+                       }
+                       //g_print("iconv(): at %d: %s\n", in_size - in_left, g_strerror(errno));
                        inbuf_p++;
                        in_left--;
                        if (out_left == 0) {
@@ -859,7 +937,8 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
                }
        }
 
-       while ((n_conv = iconv(cd, NULL, NULL, &outbuf_p, &out_left)) < 0) {
+       while ((n_conv = iconv(cd, NULL, NULL, &outbuf_p, &out_left)) ==
+              (size_t)-1) {
                if (E2BIG == errno) {
                        EXPAND_BUF();
                } else {
@@ -875,11 +954,8 @@ gchar *conv_iconv_strdup(const gchar *inbuf,
        outbuf = g_realloc(outbuf, len + 1);
        outbuf[len] = '\0';
 
-       iconv_close(cd);
-
        return outbuf;
 }
-#endif /* HAVE_ICONV */
 
 static const struct {
        CharSet charset;
@@ -927,8 +1003,10 @@ static const struct {
        {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},
@@ -958,11 +1036,15 @@ static const struct {
        {"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},
+#ifdef G_OS_WIN32
+       {"ja_JP"        , C_SHIFT_JIS   , C_ISO_2022_JP},
+#else
        {"ja_JP"        , C_EUC_JP      , C_ISO_2022_JP},
+#endif
        {"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_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},
@@ -976,6 +1058,7 @@ static const struct {
        {"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},
@@ -1184,7 +1267,7 @@ CharSet conv_get_charset_from_str(const gchar *charset)
        return GPOINTER_TO_UINT(g_hash_table_lookup(table, charset));
 }
 
-CharSet conv_get_current_charset(void)
+CharSet conv_get_locale_charset(void)
 {
        static CharSet cur_charset = -1;
        const gchar *cur_locale;
@@ -1215,14 +1298,14 @@ CharSet conv_get_current_charset(void)
 
                /* "ja_JP.EUC" matches with "ja_JP.eucJP", "ja_JP.EUC" and
                   "ja_JP". "ja_JP" matches with "ja_JP.xxxx" and "ja" */
-               if (!strncasecmp(cur_locale, locale_table[i].locale,
+               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;
                        }
@@ -1233,14 +1316,85 @@ CharSet conv_get_current_charset(void)
        return cur_charset;
 }
 
-const gchar *conv_get_current_charset_str(void)
+static CharSet conv_get_locale_charset_no_utf8(void)
+{
+       static CharSet cur_charset = -1;
+       const gchar *cur_locale;
+       const gchar *p;
+       gchar *tmp;
+       gint i;
+
+       if (cur_charset != -1)
+               return cur_charset;
+
+       cur_locale = conv_get_current_locale();
+       if (!cur_locale) {
+               cur_charset = C_US_ASCII;
+               return cur_charset;
+       }
+
+       if (strcasestr(cur_locale, "UTF-8")) {
+               tmp = g_strdup(cur_locale);
+               *(strcasestr(tmp, ".UTF-8")) = '\0';
+               cur_locale = tmp;
+       }
+
+       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", "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 &&
+                           !g_ascii_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
+                               cur_charset = locale_table[i].charset;
+                               return cur_charset;
+                       }
+               }
+       }
+
+       cur_charset = C_AUTO;
+       return cur_charset;
+}
+
+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_US_ASCII;
+       return codeset ? codeset : CS_INTERNAL;
+}
+
+const gchar *conv_get_locale_charset_str_no_utf8(void)
+{
+       static const gchar *codeset = NULL;
+
+       if (!codeset)
+               codeset = conv_get_charset_str(conv_get_locale_charset_no_utf8());
+
+       return codeset ? codeset : CS_INTERNAL;
+}
+
+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)
@@ -1259,6 +1413,11 @@ CharSet conv_get_outgoing_charset(void)
                return out_charset;
        }
 
+       if (strcasestr(cur_locale, "UTF-8")) {
+               out_charset = C_UTF_8;
+               return out_charset;
+       }
+
        if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
                out_charset = C_ISO_8859_15;
                return out_charset;
@@ -1267,29 +1426,20 @@ CharSet conv_get_outgoing_charset(void)
        for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
                const gchar *p;
 
-               if (!strncasecmp(cur_locale, locale_table[i].locale,
+               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 &&
-                           !strncasecmp(cur_locale, locale_table[i].locale, 2)) {
+                           !g_ascii_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
                                out_charset = locale_table[i].out_charset;
                                break;
                        }
                }
        }
 
-#if !HAVE_ICONV
-       /* encoding conversion without iconv() is only supported
-          on Japanese locale for now */
-       if (out_charset == C_ISO_2022_JP)
-               return out_charset;
-       else
-               return conv_get_current_charset();
-#endif
-
        return out_charset;
 }
 
@@ -1298,33 +1448,28 @@ 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 : CS_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:
@@ -1336,12 +1481,16 @@ gboolean conv_is_multibyte_encoding(CharSet encoding)
 
 const gchar *conv_get_current_locale(void)
 {
-       gchar *cur_locale;
+       const gchar *cur_locale;
 
+#ifdef G_OS_WIN32
+       cur_locale = g_win32_getlocale();
+#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);
+#endif /* G_OS_WIN32 */
 
        debug_print("current locale: %s\n",
                    cur_locale ? cur_locale : "(none)");
@@ -1349,44 +1498,51 @@ 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;
+       gchar buf[BUFFSIZE];
 
-       cur_charset = conv_get_current_charset();
+       if (is_ascii_str(str))
+               return unmime_header(str);
 
-       if (cur_charset == C_EUC_JP) {
-               gchar *buf;
-               gint buflen;
+       if (default_encoding) {
+               gchar *utf8_buf;
 
-               buflen = strlen(str) * 2 + 1;
-               Xalloca(buf, buflen, return);
-               conv_anytodisp(buf, buflen, str);
-               unmime_header(outbuf, buf);
-       } else
-               unmime_header(outbuf, str);
+               utf8_buf = conv_codeset_strdup
+                       (str, default_encoding, CS_INTERNAL);
+               if (utf8_buf) {
+                       gchar *decoded_str;
+
+                       decoded_str = unmime_header(utf8_buf);
+                       g_free(utf8_buf);
+                       return decoded_str;
+               }
+       }
+
+       if (conv_is_ja_locale())
+               conv_anytodisp(buf, sizeof(buf), str);
+       else
+               conv_localetodisp(buf, sizeof(buf), str);
+
+       return unmime_header(buf);
 }
 
 #define MAX_LINELEN            76
@@ -1394,17 +1550,15 @@ void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
 #define MIMESEP_BEGIN          "=?"
 #define MIMESEP_END            "?="
 
-#define B64LEN(len)    ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
-
 #define LBREAK_IF_REQUIRED(cond, is_plain_text)                                \
 {                                                                      \
-       if (len - (destp - dest) < MAX_LINELEN + 2) {                   \
+       if (len - (destp - (guchar *)dest) < MAX_LINELEN + 2) {         \
                *destp = '\0';                                          \
                return;                                                 \
        }                                                               \
                                                                        \
        if ((cond) && *srcp) {                                          \
-               if (destp > dest && left < MAX_LINELEN - 1) {           \
+               if (destp > (guchar *)dest && left < MAX_LINELEN - 1) { \
                        if (isspace(*(destp - 1)))                      \
                                destp--;                                \
                        else if (is_plain_text && isspace(*srcp))       \
@@ -1418,18 +1572,21 @@ void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
        }                                                               \
 }
 
-void conv_encode_header(gchar *dest, gint len, const gchar *src,
-                       gint header_len, gboolean addr_field)
+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 gchar *srcp = src;
-       gchar *destp = dest;
+       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?";
@@ -1438,10 +1595,13 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                mimesep_enc = "?Q?";
        }
 
-       cur_encoding = conv_get_current_charset_str();
-       if (!strcmp(cur_encoding, CS_US_ASCII))
-               cur_encoding = CS_ISO_8859_1;
-       out_encoding = conv_get_outgoing_charset_str();
+       cur_encoding = CS_INTERNAL;
+
+       if (out_encoding_)
+               out_encoding = out_encoding_;
+       else
+               out_encoding = conv_get_outgoing_charset_str();
+
        if (!strcmp(out_encoding, CS_US_ASCII))
                out_encoding = CS_ISO_8859_1;
 
@@ -1488,7 +1648,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                        gchar *part_str;
                        gchar *out_str;
                        gchar *enc_str;
-                       const gchar *p = srcp;
+                       const guchar *p = srcp;
                        gint out_str_len;
                        gint out_enc_str_len;
                        gint mime_block_len;
@@ -1502,22 +1662,20 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                if (addr_field && (*p == '(' || *p == ')'))
                                        break;
 
-                               if (MB_CUR_MAX > 1) {
-                                       mb_len = mblen(p, MB_CUR_MAX);
-                                       if (mb_len < 0) {
-                                               g_warning("conv_encode_header(): invalid multibyte character encountered\n");
-                                               mb_len = 1;
-                                       }
-                               } else
-                                       mb_len = 1;
+                               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);
+                                       if (strict_mode) {
+                                               *dest = '\0';
+                                               return;
+                                       } else {
+                                               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);
 
@@ -1587,4 +1745,47 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
        *destp = '\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);
+}
+
 #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);
+
+       return fs_file;
+}
+
+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);
+       }
+
+       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);
+       }
+
+       return utf8_file;
+}