From: Baptiste Assmann Date: Sun, 6 Oct 2013 21:24:13 +0000 (+0200) Subject: MINOR: tools: function my_memmem() to lookup binary contents X-Git-Tag: v1.5-dev20~152 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb77c8e26de3c35e885a4bca390015768daba261;p=thirdparty%2Fhaproxy.git MINOR: tools: function my_memmem() to lookup binary contents This function simply looks for a memory block inside another one. Signed-off-by: Baptiste Assmann --- diff --git a/include/common/standard.h b/include/common/standard.h index fe3e13c995..be0762573d 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -519,6 +519,12 @@ int parse_binary(const char *source, char **binstr, int *binstrlen, char **err); /* copies at most characters from and always terminates with '\0' */ char *my_strndup(const char *src, int n); +/* + * search needle in haystack + * returns the pointer if found, returns NULL otherwise + */ +const void *my_memmem(const void *, size_t, const void *, size_t); + /* This function returns the first unused key greater than or equal to in * ID tree . Zero is returned if no place is found. */ diff --git a/src/standard.c b/src/standard.c index c319139802..1b7fdb0b72 100644 --- a/src/standard.c +++ b/src/standard.c @@ -1415,6 +1415,31 @@ char *my_strndup(const char *src, int n) return ret; } +/* + * search needle in haystack + * returns the pointer if found, returns NULL otherwise + */ +const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) +{ + const void *c = NULL; + unsigned char f; + + if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen)) + return NULL; + + f = *(char *)needle; + c = haystack; + while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) { + if ((haystacklen - (c - haystack)) < needlelen) + return NULL; + + if (memcmp(c, needle, needlelen) == 0) + return c; + ++c; + } + return NULL; +} + /* This function returns the first unused key greater than or equal to in * ID tree . Zero is returned if no place is found. */