Skip to content

Commit

Permalink
Implement finding multiple occurrences of a byte pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Dec 12, 2023
1 parent b0bae76 commit 547c7c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
23 changes: 18 additions & 5 deletions Source/MemorySearch/HybridPatternFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,33 @@

class HybridPatternFinder {
public:
HybridPatternFinder(std::span<const std::byte> bytes, BytePattern pattern)
HybridPatternFinder(std::span<const std::byte> bytes, BytePattern pattern) noexcept
: bytes{ bytes }, pattern{ pattern }
{
}

[[nodiscard]] const std::byte* operator()() const noexcept
[[nodiscard]] const std::byte* findNextOccurrence() noexcept
{
PatternFinderSIMD simdFinder{ bytes, pattern };
if (const auto foundSIMD = simdFinder())
PatternFinderSIMD simdFinder{bytes, pattern};
if (const auto foundSIMD = simdFinder()) {
updateRemainingBytes(foundSIMD);
return foundSIMD;
return PatternFinderScalar{ simdFinder.getNotCheckedBytes(), pattern }();
}

if (const auto foundScalar{PatternFinderScalar{simdFinder.getNotCheckedBytes(), pattern}()}) {
updateRemainingBytes(foundScalar);
return foundScalar;
}

return nullptr;
}

private:
void updateRemainingBytes(const std::byte* foundPosition) noexcept
{
bytes = {foundPosition + 1, bytes.data() + bytes.size()};
}

std::span<const std::byte> bytes;
BytePattern pattern;
};
4 changes: 2 additions & 2 deletions Source/MemorySearch/PatternFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ struct PatternFinder {

[[nodiscard]] [[NOINLINE]] SafeAddress operator()(BytePattern pattern) const noexcept
{
const auto found = HybridPatternFinder{ bytes, pattern }();
const auto found = HybridPatternFinder{ bytes, pattern }.findNextOccurence();
if (!found)
notFoundHandler(pattern, bytes);
return SafeAddress{ found };
}

[[nodiscard]] [[NOINLINE]] SafeAddress operator()(BytePattern pattern, OffsetHint offsetHint) const noexcept
{
const auto foundWithHint = HybridPatternFinder{ getSliceForHint(offsetHint), pattern }();
const auto foundWithHint = HybridPatternFinder{ getSliceForHint(offsetHint), pattern }.findNextOccurence();
if (foundWithHint)
return SafeAddress{ foundWithHint };
return operator()(pattern);
Expand Down

0 comments on commit 547c7c8

Please sign in to comment.