find correct mime type of files with no suffix, e.g. Makefile
[claws.git] / src / procmime.c
index 2275c8991031eae2a05e3083498795f42da42b5d..cce1695800896b3cf792c5893728ce915753f343 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"
@@ -315,11 +314,13 @@ 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;
 
        EncodingType encoding = forced_encoding 
                                ? forced_encoding
@@ -372,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;
 
@@ -392,7 +396,6 @@ 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 = NULL;
@@ -416,17 +419,16 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                } 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")),
@@ -448,7 +450,6 @@ gboolean procmime_decode_content(MimeInfo *mimeinfo)
                                got_error = FALSE;
                        }
                }
-               base64_decoder_free(decoder);
 
                if (uncanonicalize) {
                        rewind(tmpfp);
@@ -471,7 +472,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)
@@ -526,7 +527,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)
@@ -550,7 +551,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;
                }
@@ -570,7 +571,7 @@ gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
        }
 
        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;
 
@@ -611,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;
                }
@@ -816,7 +819,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) {
@@ -834,7 +837,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);
@@ -1101,6 +1104,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
@@ -1252,7 +1257,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;
 }
@@ -1266,6 +1271,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))
@@ -2162,7 +2169,7 @@ 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;
 
        if (g_stat(filename, &buf) < 0) {
                FILE_OP_ERROR(filename, "stat");