Skip to content

Commit

Permalink
Simplify find_pattern_single for 16 byte alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroMemes committed Sep 30, 2024
1 parent 97777b0 commit 94b5890
Showing 1 changed file with 5 additions and 42 deletions.
47 changes: 5 additions & 42 deletions include/libhat/Scanner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,36 +237,15 @@ namespace hat {
const auto signature = context.signature;
const auto firstByte = *signature[0];

struct dummy_vector {
alignas(16) std::array<std::byte, 16> buf{};
const auto scanBegin = next_boundary_align<scan_alignment::X16>(begin);
const auto scanEnd = next_boundary_align<scan_alignment::X16>(end - signature.size() + 1);

[[nodiscard]] const std::byte* data() const {
return this->buf.data();
}
};

auto [pre, vec, post] = segment_scan<dummy_vector>(begin, end, signature.size(), 0);

if (!pre.empty()) {
for (auto i = next_boundary_align<scan_alignment::X16>(pre.data()); i < std::to_address(pre.end()); i += 16) {
if (*i == firstByte) {
if (static_cast<size_t>(end - i) < signature.size()) {
break;
}
auto match = std::equal(signature.begin() + 1, signature.end(), i + 1, [](auto opt, auto byte) {
return !opt.has_value() || *opt == byte;
});
if (match) LIBHAT_UNLIKELY {
return i;
}
}
}
if (scanBegin >= scanEnd) {
return nullptr;
}

for (auto& it : vec) {
const std::byte* i = it.data();
for (auto i = scanBegin; i != scanEnd; i += 16) {
if (*i == firstByte) {
// Compare everything after the first byte
auto match = std::equal(signature.begin() + 1, signature.end(), i + 1, [](auto opt, auto byte) {
return !opt.has_value() || *opt == byte;
});
Expand All @@ -276,22 +255,6 @@ namespace hat {
}
}

if (!post.empty()) {
for (auto i = next_boundary_align<scan_alignment::X16>(post.data()); i < std::to_address(post.end()); i += 16) {
if (*i == firstByte) {
if (static_cast<size_t>(end - i) < signature.size()) {
break;
}
auto match = std::equal(signature.begin() + 1, signature.end(), i + 1, [](auto opt, auto byte) {
return !opt.has_value() || *opt == byte;
});
if (match) LIBHAT_UNLIKELY {
return i;
}
}
}
}

return nullptr;
}

Expand Down

0 comments on commit 94b5890

Please sign in to comment.