forked from 1inch/profanity2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMode.cpp
More file actions
168 lines (136 loc) · 3.6 KB
/
Mode.cpp
File metadata and controls
168 lines (136 loc) · 3.6 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
#include "Mode.hpp"
#include <stdexcept>
Mode::Mode() : score(0), isMatchAll(false) {
}
Mode Mode::benchmark() {
Mode r;
r.name = "benchmark";
r.kernel = "profanity_score_benchmark";
return r;
}
Mode Mode::zeros() {
Mode r = range(0, 0);
r.name = "zeros";
return r;
}
static std::string::size_type hexValueNoException(char c) {
if (c >= 'A' && c <= 'F') {
c -= 'A' - 'a';
}
const std::string hex = "0123456789abcdef";
const std::string::size_type ret = hex.find(c);
return ret;
}
static std::string::size_type hexValue(char c) {
const std::string::size_type ret = hexValueNoException(c);
if(ret == std::string::npos) {
throw std::runtime_error("bad hex value");
}
return ret;
}
Mode Mode::matching(const std::string strHex) {
Mode r;
r.name = "matching";
r.kernel = "profanity_score_matching";
std::fill( r.data1, r.data1 + sizeof(r.data1), cl_uchar(0) );
std::fill( r.data2, r.data2 + sizeof(r.data2), cl_uchar(0) );
auto index = 0;
for( size_t i = 0; i < strHex.size(); i += 2 ) {
const auto indexHi = hexValueNoException(strHex[i]);
const auto indexLo = i + 1 < strHex.size() ? hexValueNoException(strHex[i+1]) : std::string::npos;
const auto valHi = (indexHi == std::string::npos) ? 0 : indexHi << 4;
const auto valLo = (indexLo == std::string::npos) ? 0 : indexLo;
const auto maskHi = (indexHi == std::string::npos) ? 0 : 0xF << 4;
const auto maskLo = (indexLo == std::string::npos) ? 0 : 0xF;
r.data1[index] = maskHi | maskLo;
r.data2[index] = valHi | valLo;
++index;
}
return r;
}
Mode Mode::matchAll(const std::string strHex) {
Mode r = matching(strHex);
int constrainedNibbles = 0;
for (unsigned char i : r.data1) {
if (i & 0xF0) ++constrainedNibbles;
if (i & 0x0F) ++constrainedNibbles;
}
if (constrainedNibbles < 6) {
throw std::runtime_error("--match-all pattern too permissive: constrain at least 6 nibbles to prevent ring buffer overflow. For shorter patterns use --matching instead.");
}
r.name = "match-all";
r.kernel = "profanity_match_all";
r.isMatchAll = true;
return r;
}
Mode Mode::leading(const char charLeading) {
Mode r;
r.name = "leading";
r.kernel = "profanity_score_leading";
r.data1[0] = static_cast<cl_uchar>(hexValue(charLeading));
return r;
}
Mode Mode::range(const cl_uchar min, const cl_uchar max) {
Mode r;
r.name = "range";
r.kernel = "profanity_score_range";
r.data1[0] = min;
r.data2[0] = max;
return r;
}
Mode Mode::zeroBytes() {
Mode r;
r.name = "zeroBytes";
r.kernel = "profanity_score_zerobytes";
return r;
}
Mode Mode::letters() {
Mode r = range(10, 15);
r.name = "letters";
return r;
}
Mode Mode::numbers() {
Mode r = range(0, 9);
r.name = "numbers";
return r;
}
std::string Mode::transformKernel() const {
switch (this->target) {
case ADDRESS:
return "";
case CONTRACT:
return "profanity_transform_contract";
default:
throw "No kernel for target";
}
}
std::string Mode::transformName() const {
switch (this->target) {
case ADDRESS:
return "Address";
case CONTRACT:
return "Contract";
default:
throw "No name for target";
}
}
Mode Mode::leadingRange(const cl_uchar min, const cl_uchar max) {
Mode r;
r.name = "leadingrange";
r.kernel = "profanity_score_leadingrange";
r.data1[0] = min;
r.data2[0] = max;
return r;
}
Mode Mode::mirror() {
Mode r;
r.name = "mirror";
r.kernel = "profanity_score_mirror";
return r;
}
Mode Mode::doubles() {
Mode r;
r.name = "doubles";
r.kernel = "profanity_score_doubles";
return r;
}