2011-01-06 [colin] 3.7.8cvs31
[claws.git] / src / procmime.c
index 18497ab04102ebc40982d181e09485113affeedc..4b13271ff879f755d372373577c4acae7a344309 100644 (file)
@@ -345,11 +345,13 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        FLUSH_LASTLINE();
        } else if (encoding == ENC_BASE64) {
                gchar outbuf[BUFFSIZE];
-               gint len;
+               gint len, inlen, inread;
                Base64Decoder *decoder;
                gboolean got_error = FALSE;
                gboolean uncanonicalize = FALSE;
                FILE *tmpfp = outfp;
+               gboolean null_bytes = FALSE;
+               gboolean starting = TRUE;
 
                if (mimeinfo->type == MIMETYPE_TEXT ||
                    mimeinfo->type == MIMETYPE_MESSAGE) {
@@ -364,9 +366,15 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                }
 
                decoder = base64_decoder_new();
-               while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
-                       len = base64_decoder_decode(decoder, buf, outbuf);
-                       if (len < 0 && !got_error) {
+               while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
+                       inread = fread(buf, 1, inlen, infp);
+                       len = base64_decoder_decode(decoder, buf, outbuf, inread);
+                       if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
+                               uncanonicalize = FALSE;
+                               null_bytes = TRUE;
+                       }
+                       starting = FALSE;
+                       if (((inread != inlen) || len < 0) && !got_error) {
                                g_warning("Bad BASE64 content.\n");
                                if (fwrite(_("[Error decoding BASE64]\n"),
                                        sizeof(gchar),
@@ -378,8 +386,14 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        } else if (len >= 0) {
                                /* print out the error message only once 
                                 * per block */
-                               if (fwrite(outbuf, sizeof(gchar), len, tmpfp) < len)
-                                       err = TRUE;
+                               if (null_bytes) {
+                                       /* we won't uncanonicalize, output to outfp directly */
+                                       if (fwrite(outbuf, sizeof(gchar), len, outfp) < len)
+                                               err = TRUE;
+                               } else {
+                                       if (fwrite(outbuf, sizeof(gchar), len, tmpfp) < len)
+                                               err = TRUE;
+                               }
                                got_error = FALSE;
                        }
                }
@@ -640,6 +654,7 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
                if (fwrite(buf, 1, readlength, outfp) != readlength) {
                        saved_errno = errno;
+                       fclose(infp);
                        fclose(outfp);
                        return -(saved_errno);
                }
@@ -698,8 +713,15 @@ static FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                      ? forced_charset : 
                      procmime_mimeinfo_get_parameter(mimeinfo, "charset");
 
+       /* use supersets transparently when possible */
        if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_ISO_8859_1))
                src_codeset = CS_WINDOWS_1252;
+       else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_GBK))
+               src_codeset = CS_GB18030;
+       else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GBK))
+               src_codeset = CS_GB18030;
+       else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
+               src_codeset = CS_GB18030;
 
        if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
                SC_HTMLParser *parser;
@@ -1364,32 +1386,32 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
        fseek(fp, mimeinfo->offset, SEEK_SET);
        procheader_get_header_fields(fp, hentry);
        if (hentry[0].body != NULL) {
-                tmp = conv_unmime_header(hentry[0].body, NULL);
+               tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
                 g_free(hentry[0].body);
                 hentry[0].body = tmp;
         }                
        if (hentry[2].body != NULL) {
-                tmp = conv_unmime_header(hentry[2].body, NULL);
+               tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);
                 g_free(hentry[2].body);
                 hentry[2].body = tmp;
         }                
        if (hentry[4].body != NULL) {
-                tmp = conv_unmime_header(hentry[4].body, NULL);
+               tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);
                 g_free(hentry[4].body);
                 hentry[4].body = tmp;
         }                
        if (hentry[5].body != NULL) {
-                tmp = conv_unmime_header(hentry[5].body, NULL);
+               tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);
                 g_free(hentry[5].body);
                 hentry[5].body = tmp;
         }                
        if (hentry[7].body != NULL) {
-                tmp = conv_unmime_header(hentry[7].body, NULL);
+               tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);
                 g_free(hentry[7].body);
                 hentry[7].body = tmp;
         }
        if (hentry[8].body != NULL) {
-                tmp = conv_unmime_header(hentry[8].body, NULL);
+               tmp = conv_unmime_header(hentry[8].body, NULL, FALSE);
                 g_free(hentry[8].body);
                 hentry[8].body = tmp;
         }
@@ -1487,32 +1509,32 @@ bail:
 #define GET_HEADERS() {                                                \
        procheader_get_header_fields(fp, hentry);               \
         if (hentry[0].body != NULL) {                          \
-                tmp = conv_unmime_header(hentry[0].body, NULL);        \
+               tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);  \
                 g_free(hentry[0].body);                                \
                 hentry[0].body = tmp;                          \
         }                                                      \
         if (hentry[2].body != NULL) {                          \
-                tmp = conv_unmime_header(hentry[2].body, NULL);        \
+               tmp = conv_unmime_header(hentry[2].body, NULL, FALSE);  \
                 g_free(hentry[2].body);                                \
                 hentry[2].body = tmp;                          \
         }                                                      \
         if (hentry[4].body != NULL) {                          \
-                tmp = conv_unmime_header(hentry[4].body, NULL);        \
+               tmp = conv_unmime_header(hentry[4].body, NULL, FALSE);  \
                 g_free(hentry[4].body);                                \
                 hentry[4].body = tmp;                          \
         }                                                      \
         if (hentry[5].body != NULL) {                          \
-                tmp = conv_unmime_header(hentry[5].body, NULL);        \
+               tmp = conv_unmime_header(hentry[5].body, NULL, FALSE);  \
                 g_free(hentry[5].body);                                \
                 hentry[5].body = tmp;                          \
         }                                                      \
        if (hentry[6].body != NULL) {                           \
-                tmp = conv_unmime_header(hentry[6].body, NULL);        \
+               tmp = conv_unmime_header(hentry[6].body, NULL, FALSE);  \
                 g_free(hentry[6].body);                                \
                 hentry[6].body = tmp;                          \
         }                                                      \
        if (hentry[7].body != NULL) {                           \
-                tmp = conv_unmime_header(hentry[7].body, NULL);        \
+               tmp = conv_unmime_header(hentry[7].body, NULL, FALSE);  \
                 g_free(hentry[7].body);                                \
                 hentry[7].body = tmp;                          \
         }                                                      \
@@ -1918,7 +1940,6 @@ static int procmime_parse_mimepart(MimeInfo *parent,
        /* Create MimeInfo */
        mimeinfo = procmime_mimeinfo_new();
        mimeinfo->content = MIMECONTENT_FILE;
-       mimeinfo->lax_parse = parent->lax_parse;
 
        if (parent != NULL) {
                if (g_node_depth(parent->node) > 32) {
@@ -2223,7 +2244,11 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
                                g_string_append_printf(buf, "%%%s", hexstr);
                        }
                }
-               break;          
+               break;
+#else
+       case ENC_AS_EXTENDED:
+               debug_print("Unhandled ENC_AS_EXTENDED.");
+               break;
 #endif
        case ENC_AS_ENCWORD:
                len = MAX(strlen(val)*6, 512);