From 788e4546debb65839484112dae05a5e13c1070c6 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 10 Sep 2023 22:31:41 -0400 Subject: Initial commit --- main.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..9d762b5 --- /dev/null +++ b/main.c @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ZXDUMP_STRIDE_LINE 16 +#define ZXDUMP_STRIDE_GROUP 2 +#define ZXDUMP_CHARSET_LEN 64 + +static uint16_t charset[ZXDUMP_CHARSET_LEN] = { + 0x0020, 0x2598, 0x259d, 0x2580, 0x2596, 0x258c, 0x259e, 0x259b, + 0x2592, '.', '.', '"', 0x00a3, '$', ':', '?', + '(', ')', '>', '<', '=', '+', '-', '*', + '/', ';', ',', '.', '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', +}; + +static void usage(int argc, char **argv, char *message, ...) { + if (message != NULL) { + va_list args; + + va_start(args, message); + vfprintf(stderr, message, args); + fputc('\n', stderr); + va_end(args); + } + + fprintf(stderr, "usage: %s file\n", argv[0]); + exit(1); +} + +static ssize_t dump_line(off_t offset, void *buf, size_t len) { + size_t i; + + if (printf("%08zx: ", offset) < 0) { + goto error_io; + } + + for (i=0; i 0 && (i % ZXDUMP_STRIDE_GROUP) == 0) { + if (putchar(' ') < 0) { + goto error_io; + } + } + + if (i < len) { + if (printf("%02x", ((uint8_t *)buf)[offset+i]) < 0) { + goto error_io; + } + } else { + if (printf(" ") < 0) { + goto error_io; + } + } + } + + if (printf(" ") < 0) { + goto error_io; + } + + for (i=0; i> 8), + 0x80 | (printable & 0x003f) + }; + + if (fwrite(sequence, 2, 1, stdout) < 1) { + goto error_io; + } + } else { + if (putchar((uint8_t)(printable & 0x00ff)) < 0) { + goto error_io; + } + } + } else { + if (putchar('.') < 0) { + goto error_io; + } + } + } + + if (putchar('\n') < 0) { + goto error_io; + } + + return fflush(stdout); + +error_io: + return -1; +} + +static ssize_t dump_fd(int fd) { + void *buf; + ssize_t offset = 0; + struct stat st; + + if (fstat(fd, &st) < 0) { + goto error_fstat; + } + + if ((buf = malloc(st.st_blksize)) == NULL) { + goto error_malloc; + } + + while (1) { + ssize_t len, i; + + if ((len = read(fd, buf, st.st_blksize)) < 0) { + goto error_read; + } else if (len == 0) { + break; + } + + for (i=0; i