forked from Parchive/par2cmdline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc_test.cpp
228 lines (172 loc) · 5.9 KB
/
crc_test.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// This file is part of par2cmdline (a PAR 2.0 compatible file verification and
// repair tool). See http://parchive.sourceforge.net for details of PAR 2.0.
//
// Copyright (c) 2019 Michael D. Nahas
//
// par2cmdline 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 2 of the License, or
// (at your option) any later version.
//
// par2cmdline 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// This is just a simple set of tests on md5 hash.
// The initial version just showed it was self-consistent, not accurate.
// I compile with:
// g++ -DHAVE_CONFIG_H -I.. crc_test.cpp crc.cpp
#include "par2cmdline.h"
#include <iostream>
#include <stdlib.h>
#include "crc.h"
// Example usage:
// u32 checksum = ~0 ^ CRCUpdateBlock(~0, (size_t)blocksize, buffer);
// compares UpdateBlock(crc, length) to UpdateBlock(crc,buffer,buffersize)
int test1() {
unsigned char buffer[] = {0,0,0,0,0,0,0,0};
u32 checksum1 = ~0 ^ CRCUpdateBlock(~0, sizeof(buffer), buffer);
u32 checksum2 = ~0 ^ CRCUpdateBlock(~0, sizeof(buffer));
if (checksum1 != checksum2) {
cerr << "checksum1 = " << checksum1 << endl;
cerr << "checksum2 = " << checksum2 << endl;
return 1;
}
return 0;
}
// CRC32 of "123456789" yields 0xCBF43926
// according to http://www.ross.net/crc/download/crc_v3.txt
int test2() {
unsigned char buffer[] = "123456789";
size_t buffer_length = 9;
u32 expected_checksum = 0xCBF43926u;
u32 checksum1 = ~0 ^ CRCUpdateBlock(~0, buffer_length, buffer);
if (checksum1 != expected_checksum) {
cerr << "checksum was not precalculated value: " << hex << checksum1 << dec << endl;
cerr << " expected " << hex << expected_checksum << dec << endl;
return 1;
}
return 0;
}
// generate random data.
// put it into checksum using different length blocks
// make sure output is the same.
int test3() {
srand(345087209);
unsigned char buffer[32*1024];
for (unsigned int i = 0; i < sizeof(buffer); i++) {
buffer[i] = (unsigned char) (rand() % 256);
}
u32 checksum1 = ~0;
unsigned int offset = 0;
while (offset < sizeof(buffer)) {
unsigned int length = (unsigned int) (rand() % 256);
if (offset + length > sizeof(buffer))
length = sizeof(buffer) - offset;
checksum1 = CRCUpdateBlock(checksum1, length, buffer + offset);
offset += length;
}
checksum1 = ~0 ^ checksum1;
u32 checksum2 = ~0;
offset = 0;
while (offset < sizeof(buffer)) {
unsigned int length = (unsigned int) (rand() % 256);
if (offset + length > sizeof(buffer))
length = sizeof(buffer) - offset;
checksum2 = CRCUpdateBlock(checksum2, length, buffer + offset);
offset += length;
}
checksum2 = ~0 ^ checksum2;
if (checksum1 != checksum2) {
cerr << "random checksum1 = " << checksum1 << endl;
cerr << "random checksum2 = " << checksum2 << endl;
return 1;
}
return 0;
}
// generate random data.
// compare char-at-a-time vs block
// make sure output is the same.
int test4() {
srand(113450911);
unsigned char buffer[32*1024];
for (unsigned int i = 0; i < sizeof(buffer); i++) {
buffer[i] = (unsigned char) (rand() % 256);
}
u32 checksum1 = ~0;
unsigned int offset = 0;
while (offset < sizeof(buffer)) {
unsigned int length = (int) (rand() % 256);
if (offset + length > sizeof(buffer))
length = sizeof(buffer) - offset;
checksum1 = CRCUpdateBlock(checksum1, length, buffer + offset);
offset += length;
}
checksum1 = ~0 ^ checksum1;
u32 checksum2 = ~0;
for (offset = 0; offset < sizeof(buffer); offset++) {
checksum2 = CRCUpdateChar(checksum2, *(buffer + offset));
}
checksum2 = ~0 ^ checksum2;
if (checksum1 != checksum2) {
cerr << "random checksum1 = " << checksum1 << endl;
cerr << "random checksum2 = " << checksum2 << endl;
return 1;
}
return 0;
}
// check windowing on random buffer
int test5() {
srand(113450911);
unsigned char buffer[32*1024];
for (unsigned int i = 0; i < sizeof(buffer); i++) {
buffer[i] = (unsigned char) (rand() % 256);
}
u64 window = 1024;
u32 windowtable[256];
GenerateWindowTable(window, windowtable);
u32 windowmask = ComputeWindowMask(window);
int result = 0;
u32 crc = ~0 ^ CRCUpdateBlock(~0, window, buffer);
for (int offset = 0; offset + window < sizeof(buffer) - 1; offset++) {
// compare against reference
u32 othercrc = ~0 ^ CRCUpdateBlock(~0, window, buffer + offset);
if (crc != othercrc) {
cerr << "error in window at offset " << offset << endl;
cerr << " checksum1 = " << crc << endl;
cerr << " checksum2 = " << othercrc << endl;
result = 1;
}
// slide window
crc = windowmask ^ CRCSlideChar(windowmask ^ crc, buffer[offset + window], buffer[offset], windowtable);
}
return result;
}
// Checksum of checksum table
// stolen from:
// http://www.efg2.com/Lab/Mathematics/CRC.htm
int test6() {
u32 checksum1 = ~0 ^ CRCUpdateBlock(~0, sizeof(ccitttable), &ccitttable);
u32 expected = 0x6FCF9E13;
if (checksum1 != expected) {
cerr << "error when computing checksum of checksum table " << endl;
cerr << " checksum1 = " << checksum1 << endl;
cerr << " expected = " << expected << endl;
return 0;
}
return 0;
}
int main() {
if (test1()) return 1;
if (test2()) return 1;
if (test3()) return 1;
if (test4()) return 1;
if (test5()) return 1;
if (test6()) return 1;
cout << "Tests finished" << endl;
return 0;
}