#include "codeconv.h"
#include "utils.h"
#include "prefs_common.h"
-
#include "prefs_gtk.h"
static GHashTable *procmime_get_mime_type_table (void);
gchar *mimetmpdir;
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) {
+ tmp_file = get_tmp_file();
+ if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
+ g_free(tmp_file);
+ fclose(infp);
+ }
+ if ((tmp_fp = fopen(tmp_file, "rb")) == NULL) {
+ FILE_OP_ERROR(tmp_file, "fopen");
+ unlink(tmp_file);
+ g_free(tmp_file);
+ fclose(infp);
+ }
+ }
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];
if (basetmp == NULL)
basetmp = "mimetmp";
base = g_basename(basetmp);
- if (*base == '\0') base = "mimetmp";
+ if (*base == '\0') base = g_strdup("mimetmp");
Xstrdup_a(base, base, return NULL);
subst_for_shellsafe_filename(base);
}
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
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_sprintfa(buf, "%s=%s", param, val);
break;
case ENC_AS_QUOTED_STRING:
- fprintf(fp, "; %s=", param);
- fprintf(fp, "\"%s\"", val);
+ g_string_sprintfa(buf, "%s=\"%s\"", param, val);
break;
case ENC_AS_EXTENDED:
- fprintf(fp, "; %s*=", param);
- fprintf(fp, "%s''", conv_get_current_charset_str());
+ g_string_sprintfa(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_sprintfa(buf, "%c", *valpos);
+ } else {
gchar hexstr[3] = "XX";
get_hex_str(hexstr, *valpos);
- fprintf(fp, "%%%s", hexstr);
+ g_string_sprintfa(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");
}