-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatterns.h
More file actions
202 lines (165 loc) · 4.24 KB
/
Copy pathPatterns.h
File metadata and controls
202 lines (165 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* This file is part of the CitizenFX project - http://citizen.re/
*
* See LICENSE and MENTIONS in the root of the source tree for information
* regarding licensing.
*/
#pragma once
#include <cassert>
#include <vector>
#include <string>
#include <string_view>
namespace hook
{
extern ptrdiff_t baseAddressDifference;
// sets the base address difference based on an obtained pointer
inline void set_base(uintptr_t address)
{
#ifdef _M_IX86
uintptr_t addressDiff = (address - 0x400000);
#elif defined(_M_AMD64)
uintptr_t addressDiff = (address - 0x140000000);
#endif
// pointer-style cast to ensure unsigned overflow ends up copied directly into a signed value
baseAddressDifference = *(ptrdiff_t*)&addressDiff;
}
// sets the base to the process main base
void set_base();
inline uintptr_t getRVA(uintptr_t rva)
{
set_base();
#ifdef _M_IX86
return static_cast<uintptr_t>(baseAddressDifference + 0x400000 + rva);
#elif defined(_M_AMD64)
return static_cast<uintptr_t>(baseAddressDifference + 0x140000000 + rva);
#endif
}
class pattern_match
{
private:
void* m_pointer;
public:
inline pattern_match(void* pointer)
: m_pointer(pointer)
{
}
template<typename T>
T* get(ptrdiff_t offset = 0) const
{
char* ptr = reinterpret_cast<char*>(m_pointer);
return reinterpret_cast<T*>(ptr + offset);
}
};
class pattern
{
private:
std::basic_string<uint8_t> m_bytes;
std::basic_string<uint8_t> m_mask;
#if PATTERNS_USE_HINTS
uint64_t m_hash;
#endif
std::vector<pattern_match> m_matches;
bool m_matched = false;
uintptr_t m_rangeStart;
uintptr_t m_rangeEnd;
private:
void Initialize(std::string_view pattern);
bool ConsiderHint(uintptr_t offset);
void EnsureMatches(uint32_t maxCount);
inline pattern_match _get_internal(size_t index) const
{
return m_matches[index];
}
inline pattern(uintptr_t module)
: pattern(module, 0)
{
}
inline pattern(uintptr_t begin, uintptr_t end)
: m_rangeStart(begin), m_rangeEnd(end)
{
}
public:
pattern(std::string_view pattern)
: hook::pattern(getRVA(0))
{
Initialize(std::move(pattern));
}
inline pattern(void* module, std::string_view pattern)
: hook::pattern(reinterpret_cast<uintptr_t>(module))
{
Initialize(std::move(pattern));
}
inline pattern(uintptr_t begin, uintptr_t end, std::string_view pattern)
: m_rangeStart(begin), m_rangeEnd(end)
{
Initialize(std::move(pattern));
}
inline pattern&& count(uint32_t expected)
{
EnsureMatches(expected);
return std::forward<pattern>(*this);
}
inline pattern&& count_hint(uint32_t expected)
{
EnsureMatches(expected);
return std::forward<pattern>(*this);
}
inline pattern&& clear()
{
m_matches.clear();
m_matched = false;
return std::forward<pattern>(*this);
}
inline size_t size()
{
EnsureMatches(UINT32_MAX);
return m_matches.size();
}
inline bool empty()
{
return size() == 0;
}
inline pattern_match get(size_t index)
{
EnsureMatches(UINT32_MAX);
return _get_internal(index);
}
inline pattern_match get_one()
{
return std::forward<pattern>(*this).count(1)._get_internal(0);
}
template<typename T = void>
inline auto get_first(ptrdiff_t offset = 0)
{
return get_one().get<T>(offset);
}
template <typename Pred>
inline Pred for_each_result(Pred&& pred)
{
EnsureMatches(UINT32_MAX);
for (auto it : m_matches)
{
std::forward<Pred>(pred)(it);
}
return std::forward<Pred>(pred);
}
public:
#if PATTERNS_USE_HINTS && PATTERNS_CAN_SERIALIZE_HINTS
// define a hint
static void hint(uint64_t hash, uintptr_t address);
#endif
};
inline pattern make_module_pattern(void* module, std::string_view bytes)
{
return pattern(module, std::move(bytes));
}
inline pattern make_range_pattern(uintptr_t begin, uintptr_t end, std::string_view bytes)
{
return pattern(begin, end, std::move(bytes));
}
template<typename T = void>
inline auto get_pattern(std::string_view pattern_string, ptrdiff_t offset = 0)
{
return pattern(std::move(pattern_string)).get_first<T>(offset);
}
}