From 5178a0cbbad2327c38c1fa19ed74c5697518a181 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 1 Dec 2019 16:00:44 +0000 Subject: libtokencap, simple optimised memmem implementation enough for this lib proposal --- libtokencap/libtokencap.so.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'libtokencap/libtokencap.so.c') diff --git a/libtokencap/libtokencap.so.c b/libtokencap/libtokencap.so.c index 467be05b..eea6d29f 100644 --- a/libtokencap/libtokencap.so.c +++ b/libtokencap/libtokencap.so.c @@ -425,6 +425,36 @@ char* strcasestr(const char* haystack, const char* needle) { } +#undef memmem + +void* memmem(const void *haystack, size_t haystack_len, const void *needle, size_t needle_len) +{ + if (__tokencap_is_ro(haystack)) + __tokencap_dump(haystack, haystack_len, 1); + + if (__tokencap_is_ro(needle)) __tokencap_dump(needle, needle_len, 1); + + const char *n = (const char *)needle; + const char *h = (const char *)haystack; + if (haystack_len < needle_len) return 0; + if (needle_len == 0) return (void *)haystack; + if (needle_len == 1) return memchr(haystack, *n, haystack_len); + + const char *end = h + (haystack_len - needle_len); + + do { + + if (*h == *n) { + + if (memcmp(h, n, needle_len) == 0) return (void *)h; + } + + } while (h++ <= end); + + return 0; + +} + /* Init code to open the output file (or default to stderr). */ __attribute__((constructor)) void __tokencap_init(void) { -- cgit 1.4.1