From 477595bcd52cbecf6dde3663823ac86d94bb8ac5 Mon Sep 17 00:00:00 2001 From: Silent Date: Fri, 17 Mar 2017 00:48:16 +0100 Subject: [PATCH] range patterns --- SilentPatch/Patterns.cpp | 9 +++++++-- SilentPatch/Patterns.h | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/SilentPatch/Patterns.cpp b/SilentPatch/Patterns.cpp index 5e0f054..65bd3e6 100644 --- a/SilentPatch/Patterns.cpp +++ b/SilentPatch/Patterns.cpp @@ -116,7 +116,7 @@ public: return (TReturn*)(m_begin + rva); } - executable_meta(void* module) + explicit executable_meta(void* module) : m_begin((uintptr_t)module) { PIMAGE_DOS_HEADER dosHeader = getRVA(0); @@ -125,6 +125,11 @@ public: m_end = m_begin + ntHeader->OptionalHeader.SizeOfCode; } + executable_meta(uintptr_t begin, uintptr_t end) + : m_begin(begin), m_end(end) + { + } + inline uintptr_t begin() const { return m_begin; } inline uintptr_t end() const { return m_end; } }; @@ -173,7 +178,7 @@ void pattern::EnsureMatches(uint32_t maxCount) } // scan the executable for code - executable_meta executable(m_module); + executable_meta executable = m_rangeStart != 0 && m_rangeEnd != 0 ? executable_meta(m_rangeStart, m_rangeEnd) : executable_meta(m_module); auto matchSuccess = [&] (uintptr_t address) { diff --git a/SilentPatch/Patterns.h b/SilentPatch/Patterns.h index 4882742..41c1331 100644 --- a/SilentPatch/Patterns.h +++ b/SilentPatch/Patterns.h @@ -78,11 +78,24 @@ namespace hook bool m_matched; - void* m_module; + union + { + void* m_module; + struct + { + uintptr_t m_rangeStart; + uintptr_t m_rangeEnd; + }; + }; protected: inline pattern(void* module) - : m_module(module), m_matched(false) + : m_module(module), m_rangeEnd(0), m_matched(false) + { + } + + inline pattern(uintptr_t begin, uintptr_t end) + : m_rangeStart(begin), m_rangeEnd(end), m_matched(false) { } @@ -119,6 +132,13 @@ namespace hook return *this; } + inline pattern& clear() + { + m_matches.clear(); + m_matched = false; + return *this; + } + inline size_t size() { EnsureMatches(UINT32_MAX); @@ -166,6 +186,18 @@ namespace hook } }; + class range_pattern + : public pattern + { + public: + template + range_pattern(uintptr_t begin, uintptr_t end, const char(&pattern)[Len]) + : pattern(begin, end) + { + Initialize(pattern, Len); + } + }; + template auto get_pattern(const char(&pattern_string)[Len], ptrdiff_t offset = 0)