-
Notifications
You must be signed in to change notification settings - Fork 1
/
crc.c
171 lines (117 loc) · 3.95 KB
/
crc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2004-2020 Sandboxie Holdings, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// Simple Hash Functions
//---------------------------------------------------------------------------
#ifdef CRC_HEADER_ONLY
//---------------------------------------------------------------------------
// Functions
//---------------------------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif
#include <windows.h>
ULONG CRC_Adler32(const UCHAR *data, int len);
ULONG CRC_Tzuk32(const UCHAR *data, int len);
ULONG64 CRC_AdlerTzuk64(const UCHAR *data, int len);
#ifdef __cplusplus
} // extern "C"
#endif
//---------------------------------------------------------------------------
// Body
//---------------------------------------------------------------------------
#else CRC_HEADER_ONLY
#include <windows.h>
#include "defines.h"
#ifdef CRC_WITH_ADLERTZUK64
#define CRC_WITH_ADLER32
#define CRC_WITH_TZUK32
#endif CRC_WITH_ADLERTZUK64
//---------------------------------------------------------------------------
// CRC_Adler32
//---------------------------------------------------------------------------
#ifdef CRC_WITH_ADLER32
_FX ULONG CRC_Adler32(const UCHAR *data, int len)
{
const ULONG MOD_ADLER = 65521;
ULONG a = 1, b = 0;
while (len) {
size_t tlen = len > 5550 ? 5550 : len;
len -= tlen;
do {
a += *data++;
b += a;
} while (--tlen);
a = (a & 0xffff) + (a >> 16) * (65536-MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536-MOD_ADLER);
}
if (a >= MOD_ADLER)
a -= MOD_ADLER;
b = (b & 0xffff) + (b >> 16) * (65536-MOD_ADLER);
if (b >= MOD_ADLER)
b -= MOD_ADLER;
b = (b << 16) | a;
return b;
}
#endif CRC_WITH_ADLER32
//---------------------------------------------------------------------------
// CRC_Tzuk32
//---------------------------------------------------------------------------
#ifdef CRC_WITH_TZUK32
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
_FX ULONG CRC_Tzuk32(const UCHAR *data, int len)
{
ULONG hash_val = 0x0BADF00D;
UCHAR *hash_ptr = (UCHAR *)&hash_val;
UCHAR b, c;
int j;
UCHAR sum = 0;
while (len) {
c = *data++;
--len;
for (j = 0; j < 8; ++j) {
b = 1 << j;
if (c & b)
hash_ptr[j & 3] += ROTATE_LEFT(c, (j & 0x1F));
else
hash_ptr[j & 3] += b;
}
hash_val = ROTATE_LEFT(hash_val, 1);
sum += c;
}
hash_val = ROTATE_LEFT(hash_val, (sum & 0x1F));
return hash_val;
}
#undef ROTATE_LEFT
#endif CRC_WITH_TZUK32
//---------------------------------------------------------------------------
// CRC_AdlerTzuk64
//---------------------------------------------------------------------------
#ifdef CRC_WITH_ADLERTZUK64
_FX ULONG64 CRC_AdlerTzuk64(const UCHAR *data, int len)
{
ULONG a = CRC_Adler32(data, len);
ULONG b = CRC_Tzuk32(data, len);
ULONG64 ab = (ULONG64)a;
ab = (ab << 32) | b;
return ab;
}
#endif CRC_WITH_ADLERTZUK64
//---------------------------------------------------------------------------
// End
//---------------------------------------------------------------------------
#endif CRC_HEADER_ONLY