Skip to content

Commit

Permalink
Import some Chromium Zlib changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Oct 11, 2024
1 parent d8fac40 commit a8bc7ac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion third_party/zlib/crc_folding.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ ZLIB_INTERNAL void crc_fold_copy(deflate_state *const s,
}
#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:
Expand Down
20 changes: 15 additions & 5 deletions third_party/zlib/deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions third_party/zlib/inflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a8bc7ac

Please sign in to comment.