text
stringlengths
0
357
const char* decoded_data, size_t decoded_data_bytes,
const char* desired_filename)
{
dc_mimepart_t* part = NULL;
char* pathNfilename = NULL;
/* create a free file name to use */
if ((pathNfilename=dc_get_fine_pathNfilename(parser->context, "$BLOBDIR", desired_filename))==NULL) {
goto cleanup;
}
/* copy data to file */
if (dc_write_file(parser->context, pathNfilename, decoded_data, decoded_data_bytes)==0) {
goto cleanup;
}
part = dc_mimepart_new();
part->type = msg_type;
part->int_mimetype = mime_type;
part->bytes = decoded_data_bytes;
dc_param_set(part->param, DC_PARAM_FILE, pathNfilename);
dc_param_set(part->param, DC_PARAM_MIMETYPE, raw_mime);
if (mime_type==DC_MIMETYPE_IMAGE) {
uint32_t w = 0, h = 0;
if (dc_get_filemeta(decoded_data, decoded_data_bytes, &w, &h)) {
dc_param_set_int(part->param, DC_PARAM_WIDTH, w);
dc_param_set_int(part->param, DC_PARAM_HEIGHT, h);
}
}
do_add_single_part(parser, part);
part = NULL;
cleanup:
free(pathNfilename);
dc_mimepart_unref(part);
}
static int dc_mimeparser_add_single_part_if_known(dc_mimeparser_t* mimeparser, struct mailmime* mime)
{
dc_mimepart_t* part = NULL;
int old_part_count = carray_count(mimeparser->parts);
int mime_type;
struct mailmime_data* mime_data;
char* file_suffix = NULL;
char* desired_filename = NULL;
int msg_type = 0;
char* raw_mime = NULL;
char* transfer_decoding_buffer = NULL; /* mmap_string_unref()'d if set */
char* charset_buffer = NULL; /* charconv_buffer_free()'d if set (just calls mmap_string_unref()) */
const char* decoded_data = NULL; /* must not be free()'d */
size_t decoded_data_bytes = 0;
dc_simplify_t* simplifier = NULL;
if (mime==NULL || mime->mm_data.mm_single==NULL) {
goto cleanup;
}
/* get mime type from `mime` */
mime_type = mailmime_get_mime_type(mime, &msg_type, &raw_mime);
/* get data pointer from `mime` */
mime_data = mime->mm_data.mm_single;
if (mime_data->dt_type!=MAILMIME_DATA_TEXT /* MAILMIME_DATA_FILE indicates, the data is in a file; AFAIK this is not used on parsing */
|| mime_data->dt_data.dt_text.dt_data==NULL
|| mime_data->dt_data.dt_text.dt_length <= 0) {
goto cleanup;
}
/* regard `Content-Transfer-Encoding:` */
if (!mailmime_transfer_decode(mime, &decoded_data, &decoded_data_bytes, &transfer_decoding_buffer)) {
goto cleanup; /* no always error - but no data */
}
switch (mime_type)
{
case DC_MIMETYPE_TEXT_PLAIN:
case DC_MIMETYPE_TEXT_HTML:
{
if (simplifier==NULL) {
simplifier = dc_simplify_new();
if (simplifier==NULL) {
goto cleanup;
}
}
const char* charset = mailmime_content_charset_get(mime->mm_content_type); /* get from `Content-Type: text/...; charset=utf-8`; must not be free()'d */
if (charset!=NULL && strcmp(charset, "utf-8")!=0 && strcmp(charset, "UTF-8")!=0) {
size_t ret_bytes = 0;
int r = charconv_buffer("utf-8", charset, decoded_data, decoded_data_bytes, &charset_buffer, &ret_bytes);
if (r!=MAIL_CHARCONV_NO_ERROR) {
dc_log_warning(mimeparser->context, 0, "Cannot convert %i bytes from \"%s\" to \"utf-8\"; errorcode is %i.", /* if this warning comes up for usual character sets, maybe libetpan is compiled without iconv? */
(int)decoded_data_bytes, charset, (int)r); /* continue, however */
}
else if (charset_buffer==NULL || ret_bytes <= 0) {