2005-02-25 [paul] 1.0.1cvs15.12
[claws.git] / src / codeconv.c
index d36bf87627739f0de0b99ea2dd1998856641ba34..89a2e5aa36491390cc2466aaeca5a3d221cc41c4 100644 (file)
@@ -655,7 +655,7 @@ static void conv_unreadable_latin(gchar *str)
                /* convert CR+LF -> LF */
                if (*p == '\r' && *(p + 1) == '\n')
                        memmove(p, p + 1, strlen(p));
-               else if ((*p & 0xff) >= 0x7f)
+               else if ((*p & 0xff) >= 0x7f && (*p & 0xff) <= 0x9f)
                        *p = SUBST_CHAR;
                p++;
        }
@@ -795,6 +795,8 @@ void conv_utf8todisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 static void conv_anytodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
 {
        conv_anytoutf8(outbuf, outlen, inbuf);
+       if (g_utf8_validate(outbuf, -1, NULL) != TRUE)
+               conv_unreadable_8bit(outbuf);
 }
 
 static void conv_ustodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
@@ -813,7 +815,7 @@ void conv_localetodisp(gchar *outbuf, gint outlen, const gchar *inbuf)
                strncpy2(outbuf, tmpstr, outlen);
                g_free(tmpstr);
        } else
-               strncpy2(outbuf, inbuf, outlen);
+               conv_utf8todisp(outbuf, outlen, inbuf);
 }
 
 static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
@@ -1543,57 +1545,33 @@ void conv_unmime_header_overwrite(gchar *str)
 {
        gchar *buf;
        gint buflen;
-       CharSet cur_charset;
 
-       cur_charset = conv_get_locale_charset();
+       buflen = strlen(str) * 2 + 1;
+       Xalloca(buf, buflen, return);
 
-       if (cur_charset == C_EUC_JP) {
-               buflen = strlen(str) * 2 + 1;
-               Xalloca(buf, buflen, return);
+       if (conv_get_locale_charset() == C_EUC_JP)
                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);
-       }
+       else
+               conv_localetodisp(buf, buflen, str);
+
+       unmime_header(str, buf);
 }
 
 void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
                        const gchar *charset)
 {
-       CharSet cur_charset;
-
-       cur_charset = conv_get_locale_charset();
+       gchar *buf;
+       gint buflen;
 
-       if (cur_charset == C_EUC_JP) {
-               gchar *buf;
-               gint buflen;
+       buflen = strlen(str) * 2 + 1;
+       Xalloca(buf, buflen, return);
 
-               buflen = strlen(str) * 2 + 1;
-               Xalloca(buf, buflen, return);
+       if (conv_get_locale_charset() == C_EUC_JP)
                conv_anytodisp(buf, buflen, str);
-               unmime_header(outbuf, buf);
-       } else {
-               gchar *tmp = NULL;
-               unmime_header(outbuf, str);
-
-               if (outbuf && !g_utf8_validate(outbuf, -1, NULL)) {
-                       if (conv_get_locale_charset() != C_INTERNAL)
-                               tmp = conv_codeset_strdup(outbuf,
-                                       conv_get_locale_charset_str(),
-                                       CS_INTERNAL);
-
-                       if (tmp) {
-                               strncpy(outbuf, tmp, outlen-1);
-                               g_free(tmp);
-                       } else {
-                               conv_unreadable_8bit(outbuf);
-                       }
-               }
-       }
+       else
+               conv_localetodisp(buf, buflen, str);
 
+       unmime_header(outbuf, buf);
 }
 
 #define MAX_LINELEN            76
@@ -1805,7 +1783,7 @@ gchar *conv_filename_from_utf8(const gchar *utf8_file)
 
 gchar *conv_filename_to_utf8(const gchar *fs_file)
 {
-       gchar *utf8_file;
+       gchar *utf8_file = NULL;
        GError *error = NULL;
 
        utf8_file = g_filename_to_utf8(fs_file, -1, NULL, NULL, &error);
@@ -1814,8 +1792,12 @@ gchar *conv_filename_to_utf8(const gchar *fs_file)
                          error->message);
                g_error_free(error);
        }
-       if (!utf8_file)
+
+       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;
 }