wildcard search

This commit is contained in:
CrazyRedMachine 2024-06-07 01:33:25 +02:00
parent f5efc561c0
commit 60f1b172ef
2 changed files with 27 additions and 6 deletions

View File

@ -26,7 +26,7 @@ static void badCharHeuristic(const unsigned char *str, int size, int* badchar) {
#define DEBUG_SEARCH 0 #define DEBUG_SEARCH 0
int _search(unsigned char *haystack, size_t haystack_size, const unsigned char *needle, size_t needle_size, int orig_offset, int debug) { int _search(unsigned char *haystack, size_t haystack_size, const unsigned char *needle, size_t needle_size, int orig_offset, bool wildcards, int debug) {
int badchar[NO_OF_CHARS]; int badchar[NO_OF_CHARS];
badCharHeuristic(needle, needle_size, badchar); badCharHeuristic(needle, needle_size, badchar);
@ -46,12 +46,21 @@ int _search(unsigned char *haystack, size_t haystack_size, const unsigned char *
LOG("pat..."); LOG("pat...");
for (size_t i = 0; i < needle_size; i++) for (size_t i = 0; i < needle_size; i++)
{ {
if (wildcards && needle[i] == '?')
LOG("** ");
else
LOG("%02x ", needle[i]); LOG("%02x ", needle[i]);
} }
LOG("\n"); LOG("\n");
} }
while (j >= 0 && needle[j] == haystack[orig_offset + s + j]) if ( wildcards )
{
while (j >= 0 && ( needle[j] == '?' || needle[j] == haystack[orig_offset + s + j]) )
j--; j--;
} else {
while (j >= 0 && ( needle[j] == haystack[orig_offset + s + j]) )
j--;
}
if (j < 0) { if (j < 0) {
if (debug) if (debug)
@ -70,11 +79,21 @@ int _search(unsigned char *haystack, size_t haystack_size, const unsigned char *
} }
int search(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset) { int search(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset) {
int res = _search((unsigned char*) haystack, haystack_size, (const unsigned char *)needle, needle_size, orig_offset, 0); int res = _search((unsigned char*) haystack, haystack_size, (const unsigned char *)needle, needle_size, orig_offset, false, 0);
return res;
}
int wildcard_search(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset) {
int res = _search((unsigned char*) haystack, haystack_size, (const unsigned char *)needle, needle_size, orig_offset, true, 0);
return res; return res;
} }
int search_debug(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset) { int search_debug(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset) {
int res = _search((unsigned char*) haystack, haystack_size, (const unsigned char *)needle, needle_size, orig_offset, 2); int res = _search((unsigned char*) haystack, haystack_size, (const unsigned char *)needle, needle_size, orig_offset, false, 2);
return res;
}
int wildcard_search_debug(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset) {
int res = _search((unsigned char*) haystack, haystack_size, (const unsigned char *)needle, needle_size, orig_offset, true, 2);
return res; return res;
} }

View File

@ -3,5 +3,7 @@
int search(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset); int search(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset);
int search_debug(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset); int search_debug(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset);
int wildcard_search(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset);
int wildcard_search_debug(char *haystack, size_t haystack_size, const char *needle, size_t needle_size, size_t orig_offset);
#endif #endif