crc32.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 <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. /* see: Documentation/crc32.txt 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 <Matt_Domsch@dell.com>");
  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,
  184. (const u32 (*)[256])crc32table_le, CRC32_POLY_LE);
  185. }
  186. u32 __pure __weak __crc32c_le(u32 crc, unsigned char const *p, size_t len)
  187. {
  188. return crc32_le_generic(crc, p, len,
  189. (const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE);
  190. }
  191. #endif
  192. EXPORT_SYMBOL(crc32_le);
  193. EXPORT_SYMBOL(__crc32c_le);
  194. u32 crc32_le_base(u32, unsigned char const *, size_t) __alias(crc32_le);
  195. u32 __crc32c_le_base(u32, unsigned char const *, size_t) __alias(__crc32c_le);
  196. /*
  197. * This multiplies the polynomials x and y modulo the given modulus.
  198. * This follows the "little-endian" CRC convention that the lsbit
  199. * represents the highest power of x, and the msbit represents x^0.
  200. */
  201. static u32 __attribute_const__ gf2_multiply(u32 x, u32 y, u32 modulus)
  202. {
  203. u32 product = x & 1 ? y : 0;
  204. int i;
  205. for (i = 0; i < 31; i++) {
  206. product = (product >> 1) ^ (product & 1 ? modulus : 0);
  207. x >>= 1;
  208. product ^= x & 1 ? y : 0;
  209. }
  210. return product;
  211. }
  212. /**
  213. * crc32_generic_shift - Append @len 0 bytes to crc, in logarithmic time
  214. * @crc: The original little-endian CRC (i.e. lsbit is x^31 coefficient)
  215. * @len: The number of bytes. @crc is multiplied by x^(8*@len)
  216. * @polynomial: The modulus used to reduce the result to 32 bits.
  217. *
  218. * It's possible to parallelize CRC computations by computing a CRC
  219. * over separate ranges of a buffer, then summing them.
  220. * This shifts the given CRC by 8*len bits (i.e. produces the same effect
  221. * as appending len bytes of zero to the data), in time proportional
  222. * to log(len).
  223. */
  224. static u32 __attribute_const__ crc32_generic_shift(u32 crc, size_t len,
  225. u32 polynomial)
  226. {
  227. u32 power = polynomial; /* CRC of x^32 */
  228. int i;
  229. /* Shift up to 32 bits in the simple linear way */
  230. for (i = 0; i < 8 * (int)(len & 3); i++)
  231. crc = (crc >> 1) ^ (crc & 1 ? polynomial : 0);
  232. len >>= 2;
  233. if (!len)
  234. return crc;
  235. for (;;) {
  236. /* "power" is x^(2^i), modulo the polynomial */
  237. if (len & 1)
  238. crc = gf2_multiply(crc, power, polynomial);
  239. len >>= 1;
  240. if (!len)
  241. break;
  242. /* Square power, advancing to x^(2^(i+1)) */
  243. power = gf2_multiply(power, power, polynomial);
  244. }
  245. return crc;
  246. }
  247. u32 __attribute_const__ crc32_le_shift(u32 crc, size_t len)
  248. {
  249. return crc32_generic_shift(crc, len, CRC32_POLY_LE);
  250. }
  251. u32 __attribute_const__ __crc32c_le_shift(u32 crc, size_t len)
  252. {
  253. return crc32_generic_shift(crc, len, CRC32C_POLY_LE);
  254. }
  255. EXPORT_SYMBOL(crc32_le_shift);
  256. EXPORT_SYMBOL(__crc32c_le_shift);
  257. /**
  258. * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
  259. * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
  260. * other uses, or the previous crc32 value if computing incrementally.
  261. * @p: pointer to buffer over which CRC32 is run
  262. * @len: length of buffer @p
  263. * @tab: big-endian Ethernet table
  264. * @polynomial: CRC32 BE polynomial
  265. */
  266. static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
  267. size_t len, const u32 (*tab)[256],
  268. u32 polynomial)
  269. {
  270. #if CRC_BE_BITS == 1
  271. int i;
  272. while (len--) {
  273. crc ^= *p++ << 24;
  274. for (i = 0; i < 8; i++)
  275. crc =
  276. (crc << 1) ^ ((crc & 0x80000000) ? polynomial :
  277. 0);
  278. }
  279. # elif CRC_BE_BITS == 2
  280. while (len--) {
  281. crc ^= *p++ << 24;
  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. crc = (crc << 2) ^ tab[0][crc >> 30];
  286. }
  287. # elif CRC_BE_BITS == 4
  288. while (len--) {
  289. crc ^= *p++ << 24;
  290. crc = (crc << 4) ^ tab[0][crc >> 28];
  291. crc = (crc << 4) ^ tab[0][crc >> 28];
  292. }
  293. # elif CRC_BE_BITS == 8
  294. while (len--) {
  295. crc ^= *p++ << 24;
  296. crc = (crc << 8) ^ tab[0][crc >> 24];
  297. }
  298. # else
  299. crc = (__force u32) __cpu_to_be32(crc);
  300. crc = crc32_body(crc, p, len, tab);
  301. crc = __be32_to_cpu((__force __be32)crc);
  302. # endif
  303. return crc;
  304. }
  305. #if CRC_LE_BITS == 1
  306. u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
  307. {
  308. return crc32_be_generic(crc, p, len, NULL, CRC32_POLY_BE);
  309. }
  310. #else
  311. u32 __pure crc32_be(u32 crc, unsigned char const *p, size_t len)
  312. {
  313. return crc32_be_generic(crc, p, len,
  314. (const u32 (*)[256])crc32table_be, CRC32_POLY_BE);
  315. }
  316. #endif
  317. EXPORT_SYMBOL(crc32_be);