Use GLib's implementation of Base64 instead of our own.
[claws.git] / src / procmime.c
index 887b3650e94ec5facb1a0129c869af90588b55be..a4630261dea75726d1c5b416d823ca8342541c72 100644 (file)
 #include "claws-features.h"
 #endif
 
+#define _GNU_SOURCE
+#include <stdio.h>
+
 #include "defs.h"
 
-#include <stdio.h>
 #include <glib.h>
 #include <glib/gi18n.h>
-#include <stdio.h>
 #include <string.h>
 #if HAVE_LOCALE_H
 #  include <locale.h>
@@ -40,7 +41,6 @@
 
 #include "procmime.h"
 #include "procheader.h"
-#include "base64.h"
 #include "quoted-printable.h"
 #include "uuencode.h"
 #include "unmime.h"
@@ -52,6 +52,7 @@
 #include "prefs_gtk.h"
 #include "alertpanel.h"
 #include "timing.h"
+#include "privacy.h"
 
 static GHashTable *procmime_get_mime_type_table        (void);
 static MimeInfo *procmime_scan_file_short(const gchar *filename);
@@ -252,6 +253,40 @@ const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *na
        return value;
 }
 
+#ifdef HAVE_FGETS_UNLOCKED
+#define SC_FGETS fgets_unlocked
+#define SC_FPUTS fputs_unlocked
+#define SC_FPUTC fputc_unlocked
+#define SC_FREAD fread_unlocked
+#define SC_FWRITE fwrite_unlocked
+#define SC_FEOF feof_unlocked
+#define SC_FERROR ferror_unlocked
+
+static FILE *procmime_fopen(const gchar *file, const gchar *mode)
+{
+       FILE *fp = g_fopen(file, mode);
+       if (!fp)
+               return NULL;
+       flockfile(fp);
+       return fp;
+}
+static int procmime_fclose(FILE *fp)
+{
+       funlockfile(fp);
+       return fclose(fp);
+}
+#else
+#define SC_FGETS fgets
+#define SC_FPUTS fputs
+#define SC_FPUTC fputc
+#define SC_FREAD fread
+#define SC_FWRITE fwrite
+#define SC_FEOF feof
+#define SC_FERROR ferror
+#define procmime_fopen g_fopen
+#define procmime_fclose fclose
+#endif
+
 #define FLUSH_LASTLINE() {                                                     \
        if (*lastline != '\0') {                                                \
                gint llen = 0;                                                  \
@@ -261,12 +296,12 @@ const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *na
                        /* this is flowed */                                    \
                        if (delsp)                                              \
                                lastline[llen-1] = '\0';                        \
-                       if (fputs(lastline, outfp) == EOF)                      \
+                       if (SC_FPUTS(lastline, outfp) == EOF)                   \
                                err = TRUE;                                     \
                } else {                                                        \
-                       if (fputs(lastline, outfp) == EOF)                      \
+                       if (SC_FPUTS(lastline, outfp) == EOF)                   \
                                err = TRUE;                                     \
-                       if (fputs("\n", outfp) == EOF)                          \
+                       if (SC_FPUTS("\n", outfp) == EOF)                               \
                                err = TRUE;                                     \
                }                                                               \
        }                                                                       \
@@ -284,13 +319,15 @@ 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
                                : mimeinfo->encoding_type;
        gchar lastline[BUFFSIZE];
        memset(lastline, 0, BUFFSIZE);
-                  
+
        cm_return_val_if_fail(mimeinfo != NULL, FALSE);
 
        if (prefs_common.respect_flowed_format &&
@@ -313,29 +350,42 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
            ))
                return TRUE;
 
-       infp = g_fopen(mimeinfo->data.filename, "rb");
+       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) {
                perror("tmpfile");
-               fclose(infp);
+               procmime_fclose(infp);
                return FALSE;
        }
+#ifdef HAVE_FGETS_UNLOCKED
+       flockfile(outfp);
+#endif
        tmp_file = TRUE;
        readend = mimeinfo->offset + mimeinfo->length;
 
        if (encoding == ENC_QUOTED_PRINTABLE) {
-               while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
+               while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
                        gint len;
                        len = qp_decode_line(buf);
                        buf[len]='\0';
                        if (!flowed) {
-                               if (fwrite(buf, 1, len, outfp) < len)
+                               if (SC_FWRITE(buf, 1, len, outfp) < len)
                                        err = TRUE;
                        } else {
                                FLUSH_LASTLINE();
@@ -346,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;
 
@@ -359,16 +408,20 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        tmpfp = my_tmpfile();
                        if (!tmpfp) {
                                perror("tmpfile");
-                               if (tmp_file) fclose(outfp);
-                               fclose(infp);
+                               if (tmp_file) 
+                                       procmime_fclose(outfp);
+                               procmime_fclose(infp);
                                return FALSE;
                        }
-               }
+#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 = fread(buf, 1, inlen, infp);
-                       len = base64_decoder_decode(decoder, buf, outbuf, inread);
+                       inread = SC_FREAD(buf, 1, inlen, infp);
+                       len = g_base64_decode_step(buf, inlen, outbuf, &state, &save);
                        if (uncanonicalize == TRUE && strlen(outbuf) < len && starting) {
                                uncanonicalize = FALSE;
                                null_bytes = TRUE;
@@ -376,7 +429,7 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        starting = FALSE;
                        if (((inread != inlen) || len < 0) && !got_error) {
                                g_warning("Bad BASE64 content.\n");
-                               if (fwrite(_("[Error decoding BASE64]\n"),
+                               if (SC_FWRITE(_("[Error decoding BASE64]\n"),
                                        sizeof(gchar),
                                        strlen(_("[Error decoding BASE64]\n")),
                                        tmpfp) < strlen(_("[Error decoding BASE64]\n")))
@@ -388,32 +441,31 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                                 * per block */
                                if (null_bytes) {
                                        /* we won't uncanonicalize, output to outfp directly */
-                                       if (fwrite(outbuf, sizeof(gchar), len, outfp) < len)
+                                       if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
                                                err = TRUE;
                                } else {
-                                       if (fwrite(outbuf, sizeof(gchar), len, tmpfp) < len)
+                                       if (SC_FWRITE(outbuf, sizeof(gchar), len, tmpfp) < len)
                                                err = TRUE;
                                }
                                got_error = FALSE;
                        }
                }
-               base64_decoder_free(decoder);
 
                if (uncanonicalize) {
                        rewind(tmpfp);
-                       while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
+                       while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
                                strcrchomp(buf);
-                               if (fputs(buf, outfp) == EOF)
+                               if (SC_FPUTS(buf, outfp) == EOF)
                                        err = TRUE;
                        }
-                       fclose(tmpfp);
+                       procmime_fclose(tmpfp);
                }
        } else if (encoding == ENC_X_UUENCODE) {
                gchar outbuf[BUFFSIZE];
                gint len;
                gboolean flag = FALSE;
 
-               while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
+               while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
                        if (!flag && strncmp(buf,"begin ", 6)) continue;
 
                        if (flag) {
@@ -423,15 +475,15 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                                                g_warning("Bad UUENCODE content(%d)\n", len);
                                        break;
                                }
-                               if (fwrite(outbuf, sizeof(gchar), len, outfp) < len)
+                               if (SC_FWRITE(outbuf, sizeof(gchar), len, outfp) < len)
                                        err = TRUE;
                        } else
                                flag = TRUE;
                }
        } else {
-               while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
+               while ((ftell(infp) < readend) && (SC_FGETS(buf, sizeof(buf), infp) != NULL)) {
                        if (!flowed) {
-                               if (fputs(buf, outfp) == EOF)
+                               if (SC_FPUTS(buf, outfp) == EOF)
                                        err = TRUE;
                        } else {
                                FLUSH_LASTLINE();
@@ -443,15 +495,19 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                        g_warning("write error");
        }
 
-       fclose(outfp);
-       fclose(infp);
+       procmime_fclose(outfp);
+       procmime_fclose(infp);
 
        if (err == TRUE) {
                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;
@@ -489,23 +545,33 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                perror("tmpfile");
                return FALSE;
        }
+#ifdef HAVE_FGETS_UNLOCKED
+       flockfile(outfp);
+#endif
 
        if (mimeinfo->content == MIMECONTENT_FILE && mimeinfo->data.filename) {
-               if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+               if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                        g_warning("Can't open file %s\n", mimeinfo->data.filename);
-                       fclose(outfp);
+                       procmime_fclose(outfp);
                        return FALSE;
                }
        } else if (mimeinfo->content == MIMECONTENT_MEM) {
                infp = str_open_as_stream(mimeinfo->data.mem);
                if (infp == NULL) {
-                       fclose(outfp);
+                       procmime_fclose(outfp);
                        return FALSE;
                }
+#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;
 
@@ -515,57 +581,62 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                                tmp_file = get_tmp_file();
                                if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
                                        g_free(tmp_file);
-                                       fclose(infp);
-                                       fclose(outfp);
+                                       procmime_fclose(infp);
+                                       procmime_fclose(outfp);
                                        return FALSE;
                                }
-                               if ((tmp_fp = g_fopen(tmp_file, "rb")) == NULL) {
+                               if ((tmp_fp = procmime_fopen(tmp_file, "rb")) == NULL) {
                                        FILE_OP_ERROR(tmp_file, "fopen");
                                        claws_unlink(tmp_file);
                                        g_free(tmp_file);
-                                       fclose(infp);
-                                       fclose(outfp);
+                                       procmime_fclose(infp);
+                                       procmime_fclose(outfp);
                                        return FALSE;
                                }
                        } else {
                                gchar *out = canonicalize_str(mimeinfo->data.mem);
-                               fclose(infp);
+                               procmime_fclose(infp);
                                infp = str_open_as_stream(out);
                                tmp_fp = infp;
                                g_free(out);
                                if (infp == NULL) {
-                                       fclose(outfp);
+                                       procmime_fclose(outfp);
                                        return FALSE;
                                }
+#ifdef HAVE_FGETS_UNLOCKED
+                               flockfile(infp);
+#endif
                        }
                }
 
-               while ((len = fread(inbuf, sizeof(gchar),
+               while ((len = SC_FREAD(inbuf, sizeof(gchar),
                                    B64_LINE_SIZE, tmp_fp))
                       == B64_LINE_SIZE) {
-                       base64_encode(outbuf, inbuf, B64_LINE_SIZE);
-                       if (fputs(outbuf, outfp) == EOF)
+                       out = g_base64_encode(inbuf, B64_LINE_SIZE);
+                       if (SC_FPUTS(out, outfp) == EOF)
                                err = TRUE;
-                       if (fputc('\n', outfp) == EOF)
+                       g_free(out);
+                       if (SC_FPUTC('\n', outfp) == EOF)
                                err = TRUE;
                }
-               if (len > 0 && feof(tmp_fp)) {
-                       base64_encode(outbuf, inbuf, len);
-                       if (fputs(outbuf, outfp) == EOF)
+               if (len > 0 && SC_FEOF(tmp_fp)) {
+                       out = g_base64_encode(inbuf, len);
+                       if (SC_FPUTS(out, outfp) == EOF)
                                err = TRUE;
-                       if (fputc('\n', outfp) == EOF)
+                       g_free(out);
+                       if (SC_FPUTC('\n', outfp) == EOF)
                                err = TRUE;
                }
 
                if (tmp_file) {
-                       fclose(tmp_fp);
+                       procmime_fclose(tmp_fp);
                        claws_unlink(tmp_file);
                        g_free(tmp_file);
                }
        } else if (encoding == ENC_QUOTED_PRINTABLE) {
                gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
 
-               while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
+               while (SC_FGETS(inbuf, sizeof(inbuf), infp) != NULL) {
                        qp_encode_line(outbuf, inbuf);
 
                        if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
@@ -573,52 +644,27 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
                                
                                tmpbuf += sizeof("From ")-1;
                                
-                               if (fputs("=46rom ", outfp) == EOF)
+                               if (SC_FPUTS("=46rom ", outfp) == EOF)
                                        err = TRUE;
-                               if (fputs(tmpbuf, outfp) == EOF)
+                               if (SC_FPUTS(tmpbuf, outfp) == EOF)
                                        err = TRUE;
                        } else {
-                               if (fputs(outbuf, outfp) == EOF)
+                               if (SC_FPUTS(outbuf, outfp) == EOF)
                                        err = TRUE;
                        }
                }
        } else {
-               gchar buf[MAXSMTPTEXTLEN+1];
-               gint leftover = 0;
-
-               while (fgets(buf + leftover,
-                       sizeof(buf) - leftover,
-                       infp) != NULL)
-               {
-                   gchar *l, *c = buf;
-                   leftover = 0;
-                   
-                   while (*c != '\0') {
-                       if (
-                           *c == '\n'
-                           || (*c == '\r' && *(c+1) == '\n'))
-                       {
-                           *c = '\0';
-                           break;
-                       }
-                       c++;
-                   }
-                   while (c - buf > MAXSMTPTEXTLEN - 2) {
-                       *c = *(c-1);
-                       *--c = '\0';
-                       leftover++;
-                   }
-                   
-                   if (fputs(buf, outfp) == EOF || putc('\n', outfp) == EOF)
-                       err = TRUE;
-
-                   for (l = buf; l-buf < leftover; l++)
-                       *l = *++c;
+               gchar buf[BUFFSIZE];
+
+               while (SC_FGETS(buf, sizeof(buf), infp) != NULL) {
+                       strcrchomp(buf);
+                       if (SC_FPUTS(buf, outfp) == EOF)
+                               err = TRUE;
                }
        }
 
-       fclose(outfp);
-       fclose(infp);
+       procmime_fclose(outfp);
+       procmime_fclose(infp);
 
        if (err == TRUE)
                return FALSE;
@@ -632,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;
@@ -656,7 +705,7 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
                return -EINVAL;
 
-       if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+       if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                saved_errno = errno;
                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                return -(saved_errno);
@@ -664,30 +713,30 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
                saved_errno = errno;
                FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
-               fclose(infp);
+               procmime_fclose(infp);
                return -(saved_errno);
        }
-       if ((outfp = g_fopen(outfile, "wb")) == NULL) {
+       if ((outfp = procmime_fopen(outfile, "wb")) == NULL) {
                saved_errno = errno;
                FILE_OP_ERROR(outfile, "fopen");
-               fclose(infp);
+               procmime_fclose(infp);
                return -(saved_errno);
        }
 
        restlength = mimeinfo->length;
 
-       while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
-               if (fwrite(buf, 1, readlength, outfp) != readlength) {
+       while ((restlength > 0) && ((readlength = SC_FREAD(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
+               if (SC_FWRITE(buf, 1, readlength, outfp) != readlength) {
                        saved_errno = errno;
-                       fclose(infp);
-                       fclose(outfp);
+                       procmime_fclose(infp);
+                       procmime_fclose(outfp);
                        return -(saved_errno);
                }
                restlength -= readlength;
        }
 
-       fclose(infp);
-       if (fclose(outfp) == EOF) {
+       procmime_fclose(infp);
+       if (procmime_fclose(outfp) == EOF) {
                saved_errno = errno;
                FILE_OP_ERROR(outfile, "fclose");
                claws_unlink(outfile);
@@ -697,41 +746,37 @@ gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
        return 0;
 }
 
-FILE *procmime_get_text_content(MimeInfo *mimeinfo)
+gboolean procmime_scan_text_content(MimeInfo *mimeinfo,
+               gboolean (*scan_callback)(const gchar *str, gpointer cb_data),
+               gpointer cb_data) 
 {
-       FILE *tmpfp, *outfp;
+       FILE *tmpfp;
        const gchar *src_codeset;
        gboolean conv_fail = FALSE;
        gchar buf[BUFFSIZE];
        gchar *str;
        gchar *tmpfile;
-       gboolean err = FALSE;
+       gboolean scan_ret = FALSE;
 
-       cm_return_val_if_fail(mimeinfo != NULL, NULL);
+       cm_return_val_if_fail(mimeinfo != NULL, TRUE);
+       cm_return_val_if_fail(scan_callback != NULL, TRUE);
 
        if (!procmime_decode_content(mimeinfo))
-               return NULL;
+               return TRUE;
 
        tmpfile = procmime_get_tmp_file_name(mimeinfo);
        if (tmpfile == NULL)
-               return NULL;
+               return TRUE;
 
        if (procmime_get_part(tmpfile, mimeinfo) < 0) {
                g_free(tmpfile);
-               return NULL;
+               return TRUE;
        }
 
-       tmpfp = g_fopen(tmpfile, "rb");
+       tmpfp = procmime_fopen(tmpfile, "rb");
        if (tmpfp == NULL) {
                g_free(tmpfile);
-               return NULL;
-       }
-
-       if ((outfp = my_tmpfile()) == NULL) {
-               perror("tmpfile");
-               fclose(tmpfp);
-               g_free(tmpfile);
-               return NULL;
+               return TRUE;
        }
 
        src_codeset = forced_charset
@@ -747,6 +792,8 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                src_codeset = CS_GB18030;
        else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_GB2312))
                src_codeset = CS_GB18030;
+       else if (!forced_charset && src_codeset && !strcasecmp(src_codeset, CS_X_VIET_VPS))
+               src_codeset = CS_WINDOWS_874;
 
        if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
                SC_HTMLParser *parser;
@@ -755,8 +802,8 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                conv = conv_code_converter_new(src_codeset);
                parser = sc_html_parser_new(tmpfp, conv);
                while ((str = sc_html_parse(parser)) != NULL) {
-                       if (fputs(str, outfp) == EOF)
-                               err = TRUE;
+                       if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
+                               break;
                }
                sc_html_parser_destroy(parser);
                conv_code_converter_destroy(conv);
@@ -767,22 +814,24 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
                conv = conv_code_converter_new(src_codeset);
                parser = ertf_parser_new(tmpfp, conv);
                while ((str = ertf_parse(parser)) != NULL) {
-                       if (fputs(str, outfp) == EOF)
-                               err = TRUE;
+                       if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
+                               break;
                }
                ertf_parser_destroy(parser);
                conv_code_converter_destroy(conv);
        } else if (mimeinfo->type == MIMETYPE_TEXT) {
-               while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
+               while (SC_FGETS(buf, sizeof(buf), tmpfp) != NULL) {
                        str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
                        if (str) {
-                               if (fputs(str, outfp) == EOF)
-                                       err = TRUE;
+                               if ((scan_ret = scan_callback(str, cb_data)) == TRUE) {
+                                       g_free(str);
+                                       break;
+                               }
                                g_free(str);
                        } else {
                                conv_fail = TRUE;
-                               if (fputs(buf, outfp) == EOF)
-                                       err = TRUE;
+                               if ((scan_ret = scan_callback(buf, cb_data)) == TRUE)
+                                       break;
                        }
                }
        }
@@ -790,17 +839,46 @@ FILE *procmime_get_text_content(MimeInfo *mimeinfo)
        if (conv_fail)
                g_warning("procmime_get_text_content(): Code conversion failed.\n");
 
-       fclose(tmpfp);
-       rewind(outfp);
+       procmime_fclose(tmpfp);
        claws_unlink(tmpfile);
        g_free(tmpfile);
 
-       if (err == TRUE) {
-               fclose(outfp);
+       return scan_ret;
+}
+
+static gboolean scan_fputs_cb(const gchar *str, gpointer fp)
+{
+       if (SC_FPUTS(str, (FILE *)fp) == EOF)
+               return TRUE;
+       
+       return FALSE;
+}
+
+FILE *procmime_get_text_content(MimeInfo *mimeinfo)
+{
+       FILE *outfp;
+       gboolean err;
+
+       if ((outfp = my_tmpfile()) == NULL) {
+               perror("tmpfile");
                return NULL;
        }
+#ifdef HAVE_FGETS_UNLOCKED
+       flockfile(outfp);
+#endif
 
+       err = procmime_scan_text_content(mimeinfo, scan_fputs_cb, outfp);
+
+       rewind(outfp);
+       if (err == TRUE) {
+               procmime_fclose(outfp);
+               return NULL;
+       }
+#ifdef HAVE_FGETS_UNLOCKED
+       funlockfile(outfp);
+#endif
        return outfp;
+
 }
 
 FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
@@ -822,7 +900,7 @@ FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
                return NULL;
        }
 
-       outfp = g_fopen(tmpfile, "rb");
+       outfp = procmime_fopen(tmpfile, "rb");
        if (outfp == NULL) {
                g_unlink(tmpfile);
                g_free(tmpfile);
@@ -832,6 +910,9 @@ FILE *procmime_get_binary_content(MimeInfo *mimeinfo)
        g_unlink(tmpfile);
        g_free(tmpfile);
 
+#ifdef HAVE_FGETS_UNLOCKED
+       funlockfile(outfp);
+#endif
        return outfp;
 }
 
@@ -876,6 +957,8 @@ scan_again:
                goto scan_again;
        }
        procmime_mimeinfo_free_all(mimeinfo);
+
+       /* outfp already unlocked at this time */
        return outfp;
 }
 
@@ -938,6 +1021,7 @@ FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
 
        procmime_mimeinfo_free_all(mimeinfo);
 
+       /* outfp already unlocked at this time */
        return outfp;
 }
 
@@ -1087,12 +1171,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);
        }
@@ -1114,14 +1197,14 @@ GList *procmime_get_mime_type_list(void)
                return mime_type_list;
        
 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
-       if ((fp = g_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL) 
+       if ((fp = procmime_fopen(DATAROOTDIR "/mime/globs", "rb")) == NULL) 
 #else
-       if ((fp = g_fopen("/usr/share/mime/globs", "rb")) == NULL) 
+       if ((fp = procmime_fopen("/usr/share/mime/globs", "rb")) == NULL) 
 #endif
        {
                fp_is_glob_file = FALSE;
-               if ((fp = g_fopen("/etc/mime.types", "rb")) == NULL) {
-                       if ((fp = g_fopen(SYSCONFDIR "/mime.types", "rb")) 
+               if ((fp = procmime_fopen("/etc/mime.types", "rb")) == NULL) {
+                       if ((fp = procmime_fopen(SYSCONFDIR "/mime.types", "rb")) 
                                == NULL) {
                                FILE_OP_ERROR(SYSCONFDIR "/mime.types", 
                                        "fopen");
@@ -1130,7 +1213,7 @@ GList *procmime_get_mime_type_list(void)
                }
        }
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
                p = strchr(buf, '#');
                if (p) *p = '\0';
                g_strstrip(buf);
@@ -1169,7 +1252,7 @@ GList *procmime_get_mime_type_list(void)
                list = g_list_append(list, mime_type);
        }
 
-       fclose(fp);
+       procmime_fclose(fp);
 
        if (!list)
                g_warning("Can't read mime.types\n");
@@ -1200,50 +1283,33 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *ha
 {
        FILE *fp;
        guchar buf[BUFFSIZE];
-       gboolean cr = FALSE;
        size_t len;
-       gint linelen = 0, maxlinelen = 0;
        size_t octet_chars = 0;
        size_t total_len = 0;
        gfloat octet_percentage;
+       gboolean force_b64 = FALSE;
 
-       if ((fp = g_fopen(file, "rb")) == NULL) {
+       if ((fp = procmime_fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                return ENC_UNKNOWN;
        }
 
-       while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
+       while ((len = SC_FREAD(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
                guchar *p;
                gint i;
 
                for (p = buf, i = 0; i < len; ++p, ++i) {
-                       switch (*p) {
-                       case '\n':
-                               if (cr) linelen--;
-                               maxlinelen = MAX(linelen, maxlinelen);
-                               linelen = 0;
-                               cr = FALSE;
-                               break;
-                       case '\r':
-                               cr = TRUE;
-                               linelen++;
-                               break;
-                       case '\0':
+                       if (*p & 0x80)
+                               ++octet_chars;
+                       if (*p == '\0') {
+                               force_b64 = TRUE;
                                *has_binary = TRUE;
-                               maxlinelen = G_MAXINT;
-                               cr = FALSE;
-                               break;
-                       default:
-                               if (*p & 0x80)
-                                       octet_chars++;
-                               linelen++;
-                               cr = FALSE;
                        }
                }
                total_len += len;
        }
 
-       fclose(fp);
+       procmime_fclose(fp);
        
        if (total_len > 0)
                octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
@@ -1251,20 +1317,15 @@ EncodingType procmime_get_encoding_for_text_file(const gchar *file, gboolean *ha
                octet_percentage = 0.0;
 
        debug_print("procmime_get_encoding_for_text_file(): "
-                   "8bit chars: %zd / %zd (%f%%). "
-                   "maximum line length: %d chars\n",
-                   octet_chars, total_len, 100.0 * octet_percentage,
-                   maxlinelen);
+                   "8bit chars: %zd / %zd (%f%%)\n", octet_chars, total_len,
+                   100.0 * octet_percentage);
 
-       if (octet_percentage > 0.20) {
+       if (octet_percentage > 0.20 || force_b64) {
                debug_print("using BASE64\n");
                return ENC_BASE64;
-       } else if (maxlinelen > MAXSMTPTEXTLEN-2) {
+       } else if (octet_chars > 0) {
                debug_print("using quoted-printable\n");
                return ENC_QUOTED_PRINTABLE;
-       } else if (octet_chars > 0) {
-               debug_print("using 8bit\n");
-               return ENC_8BIT;
        } else {
                debug_print("using 7bit\n");
                return ENC_7BIT;
@@ -1394,12 +1455,16 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
 
        procmime_decode_content(mimeinfo);
 
-       fp = g_fopen(mimeinfo->data.filename, "rb");
+       fp = procmime_fopen(mimeinfo->data.filename, "rb");
        if (fp == NULL) {
                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);
@@ -1433,7 +1498,7 @@ static void procmime_parse_message_rfc822(MimeInfo *mimeinfo, gboolean short_sca
         }
   
        content_start = ftell(fp);
-       fclose(fp);
+       procmime_fclose(fp);
        
        len = mimeinfo->length - (content_start - mimeinfo->offset);
        if (len < 0)
@@ -1467,12 +1532,16 @@ static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
        procmime_decode_content(mimeinfo);
 
        debug_print("parse disposition notification\n");
-       fp = g_fopen(mimeinfo->data.filename, "rb");
+       fp = procmime_fopen(mimeinfo->data.filename, "rb");
        if (fp == NULL) {
                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);
@@ -1481,7 +1550,7 @@ static void procmime_parse_disposition_notification(MimeInfo *mimeinfo,
                procheader_get_header_fields(fp, hentry);
        }
     
-        fclose(fp);
+        procmime_fclose(fp);
 
        if (!hentry[0].body || !hentry[1].body) {
                debug_print("MsgId %s, Disp %s\n",
@@ -1576,13 +1645,12 @@ 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];
        FILE *fp;
        int result = 0;
-       gboolean done = FALSE;
        gboolean start_found = FALSE;
        gboolean end_found = FALSE;
 
@@ -1593,14 +1661,19 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
 
        procmime_decode_content(mimeinfo);
 
-       fp = g_fopen(mimeinfo->data.filename, "rb");
+       fp = procmime_fopen(mimeinfo->data.filename, "rb");
        if (fp == NULL) {
                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                return;
        }
 
-       fseek(fp, mimeinfo->offset, SEEK_SET);
-       while ((p = 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;
 
@@ -1618,10 +1691,9 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
                                                        hentry[6].body, hentry[7].body,
                                                        mimeinfo->data.filename, lastoffset,
                                                        len, short_scan);
-                               if (result == 1 && short_scan) {
-                                       done = TRUE;
+                               if (result == 1 && short_scan)
                                        break;
-                               }
+                               
                        } 
                        
                        if (buf[2 + boundary_len]     == '-' &&
@@ -1657,7 +1729,7 @@ static void procmime_parse_multipart(MimeInfo *mimeinfo, gboolean short_scan)
                g_free(hentry[i].body);
                hentry[i].body = NULL;
        }
-       fclose(fp);
+       procmime_fclose(fp);
 }
 
 static void parse_parameters(const gchar *parameters, GHashTable *table)
@@ -1688,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);
@@ -1735,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;
 
@@ -1749,15 +1821,15 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                        tmp[0] = '\0';
 
                        if ((tmp[1] == '0') && (tmp[2] == '\0') && 
-                           (g_slist_find_custom(concatlist, down_attr, g_strcmp0) == NULL))
+                           (g_slist_find_custom(concatlist, down_attr, (GCompareFunc)g_strcmp0) == NULL))
                                concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
 
-                       if (convert && (g_slist_find_custom(convlist, down_attr, g_strcmp0) == NULL))
+                       if (convert && (g_slist_find_custom(convlist, tmpattr, (GCompareFunc)g_strcmp0) == NULL))
                                convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
 
                        g_free(tmpattr);
                } else if (convert) {
-                       if (g_slist_find_custom(convlist, down_attr, g_strcmp0) == NULL)
+                       if (g_slist_find_custom(convlist, down_attr, (GCompareFunc)g_strcmp0) == NULL)
                                convlist = g_slist_prepend(convlist, g_strdup(down_attr));
                }
 
@@ -1788,8 +1860,7 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
                g_string_free(value, TRUE);
        }
-       slist_free_strings(concatlist);
-       g_slist_free(concatlist);
+       slist_free_strings_full(concatlist);
 
        for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
                gchar *attribute, *key, *value;
@@ -1801,6 +1872,8 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
                        continue;
 
                charset = value;
+               if (charset == NULL)
+                       continue;
                lang = strchr(charset, '\'');
                if (lang == NULL)
                        continue;
@@ -1820,8 +1893,7 @@ static void parse_parameters(const gchar *parameters, GHashTable *table)
 
                g_hash_table_insert(table, g_strdup(attribute), newvalue);
        }
-       slist_free_strings(convlist);
-       g_slist_free(convlist);
+       slist_free_strings_full(convlist);
 
        g_free(params);
 }      
@@ -1977,6 +2049,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;
@@ -1991,6 +2064,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;
@@ -2011,9 +2085,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 */
@@ -2092,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;
@@ -2141,10 +2219,10 @@ static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean s
        cm_return_val_if_fail(filename != NULL, NULL);
 
        /* Open file */
-       if ((fp = g_fopen(filename, "rb")) == NULL)
+       if ((fp = procmime_fopen(filename, "rb")) == NULL)
                return NULL;
        /* Skip queue header */
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
                /* new way */
                if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
                        strlen("X-Claws-End-Special-Headers:"))) ||
@@ -2163,7 +2241,7 @@ static MimeInfo *procmime_scan_queue_file_full(const gchar *filename, gboolean s
                }
        }
        offset = ftell(fp);
-       fclose(fp);
+       procmime_fclose(fp);
 
        mimeinfo = procmime_scan_file_with_offset(filename, offset, short_scan);
 
@@ -2414,18 +2492,22 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
        /* write header */
        switch (mimeinfo->content) {
        case MIMECONTENT_FILE:
-               if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+               if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                        FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                        return -1;
                }
-               fseek(infp, mimeinfo->offset, SEEK_SET);
-               while (fgets(buf, sizeof(buf), infp) == buf) {
+               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 ||
@@ -2436,19 +2518,19 @@ static gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
                                continue;
                        }
                        len = strlen(buf);
-                       if (fwrite(buf, sizeof(gchar), len, fp) < len) {
+                       if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
                                g_warning("failed to dump %zd bytes from file", len);
-                               fclose(infp);
+                               procmime_fclose(infp);
                                return -1;
                        }
                        skip = FALSE;
                }
-               fclose(infp);
+               procmime_fclose(infp);
                break;
 
        case MIMECONTENT_MEM:
                len = strlen(mimeinfo->data.mem);
-               if (fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
+               if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len) {
                        g_warning("failed to dump %zd bytes from mem", len);
                        return -1;
                }
@@ -2463,7 +2545,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;
        }
@@ -2487,22 +2569,26 @@ static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
 
        switch (mimeinfo->content) {
        case MIMECONTENT_FILE:
-               if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+               if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                        FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                        return -1;
                }
-               fseek(infp, mimeinfo->offset, SEEK_SET);
-               while (fgets(buf, sizeof(buf), infp) == buf) {
+               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;
                        len = strlen(buf);
-                       if (fwrite(buf, sizeof(gchar), len, fp) < len) {
+                       if (SC_FWRITE(buf, sizeof(gchar), len, fp) < len) {
                                g_warning("failed to write %zd", len);
-                               fclose(infp);
+                               procmime_fclose(infp);
                                return -1;
                        }
                }
-               fclose(infp);
+               procmime_fclose(infp);
                break;
 
        case MIMECONTENT_MEM:
@@ -2511,7 +2597,7 @@ static gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
                    (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
                        *(str2 - 2) = '\0';
                len = strlen(str);
-               if (fwrite(str, sizeof(gchar), len, fp) < len) {
+               if (SC_FWRITE(str, sizeof(gchar), len, fp) < len) {
                        g_warning("failed to write %zd from mem", len);
                        g_free(str);
                        return -1;
@@ -2556,17 +2642,17 @@ gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
        if (G_NODE_IS_LEAF(mimeinfo->node)) {
                switch (mimeinfo->content) {
                case MIMECONTENT_FILE:
-                       if ((infp = g_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+                       if ((infp = procmime_fopen(mimeinfo->data.filename, "rb")) == NULL) {
                                FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
                                return -1;
                        }
                        copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
-                       fclose(infp);
+                       procmime_fclose(infp);
                        return 0;
 
                case MIMECONTENT_MEM:
                        len = strlen(mimeinfo->data.mem);
-                       if (fwrite(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
+                       if (SC_FWRITE(mimeinfo->data.mem, sizeof(gchar), len, fp) < len)
                                return -1;
                        return 0;