From c369e78296e2e91f31a1a324648b12acc459b60c Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 17 Mar 2010 14:27:26 -0400 Subject: [PATCH] Added crc32 command to test_physfs.c (thanks, Christoph!). --- test/test_physfs.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/test_physfs.c b/test/test_physfs.c index e8e8742..7f92faf 100644 --- a/test/test_physfs.c +++ b/test/test_physfs.c @@ -746,6 +746,53 @@ static int cmd_cat(char *args) } /* cmd_cat */ +#define CRC32_BUFFERSIZE 512 +static int cmd_crc32(char *args) +{ + PHYSFS_File *f; + + if (*args == '\"') + { + args++; + args[strlen(args) - 1] = '\0'; + } /* if */ + + f = PHYSFS_openRead(args); + if (f == NULL) + printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError()); + else + { + PHYSFS_uint8 buffer[CRC32_BUFFERSIZE]; + PHYSFS_uint32 crc = -1; + PHYSFS_sint64 bytesread; + + while ((bytesread = PHYSFS_read(f, buffer, 1, CRC32_BUFFERSIZE)) > 0) + { + PHYSFS_uint32 i, bit; + for (i = 0; i < bytesread; i++) + { + for (bit = 0; bit < 8; bit++, buffer[i] >>= 1) + crc = (crc >> 1) ^ (((crc ^ buffer[i]) & 1) ? 0xEDB88320 : 0); + } /* for */ + } /* while */ + + if (bytesread < 0) + { + printf("error while reading. Reason: [%s].\n", + PHYSFS_getLastError()); + return 1; + } /* if */ + + PHYSFS_close(f); + + crc ^= -1; + printf("CRC32 for %s: 0x%08X\n", args, crc); + } /* else */ + + return 1; +} /* cmd_crc32 */ + + static int cmd_filelength(char *args) { PHYSFS_File *f; @@ -1003,6 +1050,7 @@ static const command_info commands[] = { "getlastmodtime", cmd_getlastmodtime, 1, "" }, { "setbuffer", cmd_setbuffer, 1, "" }, { "stressbuffer", cmd_stressbuffer, 1, "" }, + { "crc32", cmd_crc32, 1, "" }, { NULL, NULL, -1, NULL } };