-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRuntimePattern.hpp
77 lines (74 loc) · 2.29 KB
/
RuntimePattern.hpp
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
#pragma once
#include "Pattern.hpp"
#include <cstddef>
#include <vector>
#include <stdexcept>
namespace patterns {
class RuntimePattern : public Pattern {
std::vector<uint8_t> m_pattern;
std::vector<uint8_t> m_mask;
public:
RuntimePattern(const char* p, size_t len) {
int n = 0;
for (int i = 0; i < len; i += 2) {
auto ptr = &p[i];
if (*ptr == '?') {
m_pattern.push_back(0);
m_mask.push_back(0);
++n;
}
// Capture where we have our offset marker 'X' at
else if (*ptr == 'X' || *ptr == 'x') {
offset_ = n;
if (p[i + 1] != ' ') {
#ifndef __arm64__
insn_len_ = get_inst_len_opt(&p[++i]);
#endif
while (p[i + 1] != ' ')
++i;
}
}
// Break from parsing the pattern, since / at the end starts the flags
else if (*ptr == '/') {
++ptr;
handle_options(ptr);
break;
}
else if (*ptr != ' ') {
m_pattern.push_back(value(ptr));
m_mask.push_back(0xFF);
++n;
}
else i--;
}
#ifdef __arm64__
while (align_ && n % align_size_) {
m_pattern.push_back(0);
m_mask.push_back(0);
++n;
}
#else
while (n % sizeof(void*)) {
m_pattern.push_back(0);
m_mask.push_back(0);
++n;
}
#endif
length_ = n;
}
RuntimePattern(const char* p) :
RuntimePattern(p, strlen(p))
{
}
~RuntimePattern() = default;
virtual const uint8_t* pattern() const override {
return m_pattern.data();
}
virtual const uint8_t* mask() const override {
return m_mask.data();
}
};
}
auto operator"" _rtpattern(const char* str, size_t size) {
return patterns::RuntimePattern(str, size);
}