Skip to content

Commit 98881dd

Browse files
committed
Update bundled zlib code to 1.2.8
1 parent b369be1 commit 98881dd

19 files changed

+1075
-515
lines changed

cbits/adler32.c

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* adler32.c -- compute the Adler-32 checksum of a data stream
2-
* Copyright (C) 1995-2007 Mark Adler
2+
* Copyright (C) 1995-2011 Mark Adler
33
* For conditions of distribution and use, see copyright notice in zlib.h
44
*/
55

@@ -9,9 +9,9 @@
99

1010
#define local static
1111

12-
local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2);
12+
local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
1313

14-
#define BASE 65521UL /* largest prime smaller than 65536 */
14+
#define BASE 65521 /* largest prime smaller than 65536 */
1515
#define NMAX 5552
1616
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
1717

@@ -21,39 +21,44 @@ local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2);
2121
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
2222
#define DO16(buf) DO8(buf,0); DO8(buf,8);
2323

24-
/* use NO_DIVIDE if your processor does not do division in hardware */
24+
/* use NO_DIVIDE if your processor does not do division in hardware --
25+
try it both ways to see which is faster */
2526
#ifdef NO_DIVIDE
26-
# define MOD(a) \
27+
/* note that this assumes BASE is 65521, where 65536 % 65521 == 15
28+
(thank you to John Reiser for pointing this out) */
29+
# define CHOP(a) \
30+
do { \
31+
unsigned long tmp = a >> 16; \
32+
a &= 0xffffUL; \
33+
a += (tmp << 4) - tmp; \
34+
} while (0)
35+
# define MOD28(a) \
2736
do { \
28-
if (a >= (BASE << 16)) a -= (BASE << 16); \
29-
if (a >= (BASE << 15)) a -= (BASE << 15); \
30-
if (a >= (BASE << 14)) a -= (BASE << 14); \
31-
if (a >= (BASE << 13)) a -= (BASE << 13); \
32-
if (a >= (BASE << 12)) a -= (BASE << 12); \
33-
if (a >= (BASE << 11)) a -= (BASE << 11); \
34-
if (a >= (BASE << 10)) a -= (BASE << 10); \
35-
if (a >= (BASE << 9)) a -= (BASE << 9); \
36-
if (a >= (BASE << 8)) a -= (BASE << 8); \
37-
if (a >= (BASE << 7)) a -= (BASE << 7); \
38-
if (a >= (BASE << 6)) a -= (BASE << 6); \
39-
if (a >= (BASE << 5)) a -= (BASE << 5); \
40-
if (a >= (BASE << 4)) a -= (BASE << 4); \
41-
if (a >= (BASE << 3)) a -= (BASE << 3); \
42-
if (a >= (BASE << 2)) a -= (BASE << 2); \
43-
if (a >= (BASE << 1)) a -= (BASE << 1); \
37+
CHOP(a); \
4438
if (a >= BASE) a -= BASE; \
4539
} while (0)
46-
# define MOD4(a) \
40+
# define MOD(a) \
4741
do { \
48-
if (a >= (BASE << 4)) a -= (BASE << 4); \
49-
if (a >= (BASE << 3)) a -= (BASE << 3); \
50-
if (a >= (BASE << 2)) a -= (BASE << 2); \
51-
if (a >= (BASE << 1)) a -= (BASE << 1); \
42+
CHOP(a); \
43+
MOD28(a); \
44+
} while (0)
45+
# define MOD63(a) \
46+
do { /* this assumes a is not negative */ \
47+
z_off64_t tmp = a >> 32; \
48+
a &= 0xffffffffL; \
49+
a += (tmp << 8) - (tmp << 5) + tmp; \
50+
tmp = a >> 16; \
51+
a &= 0xffffL; \
52+
a += (tmp << 4) - tmp; \
53+
tmp = a >> 16; \
54+
a &= 0xffffL; \
55+
a += (tmp << 4) - tmp; \
5256
if (a >= BASE) a -= BASE; \
5357
} while (0)
5458
#else
5559
# define MOD(a) a %= BASE
56-
# define MOD4(a) a %= BASE
60+
# define MOD28(a) a %= BASE
61+
# define MOD63(a) a %= BASE
5762
#endif
5863

5964
/* ========================================================================= */
@@ -92,7 +97,7 @@ uLong ZEXPORT adler32(adler, buf, len)
9297
}
9398
if (adler >= BASE)
9499
adler -= BASE;
95-
MOD4(sum2); /* only added so many BASE's */
100+
MOD28(sum2); /* only added so many BASE's */
96101
return adler | (sum2 << 16);
97102
}
98103

@@ -137,8 +142,13 @@ local uLong adler32_combine_(adler1, adler2, len2)
137142
unsigned long sum2;
138143
unsigned rem;
139144

145+
/* for negative len, return invalid adler32 as a clue for debugging */
146+
if (len2 < 0)
147+
return 0xffffffffUL;
148+
140149
/* the derivation of this formula is left as an exercise for the reader */
141-
rem = (unsigned)(len2 % BASE);
150+
MOD63(len2); /* assumes len2 >= 0 */
151+
rem = (unsigned)len2;
142152
sum1 = adler1 & 0xffff;
143153
sum2 = rem * sum1;
144154
MOD(sum2);

cbits/compress.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
2929
z_stream stream;
3030
int err;
3131

32-
stream.next_in = (Bytef*)source;
32+
stream.next_in = (z_const Bytef *)source;
3333
stream.avail_in = (uInt)sourceLen;
3434
#ifdef MAXSEG_64K
3535
/* Check for source > 64K on 16-bit machine: */

cbits/crc32.c

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* crc32.c -- compute the CRC-32 of a data stream
2-
* Copyright (C) 1995-2006, 2010 Mark Adler
2+
* Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
33
* For conditions of distribution and use, see copyright notice in zlib.h
44
*
55
* Thanks to Rodney Brown <[email protected]> for his contribution of faster
@@ -17,6 +17,8 @@
1717
of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
1818
first call get_crc_table() to initialize the tables before allowing more than
1919
one thread to use crc32().
20+
21+
DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h.
2022
*/
2123

2224
#ifdef MAKECRCH
@@ -30,31 +32,11 @@
3032

3133
#define local static
3234

33-
/* Find a four-byte integer type for crc32_little() and crc32_big(). */
34-
#ifndef NOBYFOUR
35-
# ifdef STDC /* need ANSI C limits.h to determine sizes */
36-
# include <limits.h>
37-
# define BYFOUR
38-
# if (UINT_MAX == 0xffffffffUL)
39-
typedef unsigned int u4;
40-
# else
41-
# if (ULONG_MAX == 0xffffffffUL)
42-
typedef unsigned long u4;
43-
# else
44-
# if (USHRT_MAX == 0xffffffffUL)
45-
typedef unsigned short u4;
46-
# else
47-
# undef BYFOUR /* can't find a four-byte integer type! */
48-
# endif
49-
# endif
50-
# endif
51-
# endif /* STDC */
52-
#endif /* !NOBYFOUR */
53-
5435
/* Definitions for doing the crc four data bytes at a time. */
36+
#if !defined(NOBYFOUR) && defined(Z_U4)
37+
# define BYFOUR
38+
#endif
5539
#ifdef BYFOUR
56-
# define REV(w) ((((w)>>24)&0xff)+(((w)>>8)&0xff00)+ \
57-
(((w)&0xff00)<<8)+(((w)&0xff)<<24))
5840
local unsigned long crc32_little OF((unsigned long,
5941
const unsigned char FAR *, unsigned));
6042
local unsigned long crc32_big OF((unsigned long,
@@ -68,16 +50,16 @@
6850
local unsigned long gf2_matrix_times OF((unsigned long *mat,
6951
unsigned long vec));
7052
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);
53+
local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2));
7254

7355

7456
#ifdef DYNAMIC_CRC_TABLE
7557

7658
local volatile int crc_table_empty = 1;
77-
local unsigned long FAR crc_table[TBLS][256];
59+
local z_crc_t FAR crc_table[TBLS][256];
7860
local void make_crc_table OF((void));
7961
#ifdef MAKECRCH
80-
local void write_table OF((FILE *, const unsigned long FAR *));
62+
local void write_table OF((FILE *, const z_crc_t FAR *));
8163
#endif /* MAKECRCH */
8264
/*
8365
Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
@@ -107,9 +89,9 @@ local void make_crc_table OF((void));
10789
*/
10890
local void make_crc_table()
10991
{
110-
unsigned long c;
92+
z_crc_t c;
11193
int n, k;
112-
unsigned long poly; /* polynomial exclusive-or pattern */
94+
z_crc_t poly; /* polynomial exclusive-or pattern */
11395
/* terms of polynomial defining this crc (except x^32): */
11496
static volatile int first = 1; /* flag to limit concurrent making */
11597
static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
@@ -121,13 +103,13 @@ local void make_crc_table()
121103
first = 0;
122104

123105
/* make exclusive-or pattern from polynomial (0xedb88320UL) */
124-
poly = 0UL;
125-
for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++)
126-
poly |= 1UL << (31 - p[n]);
106+
poly = 0;
107+
for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++)
108+
poly |= (z_crc_t)1 << (31 - p[n]);
127109

128110
/* generate a crc for every 8-bit value */
129111
for (n = 0; n < 256; n++) {
130-
c = (unsigned long)n;
112+
c = (z_crc_t)n;
131113
for (k = 0; k < 8; k++)
132114
c = c & 1 ? poly ^ (c >> 1) : c >> 1;
133115
crc_table[0][n] = c;
@@ -138,11 +120,11 @@ local void make_crc_table()
138120
and then the byte reversal of those as well as the first table */
139121
for (n = 0; n < 256; n++) {
140122
c = crc_table[0][n];
141-
crc_table[4][n] = REV(c);
123+
crc_table[4][n] = ZSWAP32(c);
142124
for (k = 1; k < 4; k++) {
143125
c = crc_table[0][c & 0xff] ^ (c >> 8);
144126
crc_table[k][n] = c;
145-
crc_table[k + 4][n] = REV(c);
127+
crc_table[k + 4][n] = ZSWAP32(c);
146128
}
147129
}
148130
#endif /* BYFOUR */
@@ -164,7 +146,7 @@ local void make_crc_table()
164146
if (out == NULL) return;
165147
fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n");
166148
fprintf(out, " * Generated automatically by crc32.c\n */\n\n");
167-
fprintf(out, "local const unsigned long FAR ");
149+
fprintf(out, "local const z_crc_t FAR ");
168150
fprintf(out, "crc_table[TBLS][256] =\n{\n {\n");
169151
write_table(out, crc_table[0]);
170152
# ifdef BYFOUR
@@ -184,12 +166,13 @@ local void make_crc_table()
184166
#ifdef MAKECRCH
185167
local void write_table(out, table)
186168
FILE *out;
187-
const unsigned long FAR *table;
169+
const z_crc_t FAR *table;
188170
{
189171
int n;
190172

191173
for (n = 0; n < 256; n++)
192-
fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n],
174+
fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ",
175+
(unsigned long)(table[n]),
193176
n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", "));
194177
}
195178
#endif /* MAKECRCH */
@@ -204,13 +187,13 @@ local void write_table(out, table)
204187
/* =========================================================================
205188
* This function can be used by asm versions of crc32()
206189
*/
207-
const unsigned long FAR * ZEXPORT get_crc_table()
190+
const z_crc_t FAR * ZEXPORT get_crc_table()
208191
{
209192
#ifdef DYNAMIC_CRC_TABLE
210193
if (crc_table_empty)
211194
make_crc_table();
212195
#endif /* DYNAMIC_CRC_TABLE */
213-
return (const unsigned long FAR *)crc_table;
196+
return (const z_crc_t FAR *)crc_table;
214197
}
215198

216199
/* ========================================================================= */
@@ -232,7 +215,7 @@ unsigned long ZEXPORT crc32(crc, buf, len)
232215

233216
#ifdef BYFOUR
234217
if (sizeof(void *) == sizeof(ptrdiff_t)) {
235-
u4 endian;
218+
z_crc_t endian;
236219

237220
endian = 1;
238221
if (*((unsigned char *)(&endian)))
@@ -266,17 +249,17 @@ local unsigned long crc32_little(crc, buf, len)
266249
const unsigned char FAR *buf;
267250
unsigned len;
268251
{
269-
register u4 c;
270-
register const u4 FAR *buf4;
252+
register z_crc_t c;
253+
register const z_crc_t FAR *buf4;
271254

272-
c = (u4)crc;
255+
c = (z_crc_t)crc;
273256
c = ~c;
274257
while (len && ((ptrdiff_t)buf & 3)) {
275258
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
276259
len--;
277260
}
278261

279-
buf4 = (const u4 FAR *)(const void FAR *)buf;
262+
buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
280263
while (len >= 32) {
281264
DOLIT32;
282265
len -= 32;
@@ -306,17 +289,17 @@ local unsigned long crc32_big(crc, buf, len)
306289
const unsigned char FAR *buf;
307290
unsigned len;
308291
{
309-
register u4 c;
310-
register const u4 FAR *buf4;
292+
register z_crc_t c;
293+
register const z_crc_t FAR *buf4;
311294

312-
c = REV((u4)crc);
295+
c = ZSWAP32((z_crc_t)crc);
313296
c = ~c;
314297
while (len && ((ptrdiff_t)buf & 3)) {
315298
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
316299
len--;
317300
}
318301

319-
buf4 = (const u4 FAR *)(const void FAR *)buf;
302+
buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
320303
buf4--;
321304
while (len >= 32) {
322305
DOBIG32;
@@ -333,7 +316,7 @@ local unsigned long crc32_big(crc, buf, len)
333316
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
334317
} while (--len);
335318
c = ~c;
336-
return (unsigned long)(REV(c));
319+
return (unsigned long)(ZSWAP32(c));
337320
}
338321

339322
#endif /* BYFOUR */

cbits/crc32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Generated automatically by crc32.c
33
*/
44

5-
local const unsigned long FAR crc_table[TBLS][256] =
5+
local const z_crc_t FAR crc_table[TBLS][256] =
66
{
77
{
88
0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL,

0 commit comments

Comments
 (0)