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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 <pclouds@xxxxxxxxx>
---
 .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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]