]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: GPL-2.0 |
7e280e68 DC |
2 | /* |
3 | * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin | |
4 | * cleaned up code to current version of sparse and added the slicing-by-8 | |
5 | * algorithm to the closely similar existing slicing-by-4 algorithm. | |
7e280e68 DC |
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. | |
7e280e68 DC |
11 | * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com> |
12 | * Same crc32 function was used in 5 other places in the kernel. | |
13 | * I made one version, and deleted the others. | |
14 | * There are various incantations of crc32(). Some use a seed of 0 or ~0. | |
15 | * Some xor at the end with ~0. The generic crc32() function takes | |
16 | * seed as an argument, and doesn't xor at the end. Then individual | |
17 | * users can do whatever they need. | |
18 | * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0. | |
19 | * fs/jffs2 uses seed 0, doesn't xor with ~0. | |
20 | * fs/partitions/efi.c uses seed ~0, xor's with ~0. | |
7e280e68 DC |
21 | */ |
22 | ||
23 | /* see: Documentation/crc32.txt for a description of algorithms */ | |
24 | ||
25 | /* | |
26 | * lifted from the 3.8-rc2 kernel source for xfsprogs. Killed CONFIG_X86 | |
27 | * specific bits for just the generic algorithm. Also removed the big endian | |
28 | * version of the algorithm as XFS only uses the little endian CRC version to | |
29 | * match the hardware acceleration available on Intel CPUs. | |
30 | */ | |
31 | ||
27aa8086 DW |
32 | /* |
33 | * Do not include platform_defs.h here; this will break cross builds if the | |
34 | * build host does not have liburcu-dev installed. | |
35 | */ | |
36 | #include <stdio.h> | |
37 | #include <sys/types.h> | |
b6571855 ES |
38 | #include <inttypes.h> |
39 | #include <asm/types.h> | |
40 | #include <sys/time.h> | |
b6571855 | 41 | /* For endian conversion routines */ |
6b803e5a | 42 | #include "xfs_arch.h" |
7e280e68 | 43 | #include "crc32defs.h" |
4a87b332 | 44 | #include "crc32c.h" |
7e280e68 DC |
45 | |
46 | /* types specifc to this file */ | |
47 | typedef __u8 u8; | |
48 | typedef __u16 u16; | |
49 | typedef __u32 u32; | |
50 | typedef __u32 u64; | |
51 | #define __pure | |
52 | ||
53 | #if CRC_LE_BITS > 8 | |
54 | # define tole(x) ((__force u32) __constant_cpu_to_le32(x)) | |
55 | #else | |
56 | # define tole(x) (x) | |
57 | #endif | |
58 | ||
7e280e68 DC |
59 | #include "crc32table.h" |
60 | ||
b123033a | 61 | #if CRC_LE_BITS > 8 |
7e280e68 DC |
62 | |
63 | /* implements slicing-by-4 or slicing-by-8 algorithm */ | |
64 | static inline u32 | |
65 | crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256]) | |
66 | { | |
b2dbd6a9 | 67 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
7e280e68 DC |
68 | # define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8) |
69 | # define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \ | |
70 | t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255]) | |
71 | # define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \ | |
72 | t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255]) | |
b2dbd6a9 | 73 | # elif __BYTE_ORDER == __BIG_ENDIAN |
7e280e68 DC |
74 | # define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8) |
75 | # define DO_CRC4 (t0[(q) & 255] ^ t1[(q >> 8) & 255] ^ \ | |
76 | t2[(q >> 16) & 255] ^ t3[(q >> 24) & 255]) | |
77 | # define DO_CRC8 (t4[(q) & 255] ^ t5[(q >> 8) & 255] ^ \ | |
78 | t6[(q >> 16) & 255] ^ t7[(q >> 24) & 255]) | |
b2dbd6a9 ES |
79 | # else |
80 | # error What endian are you? | |
7e280e68 DC |
81 | # endif |
82 | const u32 *b; | |
83 | size_t rem_len; | |
84 | const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3]; | |
85 | # if CRC_LE_BITS != 32 | |
86 | const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7]; | |
87 | # endif | |
88 | u32 q; | |
89 | ||
90 | /* Align it */ | |
91 | if (((long)buf & 3) && len) { | |
92 | do { | |
93 | DO_CRC(*buf++); | |
94 | } while ((--len) && ((long)buf)&3); | |
95 | } | |
96 | ||
97 | # if CRC_LE_BITS == 32 | |
98 | rem_len = len & 3; | |
99 | len = len >> 2; | |
100 | # else | |
101 | rem_len = len & 7; | |
102 | len = len >> 3; | |
103 | # endif | |
104 | ||
105 | b = (const u32 *)buf; | |
106 | for (--b; len; --len) { | |
107 | q = crc ^ *++b; /* use pre increment for speed */ | |
108 | # if CRC_LE_BITS == 32 | |
109 | crc = DO_CRC4; | |
110 | # else | |
111 | crc = DO_CRC8; | |
112 | q = *++b; | |
113 | crc ^= DO_CRC4; | |
114 | # endif | |
115 | } | |
116 | len = rem_len; | |
117 | /* And the last few bytes */ | |
118 | if (len) { | |
119 | u8 *p = (u8 *)(b + 1) - 1; | |
120 | do { | |
121 | DO_CRC(*++p); /* use pre increment for speed */ | |
122 | } while (--len); | |
123 | } | |
124 | return crc; | |
125 | #undef DO_CRC | |
126 | #undef DO_CRC4 | |
127 | #undef DO_CRC8 | |
128 | } | |
129 | #endif | |
130 | ||
131 | /** | |
132 | * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32 | |
133 | * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for | |
134 | * other uses, or the previous crc32 value if computing incrementally. | |
135 | * @p: pointer to buffer over which CRC is run | |
136 | * @len: length of buffer @p | |
137 | */ | |
138 | static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p, | |
139 | size_t len, const u32 (*tab)[256], | |
140 | u32 polynomial) | |
141 | { | |
142 | #if CRC_LE_BITS == 1 | |
143 | int i; | |
144 | while (len--) { | |
145 | crc ^= *p++; | |
146 | for (i = 0; i < 8; i++) | |
147 | crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0); | |
148 | } | |
149 | # elif CRC_LE_BITS == 2 | |
150 | while (len--) { | |
151 | crc ^= *p++; | |
152 | crc = (crc >> 2) ^ tab[0][crc & 3]; | |
153 | crc = (crc >> 2) ^ tab[0][crc & 3]; | |
154 | crc = (crc >> 2) ^ tab[0][crc & 3]; | |
155 | crc = (crc >> 2) ^ tab[0][crc & 3]; | |
156 | } | |
157 | # elif CRC_LE_BITS == 4 | |
158 | while (len--) { | |
159 | crc ^= *p++; | |
160 | crc = (crc >> 4) ^ tab[0][crc & 15]; | |
161 | crc = (crc >> 4) ^ tab[0][crc & 15]; | |
162 | } | |
163 | # elif CRC_LE_BITS == 8 | |
164 | /* aka Sarwate algorithm */ | |
165 | while (len--) { | |
166 | crc ^= *p++; | |
167 | crc = (crc >> 8) ^ tab[0][crc & 255]; | |
168 | } | |
169 | # else | |
170 | crc = (__force u32) cpu_to_le32(crc); | |
171 | crc = crc32_body(crc, p, len, tab); | |
172 | crc = le32_to_cpu((__force __le32)crc); | |
173 | #endif | |
174 | return crc; | |
175 | } | |
176 | ||
177 | #if CRC_LE_BITS == 1 | |
7e280e68 DC |
178 | u32 __pure crc32c_le(u32 crc, unsigned char const *p, size_t len) |
179 | { | |
180 | return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE); | |
181 | } | |
182 | #else | |
7e280e68 DC |
183 | u32 __pure crc32c_le(u32 crc, unsigned char const *p, size_t len) |
184 | { | |
185 | return crc32_le_generic(crc, p, len, | |
186 | (const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE); | |
187 | } | |
188 | #endif | |
189 | ||
190 | ||
191 | #ifdef CRC32_SELFTEST | |
99a5535c | 192 | # include "crc32cselftest.h" |
7e280e68 | 193 | |
7e280e68 DC |
194 | /* |
195 | * make sure we always return 0 for a successful test run, and non-zero for a | |
196 | * failed run. The build infrastructure is looking for this information to | |
197 | * determine whether to allow the build to proceed. | |
198 | */ | |
199 | int main(int argc, char **argv) | |
200 | { | |
201 | int errors; | |
202 | ||
203 | printf("CRC_LE_BITS = %d\n", CRC_LE_BITS); | |
204 | ||
ca14a570 | 205 | errors = crc32c_test(0); |
7e280e68 DC |
206 | |
207 | return errors != 0; | |
208 | } | |
209 | #endif /* CRC32_SELFTEST */ |