You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hexview/src/hex.cpp

63 lines
2.4 KiB

#include <fmt/format.h>
#include <span.hpp>
const static constexpr char ascii_map[255] = {
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
};
#ifndef FIXED_ROW_SIZE
#include <alloca.h>
#endif
#ifdef FIXED_ROW_SIZE
constexpr int ROW_SZ = FIXED_ROW_SIZE;
#endif
namespace hv {
void print_screen(const span<unsigned char> memory, unsigned long offset)
{
#ifndef FIXED_ROW_SIZE
#define S (const char*)
int ROW_SZ = 24;
#else
#define S
#endif
fmt::print("0x{:016x} ", offset);
char ascii[1 + ROW_SZ];
ascii[ROW_SZ] = 0;
std::size_t i=0;
for(;i<memory.size();i++) {
if (i && i % ROW_SZ == 0) {
fmt::print(" {}", S ascii);
fmt::print("\n0x{:016x} ", i+offset);
}
fmt::print("{:02x} ", memory[i]);
ascii[i%ROW_SZ] = ascii_map[memory[i]];
}
if (memory.size() % ROW_SZ != 0)
{
auto rest = memory.size() % ROW_SZ;
ascii[rest] = 0;
for(std::size_t j=0;j< ROW_SZ - rest;j++)
fmt::print(" ");
}
fmt::print(" {}", S ascii);
}
}