18constexpr uint32_t
INVALID = 0xFFFFFFFF;
20uint32_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)
22 uint32_t val = minval;
24 for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
25 bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
26 if (bit_sizes_it + 1 != bit_sizes.end()) {
27 if (bitpos == endpos)
break;
34 val += (1 << *bit_sizes_it);
36 for (
int b = 0; b < *bit_sizes_it; b++) {
37 if (bitpos == endpos)
return INVALID;
40 val += bit << (*bit_sizes_it - 1 - b);
48enum class Instruction : uint32_t
56const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
57Instruction DecodeType(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
59 return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
62const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
63uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
65 return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
69const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
70uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
72 return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
76const 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};
77uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
79 return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
84uint32_t
Interpret(
const std::vector<bool> &asmap,
const std::vector<bool> &
ip)
86 std::vector<bool>::const_iterator pos = asmap.begin();
87 const std::vector<bool>::const_iterator endpos = asmap.end();
88 uint8_t bits =
ip.size();
89 uint32_t default_asn = 0;
90 uint32_t jump, match, matchlen;
92 while (pos != endpos) {
93 opcode = DecodeType(pos, endpos);
94 if (opcode == Instruction::RETURN) {
95 default_asn = DecodeASN(pos, endpos);
96 if (default_asn == INVALID)
break;
98 }
else if (opcode == Instruction::JUMP) {
99 jump = DecodeJump(pos, endpos);
100 if (jump == INVALID)
break;
101 if (bits == 0)
break;
102 if (int64_t{jump} >= int64_t{endpos - pos})
break;
103 if (
ip[
ip.size() - bits]) {
107 }
else if (opcode == Instruction::MATCH) {
108 match = DecodeMatch(pos, endpos);
109 if (match == INVALID)
break;
111 if (bits < matchlen)
break;
112 for (uint32_t bit = 0; bit < matchlen; bit++) {
113 if ((
ip[
ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
118 }
else if (opcode == Instruction::DEFAULT) {
119 default_asn = DecodeASN(pos, endpos);
120 if (default_asn == INVALID)
break;
131 const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end();
132 std::vector<bool>::const_iterator pos = begin;
133 std::vector<std::pair<uint32_t, int>> jumps;
135 Instruction prevopcode = Instruction::JUMP;
136 bool had_incomplete_match =
false;
137 while (pos != endpos) {
138 uint32_t offset = pos - begin;
139 if (!jumps.empty() && offset >= jumps.back().first)
return false;
140 Instruction opcode = DecodeType(pos, endpos);
141 if (opcode == Instruction::RETURN) {
142 if (prevopcode == Instruction::DEFAULT)
return false;
143 uint32_t asn = DecodeASN(pos, endpos);
144 if (asn == INVALID)
return false;
147 if (endpos - pos > 7)
return false;
148 while (pos != endpos) {
149 if (*pos)
return false;
155 offset = pos - begin;
156 if (offset != jumps.back().first)
return false;
157 bits = jumps.back().second;
159 prevopcode = Instruction::JUMP;
161 }
else if (opcode == Instruction::JUMP) {
162 uint32_t jump = DecodeJump(pos, endpos);
163 if (jump == INVALID)
return false;
164 if (int64_t{jump} > int64_t{endpos - pos})
return false;
165 if (bits == 0)
return false;
167 uint32_t jump_offset = pos - begin + jump;
168 if (!jumps.empty() && jump_offset >= jumps.back().first)
return false;
169 jumps.emplace_back(jump_offset, bits);
170 prevopcode = Instruction::JUMP;
171 }
else if (opcode == Instruction::MATCH) {
172 uint32_t match = DecodeMatch(pos, endpos);
173 if (match == INVALID)
return false;
175 if (prevopcode != Instruction::MATCH) had_incomplete_match =
false;
176 if (matchlen < 8 && had_incomplete_match)
return false;
177 had_incomplete_match = (matchlen < 8);
178 if (bits < matchlen)
return false;
180 prevopcode = Instruction::MATCH;
181 }
else if (opcode == Instruction::DEFAULT) {
182 if (prevopcode == Instruction::DEFAULT)
return false;
183 uint32_t asn = DecodeASN(pos, endpos);
184 if (asn == INVALID)
return false;
185 prevopcode = Instruction::DEFAULT;
195 std::vector<bool> bits;
199 LogPrintf(
"Failed to open asmap file from disk\n");
202 fseek(filestr, 0, SEEK_END);
203 int length = ftell(filestr);
205 fseek(filestr, 0, SEEK_SET);
207 for (
int i = 0; i < length; ++i) {
209 for (
int bit = 0; bit < 8; ++bit) {
210 bits.push_back((cur_byte >> bit) & 1);
Non-refcounted RAII wrapper for FILE*.
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
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 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 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)