]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - lib/crc32.c
2 * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
3 * Nicer crc32 functions/docs submitted by linux@horizon.com. Thanks!
4 * Code was from the public domain, copyright abandoned. Code was
5 * subsequently included in the kernel, thus was re-licensed under the
8 * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
9 * Same crc32 function was used in 5 other places in the kernel.
10 * I made one version, and deleted the others.
11 * There are various incantations of crc32(). Some use a seed of 0 or ~0.
12 * Some xor at the end with ~0. The generic crc32() function takes
13 * seed as an argument, and doesn't xor at the end. Then individual
14 * users can do whatever they need.
15 * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
16 * fs/jffs2 uses seed 0, doesn't xor with ~0.
17 * fs/partitions/efi.c uses seed ~0, xor's with ~0.
19 * This source code is licensed under the GNU General Public License,
20 * Version 2. See the file COPYING for more details.
23 /* see: Documentation/crc32.txt for a description of algorithms */
25 #include <linux/crc32.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/compiler.h>
29 #include <linux/types.h>
30 #include <linux/init.h>
31 #include <linux/atomic.h>
32 #include "crc32defs.h"
34 # define tole(x) __constant_cpu_to_le32(x)
40 # define tobe(x) __constant_cpu_to_be32(x)
44 #include "crc32table.h"
46 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
47 MODULE_DESCRIPTION("Ethernet CRC32 calculations");
48 MODULE_LICENSE("GPL");
50 #if CRC_LE_BITS == 8 || CRC_BE_BITS == 8
53 crc32_body(u32 crc
, unsigned char const *buf
, size_t len
, const u32 (*tab
)[256])
55 # ifdef __LITTLE_ENDIAN
56 # define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
57 # define DO_CRC4 crc = t3[(crc) & 255] ^ \
58 t2[(crc >> 8) & 255] ^ \
59 t1[(crc >> 16) & 255] ^ \
62 # define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
63 # define DO_CRC4 crc = t0[(crc) & 255] ^ \
64 t1[(crc >> 8) & 255] ^ \
65 t2[(crc >> 16) & 255] ^ \
70 const u32
*t0
=tab
[0], *t1
=tab
[1], *t2
=tab
[2], *t3
=tab
[3];
73 if (unlikely((long)buf
& 3 && len
)) {
76 } while ((--len
) && ((long)buf
)&3);
79 /* load data 32 bits wide, xor data 32 bits wide. */
82 for (--b
; len
; --len
) {
83 crc
^= *++b
; /* use pre increment for speed */
87 /* And the last few bytes */
89 u8
*p
= (u8
*)(b
+ 1) - 1;
91 DO_CRC(*++p
); /* use pre increment for speed */
100 * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
101 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
102 * other uses, or the previous crc32 value if computing incrementally.
103 * @p: pointer to buffer over which CRC is run
104 * @len: length of buffer @p
106 u32 __pure
crc32_le(u32 crc
, unsigned char const *p
, size_t len
);
110 * In fact, the table-based code will work in this case, but it can be
111 * simplified by inlining the table in ?: form.
114 u32 __pure
crc32_le(u32 crc
, unsigned char const *p
, size_t len
)
119 for (i
= 0; i
< 8; i
++)
120 crc
= (crc
>> 1) ^ ((crc
& 1) ? CRCPOLY_LE
: 0);
124 #else /* Table-based approach */
126 u32 __pure
crc32_le(u32 crc
, unsigned char const *p
, size_t len
)
128 # if CRC_LE_BITS == 8
129 const u32 (*tab
)[] = crc32table_le
;
131 crc
= __cpu_to_le32(crc
);
132 crc
= crc32_body(crc
, p
, len
, tab
);
133 return __le32_to_cpu(crc
);
134 # elif CRC_LE_BITS == 4
137 crc
= (crc
>> 4) ^ crc32table_le
[crc
& 15];
138 crc
= (crc
>> 4) ^ crc32table_le
[crc
& 15];
141 # elif CRC_LE_BITS == 2
144 crc
= (crc
>> 2) ^ crc32table_le
[crc
& 3];
145 crc
= (crc
>> 2) ^ crc32table_le
[crc
& 3];
146 crc
= (crc
>> 2) ^ crc32table_le
[crc
& 3];
147 crc
= (crc
>> 2) ^ crc32table_le
[crc
& 3];
155 * crc32_be() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
156 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
157 * other uses, or the previous crc32 value if computing incrementally.
158 * @p: pointer to buffer over which CRC is run
159 * @len: length of buffer @p
161 u32 __pure
crc32_be(u32 crc
, unsigned char const *p
, size_t len
);
165 * In fact, the table-based code will work in this case, but it can be
166 * simplified by inlining the table in ?: form.
169 u32 __pure
crc32_be(u32 crc
, unsigned char const *p
, size_t len
)
174 for (i
= 0; i
< 8; i
++)
176 (crc
<< 1) ^ ((crc
& 0x80000000) ? CRCPOLY_BE
:
182 #else /* Table-based approach */
183 u32 __pure
crc32_be(u32 crc
, unsigned char const *p
, size_t len
)
185 # if CRC_BE_BITS == 8
186 const u32 (*tab
)[] = crc32table_be
;
188 crc
= __cpu_to_be32(crc
);
189 crc
= crc32_body(crc
, p
, len
, tab
);
190 return __be32_to_cpu(crc
);
191 # elif CRC_BE_BITS == 4
194 crc
= (crc
<< 4) ^ crc32table_be
[crc
>> 28];
195 crc
= (crc
<< 4) ^ crc32table_be
[crc
>> 28];
198 # elif CRC_BE_BITS == 2
201 crc
= (crc
<< 2) ^ crc32table_be
[crc
>> 30];
202 crc
= (crc
<< 2) ^ crc32table_be
[crc
>> 30];
203 crc
= (crc
<< 2) ^ crc32table_be
[crc
>> 30];
204 crc
= (crc
<< 2) ^ crc32table_be
[crc
>> 30];
211 EXPORT_SYMBOL(crc32_le
);
212 EXPORT_SYMBOL(crc32_be
);
219 #if 0 /*Not used at present */
221 buf_dump(char const *prefix
, unsigned char const *buf
, size_t len
)
223 fputs(prefix
, stdout
);
225 printf(" %02x", *buf
++);
231 static void bytereverse(unsigned char *buf
, size_t len
)
234 unsigned char x
= bitrev8(*buf
);
239 static void random_garbage(unsigned char *buf
, size_t len
)
242 *buf
++ = (unsigned char) random();
245 #if 0 /* Not used at present */
246 static void store_le(u32 x
, unsigned char *buf
)
248 buf
[0] = (unsigned char) x
;
249 buf
[1] = (unsigned char) (x
>> 8);
250 buf
[2] = (unsigned char) (x
>> 16);
251 buf
[3] = (unsigned char) (x
>> 24);
255 static void store_be(u32 x
, unsigned char *buf
)
257 buf
[0] = (unsigned char) (x
>> 24);
258 buf
[1] = (unsigned char) (x
>> 16);
259 buf
[2] = (unsigned char) (x
>> 8);
260 buf
[3] = (unsigned char) x
;
264 * This checks that CRC(buf + CRC(buf)) = 0, and that
265 * CRC commutes with bit-reversal. This has the side effect
266 * of bytewise bit-reversing the input buffer, and returns
267 * the CRC of the reversed buffer.
269 static u32
test_step(u32 init
, unsigned char *buf
, size_t len
)
274 crc1
= crc32_be(init
, buf
, len
);
275 store_be(crc1
, buf
+ len
);
276 crc2
= crc32_be(init
, buf
, len
+ 4);
278 printf("\nCRC cancellation fail: 0x%08x should be 0\n",
281 for (i
= 0; i
<= len
+ 4; i
++) {
282 crc2
= crc32_be(init
, buf
, i
);
283 crc2
= crc32_be(crc2
, buf
+ i
, len
+ 4 - i
);
285 printf("\nCRC split fail: 0x%08x\n", crc2
);
288 /* Now swap it around for the other test */
290 bytereverse(buf
, len
+ 4);
291 init
= bitrev32(init
);
292 crc2
= bitrev32(crc1
);
293 if (crc1
!= bitrev32(crc2
))
294 printf("\nBit reversal fail: 0x%08x -> 0x%08x -> 0x%08x\n",
295 crc1
, crc2
, bitrev32(crc2
));
296 crc1
= crc32_le(init
, buf
, len
);
298 printf("\nCRC endianness fail: 0x%08x != 0x%08x\n", crc1
,
300 crc2
= crc32_le(init
, buf
, len
+ 4);
302 printf("\nCRC cancellation fail: 0x%08x should be 0\n",
305 for (i
= 0; i
<= len
+ 4; i
++) {
306 crc2
= crc32_le(init
, buf
, i
);
307 crc2
= crc32_le(crc2
, buf
+ i
, len
+ 4 - i
);
309 printf("\nCRC split fail: 0x%08x\n", crc2
);
321 unsigned char buf1
[SIZE
+ 4];
322 unsigned char buf2
[SIZE
+ 4];
323 unsigned char buf3
[SIZE
+ 4];
325 u32 crc1
, crc2
, crc3
;
327 for (i
= 0; i
<= SIZE
; i
++) {
328 printf("\rTesting length %d...", i
);
330 random_garbage(buf1
, i
);
331 random_garbage(buf2
, i
);
332 for (j
= 0; j
< i
; j
++)
333 buf3
[j
] = buf1
[j
] ^ buf2
[j
];
335 crc1
= test_step(INIT1
, buf1
, i
);
336 crc2
= test_step(INIT2
, buf2
, i
);
337 /* Now check that CRC(buf1 ^ buf2) = CRC(buf1) ^ CRC(buf2) */
338 crc3
= test_step(INIT1
^ INIT2
, buf3
, i
);
339 if (crc3
!= (crc1
^ crc2
))
340 printf("CRC XOR fail: 0x%08x != 0x%08x ^ 0x%08x\n",
343 printf("\nAll test complete. No failures expected.\n");
347 #endif /* UNITTEST */