* Update the Vendored miniz to 3.1.2 The 2.1.0 copy reads every .3mf and plugin archive we open. Upstream has since fixed a central-directory offset overflow when reading the zip header, an infinite loop in tinfl_decompress on a zero code length, an integer overflow in the header corruption check, wrong sizes in mz_zip_reader_extract_to_heap and several uninitialised reads on malformed deflate streams, none of which 2.1.0 has. The local modifications are carried over onto the new amalgamation: m_is_utf8 on mz_zip_archive_file_stat, mz_zip_reader_get_extra and mz_zip_reader_get_filename_from_extra, mz_zip_reader_extract_to_file_w and mz_zip_writer_add_file_ex for Windows paths, the PrusaSlicer staged writer (mz_zip_writer_add_staged_open/data/finish) and the zip64 "version needed" fields. The max_size form of mz_zip_writer_add_read_buf_callback that PrusaSlicer had patched in is upstream since 2.2.0 and is taken from there. Verified on Linux: the application and tests build, and the [3mf], [AMF], [plugin], [project], [SL1Import] and [TextureImport] libslic3r tests pass (88 cases, 1908 assertions). * Pass UTF-8 Paths to miniz on Windows miniz 3.x converts narrow file names from UTF-8 to wide characters before opening them, where the vendored 2.1.0 passed them to fopen as local code page bytes. The call sites that wrapped their paths in encode_path() therefore handed it code page bytes to decode as UTF-8, which breaks any non-ASCII path. Pass the UTF-8 strings straight through; the zip entry names still use the native encoding plus the Unicode path extra field. * Record the Local miniz Changes as a Patch The vendored miniz carries BBS and PrusaResearch changes that README-Prusa described only in prose, so each upstream bump meant finding them again by hand. Commit the diff between the upstream 3.1.2 release and the files here, and have the README list what it carries and how to re-apply it. * Extract Plug-in Files Through extract_zip_entry_to_file install_plugin still handed miniz a destination converted to the local code page, which 3.x decodes as UTF-8, so a non-ASCII install path was mangled; the open then failed and fell back to mz_zip_reader_extract_to_file_w. That carried function never worked: mz_wfopen passed a narrow mode string to _wfopen_s, the stream was opened in text mode, and the wide path went into the narrow mz_zip_set_file_times. Use the existing helper, which opens an extended-length wide path in binary mode, and drop the _w API from miniz, the patch and the README.
Miniz
Miniz is a lossless, high performance data compression library in a single source file that implements the zlib (RFC 1950) and Deflate (RFC 1951) compressed data format specification standards. It supports the most commonly used functions exported by the zlib library, but is a completely independent implementation so zlib's licensing requirements do not apply. Miniz also contains simple to use functions for writing .PNG format image files and reading/writing/appending .ZIP format archives. Miniz's compression speed has been tuned to be comparable to zlib's, and it also has a specialized real-time compressor function designed to compare well against fastlz/minilzo.
Usage
Releases are available at the releases page 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 during build. Alternatively use as cmake or meson module (or build system of your choice).
Features
- MIT licensed
- A portable, single source and header file library written in plain C. Tested with GCC, clang and Visual Studio.
- Easily tuned and trimmed down by defines
- A drop-in replacement for zlib's most used API's (tested in several open source projects that use zlib, such as libpng and libzip).
- Fills a single threaded performance vs. compression ratio gap between several popular real-time compressors and zlib. For example, at level 1, miniz.c compresses around 5-9% better than minilzo, but is approx. 35% slower. At levels 2-9, miniz.c is designed to compare favorably against zlib's ratio and speed. See the miniz performance comparison page for example timings.
- Not a block based compressor: miniz.c fully supports stream based processing using a coroutine-style implementation. The zlib-style API functions can be called a single byte at a time if that's all you've got.
- Easy to use. The low-level compressor (tdefl) and decompressor (tinfl) have simple state structs which can be saved/restored as needed with simple memcpy's. The low-level codec API's don't use the heap in any way.
- Entire inflater (including optional zlib header parsing and Adler-32 checking) is implemented in a single function as a coroutine, which is separately available in a small (~550 line) source file: miniz_tinfl.c
- A fairly complete (but totally optional) set of .ZIP archive manipulation and extraction API's. The archive functionality is intended to solve common problems encountered in embedded, mobile, or game development situations. (The archive API's are purposely just powerful enough to write an entire archiver given a bit of additional higher-level logic.)
Building miniz - Using vcpkg
You can download and install miniz using the vcpkg dependency manager:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install miniz
The miniz port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
Known Problems
- No support for encrypted archives. Not sure how useful this stuff is in practice.
- Minimal documentation. The assumption is that the user is already familiar with the basic zlib API. I need to write an API wiki - for now I've tried to place key comments before each enum/API, and I've included 6 examples that demonstrate how to use the module's major features.
Special Thanks
Thanks to Alex Evans for the PNG writer function. Also, thanks to Paul Holden and Thorsten Scheuermann for feedback and testing, Matt Pritchard for all his encouragement, and Sean Barrett's various public domain libraries for inspiration (and encouraging me to write miniz.c in C, which was much more enjoyable and less painful than I thought it would be considering I've been programming in C++ for so long).
Thanks to Bruce Dawson for reporting a problem with the level_and_flags archive API parameter (which is fixed in v1.12) and general feedback, and Janez Zemva for indirectly encouraging me into writing more examples.
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. Also see the gzip FAQ. In my opinion, if miniz falls prey to a patent attack then zlib/gzip are likely to be at serious risk too.