From d5fb5de3863884453c0d126a3eaf93e8716ff72b Mon Sep 17 00:00:00 2001 From: g1n Date: Sun, 22 Aug 2021 18:27:46 +0300 Subject: [PATCH] Initial commit: add very simple hexdump --- .gitignore | 1 + README.org | 3 +++ src/Makefile | 11 +++++++++++ src/hexdump.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/test.bin | Bin 0 -> 32 bytes 5 files changed, 64 insertions(+) create mode 100644 .gitignore create mode 100644 README.org create mode 100644 src/Makefile create mode 100644 src/hexdump.c create mode 100644 tests/test.bin diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/README.org b/README.org new file mode 100644 index 0000000..86ce879 --- /dev/null +++ b/README.org @@ -0,0 +1,3 @@ +#+TITLE: hexutils - hexdump, xxd + +Simple tools to work with binary files and display them in ascii, hex or binary and make hexdumps diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..bf8a082 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,11 @@ +CC = gcc +CFLAGS= -Wall -Wextra +LFLAGS= + +all: hexdump + +main: $(OBJFILES) + $(CC) hexdump.c -o hexdump + +test: main + ./hexdump diff --git a/src/hexdump.c b/src/hexdump.c new file mode 100644 index 0000000..3328539 --- /dev/null +++ b/src/hexdump.c @@ -0,0 +1,49 @@ +#include +#include + +int dumpgroup = 0; // To know where newline should be +int hexnum = 1; // To put space after every second hex number + +int chardump(char *data, int end_of_line /* to display only 16 chars */) { + for (int i=0; i < end_of_line; i++) { + if (data[i] == "\0") + printf("."); + else + printf("%c", data[i]); + } + printf("\n"); + return 0; +} + + +int hexdump(char *data) { + //printf("%s\n", data); // FIXME + for (int i=0; i < strlen(data); i++) { + hexnum++; + if (data[i] == '\0') + printf("00"); + else + printf("%02x", data[i]); + if (hexnum%2) { + printf(" "); + dumpgroup++; + } + if (dumpgroup == 8) { + dumpgroup = 0; + printf(" "); + chardump(data, i); + //printf("\n"); + } + } + //printf("\n"); // FIXME +} + +int main() { + FILE *infile = fopen("../tests/test.bin", "r");//fopen("test.bin", "r"); + char data[50]; + while (fgets(data, 50, infile) != NULL) { + hexdump(data); + } + printf("\n"); // FIXME + fclose(infile); +} diff --git a/tests/test.bin b/tests/test.bin new file mode 100644 index 0000000000000000000000000000000000000000..9d60c2a57de634103e4f5dab2245b7c66463476d GIT binary patch literal 32 hcmXR(EiO?g0TF(oK0fhIks+>D3JgHN#RU@O0sy8Q2*&^b literal 0 HcmV?d00001