commit 37dbb85cda6f5a92b89c22bb410943aa011a5852
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 5 Aug 2022 18:09:15 +0200
Simple hex manipulation tools
Diffstat:
A | .gitignore | | | 3 | +++ |
A | Makefile | | | 12 | ++++++++++++ |
A | hex.c | | | 44 | ++++++++++++++++++++++++++++++++++++++++++++ |
A | hexv.c | | | 113 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | unhex.c | | | 63 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 235 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,3 @@
+hex
+unhex
+hexv
diff --git a/Makefile b/Makefile
@@ -0,0 +1,12 @@
+.PHONY: all clean
+
+all: hex hexv unhex
+
+clean:
+ rm -f hex hexv unhex
+
+hex: hex.c
+
+hexv: hexv.c
+
+unhex: unhex.c
diff --git a/hex.c b/hex.c
@@ -0,0 +1,44 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+const char *hex = "0123456789abcdef";
+
+void
+tohex(int fd)
+{
+ char buf[4096];
+ ssize_t i, nread;
+
+ while (1) {
+ nread = read(fd, buf, sizeof(buf));
+ if (nread <= 0) return;
+
+ for (i = 0; i < nread; i++) {
+ putchar(hex[(buf[i] >> 4) & 0xf]);
+ putchar(hex[(buf[i] >> 0) & 0xf]);
+ }
+ }
+}
+
+int
+main(int argc, const char **argv)
+{
+ int i, fd;
+
+ if (argc > 1) {
+ for (i = 1; i < argc; i++) {
+ fd = open(argv[i], O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "Could not open file: %s\n",
+ argv[i]);
+ continue;
+ }
+ tohex(fd);
+ close(fd);
+ }
+ } else {
+ tohex(0);
+ }
+}
diff --git a/hexv.c b/hexv.c
@@ -0,0 +1,113 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+static const char hex[] = "0123456789abcdef";
+
+static int color = 0;
+static int gradient[256] = {
+ [0] = 241,
+ [1 ... 31] = 242,
+ [32 ... 63] = 244,
+ [64 ... 95] = 246,
+ [96 ... 127] = 247,
+ [128 ... 159] = 248,
+ [160 ... 191] = 250,
+ [192 ... 223] = 252,
+ [224 ... 253] = 254,
+ [255] = 255,
+};
+
+void
+colorhex(uint8_t c)
+{
+ if (gradient[c] != color) {
+ color = gradient[c];
+ printf("\x1b[38;5;%im", color);
+ }
+ putchar(hex[(c >> 4) & 0xf]);
+ putchar(hex[(c >> 0) & 0xf]);
+}
+
+void
+printrow(char *row, size_t pos, size_t len)
+{
+ size_t i;
+
+ printf("%08lx: ", pos);
+ for (i = 0; i < 16; i++) {
+ if (i < len) {
+ colorhex(row[i]);
+ } else {
+ printf(" ");
+ }
+ if ((i + 1) % 2 == 0)
+ putchar(' ');
+ }
+ if (color != 0)
+ printf("\x1b[0m");
+ color = 0;
+
+ printf(" ");
+
+ for (i = 0; i < 16; i++) {
+ if (i < len) {
+ if (row[i] >= 0x20 && row[i] <= 126) {
+ putchar(row[i]);
+ } else {
+ putchar('.');
+ }
+ } else {
+ putchar(' ');
+ }
+ }
+ printf("\n");
+}
+
+void
+tohex(int fd)
+{
+ size_t pos, i, k;
+ ssize_t nread;
+ char buf[4096];
+ char row[16];
+
+ pos = 0;
+ while (1) {
+ nread = read(fd, buf, sizeof(buf));
+ if (nread <= 0) break;
+
+ for (i = 0; i < nread; i++, pos++) {
+ row[pos % 16] = buf[i];
+ if ((pos + 1) % 16 == 0)
+ printrow(row, pos & ~15UL, 16);
+ }
+ }
+
+ if (pos % 16 != 0)
+ printrow(row, pos & ~15UL, pos % 16);
+}
+
+int
+main(int argc, const char **argv)
+{
+ int i, fd;
+
+ if (argc > 1) {
+ for (i = 1; i < argc; i++) {
+ if (i > 1) printf("\n");
+ fd = open(argv[i], O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "Could not open file: %s\n",
+ argv[i]);
+ continue;
+ }
+ tohex(fd);
+ close(fd);
+ }
+ } else {
+ tohex(0);
+ }
+}
diff --git a/unhex.c b/unhex.c
@@ -0,0 +1,63 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+const char *hex = "0123456789abcdef";
+
+char
+hex2nib(char c)
+{
+ if (c >= '0' && c <= '9')
+ return c - '0';
+ else if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ else if (c >= 'A' && c <= 'F')
+ return c - 'A' + 10;
+
+ fprintf(stderr, "Invalid hex char\n");
+ exit(1);
+}
+
+void
+fromhex(int fd)
+{
+ ssize_t pos, i, nread;
+ char buf[4096], nib;
+
+ pos = 0;
+ while (1) {
+ nread = read(fd, buf, sizeof(buf));
+ if (nread <= 0) return;
+
+ for (i = 0; i < nread; i++, pos++) {
+ if (buf[i] == '\n') continue;
+ if (pos % 2 == 0) {
+ nib = hex2nib(buf[i]);
+ } else {
+ putchar((nib << 4) | hex2nib(buf[i]));
+ }
+ }
+ }
+}
+
+int
+main(int argc, const char **argv)
+{
+ int i, fd;
+
+ if (argc > 1) {
+ for (i = 1; i < argc; i++) {
+ fd = open(argv[i], O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "Could not open file: %s\n",
+ argv[i]);
+ continue;
+ }
+ fromhex(fd);
+ close(fd);
+ }
+ } else {
+ fromhex(0);
+ }
+}