From: Eric Leblond Date: Tue, 5 May 2015 12:31:55 +0000 (+0200) Subject: decode-mime: introduce MimeDecFindFieldsForEach X-Git-Tag: suricata-3.0RC1~115 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=714c30a127828bfff8e8c44e19c3ffc7316744df;p=thirdparty%2Fsuricata.git decode-mime: introduce MimeDecFindFieldsForEach This patch introduces a new function that can be used to handle multivalued MIME fields. A callback function can be called for each corresponding field value. --- diff --git a/src/util-decode-mime.c b/src/util-decode-mime.c index 718cdeb64b..8a36b26aaa 100644 --- a/src/util-decode-mime.c +++ b/src/util-decode-mime.c @@ -297,6 +297,35 @@ MimeDecField * MimeDecAddField(MimeDecEntity *entity) return node; } + +/** + * \brief Searches for header fields with the specified name + * + * \param entity The entity to search + * \param name The header name (lowercase) + * + * \return number of items found + * + */ +int MimeDecFindFieldsForEach(const MimeDecEntity *entity, const char *name, int (*DataCallback)(const uint8_t *val, const size_t, void *data), void *data) +{ + MimeDecField *curr = entity->field_list; + int found = 0; + + while (curr != NULL) { + /* name is stored lowercase */ + if (strlen(name) == curr->name_len) { + if (SCMemcmp(curr->name, name, curr->name_len) == 0) { + if (DataCallback(curr->value, curr->value_len, data)) + found++; + } + } + curr = curr->next; + } + + return found; +} + /** * \brief Searches for a header field with the specified name * diff --git a/src/util-decode-mime.h b/src/util-decode-mime.h index 50d67a92a3..5fb4cdb75d 100644 --- a/src/util-decode-mime.h +++ b/src/util-decode-mime.h @@ -224,6 +224,7 @@ void MimeDecFreeUrl(MimeDecUrl *url); /* List functions */ MimeDecField * MimeDecAddField(MimeDecEntity *entity); MimeDecField * MimeDecFindField(const MimeDecEntity *entity, const char *name); +int MimeDecFindFieldsForEach(const MimeDecEntity *entity, const char *name, int (*DataCallback)(const uint8_t *val, const size_t, void *data), void *data); MimeDecEntity * MimeDecAddEntity(MimeDecEntity *parent); /* Helper functions */