From ad1173a318e42cfa4609c296ada20911508d7d3e Mon Sep 17 00:00:00 2001
From: David Anderson <dvander@alliedmods.net>
Date: Sat, 11 Aug 2007 21:20:03 +0000
Subject: [PATCH] added small file-crc32'ing tool

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401314
---
 tools/crc32/build.sh |  2 ++
 tools/crc32/main.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100755 tools/crc32/build.sh
 create mode 100644 tools/crc32/main.cpp

diff --git a/tools/crc32/build.sh b/tools/crc32/build.sh
new file mode 100755
index 00000000..62b2891c
--- /dev/null
+++ b/tools/crc32/build.sh
@@ -0,0 +1,2 @@
+g++ -I../../core ../../core/sm_crc32.cpp main.cpp -ocrc32
+
diff --git a/tools/crc32/main.cpp b/tools/crc32/main.cpp
new file mode 100644
index 00000000..ef7399c3
--- /dev/null
+++ b/tools/crc32/main.cpp
@@ -0,0 +1,46 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sm_crc32.h>
+
+int main(int argc, char **argv)
+{
+	if (argc < 2)
+	{
+		fprintf(stderr, "Usage: crc32 <file>\n");
+		exit(1);
+	}
+
+	FILE *fp = fopen(argv[1], "rb");
+	if (!fp)
+	{
+		fprintf(stderr, "Could not open file: %s\n", argv[1]);
+		exit(1);
+	}
+
+	fseek(fp, 0, SEEK_END);
+	size_t size = ftell(fp);	
+	if (!size)
+	{
+		fprintf(stderr, "Cannot checksum an empty file.\n");
+		exit(1);
+	}
+
+	fseek(fp, 0, SEEK_SET);
+
+	void *buffer = malloc(size);
+	if (!buffer)
+	{
+		fprintf(stderr, "Unable to allocate %d bytes of memory.\n", size);
+		exit(1);
+	}
+
+	fread(buffer, size, 1, fp);
+	unsigned int crc32 = UTIL_CRC32(buffer, size);
+	free(buffer);
+	fclose(fp);
+
+	fprintf(stdout, "%08X\n", crc32);
+
+	return 0;
+}
+