jpeg-mess/hexdump.h

105 lines
2.5 KiB
C

#ifndef _HEXDUMP_H_
#define _HEXDUMP_H_
#include <stdlib.h>
#include <stdint.h>
void hexdump(void *data, size_t len);
void rgbdump(void *data, uint16_t w, uint16_t h);
void rgbadump(void *data, uint16_t w, uint16_t h);
#endif
#ifdef HEXDUMP_IMPLEMENTATION
#if !defined(HEXDUMP_COLORS0) && !defined(HEXDUMP_COLORS1)
#define HEXDUMP_N_COLORS 8
const uint8_t hexdump_colors[2][8] = {
{ 197, 215, 227, 47, 39, 57, 135, 92 },
{ 124, 214, 226, 46, 31, 55, 127, 53 },
};
#else
#define HEXDUMP_RAINBOW
#ifndef HEXDUMP_N_COLORS
#define HEXDUMP_N_COLORS 8
#endif
const uint8_t hexdump_colors[2][HEXDUMP_N_COLORS] = {
HEXDUMP_COLORS0,
HEXDUMP_COLORS1,
};
#endif
#include <stdio.h>
void hexdump(void *data, size_t len)
{
uint8_t *ptr = data;
for (size_t i = 0; i < len;)
{
#ifdef HEXDUMP_RAINBOW
printf("\033[%dm%08zx\033[0m\t", ((i >> 4) & 1 ? 90 : 37), i);
#else
printf("%08zx\t", i);
#endif
for (int x = 0; x < 16; x++)
{
#ifdef HEXDUMP_RAINBOW
printf("\033[38;5;%dm",
hexdump_colors[(i >> 4) & 1][(x + i) % HEXDUMP_N_COLORS]);
#endif
if (i + x >= len) printf("-- ");
else printf("%02x ", ptr[i + x]);
#ifdef HEXDUMP_RAINBOW
printf("\033[0m");
#endif
if (x % 4 == 3) printf("\t");
}
#ifdef HEXDUMP_PRINTABLE
for (int x = 0; x < 16; x++)
{
if (i + x >= len) break;
#ifdef HEXDUMP_RAINBOW
printf("\033[38;5;%dm",
hexdump_colors[(i >> 4) & 1][(x + i) % HEXDUMP_N_COLORS]);
#endif
if (isprint(ptr[i + x]))
putchar(ptr[i + x]);
else
putchar('.');
#ifdef HEXDUMP_RAINBOW
printf("\033[0m");
#endif
}
#endif
printf("\n");
i += 16;
}
}
void rgbdump(void *data, uint16_t w, uint16_t h)
{
uint8_t *ptr = data;
for (int y = 0; y < h; y += 2)
{
for (int x = 0; x < w; x++)
{
int i1 = x + y * w, i2 = x + (y + 1) * w;
printf("\033[38;2;%d;%d;%d;48;2;%d;%d;%dm\xe2\x96\x80",
ptr[i1 * 3 + 0], ptr[i1 * 3 + 1], ptr[i1 * 3 + 2],
ptr[i2 * 3 + 0], ptr[i2 * 3 + 1], ptr[i2 * 3 + 2]);
}
printf("\033[0m\n");
}
}
void rgbadump(void *data, uint16_t w, uint16_t h)
{
uint8_t *ptr = data;
for (int y = 0; y < h; y += 2)
{
for (int x = 0; x < w; x++)
{
int i1 = x + y * w, i2 = x + (y + 1) * w;
printf("\033[38;2;%d;%d;%d;48;2;%d;%d;%dm\xe2\x96\x80",
ptr[i1 * 4 + 0], ptr[i1 * 4 + 1], ptr[i1 * 4 + 2],
ptr[i2 * 4 + 0], ptr[i2 * 4 + 1], ptr[i2 * 4 + 2]);
}
printf("\033[0m\n");
}
}
#endif