20constexpr uint32_t
INVALID = 0xFFFFFFFF;
22uint32_t DecodeBits(std::vector<bool>::const_iterator &bitpos,
23 const std::vector<bool>::const_iterator &endpos,
24 uint8_t minval,
const std::vector<uint8_t> &bit_sizes) {
25 uint32_t val = minval;
27 for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
28 bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
29 if (bit_sizes_it + 1 != bit_sizes.end()) {
30 if (bitpos == endpos) {
39 val += (1 << *bit_sizes_it);
41 for (
int b = 0; b < *bit_sizes_it; b++) {
42 if (bitpos == endpos) {
48 val += bit << (*bit_sizes_it - 1 - b);
57enum class Instruction : uint32_t {
64const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
65Instruction DecodeType(std::vector<bool>::const_iterator &bitpos,
66 const std::vector<bool>::const_iterator &endpos) {
67 return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
70const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19,
72uint32_t DecodeASN(std::vector<bool>::const_iterator &bitpos,
73 const std::vector<bool>::const_iterator &endpos) {
74 return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
77const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
78uint32_t DecodeMatch(std::vector<bool>::const_iterator &bitpos,
79 const std::vector<bool>::const_iterator &endpos) {
80 return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
83const std::vector<uint8_t> JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13,
84 14, 15, 16, 17, 18, 19, 20, 21, 22,
85 23, 24, 25, 26, 27, 28, 29, 30};
86uint32_t DecodeJump(std::vector<bool>::const_iterator &bitpos,
87 const std::vector<bool>::const_iterator &endpos) {
88 return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
94 const std::vector<bool> &ip) {
95 std::vector<bool>::const_iterator pos = asmap.begin();
96 const std::vector<bool>::const_iterator endpos = asmap.end();
97 uint8_t bits = ip.size();
98 uint32_t default_asn = 0;
99 uint32_t jump, match, matchlen;
101 while (pos != endpos) {
102 opcode = DecodeType(pos, endpos);
103 if (opcode == Instruction::RETURN) {
104 default_asn = DecodeASN(pos, endpos);
105 if (default_asn == INVALID) {
110 }
else if (opcode == Instruction::JUMP) {
111 jump = DecodeJump(pos, endpos);
112 if (jump == INVALID) {
120 if (pos + jump < pos) {
124 if (pos + jump >= endpos) {
128 if (ip[ip.size() - bits]) {
132 }
else if (opcode == Instruction::MATCH) {
133 match = DecodeMatch(pos, endpos);
134 if (match == INVALID) {
138 matchlen = std::bit_width(match) - 1;
139 if (bits < matchlen) {
143 for (uint32_t bit = 0; bit < matchlen; bit++) {
144 if ((ip[ip.size() - bits]) !=
145 ((match >> (matchlen - 1 - bit)) & 1)) {
150 }
else if (opcode == Instruction::DEFAULT) {
151 default_asn = DecodeASN(pos, endpos);
152 if (default_asn == INVALID) {
171 const std::vector<bool>::const_iterator begin = asmap.begin(),
172 endpos = asmap.end();
173 std::vector<bool>::const_iterator pos = begin;
176 std::vector<std::pair<uint32_t, int>> jumps;
178 Instruction prevopcode = Instruction::JUMP;
179 bool had_incomplete_match =
false;
180 while (pos != endpos) {
181 uint32_t offset = pos - begin;
182 if (!jumps.empty() && offset >= jumps.back().first) {
186 Instruction opcode = DecodeType(pos, endpos);
187 if (opcode == Instruction::RETURN) {
188 if (prevopcode == Instruction::DEFAULT) {
193 uint32_t asn = DecodeASN(pos, endpos);
194 if (asn == INVALID) {
200 if (endpos - pos > 7) {
204 while (pos != endpos) {
215 offset = pos - begin;
216 if (offset != jumps.back().first) {
222 bits = jumps.back().second;
224 prevopcode = Instruction::JUMP;
226 }
else if (opcode == Instruction::JUMP) {
227 uint32_t jump = DecodeJump(pos, endpos);
228 if (jump == INVALID) {
232 if (pos + jump < pos) {
236 if (pos + jump > endpos) {
245 uint32_t jump_offset = pos - begin + jump;
246 if (!jumps.empty() && jump_offset >= jumps.back().first) {
250 jumps.emplace_back(jump_offset, bits);
251 prevopcode = Instruction::JUMP;
252 }
else if (opcode == Instruction::MATCH) {
253 uint32_t match = DecodeMatch(pos, endpos);
254 if (match == INVALID) {
258 int matchlen = std::bit_width(match) - 1;
259 if (prevopcode != Instruction::MATCH) {
260 had_incomplete_match =
false;
262 if (matchlen < 8 && had_incomplete_match) {
267 had_incomplete_match = (matchlen < 8);
268 if (bits < matchlen) {
273 prevopcode = Instruction::MATCH;
274 }
else if (opcode == Instruction::DEFAULT) {
275 if (prevopcode == Instruction::DEFAULT) {
280 uint32_t asn = DecodeASN(pos, endpos);
281 if (asn == INVALID) {
285 prevopcode = Instruction::DEFAULT;
296 std::vector<bool> bits;
300 LogPrintf(
"Failed to open asmap file from disk\n");
303 fseek(filestr, 0, SEEK_END);
304 int length = ftell(filestr);
305 LogPrintf(
"Opened asmap file %s (%d bytes) from disk\n",
307 fseek(filestr, 0, SEEK_SET);
309 for (
int i = 0; i < length; ++i) {
311 for (
int bit = 0; bit < 8; ++bit) {
312 bits.push_back((cur_byte >> bit) & 1);
316 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 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)