aboutsummaryrefslogtreecommitdiff
path: root/crc32.c
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2012-04-29 16:18:12 -0700
committerMark Adler <madler@alumni.caltech.edu>2012-04-29 16:18:12 -0700
commit6c9bd474aa08312ef2e2e9655a80e18db24a1680 (patch)
tree2539e04a1037206dc3853fbdbb33194bb2690aaa /crc32.c
parent1be117908397b0ce065c07c60fa2b4ae778ff112 (diff)
downloadzlib-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.
Diffstat (limited to 'crc32.c')
-rw-r--r--crc32.c67
1 files changed, 22 insertions, 45 deletions
diff --git a/crc32.c b/crc32.c
index 5cfc8ff..979a719 100644
--- a/crc32.c
+++ b/crc32.c
@@ -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
81local volatile int crc_table_empty = 1; 58local volatile int crc_table_empty = 1;
82local crc_table_t FAR crc_table[TBLS][256]; 59local z_crc_t FAR crc_table[TBLS][256];
83local void make_crc_table OF((void)); 60local 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*/
113local void make_crc_table() 90local 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
190local void write_table(out, table) 167local 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 */
213const unsigned long FAR * ZEXPORT get_crc_table() 190const 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;