2005-09-30 [paul] 1.9.14cvs63
[claws.git] / src / codeconv.c
index dbfa47761a899c8d5e836946ebf58b04e0235d78..d1226ee3590e6923619cc0dea58e3b29f803e4bf 100644 (file)
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -116,9 +116,7 @@ static void conv_anytoutf8(gchar *outbuf, gint outlen, const gchar *inbuf);
 static void conv_utf8toeuc(gchar *outbuf, gint outlen, const gchar *inbuf);
 static void conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf);
 
-static void conv_unreadable_eucjp(gchar *str);
 static void conv_unreadable_8bit(gchar *str);
-static void conv_unreadable_latin(gchar *str);
 
 static void conv_jistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
 static void conv_sjistodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
@@ -128,6 +126,13 @@ static void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
 static void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf);
 static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf);
 
+static gboolean strict_mode = FALSE;
+
+void codeconv_set_strict(gboolean mode)
+{
+       strict_mode = mode;
+}
+
 static void conv_jistoeuc(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        const guchar *in = inbuf;
@@ -511,131 +516,6 @@ static void conv_utf8tojis(gchar *outbuf, gint outlen, const gchar *inbuf)
        conv_euctojis(outbuf, outlen, eucstr);
 }
 
-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;
-}
-
-static void conv_unreadable_eucjp(gchar *str)
-{
-       register guchar *p = str;
-
-       while (*p != '\0') {
-               if (IS_ASCII(*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 (IS_ASCII(*p))
-                                               p++;
-                                       else
-                                               *p++ = SUBST_CHAR;
-                               }
-                       }
-               } else if (iseuchwkana1(*p)) {
-                       if (iseuchwkana2(*(p + 1)))
-                               /* euc-jp hankaku kana */
-                               p += 2;
-                       else
-                               *p++ = SUBST_CHAR;
-               } else if (iseucaux(*p)) {
-                       if (iseuckanji(*(p + 1)) && iseuckanji(*(p + 2))) {
-                               /* auxiliary kanji */
-                               p += 3;
-                       } else
-                               *p++ = SUBST_CHAR;
-               } else
-                       /* substitute unprintable 1 byte code */
-                       *p++ = SUBST_CHAR;
-       }
-}
-
 static void conv_unreadable_8bit(gchar *str)
 {
        register guchar *p = str;
@@ -649,20 +529,6 @@ static void conv_unreadable_8bit(gchar *str)
        }
 }
 
-static void conv_unreadable_latin(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 = SUBST_CHAR;
-               p++;
-       }
-}
-
 #define NCV    '\0'
 
 void conv_mb_alnum(gchar *str)
@@ -813,9 +679,12 @@ void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 
        tmpstr = conv_iconv_strdup(inbuf, conv_get_locale_charset_str(),
                                   CS_INTERNAL);
-       if (tmpstr) {
+       if (tmpstr && g_utf8_validate(tmpstr, -1, NULL)) {
                strncpy2(outbuf, tmpstr, outlen);
                g_free(tmpstr);
+       } else if (tmpstr && !g_utf8_validate(tmpstr, -1, NULL)) {
+               g_free(tmpstr);
+               conv_utf8todisp(outbuf, outlen, inbuf);
        } else
                conv_utf8todisp(outbuf, outlen, inbuf);
 }
@@ -825,10 +694,24 @@ static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
        strncpy2(outbuf, inbuf, outlen);
 }
 
+static const gchar *
+conv_get_fallback_for_private_encoding(const gchar *encoding)
+{
+       if (encoding && (encoding[0] == 'X' || encoding[0] == 'x') &&
+           encoding[1] == '-') {
+               if (!g_ascii_strcasecmp(encoding, CS_X_GBK))
+                       return CS_GBK;
+       }
+
+       return encoding;
+}
+
 CodeConverter *conv_code_converter_new(const gchar *src_charset)
 {
        CodeConverter *conv;
 
+       src_charset = conv_get_fallback_for_private_encoding(src_charset);
+
        conv = g_new0(CodeConverter, 1);
        conv->code_conv_func = conv_get_code_conv_func(src_charset, NULL);
        conv->charset_str = g_strdup(src_charset);
@@ -870,6 +753,7 @@ gchar *conv_codeset_strdup(const gchar *inbuf,
        size_t len;
        CodeConvFunc conv_func;
 
+       src_code = conv_get_fallback_for_private_encoding(src_code);
        conv_func = conv_get_code_conv_func(src_code, dest_code);
        if (conv_func != conv_noconv) {
                len = (strlen(inbuf) + 1) * 3;
@@ -897,7 +781,7 @@ CodeConvFunc conv_get_code_conv_func(const gchar *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;
@@ -1030,6 +914,10 @@ gchar *conv_iconv_strdup_with_cd(const gchar *inbuf, iconv_t cd)
        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--;
@@ -1148,7 +1036,11 @@ static const struct {
        {"ja_JP.ujis"   , C_EUC_JP      , C_ISO_2022_JP},
        {"ja_JP.SJIS"   , C_SHIFT_JIS   , C_ISO_2022_JP},
        {"ja_JP.JIS"    , C_ISO_2022_JP , C_ISO_2022_JP},
+#ifdef G_OS_WIN32
+       {"ja_JP"        , C_SHIFT_JIS   , C_ISO_2022_JP},
+#else
        {"ja_JP"        , C_EUC_JP      , C_ISO_2022_JP},
+#endif
        {"ko_KR.EUC-KR" , C_EUC_KR      , C_EUC_KR},
        {"ko_KR"        , C_EUC_KR      , C_EUC_KR},
        {"zh_CN.GB2312" , C_GB2312      , C_GB2312},
@@ -1424,6 +1316,60 @@ CharSet conv_get_locale_charset(void)
        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 &&
+                           !g_ascii_strncasecmp(cur_locale, locale_table[i].locale, 2)) {
+                               cur_charset = locale_table[i].charset;
+                               return cur_charset;
+                       }
+               }
+       }
+
+       cur_charset = C_AUTO;
+       return cur_charset;
+}
+
 const gchar *conv_get_locale_charset_str(void)
 {
        static const gchar *codeset = NULL;
@@ -1434,6 +1380,16 @@ const gchar *conv_get_locale_charset_str(void)
        return codeset ? codeset : CS_INTERNAL;
 }
 
+const gchar *conv_get_locale_charset_str_no_utf8(void)
+{
+       static const gchar *codeset = NULL;
+
+       if (!codeset)
+               codeset = conv_get_charset_str(conv_get_locale_charset_no_utf8());
+
+       return codeset ? codeset : CS_INTERNAL;
+}
+
 CharSet conv_get_internal_charset(void)
 {
        return C_INTERNAL;
@@ -1460,6 +1416,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;
@@ -1490,14 +1451,6 @@ 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);
 
@@ -1533,10 +1486,14 @@ const gchar *conv_get_current_locale(void)
 {
        const gchar *cur_locale;
 
+#ifdef G_OS_WIN32
+       cur_locale = g_win32_getlocale();
+#else
        cur_locale = g_getenv("LC_ALL");
        if (!cur_locale) cur_locale = g_getenv("LC_CTYPE");
        if (!cur_locale) cur_locale = g_getenv("LANG");
        if (!cur_locale) cur_locale = setlocale(LC_CTYPE, NULL);
+#endif /* G_OS_WIN32 */
 
        debug_print("current locale: %s\n",
                    cur_locale ? cur_locale : "(none)");
@@ -1544,6 +1501,24 @@ const gchar *conv_get_current_locale(void)
        return cur_locale;
 }
 
+gboolean conv_is_ja_locale(void)
+{
+       static gint is_ja_locale = -1;
+       const gchar *cur_locale;
+
+       if (is_ja_locale != -1)
+               return is_ja_locale != 0;
+
+       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;
+}
+
 gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
 {
        gchar buf[BUFFSIZE];
@@ -1565,7 +1540,7 @@ gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
                }
        }
 
-       if (conv_get_locale_charset() == C_EUC_JP)
+       if (conv_is_ja_locale())
                conv_anytodisp(buf, sizeof(buf), str);
        else
                conv_localetodisp(buf, sizeof(buf), str);
@@ -1600,8 +1575,9 @@ gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
        }                                                               \
 }
 
-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;
@@ -1623,7 +1599,12 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
        }
 
        cur_encoding = CS_INTERNAL;
-       out_encoding = conv_get_outgoing_charset_str();
+
+       if (out_encoding_)
+               out_encoding = out_encoding_;
+       else
+               out_encoding = conv_get_outgoing_charset_str();
+
        if (!strcmp(out_encoding, CS_US_ASCII))
                out_encoding = CS_ISO_8859_1;
 
@@ -1690,9 +1671,14 @@ void conv_encode_header(gchar *dest, gint len, const gchar *src,
                                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);
 
@@ -1762,6 +1748,12 @@ 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)
 {