crc32.c 9.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3 4 5
 * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin
 * cleaned up code to current version of sparse and added the slicing-by-8
 * algorithm to the closely similar existing slicing-by-4 algorithm.
 *
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
 * Nicer crc32 functions/docs submitted by linux@horizon.com.  Thanks!
 * Code was from the public domain, copyright abandoned.  Code was
 * subsequently included in the kernel, thus was re-licensed under the
 * GNU GPL v2.
 *
 * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
 * Same crc32 function was used in 5 other places in the kernel.
 * I made one version, and deleted the others.
 * There are various incantations of crc32().  Some use a seed of 0 or ~0.
 * Some xor at the end with ~0.  The generic crc32() function takes
 * seed as an argument, and doesn't xor at the end.  Then individual
 * users can do whatever they need.
 *   drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
 *   fs/jffs2 uses seed 0, doesn't xor with ~0.
 *   fs/partitions/efi.c uses seed ~0, xor's with ~0.
 *
 * This source code is licensed under the GNU General Public License,
 * Version 2.  See the file COPYING for more details.
 */

27 28
/* see: Documentation/crc32.txt for a description of algorithms */

L
Linus Torvalds 已提交
29
#include <linux/crc32.h>
30
#include <linux/crc32poly.h>
L
Linus Torvalds 已提交
31 32
#include <linux/module.h>
#include <linux/types.h>
33
#include <linux/sched.h>
L
Linus Torvalds 已提交
34
#include "crc32defs.h"
B
Bob Pearson 已提交
35

36
#if CRC_LE_BITS > 8
37
# define tole(x) ((__force u32) cpu_to_le32(x))
L
Linus Torvalds 已提交
38
#else
J
Joakim Tjernlund 已提交
39 40 41
# define tole(x) (x)
#endif

42
#if CRC_BE_BITS > 8
43
# define tobe(x) ((__force u32) cpu_to_be32(x))
J
Joakim Tjernlund 已提交
44 45
#else
# define tobe(x) (x)
L
Linus Torvalds 已提交
46
#endif
B
Bob Pearson 已提交
47

L
Linus Torvalds 已提交
48 49 50
#include "crc32table.h"

MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>");
D
Darrick J. Wong 已提交
51
MODULE_DESCRIPTION("Various CRC32 calculations");
L
Linus Torvalds 已提交
52 53
MODULE_LICENSE("GPL");

54
#if CRC_LE_BITS > 8 || CRC_BE_BITS > 8
55

56
/* implements slicing-by-4 or slicing-by-8 algorithm */
57
static inline u32 __pure
J
Joakim Tjernlund 已提交
58
crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
59
{
60
# ifdef __LITTLE_ENDIAN
J
Joakim Tjernlund 已提交
61
#  define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
62 63 64 65
#  define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \
		   t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])
#  define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \
		   t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])
66
# else
J
Joakim Tjernlund 已提交
67
#  define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
68 69 70 71
#  define DO_CRC4 (t0[(q) & 255] ^ t1[(q >> 8) & 255] ^ \
		   t2[(q >> 16) & 255] ^ t3[(q >> 24) & 255])
#  define DO_CRC8 (t4[(q) & 255] ^ t5[(q >> 8) & 255] ^ \
		   t6[(q >> 16) & 255] ^ t7[(q >> 24) & 255])
72
# endif
J
Joakim Tjernlund 已提交
73
	const u32 *b;
74
	size_t    rem_len;
75 76 77
# ifdef CONFIG_X86
	size_t i;
# endif
J
Joakim Tjernlund 已提交
78
	const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
79
# if CRC_LE_BITS != 32
80
	const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7];
81
# endif
82
	u32 q;
83 84

	/* Align it */
J
Joakim Tjernlund 已提交
85
	if (unlikely((long)buf & 3 && len)) {
86
		do {
J
Joakim Tjernlund 已提交
87 88
			DO_CRC(*buf++);
		} while ((--len) && ((long)buf)&3);
89
	}
90 91

# if CRC_LE_BITS == 32
92 93
	rem_len = len & 3;
	len = len >> 2;
94 95 96 97 98
# else
	rem_len = len & 7;
	len = len >> 3;
# endif

J
Joakim Tjernlund 已提交
99
	b = (const u32 *)buf;
100 101 102 103
# ifdef CONFIG_X86
	--b;
	for (i = 0; i < len; i++) {
# else
104
	for (--b; len; --len) {
105
# endif
106 107 108 109 110 111 112 113
		q = crc ^ *++b; /* use pre increment for speed */
# if CRC_LE_BITS == 32
		crc = DO_CRC4;
# else
		crc = DO_CRC8;
		q = *++b;
		crc ^= DO_CRC4;
# endif
114 115 116 117 118
	}
	len = rem_len;
	/* And the last few bytes */
	if (len) {
		u8 *p = (u8 *)(b + 1) - 1;
119 120 121 122
# ifdef CONFIG_X86
		for (i = 0; i < len; i++)
			DO_CRC(*++p); /* use pre increment for speed */
# else
123 124 125
		do {
			DO_CRC(*++p); /* use pre increment for speed */
		} while (--len);
126
# endif
127 128
	}
	return crc;
J
Joakim Tjernlund 已提交
129
#undef DO_CRC
J
Joakim Tjernlund 已提交
130
#undef DO_CRC4
131
#undef DO_CRC8
132 133
}
#endif
B
Bob Pearson 已提交
134

135

R
Randy Dunlap 已提交
136
/**
137 138 139 140 141
 * crc32_le_generic() - Calculate bitwise little-endian Ethernet AUTODIN II
 *			CRC32/CRC32C
 * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for other
 *	 uses, or the previous crc32/crc32c value if computing incrementally.
 * @p: pointer to buffer over which CRC32/CRC32C is run
R
Randy Dunlap 已提交
142
 * @len: length of buffer @p
143 144
 * @tab: little-endian Ethernet table
 * @polynomial: CRC32/CRC32c LE polynomial
R
Randy Dunlap 已提交
145
 */
D
Darrick J. Wong 已提交
146 147 148
static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
					  size_t len, const u32 (*tab)[256],
					  u32 polynomial)
L
Linus Torvalds 已提交
149
{
B
Bob Pearson 已提交
150
#if CRC_LE_BITS == 1
L
Linus Torvalds 已提交
151 152 153 154
	int i;
	while (len--) {
		crc ^= *p++;
		for (i = 0; i < 8; i++)
D
Darrick J. Wong 已提交
155
			crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
L
Linus Torvalds 已提交
156
	}
B
Bob Pearson 已提交
157
# elif CRC_LE_BITS == 2
L
Linus Torvalds 已提交
158 159
	while (len--) {
		crc ^= *p++;
D
Darrick J. Wong 已提交
160 161 162 163
		crc = (crc >> 2) ^ tab[0][crc & 3];
		crc = (crc >> 2) ^ tab[0][crc & 3];
		crc = (crc >> 2) ^ tab[0][crc & 3];
		crc = (crc >> 2) ^ tab[0][crc & 3];
L
Linus Torvalds 已提交
164
	}
B
Bob Pearson 已提交
165
# elif CRC_LE_BITS == 4
L
Linus Torvalds 已提交
166 167
	while (len--) {
		crc ^= *p++;
D
Darrick J. Wong 已提交
168 169
		crc = (crc >> 4) ^ tab[0][crc & 15];
		crc = (crc >> 4) ^ tab[0][crc & 15];
L
Linus Torvalds 已提交
170
	}
B
Bob Pearson 已提交
171
# elif CRC_LE_BITS == 8
172 173 174
	/* aka Sarwate algorithm */
	while (len--) {
		crc ^= *p++;
D
Darrick J. Wong 已提交
175
		crc = (crc >> 8) ^ tab[0][crc & 255];
176 177
	}
# else
178
	crc = (__force u32) __cpu_to_le32(crc);
B
Bob Pearson 已提交
179
	crc = crc32_body(crc, p, len, tab);
180
	crc = __le32_to_cpu((__force __le32)crc);
B
Bob Pearson 已提交
181
#endif
L
Linus Torvalds 已提交
182 183
	return crc;
}
D
Darrick J. Wong 已提交
184 185

#if CRC_LE_BITS == 1
186
u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
D
Darrick J. Wong 已提交
187
{
188
	return crc32_le_generic(crc, p, len, NULL, CRC32_POLY_LE);
D
Darrick J. Wong 已提交
189
}
190
u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
D
Darrick J. Wong 已提交
191 192 193 194
{
	return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE);
}
#else
195
u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
D
Darrick J. Wong 已提交
196
{
197
	return crc32_le_generic(crc, p, len,
198
			(const u32 (*)[256])crc32table_le, CRC32_POLY_LE);
D
Darrick J. Wong 已提交
199
}
200
u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
D
Darrick J. Wong 已提交
201
{
202 203
	return crc32_le_generic(crc, p, len,
			(const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE);
D
Darrick J. Wong 已提交
204 205
}
#endif
206 207 208
EXPORT_SYMBOL(crc32_le);
EXPORT_SYMBOL(__crc32c_le);

209 210
u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/*
 * This multiplies the polynomials x and y modulo the given modulus.
 * This follows the "little-endian" CRC convention that the lsbit
 * represents the highest power of x, and the msbit represents x^0.
 */
static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus)
{
	u32 product = x & 1 ? y : 0;
	int i;

	for (i = 0; i < 31; i++) {
		product = (product >> 1) ^ (product & 1 ? modulus : 0);
		x >>= 1;
		product ^= x & 1 ? y : 0;
	}

	return product;
}

/**
232
 * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
 * @crc: The original little-endian CRC (i.e. lsbit is x^31 coefficient)
 * @len: The number of bytes. @crc is multiplied by x^(8*@len)
 * @polynomial: The modulus used to reduce the result to 32 bits.
 *
 * It's possible to parallelize CRC computations by computing a CRC
 * over separate ranges of a buffer, then summing them.
 * This shifts the given CRC by 8*len bits (i.e. produces the same effect
 * as appending len bytes of zero to the data), in time proportional
 * to log(len).
 */
static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len,
						   u32 polynomial)
{
	u32 power = polynomial;	/* CRC of x^32 */
	int i;

	/* Shift up to 32 bits in the simple linear way */
	for (i = 0; i < 8 * (int)(len & 3); i++)
		crc = (crc >> 1) ^ (crc & 1 ? polynomial : 0);

	len >>= 2;
	if (!len)
		return crc;

	for (;;) {
		/* "power" is x^(2^i), modulo the polynomial */
		if (len & 1)
			crc = gf2_multiply(crc, power, polynomial);

		len >>= 1;
		if (!len)
			break;

		/* Square power, advancing to x^(2^(i+1)) */
		power = gf2_multiply(power, power, polynomial);
	}

	return crc;
}

u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len)
274
{
275
	return crc32_generic_shift(crc, len, CRC32_POLY_LE);
276 277
}

278
u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len)
279
{
280
	return crc32_generic_shift(crc, len, CRC32C_POLY_LE);
281
}
282 283
EXPORT_SYMBOL(crc32_le_shift);
EXPORT_SYMBOL(__crc32c_le_shift);
L
Linus Torvalds 已提交
284

R
Randy Dunlap 已提交
285
/**
286
 * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
R
Randy Dunlap 已提交
287 288
 * @crc: seed value for computation.  ~0 for Ethernet, sometimes 0 for
 *	other uses, or the previous crc32 value if computing incrementally.
289
 * @p: pointer to buffer over which CRC32 is run
R
Randy Dunlap 已提交
290
 * @len: length of buffer @p
291 292
 * @tab: big-endian Ethernet table
 * @polynomial: CRC32 BE polynomial
R
Randy Dunlap 已提交
293
 */
D
Darrick J. Wong 已提交
294 295 296
static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
					  size_t len, const u32 (*tab)[256],
					  u32 polynomial)
L
Linus Torvalds 已提交
297
{
B
Bob Pearson 已提交
298
#if CRC_BE_BITS == 1
L
Linus Torvalds 已提交
299 300 301 302 303
	int i;
	while (len--) {
		crc ^= *p++ << 24;
		for (i = 0; i < 8; i++)
			crc =
D
Darrick J. Wong 已提交
304
			    (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
L
Linus Torvalds 已提交
305 306
					  0);
	}
B
Bob Pearson 已提交
307
# elif CRC_BE_BITS == 2
L
Linus Torvalds 已提交
308 309
	while (len--) {
		crc ^= *p++ << 24;
D
Darrick J. Wong 已提交
310 311 312 313
		crc = (crc << 2) ^ tab[0][crc >> 30];
		crc = (crc << 2) ^ tab[0][crc >> 30];
		crc = (crc << 2) ^ tab[0][crc >> 30];
		crc = (crc << 2) ^ tab[0][crc >> 30];
L
Linus Torvalds 已提交
314
	}
B
Bob Pearson 已提交
315
# elif CRC_BE_BITS == 4
L
Linus Torvalds 已提交
316 317
	while (len--) {
		crc ^= *p++ << 24;
D
Darrick J. Wong 已提交
318 319
		crc = (crc << 4) ^ tab[0][crc >> 28];
		crc = (crc << 4) ^ tab[0][crc >> 28];
L
Linus Torvalds 已提交
320
	}
B
Bob Pearson 已提交
321
# elif CRC_BE_BITS == 8
322 323
	while (len--) {
		crc ^= *p++ << 24;
D
Darrick J. Wong 已提交
324
		crc = (crc << 8) ^ tab[0][crc >> 24];
325 326
	}
# else
327
	crc = (__force u32) __cpu_to_be32(crc);
B
Bob Pearson 已提交
328
	crc = crc32_body(crc, p, len, tab);
329
	crc = __be32_to_cpu((__force __be32)crc);
L
Linus Torvalds 已提交
330
# endif
B
Bob Pearson 已提交
331
	return crc;
L
Linus Torvalds 已提交
332
}
D
Darrick J. Wong 已提交
333 334 335 336

#if CRC_LE_BITS == 1
u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
{
337
	return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE);
D
Darrick J. Wong 已提交
338 339 340 341
}
#else
u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
{
342
	return crc32_be_generic(crc, p, len,
343
			(const u32 (*)[256])crc32table_be, CRC32_POLY_BE);
D
Darrick J. Wong 已提交
344 345
}
#endif
L
Linus Torvalds 已提交
346
EXPORT_SYMBOL(crc32_be);