From 0b23d3f2f66bf14400bfbc7e42529a2c5cc88dee Mon Sep 17 00:00:00 2001 From: JeanExtreme002 Date: Sat, 2 Dec 2023 02:25:12 -0300 Subject: [PATCH] fixed algorithm --- PyMemoryEditor/util/scan.py | 5 ++--- PyMemoryEditor/util/search/bmh.py | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/PyMemoryEditor/util/scan.py b/PyMemoryEditor/util/scan.py index f600801..a62135a 100644 --- a/PyMemoryEditor/util/scan.py +++ b/PyMemoryEditor/util/scan.py @@ -21,10 +21,10 @@ def scan_memory_for_exact_value( This method uses an efficient searching algorithm. """ - kmp_searcher = KMPSearch(target_value, target_value_size) + searcher = KMPSearch(target_value, target_value_size) last_index = 0 - for found_index in kmp_searcher.search(memory_region_data, memory_region_data_size): + for found_index in searcher.search(memory_region_data, memory_region_data_size): # Return the found index if user is searching for an exact value. if comparison is ScanTypesEnum.EXACT_VALUE: @@ -34,7 +34,6 @@ def scan_memory_for_exact_value( # Return the interval between last_index and found_address, if user is searching for a different value. for different_index in range(last_index, found_index): yield different_index - last_index = found_index + 1 # If user is searching for a different value, return the rest of the addresses that were not found. diff --git a/PyMemoryEditor/util/search/bmh.py b/PyMemoryEditor/util/search/bmh.py index 7488379..1e1a6e5 100644 --- a/PyMemoryEditor/util/search/bmh.py +++ b/PyMemoryEditor/util/search/bmh.py @@ -8,7 +8,7 @@ class BMHSearch(AbstractSearchAlgorithm): """ Algorithm Boyer-Moore-Horspool (BMH) for matching pattern in sequences. """ - def __init__(self, pattern: Sequence, pattern_length: Optional[int] = None): + def __init__(self, pattern: Sequence, pattern_length: Optional[int] = None, alphabet_length: int = 256): if pattern_length is None: pattern_length = len(pattern) @@ -18,7 +18,7 @@ def __init__(self, pattern: Sequence, pattern_length: Optional[int] = None): self.__pattern = pattern self.__pattern_length = pattern_length - self.__skip = defaultdict(lambda: self.__pattern_length) + self.__skip = [self.__pattern_length,] * alphabet_length for k in range(self.__pattern_length - 1): self.__skip[self.__get_value(pattern[k])] = self.__pattern_length - k - 1 @@ -26,10 +26,10 @@ def __init__(self, pattern: Sequence, pattern_length: Optional[int] = None): def __get_value(self, element: Union[str, int]) -> int: """ Return the ID of the element, whether element is a string. - If element is an integer, return itself or (256 - element) whether it is negative. + If element is an integer, return itself or (256 + element) whether it is negative. """ if self.__is_string: return ord(element) - else: return (256 - element) if element < 0 else element + else: return (256 + element) if element < 0 else element def search(self, sequence: Sequence, length: Optional[int] = None) -> Generator[int, None, None]: """