root/src/punutil/miniz.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. deflateInit
  2. deflateInit2
  3. deflateReset
  4. deflate
  5. deflateEnd
  6. deflateBound
  7. compress
  8. compress2
  9. compressBound
  10. inflateInit
  11. inflateInit2
  12. inflateReset
  13. inflate
  14. inflateEnd
  15. uncompress
  16. uncompress2
  17. crc32
  18. adler32
  19. zError

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

/* [previous][next][first][last][top][bottom][index][help] */