libEtPan! API
Prev | Chapter 4. MIME | Next

# Parser functions

## mailmime_content_parse

    #include <libetpan/libetpan.h>

    int mailmime_content_parse(const char * message, size_t length,
    			   size_t * index,
    			   struct mailmime_content ** result);

This function will parse the content of a Content-Type header field.

- message is a string containing the MIME content type.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) (see the Section called mailmime_content - MIME content type (Content-Type)).

Example 4-16. Parsing MIME content type

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;

    	current_index = 0;
    	r = mailimf_fields_parse(mem, stat_info.st_size,
              &current_index, &f);
    	if (r == MAILIMF_NO_ERROR) {
              clistiter * cur;

              for(cur = clist_begin(f->fld_list) ; cur != NULL ; cur =
                clist_next(cur)) {
                struct mailmime_field * mime_field;
                struct mailimf_field * field;

                field = clist_content(cur);

                if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) {
                  if (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                    "Content-Type") == 0) {
                    struct mailmime_content * content_type;
                    size_t current_index;

                    current_index = 0;
    	        r = mailmime_content_parse(field->fld_data.fld_optional_field->fld_value,
                      strlen(field->fld_data.fld_optional_field->fld_value),
                      &current_index, &content_type);
    	        if (r == MAILIMF_NO_ERROR) {
    	          display_mime_content(content_type);
    	          /* do the things */
    	          status = EXIT_SUCCESS;
    	          mailmime_content_free(content_type);
    	        }
                  }
                }
              }
    	  mailimf_fields_free(f);
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_description_parse

    #include >libetpan/libetpan.h<

    int mailmime_description_parse(const char * message, size_t length,
    			       size_t * index,
    			       char ** result);

This will parse the content of Content-Description MIME header field.

- message is a string containing the MIME content description.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result). The result string must be freed with free().

Example 4-17. Parsing MIME description

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;

    	current_index = 0;
    	r = mailimf_fields_parse(mem, stat_info.st_size,
              &current_index, &f);
    	if (r == MAILIMF_NO_ERROR) {
              clistiter * cur;

              for(cur = clist_begin(f->fld_list) ; cur != NULL ; cur =
                clist_next(cur)) {
                struct mailmime_field * mime_field;
                struct mailimf_field * field;

                field = clist_content(cur);

                if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) {
                  if (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                    "Content-Description") == 0) {
                    char * description;
                    size_t current_index;

                    current_index = 0;
                    r = mailmime_description_parse(field->fld_data.fld_optional_field->fld_value,
                      strlen(field->fld_data.fld_optional_field->fld_value),
                      &current_index, &description);
    	        if (r == MAILIMF_NO_ERROR) {
    	          printf("%s\n", description);
    	          /* do the things */
    	          status = EXIT_SUCCESS;
    	          free(description);
    	        }
                  }
                }
              }
    	  mailimf_fields_free(f);
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_encoding_parse

    #include >libetpan/libetpan.h<

    int mailmime_encoding_parse(const char * message, size_t length,
    			    size_t * index,
    			    struct mailmime_mechanism ** result);

This function will parse the content of Content-Transfer-Encoding header field.

- message is a string containing the MIME encoding mechanism.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) (see the Section called mailmime_mechanism - MIME transfer encoding mechanism (Content-Transfer-Encoding)).

Example 4-18. parsing MIME encoding mechanism

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;

    	current_index = 0;
    	r = mailimf_fields_parse(mem, stat_info.st_size,
              &current_index, &f);
    	if (r == MAILIMF_NO_ERROR) {
              clistiter * cur;

              for(cur = clist_begin(f->fld_list) ; cur != NULL ; cur =
                clist_next(cur)) {
                struct mailmime_field * mime_field;
                struct mailimf_field * field;

                field = clist_content(cur);

                if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) {
                  if (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                    "Content-Transfer-Encoding") == 0) {
                    struct mailmime_content * encoding;
                    size_t current_index;

                    current_index = 0;
                    r = mailmime_encoding_parse(field->fld_data.fld_optional_field->fld_value,
                      strlen(field->fld_data.fld_optional_field->fld_value),
                      &current_index, &encoding);
    	        if (r == MAILIMF_NO_ERROR) {
                      display_mime_mechanism(encoding);
    	          /* do the things */
    	          status = EXIT_SUCCESS;
                      mailmime_mechanism_free(encoding);
    	        }
                  }
                }
              }
    	  mailimf_fields_free(f);
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_field_parse

    #include <libetpan/libetpan.h>

    int
    mailmime_field_parse(struct mailimf_optional_field * field,
    		     struct mailmime_field ** result);

This function will parse a MIME header field.

- field is a non-parsed field (see the Section called mailimf_optional_field - non-standard header in Chapter 3).

  result. The result of the parse operation is stored in (* result) (see the Section called mailmime_field - MIME header field).

Example 4-19. parsing MIME header field

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;

    	current_index = 0;
    	r = mailimf_fields_parse(mem, stat_info.st_size,
              &current_index, &f);
    	if (r == MAILIMF_NO_ERROR) {
              clistiter * cur;

              for(cur = clist_begin(f->fld_list) ; cur != NULL ; cur =
                clist_next(cur)) {
                struct mailmime_field * mime_field;
                struct mailimf_field * field;

                field = clist_content(cur);

                if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) {
                  r = mailmime_field_parse(field->fld_data.fld_optional_field,
                    &mime_fields);
                  if (r == MAILIMF_NO_ERROR) {
                    display_mime_field(mime_field);
    	        mailmime_field_free(mime_field);
    	        status = EXIT_SUCCESS;
                  }
                }
              }

    	  mailimf_fields_free(f);
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_id_parse

    #include >libetpan/libetpan.h<

    int mailmime_id_parse(const char * message, size_t length,
    		      size_t * index, char ** result);

This will parse the content of Content-ID MIME header field.

- message is a string containing the MIME content identifier.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result). The result string must be freed with free().

Example 4-20. Parsing MIME content identifier

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;

    	current_index = 0;
    	r = mailimf_fields_parse(mem, stat_info.st_size,
              &current_index, &f);
    	if (r == MAILIMF_NO_ERROR) {
              clistiter * cur;

              for(cur = clist_begin(f->fld_list) ; cur != NULL ; cur =
                clist_next(cur)) {
                struct mailmime_field * mime_field;
                struct mailimf_field * field;

                field = clist_content(cur);

                if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) {
                  if (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                    "Content-ID") == 0) {
                    char * id;
                    size_t current_index;

                    current_index = 0;
                    r = mailmime_id_parse(field->fld_data.fld_optional_field->fld_value,
                      strlen(field->fld_data.fld_optional_field->fld_value),
                      &current_index, &id);
    	        if (r == MAILIMF_NO_ERROR) {
    	          printf("%s\n", id);
    	          /* do the things */
    	          status = EXIT_SUCCESS;
    	          free(id);
    	        }
                  }
                }
              }
    	  mailimf_fields_free(f);
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_fields_parse

    #include <libetpan/libetpan.h>

    int
    mailmime_fields_parse(struct mailimf_fields * fields,
    		      struct mailmime_fields ** result);

This function will parse a MIME header fields.

- fields is a list of RFC 2822 fields (see the Section called mailimf_fields - list of header fields in Chapter 3).

  result. The result of the parse operation is stored in (* result) (see the Section called mailmime_fields - header fields).

Example 4-21. parsing MIME header fields

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;

    	current_index = 0;
    	r = mailimf_fields_parse(mem, stat_info.st_size,
              &current_index, &f);
    	if (r == MAILIMF_NO_ERROR) {
              struct mailmime_fields * mime_fields;

    	  r = mailmime_fields_parse(f, &mime_fields);
              if (r == MAILIMF_NO_ERROR) {
    	    display_mime_fields(mime_fields);
    	    mailmime_fields_free(mime_fields);
    	    status = EXIT_SUCCESS;
              }

    	  mailimf_fields_free(f);
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_version_parse

    #include <libetpan/libetpan.h>

    int mailmime_version_parse(const char * message, size_t length,
    			   size_t * index,
    			   uint32_t * result);

This will parse the content of MIME-Version MIME header field.

- message is a string containing the MIME version.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) (see the Section called mailmime_field - MIME header field).

Example 4-22. parsing MIME version

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;

    	current_index = 0;
    	r = mailimf_fields_parse(mem, stat_info.st_size,
              &current_index, &f);
    	if (r == MAILIMF_NO_ERROR) {
              clistiter * cur;

              for(cur = clist_begin(f->fld_list) ; cur != NULL ; cur =
                clist_next(cur)) {
                struct mailmime_field * mime_field;
                struct mailimf_field * field;

                field = clist_content(cur);

                if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) {
                  if (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                    "MIME-Version") == 0) {
                    uint32_t version;
                    size_t current_index;

                    current_index = 0;
                    r = mailmime_version_parse(field->fld_data.fld_optional_field->fld_value,
                      strlen(field->fld_data.fld_optional_field->fld_value),
                      &current_index, &version);
    	        if (r == MAILIMF_NO_ERROR) {
    	          printf("%i.%i\n", version >> 16, version & 0xFFFF);
    	          /* do the things */
    	          status = EXIT_SUCCESS;
    	          free(description);
    	        }
                  }
                }
              }
    	  mailimf_fields_free(f);
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_parameter_parse

    #include <libetpan/libetpan.h>

    int mailmime_parameter_parse(const char * message, size_t length,
    			     size_t * index,
    			     struct mailmime_parameter ** result);

This will parse a MIME parameter (parameter of Content-Type or parameter of Content-Disposition).

- message is a string containing the MIME parameter.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) (see the Section called mailmime_parameter - MIME type parameter).

Example 4-23. parsing a MIME parameter

    #include <libetpan/libetpan.h>

    #define PARAM_STR "foo=bar"

    int main(int argc, char ** argv)
    {
      int fd;
      int r;
      size_t current_index;
      struct mailmime_parameter * param;
      int status;

      status = EXIT_FAILURE;

      current_index = 0;
      r = mailmime_parameter_parse(PARAM_STR, sizeof(PARAM_STR) - 1,
        &current_index, &param);
      if (r == MAILIMF_NO_ERROR) {
        display_mime_parameter(param);
        /* do the things */
        mailmime_parameter_free(param);
        status = EXIT_SUCCESS;
      }

      exit(status);
    }

## mailmime_language_parse

    #include <libetpan/libetpan.h>

    int mailmime_language_parse(const char * message, size_t length,
    			    size_t * index,
    			    struct mailmime_language ** result);

This function will parse the content of a Content-Language header.

- message is a string containing the MIME content language.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) (see the Section called mailmime_language - Language of MIME part).

Example 4-24. Parsing the MIME content langage

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;

    	current_index = 0;
    	r = mailimf_fields_parse(mem, stat_info.st_size,
              &current_index, &f);
    	if (r == MAILIMF_NO_ERROR) {
              clistiter * cur;

              for(cur = clist_begin(f->fld_list) ; cur != NULL ; cur =
                clist_next(cur)) {
                struct mailmime_field * mime_field;
                struct mailimf_field * field;

                field = clist_content(cur);

                if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) {
                  if (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                    "Content-Language") == 0) {
                    struct mailmime_language * lang;
                    size_t current_index;

                    current_index = 0;
                    r = mailmime_id_parse(field->fld_data.fld_optional_field->fld_value,
                      strlen(field->fld_data.fld_optional_field->fld_value),
                      &current_index, &lang);
    	        if (r == MAILIMF_NO_ERROR) {
    	          display_mime_language(lang);
    	          /* do the things */
    	          status = EXIT_SUCCESS;
    	          free(id);
    	        }
                  }
                }
              }
    	  mailimf_fields_free(f);
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_disposition_parse

    #include <libetpan/libetpan.h>

    int mailmime_disposition_parse(const char * message, size_t length,
    			       size_t * index,
    			       struct mailmime_disposition ** result);

This function will parse the content of a Content-Disposition MIME header field.

- message is a string containing the MIME content disposition.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) (see the Section called mailmime_disposition - MIME disposition information (Content-Disposition)).

Example 4-25. Parsing the MIME content disposition

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;

    	current_index = 0;
    	r = mailimf_fields_parse(mem, stat_info.st_size,
              &current_index, &f);
    	if (r == MAILIMF_NO_ERROR) {
              clistiter * cur;

              for(cur = clist_begin(f->fld_list) ; cur != NULL ; cur =
                clist_next(cur)) {
                struct mailmime_field * mime_field;
                struct mailimf_field * field;

                field = clist_content(cur);

                if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) {
                  if (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                    "Content-Disposition") == 0) {
                    struct mailmime_disposition * dsp;
                    size_t current_index;

                    current_index = 0;
                    r = mailmime_id_parse(field->fld_data.fld_optional_field->fld_value,
                      strlen(field->fld_data.fld_optional_field->fld_value),
                      &current_index, &dsp);
    	        if (r == MAILIMF_NO_ERROR) {
    	          display_mime_disposition(dsp);
    	          /* do the things */
    	          status = EXIT_SUCCESS;
    	          free(id);
    	        }
                  }
                }
              }
    	  mailimf_fields_free(f);
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_disposition_type_parse

    #include <libetpan/libetpan.h>

    int
    mailmime_disposition_type_parse(const char * message, size_t length,
    				size_t * index,
    				struct mailmime_disposition_type **
                                    result);

This function will parse the type of MIME content disposition.

- message is a string containing the MIME content disposition type.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) (see the Section called mailmime_disposition_type - Type of MIME disposition).

Example 4-26. parsing a MIME content disposition type

    #include <libetpan/libetpan.h>

    #define DSP_TYPE_STR "attachment"

    int main(int argc, char ** argv)
    {
      int fd;
      int r;
      size_t current_index;
      struct mailmime_disposition_type * dsp_type;
      int status;

      status = EXIT_FAILURE;

      current_index = 0;
      r = mailmime_disposition_type_parse(DSP_TYPE_STR, sizeof(DSP_TYPE_STR) - 1,
        &current_index, &dsp_type);
      if (r == MAILIMF_NO_ERROR) {
        display_mime_disposition_type(dsp_type);
        /* do the things */
        mailmime_disposition_type_free(dsp_type);
        status = EXIT_SUCCESS;
      }

      exit(status);
    }

## mailmime_encoded_phrase_parse

    #include <libetpan/libetpan.h>

    int mailmime_encoded_phrase_parse(const char * default_fromcode,
        const char * message, size_t length,
        size_t * index, const char * tocode,
        char ** result);

This function will decode a MIME encoded header string, encoded with RFC 2047.

- default_fromcode is the default code to use for parts of string that are not marked with charset.

- message is the string to decode.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- tocode is the destination charset for decoding.

- result. The result of the parse operation is stored in (* result).

Example 4-27. decoding a MIME encoded header string

    #include <libetpan/libetpan.h>

    #define TEST_STRING "=?iso-8859-1?ab?= =?iso-8859-15?cd?="

    int main(int argc, char ** argv)
    {
      size_t cur_token;
      char * decoded_subject;

      cur_token = 0;
      mailmime_encoded_phrase_parse("iso-8859-1",
        TEST_STRING, sizeof(TEST_STRING),
        &cur_token, "iso-8859-1", &decoded_subject);

      printf("%s\n", decoded_subject);

      /* do the things */

      free(decoded_subject);
    }

## mailmime_parse

    #include <libetpan/libetpan.h>

    int mailmime_parse(const char * message, size_t length,
    		   size_t * index, struct mailmime ** result);

This will parse a MIME message.

- message is a string containing the MIME message.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) (see the Section called mailmime - MIME part).

Example 4-28. parsing a MIME message

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailmime * mime;
    	size_t current_index;

    	current_index = 0;
    	r = mailmime_parse(mem, stat_info.st_size,
              &current_index, &mime);
    	if (r == MAILIMF_NO_ERROR) {
    	    display_mime(mime);
    	  /* do the things */
    	  status = EXIT_SUCCESS;
    	  mailmime_free(mime);
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_base64_body_parse

    #include <libetpan/libetpan.h>

    int mailmime_base64_body_parse(const char * message, size_t length,
    			       size_t * index, char ** result,
    			       size_t * result_len);

This function will parse a body part encoded using base64.

- message is a string encoded using base64.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) The result must be freed with mmap_string_unref().

Example 4-29. Parsing a base64 encoded part

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;
    	char * result;
    	size_t result_len;

    	current_index = 0;
    	r = mailmime_base64_body_parse(mem, stat_info.st_size,
              &current_index, &result, &result_len);
    	if (r == MAILIMF_NO_ERROR) {

    	  /* do the things */

    	  mailmime_decoded_part_free(mem);
    	  status = EXIT_SUCCESS;
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_quoted_printable_body_parse

    #include <libetpan/libetpan.h>

    int mailmime_quoted_printable_body_parse(const char * message, size_t length,
    					 size_t * index, char ** result,
    					 size_t * result_len, int in_header);

This function will parse a body part encoded using quoted printable.

- message is a string encoded using quoted printable.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) The result must be freed with mmap_string_unref().

Example 4-30. Parsing a quoted printable encoded part

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;
    	char * result;
    	size_t result_len;

    	current_index = 0;
    	r = mailmime_quoted_printable_body_parse(mem, stat_info.st_size,
              &current_index, &result, &result_len);
    	if (r == MAILIMF_NO_ERROR) {

    	  /* do the things */

    	  mailmime_decoded_part_free(mem);
    	  status = EXIT_SUCCESS;
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_binary_body_parse

    #include <libetpan/libetpan.h>

    int mailmime_binary_body_parse(const char * message, size_t length,
    			       size_t * index, char ** result,
    			       size_t * result_len);

This function will parse a body part encoded using binary (no encoding).

- message is a string encoded using binary.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- result. The result of the parse operation is stored in (* result) The result must be freed with mmap_string_unref().

Example 4-31. Parsing a binary encoded part

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;
    	char * result;
    	size_t result_len;

    	current_index = 0;
    	r = mailmime_binary_body_parse(mem, stat_info.st_size,
              &current_index, &result, &result_len);
    	if (r == MAILIMF_NO_ERROR) {

    	  /* do the things */

    	  mailmime_decoded_part_free(mem);
    	  status = EXIT_SUCCESS;
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

## mailmime_part_parse

    #include <libetpan/libetpan.h>

    enum {
      MAILMIME_MECHANISM_ERROR,
      MAILMIME_MECHANISM_7BIT,
      MAILMIME_MECHANISM_8BIT,
      MAILMIME_MECHANISM_BINARY,
      MAILMIME_MECHANISM_QUOTED_PRINTABLE,
      MAILMIME_MECHANISM_BASE64,
      MAILMIME_MECHANISM_TOKEN
    };

    int mailmime_part_parse(const char * message, size_t length,
    			size_t * index,
    			int encoding, char ** result, size_t * result_len);

This function will parse a body part encoded using a given MIME encoding mechanism.

- message is a string encoded using binary.

- length is the size of the given string.

- index is a pointer to the start of the address in the given string, (* index) is modified to point at the end of the parsed data.

- encoding is a MIME encoding mechanism. The value can be MAILMIME_MECHANISM_7BIT, MAILMIME_MECHANISM_8BIT, MAILMIME_MECHANISM_BINARY, MAILMIME_MECHANISM_QUOTED_PRINTABLE, MAILMIME_MECHANISM_BASE64 or MAILMIME_MECHANISM_TOKEN (see the Section called mailmime_mechanism - MIME transfer encoding mechanism (Content-Transfer-Encoding)).

- result. The result of the parse operation is stored in (* result) The result must be freed with mmap_string_unref().

Example 4-32. Parsing a MIME encoded part

    #include <libetpan/libetpan.h>
    #include <sys/stat.h>
    #include <sys/mman.h>

    int main(int argc, char ** argv)
    {
      int fd;
      int r;

      status = EXIT_FAILURE;

      fd = open("message.rfc2822", O_RDONLY);
      if (fd >= 0) {
        void * mem;
        struct stat stat_info;

        r = fstat(fd, &stat_info);
        if (r >= 0) {
          mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
          if (mem != MAP_FAILED) {
            struct mailimf_fields * f;
    	size_t current_index;
    	char * result;
    	size_t result_len;

    	current_index = 0;
    	r = mailmime_part_parse(mem, stat_info.st_size, &current_index,
    	  MAILMIME_MECHANISM_QUOTED_PRINTABLE, &result, &result_len);
    	if (r == MAILIMF_NO_ERROR) {

    	  /* do the things */

    	  mailmime_decoded_part_free(mem);
    	  status = EXIT_SUCCESS;
    	}
          }
          munmap(mem, stat_info.st_size);
        }

        close(fd);
      }

      exit(status);
    }

Prev | Home | Next
Data types | Up | Rendering of MIME parts