12 constexpr uint32_t
INVALID = 0xFFFFFFFF;
14 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)
16 uint32_t val = minval;
18 for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
19 bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
20 if (bit_sizes_it + 1 != bit_sizes.end()) {
21 if (bitpos == endpos)
break;
28 val += (1 << *bit_sizes_it);
30 for (
int b = 0; b < *bit_sizes_it; b++) {
31 if (bitpos == endpos)
return INVALID;
34 val += bit << (*bit_sizes_it - 1 - b);
50 const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
51 Instruction DecodeType(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
53 return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
56 const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
57 uint32_t DecodeASN(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
59 return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
63 const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
64 uint32_t DecodeMatch(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
66 return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
70 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};
71 uint32_t DecodeJump(std::vector<bool>::const_iterator& bitpos,
const std::vector<bool>::const_iterator& endpos)
73 return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
78 uint32_t
Interpret(
const std::vector<bool> &asmap,
const std::vector<bool> &ip)
80 std::vector<bool>::const_iterator pos = asmap.begin();
81 const std::vector<bool>::const_iterator endpos = asmap.end();
82 uint8_t bits = ip.size();
83 uint32_t default_asn = 0;
84 uint32_t jump, match, matchlen;
86 while (pos != endpos) {
87 opcode = DecodeType(pos, endpos);
88 if (opcode == Instruction::RETURN) {
89 default_asn = DecodeASN(pos, endpos);
90 if (default_asn == INVALID)
break;
92 }
else if (opcode == Instruction::JUMP) {
93 jump = DecodeJump(pos, endpos);
94 if (jump == INVALID)
break;
96 if (pos + jump < pos)
break;
97 if (pos + jump >= endpos)
break;
98 if (ip[ip.size() - bits]) {
102 }
else if (opcode == Instruction::MATCH) {
103 match = DecodeMatch(pos, endpos);
104 if (match == INVALID)
break;
106 if (bits < matchlen)
break;
107 for (uint32_t bit = 0; bit < matchlen; bit++) {
108 if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
113 }
else if (opcode == Instruction::DEFAULT) {
114 default_asn = DecodeASN(pos, endpos);
115 if (default_asn == INVALID)
break;
126 const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end();
127 std::vector<bool>::const_iterator pos = begin;
128 std::vector<std::pair<uint32_t, int>> jumps;
131 bool had_incomplete_match =
false;
132 while (pos != endpos) {
133 uint32_t offset = pos - begin;
134 if (!jumps.empty() && offset >= jumps.back().first)
return false;
136 if (opcode == Instruction::RETURN) {
137 if (prevopcode == Instruction::DEFAULT)
return false;
138 uint32_t asn = DecodeASN(pos, endpos);
139 if (asn == INVALID)
return false;
142 if (endpos - pos > 7)
return false;
143 while (pos != endpos) {
144 if (*pos)
return false;
150 offset = pos - begin;
151 if (offset != jumps.back().first)
return false;
152 bits = jumps.back().second;
154 prevopcode = Instruction::JUMP;
156 }
else if (opcode == Instruction::JUMP) {
157 uint32_t jump = DecodeJump(pos, endpos);
158 if (jump == INVALID)
return false;
159 if (pos + jump < pos)
return false;
160 if (pos + jump > endpos)
return false;
161 if (bits == 0)
return false;
163 uint32_t jump_offset = pos - begin + jump;
164 if (!jumps.empty() && jump_offset >= jumps.back().first)
return false;
165 jumps.emplace_back(jump_offset, bits);
166 prevopcode = Instruction::JUMP;
167 }
else if (opcode == Instruction::MATCH) {
168 uint32_t match = DecodeMatch(pos, endpos);
169 if (match == INVALID)
return false;
171 if (prevopcode != Instruction::MATCH) had_incomplete_match =
false;
172 if (matchlen < 8 && had_incomplete_match)
return false;
173 had_incomplete_match = (matchlen < 8);
174 if (bits < matchlen)
return false;
176 prevopcode = Instruction::MATCH;
177 }
else if (opcode == Instruction::DEFAULT) {
178 if (prevopcode == Instruction::DEFAULT)
return false;
179 uint32_t asn = DecodeASN(pos, endpos);
180 if (asn == INVALID)
return false;
181 prevopcode = Instruction::DEFAULT;
bool SanityCheckASMap(const std::vector< bool > &asmap, int bits)
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
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...