/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* 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
# include <locale.h>
#endif
-#include <iconv.h>
-
#include "codeconv.h"
#include "unmime.h"
#include "base64.h"
#include "utils.h"
#include "prefs_common.h"
+/* For unknown reasons the inconv.m4 macro undefs that macro if no
+ const is needed. This would break the code below so we define it. */
+#ifndef ICONV_CONST
+#define ICONV_CONST
+#endif
+
typedef enum
{
JIS_ASCII,
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;
}
}
-#define NCV '\0'
-
-void conv_mb_alnum(gchar *str)
-{
- static guchar char_tbl[] = {
- /* 0xa0 - 0xaf */
- NCV, ' ', NCV, NCV, ',', '.', NCV, ':',
- ';', '?', '!', NCV, NCV, NCV, NCV, NCV,
- /* 0xb0 - 0xbf */
- NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
- NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
- /* 0xc0 - 0xcf */
- NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV,
- NCV, NCV, '(', ')', NCV, NCV, '[', ']',
- /* 0xd0 - 0xdf */
- '{', '}', NCV, NCV, NCV, NCV, NCV, NCV,
- NCV, NCV, NCV, NCV, '+', '-', NCV, NCV,
- /* 0xe0 - 0xef */
- NCV, '=', NCV, '<', '>', NCV, NCV, NCV,
- NCV, NCV, NCV, NCV, NCV, NCV, NCV, NCV
- };
-
- register guchar *p = str;
- register gint len;
-
- len = strlen(str);
-
- while (len > 1) {
- if (*p == 0xa3) {
- register guchar ch = *(p + 1);
-
- if (ch >= 0xb0 && ch <= 0xfa) {
- /* [a-zA-Z] */
- *p = ch & 0x7f;
- p++;
- len--;
- memmove(p, p + 1, len);
- len--;
- } else {
- p += 2;
- len -= 2;
- }
- } else if (*p == 0xa1) {
- register guchar ch = *(p + 1);
-
- if (ch >= 0xa0 && ch <= 0xef &&
- NCV != char_tbl[ch - 0xa0]) {
- *p = char_tbl[ch - 0xa0];
- p++;
- len--;
- memmove(p, p + 1, len);
- len--;
- } else {
- p += 2;
- len -= 2;
- }
- } else if (iseuckanji(*p)) {
- p += 2;
- len -= 2;
- } else {
- p++;
- len--;
- }
- }
-}
-
CharSet conv_guess_ja_encoding(const gchar *str)
{
const guchar *p = str;
{
gchar *tmpstr;
+ codeconv_set_strict(TRUE);
tmpstr = conv_iconv_strdup(inbuf, conv_get_locale_charset_str(),
CS_INTERNAL);
+ 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);
+ return;
+ } else {
g_free(tmpstr);
conv_utf8todisp(outbuf, outlen, inbuf);
- } else
- conv_utf8todisp(outbuf, outlen, inbuf);
+ }
}
static void conv_noconv(gchar *outbuf, gint outlen, const gchar *inbuf)
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--;
gchar *tmp;
gint i;
+ if (prefs_common.broken_are_utf8)
+ return conv_get_locale_charset();
+
if (cur_charset != -1)
return cur_charset;
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);