tools/crc32/main.cpp
changeset 1952 b7aa2b626807
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/crc32/main.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,46 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <sm_crc32.h>
     1.7 +
     1.8 +int main(int argc, char **argv)
     1.9 +{
    1.10 +	if (argc < 2)
    1.11 +	{
    1.12 +		fprintf(stderr, "Usage: crc32 <file>\n");
    1.13 +		exit(1);
    1.14 +	}
    1.15 +
    1.16 +	FILE *fp = fopen(argv[1], "rb");
    1.17 +	if (!fp)
    1.18 +	{
    1.19 +		fprintf(stderr, "Could not open file: %s\n", argv[1]);
    1.20 +		exit(1);
    1.21 +	}
    1.22 +
    1.23 +	fseek(fp, 0, SEEK_END);
    1.24 +	size_t size = ftell(fp);	
    1.25 +	if (!size)
    1.26 +	{
    1.27 +		fprintf(stderr, "Cannot checksum an empty file.\n");
    1.28 +		exit(1);
    1.29 +	}
    1.30 +
    1.31 +	fseek(fp, 0, SEEK_SET);
    1.32 +
    1.33 +	void *buffer = malloc(size);
    1.34 +	if (!buffer)
    1.35 +	{
    1.36 +		fprintf(stderr, "Unable to allocate %d bytes of memory.\n", size);
    1.37 +		exit(1);
    1.38 +	}
    1.39 +
    1.40 +	fread(buffer, size, 1, fp);
    1.41 +	unsigned int crc32 = UTIL_CRC32(buffer, size);
    1.42 +	free(buffer);
    1.43 +	fclose(fp);
    1.44 +
    1.45 +	fprintf(stdout, "%08X\n", crc32);
    1.46 +
    1.47 +	return 0;
    1.48 +}
    1.49 +