Subversion Repositories eduke32

Rev

Rev 4642 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4625 terminx 1
// based on http://create.stephan-brumme.com/crc32/Crc32.cpp, zlib license
2
 
3
#include "compat.h"
99 terminx 4
#include "crc32.h"
5
 
4642 terminx 6
uint32_t Bcrc32(const void* data, size_t length, uint32_t crc)
99 terminx 7
{
4625 terminx 8
    const uint32_t* current = (const uint32_t*) data;
9
    uint8_t *currentChar;
10
    crc = ~crc;
109 terminx 11
 
4625 terminx 12
#ifdef BITNESS64
13
    // process eight bytes at once (Slicing-by-8)
14
    while (length >= 8)
584 terminx 15
    {
4625 terminx 16
#if B_BIG_ENDIAN != 0
17
        uint32_t one = *current ^ B_SWAP32(crc);
18
        uint32_t two = *(current+1);
19
        crc  = crc32table[0][two       & 0xFF] ^
20
               crc32table[1][(two>> 8) & 0xFF] ^
21
               crc32table[2][(two>>16) & 0xFF] ^
22
               crc32table[3][(two>>24) & 0xFF] ^
23
               crc32table[4][one       & 0xFF] ^
24
               crc32table[5][(one>> 8) & 0xFF] ^
25
               crc32table[6][(one>>16) & 0xFF] ^
26
               crc32table[7][(one>>24) & 0xFF];
27
#else
28
        uint32_t one = *current ^ crc;
29
        uint32_t two = *(current+1);
30
        crc  = crc32table[0][(two>>24) & 0xFF] ^
31
               crc32table[1][(two>>16) & 0xFF] ^
32
               crc32table[2][(two>> 8) & 0xFF] ^
33
               crc32table[3][two       & 0xFF] ^
34
               crc32table[4][(one>>24) & 0xFF] ^
35
               crc32table[5][(one>>16) & 0xFF] ^
36
               crc32table[6][(one>> 8) & 0xFF] ^
37
               crc32table[7][one       & 0xFF];
38
#endif
39
        current += 2;
40
        length -= 8;
109 terminx 41
    }
4625 terminx 42
#else
43
    // process four bytes at once (Slicing-by-4)
44
    while (length >= 4)
45
    {
46
#if B_BIG_ENDIAN != 0
47
        uint32_t one = *current++ ^ B_SWAP32(crc);
48
        crc  = crc32table[0][one       & 0xFF] ^
49
               crc32table[1][(one>> 8) & 0xFF] ^
50
               crc32table[2][(one>>16) & 0xFF] ^
51
               crc32table[3][(one>>24) & 0xFF];
52
#else
53
        uint32_t one = *current++ ^ crc;
54
        crc  = crc32table[0][(one>>24) & 0xFF] ^
55
               crc32table[1][(one>>16) & 0xFF] ^
56
               crc32table[2][(one>> 8) & 0xFF] ^
57
               crc32table[3][one       & 0xFF];
58
#endif
59
 
60
        length -= 4;
61
    }
62
#endif
63
 
64
    currentChar = (uint8_t*) current;
65
    // remaining 1 to 7 bytes (standard algorithm)
66
    while (length-- > 0)
67
        crc = (crc >> 8) ^ crc32table[0][(crc & 0xFF) ^ *currentChar++];
68
 
69
    return ~crc;
99 terminx 70
}
71
 
4625 terminx 72
#ifdef BITNESS64
73
uint32_t crc32table[8][256];
74
#else
75
uint32_t crc32table[4][256];
76
#endif
77
 
78
void initcrc32table(void)
99 terminx 79
{
4625 terminx 80
    int i;
81
    for (i = 0; i <= 0xFF; i++)
82
    {
83
      uint32_t j, crc = i;
84
      for (j = 0; j < 8; j++)
85
        crc = (crc >> 1) ^ ((crc & 1) * POLY);
86
      crc32table[0][i] = crc;
87
    }
109 terminx 88
 
4625 terminx 89
    for (i = 0; i <= 0xFF; i++)
90
    {
91
      crc32table[1][i] = (crc32table[0][i] >> 8) ^ crc32table[0][crc32table[0][i] & 0xFF];
92
      crc32table[2][i] = (crc32table[1][i] >> 8) ^ crc32table[0][crc32table[1][i] & 0xFF];
93
      crc32table[3][i] = (crc32table[2][i] >> 8) ^ crc32table[0][crc32table[2][i] & 0xFF];
94
 
95
#ifdef BITNESS64
96
      crc32table[4][i] = (crc32table[3][i] >> 8) ^ crc32table[0][crc32table[3][i] & 0xFF];
97
      crc32table[5][i] = (crc32table[4][i] >> 8) ^ crc32table[0][crc32table[4][i] & 0xFF];
98
      crc32table[6][i] = (crc32table[5][i] >> 8) ^ crc32table[0][crc32table[5][i] & 0xFF];
99
      crc32table[7][i] = (crc32table[6][i] >> 8) ^ crc32table[0][crc32table[6][i] & 0xFF];
100
#endif
101
    }
5037 hendricks2 102
}