From 60f1b172eff9b1a812f614f5398d3d00d1023579 Mon Sep 17 00:00:00 2001 From: CrazyRedMachine Date: Fri, 7 Jun 2024 01:33:25 +0200 Subject: [PATCH] wildcard search --- util/search.cc | 31 +++++++++++++++++++++++++------ util/search.h | 2 ++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/util/search.cc b/util/search.cc index 958bc67..5278bc5 100644 --- a/util/search.cc +++ b/util/search.cc @@ -26,7 +26,7 @@ static void badCharHeuristic(const unsigned char *str, int size, int* badchar) { #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]; badCharHeuristic(needle, needle_size, badchar); @@ -46,12 +46,21 @@ int _search(unsigned char *haystack, size_t haystack_size, const unsigned char * LOG("pat..."); for (size_t i = 0; i < needle_size; i++) { - LOG("%02x ", needle[i]); + if (wildcards && needle[i] == '?') + LOG("** "); + else + LOG("%02x ", needle[i]); } LOG("\n"); } - while (j >= 0 && needle[j] == haystack[orig_offset + s + j]) - j--; + if ( wildcards ) + { + while (j >= 0 && ( needle[j] == '?' || needle[j] == haystack[orig_offset + s + j]) ) + j--; + } else { + while (j >= 0 && ( needle[j] == haystack[orig_offset + s + j]) ) + j--; + } if (j < 0) { 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 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; } 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; } \ No newline at end of file diff --git a/util/search.h b/util/search.h index cb35012..9a56b80 100644 --- a/util/search.h +++ b/util/search.h @@ -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_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