Skip to content

Commit

Permalink
Sync zlib
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Dec 31, 2023
1 parent 1bb8d1f commit 509d76f
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/archive/version.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
brotli 1.1.0
liblzma: https://tukaani.org/xz/ 5.4.5
zstd: https://github.com/facebook/zstd/releases/download/v1.5.5/zstd-1.5.5.tar.gz#lib
zlib: https://github.com/chromium/chromium/tree/93c45e25631fd2b9a27d48b48b28958d3995ab5b/third_party/zlib
zlib: https://github.com/chromium/chromium/tree/91a19f36beccbe06cb3fbb21312b8c25a7fd8991/third_party/zlib
3 changes: 2 additions & 1 deletion lib/archive/zlib/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# found in the LICENSE file.

import("//build/config/compiler/compiler.gni")
import("//build/config/dcheck_always_on.gni")

declare_args() {
# Expose zlib's symbols, used by Node.js to provide zlib APIs for its native
Expand Down Expand Up @@ -33,7 +34,7 @@ config("zlib_internal_config") {
# Build code using -O3, see: crbug.com/1084371.
configs = [ "//build/config/compiler:optimize_speed" ]
}
if (is_debug || use_fuzzing_engine) {
if (is_debug || dcheck_always_on || use_fuzzing_engine) {
# Enable zlib's asserts in debug and fuzzer builds.
defines += [ "ZLIB_DEBUG" ]
}
Expand Down
4 changes: 2 additions & 2 deletions lib/archive/zlib/README.chromium
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Name: zlib
Short Name: zlib
URL: http://zlib.net/
Version: 1.2.13
CPEPrefix: cpe:/a:zlib:zlib:1.2.13
Version: 1.3.0.1
CPEPrefix: cpe:/a:zlib:zlib:1.3.0.1
Security Critical: yes
Shipped: yes
License: Zlib
Expand Down
1 change: 1 addition & 0 deletions lib/archive/zlib/contrib/minizip/README.chromium
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ License: Zlib
License File: //third_party/zlib/LICENSE
Security Critical: yes
Shipped: yes
CPEPrefix: cpe:/a:minizip_project:minizip

Description:
Minizip provides API on top of zlib that can enumerate and extract ZIP archive
Expand Down
2 changes: 1 addition & 1 deletion lib/archive/zlib/contrib/optimizations/inflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ int ZEXPORT inflateSync(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
55 changes: 55 additions & 0 deletions lib/archive/zlib/contrib/tests/utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,61 @@ TEST(ZlibTest, DeflateZFixedCorruption) {
0);
}

TEST(ZlibTest, DeflateCopy) {
// Check that deflateCopy() works.

z_stream stream1;
stream1.zalloc = Z_NULL;
stream1.zfree = Z_NULL;
int ret =
deflateInit(&stream1, Z_DEFAULT_COMPRESSION);
ASSERT_EQ(ret, Z_OK);
std::vector<uint8_t> compressed(
deflateBound(&stream1, strlen(zFixedCorruptionData)));
stream1.next_out = compressed.data();
stream1.avail_out = compressed.size();

// Compress the first 1000 bytes.
stream1.next_in = (uint8_t*)zFixedCorruptionData;
stream1.avail_in = 1000;
ret = deflate(&stream1, Z_NO_FLUSH);
ASSERT_EQ(ret, Z_OK);

// Copy the stream state.
z_stream stream2;
ret = deflateCopy(&stream2, &stream1);
ASSERT_EQ(ret, Z_OK);
deflateEnd(&stream1);

// Compress the remaining bytes.
stream2.next_in = (uint8_t*)zFixedCorruptionData + (1000 - stream2.avail_in);
stream2.avail_in = strlen(zFixedCorruptionData) - (1000 - stream2.avail_in);
ret = deflate(&stream2, Z_FINISH);
ASSERT_EQ(ret, Z_STREAM_END);
size_t compressed_sz = compressed.size() - stream2.avail_out;
deflateEnd(&stream2);

// Check that decompression is successful.
std::vector<uint8_t> decompressed(strlen(zFixedCorruptionData));
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
ret = inflateInit(&stream);
ASSERT_EQ(ret, Z_OK);
stream.next_in = compressed.data();
stream.avail_in = compressed_sz;
stream.next_out = decompressed.data();
stream.avail_out = decompressed.size();
ret = inflate(&stream, Z_FINISH);
ASSERT_EQ(ret, Z_STREAM_END);
inflateEnd(&stream);

EXPECT_EQ(decompressed.size(), strlen(zFixedCorruptionData));
EXPECT_EQ(
memcmp(zFixedCorruptionData, decompressed.data(), decompressed.size()),
0);
}

// TODO(gustavoa): make these tests run standalone.
#ifndef CMAKE_STANDALONE_UNITTESTS

Expand Down
4 changes: 2 additions & 2 deletions lib/archive/zlib/crc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,8 @@ unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf,
words = (z_word_t const *)buf;

/* Do endian check at execution time instead of compile time, since ARM
processors can change the endianess at execution time. If the
compiler knows what the endianess will be, it can optimize out the
processors can change the endianness at execution time. If the
compiler knows what the endianness will be, it can optimize out the
check and the unused branch. */
endian = 1;
if (*(unsigned char *)&endian) {
Expand Down
38 changes: 36 additions & 2 deletions lib/archive/zlib/deflate.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* deflate.c -- compress data using the deflation algorithm
* Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler
* Copyright (C) 1995-2023 Jean-loup Gailly and Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/

Expand Down Expand Up @@ -65,7 +65,7 @@
#endif

const char deflate_copyright[] =
" deflate 1.2.13.1 Copyright 1995-2022 Jean-loup Gailly and Mark Adler ";
" deflate 1.3.0.1 Copyright 1995-2023 Jean-loup Gailly and Mark Adler ";
/*
If you use the zlib library in a product, an acknowledgment is welcome
in the documentation of your product. If for some reason you cannot
Expand Down Expand Up @@ -168,6 +168,11 @@ local const config configuration_table[10] = {
* bit values at the expense of memory usage). We slide even when level == 0 to
* keep the hash table consistent if we switch back to level > 0 later.
*/
#if defined(__has_feature)
# if __has_feature(memory_sanitizer)
__attribute__((no_sanitize("memory")))
# endif
#endif
local void slide_hash(deflate_state *s) {
#if defined(DEFLATE_SLIDE_HASH_SSE2) || defined(DEFLATE_SLIDE_HASH_NEON)
slide_hash_simd(s->head, s->prev, s->w_size, s->hash_size);
Expand Down Expand Up @@ -526,7 +531,11 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
* the compressed data for a dynamic block also cannot overwrite the
* symbols from which it is being constructed.
*/
#ifdef LIT_MEM
s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 5);
#else
s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
#endif
s->pending_buf_size = (ulg)s->lit_bufsize * 4;

if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
Expand All @@ -536,8 +545,14 @@ int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
deflateEnd (strm);
return Z_MEM_ERROR;
}
#ifdef LIT_MEM
s->d_buf = (ushf *)(s->pending_buf + (s->lit_bufsize << 1));
s->l_buf = s->pending_buf + (s->lit_bufsize << 2);
s->sym_end = s->lit_bufsize - 1;
#else
s->sym_buf = s->pending_buf + s->lit_bufsize;
s->sym_end = (s->lit_bufsize - 1) * 3;
#endif
/* We avoid equality with lit_bufsize*3 because of wraparound at 64K
* on 16 bit machines and because stored blocks are restricted to
* 64K-1 bytes.
Expand Down Expand Up @@ -749,9 +764,15 @@ int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {

if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
s = strm->state;
#ifdef LIT_MEM
if (bits < 0 || bits > 16 ||
(uchf *)s->d_buf < s->pending_out + ((Buf_size + 7) >> 3))
return Z_BUF_ERROR;
#else
if (bits < 0 || bits > 16 ||
s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
return Z_BUF_ERROR;
#endif
do {
put = Buf_size - s->bi_valid;
if (put > bits)
Expand Down Expand Up @@ -1324,7 +1345,11 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
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));
#ifdef LIT_MEM
ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 5);
#else
ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4);
#endif

if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
ds->pending_buf == Z_NULL) {
Expand All @@ -1335,10 +1360,19 @@ int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
zmemcpy(ds->window, ss->window, ds->w_size * 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));
#ifdef LIT_MEM
zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->lit_bufsize * 5);
#else
zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
#endif

ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
#ifdef LIT_MEM
ds->d_buf = (ushf *)(ds->pending_buf + (ds->lit_bufsize << 1));
ds->l_buf = ds->pending_buf + (ds->lit_bufsize << 2);
#else
ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
#endif

ds->l_desc.dyn_tree = ds->dyn_ltree;
ds->d_desc.dyn_tree = ds->dyn_dtree;
Expand Down
31 changes: 30 additions & 1 deletion lib/archive/zlib/deflate.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
# define GZIP
#endif

/* define LIT_MEM to slightly increase the speed of deflate (order 1% to 2%) at
the cost of a larger memory footprint */
#define LIT_MEM

/* ===========================================================================
* Internal compression state.
*/
Expand Down Expand Up @@ -217,7 +221,12 @@ typedef struct internal_state {
/* Depth of each subtree used as tie breaker for trees of equal frequency
*/

#ifdef LIT_MEM
ushf *d_buf; /* buffer for distances */
uchf *l_buf; /* buffer for literals/lengths */
#else
uchf *sym_buf; /* buffer for distances and literals/lengths */
#endif

uInt lit_bufsize;
/* Size of match buffer for literals/lengths. There are 4 reasons for
Expand All @@ -239,7 +248,7 @@ typedef struct internal_state {
* - I can't count above 4
*/

uInt sym_next; /* running index in sym_buf */
uInt sym_next; /* running index in symbol buffer */
uInt sym_end; /* symbol table full when sym_next reaches this */

ulg opt_len; /* bit length of current block with optimal trees */
Expand Down Expand Up @@ -323,6 +332,25 @@ void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf,
extern const uch ZLIB_INTERNAL _dist_code[];
#endif

#ifdef LIT_MEM
# define _tr_tally_lit(s, c, flush) \
{ uch cc = (c); \
s->d_buf[s->sym_next] = 0; \
s->l_buf[s->sym_next++] = cc; \
s->dyn_ltree[cc].Freq++; \
flush = (s->sym_next == s->sym_end); \
}
# define _tr_tally_dist(s, distance, length, flush) \
{ uch len = (uch)(length); \
ush dist = (ush)(distance); \
s->d_buf[s->sym_next] = dist; \
s->l_buf[s->sym_next++] = len; \
dist--; \
s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
s->dyn_dtree[d_code(dist)].Freq++; \
flush = (s->sym_next == s->sym_end); \
}
#else
# define _tr_tally_lit(s, c, flush) \
{ uch cc = (c); \
s->sym_buf[s->sym_next++] = 0; \
Expand All @@ -342,6 +370,7 @@ void ZLIB_INTERNAL _tr_stored_block(deflate_state *s, charf *buf,
s->dyn_dtree[d_code(dist)].Freq++; \
flush = (s->sym_next == s->sym_end); \
}
#endif
#else
# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
# define _tr_tally_dist(s, distance, length, flush) \
Expand Down
22 changes: 11 additions & 11 deletions lib/archive/zlib/google/compression_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "third_party/zlib/google/compression_utils.h"

#include "base/bit_cast.h"
#include "base/check_op.h"
#include "base/process/memory.h"
#include "base/sys_byteorder.h"
Expand All @@ -24,8 +23,8 @@ bool GzipCompress(base::span<const char> input,
// uLongf can be larger than size_t.
uLongf compressed_size_long = static_cast<uLongf>(output_buffer_size);
if (zlib_internal::GzipCompressHelper(
base::bit_cast<Bytef*>(output_buffer), &compressed_size_long,
base::bit_cast<const Bytef*>(input.data()),
reinterpret_cast<Bytef*>(output_buffer), &compressed_size_long,
reinterpret_cast<const Bytef*>(input.data()),
static_cast<uLongf>(input.size()), malloc_fn, free_fn) != Z_OK) {
return false;
}
Expand Down Expand Up @@ -55,7 +54,7 @@ bool GzipCompress(base::span<const uint8_t> input, std::string* output) {

if (zlib_internal::GzipCompressHelper(
compressed_data, &compressed_data_size,
base::bit_cast<const Bytef*>(input.data()), input_size, nullptr,
reinterpret_cast<const Bytef*>(input.data()), input_size, nullptr,
nullptr) != Z_OK) {
free(compressed_data);
return false;
Expand All @@ -82,8 +81,8 @@ bool GzipUncompress(const std::string& input, std::string* output) {

uncompressed_output.resize(uncompressed_size);
if (zlib_internal::GzipUncompressHelper(
base::bit_cast<Bytef*>(uncompressed_output.data()),
&uncompressed_size, base::bit_cast<const Bytef*>(input.data()),
reinterpret_cast<Bytef*>(uncompressed_output.data()),
&uncompressed_size, reinterpret_cast<const Bytef*>(input.data()),
static_cast<uLongf>(input.length())) == Z_OK) {
output->swap(uncompressed_output);
return true;
Expand All @@ -102,8 +101,8 @@ bool GzipUncompress(base::span<const uint8_t> input,
if (uncompressed_size > output.size())
return false;
return zlib_internal::GzipUncompressHelper(
base::bit_cast<Bytef*>(output.data()), &uncompressed_size,
base::bit_cast<const Bytef*>(input.data()),
reinterpret_cast<Bytef*>(const_cast<uint8_t*>(output.data())),
&uncompressed_size, reinterpret_cast<const Bytef*>(input.data()),
static_cast<uLongf>(input.size())) == Z_OK;
}

Expand All @@ -117,8 +116,8 @@ bool GzipUncompress(base::span<const uint8_t> input, std::string* output) {
uLongf uncompressed_size = GetUncompressedSize(input);
output->resize(uncompressed_size);
return zlib_internal::GzipUncompressHelper(
base::bit_cast<Bytef*>(output->data()), &uncompressed_size,
base::bit_cast<const Bytef*>(input.data()),
reinterpret_cast<Bytef*>(output->data()), &uncompressed_size,
reinterpret_cast<const Bytef*>(input.data()),
static_cast<uLongf>(input.size())) == Z_OK;
}

Expand All @@ -128,7 +127,8 @@ uint32_t GetUncompressedSize(base::span<const char> compressed_data) {

uint32_t GetUncompressedSize(base::span<const uint8_t> compressed_data) {
return zlib_internal::GetGzipUncompressedSize(
base::bit_cast<Bytef*>(compressed_data.data()), compressed_data.size());
reinterpret_cast<const Bytef*>(compressed_data.data()),
compressed_data.size());
}

} // namespace compression
2 changes: 1 addition & 1 deletion lib/archive/zlib/google/zip_reader_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class ZipReaderTest : public PlatformTest {

static base::FilePath GetTestDataDirectory() {
base::FilePath path;
CHECK(base::PathService::Get(base::DIR_SOURCE_ROOT, &path));
CHECK(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &path));
return path.AppendASCII("third_party")
.AppendASCII("zlib")
.AppendASCII("google")
Expand Down
4 changes: 2 additions & 2 deletions lib/archive/zlib/google/zip_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class VirtualFileSystem : public zip::FileAccessor {

info->is_directory = !files_.count(path);
info->last_modified =
base::Time::FromDoubleT(172097977); // Some random date.
base::Time::FromSecondsSinceUnixEpoch(172097977); // Some random date.

return true;
}
Expand Down Expand Up @@ -256,7 +256,7 @@ class ZipTest : public PlatformTest {

static base::FilePath GetDataDirectory() {
base::FilePath path;
bool success = base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
bool success = base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &path);
EXPECT_TRUE(success);
return std::move(path)
.AppendASCII("third_party")
Expand Down
Loading

0 comments on commit 509d76f

Please sign in to comment.