22constexpr uint32_t
INVALID = 0xFFFFFFFF;
24uint32_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)
26 uint32_t val = minval;
28 for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
29 bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
30 if (bit_sizes_it + 1 != bit_sizes.end()) {
31 if (bitpos == endpos)
break;
38 val += (1 << *bit_sizes_it);
40 for (
int b = 0; b < *bit_sizes_it; b++) {
41 if (bitpos == endpos)
return INVALID;
44 val += bit << (*bit_sizes_it - 1 - b);
52enum class Instruction : uint32_t
60const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
61Instruction DecodeType(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
63 return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
66const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
67uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
69 return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
73const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
74uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
76 return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
80const 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};
81uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
83 return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
88uint32_t
Interpret(
const std::vector<bool> &asmap,
const std::vector<bool> &
ip)
90 std::vector<bool>::const_iterator pos = asmap.begin();
91 const std::vector<bool>::const_iterator endpos = asmap.end();
92 uint8_t bits =
ip.size();
93 uint32_t default_asn = 0;
94 uint32_t jump, match, matchlen;
96 while (pos != endpos) {
97 opcode = DecodeType(pos, endpos);
98 if (opcode == Instruction::RETURN) {
99 default_asn = DecodeASN(pos, endpos);
100 if (default_asn == INVALID)
break;
102 }
else if (opcode == Instruction::JUMP) {
103 jump = DecodeJump(pos, endpos);
104 if (jump == INVALID)
break;
105 if (bits == 0)
break;
106 if (int64_t{jump} >= int64_t{endpos - pos})
break;
107 if (
ip[
ip.size() - bits]) {
111 }
else if (opcode == Instruction::MATCH) {
112 match = DecodeMatch(pos, endpos);
113 if (match == INVALID)
break;
114 matchlen = std::bit_width(match) - 1;
115 if (bits < matchlen)
break;
116 for (uint32_t bit = 0; bit < matchlen; bit++) {
117 if ((
ip[
ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
122 }
else if (opcode == Instruction::DEFAULT) {
123 default_asn = DecodeASN(pos, endpos);
124 if (default_asn == INVALID)
break;
135 const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end();
136 std::vector<bool>::const_iterator pos = begin;
137 std::vector<std::pair<uint32_t, int>> jumps;
139 Instruction prevopcode = Instruction::JUMP;
140 bool had_incomplete_match =
false;
141 while (pos != endpos) {
142 uint32_t offset = pos - begin;
143 if (!jumps.empty() && offset >= jumps.back().first)
return false;
144 Instruction opcode = DecodeType(pos, endpos);
145 if (opcode == Instruction::RETURN) {
146 if (prevopcode == Instruction::DEFAULT)
return false;
147 uint32_t asn = DecodeASN(pos, endpos);
148 if (asn == INVALID)
return false;
151 if (endpos - pos > 7)
return false;
152 while (pos != endpos) {
153 if (*pos)
return false;
159 offset = pos - begin;
160 if (offset != jumps.back().first)
return false;
161 bits = jumps.back().second;
163 prevopcode = Instruction::JUMP;
165 }
else if (opcode == Instruction::JUMP) {
166 uint32_t jump = DecodeJump(pos, endpos);
167 if (jump == INVALID)
return false;
168 if (int64_t{jump} > int64_t{endpos - pos})
return false;
169 if (bits == 0)
return false;
171 uint32_t jump_offset = pos - begin + jump;
172 if (!jumps.empty() && jump_offset >= jumps.back().first)
return false;
173 jumps.emplace_back(jump_offset, bits);
174 prevopcode = Instruction::JUMP;
175 }
else if (opcode == Instruction::MATCH) {
176 uint32_t match = DecodeMatch(pos, endpos);
177 if (match == INVALID)
return false;
178 int matchlen = std::bit_width(match) - 1;
179 if (prevopcode != Instruction::MATCH) had_incomplete_match =
false;
180 if (matchlen < 8 && had_incomplete_match)
return false;
181 had_incomplete_match = (matchlen < 8);
182 if (bits < matchlen)
return false;
184 prevopcode = Instruction::MATCH;
185 }
else if (opcode == Instruction::DEFAULT) {
186 if (prevopcode == Instruction::DEFAULT)
return false;
187 uint32_t asn = DecodeASN(pos, endpos);
188 if (asn == INVALID)
return false;
189 prevopcode = Instruction::DEFAULT;
199 std::vector<bool> bits;
203 LogPrintf(
"Failed to open asmap file from disk\n");
206 file.seek(0, SEEK_END);
207 int length = file.tell();
209 file.seek(0, SEEK_SET);
211 for (
int i = 0; i < length; ++i) {
213 for (
int bit = 0; bit < 8; ++bit) {
214 bits.push_back((cur_byte >> bit) & 1);
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 CService ip(uint32_t i)
@ INVALID
Failed decoding.
static auto quoted(const std::string &s)
static std::string PathToString(const path &path)
Convert path object to a byte string.
FILE * fopen(const fs::path &p, const char *mode)
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)