miniz.h 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. #define MINIZ_EXPORT
  2. /* miniz.c 2.2.0 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing
  3. See "unlicense" statement at the end of this file.
  4. Rich Geldreich <richgel99@gmail.com>, last updated Oct. 13, 2013
  5. Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt
  6. Most API's defined in miniz.c are optional. For example, to disable the archive related functions just define
  7. MINIZ_NO_ARCHIVE_APIS, or to get rid of all stdio usage define MINIZ_NO_STDIO (see the list below for more macros).
  8. * Low-level Deflate/Inflate implementation notes:
  9. Compression: Use the "tdefl" API's. The compressor supports raw, static, and dynamic blocks, lazy or
  10. greedy parsing, match length filtering, RLE-only, and Huffman-only streams. It performs and compresses
  11. approximately as well as zlib.
  12. Decompression: Use the "tinfl" API's. The entire decompressor is implemented as a single function
  13. coroutine: see tinfl_decompress(). It supports decompression into a 32KB (or larger power of 2) wrapping buffer, or into a memory
  14. block large enough to hold the entire file.
  15. The low-level tdefl/tinfl API's do not make any use of dynamic memory allocation.
  16. * zlib-style API notes:
  17. miniz.c implements a fairly large subset of zlib. There's enough functionality present for it to be a drop-in
  18. zlib replacement in many apps:
  19. The z_stream struct, optional memory allocation callbacks
  20. deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound
  21. inflateInit/inflateInit2/inflate/inflateReset/inflateEnd
  22. compress, compress2, compressBound, uncompress
  23. CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly routines.
  24. Supports raw deflate streams or standard zlib streams with adler-32 checking.
  25. Limitations:
  26. The callback API's are not implemented yet. No support for gzip headers or zlib static dictionaries.
  27. I've tried to closely emulate zlib's various flavors of stream flushing and return status codes, but
  28. there are no guarantees that miniz.c pulls this off perfectly.
  29. * PNG writing: See the tdefl_write_image_to_png_file_in_memory() function, originally written by
  30. Alex Evans. Supports 1-4 bytes/pixel images.
  31. * ZIP archive API notes:
  32. The ZIP archive API's where designed with simplicity and efficiency in mind, with just enough abstraction to
  33. get the job done with minimal fuss. There are simple API's to retrieve file information, read files from
  34. existing archives, create new archives, append new files to existing archives, or clone archive data from
  35. one archive to another. It supports archives located in memory or the heap, on disk (using stdio.h),
  36. or you can specify custom file read/write callbacks.
  37. - Archive reading: Just call this function to read a single file from a disk archive:
  38. void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name,
  39. size_t *pSize, mz_uint zip_flags);
  40. For more complex cases, use the "mz_zip_reader" functions. Upon opening an archive, the entire central
  41. directory is located and read as-is into memory, and subsequent file access only occurs when reading individual files.
  42. - Archives file scanning: The simple way is to use this function to scan a loaded archive for a specific file:
  43. int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags);
  44. The locate operation can optionally check file comments too, which (as one example) can be used to identify
  45. multiple versions of the same file in an archive. This function uses a simple linear search through the central
  46. directory, so it's not very fast.
  47. Alternately, you can iterate through all the files in an archive (using mz_zip_reader_get_num_files()) and
  48. retrieve detailed info on each file by calling mz_zip_reader_file_stat().
  49. - Archive creation: Use the "mz_zip_writer" functions. The ZIP writer immediately writes compressed file data
  50. to disk and builds an exact image of the central directory in memory. The central directory image is written
  51. all at once at the end of the archive file when the archive is finalized.
  52. The archive writer can optionally align each file's local header and file data to any power of 2 alignment,
  53. which can be useful when the archive will be read from optical media. Also, the writer supports placing
  54. arbitrary data blobs at the very beginning of ZIP archives. Archives written using either feature are still
  55. readable by any ZIP tool.
  56. - Archive appending: The simple way to add a single file to an archive is to call this function:
  57. mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name,
  58. const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags);
  59. The archive will be created if it doesn't already exist, otherwise it'll be appended to.
  60. Note the appending is done in-place and is not an atomic operation, so if something goes wrong
  61. during the operation it's possible the archive could be left without a central directory (although the local
  62. file headers and file data will be fine, so the archive will be recoverable).
  63. For more complex archive modification scenarios:
  64. 1. The safest way is to use a mz_zip_reader to read the existing archive, cloning only those bits you want to
  65. preserve into a new archive using using the mz_zip_writer_add_from_zip_reader() function (which compiles the
  66. compressed file data as-is). When you're done, delete the old archive and rename the newly written archive, and
  67. you're done. This is safe but requires a bunch of temporary disk space or heap memory.
  68. 2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using mz_zip_writer_init_from_reader(),
  69. append new files as needed, then finalize the archive which will write an updated central directory to the
  70. original archive. (This is basically what mz_zip_add_mem_to_archive_file_in_place() does.) There's a
  71. possibility that the archive's central directory could be lost with this method if anything goes wrong, though.
  72. - ZIP archive support limitations:
  73. No spanning support. Extraction functions can only handle unencrypted, stored or deflated files.
  74. Requires streams capable of seeking.
  75. * This is a header file library, like stb_image.c. To get only a header file, either cut and paste the
  76. below header, or create miniz.h, #define MINIZ_HEADER_FILE_ONLY, and then include miniz.c from it.
  77. * Important: For best perf. be sure to customize the below macros for your target platform:
  78. #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
  79. #define MINIZ_LITTLE_ENDIAN 1
  80. #define MINIZ_HAS_64BIT_REGISTERS 1
  81. * On platforms using glibc, Be sure to "#define _LARGEFILE64_SOURCE 1" before including miniz.c to ensure miniz
  82. uses the 64-bit variants: fopen64(), stat64(), etc. Otherwise you won't be able to process large files
  83. (i.e. 32-bit stat() fails for me on files > 0x7FFFFFFF bytes).
  84. */
  85. #pragma once
  86. /* Defines to completely disable specific portions of miniz.c:
  87. If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl. */
  88. /* Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O. */
  89. /*#define MINIZ_NO_STDIO */
  90. /* If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able to get the current time, or */
  91. /* get/set file times, and the C run-time funcs that get/set times won't be called. */
  92. /* The current downside is the times written to your archives will be from 1979. */
  93. /*#define MINIZ_NO_TIME */
  94. /* Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. */
  95. /*#define MINIZ_NO_ARCHIVE_APIS */
  96. /* Define MINIZ_NO_ARCHIVE_WRITING_APIS to disable all writing related ZIP archive API's. */
  97. /*#define MINIZ_NO_ARCHIVE_WRITING_APIS */
  98. /* Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's. */
  99. /*#define MINIZ_NO_ZLIB_APIS */
  100. /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib. */
  101. /*#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES */
  102. /* Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc.
  103. Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc
  104. callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user
  105. functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work. */
  106. /*#define MINIZ_NO_MALLOC */
  107. #if defined(__TINYC__) && (defined(__linux) || defined(__linux__))
  108. /* TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux */
  109. #define MINIZ_NO_TIME
  110. #endif
  111. #include <stddef.h>
  112. #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS)
  113. #include <time.h>
  114. #endif
  115. #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__)
  116. /* MINIZ_X86_OR_X64_CPU is only used to help set the below macros. */
  117. #define MINIZ_X86_OR_X64_CPU 1
  118. #else
  119. #define MINIZ_X86_OR_X64_CPU 0
  120. #endif
  121. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
  122. /* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */
  123. #define MINIZ_LITTLE_ENDIAN 1
  124. #else
  125. #define MINIZ_LITTLE_ENDIAN 0
  126. #endif
  127. /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES only if not set */
  128. #if !defined(MINIZ_USE_UNALIGNED_LOADS_AND_STORES)
  129. #if MINIZ_X86_OR_X64_CPU
  130. /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses. */
  131. #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
  132. #define MINIZ_UNALIGNED_USE_MEMCPY
  133. #else
  134. #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0
  135. #endif
  136. #endif
  137. #if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
  138. /* 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). */
  139. #define MINIZ_HAS_64BIT_REGISTERS 1
  140. #else
  141. #define MINIZ_HAS_64BIT_REGISTERS 0
  142. #endif
  143. #ifdef __cplusplus
  144. extern "C" {
  145. #endif
  146. /* ------------------- zlib-style API Definitions. */
  147. /* 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! */
  148. typedef unsigned long mz_ulong;
  149. /* 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. */
  150. MINIZ_EXPORT void mz_free(void *p);
  151. #define MZ_ADLER32_INIT (1)
  152. /* mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL. */
  153. MINIZ_EXPORT mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len);
  154. #define MZ_CRC32_INIT (0)
  155. /* mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL. */
  156. MINIZ_EXPORT mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, size_t buf_len);
  157. /* Compression strategies. */
  158. enum
  159. {
  160. MZ_DEFAULT_STRATEGY = 0,
  161. MZ_FILTERED = 1,
  162. MZ_HUFFMAN_ONLY = 2,
  163. MZ_RLE = 3,
  164. MZ_FIXED = 4
  165. };
  166. /* Method */
  167. #define MZ_DEFLATED 8
  168. /* Heap allocation callbacks.
  169. Note that mz_alloc_func parameter types purposely differ from zlib's: items/size is size_t, not unsigned long. */
  170. typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size);
  171. typedef void (*mz_free_func)(void *opaque, void *address);
  172. typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size);
  173. /* 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. */
  174. enum
  175. {
  176. MZ_NO_COMPRESSION = 0,
  177. MZ_BEST_SPEED = 1,
  178. MZ_BEST_COMPRESSION = 9,
  179. MZ_UBER_COMPRESSION = 10,
  180. MZ_DEFAULT_LEVEL = 6,
  181. MZ_DEFAULT_COMPRESSION = -1
  182. };
  183. #define MZ_VERSION "10.2.0"
  184. #define MZ_VERNUM 0xA100
  185. #define MZ_VER_MAJOR 10
  186. #define MZ_VER_MINOR 2
  187. #define MZ_VER_REVISION 0
  188. #define MZ_VER_SUBREVISION 0
  189. #ifndef MINIZ_NO_ZLIB_APIS
  190. /* 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). */
  191. enum
  192. {
  193. MZ_NO_FLUSH = 0,
  194. MZ_PARTIAL_FLUSH = 1,
  195. MZ_SYNC_FLUSH = 2,
  196. MZ_FULL_FLUSH = 3,
  197. MZ_FINISH = 4,
  198. MZ_BLOCK = 5
  199. };
  200. /* Return status codes. MZ_PARAM_ERROR is non-standard. */
  201. enum
  202. {
  203. MZ_OK = 0,
  204. MZ_STREAM_END = 1,
  205. MZ_NEED_DICT = 2,
  206. MZ_ERRNO = -1,
  207. MZ_STREAM_ERROR = -2,
  208. MZ_DATA_ERROR = -3,
  209. MZ_MEM_ERROR = -4,
  210. MZ_BUF_ERROR = -5,
  211. MZ_VERSION_ERROR = -6,
  212. MZ_PARAM_ERROR = -10000
  213. };
  214. /* Window bits */
  215. #define MZ_DEFAULT_WINDOW_BITS 15
  216. struct mz_internal_state;
  217. /* Compression/decompression stream struct. */
  218. typedef struct mz_stream_s
  219. {
  220. const unsigned char *next_in; /* pointer to next byte to read */
  221. unsigned int avail_in; /* number of bytes available at next_in */
  222. mz_ulong total_in; /* total number of bytes consumed so far */
  223. unsigned char *next_out; /* pointer to next byte to write */
  224. unsigned int avail_out; /* number of bytes that can be written to next_out */
  225. mz_ulong total_out; /* total number of bytes produced so far */
  226. char *msg; /* error msg (unused) */
  227. struct mz_internal_state *state; /* internal state, allocated by zalloc/zfree */
  228. mz_alloc_func zalloc; /* optional heap allocation function (defaults to malloc) */
  229. mz_free_func zfree; /* optional heap free function (defaults to free) */
  230. void *opaque; /* heap alloc function user pointer */
  231. int data_type; /* data_type (unused) */
  232. mz_ulong adler; /* adler32 of the source or uncompressed data */
  233. mz_ulong reserved; /* not used */
  234. } mz_stream;
  235. typedef mz_stream *mz_streamp;
  236. /* Returns the version string of miniz.c. */
  237. MINIZ_EXPORT const char *mz_version(void);
  238. /* mz_deflateInit() initializes a compressor with default options: */
  239. /* Parameters: */
  240. /* pStream must point to an initialized mz_stream struct. */
  241. /* level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. */
  242. /* level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio. */
  243. /* (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.) */
  244. /* Return values: */
  245. /* MZ_OK on success. */
  246. /* MZ_STREAM_ERROR if the stream is bogus. */
  247. /* MZ_PARAM_ERROR if the input parameters are bogus. */
  248. /* MZ_MEM_ERROR on out of memory. */
  249. MINIZ_EXPORT int mz_deflateInit(mz_streamp pStream, int level);
  250. /* mz_deflateInit2() is like mz_deflate(), except with more control: */
  251. /* Additional parameters: */
  252. /* method must be MZ_DEFLATED */
  253. /* 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) */
  254. /* mem_level must be between [1, 9] (it's checked but ignored by miniz.c) */
  255. MINIZ_EXPORT int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy);
  256. /* Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2(). */
  257. MINIZ_EXPORT int mz_deflateReset(mz_streamp pStream);
  258. /* mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible. */
  259. /* Parameters: */
  260. /* 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. */
  261. /* flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH. */
  262. /* Return values: */
  263. /* 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). */
  264. /* 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. */
  265. /* MZ_STREAM_ERROR if the stream is bogus. */
  266. /* MZ_PARAM_ERROR if one of the parameters is invalid. */
  267. /* 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.) */
  268. MINIZ_EXPORT int mz_deflate(mz_streamp pStream, int flush);
  269. /* mz_deflateEnd() deinitializes a compressor: */
  270. /* Return values: */
  271. /* MZ_OK on success. */
  272. /* MZ_STREAM_ERROR if the stream is bogus. */
  273. MINIZ_EXPORT int mz_deflateEnd(mz_streamp pStream);
  274. /* 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. */
  275. MINIZ_EXPORT mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len);
  276. /* Single-call compression functions mz_compress() and mz_compress2(): */
  277. /* Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure. */
  278. MINIZ_EXPORT int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
  279. MINIZ_EXPORT int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level);
  280. /* mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress(). */
  281. MINIZ_EXPORT mz_ulong mz_compressBound(mz_ulong source_len);
  282. /* Initializes a decompressor. */
  283. MINIZ_EXPORT int mz_inflateInit(mz_streamp pStream);
  284. /* 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: */
  285. /* window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate). */
  286. MINIZ_EXPORT int mz_inflateInit2(mz_streamp pStream, int window_bits);
  287. /* Quickly resets a compressor without having to reallocate anything. Same as calling mz_inflateEnd() followed by mz_inflateInit()/mz_inflateInit2(). */
  288. MINIZ_EXPORT int mz_inflateReset(mz_streamp pStream);
  289. /* 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. */
  290. /* Parameters: */
  291. /* 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. */
  292. /* flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. */
  293. /* 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). */
  294. /* 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. */
  295. /* Return values: */
  296. /* 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. */
  297. /* 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. */
  298. /* MZ_STREAM_ERROR if the stream is bogus. */
  299. /* MZ_DATA_ERROR if the deflate stream is invalid. */
  300. /* MZ_PARAM_ERROR if one of the parameters is invalid. */
  301. /* 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 */
  302. /* with more input data, or with more room in the output buffer (except when using single call decompression, described above). */
  303. MINIZ_EXPORT int mz_inflate(mz_streamp pStream, int flush);
  304. /* Deinitializes a decompressor. */
  305. MINIZ_EXPORT int mz_inflateEnd(mz_streamp pStream);
  306. /* Single-call decompression. */
  307. /* Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure. */
  308. MINIZ_EXPORT int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
  309. MINIZ_EXPORT int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong *pSource_len);
  310. /* Returns a string description of the specified error code, or NULL if the error code is invalid. */
  311. MINIZ_EXPORT const char *mz_error(int err);
  312. /* 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. */
  313. /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project. */
  314. #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
  315. typedef unsigned char Byte;
  316. typedef unsigned int uInt;
  317. typedef mz_ulong uLong;
  318. typedef Byte Bytef;
  319. typedef uInt uIntf;
  320. typedef char charf;
  321. typedef int intf;
  322. typedef void *voidpf;
  323. typedef uLong uLongf;
  324. typedef void *voidp;
  325. typedef void *const voidpc;
  326. #define Z_NULL 0
  327. #define Z_NO_FLUSH MZ_NO_FLUSH
  328. #define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH
  329. #define Z_SYNC_FLUSH MZ_SYNC_FLUSH
  330. #define Z_FULL_FLUSH MZ_FULL_FLUSH
  331. #define Z_FINISH MZ_FINISH
  332. #define Z_BLOCK MZ_BLOCK
  333. #define Z_OK MZ_OK
  334. #define Z_STREAM_END MZ_STREAM_END
  335. #define Z_NEED_DICT MZ_NEED_DICT
  336. #define Z_ERRNO MZ_ERRNO
  337. #define Z_STREAM_ERROR MZ_STREAM_ERROR
  338. #define Z_DATA_ERROR MZ_DATA_ERROR
  339. #define Z_MEM_ERROR MZ_MEM_ERROR
  340. #define Z_BUF_ERROR MZ_BUF_ERROR
  341. #define Z_VERSION_ERROR MZ_VERSION_ERROR
  342. #define Z_PARAM_ERROR MZ_PARAM_ERROR
  343. #define Z_NO_COMPRESSION MZ_NO_COMPRESSION
  344. #define Z_BEST_SPEED MZ_BEST_SPEED
  345. #define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION
  346. #define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION
  347. #define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY
  348. #define Z_FILTERED MZ_FILTERED
  349. #define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY
  350. #define Z_RLE MZ_RLE
  351. #define Z_FIXED MZ_FIXED
  352. #define Z_DEFLATED MZ_DEFLATED
  353. #define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS
  354. #define alloc_func mz_alloc_func
  355. #define free_func mz_free_func
  356. #define internal_state mz_internal_state
  357. #define z_stream mz_stream
  358. #define deflateInit mz_deflateInit
  359. #define deflateInit2 mz_deflateInit2
  360. #define deflateReset mz_deflateReset
  361. #define deflate mz_deflate
  362. #define deflateEnd mz_deflateEnd
  363. #define deflateBound mz_deflateBound
  364. #define compress mz_compress
  365. #define compress2 mz_compress2
  366. #define compressBound mz_compressBound
  367. #define inflateInit mz_inflateInit
  368. #define inflateInit2 mz_inflateInit2
  369. #define inflateReset mz_inflateReset
  370. #define inflate mz_inflate
  371. #define inflateEnd mz_inflateEnd
  372. #define uncompress mz_uncompress
  373. #define uncompress2 mz_uncompress2
  374. #define crc32 mz_crc32
  375. #define adler32 mz_adler32
  376. #define MAX_WBITS 15
  377. #define MAX_MEM_LEVEL 9
  378. #define zError mz_error
  379. #define ZLIB_VERSION MZ_VERSION
  380. #define ZLIB_VERNUM MZ_VERNUM
  381. #define ZLIB_VER_MAJOR MZ_VER_MAJOR
  382. #define ZLIB_VER_MINOR MZ_VER_MINOR
  383. #define ZLIB_VER_REVISION MZ_VER_REVISION
  384. #define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION
  385. #define zlibVersion mz_version
  386. #define zlib_version mz_version()
  387. #endif /* #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES */
  388. #endif /* MINIZ_NO_ZLIB_APIS */
  389. #ifdef __cplusplus
  390. }
  391. #endif
  392. #pragma once
  393. #include <assert.h>
  394. #include <stdint.h>
  395. #include <stdlib.h>
  396. #include <string.h>
  397. /* ------------------- Types and macros */
  398. typedef unsigned char mz_uint8;
  399. typedef signed short mz_int16;
  400. typedef unsigned short mz_uint16;
  401. typedef unsigned int mz_uint32;
  402. typedef unsigned int mz_uint;
  403. typedef int64_t mz_int64;
  404. typedef uint64_t mz_uint64;
  405. typedef int mz_bool;
  406. #define MZ_FALSE (0)
  407. #define MZ_TRUE (1)
  408. /* Works around MSVC's spammy "warning C4127: conditional expression is constant" message. */
  409. #ifdef _MSC_VER
  410. #define MZ_MACRO_END while (0, 0)
  411. #else
  412. #define MZ_MACRO_END while (0)
  413. #endif
  414. #ifdef MINIZ_NO_STDIO
  415. #define MZ_FILE void *
  416. #else
  417. #include <stdio.h>
  418. #define MZ_FILE FILE
  419. #endif /* #ifdef MINIZ_NO_STDIO */
  420. #ifdef MINIZ_NO_TIME
  421. typedef struct mz_dummy_time_t_tag
  422. {
  423. int m_dummy;
  424. } mz_dummy_time_t;
  425. #define MZ_TIME_T mz_dummy_time_t
  426. #else
  427. #define MZ_TIME_T time_t
  428. #endif
  429. #define MZ_ASSERT(x) assert(x)
  430. #ifdef MINIZ_NO_MALLOC
  431. #define MZ_MALLOC(x) NULL
  432. #define MZ_FREE(x) (void)x, ((void)0)
  433. #define MZ_REALLOC(p, x) NULL
  434. #else
  435. #define MZ_MALLOC(x) malloc(x)
  436. #define MZ_FREE(x) free(x)
  437. #define MZ_REALLOC(p, x) realloc(p, x)
  438. #endif
  439. #define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b))
  440. #define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b))
  441. #define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
  442. #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
  443. #define MZ_READ_LE16(p) *((const mz_uint16 *)(p))
  444. #define MZ_READ_LE32(p) *((const mz_uint32 *)(p))
  445. #else
  446. #define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U))
  447. #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))
  448. #endif
  449. #define MZ_READ_LE64(p) (((mz_uint64)MZ_READ_LE32(p)) | (((mz_uint64)MZ_READ_LE32((const mz_uint8 *)(p) + sizeof(mz_uint32))) << 32U))
  450. #ifdef _MSC_VER
  451. #define MZ_FORCEINLINE __forceinline
  452. #elif defined(__GNUC__)
  453. #define MZ_FORCEINLINE __inline__ __attribute__((__always_inline__))
  454. #else
  455. #define MZ_FORCEINLINE inline
  456. #endif
  457. #ifdef __cplusplus
  458. extern "C" {
  459. #endif
  460. extern MINIZ_EXPORT void *miniz_def_alloc_func(void *opaque, size_t items, size_t size);
  461. extern MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address);
  462. extern MINIZ_EXPORT void *miniz_def_realloc_func(void *opaque, void *address, size_t items, size_t size);
  463. #define MZ_UINT16_MAX (0xFFFFU)
  464. #define MZ_UINT32_MAX (0xFFFFFFFFU)
  465. #ifdef __cplusplus
  466. }
  467. #endif
  468. #pragma once
  469. #ifdef __cplusplus
  470. extern "C" {
  471. #endif
  472. /* ------------------- Low-level Compression API Definitions */
  473. /* Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently). */
  474. #define TDEFL_LESS_MEMORY 0
  475. /* tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search): */
  476. /* 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). */
  477. enum
  478. {
  479. TDEFL_HUFFMAN_ONLY = 0,
  480. TDEFL_DEFAULT_MAX_PROBES = 128,
  481. TDEFL_MAX_PROBES_MASK = 0xFFF
  482. };
  483. /* 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. */
  484. /* TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers). */
  485. /* TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing. */
  486. /* 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). */
  487. /* TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1) */
  488. /* TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. */
  489. /* TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. */
  490. /* TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. */
  491. /* The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK). */
  492. enum
  493. {
  494. TDEFL_WRITE_ZLIB_HEADER = 0x01000,
  495. TDEFL_COMPUTE_ADLER32 = 0x02000,
  496. TDEFL_GREEDY_PARSING_FLAG = 0x04000,
  497. TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000,
  498. TDEFL_RLE_MATCHES = 0x10000,
  499. TDEFL_FILTER_MATCHES = 0x20000,
  500. TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000,
  501. TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
  502. };
  503. /* High level compression functions: */
  504. /* tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc(). */
  505. /* On entry: */
  506. /* pSrc_buf, src_buf_len: Pointer and size of source block to compress. */
  507. /* flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower but improve compression. */
  508. /* On return: */
  509. /* Function returns a pointer to the compressed data, or NULL on failure. */
  510. /* *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data. */
  511. /* The caller must free() the returned block when it's no longer needed. */
  512. MINIZ_EXPORT void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
  513. /* tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory. */
  514. /* Returns 0 on failure. */
  515. 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);
  516. /* Compresses an image to a compressed PNG file in memory. */
  517. /* On entry: */
  518. /* pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4. */
  519. /* The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in memory. */
  520. /* 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 */
  521. /* If flip is true, the image will be flipped on the Y axis (useful for OpenGL apps). */
  522. /* On return: */
  523. /* Function returns a pointer to the compressed data, or NULL on failure. */
  524. /* *pLen_out will be set to the size of the PNG image file. */
  525. /* The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no longer needed. */
  526. 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);
  527. 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);
  528. /* Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time. */
  529. typedef mz_bool (*tdefl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser);
  530. /* tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally. */
  531. 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);
  532. enum
  533. {
  534. TDEFL_MAX_HUFF_TABLES = 3,
  535. TDEFL_MAX_HUFF_SYMBOLS_0 = 288,
  536. TDEFL_MAX_HUFF_SYMBOLS_1 = 32,
  537. TDEFL_MAX_HUFF_SYMBOLS_2 = 19,
  538. TDEFL_LZ_DICT_SIZE = 32768,
  539. TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1,
  540. TDEFL_MIN_MATCH_LEN = 3,
  541. TDEFL_MAX_MATCH_LEN = 258
  542. };
  543. /* TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes). */
  544. #if TDEFL_LESS_MEMORY
  545. enum
  546. {
  547. TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024,
  548. TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10,
  549. TDEFL_MAX_HUFF_SYMBOLS = 288,
  550. TDEFL_LZ_HASH_BITS = 12,
  551. TDEFL_LEVEL1_HASH_SIZE_MASK = 4095,
  552. TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3,
  553. TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS
  554. };
  555. #else
  556. enum
  557. {
  558. TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024,
  559. TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10,
  560. TDEFL_MAX_HUFF_SYMBOLS = 288,
  561. TDEFL_LZ_HASH_BITS = 15,
  562. TDEFL_LEVEL1_HASH_SIZE_MASK = 4095,
  563. TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3,
  564. TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS
  565. };
  566. #endif
  567. /* 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. */
  568. typedef enum {
  569. TDEFL_STATUS_BAD_PARAM = -2,
  570. TDEFL_STATUS_PUT_BUF_FAILED = -1,
  571. TDEFL_STATUS_OKAY = 0,
  572. TDEFL_STATUS_DONE = 1
  573. } tdefl_status;
  574. /* Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums */
  575. typedef enum {
  576. TDEFL_NO_FLUSH = 0,
  577. TDEFL_SYNC_FLUSH = 2,
  578. TDEFL_FULL_FLUSH = 3,
  579. TDEFL_FINISH = 4
  580. } tdefl_flush;
  581. /* tdefl's compression state structure. */
  582. typedef struct
  583. {
  584. tdefl_put_buf_func_ptr m_pPut_buf_func;
  585. void *m_pPut_buf_user;
  586. mz_uint m_flags, m_max_probes[2];
  587. int m_greedy_parsing;
  588. mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
  589. mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
  590. mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer;
  591. 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;
  592. tdefl_status m_prev_return_status;
  593. const void *m_pIn_buf;
  594. void *m_pOut_buf;
  595. size_t *m_pIn_buf_size, *m_pOut_buf_size;
  596. tdefl_flush m_flush;
  597. const mz_uint8 *m_pSrc;
  598. size_t m_src_buf_left, m_out_buf_ofs;
  599. mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
  600. mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
  601. mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
  602. mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
  603. mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
  604. mz_uint16 m_next[TDEFL_LZ_DICT_SIZE];
  605. mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
  606. mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
  607. } tdefl_compressor;
  608. /* Initializes the compressor. */
  609. /* There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory. */
  610. /* 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. */
  611. /* If pBut_buf_func is NULL the user should always call the tdefl_compress() API. */
  612. /* flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.) */
  613. MINIZ_EXPORT tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
  614. /* 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. */
  615. 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);
  616. /* tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr. */
  617. /* tdefl_compress_buffer() always consumes the entire input buffer. */
  618. MINIZ_EXPORT tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush);
  619. MINIZ_EXPORT tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d);
  620. MINIZ_EXPORT mz_uint32 tdefl_get_adler32(tdefl_compressor *d);
  621. /* Create tdefl_compress() flags given zlib-style compression parameters. */
  622. /* level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files) */
  623. /* window_bits may be -15 (raw deflate) or 15 (zlib) */
  624. /* strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED */
  625. MINIZ_EXPORT mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy);
  626. #ifndef MINIZ_NO_MALLOC
  627. /* Allocate the tdefl_compressor structure in C so that */
  628. /* non-C language bindings to tdefl_ API don't need to worry about */
  629. /* structure size and allocation mechanism. */
  630. MINIZ_EXPORT tdefl_compressor *tdefl_compressor_alloc(void);
  631. MINIZ_EXPORT void tdefl_compressor_free(tdefl_compressor *pComp);
  632. #endif
  633. #ifdef __cplusplus
  634. }
  635. #endif
  636. #pragma once
  637. /* ------------------- Low-level Decompression API Definitions */
  638. #ifdef __cplusplus
  639. extern "C" {
  640. #endif
  641. /* Decompression flags used by tinfl_decompress(). */
  642. /* 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. */
  643. /* 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. */
  644. /* 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). */
  645. /* TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes. */
  646. enum
  647. {
  648. TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
  649. TINFL_FLAG_HAS_MORE_INPUT = 2,
  650. TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
  651. TINFL_FLAG_COMPUTE_ADLER32 = 8
  652. };
  653. /* High level decompression functions: */
  654. /* tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc(). */
  655. /* On entry: */
  656. /* pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress. */
  657. /* On return: */
  658. /* Function returns a pointer to the decompressed data, or NULL on failure. */
  659. /* *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data. */
  660. /* The caller must call mz_free() on the returned block when it's no longer needed. */
  661. MINIZ_EXPORT void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
  662. /* tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory. */
  663. /* Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success. */
  664. #define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
  665. 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);
  666. /* 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. */
  667. /* Returns 1 on success or 0 on failure. */
  668. typedef int (*tinfl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser);
  669. 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);
  670. struct tinfl_decompressor_tag;
  671. typedef struct tinfl_decompressor_tag tinfl_decompressor;
  672. #ifndef MINIZ_NO_MALLOC
  673. /* Allocate the tinfl_decompressor structure in C so that */
  674. /* non-C language bindings to tinfl_ API don't need to worry about */
  675. /* structure size and allocation mechanism. */
  676. MINIZ_EXPORT tinfl_decompressor *tinfl_decompressor_alloc(void);
  677. MINIZ_EXPORT void tinfl_decompressor_free(tinfl_decompressor *pDecomp);
  678. #endif
  679. /* Max size of LZ dictionary. */
  680. #define TINFL_LZ_DICT_SIZE 32768
  681. /* Return status. */
  682. typedef enum {
  683. /* 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 */
  684. /* 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). */
  685. /* If you call it again with no input you'll just get TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS again. */
  686. TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4,
  687. /* 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.) */
  688. TINFL_STATUS_BAD_PARAM = -3,
  689. /* 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. */
  690. TINFL_STATUS_ADLER32_MISMATCH = -2,
  691. /* 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. */
  692. TINFL_STATUS_FAILED = -1,
  693. /* Any status code less than TINFL_STATUS_DONE must indicate a failure. */
  694. /* 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 */
  695. /* 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. */
  696. TINFL_STATUS_DONE = 0,
  697. /* 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 */
  698. /* 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 */
  699. /* proceed, so be sure to properly set the TINFL_FLAG_HAS_MORE_INPUT flag. */
  700. TINFL_STATUS_NEEDS_MORE_INPUT = 1,
  701. /* 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. */
  702. /* 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 */
  703. /* (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 */
  704. /* so I may need to add some code to address this. */
  705. TINFL_STATUS_HAS_MORE_OUTPUT = 2
  706. } tinfl_status;
  707. /* Initializes the decompressor to its initial state. */
  708. #define tinfl_init(r) \
  709. do \
  710. { \
  711. (r)->m_state = 0; \
  712. } \
  713. MZ_MACRO_END
  714. #define tinfl_get_adler32(r) (r)->m_check_adler32
  715. /* 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. */
  716. /* 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. */
  717. 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);
  718. /* Internal/private bits follow. */
  719. enum
  720. {
  721. TINFL_MAX_HUFF_TABLES = 3,
  722. TINFL_MAX_HUFF_SYMBOLS_0 = 288,
  723. TINFL_MAX_HUFF_SYMBOLS_1 = 32,
  724. TINFL_MAX_HUFF_SYMBOLS_2 = 19,
  725. TINFL_FAST_LOOKUP_BITS = 10,
  726. TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
  727. };
  728. typedef struct
  729. {
  730. mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
  731. mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
  732. } tinfl_huff_table;
  733. #if MINIZ_HAS_64BIT_REGISTERS
  734. #define TINFL_USE_64BIT_BITBUF 1
  735. #else
  736. #define TINFL_USE_64BIT_BITBUF 0
  737. #endif
  738. #if TINFL_USE_64BIT_BITBUF
  739. typedef mz_uint64 tinfl_bit_buf_t;
  740. #define TINFL_BITBUF_SIZE (64)
  741. #else
  742. typedef mz_uint32 tinfl_bit_buf_t;
  743. #define TINFL_BITBUF_SIZE (32)
  744. #endif
  745. struct tinfl_decompressor_tag
  746. {
  747. 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];
  748. tinfl_bit_buf_t m_bit_buf;
  749. size_t m_dist_from_out_buf_start;
  750. tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
  751. mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
  752. };
  753. #ifdef __cplusplus
  754. }
  755. #endif
  756. #pragma once
  757. /* ------------------- ZIP archive reading/writing */
  758. #ifndef MINIZ_NO_ARCHIVE_APIS
  759. #ifdef __cplusplus
  760. extern "C" {
  761. #endif
  762. enum
  763. {
  764. /* Note: These enums can be reduced as needed to save memory or stack space - they are pretty conservative. */
  765. MZ_ZIP_MAX_IO_BUF_SIZE = 64 * 1024,
  766. MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 512,
  767. MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 512
  768. };
  769. typedef struct
  770. {
  771. /* Central directory file index. */
  772. mz_uint32 m_file_index;
  773. /* 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. */
  774. mz_uint64 m_central_dir_ofs;
  775. /* These fields are copied directly from the zip's central dir. */
  776. mz_uint16 m_version_made_by;
  777. mz_uint16 m_version_needed;
  778. mz_uint16 m_bit_flag;
  779. mz_uint16 m_method;
  780. #ifndef MINIZ_NO_TIME
  781. MZ_TIME_T m_time;
  782. #endif
  783. /* CRC-32 of uncompressed data. */
  784. mz_uint32 m_crc32;
  785. /* File's compressed size. */
  786. mz_uint64 m_comp_size;
  787. /* 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. */
  788. mz_uint64 m_uncomp_size;
  789. /* Zip internal and external file attributes. */
  790. mz_uint16 m_internal_attr;
  791. mz_uint32 m_external_attr;
  792. /* Entry's local header file offset in bytes. */
  793. mz_uint64 m_local_header_ofs;
  794. /* Size of comment in bytes. */
  795. mz_uint32 m_comment_size;
  796. /* MZ_TRUE if the entry appears to be a directory. */
  797. mz_bool m_is_directory;
  798. /* MZ_TRUE if the entry uses encryption/strong encryption (which miniz_zip doesn't support) */
  799. mz_bool m_is_encrypted;
  800. /* MZ_TRUE if the file is not encrypted, a patch file, and if it uses a compression method we support. */
  801. mz_bool m_is_supported;
  802. /* Filename. If string ends in '/' it's a subdirectory entry. */
  803. /* Guaranteed to be zero terminated, may be truncated to fit. */
  804. char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
  805. /* Comment field. */
  806. /* Guaranteed to be zero terminated, may be truncated to fit. */
  807. char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
  808. } mz_zip_archive_file_stat;
  809. typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n);
  810. typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n);
  811. typedef mz_bool (*mz_file_needs_keepalive)(void *pOpaque);
  812. struct mz_zip_internal_state_tag;
  813. typedef struct mz_zip_internal_state_tag mz_zip_internal_state;
  814. typedef enum {
  815. MZ_ZIP_MODE_INVALID = 0,
  816. MZ_ZIP_MODE_READING = 1,
  817. MZ_ZIP_MODE_WRITING = 2,
  818. MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3
  819. } mz_zip_mode;
  820. typedef enum {
  821. MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100,
  822. MZ_ZIP_FLAG_IGNORE_PATH = 0x0200,
  823. MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400,
  824. MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800,
  825. 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) */
  826. MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY = 0x2000, /* validate the local headers, but don't decompress the entire file and check the crc32 */
  827. 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 */
  828. MZ_ZIP_FLAG_WRITE_ALLOW_READING = 0x8000,
  829. MZ_ZIP_FLAG_ASCII_FILENAME = 0x10000,
  830. /*After adding a compressed file, seek back
  831. to local file header and set the correct sizes*/
  832. MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE = 0x20000
  833. } mz_zip_flags;
  834. typedef enum {
  835. MZ_ZIP_TYPE_INVALID = 0,
  836. MZ_ZIP_TYPE_USER,
  837. MZ_ZIP_TYPE_MEMORY,
  838. MZ_ZIP_TYPE_HEAP,
  839. MZ_ZIP_TYPE_FILE,
  840. MZ_ZIP_TYPE_CFILE,
  841. MZ_ZIP_TOTAL_TYPES
  842. } mz_zip_type;
  843. /* miniz error codes. Be sure to update mz_zip_get_error_string() if you add or modify this enum. */
  844. typedef enum {
  845. MZ_ZIP_NO_ERROR = 0,
  846. MZ_ZIP_UNDEFINED_ERROR,
  847. MZ_ZIP_TOO_MANY_FILES,
  848. MZ_ZIP_FILE_TOO_LARGE,
  849. MZ_ZIP_UNSUPPORTED_METHOD,
  850. MZ_ZIP_UNSUPPORTED_ENCRYPTION,
  851. MZ_ZIP_UNSUPPORTED_FEATURE,
  852. MZ_ZIP_FAILED_FINDING_CENTRAL_DIR,
  853. MZ_ZIP_NOT_AN_ARCHIVE,
  854. MZ_ZIP_INVALID_HEADER_OR_CORRUPTED,
  855. MZ_ZIP_UNSUPPORTED_MULTIDISK,
  856. MZ_ZIP_DECOMPRESSION_FAILED,
  857. MZ_ZIP_COMPRESSION_FAILED,
  858. MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE,
  859. MZ_ZIP_CRC_CHECK_FAILED,
  860. MZ_ZIP_UNSUPPORTED_CDIR_SIZE,
  861. MZ_ZIP_ALLOC_FAILED,
  862. MZ_ZIP_FILE_OPEN_FAILED,
  863. MZ_ZIP_FILE_CREATE_FAILED,
  864. MZ_ZIP_FILE_WRITE_FAILED,
  865. MZ_ZIP_FILE_READ_FAILED,
  866. MZ_ZIP_FILE_CLOSE_FAILED,
  867. MZ_ZIP_FILE_SEEK_FAILED,
  868. MZ_ZIP_FILE_STAT_FAILED,
  869. MZ_ZIP_INVALID_PARAMETER,
  870. MZ_ZIP_INVALID_FILENAME,
  871. MZ_ZIP_BUF_TOO_SMALL,
  872. MZ_ZIP_INTERNAL_ERROR,
  873. MZ_ZIP_FILE_NOT_FOUND,
  874. MZ_ZIP_ARCHIVE_TOO_LARGE,
  875. MZ_ZIP_VALIDATION_FAILED,
  876. MZ_ZIP_WRITE_CALLBACK_FAILED,
  877. MZ_ZIP_TOTAL_ERRORS
  878. } mz_zip_error;
  879. typedef struct
  880. {
  881. mz_uint64 m_archive_size;
  882. mz_uint64 m_central_directory_file_ofs;
  883. /* We only support up to UINT32_MAX files in zip64 mode. */
  884. mz_uint32 m_total_files;
  885. mz_zip_mode m_zip_mode;
  886. mz_zip_type m_zip_type;
  887. mz_zip_error m_last_error;
  888. mz_uint64 m_file_offset_alignment;
  889. mz_alloc_func m_pAlloc;
  890. mz_free_func m_pFree;
  891. mz_realloc_func m_pRealloc;
  892. void *m_pAlloc_opaque;
  893. mz_file_read_func m_pRead;
  894. mz_file_write_func m_pWrite;
  895. mz_file_needs_keepalive m_pNeeds_keepalive;
  896. void *m_pIO_opaque;
  897. mz_zip_internal_state *m_pState;
  898. } mz_zip_archive;
  899. typedef struct
  900. {
  901. mz_zip_archive *pZip;
  902. mz_uint flags;
  903. int status;
  904. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  905. mz_uint file_crc32;
  906. #endif
  907. mz_uint64 read_buf_size, read_buf_ofs, read_buf_avail, comp_remaining, out_buf_ofs, cur_file_ofs;
  908. mz_zip_archive_file_stat file_stat;
  909. void *pRead_buf;
  910. void *pWrite_buf;
  911. size_t out_blk_remain;
  912. tinfl_decompressor inflator;
  913. } mz_zip_reader_extract_iter_state;
  914. /* -------- ZIP reading */
  915. /* Inits a ZIP archive reader. */
  916. /* These functions read and validate the archive's central directory. */
  917. MINIZ_EXPORT mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint flags);
  918. MINIZ_EXPORT mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint flags);
  919. #ifndef MINIZ_NO_STDIO
  920. /* Read a archive from a disk file. */
  921. /* file_start_ofs is the file offset where the archive actually begins, or 0. */
  922. /* 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. */
  923. MINIZ_EXPORT mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags);
  924. 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);
  925. /* Read an archive from an already opened FILE, beginning at the current file position. */
  926. /* 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. */
  927. /* The FILE will NOT be closed when mz_zip_reader_end() is called. */
  928. MINIZ_EXPORT mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint64 archive_size, mz_uint flags);
  929. #endif
  930. /* Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was used. */
  931. MINIZ_EXPORT mz_bool mz_zip_reader_end(mz_zip_archive *pZip);
  932. /* -------- ZIP reading or writing */
  933. /* Clears a mz_zip_archive struct to all zeros. */
  934. /* Important: This must be done before passing the struct to any mz_zip functions. */
  935. MINIZ_EXPORT void mz_zip_zero_struct(mz_zip_archive *pZip);
  936. MINIZ_EXPORT mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip);
  937. MINIZ_EXPORT mz_zip_type mz_zip_get_type(mz_zip_archive *pZip);
  938. /* Returns the total number of files in the archive. */
  939. MINIZ_EXPORT mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip);
  940. MINIZ_EXPORT mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip);
  941. MINIZ_EXPORT mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip);
  942. MINIZ_EXPORT MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip);
  943. /* Reads n bytes of raw archive data, starting at file offset file_ofs, to pBuf. */
  944. MINIZ_EXPORT size_t mz_zip_read_archive_data(mz_zip_archive *pZip, mz_uint64 file_ofs, void *pBuf, size_t n);
  945. /* All mz_zip funcs set the m_last_error field in the mz_zip_archive struct. These functions retrieve/manipulate this field. */
  946. /* Note that the m_last_error functionality is not thread safe. */
  947. MINIZ_EXPORT mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, mz_zip_error err_num);
  948. MINIZ_EXPORT mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip);
  949. MINIZ_EXPORT mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip);
  950. MINIZ_EXPORT mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip);
  951. MINIZ_EXPORT const char *mz_zip_get_error_string(mz_zip_error mz_err);
  952. /* MZ_TRUE if the archive file entry is a directory entry. */
  953. MINIZ_EXPORT mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index);
  954. /* MZ_TRUE if the file is encrypted/strong encrypted. */
  955. MINIZ_EXPORT mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index);
  956. /* MZ_TRUE if the compression method is supported, and the file is not encrypted, and the file is not a compressed patch file. */
  957. MINIZ_EXPORT mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip, mz_uint file_index);
  958. /* Retrieves the filename of an archive file entry. */
  959. /* 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. */
  960. MINIZ_EXPORT mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size);
  961. /* Attempts to locates a file in the archive's central directory. */
  962. /* Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH */
  963. /* Returns -1 if the file cannot be found. */
  964. MINIZ_EXPORT int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags);
  965. 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);
  966. /* Returns detailed information about an archive file entry. */
  967. MINIZ_EXPORT mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat);
  968. /* MZ_TRUE if the file is in zip64 format. */
  969. /* 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. */
  970. MINIZ_EXPORT mz_bool mz_zip_is_zip64(mz_zip_archive *pZip);
  971. /* Returns the total central directory size in bytes. */
  972. /* The current max supported size is <= MZ_UINT32_MAX. */
  973. MINIZ_EXPORT size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip);
  974. /* Extracts a archive file to a memory buffer using no memory allocation. */
  975. /* There must be at least enough room on the stack to store the inflator's state (~34KB or so). */
  976. 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);
  977. 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);
  978. /* Extracts a archive file to a memory buffer. */
  979. 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);
  980. 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);
  981. /* Extracts a archive file to a dynamically allocated heap buffer. */
  982. /* The memory will be allocated via the mz_zip_archive's alloc/realloc functions. */
  983. /* Returns NULL and sets the last error on failure. */
  984. MINIZ_EXPORT void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags);
  985. MINIZ_EXPORT void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags);
  986. /* Extracts a archive file using a callback function to output the file's data. */
  987. 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);
  988. 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);
  989. /* Extract a file iteratively */
  990. 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);
  991. 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);
  992. MINIZ_EXPORT size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state* pState, void* pvBuf, size_t buf_size);
  993. MINIZ_EXPORT mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state* pState);
  994. #ifndef MINIZ_NO_STDIO
  995. /* Extracts a archive file to a disk file and sets its last accessed and modified times. */
  996. /* This function only extracts files, not archive directory records. */
  997. 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);
  998. 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);
  999. /* Extracts a archive file starting at the current position in the destination FILE stream. */
  1000. MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip, mz_uint file_index, MZ_FILE *File, mz_uint flags);
  1001. 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);
  1002. #endif
  1003. #if 0
  1004. /* TODO */
  1005. typedef void *mz_zip_streaming_extract_state_ptr;
  1006. mz_zip_streaming_extract_state_ptr mz_zip_streaming_extract_begin(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags);
  1007. uint64_t mz_zip_streaming_extract_get_size(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
  1008. uint64_t mz_zip_streaming_extract_get_cur_ofs(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
  1009. mz_bool mz_zip_streaming_extract_seek(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, uint64_t new_ofs);
  1010. size_t mz_zip_streaming_extract_read(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, void *pBuf, size_t buf_size);
  1011. mz_bool mz_zip_streaming_extract_end(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState);
  1012. #endif
  1013. /* 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. */
  1014. /* It also validates that each file can be successfully uncompressed unless the MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY is specified. */
  1015. MINIZ_EXPORT mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags);
  1016. /* Validates an entire archive by calling mz_zip_validate_file() on each file. */
  1017. MINIZ_EXPORT mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags);
  1018. /* Misc utils/helpers, valid for ZIP reading or writing */
  1019. MINIZ_EXPORT mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size, mz_uint flags, mz_zip_error *pErr);
  1020. MINIZ_EXPORT mz_bool mz_zip_validate_file_archive(const char *pFilename, mz_uint flags, mz_zip_error *pErr);
  1021. /* Universal end function - calls either mz_zip_reader_end() or mz_zip_writer_end(). */
  1022. MINIZ_EXPORT mz_bool mz_zip_end(mz_zip_archive *pZip);
  1023. /* -------- ZIP writing */
  1024. #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
  1025. /* Inits a ZIP archive writer. */
  1026. /*Set pZip->m_pWrite (and pZip->m_pIO_opaque) before calling mz_zip_writer_init or mz_zip_writer_init_v2*/
  1027. /*The output is streamable, i.e. file_ofs in mz_file_write_func always increases only by n*/
  1028. MINIZ_EXPORT mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size);
  1029. MINIZ_EXPORT mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip, mz_uint64 existing_size, mz_uint flags);
  1030. 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);
  1031. 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);
  1032. #ifndef MINIZ_NO_STDIO
  1033. MINIZ_EXPORT mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning);
  1034. 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);
  1035. MINIZ_EXPORT mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint flags);
  1036. #endif
  1037. /* Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an existing archive. */
  1038. /* 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. */
  1039. /* 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). */
  1040. /* Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be NULL. */
  1041. /* Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops or something goes wrong before */
  1042. /* the archive is finalized the file's central directory will be hosed. */
  1043. MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename);
  1044. MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags);
  1045. /* Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive. */
  1046. /* To add a directory entry, call this method with an archive name ending in a forwardslash with an empty buffer. */
  1047. /* 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. */
  1048. 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);
  1049. /* Like mz_zip_writer_add_mem(), except you can specify a file comment field, and optionally supply the function with already compressed data. */
  1050. /* uncomp_size/uncomp_crc32 are only used if the MZ_ZIP_FLAG_COMPRESSED_DATA flag is specified. */
  1051. 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,
  1052. mz_uint64 uncomp_size, mz_uint32 uncomp_crc32);
  1053. 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,
  1054. 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,
  1055. const char *user_extra_data_central, mz_uint user_extra_data_central_len);
  1056. /* Adds the contents of a file to an archive. This function also records the disk file's modified time into the archive. */
  1057. /* File data is supplied via a read callback function. User mz_zip_writer_add_(c)file to add a file directly.*/
  1058. 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,
  1059. 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,
  1060. const char *user_extra_data_central, mz_uint user_extra_data_central_len);
  1061. #ifndef MINIZ_NO_STDIO
  1062. /* Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive. */
  1063. /* 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. */
  1064. 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);
  1065. /* Like mz_zip_writer_add_file(), except the file data is read from the specified FILE stream. */
  1066. 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,
  1067. 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,
  1068. const char *user_extra_data_central, mz_uint user_extra_data_central_len);
  1069. #endif
  1070. /* Adds a file to an archive by fully cloning the data from another archive. */
  1071. /* 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. */
  1072. 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);
  1073. /* Finalizes the archive by writing the central directory records followed by the end of central directory record. */
  1074. /* After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end(). */
  1075. /* An archive must be manually finalized by calling this function for it to be valid. */
  1076. MINIZ_EXPORT mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip);
  1077. /* Finalizes a heap archive, returning a poiner to the heap block and its size. */
  1078. /* The heap block will be allocated using the mz_zip_archive's alloc/realloc callbacks. */
  1079. MINIZ_EXPORT mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **ppBuf, size_t *pSize);
  1080. /* Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used. */
  1081. /* Note for the archive to be valid, it *must* have been finalized before ending (this function will not do it for you). */
  1082. MINIZ_EXPORT mz_bool mz_zip_writer_end(mz_zip_archive *pZip);
  1083. /* -------- Misc. high-level helper functions: */
  1084. /* mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive. */
  1085. /* 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). */
  1086. /* 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. */
  1087. /* 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. */
  1088. 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);
  1089. 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);
  1090. /* Reads a single file from an archive into a heap block. */
  1091. /* If pComment is not NULL, only the file with the specified comment will be extracted. */
  1092. /* Returns NULL on failure. */
  1093. MINIZ_EXPORT void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint flags);
  1094. 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);
  1095. #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */
  1096. #ifdef __cplusplus
  1097. }
  1098. #endif
  1099. #endif /* MINIZ_NO_ARCHIVE_APIS */