diff options
author | Vladimir Gavrilov <105977161+0xA50C1A1@users.noreply.github.com> | 2024-04-10 16:39:08 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-10 15:39:08 +0200 |
commit | 142c8f5afb90629762920db6703831826513e00b (patch) | |
tree | f69bced760416ce737c0525fd5fc75f64053af76 /src/lib/ndpi_main.c | |
parent | 1d0be6c4f4a87e7c6d29aa35e383f7f2ba62a967 (diff) |
Add memmem() implementation (#2378)
* Add memmem() implementation
* Fix build
* Add fix to avoid too many memcmp calls
Diffstat (limited to 'src/lib/ndpi_main.c')
-rw-r--r-- | src/lib/ndpi_main.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 9bb5010fd..67eff0e99 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -11447,3 +11447,31 @@ char *ndpi_dump_config(struct ndpi_detection_module_struct *ndpi_str, } return NULL; } + +void* ndpi_memmem(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len) +{ + if (!haystack || !needle) { + return NULL; + } + + if (needle_len > haystack_len) { + return NULL; + } + + if ((size_t)(const u_int8_t*)haystack+haystack_len < haystack_len) { + return NULL; + } + + if (needle_len == 1) { + return memchr(haystack, *(int*)needle, haystack_len); + } + + const u_int8_t* h = NULL; + const u_int8_t* n = (const u_int8_t*)needle; + for (h = haystack; h <= (const u_int8_t*)haystack+haystack_len-needle_len; ++h) { + if (*h == n[0] && !memcmp(h, needle, needle_len)) + return (void*)h; + } + + return NULL; +} |