| OLD | NEW |
| 1 /* crc32.c -- compute the CRC-32 of a data stream | 1 /* crc32.c -- compute the CRC-32 of a data stream |
| 2 * Copyright (C) 1995-2005 Mark Adler | 2 * Copyright (C) 1995-2006, 2010 Mark Adler |
| 3 * For conditions of distribution and use, see copyright notice in zlib.h | 3 * For conditions of distribution and use, see copyright notice in zlib.h |
| 4 * | 4 * |
| 5 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster | 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 | 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 | 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 | 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. | 9 * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. |
| 10 */ | 10 */ |
| 11 | 11 |
| 12 /* @(#) $Id: crc32.c,v 3.6 2005/08/04 19:14:14 tor%cs.brown.edu Exp $ */ | 12 /* @(#) $Id$ */ |
| 13 | 13 |
| 14 /* | 14 /* |
| 15 Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore | 15 Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore |
| 16 protection on the static variables used to control the first-use generation | 16 protection on the static variables used to control the first-use generation |
| 17 of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should | 17 of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should |
| 18 first call get_crc_table() to initialize the tables before allowing more than | 18 first call get_crc_table() to initialize the tables before allowing more than |
| 19 one thread to use crc32(). | 19 one thread to use crc32(). |
| 20 */ | 20 */ |
| 21 | 21 |
| 22 #ifdef MAKECRCH | 22 #ifdef MAKECRCH |
| (...skipping 23 matching lines...) Expand all Loading... |
| 46 # else | 46 # else |
| 47 # undef BYFOUR /* can't find a four-byte integer type! */ | 47 # undef BYFOUR /* can't find a four-byte integer type! */ |
| 48 # endif | 48 # endif |
| 49 # endif | 49 # endif |
| 50 # endif | 50 # endif |
| 51 # endif /* STDC */ | 51 # endif /* STDC */ |
| 52 #endif /* !NOBYFOUR */ | 52 #endif /* !NOBYFOUR */ |
| 53 | 53 |
| 54 /* Definitions for doing the crc four data bytes at a time. */ | 54 /* Definitions for doing the crc four data bytes at a time. */ |
| 55 #ifdef BYFOUR | 55 #ifdef BYFOUR |
| 56 # define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \ | 56 # define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \ |
| 57 (((w)&0xff00)<<8)+(((w)&0xff)<<24)) | 57 (((w)&0xff00)<<8)+(((w)&0xff)<<24)) |
| 58 local unsigned long crc32_little OF((unsigned long, | 58 local unsigned long crc32_little OF((unsigned long, |
| 59 const unsigned char FAR *, unsigned)); | 59 const unsigned char FAR *, unsigned)); |
| 60 local unsigned long crc32_big OF((unsigned long, | 60 local unsigned long crc32_big OF((unsigned long, |
| 61 const unsigned char FAR *, unsigned)); | 61 const unsigned char FAR *, unsigned)); |
| 62 # define TBLS 8 | 62 # define TBLS 8 |
| 63 #else | 63 #else |
| 64 # define TBLS 1 | 64 # define TBLS 1 |
| 65 #endif /* BYFOUR */ | 65 #endif /* BYFOUR */ |
| 66 | 66 |
| 67 /* Local functions for crc concatenation */ | 67 /* Local functions for crc concatenation */ |
| 68 local unsigned long gf2_matrix_times OF((unsigned long *mat, | 68 local unsigned long gf2_matrix_times OF((unsigned long *mat, |
| 69 unsigned long vec)); | 69 unsigned long vec)); |
| 70 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); | 70 local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); |
| 71 local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2); |
| 72 |
| 71 | 73 |
| 72 #ifdef DYNAMIC_CRC_TABLE | 74 #ifdef DYNAMIC_CRC_TABLE |
| 73 | 75 |
| 74 local volatile int crc_table_empty = 1; | 76 local volatile int crc_table_empty = 1; |
| 75 local unsigned long FAR crc_table[TBLS][256]; | 77 local unsigned long FAR crc_table[TBLS][256]; |
| 76 local void make_crc_table OF((void)); | 78 local void make_crc_table OF((void)); |
| 77 #ifdef MAKECRCH | 79 #ifdef MAKECRCH |
| 78 local void write_table OF((FILE *, const unsigned long FAR *)); | 80 local void write_table OF((FILE *, const unsigned long FAR *)); |
| 79 #endif /* MAKECRCH */ | 81 #endif /* MAKECRCH */ |
| 80 /* | 82 /* |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 } | 214 } |
| 213 | 215 |
| 214 /* ========================================================================= */ | 216 /* ========================================================================= */ |
| 215 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) | 217 #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) |
| 216 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 | 218 #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 |
| 217 | 219 |
| 218 /* ========================================================================= */ | 220 /* ========================================================================= */ |
| 219 unsigned long ZEXPORT crc32(crc, buf, len) | 221 unsigned long ZEXPORT crc32(crc, buf, len) |
| 220 unsigned long crc; | 222 unsigned long crc; |
| 221 const unsigned char FAR *buf; | 223 const unsigned char FAR *buf; |
| 222 unsigned len; | 224 uInt len; |
| 223 { | 225 { |
| 224 if (buf == Z_NULL) return 0UL; | 226 if (buf == Z_NULL) return 0UL; |
| 225 | 227 |
| 226 #ifdef DYNAMIC_CRC_TABLE | 228 #ifdef DYNAMIC_CRC_TABLE |
| 227 if (crc_table_empty) | 229 if (crc_table_empty) |
| 228 make_crc_table(); | 230 make_crc_table(); |
| 229 #endif /* DYNAMIC_CRC_TABLE */ | 231 #endif /* DYNAMIC_CRC_TABLE */ |
| 230 | 232 |
| 231 #ifdef BYFOUR | 233 #ifdef BYFOUR |
| 232 if (sizeof(void *) == sizeof(ptrdiff_t)) { | 234 if (sizeof(void *) == sizeof(ptrdiff_t)) { |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 unsigned long *square; | 362 unsigned long *square; |
| 361 unsigned long *mat; | 363 unsigned long *mat; |
| 362 { | 364 { |
| 363 int n; | 365 int n; |
| 364 | 366 |
| 365 for (n = 0; n < GF2_DIM; n++) | 367 for (n = 0; n < GF2_DIM; n++) |
| 366 square[n] = gf2_matrix_times(mat, mat[n]); | 368 square[n] = gf2_matrix_times(mat, mat[n]); |
| 367 } | 369 } |
| 368 | 370 |
| 369 /* ========================================================================= */ | 371 /* ========================================================================= */ |
| 370 uLong ZEXPORT crc32_combine(crc1, crc2, len2) | 372 local uLong crc32_combine_(crc1, crc2, len2) |
| 371 uLong crc1; | 373 uLong crc1; |
| 372 uLong crc2; | 374 uLong crc2; |
| 373 z_off_t len2; | 375 z_off64_t len2; |
| 374 { | 376 { |
| 375 int n; | 377 int n; |
| 376 unsigned long row; | 378 unsigned long row; |
| 377 unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ | 379 unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ |
| 378 unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ | 380 unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ |
| 379 | 381 |
| 380 /* degenerate case */ | 382 /* degenerate case (also disallow negative lengths) */ |
| 381 if (len2 == 0) | 383 if (len2 <= 0) |
| 382 return crc1; | 384 return crc1; |
| 383 | 385 |
| 384 /* put operator for one zero bit in odd */ | 386 /* put operator for one zero bit in odd */ |
| 385 odd[0] = 0xedb88320L; /* CRC-32 polynomial */ | 387 odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ |
| 386 row = 1; | 388 row = 1; |
| 387 for (n = 1; n < GF2_DIM; n++) { | 389 for (n = 1; n < GF2_DIM; n++) { |
| 388 odd[n] = row; | 390 odd[n] = row; |
| 389 row <<= 1; | 391 row <<= 1; |
| 390 } | 392 } |
| 391 | 393 |
| 392 /* put operator for two zero bits in even */ | 394 /* put operator for two zero bits in even */ |
| 393 gf2_matrix_square(even, odd); | 395 gf2_matrix_square(even, odd); |
| 394 | 396 |
| 395 /* put operator for four zero bits in odd */ | 397 /* put operator for four zero bits in odd */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 414 crc1 = gf2_matrix_times(odd, crc1); | 416 crc1 = gf2_matrix_times(odd, crc1); |
| 415 len2 >>= 1; | 417 len2 >>= 1; |
| 416 | 418 |
| 417 /* if no more bits set, then done */ | 419 /* if no more bits set, then done */ |
| 418 } while (len2 != 0); | 420 } while (len2 != 0); |
| 419 | 421 |
| 420 /* return combined crc */ | 422 /* return combined crc */ |
| 421 crc1 ^= crc2; | 423 crc1 ^= crc2; |
| 422 return crc1; | 424 return crc1; |
| 423 } | 425 } |
| 426 |
| 427 /* ========================================================================= */ |
| 428 uLong ZEXPORT crc32_combine(crc1, crc2, len2) |
| 429 uLong crc1; |
| 430 uLong crc2; |
| 431 z_off_t len2; |
| 432 { |
| 433 return crc32_combine_(crc1, crc2, len2); |
| 434 } |
| 435 |
| 436 uLong ZEXPORT crc32_combine64(crc1, crc2, len2) |
| 437 uLong crc1; |
| 438 uLong crc2; |
| 439 z_off64_t len2; |
| 440 { |
| 441 return crc32_combine_(crc1, crc2, len2); |
| 442 } |
| OLD | NEW |