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 "nzbget.h"
21 #include "par2cmdline.h"
22
23 #ifdef _MSC_VER
24 #ifdef _DEBUG
25 #undef THIS_FILE
26 static char THIS_FILE[]=__FILE__;
27 #define new DEBUG_NEW
28 #endif
29 #endif
30
31 namespace Par2
32 {
33
34 // The one and only CCITT CRC32 lookup table
35 crc32table ccitttable(0xEDB88320L);
36
37 // Construct the CRC32 lookup table from the specified polynomial
GenerateCRC32Table(u32 polynomial,u32 (& table)[256])38 void GenerateCRC32Table(u32 polynomial, u32 (&table)[256])
39 {
40 for (u32 i = 0; i <= 255 ; i++)
41 {
42 u32 crc = i;
43
44 for (u32 j = 0; j < 8; j++)
45 {
46 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
47 }
48
49 table[i] = crc;
50 }
51 }
52
53 // Construct a CRC32 lookup table for windowing
GenerateWindowTable(u64 window,u32 (& target)[256])54 void GenerateWindowTable(u64 window, u32 (&target)[256])
55 {
56 for (u32 i=0; i<=255; i++)
57 {
58 u32 crc = ccitttable.table[i];
59
60 for (u64 j=0; j<window; j++)
61 {
62 crc = ((crc >> 8) & 0x00ffffffL) ^ ccitttable.table[(u8)crc];
63 }
64
65 target[i] = crc;
66 }
67 }
68
69 // Construct the mask value to apply to the CRC when windowing
ComputeWindowMask(u64 window)70 u32 ComputeWindowMask(u64 window)
71 {
72 u32 result = ~0;
73 while (window > 0)
74 {
75 result = CRCUpdateChar(result, (char)0);
76
77 window--;
78 }
79 result ^= ~0;
80
81 return result;
82 }
83
84 } // end namespace Par2
85