aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-mime-utils.c
diff options
context:
space:
mode:
authorJeffrey Stedfast <fejj@ximian.com>2001-07-20 08:08:50 +0800
committerJeffrey Stedfast <fejj@src.gnome.org>2001-07-20 08:08:50 +0800
commit25ea76f3a782b2cd0a9ed65216203b7d7472ef57 (patch)
treeb9fbdc13ea42b51c99ec31c54b883c78eaff132d /camel/camel-mime-utils.c
parent0ce3b207745b7094c31a4713f8bc08cbe926b425 (diff)
downloadgsoc2013-evolution-25ea76f3a782b2cd0a9ed65216203b7d7472ef57.tar
gsoc2013-evolution-25ea76f3a782b2cd0a9ed65216203b7d7472ef57.tar.gz
gsoc2013-evolution-25ea76f3a782b2cd0a9ed65216203b7d7472ef57.tar.bz2
gsoc2013-evolution-25ea76f3a782b2cd0a9ed65216203b7d7472ef57.tar.lz
gsoc2013-evolution-25ea76f3a782b2cd0a9ed65216203b7d7472ef57.tar.xz
gsoc2013-evolution-25ea76f3a782b2cd0a9ed65216203b7d7472ef57.tar.zst
gsoc2013-evolution-25ea76f3a782b2cd0a9ed65216203b7d7472ef57.zip
Convert to the iconv-friendly charset names.
2001-07-19 Jeffrey Stedfast <fejj@ximian.com> * camel-mime-filter-charset.c (camel_mime_filter_charset_new_convert): Convert to the iconv-friendly charset names. * providers/imap/camel-imap-store.c (create_folder): Fixed a compiler warning about returning without a value in a non-void function. Blah. * camel-mime-part.c (process_header): Pass the locale charset as the default_charset to header_decode_string(). * camel-folder-summary.c (camel_folder_summary_format_string): Pass the locale charset as the default_charset to header_decode_string(). (content_info_new): Same. * camel-mime-message.c (process_header): Pass the locale charset as the default_charset to header_decode_string(). * camel-mime-utils.c (append_8bit): New helper function who's purpose is similar to append_latin1() but for 8bit text that we are assuming is not latin1. (header_decode_text): Now takes a default_charset parameter and calls append_8bit when appropriate. (header_decode_string): Also takes a default_charset parameter now. (header_decode_mailbox): Pass NULL as the default_charset to header_decode_string(). svn path=/trunk/; revision=11250
Diffstat (limited to 'camel/camel-mime-utils.c')
-rw-r--r--camel/camel-mime-utils.c122
1 files changed, 64 insertions, 58 deletions
diff --git a/camel/camel-mime-utils.c b/camel/camel-mime-utils.c
index ba704b19dc..594c1de05e 100644
--- a/camel/camel-mime-utils.c
+++ b/camel/camel-mime-utils.c
@@ -1004,32 +1004,63 @@ g_string_append_len(GString *st, const char *s, int l)
according to the rfc's. Anyway, since the conversion to utf-8
is trivial, just do it here without iconv */
static GString *
-append_latin1(GString *out, const char *in, int len)
+append_latin1 (GString *out, const char *in, int len)
{
unsigned int c;
-
+
while (len) {
c = (unsigned int)*in++;
len--;
if (c & 0x80) {
- out = g_string_append_c(out, 0xc0 | ((c>>6) & 0x3)); /* 110000xx */
- out = g_string_append_c(out, 0x80 | (c&0x3f)); /* 10xxxxxx */
+ out = g_string_append_c (out, 0xc0 | ((c >> 6) & 0x3)); /* 110000xx */
+ out = g_string_append_c (out, 0x80 | (c & 0x3f)); /* 10xxxxxx */
} else {
- out = g_string_append_c(out, c);
+ out = g_string_append_c (out, c);
}
}
return out;
}
+static void
+append_8bit (GString *out, const char *inbuf, int inlen, const char *default_charset)
+{
+ char *outbase, *outbuf;
+ int outlen;
+ iconv_t ic;
+
+ ic = iconv_open ("UTF-8", default_charset);
+ if (ic != (iconv_t) -1) {
+ int ret;
+
+ outlen = inlen * 6 + 16;
+ outbuf = outbase = g_malloc (outlen);
+
+ ret = iconv (ic, &inbuf, &inlen, &outbuf, &outlen);
+ if (ret >= 0) {
+ iconv (ic, NULL, 0, &outbuf, &outlen);
+ *outbuf = '\0';
+ }
+
+ iconv_close (ic);
+
+ /* FIXME: is outlen == strlen (outbuf) ?? */
+ g_string_append_len (out, outbase, strlen (outbase));
+ } else {
+ /* bah, completely broken...just append as raw text */
+ g_string_append_len (out, inbuf, inlen);
+ }
+}
+
/* decodes a simple text, rfc822 */
static char *
-header_decode_text (const char *in, int inlen)
+header_decode_text (const char *in, int inlen, const char *default_charset)
{
GString *out;
char *inptr, *inend, *start, *word_start;
char *decoded;
gboolean wasdword = FALSE;
gboolean wasspace = FALSE;
+ gboolean islatin1 = FALSE;
out = g_string_new ("");
start = inptr = (char *) in;
@@ -1056,8 +1087,12 @@ header_decode_text (const char *in, int inlen)
g_string_append (out, dword);
g_free (dword);
wasdword = TRUE;
+ } else if (islatin1 || !default_charset) {
+ /* append_latin1 is safe for 7bit ascii too */
+ append_latin1 (out, start, inptr - start - 1);
+ wasdword = FALSE;
} else {
- out = append_latin1 (out, start, inptr - start - 1);
+ append_8bit (out, start, inptr - start - 1, default_charset);
wasdword = FALSE;
}
@@ -1068,6 +1103,11 @@ header_decode_text (const char *in, int inlen)
wasspace = FALSE;
if (!word_start)
word_start = inptr - 1;
+
+ if (c & 0x80 || c <= 127)
+ islatin1 = TRUE;
+ else
+ islatin1 = FALSE;
}
}
@@ -1087,8 +1127,11 @@ header_decode_text (const char *in, int inlen)
g_string_append (out, dword);
g_free (dword);
+ } else if (islatin1 || !default_charset) {
+ /* append_latin1 is safe for 7bit ascii too */
+ append_latin1 (out, start, inptr - start);
} else {
- out = append_latin1 (out, start, inptr - start);
+ append_8bit (out, start, inptr - start, default_charset);
}
}
@@ -1098,49 +1141,12 @@ header_decode_text (const char *in, int inlen)
return decoded;
}
-#if 0 /* This is broken */
-
-/* so in what way is it broken? */
-
-/* decodes a simple text, rfc822 */
-static char *
-header_decode_text(const char *in, int inlen)
-{
- GString *out;
- const char *inptr = in;
- const char *inend = in+inlen;
- char *encstart, *encend;
- char *decword;
-
- out = g_string_new("");
- while ( (encstart = strstr(inptr, "=?"))
- && (encend = strstr(encstart+2, "?=")) ) {
-
- decword = rfc2047_decode_word(encstart, encend-encstart+2);
- if (decword) {
- out = g_string_append_len(out, inptr, encstart-inptr);
- out = g_string_append_len(out, decword, strlen(decword));
- g_free (decword);
- } else {
- out = append_latin1(out, inptr, encend-inptr+2);
- }
- inptr = encend+2;
- }
- out = append_latin1(out, inptr, inend-inptr);
-
- encstart = out->str;
- g_string_free(out, FALSE);
-
- return encstart;
-}
-#endif
-
char *
-header_decode_string(const char *in)
+header_decode_string (const char *in, const char *default_charset)
{
if (in == NULL)
return NULL;
- return header_decode_text(in, strlen(in));
+ return header_decode_text (in, strlen (in), default_charset);
}
/* how long a sequence of pre-encoded words should be less than, to attempt to
@@ -2248,13 +2254,13 @@ header_decode_mailbox(const char **in)
header_decode_lwsp(&inptr);
if (!(*inptr == '.' || *inptr == '@' || *inptr==',' || *inptr=='\0')) {
/* ',' and '\0' required incase it is a simple address, no @ domain part (buggy writer) */
- name = g_string_new("");
+ name = g_string_new ("");
while (pre) {
char *text, *last;
/* perform internationalised decoding, and append */
- text = header_decode_string(pre);
- name = g_string_append(name, text);
+ text = header_decode_string (pre, NULL);
+ g_string_append (name, text);
last = pre;
g_free(text);
@@ -2362,19 +2368,19 @@ header_decode_mailbox(const char **in)
if (comend > comstart) {
d(printf(" looking at subset '%.*s'\n", comend-comstart, comstart));
- tmp = g_strndup(comstart, comend-comstart);
- text = header_decode_string(tmp);
- name = g_string_new(text);
- g_free(tmp);
- g_free(text);
+ tmp = g_strndup (comstart, comend-comstart);
+ text = header_decode_string (tmp, NULL);
+ name = g_string_new (text);
+ g_free (tmp);
+ g_free (text);
}
}
}
-
+
*in = inptr;
-
+
if (addr->len > 0) {
- address = header_address_new_name(name?name->str:"", addr->str);
+ address = header_address_new_name(name ? name->str : "", addr->str);
}
g_string_free(addr, TRUE);