Use GLib's implementation of Base64 instead of our own.
[claws.git] / src / procmime.c
index dfc765b0e7fa0ce4e3ce1cf9007333a30f76437c..a4630261dea75726d1c5b416d823ca8342541c72 100644 (file)
@@ -41,7 +41,6 @@
 
 #include "procmime.h"
 #include "procheader.h"
-#include "base64.h"
 #include "quoted-printable.h"
 #include "uuencode.h"
 #include "unmime.h"
@@ -320,6 +319,8 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        gboolean flowed = FALSE;
        gboolean delsp = FALSE; 
        gboolean err = FALSE;
+       gint state = 0;
+       guint save = 0;
 
        EncodingType encoding = forced_encoding 
                                ? forced_encoding
@@ -352,12 +353,19 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        if (mimeinfo->type == MIMETYPE_MULTIPART || mimeinfo->type == MIMETYPE_MESSAGE)
                return TRUE;
 
+       if (mimeinfo->data.filename == NULL)
+               return FALSE;
+
        infp = procmime_fopen(mimeinfo->data.filename, "rb");
        if (!infp) {
                perror("fopen");
                return FALSE;
        }
-       fseek(infp, mimeinfo->offset, SEEK_SET);
+       if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+               perror("fseek");
+               procmime_fclose(infp);
+               return FALSE;
+       }
 
        outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
        if (!outfp) {
@@ -365,6 +373,9 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                procmime_fclose(infp);
                return FALSE;
        }
+#ifdef HAVE_FGETS_UNLOCKED
+       flockfile(outfp);
+#endif
        tmp_file = TRUE;
        readend = mimeinfo->offset + mimeinfo->length;
 
@@ -385,10 +396,9 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        } else if (encoding == ENC_BASE64) {
                gchar outbuf[BUFFSIZE];
                gint len, inlen, inread;
-               Base64Decoder *decoder;
                gboolean got_error = FALSE;
                gboolean uncanonicalize = FALSE;
-               FILE *tmpfp = outfp;
+               FILE *tmpfp = NULL;
                gboolean null_bytes = FALSE;
                gboolean starting = TRUE;
 
@@ -406,12 +416,12 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
 #ifdef HAVE_FGETS_UNLOCKED
                        flockfile(tmpfp);
 #endif
-               }
+               } else
+                       tmpfp = outfp;
 
-               decoder = base64_decoder_new();
                while ((inlen = MIN(readend - ftell(infp), sizeof(buf))) > 0 && !err) {
                        inread = SC_FREAD(buf, 1, inlen, infp);
-                       len = base64_decoder_decode(decoder, buf, outbuf, inread);
+                       len = g_base64_decode_step(buf, inlen, outbuf, &state, &save);
                        if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
                                uncanonicalize = FALSE;
                                null_bytes = TRUE;
@@ -440,7 +450,6 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                                got_error = FALSE;
                        }
                }
-               base64_decoder_free(decoder);
 
                if (uncanonicalize) {
                        rewind(tmpfp);
@@ -493,8 +502,12 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                return FALSE;
        }
 
-       g_stat(tmpfilename, &statbuf);
-       if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
+       if (g_stat(tmpfilename, &statbuf) < 0) {
+               FILE_OP_ERROR(tmpfilename, "stat");
+               return FALSE;
+       }
+
+       if (mimeinfo->tmp)
                claws_unlink(mimeinfo->data.filename);
        g_free(mimeinfo->data.filename);
        mimeinfo->data.filename = tmpfilename;
@@ -551,11 +564,14 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 #ifdef HAVE_FGETS_UNLOCKED
                flockfile(infp);
 #endif
-
+       } else {
+               procmime_fclose(outfp);
+               g_warning("Unknown mimeinfo");
+               return FALSE;
        }
 
        if (encoding == ENC_BASE64) {
-               gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
+               gchar inbuf[B64_LINE_SIZE], *out;
                FILE *tmp_fp = infp;
                gchar *tmp_file = NULL;
 
@@ -596,16 +612,18 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                while ((len = SC_FREAD(inbuf, sizeof(gchar),
                                    B64_LINE_SIZE, tmp_fp))
                       == B64_LINE_SIZE) {
-                       base64_encode(outbuf, inbuf, B64_LINE_SIZE);
-                       if (SC_FPUTS(outbuf, outfp) == EOF)
+                       out = g_base64_encode(inbuf, B64_LINE_SIZE);
+                       if (SC_FPUTS(out, outfp) == EOF)
                                err = TRUE;
+                       g_free(out);
                        if (SC_FPUTC('\n', outfp) == EOF)
                                err = TRUE;
                }
                if (len > 0 && SC_FEOF(tmp_fp)) {
-                       base64_encode(outbuf, inbuf, len);
-                       if (SC_FPUTS(outbuf, outfp) == EOF)
+                       out = g_base64_encode(inbuf, len);
+                       if (SC_FPUTS(out, outfp) == EOF)
                                err = TRUE;
+                       g_free(out);
                        if (SC_FPUTC('\n', outfp) == EOF)
                                err = TRUE;
                }
@@ -660,7 +678,10 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                        g_free(mimeinfo->data.mem);
        }
 
-       g_stat(tmpfilename, &statbuf);
+       if (g_stat(tmpfilename, &statbuf) < 0) {
+               FILE_OP_ERROR(tmpfilename, "stat");
+               return FALSE;
+       }
        mimeinfo->content = MIMECONTENT_FILE;
        mimeinfo->data.filename = tmpfilename;
        mimeinfo->tmp = TRUE;
@@ -1439,7 +1460,11 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                return;
        }
-       fseek(fp, mimeinfo->offset, SEEK_SET);
+       if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+               procmime_fclose(fp);
+               return;
+       }
        procheader_get_header_fields(fp, hentry);
        if (hentry[0].body != NULL) {
                tmp = conv_unmime_header(hentry[0].body, NULL, FALSE);
@@ -1512,7 +1537,11 @@ static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                return;
        }
-       fseek(fp, mimeinfo->offset, SEEK_SET);
+       if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+               procmime_fclose(fp);
+               return;
+       }
 
        if (original_msgid && disposition_notification_hdr) {
                hentry[0].body = g_strdup(original_msgid);
@@ -1616,7 +1645,7 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
                                {"Disposition:",
                                                   NULL, TRUE},
                                {NULL,             NULL, FALSE}};
-       gchar *p, *tmp;
+       gchar *tmp;
        gchar *boundary;
        gint boundary_len = 0, lastoffset = -1, i;
        gchar buf[BUFFSIZE];
@@ -1638,8 +1667,13 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
                return;
        }
 
-       fseek(fp, mimeinfo->offset, SEEK_SET);
-       while ((p = SC_FGETS(buf, sizeof(buf), fp)) != NULL && result == 0) {
+       if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+               procmime_fclose(fp);
+               return;
+       }
+
+       while (SC_FGETS(buf, sizeof(buf), fp) != NULL && result == 0) {
                if (ftell(fp) - 1 > (mimeinfo->offset + mimeinfo->length))
                        break;
 
@@ -1726,7 +1760,7 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
 
                value[0] = '\0';
                value++;
-               while (value[0] == ' ')
+               while (value[0] != '\0' && value[0] == ' ')
                        value++;
 
                down_attr = g_utf8_strdown(attribute, -1);
@@ -1773,12 +1807,12 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                        while (down_attr[strlen(down_attr)-1] == ' ') 
                                down_attr[strlen(down_attr)-1] = '\0';
                } 
-               if (value) {
-                       while (value[0] == ' ')
-                               value++;
-                       while (value[strlen(value)-1] == ' ') 
-                               value[strlen(value)-1] = '\0';
-               }               
+
+               while (value[0] != '\0' && value[0] == ' ')
+                       value++;
+               while (value[strlen(value)-1] == ' ') 
+                       value[strlen(value)-1] = '\0';
+
                if (down_attr && strrchr(down_attr, '*') != NULL) {
                        gchar *tmpattr;
 
@@ -2015,7 +2049,7 @@ static int procmime_parse_mimepart(MimeInfo *parent,
        mimeinfo->length = length;
 
        if (content_type != NULL) {
-               remove_space(content_type);
+               g_strchomp(content_type);
                procmime_parse_content_type(content_type, mimeinfo);
        } else {
                mimeinfo->type = MIMETYPE_TEXT;
@@ -2030,7 +2064,7 @@ static int procmime_parse_mimepart(MimeInfo *parent,
        }
 
        if (content_encoding != NULL) {
-               remove_space(content_encoding);
+               g_strchomp(content_encoding);
                procmime_parse_content_encoding(content_encoding, mimeinfo);
        } else {
                mimeinfo->encoding_type = ENC_UNKNOWN;
@@ -2052,7 +2086,7 @@ static int procmime_parse_mimepart(MimeInfo *parent,
                mimeinfo->location = NULL;
 
        if (content_disposition != NULL) {
-               remove_space(content_disposition);
+               g_strchomp(content_disposition);
                procmime_parse_content_disposition(content_disposition, mimeinfo);
        } else
                mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
@@ -2133,7 +2167,10 @@ static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offse
        MimeInfo *mimeinfo;
        struct stat buf;
 
-       g_stat(filename, &buf);
+       if (g_stat(filename, &buf) < 0) {
+               FILE_OP_ERROR(filename, "stat");
+               return NULL;
+       }
 
        mimeinfo = procmime_mimeinfo_new();
        mimeinfo->content = MIMECONTENT_FILE;
@@ -2459,7 +2496,11 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                        FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                        return -1;
                }
-               fseek(infp, mimeinfo->offset, SEEK_SET);
+               if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+                       FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+                       procmime_fclose(infp);
+                       return -1;
+               }
                while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
                        strcrchomp(buf);
                        if (buf[0] == '\n' && buf[1] == '\0')
@@ -2532,7 +2573,11 @@ static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
                        FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                        return -1;
                }
-               fseek(infp, mimeinfo->offset, SEEK_SET);
+               if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+                       FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+                       procmime_fclose(infp);
+                       return -1;
+               }
                while (SC_FGETS(buf, sizeof(buf), infp) == buf) {
                        if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
                                break;