Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Bob Pearson | 78dff41 | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * 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 | |
Mauro Carvalho Chehab | 8e2a46a | 2020-06-15 08:50:25 +0200 | [diff] [blame] | 27 | /* see: Documentation/staging/crc32.rst for a description of algorithms */ |
Bob Pearson | fbedceb | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/crc32.h> |
Krzysztof Kozlowski | 1fb2e3f | 2018-07-17 18:05:36 +0200 | [diff] [blame] | 30 | #include <linux/crc32poly.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/types.h> |
Daniel Borkmann | cc0ac19 | 2013-11-04 17:10:26 +0100 | [diff] [blame] | 33 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "crc32defs.h" |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 35 | |
Bob Pearson | 9a1dbf6 | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 36 | #if CRC_LE_BITS > 8 |
Fabian Frederick | 38b4fe5 | 2014-06-04 16:11:56 -0700 | [diff] [blame] | 37 | # define tole(x) ((__force u32) cpu_to_le32(x)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #else |
Joakim Tjernlund | 4f2a9463 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 39 | # define tole(x) (x) |
| 40 | #endif |
| 41 | |
Bob Pearson | 9a1dbf6 | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 42 | #if CRC_BE_BITS > 8 |
Fabian Frederick | 38b4fe5 | 2014-06-04 16:11:56 -0700 | [diff] [blame] | 43 | # define tobe(x) ((__force u32) cpu_to_be32(x)) |
Joakim Tjernlund | 4f2a9463 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 44 | #else |
| 45 | # define tobe(x) (x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #endif |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include "crc32table.h" |
| 49 | |
| 50 | MODULE_AUTHOR("Matt Domsch <Matt_Domsch@dell.com>"); |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 51 | MODULE_DESCRIPTION("Various CRC32 calculations"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | MODULE_LICENSE("GPL"); |
| 53 | |
Bob Pearson | 9a1dbf6 | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 54 | #if CRC_LE_BITS > 8 || CRC_BE_BITS > 8 |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 55 | |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 56 | /* implements slicing-by-4 or slicing-by-8 algorithm */ |
George Spelvin | d8f1c47 | 2014-06-23 15:11:56 +0200 | [diff] [blame] | 57 | static inline u32 __pure |
Joakim Tjernlund | 836e2af | 2010-05-24 14:33:31 -0700 | [diff] [blame] | 58 | crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256]) |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 59 | { |
Andrew Morton | 0d2daf5 | 2010-05-25 23:43:03 -0700 | [diff] [blame] | 60 | # ifdef __LITTLE_ENDIAN |
Joakim Tjernlund | 5742332 | 2012-01-10 15:10:18 -0800 | [diff] [blame] | 61 | # define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8) |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 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]) |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 66 | # else |
Joakim Tjernlund | 5742332 | 2012-01-10 15:10:18 -0800 | [diff] [blame] | 67 | # define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8) |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 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]) |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 72 | # endif |
Joakim Tjernlund | 4f2a9463 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 73 | const u32 *b; |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 74 | size_t rem_len; |
Bob Pearson | 0292c49 | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 75 | # ifdef CONFIG_X86 |
| 76 | size_t i; |
| 77 | # endif |
Joakim Tjernlund | 5742332 | 2012-01-10 15:10:18 -0800 | [diff] [blame] | 78 | const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3]; |
Thiago Rafael Becker | 49ac572 | 2012-07-30 14:41:26 -0700 | [diff] [blame] | 79 | # if CRC_LE_BITS != 32 |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 80 | const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7]; |
Thiago Rafael Becker | 49ac572 | 2012-07-30 14:41:26 -0700 | [diff] [blame] | 81 | # endif |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 82 | u32 q; |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 83 | |
| 84 | /* Align it */ |
Joakim Tjernlund | 4f2a9463 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 85 | if (unlikely((long)buf & 3 && len)) { |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 86 | do { |
Joakim Tjernlund | 4f2a9463 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 87 | DO_CRC(*buf++); |
| 88 | } while ((--len) && ((long)buf)&3); |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 89 | } |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 90 | |
| 91 | # if CRC_LE_BITS == 32 |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 92 | rem_len = len & 3; |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 93 | len = len >> 2; |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 94 | # else |
| 95 | rem_len = len & 7; |
| 96 | len = len >> 3; |
| 97 | # endif |
| 98 | |
Joakim Tjernlund | 4f2a9463 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 99 | b = (const u32 *)buf; |
Bob Pearson | 0292c49 | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 100 | # ifdef CONFIG_X86 |
| 101 | --b; |
| 102 | for (i = 0; i < len; i++) { |
| 103 | # else |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 104 | for (--b; len; --len) { |
Bob Pearson | 0292c49 | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 105 | # endif |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 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 |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 114 | } |
| 115 | len = rem_len; |
| 116 | /* And the last few bytes */ |
| 117 | if (len) { |
| 118 | u8 *p = (u8 *)(b + 1) - 1; |
Bob Pearson | 0292c49 | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 119 | # ifdef CONFIG_X86 |
| 120 | for (i = 0; i < len; i++) |
| 121 | DO_CRC(*++p); /* use pre increment for speed */ |
| 122 | # else |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 123 | do { |
| 124 | DO_CRC(*++p); /* use pre increment for speed */ |
| 125 | } while (--len); |
Bob Pearson | 0292c49 | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 126 | # endif |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 127 | } |
| 128 | return crc; |
Joakim Tjernlund | 4f2a9463 | 2010-03-05 13:43:55 -0800 | [diff] [blame] | 129 | #undef DO_CRC |
Joakim Tjernlund | 836e2af | 2010-05-24 14:33:31 -0700 | [diff] [blame] | 130 | #undef DO_CRC4 |
Bob Pearson | 324eb0f | 2012-03-23 15:02:24 -0700 | [diff] [blame] | 131 | #undef DO_CRC8 |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 132 | } |
| 133 | #endif |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 134 | |
Daniel Borkmann | 6e95fca | 2013-10-30 11:50:49 +0100 | [diff] [blame] | 135 | |
Randy Dunlap | 2f72100 | 2006-06-25 05:48:59 -0700 | [diff] [blame] | 136 | /** |
Gu Zheng | f2e1d2a | 2013-09-11 14:23:52 -0700 | [diff] [blame] | 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 |
Randy Dunlap | 2f72100 | 2006-06-25 05:48:59 -0700 | [diff] [blame] | 142 | * @len: length of buffer @p |
Gu Zheng | f2e1d2a | 2013-09-11 14:23:52 -0700 | [diff] [blame] | 143 | * @tab: little-endian Ethernet table |
| 144 | * @polynomial: CRC32/CRC32c LE polynomial |
Randy Dunlap | 2f72100 | 2006-06-25 05:48:59 -0700 | [diff] [blame] | 145 | */ |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 150 | #if CRC_LE_BITS == 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | int i; |
| 152 | while (len--) { |
| 153 | crc ^= *p++; |
| 154 | for (i = 0; i < 8; i++) |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 155 | crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 157 | # elif CRC_LE_BITS == 2 |
| 158 | while (len--) { |
| 159 | crc ^= *p++; |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 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]; |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 164 | } |
| 165 | # elif CRC_LE_BITS == 4 |
| 166 | while (len--) { |
| 167 | crc ^= *p++; |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 168 | crc = (crc >> 4) ^ tab[0][crc & 15]; |
| 169 | crc = (crc >> 4) ^ tab[0][crc & 15]; |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 170 | } |
| 171 | # elif CRC_LE_BITS == 8 |
Bob Pearson | 9a1dbf6 | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 172 | /* aka Sarwate algorithm */ |
| 173 | while (len--) { |
| 174 | crc ^= *p++; |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 175 | crc = (crc >> 8) ^ tab[0][crc & 255]; |
Bob Pearson | 9a1dbf6 | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 176 | } |
| 177 | # else |
Bob Pearson | ce4320d | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 178 | crc = (__force u32) __cpu_to_le32(crc); |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 179 | crc = crc32_body(crc, p, len, tab); |
Bob Pearson | ce4320d | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 180 | crc = __le32_to_cpu((__force __le32)crc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | #endif |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 182 | return crc; |
| 183 | } |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 184 | |
| 185 | #if CRC_LE_BITS == 1 |
Ard Biesheuvel | 9784d82 | 2018-08-27 13:02:42 +0200 | [diff] [blame] | 186 | u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len) |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 187 | { |
Krzysztof Kozlowski | e37f2f9 | 2018-07-17 18:05:37 +0200 | [diff] [blame] | 188 | return crc32_le_generic(crc, p, len, NULL, CRC32_POLY_LE); |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 189 | } |
Ard Biesheuvel | 9784d82 | 2018-08-27 13:02:42 +0200 | [diff] [blame] | 190 | u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len) |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 191 | { |
| 192 | return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE); |
| 193 | } |
| 194 | #else |
Ard Biesheuvel | 9784d82 | 2018-08-27 13:02:42 +0200 | [diff] [blame] | 195 | u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len) |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 196 | { |
Joe Mario | 8f243af | 2012-10-04 17:12:15 -0700 | [diff] [blame] | 197 | return crc32_le_generic(crc, p, len, |
Krzysztof Kozlowski | e37f2f9 | 2018-07-17 18:05:37 +0200 | [diff] [blame] | 198 | (const u32 (*)[256])crc32table_le, CRC32_POLY_LE); |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 199 | } |
Ard Biesheuvel | 9784d82 | 2018-08-27 13:02:42 +0200 | [diff] [blame] | 200 | u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len) |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 201 | { |
Joe Mario | 8f243af | 2012-10-04 17:12:15 -0700 | [diff] [blame] | 202 | return crc32_le_generic(crc, p, len, |
| 203 | (const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE); |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 204 | } |
| 205 | #endif |
George Spelvin | 6d514b4 | 2014-06-23 15:11:54 +0200 | [diff] [blame] | 206 | EXPORT_SYMBOL(crc32_le); |
| 207 | EXPORT_SYMBOL(__crc32c_le); |
| 208 | |
Miguel Ojeda | ff98e20 | 2019-01-24 15:59:11 +0100 | [diff] [blame] | 209 | u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le); |
| 210 | u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le); |
Ard Biesheuvel | 9784d82 | 2018-08-27 13:02:42 +0200 | [diff] [blame] | 211 | |
George Spelvin | 6d514b4 | 2014-06-23 15:11:54 +0200 | [diff] [blame] | 212 | /* |
| 213 | * This multiplies the polynomials x and y modulo the given modulus. |
| 214 | * This follows the "little-endian" CRC convention that the lsbit |
| 215 | * represents the highest power of x, and the msbit represents x^0. |
| 216 | */ |
| 217 | static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus) |
Daniel Borkmann | 6e95fca | 2013-10-30 11:50:49 +0100 | [diff] [blame] | 218 | { |
George Spelvin | 6d514b4 | 2014-06-23 15:11:54 +0200 | [diff] [blame] | 219 | u32 product = x & 1 ? y : 0; |
| 220 | int i; |
| 221 | |
| 222 | for (i = 0; i < 31; i++) { |
| 223 | product = (product >> 1) ^ (product & 1 ? modulus : 0); |
| 224 | x >>= 1; |
| 225 | product ^= x & 1 ? y : 0; |
| 226 | } |
| 227 | |
| 228 | return product; |
Daniel Borkmann | 6e95fca | 2013-10-30 11:50:49 +0100 | [diff] [blame] | 229 | } |
| 230 | |
George Spelvin | 6d514b4 | 2014-06-23 15:11:54 +0200 | [diff] [blame] | 231 | /** |
Randy Dunlap | 8a29896 | 2017-09-08 16:35:55 -0700 | [diff] [blame] | 232 | * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time |
George Spelvin | 6d514b4 | 2014-06-23 15:11:54 +0200 | [diff] [blame] | 233 | * @crc: The original little-endian CRC (i.e. lsbit is x^31 coefficient) |
| 234 | * @len: The number of bytes. @crc is multiplied by x^(8*@len) |
| 235 | * @polynomial: The modulus used to reduce the result to 32 bits. |
| 236 | * |
| 237 | * It's possible to parallelize CRC computations by computing a CRC |
| 238 | * over separate ranges of a buffer, then summing them. |
| 239 | * This shifts the given CRC by 8*len bits (i.e. produces the same effect |
| 240 | * as appending len bytes of zero to the data), in time proportional |
| 241 | * to log(len). |
| 242 | */ |
| 243 | static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len, |
| 244 | u32 polynomial) |
Daniel Borkmann | 6e95fca | 2013-10-30 11:50:49 +0100 | [diff] [blame] | 245 | { |
George Spelvin | 6d514b4 | 2014-06-23 15:11:54 +0200 | [diff] [blame] | 246 | u32 power = polynomial; /* CRC of x^32 */ |
| 247 | int i; |
| 248 | |
| 249 | /* Shift up to 32 bits in the simple linear way */ |
| 250 | for (i = 0; i < 8 * (int)(len & 3); i++) |
| 251 | crc = (crc >> 1) ^ (crc & 1 ? polynomial : 0); |
| 252 | |
| 253 | len >>= 2; |
| 254 | if (!len) |
| 255 | return crc; |
| 256 | |
| 257 | for (;;) { |
| 258 | /* "power" is x^(2^i), modulo the polynomial */ |
| 259 | if (len & 1) |
| 260 | crc = gf2_multiply(crc, power, polynomial); |
| 261 | |
| 262 | len >>= 1; |
| 263 | if (!len) |
| 264 | break; |
| 265 | |
| 266 | /* Square power, advancing to x^(2^(i+1)) */ |
| 267 | power = gf2_multiply(power, power, polynomial); |
| 268 | } |
| 269 | |
| 270 | return crc; |
Daniel Borkmann | 6e95fca | 2013-10-30 11:50:49 +0100 | [diff] [blame] | 271 | } |
George Spelvin | 6d514b4 | 2014-06-23 15:11:54 +0200 | [diff] [blame] | 272 | |
| 273 | u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len) |
| 274 | { |
Krzysztof Kozlowski | e37f2f9 | 2018-07-17 18:05:37 +0200 | [diff] [blame] | 275 | return crc32_generic_shift(crc, len, CRC32_POLY_LE); |
George Spelvin | 6d514b4 | 2014-06-23 15:11:54 +0200 | [diff] [blame] | 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 | EXPORT_SYMBOL(crc32_le_shift); |
| 283 | EXPORT_SYMBOL(__crc32c_le_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Randy Dunlap | 2f72100 | 2006-06-25 05:48:59 -0700 | [diff] [blame] | 285 | /** |
Gu Zheng | f2e1d2a | 2013-09-11 14:23:52 -0700 | [diff] [blame] | 286 | * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32 |
Randy Dunlap | 2f72100 | 2006-06-25 05:48:59 -0700 | [diff] [blame] | 287 | * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for |
| 288 | * other uses, or the previous crc32 value if computing incrementally. |
Gu Zheng | f2e1d2a | 2013-09-11 14:23:52 -0700 | [diff] [blame] | 289 | * @p: pointer to buffer over which CRC32 is run |
Randy Dunlap | 2f72100 | 2006-06-25 05:48:59 -0700 | [diff] [blame] | 290 | * @len: length of buffer @p |
Gu Zheng | f2e1d2a | 2013-09-11 14:23:52 -0700 | [diff] [blame] | 291 | * @tab: big-endian Ethernet table |
| 292 | * @polynomial: CRC32 BE polynomial |
Randy Dunlap | 2f72100 | 2006-06-25 05:48:59 -0700 | [diff] [blame] | 293 | */ |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 294 | static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p, |
| 295 | size_t len, const u32 (*tab)[256], |
| 296 | u32 polynomial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 298 | #if CRC_BE_BITS == 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | int i; |
| 300 | while (len--) { |
| 301 | crc ^= *p++ << 24; |
| 302 | for (i = 0; i < 8; i++) |
| 303 | crc = |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 304 | (crc << 1) ^ ((crc & 0x80000000) ? polynomial : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | 0); |
| 306 | } |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 307 | # elif CRC_BE_BITS == 2 |
| 308 | while (len--) { |
| 309 | crc ^= *p++ << 24; |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 310 | crc = (crc << 2) ^ tab[0][crc >> 30]; |
| 311 | crc = (crc << 2) ^ tab[0][crc >> 30]; |
| 312 | crc = (crc << 2) ^ tab[0][crc >> 30]; |
| 313 | crc = (crc << 2) ^ tab[0][crc >> 30]; |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 314 | } |
| 315 | # elif CRC_BE_BITS == 4 |
| 316 | while (len--) { |
| 317 | crc ^= *p++ << 24; |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 318 | crc = (crc << 4) ^ tab[0][crc >> 28]; |
| 319 | crc = (crc << 4) ^ tab[0][crc >> 28]; |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 320 | } |
| 321 | # elif CRC_BE_BITS == 8 |
Bob Pearson | 9a1dbf6 | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 322 | while (len--) { |
| 323 | crc ^= *p++ << 24; |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 324 | crc = (crc << 8) ^ tab[0][crc >> 24]; |
Bob Pearson | 9a1dbf6 | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 325 | } |
| 326 | # else |
Bob Pearson | ce4320d | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 327 | crc = (__force u32) __cpu_to_be32(crc); |
Joakim Tjernlund | ddcaccb | 2009-12-14 18:01:33 -0800 | [diff] [blame] | 328 | crc = crc32_body(crc, p, len, tab); |
Bob Pearson | ce4320d | 2012-03-23 15:02:23 -0700 | [diff] [blame] | 329 | crc = __be32_to_cpu((__force __be32)crc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | # endif |
Bob Pearson | 60e58d5 | 2012-03-23 15:02:22 -0700 | [diff] [blame] | 331 | return crc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 333 | |
Tobias Jordan | 904542d | 2020-10-15 20:11:38 -0700 | [diff] [blame] | 334 | #if CRC_BE_BITS == 1 |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 335 | u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len) |
| 336 | { |
Krzysztof Kozlowski | e37f2f9 | 2018-07-17 18:05:37 +0200 | [diff] [blame] | 337 | return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE); |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 338 | } |
| 339 | #else |
| 340 | u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len) |
| 341 | { |
Joe Mario | 8f243af | 2012-10-04 17:12:15 -0700 | [diff] [blame] | 342 | return crc32_be_generic(crc, p, len, |
Krzysztof Kozlowski | e37f2f9 | 2018-07-17 18:05:37 +0200 | [diff] [blame] | 343 | (const u32 (*)[256])crc32table_be, CRC32_POLY_BE); |
Darrick J. Wong | 46c5801 | 2012-03-23 15:02:25 -0700 | [diff] [blame] | 344 | } |
| 345 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | EXPORT_SYMBOL(crc32_be); |