-
Notifications
You must be signed in to change notification settings - Fork 7
/
Patterns.h
284 lines (234 loc) · 6.39 KB
/
Patterns.h
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* 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 <cstdint>
#include <vector>
#include <string>
#include <string_view>
#if (defined(_CPPUNWIND) || defined(__EXCEPTIONS)) && !defined(PATTERNS_SUPPRESS_EXCEPTIONS)
#define PATTERNS_ENABLE_EXCEPTIONS
#endif
namespace hook
{
struct assert_err_policy
{
static void count([[maybe_unused]] bool countMatches) { assert(countMatches); }
};
#ifdef PATTERNS_ENABLE_EXCEPTIONS
class txn_exception
{
// Deliberately empty for now
};
#define TXN_CATCH() catch (const hook::txn_exception&) {}
struct exception_err_policy
{
static void count(bool countMatches) { if (!countMatches) { throw txn_exception{}; } }
};
#else
struct exception_err_policy : public assert_err_policy
{
};
#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);
}
uintptr_t get_uintptr(ptrdiff_t offset = 0) const
{
return reinterpret_cast<uintptr_t>(get<void>(offset));
}
};
namespace details
{
ptrdiff_t get_process_base();
class basic_pattern_impl
{
protected:
std::basic_string<uint8_t> m_bytes;
std::basic_string<uint8_t> m_mask;
#if PATTERNS_USE_HINTS
uint64_t m_hash = 0;
#endif
std::vector<pattern_match> m_matches;
bool m_matched = false;
uintptr_t m_rangeStart;
uintptr_t m_rangeEnd;
protected:
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];
}
private:
explicit basic_pattern_impl(uintptr_t begin, uintptr_t end = 0)
: m_rangeStart(begin), m_rangeEnd(end)
{
}
public:
explicit basic_pattern_impl(std::string_view pattern)
: basic_pattern_impl(get_process_base())
{
Initialize(std::move(pattern));
}
inline basic_pattern_impl(void* module, std::string_view pattern)
: basic_pattern_impl(reinterpret_cast<uintptr_t>(module))
{
Initialize(std::move(pattern));
}
inline basic_pattern_impl(uintptr_t begin, uintptr_t end, std::string_view pattern)
: basic_pattern_impl(begin, end)
{
Initialize(std::move(pattern));
}
// Pretransformed patterns
inline basic_pattern_impl(std::basic_string_view<uint8_t> bytes, std::basic_string_view<uint8_t> mask)
: basic_pattern_impl(get_process_base())
{
assert( bytes.length() == mask.length() );
m_bytes = std::move(bytes);
m_mask = std::move(mask);
}
inline basic_pattern_impl(void* module, std::basic_string_view<uint8_t> bytes, std::basic_string_view<uint8_t> mask)
: basic_pattern_impl(reinterpret_cast<uintptr_t>(module))
{
assert( bytes.length() == mask.length() );
m_bytes = std::move(bytes);
m_mask = std::move(mask);
}
inline basic_pattern_impl(uintptr_t begin, uintptr_t end, std::basic_string_view<uint8_t> bytes, std::basic_string_view<uint8_t> mask)
: basic_pattern_impl(begin, end)
{
assert( bytes.length() == mask.length() );
m_bytes = std::move(bytes);
m_mask = std::move(mask);
}
protected:
#if PATTERNS_USE_HINTS && PATTERNS_CAN_SERIALIZE_HINTS
// define a hint
static void hint(uint64_t hash, uintptr_t address);
#endif
};
}
template<typename err_policy>
class basic_pattern : details::basic_pattern_impl
{
public:
using details::basic_pattern_impl::basic_pattern_impl;
inline basic_pattern&& count(uint32_t expected)
{
EnsureMatches(expected);
err_policy::count(m_matches.size() == expected);
return std::forward<basic_pattern>(*this);
}
inline basic_pattern&& count_hint(uint32_t expected)
{
EnsureMatches(expected);
return std::forward<basic_pattern>(*this);
}
inline basic_pattern&& clear()
{
m_matches.clear();
m_matched = false;
return std::forward<basic_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<basic_pattern>(*this).count(1)._get_internal(0);
}
template<typename T = void>
inline auto get_first(ptrdiff_t offset = 0)
{
return get_one().template get<T>(offset);
}
template <typename Pred>
inline Pred for_each_result(Pred pred)
{
EnsureMatches(UINT32_MAX);
for (auto match : m_matches)
{
pred(match);
}
return pred;
}
public:
#if PATTERNS_USE_HINTS && PATTERNS_CAN_SERIALIZE_HINTS
// define a hint
static void hint(uint64_t hash, uintptr_t address)
{
details::basic_pattern_impl::hint(hash, address);
}
#endif
};
using pattern = basic_pattern<assert_err_policy>;
inline auto make_module_pattern(void* module, std::string_view bytes)
{
return pattern(module, std::move(bytes));
}
inline auto 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);
}
inline auto get_pattern_uintptr(std::string_view pattern_string, ptrdiff_t offset = 0)
{
return pattern(std::move(pattern_string)).get_one().get_uintptr(offset);
}
namespace txn
{
using pattern = hook::basic_pattern<exception_err_policy>;
using hook::pattern_match;
inline auto make_module_pattern(void* module, std::string_view bytes)
{
return pattern(module, std::move(bytes));
}
inline auto 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);
}
inline auto get_pattern_uintptr(std::string_view pattern_string, ptrdiff_t offset = 0)
{
return pattern(std::move(pattern_string)).get_one().get_uintptr(offset);
}
}
}