crc32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* crc32.c -- compute the CRC-32 of a data stream
  2. * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. *
  5. * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
  6. * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
  7. * tables for updating the shift register in one step with three exclusive-ors
  8. * instead of four steps with four exclusive-ors. This results in about a
  9. * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
  10. */
  11. /* @(#) $Id: crc32.c,v 1.1.1.2 2002/03/11 21:53:23 tromey Exp $ */
  12. /*
  13. Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
  14. protection on the static variables used to control the first-use generation
  15. of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
  16. first call get_crc_table() to initialize the tables before allowing more than
  17. one thread to use crc32().
  18. DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
  19. */
  20. #ifdef MAKECRCH
  21. # include <stdio.h>
  22. # ifndef DYNAMIC_CRC_TABLE
  23. # define DYNAMIC_CRC_TABLE
  24. # endif /* !DYNAMIC_CRC_TABLE */
  25. #endif /* MAKECRCH */
  26. #include "zutil.h" /* for STDC and FAR definitions */
  27. #define local static
  28. /* Definitions for doing the crc four data bytes at a time. */
  29. #if !defined(NOBYFOUR) && defined(Z_U4)
  30. # define BYFOUR
  31. #endif
  32. #ifdef BYFOUR
  33. local unsigned long crc32_little OF((unsigned long,
  34. const unsigned char FAR *, unsigned));
  35. local unsigned long crc32_big OF((unsigned long,
  36. const unsigned char FAR *, unsigned));
  37. # define TBLS 8
  38. #else
  39. # define TBLS 1
  40. #endif /* BYFOUR */
  41. /* Local functions for crc concatenation */
  42. local unsigned long gf2_matrix_times OF((unsigned long *mat,
  43. unsigned long vec));
  44. local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat));
  45. local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
  46. #ifdef DYNAMIC_CRC_TABLE
  47. local volatile int crc_table_empty = 1;
  48. local z_crc_t FAR crc_table[TBLS][256];
  49. local void make_crc_table OF((void));
  50. #ifdef MAKECRCH
  51. local void write_table OF((FILE *, const z_crc_t FAR *));
  52. #endif /* MAKECRCH */
  53. /*
  54. Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
  55. x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
  56. Polynomials over GF(2) are represented in binary, one bit per coefficient,
  57. with the lowest powers in the most significant bit. Then adding polynomials
  58. is just exclusive-or, and multiplying a polynomial by x is a right shift by
  59. one. If we call the above polynomial p, and represent a byte as the
  60. polynomial q, also with the lowest power in the most significant bit (so the
  61. byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
  62. where a mod b means the remainder after dividing a by b.
  63. This calculation is done using the shift-register method of multiplying and
  64. taking the remainder. The register is initialized to zero, and for each
  65. incoming bit, x^32 is added mod p to the register if the bit is a one (where
  66. x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
  67. x (which is shifting right by one and adding x^32 mod p if the bit shifted
  68. out is a one). We start with the highest power (least significant bit) of
  69. q and repeat for all eight bits of q.
  70. The first table is simply the CRC of all possible eight bit values. This is
  71. all the information needed to generate CRCs on data a byte at a time for all
  72. combinations of CRC register values and incoming bytes. The remaining tables
  73. allow for word-at-a-time CRC calculation for both big-endian and little-
  74. endian machines, where a word is four bytes.
  75. */
  76. local void make_crc_table()
  77. {
  78. z_crc_t c;
  79. int n, k;
  80. z_crc_t poly; /* polynomial exclusive-or pattern */
  81. /* terms of polynomial defining this crc (except x^32): */
  82. static volatile int first = 1; /* flag to limit concurrent making */
  83. static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
  84. /* See if another task is already doing this (not thread-safe, but better
  85. than nothing -- significantly reduces duration of vulnerability in
  86. case the advice about DYNAMIC_CRC_TABLE is ignored) */
  87. if (first) {
  88. first = 0;
  89. /* make exclusive-or pattern from polynomial (0xedb88320UL) */
  90. poly = 0;
  91. for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
  92. poly |= (z_crc_t)1 << (31 - p[n]);
  93. /* generate a crc for every 8-bit value */
  94. for (n = 0; n < 256; n++) {
  95. c = (z_crc_t)n;
  96. for (k = 0; k < 8; k++)
  97. c = c & 1 ? poly ^ (c >> 1) : c >> 1;
  98. crc_table[0][n] = c;
  99. }
  100. #ifdef BYFOUR
  101. /* generate crc for each value followed by one, two, and three zeros,
  102. and then the byte reversal of those as well as the first table */
  103. for (n = 0; n < 256; n++) {
  104. c = crc_table[0][n];
  105. crc_table[4][n] = ZSWAP32(c);
  106. for (k = 1; k < 4; k++) {
  107. c = crc_table[0][c & 0xff] ^ (c >> 8);
  108. crc_table[k][n] = c;
  109. crc_table[k + 4][n] = ZSWAP32(c);
  110. }
  111. }
  112. #endif /* BYFOUR */
  113. crc_table_empty = 0;
  114. }
  115. else { /* not first */
  116. /* wait for the other guy to finish (not efficient, but rare) */
  117. while (crc_table_empty)
  118. ;
  119. }
  120. #ifdef MAKECRCH
  121. /* write out CRC tables to crc32.h */
  122. {
  123. FILE *out;
  124. out = fopen("crc32.h", "w");
  125. if (out == NULL) return;
  126. fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
  127. fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
  128. fprintf(out, "local const z_crc_t FAR ");
  129. fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
  130. write_table(out, crc_table[0]);
  131. # ifdef BYFOUR
  132. fprintf(out, "#ifdef BYFOUR\n");
  133. for (k = 1; k < 8; k++) {
  134. fprintf(out, " },\n {\n");
  135. write_table(out, crc_table[k]);
  136. }
  137. fprintf(out, "#endif\n");
  138. # endif /* BYFOUR */
  139. fprintf(out, " }\n};\n");
  140. fclose(out);
  141. }
  142. #endif /* MAKECRCH */
  143. }
  144. #ifdef MAKECRCH
  145. local void write_table(out, table)
  146. FILE *out;
  147. const z_crc_t FAR *table;
  148. {
  149. int n;
  150. for (n = 0; n < 256; n++)
  151. fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",
  152. (unsigned long)(table[n]),
  153. n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
  154. }
  155. #endif /* MAKECRCH */
  156. #else /* !DYNAMIC_CRC_TABLE */
  157. /* ========================================================================
  158. * Tables of CRC-32s of all single-byte values, made by make_crc_table().
  159. */
  160. #include "crc32.h"
  161. #endif /* DYNAMIC_CRC_TABLE */
  162. /* =========================================================================
  163. * This function can be used by asm versions of crc32()
  164. */
  165. const z_crc_t FAR * ZEXPORT get_crc_table()
  166. {
  167. #ifdef DYNAMIC_CRC_TABLE
  168. if (crc_table_empty)
  169. make_crc_table();
  170. #endif /* DYNAMIC_CRC_TABLE */
  171. return (const z_crc_t FAR *)crc_table;
  172. }
  173. /* ========================================================================= */
  174. #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
  175. #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
  176. /* ========================================================================= */
  177. unsigned long ZEXPORT crc32(crc, buf, len)
  178. unsigned long crc;
  179. const unsigned char FAR *buf;
  180. uInt len;
  181. {
  182. if (buf == Z_NULL) return 0UL;
  183. #ifdef DYNAMIC_CRC_TABLE
  184. if (crc_table_empty)
  185. make_crc_table();
  186. #endif /* DYNAMIC_CRC_TABLE */
  187. #ifdef BYFOUR
  188. if (sizeof(void *) == sizeof(ptrdiff_t)) {
  189. z_crc_t endian;
  190. endian = 1;
  191. if (*((unsigned char *)(&endian)))
  192. return crc32_little(crc, buf, len);
  193. else
  194. return crc32_big(crc, buf, len);
  195. }
  196. #endif /* BYFOUR */
  197. crc = crc ^ 0xffffffffUL;
  198. while (len >= 8) {
  199. DO8;
  200. len -= 8;
  201. }
  202. if (len) do {
  203. DO1;
  204. } while (--len);
  205. return crc ^ 0xffffffffUL;
  206. }
  207. #ifdef BYFOUR
  208. /* ========================================================================= */
  209. #define DOLIT4 c ^= *buf4++; \
  210. c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
  211. crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24]
  212. #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4
  213. /* ========================================================================= */
  214. local unsigned long crc32_little(crc, buf, len)
  215. unsigned long crc;
  216. const unsigned char FAR *buf;
  217. unsigned len;
  218. {
  219. register z_crc_t c;
  220. register const z_crc_t FAR *buf4;
  221. c = (z_crc_t)crc;
  222. c = ~c;
  223. while (len && ((ptrdiff_t)buf & 3)) {
  224. c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
  225. len--;
  226. }
  227. buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
  228. while (len >= 32) {
  229. DOLIT32;
  230. len -= 32;
  231. }
  232. while (len >= 4) {
  233. DOLIT4;
  234. len -= 4;
  235. }
  236. buf = (const unsigned char FAR *)buf4;
  237. if (len) do {
  238. c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
  239. } while (--len);
  240. c = ~c;
  241. return (unsigned long)c;
  242. }
  243. /* ========================================================================= */
  244. #define DOBIG4 c ^= *++buf4; \
  245. c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
  246. crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
  247. #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
  248. /* ========================================================================= */
  249. local unsigned long crc32_big(crc, buf, len)
  250. unsigned long crc;
  251. const unsigned char FAR *buf;
  252. unsigned len;
  253. {
  254. register z_crc_t c;
  255. register const z_crc_t FAR *buf4;
  256. c = ZSWAP32((z_crc_t)crc);
  257. c = ~c;
  258. while (len && ((ptrdiff_t)buf & 3)) {
  259. c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
  260. len--;
  261. }
  262. buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
  263. buf4--;
  264. while (len >= 32) {
  265. DOBIG32;
  266. len -= 32;
  267. }
  268. while (len >= 4) {
  269. DOBIG4;
  270. len -= 4;
  271. }
  272. buf4++;
  273. buf = (const unsigned char FAR *)buf4;
  274. if (len) do {
  275. c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
  276. } while (--len);
  277. c = ~c;
  278. return (unsigned long)(ZSWAP32(c));
  279. }
  280. #endif /* BYFOUR */
  281. #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */
  282. /* ========================================================================= */
  283. local unsigned long gf2_matrix_times(mat, vec)
  284. unsigned long *mat;
  285. unsigned long vec;
  286. {
  287. unsigned long sum;
  288. sum = 0;
  289. while (vec) {
  290. if (vec & 1)
  291. sum ^= *mat;
  292. vec >>= 1;
  293. mat++;
  294. }
  295. return sum;
  296. }
  297. /* ========================================================================= */
  298. local void gf2_matrix_square(square, mat)
  299. unsigned long *square;
  300. unsigned long *mat;
  301. {
  302. int n;
  303. for (n = 0; n < GF2_DIM; n++)
  304. square[n] = gf2_matrix_times(mat, mat[n]);
  305. }
  306. /* ========================================================================= */
  307. local uLong crc32_combine_(crc1, crc2, len2)
  308. uLong crc1;
  309. uLong crc2;
  310. z_off64_t len2;
  311. {
  312. int n;
  313. unsigned long row;
  314. unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
  315. unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
  316. /* degenerate case (also disallow negative lengths) */
  317. if (len2 <= 0)
  318. return crc1;
  319. /* put operator for one zero bit in odd */
  320. odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
  321. row = 1;
  322. for (n = 1; n < GF2_DIM; n++) {
  323. odd[n] = row;
  324. row <<= 1;
  325. }
  326. /* put operator for two zero bits in even */
  327. gf2_matrix_square(even, odd);
  328. /* put operator for four zero bits in odd */
  329. gf2_matrix_square(odd, even);
  330. /* apply len2 zeros to crc1 (first square will put the operator for one
  331. zero byte, eight zero bits, in even) */
  332. do {
  333. /* apply zeros operator for this bit of len2 */
  334. gf2_matrix_square(even, odd);
  335. if (len2 & 1)
  336. crc1 = gf2_matrix_times(even, crc1);
  337. len2 >>= 1;
  338. /* if no more bits set, then done */
  339. if (len2 == 0)
  340. break;
  341. /* another iteration of the loop with odd and even swapped */
  342. gf2_matrix_square(odd, even);
  343. if (len2 & 1)
  344. crc1 = gf2_matrix_times(odd, crc1);
  345. len2 >>= 1;
  346. /* if no more bits set, then done */
  347. } while (len2 != 0);
  348. /* return combined crc */
  349. crc1 ^= crc2;
  350. return crc1;
  351. }
  352. /* ========================================================================= */
  353. uLong ZEXPORT crc32_combine(crc1, crc2, len2)
  354. uLong crc1;
  355. uLong crc2;
  356. z_off_t len2;
  357. {
  358. return crc32_combine_(crc1, crc2, len2);
  359. }
  360. uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
  361. uLong crc1;
  362. uLong crc2;
  363. z_off64_t len2;
  364. {
  365. return crc32_combine_(crc1, crc2, len2);
  366. }