]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
78dff418 BP |
2 | * 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 | * | |
1da177e4 LT |
6 | * Oct 15, 2000 Matt Domsch <[email protected]> |
7 | * Nicer crc32 functions/docs submitted by [email protected]. 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 <[email protected]> | |
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 | ||
fbedceb1 BP |
27 | /* see: Documentation/crc32.txt for a description of algorithms */ |
28 | ||
1da177e4 | 29 | #include <linux/crc32.h> |
1fb2e3f2 | 30 | #include <linux/crc32poly.h> |
1da177e4 | 31 | #include <linux/module.h> |
1da177e4 | 32 | #include <linux/types.h> |
cc0ac199 | 33 | #include <linux/sched.h> |
1da177e4 | 34 | #include "crc32defs.h" |
60e58d5c | 35 | |
9a1dbf6a | 36 | #if CRC_LE_BITS > 8 |
38b4fe5f | 37 | # define tole(x) ((__force u32) cpu_to_le32(x)) |
1da177e4 | 38 | #else |
4f2a9463 JT |
39 | # define tole(x) (x) |
40 | #endif | |
41 | ||
9a1dbf6a | 42 | #if CRC_BE_BITS > 8 |
38b4fe5f | 43 | # define tobe(x) ((__force u32) cpu_to_be32(x)) |
4f2a9463 JT |
44 | #else |
45 | # define tobe(x) (x) | |
1da177e4 | 46 | #endif |
60e58d5c | 47 | |
1da177e4 LT |
48 | #include "crc32table.h" |
49 | ||
50 | MODULE_AUTHOR("Matt Domsch <[email protected]>"); | |
46c5801e | 51 | MODULE_DESCRIPTION("Various CRC32 calculations"); |
1da177e4 LT |
52 | MODULE_LICENSE("GPL"); |
53 | ||
9a1dbf6a | 54 | #if CRC_LE_BITS > 8 || CRC_BE_BITS > 8 |
ddcaccbc | 55 | |
324eb0f1 | 56 | /* implements slicing-by-4 or slicing-by-8 algorithm */ |
d8f1c477 | 57 | static inline u32 __pure |
836e2af9 | 58 | crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256]) |
ddcaccbc | 59 | { |
0d2daf5c | 60 | # ifdef __LITTLE_ENDIAN |
5742332d | 61 | # define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8) |
324eb0f1 BP |
62 | # 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]) | |
ddcaccbc | 66 | # else |
5742332d | 67 | # define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8) |
324eb0f1 BP |
68 | # 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]) | |
ddcaccbc | 72 | # endif |
4f2a9463 | 73 | const u32 *b; |
ddcaccbc | 74 | size_t rem_len; |
0292c497 BP |
75 | # ifdef CONFIG_X86 |
76 | size_t i; | |
77 | # endif | |
5742332d | 78 | const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3]; |
49ac572b | 79 | # if CRC_LE_BITS != 32 |
324eb0f1 | 80 | const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7]; |
49ac572b | 81 | # endif |
324eb0f1 | 82 | u32 q; |
ddcaccbc JT |
83 | |
84 | /* Align it */ | |
4f2a9463 | 85 | if (unlikely((long)buf & 3 && len)) { |
ddcaccbc | 86 | do { |
4f2a9463 JT |
87 | DO_CRC(*buf++); |
88 | } while ((--len) && ((long)buf)&3); | |
ddcaccbc | 89 | } |
324eb0f1 BP |
90 | |
91 | # if CRC_LE_BITS == 32 | |
ddcaccbc | 92 | rem_len = len & 3; |
ddcaccbc | 93 | len = len >> 2; |
324eb0f1 BP |
94 | # else |
95 | rem_len = len & 7; | |
96 | len = len >> 3; | |
97 | # endif | |
98 | ||
4f2a9463 | 99 | b = (const u32 *)buf; |
0292c497 BP |
100 | # ifdef CONFIG_X86 |
101 | --b; | |
102 | for (i = 0; i < len; i++) { | |
103 | # else | |
ddcaccbc | 104 | for (--b; len; --len) { |
0292c497 | 105 | # endif |
324eb0f1 BP |
106 | 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 | |
ddcaccbc JT |
114 | } |
115 | len = rem_len; | |
116 | /* And the last few bytes */ | |
117 | if (len) { | |
118 | u8 *p = (u8 *)(b + 1) - 1; | |
0292c497 BP |
119 | # ifdef CONFIG_X86 |
120 | for (i = 0; i < len; i++) | |
121 | DO_CRC(*++p); /* use pre increment for speed */ | |
122 | # else | |
ddcaccbc JT |
123 | do { |
124 | DO_CRC(*++p); /* use pre increment for speed */ | |
125 | } while (--len); | |
0292c497 | 126 | # endif |
ddcaccbc JT |
127 | } |
128 | return crc; | |
4f2a9463 | 129 | #undef DO_CRC |
836e2af9 | 130 | #undef DO_CRC4 |
324eb0f1 | 131 | #undef DO_CRC8 |
ddcaccbc JT |
132 | } |
133 | #endif | |
60e58d5c | 134 | |
6e95fcaa | 135 | |
2f72100c | 136 | /** |
f2e1d2ac GZ |
137 | * 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 | |
2f72100c | 142 | * @len: length of buffer @p |
f2e1d2ac GZ |
143 | * @tab: little-endian Ethernet table |
144 | * @polynomial: CRC32/CRC32c LE polynomial | |
2f72100c | 145 | */ |
46c5801e DW |
146 | static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p, |
147 | size_t len, const u32 (*tab)[256], | |
148 | u32 polynomial) | |
1da177e4 | 149 | { |
60e58d5c | 150 | #if CRC_LE_BITS == 1 |
1da177e4 LT |
151 | int i; |
152 | while (len--) { | |
153 | crc ^= *p++; | |
154 | for (i = 0; i < 8; i++) | |
46c5801e | 155 | crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0); |
1da177e4 | 156 | } |
60e58d5c | 157 | # elif CRC_LE_BITS == 2 |
1da177e4 LT |
158 | while (len--) { |
159 | crc ^= *p++; | |
46c5801e DW |
160 | 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]; | |
1da177e4 | 164 | } |
60e58d5c | 165 | # elif CRC_LE_BITS == 4 |
1da177e4 LT |
166 | while (len--) { |
167 | crc ^= *p++; | |
46c5801e DW |
168 | crc = (crc >> 4) ^ tab[0][crc & 15]; |
169 | crc = (crc >> 4) ^ tab[0][crc & 15]; | |
1da177e4 | 170 | } |
60e58d5c | 171 | # elif CRC_LE_BITS == 8 |
9a1dbf6a BP |
172 | /* aka Sarwate algorithm */ |
173 | while (len--) { | |
174 | crc ^= *p++; | |
46c5801e | 175 | crc = (crc >> 8) ^ tab[0][crc & 255]; |
9a1dbf6a BP |
176 | } |
177 | # else | |
ce4320dd | 178 | crc = (__force u32) __cpu_to_le32(crc); |
60e58d5c | 179 | crc = crc32_body(crc, p, len, tab); |
ce4320dd | 180 | crc = __le32_to_cpu((__force __le32)crc); |
60e58d5c | 181 | #endif |
1da177e4 | 182 | return crc; |
1da177e4 | 183 | } |
46c5801e DW |
184 | |
185 | #if CRC_LE_BITS == 1 | |
186 | u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len) | |
187 | { | |
e37f2f93 | 188 | return crc32_le_generic(crc, p, len, NULL, CRC32_POLY_LE); |
46c5801e DW |
189 | } |
190 | u32 __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 | |
195 | u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len) | |
196 | { | |
8f243af4 | 197 | return crc32_le_generic(crc, p, len, |
e37f2f93 | 198 | (const u32 (*)[256])crc32table_le, CRC32_POLY_LE); |
46c5801e DW |
199 | } |
200 | u32 __pure __crc32c_le(u32 crc, unsigned char const *p, size_t len) | |
201 | { | |
8f243af4 JM |
202 | return crc32_le_generic(crc, p, len, |
203 | (const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE); | |
46c5801e DW |
204 | } |
205 | #endif | |
6d514b4e GS |
206 | EXPORT_SYMBOL(crc32_le); |
207 | EXPORT_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 | */ | |
214 | static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus) | |
215 | { | |
216 | 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; | |
226 | } | |
227 | ||
228 | /** | |
8a29896a | 229 | * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time |
6d514b4e GS |
230 | * @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 | */ | |
240 | static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len, | |
241 | u32 polynomial) | |
242 | { | |
243 | 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; | |
268 | } | |
269 | ||
270 | u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len) | |
6e95fcaa | 271 | { |
e37f2f93 | 272 | return crc32_generic_shift(crc, len, CRC32_POLY_LE); |
6e95fcaa DB |
273 | } |
274 | ||
6d514b4e | 275 | u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len) |
6e95fcaa | 276 | { |
6d514b4e | 277 | return crc32_generic_shift(crc, len, CRC32C_POLY_LE); |
6e95fcaa | 278 | } |
6d514b4e GS |
279 | EXPORT_SYMBOL(crc32_le_shift); |
280 | EXPORT_SYMBOL(__crc32c_le_shift); | |
1da177e4 | 281 | |
2f72100c | 282 | /** |
f2e1d2ac | 283 | * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32 |
2f72100c RD |
284 | * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for |
285 | * other uses, or the previous crc32 value if computing incrementally. | |
f2e1d2ac | 286 | * @p: pointer to buffer over which CRC32 is run |
2f72100c | 287 | * @len: length of buffer @p |
f2e1d2ac GZ |
288 | * @tab: big-endian Ethernet table |
289 | * @polynomial: CRC32 BE polynomial | |
2f72100c | 290 | */ |
46c5801e DW |
291 | static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p, |
292 | size_t len, const u32 (*tab)[256], | |
293 | u32 polynomial) | |
1da177e4 | 294 | { |
60e58d5c | 295 | #if CRC_BE_BITS == 1 |
1da177e4 LT |
296 | int i; |
297 | while (len--) { | |
298 | crc ^= *p++ << 24; | |
299 | for (i = 0; i < 8; i++) | |
300 | crc = | |
46c5801e | 301 | (crc << 1) ^ ((crc & 0x80000000) ? polynomial : |
1da177e4 LT |
302 | 0); |
303 | } | |
60e58d5c | 304 | # elif CRC_BE_BITS == 2 |
1da177e4 LT |
305 | while (len--) { |
306 | crc ^= *p++ << 24; | |
46c5801e DW |
307 | 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]; | |
1da177e4 | 311 | } |
60e58d5c | 312 | # elif CRC_BE_BITS == 4 |
1da177e4 LT |
313 | while (len--) { |
314 | crc ^= *p++ << 24; | |
46c5801e DW |
315 | crc = (crc << 4) ^ tab[0][crc >> 28]; |
316 | crc = (crc << 4) ^ tab[0][crc >> 28]; | |
1da177e4 | 317 | } |
60e58d5c | 318 | # elif CRC_BE_BITS == 8 |
9a1dbf6a BP |
319 | while (len--) { |
320 | crc ^= *p++ << 24; | |
46c5801e | 321 | crc = (crc << 8) ^ tab[0][crc >> 24]; |
9a1dbf6a BP |
322 | } |
323 | # else | |
ce4320dd | 324 | crc = (__force u32) __cpu_to_be32(crc); |
60e58d5c | 325 | crc = crc32_body(crc, p, len, tab); |
ce4320dd | 326 | crc = __be32_to_cpu((__force __be32)crc); |
1da177e4 | 327 | # endif |
60e58d5c | 328 | return crc; |
1da177e4 | 329 | } |
46c5801e DW |
330 | |
331 | #if CRC_LE_BITS == 1 | |
332 | u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len) | |
333 | { | |
e37f2f93 | 334 | return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE); |
46c5801e DW |
335 | } |
336 | #else | |
337 | u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len) | |
338 | { | |
8f243af4 | 339 | return crc32_be_generic(crc, p, len, |
e37f2f93 | 340 | (const u32 (*)[256])crc32table_be, CRC32_POLY_BE); |
46c5801e DW |
341 | } |
342 | #endif | |
1da177e4 | 343 | EXPORT_SYMBOL(crc32_be); |