Merge branch 'akpm'
[linux-2.6/next.git] / lib / crc32.c
blob07005ba65c4bd30ef7c3d07fe41147d1cd928ddf
1 /*
2 * July 20, 2011 Bob Pearson <rpearson at systemfabricworks.com>
3 * added slice by 8 algorithm to the existing conventional and
4 * slice by 4 algorithms.
6 * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
7 * Nicer crc32 functions/docs submitted by linux@horizon.com. Thanks!
8 * Code was from the public domain, copyright abandoned. Code was
9 * subsequently included in the kernel, thus was re-licensed under the
10 * GNU GPL v2.
12 * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
13 * Same crc32 function was used in 5 other places in the kernel.
14 * I made one version, and deleted the others.
15 * There are various incantations of crc32(). Some use a seed of 0 or ~0.
16 * Some xor at the end with ~0. The generic crc32() function takes
17 * seed as an argument, and doesn't xor at the end. Then individual
18 * users can do whatever they need.
19 * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
20 * fs/jffs2 uses seed 0, doesn't xor with ~0.
21 * fs/partitions/efi.c uses seed ~0, xor's with ~0.
23 * This source code is licensed under the GNU General Public License,
24 * Version 2. See the file COPYING for more details.
26 #include <linux/crc32.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/compiler.h>
30 #include <linux/types.h>
31 #include <linux/init.h>
32 #include <linux/atomic.h>
33 #include "crc32defs.h"
35 #if CRC_LE_BITS > 8
36 # define tole(x) (__force u32) __constant_cpu_to_le32(x)
37 #else
38 # define tole(x) (x)
39 #endif
41 #if CRC_BE_BITS > 8
42 # define tobe(x) (__force u32) __constant_cpu_to_be32(x)
43 #else
44 # define tobe(x) (x)
45 #endif
46 #include "crc32table.h"
48 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
49 MODULE_DESCRIPTION("Ethernet CRC32 calculations");
50 MODULE_LICENSE("GPL");
52 #if CRC_LE_BITS > 8
53 static inline u32 crc32_le_body(u32 crc, u8 const *buf, size_t len)
55 const u8 *p8;
56 const u32 *p32;
57 int init_bytes, end_bytes;
58 size_t words;
59 int i;
60 u32 q;
61 u8 i0, i1, i2, i3;
63 crc = (__force u32) __cpu_to_le32(crc);
65 #if CRC_LE_BITS == 64
66 p8 = buf;
67 p32 = (u32 *)(((uintptr_t)p8 + 7) & ~7);
69 init_bytes = (uintptr_t)p32 - (uintptr_t)p8;
70 if (init_bytes > len)
71 init_bytes = len;
72 words = (len - init_bytes) >> 3;
73 end_bytes = (len - init_bytes) & 7;
74 #else
75 p8 = buf;
76 p32 = (u32 *)(((uintptr_t)p8 + 3) & ~3);
78 init_bytes = (uintptr_t)p32 - (uintptr_t)p8;
79 if (init_bytes > len)
80 init_bytes = len;
81 words = (len - init_bytes) >> 2;
82 end_bytes = (len - init_bytes) & 3;
83 #endif
85 for (i = 0; i < init_bytes; i++) {
86 #ifdef __LITTLE_ENDIAN
87 i0 = *p8++ ^ crc;
88 crc = t0_le[i0] ^ (crc >> 8);
89 #else
90 i0 = *p8++ ^ (crc >> 24);
91 crc = t0_le[i0] ^ (crc << 8);
92 #endif
95 for (i = 0; i < words; i++) {
96 #ifdef __LITTLE_ENDIAN
97 # if CRC_LE_BITS == 64
98 /* slice by 8 algorithm */
99 q = *p32++ ^ crc;
100 i3 = q;
101 i2 = q >> 8;
102 i1 = q >> 16;
103 i0 = q >> 24;
104 crc = t7_le[i3] ^ t6_le[i2] ^ t5_le[i1] ^ t4_le[i0];
106 q = *p32++;
107 i3 = q;
108 i2 = q >> 8;
109 i1 = q >> 16;
110 i0 = q >> 24;
111 crc ^= t3_le[i3] ^ t2_le[i2] ^ t1_le[i1] ^ t0_le[i0];
112 # else
113 /* slice by 4 algorithm */
114 q = *p32++ ^ crc;
115 i3 = q;
116 i2 = q >> 8;
117 i1 = q >> 16;
118 i0 = q >> 24;
119 crc = t3_le[i3] ^ t2_le[i2] ^ t1_le[i1] ^ t0_le[i0];
120 # endif
121 #else
122 # if CRC_LE_BITS == 64
123 q = *p32++ ^ crc;
124 i3 = q >> 24;
125 i2 = q >> 16;
126 i1 = q >> 8;
127 i0 = q;
128 crc = t7_le[i3] ^ t6_le[i2] ^ t5_le[i1] ^ t4_le[i0];
130 q = *p32++;
131 i3 = q >> 24;
132 i2 = q >> 16;
133 i1 = q >> 8;
134 i0 = q;
135 crc ^= t3_le[i3] ^ t2_le[i2] ^ t1_le[i1] ^ t0_le[i0];
136 # else
137 q = *p32++ ^ crc;
138 i3 = q >> 24;
139 i2 = q >> 16;
140 i1 = q >> 8;
141 i0 = q;
142 crc = t3_le[i3] ^ t2_le[i2] ^ t1_le[i1] ^ t0_le[i0];
143 # endif
144 #endif
147 p8 = (u8 *)p32;
149 for (i = 0; i < end_bytes; i++) {
150 #ifdef __LITTLE_ENDIAN
151 i0 = *p8++ ^ crc;
152 crc = t0_le[i0] ^ (crc >> 8);
153 #else
154 i0 = *p8++ ^ (crc >> 24);
155 crc = t0_le[i0] ^ (crc << 8);
156 #endif
159 return __le32_to_cpu((__force __le32)crc);
161 #endif
163 #if CRC_BE_BITS > 8
164 static inline u32 crc32_be_body(u32 crc, u8 const *buf, size_t len)
166 const u8 *p8;
167 const u32 *p32;
168 int init_bytes, end_bytes;
169 size_t words;
170 int i;
171 u32 q;
172 u8 i0, i1, i2, i3;
174 crc = (__force u32) __cpu_to_be32(crc);
176 #if CRC_LE_BITS == 64
177 p8 = buf;
178 p32 = (u32 *)(((uintptr_t)p8 + 7) & ~7);
180 init_bytes = (uintptr_t)p32 - (uintptr_t)p8;
181 if (init_bytes > len)
182 init_bytes = len;
183 words = (len - init_bytes) >> 3;
184 end_bytes = (len - init_bytes) & 7;
185 #else
186 p8 = buf;
187 p32 = (u32 *)(((uintptr_t)p8 + 3) & ~3);
189 init_bytes = (uintptr_t)p32 - (uintptr_t)p8;
190 if (init_bytes > len)
191 init_bytes = len;
192 words = (len - init_bytes) >> 2;
193 end_bytes = (len - init_bytes) & 3;
194 #endif
196 for (i = 0; i < init_bytes; i++) {
197 #ifdef __LITTLE_ENDIAN
198 i0 = *p8++ ^ crc;
199 crc = t0_be[i0] ^ (crc >> 8);
200 #else
201 i0 = *p8++ ^ (crc >> 24);
202 crc = t0_be[i0] ^ (crc << 8);
203 #endif
206 for (i = 0; i < words; i++) {
207 #ifdef __LITTLE_ENDIAN
208 # if CRC_LE_BITS == 64
209 /* slice by 8 algorithm */
210 q = *p32++ ^ crc;
211 i3 = q;
212 i2 = q >> 8;
213 i1 = q >> 16;
214 i0 = q >> 24;
215 crc = t7_be[i3] ^ t6_be[i2] ^ t5_be[i1] ^ t4_be[i0];
217 q = *p32++;
218 i3 = q;
219 i2 = q >> 8;
220 i1 = q >> 16;
221 i0 = q >> 24;
222 crc ^= t3_be[i3] ^ t2_be[i2] ^ t1_be[i1] ^ t0_be[i0];
223 # else
224 /* slice by 4 algorithm */
225 q = *p32++ ^ crc;
226 i3 = q;
227 i2 = q >> 8;
228 i1 = q >> 16;
229 i0 = q >> 24;
230 crc = t3_be[i3] ^ t2_be[i2] ^ t1_be[i1] ^ t0_be[i0];
231 # endif
232 #else
233 # if CRC_LE_BITS == 64
234 q = *p32++ ^ crc;
235 i3 = q >> 24;
236 i2 = q >> 16;
237 i1 = q >> 8;
238 i0 = q;
239 crc = t7_be[i3] ^ t6_be[i2] ^ t5_be[i1] ^ t4_be[i0];
241 q = *p32++;
242 i3 = q >> 24;
243 i2 = q >> 16;
244 i1 = q >> 8;
245 i0 = q;
246 crc ^= t3_be[i3] ^ t2_be[i2] ^ t1_be[i1] ^ t0_be[i0];
247 # else
248 q = *p32++ ^ crc;
249 i3 = q >> 24;
250 i2 = q >> 16;
251 i1 = q >> 8;
252 i0 = q;
253 crc = t3_be[i3] ^ t2_be[i2] ^ t1_be[i1] ^ t0_be[i0];
254 # endif
255 #endif
258 p8 = (u8 *)p32;
260 for (i = 0; i < end_bytes; i++) {
261 #ifdef __LITTLE_ENDIAN
262 i0 = *p8++ ^ crc;
263 crc = t0_be[i0] ^ (crc >> 8);
264 #else
265 i0 = *p8++ ^ (crc >> 24);
266 crc = t0_be[i0] ^ (crc << 8);
267 #endif
270 return __be32_to_cpu((__force __be32)crc);
272 #endif
275 * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
276 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
277 * other uses, or the previous crc32 value if computing incrementally.
278 * @p: pointer to buffer over which CRC is run
279 * @len: length of buffer @p
281 u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
283 #if CRC_LE_BITS == 1
284 int i;
285 while (len--) {
286 crc ^= *p++;
287 for (i = 0; i < 8; i++)
288 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
290 # elif CRC_LE_BITS == 2
291 while (len--) {
292 crc ^= *p++;
293 crc = (crc >> 2) ^ t0_le[crc & 0x03];
294 crc = (crc >> 2) ^ t0_le[crc & 0x03];
295 crc = (crc >> 2) ^ t0_le[crc & 0x03];
296 crc = (crc >> 2) ^ t0_le[crc & 0x03];
298 # elif CRC_LE_BITS == 4
299 while (len--) {
300 crc ^= *p++;
301 crc = (crc >> 4) ^ t0_le[crc & 0x0f];
302 crc = (crc >> 4) ^ t0_le[crc & 0x0f];
304 # elif CRC_LE_BITS == 8
305 while (len--) {
306 crc ^= *p++;
307 crc = (crc >> 8) ^ t0_le[crc & 0xff];
309 # else
310 crc = crc32_le_body(crc, p, len);
311 # endif
312 return crc;
314 EXPORT_SYMBOL(crc32_le);
317 * crc32_be() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
318 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
319 * other uses, or the previous crc32 value if computing incrementally.
320 * @p: pointer to buffer over which CRC is run
321 * @len: length of buffer @p
323 u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
325 #if CRC_BE_BITS == 1
326 int i;
327 while (len--) {
328 crc ^= *p++ << 24;
329 for (i = 0; i < 8; i++)
330 crc = (crc << 1) ^
331 ((crc & 0x80000000) ? CRCPOLY_BE : 0);
333 # elif CRC_BE_BITS == 2
334 while (len--) {
335 crc ^= *p++ << 24;
336 crc = (crc << 2) ^ t0_be[crc >> 30];
337 crc = (crc << 2) ^ t0_be[crc >> 30];
338 crc = (crc << 2) ^ t0_be[crc >> 30];
339 crc = (crc << 2) ^ t0_be[crc >> 30];
341 # elif CRC_BE_BITS == 4
342 while (len--) {
343 crc ^= *p++ << 24;
344 crc = (crc << 4) ^ t0_be[crc >> 28];
345 crc = (crc << 4) ^ t0_be[crc >> 28];
347 # elif CRC_BE_BITS == 8
348 while (len--) {
349 crc ^= *p++ << 24;
350 crc = (crc << 8) ^ t0_be[crc >> 24];
352 # else
353 crc = crc32_be_body(crc, p, len);
354 # endif
355 return crc;
357 EXPORT_SYMBOL(crc32_be);
360 * A brief CRC tutorial.
362 * A CRC is a long-division remainder. You add the CRC to the message,
363 * and the whole thing (message+CRC) is a multiple of the given
364 * CRC polynomial. To check the CRC, you can either check that the
365 * CRC matches the recomputed value, *or* you can check that the
366 * remainder computed on the message+CRC is 0. This latter approach
367 * is used by a lot of hardware implementations, and is why so many
368 * protocols put the end-of-frame flag after the CRC.
370 * It's actually the same long division you learned in school, except that
371 * - We're working in binary, so the digits are only 0 and 1, and
372 * - When dividing polynomials, there are no carries. Rather than add and
373 * subtract, we just xor. Thus, we tend to get a bit sloppy about
374 * the difference between adding and subtracting.
376 * A 32-bit CRC polynomial is actually 33 bits long. But since it's
377 * 33 bits long, bit 32 is always going to be set, so usually the CRC
378 * is written in hex with the most significant bit omitted. (If you're
379 * familiar with the IEEE 754 floating-point format, it's the same idea.)
381 * Note that a CRC is computed over a string of *bits*, so you have
382 * to decide on the endianness of the bits within each byte. To get
383 * the best error-detecting properties, this should correspond to the
384 * order they're actually sent. For example, standard RS-232 serial is
385 * little-endian; the most significant bit (sometimes used for parity)
386 * is sent last. And when appending a CRC word to a message, you should
387 * do it in the right order, matching the endianness.
389 * Just like with ordinary division, the remainder is always smaller than
390 * the divisor (the CRC polynomial) you're dividing by. Each step of the
391 * division, you take one more digit (bit) of the dividend and append it
392 * to the current remainder. Then you figure out the appropriate multiple
393 * of the divisor to subtract to being the remainder back into range.
394 * In binary, it's easy - it has to be either 0 or 1, and to make the
395 * XOR cancel, it's just a copy of bit 32 of the remainder.
397 * When computing a CRC, we don't care about the quotient, so we can
398 * throw the quotient bit away, but subtract the appropriate multiple of
399 * the polynomial from the remainder and we're back to where we started,
400 * ready to process the next bit.
402 * A big-endian CRC written this way would be coded like:
403 * for (i = 0; i < input_bits; i++) {
404 * multiple = remainder & 0x80000000 ? CRCPOLY : 0;
405 * remainder = (remainder << 1 | next_input_bit()) ^ multiple;
407 * Notice how, to get at bit 32 of the shifted remainder, we look
408 * at bit 31 of the remainder *before* shifting it.
410 * But also notice how the next_input_bit() bits we're shifting into
411 * the remainder don't actually affect any decision-making until
412 * 32 bits later. Thus, the first 32 cycles of this are pretty boring.
413 * Also, to add the CRC to a message, we need a 32-bit-long hole for it at
414 * the end, so we have to add 32 extra cycles shifting in zeros at the
415 * end of every message,
417 * So the standard trick is to rearrage merging in the next_input_bit()
418 * until the moment it's needed. Then the first 32 cycles can be precomputed,
419 * and merging in the final 32 zero bits to make room for the CRC can be
420 * skipped entirely.
421 * This changes the code to:
422 * for (i = 0; i < input_bits; i++) {
423 * remainder ^= next_input_bit() << 31;
424 * multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
425 * remainder = (remainder << 1) ^ multiple;
427 * With this optimization, the little-endian code is simpler:
428 * for (i = 0; i < input_bits; i++) {
429 * remainder ^= next_input_bit();
430 * multiple = (remainder & 1) ? CRCPOLY : 0;
431 * remainder = (remainder >> 1) ^ multiple;
434 * Note that the other details of endianness have been hidden in CRCPOLY
435 * (which must be bit-reversed) and next_input_bit().
437 * However, as long as next_input_bit is returning the bits in a sensible
438 * order, we can actually do the merging 8 or more bits at a time rather
439 * than one bit at a time:
440 * for (i = 0; i < input_bytes; i++) {
441 * remainder ^= next_input_byte() << 24;
442 * for (j = 0; j < 8; j++) {
443 * multiple = (remainder & 0x80000000) ? CRCPOLY : 0;
444 * remainder = (remainder << 1) ^ multiple;
447 * Or in little-endian:
448 * for (i = 0; i < input_bytes; i++) {
449 * remainder ^= next_input_byte();
450 * for (j = 0; j < 8; j++) {
451 * multiple = (remainder & 1) ? CRCPOLY : 0;
452 * remainder = (remainder << 1) ^ multiple;
455 * If the input is a multiple of 32 bits, you can even XOR in a 32-bit
456 * word at a time and increase the inner loop count to 32.
458 * You can also mix and match the two loop styles, for example doing the
459 * bulk of a message byte-at-a-time and adding bit-at-a-time processing
460 * for any fractional bytes at the end.
462 * The only remaining optimization is to the byte-at-a-time table method.
463 * Here, rather than just shifting one bit of the remainder to decide
464 * in the correct multiple to subtract, we can shift a byte at a time.
465 * This produces a 40-bit (rather than a 33-bit) intermediate remainder,
466 * but again the multiple of the polynomial to subtract depends only on
467 * the high bits, the high 8 bits in this case.
469 * The multiple we need in that case is the low 32 bits of a 40-bit
470 * value whose high 8 bits are given, and which is a multiple of the
471 * generator polynomial. This is simply the CRC-32 of the given
472 * one-byte message.
474 * Two more details: normally, appending zero bits to a message which
475 * is already a multiple of a polynomial produces a larger multiple of that
476 * polynomial. To enable a CRC to detect this condition, it's common to
477 * invert the CRC before appending it. This makes the remainder of the
478 * message+crc come out not as zero, but some fixed non-zero value.
480 * The same problem applies to zero bits prepended to the message, and
481 * a similar solution is used. Instead of starting with a remainder of
482 * 0, an initial remainder of all ones is used. As long as you start
483 * the same way on decoding, it doesn't make a difference.
486 #ifdef UNITTEST
488 #include <stdlib.h>
489 #include <stdio.h>
491 #if 0 /*Not used at present */
492 static void
493 buf_dump(char const *prefix, unsigned char const *buf, size_t len)
495 fputs(prefix, stdout);
496 while (len--)
497 printf(" %02x", *buf++);
498 putchar('\n');
501 #endif
503 static void bytereverse(unsigned char *buf, size_t len)
505 while (len--) {
506 unsigned char x = bitrev8(*buf);
507 *buf++ = x;
511 static void random_garbage(unsigned char *buf, size_t len)
513 while (len--)
514 *buf++ = (unsigned char) random();
517 #if 0 /* Not used at present */
518 static void store_le(u32 x, unsigned char *buf)
520 buf[0] = (unsigned char) x;
521 buf[1] = (unsigned char) (x >> 8);
522 buf[2] = (unsigned char) (x >> 16);
523 buf[3] = (unsigned char) (x >> 24);
525 #endif
527 static void store_be(u32 x, unsigned char *buf)
529 buf[0] = (unsigned char) (x >> 24);
530 buf[1] = (unsigned char) (x >> 16);
531 buf[2] = (unsigned char) (x >> 8);
532 buf[3] = (unsigned char) x;
536 * This checks that CRC(buf + CRC(buf)) = 0, and that
537 * CRC commutes with bit-reversal. This has the side effect
538 * of bytewise bit-reversing the input buffer, and returns
539 * the CRC of the reversed buffer.
541 static u32 test_step(u32 init, unsigned char *buf, size_t len)
543 u32 crc1, crc2;
544 size_t i;
546 crc1 = crc32_be(init, buf, len);
547 store_be(crc1, buf + len);
548 crc2 = crc32_be(init, buf, len + 4);
549 if (crc2)
550 printf("\nCRC cancellation fail: 0x%08x should be 0\n",
551 crc2);
553 for (i = 0; i <= len + 4; i++) {
554 crc2 = crc32_be(init, buf, i);
555 crc2 = crc32_be(crc2, buf + i, len + 4 - i);
556 if (crc2)
557 printf("\nCRC split fail: 0x%08x\n", crc2);
560 /* Now swap it around for the other test */
562 bytereverse(buf, len + 4);
563 init = bitrev32(init);
564 crc2 = bitrev32(crc1);
565 if (crc1 != bitrev32(crc2))
566 printf("\nBit reversal fail: 0x%08x -> 0x%08x -> 0x%08x\n",
567 crc1, crc2, bitrev32(crc2));
568 crc1 = crc32_le(init, buf, len);
569 if (crc1 != crc2)
570 printf("\nCRC endianness fail: 0x%08x != 0x%08x\n", crc1,
571 crc2);
572 crc2 = crc32_le(init, buf, len + 4);
573 if (crc2)
574 printf("\nCRC cancellation fail: 0x%08x should be 0\n",
575 crc2);
577 for (i = 0; i <= len + 4; i++) {
578 crc2 = crc32_le(init, buf, i);
579 crc2 = crc32_le(crc2, buf + i, len + 4 - i);
580 if (crc2)
581 printf("\nCRC split fail: 0x%08x\n", crc2);
584 return crc1;
587 #define SIZE 64
588 #define INIT1 0
589 #define INIT2 0
591 int main(void)
593 unsigned char buf1[SIZE + 4];
594 unsigned char buf2[SIZE + 4];
595 unsigned char buf3[SIZE + 4];
596 int i, j;
597 u32 crc1, crc2, crc3;
599 for (i = 0; i <= SIZE; i++) {
600 printf("\rTesting length %d...", i);
601 fflush(stdout);
602 random_garbage(buf1, i);
603 random_garbage(buf2, i);
604 for (j = 0; j < i; j++)
605 buf3[j] = buf1[j] ^ buf2[j];
607 crc1 = test_step(INIT1, buf1, i);
608 crc2 = test_step(INIT2, buf2, i);
609 /* Now check that CRC(buf1 ^ buf2) = CRC(buf1) ^ CRC(buf2) */
610 crc3 = test_step(INIT1 ^ INIT2, buf3, i);
611 if (crc3 != (crc1 ^ crc2))
612 printf("CRC XOR fail: 0x%08x != 0x%08x ^ 0x%08x\n",
613 crc3, crc1, crc2);
615 printf("\nAll test complete. No failures expected.\n");
616 return 0;
619 #endif /* UNITTEST */