aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorVladimir Gavrilov <105977161+0xA50C1A1@users.noreply.github.com>2024-07-15 21:43:19 +0300
committerGitHub <noreply@github.com>2024-07-15 20:43:19 +0200
commit996cddbd184d98f006ac7320a1afd1db373036cd (patch)
treefbb382f7ef01f53d132a53a7a2645d31e08aa9f1 /src/lib
parent2f66a6a3e1829b764d2acabf8745a525c68ccfb6 (diff)
Refactor ndpi_strnstr to use ndpi_memmem (#2500)
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/ndpi_main.c47
1 files changed, 4 insertions, 43 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c
index 5c1596ba2..a07037b92 100644
--- a/src/lib/ndpi_main.c
+++ b/src/lib/ndpi_main.c
@@ -9737,51 +9737,11 @@ void ndpi_dump_risks_score(FILE *risk_out) {
char *ndpi_strnstr(const char *haystack, const char *needle, size_t len)
{
- if (!haystack || !needle)
- {
- return NULL;
- }
-
- const size_t needle_len = strlen(needle);
-
- if (needle_len == 0)
- {
- return (char *)haystack;
- }
-
- const size_t hs_real_len = strnlen(haystack, len);
-
- if (needle_len == 1)
- {
- return (char *)memchr(haystack, *needle, hs_real_len);
- }
-
- if (needle_len > hs_real_len)
- {
+ if (!haystack || !needle) {
return NULL;
}
- const char *const end_of_search = haystack + hs_real_len - needle_len + 1;
-
- const char *current = haystack;
- while (current < end_of_search)
- {
- current = (const char *)memchr(current, *needle, end_of_search - current);
-
- if (!current)
- {
- return NULL;
- }
-
- if (memcmp(current, needle, needle_len) == 0)
- {
- return (char *)current;
- }
-
- ++current;
- }
-
- return NULL;
+ return (char *)ndpi_memmem(haystack, strnlen(haystack, len), needle, strlen(needle));
}
/* ****************************************************** */
@@ -11452,7 +11412,8 @@ void* ndpi_memmem(const void* haystack, size_t haystack_len, const void* needle,
const u_int8_t *const end_of_search = (const u_int8_t *)haystack + haystack_len - needle_len + 1;
const u_int8_t *current = (const u_int8_t *)haystack;
- while (current < end_of_search) {
+
+ while (1) {
/* Find the first occurrence of the first character from the needle */
current = (const u_int8_t *)memchr(current, *(const u_int8_t *)needle, end_of_search - current);