#include "procmime.h"
#include "procheader.h"
-#include "base64.h"
#include "quoted-printable.h"
#include "uuencode.h"
#include "unmime.h"
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
: mimeinfo->encoding_type;
gchar lastline[BUFFSIZE];
memset(lastline, 0, BUFFSIZE);
-
+
cm_return_val_if_fail(mimeinfo != NULL, FALSE);
if (prefs_common.respect_flowed_format &&
))
return TRUE;
+ 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) {
procmime_fclose(infp);
return FALSE;
}
+#ifdef HAVE_FGETS_UNLOCKED
+ flockfile(outfp);
+#endif
tmp_file = TRUE;
readend = mimeinfo->offset + mimeinfo->length;
} 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;
#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")),
got_error = FALSE;
}
}
- base64_decoder_free(decoder);
if (uncanonicalize) {
rewind(tmpfp);
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)
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;
FILE *infp = NULL, *outfp;
gint len;
gchar *tmpfilename;
- struct stat statbuf;
+ GStatBuf statbuf;
gboolean err = FALSE;
if (mimeinfo->content == MIMECONTENT_EMPTY)
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;
}
#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;
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;
}
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;
}
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) {
g_free(str);
} else {
conv_fail = TRUE;
- if ((scan_ret = scan_callback(str, cb_data)) == TRUE)
+ if ((scan_ret = scan_callback(buf, cb_data)) == TRUE)
break;
}
}
}
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);
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
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);
}
procmime_fclose(fp);
if (!list)
- g_warning("Can't read mime.types\n");
+ g_warning("Can't read mime.types");
return list;
}
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))
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);
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);
{"Disposition:",
NULL, TRUE},
{NULL, NULL, FALSE}};
- gchar *p, *tmp;
+ gchar *tmp;
gchar *boundary;
gint boundary_len = 0, lastoffset = -1, i;
gchar buf[BUFFSIZE];
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;
value[0] = '\0';
value++;
- while (value[0] == ' ')
+ while (value[0] != '\0' && value[0] == ' ')
value++;
down_attr = g_utf8_strdown(attribute, -1);
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;
mimeinfo->length = length;
if (content_type != NULL) {
+ g_strchomp(content_type);
procmime_parse_content_type(content_type, mimeinfo);
} else {
mimeinfo->type = MIMETYPE_TEXT;
}
if (content_encoding != NULL) {
+ g_strchomp(content_encoding);
procmime_parse_content_encoding(content_encoding, mimeinfo);
} else {
mimeinfo->encoding_type = ENC_UNKNOWN;
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 */
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;
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 ||
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;
}
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;