Blame view

lib/crc32.c 14.5 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  /*
   * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
   * Nicer crc32 functions/docs submitted by linux@horizon.com.  Thanks!
   * Code was from the public domain, copyright abandoned.  Code was
   * subsequently included in the kernel, thus was re-licensed under the
   * GNU GPL v2.
   *
   * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
   * Same crc32 function was used in 5 other places in the kernel.
   * I made one version, and deleted the others.
   * There are various incantations of crc32().  Some use a seed of 0 or ~0.
   * Some xor at the end with ~0.  The generic crc32() function takes
   * seed as an argument, and doesn't xor at the end.  Then individual
   * users can do whatever they need.
   *   drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
   *   fs/jffs2 uses seed 0, doesn't xor with ~0.
   *   fs/partitions/efi.c uses seed ~0, xor's with ~0.
   *
   * This source code is licensed under the GNU General Public License,
   * Version 2.  See the file COPYING for more details.
   */
  
  #include <linux/crc32.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/compiler.h>
  #include <linux/types.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
  #include <linux/init.h>
60063497a   Arun Sharma   atomic: use <linu...
29
  #include <linux/atomic.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
31
  #include "crc32defs.h"
  #if CRC_LE_BITS == 8
4f2a9463d   Joakim Tjernlund   crc32: some minor...
32
  # define tole(x) __constant_cpu_to_le32(x)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  #else
4f2a9463d   Joakim Tjernlund   crc32: some minor...
34
35
36
37
38
39
40
  # define tole(x) (x)
  #endif
  
  #if CRC_BE_BITS == 8
  # define tobe(x) __constant_cpu_to_be32(x)
  #else
  # define tobe(x) (x)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
45
46
  #endif
  #include "crc32table.h"
  
  MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
  MODULE_DESCRIPTION("Ethernet CRC32 calculations");
  MODULE_LICENSE("GPL");
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
47
48
49
  #if CRC_LE_BITS == 8 || CRC_BE_BITS == 8
  
  static inline u32
836e2af92   Joakim Tjernlund   crc32: major opti...
50
  crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
51
  {
0d2daf5cc   Andrew Morton   revert "crc32: us...
52
  # ifdef __LITTLE_ENDIAN
5742332de   Joakim Tjernlund   crc32: optimize i...
53
54
55
56
57
  #  define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
  #  define DO_CRC4 crc = t3[(crc) & 255] ^ \
  		t2[(crc >> 8) & 255] ^ \
  		t1[(crc >> 16) & 255] ^ \
  		t0[(crc >> 24) & 255]
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
58
  # else
5742332de   Joakim Tjernlund   crc32: optimize i...
59
60
61
62
63
  #  define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
  #  define DO_CRC4 crc = t0[(crc) & 255] ^ \
  		t1[(crc >> 8) & 255] ^  \
  		t2[(crc >> 16) & 255] ^	\
  		t3[(crc >> 24) & 255]
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
64
  # endif
4f2a9463d   Joakim Tjernlund   crc32: some minor...
65
  	const u32 *b;
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
66
  	size_t    rem_len;
5742332de   Joakim Tjernlund   crc32: optimize i...
67
  	const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
68
69
  
  	/* Align it */
4f2a9463d   Joakim Tjernlund   crc32: some minor...
70
  	if (unlikely((long)buf & 3 && len)) {
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
71
  		do {
4f2a9463d   Joakim Tjernlund   crc32: some minor...
72
73
  			DO_CRC(*buf++);
  		} while ((--len) && ((long)buf)&3);
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
74
75
76
77
  	}
  	rem_len = len & 3;
  	/* load data 32 bits wide, xor data 32 bits wide. */
  	len = len >> 2;
4f2a9463d   Joakim Tjernlund   crc32: some minor...
78
  	b = (const u32 *)buf;
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
79
80
  	for (--b; len; --len) {
  		crc ^= *++b; /* use pre increment for speed */
836e2af92   Joakim Tjernlund   crc32: major opti...
81
  		DO_CRC4;
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
82
83
84
85
86
87
88
89
90
91
  	}
  	len = rem_len;
  	/* And the last few bytes */
  	if (len) {
  		u8 *p = (u8 *)(b + 1) - 1;
  		do {
  			DO_CRC(*++p); /* use pre increment for speed */
  		} while (--len);
  	}
  	return crc;
4f2a9463d   Joakim Tjernlund   crc32: some minor...
92
  #undef DO_CRC
836e2af92   Joakim Tjernlund   crc32: major opti...
93
  #undef DO_CRC4
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
94
95
  }
  #endif
2f72100c0   Randy Dunlap   [PATCH] kernel-do...
96
97
98
99
100
101
102
  /**
   * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
   * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for
   *	other uses, or the previous crc32 value if computing incrementally.
   * @p: pointer to buffer over which CRC is run
   * @len: length of buffer @p
   */
e8c44319c   Ralf Baechle   Replace __attribu...
103
  u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len);
2f72100c0   Randy Dunlap   [PATCH] kernel-do...
104

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
106
107
108
109
  #if CRC_LE_BITS == 1
  /*
   * In fact, the table-based code will work in this case, but it can be
   * simplified by inlining the table in ?: form.
   */
e8c44319c   Ralf Baechle   Replace __attribu...
110
  u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
112
113
114
115
116
117
118
119
120
  {
  	int i;
  	while (len--) {
  		crc ^= *p++;
  		for (i = 0; i < 8; i++)
  			crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  	}
  	return crc;
  }
  #else				/* Table-based approach */
e8c44319c   Ralf Baechle   Replace __attribu...
121
  u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
  {
  # if CRC_LE_BITS == 8
836e2af92   Joakim Tjernlund   crc32: major opti...
124
  	const u32      (*tab)[] = crc32table_le;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  	crc = __cpu_to_le32(crc);
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
127
  	crc = crc32_body(crc, p, len, tab);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
  	return __le32_to_cpu(crc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  # elif CRC_LE_BITS == 4
  	while (len--) {
  		crc ^= *p++;
  		crc = (crc >> 4) ^ crc32table_le[crc & 15];
  		crc = (crc >> 4) ^ crc32table_le[crc & 15];
  	}
  	return crc;
  # elif CRC_LE_BITS == 2
  	while (len--) {
  		crc ^= *p++;
  		crc = (crc >> 2) ^ crc32table_le[crc & 3];
  		crc = (crc >> 2) ^ crc32table_le[crc & 3];
  		crc = (crc >> 2) ^ crc32table_le[crc & 3];
  		crc = (crc >> 2) ^ crc32table_le[crc & 3];
  	}
  	return crc;
  # endif
  }
  #endif
2f72100c0   Randy Dunlap   [PATCH] kernel-do...
148
149
150
151
152
153
154
  /**
   * crc32_be() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
   * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for
   *	other uses, or the previous crc32 value if computing incrementally.
   * @p: pointer to buffer over which CRC is run
   * @len: length of buffer @p
   */
e8c44319c   Ralf Baechle   Replace __attribu...
155
  u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len);
2f72100c0   Randy Dunlap   [PATCH] kernel-do...
156

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
159
160
161
  #if CRC_BE_BITS == 1
  /*
   * In fact, the table-based code will work in this case, but it can be
   * simplified by inlining the table in ?: form.
   */
e8c44319c   Ralf Baechle   Replace __attribu...
162
  u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
166
167
168
169
170
171
172
173
174
175
  {
  	int i;
  	while (len--) {
  		crc ^= *p++ << 24;
  		for (i = 0; i < 8; i++)
  			crc =
  			    (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE :
  					  0);
  	}
  	return crc;
  }
  
  #else				/* Table-based approach */
e8c44319c   Ralf Baechle   Replace __attribu...
176
  u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
178
  {
  # if CRC_BE_BITS == 8
836e2af92   Joakim Tjernlund   crc32: major opti...
179
  	const u32      (*tab)[] = crc32table_be;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
  	crc = __cpu_to_be32(crc);
ddcaccbc1   Joakim Tjernlund   crc32: minor opti...
182
  	crc = crc32_body(crc, p, len, tab);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
  	return __be32_to_cpu(crc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  # elif CRC_BE_BITS == 4
  	while (len--) {
  		crc ^= *p++ << 24;
  		crc = (crc << 4) ^ crc32table_be[crc >> 28];
  		crc = (crc << 4) ^ crc32table_be[crc >> 28];
  	}
  	return crc;
  # elif CRC_BE_BITS == 2
  	while (len--) {
  		crc ^= *p++ << 24;
  		crc = (crc << 2) ^ crc32table_be[crc >> 30];
  		crc = (crc << 2) ^ crc32table_be[crc >> 30];
  		crc = (crc << 2) ^ crc32table_be[crc >> 30];
  		crc = (crc << 2) ^ crc32table_be[crc >> 30];
  	}
  	return crc;
  # endif
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
204
  EXPORT_SYMBOL(crc32_le);
  EXPORT_SYMBOL(crc32_be);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  
  /*
   * A brief CRC tutorial.
   *
   * A CRC is a long-division remainder.  You add the CRC to the message,
   * and the whole thing (message+CRC) is a multiple of the given
   * CRC polynomial.  To check the CRC, you can either check that the
   * CRC matches the recomputed value, *or* you can check that the
   * remainder computed on the message+CRC is 0.  This latter approach
   * is used by a lot of hardware implementations, and is why so many
   * protocols put the end-of-frame flag after the CRC.
   *
   * It's actually the same long division you learned in school, except that
   * - We're working in binary, so the digits are only 0 and 1, and
   * - When dividing polynomials, there are no carries.  Rather than add and
   *   subtract, we just xor.  Thus, we tend to get a bit sloppy about
   *   the difference between adding and subtracting.
   *
   * A 32-bit CRC polynomial is actually 33 bits long.  But since it's
   * 33 bits long, bit 32 is always going to be set, so usually the CRC
   * is written in hex with the most significant bit omitted.  (If you're
   * familiar with the IEEE 754 floating-point format, it's the same idea.)
   *
   * Note that a CRC is computed over a string of *bits*, so you have
   * to decide on the endianness of the bits within each byte.  To get
   * the best error-detecting properties, this should correspond to the
   * order they're actually sent.  For example, standard RS-232 serial is
   * little-endian; the most significant bit (sometimes used for parity)
   * is sent last.  And when appending a CRC word to a message, you should
   * do it in the right order, matching the endianness.
   *
   * Just like with ordinary division, the remainder is always smaller than
   * the divisor (the CRC polynomial) you're dividing by.  Each step of the
   * division, you take one more digit (bit) of the dividend and append it
   * to the current remainder.  Then you figure out the appropriate multiple
   * of the divisor to subtract to being the remainder back into range.
   * In binary, it's easy - it has to be either 0 or 1, and to make the
   * XOR cancel, it's just a copy of bit 32 of the remainder.
   *
   * When computing a CRC, we don't care about the quotient, so we can
   * throw the quotient bit away, but subtract the appropriate multiple of
   * the polynomial from the remainder and we're back to where we started,
   * ready to process the next bit.
   *
   * A big-endian CRC written this way would be coded like:
   * for (i = 0; i < input_bits; i++) {
   * 	multiple = remainder & 0x80000000 ? CRCPOLY : 0;
   * 	remainder = (remainder << 1 | next_input_bit()) ^ multiple;
   * }
   * Notice how, to get at bit 32 of the shifted remainder, we look
   * at bit 31 of the remainder *before* shifting it.
   *
   * But also notice how the next_input_bit() bits we're shifting into
   * the remainder don't actually affect any decision-making until
   * 32 bits later.  Thus, the first 32 cycles of this are pretty boring.
   * Also, to add the CRC to a message, we need a 32-bit-long hole for it at
   * the end, so we have to add 32 extra cycles shifting in zeros at the
   * end of every message,
   *
   * So the standard trick is to rearrage merging in the next_input_bit()
   * until the moment it's needed.  Then the first 32 cycles can be precomputed,
   * and merging in the final 32 zero bits to make room for the CRC can be
   * skipped entirely.
   * This changes the code to:
   * for (i = 0; i < input_bits; i++) {
   *      remainder ^= next_input_bit() << 31;
   * 	multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
   * 	remainder = (remainder << 1) ^ multiple;
   * }
   * With this optimization, the little-endian code is simpler:
   * for (i = 0; i < input_bits; i++) {
   *      remainder ^= next_input_bit();
   * 	multiple = (remainder & 1) ? CRCPOLY : 0;
   * 	remainder = (remainder >> 1) ^ multiple;
   * }
   *
   * Note that the other details of endianness have been hidden in CRCPOLY
   * (which must be bit-reversed) and next_input_bit().
   *
   * However, as long as next_input_bit is returning the bits in a sensible
   * order, we can actually do the merging 8 or more bits at a time rather
   * than one bit at a time:
   * for (i = 0; i < input_bytes; i++) {
   * 	remainder ^= next_input_byte() << 24;
   * 	for (j = 0; j < 8; j++) {
   * 		multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
   * 		remainder = (remainder << 1) ^ multiple;
   * 	}
   * }
   * Or in little-endian:
   * for (i = 0; i < input_bytes; i++) {
   * 	remainder ^= next_input_byte();
   * 	for (j = 0; j < 8; j++) {
   * 		multiple = (remainder & 1) ? CRCPOLY : 0;
   * 		remainder = (remainder << 1) ^ multiple;
   * 	}
   * }
   * If the input is a multiple of 32 bits, you can even XOR in a 32-bit
   * word at a time and increase the inner loop count to 32.
   *
   * You can also mix and match the two loop styles, for example doing the
   * bulk of a message byte-at-a-time and adding bit-at-a-time processing
   * for any fractional bytes at the end.
   *
   * The only remaining optimization is to the byte-at-a-time table method.
   * Here, rather than just shifting one bit of the remainder to decide
   * in the correct multiple to subtract, we can shift a byte at a time.
   * This produces a 40-bit (rather than a 33-bit) intermediate remainder,
   * but again the multiple of the polynomial to subtract depends only on
   * the high bits, the high 8 bits in this case.  
   *
643d1f7fe   Joe Perches   lib/: Spelling fixes
316
   * The multiple we need in that case is the low 32 bits of a 40-bit
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
   * value whose high 8 bits are given, and which is a multiple of the
   * generator polynomial.  This is simply the CRC-32 of the given
   * one-byte message.
   *
   * Two more details: normally, appending zero bits to a message which
   * is already a multiple of a polynomial produces a larger multiple of that
   * polynomial.  To enable a CRC to detect this condition, it's common to
   * invert the CRC before appending it.  This makes the remainder of the
   * message+crc come out not as zero, but some fixed non-zero value.
   *
   * The same problem applies to zero bits prepended to the message, and
   * a similar solution is used.  Instead of starting with a remainder of
   * 0, an initial remainder of all ones is used.  As long as you start
   * the same way on decoding, it doesn't make a difference.
   */
  
  #ifdef UNITTEST
  
  #include <stdlib.h>
  #include <stdio.h>
  
  #if 0				/*Not used at present */
  static void
  buf_dump(char const *prefix, unsigned char const *buf, size_t len)
  {
  	fputs(prefix, stdout);
  	while (len--)
  		printf(" %02x", *buf++);
  	putchar('
  ');
  
  }
  #endif
  
  static void bytereverse(unsigned char *buf, size_t len)
  {
  	while (len--) {
906d66df1   Akinobu Mita   [PATCH] crc32: re...
354
  		unsigned char x = bitrev8(*buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
  		*buf++ = x;
  	}
  }
  
  static void random_garbage(unsigned char *buf, size_t len)
  {
  	while (len--)
  		*buf++ = (unsigned char) random();
  }
  
  #if 0				/* Not used at present */
  static void store_le(u32 x, unsigned char *buf)
  {
  	buf[0] = (unsigned char) x;
  	buf[1] = (unsigned char) (x >> 8);
  	buf[2] = (unsigned char) (x >> 16);
  	buf[3] = (unsigned char) (x >> 24);
  }
  #endif
  
  static void store_be(u32 x, unsigned char *buf)
  {
  	buf[0] = (unsigned char) (x >> 24);
  	buf[1] = (unsigned char) (x >> 16);
  	buf[2] = (unsigned char) (x >> 8);
  	buf[3] = (unsigned char) x;
  }
  
  /*
   * This checks that CRC(buf + CRC(buf)) = 0, and that
   * CRC commutes with bit-reversal.  This has the side effect
   * of bytewise bit-reversing the input buffer, and returns
   * the CRC of the reversed buffer.
   */
  static u32 test_step(u32 init, unsigned char *buf, size_t len)
  {
  	u32 crc1, crc2;
  	size_t i;
  
  	crc1 = crc32_be(init, buf, len);
  	store_be(crc1, buf + len);
  	crc2 = crc32_be(init, buf, len + 4);
  	if (crc2)
  		printf("
  CRC cancellation fail: 0x%08x should be 0
  ",
  		       crc2);
  
  	for (i = 0; i <= len + 4; i++) {
  		crc2 = crc32_be(init, buf, i);
  		crc2 = crc32_be(crc2, buf + i, len + 4 - i);
  		if (crc2)
  			printf("
  CRC split fail: 0x%08x
  ", crc2);
  	}
  
  	/* Now swap it around for the other test */
  
  	bytereverse(buf, len + 4);
906d66df1   Akinobu Mita   [PATCH] crc32: re...
415
416
417
  	init = bitrev32(init);
  	crc2 = bitrev32(crc1);
  	if (crc1 != bitrev32(crc2))
cfc646fa8   Dominik Hackl   [PATCH] crc32.c t...
418
419
420
  		printf("
  Bit reversal fail: 0x%08x -> 0x%08x -> 0x%08x
  ",
906d66df1   Akinobu Mita   [PATCH] crc32: re...
421
  		       crc1, crc2, bitrev32(crc2));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
  	crc1 = crc32_le(init, buf, len);
  	if (crc1 != crc2)
  		printf("
  CRC endianness fail: 0x%08x != 0x%08x
  ", crc1,
  		       crc2);
  	crc2 = crc32_le(init, buf, len + 4);
  	if (crc2)
  		printf("
  CRC cancellation fail: 0x%08x should be 0
  ",
  		       crc2);
  
  	for (i = 0; i <= len + 4; i++) {
  		crc2 = crc32_le(init, buf, i);
  		crc2 = crc32_le(crc2, buf + i, len + 4 - i);
  		if (crc2)
  			printf("
  CRC split fail: 0x%08x
  ", crc2);
  	}
  
  	return crc1;
  }
  
  #define SIZE 64
  #define INIT1 0
  #define INIT2 0
  
  int main(void)
  {
  	unsigned char buf1[SIZE + 4];
  	unsigned char buf2[SIZE + 4];
  	unsigned char buf3[SIZE + 4];
  	int i, j;
  	u32 crc1, crc2, crc3;
  
  	for (i = 0; i <= SIZE; i++) {
  		printf("\rTesting length %d...", i);
  		fflush(stdout);
  		random_garbage(buf1, i);
  		random_garbage(buf2, i);
  		for (j = 0; j < i; j++)
  			buf3[j] = buf1[j] ^ buf2[j];
  
  		crc1 = test_step(INIT1, buf1, i);
  		crc2 = test_step(INIT2, buf2, i);
  		/* Now check that CRC(buf1 ^ buf2) = CRC(buf1) ^ CRC(buf2) */
  		crc3 = test_step(INIT1 ^ INIT2, buf3, i);
  		if (crc3 != (crc1 ^ crc2))
  			printf("CRC XOR fail: 0x%08x != 0x%08x ^ 0x%08x
  ",
  			       crc3, crc1, crc2);
  	}
  	printf("
  All test complete.  No failures expected.
  ");
  	return 0;
  }
  
  #endif				/* UNITTEST */