1#ifndef MINIZ_EXPORT
2#define MINIZ_EXPORT
3#endif
4/* miniz.c 3.0.0 - public domain deflate/inflate, zlib-subset, ZIP
5 reading/writing/appending, PNG writing See "unlicense" statement at the end
6 of this file. Rich Geldreich <[email protected]>, last updated Oct. 13,
7 2013 Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951:
8 http://www.ietf.org/rfc/rfc1951.txt
9
10 Most API's defined in miniz.c are optional. For example, to disable the
11 archive related functions just define MINIZ_NO_ARCHIVE_APIS, or to get rid of
12 all stdio usage define MINIZ_NO_STDIO (see the list below for more macros).
13
14 * Low-level Deflate/Inflate implementation notes:
15
16 Compression: Use the "tdefl" API's. The compressor supports raw, static,
17 and dynamic blocks, lazy or greedy parsing, match length filtering, RLE-only,
18 and Huffman-only streams. It performs and compresses approximately as well as
19 zlib.
20
21 Decompression: Use the "tinfl" API's. The entire decompressor is
22 implemented as a single function coroutine: see tinfl_decompress(). It
23 supports decompression into a 32KB (or larger power of 2) wrapping buffer, or
24 into a memory block large enough to hold the entire file.
25
26 The low-level tdefl/tinfl API's do not make any use of dynamic memory
27 allocation.
28
29 * zlib-style API notes:
30
31 miniz.c implements a fairly large subset of zlib. There's enough
32 functionality present for it to be a drop-in zlib replacement in many apps:
33 The z_stream struct, optional memory allocation callbacks
34 deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound
35 inflateInit/inflateInit2/inflate/inflateReset/inflateEnd
36 compress, compress2, compressBound, uncompress
37 CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly
38 routines. Supports raw deflate streams or standard zlib streams with adler-32
39 checking.
40
41 Limitations:
42 The callback API's are not implemented yet. No support for gzip headers or
43 zlib static dictionaries. I've tried to closely emulate zlib's various
44 flavors of stream flushing and return status codes, but there are no
45 guarantees that miniz.c pulls this off perfectly.
46
47 * PNG writing: See the tdefl_write_image_to_png_file_in_memory() function,
48 originally written by Alex Evans. Supports 1-4 bytes/pixel images.
49
50 * ZIP archive API notes:
51
52 The ZIP archive API's where designed with simplicity and efficiency in
53 mind, with just enough abstraction to get the job done with minimal fuss.
54 There are simple API's to retrieve file information, read files from existing
55 archives, create new archives, append new files to existing archives, or
56 clone archive data from one archive to another. It supports archives located
57 in memory or the heap, on disk (using stdio.h), or you can specify custom
58 file read/write callbacks.
59
60 - Archive reading: Just call this function to read a single file from a
61 disk archive:
62
63 void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const
64 char *pArchive_name, size_t *pSize, mz_uint zip_flags);
65
66 For more complex cases, use the "mz_zip_reader" functions. Upon opening an
67 archive, the entire central directory is located and read as-is into memory,
68 and subsequent file access only occurs when reading individual files.
69
70 - Archives file scanning: The simple way is to use this function to scan a
71 loaded archive for a specific file:
72
73 int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName,
74 const char *pComment, mz_uint flags);
75
76 The locate operation can optionally check file comments too, which (as one
77 example) can be used to identify multiple versions of the same file in an
78 archive. This function uses a simple linear search through the central
79 directory, so it's not very fast.
80
81 Alternately, you can iterate through all the files in an archive (using
82 mz_zip_reader_get_num_files()) and retrieve detailed info on each file by
83 calling mz_zip_reader_file_stat().
84
85 - Archive creation: Use the "mz_zip_writer" functions. The ZIP writer
86 immediately writes compressed file data to disk and builds an exact image of
87 the central directory in memory. The central directory image is written all
88 at once at the end of the archive file when the archive is finalized.
89
90 The archive writer can optionally align each file's local header and file
91 data to any power of 2 alignment, which can be useful when the archive will
92 be read from optical media. Also, the writer supports placing arbitrary data
93 blobs at the very beginning of ZIP archives. Archives written using either
94 feature are still readable by any ZIP tool.
95
96 - Archive appending: The simple way to add a single file to an archive is
97 to call this function:
98
99 mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename,
100 const char *pArchive_name, const void *pBuf, size_t buf_size, const void
101 *pComment, mz_uint16 comment_size, mz_uint level_and_flags);
102
103 The archive will be created if it doesn't already exist, otherwise it'll be
104 appended to. Note the appending is done in-place and is not an atomic
105 operation, so if something goes wrong during the operation it's possible the
106 archive could be left without a central directory (although the local file
107 headers and file data will be fine, so the archive will be recoverable).
108
109 For more complex archive modification scenarios:
110 1. The safest way is to use a mz_zip_reader to read the existing archive,
111 cloning only those bits you want to preserve into a new archive using using
112 the mz_zip_writer_add_from_zip_reader() function (which compiles the
113 compressed file data as-is). When you're done, delete the old archive and
114 rename the newly written archive, and you're done. This is safe but requires
115 a bunch of temporary disk space or heap memory.
116
117 2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using
118 mz_zip_writer_init_from_reader(), append new files as needed, then finalize
119 the archive which will write an updated central directory to the original
120 archive. (This is basically what mz_zip_add_mem_to_archive_file_in_place()
121 does.) There's a possibility that the archive's central directory could be
122 lost with this method if anything goes wrong, though.
123
124 - ZIP archive support limitations:
125 No spanning support. Extraction functions can only handle unencrypted,
126 stored or deflated files. Requires streams capable of seeking.
127
128 * This is a header file library, like stb_image.c. To get only a header file,
129 either cut and paste the below header, or create miniz.h, #define
130 MINIZ_HEADER_FILE_ONLY, and then include miniz.c from it.
131
132 * Important: For best perf. be sure to customize the below macros for your
133 target platform: #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 #define
134 MINIZ_LITTLE_ENDIAN 1 #define MINIZ_HAS_64BIT_REGISTERS 1
135
136 * On platforms using glibc, Be sure to "#define _LARGEFILE64_SOURCE 1" before
137 including miniz.c to ensure miniz uses the 64-bit variants: fopen64(),
138 stat64(), etc. Otherwise you won't be able to process large files (i.e.
139 32-bit stat() fails for me on files > 0x7FFFFFFF bytes).
140*/
141#pragma once
142
143/* Defines to completely disable specific portions of miniz.c:
144 If all macros here are defined the only functionality remaining will be
145 CRC-32 and adler-32. */
146
147/* Define MINIZ_NO_STDIO to disable all usage and any functions which rely on
148 * stdio for file I/O. */
149/*#define MINIZ_NO_STDIO */
150
151/* If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able
152 * to get the current time, or */
153/* get/set file times, and the C run-time funcs that get/set times won't be
154 * called. */
155/* The current downside is the times written to your archives will be from 1979.
156 */
157/*#define MINIZ_NO_TIME */
158
159/* Define MINIZ_NO_DEFLATE_APIS to disable all compression API's. */
160/*#define MINIZ_NO_DEFLATE_APIS */
161
162/* Define MINIZ_NO_INFLATE_APIS to disable all decompression API's. */
163/*#define MINIZ_NO_INFLATE_APIS */
164
165/* Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. */
166/*#define MINIZ_NO_ARCHIVE_APIS */
167
168/* Define MINIZ_NO_ARCHIVE_WRITING_APIS to disable all writing related ZIP
169 * archive API's. */
170/*#define MINIZ_NO_ARCHIVE_WRITING_APIS */
171
172/* Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression
173 * API's. */
174/*#define MINIZ_NO_ZLIB_APIS */
175
176/* Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent
177 * conflicts against stock zlib. */
178/*#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES */
179
180/* Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc.
181 Note if MINIZ_NO_MALLOC is defined then the user must always provide custom
182 user alloc/free/realloc callbacks to the zlib and archive API's, and a few
183 stand-alone helper API's which don't provide custom user functions (such as
184 tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work.
185 */
186/*#define MINIZ_NO_MALLOC */
187
188#ifdef MINIZ_NO_INFLATE_APIS
189#define MINIZ_NO_ARCHIVE_APIS
190#endif
191
192#ifdef MINIZ_NO_DEFLATE_APIS
193#define MINIZ_NO_ARCHIVE_WRITING_APIS
194#endif
195
196#if defined(__TINYC__) && (defined(__linux) || defined(__linux__))
197/* TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc
198 * on Linux */
199#define MINIZ_NO_TIME
200#endif
201
202#include <stddef.h>
203
204#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS)
205#include <time.h>
206#endif
207
208#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || \
209 defined(__i386) || defined(__i486__) || defined(__i486) || \
210 defined(i386) || defined(__ia64__) || defined(__x86_64__)
211/* MINIZ_X86_OR_X64_CPU is only used to help set the below macros. */
212#define MINIZ_X86_OR_X64_CPU 1
213#else
214#define MINIZ_X86_OR_X64_CPU 0
215#endif
216
217/* Set MINIZ_LITTLE_ENDIAN only if not set */
218#if !defined(MINIZ_LITTLE_ENDIAN)
219#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
220
221#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
222/* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */
223#define MINIZ_LITTLE_ENDIAN 1
224#else
225#define MINIZ_LITTLE_ENDIAN 0
226#endif
227
228#else
229
230#if MINIZ_X86_OR_X64_CPU
231#define MINIZ_LITTLE_ENDIAN 1
232#else
233#define MINIZ_LITTLE_ENDIAN 0
234#endif
235
236#endif
237#endif
238
239/* Using unaligned loads and stores causes errors when using UBSan */
240#if defined(__has_feature)
241#if __has_feature(undefined_behavior_sanitizer)
242#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0
243#endif
244#endif
245
246/* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES only if not set */
247#if !defined(MINIZ_USE_UNALIGNED_LOADS_AND_STORES)
248#if MINIZ_X86_OR_X64_CPU
249/* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient
250 * integer loads and stores from unaligned addresses. */
251#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0
252#define MINIZ_UNALIGNED_USE_MEMCPY
253#else
254#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0
255#endif
256#endif
257
258#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || \
259 defined(_LP64) || defined(__LP64__) || defined(__ia64__) || \
260 defined(__x86_64__)
261/* Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are
262 * reasonably fast (and don't involve compiler generated calls to helper
263 * functions). */
264#define MINIZ_HAS_64BIT_REGISTERS 1
265#else
266#define MINIZ_HAS_64BIT_REGISTERS 0
267#endif
268
269#ifdef __cplusplus
270extern "C" {
271#endif
272
273/* ------------------- zlib-style API Definitions. */
274
275/* For more compatibility with zlib, miniz.c uses unsigned long for some
276 * parameters/struct members. Beware: mz_ulong can be either 32 or 64-bits! */
277typedef unsigned long mz_ulong;
278
279/* mz_free() internally uses the MZ_FREE() macro (which by default calls free()
280 * unless you've modified the MZ_MALLOC macro) to release a block allocated from
281 * the heap. */
282MINIZ_EXPORT void mz_free(void *p);
283
284#define MZ_ADLER32_INIT (1)
285/* mz_adler32() returns the initial adler-32 value to use when called with
286 * ptr==NULL. */
287MINIZ_EXPORT mz_ulong mz_adler32(mz_ulong adler,
288 const unsigned char *ptr,
289 size_t buf_len);
290
291#define MZ_CRC32_INIT (0)
292/* mz_crc32() returns the initial CRC-32 value to use when called with
293 * ptr==NULL. */
294MINIZ_EXPORT mz_ulong mz_crc32(mz_ulong crc,
295 const unsigned char *ptr,
296 size_t buf_len);
297
298/* Compression strategies. */
299enum {
300 MZ_DEFAULT_STRATEGY = 0,
301 MZ_FILTERED = 1,
302 MZ_HUFFMAN_ONLY = 2,
303 MZ_RLE = 3,
304 MZ_FIXED = 4
305};
306
307/* Method */
308#define MZ_DEFLATED 8
309
310/* Heap allocation callbacks.
311Note that mz_alloc_func parameter types purposely differ from zlib's: items/size
312is size_t, not unsigned long. */
313typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size);
314typedef void (*mz_free_func)(void *opaque, void *address);
315typedef void *(*mz_realloc_func)(void *opaque,
316 void *address,
317 size_t items,
318 size_t size);
319
320/* Compression levels: 0-9 are the standard zlib-style levels, 10 is best
321 * possible compression (not zlib compatible, and may be very slow),
322 * MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL. */
323enum {
324 MZ_NO_COMPRESSION = 0,
325 MZ_BEST_SPEED = 1,
326 MZ_BEST_COMPRESSION = 9,
327 MZ_UBER_COMPRESSION = 10,
328 MZ_DEFAULT_LEVEL = 6,
329 MZ_DEFAULT_COMPRESSION = -1
330};
331
332#define MZ_VERSION "11.0.1"
333#define MZ_VERNUM 0xB001
334#define MZ_VER_MAJOR 11
335#define MZ_VER_MINOR 1
336#define MZ_VER_REVISION 0
337#define MZ_VER_SUBREVISION 0
338
339#ifndef MINIZ_NO_ZLIB_APIS
340
341/* Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The
342 * other values are for advanced use (refer to the zlib docs). */
343enum {
344 MZ_NO_FLUSH = 0,
345 MZ_PARTIAL_FLUSH = 1,
346 MZ_SYNC_FLUSH = 2,
347 MZ_FULL_FLUSH = 3,
348 MZ_FINISH = 4,
349 MZ_BLOCK = 5
350};
351
352/* Return status codes. MZ_PARAM_ERROR is non-standard. */
353enum {
354 MZ_OK = 0,
355 MZ_STREAM_END = 1,
356 MZ_NEED_DICT = 2,
357 MZ_ERRNO = -1,
358 MZ_STREAM_ERROR = -2,
359 MZ_DATA_ERROR = -3,
360 MZ_MEM_ERROR = -4,
361 MZ_BUF_ERROR = -5,
362 MZ_VERSION_ERROR = -6,
363 MZ_PARAM_ERROR = -10000
364};
365
366/* Window bits */
367#define MZ_DEFAULT_WINDOW_BITS 15
368
369struct mz_internal_state;
370
371/* Compression/decompression stream struct. */
372typedef struct mz_stream_s {
373 const unsigned char *next_in; /* pointer to next byte to read */
374 unsigned int avail_in; /* number of bytes available at next_in */
375 mz_ulong total_in; /* total number of bytes consumed so far */
376
377 unsigned char *next_out; /* pointer to next byte to write */
378 unsigned int avail_out; /* number of bytes that can be written to next_out */
379 mz_ulong total_out; /* total number of bytes produced so far */
380
381 char *msg; /* error msg (unused) */
382 struct mz_internal_state
383 *state; /* internal state, allocated by zalloc/zfree */
384
385 mz_alloc_func
386 zalloc; /* optional heap allocation function (defaults to malloc) */
387 mz_free_func zfree; /* optional heap free function (defaults to free) */
388 void *opaque; /* heap alloc function user pointer */
389
390 int data_type; /* data_type (unused) */
391 mz_ulong adler; /* adler32 of the source or uncompressed data */
392 mz_ulong reserved; /* not used */
393} mz_stream;
394
395typedef mz_stream *mz_streamp;
396
397/* Returns the version string of miniz.c. */
398MINIZ_EXPORT const char *mz_version(void);
399
400#ifndef MINIZ_NO_DEFLATE_APIS
401
402/* mz_deflateInit() initializes a compressor with default options: */
403/* Parameters: */
404/* pStream must point to an initialized mz_stream struct. */
405/* level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. */
406/* level 1 enables a specially optimized compression function that's been
407 * optimized purely for performance, not ratio. */
408/* (This special func. is currently only enabled when
409 * MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.) */
410/* Return values: */
411/* MZ_OK on success. */
412/* MZ_STREAM_ERROR if the stream is bogus. */
413/* MZ_PARAM_ERROR if the input parameters are bogus. */
414/* MZ_MEM_ERROR on out of memory. */
415MINIZ_EXPORT int mz_deflateInit(mz_streamp pStream, int level);
416
417/* mz_deflateInit2() is like mz_deflate(), except with more control: */
418/* Additional parameters: */
419/* method must be MZ_DEFLATED */
420/* window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with
421 * zlib header/adler-32 footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate/no
422 * header or footer) */
423/* mem_level must be between [1, 9] (it's checked but ignored by miniz.c) */
424MINIZ_EXPORT int mz_deflateInit2(mz_streamp pStream,
425 int level,
426 int method,
427 int window_bits,
428 int mem_level,
429 int strategy);
430
431/* Quickly resets a compressor without having to reallocate anything. Same as
432 * calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2(). */
433MINIZ_EXPORT int mz_deflateReset(mz_streamp pStream);
434
435/* mz_deflate() compresses the input to output, consuming as much of the input
436 * and producing as much output as possible. */
437/* Parameters: */
438/* pStream is the stream to read from and write to. You must initialize/update
439 * the next_in, avail_in, next_out, and avail_out members. */
440/* flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or
441 * MZ_FINISH. */
442/* Return values: */
443/* MZ_OK on success (when flushing, or if more input is needed but not
444 * available, and/or there's more output to be written but the output buffer is
445 * full). */
446/* MZ_STREAM_END if all input has been consumed and all output bytes have been
447 * written. Don't call mz_deflate() on the stream anymore. */
448/* MZ_STREAM_ERROR if the stream is bogus. */
449/* MZ_PARAM_ERROR if one of the parameters is invalid. */
450/* MZ_BUF_ERROR if no forward progress is possible because the input and/or
451 * output buffers are empty. (Fill up the input buffer or free up some output
452 * space and try again.) */
453MINIZ_EXPORT int mz_deflate(mz_streamp pStream, int flush);
454
455/* mz_deflateEnd() deinitializes a compressor: */
456/* Return values: */
457/* MZ_OK on success. */
458/* MZ_STREAM_ERROR if the stream is bogus. */
459MINIZ_EXPORT int mz_deflateEnd(mz_streamp pStream);
460
461/* mz_deflateBound() returns a (very) conservative upper bound on the amount of
462 * data that could be generated by deflate(), assuming flush is set to only
463 * MZ_NO_FLUSH or MZ_FINISH. */
464MINIZ_EXPORT mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len);
465
466/* Single-call compression functions mz_compress() and mz_compress2(): */
467/* Returns MZ_OK on success, or one of the error codes from mz_deflate() on
468 * failure. */
469MINIZ_EXPORT int mz_compress(unsigned char *pDest,
470 mz_ulong *pDest_len,
471 const unsigned char *pSource,
472 mz_ulong source_len);
473MINIZ_EXPORT int mz_compress2(unsigned char *pDest,
474 mz_ulong *pDest_len,
475 const unsigned char *pSource,
476 mz_ulong source_len,
477 int level);
478
479/* mz_compressBound() returns a (very) conservative upper bound on the amount of
480 * data that could be generated by calling mz_compress(). */
481MINIZ_EXPORT mz_ulong mz_compressBound(mz_ulong source_len);
482
483#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/
484
485#ifndef MINIZ_NO_INFLATE_APIS
486
487/* Initializes a decompressor. */
488MINIZ_EXPORT int mz_inflateInit(mz_streamp pStream);
489
490/* mz_inflateInit2() is like mz_inflateInit() with an additional option that
491 * controls the window size and whether or not the stream has been wrapped with
492 * a zlib header/footer: */
493/* window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or
494 * -MZ_DEFAULT_WINDOW_BITS (raw deflate). */
495MINIZ_EXPORT int mz_inflateInit2(mz_streamp pStream, int window_bits);
496
497/* Quickly resets a compressor without having to reallocate anything. Same as
498 * calling mz_inflateEnd() followed by mz_inflateInit()/mz_inflateInit2(). */
499MINIZ_EXPORT int mz_inflateReset(mz_streamp pStream);
500
501/* Decompresses the input stream to the output, consuming only as much of the
502 * input as needed, and writing as much to the output as possible. */
503/* Parameters: */
504/* pStream is the stream to read from and write to. You must initialize/update
505 * the next_in, avail_in, next_out, and avail_out members. */
506/* flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. */
507/* On the first call, if flush is MZ_FINISH it's assumed the input and output
508 * buffers are both sized large enough to decompress the entire stream in a
509 * single call (this is slightly faster). */
510/* MZ_FINISH implies that there are no more source bytes available beside
511 * what's already in the input buffer, and that the output buffer is large
512 * enough to hold the rest of the decompressed data. */
513/* Return values: */
514/* MZ_OK on success. Either more input is needed but not available, and/or
515 * there's more output to be written but the output buffer is full. */
516/* MZ_STREAM_END if all needed input has been consumed and all output bytes
517 * have been written. For zlib streams, the adler-32 of the decompressed data
518 * has also been verified. */
519/* MZ_STREAM_ERROR if the stream is bogus. */
520/* MZ_DATA_ERROR if the deflate stream is invalid. */
521/* MZ_PARAM_ERROR if one of the parameters is invalid. */
522/* MZ_BUF_ERROR if no forward progress is possible because the input buffer is
523 * empty but the inflater needs more input to continue, or if the output buffer
524 * is not large enough. Call mz_inflate() again */
525/* with more input data, or with more room in the output buffer (except when
526 * using single call decompression, described above). */
527MINIZ_EXPORT int mz_inflate(mz_streamp pStream, int flush);
528
529/* Deinitializes a decompressor. */
530MINIZ_EXPORT int mz_inflateEnd(mz_streamp pStream);
531
532/* Single-call decompression. */
533/* Returns MZ_OK on success, or one of the error codes from mz_inflate() on
534 * failure. */
535MINIZ_EXPORT int mz_uncompress(unsigned char *pDest,
536 mz_ulong *pDest_len,
537 const unsigned char *pSource,
538 mz_ulong source_len);
539MINIZ_EXPORT int mz_uncompress2(unsigned char *pDest,
540 mz_ulong *pDest_len,
541 const unsigned char *pSource,
542 mz_ulong *pSource_len);
543#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/
544
545/* Returns a string description of the specified error code, or NULL if the
546 * error code is invalid. */
547MINIZ_EXPORT const char *mz_error(int err);
548
549/* Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used
550 * as a drop-in replacement for the subset of zlib that miniz.c supports. */
551/* Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you
552 * use zlib in the same project. */
553#ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
554typedef unsigned char Byte;
555typedef unsigned int uInt;
556typedef mz_ulong uLong;
557typedef Byte Bytef;
558typedef uInt uIntf;
559typedef char charf;
560typedef int intf;
561typedef void *voidpf;
562typedef uLong uLongf;
563typedef void *voidp;
564typedef void *const voidpc;
565#define Z_NULL 0
566#define Z_NO_FLUSH MZ_NO_FLUSH
567#define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH
568#define Z_SYNC_FLUSH MZ_SYNC_FLUSH
569#define Z_FULL_FLUSH MZ_FULL_FLUSH
570#define Z_FINISH MZ_FINISH
571#define Z_BLOCK MZ_BLOCK
572#define Z_OK MZ_OK
573#define Z_STREAM_END MZ_STREAM_END
574#define Z_NEED_DICT MZ_NEED_DICT
575#define Z_ERRNO MZ_ERRNO
576#define Z_STREAM_ERROR MZ_STREAM_ERROR
577#define Z_DATA_ERROR MZ_DATA_ERROR
578#define Z_MEM_ERROR MZ_MEM_ERROR
579#define Z_BUF_ERROR MZ_BUF_ERROR
580#define Z_VERSION_ERROR MZ_VERSION_ERROR
581#define Z_PARAM_ERROR MZ_PARAM_ERROR
582#define Z_NO_COMPRESSION MZ_NO_COMPRESSION
583#define Z_BEST_SPEED MZ_BEST_SPEED
584#define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION
585#define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION
586#define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY
587#define Z_FILTERED MZ_FILTERED
588#define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY
589#define Z_RLE MZ_RLE
590#define Z_FIXED MZ_FIXED
591#define Z_DEFLATED MZ_DEFLATED
592#define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS
593#define alloc_func mz_alloc_func
594#define free_func mz_free_func
595#define internal_state mz_internal_state
596#define z_stream mz_stream
597
598#ifndef MINIZ_NO_DEFLATE_APIS
599#define deflateInit mz_deflateInit
600#define deflateInit2 mz_deflateInit2
601#define deflateReset mz_deflateReset
602#define deflate mz_deflate
603#define deflateEnd mz_deflateEnd
604#define deflateBound mz_deflateBound
605#define compress mz_compress
606#define compress2 mz_compress2
607#define compressBound mz_compressBound
608#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/
609
610#ifndef MINIZ_NO_INFLATE_APIS
611#define inflateInit mz_inflateInit
612#define inflateInit2 mz_inflateInit2
613#define inflateReset mz_inflateReset
614#define inflate mz_inflate
615#define inflateEnd mz_inflateEnd
616#define uncompress mz_uncompress
617#define uncompress2 mz_uncompress2
618#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/
619
620#define crc32 mz_crc32
621#define adler32 mz_adler32
622#define MAX_WBITS 15
623#define MAX_MEM_LEVEL 9
624#define zError mz_error
625#define ZLIB_VERSION MZ_VERSION
626#define ZLIB_VERNUM MZ_VERNUM
627#define ZLIB_VER_MAJOR MZ_VER_MAJOR
628#define ZLIB_VER_MINOR MZ_VER_MINOR
629#define ZLIB_VER_REVISION MZ_VER_REVISION
630#define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION
631#define zlibVersion mz_version
632#define zlib_version mz_version()
633#endif /* #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES */
634
635#endif /* MINIZ_NO_ZLIB_APIS */
636
637#ifdef __cplusplus
638}
639#endif
640
641#pragma once
642#include <assert.h>
643#include <stdint.h>
644#include <stdlib.h>
645#include <string.h>
646
647/* ------------------- Types and macros */
648typedef unsigned char mz_uint8;
649typedef signed short mz_int16;
650typedef unsigned short mz_uint16;
651typedef unsigned int mz_uint32;
652typedef unsigned int mz_uint;
653typedef int64_t mz_int64;
654typedef uint64_t mz_uint64;
655typedef int mz_bool;
656
657#define MZ_FALSE (0)
658#define MZ_TRUE (1)
659
660/* Works around MSVC's spammy "warning C4127: conditional expression is
661 * constant" message. */
662#ifdef _MSC_VER
663#define MZ_MACRO_END while (0, 0)
664#else
665#define MZ_MACRO_END while (0)
666#endif
667
668#ifdef MINIZ_NO_STDIO
669#define MZ_FILE void *
670#else
671#include <stdio.h>
672#define MZ_FILE FILE
673#endif /* #ifdef MINIZ_NO_STDIO */
674
675#ifdef MINIZ_NO_TIME
676typedef struct mz_dummy_time_t_tag {
677 mz_uint32 m_dummy1;
678 mz_uint32 m_dummy2;
679} mz_dummy_time_t;
680#define MZ_TIME_T mz_dummy_time_t
681#else
682#define MZ_TIME_T time_t
683#endif
684
685#define MZ_ASSERT(x) assert(x)
686
687#ifdef MINIZ_NO_MALLOC
688#define MZ_MALLOC(x) NULL
689#define MZ_FREE(x) (void)x, ((void)0)
690#define MZ_REALLOC(p, x) NULL
691#else
692#define MZ_MALLOC(x) malloc(x)
693#define MZ_FREE(x) free(x)
694#define MZ_REALLOC(p, x) realloc(p, x)
695#endif
696
697#define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b))
698#define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b))
699#define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
700#define MZ_CLEAR_ARR(obj) memset((obj), 0, sizeof(obj))
701#define MZ_CLEAR_PTR(obj) memset((obj), 0, sizeof(*obj))
702
703#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
704#define MZ_READ_LE16(p) *((const mz_uint16 *)(p))
705#define MZ_READ_LE32(p) *((const mz_uint32 *)(p))
706#else
707#define MZ_READ_LE16(p) \
708 ((mz_uint32)(((const mz_uint8 *)(p))[0]) | \
709 ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U))
710#define MZ_READ_LE32(p) \
711 ((mz_uint32)(((const mz_uint8 *)(p))[0]) | \
712 ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | \
713 ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | \
714 ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U))
715#endif
716
717#define MZ_READ_LE64(p) \
718 (((mz_uint64)MZ_READ_LE32(p)) | \
719 (((mz_uint64)MZ_READ_LE32((const mz_uint8 *)(p) + sizeof(mz_uint32))) \
720 << 32U))
721
722#ifdef _MSC_VER
723#define MZ_FORCEINLINE __forceinline
724#elif defined(__GNUC__)
725#define MZ_FORCEINLINE __inline__ __attribute__((__always_inline__))
726#else
727#define MZ_FORCEINLINE inline
728#endif
729
730#ifdef __cplusplus
731extern "C" {
732#endif
733
734extern MINIZ_EXPORT void *miniz_def_alloc_func(void *opaque,
735 size_t items,
736 size_t size);
737extern MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address);
738extern MINIZ_EXPORT void *miniz_def_realloc_func(void *opaque,
739 void *address,
740 size_t items,
741 size_t size);
742
743#define MZ_UINT16_MAX (0xFFFFU)
744#define MZ_UINT32_MAX (0xFFFFFFFFU)
745
746#ifdef __cplusplus
747}
748#endif
749#pragma once
750
751#ifndef MINIZ_NO_DEFLATE_APIS
752
753#ifdef __cplusplus
754extern "C" {
755#endif
756/* ------------------- Low-level Compression API Definitions */
757
758/* Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly
759 * slower, and raw/dynamic blocks will be output more frequently). */
760#define TDEFL_LESS_MEMORY 0
761
762/* tdefl_init() compression flags logically OR'd together (low 12 bits contain
763 * the max. number of probes per dictionary search): */
764/* TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes
765 * per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap
766 * compression), 4095=Huffman+LZ (slowest/best compression). */
767enum {
768 TDEFL_HUFFMAN_ONLY = 0,
769 TDEFL_DEFAULT_MAX_PROBES = 128,
770 TDEFL_MAX_PROBES_MASK = 0xFFF
771};
772
773/* TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before
774 * the deflate data, and the Adler-32 of the source data at the end. Otherwise,
775 * you'll get raw deflate data. */
776/* TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even
777 * when not writing zlib headers). */
778/* TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more
779 * efficient lazy parsing. */
780/* TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's
781 * initialization time to the minimum, but the output may vary from run to run
782 * given the same input (depending on the contents of memory). */
783/* TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1)
784 */
785/* TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. */
786/* TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. */
787/* TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. */
788/* The low 12 bits are reserved to control the max # of hash probes per
789 * dictionary lookup (see TDEFL_MAX_PROBES_MASK). */
790enum {
791 TDEFL_WRITE_ZLIB_HEADER = 0x01000,
792 TDEFL_COMPUTE_ADLER32 = 0x02000,
793 TDEFL_GREEDY_PARSING_FLAG = 0x04000,
794 TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000,
795 TDEFL_RLE_MATCHES = 0x10000,
796 TDEFL_FILTER_MATCHES = 0x20000,
797 TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000,
798 TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
799};
800
801/* High level compression functions: */
802/* tdefl_compress_mem_to_heap() compresses a block in memory to a heap block
803 * allocated via malloc(). */
804/* On entry: */
805/* pSrc_buf, src_buf_len: Pointer and size of source block to compress. */
806/* flags: The max match finder probes (default is 128) logically OR'd against
807 * the above flags. Higher probes are slower but improve compression. */
808/* On return: */
809/* Function returns a pointer to the compressed data, or NULL on failure. */
810/* *pOut_len will be set to the compressed data's size, which could be larger
811 * than src_buf_len on uncompressible data. */
812/* The caller must free() the returned block when it's no longer needed. */
813MINIZ_EXPORT void *tdefl_compress_mem_to_heap(const void *pSrc_buf,
814 size_t src_buf_len,
815 size_t *pOut_len,
816 int flags);
817
818/* tdefl_compress_mem_to_mem() compresses a block in memory to another block in
819 * memory. */
820/* Returns 0 on failure. */
821MINIZ_EXPORT size_t tdefl_compress_mem_to_mem(void *pOut_buf,
822 size_t out_buf_len,
823 const void *pSrc_buf,
824 size_t src_buf_len,
825 int flags);
826
827/* Compresses an image to a compressed PNG file in memory. */
828/* On entry: */
829/* pImage, w, h, and num_chans describe the image to compress. num_chans may be
830 * 1, 2, 3, or 4. */
831/* The image pitch in bytes per scanline will be w*num_chans. The leftmost
832 * pixel on the top scanline is stored first in memory. */
833/* level may range from [0,10], use MZ_NO_COMPRESSION, MZ_BEST_SPEED,
834 * MZ_BEST_COMPRESSION, etc. or a decent default is MZ_DEFAULT_LEVEL */
835/* If flip is true, the image will be flipped on the Y axis (useful for OpenGL
836 * apps). */
837/* On return: */
838/* Function returns a pointer to the compressed data, or NULL on failure. */
839/* *pLen_out will be set to the size of the PNG image file. */
840/* The caller must mz_free() the returned heap block (which will typically be
841 * larger than *pLen_out) when it's no longer needed. */
842MINIZ_EXPORT void *tdefl_write_image_to_png_file_in_memory_ex(
843 const void *pImage,
844 int w,
845 int h,
846 int num_chans,
847 size_t *pLen_out,
848 mz_uint level,
849 mz_bool flip);
850MINIZ_EXPORT void *tdefl_write_image_to_png_file_in_memory(const void *pImage,
851 int w,
852 int h,
853 int num_chans,
854 size_t *pLen_out);
855
856/* Output stream interface. The compressor uses this interface to write
857 * compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time. */
858typedef mz_bool (*tdefl_put_buf_func_ptr)(const void *pBuf,
859 int len,
860 void *pUser);
861
862/* tdefl_compress_mem_to_output() compresses a block to an output stream. The
863 * above helpers use this function internally. */
864MINIZ_EXPORT mz_bool
865tdefl_compress_mem_to_output(const void *pBuf,
866 size_t buf_len,
867 tdefl_put_buf_func_ptr pPut_buf_func,
868 void *pPut_buf_user,
869 int flags);
870
871enum {
872 TDEFL_MAX_HUFF_TABLES = 3,
873 TDEFL_MAX_HUFF_SYMBOLS_0 = 288,
874 TDEFL_MAX_HUFF_SYMBOLS_1 = 32,
875 TDEFL_MAX_HUFF_SYMBOLS_2 = 19,
876 TDEFL_LZ_DICT_SIZE = 32768,
877 TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1,
878 TDEFL_MIN_MATCH_LEN = 3,
879 TDEFL_MAX_MATCH_LEN = 258
880};
881
882/* TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed
883 * output block (using static/fixed Huffman codes). */
884#if TDEFL_LESS_MEMORY
885enum {
886 TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024,
887 TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10,
888 TDEFL_MAX_HUFF_SYMBOLS = 288,
889 TDEFL_LZ_HASH_BITS = 12,
890 TDEFL_LEVEL1_HASH_SIZE_MASK = 4095,
891 TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3,
892 TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS
893};
894#else
895enum {
896 TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024,
897 TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10,
898 TDEFL_MAX_HUFF_SYMBOLS = 288,
899 TDEFL_LZ_HASH_BITS = 15,
900 TDEFL_LEVEL1_HASH_SIZE_MASK = 4095,
901 TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3,
902 TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS
903};
904#endif
905
906/* The low-level tdefl functions below may be used directly if the above helper
907 * functions aren't flexible enough. The low-level functions don't make any heap
908 * allocations, unlike the above helper functions. */
909typedef enum {
910 TDEFL_STATUS_BAD_PARAM = -2,
911 TDEFL_STATUS_PUT_BUF_FAILED = -1,
912 TDEFL_STATUS_OKAY = 0,
913 TDEFL_STATUS_DONE = 1
914} tdefl_status;
915
916/* Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums */
917typedef enum {
918 TDEFL_NO_FLUSH = 0,
919 TDEFL_SYNC_FLUSH = 2,
920 TDEFL_FULL_FLUSH = 3,
921 TDEFL_FINISH = 4
922} tdefl_flush;
923
924/* tdefl's compression state structure. */
925typedef struct {
926 tdefl_put_buf_func_ptr m_pPut_buf_func;
927 void *m_pPut_buf_user;
928 mz_uint m_flags, m_max_probes[2];
929 int m_greedy_parsing;
930 mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
931 mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
932 mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in,
933 m_bit_buffer;
934 mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit,
935 m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index,
936 m_wants_to_finish;
937 tdefl_status m_prev_return_status;
938 const void *m_pIn_buf;
939 void *m_pOut_buf;
940 size_t *m_pIn_buf_size, *m_pOut_buf_size;
941 tdefl_flush m_flush;
942 const mz_uint8 *m_pSrc;
943 size_t m_src_buf_left, m_out_buf_ofs;
944 mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
945 mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
946 mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
947 mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
948 mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
949 mz_uint16 m_next[TDEFL_LZ_DICT_SIZE];
950 mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
951 mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
952} tdefl_compressor;
953
954/* Initializes the compressor. */
955/* There is no corresponding deinit() function because the tdefl API's do not
956 * dynamically allocate memory. */
957/* pBut_buf_func: If NULL, output data will be supplied to the specified
958 * callback. In this case, the user should call the tdefl_compress_buffer() API
959 * for compression. */
960/* If pBut_buf_func is NULL the user should always call the tdefl_compress()
961 * API. */
962/* flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER,
963 * etc.) */
964MINIZ_EXPORT tdefl_status tdefl_init(tdefl_compressor *d,
965 tdefl_put_buf_func_ptr pPut_buf_func,
966 void *pPut_buf_user,
967 int flags);
968
969/* Compresses a block of data, consuming as much of the specified input buffer
970 * as possible, and writing as much compressed data to the specified output
971 * buffer as possible. */
972MINIZ_EXPORT tdefl_status tdefl_compress(tdefl_compressor *d,
973 const void *pIn_buf,
974 size_t *pIn_buf_size,
975 void *pOut_buf,
976 size_t *pOut_buf_size,
977 tdefl_flush flush);
978
979/* tdefl_compress_buffer() is only usable when the tdefl_init() is called with a
980 * non-NULL tdefl_put_buf_func_ptr. */
981/* tdefl_compress_buffer() always consumes the entire input buffer. */
982MINIZ_EXPORT tdefl_status tdefl_compress_buffer(tdefl_compressor *d,
983 const void *pIn_buf,
984 size_t in_buf_size,
985 tdefl_flush flush);
986
987MINIZ_EXPORT tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d);
988MINIZ_EXPORT mz_uint32 tdefl_get_adler32(tdefl_compressor *d);
989
990/* Create tdefl_compress() flags given zlib-style compression parameters. */
991/* level may range from [0,10] (where 10 is absolute max compression, but may be
992 * much slower on some files) */
993/* window_bits may be -15 (raw deflate) or 15 (zlib) */
994/* strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY,
995 * MZ_RLE, or MZ_FIXED */
996MINIZ_EXPORT mz_uint tdefl_create_comp_flags_from_zip_params(int level,
997 int window_bits,
998 int strategy);
999
1000#ifndef MINIZ_NO_MALLOC
1001/* Allocate the tdefl_compressor structure in C so that */
1002/* non-C language bindings to tdefl_ API don't need to worry about */
1003/* structure size and allocation mechanism. */
1004MINIZ_EXPORT tdefl_compressor *tdefl_compressor_alloc(void);
1005MINIZ_EXPORT void tdefl_compressor_free(tdefl_compressor *pComp);
1006#endif
1007
1008#ifdef __cplusplus
1009}
1010#endif
1011
1012#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/
1013#pragma once
1014
1015/* ------------------- Low-level Decompression API Definitions */
1016
1017#ifndef MINIZ_NO_INFLATE_APIS
1018
1019#ifdef __cplusplus
1020extern "C" {
1021#endif
1022/* Decompression flags used by tinfl_decompress(). */
1023/* TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and
1024 * ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the
1025 * input is a raw deflate stream. */
1026/* TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available
1027 * beyond the end of the supplied input buffer. If clear, the input buffer
1028 * contains all remaining input. */
1029/* TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large
1030 * enough to hold the entire decompressed stream. If clear, the output buffer is
1031 * at least the size of the dictionary (typically 32KB). */
1032/* TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the
1033 * decompressed bytes. */
1034enum {
1035 TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
1036 TINFL_FLAG_HAS_MORE_INPUT = 2,
1037 TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
1038 TINFL_FLAG_COMPUTE_ADLER32 = 8
1039};
1040
1041/* High level decompression functions: */
1042/* tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block
1043 * allocated via malloc(). */
1044/* On entry: */
1045/* pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data
1046 * to decompress. */
1047/* On return: */
1048/* Function returns a pointer to the decompressed data, or NULL on failure. */
1049/* *pOut_len will be set to the decompressed data's size, which could be larger
1050 * than src_buf_len on uncompressible data. */
1051/* The caller must call mz_free() on the returned block when it's no longer
1052 * needed. */
1053MINIZ_EXPORT void *tinfl_decompress_mem_to_heap(const void *pSrc_buf,
1054 size_t src_buf_len,
1055 size_t *pOut_len,
1056 int flags);
1057
1058/* tinfl_decompress_mem_to_mem() decompresses a block in memory to another block
1059 * in memory. */
1060/* Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes
1061 * written on success. */
1062#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
1063MINIZ_EXPORT size_t tinfl_decompress_mem_to_mem(void *pOut_buf,
1064 size_t out_buf_len,
1065 const void *pSrc_buf,
1066 size_t src_buf_len,
1067 int flags);
1068
1069/* tinfl_decompress_mem_to_callback() decompresses a block in memory to an
1070 * internal 32KB buffer, and a user provided callback function will be called to
1071 * flush the buffer. */
1072/* Returns 1 on success or 0 on failure. */
1073typedef int (*tinfl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser);
1074MINIZ_EXPORT int tinfl_decompress_mem_to_callback(
1075 const void *pIn_buf,
1076 size_t *pIn_buf_size,
1077 tinfl_put_buf_func_ptr pPut_buf_func,
1078 void *pPut_buf_user,
1079 int flags);
1080
1081struct tinfl_decompressor_tag;
1082typedef struct tinfl_decompressor_tag tinfl_decompressor;
1083
1084#ifndef MINIZ_NO_MALLOC
1085/* Allocate the tinfl_decompressor structure in C so that */
1086/* non-C language bindings to tinfl_ API don't need to worry about */
1087/* structure size and allocation mechanism. */
1088MINIZ_EXPORT tinfl_decompressor *tinfl_decompressor_alloc(void);
1089MINIZ_EXPORT void tinfl_decompressor_free(tinfl_decompressor *pDecomp);
1090#endif
1091
1092/* Max size of LZ dictionary. */
1093#define TINFL_LZ_DICT_SIZE 32768
1094
1095/* Return status. */
1096typedef enum {
1097 /* This flags indicates the inflator needs 1 or more input bytes to make
1098 forward progress, but the caller is indicating that no more are available.
1099 The compressed data */
1100 /* is probably corrupted. If you call the inflator again with more bytes it'll
1101 try to continue processing the input but this is a BAD sign (either the
1102 data is corrupted or you called it incorrectly). */
1103 /* If you call it again with no input you'll just get
1104 TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS again. */
1105 TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4,
1106
1107 /* This flag indicates that one or more of the input parameters was obviously
1108 bogus. (You can try calling it again, but if you get this error the calling
1109 code is wrong.) */
1110 TINFL_STATUS_BAD_PARAM = -3,
1111
1112 /* This flags indicate the inflator is finished but the adler32 check of the
1113 uncompressed data didn't match. If you call it again it'll return
1114 TINFL_STATUS_DONE. */
1115 TINFL_STATUS_ADLER32_MISMATCH = -2,
1116
1117 /* This flags indicate the inflator has somehow failed (bad code, corrupted
1118 input, etc.). If you call it again without resetting via tinfl_init() it
1119 it'll just keep on returning the same status failure code. */
1120 TINFL_STATUS_FAILED = -1,
1121
1122 /* Any status code less than TINFL_STATUS_DONE must indicate a failure. */
1123
1124 /* This flag indicates the inflator has returned every byte of uncompressed
1125 data that it can, has consumed every byte that it needed, has successfully
1126 reached the end of the deflate stream, and */
1127 /* if zlib headers and adler32 checking enabled that it has successfully
1128 checked the uncompressed data's adler32. If you call it again you'll just
1129 get TINFL_STATUS_DONE over and over again. */
1130 TINFL_STATUS_DONE = 0,
1131
1132 /* This flag indicates the inflator MUST have more input data (even 1 byte)
1133 before it can make any more forward progress, or you need to clear the
1134 TINFL_FLAG_HAS_MORE_INPUT */
1135 /* flag on the next call if you don't have any more source data. If the source
1136 data was somehow corrupted it's also possible (but unlikely) for the
1137 inflator to keep on demanding input to */
1138 /* proceed, so be sure to properly set the TINFL_FLAG_HAS_MORE_INPUT flag. */
1139 TINFL_STATUS_NEEDS_MORE_INPUT = 1,
1140
1141 /* This flag indicates the inflator definitely has 1 or more bytes of
1142 uncompressed data available, but it cannot write this data into the output
1143 buffer. */
1144 /* Note if the source compressed data was corrupted it's possible for the
1145 inflator to return a lot of uncompressed data to the caller. I've been
1146 assuming you know how much uncompressed data to expect */
1147 /* (either exact or worst case) and will stop calling the inflator and fail
1148 after receiving too much. In pure streaming scenarios where you have no
1149 idea how many bytes to expect this may not be possible */
1150 /* so I may need to add some code to address this. */
1151 TINFL_STATUS_HAS_MORE_OUTPUT = 2
1152} tinfl_status;
1153
1154/* Initializes the decompressor to its initial state. */
1155#define tinfl_init(r) \
1156 do { \
1157 (r)->m_state = 0; \
1158 } \
1159 MZ_MACRO_END
1160#define tinfl_get_adler32(r) (r)->m_check_adler32
1161
1162/* Main low-level decompressor coroutine function. This is the only function
1163 * actually needed for decompression. All the other functions are just
1164 * high-level helpers for improved usability. */
1165/* This is a universal API, i.e. it can be used as a building block to build any
1166 * desired higher level decompression API. In the limit case, it can be called
1167 * once per every byte input or output. */
1168MINIZ_EXPORT tinfl_status tinfl_decompress(tinfl_decompressor *r,
1169 const mz_uint8 *pIn_buf_next,
1170 size_t *pIn_buf_size,
1171 mz_uint8 *pOut_buf_start,
1172 mz_uint8 *pOut_buf_next,
1173 size_t *pOut_buf_size,
1174 const mz_uint32 decomp_flags);
1175
1176/* Internal/private bits follow. */
1177enum {
1178 TINFL_MAX_HUFF_TABLES = 3,
1179 TINFL_MAX_HUFF_SYMBOLS_0 = 288,
1180 TINFL_MAX_HUFF_SYMBOLS_1 = 32,
1181 TINFL_MAX_HUFF_SYMBOLS_2 = 19,
1182 TINFL_FAST_LOOKUP_BITS = 10,
1183 TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
1184};
1185
1186#if MINIZ_HAS_64BIT_REGISTERS
1187#define TINFL_USE_64BIT_BITBUF 1
1188#else
1189#define TINFL_USE_64BIT_BITBUF 0
1190#endif
1191
1192#if TINFL_USE_64BIT_BITBUF
1193typedef mz_uint64 tinfl_bit_buf_t;
1194#define TINFL_BITBUF_SIZE (64)
1195#else
1196typedef mz_uint32 tinfl_bit_buf_t;
1197#define TINFL_BITBUF_SIZE (32)
1198#endif
1199
1200struct tinfl_decompressor_tag {
1201 mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type,
1202 m_check_adler32, m_dist, m_counter, m_num_extra,
1203 m_table_sizes[TINFL_MAX_HUFF_TABLES];
1204 tinfl_bit_buf_t m_bit_buf;
1205 size_t m_dist_from_out_buf_start;
1206 mz_int16 m_look_up[TINFL_MAX_HUFF_TABLES][TINFL_FAST_LOOKUP_SIZE];
1207 mz_int16 m_tree_0[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
1208 mz_int16 m_tree_1[TINFL_MAX_HUFF_SYMBOLS_1 * 2];
1209 mz_int16 m_tree_2[TINFL_MAX_HUFF_SYMBOLS_2 * 2];
1210 mz_uint8 m_code_size_0[TINFL_MAX_HUFF_SYMBOLS_0];
1211 mz_uint8 m_code_size_1[TINFL_MAX_HUFF_SYMBOLS_1];
1212 mz_uint8 m_code_size_2[TINFL_MAX_HUFF_SYMBOLS_2];
1213 mz_uint8 m_raw_header[4],
1214 m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
1215};
1216
1217#ifdef __cplusplus
1218}
1219#endif
1220
1221#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/
1222
1223#pragma once
1224
1225/* ------------------- ZIP archive reading/writing */
1226
1227#ifndef MINIZ_NO_ARCHIVE_APIS
1228
1229#ifdef __cplusplus
1230extern "C" {
1231#endif
1232
1233enum {
1234 /* Note: These enums can be reduced as needed to save memory or stack space -
1235 they are pretty conservative. */
1236 MZ_ZIP_MAX_IO_BUF_SIZE = 64 * 1024,
1237 MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 512,
1238 MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 512
1239};
1240
1241typedef struct {
1242 /* Central directory file index. */
1243 mz_uint32 m_file_index;
1244
1245 /* Byte offset of this entry in the archive's central directory. Note we
1246 * currently only support up to UINT_MAX or less bytes in the central dir. */
1247 mz_uint64 m_central_dir_ofs;
1248
1249 /* These fields are copied directly from the zip's central dir. */
1250 mz_uint16 m_version_made_by;
1251 mz_uint16 m_version_needed;
1252 mz_uint16 m_bit_flag;
1253 mz_uint16 m_method;
1254
1255 /* CRC-32 of uncompressed data. */
1256 mz_uint32 m_crc32;
1257
1258 /* File's compressed size. */
1259 mz_uint64 m_comp_size;
1260
1261 /* File's uncompressed size. Note, I've seen some old archives where directory
1262 * entries had 512 bytes for their uncompressed sizes, but when you try to
1263 * unpack them you actually get 0 bytes. */
1264 mz_uint64 m_uncomp_size;
1265
1266 /* Zip internal and external file attributes. */
1267 mz_uint16 m_internal_attr;
1268 mz_uint32 m_external_attr;
1269
1270 /* Entry's local header file offset in bytes. */
1271 mz_uint64 m_local_header_ofs;
1272
1273 /* Size of comment in bytes. */
1274 mz_uint32 m_comment_size;
1275
1276 /* MZ_TRUE if the entry appears to be a directory. */
1277 mz_bool m_is_directory;
1278
1279 /* MZ_TRUE if the entry uses encryption/strong encryption (which miniz_zip
1280 * doesn't support) */
1281 mz_bool m_is_encrypted;
1282
1283 /* MZ_TRUE if the file is not encrypted, a patch file, and if it uses a
1284 * compression method we support. */
1285 mz_bool m_is_supported;
1286
1287 /* Filename. If string ends in '/' it's a subdirectory entry. */
1288 /* Guaranteed to be zero terminated, may be truncated to fit. */
1289 char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
1290
1291 /* Comment field. */
1292 /* Guaranteed to be zero terminated, may be truncated to fit. */
1293 char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
1294
1295#ifdef MINIZ_NO_TIME
1296 MZ_TIME_T m_padding;
1297#else
1298 MZ_TIME_T m_time;
1299#endif
1300} mz_zip_archive_file_stat;
1301
1302typedef size_t (*mz_file_read_func)(void *pOpaque,
1303 mz_uint64 file_ofs,
1304 void *pBuf,
1305 size_t n);
1306typedef size_t (*mz_file_write_func)(void *pOpaque,
1307 mz_uint64 file_ofs,
1308 const void *pBuf,
1309 size_t n);
1310typedef mz_bool (*mz_file_needs_keepalive)(void *pOpaque);
1311
1312struct mz_zip_internal_state_tag;
1313typedef struct mz_zip_internal_state_tag mz_zip_internal_state;
1314
1315typedef enum {
1316 MZ_ZIP_MODE_INVALID = 0,
1317 MZ_ZIP_MODE_READING = 1,
1318 MZ_ZIP_MODE_WRITING = 2,
1319 MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3
1320} mz_zip_mode;
1321
1322typedef enum {
1323 MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100,
1324 MZ_ZIP_FLAG_IGNORE_PATH = 0x0200,
1325 MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400,
1326 MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800,
1327 MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG =
1328 0x1000, /* if enabled, mz_zip_reader_locate_file() will be called on each
1329 file as its validated to ensure the func finds the file in the
1330 central dir (intended for testing) */
1331 MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY =
1332 0x2000, /* validate the local headers, but don't decompress the entire
1333 file and check the crc32 */
1334 MZ_ZIP_FLAG_WRITE_ZIP64 =
1335 0x4000, /* always use the zip64 file format, instead of the original zip
1336 file format with automatic switch to zip64. Use as flags
1337 parameter with mz_zip_writer_init*_v2 */
1338 MZ_ZIP_FLAG_WRITE_ALLOW_READING = 0x8000,
1339 MZ_ZIP_FLAG_ASCII_FILENAME = 0x10000,
1340 /*After adding a compressed file, seek back
1341 to local file header and set the correct sizes*/
1342 MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE = 0x20000
1343} mz_zip_flags;
1344
1345typedef enum {
1346 MZ_ZIP_TYPE_INVALID = 0,
1347 MZ_ZIP_TYPE_USER,
1348 MZ_ZIP_TYPE_MEMORY,
1349 MZ_ZIP_TYPE_HEAP,
1350 MZ_ZIP_TYPE_FILE,
1351 MZ_ZIP_TYPE_CFILE,
1352 MZ_ZIP_TOTAL_TYPES
1353} mz_zip_type;
1354
1355/* miniz error codes. Be sure to update mz_zip_get_error_string() if you add or
1356 * modify this enum. */
1357typedef enum {
1358 MZ_ZIP_NO_ERROR = 0,
1359 MZ_ZIP_UNDEFINED_ERROR,
1360 MZ_ZIP_TOO_MANY_FILES,
1361 MZ_ZIP_FILE_TOO_LARGE,
1362 MZ_ZIP_UNSUPPORTED_METHOD,
1363 MZ_ZIP_UNSUPPORTED_ENCRYPTION,
1364 MZ_ZIP_UNSUPPORTED_FEATURE,
1365 MZ_ZIP_FAILED_FINDING_CENTRAL_DIR,
1366 MZ_ZIP_NOT_AN_ARCHIVE,
1367 MZ_ZIP_INVALID_HEADER_OR_CORRUPTED,
1368 MZ_ZIP_UNSUPPORTED_MULTIDISK,
1369 MZ_ZIP_DECOMPRESSION_FAILED,
1370 MZ_ZIP_COMPRESSION_FAILED,
1371 MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE,
1372 MZ_ZIP_CRC_CHECK_FAILED,
1373 MZ_ZIP_UNSUPPORTED_CDIR_SIZE,
1374 MZ_ZIP_ALLOC_FAILED,
1375 MZ_ZIP_FILE_OPEN_FAILED,
1376 MZ_ZIP_FILE_CREATE_FAILED,
1377 MZ_ZIP_FILE_WRITE_FAILED,
1378 MZ_ZIP_FILE_READ_FAILED,
1379 MZ_ZIP_FILE_CLOSE_FAILED,
1380 MZ_ZIP_FILE_SEEK_FAILED,
1381 MZ_ZIP_FILE_STAT_FAILED,
1382 MZ_ZIP_INVALID_PARAMETER,
1383 MZ_ZIP_INVALID_FILENAME,
1384 MZ_ZIP_BUF_TOO_SMALL,
1385 MZ_ZIP_INTERNAL_ERROR,
1386 MZ_ZIP_FILE_NOT_FOUND,
1387 MZ_ZIP_ARCHIVE_TOO_LARGE,
1388 MZ_ZIP_VALIDATION_FAILED,
1389 MZ_ZIP_WRITE_CALLBACK_FAILED,
1390 MZ_ZIP_TOTAL_ERRORS
1391} mz_zip_error;
1392
1393typedef struct {
1394 mz_uint64 m_archive_size;
1395 mz_uint64 m_central_directory_file_ofs;
1396
1397 /* We only support up to UINT32_MAX files in zip64 mode. */
1398 mz_uint32 m_total_files;
1399 mz_zip_mode m_zip_mode;
1400 mz_zip_type m_zip_type;
1401 mz_zip_error m_last_error;
1402
1403 mz_uint64 m_file_offset_alignment;
1404
1405 mz_alloc_func m_pAlloc;
1406 mz_free_func m_pFree;
1407 mz_realloc_func m_pRealloc;
1408 void *m_pAlloc_opaque;
1409
1410 mz_file_read_func m_pRead;
1411 mz_file_write_func m_pWrite;
1412 mz_file_needs_keepalive m_pNeeds_keepalive;
1413 void *m_pIO_opaque;
1414
1415 mz_zip_internal_state *m_pState;
1416
1417} mz_zip_archive;
1418
1419typedef struct {
1420 mz_zip_archive *pZip;
1421 mz_uint flags;
1422
1423 int status;
1424
1425 mz_uint64 read_buf_size, read_buf_ofs, read_buf_avail, comp_remaining,
1426 out_buf_ofs, cur_file_ofs;
1427 mz_zip_archive_file_stat file_stat;
1428 void *pRead_buf;
1429 void *pWrite_buf;
1430
1431 size_t out_blk_remain;
1432
1433 tinfl_decompressor inflator;
1434
1435#ifdef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
1436 mz_uint padding;
1437#else
1438 mz_uint file_crc32;
1439#endif
1440
1441} mz_zip_reader_extract_iter_state;
1442
1443/* -------- ZIP reading */
1444
1445/* Inits a ZIP archive reader. */
1446/* These functions read and validate the archive's central directory. */
1447MINIZ_EXPORT mz_bool mz_zip_reader_init(mz_zip_archive *pZip,
1448 mz_uint64 size,
1449 mz_uint flags);
1450
1451MINIZ_EXPORT mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip,
1452 const void *pMem,
1453 size_t size,
1454 mz_uint flags);
1455
1456#ifndef MINIZ_NO_STDIO
1457/* Read a archive from a disk file. */
1458/* file_start_ofs is the file offset where the archive actually begins, or 0. */
1459/* actual_archive_size is the true total size of the archive, which may be
1460 * smaller than the file's actual size on disk. If zero the entire file is
1461 * treated as the archive. */
1462MINIZ_EXPORT mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip,
1463 const char *pFilename,
1464 mz_uint32 flags);
1465MINIZ_EXPORT mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip,
1466 const char *pFilename,
1467 mz_uint flags,
1468 mz_uint64 file_start_ofs,
1469 mz_uint64 archive_size);
1470
1471/* Read an archive from an already opened FILE, beginning at the current file
1472 * position. */
1473/* The archive is assumed to be archive_size bytes long. If archive_size is 0,
1474 * then the entire rest of the file is assumed to contain the archive. */
1475/* The FILE will NOT be closed when mz_zip_reader_end() is called. */
1476MINIZ_EXPORT mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip,
1477 MZ_FILE *pFile,
1478 mz_uint64 archive_size,
1479 mz_uint flags);
1480#endif
1481
1482/* Ends archive reading, freeing all allocations, and closing the input archive
1483 * file if mz_zip_reader_init_file() was used. */
1484MINIZ_EXPORT mz_bool mz_zip_reader_end(mz_zip_archive *pZip);
1485
1486/* -------- ZIP reading or writing */
1487
1488/* Clears a mz_zip_archive struct to all zeros. */
1489/* Important: This must be done before passing the struct to any mz_zip
1490 * functions. */
1491MINIZ_EXPORT void mz_zip_zero_struct(mz_zip_archive *pZip);
1492
1493MINIZ_EXPORT mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip);
1494MINIZ_EXPORT mz_zip_type mz_zip_get_type(mz_zip_archive *pZip);
1495
1496/* Returns the total number of files in the archive. */
1497MINIZ_EXPORT mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip);
1498
1499MINIZ_EXPORT mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip);
1500MINIZ_EXPORT mz_uint64
1501mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip);
1502MINIZ_EXPORT MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip);
1503
1504/* Reads n bytes of raw archive data, starting at file offset file_ofs, to pBuf.
1505 */
1506MINIZ_EXPORT size_t mz_zip_read_archive_data(mz_zip_archive *pZip,
1507 mz_uint64 file_ofs,
1508 void *pBuf,
1509 size_t n);
1510
1511/* All mz_zip funcs set the m_last_error field in the mz_zip_archive struct.
1512 * These functions retrieve/manipulate this field. */
1513/* Note that the m_last_error functionality is not thread safe. */
1514MINIZ_EXPORT mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip,
1515 mz_zip_error err_num);
1516MINIZ_EXPORT mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip);
1517MINIZ_EXPORT mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip);
1518MINIZ_EXPORT mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip);
1519MINIZ_EXPORT const char *mz_zip_get_error_string(mz_zip_error mz_err);
1520
1521/* MZ_TRUE if the archive file entry is a directory entry. */
1522MINIZ_EXPORT mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip,
1523 mz_uint file_index);
1524
1525/* MZ_TRUE if the file is encrypted/strong encrypted. */
1526MINIZ_EXPORT mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip,
1527 mz_uint file_index);
1528
1529/* MZ_TRUE if the compression method is supported, and the file is not
1530 * encrypted, and the file is not a compressed patch file. */
1531MINIZ_EXPORT mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip,
1532 mz_uint file_index);
1533
1534/* Retrieves the filename of an archive file entry. */
1535/* Returns the number of bytes written to pFilename, or if filename_buf_size is
1536 * 0 this function returns the number of bytes needed to fully store the
1537 * filename. */
1538MINIZ_EXPORT mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip,
1539 mz_uint file_index,
1540 char *pFilename,
1541 mz_uint filename_buf_size);
1542
1543/* Attempts to locates a file in the archive's central directory. */
1544/* Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH */
1545/* Returns -1 if the file cannot be found. */
1546MINIZ_EXPORT int mz_zip_reader_locate_file(mz_zip_archive *pZip,
1547 const char *pName,
1548 const char *pComment,
1549 mz_uint flags);
1550MINIZ_EXPORT mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip,
1551 const char *pName,
1552 const char *pComment,
1553 mz_uint flags,
1554 mz_uint32 *file_index);
1555
1556/* Returns detailed information about an archive file entry. */
1557MINIZ_EXPORT mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip,
1558 mz_uint file_index,
1559 mz_zip_archive_file_stat *pStat);
1560
1561/* MZ_TRUE if the file is in zip64 format. */
1562/* A file is considered zip64 if it contained a zip64 end of central directory
1563 * marker, or if it contained any zip64 extended file information fields in the
1564 * central directory. */
1565MINIZ_EXPORT mz_bool mz_zip_is_zip64(mz_zip_archive *pZip);
1566
1567/* Returns the total central directory size in bytes. */
1568/* The current max supported size is <= MZ_UINT32_MAX. */
1569MINIZ_EXPORT size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip);
1570
1571/* Extracts a archive file to a memory buffer using no memory allocation. */
1572/* There must be at least enough room on the stack to store the inflator's state
1573 * (~34KB or so). */
1574MINIZ_EXPORT mz_bool
1575mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip,
1576 mz_uint file_index,
1577 void *pBuf,
1578 size_t buf_size,
1579 mz_uint flags,
1580 void *pUser_read_buf,
1581 size_t user_read_buf_size);
1582MINIZ_EXPORT mz_bool
1583mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip,
1584 const char *pFilename,
1585 void *pBuf,
1586 size_t buf_size,
1587 mz_uint flags,
1588 void *pUser_read_buf,
1589 size_t user_read_buf_size);
1590
1591/* Extracts a archive file to a memory buffer. */
1592MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip,
1593 mz_uint file_index,
1594 void *pBuf,
1595 size_t buf_size,
1596 mz_uint flags);
1597MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip,
1598 const char *pFilename,
1599 void *pBuf,
1600 size_t buf_size,
1601 mz_uint flags);
1602
1603/* Extracts a archive file to a dynamically allocated heap buffer. */
1604/* The memory will be allocated via the mz_zip_archive's alloc/realloc
1605 * functions. */
1606/* Returns NULL and sets the last error on failure. */
1607MINIZ_EXPORT void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip,
1608 mz_uint file_index,
1609 size_t *pSize,
1610 mz_uint flags);
1611MINIZ_EXPORT void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip,
1612 const char *pFilename,
1613 size_t *pSize,
1614 mz_uint flags);
1615
1616/* Extracts a archive file using a callback function to output the file's data.
1617 */
1618MINIZ_EXPORT mz_bool
1619mz_zip_reader_extract_to_callback(mz_zip_archive *pZip,
1620 mz_uint file_index,
1621 mz_file_write_func pCallback,
1622 void *pOpaque,
1623 mz_uint flags);
1624MINIZ_EXPORT mz_bool
1625mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip,
1626 const char *pFilename,
1627 mz_file_write_func pCallback,
1628 void *pOpaque,
1629 mz_uint flags);
1630
1631/* Extract a file iteratively */
1632MINIZ_EXPORT mz_zip_reader_extract_iter_state *mz_zip_reader_extract_iter_new(
1633 mz_zip_archive *pZip,
1634 mz_uint file_index,
1635 mz_uint flags);
1636MINIZ_EXPORT mz_zip_reader_extract_iter_state *
1637mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip,
1638 const char *pFilename,
1639 mz_uint flags);
1640MINIZ_EXPORT size_t
1641mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state *pState,
1642 void *pvBuf,
1643 size_t buf_size);
1644MINIZ_EXPORT mz_bool
1645mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state *pState);
1646
1647#ifndef MINIZ_NO_STDIO
1648/* Extracts a archive file to a disk file and sets its last accessed and
1649 * modified times. */
1650/* This function only extracts files, not archive directory records. */
1651MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip,
1652 mz_uint file_index,
1653 const char *pDst_filename,
1654 mz_uint flags);
1655MINIZ_EXPORT mz_bool
1656mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip,
1657 const char *pArchive_filename,
1658 const char *pDst_filename,
1659 mz_uint flags);
1660
1661/* Extracts a archive file starting at the current position in the destination
1662 * FILE stream. */
1663MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip,
1664 mz_uint file_index,
1665 MZ_FILE *File,
1666 mz_uint flags);
1667MINIZ_EXPORT mz_bool
1668mz_zip_reader_extract_file_to_cfile(mz_zip_archive *pZip,
1669 const char *pArchive_filename,
1670 MZ_FILE *pFile,
1671 mz_uint flags);
1672#endif
1673
1674#if 0
1675/* TODO */
1676 typedef void *mz_zip_streaming_extract_state_ptr;
1677 mz_zip_streaming_extract_state_ptr mz_zip_streaming_extract_begin(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags);
1678 mz_uint64 mz_zip_streaming_extract_get_size(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
1679 mz_uint64 mz_zip_streaming_extract_get_cur_ofs(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
1680 mz_bool mz_zip_streaming_extract_seek(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, mz_uint64 new_ofs);
1681 size_t mz_zip_streaming_extract_read(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, void *pBuf, size_t buf_size);
1682 mz_bool mz_zip_streaming_extract_end(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
1683#endif
1684
1685/* This function compares the archive's local headers, the optional local zip64
1686 * extended information block, and the optional descriptor following the
1687 * compressed data vs. the data in the central directory. */
1688/* It also validates that each file can be successfully uncompressed unless the
1689 * MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY is specified. */
1690MINIZ_EXPORT mz_bool mz_zip_validate_file(mz_zip_archive *pZip,
1691 mz_uint file_index,
1692 mz_uint flags);
1693
1694/* Validates an entire archive by calling mz_zip_validate_file() on each file.
1695 */
1696MINIZ_EXPORT mz_bool mz_zip_validate_archive(mz_zip_archive *pZip,
1697 mz_uint flags);
1698
1699/* Misc utils/helpers, valid for ZIP reading or writing */
1700MINIZ_EXPORT mz_bool mz_zip_validate_mem_archive(const void *pMem,
1701 size_t size,
1702 mz_uint flags,
1703 mz_zip_error *pErr);
1704#ifndef MINIZ_NO_STDIO
1705MINIZ_EXPORT mz_bool mz_zip_validate_file_archive(const char *pFilename,
1706 mz_uint flags,
1707 mz_zip_error *pErr);
1708#endif
1709
1710/* Universal end function - calls either mz_zip_reader_end() or
1711 * mz_zip_writer_end(). */
1712MINIZ_EXPORT mz_bool mz_zip_end(mz_zip_archive *pZip);
1713
1714/* -------- ZIP writing */
1715
1716#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
1717
1718/* Inits a ZIP archive writer. */
1719/*Set pZip->m_pWrite (and pZip->m_pIO_opaque) before calling mz_zip_writer_init
1720 * or mz_zip_writer_init_v2*/
1721/*The output is streamable, i.e. file_ofs in mz_file_write_func always increases
1722 * only by n*/
1723MINIZ_EXPORT mz_bool mz_zip_writer_init(mz_zip_archive *pZip,
1724 mz_uint64 existing_size);
1725MINIZ_EXPORT mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip,
1726 mz_uint64 existing_size,
1727 mz_uint flags);
1728
1729MINIZ_EXPORT mz_bool
1730mz_zip_writer_init_heap(mz_zip_archive *pZip,
1731 size_t size_to_reserve_at_beginning,
1732 size_t initial_allocation_size);
1733MINIZ_EXPORT mz_bool
1734mz_zip_writer_init_heap_v2(mz_zip_archive *pZip,
1735 size_t size_to_reserve_at_beginning,
1736 size_t initial_allocation_size,
1737 mz_uint flags);
1738
1739#ifndef MINIZ_NO_STDIO
1740MINIZ_EXPORT mz_bool
1741mz_zip_writer_init_file(mz_zip_archive *pZip,
1742 const char *pFilename,
1743 mz_uint64 size_to_reserve_at_beginning);
1744MINIZ_EXPORT mz_bool
1745mz_zip_writer_init_file_v2(mz_zip_archive *pZip,
1746 const char *pFilename,
1747 mz_uint64 size_to_reserve_at_beginning,
1748 mz_uint flags);
1749MINIZ_EXPORT mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip,
1750 MZ_FILE *pFile,
1751 mz_uint flags);
1752#endif
1753
1754/* Converts a ZIP archive reader object into a writer object, to allow efficient
1755 * in-place file appends to occur on an existing archive. */
1756/* For archives opened using mz_zip_reader_init_file, pFilename must be the
1757 * archive's filename so it can be reopened for writing. If the file can't be
1758 * reopened, mz_zip_reader_end() will be called. */
1759/* For archives opened using mz_zip_reader_init_mem, the memory block must be
1760 * growable using the realloc callback (which defaults to realloc unless you've
1761 * overridden it). */
1762/* Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's
1763 * user provided m_pWrite function cannot be NULL. */
1764/* Note: In-place archive modification is not recommended unless you know what
1765 * you're doing, because if execution stops or something goes wrong before */
1766/* the archive is finalized the file's central directory will be hosed. */
1767MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip,
1768 const char *pFilename);
1769MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip,
1770 const char *pFilename,
1771 mz_uint flags);
1772
1773/* Adds the contents of a memory buffer to an archive. These functions record
1774 * the current local time into the archive. */
1775/* To add a directory entry, call this method with an archive name ending in a
1776 * forwardslash with an empty buffer. */
1777/* level_and_flags - compression level (0-10, see MZ_BEST_SPEED,
1778 * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or
1779 * just set to MZ_DEFAULT_COMPRESSION. */
1780MINIZ_EXPORT mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip,
1781 const char *pArchive_name,
1782 const void *pBuf,
1783 size_t buf_size,
1784 mz_uint level_and_flags);
1785
1786/* Like mz_zip_writer_add_mem(), except you can specify a file comment field,
1787 * and optionally supply the function with already compressed data. */
1788/* uncomp_size/uncomp_crc32 are only used if the MZ_ZIP_FLAG_COMPRESSED_DATA
1789 * flag is specified. */
1790MINIZ_EXPORT mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip,
1791 const char *pArchive_name,
1792 const void *pBuf,
1793 size_t buf_size,
1794 const void *pComment,
1795 mz_uint16 comment_size,
1796 mz_uint level_and_flags,
1797 mz_uint64 uncomp_size,
1798 mz_uint32 uncomp_crc32);
1799
1800MINIZ_EXPORT mz_bool
1801mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip,
1802 const char *pArchive_name,
1803 const void *pBuf,
1804 size_t buf_size,
1805 const void *pComment,
1806 mz_uint16 comment_size,
1807 mz_uint level_and_flags,
1808 mz_uint64 uncomp_size,
1809 mz_uint32 uncomp_crc32,
1810 MZ_TIME_T *last_modified,
1811 const char *user_extra_data_local,
1812 mz_uint user_extra_data_local_len,
1813 const char *user_extra_data_central,
1814 mz_uint user_extra_data_central_len);
1815
1816/* Adds the contents of a file to an archive. This function also records the
1817 * disk file's modified time into the archive. */
1818/* File data is supplied via a read callback function. User
1819 * mz_zip_writer_add_(c)file to add a file directly.*/
1820MINIZ_EXPORT mz_bool
1821mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip,
1822 const char *pArchive_name,
1823 mz_file_read_func read_callback,
1824 void *callback_opaque,
1825 mz_uint64 max_size,
1826 const MZ_TIME_T *pFile_time,
1827 const void *pComment,
1828 mz_uint16 comment_size,
1829 mz_uint level_and_flags,
1830 const char *user_extra_data_local,
1831 mz_uint user_extra_data_local_len,
1832 const char *user_extra_data_central,
1833 mz_uint user_extra_data_central_len);
1834
1835#ifndef MINIZ_NO_STDIO
1836/* Adds the contents of a disk file to an archive. This function also records
1837 * the disk file's modified time into the archive. */
1838/* level_and_flags - compression level (0-10, see MZ_BEST_SPEED,
1839 * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or
1840 * just set to MZ_DEFAULT_COMPRESSION. */
1841MINIZ_EXPORT mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip,
1842 const char *pArchive_name,
1843 const char *pSrc_filename,
1844 const void *pComment,
1845 mz_uint16 comment_size,
1846 mz_uint level_and_flags);
1847
1848/* Like mz_zip_writer_add_file(), except the file data is read from the
1849 * specified FILE stream. */
1850MINIZ_EXPORT mz_bool
1851mz_zip_writer_add_cfile(mz_zip_archive *pZip,
1852 const char *pArchive_name,
1853 MZ_FILE *pSrc_file,
1854 mz_uint64 max_size,
1855 const MZ_TIME_T *pFile_time,
1856 const void *pComment,
1857 mz_uint16 comment_size,
1858 mz_uint level_and_flags,
1859 const char *user_extra_data_local,
1860 mz_uint user_extra_data_local_len,
1861 const char *user_extra_data_central,
1862 mz_uint user_extra_data_central_len);
1863#endif
1864
1865/* Adds a file to an archive by fully cloning the data from another archive. */
1866/* This function fully clones the source file's compressed data (no
1867 * recompression), along with its full filename, extra data (it may add or
1868 * modify the zip64 local header extra data field), and the optional descriptor
1869 * following the compressed data. */
1870MINIZ_EXPORT mz_bool
1871mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip,
1872 mz_zip_archive *pSource_zip,
1873 mz_uint src_file_index);
1874
1875/* Finalizes the archive by writing the central directory records followed by
1876 * the end of central directory record. */
1877/* After an archive is finalized, the only valid call on the mz_zip_archive
1878 * struct is mz_zip_writer_end(). */
1879/* An archive must be manually finalized by calling this function for it to be
1880 * valid. */
1881MINIZ_EXPORT mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip);
1882
1883/* Finalizes a heap archive, returning a pointer to the heap block and its size.
1884 */
1885/* The heap block will be allocated using the mz_zip_archive's alloc/realloc
1886 * callbacks. */
1887MINIZ_EXPORT mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip,
1888 void **ppBuf,
1889 size_t *pSize);
1890
1891/* Ends archive writing, freeing all allocations, and closing the output file if
1892 * mz_zip_writer_init_file() was used. */
1893/* Note for the archive to be valid, it *must* have been finalized before ending
1894 * (this function will not do it for you). */
1895MINIZ_EXPORT mz_bool mz_zip_writer_end(mz_zip_archive *pZip);
1896
1897/* -------- Misc. high-level helper functions: */
1898
1899/* mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically)
1900 * appends a memory blob to a ZIP archive. */
1901/* Note this is NOT a fully safe operation. If it crashes or dies in some way
1902 * your archive can be left in a screwed up state (without a central directory).
1903 */
1904/* level_and_flags - compression level (0-10, see MZ_BEST_SPEED,
1905 * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or
1906 * just set to MZ_DEFAULT_COMPRESSION. */
1907/* TODO: Perhaps add an option to leave the existing central dir in place in
1908 * case the add dies? We could then truncate the file (so the old central dir
1909 * would be at the end) if something goes wrong. */
1910MINIZ_EXPORT mz_bool
1911mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename,
1912 const char *pArchive_name,
1913 const void *pBuf,
1914 size_t buf_size,
1915 const void *pComment,
1916 mz_uint16 comment_size,
1917 mz_uint level_and_flags);
1918MINIZ_EXPORT mz_bool
1919mz_zip_add_mem_to_archive_file_in_place_v2(const char *pZip_filename,
1920 const char *pArchive_name,
1921 const void *pBuf,
1922 size_t buf_size,
1923 const void *pComment,
1924 mz_uint16 comment_size,
1925 mz_uint level_and_flags,
1926 mz_zip_error *pErr);
1927
1928#ifndef MINIZ_NO_STDIO
1929/* Reads a single file from an archive into a heap block. */
1930/* If pComment is not NULL, only the file with the specified comment will be
1931 * extracted. */
1932/* Returns NULL on failure. */
1933MINIZ_EXPORT void *mz_zip_extract_archive_file_to_heap(
1934 const char *pZip_filename,
1935 const char *pArchive_name,
1936 size_t *pSize,
1937 mz_uint flags);
1938MINIZ_EXPORT void *mz_zip_extract_archive_file_to_heap_v2(
1939 const char *pZip_filename,
1940 const char *pArchive_name,
1941 const char *pComment,
1942 size_t *pSize,
1943 mz_uint flags,
1944 mz_zip_error *pErr);
1945#endif
1946
1947#endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */
1948
1949#ifdef __cplusplus
1950}
1951#endif
1952
1953#endif /* MINIZ_NO_ARCHIVE_APIS */
1954