Compare commits

...

2 commits

Author SHA1 Message Date
5e12445ac7
Added mime type detection
Signed-off-by: Tyrolyean <tyrolyean@tyrolyean.net>
2020-04-29 22:18:08 +02:00
b1ef07c103
Base 64 detection improvement
Signed-off-by: Tyrolyean <tyrolyean@tyrolyean.net>
2020-04-29 20:18:10 +02:00
5 changed files with 70 additions and 10 deletions

View file

@ -23,6 +23,11 @@
#include <stdbool.h> #include <stdbool.h>
#include <sys/types.h> #include <sys/types.h>
struct type_file_info_t{
char* name;
char* mime_type;
};
struct email_t{ struct email_t{
char* message; char* message;
/* From the below values you can say pretty much anything about the /* From the below values you can say pretty much anything about the
@ -49,12 +54,20 @@ struct email_t{
size_t ct_len; size_t ct_len;
char* content_type; char* content_type;
/* NOTICE: the boundary, content_type and message fields may be updated
* when the message of the root object is modified. See the
* propagate_root_pointer function for further information
*/
size_t submes_cnt; size_t submes_cnt;
struct email_t** submes; struct email_t** submes;
struct email_t* parent; struct email_t* parent;
bool base64_encoded; bool base64_encoded;
struct type_file_info_t file_info;
}; };
int append_header(struct email_t* mail, const char* key, const char* value); int append_header(struct email_t* mail, const char* key, const char* value);
int append_to_header(struct email_t* mail, const char* pair); int append_to_header(struct email_t* mail, const char* pair);
@ -64,6 +77,8 @@ void propagate_insert_delete(struct email_t* mail, char* change_p,
ssize_t change); ssize_t change);
void propagate_root_pointer(struct email_t* mail, char* change_p, char* old_p); void propagate_root_pointer(struct email_t* mail, char* change_p, char* old_p);
struct type_file_info_t get_mime_file_info(struct email_t* mail);
#define MULTIPART_MIME "multipart/" #define MULTIPART_MIME "multipart/"
#define BASE64_ENC "base64" #define BASE64_ENC "base64"

View file

@ -29,8 +29,8 @@ char* search_header_key(const struct email_t* mail, const char* key);
char* get_value_from_key(size_t* val_len, size_t key_offset, char* get_value_from_key(size_t* val_len, size_t key_offset,
const struct email_t* mail); const struct email_t* mail);
char* get_multipart_boundary(char* content_type, size_t content_len, char* get_value_equals(char* content_type, size_t content_len,
size_t* boundary_len); size_t* boundary_len, char* key);
const char* get_next_line(const char* message, size_t len); const char* get_next_line(const char* message, size_t len);
const char* get_prev_line(const char* message, size_t len_neg); const char* get_prev_line(const char* message, size_t len_neg);

View file

@ -77,8 +77,9 @@ struct email_t* mail_from_text(char* message, size_t length,
*/ */
mail->is_multipart = true; mail->is_multipart = true;
size_t bd_len = 0; size_t bd_len = 0;
char* bd = get_multipart_boundary( char* bd = get_value_equals(
mime_type, value_length, &bd_len); mime_type, value_length, &bd_len,
"boundary");
if(bd != NULL){ if(bd != NULL){
mail->boundary = bd; mail->boundary = bd;
@ -98,6 +99,8 @@ struct email_t* mail_from_text(char* message, size_t length,
} }
mail->base64_encoded = detect_base64(mail); mail->base64_encoded = detect_base64(mail);
mail->file_info = get_mime_file_info(mail);
return mail; return mail;
} }
@ -236,9 +239,18 @@ void print_mail_structure(struct email_t *email, unsigned int level){
print_mail_structure(email->submes[i], level+1); print_mail_structure(email->submes[i], level+1);
} }
}else{ }else{
printf("final message with length %lu and type [%.*s] \n", if( email->file_info.mime_type == NULL){
email->message_length, (int)email->ct_len,
email->content_type); printf("final message with length %lu and type "
"[%.*s] \n",
email->message_length, (int)email->ct_len,
email->content_type);
}else{
printf("final message with length %lu and type [%s] \n",
email->message_length,
email->file_info.mime_type);
}
} }
return; return;

View file

@ -168,3 +168,36 @@ void propagate_root_pointer(struct email_t* mail, char* change_p, char* old_p){
return; return;
} }
struct type_file_info_t get_mime_file_info(struct email_t* mail){
struct type_file_info_t fileinfo;
fileinfo.name = NULL;
fileinfo.mime_type = NULL;
if(mail->content_type == NULL){
return fileinfo;
}
char* semic = strchr(mail->content_type, ';');
size_t mime_len = 0;
if(semic > (mail->content_type + mail->ct_len)){
mime_len = mail->ct_len;
}else{
mime_len = semic - mail->content_type;
}
fileinfo.mime_type = malloc(mime_len + 1);
memset(fileinfo.mime_type, 0, mime_len + 1);
memcpy(fileinfo.mime_type, mail->content_type, mime_len);
size_t filename_len = 0;
char* filename = get_value_equals(mail->content_type, mail->ct_len,
&filename_len, "name");
if(filename != NULL){
fileinfo.name = malloc(filename_len + 1);
memset(fileinfo.name, 0, filename_len + 1);
memcpy(fileinfo.name, filename, filename_len);
}
return fileinfo;
}

View file

@ -119,14 +119,14 @@ char* get_value_from_key(size_t* val_len, size_t key_offset,
return val; return val;
} }
char* get_multipart_boundary(char* content_type, size_t content_len, char* get_value_equals(char* content_type, size_t content_len,
size_t* boundary_len){ size_t* boundary_len, char* key){
if(content_type == NULL || boundary_len == NULL){ if(content_type == NULL || boundary_len == NULL){
return NULL; return NULL;
} }
char* bd = "boundary"; char* bd = key;
char* boundary_begin = NULL; char* boundary_begin = NULL;
size_t bd_len = strlen(bd); size_t bd_len = strlen(bd);
long boundary_offset = -1; long boundary_offset = -1;