2004-10-12 02:44:47 +00:00
|
|
|
// Calculate a CRC 32 checksum.
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/crc32.h> /*Added 10-6-04 */
|
2004-10-12 02:44:47 +00:00
|
|
|
|
|
|
|
// LAST MODIFIED:[7-28-93]
|
|
|
|
|
|
|
|
// Usage:
|
|
|
|
// unsigned long crc = -1L
|
|
|
|
// crc = crc32(buffer, length, crc)
|
|
|
|
|
2007-12-06 21:52:37 +00:00
|
|
|
unsigned int stasis_crc32(const void *buffer, unsigned int count, unsigned int crc);
|
2004-10-12 02:44:47 +00:00
|
|
|
static int BuildCRCTable(void);
|
|
|
|
|
2006-05-24 02:19:04 +00:00
|
|
|
static unsigned int CRCTable[256]; // Table constructed for fast lookup.
|
2004-10-12 02:44:47 +00:00
|
|
|
|
2004-10-17 02:17:00 +00:00
|
|
|
#define CRC32_POLYNOMIAL 0xEDB88320
|
2004-10-12 02:44:47 +00:00
|
|
|
|
|
|
|
// Initialize the CRC calculation table
|
|
|
|
//
|
|
|
|
static int BuildCRCTable(void)
|
|
|
|
{
|
|
|
|
int i, j;
|
2004-10-17 02:17:00 +00:00
|
|
|
unsigned int crc;
|
2004-10-12 02:44:47 +00:00
|
|
|
|
|
|
|
for (i = 0; i <= 255; i++)
|
|
|
|
{
|
|
|
|
crc = i;
|
|
|
|
for (j = 8; j > 0; j--)
|
|
|
|
if (crc & 1)
|
|
|
|
crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
|
|
|
|
else
|
|
|
|
crc >>= 1;
|
|
|
|
CRCTable[i] = crc;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2004-10-18 18:24:54 +00:00
|
|
|
/* changed long to int, void to const void - rusty. */
|
2007-12-06 21:52:37 +00:00
|
|
|
unsigned int stasis_crc32(const void *buffer, unsigned int count, unsigned int crc)
|
2004-10-12 02:44:47 +00:00
|
|
|
{
|
2004-10-17 02:17:00 +00:00
|
|
|
unsigned int temp1, temp2;
|
2004-10-12 02:44:47 +00:00
|
|
|
static int firsttime = 1;
|
|
|
|
unsigned char *p = (unsigned char *)buffer;
|
|
|
|
|
|
|
|
if (firsttime)
|
|
|
|
{
|
2006-05-24 02:19:04 +00:00
|
|
|
BuildCRCTable();
|
2004-10-12 02:44:47 +00:00
|
|
|
firsttime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (count-- != 0)
|
|
|
|
{
|
2004-10-17 02:17:00 +00:00
|
|
|
temp1 = (crc >> 8) & 0x00FFFFFF;
|
2004-10-12 02:44:47 +00:00
|
|
|
temp2 = CRCTable[((int)crc ^ *p++) & 0xFF];
|
|
|
|
crc = temp1 ^ temp2;
|
|
|
|
}
|
|
|
|
return crc;
|
|
|
|
}
|