crc32.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  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. *
  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. /* see: Documentation/staging/crc32.rst for a description of algorithms */
  27. #include <linux/crc32.h>
  28. #include <linux/crc32poly.h>
  29. #include <linux/module.h>
  30. #include <linux/types.h>
  31. #include <linux/sched.h>
  32. #include "crc32defs.h"
  33. #if CRC_LE_BITS > 8
  34. # define tole(x) ((__force u32) cpu_to_le32(x))
  35. #else
  36. # define tole(x) (x)
  37. #endif
  38. #if CRC_BE_BITS > 8
  39. # define tobe(x) ((__force u32) cpu_to_be32(x))
  40. #else
  41. # define tobe(x) (x)
  42. #endif
  43. #include "crc32table.h"
  44. MODULE_AUTHOR("Matt Domsch <[email protected]>");
  45. MODULE_DESCRIPTION("Various CRC32 calculations");
  46. MODULE_LICENSE("GPL");
  47. #if CRC_LE_BITS > 8 || CRC_BE_BITS > 8
  48. /* implements slicing-by-4 or slicing-by-8 algorithm */
  49. static inline u32 __pure
  50. crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
  51. {
  52. # ifdef __LITTLE_ENDIAN
  53. # define DO_CRC(x) crc = t0[(crc ^ (x)) & 255] ^ (crc >> 8)
  54. # define DO_CRC4 (t3[(q) & 255] ^ t2[(q >> 8) & 255] ^ \
  55. t1[(q >> 16) & 255] ^ t0[(q >> 24) & 255])
  56. # define DO_CRC8 (t7[(q) & 255] ^ t6[(q >> 8) & 255] ^ \
  57. t5[(q >> 16) & 255] ^ t4[(q >> 24) & 255])
  58. # else
  59. # define DO_CRC(x) crc = t0[((crc >> 24) ^ (x)) & 255] ^ (crc << 8)
  60. # define DO_CRC4 (t0[(q) & 255] ^ t1[(q >> 8) & 255] ^ \
  61. t2[(q >> 16) & 255] ^ t3[(q >> 24) & 255])
  62. # define DO_CRC8 (t4[(q) & 255] ^ t5[(q >> 8) & 255] ^ \
  63. t6[(q >> 16) & 255] ^ t7[(q >> 24) & 255])
  64. # endif
  65. const u32 *b;
  66. size_t rem_len;
  67. # ifdef CONFIG_X86
  68. size_t i;
  69. # endif
  70. const u32 *t0=tab[0], *t1=tab[1], *t2=tab[2], *t3=tab[3];
  71. # if CRC_LE_BITS != 32
  72. const u32 *t4 = tab[4], *t5 = tab[5], *t6 = tab[6], *t7 = tab[7];
  73. # endif
  74. u32 q;
  75. /* Align it */
  76. if (unlikely((long)buf & 3 && len)) {
  77. do {
  78. DO_CRC(*buf++);
  79. } while ((--len) && ((long)buf)&3);
  80. }
  81. # if CRC_LE_BITS == 32
  82. rem_len = len & 3;
  83. len = len >> 2;
  84. # else
  85. rem_len = len & 7;
  86. len = len >> 3;
  87. # endif
  88. b = (const u32 *)buf;
  89. # ifdef CONFIG_X86
  90. --b;
  91. for (i = 0; i < len; i++) {
  92. # else
  93. for (--b; len; --len) {
  94. # endif
  95. q = crc ^ *++b; /* use pre increment for speed */
  96. # if CRC_LE_BITS == 32
  97. crc = DO_CRC4;
  98. # else
  99. crc = DO_CRC8;
  100. q = *++b;
  101. crc ^= DO_CRC4;
  102. # endif
  103. }
  104. len = rem_len;
  105. /* And the last few bytes */
  106. if (len) {
  107. u8 *p = (u8 *)(b + 1) - 1;
  108. # ifdef CONFIG_X86
  109. for (i = 0; i < len; i++)
  110. DO_CRC(*++p); /* use pre increment for speed */
  111. # else
  112. do {
  113. DO_CRC(*++p); /* use pre increment for speed */
  114. } while (--len);
  115. # endif
  116. }
  117. return crc;
  118. #undef DO_CRC
  119. #undef DO_CRC4
  120. #undef DO_CRC8
  121. }
  122. #endif
  123. /**
  124. * crc32_le_generic() - Calculate bitwise little-endian Ethernet AUTODIN II
  125. * CRC32/CRC32C
  126. * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for other
  127. * uses, or the previous crc32/crc32c value if computing incrementally.
  128. * @p: pointer to buffer over which CRC32/CRC32C is run
  129. * @len: length of buffer @p
  130. * @tab: little-endian Ethernet table
  131. * @polynomial: CRC32/CRC32c LE polynomial
  132. */
  133. static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
  134. size_t len, const u32 (*tab)[256],
  135. u32 polynomial)
  136. {
  137. #if CRC_LE_BITS == 1
  138. int i;
  139. while (len--) {
  140. crc ^= *p++;
  141. for (i = 0; i < 8; i++)
  142. crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
  143. }
  144. # elif CRC_LE_BITS == 2
  145. while (len--) {
  146. crc ^= *p++;
  147. crc = (crc >> 2) ^ tab[0][crc & 3];
  148. crc = (crc >> 2) ^ tab[0][crc & 3];
  149. crc = (crc >> 2) ^ tab[0][crc & 3];
  150. crc = (crc >> 2) ^ tab[0][crc & 3];
  151. }
  152. # elif CRC_LE_BITS == 4
  153. while (len--) {
  154. crc ^= *p++;
  155. crc = (crc >> 4) ^ tab[0][crc & 15];
  156. crc = (crc >> 4) ^ tab[0][crc & 15];
  157. }
  158. # elif CRC_LE_BITS == 8
  159. /* aka Sarwate algorithm */
  160. while (len--) {
  161. crc ^= *p++;
  162. crc = (crc >> 8) ^ tab[0][crc & 255];
  163. }
  164. # else
  165. crc = (__force u32) __cpu_to_le32(crc);
  166. crc = crc32_body(crc, p, len, tab);
  167. crc = __le32_to_cpu((__force __le32)crc);
  168. #endif
  169. return crc;
  170. }
  171. #if CRC_LE_BITS == 1
  172. u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
  173. {
  174. return crc32_le_generic(crc, p, len, NULL, CRC32_POLY_LE);
  175. }
  176. u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
  177. {
  178. return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE);
  179. }
  180. #else
  181. u32 __pure __weak crc32_le(u32 crc, unsigned char const *p, size_t len)
  182. {
  183. return crc32_le_generic(crc, p, len, crc32table_le, CRC32_POLY_LE);
  184. }
  185. u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
  186. {
  187. return crc32_le_generic(crc, p, len, crc32ctable_le, CRC32C_POLY_LE);
  188. }
  189. #endif
  190. EXPORT_SYMBOL(crc32_le);
  191. EXPORT_SYMBOL(__crc32c_le);
  192. u32 __pure crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
  193. u32 __pure __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
  194. u32 __pure crc32_be_base(u32, unsigned char const *, size_t) __alias(crc32_be);
  195. /*
  196. * This multiplies the polynomials x and y modulo the given modulus.
  197. * This follows the "little-endian" CRC convention that the lsbit
  198. * represents the highest power of x, and the msbit represents x^0.
  199. */
  200. static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus)
  201. {
  202. u32 product = x & 1 ? y : 0;
  203. int i;
  204. for (i = 0; i < 31; i++) {
  205. product = (product >> 1) ^ (product & 1 ? modulus : 0);
  206. x >>= 1;
  207. product ^= x & 1 ? y : 0;
  208. }
  209. return product;
  210. }
  211. /**
  212. * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time
  213. * @crc: The original little-endian CRC (i.e. lsbit is x^31 coefficient)
  214. * @len: The number of bytes. @crc is multiplied by x^(8*@len)
  215. * @polynomial: The modulus used to reduce the result to 32 bits.
  216. *
  217. * It's possible to parallelize CRC computations by computing a CRC
  218. * over separate ranges of a buffer, then summing them.
  219. * This shifts the given CRC by 8*len bits (i.e. produces the same effect
  220. * as appending len bytes of zero to the data), in time proportional
  221. * to log(len).
  222. */
  223. static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len,
  224. u32 polynomial)
  225. {
  226. u32 power = polynomial; /* CRC of x^32 */
  227. int i;
  228. /* Shift up to 32 bits in the simple linear way */
  229. for (i = 0; i < 8 * (int)(len & 3); i++)
  230. crc = (crc >> 1) ^ (crc & 1 ? polynomial : 0);
  231. len >>= 2;
  232. if (!len)
  233. return crc;
  234. for (;;) {
  235. /* "power" is x^(2^i), modulo the polynomial */
  236. if (len & 1)
  237. crc = gf2_multiply(crc, power, polynomial);
  238. len >>= 1;
  239. if (!len)
  240. break;
  241. /* Square power, advancing to x^(2^(i+1)) */
  242. power = gf2_multiply(power, power, polynomial);
  243. }
  244. return crc;
  245. }
  246. u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len)
  247. {
  248. return crc32_generic_shift(crc, len, CRC32_POLY_LE);
  249. }
  250. u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len)
  251. {
  252. return crc32_generic_shift(crc, len, CRC32C_POLY_LE);
  253. }
  254. EXPORT_SYMBOL(crc32_le_shift);
  255. EXPORT_SYMBOL(__crc32c_le_shift);
  256. /**
  257. * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
  258. * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
  259. * other uses, or the previous crc32 value if computing incrementally.
  260. * @p: pointer to buffer over which CRC32 is run
  261. * @len: length of buffer @p
  262. * @tab: big-endian Ethernet table
  263. * @polynomial: CRC32 BE polynomial
  264. */
  265. static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
  266. size_t len, const u32 (*tab)[256],
  267. u32 polynomial)
  268. {
  269. #if CRC_BE_BITS == 1
  270. int i;
  271. while (len--) {
  272. crc ^= *p++ << 24;
  273. for (i = 0; i < 8; i++)
  274. crc =
  275. (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
  276. 0);
  277. }
  278. # elif CRC_BE_BITS == 2
  279. while (len--) {
  280. crc ^= *p++ << 24;
  281. crc = (crc << 2) ^ tab[0][crc >> 30];
  282. crc = (crc << 2) ^ tab[0][crc >> 30];
  283. crc = (crc << 2) ^ tab[0][crc >> 30];
  284. crc = (crc << 2) ^ tab[0][crc >> 30];
  285. }
  286. # elif CRC_BE_BITS == 4
  287. while (len--) {
  288. crc ^= *p++ << 24;
  289. crc = (crc << 4) ^ tab[0][crc >> 28];
  290. crc = (crc << 4) ^ tab[0][crc >> 28];
  291. }
  292. # elif CRC_BE_BITS == 8
  293. while (len--) {
  294. crc ^= *p++ << 24;
  295. crc = (crc << 8) ^ tab[0][crc >> 24];
  296. }
  297. # else
  298. crc = (__force u32) __cpu_to_be32(crc);
  299. crc = crc32_body(crc, p, len, tab);
  300. crc = __be32_to_cpu((__force __be32)crc);
  301. # endif
  302. return crc;
  303. }
  304. #if CRC_BE_BITS == 1
  305. u32 __pure __weak crc32_be(u32 crc, unsigned char const *p, size_t len)
  306. {
  307. return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE);
  308. }
  309. #else
  310. u32 __pure __weak crc32_be(u32 crc, unsigned char const *p, size_t len)
  311. {
  312. return crc32_be_generic(crc, p, len, crc32table_be, CRC32_POLY_BE);
  313. }
  314. #endif
  315. EXPORT_SYMBOL(crc32_be);