From a8bc7ac119273ed88696da89287b3d31a311d1bd Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Fri, 11 Oct 2024 07:04:02 -0700 Subject: [PATCH] Import some Chromium Zlib changes --- third_party/zlib/crc_folding.c | 2 +- third_party/zlib/deflate.c | 20 +++++++++++++++----- third_party/zlib/inflate.c | 9 ++++++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/third_party/zlib/crc_folding.c b/third_party/zlib/crc_folding.c index 2702f909a..cc2fa875a 100644 --- a/third_party/zlib/crc_folding.c +++ b/third_party/zlib/crc_folding.c @@ -406,7 +406,7 @@ partial: } #endif - _mm_storeu_si128((__m128i *)dst, xmm_crc_part); + memcpy(dst, src, len); /* TODO: Possibly generate more efficient code. */ partial_fold(s, len, &xmm_crc0, &xmm_crc1, &xmm_crc2, &xmm_crc3, &xmm_crc_part); done: diff --git a/third_party/zlib/deflate.c b/third_party/zlib/deflate.c index 58f9474e7..64ebcddc4 100644 --- a/third_party/zlib/deflate.c +++ b/third_party/zlib/deflate.c @@ -229,6 +229,8 @@ int ZEXPORT deflateInit(strm, level) /* To do: ignore strm->next_in if we use it as window */ } +#define WINDOW_PADDING 8 + /* ========================================================================= */ int ZEXPORT deflateInit2(strm, level, method, windowBits, memLevel, strategy) z_streamp strm; @@ -238,7 +240,6 @@ int ZEXPORT deflateInit2(strm, level, method, windowBits, memLevel, strategy) int memLevel; int strategy; { - unsigned window_padding = 8; deflate_state *s; int wrap = 1; @@ -325,12 +326,12 @@ int ZEXPORT deflateInit2(strm, level, method, windowBits, memLevel, strategy) s->hash_shift = ((s->hash_bits + MIN_MATCH-1) / MIN_MATCH); s->window = (Bytef *) ZALLOC(strm, - s->w_size + window_padding, + s->w_size + WINDOW_PADDING, 2*sizeof(Byte)); /* Avoid use of unitialized values in the window, see crbug.com/1137613 and * crbug.com/1144420 */ if (s->window) { /* [jart] fix regression in malloc failure checking */ - zmemzero(s->window, (s->w_size + window_padding) * (2 * sizeof(Byte))); + zmemzero(s->window, (s->w_size + WINDOW_PADDING) * (2 * sizeof(Byte))); } s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); /* Avoid use of uninitialized value, see: @@ -770,6 +771,12 @@ uLong ZEXPORT deflateBound(strm, sourceLen) wraplen = 6; } + /* With Chromium's hashing, s->hash_bits may not correspond to the + memLevel, making the computations below incorrect. Return the + conservative bound. */ + if (s->chromium_zlib_hash) + return (fixedlen > storelen ? fixedlen : storelen) + wraplen; + /* if not default parameters, return one of the conservative bounds */ if (s->w_bits != 15 || s->hash_bits != 8 + 7) return (s->w_bits <= s->hash_bits ? fixedlen : storelen) + wraplen; @@ -1199,7 +1206,9 @@ int ZEXPORT deflateCopy(dest, source) zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); ds->strm = dest; - ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); + ds->window = (Bytef *) ZALLOC(dest, + ds->w_size + WINDOW_PADDING, + 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, 4); @@ -1210,7 +1219,8 @@ int ZEXPORT deflateCopy(dest, source) return Z_MEM_ERROR; } /* following zmemcpy do not work for 16-bit MSDOS */ - zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); + zmemcpy(ds->window, ss->window, + (ds->w_size + WINDOW_PADDING) * 2 * sizeof(Byte)); zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); diff --git a/third_party/zlib/inflate.c b/third_party/zlib/inflate.c index 75fa6b56e..acc88d1dc 100644 --- a/third_party/zlib/inflate.c +++ b/third_party/zlib/inflate.c @@ -256,6 +256,8 @@ int value; struct inflate_state FAR *state; if (inflateStateCheck(strm)) return Z_STREAM_ERROR; + if (bits == 0) + return Z_OK; state = (struct inflate_state FAR *)strm->state; if (bits < 0) { state->hold = 0; @@ -1480,7 +1482,7 @@ z_streamp strm; /* if first time, start search in bit buffer */ if (state->mode != SYNC) { state->mode = SYNC; - state->hold <<= state->bits & 7; + state->hold >>= state->bits & 7; state->bits -= state->bits & 7; len = 0; while (state->bits >= 8) { @@ -1551,8 +1553,9 @@ z_streamp source; if (copy == Z_NULL) return Z_MEM_ERROR; window = Z_NULL; if (state->window != Z_NULL) { - window = (unsigned char FAR *) - ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); + window = (unsigned char FAR *)ZALLOC( + source, (1U << state->wbits) + CHUNKCOPY_CHUNK_SIZE, + sizeof(unsigned char)); if (window == Z_NULL) { ZFREE(source, copy); return Z_MEM_ERROR;