Skip to content

Commit 2008249

Browse files
committed
lib: zlib_inflate: Update zlib to 1.2.13
The code in LK seems unaffected by CVE-2022-37434 (inflateGetHeader is not called anywhere), but it's still better to have it up to date. From https://zlib.net/zlib-1.2.13.tar.gz sha256 b3a24de97a8fdbc835b9833169501030b8977031bcb54b3b3ac13740f846ab30 (only copy existing source files, keep gzguts.h and NO_GZIP diff)
1 parent 9708d01 commit 2008249

File tree

9 files changed

+67
-39
lines changed

9 files changed

+67
-39
lines changed

lib/zlib_inflate/crc32.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,22 @@
9898
# endif
9999
#endif
100100

101+
/* If available, use the ARM processor CRC32 instruction. */
102+
#if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8
103+
# define ARMCRC32
104+
#endif
105+
101106
/* Local functions. */
102107
local z_crc_t multmodp OF((z_crc_t a, z_crc_t b));
103108
local z_crc_t x2nmodp OF((z_off64_t n, unsigned k));
104109

105-
/* If available, use the ARM processor CRC32 instruction. */
106-
#if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8
107-
# define ARMCRC32
110+
#if defined(W) && (!defined(ARMCRC32) || defined(DYNAMIC_CRC_TABLE))
111+
local z_word_t byte_swap OF((z_word_t word));
112+
#endif
113+
114+
#if defined(W) && !defined(ARMCRC32)
115+
local z_crc_t crc_word OF((z_word_t data));
116+
local z_word_t crc_word_big OF((z_word_t data));
108117
#endif
109118

110119
#if defined(W) && (!defined(ARMCRC32) || defined(DYNAMIC_CRC_TABLE))
@@ -630,7 +639,7 @@ unsigned long ZEXPORT crc32_z(crc, buf, len)
630639
#endif /* DYNAMIC_CRC_TABLE */
631640

632641
/* Pre-condition the CRC */
633-
crc ^= 0xffffffff;
642+
crc = (~crc) & 0xffffffff;
634643

635644
/* Compute the CRC up to a word boundary. */
636645
while (len && ((z_size_t)buf & 7) != 0) {
@@ -645,8 +654,8 @@ unsigned long ZEXPORT crc32_z(crc, buf, len)
645654
len &= 7;
646655

647656
/* Do three interleaved CRCs to realize the throughput of one crc32x
648-
instruction per cycle. Each CRC is calcuated on Z_BATCH words. The three
649-
CRCs are combined into a single CRC after each set of batches. */
657+
instruction per cycle. Each CRC is calculated on Z_BATCH words. The
658+
three CRCs are combined into a single CRC after each set of batches. */
650659
while (num >= 3 * Z_BATCH) {
651660
crc1 = 0;
652661
crc2 = 0;
@@ -749,7 +758,7 @@ unsigned long ZEXPORT crc32_z(crc, buf, len)
749758
#endif /* DYNAMIC_CRC_TABLE */
750759

751760
/* Pre-condition the CRC */
752-
crc ^= 0xffffffff;
761+
crc = (~crc) & 0xffffffff;
753762

754763
#ifdef W
755764

@@ -1077,7 +1086,7 @@ uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
10771086
#ifdef DYNAMIC_CRC_TABLE
10781087
once(&made, make_crc_table);
10791088
#endif /* DYNAMIC_CRC_TABLE */
1080-
return multmodp(x2nmodp(len2, 3), crc1) ^ crc2;
1089+
return multmodp(x2nmodp(len2, 3), crc1) ^ (crc2 & 0xffffffff);
10811090
}
10821091

10831092
/* ========================================================================= */
@@ -1086,7 +1095,7 @@ uLong ZEXPORT crc32_combine(crc1, crc2, len2)
10861095
uLong crc2;
10871096
z_off_t len2;
10881097
{
1089-
return crc32_combine64(crc1, crc2, len2);
1098+
return crc32_combine64(crc1, crc2, (z_off64_t)len2);
10901099
}
10911100

10921101
/* ========================================================================= */
@@ -1103,14 +1112,14 @@ uLong ZEXPORT crc32_combine_gen64(len2)
11031112
uLong ZEXPORT crc32_combine_gen(len2)
11041113
z_off_t len2;
11051114
{
1106-
return crc32_combine_gen64(len2);
1115+
return crc32_combine_gen64((z_off64_t)len2);
11071116
}
11081117

11091118
/* ========================================================================= */
1110-
uLong crc32_combine_op(crc1, crc2, op)
1119+
uLong ZEXPORT crc32_combine_op(crc1, crc2, op)
11111120
uLong crc1;
11121121
uLong crc2;
11131122
uLong op;
11141123
{
1115-
return multmodp(op, crc1) ^ crc2;
1124+
return multmodp(op, crc1) ^ (crc2 & 0xffffffff);
11161125
}

lib/zlib_inflate/inflate.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ int windowBits;
170170

171171
/* extract wrap request from windowBits parameter */
172172
if (windowBits < 0) {
173+
if (windowBits < -15)
174+
return Z_STREAM_ERROR;
173175
wrap = 0;
174176
windowBits = -windowBits;
175177
}
@@ -766,8 +768,9 @@ int flush;
766768
if (copy > have) copy = have;
767769
if (copy) {
768770
if (state->head != Z_NULL &&
769-
state->head->extra != Z_NULL) {
770-
len = state->head->extra_len - state->length;
771+
state->head->extra != Z_NULL &&
772+
(len = state->head->extra_len - state->length) <
773+
state->head->extra_max) {
771774
zmemcpy(state->head->extra + len, next,
772775
len + copy > state->head->extra_max ?
773776
state->head->extra_max - len : copy);

lib/zlib_inflate/inftrees.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#define MAXBITS 15
1010

1111
const char inflate_copyright[] =
12-
" inflate 1.2.12 Copyright 1995-2022 Mark Adler ";
12+
" inflate 1.2.13 Copyright 1995-2022 Mark Adler ";
1313
/*
1414
If you use the zlib library in a product, an acknowledgment is welcome
1515
in the documentation of your product. If for some reason you cannot
@@ -62,7 +62,7 @@ unsigned short FAR *work;
6262
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
6363
static const unsigned short lext[31] = { /* Length codes 257..285 extra */
6464
16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
65-
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 199, 202};
65+
19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 194, 65};
6666
static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
6767
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
6868
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,

lib/zlib_inflate/inftrees.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef struct {
3838
/* Maximum size of the dynamic table. The maximum number of code structures is
3939
1444, which is the sum of 852 for literal/length codes and 592 for distance
4040
codes. These values were found by exhaustive searches using the program
41-
examples/enough.c found in the zlib distribtution. The arguments to that
41+
examples/enough.c found in the zlib distribution. The arguments to that
4242
program are the number of symbols, the initial root table size, and the
4343
maximum bit length of a code. "enough 286 9 15" for literal/length codes
4444
returns returns 852, and "enough 30 6 15" for distance codes returns 592.

lib/zlib_inflate/uncompr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
Z_DATA_ERROR if the input data was corrupted, including if the input data is
2525
an incomplete zlib stream.
2626
*/
27-
int ZEXPORT uncompress2 (dest, destLen, source, sourceLen)
27+
int ZEXPORT uncompress2(dest, destLen, source, sourceLen)
2828
Bytef *dest;
2929
uLongf *destLen;
3030
const Bytef *source;
@@ -83,7 +83,7 @@ int ZEXPORT uncompress2 (dest, destLen, source, sourceLen)
8383
err;
8484
}
8585

86-
int ZEXPORT uncompress (dest, destLen, source, sourceLen)
86+
int ZEXPORT uncompress(dest, destLen, source, sourceLen)
8787
Bytef *dest;
8888
uLongf *destLen;
8989
const Bytef *source;

lib/zlib_inflate/zconf.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
# define crc32 z_crc32
3939
# define crc32_combine z_crc32_combine
4040
# define crc32_combine64 z_crc32_combine64
41+
# define crc32_combine_gen z_crc32_combine_gen
42+
# define crc32_combine_gen64 z_crc32_combine_gen64
43+
# define crc32_combine_op z_crc32_combine_op
4144
# define crc32_z z_crc32_z
4245
# define deflate z_deflate
4346
# define deflateBound z_deflateBound
@@ -349,6 +352,9 @@
349352
# ifdef FAR
350353
# undef FAR
351354
# endif
355+
# ifndef WIN32_LEAN_AND_MEAN
356+
# define WIN32_LEAN_AND_MEAN
357+
# endif
352358
# include <windows.h>
353359
/* No need for _export, use ZLIB.DEF instead. */
354360
/* For complete Windows compatibility, use WINAPI, not __stdcall. */
@@ -467,11 +473,18 @@ typedef uLong FAR uLongf;
467473
# undef _LARGEFILE64_SOURCE
468474
#endif
469475

470-
#if defined(__WATCOMC__) && !defined(Z_HAVE_UNISTD_H)
471-
# define Z_HAVE_UNISTD_H
476+
#ifndef Z_HAVE_UNISTD_H
477+
# ifdef __WATCOMC__
478+
# define Z_HAVE_UNISTD_H
479+
# endif
480+
#endif
481+
#ifndef Z_HAVE_UNISTD_H
482+
# if defined(_LARGEFILE64_SOURCE) && !defined(_WIN32)
483+
# define Z_HAVE_UNISTD_H
484+
# endif
472485
#endif
473486
#ifndef Z_SOLO
474-
# if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
487+
# if defined(Z_HAVE_UNISTD_H)
475488
# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
476489
# ifdef VMS
477490
# include <unixio.h> /* for off_t */

lib/zlib_inflate/zlib.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* zlib.h -- interface of the 'zlib' general purpose compression library
2-
version 1.2.12, March 11th, 2022
2+
version 1.2.13, October 13th, 2022
33
44
Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
55
@@ -37,11 +37,11 @@
3737
extern "C" {
3838
#endif
3939

40-
#define ZLIB_VERSION "1.2.12"
41-
#define ZLIB_VERNUM 0x12c0
40+
#define ZLIB_VERSION "1.2.13"
41+
#define ZLIB_VERNUM 0x12d0
4242
#define ZLIB_VER_MAJOR 1
4343
#define ZLIB_VER_MINOR 2
44-
#define ZLIB_VER_REVISION 12
44+
#define ZLIB_VER_REVISION 13
4545
#define ZLIB_VER_SUBREVISION 0
4646

4747
/*
@@ -276,7 +276,7 @@ ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
276276
== 0), or after each call of deflate(). If deflate returns Z_OK and with
277277
zero avail_out, it must be called again after making room in the output
278278
buffer because there might be more output pending. See deflatePending(),
279-
which can be used if desired to determine whether or not there is more ouput
279+
which can be used if desired to determine whether or not there is more output
280280
in that case.
281281
282282
Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
@@ -660,7 +660,7 @@ ZEXTERN int ZEXPORT deflateGetDictionary OF((z_streamp strm,
660660
to dictionary. dictionary must have enough space, where 32768 bytes is
661661
always enough. If deflateGetDictionary() is called with dictionary equal to
662662
Z_NULL, then only the dictionary length is returned, and nothing is copied.
663-
Similary, if dictLength is Z_NULL, then it is not set.
663+
Similarly, if dictLength is Z_NULL, then it is not set.
664664
665665
deflateGetDictionary() may return a length less than the window size, even
666666
when more than the window size in input has been provided. It may return up
@@ -915,7 +915,7 @@ ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm,
915915
to dictionary. dictionary must have enough space, where 32768 bytes is
916916
always enough. If inflateGetDictionary() is called with dictionary equal to
917917
Z_NULL, then only the dictionary length is returned, and nothing is copied.
918-
Similary, if dictLength is Z_NULL, then it is not set.
918+
Similarly, if dictLength is Z_NULL, then it is not set.
919919
920920
inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the
921921
stream state is inconsistent.
@@ -1437,12 +1437,12 @@ ZEXTERN z_size_t ZEXPORT gzfread OF((voidp buf, z_size_t size, z_size_t nitems,
14371437
14381438
In the event that the end of file is reached and only a partial item is
14391439
available at the end, i.e. the remaining uncompressed data length is not a
1440-
multiple of size, then the final partial item is nevetheless read into buf
1440+
multiple of size, then the final partial item is nevertheless read into buf
14411441
and the end-of-file flag is set. The length of the partial item read is not
14421442
provided, but could be inferred from the result of gztell(). This behavior
14431443
is the same as the behavior of fread() implementations in common libraries,
14441444
but it prevents the direct use of gzfread() to read a concurrently written
1445-
file, reseting and retrying on end-of-file, when size is not 1.
1445+
file, resetting and retrying on end-of-file, when size is not 1.
14461446
*/
14471447

14481448
ZEXTERN int ZEXPORT gzwrite OF((gzFile file, voidpc buf, unsigned len));
@@ -1913,7 +1913,7 @@ ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp));
19131913
ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table OF((void));
19141914
ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int));
19151915
ZEXTERN int ZEXPORT inflateValidate OF((z_streamp, int));
1916-
ZEXTERN unsigned long ZEXPORT inflateCodesUsed OF ((z_streamp));
1916+
ZEXTERN unsigned long ZEXPORT inflateCodesUsed OF((z_streamp));
19171917
ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp));
19181918
ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp));
19191919
#if defined(_WIN32) && !defined(Z_SOLO)

lib/zlib_inflate/zutil.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ uLong ZEXPORT zlibCompileFlags()
6262
#ifdef ZLIB_DEBUG
6363
flags += 1 << 8;
6464
#endif
65+
/*
6566
#if defined(ASMV) || defined(ASMINF)
6667
flags += 1 << 9;
6768
#endif
69+
*/
6870
#ifdef ZLIB_WINAPI
6971
flags += 1 << 10;
7072
#endif
@@ -120,7 +122,7 @@ uLong ZEXPORT zlibCompileFlags()
120122
# endif
121123
int ZLIB_INTERNAL z_verbose = verbose;
122124

123-
void ZLIB_INTERNAL z_error (m)
125+
void ZLIB_INTERNAL z_error(m)
124126
char *m;
125127
{
126128
fprintf(stderr, "%s\n", m);
@@ -215,7 +217,7 @@ local ptr_table table[MAX_PTR];
215217
* a protected system like OS/2. Use Microsoft C instead.
216218
*/
217219

218-
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
220+
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size)
219221
{
220222
voidpf buf;
221223
ulg bsize = (ulg)items*size;
@@ -241,7 +243,7 @@ voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size)
241243
return buf;
242244
}
243245

244-
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
246+
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
245247
{
246248
int n;
247249

@@ -278,13 +280,13 @@ void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
278280
# define _hfree hfree
279281
#endif
280282

281-
voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size)
283+
voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, uInt items, uInt size)
282284
{
283285
(void)opaque;
284286
return _halloc((long)items, size);
285287
}
286288

287-
void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr)
289+
void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr)
288290
{
289291
(void)opaque;
290292
_hfree(ptr);
@@ -303,7 +305,7 @@ extern voidp calloc OF((uInt items, uInt size));
303305
extern void free OF((voidpf ptr));
304306
#endif
305307

306-
voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
308+
voidpf ZLIB_INTERNAL zcalloc(opaque, items, size)
307309
voidpf opaque;
308310
unsigned items;
309311
unsigned size;
@@ -313,7 +315,7 @@ voidpf ZLIB_INTERNAL zcalloc (opaque, items, size)
313315
(voidpf)calloc(items, size);
314316
}
315317

316-
void ZLIB_INTERNAL zcfree (opaque, ptr)
318+
void ZLIB_INTERNAL zcfree(opaque, ptr)
317319
voidpf opaque;
318320
voidpf ptr;
319321
{

lib/zlib_inflate/zutil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
193193
(!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0)
194194
ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
195195
ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
196+
ZEXTERN uLong ZEXPORT crc32_combine_gen64 OF((z_off_t));
196197
#endif
197198

198199
/* common defaults */

0 commit comments

Comments
 (0)