// // Created by Vlad on 10/7/2025. // #pragma once #include "pattern_scan.hpp" #include "section_scan_result.hpp" #include #include #include #include #include namespace omath { class PePatternScanner final { public: [[nodiscard]] static std::optional scan_for_pattern_in_loaded_module(const void* module_base_address, const std::string_view& pattern, const std::string_view& target_section_name = ".text"); template [[nodiscard]] static std::optional scan_for_pattern_in_loaded_module(const void* module_base_address, const std::string_view& target_section_name = ".text") { return scan_for_pattern_in_loaded_module(module_base_address, target_section_name, &PePatternScanner::scan_section_for_pattern); } [[nodiscard]] static std::optional scan_for_pattern_in_file(const std::filesystem::path& path_to_file, const std::string_view& pattern, const std::string_view& target_section_name = ".text"); template [[nodiscard]] static std::optional scan_for_pattern_in_file(const std::filesystem::path& path_to_file, const std::string_view& target_section_name = ".text") { return scan_for_pattern_in_file(path_to_file, target_section_name, &PePatternScanner::scan_section_for_pattern); } [[nodiscard]] static std::optional scan_for_pattern_in_memory_file(std::span file_data, const std::string_view& pattern, const std::string_view& target_section_name = ".text"); template [[nodiscard]] static std::optional scan_for_pattern_in_memory_file(std::span file_data, const std::string_view& target_section_name = ".text") { return scan_for_pattern_in_memory_file(file_data, target_section_name, &PePatternScanner::scan_section_for_pattern); } private: using SectionScanFunction = std::optional (*)(std::span); template [[nodiscard]] static std::optional scan_section_for_pattern(const std::span section_data) { const auto result = PatternScanner::scan_for_pattern(section_data.begin(), section_data.end()); if (result == section_data.end()) return std::nullopt; return result - section_data.begin(); } [[nodiscard]] static std::optional scan_for_pattern_in_loaded_module(const void* module_base_address, const std::string_view& target_section_name, SectionScanFunction scan_pattern); [[nodiscard]] static std::optional scan_for_pattern_in_file(const std::filesystem::path& path_to_file, const std::string_view& target_section_name, SectionScanFunction scan_pattern); [[nodiscard]] static std::optional scan_for_pattern_in_memory_file(std::span file_data, const std::string_view& target_section_name, SectionScanFunction scan_pattern); }; } // namespace omath