struct mailmime_content * mailmime_content_dup(struct mailmime_content * content) { clist * list; struct mailmime_type * type; int r; struct mailmime_content * dup_content; char * subtype; type = mailmime_type_dup(content->ct_type); if (type == NULL) goto err; subtype = strdup(content->ct_subtype); if (subtype == NULL) goto free_type; list = clist_new(); if (list == NULL) goto free_subtype; if (content->ct_parameters != NULL) { clistiter * cur; for(cur = clist_begin(content->ct_parameters) ; cur != NULL ; cur = clist_next(cur)) { struct mailmime_parameter * param; param = mailmime_parameter_dup(clist_content(cur)); if (param == NULL) goto free_list; r = clist_append(list, param); if (r < 0) { mailmime_parameter_free(param); goto free_list; } } } dup_content = mailmime_content_new(type, subtype, list); if (dup_content == NULL) goto free_list; return dup_content; free_list: clist_foreach(list, (clist_func) mailmime_parameter_free, NULL); free_subtype: free(subtype); free_type: mailmime_type_free(type); err: return NULL; }
struct mailmime_content * mailmime_get_content_message(void) { clist * list; struct mailmime_composite_type * composite_type; struct mailmime_type * mime_type; struct mailmime_content * content; char * subtype; composite_type = mailmime_composite_type_new(MAILMIME_COMPOSITE_TYPE_MESSAGE, NULL); if (composite_type == NULL) goto err; mime_type = mailmime_type_new(MAILMIME_TYPE_COMPOSITE_TYPE, NULL, composite_type); if (mime_type == NULL) goto free_composite; composite_type = NULL; list = clist_new(); if (list == NULL) goto free_mime_type; subtype = strdup("rfc822"); if (subtype == NULL) goto free_list; content = mailmime_content_new(mime_type, subtype, list); if (content == NULL) goto free_subtype; return content; free_subtype: free(subtype); free_list: clist_free(list); free_mime_type: mailmime_type_free(mime_type); free_composite: if (composite_type != NULL) mailmime_composite_type_free(composite_type); err: return NULL; }
struct mailmime_content * mailmime_get_content_text(void) { clist * list; struct mailmime_discrete_type * discrete_type; struct mailmime_type * mime_type; struct mailmime_content * content; char * subtype; discrete_type = mailmime_discrete_type_new(MAILMIME_DISCRETE_TYPE_TEXT, NULL); if (discrete_type == NULL) goto err; mime_type = mailmime_type_new(MAILMIME_TYPE_DISCRETE_TYPE, discrete_type, NULL); if (mime_type == NULL) goto free_discrete; discrete_type = NULL; list = clist_new(); if (list == NULL) goto free_type; subtype = strdup("plain"); if (subtype == NULL) goto free_list; content = mailmime_content_new(mime_type, subtype, list); if (content == NULL) goto free_subtype; return content; free_subtype: free(subtype); free_list: clist_free(list); free_type: mailmime_type_free(mime_type); free_discrete: if (discrete_type != NULL) mailmime_discrete_type_free(discrete_type); err: return NULL; }
LIBETPAN_EXPORT int mailmime_content_parse(const char * message, size_t length, size_t * indx, struct mailmime_content ** result) { size_t cur_token; struct mailmime_type * type; char * subtype; clist * parameters_list; struct mailmime_content * content; int r; int res; cur_token = * indx; mailimf_cfws_parse(message, length, &cur_token); type = NULL; r = mailmime_type_parse(message, length, &cur_token, &type); if (r != MAILIMF_NO_ERROR) { res = r; goto err; } r = mailimf_unstrict_char_parse(message, length, &cur_token, '/'); switch (r) { case MAILIMF_NO_ERROR: r = mailimf_cfws_parse(message, length, &cur_token); if ((r != MAILIMF_NO_ERROR) && (r != MAILIMF_ERROR_PARSE)) { res = r; goto free_type; } r = mailmime_subtype_parse(message, length, &cur_token, &subtype); if (r != MAILIMF_NO_ERROR) { res = r; goto free_type; } break; case MAILIMF_ERROR_PARSE: subtype = strdup("unknown"); break; default: res = r; goto free_type; } parameters_list = clist_new(); if (parameters_list == NULL) { res = MAILIMF_ERROR_MEMORY; goto free_subtype; } while (1) { size_t final_token; struct mailmime_parameter * parameter; final_token = cur_token; r = mailimf_unstrict_char_parse(message, length, &cur_token, ';'); if (r != MAILIMF_NO_ERROR) { cur_token = final_token; break; } r = mailimf_cfws_parse(message, length, &cur_token); if ((r != MAILIMF_NO_ERROR) && (r != MAILIMF_ERROR_PARSE)) { res = r; goto free_type; } r = mailmime_parameter_parse(message, length, &cur_token, ¶meter); if (r == MAILIMF_NO_ERROR) { /* do nothing */ } else if (r == MAILIMF_ERROR_PARSE) { cur_token = final_token; break; } else { res = r; goto err; } r = clist_append(parameters_list, parameter); if (r < 0) { mailmime_parameter_free(parameter); res = MAILIMF_ERROR_MEMORY; goto free_parameters; } } content = mailmime_content_new(type, subtype, parameters_list); if (content == NULL) { res = MAILIMF_ERROR_MEMORY; goto free_parameters; } * result = content; * indx = cur_token; return MAILIMF_NO_ERROR; free_parameters: clist_foreach(parameters_list, (clist_func) mailmime_parameter_free, NULL); clist_free(parameters_list); free_subtype: mailmime_subtype_free(subtype); free_type: mailmime_type_free(type); err: return res; }