Skip to content

Commit

Permalink
fixed algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanExtreme002 committed Dec 2, 2023
1 parent 154b966 commit 0b23d3f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
5 changes: 2 additions & 3 deletions PyMemoryEditor/util/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions PyMemoryEditor/util/search/bmh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -18,18 +18,18 @@ 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

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]:
"""
Expand Down

0 comments on commit 0b23d3f

Please sign in to comment.