19 constexpr uint32_t
INVALID = 0xFFFFFFFF;
21 uint32_t DecodeBits(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos, uint8_t minval,
const std::vector<uint8_t> &bit_sizes)
23 uint32_t val = minval;
25 for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
26 bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
27 if (bit_sizes_it + 1 != bit_sizes.end()) {
28 if (bitpos == endpos)
break;
35 val += (1 << *bit_sizes_it);
37 for (
int b = 0; b < *bit_sizes_it; b++) {
38 if (bitpos == endpos)
return INVALID;
41 val += bit << (*bit_sizes_it - 1 - b);
49 enum class Instruction : uint32_t
57 const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
58 Instruction DecodeType(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
60 return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
63 const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
64 uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
66 return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
70 const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
71 uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
73 return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
77 const std::vector<uint8_t> JUMP_BIT_SIZES{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};
78 uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
80 return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
85 uint32_t
Interpret(
const std::vector<bool> &asmap,
const std::vector<bool> &
ip)
87 std::vector<bool>::const_iterator pos = asmap.begin();
88 const std::vector<bool>::const_iterator endpos = asmap.end();
89 uint8_t bits =
ip.size();
90 uint32_t default_asn = 0;
91 uint32_t jump, match, matchlen;
93 while (pos != endpos) {
94 opcode = DecodeType(pos, endpos);
95 if (opcode == Instruction::RETURN) {
96 default_asn = DecodeASN(pos, endpos);
97 if (default_asn == INVALID)
break;
99 }
else if (opcode == Instruction::JUMP) {
100 jump = DecodeJump(pos, endpos);
101 if (jump == INVALID)
break;
102 if (bits == 0)
break;
103 if (int64_t{jump} >= int64_t{endpos - pos})
break;
104 if (
ip[
ip.size() - bits]) {
108 }
else if (opcode == Instruction::MATCH) {
109 match = DecodeMatch(pos, endpos);
110 if (match == INVALID)
break;
112 if (bits < matchlen)
break;
113 for (uint32_t bit = 0; bit < matchlen; bit++) {
114 if ((
ip[
ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
119 }
else if (opcode == Instruction::DEFAULT) {
120 default_asn = DecodeASN(pos, endpos);
121 if (default_asn == INVALID)
break;
132 const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end();
133 std::vector<bool>::const_iterator pos = begin;
134 std::vector<std::pair<uint32_t, int>> jumps;
136 Instruction prevopcode = Instruction::JUMP;
137 bool had_incomplete_match =
false;
138 while (pos != endpos) {
139 uint32_t offset = pos - begin;
140 if (!jumps.empty() && offset >= jumps.back().first)
return false;
141 Instruction opcode = DecodeType(pos, endpos);
142 if (opcode == Instruction::RETURN) {
143 if (prevopcode == Instruction::DEFAULT)
return false;
144 uint32_t asn = DecodeASN(pos, endpos);
145 if (asn == INVALID)
return false;
148 if (endpos - pos > 7)
return false;
149 while (pos != endpos) {
150 if (*pos)
return false;
156 offset = pos - begin;
157 if (offset != jumps.back().first)
return false;
158 bits = jumps.back().second;
160 prevopcode = Instruction::JUMP;
162 }
else if (opcode == Instruction::JUMP) {
163 uint32_t jump = DecodeJump(pos, endpos);
164 if (jump == INVALID)
return false;
165 if (int64_t{jump} > int64_t{endpos - pos})
return false;
166 if (bits == 0)
return false;
168 uint32_t jump_offset = pos - begin + jump;
169 if (!jumps.empty() && jump_offset >= jumps.back().first)
return false;
170 jumps.emplace_back(jump_offset, bits);
171 prevopcode = Instruction::JUMP;
172 }
else if (opcode == Instruction::MATCH) {
173 uint32_t match = DecodeMatch(pos, endpos);
174 if (match == INVALID)
return false;
176 if (prevopcode != Instruction::MATCH) had_incomplete_match =
false;
177 if (matchlen < 8 && had_incomplete_match)
return false;
178 had_incomplete_match = (matchlen < 8);
179 if (bits < matchlen)
return false;
181 prevopcode = Instruction::MATCH;
182 }
else if (opcode == Instruction::DEFAULT) {
183 if (prevopcode == Instruction::DEFAULT)
return false;
184 uint32_t asn = DecodeASN(pos, endpos);
185 if (asn == INVALID)
return false;
186 prevopcode = Instruction::DEFAULT;
196 std::vector<bool> bits;
200 LogPrintf(
"Failed to open asmap file from disk\n");
203 fseek(filestr, 0, SEEK_END);
204 int length = ftell(filestr);
206 fseek(filestr, 0, SEEK_SET);
208 for (
int i = 0; i < length; ++i) {
210 for (
int bit = 0; bit < 8; ++bit) {
211 bits.push_back((cur_byte >> bit) & 1);