#include "codeconv.h"
#include "utils.h"
#include "prefs_common.h"
-
#include "prefs_gtk.h"
static GHashTable *procmime_get_mime_type_table (void);
gchar *tmpfilename;
FILE *outfp, *infp;
struct stat statbuf;
+ gboolean tmp_file = FALSE;
EncodingType encoding = forced_encoding
? forced_encoding
perror("tmpfile");
return FALSE;
}
-
+ tmp_file = TRUE;
readend = mimeinfo->offset + mimeinfo->length;
if (encoding == ENC_QUOTED_PRINTABLE) {
gchar outbuf[BUFFSIZE];
gint len;
Base64Decoder *decoder;
+ gboolean uncanonicalize = FALSE;
+ FILE *tmpfp = outfp;
+
+ if (mimeinfo->type == MIMETYPE_TEXT ||
+ mimeinfo->type == MIMETYPE_MESSAGE) {
+ uncanonicalize = TRUE;
+ tmpfp = my_tmpfile();
+ if (!tmpfp) {
+ perror("tmpfile");
+ if (tmp_file) fclose(outfp);
+ return FALSE;
+ }
+ }
decoder = base64_decoder_new();
while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
g_warning("Bad BASE64 content\n");
break;
}
- fwrite(outbuf, sizeof(gchar), len, outfp);
+ fwrite(outbuf, sizeof(gchar), len, tmpfp);
}
base64_decoder_free(decoder);
+
+ if (uncanonicalize) {
+ rewind(tmpfp);
+ while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
+ strcrchomp(buf);
+ fputs(buf, outfp);
+ }
+ fclose(tmpfp);
+ }
} else if (encoding == ENC_X_UUENCODE) {
gchar outbuf[BUFFSIZE];
gint len;
if (encoding == ENC_BASE64) {
gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
+ FILE *tmp_fp = infp;
+ gchar *tmp_file = NULL;
+
+ if (mimeinfo->type == MIMETYPE_TEXT ||
+ mimeinfo->type == MIMETYPE_MESSAGE) {
+ if (mimeinfo->content == MIMECONTENT_FILE) {
+ tmp_file = get_tmp_file();
+ if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
+ g_free(tmp_file);
+ fclose(infp);
+ return FALSE;
+ }
+ if ((tmp_fp = fopen(tmp_file, "rb")) == NULL) {
+ FILE_OP_ERROR(tmp_file, "fopen");
+ unlink(tmp_file);
+ g_free(tmp_file);
+ fclose(infp);
+ return FALSE;
+ }
+ } else {
+ gchar *out = canonicalize_str(mimeinfo->data.mem);
+ fclose(infp);
+ infp = str_open_as_stream(out);
+ g_free(out);
+ if (infp == NULL)
+ return FALSE;
+ }
+ }
while ((len = fread(inbuf, sizeof(gchar),
- B64_LINE_SIZE, infp))
+ B64_LINE_SIZE, tmp_fp))
== B64_LINE_SIZE) {
base64_encode(outbuf, inbuf, B64_LINE_SIZE);
fputs(outbuf, outfp);
fputc('\n', outfp);
}
- if (len > 0 && feof(infp)) {
+ if (len > 0 && feof(tmp_fp)) {
base64_encode(outbuf, inbuf, len);
fputs(outbuf, outfp);
fputc('\n', outfp);
}
+
+ if (tmp_file) {
+ fclose(tmp_fp);
+ unlink(tmp_file);
+ g_free(tmp_file);
+ }
} else if (encoding == ENC_QUOTED_PRINTABLE) {
gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
}
gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
- const gchar *str, gboolean case_sens)
+ const gchar *str, StrFindFunc find_func)
{
FILE *outfp;
gchar buf[BUFFSIZE];
- gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
g_return_val_if_fail(mimeinfo != NULL, FALSE);
g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
g_return_val_if_fail(str != NULL, FALSE);
+ g_return_val_if_fail(find_func != NULL, FALSE);
outfp = procmime_get_text_content(mimeinfo);
if (!outfp)
return FALSE;
- if (case_sens)
- StrFindFunc = strstr;
- else
- StrFindFunc = strcasestr;
-
while (fgets(buf, sizeof(buf), outfp) != NULL) {
- if (StrFindFunc(buf, str) != NULL) {
+ strretchomp(buf);
+ if (find_func(buf, str)) {
fclose(outfp);
return TRUE;
}
}
gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
- gboolean case_sens)
+ StrFindFunc find_func)
{
MimeInfo *mimeinfo;
MimeInfo *partinfo;
g_return_val_if_fail(msginfo != NULL, FALSE);
g_return_val_if_fail(str != NULL, FALSE);
+ g_return_val_if_fail(find_func != NULL, FALSE);
filename = procmsg_get_message_file(msginfo);
if (!filename) return FALSE;
partinfo = procmime_mimeinfo_next(partinfo)) {
if (partinfo->type == MIMETYPE_TEXT) {
if (procmime_find_string_part
- (partinfo, filename, str, case_sens) == TRUE) {
+ (partinfo, filename, str, find_func) == TRUE) {
found = TRUE;
break;
}
gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
{
static guint32 id = 0;
- const gchar *base;
+ gchar *base;
gchar *filename;
gchar f_prefix[10];
g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
- base = "mimetmp.html";
+ base = g_strdup("mimetmp.html");
else {
const gchar *basetmp;
basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
if (basetmp == NULL)
basetmp = "mimetmp";
- base = g_basename(basetmp);
- if (*base == '\0') base = "mimetmp";
- Xstrdup_a(base, base, return NULL);
+ base = g_path_get_basename(basetmp);
+ if (*base == '\0') base = g_strdup("mimetmp");
subst_for_shellsafe_filename(base);
}
filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
f_prefix, base, NULL);
+ g_free(base);
return filename;
}
MimeType *mime_type;
const gchar *p;
gchar *ext;
+ gchar *base;
if (!mime_type_table) {
mime_type_table = procmime_get_mime_type_table();
if (!mime_type_table) return NULL;
}
- filename = g_basename(filename);
- p = strrchr(filename, '.');
+ base = g_path_get_basename(filename);
+ p = strrchr(base, '.');
+ g_free(base);
if (!p) return NULL;
Xstrdup_a(ext, p + 1, return NULL);
return ENC_8BIT;
}
-EncodingType procmime_get_encoding_for_file(const gchar *file)
+EncodingType procmime_get_encoding_for_text_file(const gchar *file)
{
FILE *fp;
- guchar buf[BUFSIZ];
+ guchar buf[BUFFSIZE];
size_t len;
+ size_t octet_chars = 0;
+ size_t total_len = 0;
+ gfloat octet_percentage;
if ((fp = fopen(file, "rb")) == NULL) {
FILE_OP_ERROR(file, "fopen");
return ENC_UNKNOWN;
}
- while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
+ while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
guchar *p;
gint i;
- for (p = buf, i = 0; i < len; p++, i++) {
- if (*p & 0x80) {
- fclose(fp);
- return ENC_BASE64;
- }
+ for (p = buf, i = 0; i < len; ++p, ++i) {
+ if (*p & 0x80)
+ ++octet_chars;
}
+ total_len += len;
}
fclose(fp);
- return ENC_7BIT;
+
+ if (total_len > 0)
+ octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
+ else
+ octet_percentage = 0.0;
+
+ debug_print("procmime_get_encoding_for_text_file(): "
+ "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
+ 100.0 * octet_percentage);
+
+ if (octet_percentage > 0.20) {
+ debug_print("using BASE64\n");
+ return ENC_BASE64;
+ } else if (octet_chars > 0) {
+ debug_print("using quoted-printable\n");
+ return ENC_QUOTED_PRINTABLE;
+ } else {
+ debug_print("using 7bit\n");
+ return ENC_7BIT;
+ }
}
struct EncodingTable
MimeInfo *subinfo;
subinfo = procmime_mimeinfo_new();
+ subinfo->content = MIMECONTENT_FILE;
subinfo->encoding_type = ENC_UNKNOWN;
subinfo->type = MIMETYPE_TEXT;
subinfo->subtype = g_strdup("plain");
ENC_AS_EXTENDED,
} EncodeAs;
+typedef struct _ParametersData {
+ FILE *fp;
+ guint len;
+} ParametersData;
+
static void write_parameters(gpointer key, gpointer value, gpointer user_data)
{
gchar *param = key;
gchar *val = value, *valpos;
- FILE *fp = user_data;
+ ParametersData *pdata = (ParametersData *)user_data;
+ GString *buf = g_string_new("");
+
EncodeAs encas = ENC_AS_TOKEN;
for (valpos = val; *valpos != 0; valpos++) {
switch (encas) {
case ENC_AS_TOKEN:
- fprintf(fp, "; %s=", param);
- fprintf(fp, "%s", val);
+ g_string_append_printf(buf, "%s=%s", param, val);
break;
case ENC_AS_QUOTED_STRING:
- fprintf(fp, "; %s=", param);
- fprintf(fp, "\"%s\"", val);
+ g_string_append_printf(buf, "%s=\"%s\"", param, val);
break;
case ENC_AS_EXTENDED:
- fprintf(fp, "; %s*=", param);
- fprintf(fp, "%s''", conv_get_current_charset_str());
+ g_string_append_printf(buf, "%s*=%s''", param,
+ conv_get_current_charset_str());
for (valpos = val; *valpos != '\0'; valpos++) {
- if (IS_ASCII(*valpos) && isalnum(*valpos))
- fprintf(fp, "%c", *valpos);
- else {
+ if (IS_ASCII(*valpos) && isalnum(*valpos)) {
+ g_string_append_printf(buf, "%c", *valpos);
+ } else {
gchar hexstr[3] = "XX";
get_hex_str(hexstr, *valpos);
- fprintf(fp, "%%%s", hexstr);
+ g_string_append_printf(buf, "%%%s", hexstr);
}
}
break;
}
+
+ if (buf->str && strlen(buf->str)) {
+ if (pdata->len + strlen(buf->str) + 2 > 76) {
+ fprintf(pdata->fp, ";\n %s", buf->str);
+ pdata->len = strlen(buf->str) + 1;
+ } else {
+ fprintf(pdata->fp, "; %s", buf->str);
+ pdata->len += strlen(buf->str) + 2;
+ }
+ }
+ g_string_free(buf, TRUE);
}
void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
{
struct TypeTable *type_table;
-
+ ParametersData *pdata = g_new0(ParametersData, 1);
debug_print("procmime_write_mime_header\n");
+
+ pdata->fp = fp;
for (type_table = mime_type_table; type_table->str != NULL; type_table++)
if (mimeinfo->type == type_table->type) {
- fprintf(fp, "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
+ gchar *buf = g_strdup_printf(
+ "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
+ fprintf(fp, "%s", buf);
+ pdata->len = strlen(buf);
+ g_free(buf);
break;
}
- g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, fp);
+ g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
+ g_free(pdata);
+
fprintf(fp, "\n");
if (mimeinfo->encoding_type != ENC_UNKNOWN)
fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
- fprintf(fp, "Content-Disposition: ");
+ ParametersData *pdata = g_new0(ParametersData, 1);
+ gchar *buf = NULL;
if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
- fprintf(fp, "inline");
+ buf = g_strdup("Content-Disposition: inline");
else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
- fprintf(fp, "attachment");
+ buf = g_strdup("Content-Disposition: attachment");
else
- fprintf(fp, "unknown");
+ buf = g_strdup("Content-Disposition: unknown");
+
+ fprintf(fp, "%s", buf);
+ pdata->len = strlen(buf);
+ g_free(buf);
- /* FIXME: linebreaks after too many parameters */
- g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, fp);
+ pdata->fp = fp;
+ g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
+ g_free(pdata);
fprintf(fp, "\n");
}