56 const bool bit = (std::to_integer<uint8_t>(bytes[
bitpos / 8]) >> (
bitpos % 8)) & 1;
67 const bool bit = (std::to_integer<uint8_t>(bytes[
bitpos / 8]) >> (7 - (
bitpos % 8))) & 1;
143Instruction
DecodeType(
size_t&
bitpos,
const std::span<const std::byte> data)
167constexpr 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};
191 if (
opcode == Instruction::RETURN) {
196 }
else if (
opcode == Instruction::JUMP) {
206 }
else if (
opcode == Instruction::MATCH) {
215 for (
int bit = 0; bit <
matchlen; bit++) {
221 }
else if (
opcode == Instruction::DEFAULT) {
243 std::vector<std::pair<uint32_t, int>>
jumps;
250 if (!
jumps.empty() && pos >=
jumps.back().first)
return false;
253 if (
opcode == Instruction::RETURN) {
255 if (
prevopcode == Instruction::DEFAULT)
return false;
260 if (
endpos - pos > 7)
return false;
267 if (pos !=
jumps.back().first)
return false;
268 bits =
jumps.back().second;
272 }
else if (
opcode == Instruction::JUMP) {
276 if (bits == 0)
return false;
282 }
else if (
opcode == Instruction::MATCH) {
293 }
else if (
opcode == Instruction::DEFAULT) {
295 if (
prevopcode == Instruction::DEFAULT)
return false;
313 LogWarning(
"Sanity check of asmap data failed\n");
327 LogWarning(
"Failed to open asmap file from disk");
334 std::vector<std::byte> buffer(length);
350 if (data.empty())
return {};
Non-refcounted RAII wrapper for FILE*.
A writer stream (for serialization) that computes a 256-bit hash.
static CService ip(uint32_t i)
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)
bool SanityCheckAsmap(const std::span< const std::byte > asmap, int bits)
Validates ASMap structure by simulating all possible execution paths.
bool CheckStandardAsmap(const std::span< const std::byte > data)
Provides a safe interface for validating ASMap data before use.
std::vector< std::byte > DecodeAsmap(fs::path path)
Loads an ASMap file from disk and validates it.
uint32_t Interpret(const std::span< const std::byte > asmap, const std::span< const std::byte > ip)
Execute the ASMap bytecode to find the ASN for an IP.
uint256 AsmapVersion(const std::span< const std::byte > data)
Computes SHA256 hash of ASMap data for versioning and consistency checks.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.