blob: 2ef20fe84b69056b345467f1dd17b3d9c705c1b0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bob Pearson78dff412012-03-23 15:02:24 -07002 * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin
3 * cleaned up code to current version of sparse and added the slicing-by-8
4 * algorithm to the closely similar existing slicing-by-4 algorithm.
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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.
11 *
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.
22 *
23 * This source code is licensed under the GNU General Public License,
24 * Version 2. See the file COPYING for more details.
25 */
26
Bob Pearsonfbedceb2012-03-23 15:02:22 -070027/* see: Documentation/crc32.txt for a description of algorithms */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/crc32.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/types.h>
Daniel Borkmanncc0ac192013-11-04 17:10:26 +010032#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "crc32defs.h"
Bob Pearson60e58d52012-03-23 15:02:22 -070034
Bob Pearson9a1dbf62012-03-23 15:02:23 -070035#if CRC_LE_BITS > 8
Fabian Frederick38b4fe52014-06-04 16:11:56 -070036# define tole(x) ((__force u32) cpu_to_le32(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#else
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080038# define tole(x) (x)
39#endif
40
Bob Pearson9a1dbf62012-03-23 15:02:23 -070041#if CRC_BE_BITS > 8
Fabian Frederick38b4fe52014-06-04 16:11:56 -070042# define tobe(x) ((__force u32) cpu_to_be32(x))
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080043#else
44# define tobe(x) (x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#endif
Bob Pearson60e58d52012-03-23 15:02:22 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "crc32table.h"
48
49MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
Darrick J. Wong46c58012012-03-23 15:02:25 -070050MODULE_DESCRIPTION("Various CRC32 calculations");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051MODULE_LICENSE("GPL");
52
Bob Pearson9a1dbf62012-03-23 15:02:23 -070053#if CRC_LE_BITS > 8 || CRC_BE_BITS > 8
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080054
Bob Pearson324eb0f2012-03-23 15:02:24 -070055/* implements slicing-by-4 or slicing-by-8 algorithm */
George Spelvind8f1c472014-06-23 15:11:56 +020056static inline u32 __pure
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070057crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080058{
Andrew Morton0d2daf52010-05-25 23:43:03 -070059# ifdef __LITTLE_ENDIAN
Joakim Tjernlund57423322012-01-10 15:10:18 -080060# define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
Bob Pearson324eb0f2012-03-23 15:02:24 -070061# define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \
62 t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])
63# define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \
64 t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080065# else
Joakim Tjernlund57423322012-01-10 15:10:18 -080066# define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
Bob Pearson324eb0f2012-03-23 15:02:24 -070067# define DO_CRC4 (t0[(q) & 255] ^ t1[(q >> 8) & 255] ^ \
68 t2[(q >> 16) & 255] ^ t3[(q >> 24) & 255])
69# define DO_CRC8 (t4[(q) & 255] ^ t5[(q >> 8) & 255] ^ \
70 t6[(q >> 16) & 255] ^ t7[(q >> 24) & 255])
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080071# endif
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080072 const u32 *b;
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080073 size_t rem_len;
Bob Pearson0292c492012-03-23 15:02:24 -070074# ifdef CONFIG_X86
75 size_t i;
76# endif
Joakim Tjernlund57423322012-01-10 15:10:18 -080077 const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
Thiago Rafael Becker49ac5722012-07-30 14:41:26 -070078# if CRC_LE_BITS != 32
Bob Pearson324eb0f2012-03-23 15:02:24 -070079 const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7];
Thiago Rafael Becker49ac5722012-07-30 14:41:26 -070080# endif
Bob Pearson324eb0f2012-03-23 15:02:24 -070081 u32 q;
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080082
83 /* Align it */
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080084 if (unlikely((long)buf & 3 && len)) {
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080085 do {
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080086 DO_CRC(*buf++);
87 } while ((--len) && ((long)buf)&3);
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080088 }
Bob Pearson324eb0f2012-03-23 15:02:24 -070089
90# if CRC_LE_BITS == 32
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080091 rem_len = len & 3;
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080092 len = len >> 2;
Bob Pearson324eb0f2012-03-23 15:02:24 -070093# else
94 rem_len = len & 7;
95 len = len >> 3;
96# endif
97
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080098 b = (const u32 *)buf;
Bob Pearson0292c492012-03-23 15:02:24 -070099# ifdef CONFIG_X86
100 --b;
101 for (i = 0; i < len; i++) {
102# else
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800103 for (--b; len; --len) {
Bob Pearson0292c492012-03-23 15:02:24 -0700104# endif
Bob Pearson324eb0f2012-03-23 15:02:24 -0700105 q = crc ^ *++b; /* use pre increment for speed */
106# if CRC_LE_BITS == 32
107 crc = DO_CRC4;
108# else
109 crc = DO_CRC8;
110 q = *++b;
111 crc ^= DO_CRC4;
112# endif
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800113 }
114 len = rem_len;
115 /* And the last few bytes */
116 if (len) {
117 u8 *p = (u8 *)(b + 1) - 1;
Bob Pearson0292c492012-03-23 15:02:24 -0700118# ifdef CONFIG_X86
119 for (i = 0; i < len; i++)
120 DO_CRC(*++p); /* use pre increment for speed */
121# else
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800122 do {
123 DO_CRC(*++p); /* use pre increment for speed */
124 } while (--len);
Bob Pearson0292c492012-03-23 15:02:24 -0700125# endif
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800126 }
127 return crc;
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -0800128#undef DO_CRC
Joakim Tjernlund836e2af2010-05-24 14:33:31 -0700129#undef DO_CRC4
Bob Pearson324eb0f2012-03-23 15:02:24 -0700130#undef DO_CRC8
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800131}
132#endif
Bob Pearson60e58d52012-03-23 15:02:22 -0700133
Daniel Borkmann6e95fca2013-10-30 11:50:49 +0100134
Randy Dunlap2f721002006-06-25 05:48:59 -0700135/**
Gu Zhengf2e1d2a2013-09-11 14:23:52 -0700136 * crc32_le_generic() - Calculate bitwise little-endian Ethernet AUTODIN II
137 * CRC32/CRC32C
138 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for other
139 * uses, or the previous crc32/crc32c value if computing incrementally.
140 * @p: pointer to buffer over which CRC32/CRC32C is run
Randy Dunlap2f721002006-06-25 05:48:59 -0700141 * @len: length of buffer @p
Gu Zhengf2e1d2a2013-09-11 14:23:52 -0700142 * @tab: little-endian Ethernet table
143 * @polynomial: CRC32/CRC32c LE polynomial
Randy Dunlap2f721002006-06-25 05:48:59 -0700144 */
Darrick J. Wong46c58012012-03-23 15:02:25 -0700145static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
146 size_t len, const u32 (*tab)[256],
147 u32 polynomial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Bob Pearson60e58d52012-03-23 15:02:22 -0700149#if CRC_LE_BITS == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int i;
151 while (len--) {
152 crc ^= *p++;
153 for (i = 0; i < 8; i++)
Darrick J. Wong46c58012012-03-23 15:02:25 -0700154 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
Bob Pearson60e58d52012-03-23 15:02:22 -0700156# elif CRC_LE_BITS == 2
157 while (len--) {
158 crc ^= *p++;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700159 crc = (crc >> 2) ^ tab[0][crc & 3];
160 crc = (crc >> 2) ^ tab[0][crc & 3];
161 crc = (crc >> 2) ^ tab[0][crc & 3];
162 crc = (crc >> 2) ^ tab[0][crc & 3];
Bob Pearson60e58d52012-03-23 15:02:22 -0700163 }
164# elif CRC_LE_BITS == 4
165 while (len--) {
166 crc ^= *p++;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700167 crc = (crc >> 4) ^ tab[0][crc & 15];
168 crc = (crc >> 4) ^ tab[0][crc & 15];
Bob Pearson60e58d52012-03-23 15:02:22 -0700169 }
170# elif CRC_LE_BITS == 8
Bob Pearson9a1dbf62012-03-23 15:02:23 -0700171 /* aka Sarwate algorithm */
172 while (len--) {
173 crc ^= *p++;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700174 crc = (crc >> 8) ^ tab[0][crc & 255];
Bob Pearson9a1dbf62012-03-23 15:02:23 -0700175 }
176# else
Bob Pearsonce4320d2012-03-23 15:02:23 -0700177 crc = (__force u32) __cpu_to_le32(crc);
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800178 crc = crc32_body(crc, p, len, tab);
Bob Pearsonce4320d2012-03-23 15:02:23 -0700179 crc = __le32_to_cpu((__force __le32)crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#endif
Bob Pearson60e58d52012-03-23 15:02:22 -0700181 return crc;
182}
Darrick J. Wong46c58012012-03-23 15:02:25 -0700183
184#if CRC_LE_BITS == 1
185u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
186{
187 return crc32_le_generic(crc, p, len, NULL, CRCPOLY_LE);
188}
189u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
190{
191 return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE);
192}
193#else
194u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
195{
Joe Mario8f243af2012-10-04 17:12:15 -0700196 return crc32_le_generic(crc, p, len,
197 (const u32 (*)[256])crc32table_le, CRCPOLY_LE);
Darrick J. Wong46c58012012-03-23 15:02:25 -0700198}
199u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
200{
Joe Mario8f243af2012-10-04 17:12:15 -0700201 return crc32_le_generic(crc, p, len,
202 (const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE);
Darrick J. Wong46c58012012-03-23 15:02:25 -0700203}
204#endif
George Spelvin6d514b42014-06-23 15:11:54 +0200205EXPORT_SYMBOL(crc32_le);
206EXPORT_SYMBOL(__crc32c_le);
207
208/*
209 * This multiplies the polynomials x and y modulo the given modulus.
210 * This follows the "little-endian" CRC convention that the lsbit
211 * represents the highest power of x, and the msbit represents x^0.
212 */
213static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus)
Daniel Borkmann6e95fca2013-10-30 11:50:49 +0100214{
George Spelvin6d514b42014-06-23 15:11:54 +0200215 u32 product = x & 1 ? y : 0;
216 int i;
217
218 for (i = 0; i < 31; i++) {
219 product = (product >> 1) ^ (product & 1 ? modulus : 0);
220 x >>= 1;
221 product ^= x & 1 ? y : 0;
222 }
223
224 return product;
Daniel Borkmann6e95fca2013-10-30 11:50:49 +0100225}
226
George Spelvin6d514b42014-06-23 15:11:54 +0200227/**
Randy Dunlap8a298962017-09-08 16:35:55 -0700228 * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time
George Spelvin6d514b42014-06-23 15:11:54 +0200229 * @crc: The original little-endian CRC (i.e. lsbit is x^31 coefficient)
230 * @len: The number of bytes. @crc is multiplied by x^(8*@len)
231 * @polynomial: The modulus used to reduce the result to 32 bits.
232 *
233 * It's possible to parallelize CRC computations by computing a CRC
234 * over separate ranges of a buffer, then summing them.
235 * This shifts the given CRC by 8*len bits (i.e. produces the same effect
236 * as appending len bytes of zero to the data), in time proportional
237 * to log(len).
238 */
239static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len,
240 u32 polynomial)
Daniel Borkmann6e95fca2013-10-30 11:50:49 +0100241{
George Spelvin6d514b42014-06-23 15:11:54 +0200242 u32 power = polynomial; /* CRC of x^32 */
243 int i;
244
245 /* Shift up to 32 bits in the simple linear way */
246 for (i = 0; i < 8 * (int)(len & 3); i++)
247 crc = (crc >> 1) ^ (crc & 1 ? polynomial : 0);
248
249 len >>= 2;
250 if (!len)
251 return crc;
252
253 for (;;) {
254 /* "power" is x^(2^i), modulo the polynomial */
255 if (len & 1)
256 crc = gf2_multiply(crc, power, polynomial);
257
258 len >>= 1;
259 if (!len)
260 break;
261
262 /* Square power, advancing to x^(2^(i+1)) */
263 power = gf2_multiply(power, power, polynomial);
264 }
265
266 return crc;
Daniel Borkmann6e95fca2013-10-30 11:50:49 +0100267}
George Spelvin6d514b42014-06-23 15:11:54 +0200268
269u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len)
270{
271 return crc32_generic_shift(crc, len, CRCPOLY_LE);
272}
273
274u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len)
275{
276 return crc32_generic_shift(crc, len, CRC32C_POLY_LE);
277}
278EXPORT_SYMBOL(crc32_le_shift);
279EXPORT_SYMBOL(__crc32c_le_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Randy Dunlap2f721002006-06-25 05:48:59 -0700281/**
Gu Zhengf2e1d2a2013-09-11 14:23:52 -0700282 * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
Randy Dunlap2f721002006-06-25 05:48:59 -0700283 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
284 * other uses, or the previous crc32 value if computing incrementally.
Gu Zhengf2e1d2a2013-09-11 14:23:52 -0700285 * @p: pointer to buffer over which CRC32 is run
Randy Dunlap2f721002006-06-25 05:48:59 -0700286 * @len: length of buffer @p
Gu Zhengf2e1d2a2013-09-11 14:23:52 -0700287 * @tab: big-endian Ethernet table
288 * @polynomial: CRC32 BE polynomial
Randy Dunlap2f721002006-06-25 05:48:59 -0700289 */
Darrick J. Wong46c58012012-03-23 15:02:25 -0700290static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
291 size_t len, const u32 (*tab)[256],
292 u32 polynomial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Bob Pearson60e58d52012-03-23 15:02:22 -0700294#if CRC_BE_BITS == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int i;
296 while (len--) {
297 crc ^= *p++ << 24;
298 for (i = 0; i < 8; i++)
299 crc =
Darrick J. Wong46c58012012-03-23 15:02:25 -0700300 (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 0);
302 }
Bob Pearson60e58d52012-03-23 15:02:22 -0700303# elif CRC_BE_BITS == 2
304 while (len--) {
305 crc ^= *p++ << 24;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700306 crc = (crc << 2) ^ tab[0][crc >> 30];
307 crc = (crc << 2) ^ tab[0][crc >> 30];
308 crc = (crc << 2) ^ tab[0][crc >> 30];
309 crc = (crc << 2) ^ tab[0][crc >> 30];
Bob Pearson60e58d52012-03-23 15:02:22 -0700310 }
311# elif CRC_BE_BITS == 4
312 while (len--) {
313 crc ^= *p++ << 24;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700314 crc = (crc << 4) ^ tab[0][crc >> 28];
315 crc = (crc << 4) ^ tab[0][crc >> 28];
Bob Pearson60e58d52012-03-23 15:02:22 -0700316 }
317# elif CRC_BE_BITS == 8
Bob Pearson9a1dbf62012-03-23 15:02:23 -0700318 while (len--) {
319 crc ^= *p++ << 24;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700320 crc = (crc << 8) ^ tab[0][crc >> 24];
Bob Pearson9a1dbf62012-03-23 15:02:23 -0700321 }
322# else
Bob Pearsonce4320d2012-03-23 15:02:23 -0700323 crc = (__force u32) __cpu_to_be32(crc);
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800324 crc = crc32_body(crc, p, len, tab);
Bob Pearsonce4320d2012-03-23 15:02:23 -0700325 crc = __be32_to_cpu((__force __be32)crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326# endif
Bob Pearson60e58d52012-03-23 15:02:22 -0700327 return crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
Darrick J. Wong46c58012012-03-23 15:02:25 -0700329
330#if CRC_LE_BITS == 1
331u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
332{
333 return crc32_be_generic(crc, p, len, NULL, CRCPOLY_BE);
334}
335#else
336u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
337{
Joe Mario8f243af2012-10-04 17:12:15 -0700338 return crc32_be_generic(crc, p, len,
339 (const u32 (*)[256])crc32table_be, CRCPOLY_BE);
Darrick J. Wong46c58012012-03-23 15:02:25 -0700340}
341#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342EXPORT_SYMBOL(crc32_be);