inital gtk2 patch
[claws.git] / src / codeconv.c
index 1b9dfcffcdd7e01635fba41917d04d433d205d3e..53e6e827631e4add5fb700c636b66dc7f02743a4 100644 (file)
 #include <string.h>
 #include <ctype.h>
 #include <stdlib.h>
+#include <errno.h>
 
 #if HAVE_LOCALE_H
 #  include <locale.h>
 #endif
 
-#if HAVE_LIBJCONV
-#  include <jconv.h>
+#if HAVE_ICONV
+#  include <iconv.h>
 #endif
 
 #include "intl.h"
@@ -61,8 +62,6 @@ typedef enum
        (((c) & 0xff) >= 0xa1 && ((c) & 0xff) <= 0xdf)
 #define iseucaux(c) \
        (((c) & 0xff) == 0x8f)
-#define isunprintableeuckanji(c) \
-       (((c) & 0xff) >= 0xa9 && ((c) & 0xff) <= 0xaf)
 #define issjiskanji1(c) \
        ((((c) & 0xff) >= 0x81 && ((c) & 0xff) <= 0x9f) || \
         (((c) & 0xff) >= 0xe0 && ((c) & 0xff) <= 0xfc))
@@ -286,7 +285,7 @@ void conv_sjistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 
 void conv_anytoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       switch (conv_guess_encoding(inbuf)) {
+       switch (conv_guess_ja_encoding(inbuf)) {
        case C_ISO_2022_JP:
                conv_jistoeuc(outbuf, outlen, inbuf);
                break;
@@ -299,9 +298,35 @@ void conv_anytoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
        }
 }
 
+void conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf)
+{
+       gchar *tmpstr = NULL;
+
+       switch (conv_guess_ja_encoding(inbuf)) {
+       case C_ISO_2022_JP:
+               tmpstr = conv_codeset_strdup(inbuf, CS_ISO_2022_JP, CS_UTF_8);
+               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);
+               strncpy2(outbuf, tmpstr, outlen);
+               g_free(tmpstr);
+               break;
+       default:
+               strncpy2(outbuf, inbuf, outlen);
+               break;
+       }
+}
+
 void conv_anytojis(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
-       switch (conv_guess_encoding(inbuf)) {
+       switch (conv_guess_ja_encoding(inbuf)) {
        case C_EUC_JP:
                conv_euctojis(outbuf, outlen, inbuf);
                break;
@@ -311,6 +336,88 @@ 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)
+{
+       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)
 {
        register guchar *p = str;
@@ -323,10 +430,10 @@ void conv_unreadable_eucjp(gchar *str)
                        /* printable 7 bit code */
                        p++;
                } else if (iseuckanji(*p)) {
-                       if (iseuckanji(*(p + 1)) && !isunprintableeuckanji(*p))
+                       if (isprintableeuckanji(*p, *(p + 1))) {
                                /* printable euc-jp code */
                                p += 2;
-                       else {
+                       else {
                                /* substitute unprintable code */
                                *p++ = SUBST_CHAR;
                                if (*p != '\0') {
@@ -375,12 +482,40 @@ void conv_unreadable_latin(gchar *str)
                /* convert CR+LF -> LF */
                if (*p == '\r' && *(p + 1) == '\n')
                        memmove(p, p + 1, strlen(p));
-               else if ((*p & 0xff) >= 0x80 && (*p & 0xff) <= 0x9f)
+               else if ((*p & 0xff) >= 0x7f && (*p & 0xff) <= 0x9f)
                        *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)
@@ -447,7 +582,7 @@ void conv_mb_alnum(gchar *str)
        }
 }
 
-CharSet conv_guess_encoding(const gchar *str)
+CharSet conv_guess_ja_encoding(const gchar *str)
 {
        const guchar *p = str;
        CharSet guessed = C_US_ASCII;
@@ -507,11 +642,19 @@ void conv_euctodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
        conv_unreadable_eucjp(outbuf);
 }
 
+#warning FIXME_GTK2
+#if 0
 void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        conv_anytoeuc(outbuf, outlen, inbuf);
        conv_unreadable_eucjp(outbuf);
 }
+#else
+void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+{
+       conv_anytoutf8(outbuf, outlen, inbuf);
+}
+#endif
 
 void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
@@ -525,6 +668,12 @@ void conv_latintodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
        conv_unreadable_latin(outbuf);
 }
 
+void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
+{
+       strncpy2(outbuf, inbuf, outlen);
+       conv_unreadable_locale(outbuf);
+}
+
 void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        strncpy2(outbuf, inbuf, outlen);
@@ -535,7 +684,8 @@ CodeConverter *conv_code_converter_new(const gchar *charset)
        CodeConverter *conv;
 
        conv = g_new0(CodeConverter, 1);
-       conv->code_conv_func = conv_get_code_conv_func(charset, NULL);
+#warning FIXME_GTK2
+       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);
 
@@ -551,13 +701,14 @@ void conv_code_converter_destroy(CodeConverter *conv)
 gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
                  const gchar *inbuf)
 {
-#if HAVE_LIBJCONV
+#if HAVE_ICONV
        if (conv->code_conv_func != conv_noconv)
                conv->code_conv_func(outbuf, outlen, inbuf);
        else {
                gchar *str;
 
-               str = conv_codeset_strdup(inbuf, conv->charset_str, NULL);
+#warning FIXME_GTK2
+               str = conv_iconv_strdup(inbuf, conv->charset_str, CS_UTF_8);
                if (!str)
                        return -1;
                else {
@@ -565,7 +716,7 @@ gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
                        g_free(str);
                }
        }
-#else /* !HAVE_LIBJCONV */
+#else /* !HAVE_ICONV */
        conv->code_conv_func(outbuf, outlen, inbuf);
 #endif
 
@@ -573,18 +724,13 @@ gint conv_convert(CodeConverter *conv, gchar *outbuf, gint outlen,
 }
 
 gchar *conv_codeset_strdup(const gchar *inbuf,
-                          const gchar *src_codeset, const gchar *dest_codeset)
+                          const gchar *src_code, const gchar *dest_code)
 {
        gchar *buf;
        size_t len;
        CodeConvFunc conv_func;
-#if HAVE_LIBJCONV
-       gint actual_codeset;
-       const gchar *const *codesets;
-       gint n_codesets;
-#endif
 
-       conv_func = conv_get_code_conv_func(src_codeset, dest_codeset);
+       conv_func = conv_get_code_conv_func(src_code, dest_code);
        if (conv_func != conv_noconv) {
                len = (strlen(inbuf) + 1) * 3;
                buf = g_malloc(len);
@@ -594,40 +740,11 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
                return g_realloc(buf, strlen(buf) + 1);
        }
 
-#if !HAVE_LIBJCONV
-       return g_strdup(inbuf);
+#if HAVE_ICONV
+       return conv_iconv_strdup(inbuf, src_code, dest_code);
 #else
-       /* don't convert if src and dest codeset are identical */
-       if (src_codeset && dest_codeset &&
-           !strcasecmp(src_codeset, dest_codeset))
-               return g_strdup(inbuf);
-
-       if (src_codeset) {
-               codesets = &src_codeset;
-               n_codesets = 1;
-       } else
-               codesets = jconv_info_get_pref_codesets(&n_codesets);
-       if (!dest_codeset) {
-               dest_codeset = conv_get_current_charset_str();
-               /* don't convert if current codeset is US-ASCII */
-               if (!strcasecmp(dest_codeset, CS_US_ASCII))
-                       return g_strdup(inbuf);
-       }
-
-       if (jconv_alloc_conv(inbuf, strlen(inbuf), &buf, &len,
-                            codesets, n_codesets,
-                            &actual_codeset, dest_codeset)
-           == 0)
-               return buf;
-       else {
-#if 0
-               g_warning("code conversion from %s to %s failed\n",
-                         codesets && codesets[0] ? codesets[0] : "(unknown)",
-                         dest_codeset);
-#endif /* 0 */
-               return NULL;
-       }
-#endif /* HAVE_LIBJCONV */
+       return g_strdup(inbuf);
+#endif /* HAVE_ICONV */
 }
 
 CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
@@ -652,10 +769,18 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
 
        dest_charset = conv_get_charset_from_str(dest_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)
+               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;
@@ -665,28 +790,32 @@ CodeConvFunc conv_get_code_conv_func(const gchar *src_charset_str,
                        code_conv = conv_ustodisp;
                break;
        case C_ISO_8859_1:
-#if !HAVE_LIBJCONV
        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:
-#endif
                if (dest_charset == C_AUTO)
                        code_conv = conv_latintodisp;
                break;
        case C_SHIFT_JIS:
-               if (dest_charset == C_AUTO)
+               if (dest_charset == C_AUTO &&
+                   conv_get_current_charset() == C_EUC_JP)
                        code_conv = conv_sjistodisp;
                else if (dest_charset == C_EUC_JP)
                        code_conv = conv_sjistoeuc;
                break;
        case C_EUC_JP:
-               if (dest_charset == C_AUTO)
+               if (dest_charset == C_AUTO &&
+                   conv_get_current_charset() == C_EUC_JP)
                        code_conv = conv_euctodisp;
                else if (dest_charset == C_ISO_2022_JP ||
                         dest_charset == C_ISO_2022_JP_2)
@@ -699,6 +828,98 @@ 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);
+
+       /* don't convert if src and dest codeset are identical */
+       if (!strcasecmp(src_code, dest_code))
+               return g_strdup(inbuf);
+
+       cd = iconv_open(dest_code, src_code);
+       if (cd == (iconv_t)-1)
+               return NULL;
+
+       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)) < 0) {
+               if (EILSEQ == 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)) < 0) {
+               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';
+
+       iconv_close(cd);
+
+       return outbuf;
+}
+#endif /* HAVE_ICONV */
+
 static const struct {
        CharSet charset;
        gchar *const name;
@@ -706,20 +927,42 @@ static const struct {
        {C_US_ASCII,            CS_US_ASCII},
        {C_US_ASCII,            CS_ANSI_X3_4_1968},
        {C_UTF_8,               CS_UTF_8},
+       {C_UTF_7,               CS_UTF_7},
        {C_ISO_8859_1,          CS_ISO_8859_1},
        {C_ISO_8859_2,          CS_ISO_8859_2},
+       {C_ISO_8859_3,          CS_ISO_8859_3},
        {C_ISO_8859_4,          CS_ISO_8859_4},
        {C_ISO_8859_5,          CS_ISO_8859_5},
+       {C_ISO_8859_6,          CS_ISO_8859_6},
        {C_ISO_8859_7,          CS_ISO_8859_7},
        {C_ISO_8859_8,          CS_ISO_8859_8},
        {C_ISO_8859_9,          CS_ISO_8859_9},
+       {C_ISO_8859_10,         CS_ISO_8859_10},
        {C_ISO_8859_11,         CS_ISO_8859_11},
        {C_ISO_8859_13,         CS_ISO_8859_13},
+       {C_ISO_8859_14,         CS_ISO_8859_14},
        {C_ISO_8859_15,         CS_ISO_8859_15},
        {C_BALTIC,              CS_BALTIC},
+       {C_CP1250,              CS_CP1250},
        {C_CP1251,              CS_CP1251},
+       {C_CP1252,              CS_CP1252},
+       {C_CP1253,              CS_CP1253},
+       {C_CP1254,              CS_CP1254},
+       {C_CP1255,              CS_CP1255},
+       {C_CP1256,              CS_CP1256},
+       {C_CP1257,              CS_CP1257},
+       {C_CP1258,              CS_CP1258},
+       {C_WINDOWS_1250,        CS_WINDOWS_1250},
        {C_WINDOWS_1251,        CS_WINDOWS_1251},
+       {C_WINDOWS_1252,        CS_WINDOWS_1252},
+       {C_WINDOWS_1253,        CS_WINDOWS_1253},
+       {C_WINDOWS_1254,        CS_WINDOWS_1254},
+       {C_WINDOWS_1255,        CS_WINDOWS_1255},
+       {C_WINDOWS_1256,        CS_WINDOWS_1256},
+       {C_WINDOWS_1257,        CS_WINDOWS_1257},
+       {C_WINDOWS_1258,        CS_WINDOWS_1258},
        {C_KOI8_R,              CS_KOI8_R},
+       {C_KOI8_T,              CS_KOI8_T},
        {C_KOI8_U,              CS_KOI8_U},
        {C_ISO_2022_JP,         CS_ISO_2022_JP},
        {C_ISO_2022_JP_2,       CS_ISO_2022_JP_2},
@@ -733,128 +976,263 @@ static const struct {
        {C_ISO_2022_CN,         CS_ISO_2022_CN},
        {C_EUC_CN,              CS_EUC_CN},
        {C_GB2312,              CS_GB2312},
+       {C_GBK,                 CS_GBK},
        {C_EUC_TW,              CS_EUC_TW},
        {C_BIG5,                CS_BIG5},
+       {C_BIG5_HKSCS,          CS_BIG5_HKSCS},
        {C_TIS_620,             CS_TIS_620},
        {C_WINDOWS_874,         CS_WINDOWS_874},
+       {C_GEORGIAN_PS,         CS_GEORGIAN_PS},
+       {C_TCVN5712_1,          CS_TCVN5712_1},
 };
 
-#if !HAVE_LIBJCONV
 static const struct {
        gchar *const locale;
        CharSet charset;
        CharSet out_charset;
 } locale_table[] = {
        {"ja_JP.eucJP"  , C_EUC_JP      , C_ISO_2022_JP},
-       {"ja_JP.ujis"   , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP.EUC-JP" , C_EUC_JP      , C_ISO_2022_JP},
        {"ja_JP.EUC"    , C_EUC_JP      , C_ISO_2022_JP},
+       {"ja_JP.ujis"   , C_EUC_JP      , C_ISO_2022_JP},
        {"ja_JP.SJIS"   , C_SHIFT_JIS   , C_ISO_2022_JP},
        {"ja_JP.JIS"    , C_ISO_2022_JP , C_ISO_2022_JP},
        {"ja_JP"        , C_EUC_JP      , C_ISO_2022_JP},
+       {"ko_KR.EUC-KR" , C_EUC_KR      , C_EUC_KR},
        {"ko_KR"        , C_EUC_KR      , C_EUC_KR},
        {"zh_CN.GB2312" , C_GB2312      , C_GB2312},
+       {"zh_CN.GBK"    , C_GBK         , C_GB2312},
        {"zh_CN"        , C_GB2312      , C_GB2312},
+       {"zh_HK"        , C_BIG5_HKSCS  , C_BIG5_HKSCS},
        {"zh_TW.eucTW"  , C_EUC_TW      , C_BIG5},
+       {"zh_TW.EUC-TW" , C_EUC_TW      , C_BIG5},
        {"zh_TW.Big5"   , C_BIG5        , C_BIG5},
        {"zh_TW"        , C_BIG5        , C_BIG5},
 
-       {"ru_RU.KOI8-R" , C_KOI8_R      , C_ISO_8859_5},
-       {"ru_RU.CP1251" , C_WINDOWS_1251, C_ISO_8859_5},
+       {"ru_RU.KOI8-R" , C_KOI8_R      , C_KOI8_R},
+       {"ru_RU.KOI8R"  , C_KOI8_R      , C_KOI8_R},
+       {"ru_RU.CP1251" , C_WINDOWS_1251, C_KOI8_R},
+       {"ru_RU"        , C_ISO_8859_5  , C_KOI8_R},
+       {"tg_TJ"        , C_KOI8_T      , C_KOI8_T},
+       {"ru_UA"        , C_KOI8_U      , C_KOI8_U},
+       {"uk_UA"        , C_KOI8_U      , C_KOI8_U},
 
+       {"be_BY"        , C_WINDOWS_1251, C_WINDOWS_1251},
        {"bg_BG"        , C_WINDOWS_1251, C_WINDOWS_1251},
 
-       {"en_US"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"yi_US"        , C_WINDOWS_1255, C_WINDOWS_1255},
+
+       {"af_ZA"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"br_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"ca_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"da_DK"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_AT"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_CH"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"de_DE"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"nl_NL"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"de_LU"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_AU"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_BW"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_CA"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_DK"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_GB"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_HK"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_IE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_NZ"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_PH"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_SG"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_US"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_ZA"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"en_ZW"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_AR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_BO"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_CL"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_CO"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_CR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_DO"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_EC"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_GT"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_HN"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_MX"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_NI"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PA"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_PY"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_SV"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_US"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_UY"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"es_VE"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"et_EE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"eu_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"fi_FI"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fo_FO"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_CA"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_CH"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"fr_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"fr_LU"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"ga_IE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"gl_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"gv_GB"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"id_ID"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"is_IS"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"it_CH"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"it_IT"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"kl_GL"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"kw_GB"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"ms_MY"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"nl_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"nl_NL"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"nn_NO"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"no_NO"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"pt_PT"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"oc_FR"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"pt_BR"        , C_ISO_8859_1  , C_ISO_8859_1},
-       {"es_ES"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"pt_PT"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"sq_AL"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"sv_FI"        , C_ISO_8859_1  , C_ISO_8859_1},
        {"sv_SE"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"tl_PH"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"uz_UZ"        , C_ISO_8859_1  , C_ISO_8859_1},
+       {"wa_BE"        , C_ISO_8859_1  , C_ISO_8859_1},
 
+       {"bs_BA"        , C_ISO_8859_2  , C_ISO_8859_2},
+       {"cs_CZ"        , C_ISO_8859_2  , C_ISO_8859_2},
        {"hr_HR"        , C_ISO_8859_2  , C_ISO_8859_2},
        {"hu_HU"        , C_ISO_8859_2  , C_ISO_8859_2},
        {"pl_PL"        , C_ISO_8859_2  , C_ISO_8859_2},
        {"ro_RO"        , C_ISO_8859_2  , C_ISO_8859_2},
        {"sk_SK"        , C_ISO_8859_2  , C_ISO_8859_2},
        {"sl_SI"        , C_ISO_8859_2  , C_ISO_8859_2},
-       {"ru_RU"        , C_ISO_8859_5  , C_ISO_8859_5},
+
+       {"sr_YU@cyrillic"       , C_ISO_8859_5  , C_ISO_8859_5},
+       {"sr_YU"                , C_ISO_8859_2  , C_ISO_8859_2},
+
+       {"mt_MT"                , C_ISO_8859_3  , C_ISO_8859_3},
+
+       {"lt_LT.iso88594"       , C_ISO_8859_4  , C_ISO_8859_4},
+       {"lt_LT.ISO8859-4"      , C_ISO_8859_4  , C_ISO_8859_4},
+       {"lt_LT.ISO_8859-4"     , C_ISO_8859_4  , C_ISO_8859_4},
+       {"lt_LT"                , C_ISO_8859_13 , C_ISO_8859_13},
+
+       {"mk_MK"        , C_ISO_8859_5  , C_ISO_8859_5},
+
+       {"ar_AE"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_BH"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_DZ"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_EG"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_IQ"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_JO"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_KW"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_LB"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_LY"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_MA"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_OM"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_QA"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_SA"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_SD"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_SY"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_TN"        , C_ISO_8859_6  , C_ISO_8859_6},
+       {"ar_YE"        , C_ISO_8859_6  , C_ISO_8859_6},
+
        {"el_GR"        , C_ISO_8859_7  , C_ISO_8859_7},
+       {"he_IL"        , C_ISO_8859_8  , C_ISO_8859_8},
        {"iw_IL"        , C_ISO_8859_8  , C_ISO_8859_8},
        {"tr_TR"        , C_ISO_8859_9  , C_ISO_8859_9},
 
+       {"lv_LV"        , C_ISO_8859_13 , C_ISO_8859_13},
+       {"mi_NZ"        , C_ISO_8859_13 , C_ISO_8859_13},
+
+       {"cy_GB"        , C_ISO_8859_14 , C_ISO_8859_14},
+
+       {"ar_IN"        , C_UTF_8       , C_UTF_8},
+       {"en_IN"        , C_UTF_8       , C_UTF_8},
+       {"se_NO"        , C_UTF_8       , C_UTF_8},
+       {"ta_IN"        , C_UTF_8       , C_UTF_8},
+       {"te_IN"        , C_UTF_8       , C_UTF_8},
+       {"ur_PK"        , C_UTF_8       , C_UTF_8},
+
        {"th_TH"        , C_TIS_620     , C_TIS_620},
        /* {"th_TH"     , C_WINDOWS_874}, */
        /* {"th_TH"     , C_ISO_8859_11}, */
 
-       {"lt_LT.iso88594"       , C_ISO_8859_4  , C_ISO_8859_4},
-       {"lt_LT.ISO8859-4"      , C_ISO_8859_4  , C_ISO_8859_4},
-       {"lt_LT.ISO_8859-4"     , C_ISO_8859_4  , C_ISO_8859_4},
-       {"lt_LT"                , C_ISO_8859_13 , C_ISO_8859_13},
-       {"lv_LV"                , C_ISO_8859_13 , C_ISO_8859_13},
+       {"ka_GE"        , C_GEORGIAN_PS , C_GEORGIAN_PS},
+       {"vi_VN.TCVN"   , C_TCVN5712_1  , C_TCVN5712_1},
 
        {"C"                    , C_US_ASCII    , C_US_ASCII},
        {"POSIX"                , C_US_ASCII    , C_US_ASCII},
        {"ANSI_X3.4-1968"       , C_US_ASCII    , C_US_ASCII},
 };
-#endif /* !HAVE_LIBJCONV */
 
-const gchar *conv_get_charset_str(CharSet charset)
+static GHashTable *conv_get_charset_to_str_table(void)
 {
+       static GHashTable *table;
        gint i;
 
+       if (table)
+               return table;
+
+       table = g_hash_table_new(NULL, g_direct_equal);
+
        for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
-               if (charsets[i].charset == charset)
-                       return charsets[i].name;
+               if (g_hash_table_lookup(table, GUINT_TO_POINTER(charsets[i].charset))
+                   == NULL) {
+                       g_hash_table_insert
+                               (table, GUINT_TO_POINTER(charsets[i].charset),
+                                charsets[i].name);
+               }
        }
 
-       return NULL;
+       return table;
 }
 
-CharSet conv_get_charset_from_str(const gchar *charset)
+static GHashTable *conv_get_charset_from_str_table(void)
 {
+       static GHashTable *table;
        gint i;
 
-       if (!charset) return C_AUTO;
+       if (table)
+               return table;
+
+       table = g_hash_table_new(str_case_hash, str_case_equal);
 
        for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
-               if (!strcasecmp(charsets[i].name, charset))
-                       return charsets[i].charset;
+               g_hash_table_insert(table, charsets[i].name,
+                                   GUINT_TO_POINTER(charsets[i].charset));
        }
 
-       return C_AUTO;
+       return table;
+}
+
+const gchar *conv_get_charset_str(CharSet charset)
+{
+       GHashTable *table;
+
+       table = conv_get_charset_to_str_table();
+       return g_hash_table_lookup(table, GUINT_TO_POINTER(charset));
+}
+
+CharSet conv_get_charset_from_str(const gchar *charset)
+{
+       GHashTable *table;
+
+       if (!charset) return C_AUTO;
+
+       table = conv_get_charset_from_str_table();
+       return GPOINTER_TO_UINT(g_hash_table_lookup(table, charset));
 }
 
 CharSet conv_get_current_charset(void)
 {
        static CharSet cur_charset = -1;
-       gint i;
-
-#if HAVE_LIBJCONV
-       const gchar *cur_codeset;
-#else
        const gchar *cur_locale;
-#endif
+       const gchar *p;
+       gint i;
 
        if (cur_charset != -1)
                return cur_charset;
 
-#if HAVE_LIBJCONV
-       cur_codeset = jconv_info_get_current_codeset();
-       for (i = 0; i < sizeof(charsets) / sizeof(charsets[0]); i++) {
-               if (!strcasecmp(cur_codeset, charsets[i].name)) {
-                       cur_charset = charsets[i].charset;
-                       return cur_charset;
-               }
-       }
-#else
        cur_locale = conv_get_current_locale();
        if (!cur_locale) {
                cur_charset = C_US_ASCII;
@@ -866,11 +1244,16 @@ CharSet conv_get_current_charset(void)
                return cur_charset;
        }
 
+       if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
+               cur_charset = C_ISO_8859_15;
+               return cur_charset;
+       }
+
        for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
                const gchar *p;
 
-               /* "ja_JP.EUC" matches with "ja_JP.eucJP" and "ja_JP.EUC" */
-               /* "ja_JP" matches with "ja_JP.xxxx" and "ja" */
+               /* "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,
                                 strlen(locale_table[i].locale))) {
                        cur_charset = locale_table[i].charset;
@@ -884,7 +1267,6 @@ CharSet conv_get_current_charset(void)
                        }
                }
        }
-#endif
 
        cur_charset = C_AUTO;
        return cur_charset;
@@ -897,51 +1279,30 @@ const gchar *conv_get_current_charset_str(void)
        if (!codeset)
                codeset = conv_get_charset_str(conv_get_current_charset());
 
-       return codeset ? codeset : "US-ASCII";
+       return codeset ? codeset : CS_US_ASCII;
 }
 
 CharSet conv_get_outgoing_charset(void)
 {
        static CharSet out_charset = -1;
-       gint i;
-
-#if HAVE_LIBJCONV
-       gint j, n_pref_codesets;
-       const gchar *const *pref_codesets;
-#else
        const gchar *cur_locale;
-#endif
+       const gchar *p;
+       gint i;
 
        if (out_charset != -1)
                return out_charset;
 
-#if HAVE_LIBJCONV
-       /* skip US-ASCII and UTF-8 */
-       pref_codesets = jconv_info_get_pref_codesets(&n_pref_codesets);
-       for (i = 0; i < n_pref_codesets; i++) {
-               for (j = 3; j < sizeof(charsets) / sizeof(charsets[0]); j++) {
-                       if (!strcasecmp(pref_codesets[i], charsets[j].name)) {
-                               out_charset = charsets[j].charset;
-                               return out_charset;
-                       }
-               }
-       }
-
-       for (i = 0; i < n_pref_codesets; i++) {
-               if (!strcasecmp(pref_codesets[i], "UTF-8")) {
-                       out_charset = C_UTF_8;
-                       return out_charset;
-               }
-       }
-
-       out_charset = C_AUTO;
-#else
        cur_locale = conv_get_current_locale();
        if (!cur_locale) {
                out_charset = C_AUTO;
                return out_charset;
        }
 
+       if ((p = strcasestr(cur_locale, "@euro")) && p[5] == '\0') {
+               out_charset = C_ISO_8859_15;
+               return out_charset;
+       }
+
        for (i = 0; i < sizeof(locale_table) / sizeof(locale_table[0]); i++) {
                const gchar *p;
 
@@ -959,12 +1320,13 @@ CharSet conv_get_outgoing_charset(void)
                }
        }
 
-       /* encoding conversion without libjconv is only supported
+#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;
-
-       out_charset = conv_get_current_charset();
+       else
+               return conv_get_current_charset();
 #endif
 
        return out_charset;
@@ -986,12 +1348,34 @@ const gchar *conv_get_outgoing_charset_str(void)
        out_charset = conv_get_outgoing_charset();
        str = conv_get_charset_str(out_charset);
 
-       return str ? str : "US-ASCII";
+       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_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;
+       }
 }
 
 const gchar *conv_get_current_locale(void)
 {
-       gchar *cur_locale;
+       const gchar *cur_locale;
 
        cur_locale = g_getenv("LC_ALL");
        if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
@@ -1009,10 +1393,15 @@ void conv_unmime_header_overwrite(gchar *str)
        gchar *buf;
        gint buflen;
        CharSet cur_charset;
+       const gchar *locale;
 
        cur_charset = conv_get_current_charset();
 
-       if (cur_charset == C_EUC_JP) {
+#warning FIXME_GTK2
+/* Should we always ensure to convert? */
+       locale = conv_get_current_locale();
+
+       if (locale && !strncasecmp(locale, "ja", 2)) {
                buflen = strlen(str) * 2 + 1;
                Xalloca(buf, buflen, return);
                conv_anytodisp(buf, buflen, str);
@@ -1029,10 +1418,15 @@ void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
                        const gchar *charset)
 {
        CharSet cur_charset;
+       const gchar *locale;
 
        cur_charset = conv_get_current_charset();
 
-       if (cur_charset == C_EUC_JP) {
+#warning FIXME_GTK2
+/* Should we always ensure to convert? */
+       locale = conv_get_current_locale();
+
+       if (locale && !strncasecmp(locale, "ja", 2)) {
                gchar *buf;
                gint buflen;
 
@@ -1044,34 +1438,37 @@ void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
                unmime_header(outbuf, str);
 }
 
-#define MAX_LINELEN    76
-#define MIMESEP_BEGIN  "=?"
-#define MIMESEP_END    "?="
+#define MAX_LINELEN            76
+#define MAX_HARD_LINELEN       996
+#define MIMESEP_BEGIN          "=?"
+#define MIMESEP_END            "?="
 
 #define B64LEN(len)    ((len) / 3 * 4 + ((len) % 3 ? 4 : 0))
 
-#define LBREAK_IF_REQUIRED(cond)                               \
-{                                                              \
-       if (len - (destp - dest) < MAX_LINELEN + 2) {           \
-               *destp = '\0';                                  \
-               return;                                         \
-       }                                                       \
-                                                               \
-       if ((cond) && *srcp) {                                  \
-               if (destp > dest && isspace(*(destp - 1)))      \
-                       destp--;                                \
-               else if (isspace(*srcp))                        \
-                       srcp++;                                 \
-               if (*srcp) {                                    \
-                       *destp++ = '\n';                        \
-                       *destp++ = ' ';                         \
-                       left = MAX_LINELEN - 1;                 \
-               }                                               \
-       }                                                       \
+#define LBREAK_IF_REQUIRED(cond, is_plain_text)                                \
+{                                                                      \
+       if (len - (destp - dest) < MAX_LINELEN + 2) {                   \
+               *destp = '\0';                                          \
+               return;                                                 \
+       }                                                               \
+                                                                       \
+       if ((cond) && *srcp) {                                          \
+               if (destp > dest && left < MAX_LINELEN - 1) {           \
+                       if (isspace(*(destp - 1)))                      \
+                               destp--;                                \
+                       else if (is_plain_text && isspace(*srcp))       \
+                               srcp++;                                 \
+                       if (*srcp) {                                    \
+                               *destp++ = '\n';                        \
+                               *destp++ = ' ';                         \
+                               left = MAX_LINELEN - 1;                 \
+                       }                                               \
+               }                                                       \
+       }                                                               \
 }
 
 void conv_encode_header(gchar *dest, gint len, const gchar *src,
-                       gint header_len)
+                       gint header_len, gboolean addr_field)
 {
        const gchar *cur_encoding;
        const gchar *out_encoding;
@@ -1091,11 +1488,11 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
        }
 
        cur_encoding = conv_get_current_charset_str();
-       if (!strcmp(cur_encoding, "US-ASCII"))
-               cur_encoding = "ISO-8859-1";
+       if (!strcmp(cur_encoding, CS_US_ASCII))
+               cur_encoding = CS_ISO_8859_1;
        out_encoding = conv_get_outgoing_charset_str();
-       if (!strcmp(out_encoding, "US-ASCII"))
-               out_encoding = "ISO-8859-1";
+       if (!strcmp(out_encoding, CS_US_ASCII))
+               out_encoding = CS_ISO_8859_1;
 
        mimestr_len = strlen(MIMESEP_BEGIN) + strlen(out_encoding) +
                strlen(mimesep_enc) + strlen(MIMESEP_END);
@@ -1103,12 +1500,12 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
        left = MAX_LINELEN - header_len;
 
        while (*srcp) {
-               LBREAK_IF_REQUIRED(left <= 0);
+               LBREAK_IF_REQUIRED(left <= 0, TRUE);
 
                while (isspace(*srcp)) {
                        *destp++ = *srcp++;
                        left--;
-                       LBREAK_IF_REQUIRED(left <= 0);
+                       LBREAK_IF_REQUIRED(left <= 0, TRUE);
                }
 
                /* output as it is if the next word is ASCII string */
@@ -1116,9 +1513,9 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                        gint word_len;
 
                        word_len = get_next_word_len(srcp);
-                       LBREAK_IF_REQUIRED(left < word_len);
+                       LBREAK_IF_REQUIRED(left < word_len, TRUE);
                        while (word_len > 0) {
-                               LBREAK_IF_REQUIRED(left <= 0);
+                               LBREAK_IF_REQUIRED(left + (MAX_HARD_LINELEN - MAX_LINELEN) <= 0, TRUE)
                                *destp++ = *srcp++;
                                left--;
                                word_len--;
@@ -1127,6 +1524,13 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                        continue;
                }
 
+               /* don't include parentheses in encoded strings */
+               if (addr_field && (*srcp == '(' || *srcp == ')')) {
+                       LBREAK_IF_REQUIRED(left < 2, FALSE);
+                       *destp++ = *srcp++;
+                       left--;
+               }
+
                while (1) {
                        gint mb_len = 0;
                        gint cur_len = 0;
@@ -1142,6 +1546,10 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                        while (*p != '\0') {
                                if (isspace(*p) && !is_next_nonascii(p + 1))
                                        break;
+                               /* don't include parentheses in encoded
+                                  strings */
+                               if (addr_field && (*p == '(' || *p == ')'))
+                                       break;
 
                                if (MB_CUR_MAX > 1) {
                                        mb_len = mblen(p, MB_CUR_MAX);
@@ -1157,7 +1565,8 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                        (part_str, cur_encoding, out_encoding);
                                if (!out_str) {
                                        g_warning("conv_encode_header(): code conversion failed\n");
-                                       out_str = g_strdup(out_str);
+                                       conv_unreadable_8bit(part_str);
+                                       out_str = g_strdup(part_str);
                                }
                                out_str_len = strlen(out_str);
 
@@ -1173,7 +1582,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                        cur_len += mb_len;
                                        p += mb_len;
                                } else if (cur_len == 0) {
-                                       LBREAK_IF_REQUIRED(1);
+                                       LBREAK_IF_REQUIRED(1, FALSE);
                                        continue;
                                } else {
                                        cont = TRUE;
@@ -1187,7 +1596,8 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                        (part_str, cur_encoding, out_encoding);
                                if (!out_str) {
                                        g_warning("conv_encode_header(): code conversion failed\n");
-                                       out_str = g_strdup(out_str);
+                                       conv_unreadable_8bit(part_str);
+                                       out_str = g_strdup(part_str);
                                }
                                out_str_len = strlen(out_str);
 
@@ -1216,7 +1626,7 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                left -= mime_block_len;
                        }
 
-                       LBREAK_IF_REQUIRED(cont);
+                       LBREAK_IF_REQUIRED(cont, FALSE);
 
                        if (cur_len == 0)
                                break;