SashaXser
2 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
51 additions and
6 deletions
-
src/goodbyedpi.c
|
|
@ -227,15 +227,60 @@ static void finalize_filter_strings() { |
|
|
|
} |
|
|
|
|
|
|
|
static char* dumb_memmem(const char* haystack, unsigned int hlen, const char* needle, unsigned int nlen) { |
|
|
|
|
|
|
|
if (nlen > hlen) return NULL; |
|
|
|
for (size_t i = 0; i < hlen - nlen + 1; i++) { |
|
|
|
if (memcmp(haystack + i, needle, nlen) == 0) { |
|
|
|
return (char*)(haystack + i); |
|
|
|
|
|
|
|
// KMP algorithm
|
|
|
|
int lps[nlen]; |
|
|
|
computeLPSArray(needle, nlen, lps); |
|
|
|
|
|
|
|
int i = 0; // index for haystack[]
|
|
|
|
int j = 0; // index for needle[]
|
|
|
|
while (i < hlen) { |
|
|
|
if (needle[j] == haystack[i]) { |
|
|
|
j++; |
|
|
|
i++; |
|
|
|
} |
|
|
|
|
|
|
|
if (j == nlen) { |
|
|
|
return (char*)(haystack + i - j); |
|
|
|
} |
|
|
|
|
|
|
|
// mismatch after j matches
|
|
|
|
else if (i < hlen && needle[j] != haystack[i]) { |
|
|
|
if (j != 0) |
|
|
|
j = lps[j-1]; |
|
|
|
else |
|
|
|
i = i+1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
void computeLPSArray(const char *pat, int M, int *lps) { |
|
|
|
int len = 0; |
|
|
|
lps[0] = 0; |
|
|
|
|
|
|
|
int i = 1; |
|
|
|
while (i < M) { |
|
|
|
if (pat[i] == pat[len]) { |
|
|
|
len++; |
|
|
|
lps[i] = len; |
|
|
|
i++; |
|
|
|
} |
|
|
|
else { |
|
|
|
if (len != 0) { |
|
|
|
len = lps[len-1]; |
|
|
|
} |
|
|
|
else { |
|
|
|
lps[i] = 0; |
|
|
|
i++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
unsigned short int atousi(const char *str, const char *msg) { |
|
|
|
long unsigned int res = strtoul(str, NULL, 10u); |
|
|
|
const unsigned short int limitValue = 0xFFFFu; |
|
|
|