*
* 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
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;
{
gchar *tmpstr;
+ codeconv_set_strict(TRUE);
tmpstr = conv_iconv_strdup(inbuf, conv_get_locale_charset_str(),
CS_INTERNAL);
- if (tmpstr) {
+ 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);
} else
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--;
{"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},
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;
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;
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;
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);
{
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)");
is_ja_locale = 0;
cur_locale = conv_get_current_locale();
if (cur_locale) {
- if (g_strncasecmp(cur_locale, "ja", 2) == 0)
+ if (g_ascii_strncasecmp(cur_locale, "ja", 2) == 0)
is_ja_locale = 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;
}
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;
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);
*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)
{