range patterns

This commit is contained in:
Silent 2017-03-17 00:48:16 +01:00
parent 560c925235
commit 477595bcd5
2 changed files with 41 additions and 4 deletions

View file

@ -116,7 +116,7 @@ public:
return (TReturn*)(m_begin + rva); return (TReturn*)(m_begin + rva);
} }
executable_meta(void* module) explicit executable_meta(void* module)
: m_begin((uintptr_t)module) : m_begin((uintptr_t)module)
{ {
PIMAGE_DOS_HEADER dosHeader = getRVA<IMAGE_DOS_HEADER>(0); PIMAGE_DOS_HEADER dosHeader = getRVA<IMAGE_DOS_HEADER>(0);
@ -125,6 +125,11 @@ public:
m_end = m_begin + ntHeader->OptionalHeader.SizeOfCode; 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 begin() const { return m_begin; }
inline uintptr_t end() const { return m_end; } inline uintptr_t end() const { return m_end; }
}; };
@ -173,7 +178,7 @@ void pattern::EnsureMatches(uint32_t maxCount)
} }
// scan the executable for code // 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) auto matchSuccess = [&] (uintptr_t address)
{ {

View file

@ -78,11 +78,24 @@ namespace hook
bool m_matched; bool m_matched;
union
{
void* m_module; void* m_module;
struct
{
uintptr_t m_rangeStart;
uintptr_t m_rangeEnd;
};
};
protected: protected:
inline pattern(void* module) 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; return *this;
} }
inline pattern& clear()
{
m_matches.clear();
m_matched = false;
return *this;
}
inline size_t size() inline size_t size()
{ {
EnsureMatches(UINT32_MAX); EnsureMatches(UINT32_MAX);
@ -166,6 +186,18 @@ namespace hook
} }
}; };
class range_pattern
: public pattern
{
public:
template<size_t Len>
range_pattern(uintptr_t begin, uintptr_t end, const char(&pattern)[Len])
: pattern(begin, end)
{
Initialize(pattern, Len);
}
};
template<typename T = void, size_t Len> template<typename T = void, size_t Len>
auto get_pattern(const char(&pattern_string)[Len], ptrdiff_t offset = 0) auto get_pattern(const char(&pattern_string)[Len], ptrdiff_t offset = 0)