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.2 KiB

4 years ago
#include <fmt/format.h>
#include <span.hpp>
4 years ago
const static constexpr char ascii_map[256] = {
4 years ago
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'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', '{', '|', '}', '~', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
4 years ago
'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.',
4 years ago
};
#ifndef FIXED_ROW_SIZE
#include <alloca.h>
#endif
#ifdef FIXED_ROW_SIZE
constexpr int ROW_SZ = FIXED_ROW_SIZE;
#endif
4 years ago
4 years ago
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
4 years ago
fmt::print("0x{:016x} ", offset);
4 years ago
char ascii[1 + ROW_SZ];
ascii[ROW_SZ] = 0;
4 years ago
std::size_t i=0;
for(;i<memory.size();i++) {
4 years ago
if (i && i % ROW_SZ == 0) {
fmt::print(" {}", S ascii);
4 years ago
fmt::print("\n0x{:016x} ", i+offset);
}
fmt::print("{:02x} ", memory[i]);
4 years ago
ascii[i%ROW_SZ] = ascii_map[memory[i]];
4 years ago
}
4 years ago
if (memory.size() % ROW_SZ != 0)
4 years ago
{
4 years ago
auto rest = memory.size() % ROW_SZ;
4 years ago
ascii[rest] = 0;
4 years ago
for(std::size_t j=0;j< ROW_SZ - rest;j++)
4 years ago
fmt::print(" ");
}
fmt::print(" {}", S ascii);
4 years ago
}
}