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);
42 enum class Instruction : uint32_t
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 (int64_t{jump} >= int64_t{endpos - pos})
break;
97 if (ip[ip.size() - bits]) {
101 }
else if (opcode == Instruction::MATCH) {
102 match = DecodeMatch(pos, endpos);
103 if (match == INVALID)
break;
105 if (bits < matchlen)
break;
106 for (uint32_t bit = 0; bit < matchlen; bit++) {
107 if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
112 }
else if (opcode == Instruction::DEFAULT) {
113 default_asn = DecodeASN(pos, endpos);
114 if (default_asn == INVALID)
break;
125 const std::vector<bool>::const_iterator begin = asmap.begin(), endpos = asmap.end();
126 std::vector<bool>::const_iterator pos = begin;
127 std::vector<std::pair<uint32_t, int>> jumps;
129 Instruction prevopcode = Instruction::JUMP;
130 bool had_incomplete_match =
false;
131 while (pos != endpos) {
132 uint32_t offset = pos - begin;
133 if (!jumps.empty() && offset >= jumps.back().first)
return false;
134 Instruction opcode = DecodeType(pos, endpos);
135 if (opcode == Instruction::RETURN) {
136 if (prevopcode == Instruction::DEFAULT)
return false;
137 uint32_t asn = DecodeASN(pos, endpos);
138 if (asn == INVALID)
return false;
141 if (endpos - pos > 7)
return false;
142 while (pos != endpos) {
143 if (*pos)
return false;
149 offset = pos - begin;
150 if (offset != jumps.back().first)
return false;
151 bits = jumps.back().second;
153 prevopcode = Instruction::JUMP;
155 }
else if (opcode == Instruction::JUMP) {
156 uint32_t jump = DecodeJump(pos, endpos);
157 if (jump == INVALID)
return false;
158 if (int64_t{jump} > int64_t{endpos - pos})
return false;
159 if (bits == 0)
return false;
161 uint32_t jump_offset = pos - begin + jump;
162 if (!jumps.empty() && jump_offset >= jumps.back().first)
return false;
163 jumps.emplace_back(jump_offset, bits);
164 prevopcode = Instruction::JUMP;
165 }
else if (opcode == Instruction::MATCH) {
166 uint32_t match = DecodeMatch(pos, endpos);
167 if (match == INVALID)
return false;
169 if (prevopcode != Instruction::MATCH) had_incomplete_match =
false;
170 if (matchlen < 8 && had_incomplete_match)
return false;
171 had_incomplete_match = (matchlen < 8);
172 if (bits < matchlen)
return false;
174 prevopcode = Instruction::MATCH;
175 }
else if (opcode == Instruction::DEFAULT) {
176 if (prevopcode == Instruction::DEFAULT)
return false;
177 uint32_t asn = DecodeASN(pos, endpos);
178 if (asn == INVALID)
return false;
179 prevopcode = Instruction::DEFAULT;