]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libfrog/crc32.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin
4 * cleaned up code to current version of sparse and added the slicing-by-8
5 * algorithm to the closely similar existing slicing-by-4 algorithm.
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
11 * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
12 * Same crc32 function was used in 5 other places in the kernel.
13 * I made one version, and deleted the others.
14 * There are various incantations of crc32(). Some use a seed of 0 or ~0.
15 * Some xor at the end with ~0. The generic crc32() function takes
16 * seed as an argument, and doesn't xor at the end. Then individual
17 * users can do whatever they need.
18 * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
19 * fs/jffs2 uses seed 0, doesn't xor with ~0.
20 * fs/partitions/efi.c uses seed ~0, xor's with ~0.
23 /* see: Documentation/crc32.txt for a description of algorithms */
26 * lifted from the 3.8-rc2 kernel source for xfsprogs. Killed CONFIG_X86
27 * specific bits for just the generic algorithm. Also removed the big endian
28 * version of the algorithm as XFS only uses the little endian CRC version to
29 * match the hardware acceleration available on Intel CPUs.
33 #include <asm/types.h>
35 #include "platform_defs.h"
36 /* For endian conversion routines */
38 #include "crc32defs.h"
41 /* types specifc to this file */
49 # define tole(x) ((__force u32) __constant_cpu_to_le32(x))
54 #include "crc32table.h"
58 /* implements slicing-by-4 or slicing-by-8 algorithm */
60 crc32_body(u32 crc
, unsigned char const *buf
, size_t len
, const u32 (*tab
)[256])
62 #if __BYTE_ORDER == __LITTLE_ENDIAN
63 # define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
64 # define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \
65 t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])
66 # define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \
67 t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])
68 # elif __BYTE_ORDER == __BIG_ENDIAN
69 # define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
70 # define DO_CRC4 (t0[(q) & 255] ^ t1[(q >> 8) & 255] ^ \
71 t2[(q >> 16) & 255] ^ t3[(q >> 24) & 255])
72 # define DO_CRC8 (t4[(q) & 255] ^ t5[(q >> 8) & 255] ^ \
73 t6[(q >> 16) & 255] ^ t7[(q >> 24) & 255])
75 # error What endian are you?
79 const u32
*t0
=tab
[0], *t1
=tab
[1], *t2
=tab
[2], *t3
=tab
[3];
80 # if CRC_LE_BITS != 32
81 const u32
*t4
= tab
[4], *t5
= tab
[5], *t6
= tab
[6], *t7
= tab
[7];
86 if (((long)buf
& 3) && len
) {
89 } while ((--len
) && ((long)buf
)&3);
92 # if CRC_LE_BITS == 32
100 b
= (const u32
*)buf
;
101 for (--b
; len
; --len
) {
102 q
= crc
^ *++b
; /* use pre increment for speed */
103 # if CRC_LE_BITS == 32
112 /* And the last few bytes */
114 u8
*p
= (u8
*)(b
+ 1) - 1;
116 DO_CRC(*++p
); /* use pre increment for speed */
127 * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
128 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
129 * other uses, or the previous crc32 value if computing incrementally.
130 * @p: pointer to buffer over which CRC is run
131 * @len: length of buffer @p
133 static inline u32 __pure
crc32_le_generic(u32 crc
, unsigned char const *p
,
134 size_t len
, const u32 (*tab
)[256],
141 for (i
= 0; i
< 8; i
++)
142 crc
= (crc
>> 1) ^ ((crc
& 1) ? polynomial
: 0);
144 # elif CRC_LE_BITS == 2
147 crc
= (crc
>> 2) ^ tab
[0][crc
& 3];
148 crc
= (crc
>> 2) ^ tab
[0][crc
& 3];
149 crc
= (crc
>> 2) ^ tab
[0][crc
& 3];
150 crc
= (crc
>> 2) ^ tab
[0][crc
& 3];
152 # elif CRC_LE_BITS == 4
155 crc
= (crc
>> 4) ^ tab
[0][crc
& 15];
156 crc
= (crc
>> 4) ^ tab
[0][crc
& 15];
158 # elif CRC_LE_BITS == 8
159 /* aka Sarwate algorithm */
162 crc
= (crc
>> 8) ^ tab
[0][crc
& 255];
165 crc
= (__force u32
) cpu_to_le32(crc
);
166 crc
= crc32_body(crc
, p
, len
, tab
);
167 crc
= le32_to_cpu((__force __le32
)crc
);
173 u32 __pure
crc32c_le(u32 crc
, unsigned char const *p
, size_t len
)
175 return crc32_le_generic(crc
, p
, len
, NULL
, CRC32C_POLY_LE
);
178 u32 __pure
crc32c_le(u32 crc
, unsigned char const *p
, size_t len
)
180 return crc32_le_generic(crc
, p
, len
,
181 (const u32 (*)[256])crc32ctable_le
, CRC32C_POLY_LE
);
186 #ifdef CRC32_SELFTEST
187 # include "crc32cselftest.h"
190 * make sure we always return 0 for a successful test run, and non-zero for a
191 * failed run. The build infrastructure is looking for this information to
192 * determine whether to allow the build to proceed.
194 int main(int argc
, char **argv
)
198 printf("CRC_LE_BITS = %d\n", CRC_LE_BITS
);
200 errors
= crc32c_test();
204 #endif /* CRC32_SELFTEST */