commit d5fb5de3863884453c0d126a3eaf93e8716ff72b Author: g1n Date: Sun Aug 22 18:27:46 2021 +0300 Initial commit: add very simple hexdump 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 0000000..9d60c2a Binary files /dev/null and b/tests/test.bin differ