├── Android.bp ├── BUILD.gn ├── CMakeLists.txt ├── CleanSpec.mk ├── DIR_METADATA ├── LICENSE ├── METADATA ├── MODULE_LICENSE_BSD_LIKE ├── OWNERS ├── OWNERS.android ├── README.chromium ├── TEST_MAPPING ├── adler32.c ├── adler32_simd.c ├── adler32_simd.h ├── chromeconf.h ├── compress.c ├── contrib ├── bench │ ├── check.sh │ └── zlib_bench.cc ├── minizip │ ├── ChangeLogUnzip │ ├── Makefile │ ├── README.chromium │ ├── crypt.h │ ├── ioapi.c │ ├── ioapi.h │ ├── iowin32.c │ ├── iowin32.h │ ├── miniunz.c │ ├── minizip.c │ ├── minizip.md │ ├── mztools.c │ ├── mztools.h │ ├── unzip.c │ ├── unzip.h │ ├── zip.c │ └── zip.h ├── optimizations │ ├── chunkcopy.h │ ├── inffast_chunk.c │ ├── inffast_chunk.h │ ├── inflate.c │ └── insert_string.h └── tests │ ├── DEPS │ ├── OWNERS │ ├── fuzzers │ ├── BUILD.gn │ ├── OWNERS │ ├── deflate_fuzzer.cc │ ├── deflate_set_dictionary_fuzzer.cc │ ├── inflate_fuzzer.cc │ ├── inflate_with_header_fuzzer.cc │ ├── streaming_inflate_fuzzer.cc │ └── uncompress_fuzzer.cc │ ├── infcover.cc │ ├── infcover.h │ ├── run_all_unittests.cc │ ├── standalone_test_runner.cc │ └── utils_unittest.cc ├── cpu_features.c ├── cpu_features.h ├── crc32.c ├── crc32.h ├── crc32_simd.c ├── crc32_simd.h ├── crc_folding.c ├── deflate.c ├── deflate.h ├── examples └── zpipe.c ├── google ├── BUILD.gn ├── DEPS ├── OWNERS ├── compression_utils.cc ├── compression_utils.h ├── compression_utils_portable.cc ├── compression_utils_portable.h ├── compression_utils_unittest.cc ├── redact.h ├── test │ └── data │ │ ├── Different Encryptions.zip │ │ ├── Empty Dir Same Name As File.zip │ │ ├── Mixed Paths.zip │ │ ├── Parent Dir Same Name As File.zip │ │ ├── README.md │ │ ├── Repeated Dir Name.zip │ │ ├── Repeated File Name With Different Cases.zip │ │ ├── Repeated File Name.zip │ │ ├── SJIS Bug 846195.zip │ │ ├── Windows Special Names.zip │ │ ├── Wrong CRC.zip │ │ ├── create_test_zip.sh │ │ ├── empty.zip │ │ ├── evil.zip │ │ ├── evil_via_absolute_file_name.zip │ │ ├── evil_via_invalid_utf8.zip │ │ ├── test.zip │ │ ├── test │ │ ├── foo.txt │ │ └── foo │ │ │ ├── bar.txt │ │ │ └── bar │ │ │ ├── .hidden │ │ │ ├── baz.txt │ │ │ └── quux.txt │ │ ├── test_encrypted.zip │ │ ├── test_mismatch_size.zip │ │ ├── test_nocompress.zip │ │ └── test_posix_permissions.zip ├── test_data.filelist ├── test_data.globlist ├── zip.cc ├── zip.h ├── zip_internal.cc ├── zip_internal.h ├── zip_reader.cc ├── zip_reader.h ├── zip_reader_unittest.cc ├── zip_unittest.cc ├── zip_writer.cc └── zip_writer.h ├── gzclose.c ├── gzguts.h ├── gzlib.c ├── gzread.c ├── gzwrite.c ├── infback.c ├── inffast.c ├── inffast.h ├── inffixed.h ├── inflate.c ├── inflate.h ├── inftrees.c ├── inftrees.h ├── libz.map.txt ├── patches ├── 0000-build.patch ├── 0001-simd.patch ├── 0002-uninitializedcheck.patch ├── 0003-uninitializedjump.patch ├── 0004-fix-uwp.patch ├── 0005-infcover-gtest.patch ├── 0006-fix-check_match.patch ├── 0007-zero-init-deflate-window.patch ├── 0008-minizip-zip-unzip-tools.patch ├── 0009-infcover-oob.patch ├── 0010-cmake-enable-simd.patch ├── 0011-avx512.patch ├── 0012-lfs-open64.patch ├── 0013-cpu-feature-detection-for-arm.patch ├── 0014-minizip-unzip-with-incorrect-size.patch ├── 0015-minizip-unzip-enable-decryption.patch ├── 0016-minizip-parse-unicode-path-extra-field.patch └── README ├── slide_hash_simd.h ├── test └── minigzip.c ├── trees.c ├── trees.h ├── uncompr.c ├── zconf.h ├── zconf.h.cmakein ├── zconf.h.in ├── zlib.3 ├── zlib.h ├── zlib.map ├── zlib.pc.cmakein ├── zutil.c └── zutil.h /CleanSpec.mk: -------------------------------------------------------------------------------- 1 | # Copyright (C) 2007 The Android Open Source Project 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # 15 | 16 | # If you don't need to do a full clean build but would like to touch 17 | # a file or delete some intermediate files, add a clean step to the end 18 | # of the list. These steps will only be run once, if they haven't been 19 | # run before. 20 | # 21 | # E.g.: 22 | # $(call add-clean-step, touch -c external/sqlite/sqlite3.h) 23 | # $(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libz_intermediates) 24 | # 25 | # Always use "touch -c" and "rm -f" or "rm -rf" to gracefully deal with 26 | # files that are missing or have been moved. 27 | # 28 | # Use $(PRODUCT_OUT) to get to the "out/target/product/blah/" directory. 29 | # Use $(OUT_DIR) to refer to the "out" directory. 30 | # 31 | # If you need to re-do something that's already mentioned, just copy 32 | # the command and add it to the bottom of the list. E.g., if a change 33 | # that you made last week required touching a file and a change you 34 | # made today requires touching the same file, just copy the old 35 | # touch step and add it to the end of the list. 36 | # 37 | # ************************************************ 38 | # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST 39 | # ************************************************ 40 | 41 | # For example: 42 | #$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/APPS/AndroidTests_intermediates) 43 | #$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/JAVA_LIBRARIES/core_intermediates) 44 | #$(call add-clean-step, find $(OUT_DIR) -type f -name "IGTalkSession*" -print0 | xargs -0 rm -f) 45 | #$(call add-clean-step, rm -rf $(PRODUCT_OUT)/data/*) 46 | 47 | # ************************************************ 48 | # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST 49 | # ************************************************ 50 | -------------------------------------------------------------------------------- /DIR_METADATA: -------------------------------------------------------------------------------- 1 | monorail: { 2 | component: "Internals" 3 | } 4 | buganizer_public: { 5 | component_id: 1456292 6 | } 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | version 1.2.12, March 27th, 2022 2 | 3 | Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any damages 7 | arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software 15 | in a product, an acknowledgment in the product documentation would be 16 | appreciated but is not required. 17 | 2. Altered source versions must be plainly marked as such, and must not be 18 | misrepresented as being the original software. 19 | 3. This notice may not be removed or altered from any source distribution. 20 | -------------------------------------------------------------------------------- /METADATA: -------------------------------------------------------------------------------- 1 | # This project was upgraded with external_updater. 2 | # Usage: tools/external_updater/updater.sh update external/zlib 3 | # For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md 4 | 5 | name: "zlib" 6 | description: "The Chromium fork of the zlib compression library." 7 | third_party { 8 | license_type: NOTICE 9 | last_upgrade_date { 10 | year: 2024 11 | month: 4 12 | day: 10 13 | } 14 | identifier { 15 | type: "Git" 16 | value: "https://chromium.googlesource.com/chromium/src/third_party/zlib/" 17 | version: "7d77fb7fd66d8a5640618ad32c71fdeb7d3e02df" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /MODULE_LICENSE_BSD_LIKE: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/MODULE_LICENSE_BSD_LIKE -------------------------------------------------------------------------------- /OWNERS: -------------------------------------------------------------------------------- 1 | agl@chromium.org 2 | cavalcantii@chromium.org 3 | cblume@chromium.org 4 | scroggo@google.com 5 | -------------------------------------------------------------------------------- /OWNERS.android: -------------------------------------------------------------------------------- 1 | include platform/system/core:/janitors/OWNERS 2 | -------------------------------------------------------------------------------- /README.chromium: -------------------------------------------------------------------------------- 1 | Name: zlib 2 | Short Name: zlib 3 | URL: http://zlib.net/ 4 | Version: 1.3.0.1 5 | CPEPrefix: cpe:/a:zlib:zlib:1.3.0.1 6 | Security Critical: yes 7 | Shipped: yes 8 | License: Zlib 9 | License File: LICENSE 10 | License Android Compatible: yes 11 | 12 | Description: 13 | "A massively spiffy yet delicately unobtrusive compression library." 14 | 15 | zlib is a free, general-purpose, legally unencumbered lossless data-compression 16 | library. zlib implements the "deflate" compression algorithm described by RFC 17 | 1951, which combines the LZ77 (Lempel-Ziv) algorithm with Huffman coding. zlib 18 | also implements the zlib (RFC 1950) and gzip (RFC 1952) wrapper formats. 19 | 20 | Local Modifications: 21 | - Only source code from the zlib distribution used to build the zlib and 22 | minizip libraries are present. Many other files have been omitted. Only *.c 23 | and *.h files from the upstream root directory and contrib/minizip were 24 | imported. 25 | - The contents of the google directory are original Chromium-specific 26 | additions. 27 | - Added chromeconf.h 28 | - Plus the changes in 'patches' folder. 29 | - Code in contrib/ other than contrib/minizip was added to match zlib's 30 | contributor layout. 31 | - In sync with 1.2.13 official release 32 | - ZIP reader modified to allow for progress callbacks during extraction. 33 | - ZIP reader modified to add detection of AES encrypted content. 34 | -------------------------------------------------------------------------------- /TEST_MAPPING: -------------------------------------------------------------------------------- 1 | { 2 | "presubmit": [ 3 | { 4 | "name": "puffin_unittest" 5 | }, 6 | { 7 | "name": "recovery_unit_test" 8 | }, 9 | { 10 | "name": "update_engine_unittests" 11 | }, 12 | { 13 | "name": "ziparchive-tests" 14 | }, 15 | { 16 | "name": "zlib_tests" 17 | }, 18 | { 19 | "name": "CtsLibcoreTestCases", 20 | "options": [ 21 | { 22 | "include-filter": "org.apache.harmony.tests.java.util.zip" 23 | } 24 | ] 25 | } 26 | ] 27 | } 28 | -------------------------------------------------------------------------------- /adler32.c: -------------------------------------------------------------------------------- 1 | /* adler32.c -- compute the Adler-32 checksum of a data stream 2 | * Copyright (C) 1995-2011, 2016 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #include "zutil.h" 9 | 10 | #define BASE 65521U /* largest prime smaller than 65536 */ 11 | #define NMAX 5552 12 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ 13 | 14 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} 15 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); 16 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); 17 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); 18 | #define DO16(buf) DO8(buf,0); DO8(buf,8); 19 | 20 | /* use NO_DIVIDE if your processor does not do division in hardware -- 21 | try it both ways to see which is faster */ 22 | #ifdef NO_DIVIDE 23 | /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 24 | (thank you to John Reiser for pointing this out) */ 25 | # define CHOP(a) \ 26 | do { \ 27 | unsigned long tmp = a >> 16; \ 28 | a &= 0xffffUL; \ 29 | a += (tmp << 4) - tmp; \ 30 | } while (0) 31 | # define MOD28(a) \ 32 | do { \ 33 | CHOP(a); \ 34 | if (a >= BASE) a -= BASE; \ 35 | } while (0) 36 | # define MOD(a) \ 37 | do { \ 38 | CHOP(a); \ 39 | MOD28(a); \ 40 | } while (0) 41 | # define MOD63(a) \ 42 | do { /* this assumes a is not negative */ \ 43 | z_off64_t tmp = a >> 32; \ 44 | a &= 0xffffffffL; \ 45 | a += (tmp << 8) - (tmp << 5) + tmp; \ 46 | tmp = a >> 16; \ 47 | a &= 0xffffL; \ 48 | a += (tmp << 4) - tmp; \ 49 | tmp = a >> 16; \ 50 | a &= 0xffffL; \ 51 | a += (tmp << 4) - tmp; \ 52 | if (a >= BASE) a -= BASE; \ 53 | } while (0) 54 | #else 55 | # define MOD(a) a %= BASE 56 | # define MOD28(a) a %= BASE 57 | # define MOD63(a) a %= BASE 58 | #endif 59 | 60 | #include "cpu_features.h" 61 | #if defined(ADLER32_SIMD_SSSE3) || defined(ADLER32_SIMD_NEON) || defined(ADLER32_SIMD_RVV) 62 | #include "adler32_simd.h" 63 | #endif 64 | 65 | /* ========================================================================= */ 66 | uLong ZEXPORT adler32_z(uLong adler, const Bytef *buf, z_size_t len) { 67 | unsigned long sum2; 68 | unsigned n; 69 | /* TODO(cavalcantii): verify if this lengths are optimal for current CPUs. */ 70 | #if defined(ADLER32_SIMD_SSSE3) || defined(ADLER32_SIMD_NEON) \ 71 | || defined(ADLER32_SIMD_RVV) 72 | #if defined(ADLER32_SIMD_SSSE3) 73 | if (buf != Z_NULL && len >= 64 && x86_cpu_enable_ssse3) 74 | #elif defined(ADLER32_SIMD_NEON) 75 | if (buf != Z_NULL && len >= 64) 76 | #elif defined(ADLER32_SIMD_RVV) 77 | if (buf != Z_NULL && len >= 32 && riscv_cpu_enable_rvv) 78 | #endif 79 | return adler32_simd_(adler, buf, len); 80 | #endif 81 | 82 | /* split Adler-32 into component sums */ 83 | sum2 = (adler >> 16) & 0xffff; 84 | adler &= 0xffff; 85 | 86 | /* in case user likes doing a byte at a time, keep it fast */ 87 | if (len == 1) { 88 | adler += buf[0]; 89 | if (adler >= BASE) 90 | adler -= BASE; 91 | sum2 += adler; 92 | if (sum2 >= BASE) 93 | sum2 -= BASE; 94 | return adler | (sum2 << 16); 95 | } 96 | 97 | #if defined(ADLER32_SIMD_SSSE3) || defined(ADLER32_SIMD_NEON) \ 98 | || defined(RISCV_RVV) 99 | /* 100 | * Use SIMD to compute the adler32. Since this function can be 101 | * freely used, check CPU features here. zlib convention is to 102 | * call adler32(0, NULL, 0), before making calls to adler32(). 103 | * So this is a good early (and infrequent) place to cache CPU 104 | * features for those later, more interesting adler32() calls. 105 | */ 106 | if (buf == Z_NULL) { 107 | if (!len) /* Assume user is calling adler32(0, NULL, 0); */ 108 | cpu_check_features(); 109 | return 1L; 110 | } 111 | #else 112 | /* initial Adler-32 value (deferred check for len == 1 speed) */ 113 | if (buf == Z_NULL) 114 | return 1L; 115 | #endif 116 | 117 | /* in case short lengths are provided, keep it somewhat fast */ 118 | if (len < 16) { 119 | while (len--) { 120 | adler += *buf++; 121 | sum2 += adler; 122 | } 123 | if (adler >= BASE) 124 | adler -= BASE; 125 | MOD28(sum2); /* only added so many BASE's */ 126 | return adler | (sum2 << 16); 127 | } 128 | 129 | /* do length NMAX blocks -- requires just one modulo operation */ 130 | while (len >= NMAX) { 131 | len -= NMAX; 132 | n = NMAX / 16; /* NMAX is divisible by 16 */ 133 | do { 134 | DO16(buf); /* 16 sums unrolled */ 135 | buf += 16; 136 | } while (--n); 137 | MOD(adler); 138 | MOD(sum2); 139 | } 140 | 141 | /* do remaining bytes (less than NMAX, still just one modulo) */ 142 | if (len) { /* avoid modulos if none remaining */ 143 | while (len >= 16) { 144 | len -= 16; 145 | DO16(buf); 146 | buf += 16; 147 | } 148 | while (len--) { 149 | adler += *buf++; 150 | sum2 += adler; 151 | } 152 | MOD(adler); 153 | MOD(sum2); 154 | } 155 | 156 | /* return recombined sums */ 157 | return adler | (sum2 << 16); 158 | } 159 | 160 | /* ========================================================================= */ 161 | uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len) { 162 | return adler32_z(adler, buf, len); 163 | } 164 | 165 | /* ========================================================================= */ 166 | local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2) { 167 | unsigned long sum1; 168 | unsigned long sum2; 169 | unsigned rem; 170 | 171 | /* for negative len, return invalid adler32 as a clue for debugging */ 172 | if (len2 < 0) 173 | return 0xffffffffUL; 174 | 175 | /* the derivation of this formula is left as an exercise for the reader */ 176 | MOD63(len2); /* assumes len2 >= 0 */ 177 | rem = (unsigned)len2; 178 | sum1 = adler1 & 0xffff; 179 | sum2 = rem * sum1; 180 | MOD(sum2); 181 | sum1 += (adler2 & 0xffff) + BASE - 1; 182 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; 183 | if (sum1 >= BASE) sum1 -= BASE; 184 | if (sum1 >= BASE) sum1 -= BASE; 185 | if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1); 186 | if (sum2 >= BASE) sum2 -= BASE; 187 | return sum1 | (sum2 << 16); 188 | } 189 | 190 | /* ========================================================================= */ 191 | uLong ZEXPORT adler32_combine(uLong adler1, uLong adler2, z_off_t len2) { 192 | return adler32_combine_(adler1, adler2, len2); 193 | } 194 | 195 | uLong ZEXPORT adler32_combine64(uLong adler1, uLong adler2, z_off64_t len2) { 196 | return adler32_combine_(adler1, adler2, len2); 197 | } 198 | -------------------------------------------------------------------------------- /adler32_simd.h: -------------------------------------------------------------------------------- 1 | /* adler32_simd.h 2 | * 3 | * Copyright 2017 The Chromium Authors 4 | * Use of this source code is governed by a BSD-style license that can be 5 | * found in the Chromium source repository LICENSE file. 6 | */ 7 | 8 | #include 9 | 10 | #include "zconf.h" 11 | #include "zutil.h" 12 | 13 | uint32_t ZLIB_INTERNAL adler32_simd_( 14 | uint32_t adler, 15 | const unsigned char *buf, 16 | z_size_t len); 17 | -------------------------------------------------------------------------------- /chromeconf.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2017 The Chromium Authors 2 | * Use of this source code is governed by a BSD-style license that can be 3 | * found in the LICENSE file. */ 4 | 5 | #ifndef THIRD_PARTY_ZLIB_CHROMECONF_H_ 6 | #define THIRD_PARTY_ZLIB_CHROMECONF_H_ 7 | 8 | #if defined(COMPONENT_BUILD) 9 | #if defined(WIN32) 10 | #if defined(ZLIB_IMPLEMENTATION) 11 | #define ZEXTERN __declspec(dllexport) 12 | #else 13 | #define ZEXTERN __declspec(dllimport) 14 | #endif 15 | #elif defined(ZLIB_IMPLEMENTATION) 16 | #define ZEXTERN __attribute__((visibility("default"))) 17 | #endif 18 | #endif 19 | 20 | /* Rename all zlib names with a Cr_z_ prefix. This is based on the Z_PREFIX 21 | * option from zconf.h, but with a custom prefix. Where zconf.h would rename 22 | * both a macro and its underscore-suffixed internal implementation (such as 23 | * deflateInit2 and deflateInit2_), only the implementation is renamed here. 24 | * The Byte type is also omitted. 25 | * 26 | * To generate this list, run 27 | * sed -rn -e 's/^# *define +([^ ]+) +(z_[^ ]+)$/#define \1 Cr_\2/p' zconf.h 28 | * (use -E instead of -r on macOS). 29 | * 30 | * gzread is also addressed by modifications in gzread.c and zlib.h. */ 31 | 32 | #define Z_CR_PREFIX_SET 33 | 34 | #define _dist_code Cr_z__dist_code 35 | #define _length_code Cr_z__length_code 36 | #define _tr_align Cr_z__tr_align 37 | #define _tr_flush_bits Cr_z__tr_flush_bits 38 | #define _tr_flush_block Cr_z__tr_flush_block 39 | #define _tr_init Cr_z__tr_init 40 | #define _tr_stored_block Cr_z__tr_stored_block 41 | #define _tr_tally Cr_z__tr_tally 42 | #define adler32 Cr_z_adler32 43 | #define adler32_combine Cr_z_adler32_combine 44 | #define adler32_combine64 Cr_z_adler32_combine64 45 | #define adler32_z Cr_z_adler32_z 46 | #define compress Cr_z_compress 47 | #define compress2 Cr_z_compress2 48 | #define compressBound Cr_z_compressBound 49 | #define crc32 Cr_z_crc32 50 | #define crc32_combine Cr_z_crc32_combine 51 | #define crc32_combine64 Cr_z_crc32_combine64 52 | #define crc32_combine_gen64 Cr_z_crc32_combine_gen64 53 | #define crc32_combine_gen Cr_z_crc32_combine_gen 54 | #define crc32_combine_op Cr_z_crc32_combine_op 55 | #define crc32_z Cr_z_crc32_z 56 | #define deflate Cr_z_deflate 57 | #define deflateBound Cr_z_deflateBound 58 | #define deflateCopy Cr_z_deflateCopy 59 | #define deflateEnd Cr_z_deflateEnd 60 | #define deflateGetDictionary Cr_z_deflateGetDictionary 61 | /* #undef deflateInit */ 62 | /* #undef deflateInit2 */ 63 | #define deflateInit2_ Cr_z_deflateInit2_ 64 | #define deflateInit_ Cr_z_deflateInit_ 65 | #define deflateParams Cr_z_deflateParams 66 | #define deflatePending Cr_z_deflatePending 67 | #define deflatePrime Cr_z_deflatePrime 68 | #define deflateReset Cr_z_deflateReset 69 | #define deflateResetKeep Cr_z_deflateResetKeep 70 | #define deflateSetDictionary Cr_z_deflateSetDictionary 71 | #define deflateSetHeader Cr_z_deflateSetHeader 72 | #define deflateTune Cr_z_deflateTune 73 | #define deflate_copyright Cr_z_deflate_copyright 74 | #define get_crc_table Cr_z_get_crc_table 75 | #define gz_error Cr_z_gz_error 76 | #define gz_intmax Cr_z_gz_intmax 77 | #define gz_strwinerror Cr_z_gz_strwinerror 78 | #define gzbuffer Cr_z_gzbuffer 79 | #define gzclearerr Cr_z_gzclearerr 80 | #define gzclose Cr_z_gzclose 81 | #define gzclose_r Cr_z_gzclose_r 82 | #define gzclose_w Cr_z_gzclose_w 83 | #define gzdirect Cr_z_gzdirect 84 | #define gzdopen Cr_z_gzdopen 85 | #define gzeof Cr_z_gzeof 86 | #define gzerror Cr_z_gzerror 87 | #define gzflush Cr_z_gzflush 88 | #define gzfread Cr_z_gzfread 89 | #define gzfwrite Cr_z_gzfwrite 90 | #define gzgetc Cr_z_gzgetc 91 | #define gzgetc_ Cr_z_gzgetc_ 92 | #define gzgets Cr_z_gzgets 93 | #define gzoffset Cr_z_gzoffset 94 | #define gzoffset64 Cr_z_gzoffset64 95 | #define gzopen Cr_z_gzopen 96 | #define gzopen64 Cr_z_gzopen64 97 | #define gzopen_w Cr_z_gzopen_w 98 | #define gzprintf Cr_z_gzprintf 99 | #define gzputc Cr_z_gzputc 100 | #define gzputs Cr_z_gzputs 101 | #define gzread Cr_z_gzread 102 | #define gzrewind Cr_z_gzrewind 103 | #define gzseek Cr_z_gzseek 104 | #define gzseek64 Cr_z_gzseek64 105 | #define gzsetparams Cr_z_gzsetparams 106 | #define gztell Cr_z_gztell 107 | #define gztell64 Cr_z_gztell64 108 | #define gzungetc Cr_z_gzungetc 109 | #define gzvprintf Cr_z_gzvprintf 110 | #define gzwrite Cr_z_gzwrite 111 | #define inflate Cr_z_inflate 112 | #define inflateBack Cr_z_inflateBack 113 | #define inflateBackEnd Cr_z_inflateBackEnd 114 | /* #undef inflateBackInit */ 115 | #define inflateBackInit_ Cr_z_inflateBackInit_ 116 | #define inflateCodesUsed Cr_z_inflateCodesUsed 117 | #define inflateCopy Cr_z_inflateCopy 118 | #define inflateEnd Cr_z_inflateEnd 119 | #define inflateGetDictionary Cr_z_inflateGetDictionary 120 | #define inflateGetHeader Cr_z_inflateGetHeader 121 | /* #undef inflateInit */ 122 | /* #undef inflateInit2 */ 123 | #define inflateInit2_ Cr_z_inflateInit2_ 124 | #define inflateInit_ Cr_z_inflateInit_ 125 | #define inflateMark Cr_z_inflateMark 126 | #define inflatePrime Cr_z_inflatePrime 127 | #define inflateReset Cr_z_inflateReset 128 | #define inflateReset2 Cr_z_inflateReset2 129 | #define inflateResetKeep Cr_z_inflateResetKeep 130 | #define inflateSetDictionary Cr_z_inflateSetDictionary 131 | #define inflateSync Cr_z_inflateSync 132 | #define inflateSyncPoint Cr_z_inflateSyncPoint 133 | #define inflateUndermine Cr_z_inflateUndermine 134 | #define inflateValidate Cr_z_inflateValidate 135 | #define inflate_copyright Cr_z_inflate_copyright 136 | #define inflate_fast Cr_z_inflate_fast 137 | #define inflate_table Cr_z_inflate_table 138 | #define uncompress Cr_z_uncompress 139 | #define uncompress2 Cr_z_uncompress2 140 | #define zError Cr_z_zError 141 | #define zcalloc Cr_z_zcalloc 142 | #define zcfree Cr_z_zcfree 143 | #define zlibCompileFlags Cr_z_zlibCompileFlags 144 | #define zlibVersion Cr_z_zlibVersion 145 | /* #undef Byte */ 146 | #define Bytef Cr_z_Bytef 147 | #define alloc_func Cr_z_alloc_func 148 | #define charf Cr_z_charf 149 | #define free_func Cr_z_free_func 150 | #define gzFile Cr_z_gzFile 151 | #define gz_header Cr_z_gz_header 152 | #define gz_headerp Cr_z_gz_headerp 153 | #define in_func Cr_z_in_func 154 | #define intf Cr_z_intf 155 | #define out_func Cr_z_out_func 156 | #define uInt Cr_z_uInt 157 | #define uIntf Cr_z_uIntf 158 | #define uLong Cr_z_uLong 159 | #define uLongf Cr_z_uLongf 160 | #define voidp Cr_z_voidp 161 | #define voidpc Cr_z_voidpc 162 | #define voidpf Cr_z_voidpf 163 | #define gz_header_s Cr_z_gz_header_s 164 | /* #undef internal_state */ 165 | /* #undef z_off64_t */ 166 | 167 | /* An exported symbol that isn't handled by Z_PREFIX in zconf.h */ 168 | #define z_errmsg Cr_z_z_errmsg 169 | 170 | /* Symbols added in simd.patch */ 171 | #define copy_with_crc Cr_z_copy_with_crc 172 | #define crc_finalize Cr_z_crc_finalize 173 | #define crc_fold_512to32 Cr_z_crc_fold_512to32 174 | #define crc_fold_copy Cr_z_crc_fold_copy 175 | #define crc_fold_init Cr_z_crc_fold_init 176 | #define crc_reset Cr_z_crc_reset 177 | #define fill_window_sse Cr_z_fill_window_sse 178 | #define deflate_read_buf Cr_z_deflate_read_buf 179 | #define x86_check_features Cr_z_x86_check_features 180 | #define x86_cpu_enable_simd Cr_z_x86_cpu_enable_simd 181 | 182 | /* Symbols added by adler_simd.c */ 183 | #define adler32_simd_ Cr_z_adler32_simd_ 184 | #define x86_cpu_enable_ssse3 Cr_z_x86_cpu_enable_ssse3 185 | 186 | /* Symbols added by contrib/optimizations/inffast_chunk */ 187 | #define inflate_fast_chunk_ Cr_z_inflate_fast_chunk_ 188 | 189 | /* Symbols added by crc32_simd.c */ 190 | #define crc32_sse42_simd_ Cr_z_crc32_sse42_simd_ 191 | 192 | /* Symbols added by armv8_crc32 */ 193 | #define arm_cpu_enable_crc32 Cr_z_arm_cpu_enable_crc32 194 | #define arm_cpu_enable_pmull Cr_z_arm_cpu_enable_pmull 195 | #define arm_check_features Cr_z_arm_check_features 196 | #define armv8_crc32_little Cr_z_armv8_crc32_little 197 | #define armv8_crc32_pmull_little Cr_z_armv8_crc32_pmull_little 198 | 199 | /* Symbols added by cpu_features.c */ 200 | #define cpu_check_features Cr_z_cpu_check_features 201 | #define x86_cpu_enable_sse2 Cr_z_x86_cpu_enable_sse2 202 | 203 | #endif /* THIRD_PARTY_ZLIB_CHROMECONF_H_ */ 204 | -------------------------------------------------------------------------------- /compress.c: -------------------------------------------------------------------------------- 1 | /* compress.c -- compress a memory buffer 2 | * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Compresses the source buffer into the destination buffer. The level 13 | parameter has the same meaning as in deflateInit. sourceLen is the byte 14 | length of the source buffer. Upon entry, destLen is the total size of the 15 | destination buffer, which must be at least 0.1% larger than sourceLen plus 16 | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. 17 | 18 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 19 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, 20 | Z_STREAM_ERROR if the level parameter is invalid. 21 | */ 22 | int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source, 23 | uLong sourceLen, int level) { 24 | z_stream stream; 25 | int err; 26 | const uInt max = (uInt)-1; 27 | uLong left; 28 | 29 | left = *destLen; 30 | *destLen = 0; 31 | 32 | stream.zalloc = (alloc_func)0; 33 | stream.zfree = (free_func)0; 34 | stream.opaque = (voidpf)0; 35 | 36 | err = deflateInit(&stream, level); 37 | if (err != Z_OK) return err; 38 | 39 | stream.next_out = dest; 40 | stream.avail_out = 0; 41 | stream.next_in = (z_const Bytef *)source; 42 | stream.avail_in = 0; 43 | 44 | do { 45 | if (stream.avail_out == 0) { 46 | stream.avail_out = left > (uLong)max ? max : (uInt)left; 47 | left -= stream.avail_out; 48 | } 49 | if (stream.avail_in == 0) { 50 | stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen; 51 | sourceLen -= stream.avail_in; 52 | } 53 | err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH); 54 | } while (err == Z_OK); 55 | 56 | *destLen = stream.total_out; 57 | deflateEnd(&stream); 58 | return err == Z_STREAM_END ? Z_OK : err; 59 | } 60 | 61 | /* =========================================================================== 62 | */ 63 | int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, 64 | uLong sourceLen) { 65 | return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); 66 | } 67 | 68 | /* =========================================================================== 69 | If the default memLevel or windowBits for deflateInit() is changed, then 70 | this function needs to be updated. 71 | */ 72 | uLong ZEXPORT compressBound(uLong sourceLen) { 73 | sourceLen = sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 74 | (sourceLen >> 25) + 13; 75 | /* FIXME(cavalcantii): usage of CRC32 Castagnoli as a hash function 76 | * for the hash table of symbols used for compression has a side effect 77 | * where for compression level [4, 5] it will increase the output buffer size 78 | * by 0.1% (i.e. less than 1%) for a high entropy input (i.e. random data). 79 | * To avoid a scenario where client code would fail, for safety we increase 80 | * the expected output size by 0.8% (i.e. 8x more than the worst scenario). 81 | * See: http://crbug.com/990489 82 | */ 83 | sourceLen += sourceLen >> 7; // Equivalent to 1.0078125 84 | return sourceLen; 85 | } 86 | -------------------------------------------------------------------------------- /contrib/bench/check.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Copyright 2022 The Chromium Authors 4 | # Use of this source code is governed by a BSD-style license that can be 5 | # found in the chromium source repository LICENSE file. 6 | # 7 | # Given a zlib_bench executable and some data files, run zlib_bench --check 8 | # over those data files, for all zlib types (gzip|zlib|raw) and compression 9 | # levels 1..9 for each type. Example: 10 | # 11 | # check.sh ./out/Release/zlib_bench [--check-binary] ~/snappy/testdata/* 12 | # 13 | # The --check-binary option modifies --check output: the compressed data is 14 | # also written to the program output. 15 | 16 | ZLIB_BENCH="$1" && shift 17 | 18 | CHECK_TYPE="--check" 19 | if [[ "${1}" == "--check-binary" ]]; then 20 | CHECK_TYPE="$1" && shift # output compressed data too 21 | fi 22 | 23 | DATA_FILES="$*" 24 | 25 | echo ${ZLIB_BENCH} | grep -E "/(zlib_bench|a.out)$" > /dev/null 26 | if [[ $? != 0 ]] || [[ -z "${DATA_FILES}" ]]; then 27 | echo "usage: check.sh zlib_bench [--check-binary] files ..." >&2 28 | exit 1; 29 | fi 30 | 31 | for type in gzip zlib raw; do 32 | for level in $(seq 1 9); do 33 | ${ZLIB_BENCH} $type --compression $level ${CHECK_TYPE} ${DATA_FILES} 34 | done 35 | done 36 | -------------------------------------------------------------------------------- /contrib/minizip/ChangeLogUnzip: -------------------------------------------------------------------------------- 1 | Change in 1.01e (12 feb 05) 2 | - Fix in zipOpen2 for globalcomment (Rolf Kalbermatter) 3 | - Fix possible memory leak in unzip.c (Zoran Stevanovic) 4 | 5 | Change in 1.01b (20 may 04) 6 | - Integrate patch from Debian package (submited by Mark Brown) 7 | - Add tools mztools from Xavier Roche 8 | 9 | Change in 1.01 (8 may 04) 10 | - fix buffer overrun risk in unzip.c (Xavier Roche) 11 | - fix a minor buffer insecurity in minizip.c (Mike Whittaker) 12 | 13 | Change in 1.00: (10 sept 03) 14 | - rename to 1.00 15 | - cosmetic code change 16 | 17 | Change in 0.22: (19 May 03) 18 | - crypting support (unless you define NOCRYPT) 19 | - append file in existing zipfile 20 | 21 | Change in 0.21: (10 Mar 03) 22 | - bug fixes 23 | 24 | Change in 0.17: (27 Jan 02) 25 | - bug fixes 26 | 27 | Change in 0.16: (19 Jan 02) 28 | - Support of ioapi for virtualize zip file access 29 | 30 | Change in 0.15: (19 Mar 98) 31 | - fix memory leak in minizip.c 32 | 33 | Change in 0.14: (10 Mar 98) 34 | - fix bugs in minizip.c sample for zipping big file 35 | - fix problem in month in date handling 36 | - fix bug in unzlocal_GetCurrentFileInfoInternal in unzip.c for 37 | comment handling 38 | 39 | Change in 0.13: (6 Mar 98) 40 | - fix bugs in zip.c 41 | - add real minizip sample 42 | 43 | Change in 0.12: (4 Mar 98) 44 | - add zip.c and zip.h for creates .zip file 45 | - fix change_file_date in miniunz.c for Unix (Jean-loup Gailly) 46 | - fix miniunz.c for file without specific record for directory 47 | 48 | Change in 0.11: (3 Mar 98) 49 | - fix bug in unzGetCurrentFileInfo for get extra field and comment 50 | - enhance miniunz sample, remove the bad unztst.c sample 51 | 52 | Change in 0.10: (2 Mar 98) 53 | - fix bug in unzReadCurrentFile 54 | - rename unzip* to unz* function and structure 55 | - remove Windows-like hungary notation variable name 56 | - modify some structure in unzip.h 57 | - add somes comment in source 58 | - remove unzipGetcCurrentFile function 59 | - replace ZUNZEXPORT by ZEXPORT 60 | - add unzGetLocalExtrafield for get the local extrafield info 61 | - add a new sample, miniunz.c 62 | 63 | Change in 0.4: (25 Feb 98) 64 | - suppress the type unzipFileInZip. 65 | Only on file in the zipfile can be open at the same time 66 | - fix somes typo in code 67 | - added tm_unz structure in unzip_file_info (date/time in readable format) 68 | -------------------------------------------------------------------------------- /contrib/minizip/Makefile: -------------------------------------------------------------------------------- 1 | CC=cc 2 | CFLAGS := $(CFLAGS) -O -I../.. 3 | 4 | UNZ_OBJS = miniunz.o unzip.o ioapi.o ../../libz.a 5 | ZIP_OBJS = minizip.o zip.o ioapi.o ../../libz.a 6 | 7 | .c.o: 8 | $(CC) -c $(CFLAGS) $*.c 9 | 10 | all: miniunz minizip 11 | 12 | miniunz: $(UNZ_OBJS) 13 | $(CC) $(CFLAGS) -o $@ $(UNZ_OBJS) 14 | 15 | minizip: $(ZIP_OBJS) 16 | $(CC) $(CFLAGS) -o $@ $(ZIP_OBJS) 17 | 18 | test: miniunz minizip 19 | @rm -f test.* 20 | @echo hello hello hello > test.txt 21 | ./minizip test test.txt 22 | ./miniunz -l test.zip 23 | @mv test.txt test.old 24 | ./miniunz test.zip 25 | @cmp test.txt test.old 26 | @rm -f test.* 27 | 28 | clean: 29 | /bin/rm -f *.o *~ minizip miniunz test.* 30 | -------------------------------------------------------------------------------- /contrib/minizip/README.chromium: -------------------------------------------------------------------------------- 1 | Name: ZIP file API for reading file entries in a ZIP archive 2 | Short Name: minizip 3 | URL: https://github.com/madler/zlib/tree/master/contrib/minizip 4 | Version: 1.3.0.1 5 | License: Zlib 6 | License File: //third_party/zlib/LICENSE 7 | Security Critical: yes 8 | Shipped: yes 9 | CPEPrefix: cpe:/a:minizip_project:minizip 10 | 11 | Description: 12 | Minizip provides API on top of zlib that can enumerate and extract ZIP archive 13 | files. See minizip.md for chromium build instructions. 14 | 15 | Local Modifications: 16 | - Fixed uncompressing files with wrong uncompressed size set 17 | crrev.com/268940 18 | 0014-minizip-unzip-with-incorrect-size.patch 19 | 20 | - Enable traditional PKWARE decryption in zlib/contrib/minizip 21 | Correct the value of rest_read_compressed when decompressing an encrypted 22 | zip. (crrev.com/580862) 23 | 0015-minizip-unzip-enable-decryption.patch 24 | 25 | - Add parsing of the 'Info-ZIP Unicode Path Extra Field' as described in 26 | https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT section 4.6.9. 27 | (see crrev.com/1002476) 28 | 0016-minizip-parse-unicode-path-extra-field.patch 29 | -------------------------------------------------------------------------------- /contrib/minizip/crypt.h: -------------------------------------------------------------------------------- 1 | /* crypt.h -- base code for crypt/uncrypt ZIPfile 2 | 3 | 4 | Version 1.01e, February 12th, 2005 5 | 6 | Copyright (C) 1998-2005 Gilles Vollant 7 | 8 | This code is a modified version of crypting code in Infozip distribution 9 | 10 | The encryption/decryption parts of this source code (as opposed to the 11 | non-echoing password parts) were originally written in Europe. The 12 | whole source package can be freely distributed, including from the USA. 13 | (Prior to January 2000, re-export from the US was a violation of US law.) 14 | 15 | This encryption code is a direct transcription of the algorithm from 16 | Roger Schlafly, described by Phil Katz in the file appnote.txt. This 17 | file (appnote.txt) is distributed with the PKZIP program (even in the 18 | version without encryption capabilities). 19 | 20 | If you don't need crypting in your application, just define symbols 21 | NOCRYPT and NOUNCRYPT. 22 | 23 | This code support the "Traditional PKWARE Encryption". 24 | 25 | The new AES encryption added on Zip format by Winzip (see the page 26 | http://www.winzip.com/aes_info.htm ) and PKWare PKZip 5.x Strong 27 | Encryption is not supported. 28 | */ 29 | 30 | #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8)) 31 | 32 | /*********************************************************************** 33 | * Return the next byte in the pseudo-random sequence 34 | */ 35 | static int decrypt_byte(unsigned long* pkeys, const z_crc_t* pcrc_32_tab) { 36 | unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an 37 | * unpredictable manner on 16-bit systems; not a problem 38 | * with any known compiler so far, though */ 39 | 40 | (void)pcrc_32_tab; 41 | temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2; 42 | return (int)(((temp * (temp ^ 1)) >> 8) & 0xff); 43 | } 44 | 45 | /*********************************************************************** 46 | * Update the encryption keys with the next byte of plain text 47 | */ 48 | static int update_keys(unsigned long* pkeys, const z_crc_t* pcrc_32_tab, int c) { 49 | (*(pkeys+0)) = CRC32((*(pkeys+0)), c); 50 | (*(pkeys+1)) += (*(pkeys+0)) & 0xff; 51 | (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1; 52 | { 53 | register int keyshift = (int)((*(pkeys+1)) >> 24); 54 | (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift); 55 | } 56 | return c; 57 | } 58 | 59 | 60 | /*********************************************************************** 61 | * Initialize the encryption keys and the random header according to 62 | * the given password. 63 | */ 64 | static void init_keys(const char* passwd, unsigned long* pkeys, const z_crc_t* pcrc_32_tab) { 65 | *(pkeys+0) = 305419896L; 66 | *(pkeys+1) = 591751049L; 67 | *(pkeys+2) = 878082192L; 68 | while (*passwd != '\0') { 69 | update_keys(pkeys,pcrc_32_tab,(int)*passwd); 70 | passwd++; 71 | } 72 | } 73 | 74 | #define zdecode(pkeys,pcrc_32_tab,c) \ 75 | (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab))) 76 | 77 | #define zencode(pkeys,pcrc_32_tab,c,t) \ 78 | (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), (Byte)t^(c)) 79 | 80 | #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED 81 | 82 | #define RAND_HEAD_LEN 12 83 | /* "last resort" source for second part of crypt seed pattern */ 84 | # ifndef ZCR_SEED2 85 | # define ZCR_SEED2 3141592654UL /* use PI as default pattern */ 86 | # endif 87 | 88 | static unsigned crypthead(const char* passwd, /* password string */ 89 | unsigned char* buf, /* where to write header */ 90 | int bufSize, 91 | unsigned long* pkeys, 92 | const z_crc_t* pcrc_32_tab, 93 | unsigned long crcForCrypting) { 94 | unsigned n; /* index in random header */ 95 | int t; /* temporary */ 96 | int c; /* random byte */ 97 | unsigned char header[RAND_HEAD_LEN-2]; /* random header */ 98 | static unsigned calls = 0; /* ensure different random header each time */ 99 | 100 | if (bufSize> 7) & 0xff; 115 | header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t); 116 | } 117 | /* Encrypt random header (last two bytes is high word of crc) */ 118 | init_keys(passwd, pkeys, pcrc_32_tab); 119 | for (n = 0; n < RAND_HEAD_LEN-2; n++) 120 | { 121 | buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t); 122 | } 123 | buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t); 124 | buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t); 125 | return n; 126 | } 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /contrib/minizip/ioapi.h: -------------------------------------------------------------------------------- 1 | /* ioapi.h -- IO base function header for compress/uncompress .zip 2 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 3 | 4 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 5 | 6 | Modifications for Zip64 support 7 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 8 | 9 | For more info read MiniZip_info.txt 10 | 11 | Changes 12 | 13 | Oct-2009 - Defined ZPOS64_T to fpos_t on windows and u_int64_t on linux. (might need to find a better why for this) 14 | Oct-2009 - Change to fseeko64, ftello64 and fopen64 so large files would work on linux. 15 | More if/def section may be needed to support other platforms 16 | Oct-2009 - Defined fxxxx64 calls to normal fopen/ftell/fseek so they would compile on windows. 17 | (but you should use iowin32.c for windows instead) 18 | 19 | */ 20 | 21 | #ifndef _ZLIBIOAPI64_H 22 | #define _ZLIBIOAPI64_H 23 | 24 | #if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) 25 | 26 | // Linux needs this to support file operation on files larger then 4+GB 27 | // But might need better if/def to select just the platforms that needs them. 28 | 29 | #ifndef __USE_FILE_OFFSET64 30 | #define __USE_FILE_OFFSET64 31 | #endif 32 | #ifndef __USE_LARGEFILE64 33 | #define __USE_LARGEFILE64 34 | #endif 35 | #ifndef _LARGEFILE64_SOURCE 36 | #define _LARGEFILE64_SOURCE 37 | #endif 38 | #ifndef _FILE_OFFSET_BIT 39 | #define _FILE_OFFSET_BIT 64 40 | #endif 41 | 42 | #endif 43 | 44 | #include 45 | #include 46 | #include "zlib.h" 47 | 48 | #if defined(USE_FILE32API) 49 | #define fopen64 fopen 50 | #define ftello64 ftell 51 | #define fseeko64 fseek 52 | #else 53 | #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) 54 | #define fopen64 fopen 55 | #define ftello64 ftello 56 | #define fseeko64 fseeko 57 | #endif 58 | #ifdef _MSC_VER 59 | #define fopen64 fopen 60 | #if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC))) 61 | #define ftello64 _ftelli64 62 | #define fseeko64 _fseeki64 63 | #else // old MSC 64 | #define ftello64 ftell 65 | #define fseeko64 fseek 66 | #endif 67 | #endif 68 | #endif 69 | 70 | /* 71 | #ifndef ZPOS64_T 72 | #ifdef _WIN32 73 | #define ZPOS64_T fpos_t 74 | #else 75 | #include 76 | #define ZPOS64_T uint64_t 77 | #endif 78 | #endif 79 | */ 80 | 81 | #ifdef HAVE_MINIZIP64_CONF_H 82 | #include "mz64conf.h" 83 | #endif 84 | 85 | /* a type chosen by DEFINE */ 86 | #ifdef HAVE_64BIT_INT_CUSTOM 87 | typedef 64BIT_INT_CUSTOM_TYPE ZPOS64_T; 88 | #else 89 | #ifdef HAS_STDINT_H 90 | #include "stdint.h" 91 | typedef uint64_t ZPOS64_T; 92 | #else 93 | 94 | 95 | 96 | #if defined(_MSC_VER) || defined(__BORLANDC__) 97 | typedef unsigned __int64 ZPOS64_T; 98 | #else 99 | typedef unsigned long long int ZPOS64_T; 100 | #endif 101 | #endif 102 | #endif 103 | 104 | /* Maximum unsigned 32-bit value used as placeholder for zip64 */ 105 | #ifndef MAXU32 106 | #define MAXU32 (0xffffffff) 107 | #endif 108 | 109 | #ifdef __cplusplus 110 | extern "C" { 111 | #endif 112 | 113 | 114 | #define ZLIB_FILEFUNC_SEEK_CUR (1) 115 | #define ZLIB_FILEFUNC_SEEK_END (2) 116 | #define ZLIB_FILEFUNC_SEEK_SET (0) 117 | 118 | #define ZLIB_FILEFUNC_MODE_READ (1) 119 | #define ZLIB_FILEFUNC_MODE_WRITE (2) 120 | #define ZLIB_FILEFUNC_MODE_READWRITEFILTER (3) 121 | 122 | #define ZLIB_FILEFUNC_MODE_EXISTING (4) 123 | #define ZLIB_FILEFUNC_MODE_CREATE (8) 124 | 125 | 126 | #ifndef ZCALLBACK 127 | #if (defined(WIN32) || defined(_WIN32) || defined (WINDOWS) || defined (_WINDOWS)) && defined(CALLBACK) && defined (USEWINDOWS_CALLBACK) 128 | #define ZCALLBACK CALLBACK 129 | #else 130 | #define ZCALLBACK 131 | #endif 132 | #endif 133 | 134 | 135 | 136 | 137 | typedef voidpf (ZCALLBACK *open_file_func) (voidpf opaque, const char* filename, int mode); 138 | typedef uLong (ZCALLBACK *read_file_func) (voidpf opaque, voidpf stream, void* buf, uLong size); 139 | typedef uLong (ZCALLBACK *write_file_func) (voidpf opaque, voidpf stream, const void* buf, uLong size); 140 | typedef int (ZCALLBACK *close_file_func) (voidpf opaque, voidpf stream); 141 | typedef int (ZCALLBACK *testerror_file_func) (voidpf opaque, voidpf stream); 142 | 143 | typedef long (ZCALLBACK *tell_file_func) (voidpf opaque, voidpf stream); 144 | typedef long (ZCALLBACK *seek_file_func) (voidpf opaque, voidpf stream, uLong offset, int origin); 145 | 146 | 147 | /* here is the "old" 32 bits structure */ 148 | typedef struct zlib_filefunc_def_s 149 | { 150 | open_file_func zopen_file; 151 | read_file_func zread_file; 152 | write_file_func zwrite_file; 153 | tell_file_func ztell_file; 154 | seek_file_func zseek_file; 155 | close_file_func zclose_file; 156 | testerror_file_func zerror_file; 157 | voidpf opaque; 158 | } zlib_filefunc_def; 159 | 160 | typedef ZPOS64_T (ZCALLBACK *tell64_file_func) (voidpf opaque, voidpf stream); 161 | typedef long (ZCALLBACK *seek64_file_func) (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin); 162 | typedef voidpf (ZCALLBACK *open64_file_func) (voidpf opaque, const void* filename, int mode); 163 | 164 | typedef struct zlib_filefunc64_def_s 165 | { 166 | open64_file_func zopen64_file; 167 | read_file_func zread_file; 168 | write_file_func zwrite_file; 169 | tell64_file_func ztell64_file; 170 | seek64_file_func zseek64_file; 171 | close_file_func zclose_file; 172 | testerror_file_func zerror_file; 173 | voidpf opaque; 174 | } zlib_filefunc64_def; 175 | 176 | void fill_fopen64_filefunc(zlib_filefunc64_def* pzlib_filefunc_def); 177 | void fill_fopen_filefunc(zlib_filefunc_def* pzlib_filefunc_def); 178 | 179 | /* now internal definition, only for zip.c and unzip.h */ 180 | typedef struct zlib_filefunc64_32_def_s 181 | { 182 | zlib_filefunc64_def zfile_func64; 183 | open_file_func zopen32_file; 184 | tell_file_func ztell32_file; 185 | seek_file_func zseek32_file; 186 | } zlib_filefunc64_32_def; 187 | 188 | 189 | #define ZREAD64(filefunc,filestream,buf,size) ((*((filefunc).zfile_func64.zread_file)) ((filefunc).zfile_func64.opaque,filestream,buf,size)) 190 | #define ZWRITE64(filefunc,filestream,buf,size) ((*((filefunc).zfile_func64.zwrite_file)) ((filefunc).zfile_func64.opaque,filestream,buf,size)) 191 | //#define ZTELL64(filefunc,filestream) ((*((filefunc).ztell64_file)) ((filefunc).opaque,filestream)) 192 | //#define ZSEEK64(filefunc,filestream,pos,mode) ((*((filefunc).zseek64_file)) ((filefunc).opaque,filestream,pos,mode)) 193 | #define ZCLOSE64(filefunc,filestream) ((*((filefunc).zfile_func64.zclose_file)) ((filefunc).zfile_func64.opaque,filestream)) 194 | #define ZERROR64(filefunc,filestream) ((*((filefunc).zfile_func64.zerror_file)) ((filefunc).zfile_func64.opaque,filestream)) 195 | 196 | voidpf call_zopen64(const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode); 197 | long call_zseek64(const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin); 198 | ZPOS64_T call_ztell64(const zlib_filefunc64_32_def* pfilefunc,voidpf filestream); 199 | 200 | void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32); 201 | 202 | #define ZOPEN64(filefunc,filename,mode) (call_zopen64((&(filefunc)),(filename),(mode))) 203 | #define ZTELL64(filefunc,filestream) (call_ztell64((&(filefunc)),(filestream))) 204 | #define ZSEEK64(filefunc,filestream,pos,mode) (call_zseek64((&(filefunc)),(filestream),(pos),(mode))) 205 | 206 | #ifdef __cplusplus 207 | } 208 | #endif 209 | 210 | #endif 211 | -------------------------------------------------------------------------------- /contrib/minizip/iowin32.h: -------------------------------------------------------------------------------- 1 | /* iowin32.h -- IO base function header for compress/uncompress .zip 2 | Version 1.1, February 14h, 2010 3 | part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) 4 | 5 | Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) 6 | 7 | Modifications for Zip64 support 8 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 9 | 10 | For more info read MiniZip_info.txt 11 | 12 | */ 13 | 14 | #include 15 | 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | void fill_win32_filefunc(zlib_filefunc_def* pzlib_filefunc_def); 22 | void fill_win32_filefunc64(zlib_filefunc64_def* pzlib_filefunc_def); 23 | void fill_win32_filefunc64A(zlib_filefunc64_def* pzlib_filefunc_def); 24 | void fill_win32_filefunc64W(zlib_filefunc64_def* pzlib_filefunc_def); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | -------------------------------------------------------------------------------- /contrib/minizip/minizip.md: -------------------------------------------------------------------------------- 1 | Minizip is a library provided by //third_party/zlib [1]. Its zip and unzip 2 | tools can be built in a developer checkout for testing purposes with: 3 | 4 | ```shell 5 | autoninja -C out/Release minizip_bin 6 | autoninja -C out/Release miniunz_bin 7 | ``` 8 | 9 | [1] Upstream is https://github.com/madler/zlib/tree/master/contrib/minizip 10 | -------------------------------------------------------------------------------- /contrib/minizip/mztools.h: -------------------------------------------------------------------------------- 1 | /* 2 | Additional tools for Minizip 3 | Code: Xavier Roche '2004 4 | License: Same as ZLIB (www.gzip.org) 5 | */ 6 | 7 | #ifndef _zip_tools_H 8 | #define _zip_tools_H 9 | 10 | #ifdef __cplusplus 11 | extern "C" { 12 | #endif 13 | 14 | #ifndef _ZLIB_H 15 | #include "zlib.h" 16 | #endif 17 | 18 | #include "unzip.h" 19 | 20 | /* Repair a ZIP file (missing central directory) 21 | file: file to recover 22 | fileOut: output file after recovery 23 | fileOutTmp: temporary file name used for recovery 24 | */ 25 | extern int ZEXPORT unzRepair(const char* file, 26 | const char* fileOut, 27 | const char* fileOutTmp, 28 | uLong* nRecovered, 29 | uLong* bytesRecovered); 30 | 31 | 32 | #ifdef __cplusplus 33 | } 34 | #endif 35 | 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /contrib/optimizations/inffast_chunk.h: -------------------------------------------------------------------------------- 1 | /* inffast_chunk.h -- header to use inffast_chunk.c 2 | * Copyright (C) 1995-2003, 2010 Mark Adler 3 | * Copyright (C) 2017 ARM, Inc. 4 | * Copyright 2023 The Chromium Authors 5 | * For conditions of distribution and use, see copyright notice in zlib.h 6 | */ 7 | 8 | /* WARNING: this file should *not* be used by applications. It is 9 | part of the implementation of the compression library and is 10 | subject to change. Applications should only use zlib.h. 11 | */ 12 | 13 | #include "inffast.h" 14 | 15 | /* INFLATE_FAST_MIN_INPUT: 16 | The minimum number of input bytes needed so that we can safely call 17 | inflate_fast() with only one up-front bounds check. One 18 | length/distance code pair (15 bits for the length code, 5 bits for length 19 | extra, 15 bits for the distance code, 13 bits for distance extra) requires 20 | reading up to 48 input bits. Additionally, in the same iteraction, we may 21 | decode two literals from the root-table (requiring MIN_OUTPUT = 258 + 2). 22 | 23 | Each root-table entry is up to 10 bits, for a total of 68 input bits each 24 | iteraction. 25 | 26 | The refill variant reads 8 bytes from the buffer at a time, and advances 27 | the input pointer by up to 7 bytes, ensuring there are at least 56-bits 28 | available in the bit-buffer. The technique was documented by Fabian Giesen 29 | on his blog as variant 4 in the article 'Reading bits in far too many ways': 30 | https://fgiesen.wordpress.com/2018/02/20/ 31 | 32 | In the worst case, we may refill twice in the same iteraction, requiring 33 | MIN_INPUT = 8 + 7. 34 | */ 35 | #ifdef INFLATE_CHUNK_READ_64LE 36 | #undef INFLATE_FAST_MIN_INPUT 37 | #define INFLATE_FAST_MIN_INPUT 15 38 | #undef INFLATE_FAST_MIN_OUTPUT 39 | #define INFLATE_FAST_MIN_OUTPUT 260 40 | #endif 41 | 42 | void ZLIB_INTERNAL inflate_fast_chunk_(z_streamp strm, unsigned start); 43 | -------------------------------------------------------------------------------- /contrib/optimizations/insert_string.h: -------------------------------------------------------------------------------- 1 | /* insert_string.h 2 | * 3 | * Copyright 2019 The Chromium Authors 4 | * Use of this source code is governed by a BSD-style license that can be 5 | * found in the Chromium source repository LICENSE file. 6 | */ 7 | 8 | #ifndef INSERT_STRING_H 9 | #define INSERT_STRING_H 10 | 11 | #ifndef INLINE 12 | #if defined(_MSC_VER) && !defined(__clang__) 13 | #define INLINE __inline 14 | #else 15 | #define INLINE inline 16 | #endif 17 | #endif 18 | 19 | #include 20 | 21 | /** 22 | * Some applications need to match zlib DEFLATE output exactly [3]. Use the 23 | * canonical zlib Rabin-Karp rolling hash [1,2] in that case. 24 | * 25 | * [1] For a description of the Rabin and Karp algorithm, see "Algorithms" 26 | * book by R. Sedgewick, Addison-Wesley, p252. 27 | * [2] https://www.euccas.me/zlib/#zlib_rabin_karp and also "rolling hash" 28 | * https://en.wikipedia.org/wiki/Rolling_hash 29 | * [3] crbug.com/1316541 AOSP incremental client APK package OTA upgrades. 30 | */ 31 | #ifdef CHROMIUM_ZLIB_NO_CASTAGNOLI 32 | #define USE_ZLIB_RABIN_KARP_ROLLING_HASH 33 | #endif 34 | 35 | /* =========================================================================== 36 | * Update a hash value with the given input byte (Rabin-Karp rolling hash). 37 | * IN assertion: all calls to UPDATE_HASH are made with consecutive input 38 | * characters, so that a running hash key can be computed from the previous 39 | * key instead of complete recalculation each time. 40 | */ 41 | #define UPDATE_HASH(s, h, c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask) 42 | 43 | /* =========================================================================== 44 | * Insert string str in the dictionary and set match_head to the previous head 45 | * of the hash chain (the most recent string with same hash key). Return 46 | * the previous length of the hash chain. 47 | * If this file is compiled with -DFASTEST, the compression level is forced 48 | * to 1, and no hash chains are maintained. 49 | * IN assertion: all calls to INSERT_STRING are made with consecutive input 50 | * characters and the first MIN_MATCH bytes of str are valid (except for 51 | * the last MIN_MATCH-1 bytes of the input file). 52 | */ 53 | local INLINE Pos insert_string(deflate_state* const s, const Pos str) { 54 | Pos ret; 55 | /* insert_string dictionary insertion: ANZAC++ hasher 56 | * significantly improves data compression speed. 57 | * 58 | * Note: the generated compressed output is a valid DEFLATE stream, but will 59 | * differ from canonical zlib output. 60 | */ 61 | #if defined(USE_ZLIB_RABIN_KARP_ROLLING_HASH) 62 | UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH - 1)]); 63 | #else 64 | uint32_t value; 65 | // Validated for little endian archs (i.e. x86, Arm). YMMV for big endian. 66 | zmemcpy(&value, &s->window[str], sizeof(value)); 67 | s->ins_h = ((value * 66521 + 66521) >> 16) & s->hash_mask; 68 | #endif 69 | 70 | #ifdef FASTEST 71 | ret = s->head[s->ins_h]; 72 | #else 73 | ret = s->prev[str & s->w_mask] = s->head[s->ins_h]; 74 | #endif 75 | s->head[s->ins_h] = str; 76 | 77 | return ret; 78 | } 79 | 80 | #endif /* INSERT_STRING_H */ 81 | -------------------------------------------------------------------------------- /contrib/tests/DEPS: -------------------------------------------------------------------------------- 1 | include_rules = [ 2 | "+testing/gtest", 3 | "+third_party/zlib/contrib/minizip", 4 | "+base", 5 | ] 6 | -------------------------------------------------------------------------------- /contrib/tests/OWNERS: -------------------------------------------------------------------------------- 1 | cblume@chromium.org 2 | cavalcantii@chromium.org 3 | -------------------------------------------------------------------------------- /contrib/tests/fuzzers/BUILD.gn: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | 5 | import("//testing/libfuzzer/fuzzer_test.gni") 6 | 7 | # root BUILD depends on this target. Needed for package discovery 8 | group("fuzzers") { 9 | } 10 | 11 | fuzzer_test("zlib_uncompress_fuzzer") { 12 | sources = [ "uncompress_fuzzer.cc" ] 13 | deps = [ "../../../:zlib" ] 14 | } 15 | 16 | fuzzer_test("zlib_inflate_fuzzer") { 17 | sources = [ "inflate_fuzzer.cc" ] 18 | deps = [ "../../../:zlib" ] 19 | } 20 | 21 | fuzzer_test("zlib_inflate_with_header_fuzzer") { 22 | sources = [ "inflate_with_header_fuzzer.cc" ] 23 | deps = [ "../../../:zlib" ] 24 | } 25 | 26 | fuzzer_test("zlib_streaming_inflate_fuzzer") { 27 | sources = [ "streaming_inflate_fuzzer.cc" ] 28 | deps = [ "../../../:zlib" ] 29 | libfuzzer_options = [ "max_len=256000" ] 30 | } 31 | 32 | fuzzer_test("zlib_deflate_set_dictionary_fuzzer") { 33 | sources = [ "deflate_set_dictionary_fuzzer.cc" ] 34 | deps = [ "../../../:zlib" ] 35 | } 36 | 37 | fuzzer_test("zlib_deflate_fuzzer") { 38 | sources = [ "deflate_fuzzer.cc" ] 39 | deps = [ "../../../:zlib" ] 40 | } 41 | -------------------------------------------------------------------------------- /contrib/tests/fuzzers/OWNERS: -------------------------------------------------------------------------------- 1 | cblume@chromium.org 2 | hans@chromium.org 3 | -------------------------------------------------------------------------------- /contrib/tests/fuzzers/deflate_fuzzer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "zlib.h" 14 | 15 | // Fuzzer builds often have NDEBUG set, so roll our own assert macro. 16 | #define ASSERT(cond) \ 17 | do { \ 18 | if (!(cond)) { \ 19 | fprintf(stderr, "%s:%d Assert failed: %s\n", __FILE__, __LINE__, #cond); \ 20 | exit(1); \ 21 | } \ 22 | } while (0) 23 | 24 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 25 | FuzzedDataProvider fdp(data, size); 26 | int level = fdp.PickValueInArray({-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); 27 | int windowBits = fdp.PickValueInArray({9, 10, 11, 12, 13, 14, 15}); 28 | int memLevel = fdp.PickValueInArray({1, 2, 3, 4, 5, 6, 7, 8, 9}); 29 | int strategy = fdp.PickValueInArray( 30 | {Z_DEFAULT_STRATEGY, Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE, Z_FIXED}); 31 | 32 | if (fdp.ConsumeBool()) { 33 | // Gzip wrapper. 34 | windowBits += 16; 35 | } else if (fdp.ConsumeBool()) { 36 | // Raw deflate. 37 | windowBits *= -1; 38 | } else { 39 | // Default: zlib wrapper. 40 | } 41 | 42 | std::vector src; 43 | std::vector compressed; 44 | static const int kMinChunk = 1; 45 | static const int kMaxChunk = 512 * 1024; 46 | 47 | z_stream stream; 48 | stream.zalloc = Z_NULL; 49 | stream.zfree = Z_NULL; 50 | int ret = 51 | deflateInit2(&stream, level, Z_DEFLATED, windowBits, memLevel, strategy); 52 | ASSERT(ret == Z_OK); 53 | 54 | // Stream with random-sized input and output buffers. 55 | while (fdp.ConsumeBool()) { 56 | if (fdp.ConsumeBool()) { 57 | // Check that copying the stream's state works. Gating this behind 58 | // ConsumeBool() allows to interleave deflateCopy() with deflate() calls 59 | // to better stress the code. 60 | z_stream stream2; 61 | ASSERT(deflateCopy(&stream2, &stream) == Z_OK); 62 | ret = deflateEnd(&stream); 63 | ASSERT(ret == Z_OK || Z_DATA_ERROR); 64 | memset(&stream, 0xff, sizeof(stream)); 65 | 66 | ASSERT(deflateCopy(&stream, &stream2) == Z_OK); 67 | ret = deflateEnd(&stream2); 68 | ASSERT(ret == Z_OK || Z_DATA_ERROR); 69 | } 70 | 71 | std::vector src_chunk = fdp.ConsumeBytes( 72 | fdp.ConsumeIntegralInRange(kMinChunk, kMaxChunk)); 73 | std::vector out_chunk( 74 | fdp.ConsumeIntegralInRange(kMinChunk, kMaxChunk)); 75 | stream.next_in = src_chunk.data(); 76 | stream.avail_in = src_chunk.size(); 77 | stream.next_out = out_chunk.data(); 78 | stream.avail_out = out_chunk.size(); 79 | ret = deflate(&stream, Z_NO_FLUSH); 80 | ASSERT(ret == Z_OK || ret == Z_BUF_ERROR); 81 | 82 | src.insert(src.end(), src_chunk.begin(), src_chunk.end() - stream.avail_in); 83 | compressed.insert(compressed.end(), out_chunk.begin(), 84 | out_chunk.end() - stream.avail_out); 85 | } 86 | // Finish up. 87 | while (true) { 88 | std::vector out_chunk( 89 | fdp.ConsumeIntegralInRange(kMinChunk, kMaxChunk)); 90 | stream.next_in = Z_NULL; 91 | stream.avail_in = 0; 92 | stream.next_out = out_chunk.data(); 93 | stream.avail_out = out_chunk.size(); 94 | ret = deflate(&stream, Z_FINISH); 95 | compressed.insert(compressed.end(), out_chunk.begin(), 96 | out_chunk.end() - stream.avail_out); 97 | if (ret == Z_STREAM_END) { 98 | break; 99 | } 100 | ASSERT(ret == Z_OK || Z_BUF_ERROR); 101 | } 102 | deflateEnd(&stream); 103 | 104 | // Check deflateBound(). 105 | // Use a newly initialized stream since computing the bound on a "used" stream 106 | // may not yield a correct result (https://github.com/madler/zlib/issues/944). 107 | z_stream bound_stream; 108 | bound_stream.zalloc = Z_NULL; 109 | bound_stream.zfree = Z_NULL; 110 | ret = deflateInit2(&bound_stream, level, Z_DEFLATED, windowBits, memLevel, 111 | strategy); 112 | ASSERT(ret == Z_OK); 113 | size_t deflate_bound = deflateBound(&bound_stream, src.size()); 114 | ASSERT(compressed.size() <= deflate_bound); 115 | deflateEnd(&bound_stream); 116 | 117 | // Verify that the data decompresses correctly. 118 | ret = inflateInit2(&stream, windowBits); 119 | ASSERT(ret == Z_OK); 120 | // Make room for at least one byte so it's never empty. 121 | std::vector decompressed(src.size() + 1); 122 | stream.next_in = compressed.data(); 123 | stream.avail_in = compressed.size(); 124 | stream.next_out = decompressed.data(); 125 | stream.avail_out = decompressed.size(); 126 | ret = inflate(&stream, Z_FINISH); 127 | ASSERT(ret == Z_STREAM_END); 128 | decompressed.resize(decompressed.size() - stream.avail_out); 129 | inflateEnd(&stream); 130 | 131 | ASSERT(decompressed == src); 132 | 133 | return 0; 134 | } 135 | -------------------------------------------------------------------------------- /contrib/tests/fuzzers/deflate_set_dictionary_fuzzer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "zlib.h" 11 | 12 | static Bytef buffer[256 * 1024] = {0}; 13 | 14 | // Entry point for LibFuzzer. 15 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 16 | // We need to strip the 'const' for zlib. 17 | std::vector input_buffer{data, data + size}; 18 | 19 | uLongf buffer_length = static_cast(sizeof(buffer)); 20 | 21 | z_stream stream; 22 | stream.next_in = input_buffer.data(); 23 | stream.avail_in = size; 24 | stream.total_in = size; 25 | stream.next_out = buffer; 26 | stream.avail_out = buffer_length; 27 | stream.total_out = buffer_length; 28 | stream.zalloc = Z_NULL; 29 | stream.zfree = Z_NULL; 30 | 31 | if (Z_OK != deflateInit(&stream, Z_DEFAULT_COMPRESSION)) { 32 | deflateEnd(&stream); 33 | assert(false); 34 | } 35 | 36 | auto deflate_set_dictionary_result = 37 | deflateSetDictionary(&stream, data, size); 38 | deflateEnd(&stream); 39 | if (Z_OK != deflate_set_dictionary_result) 40 | assert(false); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /contrib/tests/fuzzers/inflate_fuzzer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "zlib.h" 12 | 13 | static Bytef buffer[256 * 1024] = {0}; 14 | 15 | // Entry point for LibFuzzer. 16 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 17 | // We need to strip the 'const' for zlib 18 | std::vector input_buffer{data, data+size}; 19 | 20 | uLongf buffer_length = static_cast(sizeof(buffer)); 21 | 22 | z_stream stream; 23 | stream.next_in = input_buffer.data(); 24 | stream.avail_in = size; 25 | stream.total_in = size; 26 | stream.next_out = buffer; 27 | stream.avail_out = buffer_length; 28 | stream.total_out = buffer_length; 29 | stream.zalloc = Z_NULL; 30 | stream.zfree = Z_NULL; 31 | 32 | if (Z_OK != inflateInit(&stream)) { 33 | inflateEnd(&stream); 34 | assert(false); 35 | } 36 | 37 | inflate(&stream, Z_NO_FLUSH); 38 | inflateEnd(&stream); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /contrib/tests/fuzzers/inflate_with_header_fuzzer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | 15 | #include "zlib.h" 16 | 17 | // Fuzzer builds often have NDEBUG set, so roll our own assert macro. 18 | #define ASSERT(cond) \ 19 | do { \ 20 | if (!(cond)) { \ 21 | fprintf(stderr, "%s:%d Assert failed: %s\n", __FILE__, __LINE__, #cond); \ 22 | exit(1); \ 23 | } \ 24 | } while (0) 25 | 26 | static void chunked_inflate(gz_header* header, 27 | uint8_t* data, 28 | size_t size, 29 | size_t in_chunk_size, 30 | size_t out_chunk_size) { 31 | z_stream stream; 32 | stream.next_in = data; 33 | stream.avail_in = 0; 34 | stream.zalloc = Z_NULL; 35 | stream.zfree = Z_NULL; 36 | 37 | static const int kDefaultWindowBits = MAX_WBITS; 38 | static const int kGzipOrZlibHeader = 32; 39 | ASSERT(inflateInit2(&stream, kDefaultWindowBits + kGzipOrZlibHeader) == Z_OK); 40 | ASSERT(inflateGetHeader(&stream, header) == Z_OK); 41 | 42 | auto out_buffer = std::make_unique(out_chunk_size); 43 | while (true) { 44 | stream.next_in = &data[stream.total_in]; 45 | stream.avail_in = 46 | std::min(in_chunk_size, size - static_cast(stream.total_in)); 47 | stream.next_out = out_buffer.get(); 48 | stream.avail_out = out_chunk_size; 49 | 50 | if (inflate(&stream, stream.avail_in == 0 ? Z_SYNC_FLUSH : Z_NO_FLUSH) != 51 | Z_OK) { 52 | break; 53 | } 54 | } 55 | 56 | inflateEnd(&stream); 57 | } 58 | 59 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 60 | if (size > 250 * 1024) { 61 | // Cap the input size so the fuzzer doesn't time out. For example, 62 | // crbug.com/1362206 saw timeouts with 450 KB input, so use a limit that's 63 | // well below that but still large enough to hit most code. 64 | return 0; 65 | } 66 | 67 | FuzzedDataProvider fdp(data, size); 68 | 69 | // Fuzz zlib's inflate() with inflateGetHeader() enabled, various sizes for 70 | // the gz_header field sizes, and various-sized chunks for input/output. This 71 | // would have found CVE-2022-37434 which was a heap buffer read overflow when 72 | // filling in gz_header's extra field. 73 | 74 | gz_header header; 75 | header.extra_max = fdp.ConsumeIntegralInRange(0, 100000); 76 | header.name_max = fdp.ConsumeIntegralInRange(0, 100000); 77 | header.comm_max = fdp.ConsumeIntegralInRange(0, 100000); 78 | 79 | auto extra_buf = std::make_unique(header.extra_max); 80 | auto name_buf = std::make_unique(header.name_max); 81 | auto comment_buf = std::make_unique(header.comm_max); 82 | 83 | header.extra = extra_buf.get(); 84 | header.name = name_buf.get(); 85 | header.comment = comment_buf.get(); 86 | 87 | size_t in_chunk_size = fdp.ConsumeIntegralInRange(1, 4097); 88 | size_t out_chunk_size = fdp.ConsumeIntegralInRange(1, 4097); 89 | std::vector remaining_data = fdp.ConsumeRemainingBytes(); 90 | 91 | chunked_inflate(&header, remaining_data.data(), remaining_data.size(), 92 | in_chunk_size, out_chunk_size); 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /contrib/tests/fuzzers/streaming_inflate_fuzzer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "zlib.h" 11 | 12 | // Fuzzer builds often have NDEBUG set, so roll our own assert macro. 13 | #define ASSERT(cond) \ 14 | do { \ 15 | if (!(cond)) { \ 16 | fprintf(stderr, "%s:%d Assert failed: %s\n", __FILE__, __LINE__, #cond); \ 17 | exit(1); \ 18 | } \ 19 | } while (0) 20 | 21 | // Entry point for LibFuzzer. 22 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 23 | // Deflate data. 24 | z_stream comp_strm; 25 | comp_strm.zalloc = Z_NULL; 26 | comp_strm.zfree = Z_NULL; 27 | comp_strm.opaque = Z_NULL; 28 | int ret = deflateInit(&comp_strm, Z_DEFAULT_COMPRESSION); 29 | ASSERT(ret == Z_OK); 30 | 31 | size_t comp_buf_cap = deflateBound(&comp_strm, size); 32 | uint8_t* comp_buf = (uint8_t*)malloc(comp_buf_cap); 33 | ASSERT(comp_buf != nullptr); 34 | comp_strm.next_out = comp_buf; 35 | comp_strm.avail_out = comp_buf_cap; 36 | comp_strm.next_in = (unsigned char*)data; 37 | comp_strm.avail_in = size; 38 | ret = deflate(&comp_strm, Z_FINISH); 39 | ASSERT(ret == Z_STREAM_END); 40 | size_t comp_sz = comp_buf_cap - comp_strm.avail_out; 41 | 42 | // Inflate comp_buf one chunk at a time. 43 | z_stream decomp_strm; 44 | decomp_strm.zalloc = Z_NULL; 45 | decomp_strm.zfree = Z_NULL; 46 | decomp_strm.opaque = Z_NULL; 47 | ret = inflateInit(&decomp_strm); 48 | ASSERT(ret == Z_OK); 49 | decomp_strm.next_in = comp_buf; 50 | decomp_strm.avail_in = comp_sz; 51 | 52 | while (decomp_strm.avail_in > 0) { 53 | uint8_t decomp_buf[1024]; 54 | decomp_strm.next_out = decomp_buf; 55 | decomp_strm.avail_out = sizeof(decomp_buf); 56 | ret = inflate(&decomp_strm, Z_FINISH); 57 | ASSERT(ret == Z_OK || ret == Z_STREAM_END || ret == Z_BUF_ERROR); 58 | 59 | // Verify the output bytes. 60 | size_t num_out = sizeof(decomp_buf) - decomp_strm.avail_out; 61 | for (size_t i = 0; i < num_out; i++) { 62 | ASSERT(decomp_buf[i] == data[decomp_strm.total_out - num_out + i]); 63 | } 64 | } 65 | 66 | ret = deflateEnd(&comp_strm); 67 | ASSERT(ret == Z_OK); 68 | free(comp_buf); 69 | 70 | inflateEnd(&decomp_strm); 71 | ASSERT(ret == Z_OK); 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /contrib/tests/fuzzers/uncompress_fuzzer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "zlib.h" 10 | 11 | static Bytef buffer[256 * 1024] = {0}; 12 | 13 | // Entry point for LibFuzzer. 14 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 15 | uLongf buffer_length = static_cast(sizeof(buffer)); 16 | if (Z_OK != 17 | uncompress(buffer, &buffer_length, data, static_cast(size))) { 18 | return 0; 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /contrib/tests/infcover.h: -------------------------------------------------------------------------------- 1 | #ifndef __INF_COVER_H__ 2 | #define __INF_COVER_H__ 3 | 4 | void cover_support(void); 5 | void cover_wrap(void); 6 | void cover_back(void); 7 | void cover_inflate(void); 8 | void cover_trees(void); 9 | void cover_fast(void); 10 | 11 | void cover_CVE_2022_37434(void); 12 | #endif 13 | -------------------------------------------------------------------------------- /contrib/tests/run_all_unittests.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include "base/functional/bind.h" 6 | #include "base/test/launcher/unit_test_launcher.h" 7 | #include "base/test/test_suite.h" 8 | 9 | int main(int argc, char** argv) { 10 | base::TestSuite test_suite(argc, argv); 11 | return base::LaunchUnitTests( 12 | argc, argv, 13 | base::BindOnce(&base::TestSuite::Run, base::Unretained(&test_suite))); 14 | } 15 | -------------------------------------------------------------------------------- /contrib/tests/standalone_test_runner.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the Chromium source repository LICENSE file. 4 | #include 5 | 6 | int main(int argc, char** argv) { 7 | ::testing::InitGoogleTest(&argc, argv); 8 | return RUN_ALL_TESTS(); 9 | } 10 | -------------------------------------------------------------------------------- /cpu_features.c: -------------------------------------------------------------------------------- 1 | /* cpu_features.c -- Processor features detection. 2 | * 3 | * Copyright 2018 The Chromium Authors 4 | * Use of this source code is governed by a BSD-style license that can be 5 | * found in the Chromium source repository LICENSE file. 6 | */ 7 | 8 | #include "cpu_features.h" 9 | #include "zutil.h" 10 | 11 | #include 12 | #if defined(_MSC_VER) 13 | #include 14 | #elif defined(ADLER32_SIMD_SSSE3) 15 | #include 16 | #endif 17 | 18 | /* TODO(cavalcantii): remove checks for x86_flags on deflate. 19 | */ 20 | #if defined(ARMV8_OS_MACOS) 21 | /* Crypto extensions (crc32/pmull) are a baseline feature in ARMv8.1-A, and 22 | * OSX running on arm64 is new enough that these can be assumed without 23 | * runtime detection. 24 | */ 25 | int ZLIB_INTERNAL arm_cpu_enable_crc32 = 1; 26 | int ZLIB_INTERNAL arm_cpu_enable_pmull = 1; 27 | #else 28 | int ZLIB_INTERNAL arm_cpu_enable_crc32 = 0; 29 | int ZLIB_INTERNAL arm_cpu_enable_pmull = 0; 30 | #endif 31 | int ZLIB_INTERNAL x86_cpu_enable_sse2 = 0; 32 | int ZLIB_INTERNAL x86_cpu_enable_ssse3 = 0; 33 | int ZLIB_INTERNAL x86_cpu_enable_simd = 0; 34 | int ZLIB_INTERNAL x86_cpu_enable_avx512 = 0; 35 | 36 | int ZLIB_INTERNAL riscv_cpu_enable_rvv = 0; 37 | int ZLIB_INTERNAL riscv_cpu_enable_vclmul = 0; 38 | 39 | #ifndef CPU_NO_SIMD 40 | 41 | #if defined(ARMV8_OS_ANDROID) || defined(ARMV8_OS_LINUX) || \ 42 | defined(ARMV8_OS_FUCHSIA) || defined(ARMV8_OS_IOS) 43 | #include 44 | #endif 45 | 46 | #if defined(ARMV8_OS_ANDROID) 47 | #include 48 | #elif defined(ARMV8_OS_LINUX) 49 | #include 50 | #include 51 | #elif defined(ARMV8_OS_FUCHSIA) 52 | #include 53 | #include 54 | #include 55 | #elif defined(ARMV8_OS_WINDOWS) || defined(X86_WINDOWS) 56 | #include 57 | #elif defined(ARMV8_OS_IOS) 58 | #include 59 | #elif !defined(_MSC_VER) 60 | #include 61 | #else 62 | #error cpu_features.c CPU feature detection in not defined for your platform 63 | #endif 64 | 65 | #if !defined(CPU_NO_SIMD) && !defined(ARMV8_OS_MACOS) 66 | static void _cpu_check_features(void); 67 | #endif 68 | 69 | #if defined(ARMV8_OS_ANDROID) || defined(ARMV8_OS_LINUX) || \ 70 | defined(ARMV8_OS_MACOS) || defined(ARMV8_OS_FUCHSIA) || \ 71 | defined(X86_NOT_WINDOWS) || defined(ARMV8_OS_IOS) || \ 72 | defined(RISCV_RVV) 73 | #if !defined(ARMV8_OS_MACOS) 74 | // _cpu_check_features() doesn't need to do anything on mac/arm since all 75 | // features are known at build time, so don't call it. 76 | // Do provide cpu_check_features() (with a no-op implementation) so that we 77 | // don't have to make all callers of it check for mac/arm. 78 | static pthread_once_t cpu_check_inited_once = PTHREAD_ONCE_INIT; 79 | #endif 80 | void ZLIB_INTERNAL cpu_check_features(void) 81 | { 82 | #if !defined(ARMV8_OS_MACOS) 83 | pthread_once(&cpu_check_inited_once, _cpu_check_features); 84 | #endif 85 | } 86 | #elif defined(ARMV8_OS_WINDOWS) || defined(X86_WINDOWS) 87 | static INIT_ONCE cpu_check_inited_once = INIT_ONCE_STATIC_INIT; 88 | static BOOL CALLBACK _cpu_check_features_forwarder(PINIT_ONCE once, PVOID param, PVOID* context) 89 | { 90 | _cpu_check_features(); 91 | return TRUE; 92 | } 93 | void ZLIB_INTERNAL cpu_check_features(void) 94 | { 95 | InitOnceExecuteOnce(&cpu_check_inited_once, _cpu_check_features_forwarder, 96 | NULL, NULL); 97 | } 98 | #endif 99 | 100 | #if (defined(__ARM_NEON__) || defined(__ARM_NEON)) 101 | #if !defined(ARMV8_OS_MACOS) 102 | /* 103 | * See http://bit.ly/2CcoEsr for run-time detection of ARM features and also 104 | * crbug.com/931275 for android_getCpuFeatures() use in the Android sandbox. 105 | */ 106 | static void _cpu_check_features(void) 107 | { 108 | #if defined(ARMV8_OS_ANDROID) && defined(__aarch64__) 109 | uint64_t features = android_getCpuFeatures(); 110 | arm_cpu_enable_crc32 = !!(features & ANDROID_CPU_ARM64_FEATURE_CRC32); 111 | arm_cpu_enable_pmull = !!(features & ANDROID_CPU_ARM64_FEATURE_PMULL); 112 | #elif defined(ARMV8_OS_ANDROID) /* aarch32 */ 113 | uint64_t features = android_getCpuFeatures(); 114 | arm_cpu_enable_crc32 = !!(features & ANDROID_CPU_ARM_FEATURE_CRC32); 115 | arm_cpu_enable_pmull = !!(features & ANDROID_CPU_ARM_FEATURE_PMULL); 116 | #elif defined(ARMV8_OS_LINUX) && defined(__aarch64__) 117 | unsigned long features = getauxval(AT_HWCAP); 118 | arm_cpu_enable_crc32 = !!(features & HWCAP_CRC32); 119 | arm_cpu_enable_pmull = !!(features & HWCAP_PMULL); 120 | #elif defined(ARMV8_OS_LINUX) && (defined(__ARM_NEON) || defined(__ARM_NEON__)) 121 | /* Query HWCAP2 for ARMV8-A SoCs running in aarch32 mode */ 122 | unsigned long features = getauxval(AT_HWCAP2); 123 | arm_cpu_enable_crc32 = !!(features & HWCAP2_CRC32); 124 | arm_cpu_enable_pmull = !!(features & HWCAP2_PMULL); 125 | #elif defined(ARMV8_OS_FUCHSIA) 126 | uint32_t features; 127 | zx_status_t rc = zx_system_get_features(ZX_FEATURE_KIND_CPU, &features); 128 | if (rc != ZX_OK || (features & ZX_ARM64_FEATURE_ISA_ASIMD) == 0) 129 | return; /* Report nothing if ASIMD(NEON) is missing */ 130 | arm_cpu_enable_crc32 = !!(features & ZX_ARM64_FEATURE_ISA_CRC32); 131 | arm_cpu_enable_pmull = !!(features & ZX_ARM64_FEATURE_ISA_PMULL); 132 | #elif defined(ARMV8_OS_WINDOWS) 133 | arm_cpu_enable_crc32 = IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE); 134 | arm_cpu_enable_pmull = IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE); 135 | #elif defined(ARMV8_OS_IOS) 136 | // Determine what features are supported dynamically. This code is applicable to macOS 137 | // as well if we wish to do that dynamically on that platform in the future. 138 | // See https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics 139 | int val = 0; 140 | size_t len = sizeof(val); 141 | arm_cpu_enable_crc32 = sysctlbyname("hw.optional.armv8_crc32", &val, &len, 0, 0) == 0 142 | && val != 0; 143 | val = 0; 144 | len = sizeof(val); 145 | arm_cpu_enable_pmull = sysctlbyname("hw.optional.arm.FEAT_PMULL", &val, &len, 0, 0) == 0 146 | && val != 0; 147 | #endif 148 | } 149 | #endif 150 | #elif defined(X86_NOT_WINDOWS) || defined(X86_WINDOWS) 151 | /* 152 | * iOS@x86 (i.e. emulator) is another special case where we disable 153 | * SIMD optimizations. 154 | */ 155 | #ifndef CPU_NO_SIMD 156 | /* On x86 we simply use a instruction to check the CPU features. 157 | * (i.e. CPUID). 158 | */ 159 | #ifdef CRC32_SIMD_AVX512_PCLMUL 160 | #include 161 | #include 162 | #endif 163 | static void _cpu_check_features(void) 164 | { 165 | int x86_cpu_has_sse2; 166 | int x86_cpu_has_ssse3; 167 | int x86_cpu_has_sse42; 168 | int x86_cpu_has_pclmulqdq; 169 | int abcd[4]; 170 | 171 | #ifdef _MSC_VER 172 | __cpuid(abcd, 1); 173 | #else 174 | __cpuid(1, abcd[0], abcd[1], abcd[2], abcd[3]); 175 | #endif 176 | 177 | x86_cpu_has_sse2 = abcd[3] & 0x4000000; 178 | x86_cpu_has_ssse3 = abcd[2] & 0x000200; 179 | x86_cpu_has_sse42 = abcd[2] & 0x100000; 180 | x86_cpu_has_pclmulqdq = abcd[2] & 0x2; 181 | 182 | x86_cpu_enable_sse2 = x86_cpu_has_sse2; 183 | 184 | x86_cpu_enable_ssse3 = x86_cpu_has_ssse3; 185 | 186 | x86_cpu_enable_simd = x86_cpu_has_sse2 && 187 | x86_cpu_has_sse42 && 188 | x86_cpu_has_pclmulqdq; 189 | 190 | #ifdef CRC32_SIMD_AVX512_PCLMUL 191 | x86_cpu_enable_avx512 = _xgetbv(0) & 0x00000040; 192 | #endif 193 | } 194 | #endif // x86 & NO_SIMD 195 | 196 | #elif defined(RISCV_RVV) 197 | #include 198 | 199 | #ifndef ZLIB_HWCAP_RVV 200 | #define ZLIB_HWCAP_RVV (1 << ('v' - 'a')) 201 | #endif 202 | 203 | /* TODO(cavalcantii) 204 | * - add support for Android@RISCV i.e. __riscv_hwprobe(). 205 | * - detect vclmul (crypto extensions). 206 | */ 207 | static void _cpu_check_features(void) 208 | { 209 | unsigned long features = getauxval(AT_HWCAP); 210 | riscv_cpu_enable_rvv = !!(features & ZLIB_HWCAP_RVV); 211 | } 212 | #endif // ARM | x86 | RISCV 213 | #endif // NO SIMD CPU 214 | -------------------------------------------------------------------------------- /cpu_features.h: -------------------------------------------------------------------------------- 1 | /* cpu_features.h -- Processor features detection. 2 | * 3 | * Copyright 2018 The Chromium Authors 4 | * Use of this source code is governed by a BSD-style license that can be 5 | * found in the Chromium source repository LICENSE file. 6 | */ 7 | 8 | #include "zlib.h" 9 | 10 | /* TODO(cavalcantii): remove checks for x86_flags on deflate. 11 | */ 12 | extern int arm_cpu_enable_crc32; 13 | extern int arm_cpu_enable_pmull; 14 | extern int x86_cpu_enable_sse2; 15 | extern int x86_cpu_enable_ssse3; 16 | extern int x86_cpu_enable_simd; 17 | extern int x86_cpu_enable_avx512; 18 | 19 | extern int riscv_cpu_enable_rvv; 20 | extern int riscv_cpu_enable_vclmul; 21 | 22 | void cpu_check_features(void); 23 | -------------------------------------------------------------------------------- /crc32_simd.h: -------------------------------------------------------------------------------- 1 | /* crc32_simd.h 2 | * 3 | * Copyright 2017 The Chromium Authors 4 | * Use of this source code is governed by a BSD-style license that can be 5 | * found in the Chromium source repository LICENSE file. 6 | */ 7 | 8 | #include 9 | 10 | #include "zconf.h" 11 | #include "zutil.h" 12 | #include "deflate.h" 13 | 14 | /* 15 | * crc32_sse42_simd_(): compute the crc32 of the buffer, where the buffer 16 | * length must be at least 64, and a multiple of 16. 17 | */ 18 | uint32_t ZLIB_INTERNAL crc32_sse42_simd_(const unsigned char* buf, 19 | z_size_t len, 20 | uint32_t crc); 21 | 22 | uint32_t ZLIB_INTERNAL crc32_avx512_simd_(const unsigned char* buf, 23 | z_size_t len, 24 | uint32_t crc); 25 | 26 | /* 27 | * crc32_sse42_simd_ buffer size constraints: see the use in zlib/crc32.c 28 | * for computing the crc32 of an arbitrary length buffer. 29 | */ 30 | #define Z_CRC32_SSE42_MINIMUM_LENGTH 64 31 | #define Z_CRC32_SSE42_CHUNKSIZE_MASK 15 32 | #define Z_CRC32_AVX512_MINIMUM_LENGTH 256 33 | #define Z_CRC32_AVX512_CHUNKSIZE_MASK 63 34 | 35 | /* 36 | * CRC32 checksums using ARMv8-a crypto instructions. 37 | */ 38 | uint32_t ZLIB_INTERNAL armv8_crc32_little(const unsigned char* buf, 39 | z_size_t len, 40 | uint32_t crc); 41 | 42 | /* aarch64 specific code. */ 43 | #if defined(__aarch64__) 44 | 45 | /* 128 is the sweet spot at the time of coding (late 2020). */ 46 | #define Z_CRC32_PMULL_MINIMUM_LENGTH 128 47 | #define Z_CRC32_PMULL_CHUNKSIZE_MASK 15 48 | 49 | /* 50 | * CRC32 checksums using ARMv8-a PMULL instructions, where the buffer 51 | * length must be at least 64, and a multiple of 16. 52 | */ 53 | uint32_t ZLIB_INTERNAL armv8_crc32_pmull_little(const unsigned char* buf, 54 | z_size_t len, 55 | uint32_t crc); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /examples/zpipe.c: -------------------------------------------------------------------------------- 1 | /* zpipe.c: example of proper use of zlib's inflate() and deflate() 2 | Not copyrighted -- provided to the public domain 3 | Version 1.4 11 December 2005 Mark Adler */ 4 | 5 | /* Version history: 6 | 1.0 30 Oct 2004 First version 7 | 1.1 8 Nov 2004 Add void casting for unused return values 8 | Use switch statement for inflate() return values 9 | 1.2 9 Nov 2004 Add assertions to document zlib guarantees 10 | 1.3 6 Apr 2005 Remove incorrect assertion in inf() 11 | 1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions 12 | Avoid some compiler warnings for input and output buffers 13 | */ 14 | 15 | #if defined(_WIN32) && !defined(_CRT_NONSTDC_NO_DEPRECATE) 16 | # define _CRT_NONSTDC_NO_DEPRECATE 17 | #endif 18 | 19 | #include 20 | #include 21 | #include 22 | #include "zlib.h" 23 | 24 | #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) 25 | # include 26 | # include 27 | # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) 28 | #else 29 | # define SET_BINARY_MODE(file) 30 | #endif 31 | 32 | #define CHUNK 16384 33 | 34 | /* Compress from file source to file dest until EOF on source. 35 | def() returns Z_OK on success, Z_MEM_ERROR if memory could not be 36 | allocated for processing, Z_STREAM_ERROR if an invalid compression 37 | level is supplied, Z_VERSION_ERROR if the version of zlib.h and the 38 | version of the library linked do not match, or Z_ERRNO if there is 39 | an error reading or writing the files. */ 40 | int def(FILE *source, FILE *dest, int level) 41 | { 42 | int ret, flush; 43 | unsigned have; 44 | z_stream strm; 45 | unsigned char in[CHUNK]; 46 | unsigned char out[CHUNK]; 47 | 48 | /* allocate deflate state */ 49 | strm.zalloc = Z_NULL; 50 | strm.zfree = Z_NULL; 51 | strm.opaque = Z_NULL; 52 | ret = deflateInit(&strm, level); 53 | if (ret != Z_OK) 54 | return ret; 55 | 56 | /* compress until end of file */ 57 | do { 58 | strm.avail_in = fread(in, 1, CHUNK, source); 59 | if (ferror(source)) { 60 | (void)deflateEnd(&strm); 61 | return Z_ERRNO; 62 | } 63 | flush = feof(source) ? Z_FINISH : Z_NO_FLUSH; 64 | strm.next_in = in; 65 | 66 | /* run deflate() on input until output buffer not full, finish 67 | compression if all of source has been read in */ 68 | do { 69 | strm.avail_out = CHUNK; 70 | strm.next_out = out; 71 | ret = deflate(&strm, flush); /* no bad return value */ 72 | assert(ret != Z_STREAM_ERROR); /* state not clobbered */ 73 | have = CHUNK - strm.avail_out; 74 | if (fwrite(out, 1, have, dest) != have || ferror(dest)) { 75 | (void)deflateEnd(&strm); 76 | return Z_ERRNO; 77 | } 78 | } while (strm.avail_out == 0); 79 | assert(strm.avail_in == 0); /* all input will be used */ 80 | 81 | /* done when last data in file processed */ 82 | } while (flush != Z_FINISH); 83 | assert(ret == Z_STREAM_END); /* stream will be complete */ 84 | 85 | /* clean up and return */ 86 | (void)deflateEnd(&strm); 87 | return Z_OK; 88 | } 89 | 90 | /* Decompress from file source to file dest until stream ends or EOF. 91 | inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be 92 | allocated for processing, Z_DATA_ERROR if the deflate data is 93 | invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and 94 | the version of the library linked do not match, or Z_ERRNO if there 95 | is an error reading or writing the files. */ 96 | int inf(FILE *source, FILE *dest) 97 | { 98 | int ret; 99 | unsigned have; 100 | z_stream strm; 101 | unsigned char in[CHUNK]; 102 | unsigned char out[CHUNK]; 103 | 104 | /* allocate inflate state */ 105 | strm.zalloc = Z_NULL; 106 | strm.zfree = Z_NULL; 107 | strm.opaque = Z_NULL; 108 | strm.avail_in = 0; 109 | strm.next_in = Z_NULL; 110 | ret = inflateInit(&strm); 111 | if (ret != Z_OK) 112 | return ret; 113 | 114 | /* decompress until deflate stream ends or end of file */ 115 | do { 116 | strm.avail_in = fread(in, 1, CHUNK, source); 117 | if (ferror(source)) { 118 | (void)inflateEnd(&strm); 119 | return Z_ERRNO; 120 | } 121 | if (strm.avail_in == 0) 122 | break; 123 | strm.next_in = in; 124 | 125 | /* run inflate() on input until output buffer not full */ 126 | do { 127 | strm.avail_out = CHUNK; 128 | strm.next_out = out; 129 | ret = inflate(&strm, Z_NO_FLUSH); 130 | assert(ret != Z_STREAM_ERROR); /* state not clobbered */ 131 | switch (ret) { 132 | case Z_NEED_DICT: 133 | ret = Z_DATA_ERROR; /* and fall through */ 134 | case Z_DATA_ERROR: 135 | case Z_MEM_ERROR: 136 | (void)inflateEnd(&strm); 137 | return ret; 138 | } 139 | have = CHUNK - strm.avail_out; 140 | if (fwrite(out, 1, have, dest) != have || ferror(dest)) { 141 | (void)inflateEnd(&strm); 142 | return Z_ERRNO; 143 | } 144 | } while (strm.avail_out == 0); 145 | 146 | /* done when inflate() says it's done */ 147 | } while (ret != Z_STREAM_END); 148 | 149 | /* clean up and return */ 150 | (void)inflateEnd(&strm); 151 | return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; 152 | } 153 | 154 | /* report a zlib or i/o error */ 155 | void zerr(int ret) 156 | { 157 | fputs("zpipe: ", stderr); 158 | switch (ret) { 159 | case Z_ERRNO: 160 | if (ferror(stdin)) 161 | fputs("error reading stdin\n", stderr); 162 | if (ferror(stdout)) 163 | fputs("error writing stdout\n", stderr); 164 | break; 165 | case Z_STREAM_ERROR: 166 | fputs("invalid compression level\n", stderr); 167 | break; 168 | case Z_DATA_ERROR: 169 | fputs("invalid or incomplete deflate data\n", stderr); 170 | break; 171 | case Z_MEM_ERROR: 172 | fputs("out of memory\n", stderr); 173 | break; 174 | case Z_VERSION_ERROR: 175 | fputs("zlib version mismatch!\n", stderr); 176 | } 177 | } 178 | 179 | /* compress or decompress from stdin to stdout */ 180 | int main(int argc, char **argv) 181 | { 182 | int ret; 183 | 184 | /* avoid end-of-line conversions */ 185 | SET_BINARY_MODE(stdin); 186 | SET_BINARY_MODE(stdout); 187 | 188 | /* do compression if no arguments */ 189 | if (argc == 1) { 190 | ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION); 191 | if (ret != Z_OK) 192 | zerr(ret); 193 | return ret; 194 | } 195 | 196 | /* do decompression if -d specified */ 197 | else if (argc == 2 && strcmp(argv[1], "-d") == 0) { 198 | ret = inf(stdin, stdout); 199 | if (ret != Z_OK) 200 | zerr(ret); 201 | return ret; 202 | } 203 | 204 | /* otherwise, report usage */ 205 | else { 206 | fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr); 207 | return 1; 208 | } 209 | } 210 | -------------------------------------------------------------------------------- /google/BUILD.gn: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | 5 | import("//build_overrides/build.gni") 6 | 7 | if (build_with_chromium && is_ios) { 8 | import("//build/config/ios/bundle_data_from_filelist.gni") 9 | } 10 | 11 | if (build_with_chromium) { 12 | static_library("zip") { 13 | sources = [ 14 | "redact.h", 15 | "zip.cc", 16 | "zip.h", 17 | "zip_internal.cc", 18 | "zip_internal.h", 19 | "zip_reader.cc", 20 | "zip_reader.h", 21 | "zip_writer.cc", 22 | "zip_writer.h", 23 | ] 24 | deps = [ 25 | "..:minizip", 26 | "//base", 27 | "//base:i18n", 28 | ] 29 | } 30 | 31 | static_library("compression_utils") { 32 | sources = [ 33 | "compression_utils.cc", 34 | "compression_utils.h", 35 | ] 36 | deps = [ 37 | "..", 38 | "//base", 39 | ] 40 | public_deps = [ ":compression_utils_portable" ] 41 | } 42 | 43 | if (is_ios) { 44 | bundle_data_from_filelist("zlib_pak_bundle_data") { 45 | testonly = true 46 | filelist_name = "test_data.filelist" 47 | } 48 | } 49 | } 50 | 51 | # This allows other users of Chromium's zlib library, but don't use Chromium's 52 | # //base, to reuse some boilerplate code. 53 | static_library("compression_utils_portable") { 54 | sources = [ 55 | "compression_utils_portable.cc", 56 | "compression_utils_portable.h", 57 | ] 58 | public_deps = [ ".." ] 59 | } 60 | -------------------------------------------------------------------------------- /google/DEPS: -------------------------------------------------------------------------------- 1 | include_rules = [ 2 | '+base', 3 | '+build', 4 | '+testing', 5 | '+third_party/icu/source/i18n/unicode', 6 | '+third_party/zlib/zlib.h', 7 | ] 8 | -------------------------------------------------------------------------------- /google/OWNERS: -------------------------------------------------------------------------------- 1 | satorux@chromium.org 2 | 3 | # compression_utils* 4 | asvitkine@chromium.org 5 | isherman@chromium.org 6 | cavalcantii@chromium.org 7 | -------------------------------------------------------------------------------- /google/compression_utils.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include "third_party/zlib/google/compression_utils.h" 6 | 7 | #include "base/check_op.h" 8 | #include "base/process/memory.h" 9 | 10 | #include "third_party/zlib/google/compression_utils_portable.h" 11 | 12 | namespace compression { 13 | 14 | bool GzipCompress(base::span input, 15 | char* output_buffer, 16 | size_t output_buffer_size, 17 | size_t* compressed_size, 18 | void* (*malloc_fn)(size_t), 19 | void (*free_fn)(void*)) { 20 | static_assert(sizeof(Bytef) == 1, ""); 21 | 22 | // uLongf can be larger than size_t. 23 | uLongf compressed_size_long = static_cast(output_buffer_size); 24 | if (zlib_internal::GzipCompressHelper( 25 | reinterpret_cast(output_buffer), &compressed_size_long, 26 | reinterpret_cast(input.data()), 27 | static_cast(input.size()), malloc_fn, free_fn) != Z_OK) { 28 | return false; 29 | } 30 | // No overflow, as compressed_size_long <= output.size() which is a size_t. 31 | *compressed_size = static_cast(compressed_size_long); 32 | return true; 33 | } 34 | 35 | bool GzipCompress(base::span input, std::string* output) { 36 | return GzipCompress(base::as_bytes(input), output); 37 | } 38 | 39 | bool GzipCompress(base::span input, std::string* output) { 40 | // Not using std::vector<> because allocation failures are recoverable, 41 | // which is hidden by std::vector<>. 42 | static_assert(sizeof(Bytef) == 1, ""); 43 | const uLongf input_size = static_cast(input.size()); 44 | 45 | uLongf compressed_data_size = 46 | zlib_internal::GzipExpectedCompressedSize(input_size); 47 | 48 | Bytef* compressed_data; 49 | if (!base::UncheckedMalloc(compressed_data_size, 50 | reinterpret_cast(&compressed_data))) { 51 | return false; 52 | } 53 | 54 | if (zlib_internal::GzipCompressHelper( 55 | compressed_data, &compressed_data_size, 56 | reinterpret_cast(input.data()), input_size, nullptr, 57 | nullptr) != Z_OK) { 58 | free(compressed_data); 59 | return false; 60 | } 61 | 62 | Bytef* resized_data = 63 | reinterpret_cast(realloc(compressed_data, compressed_data_size)); 64 | if (!resized_data) { 65 | free(compressed_data); 66 | return false; 67 | } 68 | output->assign(resized_data, resized_data + compressed_data_size); 69 | DCHECK_EQ(input_size, GetUncompressedSize(*output)); 70 | 71 | free(resized_data); 72 | return true; 73 | } 74 | 75 | bool GzipUncompress(const std::string& input, std::string* output) { 76 | std::string uncompressed_output; 77 | uLongf uncompressed_size = static_cast(GetUncompressedSize(input)); 78 | if (size_t{uncompressed_size} > uncompressed_output.max_size()) 79 | return false; 80 | 81 | uncompressed_output.resize(uncompressed_size); 82 | if (zlib_internal::GzipUncompressHelper( 83 | reinterpret_cast(uncompressed_output.data()), 84 | &uncompressed_size, reinterpret_cast(input.data()), 85 | static_cast(input.length())) == Z_OK) { 86 | output->swap(uncompressed_output); 87 | return true; 88 | } 89 | return false; 90 | } 91 | 92 | bool GzipUncompress(base::span input, 93 | base::span output) { 94 | return GzipUncompress(base::as_bytes(input), base::as_bytes(output)); 95 | } 96 | 97 | bool GzipUncompress(base::span input, 98 | base::span output) { 99 | uLongf uncompressed_size = GetUncompressedSize(input); 100 | if (uncompressed_size > output.size()) 101 | return false; 102 | return zlib_internal::GzipUncompressHelper( 103 | reinterpret_cast(const_cast(output.data())), 104 | &uncompressed_size, reinterpret_cast(input.data()), 105 | static_cast(input.size())) == Z_OK; 106 | } 107 | 108 | bool GzipUncompress(base::span input, std::string* output) { 109 | return GzipUncompress(base::as_bytes(input), output); 110 | } 111 | 112 | bool GzipUncompress(base::span input, std::string* output) { 113 | // Disallow in-place usage, i.e., |input| using |*output| as underlying data. 114 | DCHECK_NE(reinterpret_cast(input.data()), output->data()); 115 | uLongf uncompressed_size = GetUncompressedSize(input); 116 | output->resize(uncompressed_size); 117 | return zlib_internal::GzipUncompressHelper( 118 | reinterpret_cast(output->data()), &uncompressed_size, 119 | reinterpret_cast(input.data()), 120 | static_cast(input.size())) == Z_OK; 121 | } 122 | 123 | uint32_t GetUncompressedSize(base::span compressed_data) { 124 | return GetUncompressedSize(base::as_bytes(compressed_data)); 125 | } 126 | 127 | uint32_t GetUncompressedSize(base::span compressed_data) { 128 | return zlib_internal::GetGzipUncompressedSize( 129 | reinterpret_cast(compressed_data.data()), 130 | compressed_data.size()); 131 | } 132 | 133 | } // namespace compression 134 | -------------------------------------------------------------------------------- /google/compression_utils.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #ifndef THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_H_ 6 | #define THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_H_ 7 | 8 | #include 9 | 10 | #include "base/containers/span.h" 11 | 12 | namespace compression { 13 | 14 | // Compresses the data in |input| using gzip, storing the result in 15 | // |output_buffer|, of size |output_buffer_size|. If the buffer is large enough 16 | // and compression succeeds, |compressed_size| points to the compressed data 17 | // size after the call. 18 | // |malloc_fn| and |free_fn| are pointers to malloc() and free()-like functions, 19 | // or nullptr to use the standard ones. 20 | // Returns true for success. 21 | bool GzipCompress(base::span input, 22 | char* output_buffer, 23 | size_t output_buffer_size, 24 | size_t* compressed_size, 25 | void* (*malloc_fn)(size_t), 26 | void (*free_fn)(void*)); 27 | 28 | // Compresses the data in |input| using gzip, storing the result in |output|. 29 | // |input| and |output| are allowed to point to the same string (in-place 30 | // operation). 31 | // Returns true for success. 32 | bool GzipCompress(base::span input, std::string* output); 33 | 34 | // Like the above method, but using uint8_t instead. 35 | bool GzipCompress(base::span input, std::string* output); 36 | 37 | // Uncompresses the data in |input| using gzip, storing the result in |output|. 38 | // |input| and |output| are allowed to be the same string (in-place operation). 39 | // Returns true for success. 40 | bool GzipUncompress(const std::string& input, std::string* output); 41 | 42 | // Like the above method, but uses base::span to avoid allocations if 43 | // needed. |output|'s size must be at least as large as the return value from 44 | // GetUncompressedSize. 45 | // Returns true for success. 46 | bool GzipUncompress(base::span input, 47 | base::span output); 48 | 49 | // Like the above method, but using uint8_t instead. 50 | bool GzipUncompress(base::span input, 51 | base::span output); 52 | 53 | // Uncompresses the data in |input| using gzip, and writes the results to 54 | // |output|, which must NOT be the underlying string of |input|, and is resized 55 | // if necessary. 56 | // Returns true for success. 57 | bool GzipUncompress(base::span input, std::string* output); 58 | 59 | // Like the above method, but using uint8_t instead. 60 | bool GzipUncompress(base::span input, std::string* output); 61 | 62 | // Returns the uncompressed size from GZIP-compressed |compressed_data|. 63 | uint32_t GetUncompressedSize(base::span compressed_data); 64 | 65 | // Like the above method, but using uint8_t instead. 66 | uint32_t GetUncompressedSize(base::span compressed_data); 67 | 68 | } // namespace compression 69 | 70 | #endif // THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_H_ 71 | -------------------------------------------------------------------------------- /google/compression_utils_portable.cc: -------------------------------------------------------------------------------- 1 | /* compression_utils_portable.cc 2 | * 3 | * Copyright 2019 The Chromium Authors 4 | * Use of this source code is governed by a BSD-style license that can be 5 | * found in the Chromium source repository LICENSE file. 6 | */ 7 | 8 | #include "compression_utils_portable.h" 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | namespace zlib_internal { 15 | 16 | // The difference in bytes between a zlib header and a gzip header. 17 | const size_t kGzipZlibHeaderDifferenceBytes = 16; 18 | 19 | // Pass an integer greater than the following get a gzip header instead of a 20 | // zlib header when calling deflateInit2() and inflateInit2(). 21 | const int kWindowBitsToGetGzipHeader = 16; 22 | 23 | // This describes the amount of memory zlib uses to compress data. It can go 24 | // from 1 to 9, with 8 being the default. For details, see: 25 | // http://www.zlib.net/manual.html (search for memLevel). 26 | const int kZlibMemoryLevel = 8; 27 | 28 | // The expected compressed size is based on the input size factored by 29 | // internal Zlib constants (e.g. window size, etc) plus the wrapper 30 | // header size. 31 | uLongf GzipExpectedCompressedSize(uLongf input_size) { 32 | return kGzipZlibHeaderDifferenceBytes + compressBound(input_size); 33 | } 34 | 35 | // The expected decompressed size is stored in the last 36 | // 4 bytes of |input| in LE. See https://tools.ietf.org/html/rfc1952#page-5 37 | uint32_t GetGzipUncompressedSize(const Bytef* compressed_data, size_t length) { 38 | uint32_t size; 39 | if (length < sizeof(size)) 40 | return 0; 41 | 42 | memcpy(&size, &compressed_data[length - sizeof(size)], sizeof(size)); 43 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 44 | return size; 45 | #else 46 | return __builtin_bswap32(size); 47 | #endif 48 | } 49 | 50 | // The number of window bits determines the type of wrapper to use - see 51 | // https://cs.chromium.org/chromium/src/third_party/zlib/zlib.h?l=566 52 | inline int ZlibStreamWrapperType(WrapperType type) { 53 | if (type == ZLIB) // zlib DEFLATE stream wrapper 54 | return MAX_WBITS; 55 | if (type == GZIP) // gzip DEFLATE stream wrapper 56 | return MAX_WBITS + kWindowBitsToGetGzipHeader; 57 | if (type == ZRAW) // no wrapper, use raw DEFLATE 58 | return -MAX_WBITS; 59 | return 0; 60 | } 61 | 62 | int GzipCompressHelper(Bytef* dest, 63 | uLongf* dest_length, 64 | const Bytef* source, 65 | uLong source_length, 66 | void* (*malloc_fn)(size_t), 67 | void (*free_fn)(void*)) { 68 | return CompressHelper(GZIP, dest, dest_length, source, source_length, 69 | Z_DEFAULT_COMPRESSION, malloc_fn, free_fn); 70 | } 71 | 72 | // This code is taken almost verbatim from third_party/zlib/compress.c. The only 73 | // difference is deflateInit2() is called which allows different window bits to 74 | // be set. > 16 causes a gzip header to be emitted rather than a zlib header, 75 | // and negative causes no header to emitted. 76 | // 77 | // Compression level can be a number from 1-9, with 1 being the fastest, 9 being 78 | // the best compression. The default, which the GZIP helper uses, is 6. 79 | int CompressHelper(WrapperType wrapper_type, 80 | Bytef* dest, 81 | uLongf* dest_length, 82 | const Bytef* source, 83 | uLong source_length, 84 | int compression_level, 85 | void* (*malloc_fn)(size_t), 86 | void (*free_fn)(void*)) { 87 | if (compression_level < 0 || compression_level > 9) { 88 | compression_level = Z_DEFAULT_COMPRESSION; 89 | } 90 | 91 | z_stream stream; 92 | 93 | // FIXME(cavalcantii): z_const is not defined as 'const'. 94 | stream.next_in = static_cast(const_cast(source)); 95 | stream.avail_in = static_cast(source_length); 96 | stream.next_out = dest; 97 | stream.avail_out = static_cast(*dest_length); 98 | if (static_cast(stream.avail_out) != *dest_length) 99 | return Z_BUF_ERROR; 100 | 101 | // Cannot convert capturing lambdas to function pointers directly, hence the 102 | // structure. 103 | struct MallocFreeFunctions { 104 | void* (*malloc_fn)(size_t); 105 | void (*free_fn)(void*); 106 | } malloc_free = {malloc_fn, free_fn}; 107 | 108 | if (malloc_fn) { 109 | if (!free_fn) 110 | return Z_BUF_ERROR; 111 | 112 | auto zalloc = [](void* opaque, uInt items, uInt size) { 113 | return reinterpret_cast(opaque)->malloc_fn(items * 114 | size); 115 | }; 116 | auto zfree = [](void* opaque, void* address) { 117 | return reinterpret_cast(opaque)->free_fn(address); 118 | }; 119 | 120 | stream.zalloc = static_cast(zalloc); 121 | stream.zfree = static_cast(zfree); 122 | stream.opaque = static_cast(&malloc_free); 123 | } else { 124 | stream.zalloc = static_cast(0); 125 | stream.zfree = static_cast(0); 126 | stream.opaque = static_cast(0); 127 | } 128 | 129 | int err = deflateInit2(&stream, compression_level, Z_DEFLATED, 130 | ZlibStreamWrapperType(wrapper_type), kZlibMemoryLevel, 131 | Z_DEFAULT_STRATEGY); 132 | if (err != Z_OK) 133 | return err; 134 | 135 | // This has to exist outside of the if statement to prevent it going off the 136 | // stack before deflate(), which will use this object. 137 | gz_header gzip_header; 138 | if (wrapper_type == GZIP) { 139 | memset(&gzip_header, 0, sizeof(gzip_header)); 140 | err = deflateSetHeader(&stream, &gzip_header); 141 | if (err != Z_OK) 142 | return err; 143 | } 144 | 145 | err = deflate(&stream, Z_FINISH); 146 | if (err != Z_STREAM_END) { 147 | deflateEnd(&stream); 148 | return err == Z_OK ? Z_BUF_ERROR : err; 149 | } 150 | *dest_length = stream.total_out; 151 | 152 | err = deflateEnd(&stream); 153 | return err; 154 | } 155 | 156 | int GzipUncompressHelper(Bytef* dest, 157 | uLongf* dest_length, 158 | const Bytef* source, 159 | uLong source_length) { 160 | return UncompressHelper(GZIP, dest, dest_length, source, source_length); 161 | } 162 | 163 | // This code is taken almost verbatim from third_party/zlib/uncompr.c. The only 164 | // difference is inflateInit2() is called which allows different window bits to 165 | // be set. > 16 causes a gzip header to be emitted rather than a zlib header, 166 | // and negative causes no header to emitted. 167 | int UncompressHelper(WrapperType wrapper_type, 168 | Bytef* dest, 169 | uLongf* dest_length, 170 | const Bytef* source, 171 | uLong source_length) { 172 | z_stream stream; 173 | 174 | // FIXME(cavalcantii): z_const is not defined as 'const'. 175 | stream.next_in = static_cast(const_cast(source)); 176 | stream.avail_in = static_cast(source_length); 177 | if (static_cast(stream.avail_in) != source_length) 178 | return Z_BUF_ERROR; 179 | 180 | stream.next_out = dest; 181 | stream.avail_out = static_cast(*dest_length); 182 | if (static_cast(stream.avail_out) != *dest_length) 183 | return Z_BUF_ERROR; 184 | 185 | stream.zalloc = static_cast(0); 186 | stream.zfree = static_cast(0); 187 | 188 | int err = inflateInit2(&stream, ZlibStreamWrapperType(wrapper_type)); 189 | if (err != Z_OK) 190 | return err; 191 | 192 | err = inflate(&stream, Z_FINISH); 193 | if (err != Z_STREAM_END) { 194 | inflateEnd(&stream); 195 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) 196 | return Z_DATA_ERROR; 197 | return err; 198 | } 199 | *dest_length = stream.total_out; 200 | 201 | err = inflateEnd(&stream); 202 | return err; 203 | } 204 | 205 | } // namespace zlib_internal 206 | -------------------------------------------------------------------------------- /google/compression_utils_portable.h: -------------------------------------------------------------------------------- 1 | /* compression_utils_portable.h 2 | * 3 | * Copyright 2019 The Chromium Authors 4 | * Use of this source code is governed by a BSD-style license that can be 5 | * found in the Chromium source repository LICENSE file. 6 | */ 7 | #ifndef THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_PORTABLE_H_ 8 | #define THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_PORTABLE_H_ 9 | 10 | #include 11 | 12 | /* TODO(cavalcantii): remove support for Chromium ever building with a system 13 | * zlib. 14 | */ 15 | #if defined(USE_SYSTEM_ZLIB) 16 | #include 17 | /* AOSP build requires relative paths. */ 18 | #else 19 | #include "zlib.h" 20 | #endif 21 | 22 | namespace zlib_internal { 23 | 24 | enum WrapperType { 25 | ZLIB, 26 | GZIP, 27 | ZRAW, 28 | }; 29 | 30 | uLongf GzipExpectedCompressedSize(uLongf input_size); 31 | 32 | uint32_t GetGzipUncompressedSize(const Bytef* compressed_data, size_t length); 33 | 34 | int GzipCompressHelper(Bytef* dest, 35 | uLongf* dest_length, 36 | const Bytef* source, 37 | uLong source_length, 38 | void* (*malloc_fn)(size_t), 39 | void (*free_fn)(void*)); 40 | 41 | int CompressHelper(WrapperType wrapper_type, 42 | Bytef* dest, 43 | uLongf* dest_length, 44 | const Bytef* source, 45 | uLong source_length, 46 | int compression_level, 47 | void* (*malloc_fn)(size_t), 48 | void (*free_fn)(void*)); 49 | 50 | int GzipUncompressHelper(Bytef* dest, 51 | uLongf* dest_length, 52 | const Bytef* source, 53 | uLong source_length); 54 | 55 | int UncompressHelper(WrapperType wrapper_type, 56 | Bytef* dest, 57 | uLongf* dest_length, 58 | const Bytef* source, 59 | uLong source_length); 60 | 61 | } // namespace zlib_internal 62 | 63 | #endif // THIRD_PARTY_ZLIB_GOOGLE_COMPRESSION_UTILS_PORTABLE_H_ 64 | -------------------------------------------------------------------------------- /google/compression_utils_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #include "third_party/zlib/google/compression_utils.h" 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include "testing/gtest/include/gtest/gtest.h" 14 | 15 | namespace compression { 16 | 17 | namespace { 18 | 19 | // The data to be compressed by gzip. This is the hex representation of "hello 20 | // world". 21 | const uint8_t kData[] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 22 | 0x77, 0x6f, 0x72, 0x6c, 0x64}; 23 | 24 | // This is the string representation of gzip compressed string above. It was 25 | // obtained by running echo -n "hello world" | gzip -c | hexdump -e '8 1 ", 26 | // 0x%x"' followed by 0'ing out the OS byte (10th byte) in the header. This is 27 | // so that the test passes on all platforms (that run various OS'es). 28 | const uint8_t kCompressedData[] = { 29 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcb, 30 | 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0x01, 31 | 0x00, 0x85, 0x11, 0x4a, 0x0d, 0x0b, 0x00, 0x00, 0x00}; 32 | 33 | } // namespace 34 | 35 | TEST(CompressionUtilsTest, GzipCompression) { 36 | std::string data(reinterpret_cast(kData), std::size(kData)); 37 | std::string compressed_data; 38 | EXPECT_TRUE(GzipCompress(data, &compressed_data)); 39 | std::string golden_compressed_data( 40 | reinterpret_cast(kCompressedData), 41 | std::size(kCompressedData)); 42 | EXPECT_EQ(golden_compressed_data, compressed_data); 43 | } 44 | 45 | TEST(CompressionUtilsTest, GzipUncompression) { 46 | std::string compressed_data(reinterpret_cast(kCompressedData), 47 | std::size(kCompressedData)); 48 | 49 | std::string uncompressed_data; 50 | EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data)); 51 | 52 | std::string golden_data(reinterpret_cast(kData), 53 | std::size(kData)); 54 | EXPECT_EQ(golden_data, uncompressed_data); 55 | } 56 | 57 | TEST(CompressionUtilsTest, GzipUncompressionFromSpanToString) { 58 | std::string uncompressed_data; 59 | EXPECT_TRUE(GzipUncompress(kCompressedData, &uncompressed_data)); 60 | 61 | std::string golden_data(reinterpret_cast(kData), 62 | std::size(kData)); 63 | EXPECT_EQ(golden_data, uncompressed_data); 64 | } 65 | 66 | // Checks that compressing/decompressing input > 256 bytes works as expected. 67 | TEST(CompressionUtilsTest, LargeInput) { 68 | const size_t kSize = 32 * 1024; 69 | 70 | // Generate a data string of |kSize| for testing. 71 | std::string data; 72 | data.resize(kSize); 73 | for (size_t i = 0; i < kSize; ++i) 74 | data[i] = static_cast(i & 0xFF); 75 | 76 | std::string compressed_data; 77 | EXPECT_TRUE(GzipCompress(data, &compressed_data)); 78 | 79 | std::string uncompressed_data; 80 | EXPECT_TRUE(GzipUncompress(compressed_data, &uncompressed_data)); 81 | 82 | EXPECT_EQ(data, uncompressed_data); 83 | } 84 | 85 | TEST(CompressionUtilsTest, InPlace) { 86 | const std::string original_data(reinterpret_cast(kData), 87 | std::size(kData)); 88 | const std::string golden_compressed_data( 89 | reinterpret_cast(kCompressedData), 90 | std::size(kCompressedData)); 91 | 92 | std::string data(original_data); 93 | EXPECT_TRUE(GzipCompress(data, &data)); 94 | EXPECT_EQ(golden_compressed_data, data); 95 | EXPECT_TRUE(GzipUncompress(data, &data)); 96 | EXPECT_EQ(original_data, data); 97 | } 98 | 99 | } // namespace compression 100 | -------------------------------------------------------------------------------- /google/redact.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | #ifndef THIRD_PARTY_ZLIB_GOOGLE_REDACT_H_ 5 | #define THIRD_PARTY_ZLIB_GOOGLE_REDACT_H_ 6 | 7 | #include 8 | 9 | #include "base/files/file_path.h" 10 | #include "base/logging.h" 11 | 12 | namespace zip { 13 | 14 | // Redacts file paths in log messages. 15 | // Example: 16 | // LOG(ERROR) << "Cannot open " << Redact(path); 17 | class Redact { 18 | public: 19 | explicit Redact(const base::FilePath& path) : path_(path) {} 20 | 21 | friend std::ostream& operator<<(std::ostream& out, const Redact&& r) { 22 | return LOG_IS_ON(INFO) ? out << "'" << r.path_ << "'" : out << "(redacted)"; 23 | } 24 | 25 | private: 26 | const base::FilePath& path_; 27 | }; 28 | 29 | } // namespace zip 30 | 31 | #endif // THIRD_PARTY_ZLIB_GOOGLE_REDACT_H_ 32 | -------------------------------------------------------------------------------- /google/test/data/Different Encryptions.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/Different Encryptions.zip -------------------------------------------------------------------------------- /google/test/data/Empty Dir Same Name As File.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/Empty Dir Same Name As File.zip -------------------------------------------------------------------------------- /google/test/data/Mixed Paths.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/Mixed Paths.zip -------------------------------------------------------------------------------- /google/test/data/Parent Dir Same Name As File.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/Parent Dir Same Name As File.zip -------------------------------------------------------------------------------- /google/test/data/README.md: -------------------------------------------------------------------------------- 1 | ## test\_posix\_permissions.zip 2 | Rebuild this zip by running: 3 | ``` 4 | rm test_posix_permissions.zip && 5 | mkdir z && 6 | cd z && 7 | touch 0.txt 1.txt 2.txt 3.txt && 8 | chmod a+x 0.txt && 9 | chmod o+x 1.txt && 10 | chmod u+x 2.txt && 11 | zip test_posix_permissions.zip * && 12 | mv test_posix_permissions.zip .. && 13 | cd .. && 14 | rm -r z 15 | ``` 16 | -------------------------------------------------------------------------------- /google/test/data/Repeated Dir Name.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/Repeated Dir Name.zip -------------------------------------------------------------------------------- /google/test/data/Repeated File Name With Different Cases.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/Repeated File Name With Different Cases.zip -------------------------------------------------------------------------------- /google/test/data/Repeated File Name.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/Repeated File Name.zip -------------------------------------------------------------------------------- /google/test/data/SJIS Bug 846195.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/SJIS Bug 846195.zip -------------------------------------------------------------------------------- /google/test/data/Windows Special Names.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/Windows Special Names.zip -------------------------------------------------------------------------------- /google/test/data/Wrong CRC.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/Wrong CRC.zip -------------------------------------------------------------------------------- /google/test/data/create_test_zip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Copyright 2011 The Chromium Authors 4 | # Use of this source code is governed by a BSD-style license that can be 5 | # found in the LICENSE file. 6 | # 7 | # Run this script in its directory to recreate test.zip, test_nocompress.zip, 8 | # and test_encrypted.zip 9 | 10 | rm test.zip 11 | rm test_nocompress.zip 12 | rm test_encrypted.zip 13 | pushd test 14 | zip -r ../test.zip . 15 | zip -r -0 ../test_nocompress.zip . 16 | zip -P "fake_password" -r ../test_encrypted.zip . 17 | popd 18 | -------------------------------------------------------------------------------- /google/test/data/empty.zip: -------------------------------------------------------------------------------- 1 | PK -------------------------------------------------------------------------------- /google/test/data/evil.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/evil.zip -------------------------------------------------------------------------------- /google/test/data/evil_via_absolute_file_name.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/evil_via_absolute_file_name.zip -------------------------------------------------------------------------------- /google/test/data/evil_via_invalid_utf8.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/evil_via_invalid_utf8.zip -------------------------------------------------------------------------------- /google/test/data/test.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/test.zip -------------------------------------------------------------------------------- /google/test/data/test/foo.txt: -------------------------------------------------------------------------------- 1 | foo 2 | -------------------------------------------------------------------------------- /google/test/data/test/foo/bar.txt: -------------------------------------------------------------------------------- 1 | bar 2 | -------------------------------------------------------------------------------- /google/test/data/test/foo/bar/.hidden: -------------------------------------------------------------------------------- 1 | hidden 2 | -------------------------------------------------------------------------------- /google/test/data/test/foo/bar/baz.txt: -------------------------------------------------------------------------------- 1 | baz 2 | -------------------------------------------------------------------------------- /google/test/data/test_encrypted.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/test_encrypted.zip -------------------------------------------------------------------------------- /google/test/data/test_mismatch_size.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/test_mismatch_size.zip -------------------------------------------------------------------------------- /google/test/data/test_nocompress.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/test_nocompress.zip -------------------------------------------------------------------------------- /google/test/data/test_posix_permissions.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aosp-mirror/platform_external_zlib/f29fc757b1f27c182f36790eb0ccc6cf8ec27e8e/google/test/data/test_posix_permissions.zip -------------------------------------------------------------------------------- /google/test_data.filelist: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | # NOTE: this file is generated by build/ios/update_bundle_filelist.py 5 | # If it requires updating, you should get a presubmit error with 6 | # instructions on how to regenerate. Otherwise, do not edit. 7 | test/data/Different Encryptions.zip 8 | test/data/Empty Dir Same Name As File.zip 9 | test/data/Mixed Paths.zip 10 | test/data/Parent Dir Same Name As File.zip 11 | test/data/README.md 12 | test/data/Repeated Dir Name.zip 13 | test/data/Repeated File Name With Different Cases.zip 14 | test/data/Repeated File Name.zip 15 | test/data/SJIS Bug 846195.zip 16 | test/data/Windows Special Names.zip 17 | test/data/Wrong CRC.zip 18 | test/data/create_test_zip.sh 19 | test/data/empty.zip 20 | test/data/evil.zip 21 | test/data/evil_via_absolute_file_name.zip 22 | test/data/evil_via_invalid_utf8.zip 23 | test/data/test.zip 24 | test/data/test/foo.txt 25 | test/data/test/foo/bar.txt 26 | test/data/test/foo/bar/.hidden 27 | test/data/test/foo/bar/baz.txt 28 | test/data/test/foo/bar/quux.txt 29 | test/data/test_encrypted.zip 30 | test/data/test_mismatch_size.zip 31 | test/data/test_nocompress.zip 32 | test/data/test_posix_permissions.zip 33 | -------------------------------------------------------------------------------- /google/test_data.globlist: -------------------------------------------------------------------------------- 1 | # Copyright 2023 The Chromium Authors 2 | # Use of this source code is governed by a BSD-style license that can be 3 | # found in the LICENSE file. 4 | # 5 | # See build/ios/update_bundle_filelist.py for details on how .globlist 6 | # files are used to update their .filelist counterparts. 7 | test/data/** 8 | test/data/test/foo/bar/.hidden -------------------------------------------------------------------------------- /google/zip_internal.h: -------------------------------------------------------------------------------- 1 | // Copyright 2011 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_INTERNAL_H_ 6 | #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_INTERNAL_H_ 7 | 8 | #include 9 | 10 | #include "base/time/time.h" 11 | #include "build/build_config.h" 12 | 13 | #if defined(OS_WIN) 14 | #include 15 | #endif 16 | 17 | #if defined(USE_SYSTEM_MINIZIP) 18 | #include 19 | #include 20 | #else 21 | #include "third_party/zlib/contrib/minizip/unzip.h" 22 | #include "third_party/zlib/contrib/minizip/zip.h" 23 | #endif 24 | 25 | namespace base { 26 | class FilePath; 27 | } 28 | 29 | // Utility functions and constants used internally for the zip file 30 | // library in the directory. Don't use them outside of the library. 31 | namespace zip { 32 | namespace internal { 33 | 34 | // Opens the given file name in UTF-8 for unzipping, with some setup for 35 | // Windows. 36 | unzFile OpenForUnzipping(const std::string& file_name_utf8); 37 | 38 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) 39 | // Opens the file referred to by |zip_fd| for unzipping. 40 | unzFile OpenFdForUnzipping(int zip_fd); 41 | #endif 42 | 43 | #if defined(OS_WIN) 44 | // Opens the file referred to by |zip_handle| for unzipping. 45 | unzFile OpenHandleForUnzipping(HANDLE zip_handle); 46 | #endif 47 | 48 | // Creates a custom unzFile object which reads data from the specified string. 49 | // This custom unzFile object overrides the I/O API functions of zlib so it can 50 | // read data from the specified string. 51 | unzFile PrepareMemoryForUnzipping(const std::string& data); 52 | 53 | // Opens the given file name in UTF-8 for zipping, with some setup for 54 | // Windows. |append_flag| will be passed to zipOpen2(). 55 | zipFile OpenForZipping(const std::string& file_name_utf8, int append_flag); 56 | 57 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) 58 | // Opens the file referred to by |zip_fd| for zipping. |append_flag| will be 59 | // passed to zipOpen2(). 60 | zipFile OpenFdForZipping(int zip_fd, int append_flag); 61 | #endif 62 | 63 | // Compression methods. 64 | enum Compression { 65 | kStored = 0, // Stored (no compression) 66 | kDeflated = Z_DEFLATED, // Deflated 67 | }; 68 | 69 | // Adds a file (or directory) entry to the ZIP archive. 70 | bool ZipOpenNewFileInZip(zipFile zip_file, 71 | const std::string& str_path, 72 | base::Time last_modified_time, 73 | Compression compression); 74 | 75 | // Selects the best compression method for the given file. The heuristic is 76 | // based on the filename extension. By default, the compression method is 77 | // kDeflated. But if the given path has an extension indicating a well known 78 | // file format which is likely to be already compressed (eg ZIP, RAR, JPG, 79 | // PNG...) then the compression method is simply kStored. 80 | Compression GetCompressionMethod(const base::FilePath& path); 81 | 82 | const int kZipMaxPath = 256; 83 | const int kZipBufSize = 8192; 84 | 85 | } // namespace internal 86 | } // namespace zip 87 | 88 | #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_INTERNAL_H_ 89 | -------------------------------------------------------------------------------- /google/zip_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The Chromium Authors 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | #ifndef THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_ 6 | #define THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_ 7 | 8 | #include 9 | #include 10 | 11 | #include "base/files/file_path.h" 12 | #include "base/time/time.h" 13 | #include "build/build_config.h" 14 | #include "third_party/zlib/google/zip.h" 15 | 16 | #if defined(USE_SYSTEM_MINIZIP) 17 | #include 18 | #include 19 | #else 20 | #include "third_party/zlib/contrib/minizip/unzip.h" 21 | #include "third_party/zlib/contrib/minizip/zip.h" 22 | #endif 23 | 24 | namespace zip { 25 | namespace internal { 26 | 27 | // A class used to write entries to a ZIP file and buffering the reading of 28 | // files to limit the number of calls to the FileAccessor. This is for 29 | // performance reasons as these calls may be expensive when IPC based). 30 | // This class is so far internal and only used by zip.cc, but could be made 31 | // public if needed. 32 | // 33 | // All methods returning a bool return true on success and false on error. 34 | class ZipWriter { 35 | public: 36 | // Creates a writer that will write a ZIP file to |zip_file_fd| or |zip_file| 37 | // and which entries are relative to |file_accessor|'s source directory. 38 | // All file reads are performed using |file_accessor|. 39 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) 40 | static std::unique_ptr CreateWithFd(int zip_file_fd, 41 | FileAccessor* file_accessor); 42 | #endif 43 | 44 | static std::unique_ptr Create(const base::FilePath& zip_file, 45 | FileAccessor* file_accessor); 46 | 47 | ZipWriter(const ZipWriter&) = delete; 48 | ZipWriter& operator=(const ZipWriter&) = delete; 49 | 50 | ~ZipWriter(); 51 | 52 | // Sets the optional progress callback. The callback is called once for each 53 | // time |period|. The final callback is always called when the ZIP operation 54 | // completes. 55 | void SetProgressCallback(ProgressCallback callback, base::TimeDelta period) { 56 | progress_callback_ = std::move(callback); 57 | progress_period_ = std::move(period); 58 | } 59 | 60 | // Should ignore missing files and directories? 61 | void ContinueOnError(bool continue_on_error) { 62 | continue_on_error_ = continue_on_error; 63 | } 64 | 65 | // Sets the recursive flag, indicating whether the contents of subdirectories 66 | // should be included. 67 | void SetRecursive(bool b) { recursive_ = b; } 68 | 69 | // Sets the filter callback. 70 | void SetFilterCallback(FilterCallback callback) { 71 | filter_callback_ = std::move(callback); 72 | } 73 | 74 | // Adds the contents of a directory. If the recursive flag is set, the 75 | // contents of subdirectories are also added. 76 | bool AddDirectoryContents(const base::FilePath& path); 77 | 78 | // Adds the entries at |paths| to the ZIP file. These can be a mixed bag of 79 | // files and directories. If the recursive flag is set, the contents of 80 | // subdirectories is also added. 81 | bool AddMixedEntries(Paths paths); 82 | 83 | // Closes the ZIP file. 84 | bool Close(); 85 | 86 | private: 87 | // Takes ownership of |zip_file|. 88 | ZipWriter(zipFile zip_file, FileAccessor* file_accessor); 89 | 90 | // Regularly called during processing to check whether zipping should continue 91 | // or should be cancelled. 92 | bool ShouldContinue(); 93 | 94 | // Adds file content to currently open file entry. 95 | bool AddFileContent(const base::FilePath& path, base::File file); 96 | 97 | // Adds a file entry (including file contents). 98 | bool AddFileEntry(const base::FilePath& path, base::File file); 99 | 100 | // Adds file entries. All the paths should be existing files. 101 | bool AddFileEntries(Paths paths); 102 | 103 | // Adds a directory entry. If the recursive flag is set, the contents of this 104 | // directory are also added. 105 | bool AddDirectoryEntry(const base::FilePath& path); 106 | 107 | // Adds directory entries. All the paths should be existing directories. If 108 | // the recursive flag is set, the contents of these directories are also 109 | // added. 110 | bool AddDirectoryEntries(Paths paths); 111 | 112 | // Opens a file or directory entry. 113 | bool OpenNewFileEntry(const base::FilePath& path, 114 | bool is_directory, 115 | base::Time last_modified); 116 | 117 | // Closes the currently open entry. 118 | bool CloseNewFileEntry(); 119 | 120 | // Filters entries. 121 | void Filter(std::vector* paths); 122 | 123 | // The actual zip file. 124 | zipFile zip_file_; 125 | 126 | // Abstraction over file access methods used to read files. 127 | FileAccessor* const file_accessor_; 128 | 129 | // Progress stats. 130 | Progress progress_; 131 | 132 | // Optional progress callback. 133 | ProgressCallback progress_callback_; 134 | 135 | // Optional progress reporting period. 136 | base::TimeDelta progress_period_; 137 | 138 | // Next time to report progress. 139 | base::TimeTicks next_progress_report_time_ = base::TimeTicks::Now(); 140 | 141 | // Filter used to exclude files from the ZIP file. 142 | FilterCallback filter_callback_; 143 | 144 | // Should recursively add directories? 145 | bool recursive_ = false; 146 | 147 | // Should ignore missing files and directories? 148 | bool continue_on_error_ = false; 149 | }; 150 | 151 | } // namespace internal 152 | } // namespace zip 153 | 154 | #endif // THIRD_PARTY_ZLIB_GOOGLE_ZIP_WRITER_H_ 155 | -------------------------------------------------------------------------------- /gzclose.c: -------------------------------------------------------------------------------- 1 | /* gzclose.c -- zlib gzclose() function 2 | * Copyright (C) 2004, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #include "gzguts.h" 7 | 8 | /* gzclose() is in a separate file so that it is linked in only if it is used. 9 | That way the other gzclose functions can be used instead to avoid linking in 10 | unneeded compression or decompression routines. */ 11 | int ZEXPORT gzclose(gzFile file) { 12 | #ifndef NO_GZCOMPRESS 13 | gz_statep state; 14 | 15 | if (file == NULL) 16 | return Z_STREAM_ERROR; 17 | state = (gz_statep)file; 18 | 19 | return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file); 20 | #else 21 | return gzclose_r(file); 22 | #endif 23 | } 24 | -------------------------------------------------------------------------------- /gzguts.h: -------------------------------------------------------------------------------- 1 | /* gzguts.h -- zlib internal header definitions for gz* operations 2 | * Copyright (C) 2004-2019 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | #ifdef _LARGEFILE64_SOURCE 7 | # ifndef _LARGEFILE_SOURCE 8 | # define _LARGEFILE_SOURCE 1 9 | # endif 10 | # undef _FILE_OFFSET_BITS 11 | # undef _TIME_BITS 12 | #endif 13 | 14 | #ifdef HAVE_HIDDEN 15 | # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 16 | #else 17 | # define ZLIB_INTERNAL 18 | #endif 19 | 20 | #include 21 | #include "zlib.h" 22 | #ifdef STDC 23 | # include 24 | # include 25 | # include 26 | #endif 27 | 28 | #ifndef _POSIX_SOURCE 29 | # define _POSIX_SOURCE 30 | #endif 31 | #include 32 | 33 | #ifdef _WIN32 34 | # include 35 | #endif 36 | 37 | #if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) 38 | # include 39 | #endif 40 | 41 | #if defined(_WIN32) 42 | # define WIDECHAR 43 | #endif 44 | 45 | #ifdef WINAPI_FAMILY 46 | # define open _open 47 | # define read _read 48 | # define write _write 49 | # define close _close 50 | #endif 51 | 52 | #ifdef NO_DEFLATE /* for compatibility with old definition */ 53 | # define NO_GZCOMPRESS 54 | #endif 55 | 56 | #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) 57 | # ifndef HAVE_VSNPRINTF 58 | # define HAVE_VSNPRINTF 59 | # endif 60 | #endif 61 | 62 | #if defined(__CYGWIN__) 63 | # ifndef HAVE_VSNPRINTF 64 | # define HAVE_VSNPRINTF 65 | # endif 66 | #endif 67 | 68 | #if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) 69 | # ifndef HAVE_VSNPRINTF 70 | # define HAVE_VSNPRINTF 71 | # endif 72 | #endif 73 | 74 | #ifndef HAVE_VSNPRINTF 75 | # ifdef MSDOS 76 | /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), 77 | but for now we just assume it doesn't. */ 78 | # define NO_vsnprintf 79 | # endif 80 | # ifdef __TURBOC__ 81 | # define NO_vsnprintf 82 | # endif 83 | # ifdef WIN32 84 | /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ 85 | # if !defined(vsnprintf) && !defined(NO_vsnprintf) 86 | # if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) 87 | # define vsnprintf _vsnprintf 88 | # endif 89 | # endif 90 | # endif 91 | # ifdef __SASC 92 | # define NO_vsnprintf 93 | # endif 94 | # ifdef VMS 95 | # define NO_vsnprintf 96 | # endif 97 | # ifdef __OS400__ 98 | # define NO_vsnprintf 99 | # endif 100 | # ifdef __MVS__ 101 | # define NO_vsnprintf 102 | # endif 103 | #endif 104 | 105 | /* unlike snprintf (which is required in C99), _snprintf does not guarantee 106 | null termination of the result -- however this is only used in gzlib.c where 107 | the result is assured to fit in the space provided */ 108 | #if defined(_MSC_VER) && _MSC_VER < 1900 109 | # define snprintf _snprintf 110 | #endif 111 | 112 | #ifndef local 113 | # define local static 114 | #endif 115 | /* since "static" is used to mean two completely different things in C, we 116 | define "local" for the non-static meaning of "static", for readability 117 | (compile with -Dlocal if your debugger can't find static symbols) */ 118 | 119 | /* gz* functions always use library allocation functions */ 120 | #ifndef STDC 121 | extern voidp malloc(uInt size); 122 | extern void free(voidpf ptr); 123 | #endif 124 | 125 | /* get errno and strerror definition */ 126 | #if defined UNDER_CE 127 | # include 128 | # define zstrerror() gz_strwinerror((DWORD)GetLastError()) 129 | #else 130 | # ifndef NO_STRERROR 131 | # include 132 | # define zstrerror() strerror(errno) 133 | # else 134 | # define zstrerror() "stdio error (consult errno)" 135 | # endif 136 | #endif 137 | 138 | /* provide prototypes for these when building zlib without LFS */ 139 | #if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 140 | ZEXTERN gzFile ZEXPORT gzopen64(const char *, const char *); 141 | ZEXTERN z_off64_t ZEXPORT gzseek64(gzFile, z_off64_t, int); 142 | ZEXTERN z_off64_t ZEXPORT gztell64(gzFile); 143 | ZEXTERN z_off64_t ZEXPORT gzoffset64(gzFile); 144 | #endif 145 | 146 | /* default memLevel */ 147 | #if MAX_MEM_LEVEL >= 8 148 | # define DEF_MEM_LEVEL 8 149 | #else 150 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 151 | #endif 152 | 153 | /* default i/o buffer size -- double this for output when reading (this and 154 | twice this must be able to fit in an unsigned type) */ 155 | #define GZBUFSIZE 8192 156 | 157 | /* gzip modes, also provide a little integrity check on the passed structure */ 158 | #define GZ_NONE 0 159 | #define GZ_READ 7247 160 | #define GZ_WRITE 31153 161 | #define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ 162 | 163 | /* values for gz_state how */ 164 | #define LOOK 0 /* look for a gzip header */ 165 | #define COPY 1 /* copy input directly */ 166 | #define GZIP 2 /* decompress a gzip stream */ 167 | 168 | /* internal gzip file state data structure */ 169 | typedef struct { 170 | /* exposed contents for gzgetc() macro */ 171 | struct gzFile_s x; /* "x" for exposed */ 172 | /* x.have: number of bytes available at x.next */ 173 | /* x.next: next output data to deliver or write */ 174 | /* x.pos: current position in uncompressed data */ 175 | /* used for both reading and writing */ 176 | int mode; /* see gzip modes above */ 177 | int fd; /* file descriptor */ 178 | char *path; /* path or fd for error messages */ 179 | unsigned size; /* buffer size, zero if not allocated yet */ 180 | unsigned want; /* requested buffer size, default is GZBUFSIZE */ 181 | unsigned char *in; /* input buffer (double-sized when writing) */ 182 | unsigned char *out; /* output buffer (double-sized when reading) */ 183 | int direct; /* 0 if processing gzip, 1 if transparent */ 184 | /* just for reading */ 185 | int how; /* 0: get header, 1: copy, 2: decompress */ 186 | z_off64_t start; /* where the gzip data started, for rewinding */ 187 | int eof; /* true if end of input file reached */ 188 | int past; /* true if read requested past end */ 189 | /* just for writing */ 190 | int level; /* compression level */ 191 | int strategy; /* compression strategy */ 192 | int reset; /* true if a reset is pending after a Z_FINISH */ 193 | /* seek request */ 194 | z_off64_t skip; /* amount to skip (already rewound if backwards) */ 195 | int seek; /* true if seek request pending */ 196 | /* error information */ 197 | int err; /* error code */ 198 | char *msg; /* error message */ 199 | /* zlib inflate or deflate stream */ 200 | z_stream strm; /* stream structure in-place (not a pointer) */ 201 | } gz_state; 202 | typedef gz_state FAR *gz_statep; 203 | 204 | /* shared functions */ 205 | void ZLIB_INTERNAL gz_error(gz_statep, int, const char *); 206 | #if defined UNDER_CE 207 | char ZLIB_INTERNAL *gz_strwinerror(DWORD error); 208 | #endif 209 | 210 | /* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t 211 | value -- needed when comparing unsigned to z_off64_t, which is signed 212 | (possible z_off64_t types off_t, off64_t, and long are all signed) */ 213 | #ifdef INT_MAX 214 | # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX) 215 | #else 216 | unsigned ZLIB_INTERNAL gz_intmax(void); 217 | # define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) 218 | #endif 219 | -------------------------------------------------------------------------------- /inffast.h: -------------------------------------------------------------------------------- 1 | /* inffast.h -- header to use inffast.c 2 | * Copyright (C) 1995-2003, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* INFLATE_FAST_MIN_INPUT: the minimum number of input bytes needed so that 12 | we can safely call inflate_fast() with only one up-front bounds check. One 13 | length/distance code pair (15 bits for the length code, 5 bits for length 14 | extra, 15 bits for the distance code, 13 bits for distance extra) requires 15 | reading up to 48 input bits (6 bytes). 16 | */ 17 | #define INFLATE_FAST_MIN_INPUT 6 18 | 19 | /* INFLATE_FAST_MIN_OUTPUT: the minimum number of output bytes needed so that 20 | we can safely call inflate_fast() with only one up-front bounds check. One 21 | length/distance code pair can output up to 258 bytes, which is the maximum 22 | length that can be coded. 23 | */ 24 | #define INFLATE_FAST_MIN_OUTPUT 258 25 | 26 | void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start); 27 | -------------------------------------------------------------------------------- /inffixed.h: -------------------------------------------------------------------------------- 1 | /* inffixed.h -- table for decoding fixed codes 2 | * Generated automatically by makefixed(). 3 | */ 4 | 5 | /* WARNING: this file should *not* be used by applications. 6 | It is part of the implementation of this library and is 7 | subject to change. Applications should only use zlib.h. 8 | */ 9 | 10 | static const code lenfix[512] = { 11 | {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48}, 12 | {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128}, 13 | {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59}, 14 | {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176}, 15 | {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20}, 16 | {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100}, 17 | {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8}, 18 | {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216}, 19 | {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76}, 20 | {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114}, 21 | {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2}, 22 | {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148}, 23 | {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42}, 24 | {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86}, 25 | {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15}, 26 | {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236}, 27 | {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62}, 28 | {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142}, 29 | {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31}, 30 | {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162}, 31 | {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25}, 32 | {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105}, 33 | {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4}, 34 | {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202}, 35 | {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69}, 36 | {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125}, 37 | {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13}, 38 | {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195}, 39 | {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35}, 40 | {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91}, 41 | {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19}, 42 | {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246}, 43 | {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55}, 44 | {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135}, 45 | {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99}, 46 | {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190}, 47 | {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16}, 48 | {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96}, 49 | {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6}, 50 | {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209}, 51 | {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72}, 52 | {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116}, 53 | {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4}, 54 | {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153}, 55 | {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44}, 56 | {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82}, 57 | {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11}, 58 | {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229}, 59 | {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58}, 60 | {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138}, 61 | {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51}, 62 | {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173}, 63 | {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30}, 64 | {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110}, 65 | {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0}, 66 | {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195}, 67 | {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65}, 68 | {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121}, 69 | {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9}, 70 | {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258}, 71 | {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37}, 72 | {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93}, 73 | {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23}, 74 | {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251}, 75 | {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51}, 76 | {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131}, 77 | {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67}, 78 | {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183}, 79 | {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23}, 80 | {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103}, 81 | {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9}, 82 | {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223}, 83 | {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79}, 84 | {0,9,255} 85 | }; 86 | 87 | static const code distfix[32] = { 88 | {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025}, 89 | {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193}, 90 | {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385}, 91 | {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577}, 92 | {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073}, 93 | {22,5,193},{64,5,0} 94 | }; 95 | -------------------------------------------------------------------------------- /inflate.h: -------------------------------------------------------------------------------- 1 | /* inflate.h -- internal inflate state definition 2 | * Copyright (C) 1995-2019 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* define NO_GZIP when compiling if you want to disable gzip header and 12 | trailer decoding by inflate(). NO_GZIP would be used to avoid linking in 13 | the crc code when it is not needed. For shared libraries, gzip decoding 14 | should be left enabled. */ 15 | #ifndef NO_GZIP 16 | # define GUNZIP 17 | #endif 18 | 19 | /* Possible inflate modes between inflate() calls */ 20 | typedef enum { 21 | HEAD = 16180, /* i: waiting for magic header */ 22 | FLAGS, /* i: waiting for method and flags (gzip) */ 23 | TIME, /* i: waiting for modification time (gzip) */ 24 | OS, /* i: waiting for extra flags and operating system (gzip) */ 25 | EXLEN, /* i: waiting for extra length (gzip) */ 26 | EXTRA, /* i: waiting for extra bytes (gzip) */ 27 | NAME, /* i: waiting for end of file name (gzip) */ 28 | COMMENT, /* i: waiting for end of comment (gzip) */ 29 | HCRC, /* i: waiting for header crc (gzip) */ 30 | DICTID, /* i: waiting for dictionary check value */ 31 | DICT, /* waiting for inflateSetDictionary() call */ 32 | TYPE, /* i: waiting for type bits, including last-flag bit */ 33 | TYPEDO, /* i: same, but skip check to exit inflate on new block */ 34 | STORED, /* i: waiting for stored size (length and complement) */ 35 | COPY_, /* i/o: same as COPY below, but only first time in */ 36 | COPY, /* i/o: waiting for input or output to copy stored block */ 37 | TABLE, /* i: waiting for dynamic block table lengths */ 38 | LENLENS, /* i: waiting for code length code lengths */ 39 | CODELENS, /* i: waiting for length/lit and distance code lengths */ 40 | LEN_, /* i: same as LEN below, but only first time in */ 41 | LEN, /* i: waiting for length/lit/eob code */ 42 | LENEXT, /* i: waiting for length extra bits */ 43 | DIST, /* i: waiting for distance code */ 44 | DISTEXT, /* i: waiting for distance extra bits */ 45 | MATCH, /* o: waiting for output space to copy string */ 46 | LIT, /* o: waiting for output space to write literal */ 47 | CHECK, /* i: waiting for 32-bit check value */ 48 | LENGTH, /* i: waiting for 32-bit length (gzip) */ 49 | DONE, /* finished check, done -- remain here until reset */ 50 | BAD, /* got a data error -- remain here until reset */ 51 | MEM, /* got an inflate() memory error -- remain here until reset */ 52 | SYNC /* looking for synchronization bytes to restart inflate() */ 53 | } inflate_mode; 54 | 55 | /* 56 | State transitions between above modes - 57 | 58 | (most modes can go to BAD or MEM on error -- not shown for clarity) 59 | 60 | Process header: 61 | HEAD -> (gzip) or (zlib) or (raw) 62 | (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT -> 63 | HCRC -> TYPE 64 | (zlib) -> DICTID or TYPE 65 | DICTID -> DICT -> TYPE 66 | (raw) -> TYPEDO 67 | Read deflate blocks: 68 | TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK 69 | STORED -> COPY_ -> COPY -> TYPE 70 | TABLE -> LENLENS -> CODELENS -> LEN_ 71 | LEN_ -> LEN 72 | Read deflate codes in fixed or dynamic block: 73 | LEN -> LENEXT or LIT or TYPE 74 | LENEXT -> DIST -> DISTEXT -> MATCH -> LEN 75 | LIT -> LEN 76 | Process trailer: 77 | CHECK -> LENGTH -> DONE 78 | */ 79 | 80 | /* State maintained between inflate() calls -- approximately 7K bytes, not 81 | including the allocated sliding window, which is up to 32K bytes. */ 82 | struct inflate_state { 83 | z_streamp strm; /* pointer back to this zlib stream */ 84 | inflate_mode mode; /* current inflate mode */ 85 | int last; /* true if processing last block */ 86 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip, 87 | bit 2 true to validate check value */ 88 | int havedict; /* true if dictionary provided */ 89 | int flags; /* gzip header method and flags, 0 if zlib, or 90 | -1 if raw or no header yet */ 91 | unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ 92 | unsigned long check; /* protected copy of check value */ 93 | unsigned long total; /* protected copy of output count */ 94 | gz_headerp head; /* where to save gzip header information */ 95 | /* sliding window */ 96 | unsigned wbits; /* log base 2 of requested window size */ 97 | unsigned wsize; /* window size or zero if not using window */ 98 | unsigned whave; /* valid bytes in the window */ 99 | unsigned wnext; /* window write index */ 100 | unsigned char FAR *window; /* allocated sliding window, if needed */ 101 | /* bit accumulator */ 102 | unsigned long hold; /* input bit accumulator */ 103 | unsigned bits; /* number of bits in "in" */ 104 | /* for string and stored block copying */ 105 | unsigned length; /* literal or length of data to copy */ 106 | unsigned offset; /* distance back to copy string from */ 107 | /* for table and code decoding */ 108 | unsigned extra; /* extra bits needed */ 109 | /* fixed and dynamic code tables */ 110 | code const FAR *lencode; /* starting table for length/literal codes */ 111 | code const FAR *distcode; /* starting table for distance codes */ 112 | unsigned lenbits; /* index bits for lencode */ 113 | unsigned distbits; /* index bits for distcode */ 114 | /* dynamic table building */ 115 | unsigned ncode; /* number of code length code lengths */ 116 | unsigned nlen; /* number of length code lengths */ 117 | unsigned ndist; /* number of distance code lengths */ 118 | unsigned have; /* number of code lengths in lens[] */ 119 | code FAR *next; /* next available space in codes[] */ 120 | unsigned short lens[320]; /* temporary storage for code lengths */ 121 | unsigned short work[288]; /* work area for code table building */ 122 | code codes[ENOUGH]; /* space for code tables */ 123 | int sane; /* if false, allow invalid distance too far */ 124 | int back; /* bits back of last unprocessed length/lit */ 125 | unsigned was; /* initial length of match */ 126 | }; 127 | -------------------------------------------------------------------------------- /inftrees.h: -------------------------------------------------------------------------------- 1 | /* inftrees.h -- header to use inftrees.c 2 | * Copyright (C) 1995-2005, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* Structure for decoding tables. Each entry provides either the 12 | information needed to do the operation requested by the code that 13 | indexed that table entry, or it provides a pointer to another 14 | table that indexes more bits of the code. op indicates whether 15 | the entry is a pointer to another table, a literal, a length or 16 | distance, an end-of-block, or an invalid code. For a table 17 | pointer, the low four bits of op is the number of index bits of 18 | that table. For a length or distance, the low four bits of op 19 | is the number of extra bits to get after the code. bits is 20 | the number of bits in this code or part of the code to drop off 21 | of the bit buffer. val is the actual byte to output in the case 22 | of a literal, the base length or distance, or the offset from 23 | the current table to the next table. Each entry is four bytes. */ 24 | typedef struct { 25 | unsigned char op; /* operation, extra bits, table bits */ 26 | unsigned char bits; /* bits in this part of the code */ 27 | unsigned short val; /* offset in table or code value */ 28 | } code; 29 | 30 | /* op values as set by inflate_table(): 31 | 00000000 - literal 32 | 0000tttt - table link, tttt != 0 is the number of table index bits 33 | 0001eeee - length or distance, eeee is the number of extra bits 34 | 01100000 - end of block 35 | 01000000 - invalid code 36 | */ 37 | 38 | /* Maximum size of the dynamic table. The maximum number of code structures is 39 | 1924, which is the sum of 1332 for literal/length codes and 592 for distance 40 | codes. These values were found by exhaustive searches using the program 41 | examples/enough.c found in the zlib distribution. The arguments to that 42 | program are the number of symbols, the initial root table size, and the 43 | maximum bit length of a code. "enough 286 10 15" for literal/length codes 44 | returns returns 1332, and "enough 30 9 15" for distance codes returns 592. 45 | The initial root table size (10 or 9) is found in the fifth argument of the 46 | inflate_table() calls in inflate.c and infback.c. If the root table size is 47 | changed, then these maximum sizes would be need to be recalculated and 48 | updated. */ 49 | #define ENOUGH_LENS 1332 50 | #define ENOUGH_DISTS 592 51 | #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS) 52 | 53 | /* Type of code to build for inflate_table() */ 54 | typedef enum { 55 | CODES, 56 | LENS, 57 | DISTS 58 | } codetype; 59 | 60 | int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens, 61 | unsigned codes, code FAR * FAR *table, 62 | unsigned FAR *bits, unsigned short FAR *work); 63 | -------------------------------------------------------------------------------- /libz.map.txt: -------------------------------------------------------------------------------- 1 | # This file is copied from src/zlib.map and annotated with comments for the NDK 2 | # stub library generation script. 3 | ZLIB_1.2.0 { 4 | global: 5 | compressBound; 6 | deflateBound; 7 | inflateBack; 8 | inflateBackEnd; 9 | inflateBackInit_; 10 | inflateCopy; 11 | local: 12 | deflate_copyright; # var 13 | inflate_copyright; # var 14 | inflate_fast; 15 | inflate_table; 16 | zcalloc; 17 | zcfree; 18 | z_errmsg; # var 19 | gz_error; 20 | gz_intmax; 21 | _*; 22 | }; 23 | 24 | ZLIB_1.2.0.2 { 25 | gzclearerr; 26 | gzungetc; 27 | zlibCompileFlags; 28 | } ZLIB_1.2.0; 29 | 30 | ZLIB_1.2.0.8 { 31 | deflatePrime; 32 | } ZLIB_1.2.0.2; 33 | 34 | ZLIB_1.2.2 { 35 | adler32_combine; 36 | crc32_combine; 37 | deflateSetHeader; 38 | inflateGetHeader; 39 | } ZLIB_1.2.0.8; 40 | 41 | ZLIB_1.2.2.3 { 42 | deflateTune; 43 | gzdirect; 44 | } ZLIB_1.2.2; 45 | 46 | ZLIB_1.2.2.4 { 47 | inflatePrime; 48 | } ZLIB_1.2.2.3; 49 | 50 | ZLIB_1.2.3.3 { 51 | adler32_combine64; 52 | crc32_combine64; 53 | gzopen64; 54 | gzseek64; 55 | gztell64; 56 | inflateUndermine; 57 | } ZLIB_1.2.2.4; 58 | 59 | ZLIB_1.2.3.4 { 60 | inflateReset2; 61 | inflateMark; 62 | } ZLIB_1.2.3.3; 63 | 64 | ZLIB_1.2.3.5 { 65 | gzbuffer; 66 | gzoffset; 67 | gzoffset64; 68 | gzclose_r; 69 | gzclose_w; 70 | } ZLIB_1.2.3.4; 71 | 72 | ZLIB_1.2.5.1 { 73 | deflatePending; 74 | } ZLIB_1.2.3.5; 75 | 76 | ZLIB_1.2.5.2 { 77 | deflateResetKeep; 78 | gzgetc_; 79 | inflateResetKeep; 80 | } ZLIB_1.2.5.1; 81 | 82 | ZLIB_1.2.7.1 { # introduced=19 83 | inflateGetDictionary; 84 | gzvprintf; 85 | } ZLIB_1.2.5.2; 86 | 87 | ZLIB_1.2.9 { # introduced=28 88 | inflateCodesUsed; 89 | inflateValidate; 90 | uncompress2; 91 | gzfread; 92 | gzfwrite; 93 | deflateGetDictionary; 94 | adler32_z; 95 | crc32_z; 96 | } ZLIB_1.2.7.1; 97 | 98 | # These were all exposed by the old NDK stub library. Unclear if they still 99 | # should be, but at least some of them are marked as being exported in zlib.h 100 | # and the tree doesn't build without them. 101 | ZLIB_NDK { 102 | _dist_code; 103 | _length_code; 104 | _tr_align; 105 | _tr_flush_bits; # introduced=21 106 | _tr_flush_block; 107 | _tr_init; 108 | _tr_stored_block; 109 | _tr_tally; 110 | adler32; 111 | compress2; 112 | compress; 113 | crc32; 114 | deflate; 115 | deflateCopy; 116 | deflateEnd; 117 | deflateInit2_; 118 | deflateInit_; 119 | deflateParams; 120 | deflateReset; 121 | deflateSetDictionary; 122 | get_crc_table; 123 | gzclose; 124 | gzdopen; 125 | gzeof; 126 | gzerror; 127 | gzflush; 128 | gzgetc; 129 | gzgets; 130 | gzopen; 131 | gzprintf; 132 | gzputc; 133 | gzputs; 134 | gzread; 135 | gzrewind; 136 | gzseek; 137 | gzsetparams; 138 | gztell; 139 | gzwrite; 140 | inflate; 141 | inflateEnd; 142 | inflateInit2_; 143 | inflateInit_; 144 | inflateReset; 145 | inflateSetDictionary; 146 | inflateSync; 147 | inflateSyncPoint; 148 | uncompress; 149 | zError; 150 | zlibVersion; 151 | }; 152 | -------------------------------------------------------------------------------- /patches/0000-build.patch: -------------------------------------------------------------------------------- 1 | diff --git a/contrib/minizip/ioapi.c b/contrib/minizip/ioapi.c 2 | index 782d32469ae5d..a38881dca90a2 100644 3 | --- a/contrib/minizip/ioapi.c 4 | +++ b/contrib/minizip/ioapi.c 5 | @@ -14,7 +14,7 @@ 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #endif 8 | 9 | -#if defined(__APPLE__) || defined(IOAPI_NO_64) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) 10 | +#if defined(__APPLE__) || defined(__Fuchsia__) || defined(IOAPI_NO_64) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) 11 | // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions 12 | #define FOPEN_FUNC(filename, mode) fopen(filename, mode) 13 | #define FTELLO_FUNC(stream) ftello(stream) 14 | diff --git a/contrib/minizip/iowin32.c b/contrib/minizip/iowin32.c 15 | index 08536e94b8a28..bbd7773e67146 100644 16 | --- a/contrib/minizip/iowin32.c 17 | +++ b/contrib/minizip/iowin32.c 18 | @@ -25,7 +25,12 @@ 19 | #define INVALID_SET_FILE_POINTER ((DWORD)-1) 20 | #endif 21 | 22 | +#ifdef _WIN32_WINNT 23 | +#undef _WIN32_WINNT 24 | +#define _WIN32_WINNT 0x601 25 | +#endif 26 | 27 | +#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 28 | // see Include/shared/winapifamily.h in the Windows Kit 29 | #if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API))) 30 | 31 | @@ -37,6 +42,7 @@ 32 | #define IOWIN32_USING_WINRT_API 1 33 | #endif 34 | #endif 35 | +#endif 36 | 37 | typedef struct 38 | { 39 | diff --git a/gzread.c b/gzread.c 40 | index 956b91ea7d9e..832d3ef98c59 100644 41 | --- a/gzread.c 42 | +++ b/gzread.c 43 | @@ -443,7 +443,11 @@ z_size_t ZEXPORT gzfread(buf, size, nitems, file) 44 | # undef z_gzgetc 45 | #else 46 | # undef gzgetc 47 | +# ifdef Z_CR_PREFIX_SET 48 | +# define gzgetc Cr_z_gzgetc 49 | +# endif 50 | #endif 51 | + 52 | int ZEXPORT gzgetc(file) 53 | gzFile file; 54 | { 55 | diff --git a/zconf.h b/zconf.h 56 | index 5e1d68a004e9..a7a815f575a7 100644 57 | --- a/zconf.h 58 | +++ b/zconf.h 59 | @@ -8,6 +8,10 @@ 60 | #ifndef ZCONF_H 61 | #define ZCONF_H 62 | 63 | +/* 64 | + * This library is also built as a part of AOSP, which does not need to include 65 | + * chromeconf.h. This config does not want chromeconf.h, so it can set this 66 | + * macro to opt out. While this works today, there's no guarantee that building 67 | + * zlib outside of Chromium keeps working in the future. 68 | + */ 69 | +#if !defined(CHROMIUM_ZLIB_NO_CHROMECONF) 70 | +/* This include does prefixing as below, but with an updated set of names. Also 71 | + * sets up export macros in component builds. */ 72 | +#include "chromeconf.h" 73 | +#endif 74 | + 75 | /* 76 | * If you *really* need a unique prefix for all types and library functions, 77 | * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. 78 | @@ -431,7 +434,7 @@ typedef uLong FAR uLongf; 79 | typedef unsigned long z_crc_t; 80 | #endif 81 | 82 | -#ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ 83 | +#if !defined(_WIN32) 84 | # define Z_HAVE_UNISTD_H 85 | #endif 86 | 87 | diff --git a/zlib.h b/zlib.h 88 | index f09cdaf1e054..99fd467f6b1a 100644 89 | --- a/zlib.h 90 | +++ b/zlib.h 91 | @@ -1824,6 +1824,11 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ 92 | # undef z_gzgetc 93 | # define z_gzgetc(g) \ 94 | ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) 95 | +#elif defined(Z_CR_PREFIX_SET) 96 | +# undef gzgetc 97 | +# define gzgetc(g) \ 98 | + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) \ 99 | + : (Cr_z_gzgetc)(g)) 100 | #else 101 | # define gzgetc(g) \ 102 | ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : (gzgetc)(g)) 103 | @@ -1853,11 +1858,29 @@ ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ 104 | # define z_adler32_combine z_adler32_combine64 105 | # define z_crc32_combine z_crc32_combine64 106 | # else 107 | +# ifdef gzopen 108 | +# undef gzopen 109 | +# endif 110 | # define gzopen gzopen64 111 | +# ifdef gzseek 112 | +# undef gzseek 113 | +# endif 114 | # define gzseek gzseek64 115 | +# ifdef gztell 116 | +# undef gztell 117 | +# endif 118 | # define gztell gztell64 119 | +# ifdef gzoffset 120 | +# undef gzoffset 121 | +# endif 122 | # define gzoffset gzoffset64 123 | +# ifdef adler32_combine 124 | +# undef adler32_combine 125 | +# endif 126 | # define adler32_combine adler32_combine64 127 | +# ifdef crc32_combine 128 | +# undef crc32_combine 129 | +# endif 130 | # define crc32_combine crc32_combine64 131 | # endif 132 | # ifndef Z_LARGE64 133 | diff --git a/zutil.h b/zutil.h 134 | index b079ea6a80f5..80375b8b6109 100644 135 | --- a/zutil.h 136 | +++ b/zutil.h 137 | @@ -28,6 +28,21 @@ 138 | # include 139 | # include 140 | #endif 141 | +#ifdef NO_ERRNO_H 142 | +# ifdef _WIN32_WCE 143 | + /* The Microsoft C Run-Time Library for Windows CE doesn't have 144 | + * errno. We define it as a global variable to simplify porting. 145 | + * Its value is always 0 and should not be used. We rename it to 146 | + * avoid conflict with other libraries that use the same workaround. 147 | + */ 148 | +# define errno z_errno 149 | +# endif 150 | + extern int errno; 151 | +#else 152 | +# ifndef _WIN32_WCE 153 | +# include 154 | +# endif 155 | +#endif 156 | 157 | #ifdef Z_SOLO 158 | typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */ 159 | -------------------------------------------------------------------------------- /patches/0002-uninitializedcheck.patch: -------------------------------------------------------------------------------- 1 | From 1a03e7aec95d89c659fd91f195b99893b6458cd7 Mon Sep 17 00:00:00 2001 2 | From: Adenilson Cavalcanti 3 | Date: Wed, 27 Sep 2017 13:59:04 -0700 4 | Subject: [PATCH] Zlib patch: prevent uninitialized use of state->check 5 | 6 | No need to call the Adler32 checksum function, just set 7 | the struct field to the expected value. 8 | 9 | Upstream bug: madler/zlib#245 10 | --- 11 | third_party/zlib/inflate.c | 2 +- 12 | 1 files changed, 1 insertion(+), 1 deletions(-) 13 | delete mode 100644 third_party/zlib/patches/0002-uninitializedcheck.patch 14 | 15 | diff --git a/third_party/zlib/inflate.c b/third_party/zlib/inflate.c 16 | index bec9497..5c40cf1 100644 17 | --- a/third_party/zlib/inflate.c 18 | +++ b/third_party/zlib/inflate.c 19 | @@ -228,7 +228,7 @@ int stream_size; 20 | state->strm = strm; 21 | state->window = Z_NULL; 22 | state->mode = HEAD; /* to pass state test in inflateReset2() */ 23 | - state->check = adler32(0L, Z_NULL, 0); 24 | + state->check = 1L; /* 1L is the result of adler32() zero length data */ 25 | ret = inflateReset2(strm, windowBits); 26 | if (ret != Z_OK) { 27 | ZFREE(strm, state); 28 | -------------------------------------------------------------------------------- /patches/0003-uninitializedjump.patch: -------------------------------------------------------------------------------- 1 | diff --git a/third_party/zlib/deflate.c b/third_party/zlib/deflate.c 2 | index a39e62787862..c6053fd1c7ea 100644 3 | --- a/third_party/zlib/deflate.c 4 | +++ b/third_party/zlib/deflate.c 5 | @@ -318,6 +318,10 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, 6 | s->w_size + window_padding, 7 | 2*sizeof(Byte)); 8 | s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 9 | + /* Avoid use of uninitialized value, see: 10 | + * https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11360 11 | + */ 12 | + zmemzero(s->prev, s->w_size * sizeof(Pos)); 13 | s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); 14 | 15 | s->high_water = 0; /* nothing written to s->window yet */ 16 | -------------------------------------------------------------------------------- /patches/0004-fix-uwp.patch: -------------------------------------------------------------------------------- 1 | diff --git a/third_party/zlib/contrib/minizip/iowin32.c b/third_party/zlib/contrib/minizip/iowin32.c 2 | index bbd7773e67146..3f6867fd7e40b 100644 3 | --- a/third_party/zlib/contrib/minizip/iowin32.c 4 | +++ b/third_party/zlib/contrib/minizip/iowin32.c 5 | @@ -30,19 +30,12 @@ 6 | #define _WIN32_WINNT 0x601 7 | #endif 8 | 9 | -#if _WIN32_WINNT >= _WIN32_WINNT_WIN8 10 | -// see Include/shared/winapifamily.h in the Windows Kit 11 | -#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API))) 12 | - 13 | -#if !defined(WINAPI_FAMILY_ONE_PARTITION) 14 | -#define WINAPI_FAMILY_ONE_PARTITION(PartitionSet, Partition) ((WINAPI_FAMILY & PartitionSet) == Partition) 15 | -#endif 16 | - 17 | -#if WINAPI_FAMILY_ONE_PARTITION(WINAPI_FAMILY, WINAPI_PARTITION_APP) 18 | +#if !defined(IOWIN32_USING_WINRT_API) 19 | +#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) 20 | +// Windows Store or Universal Windows Platform 21 | #define IOWIN32_USING_WINRT_API 1 22 | #endif 23 | #endif 24 | -#endif 25 | 26 | typedef struct 27 | { 28 | -------------------------------------------------------------------------------- /patches/0006-fix-check_match.patch: -------------------------------------------------------------------------------- 1 | From 8304bdda5293ffd5b3efce8e4f54904b387029d6 Mon Sep 17 00:00:00 2001 2 | From: Hans Wennborg 3 | Date: Wed, 23 Sep 2020 16:36:38 +0200 4 | Subject: [PATCH] Avoid crashing in check_match when prev_match == -1 5 | 6 | prev_match can be set to -1 after sliding the window. In that case, the 7 | window has slid past the first byte of the last match, which means it 8 | cannot be compared in check_match. 9 | 10 | This would cause zlib to crash on some inputs to deflate when built 11 | with ZLIB_DEBUG enabled. 12 | 13 | Check for this situation and avoid crashing by not trying to compare 14 | the first byte. 15 | 16 | Bug: 1113142 17 | --- 18 | third_party/zlib/deflate.c | 8 +++++++- 19 | 1 file changed, 7 insertions(+), 1 deletion(-) 20 | 21 | diff --git a/third_party/zlib/deflate.c b/third_party/zlib/deflate.c 22 | index cfdd2f46b230..d70732ec6fc2 100644 23 | --- a/third_party/zlib/deflate.c 24 | +++ b/third_party/zlib/deflate.c 25 | @@ -2060,7 +2060,13 @@ local block_state deflate_slow(s, flush) 26 | uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; 27 | /* Do not insert strings in hash table beyond this. */ 28 | 29 | - check_match(s, s->strstart-1, s->prev_match, s->prev_length); 30 | + if (s->prev_match == -1) { 31 | + /* The window has slid one byte past the previous match, 32 | + * so the first byte cannot be compared. */ 33 | + check_match(s, s->strstart, s->prev_match+1, s->prev_length-1); 34 | + } else { 35 | + check_match(s, s->strstart-1, s->prev_match, s->prev_length); 36 | + } 37 | 38 | _tr_tally_dist(s, s->strstart -1 - s->prev_match, 39 | s->prev_length - MIN_MATCH, bflush); 40 | -- 41 | 2.28.0.681.g6f77f65b4e-goog 42 | 43 | -------------------------------------------------------------------------------- /patches/0007-zero-init-deflate-window.patch: -------------------------------------------------------------------------------- 1 | From 92537ee19784e0e545f06d89b7d89ab532a18cff Mon Sep 17 00:00:00 2001 2 | From: Hans Wennborg 3 | Date: Tue, 3 Nov 2020 15:54:09 +0100 4 | Subject: [PATCH] [zlib] Zero-initialize the window used for deflation 5 | 6 | Otherwise MSan complains about use-of-uninitialized values in the 7 | window. 8 | This happens in both regular deflate's longest_match and deflate_rle. 9 | 10 | Before crrev.com/822755 we used to suppress those reports, but it seems 11 | better to fix it properly. That will also allow us to catch other 12 | potential issues with MSan in these functions. 13 | 14 | The instances of this that we've seen only reproduce with 15 | fill_window_sse(), not with the regular fill_window() function. Since 16 | the former doesn't exist in upstream zlib, I'm not planning to send this 17 | patch upstream. 18 | 19 | Bug: 1137613, 1144420 20 | --- 21 | third_party/zlib/deflate.c | 3 +++ 22 | 1 file changed, 3 insertions(+) 23 | 24 | diff --git a/third_party/zlib/deflate.c b/third_party/zlib/deflate.c 25 | index 8bf93e524875..fc7ae45905ff 100644 26 | --- a/third_party/zlib/deflate.c 27 | +++ b/third_party/zlib/deflate.c 28 | @@ -321,6 +321,9 @@ int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, 29 | s->window = (Bytef *) ZALLOC(strm, 30 | s->w_size + window_padding, 31 | 2*sizeof(Byte)); 32 | + /* Avoid use of unitialized values in the window, see crbug.com/1137613 and 33 | + * crbug.com/1144420 */ 34 | + zmemzero(s->window, (s->w_size + window_padding) * (2 * sizeof(Byte))); 35 | s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); 36 | /* Avoid use of uninitialized value, see: 37 | * https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11360 38 | -- 39 | 2.29.1.341.ge80a0c044ae-goog 40 | 41 | -------------------------------------------------------------------------------- /patches/0008-minizip-zip-unzip-tools.patch: -------------------------------------------------------------------------------- 1 | From 0c7de17000659f4f79de878296892c46be0aff77 Mon Sep 17 00:00:00 2001 2 | From: Noel Gordon 3 | Date: Wed, 26 May 2021 21:57:43 +1000 4 | Subject: [PATCH] Build minizip zip and unzip tools 5 | 6 | --- 7 | third_party/zlib/contrib/minizip/miniunz.c | 13 ++++++------- 8 | third_party/zlib/contrib/minizip/minizip.c | 7 +++---- 9 | 2 files changed, 9 insertions(+), 11 deletions(-) 10 | 11 | diff --git a/third_party/zlib/contrib/minizip/miniunz.c b/third_party/zlib/contrib/minizip/miniunz.c 12 | index 8ada038dbd4e7..5b4312e5647cd 100644 13 | --- a/third_party/zlib/contrib/minizip/miniunz.c 14 | +++ b/third_party/zlib/contrib/minizip/miniunz.c 15 | @@ -12,7 +12,7 @@ 16 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 17 | */ 18 | 19 | -#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) 20 | +#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) && (!defined(__ANDROID_API__)) 21 | #ifndef __USE_FILE_OFFSET64 22 | #define __USE_FILE_OFFSET64 23 | #endif 24 | @@ -27,7 +27,7 @@ 25 | #endif 26 | #endif 27 | 28 | -#if defined(__APPLE__) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) 29 | +#if defined(__APPLE__) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) || defined(__Fuchsia__) || defined(__ANDROID_API__) 30 | // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions 31 | #define FOPEN_FUNC(filename, mode) fopen(filename, mode) 32 | #define FTELLO_FUNC(stream) ftello(stream) 33 | @@ -94,7 +94,7 @@ static void change_file_date(const char *filename, uLong dosdate, tm_unz tmu_dat 34 | SetFileTime(hFile,&ftm,&ftLastAcc,&ftm); 35 | CloseHandle(hFile); 36 | #else 37 | -#if defined(unix) || defined(__APPLE__) 38 | +#if defined(unix) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__ANDROID_API__) 39 | (void)dosdate; 40 | struct utimbuf ut; 41 | struct tm newdate; 42 | @@ -125,11 +125,9 @@ static void change_file_date(const char *filename, uLong dosdate, tm_unz tmu_dat 43 | 44 | static int mymkdir(const char* dirname) { 45 | int ret=0; 46 | -#ifdef _WIN32 47 | +#if defined(_WIN32) 48 | ret = _mkdir(dirname); 49 | -#elif unix 50 | - ret = mkdir (dirname,0775); 51 | -#elif __APPLE__ 52 | +#elif defined(unix) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__ANDROID_API__) 53 | ret = mkdir (dirname,0775); 54 | #else 55 | (void)dirname; 56 | diff --git a/third_party/zlib/contrib/minizip/minizip.c b/third_party/zlib/contrib/minizip/minizip.c 57 | index 26ee8d029efe6..9eb3956a55e00 100644 58 | --- a/third_party/zlib/contrib/minizip/minizip.c 59 | +++ b/third_party/zlib/contrib/minizip/minizip.c 60 | @@ -12,8 +12,7 @@ 61 | Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) 62 | */ 63 | 64 | - 65 | -#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) 66 | +#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__)) && (!defined(__ANDROID_API__)) 67 | #ifndef __USE_FILE_OFFSET64 68 | #define __USE_FILE_OFFSET64 69 | #endif 70 | @@ -28,7 +27,7 @@ 71 | #endif 72 | #endif 73 | 74 | -#if defined(__APPLE__) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) 75 | +#if defined(__APPLE__) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64) || defined(__Fuchsia__) || defined(__ANDROID_API__) 76 | // In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions 77 | #define FOPEN_FUNC(filename, mode) fopen(filename, mode) 78 | #define FTELLO_FUNC(stream) ftello(stream) 79 | @@ -92,7 +91,7 @@ static int filetime(const char *f, tm_zip *tmzip, uLong *dt) { 80 | return ret; 81 | } 82 | #else 83 | -#if defined(unix) || defined(__APPLE__) 84 | +#if defined(unix) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__ANDROID_API__) 85 | /* f: name of file to get info on, tmzip: return value: access, 86 | modification and creation times, dt: dostime */ 87 | static int filetime(const char *f, tm_zip *tmzip, uLong *dt) { 88 | -- 89 | 2.31.1.818.g46aad6cb9e-goog 90 | -------------------------------------------------------------------------------- /patches/0009-infcover-oob.patch: -------------------------------------------------------------------------------- 1 | From 75690b2683667be5535ac6243438115dc9c40f6a Mon Sep 17 00:00:00 2001 2 | From: Florian Mayer 3 | Date: Wed, 16 Mar 2022 16:38:36 -0700 4 | Subject: [PATCH] Fix out of bounds in infcover.c. 5 | 6 | --- 7 | test/infcover.c | 4 +++- 8 | 1 file changed, 3 insertions(+), 1 deletion(-) 9 | 10 | diff --git a/test/infcover.c b/test/infcover.c 11 | index 2be01646c..a6d83693c 100644 12 | --- a/test/infcover.c 13 | +++ b/test/infcover.c 14 | @@ -373,7 +373,9 @@ local void cover_support(void) 15 | mem_setup(&strm); 16 | strm.avail_in = 0; 17 | strm.next_in = Z_NULL; 18 | - ret = inflateInit_(&strm, ZLIB_VERSION - 1, (int)sizeof(z_stream)); 19 | + char versioncpy[] = ZLIB_VERSION; 20 | + versioncpy[0] -= 1; 21 | + ret = inflateInit_(&strm, versioncpy, (int)sizeof(z_stream)); 22 | assert(ret == Z_VERSION_ERROR); 23 | mem_done(&strm, "wrong version"); 24 | 25 | -------------------------------------------------------------------------------- /patches/0010-cmake-enable-simd.patch: -------------------------------------------------------------------------------- 1 | diff --git a/third_party/zlib/CMakeLists.txt b/third_party/zlib/CMakeLists.txt 2 | index b412dc7feb732..0431278405046 100644 3 | --- a/third_party/zlib/CMakeLists.txt 4 | +++ b/third_party/zlib/CMakeLists.txt 5 | @@ -1,4 +1,4 @@ 6 | -cmake_minimum_required(VERSION 2.4.4) 7 | +cmake_minimum_required(VERSION 3.0) 8 | set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON) 9 | 10 | project(zlib C) 11 | @@ -21,6 +21,26 @@ check_include_file(sys/types.h HAVE_SYS_TYPES_H) 12 | check_include_file(stdint.h HAVE_STDINT_H) 13 | check_include_file(stddef.h HAVE_STDDEF_H) 14 | 15 | +option(ENABLE_SIMD_OPTIMIZATIONS "Enable all SIMD optimizations" OFF) 16 | + 17 | +# TODO(cavalcantii): add support for other OSes (e.g. Android, fuchsia, osx) 18 | +# and architectures (e.g. Arm). 19 | +if (ENABLE_SIMD_OPTIMIZATIONS) 20 | + add_definitions(-DINFLATE_CHUNK_SIMD_SSE2) 21 | + add_definitions(-DADLER32_SIMD_SSSE3) 22 | + add_definitions(-DINFLATE_CHUNK_READ_64LE) 23 | + add_definitions(-DCRC32_SIMD_SSE42_PCLMUL) 24 | + add_definitions(-DDEFLATE_SLIDE_HASH_SSE2) 25 | + add_compile_options(-msse4.2 -mpclmul) 26 | + # Required by CPU features detection code. 27 | + add_definitions(-DX86_NOT_WINDOWS) 28 | + # Apparently some environments (e.g. CentOS) require to explicitly link 29 | + # with pthread and that is required by the CPU features detection code. 30 | + find_package (Threads REQUIRED) 31 | + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread") 32 | + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") 33 | +endif() 34 | + 35 | # 36 | # Check to see if we have large file support 37 | # 38 | @@ -120,10 +140,25 @@ set(ZLIB_SRCS 39 | zutil.c 40 | ) 41 | 42 | -if(NOT MINGW) 43 | - set(ZLIB_DLL_SRCS 44 | - win32/zlib1.rc # If present will override custom build rule below. 45 | - ) 46 | + 47 | +#============================================================================ 48 | +# Update list of source files if optimizations were enabled 49 | +#============================================================================ 50 | +if (ENABLE_SIMD_OPTIMIZATIONS) 51 | + list(REMOVE_ITEM ZLIB_SRCS inflate.c) 52 | + 53 | + list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/adler32_simd.h) 54 | + list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/chunkcopy.h) 55 | + list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/inffast_chunk.h) 56 | + list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/cpu_features.h) 57 | + list(APPEND ZLIB_PRIVATE_HDRS ${CMAKE_CURRENT_SOURCE_DIR}/crc32_simd.h) 58 | + 59 | + list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/adler32_simd.c) 60 | + list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/inffast_chunk.c) 61 | + list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/contrib/optimizations/inflate.c) 62 | + list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/cpu_features.c) 63 | + list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/crc32_simd.c) 64 | + list(APPEND ZLIB_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/crc_folding.c) 65 | endif() 66 | 67 | # parse the full version number from zlib.h and include in ZLIB_FULL_VERSION 68 | @@ -191,23 +226,9 @@ if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL ) 69 | endif() 70 | 71 | #============================================================================ 72 | -# Example binaries 73 | +# Benchmarker 74 | #============================================================================ 75 | - 76 | -add_executable(example test/example.c) 77 | -target_link_libraries(example zlib) 78 | -add_test(example example) 79 | - 80 | -add_executable(minigzip test/minigzip.c) 81 | -target_link_libraries(minigzip zlib) 82 | - 83 | -if(HAVE_OFF64_T) 84 | - add_executable(example64 test/example.c) 85 | - target_link_libraries(example64 zlib) 86 | - set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64") 87 | - add_test(example64 example64) 88 | - 89 | - add_executable(minigzip64 test/minigzip.c) 90 | - target_link_libraries(minigzip64 zlib) 91 | - set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64") 92 | -endif() 93 | +enable_language(CXX) 94 | +set(CMAKE_CXX_STANDARD 14) # workaround for older compilers (e.g. g++ 5.4). 95 | +add_executable(zlib_bench contrib/bench/zlib_bench.cc) 96 | +target_link_libraries(zlib_bench zlib) 97 | -------------------------------------------------------------------------------- /patches/0012-lfs-open64.patch: -------------------------------------------------------------------------------- 1 | From 6f21cb4b209d750486ede5472fdf7e35cf5ac3aa Mon Sep 17 00:00:00 2001 2 | From: Ramin Halavati 3 | Date: Wed, 17 May 2023 15:21:43 +0200 4 | Subject: [PATCH] Add open64 for Large File System support to gzlib. 5 | 6 | --- 7 | third_party/zlib/gzlib.c | 5 ++++- 8 | 1 file changed, 4 insertions(+), 1 deletion(-) 9 | 10 | diff --git a/third_party/zlib/gzlib.c b/third_party/zlib/gzlib.c 11 | index 55da46a453fd1..bbdb797e8079d 100644 12 | --- a/third_party/zlib/gzlib.c 13 | +++ b/third_party/zlib/gzlib.c 14 | @@ -7,11 +7,14 @@ 15 | 16 | #if defined(_WIN32) && !defined(__BORLANDC__) 17 | # define LSEEK _lseeki64 18 | +# define OPEN open 19 | #else 20 | #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 21 | # define LSEEK lseek64 22 | +# define OPEN open64 23 | #else 24 | # define LSEEK lseek 25 | +# define OPEN open 26 | #endif 27 | #endif 28 | 29 | @@ -244,7 +247,7 @@ local gzFile gz_open(path, fd, mode) 30 | #ifdef WIDECHAR 31 | fd == -2 ? _wopen(path, oflag, 0666) : 32 | #endif 33 | - open((const char *)path, oflag, 0666)); 34 | + OPEN((const char *)path, oflag, 0666)); 35 | if (state->fd == -1) { 36 | free(state->path); 37 | free(state); 38 | -- 39 | 2.40.1.606.ga4b1b128d6-goog 40 | 41 | -------------------------------------------------------------------------------- /patches/0013-cpu-feature-detection-for-arm.patch: -------------------------------------------------------------------------------- 1 | From c43ba7a55f091c0dcbfd8d89f7a5121269ce1b81 Mon Sep 17 00:00:00 2001 2 | From: Ho Cheung 3 | Date: Thu, 27 Jul 2023 09:47:52 +0800 4 | Subject: [PATCH] [zlib] Perform CPU feature detection for ARM inside adler32() 5 | 6 | Perform CPU feature detection for ARM within adler32() to have the same 7 | behavior as x86. 8 | 9 | --- 10 | third_party/zlib/adler32.c | 4 ++-- 11 | 1 file changed, 2 insertions(+), 2 deletions(-) 12 | 13 | diff --git a/third_party/zlib/adler32.c b/third_party/zlib/adler32.c 14 | index 81c584f68e233..99a294496f7eb 100644 15 | --- a/third_party/zlib/adler32.c 16 | +++ b/third_party/zlib/adler32.c 17 | @@ -90,9 +90,9 @@ uLong ZEXPORT adler32_z(uLong adler, const Bytef *buf, z_size_t len) { 18 | return adler | (sum2 << 16); 19 | } 20 | 21 | -#if defined(ADLER32_SIMD_SSSE3) 22 | +#if defined(ADLER32_SIMD_SSSE3) || defined(ADLER32_SIMD_NEON) 23 | /* 24 | - * Use SSSE3 to compute the adler32. Since this routine can be 25 | + * Use SIMD to compute the adler32. Since this function can be 26 | * freely used, check CPU features here. zlib convention is to 27 | * call adler32(0, NULL, 0), before making calls to adler32(). 28 | * So this is a good early (and infrequent) place to cache CPU 29 | -- 30 | 2.41.0.windows.3 31 | -------------------------------------------------------------------------------- /patches/0014-minizip-unzip-with-incorrect-size.patch: -------------------------------------------------------------------------------- 1 | commit 764f0715d75c8d49339aa73d0ee2feb75d63473f 2 | Author: joaoe@opera.com 3 | Date: Wed May 7 20:53:02 2014 +0000 4 | 5 | Fixed uncompressing files with wrong uncompressed size set. 6 | 7 | A zip file carries some metadata for each archived file, including the total 8 | uncompressed size. If that size was incorrect, therefore the compressed file 9 | being different in size when unpacking, the minizip code would fail with a 10 | CRC error. Every other zip utility handles these files, so should the minizip 11 | code for safety sake. 12 | 13 | BUG=359516 14 | 15 | Review URL: https://codereview.chromium.org/222243003 16 | 17 | git-svn-id: svn://svn.chromium.org/chrome/trunk/src@268940 0039d316-1c4b-4281-b951-d872f2087c98 18 | 19 | diff --git a/third_party/zlib/contrib/minizip/unzip.c b/third_party/zlib/contrib/minizip/unzip.c 20 | index ed763f89f1f87..82275d6c1775d 100644 21 | --- a/third_party/zlib/contrib/minizip/unzip.c 22 | +++ b/third_party/zlib/contrib/minizip/unzip.c 23 | @@ -1572,11 +1572,6 @@ extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len) { 24 | 25 | pfile_in_zip_read_info->stream.avail_out = (uInt)len; 26 | 27 | - if ((len>pfile_in_zip_read_info->rest_read_uncompressed) && 28 | - (!(pfile_in_zip_read_info->raw))) 29 | - pfile_in_zip_read_info->stream.avail_out = 30 | - (uInt)pfile_in_zip_read_info->rest_read_uncompressed; 31 | - 32 | if ((len>pfile_in_zip_read_info->rest_read_compressed+ 33 | pfile_in_zip_read_info->stream.avail_in) && 34 | (pfile_in_zip_read_info->raw)) 35 | -------------------------------------------------------------------------------- /patches/0015-minizip-unzip-enable-decryption.patch: -------------------------------------------------------------------------------- 1 | commit f3ace98803035b8425d127fb3d874dafe0b9475a 2 | Author: Che-yu Wu 3 | Date: Mon Aug 6 14:09:22 2018 +0000 4 | 5 | Enable traditional PKWARE decryption in zlib/contrib/minizip. 6 | 7 | Remove the #define which enables NOUNCRYPT by default. 8 | Correct the value of rest_read_compressed when decompressing an encrypted zip. 9 | 10 | Bug: crbug.com/869541 11 | Change-Id: Ia86c1d234a8193f405147d35ad05c29fe86f812d 12 | Reviewed-on: https://chromium-review.googlesource.com/1161109 13 | Reviewed-by: Chris Blume 14 | Commit-Queue: Che-yu Wu 15 | Cr-Commit-Position: refs/heads/master@{#580862} 16 | 17 | diff --git a/third_party/zlib/contrib/minizip/unzip.c b/third_party/zlib/contrib/minizip/unzip.c 18 | index 82275d6c1775d..c8a01b23efd42 100644 19 | --- a/third_party/zlib/contrib/minizip/unzip.c 20 | +++ b/third_party/zlib/contrib/minizip/unzip.c 21 | @@ -68,10 +68,6 @@ 22 | #include 23 | #include 24 | 25 | -#ifndef NOUNCRYPT 26 | - #define NOUNCRYPT 27 | -#endif 28 | - 29 | #include "zlib.h" 30 | #include "unzip.h" 31 | 32 | @@ -1502,6 +1498,7 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int* method, 33 | zdecode(s->keys,s->pcrc_32_tab,source[i]); 34 | 35 | s->pfile_in_zip_read->pos_in_zipfile+=12; 36 | + s->pfile_in_zip_read->rest_read_compressed-=12; 37 | s->encrypted=1; 38 | } 39 | # endif 40 | -------------------------------------------------------------------------------- /patches/0016-minizip-parse-unicode-path-extra-field.patch: -------------------------------------------------------------------------------- 1 | commit c8834821f452a3d424edd0ed2a1e9ceeda38d0ea 2 | Author: Alex Danilo 3 | Date: Thu May 12 03:29:52 2022 +0000 4 | 5 | Extract: Parse Unicode Path Extra field in minizip 6 | 7 | Adds parsing of the Info-ZIP Extra field which overrides the 8 | file name in the File Header only if the CRC in the extra field 9 | is a CRC of the file name in the File Header. 10 | 11 | See https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT 12 | section 4.6.9 for reference. 13 | 14 | Also tidied up some whitespace indent. 15 | 16 | Bug: 953256, 953599 17 | Tests: Manually tested, auto test in follow on CL 18 | Change-Id: I1283dcb88a203c3bb56c1d9c504035a2e51aecbd 19 | Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3641742 20 | Reviewed-by: Noel Gordon 21 | Commit-Queue: Alex Danilo 22 | Cr-Commit-Position: refs/heads/main@{#1002476} 23 | 24 | diff --git a/third_party/zlib/contrib/minizip/unzip.c b/third_party/zlib/contrib/minizip/unzip.c 25 | index c8a01b23efd42..42677cff82c96 100644 26 | --- a/third_party/zlib/contrib/minizip/unzip.c 27 | +++ b/third_party/zlib/contrib/minizip/unzip.c 28 | @@ -193,6 +193,26 @@ typedef struct 29 | Reads a long in LSB order from the given gz_stream. Sets 30 | */ 31 | 32 | +local int unz64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, 33 | + voidpf filestream, 34 | + int *pi) { 35 | + unsigned char c; 36 | + int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1); 37 | + if (err==1) 38 | + { 39 | + *pi = (int)c; 40 | + return UNZ_OK; 41 | + } 42 | + else 43 | + { 44 | + *pi = 0; 45 | + if (ZERROR64(*pzlib_filefunc_def,filestream)) 46 | + return UNZ_ERRNO; 47 | + else 48 | + return UNZ_EOF; 49 | + } 50 | +} 51 | + 52 | local int unz64local_getShort(const zlib_filefunc64_32_def* pzlib_filefunc_def, 53 | voidpf filestream, 54 | uLong *pX) { 55 | @@ -948,6 +968,62 @@ local int unz64local_GetCurrentFileInfoInternal(unzFile file, 56 | } 57 | 58 | } 59 | + else if (headerId == 0x7075) /* Info-ZIP Unicode Path Extra Field */ 60 | + { 61 | + int version = 0; 62 | + 63 | + if (unz64local_getByte(&s->z_filefunc, s->filestream, &version) != UNZ_OK) 64 | + { 65 | + err = UNZ_ERRNO; 66 | + } 67 | + if (version != 1) 68 | + { 69 | + if (ZSEEK64(s->z_filefunc, s->filestream,dataSize - 1, ZLIB_FILEFUNC_SEEK_CUR) != 0) 70 | + { 71 | + err = UNZ_ERRNO; 72 | + } 73 | + } 74 | + else 75 | + { 76 | + uLong uCrc, uHeaderCrc, fileNameSize; 77 | + 78 | + if (unz64local_getLong(&s->z_filefunc, s->filestream, &uCrc) != UNZ_OK) 79 | + { 80 | + err = UNZ_ERRNO; 81 | + } 82 | + uHeaderCrc = crc32(0, (const unsigned char *)szFileName, file_info.size_filename); 83 | + fileNameSize = dataSize - (2 * sizeof (short) + 1); 84 | + /* Check CRC against file name in the header. */ 85 | + if (uHeaderCrc != uCrc) 86 | + { 87 | + if (ZSEEK64(s->z_filefunc, s->filestream, fileNameSize, ZLIB_FILEFUNC_SEEK_CUR) != 0) 88 | + { 89 | + err = UNZ_ERRNO; 90 | + } 91 | + } 92 | + else 93 | + { 94 | + uLong uSizeRead; 95 | + 96 | + if (fileNameSize < fileNameBufferSize) 97 | + { 98 | + *(szFileName + fileNameSize) = '\0'; 99 | + uSizeRead = fileNameSize; 100 | + } 101 | + else 102 | + { 103 | + uSizeRead = fileNameBufferSize; 104 | + } 105 | + if ((fileNameSize > 0) && (fileNameBufferSize > 0)) 106 | + { 107 | + if (ZREAD64(s->z_filefunc, s->filestream, szFileName, uSizeRead) != uSizeRead) 108 | + { 109 | + err = UNZ_ERRNO; 110 | + } 111 | + } 112 | + } 113 | + } 114 | + } 115 | else 116 | { 117 | if (ZSEEK64(s->z_filefunc, s->filestream,dataSize,ZLIB_FILEFUNC_SEEK_CUR)!=0) 118 | -------------------------------------------------------------------------------- /patches/README: -------------------------------------------------------------------------------- 1 | == Patches applied on top of zlib == 2 | 3 | - 0000-build.patch: changes from the upstream version, mostly related to the 4 | build. 5 | - 0001-simd.patch: integrate Intel SIMD optimizations from 6 | https://github.com/jtkukunas/zlib/ 7 | - 0002-uninitializedcheck.patch: prevent uninitialized use of state->check 8 | 9 | == Procedure to create a patch file == 10 | 11 | Assuming you are working in a new feature branch: 12 | - git format-patch master --stdout > foo.patch # where naming follows a growing 13 | # number plus patch description. 14 | - git add foo.patch 15 | - git commit -a -m "Local patch." 16 | - git rebase -i HEAD~2 # Squashing the second commit 17 | 18 | As patches created in this way will feature a ChangeLog, there is no longer 19 | the need to append this file with a description of what the patch does. This 20 | should help to solve frequent conflicts in pending new patches on 21 | Chromium's zlib. 22 | 23 | The plan for the near future is to better insulate the platform specific 24 | changes to ease update adoption with new releases of zlib. This insulation 25 | happens by making changes inside contrib/ rather than the root directory 26 | (where conflicts can happen). 27 | 28 | If a change modifies enough things inside the root directory that the 29 | intention is not immediately clear, generate a .patch file to go with your 30 | change. If the change's modifications in the root directory are small, like: 31 | 32 | #ifdef FEATURE_FLAG 33 | use_special_feature(); 34 | #elif 35 | use_default_behavior(); 36 | #endif 37 | 38 | then the intent is clear and a .patch file doesn't need to be generated (since 39 | it would not provide much value). 40 | 41 | Ideally local changes should have a merge request featured in either: 42 | - canonical zlib: https://github.com/madler/zlib/ 43 | - zlib-ng: https://github.com/Dead2/zlib-ng 44 | -------------------------------------------------------------------------------- /slide_hash_simd.h: -------------------------------------------------------------------------------- 1 | /* slide_hash_simd.h 2 | * 3 | * Copyright 2022 The Chromium Authors 4 | * Use of this source code is governed by a BSD-style license that can be 5 | * found in the Chromium source repository LICENSE file. 6 | */ 7 | 8 | #ifndef SLIDE_HASH_SIMD_H 9 | #define SLIDE_HASH_SIMD_H 10 | 11 | #include "deflate.h" 12 | 13 | #ifndef INLINE 14 | #if defined(_MSC_VER) && !defined(__clang__) 15 | #define INLINE __inline 16 | #else 17 | #define INLINE inline 18 | #endif 19 | #endif 20 | 21 | #if defined(CPU_NO_SIMD) 22 | 23 | #error SIMD has been disabled for your build target 24 | 25 | #elif defined(DEFLATE_SLIDE_HASH_SSE2) 26 | 27 | #include /* SSE2 */ 28 | 29 | #define Z_SLIDE_INIT_SIMD(wsize) _mm_set1_epi16((ush)(wsize)) 30 | 31 | #define Z_SLIDE_HASH_SIMD(table, size, vector_wsize) \ 32 | for (const Posf* const end = table + size; table != end;) { \ 33 | __m128i vO = _mm_loadu_si128((__m128i *)(table + 0)); \ 34 | vO = _mm_subs_epu16(vO, vector_wsize); \ 35 | _mm_storeu_si128((__m128i *)(table + 0), vO); \ 36 | table += 8; \ 37 | } 38 | 39 | typedef __m128i z_vec128i_u16x8_t; 40 | 41 | #elif defined(DEFLATE_SLIDE_HASH_NEON) 42 | 43 | #include /* NEON */ 44 | 45 | #define Z_SLIDE_INIT_SIMD(wsize) vdupq_n_u16((ush)(wsize)) 46 | 47 | #define Z_SLIDE_HASH_SIMD(table, size, vector_wsize) \ 48 | for (const Posf* const end = table + size; table != end;) { \ 49 | uint16x8_t vO = vld1q_u16(table + 0); \ 50 | uint16x8_t v8 = vld1q_u16(table + 8); \ 51 | vO = vqsubq_u16(vO, vector_wsize); \ 52 | v8 = vqsubq_u16(v8, vector_wsize); \ 53 | vst1q_u16(table + 0, vO); \ 54 | vst1q_u16(table + 8, v8); \ 55 | table += 8 + 8; \ 56 | } 57 | 58 | typedef uint16x8_t z_vec128i_u16x8_t; 59 | 60 | #else 61 | 62 | #error slide_hash_simd is not defined for your build target 63 | 64 | #endif 65 | 66 | /* =========================================================================== 67 | * Slide the hash table when sliding the window down (could be avoided with 32 68 | * bit values at the expense of memory usage). We slide even when level == 0 to 69 | * keep the hash table consistent if we switch back to level > 0 later. 70 | */ 71 | local INLINE void slide_hash_simd( 72 | Posf *head, Posf *prev, const uInt w_size, const uInt hash_size) { 73 | /* 74 | * The SIMD implementation of the hash table slider assumes: 75 | * 76 | * 1. hash chain offset is 2 bytes. Should be true as Pos is "ush" type. 77 | */ 78 | Assert(sizeof(Pos) == 2, "Pos type size error: should be 2 bytes"); 79 | Assert(sizeof(ush) == 2, "ush type size error: should be 2 bytes"); 80 | 81 | Assert(hash_size <= (1 << 16), "Hash table maximum size error"); 82 | Assert(hash_size >= (1 << 8), "Hash table minimum size error"); 83 | Assert(w_size == (ush)w_size, "Prev table size error"); 84 | 85 | /* 86 | * 2. The hash & prev table sizes are a multiple of 32 bytes (256 bits), 87 | * since the NEON table slider moves two 128-bit items per loop (loop is 88 | * unrolled on NEON for performance, see http://crbug.com/863257). 89 | */ 90 | Assert(!((hash_size * sizeof(head[0])) & (32 - 1)), 91 | "Hash table size error: should be a multiple of 32 bytes"); 92 | Assert(!((w_size * sizeof(prev[0])) & (32 - 1)), 93 | "Prev table size error: should be a multiple of 32 bytes"); 94 | 95 | /* 96 | * Duplicate (ush)w_size in each uint16_t component of a 128-bit vector. 97 | */ 98 | const z_vec128i_u16x8_t vec_wsize = Z_SLIDE_INIT_SIMD(w_size); 99 | 100 | /* 101 | * Slide {head,prev} hash chain values: subtracts (ush)w_size from every 102 | * value with a saturating SIMD subtract, to clamp the result to 0(NIL), 103 | * to implement slide_hash() `(m >= wsize ? m - wsize : NIL);` code. 104 | */ 105 | Z_SLIDE_HASH_SIMD(head, hash_size, vec_wsize); 106 | #ifndef FASTEST 107 | Z_SLIDE_HASH_SIMD(prev, w_size, vec_wsize); 108 | #endif 109 | 110 | } 111 | 112 | #undef z_vec128i_u16x8_t 113 | #undef Z_SLIDE_HASH_SIMD 114 | #undef Z_SLIDE_INIT_SIMD 115 | 116 | #endif /* SLIDE_HASH_SIMD_H */ 117 | -------------------------------------------------------------------------------- /uncompr.c: -------------------------------------------------------------------------------- 1 | /* uncompr.c -- decompress a memory buffer 2 | * Copyright (C) 1995-2003, 2010, 2014, 2016 Jean-loup Gailly, Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* @(#) $Id$ */ 7 | 8 | #define ZLIB_INTERNAL 9 | #include "zlib.h" 10 | 11 | /* =========================================================================== 12 | Decompresses the source buffer into the destination buffer. *sourceLen is 13 | the byte length of the source buffer. Upon entry, *destLen is the total size 14 | of the destination buffer, which must be large enough to hold the entire 15 | uncompressed data. (The size of the uncompressed data must have been saved 16 | previously by the compressor and transmitted to the decompressor by some 17 | mechanism outside the scope of this compression library.) Upon exit, 18 | *destLen is the size of the decompressed data and *sourceLen is the number 19 | of source bytes consumed. Upon return, source + *sourceLen points to the 20 | first unused input byte. 21 | 22 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough 23 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, or 24 | Z_DATA_ERROR if the input data was corrupted, including if the input data is 25 | an incomplete zlib stream. 26 | */ 27 | int ZEXPORT uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source, 28 | uLong *sourceLen) { 29 | z_stream stream; 30 | int err; 31 | const uInt max = (uInt)-1; 32 | uLong len, left; 33 | Byte buf[1]; /* for detection of incomplete stream when *destLen == 0 */ 34 | 35 | len = *sourceLen; 36 | if (*destLen) { 37 | left = *destLen; 38 | *destLen = 0; 39 | } 40 | else { 41 | left = 1; 42 | dest = buf; 43 | } 44 | 45 | stream.next_in = (z_const Bytef *)source; 46 | stream.avail_in = 0; 47 | stream.zalloc = (alloc_func)0; 48 | stream.zfree = (free_func)0; 49 | stream.opaque = (voidpf)0; 50 | 51 | err = inflateInit(&stream); 52 | if (err != Z_OK) return err; 53 | 54 | stream.next_out = dest; 55 | stream.avail_out = 0; 56 | 57 | do { 58 | if (stream.avail_out == 0) { 59 | stream.avail_out = left > (uLong)max ? max : (uInt)left; 60 | left -= stream.avail_out; 61 | } 62 | if (stream.avail_in == 0) { 63 | stream.avail_in = len > (uLong)max ? max : (uInt)len; 64 | len -= stream.avail_in; 65 | } 66 | err = inflate(&stream, Z_NO_FLUSH); 67 | } while (err == Z_OK); 68 | 69 | *sourceLen -= len + stream.avail_in; 70 | if (dest != buf) 71 | *destLen = stream.total_out; 72 | else if (stream.total_out && err == Z_BUF_ERROR) 73 | left = 1; 74 | 75 | inflateEnd(&stream); 76 | return err == Z_STREAM_END ? Z_OK : 77 | err == Z_NEED_DICT ? Z_DATA_ERROR : 78 | err == Z_BUF_ERROR && left + stream.avail_out ? Z_DATA_ERROR : 79 | err; 80 | } 81 | 82 | int ZEXPORT uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, 83 | uLong sourceLen) { 84 | return uncompress2(dest, destLen, source, &sourceLen); 85 | } 86 | -------------------------------------------------------------------------------- /zlib.3: -------------------------------------------------------------------------------- 1 | .TH ZLIB 3 "xx Aug 2023" 2 | .SH NAME 3 | zlib \- compression/decompression library 4 | .SH SYNOPSIS 5 | [see 6 | .I zlib.h 7 | for full description] 8 | .SH DESCRIPTION 9 | The 10 | .I zlib 11 | library is a general purpose data compression library. 12 | The code is thread safe, assuming that the standard library functions 13 | used are thread safe, such as memory allocation routines. 14 | It provides in-memory compression and decompression functions, 15 | including integrity checks of the uncompressed data. 16 | This version of the library supports only one compression method (deflation) 17 | but other algorithms may be added later 18 | with the same stream interface. 19 | .LP 20 | Compression can be done in a single step if the buffers are large enough 21 | or can be done by repeated calls of the compression function. 22 | In the latter case, 23 | the application must provide more input and/or consume the output 24 | (providing more output space) before each call. 25 | .LP 26 | The library also supports reading and writing files in 27 | .IR gzip (1) 28 | (.gz) format 29 | with an interface similar to that of stdio. 30 | .LP 31 | The library does not install any signal handler. 32 | The decoder checks the consistency of the compressed data, 33 | so the library should never crash even in the case of corrupted input. 34 | .LP 35 | All functions of the compression library are documented in the file 36 | .IR zlib.h . 37 | The distribution source includes examples of use of the library 38 | in the files 39 | .I test/example.c 40 | and 41 | .IR test/minigzip.c, 42 | as well as other examples in the 43 | .IR examples/ 44 | directory. 45 | .LP 46 | Changes to this version are documented in the file 47 | .I ChangeLog 48 | that accompanies the source. 49 | .LP 50 | .I zlib 51 | is built in to many languages and operating systems, including but not limited to 52 | Java, Python, .NET, PHP, Perl, Ruby, Swift, and Go. 53 | .LP 54 | An experimental package to read and write files in the .zip format, 55 | written on top of 56 | .I zlib 57 | by Gilles Vollant (info@winimage.com), 58 | is available at: 59 | .IP 60 | http://www.winimage.com/zLibDll/minizip.html 61 | and also in the 62 | .I contrib/minizip 63 | directory of the main 64 | .I zlib 65 | source distribution. 66 | .SH "SEE ALSO" 67 | The 68 | .I zlib 69 | web site can be found at: 70 | .IP 71 | http://zlib.net/ 72 | .LP 73 | The data format used by the 74 | .I zlib 75 | library is described by RFC 76 | (Request for Comments) 1950 to 1952 in the files: 77 | .IP 78 | http://tools.ietf.org/html/rfc1950 (for the zlib header and trailer format) 79 | .br 80 | http://tools.ietf.org/html/rfc1951 (for the deflate compressed data format) 81 | .br 82 | http://tools.ietf.org/html/rfc1952 (for the gzip header and trailer format) 83 | .LP 84 | Mark Nelson wrote an article about 85 | .I zlib 86 | for the Jan. 1997 issue of Dr. Dobb's Journal; 87 | a copy of the article is available at: 88 | .IP 89 | http://marknelson.us/1997/01/01/zlib-engine/ 90 | .SH "REPORTING PROBLEMS" 91 | Before reporting a problem, 92 | please check the 93 | .I zlib 94 | web site to verify that you have the latest version of 95 | .IR zlib ; 96 | otherwise, 97 | obtain the latest version and see if the problem still exists. 98 | Please read the 99 | .I zlib 100 | FAQ at: 101 | .IP 102 | http://zlib.net/zlib_faq.html 103 | .LP 104 | before asking for help. 105 | Send questions and/or comments to zlib@gzip.org, 106 | or (for the Windows DLL version) to Gilles Vollant (info@winimage.com). 107 | .SH AUTHORS AND LICENSE 108 | Version 1.2.3.0.1 109 | .LP 110 | Copyright (C) 1995-2022 Jean-loup Gailly and Mark Adler 111 | .LP 112 | This software is provided 'as-is', without any express or implied 113 | warranty. In no event will the authors be held liable for any damages 114 | arising from the use of this software. 115 | .LP 116 | Permission is granted to anyone to use this software for any purpose, 117 | including commercial applications, and to alter it and redistribute it 118 | freely, subject to the following restrictions: 119 | .LP 120 | .nr step 1 1 121 | .IP \n[step]. 3 122 | The origin of this software must not be misrepresented; you must not 123 | claim that you wrote the original software. If you use this software 124 | in a product, an acknowledgment in the product documentation would be 125 | appreciated but is not required. 126 | .IP \n+[step]. 127 | Altered source versions must be plainly marked as such, and must not be 128 | misrepresented as being the original software. 129 | .IP \n+[step]. 130 | This notice may not be removed or altered from any source distribution. 131 | .LP 132 | Jean-loup Gailly Mark Adler 133 | .br 134 | jloup@gzip.org madler@alumni.caltech.edu 135 | .LP 136 | The deflate format used by 137 | .I zlib 138 | was defined by Phil Katz. 139 | The deflate and 140 | .I zlib 141 | specifications were written by L. Peter Deutsch. 142 | Thanks to all the people who reported problems and suggested various 143 | improvements in 144 | .IR zlib ; 145 | who are too numerous to cite here. 146 | .LP 147 | UNIX manual page by R. P. C. Rodgers, 148 | U.S. National Library of Medicine (rodgers@nlm.nih.gov). 149 | .\" end of man page 150 | -------------------------------------------------------------------------------- /zlib.map: -------------------------------------------------------------------------------- 1 | ZLIB_1.2.0 { 2 | global: 3 | compressBound; 4 | deflateBound; 5 | inflateBack; 6 | inflateBackEnd; 7 | inflateBackInit_; 8 | inflateCopy; 9 | local: 10 | deflate_copyright; 11 | inflate_copyright; 12 | inflate_fast; 13 | inflate_table; 14 | zcalloc; 15 | zcfree; 16 | z_errmsg; 17 | gz_error; 18 | gz_intmax; 19 | _*; 20 | }; 21 | 22 | ZLIB_1.2.0.2 { 23 | gzclearerr; 24 | gzungetc; 25 | zlibCompileFlags; 26 | } ZLIB_1.2.0; 27 | 28 | ZLIB_1.2.0.8 { 29 | deflatePrime; 30 | } ZLIB_1.2.0.2; 31 | 32 | ZLIB_1.2.2 { 33 | adler32_combine; 34 | crc32_combine; 35 | deflateSetHeader; 36 | inflateGetHeader; 37 | } ZLIB_1.2.0.8; 38 | 39 | ZLIB_1.2.2.3 { 40 | deflateTune; 41 | gzdirect; 42 | } ZLIB_1.2.2; 43 | 44 | ZLIB_1.2.2.4 { 45 | inflatePrime; 46 | } ZLIB_1.2.2.3; 47 | 48 | ZLIB_1.2.3.3 { 49 | adler32_combine64; 50 | crc32_combine64; 51 | gzopen64; 52 | gzseek64; 53 | gztell64; 54 | inflateUndermine; 55 | } ZLIB_1.2.2.4; 56 | 57 | ZLIB_1.2.3.4 { 58 | inflateReset2; 59 | inflateMark; 60 | } ZLIB_1.2.3.3; 61 | 62 | ZLIB_1.2.3.5 { 63 | gzbuffer; 64 | gzoffset; 65 | gzoffset64; 66 | gzclose_r; 67 | gzclose_w; 68 | } ZLIB_1.2.3.4; 69 | 70 | ZLIB_1.2.5.1 { 71 | deflatePending; 72 | } ZLIB_1.2.3.5; 73 | 74 | ZLIB_1.2.5.2 { 75 | deflateResetKeep; 76 | gzgetc_; 77 | inflateResetKeep; 78 | } ZLIB_1.2.5.1; 79 | 80 | ZLIB_1.2.7.1 { 81 | inflateGetDictionary; 82 | gzvprintf; 83 | } ZLIB_1.2.5.2; 84 | 85 | ZLIB_1.2.9 { 86 | inflateCodesUsed; 87 | inflateValidate; 88 | uncompress2; 89 | gzfread; 90 | gzfwrite; 91 | deflateGetDictionary; 92 | adler32_z; 93 | crc32_z; 94 | } ZLIB_1.2.7.1; 95 | 96 | ZLIB_1.2.12 { 97 | crc32_combine_gen; 98 | crc32_combine_gen64; 99 | crc32_combine_op; 100 | } ZLIB_1.2.9; 101 | -------------------------------------------------------------------------------- /zlib.pc.cmakein: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=@CMAKE_INSTALL_PREFIX@ 3 | libdir=@INSTALL_LIB_DIR@ 4 | sharedlibdir=@INSTALL_LIB_DIR@ 5 | includedir=@INSTALL_INC_DIR@ 6 | 7 | Name: zlib 8 | Description: zlib compression library 9 | Version: @VERSION@ 10 | 11 | Requires: 12 | Libs: -L${libdir} -L${sharedlibdir} -lz 13 | Cflags: -I${includedir} 14 | -------------------------------------------------------------------------------- /zutil.h: -------------------------------------------------------------------------------- 1 | /* zutil.h -- internal interface and configuration of the compression library 2 | * Copyright (C) 1995-2022 Jean-loup Gailly, Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* @(#) $Id$ */ 12 | 13 | #ifndef ZUTIL_H 14 | #define ZUTIL_H 15 | 16 | #ifdef HAVE_HIDDEN 17 | # define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) 18 | #else 19 | # define ZLIB_INTERNAL 20 | #endif 21 | 22 | #include "zlib.h" 23 | 24 | #if defined(STDC) && !defined(Z_SOLO) 25 | # if !(defined(_WIN32_WCE) && defined(_MSC_VER)) 26 | # include 27 | # endif 28 | # include 29 | # include 30 | #endif 31 | #ifdef NO_ERRNO_H 32 | # ifdef _WIN32_WCE 33 | /* The Microsoft C Run-Time Library for Windows CE doesn't have 34 | * errno. We define it as a global variable to simplify porting. 35 | * Its value is always 0 and should not be used. We rename it to 36 | * avoid conflict with other libraries that use the same workaround. 37 | */ 38 | # define errno z_errno 39 | # endif 40 | extern int errno; 41 | #else 42 | # ifndef _WIN32_WCE 43 | # include 44 | # endif 45 | #endif 46 | 47 | #ifndef local 48 | # define local static 49 | #endif 50 | /* since "static" is used to mean two completely different things in C, we 51 | define "local" for the non-static meaning of "static", for readability 52 | (compile with -Dlocal if your debugger can't find static symbols) */ 53 | 54 | typedef unsigned char uch; 55 | typedef uch FAR uchf; 56 | typedef unsigned short ush; 57 | typedef ush FAR ushf; 58 | typedef unsigned long ulg; 59 | 60 | #if !defined(Z_U8) && !defined(Z_SOLO) && defined(STDC) 61 | # include 62 | # if (ULONG_MAX == 0xffffffffffffffff) 63 | # define Z_U8 unsigned long 64 | # elif (ULLONG_MAX == 0xffffffffffffffff) 65 | # define Z_U8 unsigned long long 66 | # elif (UINT_MAX == 0xffffffffffffffff) 67 | # define Z_U8 unsigned 68 | # endif 69 | #endif 70 | 71 | extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ 72 | /* (size given to avoid silly warnings with Visual C++) */ 73 | 74 | #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] 75 | 76 | #define ERR_RETURN(strm,err) \ 77 | return (strm->msg = ERR_MSG(err), (err)) 78 | /* To be used only when the state is known to be valid */ 79 | 80 | /* common constants */ 81 | 82 | #ifndef DEF_WBITS 83 | # define DEF_WBITS MAX_WBITS 84 | #endif 85 | /* default windowBits for decompression. MAX_WBITS is for compression only */ 86 | 87 | #if MAX_MEM_LEVEL >= 8 88 | # define DEF_MEM_LEVEL 8 89 | #else 90 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL 91 | #endif 92 | /* default memLevel */ 93 | 94 | #define STORED_BLOCK 0 95 | #define STATIC_TREES 1 96 | #define DYN_TREES 2 97 | /* The three kinds of block type */ 98 | 99 | #define MIN_MATCH 3 100 | #define MAX_MATCH 258 101 | /* The minimum and maximum match lengths */ 102 | 103 | #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ 104 | 105 | /* target dependencies */ 106 | 107 | #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) 108 | # define OS_CODE 0x00 109 | # ifndef Z_SOLO 110 | # if defined(__TURBOC__) || defined(__BORLANDC__) 111 | # if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) 112 | /* Allow compilation with ANSI keywords only enabled */ 113 | void _Cdecl farfree( void *block ); 114 | void *_Cdecl farmalloc( unsigned long nbytes ); 115 | # else 116 | # include 117 | # endif 118 | # else /* MSC or DJGPP */ 119 | # include 120 | # endif 121 | # endif 122 | #endif 123 | 124 | #ifdef AMIGA 125 | # define OS_CODE 1 126 | #endif 127 | 128 | #if defined(VAXC) || defined(VMS) 129 | # define OS_CODE 2 130 | # define F_OPEN(name, mode) \ 131 | fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") 132 | #endif 133 | 134 | #ifdef __370__ 135 | # if __TARGET_LIB__ < 0x20000000 136 | # define OS_CODE 4 137 | # elif __TARGET_LIB__ < 0x40000000 138 | # define OS_CODE 11 139 | # else 140 | # define OS_CODE 8 141 | # endif 142 | #endif 143 | 144 | #if defined(ATARI) || defined(atarist) 145 | # define OS_CODE 5 146 | #endif 147 | 148 | #ifdef OS2 149 | # define OS_CODE 6 150 | # if defined(M_I86) && !defined(Z_SOLO) 151 | # include 152 | # endif 153 | #endif 154 | 155 | #if defined(MACOS) 156 | # define OS_CODE 7 157 | #endif 158 | 159 | #ifdef __acorn 160 | # define OS_CODE 13 161 | #endif 162 | 163 | #if defined(WIN32) && !defined(__CYGWIN__) 164 | # define OS_CODE 10 165 | #endif 166 | 167 | #ifdef _BEOS_ 168 | # define OS_CODE 16 169 | #endif 170 | 171 | #ifdef __TOS_OS400__ 172 | # define OS_CODE 18 173 | #endif 174 | 175 | #ifdef __APPLE__ 176 | # define OS_CODE 19 177 | #endif 178 | 179 | #if defined(__BORLANDC__) && !defined(MSDOS) 180 | #pragma warn -8004 181 | #pragma warn -8008 182 | #pragma warn -8066 183 | #endif 184 | 185 | /* provide prototypes for these when building zlib without LFS */ 186 | #if !defined(_WIN32) && \ 187 | (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0) 188 | ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off_t); 189 | ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off_t); 190 | ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off_t); 191 | #endif 192 | 193 | /* common defaults */ 194 | 195 | #ifndef OS_CODE 196 | # define OS_CODE 3 /* assume Unix */ 197 | #endif 198 | 199 | #ifndef F_OPEN 200 | # define F_OPEN(name, mode) fopen((name), (mode)) 201 | #endif 202 | 203 | /* functions */ 204 | 205 | #if defined(pyr) || defined(Z_SOLO) 206 | # define NO_MEMCPY 207 | #endif 208 | #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) 209 | /* Use our own functions for small and medium model with MSC <= 5.0. 210 | * You may have to use the same strategy for Borland C (untested). 211 | * The __SC__ check is for Symantec. 212 | */ 213 | # define NO_MEMCPY 214 | #endif 215 | #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) 216 | # define HAVE_MEMCPY 217 | #endif 218 | #ifdef HAVE_MEMCPY 219 | # ifdef SMALL_MEDIUM /* MSDOS small or medium model */ 220 | # define zmemcpy _fmemcpy 221 | # define zmemcmp _fmemcmp 222 | # define zmemzero(dest, len) _fmemset(dest, 0, len) 223 | # else 224 | # define zmemcpy memcpy 225 | # define zmemcmp memcmp 226 | # define zmemzero(dest, len) memset(dest, 0, len) 227 | # endif 228 | #else 229 | void ZLIB_INTERNAL zmemcpy(Bytef* dest, const Bytef* source, uInt len); 230 | int ZLIB_INTERNAL zmemcmp(const Bytef* s1, const Bytef* s2, uInt len); 231 | void ZLIB_INTERNAL zmemzero(Bytef* dest, uInt len); 232 | #endif 233 | 234 | /* Diagnostic functions */ 235 | #ifdef ZLIB_DEBUG 236 | # include 237 | extern int ZLIB_INTERNAL z_verbose; 238 | extern void ZLIB_INTERNAL z_error(char *m); 239 | # define Assert(cond,msg) {if(!(cond)) z_error(msg);} 240 | # define Trace(x) {if (z_verbose>=0) fprintf x ;} 241 | # define Tracev(x) {if (z_verbose>0) fprintf x ;} 242 | # define Tracevv(x) {if (z_verbose>1) fprintf x ;} 243 | # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} 244 | # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} 245 | #else 246 | # define Assert(cond,msg) 247 | # define Trace(x) 248 | # define Tracev(x) 249 | # define Tracevv(x) 250 | # define Tracec(c,x) 251 | # define Tracecv(c,x) 252 | #endif 253 | 254 | #ifndef Z_SOLO 255 | voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, 256 | unsigned size); 257 | void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr); 258 | #endif 259 | 260 | #define ZALLOC(strm, items, size) \ 261 | (*((strm)->zalloc))((strm)->opaque, (items), (size)) 262 | #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) 263 | #define TRY_FREE(s, p) {if (p) ZFREE(s, p);} 264 | 265 | /* Reverse the bytes in a 32-bit value */ 266 | #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ 267 | (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) 268 | 269 | #ifdef _MSC_VER 270 | #define zalign(x) __declspec(align(x)) 271 | #else 272 | #define zalign(x) __attribute__((aligned((x)))) 273 | #endif 274 | 275 | #endif /* ZUTIL_H */ 276 | --------------------------------------------------------------------------------