struct mailmime_fields * mailmime_fields_new_filename(int dsp_type,
    char * filename, int encoding_type)
{
  struct mailmime_disposition * dsp;
  struct mailmime_mechanism * encoding;
  struct mailmime_fields * mime_fields;

  dsp = mailmime_disposition_new_with_data(dsp_type,
    filename, NULL, NULL, NULL, (size_t) -1);
  if (dsp == NULL)
    goto err;

  encoding = mailmime_mechanism_new(encoding_type, NULL);
  if (encoding == NULL)
    goto free_dsp;

  mime_fields = mailmime_fields_new_with_data(encoding,
			      NULL, NULL, dsp, NULL);
  if (mime_fields == NULL)
    goto free_encoding;

  return mime_fields;

 free_encoding:
  mailmime_encoding_free(encoding);
 free_dsp:
  mailmime_disposition_free(dsp);
 err:
  return NULL;
}
Exemple #2
0
LIBETPAN_EXPORT
int
mailmime_field_parse(struct mailimf_optional_field * field,
		     struct mailmime_field ** result)
{
  char * name;
  char * value;
  int guessed_type;
  size_t cur_token;
  struct mailmime_content * content;
  struct mailmime_mechanism * encoding;
  char * id;
  char * description;
  uint32_t version;
  struct mailmime_field * mime_field;
  struct mailmime_language * language;
  struct mailmime_disposition * disposition;
  char * location;
  int res;
  int r;

  name = field->fld_name;
  value = field->fld_value;
  cur_token = 0;

  content = NULL;
  encoding = NULL;
  id = NULL;
  description = NULL;
  version = 0;
  disposition = NULL;
  language = NULL;
  location = NULL;

  guessed_type = guess_field_type(name);

  switch (guessed_type) {
  case MAILMIME_FIELD_TYPE:
    if (strcasecmp(name, "Content-Type") != 0)
      return MAILIMF_ERROR_PARSE;
    {
      size_t cur_token = 0;
      char * decoded_value;
      r = mailmime_encoded_phrase_parse("us-ascii",
          value, strlen(value),
          &cur_token, "utf-8", &decoded_value);
      if (r != MAILIMF_NO_ERROR) {
        cur_token = 0;
        r = mailmime_content_parse(value, strlen(value), &cur_token, &content);
      }
      else {
        cur_token = 0;
        r = mailmime_content_parse(decoded_value, strlen(decoded_value), &cur_token, &content);
        free(decoded_value);
      }
      if (r != MAILIMF_NO_ERROR)
        return r;
    }
    break;

  case MAILMIME_FIELD_TRANSFER_ENCODING:
    if (strcasecmp(name, "Content-Transfer-Encoding") != 0)
      return MAILIMF_ERROR_PARSE;
    r = mailmime_encoding_parse(value, strlen(value), &cur_token, &encoding);
    if (r != MAILIMF_NO_ERROR)
      return r;
    break;

  case MAILMIME_FIELD_ID:
    if (strcasecmp(name, "Content-ID") != 0)
      return MAILIMF_ERROR_PARSE;
    r = mailmime_id_parse(value, strlen(value), &cur_token, &id);
    if (r != MAILIMF_NO_ERROR)
      return r;
    break;

  case MAILMIME_FIELD_DESCRIPTION:
    if (strcasecmp(name, "Content-Description") != 0)
      return MAILIMF_ERROR_PARSE;
    r = mailmime_description_parse(value, strlen(value),
				   &cur_token, &description);
    if (r != MAILIMF_NO_ERROR)
      return r;
    break;

  case MAILMIME_FIELD_VERSION:
    if (strcasecmp(name, "MIME-Version") != 0)
      return MAILIMF_ERROR_PARSE;
    r = mailmime_version_parse(value, strlen(value), &cur_token, &version);
    if (r != MAILIMF_NO_ERROR)
      return r;
    break;

  case MAILMIME_FIELD_DISPOSITION:
    if (strcasecmp(name, "Content-Disposition") != 0)
      return MAILIMF_ERROR_PARSE;
    r = mailmime_disposition_parse(value, strlen(value),
				   &cur_token, &disposition);
    if (r != MAILIMF_NO_ERROR)
      return r;
    break;

  case MAILMIME_FIELD_LANGUAGE:
    if (strcasecmp(name, "Content-Language") != 0)
      return MAILIMF_ERROR_PARSE;
    r = mailmime_language_parse(value, strlen(value), &cur_token, &language);
    if (r != MAILIMF_NO_ERROR)
      return r;
    break;

  case MAILMIME_FIELD_LOCATION:
    if (strcasecmp(name, "Content-Location") != 0)
      return MAILIMF_ERROR_PARSE;
    r = mailmime_location_parse(value, strlen(value), &cur_token, &location);
    if (r != MAILIMF_NO_ERROR)
      return r;
    break;
      
  default:
    return MAILIMF_ERROR_PARSE;
  }

  mime_field = mailmime_field_new(guessed_type, content, encoding,
				  id, description, version, disposition,
				  language, location);
  if (mime_field == NULL) {
    res = MAILIMF_ERROR_MEMORY;
    goto free;
  }
  
  * result = mime_field;

  return MAILIMF_NO_ERROR;

 free:
  if (location != NULL)
    mailmime_location_free(location);
  if (language != NULL)
    mailmime_language_free(language);
  if (content != NULL)
    mailmime_content_free(content);
  if (encoding != NULL)
    mailmime_encoding_free(encoding);
  if (id != NULL)
    mailmime_id_free(id);
  if (description != NULL)
    mailmime_description_free(description);
  return res;
}