-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMode.cpp
191 lines (161 loc) · 5.07 KB
/
Mode.cpp
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
#include "Mode.hpp"
#include <stdexcept>
#include <cstdio>
#include <iostream>
Mode::Mode() : score(0) {
}
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;
}
uint8_t hexCharToByte(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return 10 + c - 'a';
} else if (c >= 'A' && c <= 'F') {
return 10 + c - 'A';
} else {
// Handle invalid hexadecimal character
// You may choose to ignore, throw an exception, or handle it differently
return 0;
}
}
Mode Mode::leading(const char charLeading) {
Mode r;
r.name = "leading";
r.kernel = "profanity_score_leading";
r.data1[0] = static_cast<cl_uchar>(hexValue('0'));
r.data1[1] = static_cast<cl_uchar>(hexValue('1'));
r.data1[2] = static_cast<cl_uchar>(hexValue('2'));
r.data1[3] = static_cast<cl_uchar>(hexValue('3'));
r.data1[4] = static_cast<cl_uchar>(hexValue('4'));
r.data1[5] = static_cast<cl_uchar>(hexValue('5'));
r.data1[6] = static_cast<cl_uchar>(hexValue('6'));
r.data1[7] = static_cast<cl_uchar>(hexValue('7'));
r.data1[8] = static_cast<cl_uchar>(hexValue('8'));
r.data1[9] = static_cast<cl_uchar>(hexValue('9'));
r.data1[10] = static_cast<cl_uchar>(hexValue('a'));
r.data1[11] = static_cast<cl_uchar>(hexValue('b'));
r.data1[12] = static_cast<cl_uchar>(hexValue('c'));
r.data1[13] = static_cast<cl_uchar>(hexValue('d'));
r.data1[14] = static_cast<cl_uchar>(hexValue('e'));
r.data1[15] = static_cast<cl_uchar>(hexValue('f'));
/*
const char* ethereumAddress = "00000000219ab540356cbb839cbe05303d7705fa"; // Example Ethereum address
//uint8_t hash[20]; // Assuming your hash array is 20 elements long and each element is 8 bits (1 byte)
// Store the Ethereum address in the hash array
for (int i = 0; i < 40; i += 2) {
// Convert two hexadecimal characters to a byte and store in the hash array
r.data2[i / 2] = (hexCharToByte(ethereumAddress[i]) << 4) | hexCharToByte(ethereumAddress[i + 1]);
}
// Output the Ethereum address stored in the hash array
std::cout << "Ethereum address stored in hash array: ";
for (int i = 0; i < 20; ++i) {
std::cout << std::hex << static_cast<int>(r.data2[i] >> 4); // Output upper 4 bits (first character)
std::cout << std::hex << static_cast<int>(r.data2[i] & 0x0F); // Output lower 4 bits (second character)
}
std::cout << std::endl;
*/
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::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;
}