# include "config.h"
#endif
+#include "defs.h"
+
#include <glib.h>
#include <glib/gi18n.h>
#include <string.h>
/* 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++;
}
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)
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)
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);
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;
{"ko_KR.EUC-KR" , C_EUC_KR , C_EUC_KR},
{"ko_KR" , C_EUC_KR , C_EUC_KR},
{"zh_CN.GB2312" , C_GB2312 , C_GB2312},
- {"zh_CN.GBK" , C_GBK , C_GB2312},
+ {"zh_CN.GBK" , C_GBK , C_GBK},
{"zh_CN" , C_GB2312 , C_GB2312},
{"zh_HK" , C_BIG5_HKSCS , C_BIG5_HKSCS},
{"zh_TW.eucTW" , C_EUC_TW , C_BIG5},
case C_ISO_2022_CN:
case C_SHIFT_JIS:
case C_GB2312:
+ case C_GBK:
case C_BIG5:
case C_UTF_8:
case C_UTF_7:
return cur_locale;
}
-void conv_unmime_header_overwrite(gchar *str)
+gchar *conv_unmime_header(const gchar *str, const gchar *default_encoding)
{
- gchar *buf;
- gint buflen;
- CharSet cur_charset;
-
- cur_charset = conv_get_locale_charset();
+ gchar buf[BUFFSIZE];
- if (cur_charset == C_EUC_JP) {
- buflen = strlen(str) * 2 + 1;
- Xalloca(buf, buflen, return);
- conv_anytodisp(buf, buflen, str);
- unmime_header(str, buf);
- } else {
- buflen = strlen(str) + 1;
- Xalloca(buf, buflen, return);
- unmime_header(buf, str);
- strncpy2(str, buf, buflen);
- }
-}
-
-void conv_unmime_header(gchar *outbuf, gint outlen, const gchar *str,
- const gchar *charset)
-{
- CharSet cur_charset;
+ if (is_ascii_str(str))
+ return unmime_header(str);
- cur_charset = conv_get_locale_charset();
+ if (default_encoding) {
+ gchar *utf8_buf;
- if (cur_charset == C_EUC_JP) {
- gchar *buf;
- gint buflen;
+ utf8_buf = conv_codeset_strdup
+ (str, default_encoding, CS_INTERNAL);
+ if (utf8_buf) {
+ gchar *decoded_str;
- buflen = strlen(str) * 2 + 1;
- Xalloca(buf, buflen, return);
- 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);
- }
+ decoded_str = unmime_header(utf8_buf);
+ g_free(utf8_buf);
+ return decoded_str;
}
}
+ if (conv_get_locale_charset() == C_EUC_JP)
+ conv_anytodisp(buf, sizeof(buf), str);
+ else
+ conv_localetodisp(buf, sizeof(buf), str);
+
+ return unmime_header(buf);
}
#define MAX_LINELEN 76