#pragma once #include #include #include #include std::string hex_dump(size_t width, void const* data, size_t size); bool slurp_file(std::filesystem::path path, std::vector& data); bool dump_file(std::filesystem::path filename, void const* data, size_t size); struct reader { reader(void const* p, size_t n) : reader(reinterpret_cast(p), n) {} template reader(T const* p, size_t n) : p_(reinterpret_cast(p)), n_(n * sizeof(T)) {} template bool read(T& t) { size_t const k = sizeof(T); if (n_ < k) { return false; } memcpy(&t, p_, k); p_ += k; n_ -= k; return true; } template bool read(std::vector& v) { size_t const k = v.size() * sizeof(T); if (n_ < k) { return false; } memcpy(v.data(), p_, k); p_ += k; n_ -= k; return true; } bool read(std::string& s) { auto* beg = reinterpret_cast(p_); auto* end = reinterpret_cast(memchr(p_, 0, n_)); if (!end) { return false; } s.assign(beg, end); p_ += s.size() + 1; n_ -= s.size() + 1; return true; } uint8_t const* p_; size_t n_; };