]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: function my_memmem() to lookup binary contents
authorBaptiste Assmann <bedis9@gmail.com>
Sun, 6 Oct 2013 21:24:13 +0000 (23:24 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 6 Dec 2013 10:50:47 +0000 (11:50 +0100)
This function simply looks for a memory block inside another one.

Signed-off-by: Baptiste Assmann <bedis9@gmail.com>
include/common/standard.h
src/standard.c

index fe3e13c99547c288f667f03e826c481a2a79183a..be0762573d9c5575bc4256e4ba8ff0af3ef83280 100644 (file)
@@ -519,6 +519,12 @@ int parse_binary(const char *source, char **binstr, int *binstrlen, char **err);
 /* copies at most <n> characters from <src> 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 <key> in
  * ID tree <root>. Zero is returned if no place is found.
  */
index c31913980257e13a0c831ebe3cd6fbe5853f1ceb..1b7fdb0b720381dfc493efeb531d2e9c85c4c20b 100644 (file)
@@ -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 <key> in
  * ID tree <root>. Zero is returned if no place is found.
  */