raze-gles/source/thirdparty/src/crc32.cpp
Christoph Oelckers 2cbe211e7c - transitioned project to CMake and deleted most of the old build system.
The EDuke32 and RedNukem frontends are working, Blood isn't yet.

Notes:

many of the CMake variables and its output still refer to zdoom. Before changing that I wanted to make sure to be able to commit something that works.
support code for Windows XP has been entirely removed. On Windows this will only target Vista and up.
the crc32.h header had to be renamed to deconflict from zlib.
several Windows API calls were changed to call the A-versions directly. Weirdly enough there were places that defined their parameters as T types but in a non-working way.
removed some remaining editor files and support for the native software rendering only Windows backend.
in a few simple cases, replaced 'char' with 'uint8_t'. The code as-is depends on chars being unsigned which is non-portable. This needs to be carefully reviewed.
2019-09-22 23:15:46 +02:00

102 lines
3.2 KiB
C++

// based on http://create.stephan-brumme.com/crc32/Crc32.cpp, zlib license
#include "compat.h"
#include "crc32_.h"
uint32_t Bcrc32(const void* data, int length, uint32_t crc)
{
const uint32_t* current = (const uint32_t*) data;
uint8_t const * currentChar;
crc = ~crc;
#ifdef BITNESS64
// process eight bytes at once (Slicing-by-8)
while (length >= 8)
{
#if B_BIG_ENDIAN != 0
uint32_t one = *current ^ B_SWAP32(crc);
uint32_t two = *(current+1);
crc = crc32table[0][two & 0xFF] ^
crc32table[1][(two>> 8) & 0xFF] ^
crc32table[2][(two>>16) & 0xFF] ^
crc32table[3][(two>>24) & 0xFF] ^
crc32table[4][one & 0xFF] ^
crc32table[5][(one>> 8) & 0xFF] ^
crc32table[6][(one>>16) & 0xFF] ^
crc32table[7][(one>>24) & 0xFF];
#else
uint32_t one = *current ^ crc;
uint32_t two = *(current+1);
crc = crc32table[0][(two>>24) & 0xFF] ^
crc32table[1][(two>>16) & 0xFF] ^
crc32table[2][(two>> 8) & 0xFF] ^
crc32table[3][two & 0xFF] ^
crc32table[4][(one>>24) & 0xFF] ^
crc32table[5][(one>>16) & 0xFF] ^
crc32table[6][(one>> 8) & 0xFF] ^
crc32table[7][one & 0xFF];
#endif
current += 2;
length -= 8;
}
#else
// process four bytes at once (Slicing-by-4)
while (length >= 4)
{
#if B_BIG_ENDIAN != 0
uint32_t one = *current++ ^ B_SWAP32(crc);
crc = crc32table[0][one & 0xFF] ^
crc32table[1][(one>> 8) & 0xFF] ^
crc32table[2][(one>>16) & 0xFF] ^
crc32table[3][(one>>24) & 0xFF];
#else
uint32_t one = *current++ ^ crc;
crc = crc32table[0][(one>>24) & 0xFF] ^
crc32table[1][(one>>16) & 0xFF] ^
crc32table[2][(one>> 8) & 0xFF] ^
crc32table[3][one & 0xFF];
#endif
length -= 4;
}
#endif
currentChar = (uint8_t const *) current;
// remaining 1 to 7 bytes (standard algorithm)
while (length-- > 0)
crc = (crc >> 8) ^ crc32table[0][(crc & 0xFF) ^ *currentChar++];
return ~crc;
}
#ifdef BITNESS64
uint32_t crc32table[8][256];
#else
uint32_t crc32table[4][256];
#endif
void initcrc32table(void)
{
int i;
for (i = 0; i <= 0xFF; i++)
{
uint32_t j, crc = i;
for (j = 0; j < 8; j++)
crc = (crc >> 1) ^ ((crc & 1) * POLY);
crc32table[0][i] = crc;
}
for (i = 0; i <= 0xFF; i++)
{
crc32table[1][i] = (crc32table[0][i] >> 8) ^ crc32table[0][crc32table[0][i] & 0xFF];
crc32table[2][i] = (crc32table[1][i] >> 8) ^ crc32table[0][crc32table[1][i] & 0xFF];
crc32table[3][i] = (crc32table[2][i] >> 8) ^ crc32table[0][crc32table[2][i] & 0xFF];
#ifdef BITNESS64
crc32table[4][i] = (crc32table[3][i] >> 8) ^ crc32table[0][crc32table[3][i] & 0xFF];
crc32table[5][i] = (crc32table[4][i] >> 8) ^ crc32table[0][crc32table[4][i] & 0xFF];
crc32table[6][i] = (crc32table[5][i] >> 8) ^ crc32table[0][crc32table[5][i] & 0xFF];
crc32table[7][i] = (crc32table[6][i] >> 8) ^ crc32table[0][crc32table[6][i] & 0xFF];
#endif
}
}