From: Theo Buehler Subject: zlib 1.4 To: tech@openbsd.org Date: Tue, 17 Nov 2026 11:33:21 +0100 We have already landed and errataed the security-relevant bits. Here is the rest of the update. The diff went through numerous bulks, but people may want to test the kernel bits with their favorite features (hibernation, ipsec). No real header changes except for some docs (some of which I pulled into compress.3) and of course the version bumps. Unless people shout, I am going to land this soon. oks are of course welcome. Index: lib/libz/ChangeLog =================================================================== RCS file: /cvs/src/lib/libz/ChangeLog,v diff -u -p -r1.12 ChangeLog --- lib/libz/ChangeLog 19 Mar 2026 15:10:55 -0000 1.12 +++ lib/libz/ChangeLog 17 Nov 2026 07:55:25 -0000 @@ -1,6 +1,37 @@ ChangeLog file for zlib +Changes in 1.4 (4 Nov 2026) +- Change CMake library names on MSVC +- Return z_off_t default to long for portability +- Fail gzopen() with "x" if O_EXCL not available +- Deliver all gzip data before reporting error +- Fix gzwrite() buffer overflow (CVE-2026-85091) +- Fix inflateGetDictionary() write past buffer on invalid window size +- Fix window write overrun after inflateSync() +- Fix read before provided data after repeated inflatePrime()'s +- Fix write past pending buffer after repeated deflatePrime()'s +- Fix copy of uninitialized memory by inflateBack() on invalid data +- Fix copy of uninitialized memory after gzseek() as first write action +- Fix read outside window after a deflateResetKeep() mid-stream +- Avoid out-of-bounds pointer arithmetic in infback9() (CVE-2026-4738) +- Fix several vulnerabilities in contrib/minizip +- Fix truncated stream detection in gzread() +- Fix pending skip handling in gzread() +- Fix loss of zero fill on repeated gzseek()'s when writing +- Fix inaccurate total_in after Z_NEED_DICT returned +- Detect and report bad deflateSetDictionary() calls +- Avoid undefined behaviors in C language usage +- Fix minizip extra field overflows when almost full and need Zip64 +- Don't assume NULL is defined in compress.c and uncompr.c +- Use the generated zconf.h when building test programs out of tree +- Improve portability and functionality of CMake builds +- Provide 64-bit file offsets on Windows with #defines as on Linux +- Improve portability to Android, AIX, PowerPC, s390x, and Windows +- Use a PCG random number generator instead of rand() in minizip +- Clarify documentation +- (See https://github.com/madler/zlib for all 195 commits) + Changes in 1.3.2 (17 Feb 2026) - Continued rewrite of CMake build [Vollstrecker] - Various portability improvements Index: lib/libz/README =================================================================== RCS file: /cvs/src/lib/libz/README,v diff -u -p -r1.20 README --- lib/libz/README 19 Mar 2026 15:10:55 -0000 1.20 +++ lib/libz/README 17 Nov 2026 07:55:25 -0000 @@ -1,6 +1,6 @@ ZLIB DATA COMPRESSION LIBRARY -zlib 1.3.2 is a general purpose data compression library. All the code is +zlib 1.4 is a general purpose data compression library. All the code is thread safe (though see the FAQ for caveats). The data format used by the zlib library is described by RFCs (Request for Comments) 1950 to 1952 at https://datatracker.ietf.org/doc/html/rfc1950 (zlib format), rfc1951 (deflate @@ -31,7 +31,7 @@ Mark Nelson wrote an ar issue of Dr. Dobb's Journal; a copy of the article is available at https://zlib.net/nelson/ . -The changes made in version 1.3.2 are documented in the file ChangeLog. +The changes made in version 1.4 are documented in the file ChangeLog. Unsupported third party contributions are provided in directory contrib/ . Index: lib/libz/compress.3 =================================================================== RCS file: /cvs/src/lib/libz/compress.3,v diff -u -p -r1.37 compress.3 --- lib/libz/compress.3 10 Sep 2026 01:28:12 -0000 1.37 +++ lib/libz/compress.3 17 Nov 2026 07:55:25 -0000 @@ -1345,6 +1345,18 @@ if the source stream state was inconsist being NULL). .Fa msg is left unchanged in both source and destination. +If +.Fn deflateCopy +fails and +.Fa dest +is not +.Dv Z_NULL , +then dest->state is set to +.Dv Z_NULL +with +.Fa dest +otherwise unchanged, so that deflateEnd(dest) can be called safely, +though dest is not a usable stream. .It Xo .Fa int .Fn deflateReset "z_streamp strm" ; @@ -1485,7 +1497,9 @@ It must be called after or .Fn deflateInit2 . and after -.Fn deflateSetHeader , +.Fn deflateSetHeader +or +.Fn deflateSetDictionary , if used. This would be used to allocate an output buffer for deflation in a single pass, and so would be called before @@ -1690,9 +1704,8 @@ while compressing, or it must be equal t was not used. If a compressed stream with a larger window size is given as input, .Fn inflate -will return with the error code -.Dv Z_DATA_ERROR -instead of trying to allocate a larger window. +will return with a +.Dv Z_DATA_ERROR . .Pp .Fa windowBits can also be zero to request that inflate use the window size in the zlib header @@ -1898,6 +1911,20 @@ is left unchanged in both .Fa source and .Fa dest . +If +.Fn inflateCopy +fails and +.Fa dest +is not +.Dv Z_NULL , +then dest->state is set to +.Dv Z_NULL , +with +.Fa dest +otherwise unchanged, so that inflateEnd(dest) can be called safely, +though +.Fa dest +is not a usable stream. .It Xo .Fa int .Fn inflateReset "z_streamp strm" ; @@ -2154,6 +2181,8 @@ to signal its absence. This allows the use of .Fn deflateSetHeader with the returned structure to duplicate the header. +Though take care that none of the fields were truncated before using it, +lest havoc ensue. However if those fields are set to allocated memory, then the application will need to save those pointers elsewhere so that they can be eventually freed. @@ -3089,6 +3118,11 @@ or if there was an error. .Fn gzerror must be consulted if zero is returned in order to determine if there was an error. +If some data was read before an error, +then that data is returned until exhausted, +after which the next call will return zero and +.Fn gzerror +will report an error. If the multiplication of .Fa size and @@ -3398,7 +3432,11 @@ the next read or write operation, or clo returns the resulting offset location as measured in bytes from the beginning of the uncompressed stream, or -1 in case of error, in particular if the file is opened for writing and the new starting position -would be before the current position. +would be before the current position, +or if the resulting position would not be representable in the offset type. +If +.Fn gzseek +returns \-1, the stream position is unchanged. .It Xo .Fa int .Fn gzrewind "gzFile file" ; @@ -3601,6 +3639,10 @@ is set to and the application may consult .Va errno to get the exact error code. +The error is reported by +.Fn gzerror +as ssoon as it is detected, whic may be +before all of the data read prior to the error has been returned. .Pp The application must not modify the returned string. Future calls to this function may invalidate the previously returned string. Index: lib/libz/compress.c =================================================================== RCS file: /cvs/src/lib/libz/compress.c,v diff -u -p -r1.11 compress.c --- lib/libz/compress.c 19 Mar 2026 15:10:55 -0000 1.11 +++ lib/libz/compress.c 17 Nov 2026 07:55:25 -0000 @@ -26,8 +26,8 @@ int ZEXPORT compress2_z(Bytef *dest, z_s const uInt max = (uInt)-1; z_size_t left; - if ((sourceLen > 0 && source == NULL) || - destLen == NULL || (*destLen > 0 && dest == NULL)) + if ((sourceLen > 0 && source == Z_NULL) || + destLen == Z_NULL || (*destLen > 0 && dest == Z_NULL)) return Z_STREAM_ERROR; left = *destLen; Index: lib/libz/crc32.c =================================================================== RCS file: /cvs/src/lib/libz/crc32.c,v diff -u -p -r1.19 crc32.c --- lib/libz/crc32.c 19 Mar 2026 15:10:55 -0000 1.19 +++ lib/libz/crc32.c 17 Nov 2026 07:55:25 -0000 @@ -684,7 +684,7 @@ uLong ZEXPORT crc32_z(uLong crc, const u #endif /* Initialize the CRC for each braid. */ - crc0 = crc; + crc0 = (z_crc_t)crc; #if N > 1 crc1 = 0; #if N > 2 Index: lib/libz/deflate.c =================================================================== RCS file: /cvs/src/lib/libz/deflate.c,v diff -u -p -r1.32 deflate.c --- lib/libz/deflate.c 6 Nov 2026 15:30:25 -0000 1.32 +++ lib/libz/deflate.c 17 Nov 2026 07:55:25 -0000 @@ -441,7 +441,6 @@ int ZEXPORT deflateInit2_(z_streamp strm s->status = INIT_STATE; /* to pass state test in deflateReset() */ s->wrap = wrap; - s->gzhead = Z_NULL; s->w_bits = (uInt)windowBits; s->w_size = 1 << s->w_bits; s->w_mask = s->w_size - 1; @@ -564,7 +563,8 @@ int ZEXPORT deflateSetDictionary(z_strea return Z_STREAM_ERROR; s = strm->state; wrap = s->wrap; - if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) + if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead || + (wrap == 0 && (long)s->strstart != s->block_start)) return Z_STREAM_ERROR; /* when using zlib wrappers, compute Adler-32 for provided dictionary */ @@ -667,9 +667,11 @@ int ZEXPORT deflateResetKeep(z_streamp s adler32(0L, Z_NULL, 0); s->last_flush = -2; + s->gzhead = Z_NULL; + { /* Retain the hash work done for level 0. */ - int matches = s->level == 0 ? s->matches : 0; + uInt matches = s->level == 0 ? s->matches : 0; _tr_init(s); s->matches = matches; } @@ -1329,39 +1331,49 @@ int ZEXPORT deflateEnd(z_streamp strm) { */ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) { #ifdef MAXSEG_64K - (void)dest; (void)source; + if (dest != Z_NULL) + dest->state = Z_NULL; return Z_STREAM_ERROR; #else deflate_state *ds; deflate_state *ss; - - if (deflateStateCheck(source) || dest == Z_NULL) { + if (dest == Z_NULL) + return Z_STREAM_ERROR; + if (deflateStateCheck(source)) { + dest->state = Z_NULL; return Z_STREAM_ERROR; } ss = source->state; - zmemcpy(dest, source, sizeof(z_stream)); - - ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); - if (ds == Z_NULL) return Z_MEM_ERROR; - zmemzero(ds, sizeof(deflate_state)); - dest->state = (struct internal_state FAR *) ds; + ds = (deflate_state *) ZALLOC(source, 1, sizeof(deflate_state)); + if (ds == Z_NULL) { + dest->state = Z_NULL; + return Z_MEM_ERROR; + } zmemcpy(ds, ss, sizeof(deflate_state)); - ds->strm = dest; - - ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); - ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); - ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); - ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, LIT_BUFS); - + ds->window = (Bytef *) ZALLOC(source, ds->w_size, 2*sizeof(Byte)); + ds->prev = (Posf *) ZALLOC(source, ds->w_size, sizeof(Pos)); + ds->head = (Posf *) ZALLOC(source, ds->hash_size, sizeof(Pos)); + ds->pending_buf = (uchf *) ZALLOC(source, ds->lit_bufsize, LIT_BUFS); if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || ds->pending_buf == Z_NULL) { - deflateEnd (dest); + TRY_FREE(source, ds->pending_buf); + TRY_FREE(source, ds->head); + TRY_FREE(source, ds->prev); + TRY_FREE(source, ds->window); + ZFREE(source, ds); + dest->state = Z_NULL; return Z_MEM_ERROR; } + + /* copy the stream and attach the new state */ + zmemcpy(dest, source, sizeof(z_stream)); + dest->state = (struct internal_state FAR *) ds; + ds->strm = dest; + /* following zmemcpy's do not work for 16-bit MSDOS */ zmemcpy(ds->window, ss->window, ss->high_water); zmemcpy(ds->prev, ss->prev, @@ -1654,7 +1666,10 @@ local void check_match(deflate_state *s, /* Same but force premature exit if necessary. */ #define FLUSH_BLOCK(s, last) { \ FLUSH_BLOCK_ONLY(s, last); \ - if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ + if (s->strm->avail_out == 0) { \ + if (!(last)) return need_more; \ + if (s->pending != 0 || s->wrap != 0) return finish_started; \ + } \ } /* Maximum stored block length in deflate format (not including header). */ @@ -1689,7 +1704,7 @@ local block_state deflate_stored(deflate * possible. If flushing, copy the remaining available input to next_out as * stored blocks, if there is enough space. */ - int last = 0; + int last = 0, all; unsigned len, left, have; unsigned used = s->strm->avail_in; do { @@ -1704,11 +1719,14 @@ local block_state deflate_stored(deflate /* maximum stored block length that will fit in avail_out: */ have = s->strm->avail_out - have; left = (unsigned)(s->strstart - s->block_start); /* window bytes */ - if (len > (ulg)left + s->strm->avail_in) + if (left < len && s->strm->avail_in < len - left) len = left + s->strm->avail_in; /* limit len to the input */ if (len > have) len = have; /* limit len to the output */ + /* all is true if len is all of the available input */ + all = len >= left && len - left == s->strm->avail_in; + /* If the stored block would be less than min_block in length, or if * unable to copy all of the available input when flushing, then try * copying to the window and the pending buffer instead. Also don't @@ -1716,13 +1734,13 @@ local block_state deflate_stored(deflate */ if (len < min_block && ((len == 0 && flush != Z_FINISH) || flush == Z_NO_FLUSH || - len != left + s->strm->avail_in)) + !all)) break; /* Make a dummy stored block in pending to get the header bytes, * including any pending bits. This also updates the debugging counts. */ - last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; + last = flush == Z_FINISH && all ? 1 : 0; _tr_stored_block(s, (char *)0, 0L, last); /* Replace the lengths in the dummy stored block with len. */ @@ -2027,7 +2045,8 @@ local block_state deflate_slow(deflate_s uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; /* Do not insert strings in hash table beyond this. */ - check_match(s, s->strstart - 1, s->prev_match, (int)s->prev_length); + check_match(s, s->strstart - 1, s->prev_match, + (int)s->prev_length); _tr_tally_dist(s, s->strstart - 1 - s->prev_match, s->prev_length - MIN_MATCH, bflush); Index: lib/libz/gzguts.h =================================================================== RCS file: /cvs/src/lib/libz/gzguts.h,v diff -u -p -r1.14 gzguts.h --- lib/libz/gzguts.h 19 Mar 2026 15:10:55 -0000 1.14 +++ lib/libz/gzguts.h 17 Nov 2026 07:55:25 -0000 @@ -92,8 +92,10 @@ # define vsnprintf _vsnprintf # endif # endif -# elif !defined(__STDC_VERSION__) || __STDC_VERSION__-0 < 199901L -/* Otherwise if C89/90, assume no C99 snprintf() or vsnprintf() */ +# elif (!defined(__STDC_VERSION__) || __STDC_VERSION__-0 < 199901L) && \ + (!defined(__cplusplus) || __cplusplus < 201103L) +/* Otherwise if C89/90 (and not C++11 or later), assume no C99 snprintf() or + vsnprintf() */ # ifndef NO_snprintf # define NO_snprintf # endif @@ -128,14 +130,28 @@ # include # define zstrerror() gz_strwinerror((DWORD)GetLastError()) #else +# include # ifndef NO_STRERROR -# include # define zstrerror() strerror(errno) # else # define zstrerror() "stdio error (consult errno)" # endif #endif +#ifdef EAGAIN +# ifdef EWOULDBLOCK +# define BLOCKED(e) ((e) == EAGAIN || (e) == EWOULDBLOCK) +# else +# define BLOCKED(e) ((e) == EAGAIN) +# endif +#else +# ifdef EWOULDBLOCK +# define BLOCKED(e) ((e) == EWOULDBLOCK) +# else +# define BLOCKED(e) (0) +# endif +#endif + /* provide prototypes for these when building zlib without LFS */ #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); @@ -214,3 +230,10 @@ char ZLIB_INTERNAL *gz_strwinerror(DWORD (possible z_off64_t types off_t, off64_t, and long are all signed) */ unsigned ZLIB_INTERNAL gz_intmax(void); #define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) + +/* largest value representable in z_off64_t */ +#ifndef CHAR_BIT +# define CHAR_BIT 8 +#endif +#define GZ_OFF_MAX \ + ((((z_off64_t)1 << (sizeof(z_off64_t) * CHAR_BIT - 2)) - 1) * 2 + 1) Index: lib/libz/gzlib.c =================================================================== RCS file: /cvs/src/lib/libz/gzlib.c,v diff -u -p -r1.13 gzlib.c --- lib/libz/gzlib.c 6 Nov 2026 15:30:25 -0000 1.13 +++ lib/libz/gzlib.c 17 Nov 2026 07:55:25 -0000 @@ -68,9 +68,9 @@ char ZLIB_INTERNAL *gz_strwinerror(DWORD /* Reset gzip file state */ local void gz_reset(gz_statep state) { state->x.have = 0; /* no output data available */ + state->past = 0; /* have not read past end yet */ if (state->mode == GZ_READ) { /* for reading ... */ state->eof = 0; /* not at end of file */ - state->past = 0; /* have not read past end yet */ state->how = LOOK; /* look for gzip header */ state->junk = -1; /* mark first member */ } @@ -88,9 +88,7 @@ local gzFile gz_open(const void *path, i gz_statep state; z_size_t len; int oflag = 0; -#ifdef O_EXCL int exclusive = 0; -#endif /* check input */ if (path == NULL || mode == NULL) @@ -136,11 +134,9 @@ local gzFile gz_open(const void *path, i oflag |= O_CLOEXEC; break; #endif -#ifdef O_EXCL case 'x': exclusive = 1; break; -#endif case 'f': state->strategy = Z_FILTERED; break; @@ -196,10 +192,21 @@ local gzFile gz_open(const void *path, i /* if reading, direct == 1 for auto-detect, -1 for gzip only; if writing or appending, direct == 0 for gzip, 1 for transparent (copy in to out) */ +#ifndef O_EXCL + if (exclusive && state->mode != GZ_READ && fd < 0) { + /* exclusive creation request can't be honored -- fail out */ + free(state); + return NULL; + } +#endif + /* save the path name for error messages */ #ifdef WIDECHAR - if (fd == -2) + if (fd == -2) { len = wcstombs(NULL, path, 0); + if (len == (z_size_t)-1) + len = 0; /* not representable in the current locale */ + } else #endif len = strlen((const char *)path); @@ -210,9 +217,7 @@ local gzFile gz_open(const void *path, i } #ifdef WIDECHAR if (fd == -2) { - if (len) - wcstombs(state->path, path, len + 1); - else + if (len == 0 || wcstombs(state->path, path, len + 1) != len) *(state->path) = 0; } else @@ -221,7 +226,7 @@ local gzFile gz_open(const void *path, i #if !defined(NO_snprintf) && !defined(NO_vsnprintf) (void)snprintf(state->path, len + 1, "%s", (const char *)path); #else - strcpy(state->path, path); + strcpy(state->path, (const char *)path); #endif } @@ -299,7 +304,7 @@ gzFile ZEXPORT gzdopen(int fd, const cha char *path; /* identifier for error messages */ gzFile gz; - if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) + if (fd < 0 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) return NULL; #if !defined(NO_snprintf) && !defined(NO_vsnprintf) (void)snprintf(path, 7 + 3 * sizeof(int), "", fd); @@ -366,7 +371,7 @@ int ZEXPORT gzrewind(gzFile file) { /* -- see zlib.h -- */ z_off64_t ZEXPORT gzseek64(gzFile file, z_off64_t offset, int whence) { unsigned n; - z_off64_t ret; + z_off64_t ret, pos; gz_statep state; /* get internal structure and check integrity */ @@ -384,18 +389,19 @@ z_off64_t ZEXPORT gzseek64(gzFile file, if (whence != SEEK_SET && whence != SEEK_CUR) return -1; - /* normalize offset to a SEEK_CUR specification */ - if (whence == SEEK_SET) - offset -= state->x.pos; - else { - offset += state->past ? 0 : state->skip; - state->skip = 0; - } + /* compute the absolute position */ + pos = whence == SEEK_SET ? 0 : + state->x.pos + (state->past ? 0 : state->skip); + if (offset > GZ_OFF_MAX - pos) + return -1; /* would overflow z_off64_t */ + if (offset < -pos) + return -1; /* before start of file */ + pos += offset; /* if within raw area while reading, just go there */ - if (state->mode == GZ_READ && state->how == COPY && - state->x.pos + offset >= 0) { - ret = LSEEK(state->fd, offset - (z_off64_t)state->x.have, SEEK_CUR); + if (state->mode == GZ_READ && state->how == COPY) { + ret = LSEEK(state->fd, pos - state->x.pos - (z_off64_t)state->x.have, + SEEK_CUR); if (ret == -1) return -1; state->x.have = 0; @@ -404,20 +410,18 @@ z_off64_t ZEXPORT gzseek64(gzFile file, state->skip = 0; gz_error(state, Z_OK, NULL); state->strm.avail_in = 0; - state->x.pos += offset; - return state->x.pos; + state->x.pos = pos; + return pos; } - /* calculate skip amount, rewinding if needed for back seek when reading */ - if (offset < 0) { + /* rewind if needed for back seek when reading */ + if (pos < state->x.pos) { if (state->mode != GZ_READ) /* writing -- can't go backwards */ return -1; - offset += state->x.pos; - if (offset < 0) /* before start of file! */ - return -1; - if (gzrewind(file) == -1) /* rewind, then skip to offset */ + if (gzrewind(file) == -1) /* rewind, then skip to pos */ return -1; } + offset = pos - state->x.pos; /* if reading, skip what's in output buffer (one less gzgetc() check) */ if (state->mode == GZ_READ) { @@ -431,7 +435,7 @@ z_off64_t ZEXPORT gzseek64(gzFile file, /* request skip (if not zero) */ state->skip = offset; - return state->x.pos + offset; + return pos; } /* -- see zlib.h -- */ @@ -559,10 +563,6 @@ void ZLIB_INTERNAL gz_error(gz_statep st free(state->msg); state->msg = NULL; } - - /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */ - if (err != Z_OK && err != Z_BUF_ERROR && !state->again) - state->x.have = 0; /* set error code, and if no message, then done */ state->err = err; Index: lib/libz/gzread.c =================================================================== RCS file: /cvs/src/lib/libz/gzread.c,v diff -u -p -r1.8 gzread.c --- lib/libz/gzread.c 19 Mar 2026 15:10:55 -0000 1.8 +++ lib/libz/gzread.c 17 Nov 2026 07:55:25 -0000 @@ -33,7 +33,7 @@ local int gz_load(gz_statep state, unsig *have += (unsigned)ret; } while (*have < len); if (ret < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { + if (BLOCKED(errno)) { state->again = 1; if (*have != 0) return 0; @@ -125,6 +125,13 @@ local int gz_look(gz_statep state) { if we're looking for a gzip member after the first one, which is not at the start, then proceed directly to look for a gzip member next */ if (state->direct == -1 || state->junk == 0) { + /* wait for input before committing to another gzip member */ + if (state->junk == 0 && strm->avail_in == 0) { + if (gz_avail(state) == -1) + return -1; + if (strm->avail_in == 0) + return 0; + } inflateReset(strm); state->how = GZIP; state->junk = state->junk != -1; @@ -153,7 +160,7 @@ local int gz_look(gz_statep state) { strm->next_in[2] == 8 && strm->next_in[3] < 32) { inflateReset(strm); state->how = GZIP; - state->junk = 1; + state->junk = 0; state->direct = 0; return 0; } @@ -248,6 +255,11 @@ local int gz_decomp(gz_statep state) { local int gz_fetch(gz_statep state) { z_streamp strm = &(state->strm); + /* after a serious error, only what is already in the output buffer is + delivered -- don't try to decompress any more */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + return -1; + do { switch(state->how) { case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */ @@ -276,32 +288,46 @@ local int gz_fetch(gz_statep state) { return 0; } +/* Apply as much of a pending skip request as the output already available in + state->x allows. */ +local void gz_skip_have(gz_statep state) { + unsigned n; + + n = GT_OFF(state->x.have) || + (z_off64_t)state->x.have > state->skip ? + (unsigned)state->skip : state->x.have; + state->x.have -= n; + state->x.next += n; + state->x.pos += n; + state->skip -= n; +} + /* Skip state->skip (> 0) uncompressed bytes of output. Return -1 on error, 0 on success. */ local int gz_skip(gz_statep state) { - unsigned n; - /* skip over len bytes or reach end-of-file, whichever comes first */ do { /* skip over whatever is in output buffer */ - if (state->x.have) { - n = GT_OFF(state->x.have) || - (z_off64_t)state->x.have > state->skip ? - (unsigned)state->skip : state->x.have; - state->x.have -= n; - state->x.next += n; - state->x.pos += n; - state->skip -= n; - } + if (state->x.have) + gz_skip_have(state); /* output buffer empty -- return if we're at the end of the input */ - else if (state->eof && state->strm.avail_in == 0) + else if (state->eof && state->strm.avail_in == 0) { + if (state->how == GZIP && state->err == Z_OK) + gz_error(state, Z_BUF_ERROR, "unexpected end of file"); + state->skip = 0; + state->past = 1; break; + } /* need more data to skip -- load up output buffer */ else { - /* get more output, looking for header if required */ - if (gz_fetch(state) == -1) + /* get more output, looking for header if required. If the fetch + stalled but provided some output, consume it before returning + so that x.have > 0 always implies the data is at the current + logical position (x.pos), as assumed by the gzgetc() fast path + in zlib.h and gzread.c */ + if (gz_fetch(state) == -1 && state->x.have == 0) return -1; } } while (state->skip); @@ -349,8 +375,11 @@ local z_size_t gz_read(gz_statep state, } /* output buffer empty -- return if we're at the end of the input */ - else if (state->eof && state->strm.avail_in == 0) + else if (state->eof && state->strm.avail_in == 0) { + if (state->how == GZIP && state->err == Z_OK) + gz_error(state, Z_BUF_ERROR, "unexpected end of file"); break; + } /* need output data -- for small len or new stream load up our output buffer, so that gzgetc() can be fast */ @@ -403,15 +432,21 @@ int ZEXPORT gzread(gzFile file, voidp bu if (state->mode != GZ_READ) return -1; - /* check that there was no (serious) error */ - if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + /* check that there was no (serious) error -- if there was, deliver any + data read before the error until it is exhausted, and only then return + the error */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again && + state->x.have == 0) return -1; - gz_error(state, Z_OK, NULL); + if (state->again) + gz_error(state, Z_OK, NULL); /* since an int is returned, make sure len fits in one, otherwise return - with an error (this avoids a flaw in the interface) */ + with an error (this avoids a flaw in the interface) -- keep a pending + serious error, which is the one the caller needs to see */ if ((int)len < 0) { - gz_error(state, Z_STREAM_ERROR, "request does not fit in an int"); + if (state->err == Z_OK || state->err == Z_BUF_ERROR) + gz_error(state, Z_STREAM_ERROR, "request does not fit in an int"); return -1; } @@ -448,15 +483,22 @@ z_size_t ZEXPORT gzfread(voidp buf, z_si if (state->mode != GZ_READ) return 0; - /* check that there was no (serious) error */ - if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + /* check that there was no (serious) error -- if there was, deliver any + data read before the error until it is exhausted, and only then return + the error */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again && + state->x.have == 0) return 0; - gz_error(state, Z_OK, NULL); + if (state->again) + gz_error(state, Z_OK, NULL); - /* compute bytes to read -- error on overflow */ + /* compute bytes to read -- error on overflow, keeping a pending serious + error, which is the one the caller needs to see */ len = nitems * size; if (size && len / size != nitems) { - gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t"); + if (state->err == Z_OK || state->err == Z_BUF_ERROR) + gz_error(state, Z_STREAM_ERROR, + "request does not fit in a size_t"); return 0; } @@ -481,10 +523,14 @@ int ZEXPORT gzgetc(gzFile file) { if (state->mode != GZ_READ) return -1; - /* check that there was no (serious) error */ - if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + /* check that there was no (serious) error -- if there was, deliver any + data read before the error until it is exhausted, and only then return + the error, as the gzgetc() macro does */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again && + state->x.have == 0) return -1; - gz_error(state, Z_OK, NULL); + if (state->again) + gz_error(state, Z_OK, NULL); /* try output buffer (no need to check for skip request) */ if (state->x.have) { @@ -516,10 +562,15 @@ int ZEXPORT gzungetc(int c, gzFile file) if (state->how == LOOK && state->x.have == 0) (void)gz_look(state); - /* check that there was no (serious) error */ - if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + /* check that there was no (serious) error -- a character can still be + pushed back after one, since it is data that was read before the + error, and it will be returned before the error is signaled -- but only + if there is an output buffer to put it in */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again && + state->size == 0) return -1; - gz_error(state, Z_OK, NULL); + if (state->again) + gz_error(state, Z_OK, NULL); /* process a skip request */ if (state->skip && gz_skip(state) == -1) @@ -541,7 +592,8 @@ int ZEXPORT gzungetc(int c, gzFile file) /* if no room, give up (must have already done a gzungetc()) */ if (state->x.have == (state->size << 1)) { - gz_error(state, Z_DATA_ERROR, "out of room to push characters"); + if (state->err == Z_OK || state->err == Z_BUF_ERROR) + gz_error(state, Z_DATA_ERROR, "out of room to push characters"); return -1; } @@ -577,10 +629,14 @@ char * ZEXPORT gzgets(gzFile file, char if (state->mode != GZ_READ) return NULL; - /* check that there was no (serious) error */ - if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again) + /* check that there was no (serious) error -- if there was, deliver any + data read before the error until it is exhausted, and only then return + the error */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR && !state->again && + state->x.have == 0) return NULL; - gz_error(state, Z_OK, NULL); + if (state->again) + gz_error(state, Z_OK, NULL); /* process a skip request */ if (state->skip && gz_skip(state) == -1) @@ -592,8 +648,9 @@ char * ZEXPORT gzgets(gzFile file, char left = (unsigned)len - 1; if (left) do { /* assure that something is in the output buffer */ - if (state->x.have == 0 && gz_fetch(state) == -1) - break; /* error */ + if (state->x.have == 0 && gz_fetch(state) == -1 && + state->x.have == 0) + break; /* error, and nothing left to return */ if (state->x.have == 0) { /* end of file */ state->past = 1; /* read past end */ break; /* return what we have */ @@ -617,7 +674,7 @@ char * ZEXPORT gzgets(gzFile file, char /* append a terminating zero to the string (we don't check for a zero in the contents, let the user worry about that) -- return the terminated string, or if nothing was read, NULL */ - if (buf == str) + if (buf == str && len > 1) return NULL; buf[0] = 0; return str; @@ -634,8 +691,10 @@ int ZEXPORT gzdirect(gzFile file) { /* if the state is not known, but we can find out, then do so (this is mainly for right after a gzopen() or gzdopen()) */ - if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0) + if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0) { (void)gz_look(state); + gz_skip_have(state); /* apply skip to current buffer */ + } /* return 1 if transparent, 0 if processing a gzip stream */ return state->direct == 1; Index: lib/libz/gzwrite.c =================================================================== RCS file: /cvs/src/lib/libz/gzwrite.c,v diff -u -p -r1.8 gzwrite.c --- lib/libz/gzwrite.c 6 Nov 2026 15:30:25 -0000 1.8 +++ lib/libz/gzwrite.c 17 Nov 2026 07:55:25 -0000 @@ -41,8 +41,8 @@ local int gz_init(gz_statep state) { gz_error(state, Z_MEM_ERROR, "out of memory"); return -1; } - strm->next_in = NULL; } + strm->next_in = NULL; /* mark state as initialized */ state->size = state->want; @@ -79,7 +79,7 @@ local int gz_comp(gz_statep state, int f put = strm->avail_in > max ? max : strm->avail_in; writ = (int)write(state->fd, strm->next_in, put); if (writ < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) + if (BLOCKED(errno)) state->again = 1; gz_error(state, Z_ERRNO, zstrerror()); return -1; @@ -114,7 +114,7 @@ local int gz_comp(gz_statep state, int f (unsigned)(strm->next_out - state->x.next); writ = (int)write(state->fd, state->x.next, put); if (writ < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) + if (BLOCKED(errno)) state->again = 1; gz_error(state, Z_ERRNO, zstrerror()); return -1; @@ -180,8 +180,14 @@ local int gz_zero(gz_statep state) { n -= strm->avail_in; state->x.pos += n; state->skip -= n; - if (ret == -1) + if (ret == -1) { + /* discard any unconsumed zero fill -- it was not yet accounted + for in state->skip, so it will be generated again when the + operation is retried */ + strm->avail_in = 0; + strm->next_in = state->in; return -1; + } } while (state->skip); return 0; } @@ -253,6 +259,7 @@ local z_size_t gz_write(gz_statep state, return state->again ? put - len : 0; } } while (len); + state->strm.next_in = state->in; } /* input was all buffered or compressed */ @@ -379,9 +386,10 @@ int ZEXPORT gzputs(gzFile file, const ch return len && put == 0 ? -1 : (int)put; } -#if (((!defined(STDC) && !defined(Z_HAVE_STDARG_H)) || !defined(NO_vsnprintf)) && \ - (defined(STDC) || defined(Z_HAVE_STDARG_H) || !defined(NO_snprintf))) || \ - defined(ZLIB_INSECURE) +#if (((!defined(STDC) && !defined(Z_HAVE_STDARG_H)) || \ + !defined(NO_vsnprintf)) && \ + (defined(STDC) || defined(Z_HAVE_STDARG_H) || \ + !defined(NO_snprintf))) || defined(ZLIB_INSECURE) /* If the second half of the input buffer is occupied, write out the contents. If there is any input remaining due to a non-blocking stall on write, move it to the start of the buffer. Return true if this did not open up the @@ -391,7 +399,8 @@ local int gz_vacate(gz_statep state) { z_streamp strm; strm = &(state->strm); - if (strm->next_in + strm->avail_in <= state->in + state->size) + if (strm->next_in == NULL || + strm->next_in + strm->avail_in <= state->in + state->size) return 0; (void)gz_comp(state, Z_NO_FLUSH); if (strm->avail_in == 0) { @@ -451,6 +460,7 @@ int ZEXPORTVA gzvprintf(gzFile file, con a Z_BUF_ERROR to let the application know that this gzprintf() needs to be retried. */ gz_error(state, Z_BUF_ERROR, "stalled write on gzprintf"); + return state->err; } if (!state->again) return state->err; @@ -486,6 +496,14 @@ int ZEXPORTVA gzvprintf(gzFile file, con /* write out buffer if more than half is occupied */ ret = gz_vacate(state); + if (ret) { + /* discard unwritten formatted output */ + strm->avail_in -= (unsigned)len; + state->x.pos -= len; + if (state->again) + gz_error(state, Z_BUF_ERROR, "stalled write on gzprintf"); + return state->err; + } if (state->err && !state->again) return state->err; return len; @@ -559,6 +577,7 @@ int ZEXPORTVA gzprintf(gzFile file, cons a Z_BUF_ERROR to let the application know that this gzprintf() needs to be retried. */ gz_error(state, Z_BUF_ERROR, "stalled write on gzprintf"); + return state->err; } if (!state->again) return state->err; @@ -599,6 +618,14 @@ int ZEXPORTVA gzprintf(gzFile file, cons /* write out buffer if more than half is occupied */ ret = gz_vacate(state); + if (ret) { + /* discard unwritten formatted output */ + strm->avail_in -= (unsigned)len; + state->x.pos -= len; + if (state->again) + gz_error(state, Z_BUF_ERROR, "stalled write on gzprintf"); + return state->err; + } if (state->err && !state->again) return state->err; return (int)len; Index: lib/libz/inflate.c =================================================================== RCS file: /cvs/src/lib/libz/inflate.c,v diff -u -p -r1.26 inflate.c --- lib/libz/inflate.c 6 Nov 2026 15:30:25 -0000 1.26 +++ lib/libz/inflate.c 17 Nov 2026 07:55:25 -0000 @@ -127,6 +127,14 @@ int ZEXPORT inflateReset(z_streamp strm) if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; + if (state->wbits != state->reqbits) { + if (state->window != Z_NULL) { + ZFREE(strm, state->window); + state->window = Z_NULL; + } + state->wbits = state->reqbits; + } + state->wrap = state->reqwrap; state->wsize = 0; state->whave = 0; state->wnext = 0; @@ -165,8 +173,8 @@ int ZEXPORT inflateReset2(z_streamp strm } /* update state and reset the rest of it */ - state->wrap = wrap; - state->wbits = (unsigned)windowBits; + state->reqwrap = state->wrap = wrap; + state->reqbits = state->wbits = (unsigned)windowBits; return inflateReset(strm); } @@ -724,6 +732,7 @@ int ZEXPORT inflate(z_streamp strm, int case DICT: if (state->havedict == 0) { RESTORE(); + strm->total_in += in - strm->avail_in; return Z_NEED_DICT; } strm->adler = state->check = adler32(0L, Z_NULL, 0); @@ -1277,7 +1286,8 @@ int ZEXPORT inflateSetDictionary(z_strea int ret; /* check state */ - if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + if (inflateStateCheck(strm) || dictionary == Z_NULL) + return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; if (state->wrap != 0 && state->mode != DICT) return Z_STREAM_ERROR; @@ -1382,15 +1392,15 @@ int ZEXPORT inflateSync(z_streamp strm) /* return no joy or set up to restart inflate() on a new block */ if (state->have != 4) return Z_DATA_ERROR; - if (state->flags == -1) - state->wrap = 0; /* if no header yet, treat as raw */ - else - state->wrap &= ~4; /* no point in computing a check value now */ flags = state->flags; in = strm->total_in; out = strm->total_out; inflateReset(strm); strm->total_in = in; strm->total_out = out; state->flags = flags; + if (flags == -1) + state->wrap = 0; /* if no header yet, treat as raw */ + else + state->wrap &= ~4; /* no point in computing a check value now */ if (state->wbits == 0) { /* we never got to see a header for the window size -- set to max */ if (state->window != Z_NULL) { @@ -1425,14 +1435,21 @@ int ZEXPORT inflateCopy(z_streamp dest, unsigned char FAR *window; /* check input */ - if (inflateStateCheck(source) || dest == Z_NULL) + if (dest == Z_NULL) + return Z_STREAM_ERROR; + if (inflateStateCheck(source)) { + dest->state = Z_NULL; return Z_STREAM_ERROR; + } state = (struct inflate_state FAR *)source->state; /* allocate space */ copy = (struct inflate_state FAR *) ZALLOC(source, 1, sizeof(struct inflate_state)); - if (copy == Z_NULL) return Z_MEM_ERROR; + if (copy == Z_NULL) { + dest->state = Z_NULL; + return Z_MEM_ERROR; + } zmemzero(copy, sizeof(struct inflate_state)); window = Z_NULL; if (state->window != Z_NULL) { @@ -1440,6 +1457,7 @@ int ZEXPORT inflateCopy(z_streamp dest, ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); if (window == Z_NULL) { ZFREE(source, copy); + dest->state = Z_NULL; return Z_MEM_ERROR; } } @@ -1485,6 +1503,10 @@ int ZEXPORT inflateValidate(z_streamp st state->wrap |= 4; else state->wrap &= ~4; + if (check && state->reqwrap) + state->reqwrap |= 4; + else + state->reqwrap &= ~4; return Z_OK; } Index: lib/libz/inflate.h =================================================================== RCS file: /cvs/src/lib/libz/inflate.h,v diff -u -p -r1.7 inflate.h --- lib/libz/inflate.h 27 Mar 2024 10:21:11 -0000 1.7 +++ lib/libz/inflate.h 17 Nov 2026 07:55:25 -0000 @@ -85,6 +85,7 @@ struct inflate_state { int last; /* true if processing last block */ int wrap; /* bit 0 true for zlib, bit 1 true for gzip, bit 2 true to validate check value */ + int reqwrap; /* requested wrap */ int havedict; /* true if dictionary provided */ int flags; /* gzip header method and flags, 0 if zlib, or -1 if raw or no header yet */ @@ -93,7 +94,8 @@ struct inflate_state { unsigned long total; /* protected copy of output count */ gz_headerp head; /* where to save gzip header information */ /* sliding window */ - unsigned wbits; /* log base 2 of requested window size */ + unsigned reqbits; /* requested log base 2 of window size */ + unsigned wbits; /* current log base 2 of window size */ unsigned wsize; /* window size or zero if not using window */ unsigned whave; /* valid bytes in the window */ unsigned wnext; /* window write index */ Index: lib/libz/inftrees.c =================================================================== RCS file: /cvs/src/lib/libz/inftrees.c,v diff -u -p -r1.18 inftrees.c --- lib/libz/inftrees.c 19 Mar 2026 15:10:55 -0000 1.18 +++ lib/libz/inftrees.c 17 Nov 2026 07:55:25 -0000 @@ -69,7 +69,7 @@ int ZLIB_INTERNAL inflate_table(codetype 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; static const unsigned short lext[31] = { /* Length codes 257..285 extra */ 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, - 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 199, 75}; + 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 71, 77}; static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, Index: lib/libz/inftrees.h =================================================================== RCS file: /cvs/src/lib/libz/inftrees.h,v diff -u -p -r1.12 inftrees.h --- lib/libz/inftrees.h 19 Mar 2026 15:10:55 -0000 1.12 +++ lib/libz/inftrees.h 17 Nov 2026 07:55:25 -0000 @@ -46,8 +46,14 @@ typedef struct { inflate_table() calls in inflate.c and infback.c. If the root table size is changed, then these maximum sizes would be need to be recalculated and updated. */ -#define ENOUGH_LENS 852 -#define ENOUGH_DISTS 592 +#ifndef PKZIP_BUG_WORKAROUND +# define ENOUGH_LENS 852 +# define ENOUGH_DISTS 592 +#else +/* That workaround permits 288 literal/length codes and 32 distance codes. */ +# define ENOUGH_LENS 854 +# define ENOUGH_DISTS 594 +#endif #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS) /* Type of code to build for inflate_table() */ Index: lib/libz/trees.c =================================================================== RCS file: /cvs/src/lib/libz/trees.c,v diff -u -p -r1.20 trees.c --- lib/libz/trees.c 19 Mar 2026 15:10:55 -0000 1.20 +++ lib/libz/trees.c 17 Nov 2026 07:55:25 -0000 @@ -225,7 +225,7 @@ local void gen_codes(ct_data *tree, int tree[n].Code = (ush)bi_reverse(next_code[len]++, len); Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", - n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len] - 1)); + n, (isgraph(n & 0xff) ? n : ' '), len, tree[n].Code, next_code[len] - 1)); } } Index: lib/libz/uncompr.c =================================================================== RCS file: /cvs/src/lib/libz/uncompr.c,v diff -u -p -r1.10 uncompr.c --- lib/libz/uncompr.c 19 Mar 2026 15:10:55 -0000 1.10 +++ lib/libz/uncompr.c 17 Nov 2026 07:55:25 -0000 @@ -31,8 +31,8 @@ int ZEXPORT uncompress2_z(Bytef *dest, z const uInt max = (uInt)-1; z_size_t len, left; - if (sourceLen == NULL || (*sourceLen > 0 && source == NULL) || - destLen == NULL || (*destLen > 0 && dest == NULL)) + if (sourceLen == Z_NULL || (*sourceLen > 0 && source == Z_NULL) || + destLen == Z_NULL || (*destLen > 0 && dest == Z_NULL)) return Z_STREAM_ERROR; len = *sourceLen; Index: lib/libz/zconf.h =================================================================== RCS file: /cvs/src/lib/libz/zconf.h,v diff -u -p -r1.23 zconf.h --- lib/libz/zconf.h 19 Mar 2026 15:10:55 -0000 1.23 +++ lib/libz/zconf.h 17 Nov 2026 07:55:25 -0000 @@ -497,6 +497,10 @@ typedef uLong FAR uLongf; #if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0 # define Z_LFS64 +#elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(__BORLANDC__) && \ + !defined(UNDER_CE) && (defined(__MINGW32__) || !defined(__GNUC__)) +# define Z_LFS64 /* native Windows has 64-bit file offsets */ +# define Z_WINLFS #endif #if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64) @@ -514,7 +518,7 @@ typedef uLong FAR uLongf; #endif #ifndef z_off_t -# define z_off_t long long +# define z_off_t long #endif #if !defined(_WIN32) && defined(Z_LARGE64) @@ -527,6 +531,11 @@ typedef uLong FAR uLongf; # define z_off64_t offset_t #else # define z_off64_t z_off_t +#endif + +#if defined(Z_WINLFS) && defined(Z_WANT64) && !defined(Z_SOLO) +# undef z_off_t +# define z_off_t z_off64_t #endif /* MVS linker does not support external names larger than 8 bytes */ Index: lib/libz/zlib.h =================================================================== RCS file: /cvs/src/lib/libz/zlib.h,v diff -u -p -r1.33 zlib.h --- lib/libz/zlib.h 23 Mar 2026 09:11:44 -0000 1.33 +++ lib/libz/zlib.h 17 Nov 2026 07:55:25 -0000 @@ -1,5 +1,5 @@ /* zlib.h -- interface of the 'zlib' general purpose compression library - version 1.3.2, February 17th, 2026 + version 1.4, November 4th, 2026 Copyright (C) 1995-2026 Jean-loup Gailly and Mark Adler @@ -41,11 +41,11 @@ extern "C" { #endif -#define ZLIB_VERSION "1.3.2" -#define ZLIB_VERNUM 0x1320 +#define ZLIB_VERSION "1.4" +#define ZLIB_VERNUM 0x1400 #define ZLIB_VER_MAJOR 1 -#define ZLIB_VER_MINOR 3 -#define ZLIB_VER_REVISION 2 +#define ZLIB_VER_MINOR 4 +#define ZLIB_VER_REVISION 0 #define ZLIB_VER_SUBREVISION 0 /* @@ -696,7 +696,9 @@ ZEXTERN int ZEXPORT deflateCopy(z_stream deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_STREAM_ERROR if the source stream state was inconsistent (such as zalloc being Z_NULL). msg is left unchanged in both source and - destination. + destination. If deflateCopy fails and dest is not Z_NULL, then dest->state + is set to Z_NULL with dest otherwise unchanged, so that deflateEnd(dest) can + be called safely, though dest is not a usable stream. */ ZEXTERN int ZEXPORT deflateReset(z_streamp strm); @@ -769,17 +771,17 @@ ZEXTERN uLong ZEXPORT deflateBound(z_str ZEXTERN z_size_t ZEXPORT deflateBound_z(z_streamp strm, z_size_t sourceLen); /* deflateBound() returns an upper bound on the compressed size after - deflation of sourceLen bytes. It must be called after deflateInit() or - deflateInit2(), and after deflateSetHeader(), if used. This would be used - to allocate an output buffer for deflation in a single pass, and so would be - called before deflate(). If that first deflate() call is provided the - sourceLen input bytes, an output buffer allocated to the size returned by - deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed - to return Z_STREAM_END. Note that it is possible for the compressed size to - be larger than the value returned by deflateBound() if flush options other - than Z_FINISH or Z_NO_FLUSH are used. + deflation of sourceLen bytes. It must be called after deflateInit() or + deflateInit2(), and after deflateSetHeader() or deflateSetDictionary(), if + used. This would be used to allocate an output buffer for deflation in a + single pass, and so would be called before deflate(). If that first + deflate() call is provided the sourceLen input bytes, an output buffer + allocated to the size returned by deflateBound(), and the flush value + Z_FINISH, then deflate() is guaranteed to return Z_STREAM_END. Note that it + is possible for the compressed size to be larger than the value returned by + deflateBound() if flush options other than Z_FINISH or Z_NO_FLUSH are used. - delfateBound_z() is the same, but takes and returns a size_t length. Note + deflateBound_z() is the same, but takes and returns a size_t length. Note that a long is 32 bits on Windows. */ @@ -789,7 +791,7 @@ ZEXTERN int ZEXPORT deflatePending(z_str /* deflatePending() returns the number of bytes and bits of output that have been generated, but not yet provided in the available output. The bytes not - provided would be due to the available output space having being consumed. + provided would be due to the available output space having been consumed. The number of bits of output not provided are between 0 and 7, where they await more bits to join them in order to fill out a full byte. If pending or bits are Z_NULL, then those values are not set. @@ -869,8 +871,7 @@ ZEXTERN int ZEXPORT inflateInit2(z_strea instead. windowBits must be greater than or equal to the windowBits value provided to deflateInit2() while compressing, or it must be equal to 15 if deflateInit2() was not used. If a compressed stream with a larger window - size is given as input, inflate() will return with the error code - Z_DATA_ERROR instead of trying to allocate a larger window. + size is given as input, inflate() may return with a Z_DATA_ERROR. windowBits can also be zero to request that inflate use the window size in the zlib header of the compressed stream. @@ -980,7 +981,9 @@ ZEXTERN int ZEXPORT inflateCopy(z_stream inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_STREAM_ERROR if the source stream state was inconsistent (such as zalloc being Z_NULL). msg is left unchanged in both source and - destination. + destination. If inflateCopy fails and dest is not Z_NULL, then dest->state + is set to Z_NULL with dest otherwise unchanged, so that inflateEnd(dest) can + be called safely, though dest is not a usable stream. */ ZEXTERN int ZEXPORT inflateReset(z_streamp strm); @@ -1095,9 +1098,10 @@ ZEXTERN int ZEXPORT inflateGetHeader(z_s a terminating zero. If any of extra, name, or comment are not present in the header, then that field's pointer is set to Z_NULL. This allows the use of deflateSetHeader() with the returned structure to duplicate the header. - Note that if those fields initially pointed to allocated memory, then the - application will need to save them elsewhere so that they can be eventually - freed. + Though take care that none of the fields were truncated before so using it, + lest havoc ensue. Note that if those fields initially pointed to allocated + memory, then the application will need to save them elsewhere so that they + can be eventually freed. If inflateGetHeader is not used, then the header information is simply discarded. The header is always checked for validity, including the header @@ -1394,8 +1398,8 @@ ZEXTERN gzFile ZEXPORT gzopen(const char errno can be checked to determine if the reason gzopen failed was that the file could not be opened. Note that if 'N' is in mode for non-blocking, the open() itself can fail in order to not block. In that case gzopen() will - return NULL and errno will be EAGAIN or EWOULDBLOCK. The call to gzopen() can - then be re-tried. If the application would like to block on opening the + return NULL and errno will be EAGAIN or EWOULDBLOCK. The call to gzopen() + can then be re-tried. If the application would like to block on opening the file, then it can use open() without O_NONBLOCK, and then gzdopen() with the resulting file descriptor and 'N' in the mode, which will set it to non- blocking. @@ -1501,9 +1505,12 @@ ZEXTERN z_size_t ZEXPORT gzfread(voidp b gzfread() returns the number of full items read of size size, or zero if the end of the file was reached and a full item could not be read, or if there was an error. gzerror() must be consulted if zero is returned in - order to determine if there was an error. If the multiplication of size and - nitems overflows, i.e. the product does not fit in a z_size_t, then nothing - is read, zero is returned, and the error state is set to Z_STREAM_ERROR. + order to determine if there was an error. If some data was read before an + error, then that data is returned until exhausted, after which the next call + will return zero and gzerror() will report the error. If the multiplication + of size and nitems overflows, i.e. the product does not fit in a z_size_t, + then nothing is read, zero is returned, and the error state is set to + Z_STREAM_ERROR. In the event that the end of file is reached and only a partial item is available at the end, i.e. the remaining uncompressed data length is not a @@ -1569,7 +1576,7 @@ ZEXTERN int ZEXPORTVA gzprintf(); these possibilities can be determined using zlibCompileFlags(). If a Z_BUF_ERROR is returned, then nothing was written due to a stall on - the non-blocking write destination. + the non-blocking write destination. The operation can be retried. */ ZEXTERN int ZEXPORT gzputs(gzFile file, const char *s); @@ -1677,7 +1684,9 @@ ZEXTERN z_off_t ZEXPORT gzseek(gzFile fi gzseek returns the resulting offset location as measured in bytes from the beginning of the uncompressed stream, or -1 in case of error, in particular if the file is opened for writing and the new starting position - would be before the current position. + would be before the current position, or if the resulting position would + not be representable in the offset type. If gzseek returns -1, the stream + position is unchanged. */ ZEXTERN int ZEXPORT gzrewind(gzFile file); @@ -1778,7 +1787,8 @@ ZEXTERN const char * ZEXPORT gzerror(gzF If errnum is not NULL, *errnum is set to zlib error number. If an error occurred in the file system and not in the compression library, *errnum is set to Z_ERRNO and the application may consult errno to get the exact error - code. + code. The error is reported by gzerror() as soon as it is detected, which + may be before all of the data read prior to the error has been returned. The application must not modify the returned string. Future calls to this function may invalidate the previously returned string. If file is @@ -1972,7 +1982,8 @@ ZEXTERN int ZEXPORT gzgetc_(gzFile file) * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if * both are true, the application gets the *64 functions, and the regular * functions are changed to 64 bits) -- in case these are set on systems - * without large file support, _LFS64_LARGEFILE must also be true + * without large file support, _LFS64_LARGEFILE must also be true, except on + * native Windows, where 64-bit offsets are always available */ #ifdef Z_LARGE64 ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); @@ -2029,7 +2040,7 @@ ZEXTERN int ZEXPORT gzgetc_(gzFile file) #endif /* !Z_SOLO */ -/* undocumented functions */ +/* undocumented functions (not part of the zlib API) */ ZEXTERN const char * ZEXPORT zError(int); ZEXTERN int ZEXPORT inflateSyncPoint(z_streamp); ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table(void); Index: lib/libz/zutil.c =================================================================== RCS file: /cvs/src/lib/libz/zutil.c,v diff -u -p -r1.15 zutil.c --- lib/libz/zutil.c 19 Mar 2026 15:10:55 -0000 1.15 +++ lib/libz/zutil.c 17 Nov 2026 07:55:25 -0000 @@ -169,7 +169,6 @@ int ZLIB_INTERNAL zmemcmp(const void FAR void ZLIB_INTERNAL zmemzero(void FAR *b, z_size_t len) { uchf *p = b; - if (len == 0) return; while (len) { *p++ = 0; len--; @@ -226,7 +225,11 @@ voidpf ZLIB_INTERNAL zcalloc(voidpf opaq } else { buf = farmalloc(bsize + 16L); } - if (buf == NULL || next_ptr >= MAX_PTR) return NULL; + if (buf == NULL) return NULL; + if (next_ptr >= MAX_PTR) { + farfree(buf); + return NULL; + } table[next_ptr].org_ptr = buf; /* Normalize the pointer to seg:0 */ Index: sys/lib/libz/compress.c =================================================================== RCS file: /cvs/src/sys/lib/libz/compress.c,v diff -u -p -r1.6 compress.c --- sys/lib/libz/compress.c 19 Mar 2026 15:14:51 -0000 1.6 +++ sys/lib/libz/compress.c 17 Nov 2026 07:55:25 -0000 @@ -26,8 +26,8 @@ int ZEXPORT compress2_z(Bytef *dest, z_s const uInt max = (uInt)-1; z_size_t left; - if ((sourceLen > 0 && source == NULL) || - destLen == NULL || (*destLen > 0 && dest == NULL)) + if ((sourceLen > 0 && source == Z_NULL) || + destLen == Z_NULL || (*destLen > 0 && dest == Z_NULL)) return Z_STREAM_ERROR; left = *destLen; Index: sys/lib/libz/crc32.c =================================================================== RCS file: /cvs/src/sys/lib/libz/crc32.c,v diff -u -p -r1.21 crc32.c --- sys/lib/libz/crc32.c 19 Mar 2026 15:14:51 -0000 1.21 +++ sys/lib/libz/crc32.c 17 Nov 2026 07:55:25 -0000 @@ -684,7 +684,7 @@ uLong ZEXPORT crc32_z(uLong crc, const u #endif /* Initialize the CRC for each braid. */ - crc0 = crc; + crc0 = (z_crc_t)crc; #if N > 1 crc1 = 0; #if N > 2 Index: sys/lib/libz/deflate.c =================================================================== RCS file: /cvs/src/sys/lib/libz/deflate.c,v diff -u -p -r1.25 deflate.c --- sys/lib/libz/deflate.c 6 Nov 2026 15:30:25 -0000 1.25 +++ sys/lib/libz/deflate.c 17 Nov 2026 07:55:25 -0000 @@ -441,7 +441,6 @@ int ZEXPORT deflateInit2_(z_streamp strm s->status = INIT_STATE; /* to pass state test in deflateReset() */ s->wrap = wrap; - s->gzhead = Z_NULL; s->w_bits = (uInt)windowBits; s->w_size = 1 << s->w_bits; s->w_mask = s->w_size - 1; @@ -564,7 +563,8 @@ int ZEXPORT deflateSetDictionary(z_strea return Z_STREAM_ERROR; s = strm->state; wrap = s->wrap; - if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) + if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead || + (wrap == 0 && (long)s->strstart != s->block_start)) return Z_STREAM_ERROR; /* when using zlib wrappers, compute Adler-32 for provided dictionary */ @@ -667,9 +667,11 @@ int ZEXPORT deflateResetKeep(z_streamp s adler32(0L, Z_NULL, 0); s->last_flush = -2; + s->gzhead = Z_NULL; + { /* Retain the hash work done for level 0. */ - int matches = s->level == 0 ? s->matches : 0; + uInt matches = s->level == 0 ? s->matches : 0; _tr_init(s); s->matches = matches; } @@ -1329,39 +1331,49 @@ int ZEXPORT deflateEnd(z_streamp strm) { */ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) { #ifdef MAXSEG_64K - (void)dest; (void)source; + if (dest != Z_NULL) + dest->state = Z_NULL; return Z_STREAM_ERROR; #else deflate_state *ds; deflate_state *ss; - - if (deflateStateCheck(source) || dest == Z_NULL) { + if (dest == Z_NULL) + return Z_STREAM_ERROR; + if (deflateStateCheck(source)) { + dest->state = Z_NULL; return Z_STREAM_ERROR; } ss = source->state; - zmemcpy(dest, source, sizeof(z_stream)); - - ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); - if (ds == Z_NULL) return Z_MEM_ERROR; - zmemzero(ds, sizeof(deflate_state)); - dest->state = (struct internal_state FAR *) ds; + ds = (deflate_state *) ZALLOC(source, 1, sizeof(deflate_state)); + if (ds == Z_NULL) { + dest->state = Z_NULL; + return Z_MEM_ERROR; + } zmemcpy(ds, ss, sizeof(deflate_state)); - ds->strm = dest; - - ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); - ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); - ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); - ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, LIT_BUFS); - + ds->window = (Bytef *) ZALLOC(source, ds->w_size, 2*sizeof(Byte)); + ds->prev = (Posf *) ZALLOC(source, ds->w_size, sizeof(Pos)); + ds->head = (Posf *) ZALLOC(source, ds->hash_size, sizeof(Pos)); + ds->pending_buf = (uchf *) ZALLOC(source, ds->lit_bufsize, LIT_BUFS); if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || ds->pending_buf == Z_NULL) { - deflateEnd (dest); + TRY_FREE(source, ds->pending_buf, ds->lit_bufsize * LIT_BUFS); + TRY_FREE(source, ds->head, ds->hash_size * sizeof(Pos)); + TRY_FREE(source, ds->prev, ds->w_size * sizeof(Pos)); + TRY_FREE(source, ds->window, ds->w_size * 2 * sizeof(Byte)); + ZFREE(source, ds, sizeof(deflate_state)); + dest->state = Z_NULL; return Z_MEM_ERROR; } + + /* copy the stream and attach the new state */ + zmemcpy(dest, source, sizeof(z_stream)); + dest->state = (struct internal_state FAR *) ds; + ds->strm = dest; + /* following zmemcpy's do not work for 16-bit MSDOS */ zmemcpy(ds->window, ss->window, ss->high_water); zmemcpy(ds->prev, ss->prev, @@ -1654,7 +1666,10 @@ local void check_match(deflate_state *s, /* Same but force premature exit if necessary. */ #define FLUSH_BLOCK(s, last) { \ FLUSH_BLOCK_ONLY(s, last); \ - if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ + if (s->strm->avail_out == 0) { \ + if (!(last)) return need_more; \ + if (s->pending != 0 || s->wrap != 0) return finish_started; \ + } \ } /* Maximum stored block length in deflate format (not including header). */ @@ -1691,7 +1706,7 @@ local block_state deflate_stored(deflate * possible. If flushing, copy the remaining available input to next_out as * stored blocks, if there is enough space. */ - int last = 0; + int last = 0, all; unsigned len, left, have; unsigned used = s->strm->avail_in; do { @@ -1706,11 +1721,14 @@ local block_state deflate_stored(deflate /* maximum stored block length that will fit in avail_out: */ have = s->strm->avail_out - have; left = (unsigned)(s->strstart - s->block_start); /* window bytes */ - if (len > (ulg)left + s->strm->avail_in) + if (left < len && s->strm->avail_in < len - left) len = left + s->strm->avail_in; /* limit len to the input */ if (len > have) len = have; /* limit len to the output */ + /* all is true if len is all of the available input */ + all = len >= left && len - left == s->strm->avail_in; + /* If the stored block would be less than min_block in length, or if * unable to copy all of the available input when flushing, then try * copying to the window and the pending buffer instead. Also don't @@ -1718,13 +1736,13 @@ local block_state deflate_stored(deflate */ if (len < min_block && ((len == 0 && flush != Z_FINISH) || flush == Z_NO_FLUSH || - len != left + s->strm->avail_in)) + !all)) break; /* Make a dummy stored block in pending to get the header bytes, * including any pending bits. This also updates the debugging counts. */ - last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; + last = flush == Z_FINISH && all ? 1 : 0; _tr_stored_block(s, (char *)0, 0L, last); /* Replace the lengths in the dummy stored block with len. */ @@ -2029,7 +2047,8 @@ local block_state deflate_slow(deflate_s uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; /* Do not insert strings in hash table beyond this. */ - check_match(s, s->strstart - 1, s->prev_match, (int)s->prev_length); + check_match(s, s->strstart - 1, s->prev_match, + (int)s->prev_length); _tr_tally_dist(s, s->strstart - 1 - s->prev_match, s->prev_length - MIN_MATCH, bflush); Index: sys/lib/libz/inflate.c =================================================================== RCS file: /cvs/src/sys/lib/libz/inflate.c,v diff -u -p -r1.30 inflate.c --- sys/lib/libz/inflate.c 6 Nov 2026 15:30:25 -0000 1.30 +++ sys/lib/libz/inflate.c 17 Nov 2026 07:55:25 -0000 @@ -127,6 +127,14 @@ int ZEXPORT inflateReset(z_streamp strm) if (inflateStateCheck(strm)) return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; + if (state->wbits != state->reqbits) { + if (state->window != Z_NULL) { + ZFREE(strm, state->window, 1U << state->wbits); + state->window = Z_NULL; + } + state->wbits = state->reqbits; + } + state->wrap = state->reqwrap; state->wsize = 0; state->whave = 0; state->wnext = 0; @@ -165,8 +173,8 @@ int ZEXPORT inflateReset2(z_streamp strm } /* update state and reset the rest of it */ - state->wrap = wrap; - state->wbits = (unsigned)windowBits; + state->reqwrap = state->wrap = wrap; + state->reqbits = state->wbits = (unsigned)windowBits; return inflateReset(strm); } @@ -724,6 +732,7 @@ int ZEXPORT inflate(z_streamp strm, int case DICT: if (state->havedict == 0) { RESTORE(); + strm->total_in += in - strm->avail_in; return Z_NEED_DICT; } strm->adler = state->check = adler32(0L, Z_NULL, 0); @@ -1277,7 +1286,8 @@ int ZEXPORT inflateSetDictionary(z_strea int ret; /* check state */ - if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + if (inflateStateCheck(strm) || dictionary == Z_NULL) + return Z_STREAM_ERROR; state = (struct inflate_state FAR *)strm->state; if (state->wrap != 0 && state->mode != DICT) return Z_STREAM_ERROR; @@ -1382,15 +1392,15 @@ int ZEXPORT inflateSync(z_streamp strm) /* return no joy or set up to restart inflate() on a new block */ if (state->have != 4) return Z_DATA_ERROR; - if (state->flags == -1) - state->wrap = 0; /* if no header yet, treat as raw */ - else - state->wrap &= ~4; /* no point in computing a check value now */ flags = state->flags; in = strm->total_in; out = strm->total_out; inflateReset(strm); strm->total_in = in; strm->total_out = out; state->flags = flags; + if (flags == -1) + state->wrap = 0; /* if no header yet, treat as raw */ + else + state->wrap &= ~4; /* no point in computing a check value now */ if (state->wbits == 0) { /* we never got to see a header for the window size -- set to max */ if (state->window != Z_NULL) { @@ -1425,14 +1435,21 @@ int ZEXPORT inflateCopy(z_streamp dest, unsigned char FAR *window; /* check input */ - if (inflateStateCheck(source) || dest == Z_NULL) + if (dest == Z_NULL) + return Z_STREAM_ERROR; + if (inflateStateCheck(source)) { + dest->state = Z_NULL; return Z_STREAM_ERROR; + } state = (struct inflate_state FAR *)source->state; /* allocate space */ copy = (struct inflate_state FAR *) ZALLOC(source, 1, sizeof(struct inflate_state)); - if (copy == Z_NULL) return Z_MEM_ERROR; + if (copy == Z_NULL) { + dest->state = Z_NULL; + return Z_MEM_ERROR; + } zmemzero(copy, sizeof(struct inflate_state)); window = Z_NULL; if (state->window != Z_NULL) { @@ -1440,6 +1457,7 @@ int ZEXPORT inflateCopy(z_streamp dest, ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); if (window == Z_NULL) { ZFREE(source, copy, sizeof(struct inflate_state)); + dest->state = Z_NULL; return Z_MEM_ERROR; } } @@ -1485,6 +1503,10 @@ int ZEXPORT inflateValidate(z_streamp st state->wrap |= 4; else state->wrap &= ~4; + if (check && state->reqwrap) + state->reqwrap |= 4; + else + state->reqwrap &= ~4; return Z_OK; } Index: sys/lib/libz/inflate.h =================================================================== RCS file: /cvs/src/sys/lib/libz/inflate.h,v diff -u -p -r1.7 inflate.h --- sys/lib/libz/inflate.h 27 Mar 2024 10:21:36 -0000 1.7 +++ sys/lib/libz/inflate.h 17 Nov 2026 07:55:25 -0000 @@ -85,6 +85,7 @@ struct inflate_state { int last; /* true if processing last block */ int wrap; /* bit 0 true for zlib, bit 1 true for gzip, bit 2 true to validate check value */ + int reqwrap; /* requested wrap */ int havedict; /* true if dictionary provided */ int flags; /* gzip header method and flags, 0 if zlib, or -1 if raw or no header yet */ @@ -93,7 +94,8 @@ struct inflate_state { unsigned long total; /* protected copy of output count */ gz_headerp head; /* where to save gzip header information */ /* sliding window */ - unsigned wbits; /* log base 2 of requested window size */ + unsigned reqbits; /* requested log base 2 of window size */ + unsigned wbits; /* current log base 2 of window size */ unsigned wsize; /* window size or zero if not using window */ unsigned whave; /* valid bytes in the window */ unsigned wnext; /* window write index */ Index: sys/lib/libz/inftrees.c =================================================================== RCS file: /cvs/src/sys/lib/libz/inftrees.c,v diff -u -p -r1.24 inftrees.c --- sys/lib/libz/inftrees.c 19 Mar 2026 15:14:51 -0000 1.24 +++ sys/lib/libz/inftrees.c 17 Nov 2026 07:55:25 -0000 @@ -69,7 +69,7 @@ int ZLIB_INTERNAL inflate_table(codetype 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; static const unsigned short lext[31] = { /* Length codes 257..285 extra */ 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, - 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 199, 75}; + 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 71, 77}; static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, Index: sys/lib/libz/inftrees.h =================================================================== RCS file: /cvs/src/sys/lib/libz/inftrees.h,v diff -u -p -r1.15 inftrees.h --- sys/lib/libz/inftrees.h 19 Mar 2026 15:14:51 -0000 1.15 +++ sys/lib/libz/inftrees.h 17 Nov 2026 07:55:25 -0000 @@ -46,8 +46,14 @@ typedef struct { inflate_table() calls in inflate.c and infback.c. If the root table size is changed, then these maximum sizes would be need to be recalculated and updated. */ -#define ENOUGH_LENS 852 -#define ENOUGH_DISTS 592 +#ifndef PKZIP_BUG_WORKAROUND +# define ENOUGH_LENS 852 +# define ENOUGH_DISTS 592 +#else +/* That workaround permits 288 literal/length codes and 32 distance codes. */ +# define ENOUGH_LENS 854 +# define ENOUGH_DISTS 594 +#endif #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS) /* Type of code to build for inflate_table() */ Index: sys/lib/libz/trees.c =================================================================== RCS file: /cvs/src/sys/lib/libz/trees.c,v diff -u -p -r1.15 trees.c --- sys/lib/libz/trees.c 19 Mar 2026 15:14:51 -0000 1.15 +++ sys/lib/libz/trees.c 17 Nov 2026 07:55:25 -0000 @@ -225,7 +225,7 @@ local void gen_codes(ct_data *tree, int tree[n].Code = (ush)bi_reverse(next_code[len]++, len); Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ", - n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len] - 1)); + n, (isgraph(n & 0xff) ? n : ' '), len, tree[n].Code, next_code[len] - 1)); } } Index: sys/lib/libz/zconf.h =================================================================== RCS file: /cvs/src/sys/lib/libz/zconf.h,v diff -u -p -r1.28 zconf.h --- sys/lib/libz/zconf.h 19 Mar 2026 15:14:51 -0000 1.28 +++ sys/lib/libz/zconf.h 17 Nov 2026 07:55:25 -0000 @@ -505,6 +505,10 @@ typedef uLong FAR uLongf; #if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0 # define Z_LFS64 +#elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(__BORLANDC__) && \ + !defined(UNDER_CE) && (defined(__MINGW32__) || !defined(__GNUC__)) +# define Z_LFS64 /* native Windows has 64-bit file offsets */ +# define Z_WINLFS #endif #if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64) @@ -522,7 +526,7 @@ typedef uLong FAR uLongf; #endif #ifndef z_off_t -# define z_off_t long long +# define z_off_t long #endif #if !defined(_WIN32) && defined(Z_LARGE64) @@ -535,6 +539,11 @@ typedef uLong FAR uLongf; # define z_off64_t offset_t #else # define z_off64_t z_off_t +#endif + +#if defined(Z_WINLFS) && defined(Z_WANT64) && !defined(Z_SOLO) +# undef z_off_t +# define z_off_t z_off64_t #endif /* MVS linker does not support external names larger than 8 bytes */ Index: sys/lib/libz/zlib.h =================================================================== RCS file: /cvs/src/sys/lib/libz/zlib.h,v diff -u -p -r1.35 zlib.h --- sys/lib/libz/zlib.h 23 Mar 2026 09:11:44 -0000 1.35 +++ sys/lib/libz/zlib.h 17 Nov 2026 07:55:25 -0000 @@ -1,5 +1,5 @@ /* zlib.h -- interface of the 'zlib' general purpose compression library - version 1.3.2, February 17th, 2026 + version 1.4, November 4th, 2026 Copyright (C) 1995-2026 Jean-loup Gailly and Mark Adler @@ -41,11 +41,11 @@ extern "C" { #endif -#define ZLIB_VERSION "1.3.2" -#define ZLIB_VERNUM 0x1320 +#define ZLIB_VERSION "1.4" +#define ZLIB_VERNUM 0x1400 #define ZLIB_VER_MAJOR 1 -#define ZLIB_VER_MINOR 3 -#define ZLIB_VER_REVISION 2 +#define ZLIB_VER_MINOR 4 +#define ZLIB_VER_REVISION 0 #define ZLIB_VER_SUBREVISION 0 /* @@ -696,7 +696,9 @@ ZEXTERN int ZEXPORT deflateCopy(z_stream deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_STREAM_ERROR if the source stream state was inconsistent (such as zalloc being Z_NULL). msg is left unchanged in both source and - destination. + destination. If deflateCopy fails and dest is not Z_NULL, then dest->state + is set to Z_NULL with dest otherwise unchanged, so that deflateEnd(dest) can + be called safely, though dest is not a usable stream. */ ZEXTERN int ZEXPORT deflateReset(z_streamp strm); @@ -769,17 +771,17 @@ ZEXTERN uLong ZEXPORT deflateBound(z_str ZEXTERN z_size_t ZEXPORT deflateBound_z(z_streamp strm, z_size_t sourceLen); /* deflateBound() returns an upper bound on the compressed size after - deflation of sourceLen bytes. It must be called after deflateInit() or - deflateInit2(), and after deflateSetHeader(), if used. This would be used - to allocate an output buffer for deflation in a single pass, and so would be - called before deflate(). If that first deflate() call is provided the - sourceLen input bytes, an output buffer allocated to the size returned by - deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed - to return Z_STREAM_END. Note that it is possible for the compressed size to - be larger than the value returned by deflateBound() if flush options other - than Z_FINISH or Z_NO_FLUSH are used. + deflation of sourceLen bytes. It must be called after deflateInit() or + deflateInit2(), and after deflateSetHeader() or deflateSetDictionary(), if + used. This would be used to allocate an output buffer for deflation in a + single pass, and so would be called before deflate(). If that first + deflate() call is provided the sourceLen input bytes, an output buffer + allocated to the size returned by deflateBound(), and the flush value + Z_FINISH, then deflate() is guaranteed to return Z_STREAM_END. Note that it + is possible for the compressed size to be larger than the value returned by + deflateBound() if flush options other than Z_FINISH or Z_NO_FLUSH are used. - delfateBound_z() is the same, but takes and returns a size_t length. Note + deflateBound_z() is the same, but takes and returns a size_t length. Note that a long is 32 bits on Windows. */ @@ -789,7 +791,7 @@ ZEXTERN int ZEXPORT deflatePending(z_str /* deflatePending() returns the number of bytes and bits of output that have been generated, but not yet provided in the available output. The bytes not - provided would be due to the available output space having being consumed. + provided would be due to the available output space having been consumed. The number of bits of output not provided are between 0 and 7, where they await more bits to join them in order to fill out a full byte. If pending or bits are Z_NULL, then those values are not set. @@ -869,8 +871,7 @@ ZEXTERN int ZEXPORT inflateInit2(z_strea instead. windowBits must be greater than or equal to the windowBits value provided to deflateInit2() while compressing, or it must be equal to 15 if deflateInit2() was not used. If a compressed stream with a larger window - size is given as input, inflate() will return with the error code - Z_DATA_ERROR instead of trying to allocate a larger window. + size is given as input, inflate() may return with a Z_DATA_ERROR. windowBits can also be zero to request that inflate use the window size in the zlib header of the compressed stream. @@ -980,7 +981,9 @@ ZEXTERN int ZEXPORT inflateCopy(z_stream inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_STREAM_ERROR if the source stream state was inconsistent (such as zalloc being Z_NULL). msg is left unchanged in both source and - destination. + destination. If inflateCopy fails and dest is not Z_NULL, then dest->state + is set to Z_NULL with dest otherwise unchanged, so that inflateEnd(dest) can + be called safely, though dest is not a usable stream. */ ZEXTERN int ZEXPORT inflateReset(z_streamp strm); @@ -1095,9 +1098,10 @@ ZEXTERN int ZEXPORT inflateGetHeader(z_s a terminating zero. If any of extra, name, or comment are not present in the header, then that field's pointer is set to Z_NULL. This allows the use of deflateSetHeader() with the returned structure to duplicate the header. - Note that if those fields initially pointed to allocated memory, then the - application will need to save them elsewhere so that they can be eventually - freed. + Though take care that none of the fields were truncated before so using it, + lest havoc ensue. Note that if those fields initially pointed to allocated + memory, then the application will need to save them elsewhere so that they + can be eventually freed. If inflateGetHeader is not used, then the header information is simply discarded. The header is always checked for validity, including the header @@ -1394,8 +1398,8 @@ ZEXTERN gzFile ZEXPORT gzopen(const char errno can be checked to determine if the reason gzopen failed was that the file could not be opened. Note that if 'N' is in mode for non-blocking, the open() itself can fail in order to not block. In that case gzopen() will - return NULL and errno will be EAGAIN or EWOULDBLOCK. The call to gzopen() can - then be re-tried. If the application would like to block on opening the + return NULL and errno will be EAGAIN or EWOULDBLOCK. The call to gzopen() + can then be re-tried. If the application would like to block on opening the file, then it can use open() without O_NONBLOCK, and then gzdopen() with the resulting file descriptor and 'N' in the mode, which will set it to non- blocking. @@ -1501,9 +1505,12 @@ ZEXTERN z_size_t ZEXPORT gzfread(voidp b gzfread() returns the number of full items read of size size, or zero if the end of the file was reached and a full item could not be read, or if there was an error. gzerror() must be consulted if zero is returned in - order to determine if there was an error. If the multiplication of size and - nitems overflows, i.e. the product does not fit in a z_size_t, then nothing - is read, zero is returned, and the error state is set to Z_STREAM_ERROR. + order to determine if there was an error. If some data was read before an + error, then that data is returned until exhausted, after which the next call + will return zero and gzerror() will report the error. If the multiplication + of size and nitems overflows, i.e. the product does not fit in a z_size_t, + then nothing is read, zero is returned, and the error state is set to + Z_STREAM_ERROR. In the event that the end of file is reached and only a partial item is available at the end, i.e. the remaining uncompressed data length is not a @@ -1569,7 +1576,7 @@ ZEXTERN int ZEXPORTVA gzprintf(); these possibilities can be determined using zlibCompileFlags(). If a Z_BUF_ERROR is returned, then nothing was written due to a stall on - the non-blocking write destination. + the non-blocking write destination. The operation can be retried. */ ZEXTERN int ZEXPORT gzputs(gzFile file, const char *s); @@ -1677,7 +1684,9 @@ ZEXTERN z_off_t ZEXPORT gzseek(gzFile fi gzseek returns the resulting offset location as measured in bytes from the beginning of the uncompressed stream, or -1 in case of error, in particular if the file is opened for writing and the new starting position - would be before the current position. + would be before the current position, or if the resulting position would + not be representable in the offset type. If gzseek returns -1, the stream + position is unchanged. */ ZEXTERN int ZEXPORT gzrewind(gzFile file); @@ -1778,7 +1787,8 @@ ZEXTERN const char * ZEXPORT gzerror(gzF If errnum is not NULL, *errnum is set to zlib error number. If an error occurred in the file system and not in the compression library, *errnum is set to Z_ERRNO and the application may consult errno to get the exact error - code. + code. The error is reported by gzerror() as soon as it is detected, which + may be before all of the data read prior to the error has been returned. The application must not modify the returned string. Future calls to this function may invalidate the previously returned string. If file is @@ -1972,7 +1982,8 @@ ZEXTERN int ZEXPORT gzgetc_(gzFile file) * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if * both are true, the application gets the *64 functions, and the regular * functions are changed to 64 bits) -- in case these are set on systems - * without large file support, _LFS64_LARGEFILE must also be true + * without large file support, _LFS64_LARGEFILE must also be true, except on + * native Windows, where 64-bit offsets are always available */ #ifdef Z_LARGE64 ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); @@ -2029,7 +2040,7 @@ ZEXTERN int ZEXPORT gzgetc_(gzFile file) #endif /* !Z_SOLO */ -/* undocumented functions */ +/* undocumented functions (not part of the zlib API) */ ZEXTERN const char * ZEXPORT zError(int); ZEXTERN int ZEXPORT inflateSyncPoint(z_streamp); ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table(void); Index: sys/lib/libz/zutil.c =================================================================== RCS file: /cvs/src/sys/lib/libz/zutil.c,v diff -u -p -r1.9 zutil.c --- sys/lib/libz/zutil.c 9 Oct 2026 16:27:55 -0000 1.9 +++ sys/lib/libz/zutil.c 17 Nov 2026 07:55:25 -0000 @@ -174,7 +174,6 @@ int ZLIB_INTERNAL zmemcmp(const void FAR void ZLIB_INTERNAL zmemzero(void FAR *b, z_size_t len) { uchf *p = b; - if (len == 0) return; while (len) { *p++ = 0; len--; @@ -231,7 +230,11 @@ voidpf ZLIB_INTERNAL zcalloc(voidpf opaq } else { buf = farmalloc(bsize + 16L); } - if (buf == NULL || next_ptr >= MAX_PTR) return NULL; + if (buf == NULL) return NULL; + if (next_ptr >= MAX_PTR) { + farfree(buf); + return NULL; + } table[next_ptr].org_ptr = buf; /* Normalize the pointer to seg:0 */