Skip to content
This repository was archived by the owner on Jul 17, 2024. It is now read-only.
/ jdk22u Public archive

Commit 5968567

Browse files
Ravi Reddycoffeys
Ravi Reddy
authored andcommitted
8324632: Update Zlib Data Compression Library to Version 1.3.1
Reviewed-by: lancea Backport-of: b5c267fc8a0af50be9e3d1d09cdaa6bf4bb29851
1 parent ae466e2 commit 5968567

File tree

14 files changed

+129
-82
lines changed

14 files changed

+129
-82
lines changed

src/java.base/share/native/libzip/zlib/ChangeLog

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11

22
ChangeLog file for zlib
33

4+
Changes in 1.3.1 (22 Jan 2024)
5+
- Reject overflows of zip header fields in minizip
6+
- Fix bug in inflateSync() for data held in bit buffer
7+
- Add LIT_MEM define to use more memory for a small deflate speedup
8+
- Fix decision on the emission of Zip64 end records in minizip
9+
- Add bounds checking to ERR_MSG() macro, used by zError()
10+
- Neutralize zip file traversal attacks in miniunz
11+
- Fix a bug in ZLIB_DEBUG compiles in check_match()
12+
- Various portability and appearance improvements
13+
414
Changes in 1.3 (18 Aug 2023)
515
- Remove K&R function definitions and zlib2ansi
616
- Fix bug in deflateBound() for level 0 and memLevel 9

src/java.base/share/native/libzip/zlib/README

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ZLIB DATA COMPRESSION LIBRARY
22

3-
zlib 1.3 is a general purpose data compression library. All the code is
3+
zlib 1.3.1 is a general purpose data compression library. All the code is
44
thread safe. The data format used by the zlib library is described by RFCs
55
(Request for Comments) 1950 to 1952 in the files
66
http://tools.ietf.org/html/rfc1950 (zlib format), rfc1951 (deflate format) and
@@ -31,7 +31,7 @@ Mark Nelson <markn@ieee.org> wrote an article about zlib for the Jan. 1997
3131
issue of Dr. Dobb's Journal; a copy of the article is available at
3232
https://marknelson.us/posts/1997/01/01/zlib-engine.html .
3333

34-
The changes made in version 1.3 are documented in the file ChangeLog.
34+
The changes made in version 1.3.1 are documented in the file ChangeLog.
3535

3636
Unsupported third party contributions are provided in directory contrib/ .
3737

@@ -83,7 +83,7 @@ Acknowledgments:
8383

8484
Copyright notice:
8585

86-
(C) 1995-2023 Jean-loup Gailly and Mark Adler
86+
(C) 1995-2024 Jean-loup Gailly and Mark Adler
8787

8888
This software is provided 'as-is', without any express or implied
8989
warranty. In no event will the authors be held liable for any damages

src/java.base/share/native/libzip/zlib/deflate.c

+36-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
/* deflate.c -- compress data using the deflation algorithm
26-
* Copyright (C) 1995-2023 Jean-loup Gailly and Mark Adler
26+
* Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
2727
* For conditions of distribution and use, see copyright notice in zlib.h
2828
*/
2929

@@ -76,7 +76,7 @@
7676
#include "deflate.h"
7777

7878
const char deflate_copyright[] =
79-
" deflate 1.3 Copyright 1995-2023 Jean-loup Gailly and Mark Adler ";
79+
" deflate 1.3.1 Copyright 1995-2024 Jean-loup Gailly and Mark Adler ";
8080
/*
8181
If you use the zlib library in a product, an acknowledgment is welcome
8282
in the documentation of your product. If for some reason you cannot
@@ -517,7 +517,7 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
517517
* symbols from which it is being constructed.
518518
*/
519519

520-
s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
520+
s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, LIT_BUFS);
521521
s->pending_buf_size = (ulg)s->lit_bufsize * 4;
522522

523523
if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
@@ -527,8 +527,14 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
527527
deflateEnd (strm);
528528
return Z_MEM_ERROR;
529529
}
530+
#ifdef LIT_MEM
531+
s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1));
532+
s->l_buf = s->pending_buf + (s->lit_bufsize << 2);
533+
s->sym_end = s->lit_bufsize - 1;
534+
#else
530535
s->sym_buf = s->pending_buf + s->lit_bufsize;
531536
s->sym_end = (s->lit_bufsize - 1) * 3;
537+
#endif
532538
/* We avoid equality with lit_bufsize*3 because of wraparound at 64K
533539
* on 16 bit machines and because stored blocks are restricted to
534540
* 64K-1 bytes.
@@ -744,9 +750,15 @@ int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
744750

745751
if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
746752
s = strm->state;
753+
#ifdef LIT_MEM
754+
if (bits < 0 || bits > 16 ||
755+
(uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3))
756+
return Z_BUF_ERROR;
757+
#else
747758
if (bits < 0 || bits > 16 ||
748759
s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
749760
return Z_BUF_ERROR;
761+
#endif
750762
do {
751763
put = Buf_size - s->bi_valid;
752764
if (put > bits)
@@ -1318,7 +1330,7 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
13181330
ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
13191331
ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
13201332
ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
1321-
ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4);
1333+
ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, LIT_BUFS);
13221334

13231335
if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
13241336
ds->pending_buf == Z_NULL) {
@@ -1329,10 +1341,15 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
13291341
zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
13301342
zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
13311343
zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
1332-
zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
1344+
zmemcpy(ds->pending_buf, ss->pending_buf, ds->lit_bufsize * LIT_BUFS);
13331345

13341346
ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
1347+
#ifdef LIT_MEM
1348+
ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1));
1349+
ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
1350+
#else
13351351
ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
1352+
#endif
13361353

13371354
ds->l_desc.dyn_tree = ds->dyn_ltree;
13381355
ds->d_desc.dyn_tree = ds->dyn_dtree;
@@ -1563,13 +1580,21 @@ local uInt longest_match(deflate_state *s, IPos cur_match) {
15631580
*/
15641581
local void check_match(deflate_state *s, IPos start, IPos match, int length) {
15651582
/* check that the match is indeed a match */
1566-
if (zmemcmp(s->window + match,
1567-
s->window + start, length) != EQUAL) {
1568-
fprintf(stderr, " start %u, match %u, length %d\n",
1569-
start, match, length);
1583+
Bytef *back = s->window + (int)match, *here = s->window + start;
1584+
IPos len = length;
1585+
if (match == (IPos)-1) {
1586+
/* match starts one byte before the current window -- just compare the
1587+
subsequent length-1 bytes */
1588+
back++;
1589+
here++;
1590+
len--;
1591+
}
1592+
if (zmemcmp(back, here, len) != EQUAL) {
1593+
fprintf(stderr, " start %u, match %d, length %d\n",
1594+
start, (int)match, length);
15701595
do {
1571-
fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
1572-
} while (--length != 0);
1596+
fprintf(stderr, "(%02x %02x)", *back++, *here++);
1597+
} while (--len != 0);
15731598
z_error("invalid match");
15741599
}
15751600
if (z_verbose > 1) {

src/java.base/share/native/libzip/zlib/deflate.h

+33-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
/* deflate.h -- internal compression state
26-
* Copyright (C) 1995-2018 Jean-loup Gailly
26+
* Copyright (C) 1995-2024 Jean-loup Gailly
2727
* For conditions of distribution and use, see copyright notice in zlib.h
2828
*/
2929

@@ -47,6 +47,10 @@
4747
# define GZIP
4848
#endif
4949

50+
/* define LIT_MEM to slightly increase the speed of deflate (order 1% to 2%) at
51+
the cost of a larger memory footprint */
52+
/* #define LIT_MEM */
53+
5054
/* ===========================================================================
5155
* Internal compression state.
5256
*/
@@ -241,7 +245,14 @@ typedef struct internal_state {
241245
/* Depth of each subtree used as tie breaker for trees of equal frequency
242246
*/
243247

248+
#ifdef LIT_MEM
249+
# define LIT_BUFS 5
250+
ushf *d_buf; /* buffer for distances */
251+
uchf *l_buf; /* buffer for literals/lengths */
252+
#else
253+
# define LIT_BUFS 4
244254
uchf *sym_buf; /* buffer for distances and literals/lengths */
255+
#endif
245256

246257
uInt lit_bufsize;
247258
/* Size of match buffer for literals/lengths. There are 4 reasons for
@@ -263,7 +274,7 @@ typedef struct internal_state {
263274
* - I can't count above 4
264275
*/
265276

266-
uInt sym_next; /* running index in sym_buf */
277+
uInt sym_next; /* running index in symbol buffer */
267278
uInt sym_end; /* symbol table full when sym_next reaches this */
268279

269280
ulg opt_len; /* bit length of current block with optimal trees */
@@ -342,6 +353,25 @@ void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf,
342353
extern const uch ZLIB_INTERNAL _dist_code[];
343354
#endif
344355

356+
#ifdef LIT_MEM
357+
# define _tr_tally_lit(s, c, flush) \
358+
{ uch cc = (c); \
359+
s->d_buf[s->sym_next] = 0; \
360+
s->l_buf[s->sym_next++] = cc; \
361+
s->dyn_ltree[cc].Freq++; \
362+
flush = (s->sym_next == s->sym_end); \
363+
}
364+
# define _tr_tally_dist(s, distance, length, flush) \
365+
{ uch len = (uch)(length); \
366+
ush dist = (ush)(distance); \
367+
s->d_buf[s->sym_next] = dist; \
368+
s->l_buf[s->sym_next++] = len; \
369+
dist--; \
370+
s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
371+
s->dyn_dtree[d_code(dist)].Freq++; \
372+
flush = (s->sym_next == s->sym_end); \
373+
}
374+
#else
345375
# define _tr_tally_lit(s, c, flush) \
346376
{ uch cc = (c); \
347377
s->sym_buf[s->sym_next++] = 0; \
@@ -361,6 +391,7 @@ void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf,
361391
s->dyn_dtree[d_code(dist)].Freq++; \
362392
flush = (s->sym_next == s->sym_end); \
363393
}
394+
#endif
364395
#else
365396
# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
366397
# define _tr_tally_dist(s, distance, length, flush) \

src/java.base/share/native/libzip/zlib/gzguts.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
/* gzguts.h -- zlib internal header definitions for gz* operations
26-
* Copyright (C) 2004-2019 Mark Adler
26+
* Copyright (C) 2004-2024 Mark Adler
2727
* For conditions of distribution and use, see copyright notice in zlib.h
2828
*/
2929

@@ -234,9 +234,5 @@ char ZLIB_INTERNAL *gz_strwinerror(DWORD error);
234234
/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t
235235
value -- needed when comparing unsigned to z_off64_t, which is signed
236236
(possible z_off64_t types off_t, off64_t, and long are all signed) */
237-
#ifdef INT_MAX
238-
# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX)
239-
#else
240237
unsigned ZLIB_INTERNAL gz_intmax(void);
241-
# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())
242-
#endif
238+
#define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax())

src/java.base/share/native/libzip/zlib/gzlib.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
/* gzlib.c -- zlib functions common to reading and writing gzip files
26-
* Copyright (C) 2004-2019 Mark Adler
26+
* Copyright (C) 2004-2024 Mark Adler
2727
* For conditions of distribution and use, see copyright notice in zlib.h
2828
*/
2929

@@ -587,20 +587,20 @@ void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg) {
587587
#endif
588588
}
589589

590-
#ifndef INT_MAX
591590
/* portably return maximum value for an int (when limits.h presumed not
592591
available) -- we need to do this to cover cases where 2's complement not
593592
used, since C standard permits 1's complement and sign-bit representations,
594593
otherwise we could just use ((unsigned)-1) >> 1 */
595594
unsigned ZLIB_INTERNAL gz_intmax(void) {
596-
unsigned p, q;
597-
598-
p = 1;
595+
#ifdef INT_MAX
596+
return INT_MAX;
597+
#else
598+
unsigned p = 1, q;
599599
do {
600600
q = p;
601601
p <<= 1;
602602
p++;
603603
} while (p > q);
604604
return q >> 1;
605-
}
606605
#endif
606+
}

src/java.base/share/native/libzip/zlib/inflate.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ int ZEXPORT inflateSync(z_streamp strm) {
14111411
/* if first time, start search in bit buffer */
14121412
if (state->mode != SYNC) {
14131413
state->mode = SYNC;
1414-
state->hold <<= state->bits & 7;
1414+
state->hold >>= state->bits & 7;
14151415
state->bits -= state->bits & 7;
14161416
len = 0;
14171417
while (state->bits >= 8) {

src/java.base/share/native/libzip/zlib/inftrees.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
/* inftrees.c -- generate Huffman trees for efficient decoding
26-
* Copyright (C) 1995-2023 Mark Adler
26+
* Copyright (C) 1995-2024 Mark Adler
2727
* For conditions of distribution and use, see copyright notice in zlib.h
2828
*/
2929

@@ -33,7 +33,7 @@
3333
#define MAXBITS 15
3434

3535
const char inflate_copyright[] =
36-
" inflate 1.3 Copyright 1995-2023 Mark Adler ";
36+
" inflate 1.3.1 Copyright 1995-2024 Mark Adler ";
3737
/*
3838
If you use the zlib library in a product, an acknowledgment is welcome
3939
in the documentation of your product. If for some reason you cannot
@@ -81,7 +81,7 @@ int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens,
8181
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
8282
static const unsigned short lext[31] = { /* Length codes 257..285 extra */
8383
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
84-
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 198, 203};
84+
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 203, 77};
8585
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
8686
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
8787
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,

src/java.base/share/native/libzip/zlib/inftrees.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ typedef struct {
6565
examples/enough.c found in the zlib distribution. The arguments to that
6666
program are the number of symbols, the initial root table size, and the
6767
maximum bit length of a code. "enough 286 9 15" for literal/length codes
68-
returns returns 852, and "enough 30 6 15" for distance codes returns 592.
69-
The initial root table size (9 or 6) is found in the fifth argument of the
68+
returns 852, and "enough 30 6 15" for distance codes returns 592. The
69+
initial root table size (9 or 6) is found in the fifth argument of the
7070
inflate_table() calls in inflate.c and infback.c. If the root table size is
7171
changed, then these maximum sizes would be need to be recalculated and
7272
updated. */

src/java.base/share/native/libzip/zlib/patches/ChangeLog_java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Changes from zlib 1.3
1+
Changes from zlib 1.3.1
22

33
(1) renamed adler32.c -> zadler32.c, crc32c -> zcrc32.c
44

src/java.base/share/native/libzip/zlib/trees.c

+17-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
/* trees.c -- output deflated data using Huffman coding
26-
* Copyright (C) 1995-2021 Jean-loup Gailly
26+
* Copyright (C) 1995-2024 Jean-loup Gailly
2727
* detect_data_type() function provided freely by Cosmin Truta, 2006
2828
* For conditions of distribution and use, see copyright notice in zlib.h
2929
*/
@@ -923,14 +923,19 @@ local void compress_block(deflate_state *s, const ct_data *ltree,
923923
const ct_data *dtree) {
924924
unsigned dist; /* distance of matched string */
925925
int lc; /* match length or unmatched char (if dist == 0) */
926-
unsigned sx = 0; /* running index in sym_buf */
926+
unsigned sx = 0; /* running index in symbol buffers */
927927
unsigned code; /* the code to send */
928928
int extra; /* number of extra bits to send */
929929

930930
if (s->sym_next != 0) do {
931+
#ifdef LIT_MEM
932+
dist = s->d_buf[sx];
933+
lc = s->l_buf[sx++];
934+
#else
931935
dist = s->sym_buf[sx++] & 0xff;
932936
dist += (unsigned)(s->sym_buf[sx++] & 0xff) << 8;
933937
lc = s->sym_buf[sx++];
938+
#endif
934939
if (dist == 0) {
935940
send_code(s, lc, ltree); /* send a literal byte */
936941
Tracecv(isgraph(lc), (stderr," '%c' ", lc));
@@ -955,8 +960,12 @@ local void compress_block(deflate_state *s, const ct_data *ltree,
955960
}
956961
} /* literal or match pair ? */
957962

958-
/* Check that the overlay between pending_buf and sym_buf is ok: */
963+
/* Check for no overlay of pending_buf on needed symbols */
964+
#ifdef LIT_MEM
965+
Assert(s->pending < 2 * (s->lit_bufsize + sx), "pendingBuf overflow");
966+
#else
959967
Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
968+
#endif
960969

961970
} while (sx < s->sym_next);
962971

@@ -1106,9 +1115,14 @@ void ZLIB_INTERNAL _tr_flush_block(deflate_state *s, charf *buf,
11061115
* the current block must be flushed.
11071116
*/
11081117
int ZLIB_INTERNAL _tr_tally(deflate_state *s, unsigned dist, unsigned lc) {
1118+
#ifdef LIT_MEM
1119+
s->d_buf[s->sym_next] = (ush)dist;
1120+
s->l_buf[s->sym_next++] = (uch)lc;
1121+
#else
11091122
s->sym_buf[s->sym_next++] = (uch)dist;
11101123
s->sym_buf[s->sym_next++] = (uch)(dist >> 8);
11111124
s->sym_buf[s->sym_next++] = (uch)lc;
1125+
#endif
11121126
if (dist == 0) {
11131127
/* lc is the unmatched char */
11141128
s->dyn_ltree[lc].Freq++;

0 commit comments

Comments
 (0)