blob: a6c9afafc8c85d84ed522912649eb0c5e930cd03 [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>
Krzysztof Kozlowski1fb2e3f2018-07-17 18:05:36 +020030#include <linux/crc32poly.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/types.h>
Daniel Borkmanncc0ac192013-11-04 17:10:26 +010033#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "crc32defs.h"
Bob Pearson60e58d52012-03-23 15:02:22 -070035
Bob Pearson9a1dbf62012-03-23 15:02:23 -070036#if CRC_LE_BITS > 8
Fabian Frederick38b4fe52014-06-04 16:11:56 -070037# define tole(x) ((__force u32) cpu_to_le32(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#else
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080039# define tole(x) (x)
40#endif
41
Bob Pearson9a1dbf62012-03-23 15:02:23 -070042#if CRC_BE_BITS > 8
Fabian Frederick38b4fe52014-06-04 16:11:56 -070043# define tobe(x) ((__force u32) cpu_to_be32(x))
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080044#else
45# define tobe(x) (x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#endif
Bob Pearson60e58d52012-03-23 15:02:22 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "crc32table.h"
49
50MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
Darrick J. Wong46c58012012-03-23 15:02:25 -070051MODULE_DESCRIPTION("Various CRC32 calculations");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052MODULE_LICENSE("GPL");
53
Bob Pearson9a1dbf62012-03-23 15:02:23 -070054#if CRC_LE_BITS > 8 || CRC_BE_BITS > 8
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080055
Bob Pearson324eb0f2012-03-23 15:02:24 -070056/* implements slicing-by-4 or slicing-by-8 algorithm */
George Spelvind8f1c472014-06-23 15:11:56 +020057static inline u32 __pure
Joakim Tjernlund836e2af2010-05-24 14:33:31 -070058crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080059{
Andrew Morton0d2daf52010-05-25 23:43:03 -070060# ifdef __LITTLE_ENDIAN
Joakim Tjernlund57423322012-01-10 15:10:18 -080061# define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
Bob Pearson324eb0f2012-03-23 15:02:24 -070062# define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \
63 t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])
64# define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \
65 t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080066# else
Joakim Tjernlund57423322012-01-10 15:10:18 -080067# define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
Bob Pearson324eb0f2012-03-23 15:02:24 -070068# define DO_CRC4 (t0[(q) & 255] ^ t1[(q >> 8) & 255] ^ \
69 t2[(q >> 16) & 255] ^ t3[(q >> 24) & 255])
70# define DO_CRC8 (t4[(q) & 255] ^ t5[(q >> 8) & 255] ^ \
71 t6[(q >> 16) & 255] ^ t7[(q >> 24) & 255])
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080072# endif
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080073 const u32 *b;
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080074 size_t rem_len;
Bob Pearson0292c492012-03-23 15:02:24 -070075# ifdef CONFIG_X86
76 size_t i;
77# endif
Joakim Tjernlund57423322012-01-10 15:10:18 -080078 const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
Thiago Rafael Becker49ac5722012-07-30 14:41:26 -070079# if CRC_LE_BITS != 32
Bob Pearson324eb0f2012-03-23 15:02:24 -070080 const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7];
Thiago Rafael Becker49ac5722012-07-30 14:41:26 -070081# endif
Bob Pearson324eb0f2012-03-23 15:02:24 -070082 u32 q;
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080083
84 /* Align it */
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080085 if (unlikely((long)buf & 3 && len)) {
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080086 do {
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080087 DO_CRC(*buf++);
88 } while ((--len) && ((long)buf)&3);
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080089 }
Bob Pearson324eb0f2012-03-23 15:02:24 -070090
91# if CRC_LE_BITS == 32
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080092 rem_len = len & 3;
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -080093 len = len >> 2;
Bob Pearson324eb0f2012-03-23 15:02:24 -070094# else
95 rem_len = len & 7;
96 len = len >> 3;
97# endif
98
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -080099 b = (const u32 *)buf;
Bob Pearson0292c492012-03-23 15:02:24 -0700100# ifdef CONFIG_X86
101 --b;
102 for (i = 0; i < len; i++) {
103# else
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800104 for (--b; len; --len) {
Bob Pearson0292c492012-03-23 15:02:24 -0700105# endif
Bob Pearson324eb0f2012-03-23 15:02:24 -0700106 q = crc ^ *++b; /* use pre increment for speed */
107# if CRC_LE_BITS == 32
108 crc = DO_CRC4;
109# else
110 crc = DO_CRC8;
111 q = *++b;
112 crc ^= DO_CRC4;
113# endif
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800114 }
115 len = rem_len;
116 /* And the last few bytes */
117 if (len) {
118 u8 *p = (u8 *)(b + 1) - 1;
Bob Pearson0292c492012-03-23 15:02:24 -0700119# ifdef CONFIG_X86
120 for (i = 0; i < len; i++)
121 DO_CRC(*++p); /* use pre increment for speed */
122# else
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800123 do {
124 DO_CRC(*++p); /* use pre increment for speed */
125 } while (--len);
Bob Pearson0292c492012-03-23 15:02:24 -0700126# endif
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800127 }
128 return crc;
Joakim Tjernlund4f2a94632010-03-05 13:43:55 -0800129#undef DO_CRC
Joakim Tjernlund836e2af2010-05-24 14:33:31 -0700130#undef DO_CRC4
Bob Pearson324eb0f2012-03-23 15:02:24 -0700131#undef DO_CRC8
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800132}
133#endif
Bob Pearson60e58d52012-03-23 15:02:22 -0700134
Daniel Borkmann6e95fca2013-10-30 11:50:49 +0100135
Randy Dunlap2f721002006-06-25 05:48:59 -0700136/**
Gu Zhengf2e1d2a2013-09-11 14:23:52 -0700137 * crc32_le_generic() - Calculate bitwise little-endian Ethernet AUTODIN II
138 * CRC32/CRC32C
139 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for other
140 * uses, or the previous crc32/crc32c value if computing incrementally.
141 * @p: pointer to buffer over which CRC32/CRC32C is run
Randy Dunlap2f721002006-06-25 05:48:59 -0700142 * @len: length of buffer @p
Gu Zhengf2e1d2a2013-09-11 14:23:52 -0700143 * @tab: little-endian Ethernet table
144 * @polynomial: CRC32/CRC32c LE polynomial
Randy Dunlap2f721002006-06-25 05:48:59 -0700145 */
Darrick J. Wong46c58012012-03-23 15:02:25 -0700146static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
147 size_t len, const u32 (*tab)[256],
148 u32 polynomial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Bob Pearson60e58d52012-03-23 15:02:22 -0700150#if CRC_LE_BITS == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int i;
152 while (len--) {
153 crc ^= *p++;
154 for (i = 0; i < 8; i++)
Darrick J. Wong46c58012012-03-23 15:02:25 -0700155 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
Bob Pearson60e58d52012-03-23 15:02:22 -0700157# elif CRC_LE_BITS == 2
158 while (len--) {
159 crc ^= *p++;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700160 crc = (crc >> 2) ^ tab[0][crc & 3];
161 crc = (crc >> 2) ^ tab[0][crc & 3];
162 crc = (crc >> 2) ^ tab[0][crc & 3];
163 crc = (crc >> 2) ^ tab[0][crc & 3];
Bob Pearson60e58d52012-03-23 15:02:22 -0700164 }
165# elif CRC_LE_BITS == 4
166 while (len--) {
167 crc ^= *p++;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700168 crc = (crc >> 4) ^ tab[0][crc & 15];
169 crc = (crc >> 4) ^ tab[0][crc & 15];
Bob Pearson60e58d52012-03-23 15:02:22 -0700170 }
171# elif CRC_LE_BITS == 8
Bob Pearson9a1dbf62012-03-23 15:02:23 -0700172 /* aka Sarwate algorithm */
173 while (len--) {
174 crc ^= *p++;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700175 crc = (crc >> 8) ^ tab[0][crc & 255];
Bob Pearson9a1dbf62012-03-23 15:02:23 -0700176 }
177# else
Bob Pearsonce4320d2012-03-23 15:02:23 -0700178 crc = (__force u32) __cpu_to_le32(crc);
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800179 crc = crc32_body(crc, p, len, tab);
Bob Pearsonce4320d2012-03-23 15:02:23 -0700180 crc = __le32_to_cpu((__force __le32)crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#endif
Bob Pearson60e58d52012-03-23 15:02:22 -0700182 return crc;
183}
Darrick J. Wong46c58012012-03-23 15:02:25 -0700184
185#if CRC_LE_BITS == 1
186u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
187{
Krzysztof Kozlowskie37f2f92018-07-17 18:05:37 +0200188 return crc32_le_generic(crc, p, len, NULL, CRC32_POLY_LE);
Darrick J. Wong46c58012012-03-23 15:02:25 -0700189}
190u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
191{
192 return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE);
193}
194#else
195u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len)
196{
Joe Mario8f243af2012-10-04 17:12:15 -0700197 return crc32_le_generic(crc, p, len,
Krzysztof Kozlowskie37f2f92018-07-17 18:05:37 +0200198 (const u32 (*)[256])crc32table_le, CRC32_POLY_LE);
Darrick J. Wong46c58012012-03-23 15:02:25 -0700199}
200u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len)
201{
Joe Mario8f243af2012-10-04 17:12:15 -0700202 return crc32_le_generic(crc, p, len,
203 (const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE);
Darrick J. Wong46c58012012-03-23 15:02:25 -0700204}
205#endif
George Spelvin6d514b42014-06-23 15:11:54 +0200206EXPORT_SYMBOL(crc32_le);
207EXPORT_SYMBOL(__crc32c_le);
208
209/*
210 * This multiplies the polynomials x and y modulo the given modulus.
211 * This follows the "little-endian" CRC convention that the lsbit
212 * represents the highest power of x, and the msbit represents x^0.
213 */
214static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus)
Daniel Borkmann6e95fca2013-10-30 11:50:49 +0100215{
George Spelvin6d514b42014-06-23 15:11:54 +0200216 u32 product = x & 1 ? y : 0;
217 int i;
218
219 for (i = 0; i < 31; i++) {
220 product = (product >> 1) ^ (product & 1 ? modulus : 0);
221 x >>= 1;
222 product ^= x & 1 ? y : 0;
223 }
224
225 return product;
Daniel Borkmann6e95fca2013-10-30 11:50:49 +0100226}
227
George Spelvin6d514b42014-06-23 15:11:54 +0200228/**
Randy Dunlap8a298962017-09-08 16:35:55 -0700229 * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time
George Spelvin6d514b42014-06-23 15:11:54 +0200230 * @crc: The original little-endian CRC (i.e. lsbit is x^31 coefficient)
231 * @len: The number of bytes. @crc is multiplied by x^(8*@len)
232 * @polynomial: The modulus used to reduce the result to 32 bits.
233 *
234 * It's possible to parallelize CRC computations by computing a CRC
235 * over separate ranges of a buffer, then summing them.
236 * This shifts the given CRC by 8*len bits (i.e. produces the same effect
237 * as appending len bytes of zero to the data), in time proportional
238 * to log(len).
239 */
240static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len,
241 u32 polynomial)
Daniel Borkmann6e95fca2013-10-30 11:50:49 +0100242{
George Spelvin6d514b42014-06-23 15:11:54 +0200243 u32 power = polynomial; /* CRC of x^32 */
244 int i;
245
246 /* Shift up to 32 bits in the simple linear way */
247 for (i = 0; i < 8 * (int)(len & 3); i++)
248 crc = (crc >> 1) ^ (crc & 1 ? polynomial : 0);
249
250 len >>= 2;
251 if (!len)
252 return crc;
253
254 for (;;) {
255 /* "power" is x^(2^i), modulo the polynomial */
256 if (len & 1)
257 crc = gf2_multiply(crc, power, polynomial);
258
259 len >>= 1;
260 if (!len)
261 break;
262
263 /* Square power, advancing to x^(2^(i+1)) */
264 power = gf2_multiply(power, power, polynomial);
265 }
266
267 return crc;
Daniel Borkmann6e95fca2013-10-30 11:50:49 +0100268}
George Spelvin6d514b42014-06-23 15:11:54 +0200269
270u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len)
271{
Krzysztof Kozlowskie37f2f92018-07-17 18:05:37 +0200272 return crc32_generic_shift(crc, len, CRC32_POLY_LE);
George Spelvin6d514b42014-06-23 15:11:54 +0200273}
274
275u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len)
276{
277 return crc32_generic_shift(crc, len, CRC32C_POLY_LE);
278}
279EXPORT_SYMBOL(crc32_le_shift);
280EXPORT_SYMBOL(__crc32c_le_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Randy Dunlap2f721002006-06-25 05:48:59 -0700282/**
Gu Zhengf2e1d2a2013-09-11 14:23:52 -0700283 * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
Randy Dunlap2f721002006-06-25 05:48:59 -0700284 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
285 * other uses, or the previous crc32 value if computing incrementally.
Gu Zhengf2e1d2a2013-09-11 14:23:52 -0700286 * @p: pointer to buffer over which CRC32 is run
Randy Dunlap2f721002006-06-25 05:48:59 -0700287 * @len: length of buffer @p
Gu Zhengf2e1d2a2013-09-11 14:23:52 -0700288 * @tab: big-endian Ethernet table
289 * @polynomial: CRC32 BE polynomial
Randy Dunlap2f721002006-06-25 05:48:59 -0700290 */
Darrick J. Wong46c58012012-03-23 15:02:25 -0700291static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
292 size_t len, const u32 (*tab)[256],
293 u32 polynomial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Bob Pearson60e58d52012-03-23 15:02:22 -0700295#if CRC_BE_BITS == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int i;
297 while (len--) {
298 crc ^= *p++ << 24;
299 for (i = 0; i < 8; i++)
300 crc =
Darrick J. Wong46c58012012-03-23 15:02:25 -0700301 (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 0);
303 }
Bob Pearson60e58d52012-03-23 15:02:22 -0700304# elif CRC_BE_BITS == 2
305 while (len--) {
306 crc ^= *p++ << 24;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700307 crc = (crc << 2) ^ tab[0][crc >> 30];
308 crc = (crc << 2) ^ tab[0][crc >> 30];
309 crc = (crc << 2) ^ tab[0][crc >> 30];
310 crc = (crc << 2) ^ tab[0][crc >> 30];
Bob Pearson60e58d52012-03-23 15:02:22 -0700311 }
312# elif CRC_BE_BITS == 4
313 while (len--) {
314 crc ^= *p++ << 24;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700315 crc = (crc << 4) ^ tab[0][crc >> 28];
316 crc = (crc << 4) ^ tab[0][crc >> 28];
Bob Pearson60e58d52012-03-23 15:02:22 -0700317 }
318# elif CRC_BE_BITS == 8
Bob Pearson9a1dbf62012-03-23 15:02:23 -0700319 while (len--) {
320 crc ^= *p++ << 24;
Darrick J. Wong46c58012012-03-23 15:02:25 -0700321 crc = (crc << 8) ^ tab[0][crc >> 24];
Bob Pearson9a1dbf62012-03-23 15:02:23 -0700322 }
323# else
Bob Pearsonce4320d2012-03-23 15:02:23 -0700324 crc = (__force u32) __cpu_to_be32(crc);
Joakim Tjernlundddcaccb2009-12-14 18:01:33 -0800325 crc = crc32_body(crc, p, len, tab);
Bob Pearsonce4320d2012-03-23 15:02:23 -0700326 crc = __be32_to_cpu((__force __be32)crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327# endif
Bob Pearson60e58d52012-03-23 15:02:22 -0700328 return crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
Darrick J. Wong46c58012012-03-23 15:02:25 -0700330
331#if CRC_LE_BITS == 1
332u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
333{
Krzysztof Kozlowskie37f2f92018-07-17 18:05:37 +0200334 return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE);
Darrick J. Wong46c58012012-03-23 15:02:25 -0700335}
336#else
337u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
338{
Joe Mario8f243af2012-10-04 17:12:15 -0700339 return crc32_be_generic(crc, p, len,
Krzysztof Kozlowskie37f2f92018-07-17 18:05:37 +0200340 (const u32 (*)[256])crc32table_be, CRC32_POLY_BE);
Darrick J. Wong46c58012012-03-23 15:02:25 -0700341}
342#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343EXPORT_SYMBOL(crc32_be);