From 1e8c7ce810af43375fea1fa4fab2ebd1ef29cd6a Mon Sep 17 00:00:00 2001 From: Andreas Stefl Date: Thu, 23 Jan 2020 17:13:41 +0100 Subject: [PATCH 01/63] write with dynamic size --- miniz_zip.c | 56 +++++++++++++++++++++++++++++++---------------------- miniz_zip.h | 4 ++-- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/miniz_zip.c b/miniz_zip.c index 5ae3193..c0b6cee 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -3425,13 +3425,13 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n return MZ_TRUE; } -mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pArchive_name, mz_file_read_func read_callback, void* callback_opaque, mz_uint64 size_to_add, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, +mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pArchive_name, mz_file_read_func read_callback, void* callback_opaque, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len) { mz_uint16 gen_flags = MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes; mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0; - mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = size_to_add, comp_size = 0; + mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0; size_t archive_name_size; mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE]; mz_uint8 *pExtra_data = NULL; @@ -3453,7 +3453,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA pState = pZip->m_pState; - if ((!pState->m_zip64) && (uncomp_size > MZ_UINT32_MAX)) + if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX)) { /* Source file is too large for non-zip64 */ /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ @@ -3510,7 +3510,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA } #endif - if (uncomp_size <= 3) + if (max_size <= 3) level = 0; if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes)) @@ -3526,7 +3526,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA MZ_ASSERT((cur_archive_file_ofs & (pZip->m_file_offset_alignment - 1)) == 0); } - if (uncomp_size && level) + if (max_size && level) { method = MZ_DEFLATED; } @@ -3534,11 +3534,11 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA MZ_CLEAR_OBJ(local_dir_header); if (pState->m_zip64) { - if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) + if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) { pExtra_data = extra_data; - extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, - (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); + extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, + (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); } if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, gen_flags, dos_time, dos_date)) @@ -3589,9 +3589,8 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA cur_archive_file_ofs += user_extra_data_len; } - if (uncomp_size) + if (max_size) { - mz_uint64 uncomp_remaining = uncomp_size; void *pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE); if (!pRead_buf) { @@ -3600,19 +3599,27 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA if (!level) { - while (uncomp_remaining) + while (1) { - mz_uint n = (mz_uint)MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, uncomp_remaining); - if ((read_callback(callback_opaque, file_ofs, pRead_buf, n) != n) || (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, n) != n)) + size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE); + if (n == 0) + break; + + if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) { pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); } - file_ofs += n; + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, n) != n) + { + pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + file_ofs += n; uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n); - uncomp_remaining -= n; cur_archive_file_ofs += n; } + uncomp_size = file_ofs; comp_size = uncomp_size; } else @@ -3639,24 +3646,26 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA for (;;) { - size_t in_buf_size = (mz_uint32)MZ_MIN(uncomp_remaining, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE); tdefl_status status; tdefl_flush flush = TDEFL_NO_FLUSH; - if (read_callback(callback_opaque, file_ofs, pRead_buf, in_buf_size)!= in_buf_size) + size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE); + if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) { mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED); break; } - file_ofs += in_buf_size; - uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, in_buf_size); - uncomp_remaining -= in_buf_size; + file_ofs += n; + uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n); if (pZip->m_pNeeds_keepalive != NULL && pZip->m_pNeeds_keepalive(pZip->m_pIO_opaque)) flush = TDEFL_FULL_FLUSH; - status = tdefl_compress_buffer(pComp, pRead_buf, in_buf_size, uncomp_remaining ? flush : TDEFL_FINISH); + if (n == 0) + flush = TDEFL_FINISH; + + status = tdefl_compress_buffer(pComp, pRead_buf, n, flush); if (status == TDEFL_STATUS_DONE) { result = MZ_TRUE; @@ -3677,6 +3686,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA return MZ_FALSE; } + uncomp_size = file_ofs; comp_size = state.m_comp_size; cur_archive_file_ofs = state.m_cur_archive_file_ofs; } @@ -3741,10 +3751,10 @@ static size_t mz_file_read_func_stdio(void *pOpaque, mz_uint64 file_ofs, void *p return MZ_FREAD(pBuf, 1, n, pSrc_file); } -mz_bool mz_zip_writer_add_cfile(mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file, mz_uint64 size_to_add, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, +mz_bool mz_zip_writer_add_cfile(mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len) { - return mz_zip_writer_add_read_buf_callback(pZip, pArchive_name, mz_file_read_func_stdio, pSrc_file, size_to_add, pFile_time, pComment, comment_size, level_and_flags, + return mz_zip_writer_add_read_buf_callback(pZip, pArchive_name, mz_file_read_func_stdio, pSrc_file, max_size, pFile_time, pComment, comment_size, level_and_flags, user_extra_data, user_extra_data_len, user_extra_data_central, user_extra_data_central_len); } diff --git a/miniz_zip.h b/miniz_zip.h index 9bd707f..8b51dd4 100644 --- a/miniz_zip.h +++ b/miniz_zip.h @@ -380,7 +380,7 @@ MINIZ_EXPORT mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const cha /* Adds the contents of a file to an archive. This function also records the disk file's modified time into the archive. */ /* File data is supplied via a read callback function. User mz_zip_writer_add_(c)file to add a file directly.*/ -MINIZ_EXPORT mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pArchive_name, mz_file_read_func read_callback, void* callback_opaque, mz_uint64 size_to_add, +MINIZ_EXPORT mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pArchive_name, mz_file_read_func read_callback, void* callback_opaque, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, const char *user_extra_data_local, mz_uint user_extra_data_local_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len); @@ -390,7 +390,7 @@ MINIZ_EXPORT mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, c MINIZ_EXPORT mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); /* Like mz_zip_writer_add_file(), except the file data is read from the specified FILE stream. */ -MINIZ_EXPORT mz_bool mz_zip_writer_add_cfile(mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file, mz_uint64 size_to_add, +MINIZ_EXPORT mz_bool mz_zip_writer_add_cfile(mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, const char *user_extra_data_local, mz_uint user_extra_data_local_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len); #endif From fa09b1f3d483afc9f5232f734db1f1e8626e068a Mon Sep 17 00:00:00 2001 From: Wouter Deconinck Date: Mon, 20 Apr 2020 11:19:09 -0500 Subject: [PATCH 02/63] =?UTF-8?q?Fix=20unused=20parameter=20=E2=80=98pArra?= =?UTF-8?q?y=E2=80=99=20in=20mz=5Fzip=5Farray=5Frange=5Fcheck?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When NDEBUG, the assert in mz_zip_array_range_check does nothing and an unused variable warning is generated at https://github.com/richgel999/miniz/blob/4159f8c8c3d91a76648107ce6adf54bb6f06e3a7/miniz_zip.c#L280 This commit removes the `|| define (NDEBUG)` so when debugging is turned off, no range check is performed. --- miniz_zip.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz_zip.c b/miniz_zip.c index 5ae3193..064d448 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -277,7 +277,7 @@ struct mz_zip_internal_state_tag #define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size -#if defined(DEBUG) || defined(_DEBUG) || defined(NDEBUG) +#if defined(DEBUG) || defined(_DEBUG) static MZ_FORCEINLINE mz_uint mz_zip_array_range_check(const mz_zip_array *pArray, mz_uint index) { MZ_ASSERT(index < pArray->m_size); From 436d915546f17debde388f41a3d6140622d1ed5b Mon Sep 17 00:00:00 2001 From: Randy Date: Tue, 7 Jul 2020 13:08:22 +0200 Subject: [PATCH 03/63] ossfuzz: add fuzzers --- tests/checksum_fuzzer.c | 25 +++++++++++ tests/flush_fuzzer.c | 87 +++++++++++++++++++++++++++++++++++++++ tests/fuzz_main.c | 52 +++++++++++++++++++++++ tests/ossfuzz.sh | 32 ++++++++++++++ tests/uncompress_fuzzer.c | 20 +++++++++ 5 files changed, 216 insertions(+) create mode 100644 tests/checksum_fuzzer.c create mode 100644 tests/flush_fuzzer.c create mode 100644 tests/fuzz_main.c create mode 100755 tests/ossfuzz.sh create mode 100644 tests/uncompress_fuzzer.c diff --git a/tests/checksum_fuzzer.c b/tests/checksum_fuzzer.c new file mode 100644 index 0000000..b3648da --- /dev/null +++ b/tests/checksum_fuzzer.c @@ -0,0 +1,25 @@ +/* Derived from zlib fuzzers at http://github.com/google/oss-fuzz/tree/master/projects/zlib, + * see ossfuzz.sh for full license text. +*/ + +#include +#include +#include + +#include "miniz.h" + +static const size_t kMaxSize = 1024 * 1024; + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataLen) +{ + /* Discard inputs larger than 1Mb. */ + if (dataLen < 1 || dataLen > kMaxSize) return 0; + + uint32_t crc = crc32(0L, NULL, 0); + uint32_t adler = adler32(0L, NULL, 0); + + crc = crc32(crc, data, (uint32_t) dataLen); + adler = adler32(adler, data, (uint32_t) dataLen); + + return 0; +} diff --git a/tests/flush_fuzzer.c b/tests/flush_fuzzer.c new file mode 100644 index 0000000..d0d1c61 --- /dev/null +++ b/tests/flush_fuzzer.c @@ -0,0 +1,87 @@ +/* Derived from zlib fuzzers at http://github.com/google/oss-fuzz/tree/master/projects/zlib, + * see ossfuzz.sh for full license text. +*/ + +#include +#include +#include +#include +#include +#include + +#include "miniz.h" + +#define CHECK_ERR(err, msg) { \ + if (err != Z_OK) { \ + fprintf(stderr, "%s error: %d\n", msg, err); \ + exit(1); \ + } \ +} + +static const uint8_t *data; +static size_t dataLen; +static alloc_func zalloc = NULL; +static free_func zfree = NULL; + + +void test_flush(unsigned char *compr, size_t *comprLen) +{ + z_stream c_stream; /* compression stream */ + int err; + unsigned int len = dataLen; + + c_stream.zalloc = zalloc; + c_stream.zfree = zfree; + c_stream.opaque = (void *)0; + + err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); + CHECK_ERR(err, "deflateInit"); + + c_stream.next_in = (Bytef *)data; + c_stream.next_out = compr; + c_stream.avail_in = 3; + c_stream.avail_out = (unsigned int)*comprLen; + err = deflate(&c_stream, Z_FULL_FLUSH); + CHECK_ERR(err, "deflate flush 1"); + + compr[3]++; /* force an error in first compressed block */ + c_stream.avail_in = len - 3; + + err = deflate(&c_stream, Z_FINISH); + + if (err != Z_STREAM_END) + { + CHECK_ERR(err, "deflate flush 2"); + } + + err = deflateEnd(&c_stream); + CHECK_ERR(err, "deflateEnd"); + + *comprLen = (size_t)c_stream.total_out; +} + +int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) +{ + size_t comprLen = 100 + 2 * compressBound(size); + size_t uncomprLen = size; + uint8_t *compr, *uncompr; + + /* Discard inputs larger than 1Mb. */ + static const size_t kMaxSize = 1024 * 1024; + + /* This test requires at least 3 bytes of input data. */ + if (size <= 3 || size > kMaxSize) + return 0; + + data = d; + dataLen = size; + compr = calloc(1, comprLen); + uncompr = calloc(1, uncomprLen); + + test_flush(compr, &comprLen); + + free(compr); + free(uncompr); + + return 0; +} diff --git a/tests/fuzz_main.c b/tests/fuzz_main.c new file mode 100644 index 0000000..2ece02d --- /dev/null +++ b/tests/fuzz_main.c @@ -0,0 +1,52 @@ +#include +#include +#include + +/* Fuzz target entry point for building without libFuzzer */ + +int main(int argc, char **argv) +{ + FILE *f; + char *buf = NULL; + long siz_buf; + + if(argc < 2) + { + fprintf(stderr, "no input file\n"); + goto err; + } + + f = fopen(argv[1], "rb"); + if(f == NULL) + { + fprintf(stderr, "error opening input file %s\n", argv[1]); + goto err; + } + + fseek(f, 0, SEEK_END); + + siz_buf = ftell(f); + rewind(f); + + if(siz_buf < 1) goto err; + + buf = (char*)malloc(siz_buf); + if(buf == NULL) + { + fprintf(stderr, "malloc() failed\n"); + goto err; + } + + if(fread(buf, siz_buf, 1, f) != 1) + { + fprintf(stderr, "fread() failed\n"); + goto err; + } + + (void)LLVMFuzzerTestOneInput((uint8_t*)buf, siz_buf); + +err: + free(buf); + + return 0; +} \ No newline at end of file diff --git a/tests/ossfuzz.sh b/tests/ossfuzz.sh new file mode 100755 index 0000000..ae8802a --- /dev/null +++ b/tests/ossfuzz.sh @@ -0,0 +1,32 @@ +#!/bin/bash -eu +# Copyright 2020 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +################################################################################ + +mkdir build +cd build +cmake .. -DAMALGAMATE_SOURCES=ON -DBUILD_SHARED_LIBS=OFF -DBUILD_FUZZERS=ON +make -j$(nproc) +cd .. + +zip $OUT/seed_corpus.zip *.* + +for f in $(find $SRC -name '*_fuzzer.c'); do + b=$(basename -s .c $f) + $CC $CFLAGS -Ibuild/amalgamation $f -c -o /tmp/$b.o + $CXX $CXXFLAGS -stdlib=libc++ -Ibuild/amalgamation /tmp/$b.o -o $OUT/$b $LIB_FUZZING_ENGINE ./build/libminiz.a + rm -f /tmp/$b.o + ln -sf $OUT/seed_corpus.zip $OUT/${b}_seed_corpus.zip +done diff --git a/tests/uncompress_fuzzer.c b/tests/uncompress_fuzzer.c new file mode 100644 index 0000000..77d2b19 --- /dev/null +++ b/tests/uncompress_fuzzer.c @@ -0,0 +1,20 @@ +/* Derived from zlib fuzzers at http://github.com/google/oss-fuzz/tree/master/projects/zlib, + * see ossfuzz.sh for full license text. +*/ + +#include +#include +#include + +#include "miniz.h" + +static unsigned char buffer[256 * 1024] = { 0 }; + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + unsigned long int buffer_length = sizeof(buffer); + + if (Z_OK != uncompress(buffer, &buffer_length, data, size))) return 0; + + return 0; +} From d2d7d090faf35f962f86f0cf530845dfc49cf8c3 Mon Sep 17 00:00:00 2001 From: Randy Date: Tue, 7 Jul 2020 13:12:09 +0200 Subject: [PATCH 04/63] ossfuzz: fix build for uncompress_fuzzer --- tests/uncompress_fuzzer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/uncompress_fuzzer.c b/tests/uncompress_fuzzer.c index 77d2b19..5c8bd54 100644 --- a/tests/uncompress_fuzzer.c +++ b/tests/uncompress_fuzzer.c @@ -10,7 +10,7 @@ static unsigned char buffer[256 * 1024] = { 0 }; -extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { unsigned long int buffer_length = sizeof(buffer); From 797c58be3ccfab1dcd7323641f426babd3a89459 Mon Sep 17 00:00:00 2001 From: Randy Date: Tue, 7 Jul 2020 13:14:58 +0200 Subject: [PATCH 05/63] ossfuzz: fix typo --- tests/uncompress_fuzzer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/uncompress_fuzzer.c b/tests/uncompress_fuzzer.c index 5c8bd54..bf9c580 100644 --- a/tests/uncompress_fuzzer.c +++ b/tests/uncompress_fuzzer.c @@ -14,7 +14,7 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { unsigned long int buffer_length = sizeof(buffer); - if (Z_OK != uncompress(buffer, &buffer_length, data, size))) return 0; + if (Z_OK != uncompress(buffer, &buffer_length, data, size)) return 0; return 0; } From b36e55e48d61ddb50e04cbf44f6cd07dba4da1f1 Mon Sep 17 00:00:00 2001 From: Randy Date: Tue, 7 Jul 2020 13:45:05 +0200 Subject: [PATCH 06/63] ossfuzz: update build file --- tests/ossfuzz.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/ossfuzz.sh b/tests/ossfuzz.sh index ae8802a..6e43b7c 100755 --- a/tests/ossfuzz.sh +++ b/tests/ossfuzz.sh @@ -15,6 +15,9 @@ # ################################################################################ +# This script is meant to be run by +# https://github.com/google/oss-fuzz/blob/master/projects/miniz/Dockerfile + mkdir build cd build cmake .. -DAMALGAMATE_SOURCES=ON -DBUILD_SHARED_LIBS=OFF -DBUILD_FUZZERS=ON From f33da29f61a0441e0b5cba59e2389529cba39971 Mon Sep 17 00:00:00 2001 From: Randy Date: Tue, 7 Jul 2020 13:56:04 +0200 Subject: [PATCH 07/63] cmake: add fuzzers to build --- CMakeLists.txt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 193e95e..d2d3811 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." FORCE) endif () option(BUILD_EXAMPLES "Build examples" ON) +option(BUILD_FUZZERS "Build fuzz targets" OFF) option(AMALGAMATE_SOURCES "Amalgamate sources into miniz.h/c" OFF) option(BUILD_HEADER_ONLY "Build a header-only version" OFF) option(BUILD_SHARED_LIBS "Build shared library instead of static" ON) @@ -202,6 +203,23 @@ if(BUILD_EXAMPLES) # target_link_libraries(miniz_tester miniz) endif(BUILD_EXAMPLES) +if(BUILD_FUZZERS) + set(FUZZ_MAIN_SRC "${CMAKE_CURRENT_SOURCE_DIR}/tests/fuzz_main.c") + + set(CHECKSUM_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/checksum_fuzzer.c") + set(FLUSH_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/flush_fuzzer.c") + set(UNCOMPRESS_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/uncompress_fuzzer.c") + + add_executable(checksum_fuzzer ${CHECKSUM_FUZZER_SRC_LIST}) + target_link_libraries(checksum_fuzzer miniz) + + add_executable(flush_fuzzer ${FLUSH_FUZZER_SRC_LIST}) + target_link_libraries(flush_fuzzer miniz) + + add_executable(uncompress_fuzzer ${UNCOMPRESS_FUZZER_SRC_LIST}) + target_link_libraries(uncompress_fuzzer miniz) +endif() + set(INCLUDE_INSTALL_DIR "include") install(FILES ${INSTALL_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) From aa3c99a7b4d818f13975256b132e4ab6b317c33b Mon Sep 17 00:00:00 2001 From: Randy Date: Tue, 7 Jul 2020 15:34:08 +0200 Subject: [PATCH 08/63] fuzz_main: fix warning --- tests/fuzz_main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/fuzz_main.c b/tests/fuzz_main.c index 2ece02d..f524700 100644 --- a/tests/fuzz_main.c +++ b/tests/fuzz_main.c @@ -4,6 +4,8 @@ /* Fuzz target entry point for building without libFuzzer */ +int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size); + int main(int argc, char **argv) { FILE *f; From 320764e3cad4b71c5980f995d2987bb63cbc7d0a Mon Sep 17 00:00:00 2001 From: otreblan Date: Mon, 14 Sep 2020 11:46:49 -0500 Subject: [PATCH 09/63] Add pkg-config file --- CMakeLists.txt | 7 +++++++ miniz.pc.in | 13 +++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 miniz.pc.in diff --git a/CMakeLists.txt b/CMakeLists.txt index d2d3811..ffc604b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,6 +121,13 @@ endif(AMALGAMATE_SOURCES) if(NOT BUILD_HEADER_ONLY) target_compile_definitions(${PROJECT_NAME} PRIVATE $<$:_GNU_SOURCE>) + + # pkg-config file + configure_file(miniz.pc.in ${CMAKE_BINARY_DIR}/miniz.pc @ONLY) + + install(FILES + ${CMAKE_BINARY_DIR}/miniz.pc + DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig) endif() set_property(TARGET ${PROJECT_NAME} PROPERTY diff --git a/miniz.pc.in b/miniz.pc.in new file mode 100644 index 0000000..43be7f9 --- /dev/null +++ b/miniz.pc.in @@ -0,0 +1,13 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@ +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ + +Name: @PROJECT_NAME@ +Description: @PROJECT_DESCRIPTION@ +Version: @PROJECT_VERSION@ +URL: @PROJECT_HOMEPAGE_URL@ + +Requires: +Libs: -L${libdir} -lminiz +Cflags: -I${includedir} From 65ad3cdf66cd4aa80733d07d6acc0642fdf4c6e4 Mon Sep 17 00:00:00 2001 From: otreblan Date: Mon, 14 Sep 2020 11:56:38 -0500 Subject: [PATCH 10/63] Remove hardcoded install dirs --- CMakeLists.txt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ffc604b..69feef9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ else() project(miniz C) endif() +include(GNUInstallDirs) set(MINIZ_API_VERSION 2) set(MINIZ_MINOR_VERSION 1) @@ -137,11 +138,11 @@ set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY ) install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Targets - RUNTIME DESTINATION bin - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # users can use or - INCLUDES DESTINATION include include/${PROJECT_NAME} + INCLUDES DESTINATION include ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} ) include(CMakePackageConfigHelpers) @@ -160,7 +161,7 @@ configure_file(Config.cmake.in @ONLY ) -set(ConfigPackageLocation lib/cmake/${PROJECT_NAME}) +set(ConfigPackageLocation ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) install(EXPORT ${PROJECT_NAME}Targets FILE ${PROJECT_NAME}Targets.cmake @@ -229,6 +230,6 @@ endif() set(INCLUDE_INSTALL_DIR "include") -install(FILES ${INSTALL_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) +install(FILES ${INSTALL_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}) From e6faac98813160daa065fdd5d72d48e3d1cb6888 Mon Sep 17 00:00:00 2001 From: otreblan Date: Mon, 14 Sep 2020 11:59:07 -0500 Subject: [PATCH 11/63] Use MINIZ_VERSION for the pkg-config version --- miniz.pc.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz.pc.in b/miniz.pc.in index 43be7f9..95faf5f 100644 --- a/miniz.pc.in +++ b/miniz.pc.in @@ -5,7 +5,7 @@ includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ Name: @PROJECT_NAME@ Description: @PROJECT_DESCRIPTION@ -Version: @PROJECT_VERSION@ +Version: @MINIZ_VERSION@ URL: @PROJECT_HOMEPAGE_URL@ Requires: From 1e7621d96cb9d0821c61db6f4e3ef36ddc19b0cd Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Sat, 17 Oct 2020 16:10:59 -0700 Subject: [PATCH 12/63] Use variable size input buffer in uncompress fuzzer. --- tests/uncompress_fuzzer.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/uncompress_fuzzer.c b/tests/uncompress_fuzzer.c index bf9c580..933ff69 100644 --- a/tests/uncompress_fuzzer.c +++ b/tests/uncompress_fuzzer.c @@ -8,13 +8,23 @@ #include "miniz.h" -static unsigned char buffer[256 * 1024] = { 0 }; - int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - unsigned long int buffer_length = sizeof(buffer); + unsigned long int buffer_length = 1; + unsigned char *buffer = NULL; + int z_status = 0; - if (Z_OK != uncompress(buffer, &buffer_length, data, size)) return 0; - + if (size > 0) + buffer_length *= data[0]; + if (size > 1) + buffer_length *= data[1]; + + buffer = (unsigned char *)malloc(buffer_length); + + z_status = uncompress(buffer, &buffer_length, data, size); + free(buffer); + + if (Z_OK != z_status) + return 0; return 0; } From 8410f3c640ad0c0e667af174cccf8ec5de026e9d Mon Sep 17 00:00:00 2001 From: ariel shtul Date: Sun, 1 Nov 2020 12:02:06 +0200 Subject: [PATCH 13/63] remove redundant condition L#1067 asserts that (match_len >= TDEFL_MIN_MATCH_LEN) --- miniz_tdef.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/miniz_tdef.c b/miniz_tdef.c index 64113f8..e42fc6e 100644 --- a/miniz_tdef.c +++ b/miniz_tdef.c @@ -1074,9 +1074,7 @@ static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, mz_uint match s0 = s_tdefl_small_dist_sym[match_dist & 511]; s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127]; d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++; - - if (match_len >= TDEFL_MIN_MATCH_LEN) - d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++; + d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++; } static mz_bool tdefl_compress_normal(tdefl_compressor *d) From 9457abb670a2c0a9f907d353bdf257593d0498a5 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Mon, 2 Nov 2020 17:18:01 -0800 Subject: [PATCH 14/63] Fixed use-of-uninitialized value msan error when copying dist bytes with no output bytes written. --- miniz_tinfl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz_tinfl.c b/miniz_tinfl.c index 992de7a..749aab4 100644 --- a/miniz_tinfl.c +++ b/miniz_tinfl.c @@ -498,7 +498,7 @@ tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_nex } dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start; - if ((dist > dist_from_out_buf_start) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) + if ((dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) { TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED); } From 1721df91bb6be0abd8d40c4825d1550ffb7ed4b2 Mon Sep 17 00:00:00 2001 From: Martin Raiber Date: Sun, 8 Nov 2020 23:29:35 +0100 Subject: [PATCH 15/63] Add gh actions CI --- .github/workflows/c-cpp.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/workflows/c-cpp.yml diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml new file mode 100644 index 0000000..47e7386 --- /dev/null +++ b/.github/workflows/c-cpp.yml @@ -0,0 +1,17 @@ +name: C/C++ CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: amalgamate + run: bash amalgamate.sh From cd659959530d5a890d1ec75bc445819d88183ff1 Mon Sep 17 00:00:00 2001 From: Martin Raiber Date: Sun, 8 Nov 2020 23:30:45 +0100 Subject: [PATCH 16/63] Remove travis build status --- readme.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/readme.md b/readme.md index 74b7eb3..127d16f 100644 --- a/readme.md +++ b/readme.md @@ -32,6 +32,3 @@ Thanks to Bruce Dawson for reporting a problem with the level_and_flags archive ## Patents I was recently asked if miniz avoids patent issues. miniz purposely uses the same core algorithms as the ones used by zlib. The compressor uses vanilla hash chaining as described [here](http://www.gzip.org/zlib/rfc-deflate.html#algorithm). Also see the [gzip FAQ](http://www.gzip.org/#faq11). In my opinion, if miniz falls prey to a patent attack then zlib/gzip are likely to be at serious risk too. - - -[![Build Status](https://travis-ci.org/uroni/miniz.svg?branch=master)](https://travis-ci.org/uroni/miniz) \ No newline at end of file From 1cdb9afad0b0705f4643bf97e824400fb4ad30fe Mon Sep 17 00:00:00 2001 From: Randy Date: Tue, 24 Nov 2020 15:03:16 +0100 Subject: [PATCH 17/63] Integrate with CIFuzz --- .github/workflows/ci-fuzz.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/ci-fuzz.yml diff --git a/.github/workflows/ci-fuzz.yml b/.github/workflows/ci-fuzz.yml new file mode 100644 index 0000000..849de22 --- /dev/null +++ b/.github/workflows/ci-fuzz.yml @@ -0,0 +1,23 @@ +name: CIFuzz +on: [pull_request] +jobs: + Fuzzing: + runs-on: ubuntu-latest + steps: + - name: Build Fuzzers + uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master + with: + oss-fuzz-project-name: 'miniz' + dry-run: false + - name: Run Fuzzers + uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master + with: + oss-fuzz-project-name: 'miniz' + fuzz-seconds: 900 + dry-run: false + - name: Upload Crash + uses: actions/upload-artifact@v1 + if: failure() + with: + name: artifacts + path: ./out/artifacts \ No newline at end of file From b485d01faff54c337db972da9dee2d54d2db2a52 Mon Sep 17 00:00:00 2001 From: Randy Date: Tue, 24 Nov 2020 16:51:30 +0100 Subject: [PATCH 18/63] updates to OSS-Fuzz integration adapt more fuzz targets from zlib, add zip fuzzer, zip dictionary. update uncompress_fuzzer to seed the buffer length separately. --- CMakeLists.txt | 20 ++++++ tests/compress_fuzzer.c | 88 +++++++++++++++++++++++++ tests/flush_fuzzer.c | 2 +- tests/large_fuzzer.c | 130 +++++++++++++++++++++++++++++++++++++ tests/ossfuzz.sh | 7 ++ tests/small_fuzzer.c | 124 +++++++++++++++++++++++++++++++++++ tests/uncompress2_fuzzer.c | 20 ++++++ tests/uncompress_fuzzer.c | 15 +++-- tests/zip.dict | 9 +++ tests/zip_fuzzer.c | 58 +++++++++++++++++ 10 files changed, 466 insertions(+), 7 deletions(-) create mode 100644 tests/compress_fuzzer.c create mode 100644 tests/large_fuzzer.c create mode 100644 tests/small_fuzzer.c create mode 100644 tests/uncompress2_fuzzer.c create mode 100644 tests/zip.dict create mode 100644 tests/zip_fuzzer.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 69feef9..9b4873e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -217,6 +217,11 @@ if(BUILD_FUZZERS) set(CHECKSUM_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/checksum_fuzzer.c") set(FLUSH_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/flush_fuzzer.c") set(UNCOMPRESS_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/uncompress_fuzzer.c") + set(UNCOMPRESS2_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/uncompress2_fuzzer.c") + set(COMPRESS_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/compress_fuzzer.c") + set(SMALL_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/small_fuzzer.c") + set(LARGE_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/large_fuzzer.c") + set(ZIP_FUZZER_SRC_LIST "${FUZZ_MAIN_SRC}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/zip_fuzzer.c") add_executable(checksum_fuzzer ${CHECKSUM_FUZZER_SRC_LIST}) target_link_libraries(checksum_fuzzer miniz) @@ -226,6 +231,21 @@ if(BUILD_FUZZERS) add_executable(uncompress_fuzzer ${UNCOMPRESS_FUZZER_SRC_LIST}) target_link_libraries(uncompress_fuzzer miniz) + + add_executable(uncompress2_fuzzer ${UNCOMPRESS2_FUZZER_SRC_LIST}) + target_link_libraries(uncompress2_fuzzer miniz) + + add_executable(compress_fuzzer ${COMPRESS_FUZZER_SRC_LIST}) + target_link_libraries(compress_fuzzer miniz) + + add_executable(small_fuzzer ${SMALL_FUZZER_SRC_LIST}) + target_link_libraries(small_fuzzer miniz) + + add_executable(large_fuzzer ${LARGE_FUZZER_SRC_LIST}) + target_link_libraries(large_fuzzer miniz) + + add_executable(zip_fuzzer ${ZIP_FUZZER_SRC_LIST}) + target_link_libraries(zip_fuzzer miniz) endif() set(INCLUDE_INSTALL_DIR "include") diff --git a/tests/compress_fuzzer.c b/tests/compress_fuzzer.c new file mode 100644 index 0000000..980b94c --- /dev/null +++ b/tests/compress_fuzzer.c @@ -0,0 +1,88 @@ +/* Derived from zlib fuzzers at http://github.com/google/oss-fuzz/tree/master/projects/zlib, + * see ossfuzz.sh for full license text. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include "miniz.h" + +static const uint8_t *data; +static size_t dataLen; + +static void check_compress_level(uint8_t *compr, size_t comprLen, + uint8_t *uncompr, size_t uncomprLen, + int level) +{ + compress2(compr, &comprLen, data, dataLen, level); + uncompress(uncompr, &uncomprLen, compr, comprLen); + + /* Make sure compress + uncompress gives back the input data. */ + assert(dataLen == uncomprLen); + assert(0 == memcmp(data, uncompr, dataLen)); +} + +#define put_byte(s, i, c) {s[i] = (unsigned char)(c);} + +static void write_zlib_header(uint8_t *s) +{ + unsigned level_flags = 0; /* compression level (0..3) */ + unsigned w_bits = 8; /* window size log2(w_size) (8..16) */ + unsigned int header = (Z_DEFLATED + ((w_bits-8)<<4)) << 8; + header |= (level_flags << 6); + + header += 31 - (header % 31); + + /* s is guaranteed to be longer than 2 bytes. */ + put_byte(s, 0, (unsigned char)(header >> 8)); + put_byte(s, 1, (unsigned char)(header & 0xff)); +} + +static void check_decompress(uint8_t *compr, size_t comprLen) +{ + /* We need to write a valid zlib header of size two bytes. Copy the input data + in a larger buffer. Do not modify the input data to avoid libFuzzer error: + fuzz target overwrites its const input. */ + size_t copyLen = dataLen + 2; + uint8_t *copy = malloc(copyLen); + memcpy(copy + 2, data, dataLen); + write_zlib_header(copy); + + uncompress(compr, &comprLen, copy, copyLen); + free(copy); +} + +int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) +{ + /* compressBound does not provide enough space for low compression levels. */ + size_t comprLen = 100 + 2 * compressBound(size); + size_t uncomprLen = size; + uint8_t *compr, *uncompr; + + /* Discard inputs larger than 1Mb. */ + static size_t kMaxSize = 1024 * 1024; + + if (size < 1 || size > kMaxSize) + return 0; + + data = d; + dataLen = size; + compr = calloc(1, comprLen); + uncompr = calloc(1, uncomprLen); + + check_compress_level(compr, comprLen, uncompr, uncomprLen, 1); + check_compress_level(compr, comprLen, uncompr, uncomprLen, 3); + check_compress_level(compr, comprLen, uncompr, uncomprLen, 6); + check_compress_level(compr, comprLen, uncompr, uncomprLen, 7); + + check_decompress(compr, comprLen); + + free(compr); + free(uncompr); + + return 0; +} diff --git a/tests/flush_fuzzer.c b/tests/flush_fuzzer.c index d0d1c61..fc12fca 100644 --- a/tests/flush_fuzzer.c +++ b/tests/flush_fuzzer.c @@ -32,7 +32,7 @@ void test_flush(unsigned char *compr, size_t *comprLen) c_stream.zalloc = zalloc; c_stream.zfree = zfree; - c_stream.opaque = (void *)0; + c_stream.opaque = NULL; err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); CHECK_ERR(err, "deflateInit"); diff --git a/tests/large_fuzzer.c b/tests/large_fuzzer.c new file mode 100644 index 0000000..ad5213d --- /dev/null +++ b/tests/large_fuzzer.c @@ -0,0 +1,130 @@ +/* Derived from zlib fuzzers at http://github.com/google/oss-fuzz/tree/master/projects/zlib, + * see ossfuzz.sh for full license text. +*/ + +#include +#include +#include +#include +#include +#include + +#include "miniz.h" + +#define CHECK_ERR(err, msg) { \ + if (err != Z_OK) { \ + fprintf(stderr, "%s error: %d\n", msg, err); \ + exit(1); \ + } \ +} + +static const uint8_t *data; +static size_t dataLen; +static alloc_func zalloc = NULL; +static free_func zfree = NULL; +static unsigned int diff; + +/* Test deflate() with large buffers and dynamic change of compression level */ +void test_large_deflate(unsigned char *compr, size_t comprLen, + unsigned char *uncompr, size_t uncomprLen) +{ + z_stream c_stream; /* compression stream */ + int err; + + c_stream.zalloc = zalloc; + c_stream.zfree = zfree; + c_stream.opaque = NULL; + + err = deflateInit(&c_stream, Z_BEST_COMPRESSION); + CHECK_ERR(err, "deflateInit"); + + c_stream.next_out = compr; + c_stream.avail_out = (unsigned int)comprLen; + + /* At this point, uncompr is still mostly zeroes, so it should compress + * very well: + */ + c_stream.next_in = uncompr; + c_stream.avail_in = (unsigned int)uncomprLen; + err = deflate(&c_stream, Z_NO_FLUSH); + CHECK_ERR(err, "deflate large 1"); + + if (c_stream.avail_in != 0) + { + fprintf(stderr, "deflate not greedy\n"); + exit(1); + } + + /* Feed in already compressed data: */ + c_stream.next_in = compr; + diff = (unsigned int)(c_stream.next_out - compr); + c_stream.avail_in = diff; + + deflate(&c_stream, Z_NO_FLUSH); + err = deflate(&c_stream, Z_FINISH); + + if (err != Z_STREAM_END) + { + fprintf(stderr, "deflate large should report Z_STREAM_END\n"); + exit(1); + } + err = deflateEnd(&c_stream); + CHECK_ERR(err, "deflateEnd"); +} + +/* Test inflate() with large buffers */ +void test_large_inflate(unsigned char *compr, size_t comprLen, + unsigned char *uncompr, size_t uncomprLen) +{ + int err; + z_stream d_stream; /* decompression stream */ + + d_stream.zalloc = zalloc; + d_stream.zfree = zfree; + d_stream.opaque = NULL; + + d_stream.next_in = compr; + d_stream.avail_in = (unsigned int)comprLen; + + err = inflateInit(&d_stream); + CHECK_ERR(err, "inflateInit"); + + for (;;) + { + d_stream.next_out = uncompr; /* discard the output */ + d_stream.avail_out = (unsigned int)uncomprLen; + err = inflate(&d_stream, Z_NO_FLUSH); + if (err == Z_STREAM_END) break; + + CHECK_ERR(err, "large inflate"); + } + + err = inflateEnd(&d_stream); + CHECK_ERR(err, "inflateEnd"); +} + +int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) +{ + size_t comprLen = 100 + 3 * size; + size_t uncomprLen = comprLen; + uint8_t *compr, *uncompr; + + /* Discard inputs larger than 512Kb. */ + static size_t kMaxSize = 512 * 1024; + + if (size < 1 || size > kMaxSize) + return 0; + + data = d; + dataLen = size; + compr = calloc(1, comprLen); + uncompr = calloc(1, uncomprLen); + + test_large_deflate(compr, comprLen, uncompr, uncomprLen); + test_large_inflate(compr, comprLen, uncompr, uncomprLen); + + free(compr); + free(uncompr); + + return 0; +} diff --git a/tests/ossfuzz.sh b/tests/ossfuzz.sh index 6e43b7c..c437988 100755 --- a/tests/ossfuzz.sh +++ b/tests/ossfuzz.sh @@ -33,3 +33,10 @@ for f in $(find $SRC -name '*_fuzzer.c'); do rm -f /tmp/$b.o ln -sf $OUT/seed_corpus.zip $OUT/${b}_seed_corpus.zip done + + +# Add .zip input file for the zip fuzzer +rm -f $OUT/zip_fuzzer_seed_corpus.zip +zip $OUT/zip_fuzzer_seed_corpus.zip $OUT/seed_corpus.zip + +cp tests/zip.dict $OUT/zip_fuzzer.dict \ No newline at end of file diff --git a/tests/small_fuzzer.c b/tests/small_fuzzer.c new file mode 100644 index 0000000..500881a --- /dev/null +++ b/tests/small_fuzzer.c @@ -0,0 +1,124 @@ +/* Derived from zlib fuzzers at http://github.com/google/oss-fuzz/tree/master/projects/zlib, + * see ossfuzz.sh for full license text. +*/ + +#include +#include +#include +#include +#include +#include + +#include "miniz.h" + +#define CHECK_ERR(err, msg) { \ + if (err != Z_OK) { \ + fprintf(stderr, "%s error: %d\n", msg, err); \ + exit(1); \ + } \ +} + +static const uint8_t *data; +static size_t dataLen; +static alloc_func zalloc = NULL; +static free_func zfree = NULL; + +/* Test deflate() with small buffers */ +void test_deflate(unsigned char *compr, size_t comprLen) +{ + z_stream c_stream; /* compression stream */ + int err; + unsigned long len = dataLen; + + c_stream.zalloc = zalloc; + c_stream.zfree = zfree; + c_stream.opaque = NULL; + + err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION); + CHECK_ERR(err, "deflateInit"); + + c_stream.next_in = (Bytef *)data; + c_stream.next_out = compr; + + while (c_stream.total_in != len && c_stream.total_out < comprLen) + { + c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ + err = deflate(&c_stream, Z_NO_FLUSH); + CHECK_ERR(err, "deflate small 1"); + } + + /* Finish the stream, still forcing small buffers: */ + for (;;) + { + c_stream.avail_out = 1; + err = deflate(&c_stream, Z_FINISH); + if (err == Z_STREAM_END) + break; + CHECK_ERR(err, "deflate small 2"); + } + + err = deflateEnd(&c_stream); + CHECK_ERR(err, "deflateEnd"); +} + +/* Test inflate() with small buffers */ +void test_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) +{ + int err; + z_stream d_stream; /* decompression stream */ + + d_stream.zalloc = zalloc; + d_stream.zfree = zfree; + d_stream.opaque = NULL; + + d_stream.next_in = compr; + d_stream.avail_in = 0; + d_stream.next_out = uncompr; + + err = inflateInit(&d_stream); + CHECK_ERR(err, "inflateInit"); + + while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) + { + d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ + err = inflate(&d_stream, Z_NO_FLUSH); + if (err == Z_STREAM_END) + break; + CHECK_ERR(err, "inflate"); + } + + err = inflateEnd(&d_stream); + CHECK_ERR(err, "inflateEnd"); + + if (memcmp(uncompr, data, dataLen)) + { + fprintf(stderr, "bad inflate\n"); + exit(1); + } +} + +int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) +{ + size_t comprLen = compressBound(size); + size_t uncomprLen = size; + uint8_t *compr, *uncompr; + + /* Discard inputs larger than 1Mb. */ + static size_t kMaxSize = 1024 * 1024; + + if (size < 1 || size > kMaxSize) + return 0; + + data = d; + dataLen = size; + compr = calloc(1, comprLen); + uncompr = calloc(1, uncomprLen); + + test_deflate(compr, comprLen); + test_inflate(compr, comprLen, uncompr, uncomprLen); + + free(compr); + free(uncompr); + + return 0; +} diff --git a/tests/uncompress2_fuzzer.c b/tests/uncompress2_fuzzer.c new file mode 100644 index 0000000..98fe81f --- /dev/null +++ b/tests/uncompress2_fuzzer.c @@ -0,0 +1,20 @@ +/* Derived from zlib fuzzers at http://github.com/google/oss-fuzz/tree/master/projects/zlib, + * see ossfuzz.sh for full license text. +*/ + +#include +#include +#include + +#include "miniz.h" + +static unsigned char buffer[256 * 1024] = { 0 }; + +int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + unsigned long int buffer_length = sizeof(buffer); + + if (Z_OK != uncompress2(buffer, &buffer_length, data, &size)) return 0; + + return 0; +} diff --git a/tests/uncompress_fuzzer.c b/tests/uncompress_fuzzer.c index 933ff69..96015bc 100644 --- a/tests/uncompress_fuzzer.c +++ b/tests/uncompress_fuzzer.c @@ -10,18 +10,21 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - unsigned long int buffer_length = 1; + unsigned long int buffer_length; unsigned char *buffer = NULL; int z_status = 0; - if (size > 0) - buffer_length *= data[0]; - if (size > 1) - buffer_length *= data[1]; + if(size > 4) return 0; + + uint32_t n; + memcpy(&n, data, 4); + buffer_length = n; + + if(buffer_length > (1024 * 256)) return 0; buffer = (unsigned char *)malloc(buffer_length); - z_status = uncompress(buffer, &buffer_length, data, size); + z_status = uncompress(buffer, &buffer_length, data + 4, size - 4); free(buffer); if (Z_OK != z_status) diff --git a/tests/zip.dict b/tests/zip.dict new file mode 100644 index 0000000..43ff7d1 --- /dev/null +++ b/tests/zip.dict @@ -0,0 +1,9 @@ +# Fuzzing dictionary for .zip files + +header_lfh="\x50\x4b\x03\x04" +header_cd="\x50\x4b\x01\x02" +header_eocd="\x50\x4b\x05\x06" +header_eocd64="\x50\x4b\x06\x06" +data_descriptor="\x50\x4b\x07\x08" +extra_data_sig="\x50\x4b\x06\x08" +digital_sig="\x50\x4b\x05\x05" diff --git a/tests/zip_fuzzer.c b/tests/zip_fuzzer.c new file mode 100644 index 0000000..7b8af0f --- /dev/null +++ b/tests/zip_fuzzer.c @@ -0,0 +1,58 @@ +#include +#include + +#include "miniz.h" + +static char filename[260]; +static unsigned char read_buf[1024 * 256]; + +static const size_t filename_max = sizeof(filename); +static const size_t read_buf_size = sizeof(read_buf); +static const size_t data_max = 1024 * 256; + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + if(size > data_max) return 0; + + int ret = 0; + mz_zip_archive zip; + mz_zip_zero_struct(&zip); + + mz_uint flags = 0; + + if(!mz_zip_reader_init_mem(&zip, data, size, flags)) return 0; + + mz_uint i, files; + + files = mz_zip_reader_get_num_files(&zip); + + for(i=0; i < files; i++) + { + mz_zip_clear_last_error(&zip); + + if(mz_zip_reader_is_file_a_directory(&zip, i)) continue; + + mz_zip_validate_file(&zip, i, MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY); + mz_zip_validate_file(&zip, i, 0); + + if(mz_zip_reader_is_file_encrypted(&zip, i)) continue; + + mz_zip_clear_last_error(&zip); + + mz_uint ret = mz_zip_reader_get_filename(&zip, i, filename, filename_max); + + if(mz_zip_get_last_error(&zip)) continue; + + mz_zip_archive_file_stat file_stat = {0}; + mz_bool status = mz_zip_reader_file_stat(&zip, i, &file_stat) != 0; + + if ((file_stat.m_method) && (file_stat.m_method != MZ_DEFLATED)) continue; + + mz_zip_reader_extract_file_to_mem(&zip, file_stat.m_filename, read_buf, read_buf_size, 0); + } + +cleanup: + mz_zip_reader_end(&zip); + + return ret; +} \ No newline at end of file From 225354290d52d700b98f4849b6a3836c16534337 Mon Sep 17 00:00:00 2001 From: Randy Date: Tue, 24 Nov 2020 17:00:05 +0100 Subject: [PATCH 19/63] revert uncompress_fuzzer changes --- tests/uncompress_fuzzer.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/uncompress_fuzzer.c b/tests/uncompress_fuzzer.c index 96015bc..933ff69 100644 --- a/tests/uncompress_fuzzer.c +++ b/tests/uncompress_fuzzer.c @@ -10,21 +10,18 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - unsigned long int buffer_length; + unsigned long int buffer_length = 1; unsigned char *buffer = NULL; int z_status = 0; - if(size > 4) return 0; - - uint32_t n; - memcpy(&n, data, 4); - buffer_length = n; - - if(buffer_length > (1024 * 256)) return 0; + if (size > 0) + buffer_length *= data[0]; + if (size > 1) + buffer_length *= data[1]; buffer = (unsigned char *)malloc(buffer_length); - z_status = uncompress(buffer, &buffer_length, data + 4, size - 4); + z_status = uncompress(buffer, &buffer_length, data, size); free(buffer); if (Z_OK != z_status) From 90ff5d51e41c7b4fe781835a56bccd94e45bd7de Mon Sep 17 00:00:00 2001 From: Leonard Lausen Date: Tue, 24 Nov 2020 15:38:08 -0800 Subject: [PATCH 20/63] Correct zip64 ZIP archive support limitations entry According to ChangeLog.md Zip64 format is supported since 2.0 release --- miniz.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz.h b/miniz.h index 7789be9..754bc5a 100644 --- a/miniz.h +++ b/miniz.h @@ -95,7 +95,7 @@ possibility that the archive's central directory could be lost with this method if anything goes wrong, though. - ZIP archive support limitations: - No zip64 or spanning support. Extraction functions can only handle unencrypted, stored or deflated files. + No spanning support. Extraction functions can only handle unencrypted, stored or deflated files. Requires streams capable of seeking. * This is a header file library, like stb_image.c. To get only a header file, either cut and paste the From f52e09a208c2e1fc603214806ac037ecbd5924df Mon Sep 17 00:00:00 2001 From: Randy Date: Thu, 26 Nov 2020 02:21:57 +0100 Subject: [PATCH 21/63] mz_zip_validate_file(): fix memory leak on errors https://oss-fuzz.com/testcase?key=5744008051294208 --- miniz_zip.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/miniz_zip.c b/miniz_zip.c index 5ae3193..691ff42 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -2301,7 +2301,10 @@ mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index, mz_uint f return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); if (!mz_zip_array_resize(pZip, &file_data_array, MZ_MAX(local_header_filename_len, local_header_extra_len), MZ_FALSE)) - return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + { + mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED); + goto handle_failure; + } if (local_header_filename_len) { @@ -2335,14 +2338,20 @@ mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index, mz_uint f mz_uint32 field_id, field_data_size, field_total_size; if (extra_size_remaining < (sizeof(mz_uint16) * 2)) - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + goto handle_failure; + } field_id = MZ_READ_LE16(pExtra_data); field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16)); field_total_size = field_data_size + sizeof(mz_uint16) * 2; if (field_total_size > extra_size_remaining) - return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + { + mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED); + goto handle_failure; + } if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) { From 864ecc8d14466bcdf04287cc42b0d7631ad4d831 Mon Sep 17 00:00:00 2001 From: Randy Date: Fri, 27 Nov 2020 02:28:21 +0100 Subject: [PATCH 22/63] zip_fuzzer: omit file validation this is an unbounded operation --- tests/zip_fuzzer.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/zip_fuzzer.c b/tests/zip_fuzzer.c index 7b8af0f..5737d89 100644 --- a/tests/zip_fuzzer.c +++ b/tests/zip_fuzzer.c @@ -33,7 +33,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) if(mz_zip_reader_is_file_a_directory(&zip, i)) continue; mz_zip_validate_file(&zip, i, MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY); - mz_zip_validate_file(&zip, i, 0); if(mz_zip_reader_is_file_encrypted(&zip, i)) continue; From 6241bc718ccf250f80613ca399107a3a8c2c746a Mon Sep 17 00:00:00 2001 From: Tim Gates Date: Tue, 8 Dec 2020 07:06:18 +1100 Subject: [PATCH 23/63] docs: fix simple typo, purpsosely -> purposely There is a small typo in miniz.h. Should read `purposely` rather than `purpsosely`. --- miniz.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz.h b/miniz.h index 754bc5a..f897e17 100644 --- a/miniz.h +++ b/miniz.h @@ -220,7 +220,7 @@ enum #define MZ_DEFLATED 8 /* Heap allocation callbacks. -Note that mz_alloc_func parameter types purpsosely differ from zlib's: items/size is size_t, not unsigned long. */ +Note that mz_alloc_func parameter types purposely differ from zlib's: items/size is size_t, not unsigned long. */ typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size); typedef void (*mz_free_func)(void *opaque, void *address); typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size); From b99e5c3de3be94f2c05461c8d60735324693ac17 Mon Sep 17 00:00:00 2001 From: Randy Date: Sun, 20 Dec 2020 22:11:45 +0100 Subject: [PATCH 24/63] Update meson.build --- meson.build | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index fd3f1c3..1be4706 100644 --- a/meson.build +++ b/meson.build @@ -12,4 +12,10 @@ libminiz = static_library('miniz', include_directories: miniz_includes) miniz_dependency = declare_dependency(link_with: libminiz, - include_directories: miniz_includes) \ No newline at end of file + include_directories: miniz_includes) + +miniz_dep = miniz_dependency # Compatibility for WrapDB users + +if meson.version().version_compare('>= 0.54.0') + meson.override_dependency('miniz', miniz_dep) +endif From 60bbf6c8082c01d8c0b5eae0d2314a1d9085347a Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Sat, 30 Jan 2021 16:57:35 -0800 Subject: [PATCH 25/63] Fixed MSAN use-of-uninitialized in tinfl_decompress when invalid dist is decoded. In this instance dist was 31 which s_dist_base translates as 0. https://oss-fuzz.com/testcase-detail/4863557237473280 --- miniz_tinfl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz_tinfl.c b/miniz_tinfl.c index 749aab4..69c3f2d 100644 --- a/miniz_tinfl.c +++ b/miniz_tinfl.c @@ -498,7 +498,7 @@ tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_nex } dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start; - if ((dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) + if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) { TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED); } From 75744d32d45b22033d14392d0876f1708f16cc64 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 7 Feb 2021 22:40:58 +0100 Subject: [PATCH 26/63] Add flag to set (compressed) size in local file header --- miniz_zip.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- miniz_zip.h | 6 +++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/miniz_zip.c b/miniz_zip.c index c07b50d..dce07fe 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -3437,7 +3437,7 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pArchive_name, mz_file_read_func read_callback, void* callback_opaque, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len) { - mz_uint16 gen_flags = MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; + mz_uint16 gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes; mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0; mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0; @@ -3447,7 +3447,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA mz_uint32 extra_size = 0; mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE]; mz_zip_internal_state *pState; - mz_uint64 file_ofs = 0; + mz_uint64 file_ofs = 0, cur_archive_header_file_ofs; if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) gen_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8; @@ -3546,8 +3546,14 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) { pExtra_data = extra_data; - extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, - (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); + if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) + extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, + (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, + (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); + else + extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, NULL, + NULL, + (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); } if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, gen_flags, dos_time, dos_date)) @@ -3703,6 +3709,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf); } + if (!(level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)) { mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64]; mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32; @@ -3730,6 +3737,44 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA cur_archive_file_ofs += local_dir_footer_size; } + if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) + { + if (pExtra_data != NULL) + { + extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, + (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); + } + + if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, + (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), + (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : uncomp_size, + (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : comp_size, + uncomp_crc32, method, gen_flags, dos_time, dos_date)) + return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); + + cur_archive_header_file_ofs = local_dir_header_ofs; + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + if (pExtra_data != NULL) + { + cur_archive_header_file_ofs += sizeof(local_dir_header); + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, pArchive_name, archive_name_size) != archive_name_size) + { + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + } + + cur_archive_header_file_ofs += archive_name_size; + + if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, extra_data, extra_size) != extra_size) + return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED); + + cur_archive_header_file_ofs += extra_size; + } + } + if (pExtra_data != NULL) { extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, diff --git a/miniz_zip.h b/miniz_zip.h index 8b51dd4..82502bd 100644 --- a/miniz_zip.h +++ b/miniz_zip.h @@ -97,7 +97,10 @@ typedef enum { MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY = 0x2000, /* validate the local headers, but don't decompress the entire file and check the crc32 */ MZ_ZIP_FLAG_WRITE_ZIP64 = 0x4000, /* always use the zip64 file format, instead of the original zip file format with automatic switch to zip64. Use as flags parameter with mz_zip_writer_init*_v2 */ MZ_ZIP_FLAG_WRITE_ALLOW_READING = 0x8000, - MZ_ZIP_FLAG_ASCII_FILENAME = 0x10000 + MZ_ZIP_FLAG_ASCII_FILENAME = 0x10000, + /*After adding a compressed file, seek back + to local file header and set the correct sizes*/ + MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE = 0x20000 } mz_zip_flags; typedef enum { @@ -384,6 +387,7 @@ MINIZ_EXPORT mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, c const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, const char *user_extra_data_local, mz_uint user_extra_data_local_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len); + #ifndef MINIZ_NO_STDIO /* Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive. */ /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. */ From b7f04b700bfedc447f5f048a534513341d8d7cca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D1=81=D0=BB=D0=B0=D0=B2=20?= =?UTF-8?q?=D0=A9=D0=B0=D0=BF=D0=BE=D0=B2?= Date: Fri, 26 Feb 2021 20:08:39 +0500 Subject: [PATCH 27/63] Improved cmake subproject support - Added code to determine whether this is a standalone project or included by other projects. - Added option INSTALL_PROJECT to enable/disable install project. - Replaced CMAKE_BINARY_DIR to CMAKE_CURRENT_BINARY_DIR. --- CMakeLists.txt | 110 ++++++++++++++++++++++++++++--------------------- 1 file changed, 62 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b4873e..c9f5827 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,11 @@ else() project(miniz C) endif() -include(GNUInstallDirs) +# determine whether this is a standalone project or included by other projects +set(MINIZ_STANDALONE_PROJECT OFF) +if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(MINIZ_STANDALONE_PROJECT ON) +endif () set(MINIZ_API_VERSION 2) set(MINIZ_MINOR_VERSION 1) @@ -23,14 +27,19 @@ if(CMAKE_BUILD_TYPE STREQUAL "") CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." FORCE) endif () -option(BUILD_EXAMPLES "Build examples" ON) +option(BUILD_EXAMPLES "Build examples" ${MINIZ_STANDALONE_PROJECT}) option(BUILD_FUZZERS "Build fuzz targets" OFF) option(AMALGAMATE_SOURCES "Amalgamate sources into miniz.h/c" OFF) option(BUILD_HEADER_ONLY "Build a header-only version" OFF) option(BUILD_SHARED_LIBS "Build shared library instead of static" ON) +option(INSTALL_PROJECT "Install project" ${MINIZ_STANDALONE_PROJECT}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin) +if(INSTALL_PROJECT) + include(GNUInstallDirs) +endif() + if(BUILD_HEADER_ONLY) set(AMALGAMATE_SOURCES ON CACHE BOOL "Build a header-only version" FORCE) endif(BUILD_HEADER_ONLY) @@ -124,11 +133,13 @@ if(NOT BUILD_HEADER_ONLY) PRIVATE $<$:_GNU_SOURCE>) # pkg-config file - configure_file(miniz.pc.in ${CMAKE_BINARY_DIR}/miniz.pc @ONLY) + configure_file(miniz.pc.in ${CMAKE_CURRENT_BINARY_DIR}/miniz.pc @ONLY) - install(FILES - ${CMAKE_BINARY_DIR}/miniz.pc - DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig) + if(INSTALL_PROJECT) + install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/miniz.pc + DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig) + endif() endif() set_property(TARGET ${PROJECT_NAME} PROPERTY @@ -137,48 +148,50 @@ set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING ${PROJECT_NAME}_MAJOR_VERSION ) -install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Targets - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - # users can use or - INCLUDES DESTINATION include ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} -) +if(INSTALL_PROJECT) + install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Targets + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + # users can use or + INCLUDES DESTINATION include ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} + ) -include(CMakePackageConfigHelpers) -write_basic_package_version_file( - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}ConfigVersion.cmake" - VERSION ${MINIZ_VERSION} - COMPATIBILITY AnyNewerVersion -) - -export(EXPORT ${PROJECT_NAME}Targets - FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Targets.cmake" - NAMESPACE ${PROJECT_NAME}:: -) -configure_file(Config.cmake.in - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake" - @ONLY -) - -set(ConfigPackageLocation ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) -install(EXPORT ${PROJECT_NAME}Targets - FILE - ${PROJECT_NAME}Targets.cmake - NAMESPACE - ${PROJECT_NAME}:: - DESTINATION - ${ConfigPackageLocation} -) -install( - FILES - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake" + include(CMakePackageConfigHelpers) + write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}ConfigVersion.cmake" - DESTINATION - ${ConfigPackageLocation} - COMPONENT - Devel -) + VERSION ${MINIZ_VERSION} + COMPATIBILITY AnyNewerVersion + ) + + export(EXPORT ${PROJECT_NAME}Targets + FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Targets.cmake" + NAMESPACE ${PROJECT_NAME}:: + ) + configure_file(Config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake" + @ONLY + ) + + set(ConfigPackageLocation ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) + install(EXPORT ${PROJECT_NAME}Targets + FILE + ${PROJECT_NAME}Targets.cmake + NAMESPACE + ${PROJECT_NAME}:: + DESTINATION + ${ConfigPackageLocation} + ) + install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}Config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}/${PROJECT_NAME}ConfigVersion.cmake" + DESTINATION + ${ConfigPackageLocation} + COMPONENT + Devel + ) +endif() if(BUILD_EXAMPLES) set(EXAMPLE1_SRC_LIST "${CMAKE_CURRENT_SOURCE_DIR}/examples/example1.c") @@ -250,6 +263,7 @@ endif() set(INCLUDE_INSTALL_DIR "include") -install(FILES ${INSTALL_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}) - +if(INSTALL_PROJECT) + install(FILES ${INSTALL_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}) +endif() From 57176046b57ba2002d9c2fde5fb2065e209a5d95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20=C5=A0amla?= Date: Wed, 19 May 2021 10:37:15 +0200 Subject: [PATCH 28/63] Make cmake recognize FetchContent subproject --- CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c9f5827..1cb713a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,11 @@ cmake_minimum_required(VERSION 3.0) +# determine whether this is a standalone project or included by other projects +set (MINIZ_STANDALONE_PROJECT ON) +if(DEFINED PROJECT_NAME) + set(MINIZ_STANDALONE_PROJECT OFF) +endif() + if(CMAKE_MINOR_VERSION LESS 12) project(miniz) # see issue https://gitlab.kitware.com/cmake/cmake/merge_requests/1799 @@ -7,12 +13,6 @@ else() project(miniz C) endif() -# determine whether this is a standalone project or included by other projects -set(MINIZ_STANDALONE_PROJECT OFF) -if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) - set(MINIZ_STANDALONE_PROJECT ON) -endif () - set(MINIZ_API_VERSION 2) set(MINIZ_MINOR_VERSION 1) set(MINIZ_PATCH_VERSION 0) From cf2833fdc137f6388138067c62fdf29d6cbf18b6 Mon Sep 17 00:00:00 2001 From: Andre Maroneze Date: Tue, 25 May 2021 15:23:35 +0200 Subject: [PATCH 29/63] avoid use of uninitialized value in tdefl_record_literal In tdefl_record_literal, the following expression may read an uninitialized value in the m_pLZ_flags field: *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1); By explicitly initializing it, we avoid possible undefined behaviors. Issue found with Frama-C. --- miniz_tdef.c | 1 + 1 file changed, 1 insertion(+) diff --git a/miniz_tdef.c b/miniz_tdef.c index 64113f8..4eb8e07 100644 --- a/miniz_tdef.c +++ b/miniz_tdef.c @@ -1333,6 +1333,7 @@ tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_fun d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = d->m_block_index = d->m_bit_buffer = d->m_wants_to_finish = 0; d->m_pLZ_code_buf = d->m_lz_code_buf + 1; d->m_pLZ_flags = d->m_lz_code_buf; + *d->m_pLZ_flags = 0; d->m_num_flags_left = 8; d->m_pOutput_buf = d->m_output_buf; d->m_pOutput_buf_end = d->m_output_buf; From 58254a324168b6142cc87ccb3ca258f5284153f1 Mon Sep 17 00:00:00 2001 From: Andreas Deininger Date: Wed, 2 Jun 2021 22:48:50 +0200 Subject: [PATCH 30/63] readme.md: fix broken links ChangeLog.md: fix typo --- ChangeLog.md | 2 +- readme.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 3ee292d..13cbd18 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -82,7 +82,7 @@ The inflator now has a new failure status TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRE - The inflator coroutine func. is subtle and complex so I'm being cautious about this release. I would greatly appreciate any help with testing or any feedback. I feel good about these changes, and they've been through several hours of automated testing, but they will probably not fix anything for the majority of prev. users so I'm going to mark this release as beta for a few weeks and continue testing it at work/home on various things. -- The inflator in raw (non-zlib) mode is now usable on gzip or similiar data streams that have a bunch of bytes following the raw deflate data (problem discovered by rustyzip author williamw520). +- The inflator in raw (non-zlib) mode is now usable on gzip or similar data streams that have a bunch of bytes following the raw deflate data (problem discovered by rustyzip author williamw520). This version should *never* read beyond the last byte of the raw deflate data independent of how many bytes you pass into the input buffer. This issue was caused by the various Huffman bitbuffer lookahead optimizations, and would not be an issue if the caller knew and enforced the precise size of the raw compressed data *or* if the compressed data was in zlib format (i.e. always followed by the byte aligned zlib adler32). So in other words, you can now call the inflator on deflate streams that are followed by arbitrary amounts of data and it's guaranteed that decompression will stop exactly on the last byte. diff --git a/readme.md b/readme.md index 127d16f..3f8fd73 100644 --- a/readme.md +++ b/readme.md @@ -31,4 +31,4 @@ Thanks to Bruce Dawson for reporting a problem with the level_and_flags archive ## Patents -I was recently asked if miniz avoids patent issues. miniz purposely uses the same core algorithms as the ones used by zlib. The compressor uses vanilla hash chaining as described [here](http://www.gzip.org/zlib/rfc-deflate.html#algorithm). Also see the [gzip FAQ](http://www.gzip.org/#faq11). In my opinion, if miniz falls prey to a patent attack then zlib/gzip are likely to be at serious risk too. +I was recently asked if miniz avoids patent issues. miniz purposely uses the same core algorithms as the ones used by zlib. The compressor uses vanilla hash chaining as described [here](https://datatracker.ietf.org/doc/html/rfc1951#section-4). Also see the [gzip FAQ](https://web.archive.org/web/20160308045258/http://www.gzip.org/#faq11). In my opinion, if miniz falls prey to a patent attack then zlib/gzip are likely to be at serious risk too. From f2df122f6f5eca7bbc90e075207205e248d6b61c Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 27 Jun 2021 22:25:00 +0200 Subject: [PATCH 31/63] Create release zip via cmake --- CMakeLists.txt | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1cb713a..f12435b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ else() endif() set(MINIZ_API_VERSION 2) -set(MINIZ_MINOR_VERSION 1) +set(MINIZ_MINOR_VERSION 2) set(MINIZ_PATCH_VERSION 0) set(MINIZ_VERSION ${MINIZ_API_VERSION}.${MINIZ_MINOR_VERSION}.${MINIZ_PATCH_VERSION}) @@ -100,6 +100,31 @@ if(AMALGAMATE_SOURCES) endif(BUILD_HEADER_ONLY) set(INSTALL_HEADERS ${CMAKE_CURRENT_BINARY_DIR}/amalgamation/miniz.h) + + file(GLOB_RECURSE ZIP_FILES RELATIVE "${CMAKE_CURRENT_BINARY_DIR}/amalgamation" "${CMAKE_CURRENT_BINARY_DIR}/amalgamation/*") + file(GLOB_RECURSE ZIP_FILES2 RELATIVE "${CMAKE_SOURCE_DIR}" "${CMAKE_SOURCE_DIR}/examples/*") + list(APPEND ZIP_FILES ${ZIP_FILES2}) + list(APPEND ZIP_FILES "ChangeLog.md") + list(APPEND ZIP_FILES "readme.md") + list(APPEND ZIP_FILES "LICENSE") + set(ZIP_OUT_FN "${CMAKE_CURRENT_BINARY_DIR}/miniz-${MINIZ_VERSION}.zip") + message(STATUS "Zip files: ${ZIP_FILES}") + add_custom_command( + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/examples ${CMAKE_CURRENT_BINARY_DIR}/amalgamation/examples + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/ChangeLog.md ${CMAKE_CURRENT_BINARY_DIR}/amalgamation/ChangeLog.md + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/readme.md ${CMAKE_CURRENT_BINARY_DIR}/amalgamation/readme.md + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/LICENSE ${CMAKE_CURRENT_BINARY_DIR}/amalgamation/LICENSE + COMMAND ${CMAKE_COMMAND} -E tar "cf" "${ZIP_OUT_FN}" --format=zip -- ${ZIP_FILES} + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/amalgamation" + OUTPUT "${ZIP_OUT_FN}" + DEPENDS ${ZIP_FILES} + COMMENT "Zipping to ${CMAKE_CURRENT_BINARY_DIR}/miniz.zip." + ) + + add_custom_target( + create_zip ALL + DEPENDS "${ZIP_OUT_FN}" + ) else(AMALGAMATE_SOURCES) include(GenerateExportHeader) set(miniz_SOURCE miniz.c miniz_zip.c miniz_tinfl.c miniz_tdef.c) From 17b4f33dde0e74f8b70a897c571ec16d0c7d47af Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 27 Jun 2021 22:25:12 +0200 Subject: [PATCH 32/63] Update changelog --- ChangeLog.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 13cbd18..a1abf16 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,25 @@ ## Changelog +### 2.2.0 + + - Fix examples with amalgamation + - Modified cmake script to support shared library mode and find_package + - Fix for misleading doc comment on `mz_zip_reader_init_cfile` function + - Add include location tolerance and stop forcing `_GNU_SOURCE` + - Fix: mz_zip_reader_locate_file_v2 returns an mz_bool + - Fix large file system checks + - Add #elif to enable an external mz_crc32() to be linked in + - Write with dynamic size (size of file/data to be added not known before adding) + - Added uncompress2 for zlib compatibility + - Add support for building as a Meson subproject + - Added OSSFuzz support; Integrate with CIFuzz + - Add pkg-config file + - Fixed use-of-uninitialized value msan error when copying dist bytes with no output bytes written. + - mz_zip_validate_file(): fix memory leak on errors + - Fixed MSAN use-of-uninitialized in tinfl_decompress when invalid dist is decoded. In this instance dist was 31 which s_dist_base translates as 0 + - Add flag to set (compressed) size in local file header + - avoid use of uninitialized value in tdefl_record_literal + ### 2.1.0 - More instances of memcpy instead of cast and use memcpy per default From 2281a42c77a08188a57609c8950b447ec62d6fb8 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 27 Jun 2021 22:38:35 +0200 Subject: [PATCH 33/63] Add release workflow --- .github/workflows/release.yml | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..052d441 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,60 @@ +name: Create release + +# Controls when the action will run. +on: + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + build: + name: Create new release + runs-on: ubuntu-latest + + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - name: Checkout code + uses: actions/checkout@v2 + with: + path: source + + - name: Install dependencies + run: | + sudo apt-get install -y cmake + + - name: Configure + run: | + mkdir build + mkdir inst + cd build + cmake ../source -G"Unix Makefiles" -DAMALGAMATE_SOURCES=ON -DCMAKE_INSTALL_PREFIX=../inst + + - name: Build + run: | + cd build + make install + + - name: Get current version + id: relver + run: echo "::set-output name=relver::$(cat build/miniz.pc | grep Version | cut -d ':' -f2 | xargs)" + + - name: Create Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ steps.relver.outputs.relver }} + release_name: Release ${{ steps.relver.outputs.relver }} + draft: false + prerelease: false + + - name: Upload Release Asset + id: upload-release-asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./build/miniz-${{ steps.relver.outputs.relver }}.zip + asset_name: miniz-${{ steps.relver.outputs.relver }}.zip + asset_content_type: application/zip \ No newline at end of file From 8ac52a6caec42f4b3c1bb94d6a1f2ab0fe329535 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 27 Jun 2021 22:39:44 +0200 Subject: [PATCH 34/63] Increment version --- miniz.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/miniz.h b/miniz.h index f897e17..42e1ea2 100644 --- a/miniz.h +++ b/miniz.h @@ -1,4 +1,4 @@ -/* miniz.c 2.1.0 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing +/* miniz.c 2.2.0 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing See "unlicense" statement at the end of this file. Rich Geldreich , last updated Oct. 13, 2013 Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt @@ -236,10 +236,10 @@ enum MZ_DEFAULT_COMPRESSION = -1 }; -#define MZ_VERSION "10.1.0" +#define MZ_VERSION "10.2.0" #define MZ_VERNUM 0xA100 #define MZ_VER_MAJOR 10 -#define MZ_VER_MINOR 1 +#define MZ_VER_MINOR 2 #define MZ_VER_REVISION 0 #define MZ_VER_SUBREVISION 0 From d301afc4985ac3e863b5e57e743e3ba9088f394a Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 27 Jun 2021 22:41:31 +0200 Subject: [PATCH 35/63] Add release workflow --- .github/workflows/release.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 052d441..50dd38e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,8 +14,8 @@ jobs: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - name: Checkout code uses: actions/checkout@v2 - with: - path: source + with: + path: source - name: Install dependencies run: | @@ -24,14 +24,14 @@ jobs: - name: Configure run: | mkdir build - mkdir inst - cd build - cmake ../source -G"Unix Makefiles" -DAMALGAMATE_SOURCES=ON -DCMAKE_INSTALL_PREFIX=../inst + mkdir inst + cd build + cmake ../source -G"Unix Makefiles" -DAMALGAMATE_SOURCES=ON -DCMAKE_INSTALL_PREFIX=../inst - name: Build run: | cd build - make install + make install - name: Get current version id: relver From cb97387d20909c0a4728972fd0f19bfacd083c42 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 27 Jun 2021 22:44:34 +0200 Subject: [PATCH 36/63] Add release workflow --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 50dd38e..4815c5e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,7 +14,7 @@ jobs: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - name: Checkout code uses: actions/checkout@v2 - with: + with: path: source - name: Install dependencies From 08f2c2d7e36611da3f5775456ee9ac3cec77b42f Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 27 Jun 2021 23:22:31 +0200 Subject: [PATCH 37/63] Fix test --- test.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test.sh b/test.sh index 25d651a..6eab345 100644 --- a/test.sh +++ b/test.sh @@ -4,6 +4,11 @@ set -e . amalgamate.sh +cat << "EOF" > miniz_export.h +#ifndef MINIZ_EXPORT +#define MINIZ_EXPORT +#endif +EOF g++ tests/miniz_tester.cpp tests/timer.cpp amalgamation/miniz.c -o miniz_tester -I. -ggdb -O2 for i in 1 2 3 4 5 6 From c0aad72d46a9c91542ffd3e1368637e10b8bbe0e Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Tue, 20 Jul 2021 15:41:59 +0100 Subject: [PATCH 38/63] Use _ftelli64, _fseeki64 and stat with MinGW32 and OpenWatcom --- miniz_zip.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/miniz_zip.c b/miniz_zip.c index dce07fe..b372bb2 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -67,7 +67,7 @@ static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) #define MZ_FFLUSH fflush #define MZ_FREOPEN mz_freopen #define MZ_DELETE_FILE remove -#elif defined(__MINGW32__) +#elif defined(__MINGW32__) || defined(__WATCOMC__) #ifndef MINIZ_NO_TIME #include #endif @@ -75,10 +75,10 @@ static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) #define MZ_FCLOSE fclose #define MZ_FREAD fread #define MZ_FWRITE fwrite -#define MZ_FTELL64 ftello64 -#define MZ_FSEEK64 fseeko64 -#define MZ_FILE_STAT_STRUCT _stat -#define MZ_FILE_STAT _stat +#define MZ_FTELL64 _ftelli64 +#define MZ_FSEEK64 _fseeki64 +#define MZ_FILE_STAT_STRUCT stat +#define MZ_FILE_STAT stat #define MZ_FFLUSH fflush #define MZ_FREOPEN(f, m, s) freopen(f, m, s) #define MZ_DELETE_FILE remove From 46347635c853d82102f478032442063c371c1201 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Tue, 20 Jul 2021 15:42:37 +0100 Subject: [PATCH 39/63] Fix building example6 with C89 compilers --- examples/example6.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/example6.c b/examples/example6.c index abbb64f..c2ac541 100644 --- a/examples/example6.c +++ b/examples/example6.c @@ -25,6 +25,7 @@ typedef struct static void hsv_to_rgb(int hue, int min, int max, rgb_t *p) { + double h, c, X; const int invert = 0; const int saturation = 1; const int color_rotate = 0; @@ -35,9 +36,9 @@ static void hsv_to_rgb(int hue, int min, int max, rgb_t *p) p->r = p->g = p->b = 255 * (max - hue) / (max - min); return; } - double h = fmod(color_rotate + 1e-4 + 4.0 * (hue - min) / (max - min), 6); - double c = 255.0f * saturation; - double X = c * (1 - fabs(fmod(h, 2) - 1)); + h = fmod(color_rotate + 1e-4 + 4.0 * (hue - min) / (max - min), 6); + c = 255.0f * saturation; + X = c * (1 - fabs(fmod(h, 2) - 1)); p->r = p->g = p->b = 0; @@ -53,8 +54,6 @@ static void hsv_to_rgb(int hue, int min, int max, rgb_t *p) int main(int argc, char *argv[]) { - (void)argc, (void)argv; - // Image resolution const int iXmax = 4096; const int iYmax = 4096; @@ -89,6 +88,8 @@ int main(int argc, char *argv[]) int MinIter = 9999, MaxIter = 0; + (void)argc, (void)argv; + for(iY = 0; iY < iYmax; iY++) { Cy = CyMin + iY * PixelHeight; From f542e6df737167a9850a4f60104e9dadbdf5ebad Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Tue, 20 Jul 2021 17:48:56 +0100 Subject: [PATCH 40/63] Fix "Comparison result always 0" warnings with OpenWatcom --- examples/example3.c | 2 +- examples/example4.c | 2 +- examples/example5.c | 2 +- miniz.c | 4 ++-- miniz_zip.c | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/example3.c b/examples/example3.c index a97ba84..a2c6846 100644 --- a/examples/example3.c +++ b/examples/example3.c @@ -100,7 +100,7 @@ int main(int argc, char *argv[]) file_loc = ftell(pInfile); fseek(pInfile, 0, SEEK_SET); - if ((file_loc < 0) || (file_loc > INT_MAX)) + if ((file_loc < 0) || ((mz_uint64)file_loc > INT_MAX)) { // This is not a limitation of miniz or tinfl, but this example. printf("File is too large to be processed by this example.\n"); diff --git a/examples/example4.c b/examples/example4.c index 3f2d7cf..ac49e7f 100644 --- a/examples/example4.c +++ b/examples/example4.c @@ -47,7 +47,7 @@ int main(int argc, char *argv[]) file_loc = ftell(pInfile); fseek(pInfile, 0, SEEK_SET); - if ((file_loc < 0) || (file_loc > INT_MAX)) + if ((file_loc < 0) || ((mz_uint64)file_loc > INT_MAX)) { // This is not a limitation of miniz or tinfl, but this example. printf("File is too large to be processed by this example.\n"); diff --git a/examples/example5.c b/examples/example5.c index a190357..2e47199 100644 --- a/examples/example5.c +++ b/examples/example5.c @@ -132,7 +132,7 @@ int main(int argc, char *argv[]) file_loc = ftell(pInfile); fseek(pInfile, 0, SEEK_SET); - if ((file_loc < 0) || (file_loc > INT_MAX)) + if ((file_loc < 0) || ((mz_uint64)file_loc > INT_MAX)) { // This is not a limitation of miniz or tinfl, but this example. printf("File is too large to be processed by this example.\n"); diff --git a/miniz.c b/miniz.c index bfc234a..83e538a 100644 --- a/miniz.c +++ b/miniz.c @@ -320,7 +320,7 @@ int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char memset(&stream, 0, sizeof(stream)); /* In case mz_ulong is 64-bits (argh I hate longs). */ - if ((source_len | *pDest_len) > 0xFFFFFFFFU) + if ((mz_uint64)(source_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; stream.next_in = pSource; @@ -559,7 +559,7 @@ int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned cha memset(&stream, 0, sizeof(stream)); /* In case mz_ulong is 64-bits (argh I hate longs). */ - if ((*pSource_len | *pDest_len) > 0xFFFFFFFFU) + if ((mz_uint64)(*pSource_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR; stream.next_in = pSource; diff --git a/miniz_zip.c b/miniz_zip.c index b372bb2..0ecaf91 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -3194,7 +3194,7 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n pState->m_zip64 = MZ_TRUE; /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */ } - if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) + if (((mz_uint64)buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) { pState->m_zip64 = MZ_TRUE; /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */ @@ -4299,7 +4299,7 @@ mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip) if (pState->m_zip64) { - if ((pZip->m_total_files > MZ_UINT32_MAX) || (pState->m_central_dir.m_size >= MZ_UINT32_MAX)) + if (((mz_uint64)pZip->m_total_files > MZ_UINT32_MAX) || (pState->m_central_dir.m_size >= MZ_UINT32_MAX)) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); } else From 78ae3750168773afcddadad11b9b3e0871be7998 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Tue, 20 Jul 2021 18:03:56 +0100 Subject: [PATCH 41/63] Fix "'&array' may not produce intended result" warnings with OpenWatcom Co-authored-by: sezero --- miniz_common.h | 2 ++ miniz_tdef.c | 16 ++++++++-------- miniz_tinfl.c | 8 ++++---- miniz_zip.c | 14 +++++++------- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/miniz_common.h b/miniz_common.h index 231f5bc..afeb80e 100644 --- a/miniz_common.h +++ b/miniz_common.h @@ -58,6 +58,8 @@ typedef struct mz_dummy_time_t_tag #define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b)) #define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj)) +#define MZ_CLEAR_ARR(obj) memset((obj), 0, sizeof(obj)) +#define MZ_CLEAR_PTR(obj) memset((obj), 0, sizeof(*obj)) #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN #define MZ_READ_LE16(p) *((const mz_uint16 *)(p)) diff --git a/miniz_tdef.c b/miniz_tdef.c index 7ad65d6..e3fa672 100644 --- a/miniz_tdef.c +++ b/miniz_tdef.c @@ -104,7 +104,7 @@ static tdefl_sym_freq *tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq *p { mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2]; tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1; - MZ_CLEAR_OBJ(hist); + MZ_CLEAR_ARR(hist); for (i = 0; i < num_syms; i++) { mz_uint freq = pSyms0[i].m_key; @@ -222,7 +222,7 @@ static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int { int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE]; mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1]; - MZ_CLEAR_OBJ(num_codes); + MZ_CLEAR_ARR(num_codes); if (static_table) { for (i = 0; i < table_len; i++) @@ -248,8 +248,8 @@ static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit); - MZ_CLEAR_OBJ(d->m_huff_code_sizes[table_num]); - MZ_CLEAR_OBJ(d->m_huff_codes[table_num]); + MZ_CLEAR_ARR(d->m_huff_code_sizes[table_num]); + MZ_CLEAR_ARR(d->m_huff_codes[table_num]); for (i = 1, j = num_used_syms; i <= code_size_limit; i++) for (l = num_codes[i]; l > 0; l--) d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i); @@ -1302,8 +1302,8 @@ tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pI d->m_finished = (flush == TDEFL_FINISH); if (flush == TDEFL_FULL_FLUSH) { - MZ_CLEAR_OBJ(d->m_hash); - MZ_CLEAR_OBJ(d->m_next); + MZ_CLEAR_ARR(d->m_hash); + MZ_CLEAR_ARR(d->m_next); d->m_dict_size = 0; } } @@ -1326,7 +1326,7 @@ tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_fun d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0; d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3; if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) - MZ_CLEAR_OBJ(d->m_hash); + MZ_CLEAR_ARR(d->m_hash); d->m_lookahead_pos = d->m_lookahead_size = d->m_dict_size = d->m_total_lz_bytes = d->m_lz_code_buf_dict_pos = d->m_bits_in = 0; d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = d->m_block_index = d->m_bit_buffer = d->m_wants_to_finish = 0; d->m_pLZ_code_buf = d->m_lz_code_buf + 1; @@ -1347,7 +1347,7 @@ tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_fun d->m_src_buf_left = 0; d->m_out_buf_ofs = 0; if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) - MZ_CLEAR_OBJ(d->m_dict); + MZ_CLEAR_ARR(d->m_dict); memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0); memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1); return TDEFL_STATUS_OKAY; diff --git a/miniz_tinfl.c b/miniz_tinfl.c index 69c3f2d..05f42ac 100644 --- a/miniz_tinfl.c +++ b/miniz_tinfl.c @@ -292,7 +292,7 @@ tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_nex TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); r->m_table_sizes[counter] += s_min_table_sizes[counter]; } - MZ_CLEAR_OBJ(r->m_tables[2].m_code_size); + MZ_CLEAR_ARR(r->m_tables[2].m_code_size); for (counter = 0; counter < r->m_table_sizes[2]; counter++) { mz_uint s; @@ -307,9 +307,9 @@ tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_nex tinfl_huff_table *pTable; mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16]; pTable = &r->m_tables[r->m_type]; - MZ_CLEAR_OBJ(total_syms); - MZ_CLEAR_OBJ(pTable->m_look_up); - MZ_CLEAR_OBJ(pTable->m_tree); + MZ_CLEAR_ARR(total_syms); + MZ_CLEAR_ARR(pTable->m_look_up); + MZ_CLEAR_ARR(pTable->m_tree); for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) total_syms[pTable->m_code_size[i]]++; used_syms = 0, total = 0; diff --git a/miniz_zip.c b/miniz_zip.c index 0ecaf91..266fd70 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -845,7 +845,7 @@ static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, mz_uint flag void mz_zip_zero_struct(mz_zip_archive *pZip) { if (pZip) - MZ_CLEAR_OBJ(*pZip); + MZ_CLEAR_PTR(pZip); } static mz_bool mz_zip_reader_end_internal(mz_zip_archive *pZip, mz_bool set_last_error) @@ -2831,7 +2831,7 @@ mz_bool mz_zip_writer_init_file_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint64 cur_ofs = 0; char buf[4096]; - MZ_CLEAR_OBJ(buf); + MZ_CLEAR_ARR(buf); do { @@ -3287,7 +3287,7 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n } cur_archive_file_ofs += num_alignment_padding_bytes; - MZ_CLEAR_OBJ(local_dir_header); + MZ_CLEAR_ARR(local_dir_header); if (!store_data_uncompressed || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) { @@ -3540,7 +3540,7 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA method = MZ_DEFLATED; } - MZ_CLEAR_OBJ(local_dir_header); + MZ_CLEAR_ARR(local_dir_header); if (pState->m_zip64) { if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) @@ -4327,7 +4327,7 @@ mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip) /* Write zip64 end of central directory header */ mz_uint64 rel_ofs_to_zip64_ecdr = pZip->m_archive_size; - MZ_CLEAR_OBJ(hdr); + MZ_CLEAR_ARR(hdr); MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDH_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG); MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - sizeof(mz_uint32) - sizeof(mz_uint64)); MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS, 0x031E); /* TODO: always Unix */ @@ -4342,7 +4342,7 @@ mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip) pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE; /* Write zip64 end of central directory locator */ - MZ_CLEAR_OBJ(hdr); + MZ_CLEAR_ARR(hdr); MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG); MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS, rel_ofs_to_zip64_ecdr); MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS, 1); @@ -4353,7 +4353,7 @@ mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip) } /* Write end of central directory record */ - MZ_CLEAR_OBJ(hdr); + MZ_CLEAR_ARR(hdr); MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_SIG_OFS, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG); MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files)); MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files)); From fc0c0f585eace7293bbf921985b3e1c44732ce30 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Tue, 20 Jul 2021 18:22:34 +0100 Subject: [PATCH 42/63] Avoid using unaligned memory access in UBSan builds --- miniz.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/miniz.h b/miniz.h index 42e1ea2..185e596 100644 --- a/miniz.h +++ b/miniz.h @@ -168,6 +168,13 @@ #define MINIZ_LITTLE_ENDIAN 0 #endif +/* Using unaligned loads and stores causes errors when using UBSan */ +#if defined(__has_feature) +#if __has_feature(undefined_behavior_sanitizer) +#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 +#endif +#endif + /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES only if not set */ #if !defined(MINIZ_USE_UNALIGNED_LOADS_AND_STORES) #if MINIZ_X86_OR_X64_CPU From f99eae6e0467cad2511dd3ee5d74d2207fcfc81d Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Tue, 20 Jul 2021 20:17:06 +0100 Subject: [PATCH 43/63] Set MINIZ_LITTLE_ENDIAN only if not set --- miniz.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/miniz.h b/miniz.h index 42e1ea2..ec918d2 100644 --- a/miniz.h +++ b/miniz.h @@ -161,12 +161,15 @@ #define MINIZ_X86_OR_X64_CPU 0 #endif +/* Set MINIZ_LITTLE_ENDIAN only if not set */ +#if !defined(MINIZ_LITTLE_ENDIAN) #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU /* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */ #define MINIZ_LITTLE_ENDIAN 1 #else #define MINIZ_LITTLE_ENDIAN 0 #endif +#endif /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES only if not set */ #if !defined(MINIZ_USE_UNALIGNED_LOADS_AND_STORES) From 9b896897231d06439b2973beef1723b9782e29c6 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Tue, 20 Jul 2021 15:21:19 +0100 Subject: [PATCH 44/63] Add MINIZ_NO_DEFLATE_APIS and MINIZ_NO_INFLATE_APIS --- amalgamate.sh | 2 +- miniz.c | 8 ++++++++ miniz.h | 30 +++++++++++++++++++++++++++++- miniz_tdef.c | 4 ++++ miniz_tdef.h | 4 ++++ miniz_tinfl.c | 4 ++++ miniz_tinfl.h | 4 ++++ 7 files changed, 54 insertions(+), 2 deletions(-) diff --git a/amalgamate.sh b/amalgamate.sh index 796ee02..63130d8 100755 --- a/amalgamate.sh +++ b/amalgamate.sh @@ -18,7 +18,7 @@ then echo "Test compile with clang..." clang -Wall -Wpedantic -fsanitize=unsigned-integer-overflow -I$OUTPUT_PREFIX main.c $OUTPUT_PREFIX/miniz.c -o test.out fi -for def in MINIZ_NO_STDIO MINIZ_NO_TIME MINIZ_NO_ARCHIVE_APIS MINIZ_NO_ARCHIVE_WRITING_APIS MINIZ_NO_ZLIB_APIS MINIZ_NO_ZLIB_COMPATIBLE_NAMES MINIZ_NO_MALLOC +for def in MINIZ_NO_STDIO MINIZ_NO_TIME MINIZ_NO_DEFLATE_APIS MINIZ_NO_INFLATE_APIS MINIZ_NO_ARCHIVE_APIS MINIZ_NO_ARCHIVE_WRITING_APIS MINIZ_NO_ZLIB_APIS MINIZ_NO_ZLIB_COMPATIBLE_NAMES MINIZ_NO_MALLOC do echo "Test compile with GCC and define $def..." gcc -ansi -pedantic -Wall -I$OUTPUT_PREFIX main.c $OUTPUT_PREFIX/miniz.c -o test.out -D${def} diff --git a/miniz.c b/miniz.c index bfc234a..aa68b60 100644 --- a/miniz.c +++ b/miniz.c @@ -186,6 +186,8 @@ const char *mz_version(void) #ifndef MINIZ_NO_ZLIB_APIS +#ifndef MINIZ_NO_DEFLATE_APIS + int mz_deflateInit(mz_streamp pStream, int level) { return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, MZ_DEFAULT_STRATEGY); @@ -353,6 +355,10 @@ mz_ulong mz_compressBound(mz_ulong source_len) return mz_deflateBound(NULL, source_len); } +#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ + +#ifndef MINIZ_NO_INFLATE_APIS + typedef struct { tinfl_decompressor m_decomp; @@ -588,6 +594,8 @@ int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char return mz_uncompress2(pDest, pDest_len, pSource, &source_len); } +#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ + const char *mz_error(int err) { static struct diff --git a/miniz.h b/miniz.h index 42e1ea2..3397891 100644 --- a/miniz.h +++ b/miniz.h @@ -115,7 +115,7 @@ #include "miniz_export.h" /* Defines to completely disable specific portions of miniz.c: - If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl. */ + If all macros here are defined the only functionality remaining will be CRC-32 and adler-32. */ /* Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O. */ /*#define MINIZ_NO_STDIO */ @@ -125,6 +125,12 @@ /* The current downside is the times written to your archives will be from 1979. */ /*#define MINIZ_NO_TIME */ +/* Define MINIZ_NO_DEFLATE_APIS to disable all compression API's. */ +/*#define MINIZ_NO_DEFLATE_APIS */ + +/* Define MINIZ_NO_INFLATE_APIS to disable all decompression API's. */ +/*#define MINIZ_NO_INFLATE_APIS */ + /* Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. */ /*#define MINIZ_NO_ARCHIVE_APIS */ @@ -143,6 +149,14 @@ functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work. */ /*#define MINIZ_NO_MALLOC */ +#ifdef MINIZ_NO_INFLATE_APIS +#define MINIZ_NO_ARCHIVE_APIS +#endif + +#ifdef MINIZ_NO_DEFLATE_APIS +#define MINIZ_NO_ARCHIVE_WRITING_APIS +#endif + #if defined(__TINYC__) && (defined(__linux) || defined(__linux__)) /* TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux */ #define MINIZ_NO_TIME @@ -304,6 +318,8 @@ typedef mz_stream *mz_streamp; /* Returns the version string of miniz.c. */ MINIZ_EXPORT const char *mz_version(void); +#ifndef MINIZ_NO_DEFLATE_APIS + /* mz_deflateInit() initializes a compressor with default options: */ /* Parameters: */ /* pStream must point to an initialized mz_stream struct. */ @@ -356,6 +372,10 @@ MINIZ_EXPORT int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const u /* mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress(). */ MINIZ_EXPORT mz_ulong mz_compressBound(mz_ulong source_len); +#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ + +#ifndef MINIZ_NO_INFLATE_APIS + /* Initializes a decompressor. */ MINIZ_EXPORT int mz_inflateInit(mz_streamp pStream); @@ -389,6 +409,7 @@ MINIZ_EXPORT int mz_inflateEnd(mz_streamp pStream); /* Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure. */ MINIZ_EXPORT int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len); MINIZ_EXPORT int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong *pSource_len); +#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ /* Returns a string description of the specified error code, or NULL if the error code is invalid. */ MINIZ_EXPORT const char *mz_error(int err); @@ -439,6 +460,8 @@ typedef void *const voidpc; #define free_func mz_free_func #define internal_state mz_internal_state #define z_stream mz_stream + +#ifndef MINIZ_NO_DEFLATE_APIS #define deflateInit mz_deflateInit #define deflateInit2 mz_deflateInit2 #define deflateReset mz_deflateReset @@ -448,6 +471,9 @@ typedef void *const voidpc; #define compress mz_compress #define compress2 mz_compress2 #define compressBound mz_compressBound +#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ + +#ifndef MINIZ_NO_INFLATE_APIS #define inflateInit mz_inflateInit #define inflateInit2 mz_inflateInit2 #define inflateReset mz_inflateReset @@ -455,6 +481,8 @@ typedef void *const voidpc; #define inflateEnd mz_inflateEnd #define uncompress mz_uncompress #define uncompress2 mz_uncompress2 +#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ + #define crc32 mz_crc32 #define adler32 mz_adler32 #define MAX_WBITS 15 diff --git a/miniz_tdef.c b/miniz_tdef.c index 7ad65d6..353e124 100644 --- a/miniz_tdef.c +++ b/miniz_tdef.c @@ -26,6 +26,8 @@ #include "miniz.h" +#ifndef MINIZ_NO_DEFLATE_APIS + #ifdef __cplusplus extern "C" { #endif @@ -1575,3 +1577,5 @@ void tdefl_compressor_free(tdefl_compressor *pComp) #ifdef __cplusplus } #endif + +#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ diff --git a/miniz_tdef.h b/miniz_tdef.h index 9b3e9d2..6ce6938 100644 --- a/miniz_tdef.h +++ b/miniz_tdef.h @@ -1,6 +1,8 @@ #pragma once #include "miniz_common.h" +#ifndef MINIZ_NO_DEFLATE_APIS + #ifdef __cplusplus extern "C" { #endif @@ -188,3 +190,5 @@ MINIZ_EXPORT void tdefl_compressor_free(tdefl_compressor *pComp); #ifdef __cplusplus } #endif + +#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ diff --git a/miniz_tinfl.c b/miniz_tinfl.c index 69c3f2d..a16b371 100644 --- a/miniz_tinfl.c +++ b/miniz_tinfl.c @@ -26,6 +26,8 @@ #include "miniz.h" +#ifndef MINIZ_NO_INFLATE_APIS + #ifdef __cplusplus extern "C" { #endif @@ -738,3 +740,5 @@ void tinfl_decompressor_free(tinfl_decompressor *pDecomp) #ifdef __cplusplus } #endif + +#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ diff --git a/miniz_tinfl.h b/miniz_tinfl.h index 1b4a820..1918774 100644 --- a/miniz_tinfl.h +++ b/miniz_tinfl.h @@ -2,6 +2,8 @@ #include "miniz_common.h" /* ------------------- Low-level Decompression API Definitions */ +#ifndef MINIZ_NO_INFLATE_APIS + #ifdef __cplusplus extern "C" { #endif @@ -142,3 +144,5 @@ struct tinfl_decompressor_tag #ifdef __cplusplus } #endif + +#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ From 583f4f27c2d527a245d2272e10a43de35a1e7357 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Sun, 25 Jul 2021 15:36:22 +0100 Subject: [PATCH 45/63] Fix use of uninitialized memory in tinfl_decompress_mem_to_callback() Co-authored-by: sezero --- miniz_tinfl.c | 1 + 1 file changed, 1 insertion(+) diff --git a/miniz_tinfl.c b/miniz_tinfl.c index 69c3f2d..6e9849f 100644 --- a/miniz_tinfl.c +++ b/miniz_tinfl.c @@ -699,6 +699,7 @@ int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, size_t in_buf_ofs = 0, dict_ofs = 0; if (!pDict) return TINFL_STATUS_FAILED; + memset(pDict,0,TINFL_LZ_DICT_SIZE); tinfl_init(&decomp); for (;;) { From 70c79cc9d501ca226796ba30faaafed23b329499 Mon Sep 17 00:00:00 2001 From: Zsombor Fazekas Date: Thu, 16 Sep 2021 12:00:44 +0200 Subject: [PATCH 46/63] Use wfopen on windows --- miniz_zip.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/miniz_zip.c b/miniz_zip.c index dce07fe..e1e0d47 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -40,19 +40,40 @@ extern "C" { #include #if defined(_MSC_VER) || defined(__MINGW64__) + +#define WIN32_LEAN_AND_MEAN +#include + +static WCHAR* mz_utf8z_to_widechar(const char* str) +{ + int reqChars = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); + WCHAR* wStr = (WCHAR*)malloc(reqChars * sizeof(WCHAR)); + MultiByteToWideChar(CP_UTF8, 0, str, -1, wStr, sizeof(WCHAR) * reqChars); + return wStr; +} + static FILE *mz_fopen(const char *pFilename, const char *pMode) { - FILE *pFile = NULL; - fopen_s(&pFile, pFilename, pMode); - return pFile; + WCHAR* wFilename = mz_utf8z_to_widechar(pFilename); + WCHAR* wMode = mz_utf8z_to_widechar(pMode); + FILE* pFile = NULL; + errno_t err = _wfopen_s(&pFile, wFilename, wMode); + free(wFilename); + free(wMode); + return err ? NULL : pFile; } + static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) { - FILE *pFile = NULL; - if (freopen_s(&pFile, pPath, pMode, pStream)) - return NULL; - return pFile; + WCHAR* wPath = mz_utf8z_to_widechar(pPath); + WCHAR* wMode = mz_utf8z_to_widechar(pMode); + FILE* pFile = NULL; + errno_t err = _wfreopen_s(&pFile, wPath, wMode, pStream); + free(wPath); + free(wMode); + return err ? NULL : pFile; } + #ifndef MINIZ_NO_TIME #include #endif From d9c899cea49cacd6caa344628aa8518765960020 Mon Sep 17 00:00:00 2001 From: Zsombor Fazekas Date: Thu, 16 Sep 2021 12:12:07 +0200 Subject: [PATCH 47/63] Use _wstat64 instead _stat64 on windows --- miniz_zip.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/miniz_zip.c b/miniz_zip.c index e1e0d47..5b8f7c1 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -74,6 +74,14 @@ static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) return err ? NULL : pFile; } +static int mz_stat64(const char *path, struct __stat64 *buffer) +{ + WCHAR* wPath = mz_utf8z_to_widechar(path); + int res = _wstat64(wPath, buffer); + free(wPath); + return res; +} + #ifndef MINIZ_NO_TIME #include #endif @@ -84,7 +92,7 @@ static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) #define MZ_FTELL64 _ftelli64 #define MZ_FSEEK64 _fseeki64 #define MZ_FILE_STAT_STRUCT _stat64 -#define MZ_FILE_STAT _stat64 +#define MZ_FILE_STAT mz_stat64 #define MZ_FFLUSH fflush #define MZ_FREOPEN mz_freopen #define MZ_DELETE_FILE remove From a7961f5b4c955c0ccc090e0b24233d7d764e1289 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 3 Nov 2021 14:04:11 +0100 Subject: [PATCH 48/63] Fix a couple typos found by codespell --- ChangeLog.md | 2 +- miniz_zip.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index a1abf16..da678a5 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -123,7 +123,7 @@ Merged over a few very minor bug fixes that I fixed in the zip64 branch. This is Interim bugfix release while I work on the next major release with zip64 and streaming compression/decompression support. Fixed the MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY bug (thanks kahmyong.moon@hp.com), which could cause the locate files func to not find files when this flag was specified. Also fixed a bug in mz_zip_reader_extract_to_mem_no_alloc() with user provided read buffers (thanks kymoon). I also merged lots of compiler fixes from various github repo branches and Google Code issue reports. I finally added cmake support (only tested under for Linux so far), compiled and tested with clang v3.3 and gcc 4.6 (under Linux), added defl_write_image_to_png_file_in_memory_ex() (supports Y flipping for OpenGL use, real-time compression), added a new PNG example (example6.c - Mandelbrot), and I added 64-bit file I/O support (stat64(), etc.) for glibc. - Critical fix for the MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY bug (thanks kahmyong.moon@hp.com) which could cause locate files to not find files. This bug - would only have occured in earlier versions if you explicitly used this flag, OR if you used mz_zip_extract_archive_file_to_heap() or mz_zip_add_mem_to_archive_file_in_place() + would only have occurred in earlier versions if you explicitly used this flag, OR if you used mz_zip_extract_archive_file_to_heap() or mz_zip_add_mem_to_archive_file_in_place() (which used this flag). If you can't switch to v1.15 but want to fix this bug, just remove the uses of this flag from both helper funcs (and of course don't use the flag). - Bugfix in mz_zip_reader_extract_to_mem_no_alloc() from kymoon when pUser_read_buf is not NULL and compressed size is > uncompressed size - Fixing mz_zip_reader_extract_*() funcs so they don't try to extract compressed data from directory entries, to account for weird zipfiles which contain zero-size compressed data on dir entries. diff --git a/miniz_zip.h b/miniz_zip.h index 82502bd..17ff498 100644 --- a/miniz_zip.h +++ b/miniz_zip.h @@ -408,7 +408,7 @@ MINIZ_EXPORT mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_ /* An archive must be manually finalized by calling this function for it to be valid. */ MINIZ_EXPORT mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip); -/* Finalizes a heap archive, returning a poiner to the heap block and its size. */ +/* Finalizes a heap archive, returning a pointer to the heap block and its size. */ /* The heap block will be allocated using the mz_zip_archive's alloc/realloc callbacks. */ MINIZ_EXPORT mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **ppBuf, size_t *pSize); From e841503cad78efd62e8c12b900b36243776dab34 Mon Sep 17 00:00:00 2001 From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com> Date: Wed, 10 Nov 2021 21:59:32 -0500 Subject: [PATCH 49/63] [CMakeLists.txt] Enforce C89; [examples/example6.c] Conform to C89; [miniz_zip.c] Use lowercase --- CMakeLists.txt | 10 ++++++++++ examples/example6.c | 32 +++++++++++++++++--------------- miniz_zip.c | 2 +- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f12435b..112b8a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,16 @@ if(CMAKE_MINOR_VERSION LESS 12) # see issue https://gitlab.kitware.com/cmake/cmake/merge_requests/1799 else() project(miniz C) + set(CMAKE_C_STANDARD 90) + set(CMAKE_VERBOSE_MAKEFILE ON) + # set(CMAKE_C_VISIBILITY_PRESET hidden) + # set(CMAKE_VISIBILITY_INLINES_HIDDEN YES) + + if (MSVC) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /WX /Zi /permissive-") + else () + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow -Wformat=2 -Wall -Wno-overlength-strings -pedantic") + endif () endif() set(MINIZ_API_VERSION 2) diff --git a/examples/example6.c b/examples/example6.c index abbb64f..08df8b5 100644 --- a/examples/example6.c +++ b/examples/example6.c @@ -34,27 +34,29 @@ static void hsv_to_rgb(int hue, int min, int max, rgb_t *p) if (!saturation) { p->r = p->g = p->b = 255 * (max - hue) / (max - min); return; - } - double h = fmod(color_rotate + 1e-4 + 4.0 * (hue - min) / (max - min), 6); - double c = 255.0f * saturation; - double X = c * (1 - fabs(fmod(h, 2) - 1)); + } else { + const double h_dbl = fmod(color_rotate + 1e-4 + 4.0 * (hue - min) / (max - min), 6); + const double c_dbl = 255 * saturation; + const double X_dbl = c_dbl * (1 - fabs(fmod(h_dbl, 2) - 1)); + const int h = (int)h_dbl; + const int c = (int)c_dbl; + const int X = (int)X_dbl; - p->r = p->g = p->b = 0; + p->r = p->g = p->b = 0; - switch((int)h) { - case 0: p->r = c; p->g = X; return; - case 1: p->r = X; p->g = c; return; - case 2: p->g = c; p->b = X; return; - case 3: p->g = X; p->b = c; return; - case 4: p->r = X; p->b = c; return; - default:p->r = c; p->b = X; + switch(h) { + case 0: p->r = c; p->g = X; return; + case 1: p->r = X; p->g = c; return; + case 2: p->g = c; p->b = X; return; + case 3: p->g = X; p->b = c; return; + case 4: p->r = X; p->b = c; return; + default:p->r = c; p->b = X; + } } } int main(int argc, char *argv[]) { - (void)argc, (void)argv; - // Image resolution const int iXmax = 4096; const int iYmax = 4096; @@ -134,7 +136,7 @@ int main(int argc, char *argv[]) uint Iterations = color[0] | (color[1] << 8U); - hsv_to_rgb(Iterations, MinIter, MaxIter, (rgb_t *)color); + hsv_to_rgb((int)Iterations, MinIter, MaxIter, (rgb_t *)color); } } diff --git a/miniz_zip.c b/miniz_zip.c index 5b8f7c1..942a057 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -42,7 +42,7 @@ extern "C" { #if defined(_MSC_VER) || defined(__MINGW64__) #define WIN32_LEAN_AND_MEAN -#include +#include static WCHAR* mz_utf8z_to_widechar(const char* str) { From cda0b3e7f0944b728d368f9601cb4aaf5228223a Mon Sep 17 00:00:00 2001 From: Ellie Date: Thu, 11 Nov 2021 13:38:57 +0100 Subject: [PATCH 50/63] Fix MinGW cross compilation by removing unneeded capitalization --- miniz_zip.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz_zip.c b/miniz_zip.c index 5b8f7c1..942a057 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -42,7 +42,7 @@ extern "C" { #if defined(_MSC_VER) || defined(__MINGW64__) #define WIN32_LEAN_AND_MEAN -#include +#include static WCHAR* mz_utf8z_to_widechar(const char* str) { From b4108c6fcf109a386cab981551550be469315222 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 28 Nov 2021 17:26:05 +0100 Subject: [PATCH 51/63] Adjust usage text Miniz has a history of being a single header library. Nowadays this might be less important. So clarify that the library can be used via other ways. --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 3f8fd73..19a8212 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ Miniz is a lossless, high performance data compression library in a single sourc ## Usage -Please use the files from the [releases page](https://github.com/richgel999/miniz/releases) in your projects. Do not use the git checkout directly! The different source and header files are [amalgamated](https://www.sqlite.org/amalgamation.html) into one `miniz.c`/`miniz.h` pair in a build step (`amalgamate.sh`). Include `miniz.c` and `miniz.h` in your project to use Miniz. +Releases are available at the [releases page](https://github.com/richgel999/miniz/releases) as a pair of `miniz.c`/`miniz.h` files which can be simply added to a project. To create this file pair the different source and header files are [amalgamated](https://www.sqlite.org/amalgamation.html) during build. Alternatively use as cmake or meson module (or build system of your choice). ## Features From 75c948a1dd9a7638136d6a3610aaf9aa92a1f14a Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 28 Nov 2021 17:35:49 +0100 Subject: [PATCH 52/63] Only define MINIZ_EXPORT if not already defined --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 112b8a5..61b7a6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,7 +78,7 @@ if(AMALGAMATE_SOURCES) string(REPLACE "#include \"${REPLACE_STRING}.h\"" "" AMAL_MINIZ_H "${AMAL_MINIZ_H}") string(REPLACE "#include \"${REPLACE_STRING}.h\"" "" AMAL_MINIZ_C "${AMAL_MINIZ_C}") endforeach() - string(CONCAT AMAL_MINIZ_H "#define MINIZ_EXPORT\n" "${AMAL_MINIZ_H}") + string(CONCAT AMAL_MINIZ_H "#ifndef MINIZ_EXPORT\n#define MINIZ_EXPORT\n#endif\n" "${AMAL_MINIZ_H}") if(BUILD_HEADER_ONLY) string(CONCAT AMAL_MINIZ_H "${AMAL_MINIZ_H}" "\n#ifndef MINIZ_HEADER_FILE_ONLY\n" "${AMAL_MINIZ_C}" "\n#endif // MINIZ_HEADER_FILE_ONLY\n") From 3e5e488ba9e86bfcbd7532f3d68b4e42bc540f00 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 28 Nov 2021 18:08:27 +0100 Subject: [PATCH 53/63] Use level_and_flags after MZ_DEFAULT_COMPRESSION has been handled --- miniz_zip.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/miniz_zip.c b/miniz_zip.c index 3a56479..e38f437 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -3466,7 +3466,7 @@ mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_n mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pArchive_name, mz_file_read_func read_callback, void* callback_opaque, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len) { - mz_uint16 gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; + mz_uint16 gen_flags; mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes; mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0; mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0; @@ -3485,6 +3485,8 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA level_and_flags = MZ_DEFAULT_LEVEL; level = level_and_flags & 0xF; + gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; + /* Sanity checks */ if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); From 0a880835892c33c4a272db875d70729311f04a5d Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 28 Nov 2021 18:35:19 +0100 Subject: [PATCH 54/63] Improve endianess detection --- miniz.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/miniz.h b/miniz.h index 99d71ee..f794df6 100644 --- a/miniz.h +++ b/miniz.h @@ -177,11 +177,23 @@ /* Set MINIZ_LITTLE_ENDIAN only if not set */ #if !defined(MINIZ_LITTLE_ENDIAN) -#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU +#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) + +#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) /* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */ #define MINIZ_LITTLE_ENDIAN 1 #else #define MINIZ_LITTLE_ENDIAN 0 +#endif + +#else + +#if MINIZ_X86_OR_X64_CPU +#define MINIZ_LITTLE_ENDIAN 1 +#else +#define MINIZ_LITTLE_ENDIAN 0 +#endif + #endif #endif From 2beb69d0a5633682148cfc0722f07329612a4478 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 28 Nov 2021 18:35:46 +0100 Subject: [PATCH 55/63] Don't use unaligned stores and loads per default --- miniz.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz.h b/miniz.h index f794df6..ec3e1f9 100644 --- a/miniz.h +++ b/miniz.h @@ -208,7 +208,7 @@ #if !defined(MINIZ_USE_UNALIGNED_LOADS_AND_STORES) #if MINIZ_X86_OR_X64_CPU /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses. */ -#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 +#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 #define MINIZ_UNALIGNED_USE_MEMCPY #else #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 From 12b78b6d5aa23c1958cbbf98fcb6b9c1e2ad73ee Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 28 Nov 2021 18:36:40 +0100 Subject: [PATCH 56/63] Fix function definitions Fix function definitions tdefl_compressor_alloc(void) and tinfl_decompressor_alloc(void) to not accept any amount of arguments. --- miniz_tdef.c | 2 +- miniz_tinfl.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/miniz_tdef.c b/miniz_tdef.c index b3e95f2..313a03c 100644 --- a/miniz_tdef.c +++ b/miniz_tdef.c @@ -1559,7 +1559,7 @@ void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, /* Allocate the tdefl_compressor and tinfl_decompressor structures in C so that */ /* non-C language bindings to tdefL_ and tinfl_ API don't need to worry about */ /* structure size and allocation mechanism. */ -tdefl_compressor *tdefl_compressor_alloc() +tdefl_compressor *tdefl_compressor_alloc(void) { return (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor)); } diff --git a/miniz_tinfl.c b/miniz_tinfl.c index a62cdaa..938ca09 100644 --- a/miniz_tinfl.c +++ b/miniz_tinfl.c @@ -724,7 +724,7 @@ int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, } #ifndef MINIZ_NO_MALLOC -tinfl_decompressor *tinfl_decompressor_alloc() +tinfl_decompressor *tinfl_decompressor_alloc(void) { tinfl_decompressor *pDecomp = (tinfl_decompressor *)MZ_MALLOC(sizeof(tinfl_decompressor)); if (pDecomp) From 72e7267054f90f6c2c3983db34876566f8fe8382 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 28 Nov 2021 18:37:55 +0100 Subject: [PATCH 57/63] Fix unaligned pointer access --- miniz_zip.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/miniz_zip.c b/miniz_zip.c index e38f437..8e16cf7 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -4191,10 +4191,10 @@ mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive * if (pZip->m_pState->m_zip64) { /* dest is zip64, so upgrade the data descriptor */ - const mz_uint32 *pSrc_descriptor = (const mz_uint32 *)((const mz_uint8 *)pBuf + (has_id ? sizeof(mz_uint32) : 0)); - const mz_uint32 src_crc32 = pSrc_descriptor[0]; - const mz_uint64 src_comp_size = pSrc_descriptor[1]; - const mz_uint64 src_uncomp_size = pSrc_descriptor[2]; + const mz_uint8 *pSrc_descriptor = (const mz_uint8 *)pBuf + (has_id ? sizeof(mz_uint32) : 0); + const mz_uint32 src_crc32 = MZ_READ_LE32(pSrc_descriptor); + const mz_uint64 src_comp_size = MZ_READ_LE32(pSrc_descriptor + sizeof(mz_uint32)); + const mz_uint64 src_uncomp_size = MZ_READ_LE32(pSrc_descriptor + 2*sizeof(mz_uint32)); mz_write_le32((mz_uint8 *)pBuf, MZ_ZIP_DATA_DESCRIPTOR_ID); mz_write_le32((mz_uint8 *)pBuf + sizeof(mz_uint32) * 1, src_crc32); From ecbbbe013865f8aa7f0fe83bfef26254b7d271c9 Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 28 Nov 2021 18:38:56 +0100 Subject: [PATCH 58/63] Fix function declaration if MINIZ_NO_STDIO is used --- miniz_zip.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/miniz_zip.h b/miniz_zip.h index 17ff498..33033f3 100644 --- a/miniz_zip.h +++ b/miniz_zip.h @@ -334,7 +334,9 @@ MINIZ_EXPORT mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags /* Misc utils/helpers, valid for ZIP reading or writing */ MINIZ_EXPORT mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size, mz_uint flags, mz_zip_error *pErr); +#ifndef MINIZ_NO_STDIO MINIZ_EXPORT mz_bool mz_zip_validate_file_archive(const char *pFilename, mz_uint flags, mz_zip_error *pErr); +#endif /* Universal end function - calls either mz_zip_reader_end() or mz_zip_writer_end(). */ MINIZ_EXPORT mz_bool mz_zip_end(mz_zip_archive *pZip); @@ -425,11 +427,13 @@ MINIZ_EXPORT mz_bool mz_zip_writer_end(mz_zip_archive *pZip); MINIZ_EXPORT mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); MINIZ_EXPORT mz_bool mz_zip_add_mem_to_archive_file_in_place_v2(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_zip_error *pErr); +#ifndef MINIZ_NO_STDIO /* Reads a single file from an archive into a heap block. */ /* If pComment is not NULL, only the file with the specified comment will be extracted. */ /* Returns NULL on failure. */ MINIZ_EXPORT void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint flags); MINIZ_EXPORT void *mz_zip_extract_archive_file_to_heap_v2(const char *pZip_filename, const char *pArchive_name, const char *pComment, size_t *pSize, mz_uint flags, mz_zip_error *pErr); +#endif #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */ From a956b4c753fb929d3f0c0608b518a3013903333b Mon Sep 17 00:00:00 2001 From: Martin Date: Sat, 11 Dec 2021 12:51:50 +0100 Subject: [PATCH 59/63] Fix MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8 not being set --- miniz_zip.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/miniz_zip.c b/miniz_zip.c index 8e16cf7..b529759 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -3478,15 +3478,15 @@ mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pA mz_zip_internal_state *pState; mz_uint64 file_ofs = 0, cur_archive_header_file_ofs; - if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) - gen_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8; - if ((int)level_and_flags < 0) level_and_flags = MZ_DEFAULT_LEVEL; level = level_and_flags & 0xF; gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; + if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) + gen_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8; + /* Sanity checks */ if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); From 13b21dddc733862f3a653809b671761849e18285 Mon Sep 17 00:00:00 2001 From: Martin Date: Sat, 11 Dec 2021 12:59:21 +0100 Subject: [PATCH 60/63] Remove total files check (its 32-bit uint) This fixes a compiler warning. The check didn't do anything since m_total_files is a 32-bit uint and MZ_UINT32_MAX is actually a valid value. --- miniz_zip.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/miniz_zip.c b/miniz_zip.c index b529759..6bb6621 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -2496,9 +2496,6 @@ mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags) } else { - if (pZip->m_total_files >= MZ_UINT32_MAX) - return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); - if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); } @@ -4330,7 +4327,7 @@ mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip) if (pState->m_zip64) { - if (((mz_uint64)pZip->m_total_files > MZ_UINT32_MAX) || (pState->m_central_dir.m_size >= MZ_UINT32_MAX)) + if ((mz_uint64)pState->m_central_dir.m_size >= MZ_UINT32_MAX) return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); } else From aa70ef16445f239c540f06bfbf8a208204a04afe Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Mon, 27 Dec 2021 10:50:32 +0300 Subject: [PATCH 61/63] tinfl_decompress: avoid NULL ptr arithmetic UB: Fixes: https://github.com/richgel999/miniz/issues/216 Also see: https://github.com/mitkus/miniz/commit/6b8c30e4a95dd649adf14cf4bcad90701e323b9a --- miniz_tinfl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz_tinfl.c b/miniz_tinfl.c index 938ca09..11467be 100644 --- a/miniz_tinfl.c +++ b/miniz_tinfl.c @@ -186,7 +186,7 @@ tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_nex mz_uint32 num_bits, dist, counter, num_extra; tinfl_bit_buf_t bit_buf; const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size; - mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size; + mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next ? pOut_buf_next + *pOut_buf_size : NULL; size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start; /* Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter). */ From 501a76154940c465bc3e97f7e2d16134021bd8aa Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Sun, 2 Jan 2022 20:56:56 +0300 Subject: [PATCH 62/63] miniz_zip: fix mz_zip_reader_extract_to_heap to read correct sizes Fixes: https://github.com/richgel999/miniz/issues/218 --- miniz_zip.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/miniz_zip.c b/miniz_zip.c index 6bb6621..3b88d7d 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -1456,7 +1456,8 @@ mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip, const char *pName, co return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND); } -mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) +static +mz_bool mz_zip_reader_extract_to_mem_no_alloc1(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size, const mz_zip_archive_file_stat *st) { int status = TINFL_STATUS_DONE; mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail; @@ -1469,6 +1470,9 @@ mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead)) return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); + if (st) { + file_stat = *st; + } else if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) return MZ_FALSE; @@ -1599,17 +1603,22 @@ mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file return status == TINFL_STATUS_DONE; } +mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) +{ + return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size, NULL); +} + mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size) { mz_uint32 file_index; if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) return MZ_FALSE; - return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size); + return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size, NULL); } mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags) { - return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, NULL, 0); + return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, NULL, 0, NULL); } mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags) @@ -1619,23 +1628,17 @@ mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFil void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags) { - mz_uint64 comp_size, uncomp_size, alloc_size; - const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index); + mz_zip_archive_file_stat file_stat; + mz_uint64 alloc_size; void *pBuf; if (pSize) *pSize = 0; - if (!p) - { - mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER); - return NULL; - } + if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) + return MZ_FALSE; - comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS); - uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS); - - alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? comp_size : uncomp_size; + alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size; if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) { mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR); @@ -1648,7 +1651,7 @@ void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, si return NULL; } - if (!mz_zip_reader_extract_to_mem(pZip, file_index, pBuf, (size_t)alloc_size, flags)) + if (!mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, (size_t)alloc_size, flags, NULL, 0, &file_stat)) { pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf); return NULL; From f3d9e2293bdf9da952747cdd794a4fa83e0e5b24 Mon Sep 17 00:00:00 2001 From: Martin Raiber Date: Sat, 22 Jan 2022 22:14:31 +0100 Subject: [PATCH 63/63] Fix return value --- miniz_zip.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniz_zip.c b/miniz_zip.c index 3b88d7d..f9559e2 100644 --- a/miniz_zip.c +++ b/miniz_zip.c @@ -1636,7 +1636,7 @@ void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, si *pSize = 0; if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) - return MZ_FALSE; + return NULL; alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size; if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF))