1 // This file is part of par2cmdline (a PAR 2.0 compatible file verification and
2 // repair tool). See http://parchive.sourceforge.net for details of PAR 2.0.
3 //
4 // Copyright (c) 2003 Peter Brian Clements
5 //
6 // par2cmdline is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // par2cmdline is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19
20 #include "par2cmdline.h"
21
22 #ifdef _MSC_VER
23 #ifdef _DEBUG
24 #undef THIS_FILE
25 static char THIS_FILE[]=__FILE__;
26 #define new DEBUG_NEW
27 #endif
28 #endif
29
30 // The one and only CCITT CRC32 lookup table
31 crc32table ccitttable(0xEDB88320L);
32
33 // Construct the CRC32 lookup table from the specified polynomial
GenerateCRC32Table(u32 polynomial,u32 (& table)[256])34 void GenerateCRC32Table(u32 polynomial, u32 (&table)[256])
35 {
36 for (u32 i = 0; i <= 255 ; i++)
37 {
38 u32 crc = i;
39
40 for (u32 j = 0; j < 8; j++)
41 {
42 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
43 }
44
45 table[i] = crc;
46 }
47 }
48
49 // Construct a CRC32 lookup table for windowing
GenerateWindowTable(u64 window,u32 (& target)[256])50 void GenerateWindowTable(u64 window, u32 (&target)[256])
51 {
52 for (u32 i=0; i<=255; i++)
53 {
54 u32 crc = ccitttable.table[i];
55
56 for (u64 j=0; j<window; j++)
57 {
58 crc = ((crc >> 8) & 0x00ffffffL) ^ ccitttable.table[(u8)crc];
59 }
60
61 target[i] = crc;
62 }
63 }
64
65 // Construct the mask value to apply to the CRC when windowing
ComputeWindowMask(u64 window)66 u32 ComputeWindowMask(u64 window)
67 {
68 u32 result = ~0;
69 while (window > 0)
70 {
71 result = CRCUpdateChar(result, (char)0);
72
73 window--;
74 }
75 result ^= ~0;
76
77 return result;
78 }
79