Subject: [PATCH 01/10] test-dump: new test program to examine binary data

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 26 Sep 2013 09:26:40 +0700

This will come handy for verifying binary files like .pack, .idx or
$GIT_DIR/index. For now the only supported command is "ntohl". This
command looks into the given file at the given offset, do ntohl() and
return the value in decimal.

More commands may be added later to decode varint, or decompress a
zlib stream and so on...

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 .gitignore        |  1 +
 Makefile          |  1 +
 test-dump.c (new) | 27 +++++++++++++++++++++++++++
 3 files changed, 29 insertions(+)
 create mode 100644 test-dump.c

diff --git a/.gitignore b/.gitignore
index 2e2ae2b..6d835e2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -182,6 +182,7 @@
 /test-ctype
 /test-date
 /test-delta
+/test-dump
 /test-dump-cache-tree
 /test-scrap-cache-tree
 /test-genrandom
diff --git a/Makefile b/Makefile
index af2e3e3..a07a194 100644
--- a/Makefile
+++ b/Makefile
@@ -560,6 +560,7 @@ TEST_PROGRAMS_NEED_X += test-chmtime
 TEST_PROGRAMS_NEED_X += test-ctype
 TEST_PROGRAMS_NEED_X += test-date
 TEST_PROGRAMS_NEED_X += test-delta
+TEST_PROGRAMS_NEED_X += test-dump
 TEST_PROGRAMS_NEED_X += test-dump-cache-tree
 TEST_PROGRAMS_NEED_X += test-genrandom
 TEST_PROGRAMS_NEED_X += test-index-version
diff --git a/test-dump.c b/test-dump.c
new file mode 100644
index 0000000..71c6f8f
--- /dev/null
+++ b/test-dump.c
@@ -0,0 +1,27 @@
+#include "cache.h"
+
+static inline uint32_t ntoh_l_force_align(void *p)
+{
+	uint32_t x;
+	memcpy(&x, p, sizeof(x));
+	return ntohl(x);
+}
+
+int main(int ac, char **av)
+{
+	unsigned char *p;
+	int fd = open(av[2], O_RDONLY);
+	struct stat st;
+	if (lstat(av[2], &st))
+		return 1;
+	p = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+	if (p == (unsigned char*)MAP_FAILED)
+		return 2;
+	if (!strcmp(av[1], "ntohl"))
+		printf("%u\n", ntoh_l_force_align(p + atoi(av[3])));
+	else {
+		fprintf(stderr, "unknown command %s\n", av[1]);
+		return 3;
+	}
+	return 0;
+}
-- 
1.8.2.82.gc24b958