mirror of
https://github.com/orange-cpp/omath.git
synced 2026-06-10 09:14:34 +00:00
improved file scanners
This commit is contained in:
@@ -2,12 +2,13 @@
|
||||
// Created by Vladislav on 30.12.2025.
|
||||
//
|
||||
#pragma once
|
||||
#include "pattern_scan.hpp"
|
||||
#include "section_scan_result.hpp"
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include "section_scan_result.hpp"
|
||||
namespace omath
|
||||
{
|
||||
class ElfPatternScanner final
|
||||
@@ -18,14 +19,74 @@ namespace omath
|
||||
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<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<std::uintptr_t>
|
||||
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,
|
||||
&ElfPatternScanner::scan_section_for_pattern<Pattern>);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
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<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
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,
|
||||
&ElfPatternScanner::scan_section_for_pattern<Pattern>);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
scan_for_pattern_in_memory_file(std::span<const std::byte> file_data, const std::string_view& pattern,
|
||||
const std::string_view& target_section_name = ".text");
|
||||
|
||||
template<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
scan_for_pattern_in_memory_file(std::span<const std::byte> file_data,
|
||||
const std::string_view& target_section_name = ".text")
|
||||
{
|
||||
return scan_for_pattern_in_memory_file(file_data, target_section_name,
|
||||
&ElfPatternScanner::scan_section_for_pattern<Pattern>);
|
||||
}
|
||||
|
||||
private:
|
||||
using SectionScanFunction = std::optional<std::ptrdiff_t> (*)(std::span<const std::byte>);
|
||||
|
||||
template<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<std::ptrdiff_t> scan_section_for_pattern(const std::span<const std::byte> section_data)
|
||||
{
|
||||
const auto result = PatternScanner::scan_for_pattern<Pattern>(section_data.begin(), section_data.end());
|
||||
|
||||
if (result == section_data.end())
|
||||
return std::nullopt;
|
||||
|
||||
return result - section_data.begin();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::optional<std::uintptr_t>
|
||||
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<SectionScanResult> 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<SectionScanResult>
|
||||
scan_for_pattern_in_memory_file(std::span<const std::byte> file_data,
|
||||
const std::string_view& target_section_name, SectionScanFunction scan_pattern);
|
||||
};
|
||||
} // namespace omath
|
||||
} // namespace omath
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
// Created by Copilot on 04.02.2026.
|
||||
//
|
||||
#pragma once
|
||||
#include "pattern_scan.hpp"
|
||||
#include "section_scan_result.hpp"
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include "section_scan_result.hpp"
|
||||
namespace omath
|
||||
{
|
||||
class MachOPatternScanner final
|
||||
@@ -18,14 +19,74 @@ namespace omath
|
||||
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<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<std::uintptr_t>
|
||||
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,
|
||||
&MachOPatternScanner::scan_section_for_pattern<Pattern>);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
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<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
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,
|
||||
&MachOPatternScanner::scan_section_for_pattern<Pattern>);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
scan_for_pattern_in_memory_file(std::span<const std::byte> file_data, const std::string_view& pattern,
|
||||
const std::string_view& target_section_name = "__text");
|
||||
|
||||
template<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
scan_for_pattern_in_memory_file(std::span<const std::byte> file_data,
|
||||
const std::string_view& target_section_name = "__text")
|
||||
{
|
||||
return scan_for_pattern_in_memory_file(file_data, target_section_name,
|
||||
&MachOPatternScanner::scan_section_for_pattern<Pattern>);
|
||||
}
|
||||
|
||||
private:
|
||||
using SectionScanFunction = std::optional<std::ptrdiff_t> (*)(std::span<const std::byte>);
|
||||
|
||||
template<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<std::ptrdiff_t> scan_section_for_pattern(const std::span<const std::byte> section_data)
|
||||
{
|
||||
const auto result = PatternScanner::scan_for_pattern<Pattern>(section_data.begin(), section_data.end());
|
||||
|
||||
if (result == section_data.end())
|
||||
return std::nullopt;
|
||||
|
||||
return result - section_data.begin();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::optional<std::uintptr_t>
|
||||
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<SectionScanResult> 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<SectionScanResult>
|
||||
scan_for_pattern_in_memory_file(std::span<const std::byte> file_data,
|
||||
const std::string_view& target_section_name, SectionScanFunction scan_pattern);
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
@@ -3,12 +3,13 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "pattern_scan.hpp"
|
||||
#include "section_scan_result.hpp"
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include "section_scan_result.hpp"
|
||||
namespace omath
|
||||
{
|
||||
|
||||
@@ -20,14 +21,74 @@ namespace omath
|
||||
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<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<std::uintptr_t>
|
||||
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<Pattern>);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
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<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
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<Pattern>);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
scan_for_pattern_in_memory_file(std::span<const std::byte> file_data, const std::string_view& pattern,
|
||||
const std::string_view& target_section_name = ".text");
|
||||
|
||||
template<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<SectionScanResult>
|
||||
scan_for_pattern_in_memory_file(std::span<const std::byte> 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<Pattern>);
|
||||
}
|
||||
|
||||
private:
|
||||
using SectionScanFunction = std::optional<std::ptrdiff_t> (*)(std::span<const std::byte>);
|
||||
|
||||
template<PatternScanner::fixed_string Pattern>
|
||||
[[nodiscard]]
|
||||
static std::optional<std::ptrdiff_t> scan_section_for_pattern(const std::span<const std::byte> section_data)
|
||||
{
|
||||
const auto result = PatternScanner::scan_for_pattern<Pattern>(section_data.begin(), section_data.end());
|
||||
|
||||
if (result == section_data.end())
|
||||
return std::nullopt;
|
||||
|
||||
return result - section_data.begin();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::optional<std::uintptr_t>
|
||||
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<SectionScanResult> 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<SectionScanResult>
|
||||
scan_for_pattern_in_memory_file(std::span<const std::byte> file_data,
|
||||
const std::string_view& target_section_name, SectionScanFunction scan_pattern);
|
||||
};
|
||||
} // namespace omath
|
||||
} // namespace omath
|
||||
|
||||
Reference in New Issue
Block a user