1 |
//$Header$ |
2 |
//------------------------------------------------------------------------------------------------- |
3 |
//This file is part of "David T. Ashley's Shared Source Code", a set of shared components |
4 |
//integrated into many of David T. Ashley's projects. |
5 |
//------------------------------------------------------------------------------------------------- |
6 |
//This source code and any program in which it is compiled/used is provided under the MIT License, |
7 |
//reproduced below. |
8 |
//------------------------------------------------------------------------------------------------- |
9 |
//Permission is hereby granted, free of charge, to any person obtaining a copy of |
10 |
//this software and associated documentation files(the "Software"), to deal in the |
11 |
//Software without restriction, including without limitation the rights to use, |
12 |
//copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the |
13 |
//Software, and to permit persons to whom the Software is furnished to do so, |
14 |
//subject to the following conditions : |
15 |
// |
16 |
//The above copyright notice and this permission notice shall be included in all |
17 |
//copies or substantial portions of the Software. |
18 |
// |
19 |
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 |
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 |
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
22 |
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 |
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 |
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25 |
//SOFTWARE. |
26 |
//------------------------------------------------------------------------------------------------- |
27 |
#define MODULE_CRCHASHFUNCS |
28 |
|
29 |
#include <assert.h> |
30 |
#include <stdio.h> |
31 |
#include <string.h> |
32 |
|
33 |
#include "crchashfuncs.h" |
34 |
|
35 |
|
36 |
//This is the data table used to generate CRC-32 values. The code to |
37 |
//generate these values isn't reproduced here (it was discarded after |
38 |
//the values were generated), but this code originally came from a |
39 |
//web page by Richard A. Ellingson. I do not at this time understand |
40 |
//the math behind the generation or usage of this table. |
41 |
static const unsigned CRCHASHFUNCS_crc32tab[256] = |
42 |
{ |
43 |
/* [000] */ 0x00000000, |
44 |
/* [001] */ 0x77073096, |
45 |
/* [002] */ 0xEE0E612C, |
46 |
/* [003] */ 0x990951BA, |
47 |
/* [004] */ 0x076DC419, |
48 |
/* [005] */ 0x706AF48F, |
49 |
/* [006] */ 0xE963A535, |
50 |
/* [007] */ 0x9E6495A3, |
51 |
/* [008] */ 0x0EDB8832, |
52 |
/* [009] */ 0x79DCB8A4, |
53 |
/* [010] */ 0xE0D5E91E, |
54 |
/* [011] */ 0x97D2D988, |
55 |
/* [012] */ 0x09B64C2B, |
56 |
/* [013] */ 0x7EB17CBD, |
57 |
/* [014] */ 0xE7B82D07, |
58 |
/* [015] */ 0x90BF1D91, |
59 |
/* [016] */ 0x1DB71064, |
60 |
/* [017] */ 0x6AB020F2, |
61 |
/* [018] */ 0xF3B97148, |
62 |
/* [019] */ 0x84BE41DE, |
63 |
/* [020] */ 0x1ADAD47D, |
64 |
/* [021] */ 0x6DDDE4EB, |
65 |
/* [022] */ 0xF4D4B551, |
66 |
/* [023] */ 0x83D385C7, |
67 |
/* [024] */ 0x136C9856, |
68 |
/* [025] */ 0x646BA8C0, |
69 |
/* [026] */ 0xFD62F97A, |
70 |
/* [027] */ 0x8A65C9EC, |
71 |
/* [028] */ 0x14015C4F, |
72 |
/* [029] */ 0x63066CD9, |
73 |
/* [030] */ 0xFA0F3D63, |
74 |
/* [031] */ 0x8D080DF5, |
75 |
/* [032] */ 0x3B6E20C8, |
76 |
/* [033] */ 0x4C69105E, |
77 |
/* [034] */ 0xD56041E4, |
78 |
/* [035] */ 0xA2677172, |
79 |
/* [036] */ 0x3C03E4D1, |
80 |
/* [037] */ 0x4B04D447, |
81 |
/* [038] */ 0xD20D85FD, |
82 |
/* [039] */ 0xA50AB56B, |
83 |
/* [040] */ 0x35B5A8FA, |
84 |
/* [041] */ 0x42B2986C, |
85 |
/* [042] */ 0xDBBBC9D6, |
86 |
/* [043] */ 0xACBCF940, |
87 |
/* [044] */ 0x32D86CE3, |
88 |
/* [045] */ 0x45DF5C75, |
89 |
/* [046] */ 0xDCD60DCF, |
90 |
/* [047] */ 0xABD13D59, |
91 |
/* [048] */ 0x26D930AC, |
92 |
/* [049] */ 0x51DE003A, |
93 |
/* [050] */ 0xC8D75180, |
94 |
/* [051] */ 0xBFD06116, |
95 |
/* [052] */ 0x21B4F4B5, |
96 |
/* [053] */ 0x56B3C423, |
97 |
/* [054] */ 0xCFBA9599, |
98 |
/* [055] */ 0xB8BDA50F, |
99 |
/* [056] */ 0x2802B89E, |
100 |
/* [057] */ 0x5F058808, |
101 |
/* [058] */ 0xC60CD9B2, |
102 |
/* [059] */ 0xB10BE924, |
103 |
/* [060] */ 0x2F6F7C87, |
104 |
/* [061] */ 0x58684C11, |
105 |
/* [062] */ 0xC1611DAB, |
106 |
/* [063] */ 0xB6662D3D, |
107 |
/* [064] */ 0x76DC4190, |
108 |
/* [065] */ 0x01DB7106, |
109 |
/* [066] */ 0x98D220BC, |
110 |
/* [067] */ 0xEFD5102A, |
111 |
/* [068] */ 0x71B18589, |
112 |
/* [069] */ 0x06B6B51F, |
113 |
/* [070] */ 0x9FBFE4A5, |
114 |
/* [071] */ 0xE8B8D433, |
115 |
/* [072] */ 0x7807C9A2, |
116 |
/* [073] */ 0x0F00F934, |
117 |
/* [074] */ 0x9609A88E, |
118 |
/* [075] */ 0xE10E9818, |
119 |
/* [076] */ 0x7F6A0DBB, |
120 |
/* [077] */ 0x086D3D2D, |
121 |
/* [078] */ 0x91646C97, |
122 |
/* [079] */ 0xE6635C01, |
123 |
/* [080] */ 0x6B6B51F4, |
124 |
/* [081] */ 0x1C6C6162, |
125 |
/* [082] */ 0x856530D8, |
126 |
/* [083] */ 0xF262004E, |
127 |
/* [084] */ 0x6C0695ED, |
128 |
/* [085] */ 0x1B01A57B, |
129 |
/* [086] */ 0x8208F4C1, |
130 |
/* [087] */ 0xF50FC457, |
131 |
/* [088] */ 0x65B0D9C6, |
132 |
/* [089] */ 0x12B7E950, |
133 |
/* [090] */ 0x8BBEB8EA, |
134 |
/* [091] */ 0xFCB9887C, |
135 |
/* [092] */ 0x62DD1DDF, |
136 |
/* [093] */ 0x15DA2D49, |
137 |
/* [094] */ 0x8CD37CF3, |
138 |
/* [095] */ 0xFBD44C65, |
139 |
/* [096] */ 0x4DB26158, |
140 |
/* [097] */ 0x3AB551CE, |
141 |
/* [098] */ 0xA3BC0074, |
142 |
/* [099] */ 0xD4BB30E2, |
143 |
/* [100] */ 0x4ADFA541, |
144 |
/* [101] */ 0x3DD895D7, |
145 |
/* [102] */ 0xA4D1C46D, |
146 |
/* [103] */ 0xD3D6F4FB, |
147 |
/* [104] */ 0x4369E96A, |
148 |
/* [105] */ 0x346ED9FC, |
149 |
/* [106] */ 0xAD678846, |
150 |
/* [107] */ 0xDA60B8D0, |
151 |
/* [108] */ 0x44042D73, |
152 |
/* [109] */ 0x33031DE5, |
153 |
/* [110] */ 0xAA0A4C5F, |
154 |
/* [111] */ 0xDD0D7CC9, |
155 |
/* [112] */ 0x5005713C, |
156 |
/* [113] */ 0x270241AA, |
157 |
/* [114] */ 0xBE0B1010, |
158 |
/* [115] */ 0xC90C2086, |
159 |
/* [116] */ 0x5768B525, |
160 |
/* [117] */ 0x206F85B3, |
161 |
/* [118] */ 0xB966D409, |
162 |
/* [119] */ 0xCE61E49F, |
163 |
/* [120] */ 0x5EDEF90E, |
164 |
/* [121] */ 0x29D9C998, |
165 |
/* [122] */ 0xB0D09822, |
166 |
/* [123] */ 0xC7D7A8B4, |
167 |
/* [124] */ 0x59B33D17, |
168 |
/* [125] */ 0x2EB40D81, |
169 |
/* [126] */ 0xB7BD5C3B, |
170 |
/* [127] */ 0xC0BA6CAD, |
171 |
/* [128] */ 0xEDB88320, |
172 |
/* [129] */ 0x9ABFB3B6, |
173 |
/* [130] */ 0x03B6E20C, |
174 |
/* [131] */ 0x74B1D29A, |
175 |
/* [132] */ 0xEAD54739, |
176 |
/* [133] */ 0x9DD277AF, |
177 |
/* [134] */ 0x04DB2615, |
178 |
/* [135] */ 0x73DC1683, |
179 |
/* [136] */ 0xE3630B12, |
180 |
/* [137] */ 0x94643B84, |
181 |
/* [138] */ 0x0D6D6A3E, |
182 |
/* [139] */ 0x7A6A5AA8, |
183 |
/* [140] */ 0xE40ECF0B, |
184 |
/* [141] */ 0x9309FF9D, |
185 |
/* [142] */ 0x0A00AE27, |
186 |
/* [143] */ 0x7D079EB1, |
187 |
/* [144] */ 0xF00F9344, |
188 |
/* [145] */ 0x8708A3D2, |
189 |
/* [146] */ 0x1E01F268, |
190 |
/* [147] */ 0x6906C2FE, |
191 |
/* [148] */ 0xF762575D, |
192 |
/* [149] */ 0x806567CB, |
193 |
/* [150] */ 0x196C3671, |
194 |
/* [151] */ 0x6E6B06E7, |
195 |
/* [152] */ 0xFED41B76, |
196 |
/* [153] */ 0x89D32BE0, |
197 |
/* [154] */ 0x10DA7A5A, |
198 |
/* [155] */ 0x67DD4ACC, |
199 |
/* [156] */ 0xF9B9DF6F, |
200 |
/* [157] */ 0x8EBEEFF9, |
201 |
/* [158] */ 0x17B7BE43, |
202 |
/* [159] */ 0x60B08ED5, |
203 |
/* [160] */ 0xD6D6A3E8, |
204 |
/* [161] */ 0xA1D1937E, |
205 |
/* [162] */ 0x38D8C2C4, |
206 |
/* [163] */ 0x4FDFF252, |
207 |
/* [164] */ 0xD1BB67F1, |
208 |
/* [165] */ 0xA6BC5767, |
209 |
/* [166] */ 0x3FB506DD, |
210 |
/* [167] */ 0x48B2364B, |
211 |
/* [168] */ 0xD80D2BDA, |
212 |
/* [169] */ 0xAF0A1B4C, |
213 |
/* [170] */ 0x36034AF6, |
214 |
/* [171] */ 0x41047A60, |
215 |
/* [172] */ 0xDF60EFC3, |
216 |
/* [173] */ 0xA867DF55, |
217 |
/* [174] */ 0x316E8EEF, |
218 |
/* [175] */ 0x4669BE79, |
219 |
/* [176] */ 0xCB61B38C, |
220 |
/* [177] */ 0xBC66831A, |
221 |
/* [178] */ 0x256FD2A0, |
222 |
/* [179] */ 0x5268E236, |
223 |
/* [180] */ 0xCC0C7795, |
224 |
/* [181] */ 0xBB0B4703, |
225 |
/* [182] */ 0x220216B9, |
226 |
/* [183] */ 0x5505262F, |
227 |
/* [184] */ 0xC5BA3BBE, |
228 |
/* [185] */ 0xB2BD0B28, |
229 |
/* [186] */ 0x2BB45A92, |
230 |
/* [187] */ 0x5CB36A04, |
231 |
/* [188] */ 0xC2D7FFA7, |
232 |
/* [189] */ 0xB5D0CF31, |
233 |
/* [190] */ 0x2CD99E8B, |
234 |
/* [191] */ 0x5BDEAE1D, |
235 |
/* [192] */ 0x9B64C2B0, |
236 |
/* [193] */ 0xEC63F226, |
237 |
/* [194] */ 0x756AA39C, |
238 |
/* [195] */ 0x026D930A, |
239 |
/* [196] */ 0x9C0906A9, |
240 |
/* [197] */ 0xEB0E363F, |
241 |
/* [198] */ 0x72076785, |
242 |
/* [199] */ 0x05005713, |
243 |
/* [200] */ 0x95BF4A82, |
244 |
/* [201] */ 0xE2B87A14, |
245 |
/* [202] */ 0x7BB12BAE, |
246 |
/* [203] */ 0x0CB61B38, |
247 |
/* [204] */ 0x92D28E9B, |
248 |
/* [205] */ 0xE5D5BE0D, |
249 |
/* [206] */ 0x7CDCEFB7, |
250 |
/* [207] */ 0x0BDBDF21, |
251 |
/* [208] */ 0x86D3D2D4, |
252 |
/* [209] */ 0xF1D4E242, |
253 |
/* [210] */ 0x68DDB3F8, |
254 |
/* [211] */ 0x1FDA836E, |
255 |
/* [212] */ 0x81BE16CD, |
256 |
/* [213] */ 0xF6B9265B, |
257 |
/* [214] */ 0x6FB077E1, |
258 |
/* [215] */ 0x18B74777, |
259 |
/* [216] */ 0x88085AE6, |
260 |
/* [217] */ 0xFF0F6A70, |
261 |
/* [218] */ 0x66063BCA, |
262 |
/* [219] */ 0x11010B5C, |
263 |
/* [220] */ 0x8F659EFF, |
264 |
/* [221] */ 0xF862AE69, |
265 |
/* [222] */ 0x616BFFD3, |
266 |
/* [223] */ 0x166CCF45, |
267 |
/* [224] */ 0xA00AE278, |
268 |
/* [225] */ 0xD70DD2EE, |
269 |
/* [226] */ 0x4E048354, |
270 |
/* [227] */ 0x3903B3C2, |
271 |
/* [228] */ 0xA7672661, |
272 |
/* [229] */ 0xD06016F7, |
273 |
/* [230] */ 0x4969474D, |
274 |
/* [231] */ 0x3E6E77DB, |
275 |
/* [232] */ 0xAED16A4A, |
276 |
/* [233] */ 0xD9D65ADC, |
277 |
/* [234] */ 0x40DF0B66, |
278 |
/* [235] */ 0x37D83BF0, |
279 |
/* [236] */ 0xA9BCAE53, |
280 |
/* [237] */ 0xDEBB9EC5, |
281 |
/* [238] */ 0x47B2CF7F, |
282 |
/* [239] */ 0x30B5FFE9, |
283 |
/* [240] */ 0xBDBDF21C, |
284 |
/* [241] */ 0xCABAC28A, |
285 |
/* [242] */ 0x53B39330, |
286 |
/* [243] */ 0x24B4A3A6, |
287 |
/* [244] */ 0xBAD03605, |
288 |
/* [245] */ 0xCDD70693, |
289 |
/* [246] */ 0x54DE5729, |
290 |
/* [247] */ 0x23D967BF, |
291 |
/* [248] */ 0xB3667A2E, |
292 |
/* [249] */ 0xC4614AB8, |
293 |
/* [250] */ 0x5D681B02, |
294 |
/* [251] */ 0x2A6F2B94, |
295 |
/* [252] */ 0xB40BBE37, |
296 |
/* [253] */ 0xC30C8EA1, |
297 |
/* [254] */ 0x5A05DF1B, |
298 |
/* [255] */ 0x2D02EF8D |
299 |
}; |
300 |
|
301 |
//It is known that the XOR of all of the table values above is 0, |
302 |
//and that the sum is 0xFFFFFF80. This is used to check the table |
303 |
//for corruption on demand. |
304 |
#define CRCHASHFUNCS_CRC32TABLE_XOR (0x00000000) |
305 |
#define CRCHASHFUNCS_CRC32TABLE_SUM (0xFFFFFF80) |
306 |
|
307 |
|
308 |
//Rotates an unsigned int left with wraparound. |
309 |
static unsigned CRCHASHFUNCS_Crc32RotateLeftWithWrap(unsigned arg) |
310 |
{ |
311 |
if (arg & 0x80000000) |
312 |
{ |
313 |
arg <<= 1; |
314 |
arg |= (unsigned)1; |
315 |
} |
316 |
else |
317 |
{ |
318 |
arg <<= 1; |
319 |
arg &= 0xFFFFFFFE; |
320 |
} |
321 |
|
322 |
return(arg); |
323 |
} |
324 |
|
325 |
|
326 |
//Appears to swap the bits in a data value, bit 0 for bit 7, bit 1 |
327 |
//for bit 6, etc. Obtained from the Internet (Richard A. Ellingson). |
328 |
static unsigned CRCHASHFUNCS_Crc32Reflect(unsigned ref, |
329 |
unsigned char ch) |
330 |
{ |
331 |
unsigned i; |
332 |
unsigned value; |
333 |
|
334 |
value = 0; |
335 |
|
336 |
for (i=1; i<((unsigned)(ch+1)); i++) |
337 |
{ |
338 |
if (ref & 1) |
339 |
{ |
340 |
value |= (1 << (ch-i)); |
341 |
} |
342 |
ref >>= 1; |
343 |
} |
344 |
|
345 |
return(value); |
346 |
} |
347 |
|
348 |
|
349 |
//Returns !=0 if the internal table is alright. |
350 |
int CRCHASHFUNCS_Crc32TableCheck(void) |
351 |
{ |
352 |
int i; |
353 |
unsigned xor_val; |
354 |
unsigned sum_val; |
355 |
|
356 |
xor_val = sum_val = 0; |
357 |
|
358 |
for (i=0; i<256; i++) |
359 |
{ |
360 |
xor_val ^= CRCHASHFUNCS_crc32tab[i]; |
361 |
sum_val += CRCHASHFUNCS_crc32tab[i]; |
362 |
} |
363 |
|
364 |
return((xor_val == CRCHASHFUNCS_CRC32TABLE_XOR) && |
365 |
(sum_val == CRCHASHFUNCS_CRC32TABLE_SUM)); |
366 |
} |
367 |
|
368 |
|
369 |
//Initializes a CRC state struct. This will be added to, the CRC will be |
370 |
//extracted, and the data structure will finally be closed. |
371 |
void CRCHASHFUNCS_Crc32StateStructOpen(struct CRCHASHFUNCS_Crc32StateStruct *arg) |
372 |
{ |
373 |
arg->crc = 0xFFFFFFFF; |
374 |
} |
375 |
|
376 |
|
377 |
//Adds data to the data structure. |
378 |
void CRCHASHFUNCS_Crc32StateStructAddData(struct CRCHASHFUNCS_Crc32StateStruct *arg, |
379 |
void *data, |
380 |
unsigned len) |
381 |
{ |
382 |
assert(arg != NULL); |
383 |
assert(data != NULL); |
384 |
|
385 |
while (len--) |
386 |
{ |
387 |
arg->crc = (arg->crc >> 8) ^ CRCHASHFUNCS_crc32tab[((arg->crc & 0xFF) ^ *((unsigned char *)data)) & 0xFF]; |
388 |
data = (unsigned char *)data + 1; |
389 |
} |
390 |
} |
391 |
|
392 |
|
393 |
unsigned CRCHASHFUNCS_Crc32Extract(struct CRCHASHFUNCS_Crc32StateStruct *arg) |
394 |
{ |
395 |
assert(arg != NULL); |
396 |
|
397 |
return(arg->crc ^ 0xFFFFFFFF); |
398 |
} |
399 |
|
400 |
|
401 |
void CRCHASHFUNCS_Crc32StateStructClose(struct CRCHASHFUNCS_Crc32StateStruct *arg) |
402 |
{ |
403 |
assert(arg != NULL); |
404 |
|
405 |
//Do nothing at this time. |
406 |
} |
407 |
|
408 |
|
409 |
//Returns version control string for file. |
410 |
// |
411 |
const char *CRCHASHFUNCS_cvcinfo(void) |
412 |
{ |
413 |
return ("$Header$"); |
414 |
} |
415 |
|
416 |
|
417 |
//Returns version control string for associated .H file. |
418 |
// |
419 |
const char *CRCHASHFUNCS_hvcinfo(void) |
420 |
{ |
421 |
return (CRCHASHFUNCS_H_VERSION); |
422 |
} |
423 |
|
424 |
//End of crchashfuncs.c. |