fix quoting in reply to format=flowed message
[claws.git] / src / procmime.c
index 95b326a37b09c8d6c75c34e661e0e7f5a743c6e0..76ee3170244f32d89a1f31a6d8f39ba4c9bd5d92 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Hiroyuki Yamamoto & The Claws Mail Team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2016 Hiroyuki Yamamoto & The Claws Mail 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
@@ -14,7 +14,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -41,7 +40,6 @@
 
 #include "procmime.h"
 #include "procheader.h"
-#include "base64.h"
 #include "quoted-printable.h"
 #include "uuencode.h"
 #include "unmime.h"
@@ -139,13 +137,12 @@ static gboolean free_func(GNode *node, gpointer data)
        if (mimeinfo->privacy)
                privacy_free_privacydata(mimeinfo->privacy);
 
-       g_free(mimeinfo);
-
        return FALSE;
 }
 
-void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
+void procmime_mimeinfo_free_all(MimeInfo **mimeinfo_ptr)
 {
+       MimeInfo *mimeinfo = *mimeinfo_ptr;
        GNode *node;
 
        if (!mimeinfo)
@@ -155,6 +152,9 @@ void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
        g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
 
        g_node_destroy(node);
+
+       g_free(mimeinfo);
+       *mimeinfo_ptr = NULL;
 }
 
 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
@@ -293,7 +293,8 @@ static int procmime_fclose(FILE *fp)
                gint llen = 0;                                                  \
                strretchomp(lastline);                                          \
                llen = strlen(lastline);                                        \
-               if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ")) {        \
+               if (lastline[llen-1] == ' ' && strcmp(lastline,"-- ") &&        \
+                   strcmp(lastline,"> ")) {                                    \
                        /* this is flowed */                                    \
                        if (delsp)                                              \
                                lastline[llen-1] = '\0';                        \
@@ -315,11 +316,15 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        gint readend;
        gchar *tmpfilename;
        FILE *outfp, *infp;
-       struct stat statbuf;
+       GStatBuf statbuf;
        gboolean tmp_file = FALSE;
        gboolean flowed = FALSE;
        gboolean delsp = FALSE; 
        gboolean err = FALSE;
+       gint state = 0;
+       guint save = 0;
+
+       cm_return_val_if_fail(mimeinfo != NULL, FALSE);
 
        EncodingType encoding = forced_encoding 
                                ? forced_encoding
@@ -327,8 +332,6 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
        gchar lastline[BUFFSIZE];
        memset(lastline, 0, BUFFSIZE);
 
-       cm_return_val_if_fail(mimeinfo != NULL, FALSE);
-
        if (prefs_common.respect_flowed_format &&
            mimeinfo->type == MIMETYPE_TEXT && 
            !strcasecmp(mimeinfo->subtype, "plain")) {
@@ -349,16 +352,22 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
            ))
                return TRUE;
 
-       if (mimeinfo->type == MIMETYPE_MULTIPART && 
-           !strcasecmp(mimeinfo->subtype, "signed"))
+       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");
+               FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+               return FALSE;
+       }
+       if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+               procmime_fclose(infp);
                return FALSE;
        }
-       fseek(infp, mimeinfo->offset, SEEK_SET);
 
        outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
        if (!outfp) {
@@ -366,14 +375,18 @@ 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;
 
+       *buf = '\0';
        if (encoding == ENC_QUOTED_PRINTABLE) {
                while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
                        gint len;
                        len = qp_decode_line(buf);
-                       buf[len]='\0';
+                       buf[len] = '\0';
                        if (!flowed) {
                                if (SC_FWRITE(buf, 1, len, outfp) < len)
                                        err = TRUE;
@@ -386,10 +399,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;
 
@@ -398,7 +410,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        uncanonicalize = TRUE;
                        tmpfp = my_tmpfile();
                        if (!tmpfp) {
-                               perror("tmpfile");
+                               perror("my_tmpfile");
                                if (tmp_file) 
                                        procmime_fclose(outfp);
                                procmime_fclose(infp);
@@ -407,19 +419,19 @@ 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;
                        }
                        starting = FALSE;
                        if (((inread != inlen) || len < 0) && !got_error) {
-                               g_warning("Bad BASE64 content.\n");
+                               g_warning("Bad BASE64 content.");
                                if (SC_FWRITE(_("[Error decoding BASE64]\n"),
                                        sizeof(gchar),
                                        strlen(_("[Error decoding BASE64]\n")),
@@ -441,7 +453,6 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                                got_error = FALSE;
                        }
                }
-               base64_decoder_free(decoder);
 
                if (uncanonicalize) {
                        rewind(tmpfp);
@@ -450,8 +461,9 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                                if (SC_FPUTS(buf, outfp) == EOF)
                                        err = TRUE;
                        }
-                       procmime_fclose(tmpfp);
                }
+               if (tmpfp != outfp)
+                       procmime_fclose(tmpfp);
        } else if (encoding == ENC_X_UUENCODE) {
                gchar outbuf[BUFFSIZE];
                gint len;
@@ -464,7 +476,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                                len = fromuutobits(outbuf, buf);
                                if (len <= 0) {
                                        if (len < 0) 
-                                               g_warning("Bad UUENCODE content(%d)\n", len);
+                                               g_warning("Bad UUENCODE content (%d)", len);
                                        break;
                                }
                                if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
@@ -494,8 +506,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;
@@ -515,7 +531,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
        FILE *infp = NULL, *outfp;
        gint len;
        gchar *tmpfilename;
-       struct stat statbuf;
+       GStatBuf statbuf;
        gboolean err = FALSE;
 
        if (mimeinfo->content == MIMECONTENT_EMPTY)
@@ -539,7 +555,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
 
        if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
                if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
-                       g_warning("Can't open file %s\n", mimeinfo->data.filename);
+                       g_warning("Can't open file %s", mimeinfo->data.filename);
                        procmime_fclose(outfp);
                        return FALSE;
                }
@@ -552,11 +568,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;
 
@@ -597,16 +616,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;
                }
@@ -661,7 +682,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;
@@ -799,7 +823,7 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
                }
                ertf_parser_destroy(parser);
                conv_code_converter_destroy(conv);
-       } else if (mimeinfo->type == MIMETYPE_TEXT) {
+       } else if (mimeinfo->type == MIMETYPE_TEXT && mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
                while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
                        str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
                        if (str) {
@@ -817,7 +841,7 @@ gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
        }
 
        if (conv_fail)
-               g_warning("procmime_get_text_content(): Code conversion failed.\n");
+               g_warning("procmime_get_text_content(): Code conversion failed.");
 
        procmime_fclose(tmpfp);
        claws_unlink(tmpfile);
@@ -840,7 +864,7 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
        gboolean err;
 
        if ((outfp = my_tmpfile()) == NULL) {
-               perror("tmpfile");
+               perror("my_tmpfile");
                return NULL;
        }
 #ifdef HAVE_FGETS_UNLOCKED
@@ -926,17 +950,17 @@ scan_again:
                 * fully for non-empty parts
                 */
                short_scan = FALSE;
-               procmime_mimeinfo_free_all(mimeinfo);
+               procmime_mimeinfo_free_all(&mimeinfo);
                goto scan_again;
        } else if (!empty_ok && !short_scan) {
                /* if full scan didn't find a non-empty part, rescan
                 * accepting empty parts 
                 */
                empty_ok = TRUE;
-               procmime_mimeinfo_free_all(mimeinfo);
+               procmime_mimeinfo_free_all(&mimeinfo);
                goto scan_again;
        }
-       procmime_mimeinfo_free_all(mimeinfo);
+       procmime_mimeinfo_free_all(&mimeinfo);
 
        /* outfp already unlocked at this time */
        return outfp;
@@ -999,7 +1023,7 @@ FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
        if (partinfo)
                outfp = procmime_get_text_content(partinfo);
 
-       procmime_mimeinfo_free_all(mimeinfo);
+       procmime_mimeinfo_free_all(&mimeinfo);
 
        /* outfp already unlocked at this time */
        return outfp;
@@ -1019,7 +1043,7 @@ gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
 
        partinfo = mimeinfo;
        result = (find_encrypted_part(partinfo) != NULL);
-       procmime_mimeinfo_free_all(mimeinfo);
+       procmime_mimeinfo_free_all(&mimeinfo);
 
        return result;
 }
@@ -1084,6 +1108,8 @@ gchar *procmime_get_mime_type(const gchar *filename)
        base = g_path_get_basename(filename);
        if ((p = strrchr(base, '.')) != NULL)
                ext = g_utf8_strdown(p + 1, -1);
+       else
+               ext = g_utf8_strdown(base, -1);
        g_free(base);
 
 #ifndef G_OS_WIN32
@@ -1096,7 +1122,8 @@ gchar *procmime_get_mime_type(const gchar *filename)
                debug_print("got type %s for %s\n", str, ext);
                g_free(ext);
                return str;
-       } 
+       }
+       g_free(ext);
        return NULL;
 #else
        gchar *str = get_content_type_from_registry_with_ext(ext);
@@ -1151,12 +1178,11 @@ static GHashTable *procmime_get_mime_type_table(void)
 
                exts = g_strsplit(mime_type->extension, " ", 16);
                for (i = 0; exts[i] != NULL; i++) {
-                       /* use previously dup'd key on overwriting */
-                       if (g_hash_table_lookup(table, exts[i]))
-                               key = exts[i];
-                       else
+                       /* Don't overwrite previously inserted extension */
+                       if (!g_hash_table_lookup(table, exts[i])) {
                                key = g_strdup(exts[i]);
-                       g_hash_table_insert(table, key, mime_type);
+                               g_hash_table_insert(table, key, mime_type);
+                       }
                }
                g_strfreev(exts);
        }
@@ -1236,7 +1262,7 @@ GList *procmime_get_mime_type_list(void)
        procmime_fclose(fp);
 
        if (!list)
-               g_warning("Can't read mime.types\n");
+               g_warning("Can't read mime.types");
 
        return list;
 }
@@ -1250,6 +1276,8 @@ EncodingType procmime_get_encoding_for_charset(const gchar *charset)
                return ENC_7BIT;
        else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
                 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
+                !g_ascii_strcasecmp(charset, "X-MAC-CYRILLIC") ||
+                !g_ascii_strcasecmp(charset, "MAC-CYRILLIC") ||
                 !g_ascii_strcasecmp(charset, "Windows-1251"))
                return ENC_8BIT;
        else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
@@ -1441,7 +1469,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);
@@ -1514,7 +1546,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);
@@ -1553,7 +1589,7 @@ static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
                        debug_print("%s %s in %s\n", info?"found":"didn't find", orig_msg_id, outbox->path);
                        if (info) {
                                procmsg_msginfo_set_flags(info, MSG_RETRCPT_GOT, 0);
-                               procmsg_msginfo_free(info);
+                               procmsg_msginfo_free(&info);
                        }
                }
        }
@@ -1618,7 +1654,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];
@@ -1640,8 +1676,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;
 
@@ -1728,7 +1769,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);
@@ -1775,12 +1816,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;
 
@@ -2007,7 +2048,7 @@ static int procmime_parse_mimepart(MimeInfo *parent,
                         * this avoids DOSsing ourselves 
                         * with enormous messages
                         */
-                       procmime_mimeinfo_free_all(mimeinfo);
+                       procmime_mimeinfo_free_all(&mimeinfo);
                        return -1;                      
                }
                g_node_append(parent->node, mimeinfo->node);
@@ -2017,6 +2058,7 @@ static int procmime_parse_mimepart(MimeInfo *parent,
        mimeinfo->length = length;
 
        if (content_type != NULL) {
+               g_strchomp(content_type);
                procmime_parse_content_type(content_type, mimeinfo);
        } else {
                mimeinfo->type = MIMETYPE_TEXT;
@@ -2031,6 +2073,7 @@ static int procmime_parse_mimepart(MimeInfo *parent,
        }
 
        if (content_encoding != NULL) {
+               g_strchomp(content_encoding);
                procmime_parse_content_encoding(content_encoding, mimeinfo);
        } else {
                mimeinfo->encoding_type = ENC_UNKNOWN;
@@ -2051,9 +2094,10 @@ static int procmime_parse_mimepart(MimeInfo *parent,
        else
                mimeinfo->location = NULL;
 
-       if (content_disposition != NULL) 
+       if (content_disposition != NULL) {
+               g_strchomp(content_disposition);
                procmime_parse_content_disposition(content_disposition, mimeinfo);
-       else
+       else
                mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
 
        /* Call parser for mime type */
@@ -2130,9 +2174,12 @@ static void output_mime_structure(MimeInfo *mimeinfo, int indent)
 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset, gboolean short_scan)
 {
        MimeInfo *mimeinfo;
-       struct stat buf;
+       GStatBuf 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;
@@ -2307,7 +2354,7 @@ static void write_parameters(gpointer key, gpointer value, gpointer user_data)
                break;
 #else
        case ENC_AS_EXTENDED:
-               debug_print("Unhandled ENC_AS_EXTENDED.");
+               debug_print("Unhandled ENC_AS_EXTENDED.\n");
                break;
 #endif
        case ENC_AS_ENCWORD:
@@ -2458,14 +2505,18 @@ 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')
                                break;
                        if (skip && (buf[0] == ' ' || buf[0] == '\t'))
                                continue;
-                       if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
+                       if (g_ascii_strncasecmp(buf, "MIME-Version:", 13) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
                            g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
@@ -2503,7 +2554,7 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                return -1;
 
        child = (MimeInfo *) childnode->data;
-       if (fprintf(fp, "Mime-Version: 1.0\n") < 0) {
+       if (fprintf(fp, "MIME-Version: 1.0\n") < 0) {
                g_warning("failed to write mime version");
                return -1;
        }
@@ -2531,7 +2582,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;