1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin
3*4882a593Smuzhiyun * cleaned up code to current version of sparse and added the slicing-by-8
4*4882a593Smuzhiyun * algorithm to the closely similar existing slicing-by-4 algorithm.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
7*4882a593Smuzhiyun * Nicer crc32 functions/docs submitted by linux@horizon.com. Thanks!
8*4882a593Smuzhiyun * Code was from the public domain, copyright abandoned. Code was
9*4882a593Smuzhiyun * subsequently included in the kernel, thus was re-licensed under the
10*4882a593Smuzhiyun * GNU GPL v2.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
13*4882a593Smuzhiyun * Same crc32 function was used in 5 other places in the kernel.
14*4882a593Smuzhiyun * I made one version, and deleted the others.
15*4882a593Smuzhiyun * There are various incantations of crc32(). Some use a seed of 0 or ~0.
16*4882a593Smuzhiyun * Some xor at the end with ~0. The generic crc32() function takes
17*4882a593Smuzhiyun * seed as an argument, and doesn't xor at the end. Then individual
18*4882a593Smuzhiyun * users can do whatever they need.
19*4882a593Smuzhiyun * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
20*4882a593Smuzhiyun * fs/jffs2 uses seed 0, doesn't xor with ~0.
21*4882a593Smuzhiyun * fs/partitions/efi.c uses seed ~0, xor's with ~0.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * This source code is licensed under the GNU General Public License,
24*4882a593Smuzhiyun * Version 2. See the file COPYING for more details.
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /* see: Documentation/staging/crc32.rst for a description of algorithms */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <linux/crc32.h>
30*4882a593Smuzhiyun #include <linux/crc32poly.h>
31*4882a593Smuzhiyun #include <linux/module.h>
32*4882a593Smuzhiyun #include <linux/types.h>
33*4882a593Smuzhiyun #include <linux/sched.h>
34*4882a593Smuzhiyun #include "crc32defs.h"
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #if CRC_LE_BITS > 8
37*4882a593Smuzhiyun # define tole(x) ((__force u32) cpu_to_le32(x))
38*4882a593Smuzhiyun #else
39*4882a593Smuzhiyun # define tole(x) (x)
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #if CRC_BE_BITS > 8
43*4882a593Smuzhiyun # define tobe(x) ((__force u32) cpu_to_be32(x))
44*4882a593Smuzhiyun #else
45*4882a593Smuzhiyun # define tobe(x) (x)
46*4882a593Smuzhiyun #endif
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #include "crc32table.h"
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
51*4882a593Smuzhiyun MODULE_DESCRIPTION("Various CRC32 calculations");
52*4882a593Smuzhiyun MODULE_LICENSE("GPL");
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun #if CRC_LE_BITS > 8 || CRC_BE_BITS > 8
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /* implements slicing-by-4 or slicing-by-8 algorithm */
57*4882a593Smuzhiyun static inline u32 __pure
crc32_body(u32 crc,unsigned char const * buf,size_t len,const u32 (* tab)[256])58*4882a593Smuzhiyun crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun # ifdef __LITTLE_ENDIAN
61*4882a593Smuzhiyun # define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
62*4882a593Smuzhiyun # define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \
63*4882a593Smuzhiyun t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])
64*4882a593Smuzhiyun # define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \
65*4882a593Smuzhiyun t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])
66*4882a593Smuzhiyun # else
67*4882a593Smuzhiyun # define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
68*4882a593Smuzhiyun # define DO_CRC4 (t0[(q) & 255] ^ t1[(q >> 8) & 255] ^ \
69*4882a593Smuzhiyun t2[(q >> 16) & 255] ^ t3[(q >> 24) & 255])
70*4882a593Smuzhiyun # define DO_CRC8 (t4[(q) & 255] ^ t5[(q >> 8) & 255] ^ \
71*4882a593Smuzhiyun t6[(q >> 16) & 255] ^ t7[(q >> 24) & 255])
72*4882a593Smuzhiyun # endif
73*4882a593Smuzhiyun const u32 *b;
74*4882a593Smuzhiyun size_t rem_len;
75*4882a593Smuzhiyun # ifdef CONFIG_X86
76*4882a593Smuzhiyun size_t i;
77*4882a593Smuzhiyun # endif
78*4882a593Smuzhiyun const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
79*4882a593Smuzhiyun # if CRC_LE_BITS != 32
80*4882a593Smuzhiyun const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7];
81*4882a593Smuzhiyun # endif
82*4882a593Smuzhiyun u32 q;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /* Align it */
85*4882a593Smuzhiyun if (unlikely((long)buf & 3 && len)) {
86*4882a593Smuzhiyun do {
87*4882a593Smuzhiyun DO_CRC(*buf++);
88*4882a593Smuzhiyun } while ((--len) && ((long)buf)&3);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun # if CRC_LE_BITS == 32
92*4882a593Smuzhiyun rem_len = len & 3;
93*4882a593Smuzhiyun len = len >> 2;
94*4882a593Smuzhiyun # else
95*4882a593Smuzhiyun rem_len = len & 7;
96*4882a593Smuzhiyun len = len >> 3;
97*4882a593Smuzhiyun # endif
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun b = (const u32 *)buf;
100*4882a593Smuzhiyun # ifdef CONFIG_X86
101*4882a593Smuzhiyun --b;
102*4882a593Smuzhiyun for (i = 0; i < len; i++) {
103*4882a593Smuzhiyun # else
104*4882a593Smuzhiyun for (--b; len; --len) {
105*4882a593Smuzhiyun # endif
106*4882a593Smuzhiyun q = crc ^ *++b; /* use pre increment for speed */
107*4882a593Smuzhiyun # if CRC_LE_BITS == 32
108*4882a593Smuzhiyun crc = DO_CRC4;
109*4882a593Smuzhiyun # else
110*4882a593Smuzhiyun crc = DO_CRC8;
111*4882a593Smuzhiyun q = *++b;
112*4882a593Smuzhiyun crc ^= DO_CRC4;
113*4882a593Smuzhiyun # endif
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun len = rem_len;
116*4882a593Smuzhiyun /* And the last few bytes */
117*4882a593Smuzhiyun if (len) {
118*4882a593Smuzhiyun u8 *p = (u8 *)(b + 1) - 1;
119*4882a593Smuzhiyun # ifdef CONFIG_X86
120*4882a593Smuzhiyun for (i = 0; i < len; i++)
121*4882a593Smuzhiyun DO_CRC(*++p); /* use pre increment for speed */
122*4882a593Smuzhiyun # else
123*4882a593Smuzhiyun do {
124*4882a593Smuzhiyun DO_CRC(*++p); /* use pre increment for speed */
125*4882a593Smuzhiyun } while (--len);
126*4882a593Smuzhiyun # endif
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun return crc;
129*4882a593Smuzhiyun #undef DO_CRC
130*4882a593Smuzhiyun #undef DO_CRC4
131*4882a593Smuzhiyun #undef DO_CRC8
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun #endif
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun * crc32_le_generic() - Calculate bitwise little-endian Ethernet AUTODIN II
138*4882a593Smuzhiyun * CRC32/CRC32C
139*4882a593Smuzhiyun * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for other
140*4882a593Smuzhiyun * uses, or the previous crc32/crc32c value if computing incrementally.
141*4882a593Smuzhiyun * @p: pointer to buffer over which CRC32/CRC32C is run
142*4882a593Smuzhiyun * @len: length of buffer @p
143*4882a593Smuzhiyun * @tab: little-endian Ethernet table
144*4882a593Smuzhiyun * @polynomial: CRC32/CRC32c LE polynomial
145*4882a593Smuzhiyun */
146*4882a593Smuzhiyun static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
147*4882a593Smuzhiyun size_t len, const u32 (*tab)[256],
148*4882a593Smuzhiyun u32 polynomial)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun #if CRC_LE_BITS == 1
151*4882a593Smuzhiyun int i;
152*4882a593Smuzhiyun while (len--) {
153*4882a593Smuzhiyun crc ^= *p++;
154*4882a593Smuzhiyun for (i = 0; i < 8; i++)
155*4882a593Smuzhiyun crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun # elif CRC_LE_BITS == 2
158*4882a593Smuzhiyun while (len--) {
159*4882a593Smuzhiyun crc ^= *p++;
160*4882a593Smuzhiyun crc = (crc >> 2) ^ tab[0][crc & 3];
161*4882a593Smuzhiyun crc = (crc >> 2) ^ tab[0][crc & 3];
162*4882a593Smuzhiyun crc = (crc >> 2) ^ tab[0][crc & 3];
163*4882a593Smuzhiyun crc = (crc >> 2) ^ tab[0][crc & 3];
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun # elif CRC_LE_BITS == 4
166*4882a593Smuzhiyun while (len--) {
167*4882a593Smuzhiyun crc ^= *p++;
168*4882a593Smuzhiyun crc = (crc >> 4) ^ tab[0][crc & 15];
169*4882a593Smuzhiyun crc = (crc >> 4) ^ tab[0][crc & 15];
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun # elif CRC_LE_BITS == 8
172*4882a593Smuzhiyun /* aka Sarwate algorithm */
173*4882a593Smuzhiyun while (len--) {
174*4882a593Smuzhiyun crc ^= *p++;
175*4882a593Smuzhiyun crc = (crc >> 8) ^ tab[0][crc & 255];
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun # else
178*4882a593Smuzhiyun crc = (__force u32) __cpu_to_le32(crc);
179*4882a593Smuzhiyun crc = crc32_body(crc, p, len, tab);
180*4882a593Smuzhiyun crc = __le32_to_cpu((__force __le32)crc);
181*4882a593Smuzhiyun #endif
182*4882a593Smuzhiyun return crc;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun #if CRC_LE_BITS == 1
186*4882a593Smuzhiyun u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun return crc32_le_generic(crc, p, len, NULL, CRC32_POLY_LE);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun #else
195*4882a593Smuzhiyun u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun return crc32_le_generic(crc, p, len,
198*4882a593Smuzhiyun (const u32 (*)[256])crc32table_le, CRC32_POLY_LE);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun return crc32_le_generic(crc, p, len,
203*4882a593Smuzhiyun (const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun #endif
206*4882a593Smuzhiyun EXPORT_SYMBOL(crc32_le);
207*4882a593Smuzhiyun EXPORT_SYMBOL(__crc32c_le);
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
210*4882a593Smuzhiyun u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /*
213*4882a593Smuzhiyun * This multiplies the polynomials x and y modulo the given modulus.
214*4882a593Smuzhiyun * This follows the "little-endian" CRC convention that the lsbit
215*4882a593Smuzhiyun * represents the highest power of x, and the msbit represents x^0.
216*4882a593Smuzhiyun */
217*4882a593Smuzhiyun static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun u32 product = x & 1 ? y : 0;
220*4882a593Smuzhiyun int i;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun for (i = 0; i < 31; i++) {
223*4882a593Smuzhiyun product = (product >> 1) ^ (product & 1 ? modulus : 0);
224*4882a593Smuzhiyun x >>= 1;
225*4882a593Smuzhiyun product ^= x & 1 ? y : 0;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun return product;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time
233*4882a593Smuzhiyun * @crc: The original little-endian CRC (i.e. lsbit is x^31 coefficient)
234*4882a593Smuzhiyun * @len: The number of bytes. @crc is multiplied by x^(8*@len)
235*4882a593Smuzhiyun * @polynomial: The modulus used to reduce the result to 32 bits.
236*4882a593Smuzhiyun *
237*4882a593Smuzhiyun * It's possible to parallelize CRC computations by computing a CRC
238*4882a593Smuzhiyun * over separate ranges of a buffer, then summing them.
239*4882a593Smuzhiyun * This shifts the given CRC by 8*len bits (i.e. produces the same effect
240*4882a593Smuzhiyun * as appending len bytes of zero to the data), in time proportional
241*4882a593Smuzhiyun * to log(len).
242*4882a593Smuzhiyun */
243*4882a593Smuzhiyun static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len,
244*4882a593Smuzhiyun u32 polynomial)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun u32 power = polynomial; /* CRC of x^32 */
247*4882a593Smuzhiyun int i;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun /* Shift up to 32 bits in the simple linear way */
250*4882a593Smuzhiyun for (i = 0; i < 8 * (int)(len & 3); i++)
251*4882a593Smuzhiyun crc = (crc >> 1) ^ (crc & 1 ? polynomial : 0);
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun len >>= 2;
254*4882a593Smuzhiyun if (!len)
255*4882a593Smuzhiyun return crc;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun for (;;) {
258*4882a593Smuzhiyun /* "power" is x^(2^i), modulo the polynomial */
259*4882a593Smuzhiyun if (len & 1)
260*4882a593Smuzhiyun crc = gf2_multiply(crc, power, polynomial);
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun len >>= 1;
263*4882a593Smuzhiyun if (!len)
264*4882a593Smuzhiyun break;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /* Square power, advancing to x^(2^(i+1)) */
267*4882a593Smuzhiyun power = gf2_multiply(power, power, polynomial);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun return crc;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun return crc32_generic_shift(crc, len, CRC32_POLY_LE);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun return crc32_generic_shift(crc, len, CRC32C_POLY_LE);
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun EXPORT_SYMBOL(crc32_le_shift);
283*4882a593Smuzhiyun EXPORT_SYMBOL(__crc32c_le_shift);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /**
286*4882a593Smuzhiyun * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
287*4882a593Smuzhiyun * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
288*4882a593Smuzhiyun * other uses, or the previous crc32 value if computing incrementally.
289*4882a593Smuzhiyun * @p: pointer to buffer over which CRC32 is run
290*4882a593Smuzhiyun * @len: length of buffer @p
291*4882a593Smuzhiyun * @tab: big-endian Ethernet table
292*4882a593Smuzhiyun * @polynomial: CRC32 BE polynomial
293*4882a593Smuzhiyun */
294*4882a593Smuzhiyun static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
295*4882a593Smuzhiyun size_t len, const u32 (*tab)[256],
296*4882a593Smuzhiyun u32 polynomial)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun #if CRC_BE_BITS == 1
299*4882a593Smuzhiyun int i;
300*4882a593Smuzhiyun while (len--) {
301*4882a593Smuzhiyun crc ^= *p++ << 24;
302*4882a593Smuzhiyun for (i = 0; i < 8; i++)
303*4882a593Smuzhiyun crc =
304*4882a593Smuzhiyun (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
305*4882a593Smuzhiyun 0);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun # elif CRC_BE_BITS == 2
308*4882a593Smuzhiyun while (len--) {
309*4882a593Smuzhiyun crc ^= *p++ << 24;
310*4882a593Smuzhiyun crc = (crc << 2) ^ tab[0][crc >> 30];
311*4882a593Smuzhiyun crc = (crc << 2) ^ tab[0][crc >> 30];
312*4882a593Smuzhiyun crc = (crc << 2) ^ tab[0][crc >> 30];
313*4882a593Smuzhiyun crc = (crc << 2) ^ tab[0][crc >> 30];
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun # elif CRC_BE_BITS == 4
316*4882a593Smuzhiyun while (len--) {
317*4882a593Smuzhiyun crc ^= *p++ << 24;
318*4882a593Smuzhiyun crc = (crc << 4) ^ tab[0][crc >> 28];
319*4882a593Smuzhiyun crc = (crc << 4) ^ tab[0][crc >> 28];
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun # elif CRC_BE_BITS == 8
322*4882a593Smuzhiyun while (len--) {
323*4882a593Smuzhiyun crc ^= *p++ << 24;
324*4882a593Smuzhiyun crc = (crc << 8) ^ tab[0][crc >> 24];
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun # else
327*4882a593Smuzhiyun crc = (__force u32) __cpu_to_be32(crc);
328*4882a593Smuzhiyun crc = crc32_body(crc, p, len, tab);
329*4882a593Smuzhiyun crc = __be32_to_cpu((__force __be32)crc);
330*4882a593Smuzhiyun # endif
331*4882a593Smuzhiyun return crc;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun #if CRC_BE_BITS == 1
335*4882a593Smuzhiyun u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE);
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun #else
340*4882a593Smuzhiyun u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun return crc32_be_generic(crc, p, len,
343*4882a593Smuzhiyun (const u32 (*)[256])crc32table_be, CRC32_POLY_BE);
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun #endif
346*4882a593Smuzhiyun EXPORT_SYMBOL(crc32_be);
347