2011-01-06 [colin] 3.7.8cvs31
[claws.git] / src / codeconv.c
index c634b0dbf330bbbaf84fa7f9585e7cbd2f377713..b1666c0497b2e0ff31cd8de2eb02de2f1974441a 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
  *
  * 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * 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.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #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>
@@ -31,7 +34,6 @@
 #  include <locale.h>
 #endif
 
-#include "intl.h"
 #include "codeconv.h"
 #include "unmime.h"
 #include "base64.h"
 #include "utils.h"
 #include "prefs_common.h"
 
+/* For unknown reasons the inconv.m4 macro undefs that macro if no
+   const is needed.  This would break the code below so we define it. */
+#ifndef ICONV_CONST
+#define ICONV_CONST
+#endif
+
 typedef enum
 {
        JIS_ASCII,
@@ -100,7 +108,51 @@ typedef enum
                state = JIS_AUXKANJI;   \
        }
 
-void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
+static CodeConvFunc conv_get_code_conv_func    (const gchar    *src_charset_str,
+                                        const gchar    *dest_charset_str);
+
+static gchar *conv_iconv_strdup_with_cd        (const gchar    *inbuf,
+                                        iconv_t         cd);
+
+static gchar *conv_iconv_strdup                (const gchar    *inbuf,
+                                        const gchar    *src_code,
+                                        const gchar    *dest_code);
+
+static CharSet conv_get_locale_charset                 (void);
+static CharSet conv_get_outgoing_charset               (void);
+static CharSet conv_guess_ja_encoding(const gchar *str);
+static gboolean conv_is_ja_locale                      (void);
+
+static gint conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static gint conv_jistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_euctoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static gint conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static void conv_unreadable_8bit(gchar *str);
+
+static gint conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static gint conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
+static gint conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf);
+
+static gboolean strict_mode = FALSE;
+
+void codeconv_set_strict(gboolean mode)
+{
+       strict_mode = mode;
+}
+
+static gint conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
        guchar *out = outbuf;
@@ -167,6 +219,7 @@ void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        }
 
        *out = '\0';
+       return 0;
 }
 
 #define JIS_HWDAKUTEN          0x5e
@@ -235,14 +288,14 @@ static gint conv_jis_hantozen(guchar *outbuf, guchar jis_code, guchar sound_sym)
        return 1;
 }
 
-void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint 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)) {
@@ -254,7 +307,7 @@ 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++;
                                }
@@ -290,7 +343,7 @@ void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
                        } else {
                                K_OUT();
                                in++;
-                               if (*in != '\0' && !isascii(*in)) {
+                               if (*in != '\0' && !IS_ASCII(*in)) {
                                        *out++ = SUBST_CHAR;
                                        in++;
                                }
@@ -303,10 +356,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++;
                                        }
@@ -321,15 +374,16 @@ void conv_euctojis(gchar *outbuf, gint outlen, const gchar *inbuf)
 
        K_OUT();
        *out = '\0';
+       return 0;
 }
 
-void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint 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))) {
@@ -352,7 +406,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++;
                                }
@@ -367,327 +421,157 @@ void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        }
 
        *out = '\0';
+       return 0;
 }
 
-void conv_anytoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint 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:
-               strncpy2(outbuf, inbuf, outlen);
-               break;
-       }
+       gchar *eucstr;
+
+       Xalloca(eucstr, outlen, return -1);
+
+       if (conv_jistoeuc(eucstr, outlen, inbuf) <0)
+               return -1;
+       if (conv_euctoutf8(outbuf, outlen, eucstr) < 0)
+               return -1;
+       return 0;
 }
 
-void conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_sjistoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       gchar *tmpstr = NULL;
+       gchar *tmpstr;
 
-       switch (conv_guess_ja_encoding(inbuf)) {
-       case C_ISO_2022_JP:
-               tmpstr = conv_codeset_strdup(inbuf, CS_ISO_2022_JP, CS_UTF_8);
+       tmpstr = conv_iconv_strdup(inbuf, CS_SHIFT_JIS, CS_UTF_8);
+       if (tmpstr) {
                strncpy2(outbuf, tmpstr, outlen);
                g_free(tmpstr);
-               break;
-       case C_SHIFT_JIS:
-               tmpstr = conv_codeset_strdup(inbuf, CS_SHIFT_JIS, CS_UTF_8);
-               strncpy2(outbuf, tmpstr, outlen);
-               g_free(tmpstr);
-               break;
-       case C_EUC_JP:
-               tmpstr = conv_codeset_strdup(inbuf, CS_EUC_JP, CS_UTF_8);
+               return 0;
+       } else {
+               strncpy2(outbuf, inbuf, outlen);
+               return -1;
+       }
+}
+
+static gint 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 -1;
+               }
+               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 -1;
+                       }
+               }
+       }
+
+       tmpstr = conv_iconv_strdup_with_cd(inbuf, cd);
+       if (tmpstr) {
                strncpy2(outbuf, tmpstr, outlen);
                g_free(tmpstr);
-               break;
-       default:
+               return 0;
+       } else {
                strncpy2(outbuf, inbuf, outlen);
-               break;
+               return -1;
        }
 }
 
-void conv_anytojis(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
+       gint r = -1;
        switch (conv_guess_ja_encoding(inbuf)) {
+       case C_ISO_2022_JP:
+               r = conv_jistoutf8(outbuf, outlen, inbuf);
+               break;
+       case C_SHIFT_JIS:
+               r = conv_sjistoutf8(outbuf, outlen, inbuf);
+               break;
        case C_EUC_JP:
-               conv_euctojis(outbuf, outlen, inbuf);
+               r = conv_euctoutf8(outbuf, outlen, inbuf);
                break;
        default:
+               r = 0;
                strncpy2(outbuf, inbuf, outlen);
                break;
        }
+       
+       return r;
 }
 
-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)
-{
-       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 <= 0xfe)
-                       return FALSE;
-       } else if (c1 == 0xf4) {
-               if (c2 >= 0xa7 && c2 <= 0xfe)
-                       return FALSE;
-       }
-
-       return TRUE;
-}
-
-void conv_unreadable_eucjp(gchar *str)
+static gint conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       register guchar *p = str;
+       static iconv_t cd = (iconv_t)-1;
+       static gboolean iconv_ok = TRUE;
+       gchar *tmpstr;
 
-       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;
-                               }
+       if (cd == (iconv_t)-1) {
+               if (!iconv_ok) {
+                       strncpy2(outbuf, inbuf, outlen);
+                       return -1;
+               }
+               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 -1;
                        }
-               } 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;
+               }
        }
-}
-
-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++;
+       tmpstr = conv_iconv_strdup_with_cd(inbuf, cd);
+       if (tmpstr) {
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+               return 0;
+       } else {
+               strncpy2(outbuf, inbuf, outlen);
+               return -1;
        }
 }
 
-void conv_unreadable_latin(gchar *str)
+static gint 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 ((*p & 0xff) >= 0x7f && (*p & 0xff) <= 0x9f)
-                       *p = SUBST_CHAR;
-               p++;
-       }
+       Xalloca(eucstr, outlen, return -1);
+
+       if (conv_utf8toeuc(eucstr, outlen, inbuf) < 0)
+               return -1;
+       if (conv_euctojis(outbuf, outlen, eucstr) < 0)
+               return -1;
+               
+       return 0;
 }
 
-void conv_unreadable_utf8(gchar *str)
+static 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 (((*p & 0xff) >= 0x7f && (*p & 0xff) <= 0x9f) 
-                        || *p == 0xfc)
-                       *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;
-       case C_UTF_8:
-               conv_unreadable_utf8(str);
-               break;
-       default:
-               break;
-       }
-}
-
-#define NCV    '\0'
-
-void conv_mb_alnum(gchar *str)
-{
-       static guchar char_tbl[] = {
-               /* 0xa0 - 0xaf */
-               NCV, ' ', NCV, NCV, ',', '.', NCV, ':',
-               ';', '?', '!', NCV, NCV, NCV, NCV, NCV,
-               /* 0xb0 - 0xbf */
-               NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
-               NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
-               /* 0xc0 - 0xcf */
-               NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
-               NCV, NCV, '(', ')', NCV, NCV, '[', ']',
-               /* 0xd0 - 0xdf */
-               '{', '}', NCV, NCV, NCV, NCV, NCV, NCV,
-               NCV, NCV, NCV, NCV, '+', '-', NCV, NCV,
-               /* 0xe0 - 0xef */
-               NCV, '=', NCV, '<', '>', NCV, NCV, NCV,
-               NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV
-       };
-
-       register guchar *p = str;
-       register gint len;
-
-       len = strlen(str);
-
-       while (len > 1) {
-               if (*p == 0xa3) {
-                       register guchar ch = *(p + 1);
-
-                       if (ch >= 0xb0 && ch <= 0xfa) {
-                               /* [a-zA-Z] */
-                               *p = ch & 0x7f;
-                               p++;
-                               len--;
-                               memmove(p, p + 1, len);
-                               len--;
-                       } else  {
-                               p += 2;
-                               len -= 2;
-                       }
-               } else if (*p == 0xa1) {
-                       register guchar ch = *(p + 1);
-
-                       if (ch >= 0xa0 && ch <= 0xef &&
-                           NCV != char_tbl[ch - 0xa0]) {
-                               *p = char_tbl[ch - 0xa0];
-                               p++;
-                               len--;
-                               memmove(p, p + 1, len);
-                               len--;
-                       } else {
-                               p += 2;
-                               len -= 2;
-                       }
-               } else if (iseuckanji(*p)) {
-                       p += 2;
-                       len -= 2;
-               } else {
-                       p++;
-                       len--;
-               }
-       }
-}
-
-CharSet conv_guess_ja_encoding(const gchar *str)
+static CharSet conv_guess_ja_encoding(const gchar *str)
 {
        const guchar *p = str;
        CharSet guessed = C_US_ASCII;
@@ -697,7 +581,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)
@@ -729,63 +613,105 @@ CharSet conv_guess_ja_encoding(const gchar *str)
        return guessed;
 }
 
-void conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       conv_jistoeuc(outbuf, outlen, inbuf);
-       conv_unreadable_eucjp(outbuf);
+       return conv_jistoutf8(outbuf, outlen, inbuf);
 }
 
-void conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       conv_sjistoeuc(outbuf, outlen, inbuf);
-       conv_unreadable_eucjp(outbuf);
+       return conv_sjistoutf8(outbuf, outlen, inbuf);
 }
 
-void conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       strncpy2(outbuf, inbuf, outlen);
-       conv_unreadable_eucjp(outbuf);
+       return 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_anytoutf8(outbuf, outlen, inbuf);
+       if (g_utf8_validate(inbuf, -1, NULL) == TRUE)
+               strncpy2(outbuf, inbuf, outlen);
+       else
+               conv_ustodisp(outbuf, outlen, inbuf);
 }
 
-#warning FIXME_GTK2
-void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       strncpy2(outbuf, inbuf, outlen);
-       conv_unreadable_8bit(outbuf);
+       gint r = 0;
+       if (conv_anytoutf8(outbuf, outlen, inbuf) < 0)
+               r = -1;
+       if (g_utf8_validate(outbuf, -1, NULL) != TRUE)
+               conv_unreadable_8bit(outbuf);
+       return r;
 }
 
-#warning FIXME_GTK2
-void conv_latintodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        strncpy2(outbuf, inbuf, outlen);
-       conv_unreadable_latin(outbuf);
+       conv_unreadable_8bit(outbuf);
+       
+       return 0;
 }
 
-#warning FIXME_GTK2
 void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       strncpy2(outbuf, inbuf, outlen);
-       conv_unreadable_locale(outbuf);
+       gchar *tmpstr;
+
+       codeconv_set_strict(TRUE);
+       tmpstr = conv_iconv_strdup(inbuf, conv_get_locale_charset_str(),
+                                  CS_INTERNAL);
+       codeconv_set_strict(FALSE);
+       if (tmpstr && g_utf8_validate(tmpstr, -1, NULL)) {
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+               return;
+       } else if (tmpstr && !g_utf8_validate(tmpstr, -1, NULL)) {
+               g_free(tmpstr);
+               codeconv_set_strict(TRUE);
+               tmpstr = conv_iconv_strdup(inbuf, 
+                               conv_get_locale_charset_str_no_utf8(),
+                               CS_INTERNAL);
+               codeconv_set_strict(FALSE);
+       }
+       if (tmpstr && g_utf8_validate(tmpstr, -1, NULL)) {
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+               return;
+       } else {
+               g_free(tmpstr);
+               conv_utf8todisp(outbuf, outlen, inbuf);
+       }
 }
 
-void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
+static gint conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        strncpy2(outbuf, inbuf, outlen);
+       return 0;
+}
+
+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 *charset)
+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, CS_UTF_8);
-       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;
 }
@@ -800,11 +726,11 @@ gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
                  const gchar *inbuf)
 {
        if (conv->code_conv_func != conv_noconv)
-               conv->code_conv_func(outbuf, outlen, inbuf);
+               return conv->code_conv_func(outbuf, outlen, inbuf);
        else {
                gchar *str;
 
-               str = conv_iconv_strdup(inbuf, conv->charset_str, CS_UTF_8);
+               str = conv_iconv_strdup(inbuf, conv->charset_str, NULL);
                if (!str)
                        return -1;
                else {
@@ -823,20 +749,31 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        size_t len;
        CodeConvFunc conv_func;
 
+       if (!strcmp2(src_code, dest_code))
+               return g_strdup(inbuf);
+
+       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_ustodisp && strict_mode && !is_ascii_str(inbuf))
+               return NULL;
+
        if (conv_func != conv_noconv) {
                len = (strlen(inbuf) + 1) * 3;
                buf = g_malloc(len);
                if (!buf) return NULL;
 
-               conv_func(buf, len, inbuf);
-               return g_realloc(buf, strlen(buf) + 1);
+               if (conv_func(buf, len, inbuf) == 0 || !strict_mode)
+                       return g_realloc(buf, strlen(buf) + 1);
+               else {
+                       g_free(buf);
+                       return NULL;
+               }
        }
 
        return conv_iconv_strdup(inbuf, src_code, dest_code);
 }
 
-CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
+static CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
                                     const gchar *dest_charset_str)
 {
        CodeConvFunc code_conv = conv_noconv;
@@ -844,13 +781,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;
@@ -860,25 +797,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:
-       case C_ISO_2022_JP_3:
-               if (dest_charset == C_AUTO &&
-                   conv_get_current_charset() == C_EUC_JP)
-                       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:
@@ -893,26 +814,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:
-               if (dest_charset == C_AUTO &&
-                   (conv_get_current_charset() == src_charset ||
-                    MB_CUR_MAX > 1))
-                       code_conv = conv_latintodisp;
+               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 &&
-                   conv_get_current_charset() == C_EUC_JP)
+               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 &&
-                   conv_get_current_charset() == 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;
@@ -921,38 +858,117 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
        return code_conv;
 }
 
-gchar *conv_iconv_strdup(const gchar *inbuf,
-                        const gchar *isrc_code, const gchar *idest_code)
+static gchar *conv_iconv_strdup(const gchar *inbuf,
+                        const gchar *src_code, const gchar *dest_code)
 {
-       /* presumably GLib 2's function handles the conversion details,
-        * whether iconv is sitting below, or something else */
+       iconv_t cd;
        gchar *outbuf;
-       gsize read_len, written_len;
-       gchar *src_code = (char *)conv_get_outgoing_charset_str();
-       gchar *dest_code = (char *)conv_get_current_charset_str();
-       
-       if (isrc_code)
-               src_code = (char *)isrc_code;
-       if (idest_code)
-               dest_code = (char *)idest_code;
 
-       /* don't convert if current codeset is US-ASCII */
-       if (!strcasecmp(dest_code, CS_US_ASCII))
-               return g_strdup(inbuf);
+       if (!src_code && !dest_code && 
+           g_utf8_validate(inbuf, -1, NULL))
+               return g_strdup(inbuf);
+
+       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 (!strcasecmp(src_code, dest_code))
                return g_strdup(inbuf);
 
-       /* FIXME: unchecked inbuf? Can't see at this level. */
-       outbuf = g_convert(inbuf, strlen(inbuf), dest_code, src_code,
-                          &read_len, &written_len, NULL);
+       /* don't convert if dest codeset is US-ASCII */
+       if (!strcasecmp(src_code, CS_US_ASCII))
+               return g_strdup(inbuf);
 
-       if (outbuf == NULL)
-               g_warning(_("Valid locale type set? (Currently: %s to %s)\n"),
-                         src_code, dest_code);
-       
-       return outbuf;                     
+       /* don't convert if dest 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;
+       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) {
+                       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) {
+                               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;
+               }
+       }
+
+       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;
+               }
+       }
+
+#undef EXPAND_BUF
+
+       len = outbuf_p - outbuf;
+       outbuf = g_realloc(outbuf, len + 1);
+       outbuf[len] = '\0';
+
+       return outbuf;
 }
 
 static const struct {
@@ -1004,6 +1020,7 @@ static const struct {
        {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},
@@ -1011,6 +1028,7 @@ static const struct {
        {C_EUC_KR,              CS_EUC_KR},
        {C_ISO_2022_CN,         CS_ISO_2022_CN},
        {C_EUC_CN,              CS_EUC_CN},
+       {C_GB18030,             CS_GB18030},
        {C_GB2312,              CS_GB2312},
        {C_GBK,                 CS_GBK},
        {C_EUC_TW,              CS_EUC_TW},
@@ -1033,12 +1051,17 @@ 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.GB18030"        , C_GB18030     , C_GB18030},
        {"zh_CN.GB2312" , C_GB2312      , C_GB2312},
-       {"zh_CN.GBK"    , C_GBK         , C_GB2312},
-       {"zh_CN"        , C_GB2312      , C_GB2312},
+       {"zh_CN.GBK"    , C_GBK         , C_GBK},
+       {"zh_CN"        , C_GB18030     , C_GB18030},
        {"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},
@@ -1122,6 +1145,7 @@ static const struct {
        {"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},
+       {"nb_NO"        , 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},
@@ -1260,7 +1284,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)
+static CharSet conv_get_locale_charset(void)
 {
        static CharSet cur_charset = -1;
        const gchar *cur_locale;
@@ -1276,7 +1300,8 @@ CharSet conv_get_current_charset(void)
                return cur_charset;
        }
 
-       if (strcasestr(cur_locale, "UTF-8")) {
+       if (strcasestr(cur_locale, ".UTF-8") ||
+           strcasestr(cur_locale, ".utf8")) {
                cur_charset = C_UTF_8;
                return cur_charset;
        }
@@ -1291,14 +1316,68 @@ 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 &&
+                           !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;
+}
+
+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 (prefs_common.broken_are_utf8)
+               return conv_get_locale_charset();
+
+       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 &&
-                           !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;
                        }
@@ -1309,17 +1388,27 @@ CharSet conv_get_current_charset(void)
        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_US_ASCII;
+       return codeset ? codeset : CS_INTERNAL;
 }
 
-CharSet conv_get_outgoing_charset(void)
+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;
+}
+
+static CharSet conv_get_outgoing_charset(void)
 {
        static CharSet out_charset = -1;
        const gchar *cur_locale;
@@ -1335,6 +1424,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;
@@ -1343,14 +1437,14 @@ 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;
                        }
@@ -1365,127 +1459,77 @@ const gchar *conv_get_outgoing_charset_str(void)
        CharSet out_charset;
        const gchar *str;
 
-       if (prefs_common.outgoing_charset) {
-               if (!isalpha((guchar)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;
-}
-
-gboolean conv_is_multibyte_encoding(CharSet encoding)
-{
-       switch (encoding) {
-       case C_EUC_JP:
-       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_BIG5:
-       case C_UTF_8:
-       case C_UTF_7:
-               return TRUE;
-       default:
-               return FALSE;
-       }
+       return str ? str : CS_UTF_8;
 }
 
 const gchar *conv_get_current_locale(void)
 {
-       static const gchar *cur_locale = NULL;
+       const gchar *cur_locale;
 
-       if (cur_locale != NULL)
-               return cur_locale;
-       
+#ifdef G_OS_WIN32
+       cur_locale = g_win32_getlocale();
+#else
        cur_locale = g_getenv("LC_ALL");
-       if (!cur_locale || !strlen(cur_locale)) 
-               cur_locale = g_getenv("LC_CTYPE");
-       if (!cur_locale || !strlen(cur_locale)) 
-               cur_locale = g_getenv("LANG");
-       if (!cur_locale || !strlen(cur_locale)) 
-               cur_locale = setlocale(LC_CTYPE, NULL);
-
-       if (cur_locale && strlen(cur_locale)) {
-               gchar *tmp = g_strdup(cur_locale);
-               cur_locale = g_strdup(tmp);
-               g_free(tmp);
-       }
+       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)");
 
        return cur_locale;
 }
 
-void conv_unmime_header_overwrite(gchar *str)
+static gboolean conv_is_ja_locale(void)
 {
-       gchar *buf;
-       gint buflen;
-       CharSet cur_charset;
-       const gchar *locale;
-
-       g_return_if_fail(str != NULL);
-       
-       cur_charset = conv_get_current_charset();
+       static gint is_ja_locale = -1;
+       const gchar *cur_locale;
 
-#warning FIXME_GTK2
-/* Should we always ensure to convert? */
-       locale = conv_get_current_locale();
+       if (is_ja_locale != -1)
+               return is_ja_locale != 0;
 
-       if (locale && !strncasecmp(locale, "ja", 2)) {
-               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,
+                          gboolean addr_field)
 {
-       const gchar *locale;
+       gchar buf[BUFFSIZE];
 
-       memset(outbuf, 0, outlen);
-       
-#warning FIXME_GTK2
-/* Should we always ensure to convert? */
-       locale = conv_get_current_locale();
-
-       if (locale && !strncasecmp(locale, "ja", 2)) {
-               gchar *buf;
-               gint buflen;
-
-               buflen = strlen(str) * 2 + 1;
-               Xalloca(buf, buflen, return);
-               conv_anytodisp(buf, buflen, str);
-               unmime_header(outbuf, buf);
-       } else {
-               gchar *tmp;
-               unmime_header(outbuf, str);
-               if (outbuf && !g_utf8_validate(outbuf, -1, NULL)) {
-                       tmp = conv_codeset_strdup(outbuf,
-                                       conv_get_current_charset_str(),
-                                       CS_UTF_8);
-                       if (tmp) {
-                               strncpy(outbuf, tmp, outlen-1);
-                               g_free(tmp);
-                       }
+       if (is_ascii_str(str))
+               return unmime_header(str, addr_field);
+
+       if (default_encoding) {
+               gchar *utf8_buf;
+
+               utf8_buf = conv_codeset_strdup
+                       (str, default_encoding, CS_INTERNAL);
+               if (utf8_buf) {
+                       gchar *decoded_str;
+
+                       decoded_str = unmime_header(utf8_buf, addr_field);
+                       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, addr_field);
 }
 
 #define MAX_LINELEN            76
@@ -1493,8 +1537,6 @@ 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 - (guchar *)dest) < MAX_LINELEN + 2) {         \
@@ -1513,12 +1555,23 @@ void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
                                *destp++ = ' ';                         \
                                left = MAX_LINELEN - 1;                 \
                        }                                               \
+               } else if (destp == (guchar *)dest && left < 7) {       \
+                       if (isspace(*(destp - 1)))                      \
+                               destp--;                                \
+                       else if (is_plain_text && isspace(*srcp))       \
+                               srcp++;                                 \
+                       if (*srcp) {                                    \
+                               *destp++ = '\n';                        \
+                               *destp++ = ' ';                         \
+                               left = MAX_LINELEN - 1;                 \
+                       }                                               \
                }                                                       \
        }                                                               \
 }
 
-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;
@@ -1528,8 +1581,10 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
        const guchar *srcp = src;
        guchar *destp = dest;
        gboolean use_base64;
-       gchar *testbuf;
-       
+
+       cm_return_if_fail(g_utf8_validate(src, -1, NULL) == TRUE);
+       cm_return_if_fail(destp != NULL);
+
        if (MB_CUR_MAX > 1) {
                use_base64 = TRUE;
                mimesep_enc = "?B?";
@@ -1538,19 +1593,16 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                mimesep_enc = "?Q?";
        }
 
-       cur_encoding = CS_UTF_8; /* gtk2 */
+       cur_encoding = CS_INTERNAL;
+
+       if (out_encoding_)
+               out_encoding = out_encoding_;
+       else
+               out_encoding = conv_get_outgoing_charset_str();
 
-       out_encoding = conv_get_outgoing_charset_str();
        if (!strcmp(out_encoding, CS_US_ASCII))
                out_encoding = CS_ISO_8859_1;
 
-       testbuf = conv_codeset_strdup(src, cur_encoding, out_encoding);
-       
-       if (testbuf != NULL) 
-               g_free(testbuf);
-       else
-               out_encoding = CS_UTF_8;
-       
        mimestr_len = strlen(MIMESEP_BEGIN) + strlen(out_encoding) +
                strlen(mimesep_enc) + strlen(MIMESEP_END);
 
@@ -1581,8 +1633,8 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                        continue;
                }
 
-               /* don't include parentheses in encoded strings */
-               if (addr_field && (*srcp == '(' || *srcp == ')')) {
+               /* don't include parentheses and quotes in encoded strings */
+               if (addr_field && (*srcp == '(' || *srcp == ')' || *srcp == '"')) {
                        LBREAK_IF_REQUIRED(left < 2, FALSE);
                        *destp++ = *srcp++;
                        left--;
@@ -1605,25 +1657,23 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                        break;
                                /* don't include parentheses in encoded
                                   strings */
-                               if (addr_field && (*p == '(' || *p == ')'))
+                               if (addr_field && (*p == '(' || *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);
 
@@ -1693,4 +1743,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) {
+               debug_print("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;
+}