tools/crc32/main.cpp
author David Anderson <dvander@alliedmods.net>
Sat Aug 10 21:23:02 2013 -0700 (2013-08-10)
changeset 3982 be7835a7c28f
parent 1308 4054a74a7a2e
permissions -rw-r--r--
Move tracker related opcodes entirely to C++.

The tracker related opcodes:
GENARRAY
GENARRAY_Z
TRACKER_POP_SETHEAP
TRACKER_PUSH_C

All contain some vastly overcomplicated assembly containing logic that could
be implemented much easier in C++. If it were a performance concern, these
opcodes would be entirely in C++, but most of them call out to one or more
routines to do additional work. This patch just moves most of the logic out
to C++ to reduce complexity and fix reported bugs.
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <sm_crc32.h>
     4 
     5 int main(int argc, char **argv)
     6 {
     7 	if (argc < 2)
     8 	{
     9 		fprintf(stderr, "Usage: crc32 <file>\n");
    10 		exit(1);
    11 	}
    12 
    13 	FILE *fp = fopen(argv[1], "rb");
    14 	if (!fp)
    15 	{
    16 		fprintf(stderr, "Could not open file: %s\n", argv[1]);
    17 		exit(1);
    18 	}
    19 
    20 	fseek(fp, 0, SEEK_END);
    21 	size_t size = ftell(fp);	
    22 	if (!size)
    23 	{
    24 		fprintf(stderr, "Cannot checksum an empty file.\n");
    25 		exit(1);
    26 	}
    27 
    28 	fseek(fp, 0, SEEK_SET);
    29 
    30 	void *buffer = malloc(size);
    31 	if (!buffer)
    32 	{
    33 		fprintf(stderr, "Unable to allocate %d bytes of memory.\n", size);
    34 		exit(1);
    35 	}
    36 
    37 	fread(buffer, size, 1, fp);
    38 	unsigned int crc32 = UTIL_CRC32(buffer, size);
    39 	free(buffer);
    40 	fclose(fp);
    41 
    42 	fprintf(stdout, "%08X\n", crc32);
    43 
    44 	return 0;
    45 }
    46