fixed for mac improved readability

This commit is contained in:
2025-10-08 07:26:23 +03:00
parent d84259fdcc
commit e1b4375621
6 changed files with 37 additions and 17 deletions

4
.idea/editor.xml generated
View File

@@ -201,7 +201,7 @@
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStaticDataMemberInUnnamedStruct/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStaticDataMemberInUnnamedStruct/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStaticSpecifierOnAnonymousNamespaceMember/@EntryIndexedValue" value="SUGGESTION" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStaticSpecifierOnAnonymousNamespaceMember/@EntryIndexedValue" value="SUGGESTION" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStringLiteralToCharPointerConversion/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppStringLiteralToCharPointerConversion/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTabsAreDisallowed/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTabsAreDisallowed/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateArgumentsCanBeDeduced/@EntryIndexedValue" value="HINT" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateArgumentsCanBeDeduced/@EntryIndexedValue" value="HINT" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateParameterNeverUsed/@EntryIndexedValue" value="HINT" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateParameterNeverUsed/@EntryIndexedValue" value="HINT" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateParameterShadowing/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppTemplateParameterShadowing/@EntryIndexedValue" value="WARNING" type="string" />
@@ -215,7 +215,7 @@
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnmatchedPragmaEndRegionDirective/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnmatchedPragmaEndRegionDirective/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnmatchedPragmaRegionDirective/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnmatchedPragmaRegionDirective/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnnamedNamespaceInHeaderFile/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnnamedNamespaceInHeaderFile/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnnecessaryWhitespace/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnnecessaryWhitespace/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnsignedZeroComparison/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnsignedZeroComparison/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnusedIncludeDirective/@EntryIndexedValue" value="WARNING" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUnusedIncludeDirective/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUseAlgorithmWithCount/@EntryIndexedValue" value="SUGGESTION" type="string" /> <option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppUseAlgorithmWithCount/@EntryIndexedValue" value="SUGGESTION" type="string" />

View File

@@ -36,11 +36,11 @@ namespace omath
public: public:
[[nodiscard]] [[nodiscard]]
static std::span<std::byte>::const_iterator scan_for_pattern(const std::span<std::byte>& range, static std::span<std::byte>::iterator scan_for_pattern(const std::span<std::byte>& range,
const std::string_view& pattern); const std::string_view& pattern);
[[nodiscard]] [[nodiscard]]
static std::span<std::byte>::const_iterator scan_for_pattern(std::span<std::byte>&& range, static std::span<std::byte>::iterator scan_for_pattern(std::span<std::byte>&& range,
const std::string_view& pattern) = delete; const std::string_view& pattern) = delete;
template<class IteratorType> template<class IteratorType>

View File

@@ -7,6 +7,8 @@
#include <filesystem> #include <filesystem>
#include <optional> #include <optional>
#include <string_view> #include <string_view>
#include <vector>
namespace omath namespace omath
{ {
class PePatternScanner final class PePatternScanner final

View File

@@ -4,13 +4,30 @@
#include "omath/utility/pattern_scan.hpp" #include "omath/utility/pattern_scan.hpp"
#include <charconv> #include <charconv>
#include <cstdint> #include <cstdint>
#include <algorithm>
namespace
{
[[nodiscard]]
constexpr bool is_wildcard(const std::string_view& byte_str)
{
return byte_str == "?" || byte_str == "??";
}
[[nodiscard]]
constexpr bool invalid_byte_str_size(const std::string_view& byte_str)
{
return byte_str.empty() || byte_str.size() >= 3;
}
}
namespace omath namespace omath
{ {
std::span<std::byte>::const_iterator std::span<std::byte>::iterator
PatternScanner::scan_for_pattern(const std::span<std::byte>& range, const std::string_view& pattern) PatternScanner::scan_for_pattern(const std::span<std::byte>& range, const std::string_view& pattern)
{ {
return scan_for_pattern(range.cbegin(), range.cend(), pattern); return scan_for_pattern(range.begin(), range.end(), pattern);
} }
std::expected<std::vector<std::optional<std::byte>>, PatternScanError> std::expected<std::vector<std::optional<std::byte>>, PatternScanError>
PatternScanner::parse_pattern(const std::string_view& pattern_string) PatternScanner::parse_pattern(const std::string_view& pattern_string)
@@ -28,13 +45,13 @@ namespace omath
const std::string_view byte_str = pattern_string.substr(sting_view_start, sting_view_end); const std::string_view byte_str = pattern_string.substr(sting_view_start, sting_view_end);
if (byte_str.empty()) [[unlikely]] if (invalid_byte_str_size(byte_str)) [[unlikely]]
{ {
start = end != pattern_string.end() ? std::next(end) : end; start = end != pattern_string.end() ? std::next(end) : end;
continue; continue;
} }
if (byte_str == "?" || byte_str == "??") if (is_wildcard(byte_str))
{ {
pattern.emplace_back(std::nullopt); pattern.emplace_back(std::nullopt);

View File

@@ -15,8 +15,8 @@ namespace omath
{ {
std::optional<std::uintptr_t> std::optional<std::uintptr_t>
PePatternScanner::scan_for_pattern_in_loaded_module(const std::string_view& module_name, PePatternScanner::scan_for_pattern_in_loaded_module([[maybe_unused]] const std::string_view& module_name,
const std::string_view& pattern) [[maybe_unused]] const std::string_view& pattern)
{ {
#ifdef _WIN32 #ifdef _WIN32
const auto base_address = reinterpret_cast<std::uintptr_t>(GetModuleHandleA(module_name.data())); const auto base_address = reinterpret_cast<std::uintptr_t>(GetModuleHandleA(module_name.data()));
@@ -59,9 +59,10 @@ namespace omath
return std::distance(pe_section->begin(), pe_section->end()); return std::distance(pe_section->begin(), pe_section->end());
} }
std::optional<std::vector<std::byte>> std::optional<std::vector<std::byte>>
PePatternScanner::extract_section_from_pe_file(const std::filesystem::path& path_to_file, PePatternScanner::extract_section_from_pe_file([[maybe_unused]] const std::filesystem::path& path_to_file,
const std::string_view& section_name) [[maybe_unused]] const std::string_view& section_name)
{ {
#ifdef _WIN32
std::fstream file(path_to_file, std::ios::binary | std::ios::in); std::fstream file(path_to_file, std::ios::binary | std::ios::in);
if (!file.is_open()) [[unlikely]] if (!file.is_open()) [[unlikely]]
@@ -103,5 +104,8 @@ namespace omath
return section_data; return section_data;
} }
return std::nullopt; return std::nullopt;
#else
throw std::runtime_error("Pattern scan for loaded modules is only for windows platform");
#endif
} }
} // namespace omath } // namespace omath

View File

@@ -48,10 +48,7 @@ TEST(unit_test_pattern_scan, corner_case_3)
TEST(unit_test_pattern_scan, corner_case_4) TEST(unit_test_pattern_scan, corner_case_4)
{ {
const auto result = omath::PatternScanner::parse_pattern("X ? ?? E9 "); const auto result = omath::PatternScanner::parse_pattern("XZ");
const auto result2 = omath::PePatternScanner::scan_for_pattern_in_file(
std::filesystem::path{
"C:\\Users\\Vlad\\CLionProjects\\l4bhop\\cmake-build\\build\\windows-release\\l4bhop.dll"},
"4C 8B D1 B8 ? ? ? ? F6 04 25 ? ? ? ? ? 75 ? 0F 05 C3");
EXPECT_FALSE(result.has_value()); EXPECT_FALSE(result.has_value());
} }