19constexpr uint32_t
INVALID = 0xFFFFFFFF;
21uint32_t DecodeBits(std::vector<bool>::const_iterator &bitpos,
22 const std::vector<bool>::const_iterator &endpos,
23 uint8_t minval,
const std::vector<uint8_t> &bit_sizes) {
24 uint32_t val = minval;
26 for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
27 bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
28 if (bit_sizes_it + 1 != bit_sizes.end()) {
29 if (bitpos == endpos) {
38 val += (1 << *bit_sizes_it);
40 for (
int b = 0; b < *bit_sizes_it; b++) {
41 if (bitpos == endpos) {
47 val += bit << (*bit_sizes_it - 1 - b);
56enum class Instruction : uint32_t {
63const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
64Instruction DecodeType(std::vector<bool>::const_iterator &bitpos,
65 const std::vector<bool>::const_iterator &endpos) {
66 return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
69const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19,
71uint32_t DecodeASN(std::vector<bool>::const_iterator &bitpos,
72 const std::vector<bool>::const_iterator &endpos) {
73 return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
76const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
77uint32_t DecodeMatch(std::vector<bool>::const_iterator &bitpos,
78 const std::vector<bool>::const_iterator &endpos) {
79 return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
82const std::vector<uint8_t> JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13,
83 14, 15, 16, 17, 18, 19, 20, 21, 22,
84 23, 24, 25, 26, 27, 28, 29, 30};
85uint32_t DecodeJump(std::vector<bool>::const_iterator &bitpos,
86 const std::vector<bool>::const_iterator &endpos) {
87 return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
93 const std::vector<bool> &ip) {
94 std::vector<bool>::const_iterator pos = asmap.begin();
95 const std::vector<bool>::const_iterator endpos = asmap.end();
96 uint8_t bits = ip.size();
97 uint32_t default_asn = 0;
98 uint32_t jump, match, matchlen;
100 while (pos != endpos) {
101 opcode = DecodeType(pos, endpos);
102 if (opcode == Instruction::RETURN) {
103 default_asn = DecodeASN(pos, endpos);
104 if (default_asn == INVALID) {
109 }
else if (opcode == Instruction::JUMP) {
110 jump = DecodeJump(pos, endpos);
111 if (jump == INVALID) {
119 if (pos + jump < pos) {
123 if (pos + jump >= endpos) {
127 if (ip[ip.size() - bits]) {
131 }
else if (opcode == Instruction::MATCH) {
132 match = DecodeMatch(pos, endpos);
133 if (match == INVALID) {
138 if (bits < matchlen) {
142 for (uint32_t bit = 0; bit < matchlen; bit++) {
143 if ((ip[ip.size() - bits]) !=
144 ((match >> (matchlen - 1 - bit)) & 1)) {
149 }
else if (opcode == Instruction::DEFAULT) {
150 default_asn = DecodeASN(pos, endpos);
151 if (default_asn == INVALID) {
170 const std::vector<bool>::const_iterator begin = asmap.begin(),
171 endpos = asmap.end();
172 std::vector<bool>::const_iterator pos = begin;
175 std::vector<std::pair<uint32_t, int>> jumps;
177 Instruction prevopcode = Instruction::JUMP;
178 bool had_incomplete_match =
false;
179 while (pos != endpos) {
180 uint32_t offset = pos - begin;
181 if (!jumps.empty() && offset >= jumps.back().first) {
185 Instruction opcode = DecodeType(pos, endpos);
186 if (opcode == Instruction::RETURN) {
187 if (prevopcode == Instruction::DEFAULT) {
192 uint32_t asn = DecodeASN(pos, endpos);
193 if (asn == INVALID) {
199 if (endpos - pos > 7) {
203 while (pos != endpos) {
214 offset = pos - begin;
215 if (offset != jumps.back().first) {
221 bits = jumps.back().second;
223 prevopcode = Instruction::JUMP;
225 }
else if (opcode == Instruction::JUMP) {
226 uint32_t jump = DecodeJump(pos, endpos);
227 if (jump == INVALID) {
231 if (pos + jump < pos) {
235 if (pos + jump > endpos) {
244 uint32_t jump_offset = pos - begin + jump;
245 if (!jumps.empty() && jump_offset >= jumps.back().first) {
249 jumps.emplace_back(jump_offset, bits);
250 prevopcode = Instruction::JUMP;
251 }
else if (opcode == Instruction::MATCH) {
252 uint32_t match = DecodeMatch(pos, endpos);
253 if (match == INVALID) {
258 if (prevopcode != Instruction::MATCH) {
259 had_incomplete_match =
false;
261 if (matchlen < 8 && had_incomplete_match) {
266 had_incomplete_match = (matchlen < 8);
267 if (bits < matchlen) {
272 prevopcode = Instruction::MATCH;
273 }
else if (opcode == Instruction::DEFAULT) {
274 if (prevopcode == Instruction::DEFAULT) {
279 uint32_t asn = DecodeASN(pos, endpos);
280 if (asn == INVALID) {
284 prevopcode = Instruction::DEFAULT;
295 std::vector<bool> bits;
299 LogPrintf(
"Failed to open asmap file from disk\n");
302 fseek(filestr, 0, SEEK_END);
303 int length = ftell(filestr);
304 LogPrintf(
"Opened asmap file %s (%d bytes) from disk\n",
306 fseek(filestr, 0, SEEK_SET);
308 for (
int i = 0; i < length; ++i) {
310 for (
int bit = 0; bit < 8; ++bit) {
311 bits.push_back((cur_byte >> bit) & 1);
315 LogPrintf(
"Sanity check of asmap file %s failed\n",
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
std::vector< bool > DecodeAsmap(fs::path path)
Read asmap from provided binary file.
bool SanityCheckASMap(const std::vector< bool > &asmap, int bits)
Non-refcounted RAII wrapper for FILE*.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static uint64_t CountBits(uint64_t x)
Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set.
static auto quoted(const std::string &s)
static std::string PathToString(const path &path)
Convert path object to byte string.
FILE * fopen(const fs::path &p, const char *mode)