diff options
| author | Mark Adler <madler@alumni.caltech.edu> | 2012-04-29 16:18:12 -0700 |
|---|---|---|
| committer | Mark Adler <madler@alumni.caltech.edu> | 2012-04-29 16:18:12 -0700 |
| commit | 6c9bd474aa08312ef2e2e9655a80e18db24a1680 (patch) | |
| tree | 2539e04a1037206dc3853fbdbb33194bb2690aaa | |
| parent | 1be117908397b0ce065c07c60fa2b4ae778ff112 (diff) | |
| download | zlib-6c9bd474aa08312ef2e2e9655a80e18db24a1680.tar.gz zlib-6c9bd474aa08312ef2e2e9655a80e18db24a1680.tar.bz2 zlib-6c9bd474aa08312ef2e2e9655a80e18db24a1680.zip | |
Fix type mismatch between get_crc_table() and crc_table.
crc_table is made using a four-byte integer (when that can be
determined). However get_crc_table() returned a pointer to an
unsigned long, which could be eight bytes. This fixes that by
creating a new z_crc_t type for the crc_table.
This type is also used for the BYFOUR crc calculations that depend
on a four-byte type. The four-byte type can now be determined by
./configure, which also solves a problem where ./configure --solo
would never use BYFOUR. No the Z_U4 #define indicates that four-
byte integer was found either by ./configure or by zconf.h.
| -rwxr-xr-x | configure | 27 | ||||
| -rw-r--r-- | crc32.c | 67 | ||||
| -rw-r--r-- | crc32.h | 2 | ||||
| -rw-r--r-- | zconf.h | 23 | ||||
| -rw-r--r-- | zconf.h.cmakein | 23 | ||||
| -rw-r--r-- | zconf.h.in | 23 | ||||
| -rw-r--r-- | zlib.h | 2 |
7 files changed, 120 insertions, 47 deletions
| @@ -698,6 +698,32 @@ EOF | |||
| 698 | fi | 698 | fi |
| 699 | fi | 699 | fi |
| 700 | 700 | ||
| 701 | echo >> configure.log | ||
| 702 | |||
| 703 | # find a four-byte unsiged integer type for crc calculations | ||
| 704 | cat > $test.c <<EOF | ||
| 705 | #include <stdio.h> | ||
| 706 | #define is32(n,t) for(n=1,k=0;n;n<<=1,k++);if(k==32){puts(t);return 0;} | ||
| 707 | int main() { | ||
| 708 | int k; | ||
| 709 | unsigned i; | ||
| 710 | unsigned long l; | ||
| 711 | unsigned short s; | ||
| 712 | is32(i, "unsigned") | ||
| 713 | is32(l, "unsigned long") | ||
| 714 | is32(s, "unsigned short") | ||
| 715 | return 1; | ||
| 716 | } | ||
| 717 | EOF | ||
| 718 | Z_U4="" | ||
| 719 | if try $CC $CFLAGS $test.c -o $test && Z_U4=`$test` && test -n "$Z_U4"; then | ||
| 720 | sed < zconf.h "/#define Z_U4/s/\/\* \.\/configure may/#define Z_U4 $Z_U4 \/* .\/configure put the/" > zconf.temp.h | ||
| 721 | mv zconf.temp.h zconf.h | ||
| 722 | echo "Looking for a four-byte integer type... Found." | tee -a configure.log | ||
| 723 | else | ||
| 724 | echo "Looking for a four-byte integer type... Not found." | tee -a configure.log | ||
| 725 | fi | ||
| 726 | |||
| 701 | # clean up files produced by running the compiler and linker | 727 | # clean up files produced by running the compiler and linker |
| 702 | rm -f $test.[co] $test $test$shared_ext $test.gcno | 728 | rm -f $test.[co] $test $test$shared_ext $test.gcno |
| 703 | 729 | ||
| @@ -724,6 +750,7 @@ echo SHAREDLIBV = $SHAREDLIBV >> configure.log | |||
| 724 | echo STATICLIB = $STATICLIB >> configure.log | 750 | echo STATICLIB = $STATICLIB >> configure.log |
| 725 | echo TEST = $TEST >> configure.log | 751 | echo TEST = $TEST >> configure.log |
| 726 | echo VER = $VER >> configure.log | 752 | echo VER = $VER >> configure.log |
| 753 | echo Z_U4 = $Z_U4 >> configure.log | ||
| 727 | echo exec_prefix = $exec_prefix >> configure.log | 754 | echo exec_prefix = $exec_prefix >> configure.log |
| 728 | echo includedir = $includedir >> configure.log | 755 | echo includedir = $includedir >> configure.log |
| 729 | echo libdir = $libdir >> configure.log | 756 | echo libdir = $libdir >> configure.log |
| @@ -32,40 +32,17 @@ | |||
| 32 | 32 | ||
| 33 | #define local static | 33 | #define local static |
| 34 | 34 | ||
| 35 | /* Find a four-byte integer type for crc32_little() and crc32_big(). */ | ||
| 36 | #ifdef Z_SOLO | ||
| 37 | # define NOBYFOUR | ||
| 38 | #endif | ||
| 39 | #ifndef NOBYFOUR | ||
| 40 | # ifdef STDC /* need ANSI C limits.h to determine sizes */ | ||
| 41 | # include <limits.h> | ||
| 42 | # define BYFOUR | ||
| 43 | # if (UINT_MAX == 0xffffffffUL) | ||
| 44 | typedef unsigned int u4; | ||
| 45 | # else | ||
| 46 | # if (ULONG_MAX == 0xffffffffUL) | ||
| 47 | typedef unsigned long u4; | ||
| 48 | # else | ||
| 49 | # if (USHRT_MAX == 0xffffffffUL) | ||
| 50 | typedef unsigned short u4; | ||
| 51 | # else | ||
| 52 | # undef BYFOUR /* can't find a four-byte integer type! */ | ||
| 53 | # endif | ||
| 54 | # endif | ||
| 55 | # endif | ||
| 56 | # endif /* STDC */ | ||
| 57 | #endif /* !NOBYFOUR */ | ||
| 58 | |||
| 59 | /* Definitions for doing the crc four data bytes at a time. */ | 35 | /* Definitions for doing the crc four data bytes at a time. */ |
| 36 | #if !defined(NOBYFOUR) && defined(Z_U4) | ||
| 37 | # define BYFOUR | ||
| 38 | #endif | ||
| 60 | #ifdef BYFOUR | 39 | #ifdef BYFOUR |
| 61 | typedef u4 crc_table_t; | ||
| 62 | local unsigned long crc32_little OF((unsigned long, | 40 | local unsigned long crc32_little OF((unsigned long, |
| 63 | const unsigned char FAR *, unsigned)); | 41 | const unsigned char FAR *, unsigned)); |
| 64 | local unsigned long crc32_big OF((unsigned long, | 42 | local unsigned long crc32_big OF((unsigned long, |
| 65 | const unsigned char FAR *, unsigned)); | 43 | const unsigned char FAR *, unsigned)); |
| 66 | # define TBLS 8 | 44 | # define TBLS 8 |
| 67 | #else | 45 | #else |
| 68 | typedef unsigned long crc_table_t; | ||
| 69 | # define TBLS 1 | 46 | # define TBLS 1 |
| 70 | #endif /* BYFOUR */ | 47 | #endif /* BYFOUR */ |
| 71 | 48 | ||
| @@ -79,10 +56,10 @@ local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); | |||
| 79 | #ifdef DYNAMIC_CRC_TABLE | 56 | #ifdef DYNAMIC_CRC_TABLE |
| 80 | 57 | ||
| 81 | local volatile int crc_table_empty = 1; | 58 | local volatile int crc_table_empty = 1; |
| 82 | local crc_table_t FAR crc_table[TBLS][256]; | 59 | local z_crc_t FAR crc_table[TBLS][256]; |
| 83 | local void make_crc_table OF((void)); | 60 | local void make_crc_table OF((void)); |
| 84 | #ifdef MAKECRCH | 61 | #ifdef MAKECRCH |
| 85 | local void write_table OF((FILE *, const crc_table_t FAR *)); | 62 | local void write_table OF((FILE *, const z_crc_t FAR *)); |
| 86 | #endif /* MAKECRCH */ | 63 | #endif /* MAKECRCH */ |
| 87 | /* | 64 | /* |
| 88 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: | 65 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: |
| @@ -112,9 +89,9 @@ local void make_crc_table OF((void)); | |||
| 112 | */ | 89 | */ |
| 113 | local void make_crc_table() | 90 | local void make_crc_table() |
| 114 | { | 91 | { |
| 115 | crc_table_t c; | 92 | z_crc_t c; |
| 116 | int n, k; | 93 | int n, k; |
| 117 | crc_table_t poly; /* polynomial exclusive-or pattern */ | 94 | z_crc_t poly; /* polynomial exclusive-or pattern */ |
| 118 | /* terms of polynomial defining this crc (except x^32): */ | 95 | /* terms of polynomial defining this crc (except x^32): */ |
| 119 | static volatile int first = 1; /* flag to limit concurrent making */ | 96 | static volatile int first = 1; /* flag to limit concurrent making */ |
| 120 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; | 97 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; |
| @@ -128,11 +105,11 @@ local void make_crc_table() | |||
| 128 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ | 105 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ |
| 129 | poly = 0; | 106 | poly = 0; |
| 130 | for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) | 107 | for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) |
| 131 | poly |= (crc_table_t)1 << (31 - p[n]); | 108 | poly |= (z_crc_t)1 << (31 - p[n]); |
| 132 | 109 | ||
| 133 | /* generate a crc for every 8-bit value */ | 110 | /* generate a crc for every 8-bit value */ |
| 134 | for (n = 0; n < 256; n++) { | 111 | for (n = 0; n < 256; n++) { |
| 135 | c = (crc_table_t)n; | 112 | c = (z_crc_t)n; |
| 136 | for (k = 0; k < 8; k++) | 113 | for (k = 0; k < 8; k++) |
| 137 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; | 114 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; |
| 138 | crc_table[0][n] = c; | 115 | crc_table[0][n] = c; |
| @@ -169,7 +146,7 @@ local void make_crc_table() | |||
| 169 | if (out == NULL) return; | 146 | if (out == NULL) return; |
| 170 | fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); | 147 | fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); |
| 171 | fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); | 148 | fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); |
| 172 | fprintf(out, "local const crc_table_t FAR "); | 149 | fprintf(out, "local const z_crc_t FAR "); |
| 173 | fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); | 150 | fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); |
| 174 | write_table(out, crc_table[0]); | 151 | write_table(out, crc_table[0]); |
| 175 | # ifdef BYFOUR | 152 | # ifdef BYFOUR |
| @@ -189,7 +166,7 @@ local void make_crc_table() | |||
| 189 | #ifdef MAKECRCH | 166 | #ifdef MAKECRCH |
| 190 | local void write_table(out, table) | 167 | local void write_table(out, table) |
| 191 | FILE *out; | 168 | FILE *out; |
| 192 | const crc_table_t FAR *table; | 169 | const z_crc_t FAR *table; |
| 193 | { | 170 | { |
| 194 | int n; | 171 | int n; |
| 195 | 172 | ||
| @@ -210,13 +187,13 @@ local void write_table(out, table) | |||
| 210 | /* ========================================================================= | 187 | /* ========================================================================= |
| 211 | * This function can be used by asm versions of crc32() | 188 | * This function can be used by asm versions of crc32() |
| 212 | */ | 189 | */ |
| 213 | const unsigned long FAR * ZEXPORT get_crc_table() | 190 | const z_crc_t FAR * ZEXPORT get_crc_table() |
| 214 | { | 191 | { |
| 215 | #ifdef DYNAMIC_CRC_TABLE | 192 | #ifdef DYNAMIC_CRC_TABLE |
| 216 | if (crc_table_empty) | 193 | if (crc_table_empty) |
| 217 | make_crc_table(); | 194 | make_crc_table(); |
| 218 | #endif /* DYNAMIC_CRC_TABLE */ | 195 | #endif /* DYNAMIC_CRC_TABLE */ |
| 219 | return (const unsigned long FAR *)crc_table; | 196 | return (const z_crc_t FAR *)crc_table; |
| 220 | } | 197 | } |
| 221 | 198 | ||
| 222 | /* ========================================================================= */ | 199 | /* ========================================================================= */ |
| @@ -238,7 +215,7 @@ unsigned long ZEXPORT crc32(crc, buf, len) | |||
| 238 | 215 | ||
| 239 | #ifdef BYFOUR | 216 | #ifdef BYFOUR |
| 240 | if (sizeof(void *) == sizeof(ptrdiff_t)) { | 217 | if (sizeof(void *) == sizeof(ptrdiff_t)) { |
| 241 | u4 endian; | 218 | z_crc_t endian; |
| 242 | 219 | ||
| 243 | endian = 1; | 220 | endian = 1; |
| 244 | if (*((unsigned char *)(&endian))) | 221 | if (*((unsigned char *)(&endian))) |
| @@ -272,17 +249,17 @@ local unsigned long crc32_little(crc, buf, len) | |||
| 272 | const unsigned char FAR *buf; | 249 | const unsigned char FAR *buf; |
| 273 | unsigned len; | 250 | unsigned len; |
| 274 | { | 251 | { |
| 275 | register u4 c; | 252 | register z_crc_t c; |
| 276 | register const u4 FAR *buf4; | 253 | register const z_crc_t FAR *buf4; |
| 277 | 254 | ||
| 278 | c = (u4)crc; | 255 | c = (z_crc_t)crc; |
| 279 | c = ~c; | 256 | c = ~c; |
| 280 | while (len && ((ptrdiff_t)buf & 3)) { | 257 | while (len && ((ptrdiff_t)buf & 3)) { |
| 281 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); | 258 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); |
| 282 | len--; | 259 | len--; |
| 283 | } | 260 | } |
| 284 | 261 | ||
| 285 | buf4 = (const u4 FAR *)(const void FAR *)buf; | 262 | buf4 = (const z_crc_t FAR *)(const void FAR *)buf; |
| 286 | while (len >= 32) { | 263 | while (len >= 32) { |
| 287 | DOLIT32; | 264 | DOLIT32; |
| 288 | len -= 32; | 265 | len -= 32; |
| @@ -312,17 +289,17 @@ local unsigned long crc32_big(crc, buf, len) | |||
| 312 | const unsigned char FAR *buf; | 289 | const unsigned char FAR *buf; |
| 313 | unsigned len; | 290 | unsigned len; |
| 314 | { | 291 | { |
| 315 | register u4 c; | 292 | register z_crc_t c; |
| 316 | register const u4 FAR *buf4; | 293 | register const z_crc_t FAR *buf4; |
| 317 | 294 | ||
| 318 | c = ZSWAP32((u4)crc); | 295 | c = ZSWAP32((z_crc_t)crc); |
| 319 | c = ~c; | 296 | c = ~c; |
| 320 | while (len && ((ptrdiff_t)buf & 3)) { | 297 | while (len && ((ptrdiff_t)buf & 3)) { |
| 321 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); | 298 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); |
| 322 | len--; | 299 | len--; |
| 323 | } | 300 | } |
| 324 | 301 | ||
| 325 | buf4 = (const u4 FAR *)(const void FAR *)buf; | 302 | buf4 = (const z_crc_t FAR *)(const void FAR *)buf; |
| 326 | buf4--; | 303 | buf4--; |
| 327 | while (len >= 32) { | 304 | while (len >= 32) { |
| 328 | DOBIG32; | 305 | DOBIG32; |
| @@ -2,7 +2,7 @@ | |||
| 2 | * Generated automatically by crc32.c | 2 | * Generated automatically by crc32.c |
| 3 | */ | 3 | */ |
| 4 | 4 | ||
| 5 | local const crc_table_t FAR crc_table[TBLS][256] = | 5 | local const z_crc_t FAR crc_table[TBLS][256] = |
| 6 | { | 6 | { |
| 7 | { | 7 | { |
| 8 | 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL, | 8 | 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL, |
| @@ -388,6 +388,29 @@ typedef uLong FAR uLongf; | |||
| 388 | typedef Byte *voidp; | 388 | typedef Byte *voidp; |
| 389 | #endif | 389 | #endif |
| 390 | 390 | ||
| 391 | /* ./configure may #define Z_U4 here */ | ||
| 392 | |||
| 393 | #if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC) | ||
| 394 | # include <limits.h> | ||
| 395 | # if (UINT_MAX == 0xffffffffUL) | ||
| 396 | # define Z_U4 unsigned | ||
| 397 | # else | ||
| 398 | # if (ULONG_MAX == 0xffffffffUL) | ||
| 399 | # define Z_U4 unsigned long | ||
| 400 | # else | ||
| 401 | # if (USHRT_MAX == 0xffffffffUL) | ||
| 402 | # define Z_U4 unsigned short | ||
| 403 | # endif | ||
| 404 | # endif | ||
| 405 | # endif | ||
| 406 | #endif | ||
| 407 | |||
| 408 | #ifdef Z_U4 | ||
| 409 | typedef Z_U4 z_crc_t; | ||
| 410 | #else | ||
| 411 | typedef unsigned long z_crc_t; | ||
| 412 | #endif | ||
| 413 | |||
| 391 | #ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ | 414 | #ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ |
| 392 | # define Z_HAVE_UNISTD_H | 415 | # define Z_HAVE_UNISTD_H |
| 393 | #endif | 416 | #endif |
diff --git a/zconf.h.cmakein b/zconf.h.cmakein index 4ade487..b6ca59a 100644 --- a/zconf.h.cmakein +++ b/zconf.h.cmakein | |||
| @@ -390,6 +390,29 @@ typedef uLong FAR uLongf; | |||
| 390 | typedef Byte *voidp; | 390 | typedef Byte *voidp; |
| 391 | #endif | 391 | #endif |
| 392 | 392 | ||
| 393 | /* ./configure may #define Z_U4 here */ | ||
| 394 | |||
| 395 | #if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC) | ||
| 396 | # include <limits.h> | ||
| 397 | # if (UINT_MAX == 0xffffffffUL) | ||
| 398 | # define Z_U4 unsigned | ||
| 399 | # else | ||
| 400 | # if (ULONG_MAX == 0xffffffffUL) | ||
| 401 | # define Z_U4 unsigned long | ||
| 402 | # else | ||
| 403 | # if (USHRT_MAX == 0xffffffffUL) | ||
| 404 | # define Z_U4 unsigned short | ||
| 405 | # endif | ||
| 406 | # endif | ||
| 407 | # endif | ||
| 408 | #endif | ||
| 409 | |||
| 410 | #ifdef Z_U4 | ||
| 411 | typedef Z_U4 z_crc_t; | ||
| 412 | #else | ||
| 413 | typedef unsigned long z_crc_t; | ||
| 414 | #endif | ||
| 415 | |||
| 393 | #ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ | 416 | #ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ |
| 394 | # define Z_HAVE_UNISTD_H | 417 | # define Z_HAVE_UNISTD_H |
| 395 | #endif | 418 | #endif |
| @@ -388,6 +388,29 @@ typedef uLong FAR uLongf; | |||
| 388 | typedef Byte *voidp; | 388 | typedef Byte *voidp; |
| 389 | #endif | 389 | #endif |
| 390 | 390 | ||
| 391 | /* ./configure may #define Z_U4 here */ | ||
| 392 | |||
| 393 | #if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC) | ||
| 394 | # include <limits.h> | ||
| 395 | # if (UINT_MAX == 0xffffffffUL) | ||
| 396 | # define Z_U4 unsigned | ||
| 397 | # else | ||
| 398 | # if (ULONG_MAX == 0xffffffffUL) | ||
| 399 | # define Z_U4 unsigned long | ||
| 400 | # else | ||
| 401 | # if (USHRT_MAX == 0xffffffffUL) | ||
| 402 | # define Z_U4 unsigned short | ||
| 403 | # endif | ||
| 404 | # endif | ||
| 405 | # endif | ||
| 406 | #endif | ||
| 407 | |||
| 408 | #ifdef Z_U4 | ||
| 409 | typedef Z_U4 z_crc_t; | ||
| 410 | #else | ||
| 411 | typedef unsigned long z_crc_t; | ||
| 412 | #endif | ||
| 413 | |||
| 391 | #ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ | 414 | #ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ |
| 392 | # define Z_HAVE_UNISTD_H | 415 | # define Z_HAVE_UNISTD_H |
| 393 | #endif | 416 | #endif |
| @@ -1728,7 +1728,7 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ | |||
| 1728 | /* undocumented functions */ | 1728 | /* undocumented functions */ |
| 1729 | ZEXTERN const char * ZEXPORT zError OF((int)); | 1729 | ZEXTERN const char * ZEXPORT zError OF((int)); |
| 1730 | ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp)); | 1730 | ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp)); |
| 1731 | ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void)); | 1731 | ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table OF((void)); |
| 1732 | ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int)); | 1732 | ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int)); |
| 1733 | ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp)); | 1733 | ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp)); |
| 1734 | ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp)); | 1734 | ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp)); |
