| 1 | #ifndef MINIZ_EXPORT |
| 2 | #define MINIZ_EXPORT |
| 3 | #endif |
| 4 | /* miniz.c 3.0.2 - public domain deflate/inflate, zlib-subset, ZIP |
| 5 | reading/writing/appending, PNG writing See "unlicense" statement at the end |
| 6 | of this file. Rich Geldreich <[email protected]>, last updated Oct. 13, |
| 7 | 2013 Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: |
| 8 | http://www.ietf.org/rfc/rfc1951.txt |
| 9 | |
| 10 | Most API's defined in miniz.c are optional. For example, to disable the |
| 11 | archive related functions just define MINIZ_NO_ARCHIVE_APIS, or to get rid of |
| 12 | all stdio usage define MINIZ_NO_STDIO (see the list below for more macros). |
| 13 | |
| 14 | * Low-level Deflate/Inflate implementation notes: |
| 15 | |
| 16 | Compression: Use the "tdefl" API's. The compressor supports raw, static, |
| 17 | and dynamic blocks, lazy or greedy parsing, match length filtering, RLE-only, |
| 18 | and Huffman-only streams. It performs and compresses approximately as well as |
| 19 | zlib. |
| 20 | |
| 21 | Decompression: Use the "tinfl" API's. The entire decompressor is |
| 22 | implemented as a single function coroutine: see tinfl_decompress(). It |
| 23 | supports decompression into a 32KB (or larger power of 2) wrapping buffer, or |
| 24 | into a memory block large enough to hold the entire file. |
| 25 | |
| 26 | The low-level tdefl/tinfl API's do not make any use of dynamic memory |
| 27 | allocation. |
| 28 | |
| 29 | * zlib-style API notes: |
| 30 | |
| 31 | miniz.c implements a fairly large subset of zlib. There's enough |
| 32 | functionality present for it to be a drop-in zlib replacement in many apps: |
| 33 | The z_stream struct, optional memory allocation callbacks |
| 34 | deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound |
| 35 | inflateInit/inflateInit2/inflate/inflateReset/inflateEnd |
| 36 | compress, compress2, compressBound, uncompress |
| 37 | CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly |
| 38 | routines. Supports raw deflate streams or standard zlib streams with adler-32 |
| 39 | checking. |
| 40 | |
| 41 | Limitations: |
| 42 | The callback API's are not implemented yet. No support for gzip headers or |
| 43 | zlib static dictionaries. I've tried to closely emulate zlib's various |
| 44 | flavors of stream flushing and return status codes, but there are no |
| 45 | guarantees that miniz.c pulls this off perfectly. |
| 46 | |
| 47 | * PNG writing: See the tdefl_write_image_to_png_file_in_memory() function, |
| 48 | originally written by Alex Evans. Supports 1-4 bytes/pixel images. |
| 49 | |
| 50 | * ZIP archive API notes: |
| 51 | |
| 52 | The ZIP archive API's where designed with simplicity and efficiency in |
| 53 | mind, with just enough abstraction to get the job done with minimal fuss. |
| 54 | There are simple API's to retrieve file information, read files from existing |
| 55 | archives, create new archives, append new files to existing archives, or |
| 56 | clone archive data from one archive to another. It supports archives located |
| 57 | in memory or the heap, on disk (using stdio.h), or you can specify custom |
| 58 | file read/write callbacks. |
| 59 | |
| 60 | - Archive reading: Just call this function to read a single file from a |
| 61 | disk archive: |
| 62 | |
| 63 | void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const |
| 64 | char *pArchive_name, size_t *pSize, mz_uint zip_flags); |
| 65 | |
| 66 | For more complex cases, use the "mz_zip_reader" functions. Upon opening an |
| 67 | archive, the entire central directory is located and read as-is into memory, |
| 68 | and subsequent file access only occurs when reading individual files. |
| 69 | |
| 70 | - Archives file scanning: The simple way is to use this function to scan a |
| 71 | loaded archive for a specific file: |
| 72 | |
| 73 | int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, |
| 74 | const char *pComment, mz_uint flags); |
| 75 | |
| 76 | The locate operation can optionally check file comments too, which (as one |
| 77 | example) can be used to identify multiple versions of the same file in an |
| 78 | archive. This function uses a simple linear search through the central |
| 79 | directory, so it's not very fast. |
| 80 | |
| 81 | Alternately, you can iterate through all the files in an archive (using |
| 82 | mz_zip_reader_get_num_files()) and retrieve detailed info on each file by |
| 83 | calling mz_zip_reader_file_stat(). |
| 84 | |
| 85 | - Archive creation: Use the "mz_zip_writer" functions. The ZIP writer |
| 86 | immediately writes compressed file data to disk and builds an exact image of |
| 87 | the central directory in memory. The central directory image is written all |
| 88 | at once at the end of the archive file when the archive is finalized. |
| 89 | |
| 90 | The archive writer can optionally align each file's local header and file |
| 91 | data to any power of 2 alignment, which can be useful when the archive will |
| 92 | be read from optical media. Also, the writer supports placing arbitrary data |
| 93 | blobs at the very beginning of ZIP archives. Archives written using either |
| 94 | feature are still readable by any ZIP tool. |
| 95 | |
| 96 | - Archive appending: The simple way to add a single file to an archive is |
| 97 | to call this function: |
| 98 | |
| 99 | mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, |
| 100 | const char *pArchive_name, const void *pBuf, size_t buf_size, const void |
| 101 | *pComment, mz_uint16 comment_size, mz_uint level_and_flags); |
| 102 | |
| 103 | The archive will be created if it doesn't already exist, otherwise it'll be |
| 104 | appended to. Note the appending is done in-place and is not an atomic |
| 105 | operation, so if something goes wrong during the operation it's possible the |
| 106 | archive could be left without a central directory (although the local file |
| 107 | headers and file data will be fine, so the archive will be recoverable). |
| 108 | |
| 109 | For more complex archive modification scenarios: |
| 110 | 1. The safest way is to use a mz_zip_reader to read the existing archive, |
| 111 | cloning only those bits you want to preserve into a new archive using using |
| 112 | the mz_zip_writer_add_from_zip_reader() function (which compiles the |
| 113 | compressed file data as-is). When you're done, delete the old archive and |
| 114 | rename the newly written archive, and you're done. This is safe but requires |
| 115 | a bunch of temporary disk space or heap memory. |
| 116 | |
| 117 | 2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using |
| 118 | mz_zip_writer_init_from_reader(), append new files as needed, then finalize |
| 119 | the archive which will write an updated central directory to the original |
| 120 | archive. (This is basically what mz_zip_add_mem_to_archive_file_in_place() |
| 121 | does.) There's a possibility that the archive's central directory could be |
| 122 | lost with this method if anything goes wrong, though. |
| 123 | |
| 124 | - ZIP archive support limitations: |
| 125 | No spanning support. Extraction functions can only handle unencrypted, |
| 126 | stored or deflated files. Requires streams capable of seeking. |
| 127 | |
| 128 | * This is a header file library, like stb_image.c. To get only a header file, |
| 129 | either cut and paste the below header, or create miniz.h, #define |
| 130 | MINIZ_HEADER_FILE_ONLY, and then include miniz.c from it. |
| 131 | |
| 132 | * Important: For best perf. be sure to customize the below macros for your |
| 133 | target platform: #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 #define |
| 134 | MINIZ_LITTLE_ENDIAN 1 #define MINIZ_HAS_64BIT_REGISTERS 1 |
| 135 | |
| 136 | * On platforms using glibc, Be sure to "#define _LARGEFILE64_SOURCE 1" before |
| 137 | including miniz.c to ensure miniz uses the 64-bit variants: fopen64(), |
| 138 | stat64(), etc. Otherwise you won't be able to process large files (i.e. |
| 139 | 32-bit stat() fails for me on files > 0x7FFFFFFF bytes). |
| 140 | */ |
| 141 | #pragma once |
| 142 | |
| 143 | /* Defines to completely disable specific portions of miniz.c: |
| 144 | If all macros here are defined the only functionality remaining will be |
| 145 | CRC-32 and adler-32. */ |
| 146 | |
| 147 | /* Define MINIZ_NO_STDIO to disable all usage and any functions which rely on |
| 148 | * stdio for file I/O. */ |
| 149 | /*#define MINIZ_NO_STDIO */ |
| 150 | |
| 151 | /* If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able |
| 152 | * to get the current time, or */ |
| 153 | /* get/set file times, and the C run-time funcs that get/set times won't be |
| 154 | * called. */ |
| 155 | /* The current downside is the times written to your archives will be from 1979. |
| 156 | */ |
| 157 | /*#define MINIZ_NO_TIME */ |
| 158 | |
| 159 | /* Define MINIZ_NO_DEFLATE_APIS to disable all compression API's. */ |
| 160 | /*#define MINIZ_NO_DEFLATE_APIS */ |
| 161 | |
| 162 | /* Define MINIZ_NO_INFLATE_APIS to disable all decompression API's. */ |
| 163 | /*#define MINIZ_NO_INFLATE_APIS */ |
| 164 | |
| 165 | /* Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. */ |
| 166 | /*#define MINIZ_NO_ARCHIVE_APIS */ |
| 167 | |
| 168 | /* Define MINIZ_NO_ARCHIVE_WRITING_APIS to disable all writing related ZIP |
| 169 | * archive API's. */ |
| 170 | /*#define MINIZ_NO_ARCHIVE_WRITING_APIS */ |
| 171 | |
| 172 | /* Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression |
| 173 | * API's. */ |
| 174 | /*#define MINIZ_NO_ZLIB_APIS */ |
| 175 | |
| 176 | /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent |
| 177 | * conflicts against stock zlib. */ |
| 178 | /*#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES */ |
| 179 | |
| 180 | /* Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc. |
| 181 | Note if MINIZ_NO_MALLOC is defined then the user must always provide custom |
| 182 | user alloc/free/realloc callbacks to the zlib and archive API's, and a few |
| 183 | stand-alone helper API's which don't provide custom user functions (such as |
| 184 | tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work. |
| 185 | */ |
| 186 | /*#define MINIZ_NO_MALLOC */ |
| 187 | |
| 188 | #ifdef MINIZ_NO_INFLATE_APIS |
| 189 | #define MINIZ_NO_ARCHIVE_APIS |
| 190 | #endif |
| 191 | |
| 192 | #ifdef MINIZ_NO_DEFLATE_APIS |
| 193 | #define MINIZ_NO_ARCHIVE_WRITING_APIS |
| 194 | #endif |
| 195 | |
| 196 | #if defined(__TINYC__) && (defined(__linux) || defined(__linux__)) |
| 197 | /* TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc |
| 198 | * on Linux */ |
| 199 | #define MINIZ_NO_TIME |
| 200 | #endif |
| 201 | |
| 202 | #include <stddef.h> |
| 203 | |
| 204 | #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS) |
| 205 | #include <time.h> |
| 206 | #endif |
| 207 | |
| 208 | #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || \ |
| 209 | defined(__i386) || defined(__i486__) || defined(__i486) || \ |
| 210 | defined(i386) || defined(__ia64__) || defined(__x86_64__) |
| 211 | /* MINIZ_X86_OR_X64_CPU is only used to help set the below macros. */ |
| 212 | #define MINIZ_X86_OR_X64_CPU 1 |
| 213 | #else |
| 214 | #define MINIZ_X86_OR_X64_CPU 0 |
| 215 | #endif |
| 216 | |
| 217 | /* Set MINIZ_LITTLE_ENDIAN only if not set */ |
| 218 | #if !defined(MINIZ_LITTLE_ENDIAN) |
| 219 | #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) |
| 220 | |
| 221 | #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) |
| 222 | /* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */ |
| 223 | #define MINIZ_LITTLE_ENDIAN 1 |
| 224 | #else |
| 225 | #define MINIZ_LITTLE_ENDIAN 0 |
| 226 | #endif |
| 227 | |
| 228 | #else |
| 229 | |
| 230 | #if MINIZ_X86_OR_X64_CPU |
| 231 | #define MINIZ_LITTLE_ENDIAN 1 |
| 232 | #else |
| 233 | #define MINIZ_LITTLE_ENDIAN 0 |
| 234 | #endif |
| 235 | |
| 236 | #endif |
| 237 | #endif |
| 238 | |
| 239 | /* Using unaligned loads and stores causes errors when using UBSan */ |
| 240 | #if defined(__has_feature) |
| 241 | #if __has_feature(undefined_behavior_sanitizer) |
| 242 | #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 |
| 243 | #endif |
| 244 | #endif |
| 245 | |
| 246 | /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES only if not set */ |
| 247 | #if !defined(MINIZ_USE_UNALIGNED_LOADS_AND_STORES) |
| 248 | #if MINIZ_X86_OR_X64_CPU |
| 249 | /* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient |
| 250 | * integer loads and stores from unaligned addresses. */ |
| 251 | #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 |
| 252 | #define MINIZ_UNALIGNED_USE_MEMCPY |
| 253 | #else |
| 254 | #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0 |
| 255 | #endif |
| 256 | #endif |
| 257 | |
| 258 | #if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || \ |
| 259 | defined(_LP64) || defined(__LP64__) || defined(__ia64__) || \ |
| 260 | defined(__x86_64__) |
| 261 | /* Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are |
| 262 | * reasonably fast (and don't involve compiler generated calls to helper |
| 263 | * functions). */ |
| 264 | #define MINIZ_HAS_64BIT_REGISTERS 1 |
| 265 | #else |
| 266 | #define MINIZ_HAS_64BIT_REGISTERS 0 |
| 267 | #endif |
| 268 | |
| 269 | #ifdef __cplusplus |
| 270 | extern "C" { |
| 271 | #endif |
| 272 | |
| 273 | /* ------------------- zlib-style API Definitions. */ |
| 274 | |
| 275 | /* For more compatibility with zlib, miniz.c uses unsigned long for some |
| 276 | * parameters/struct members. Beware: mz_ulong can be either 32 or 64-bits! */ |
| 277 | typedef unsigned long mz_ulong; |
| 278 | |
| 279 | /* mz_free() internally uses the MZ_FREE() macro (which by default calls free() |
| 280 | * unless you've modified the MZ_MALLOC macro) to release a block allocated from |
| 281 | * the heap. */ |
| 282 | MINIZ_EXPORT void mz_free(void *p); |
| 283 | |
| 284 | #define MZ_ADLER32_INIT (1) |
| 285 | /* mz_adler32() returns the initial adler-32 value to use when called with |
| 286 | * ptr==NULL. */ |
| 287 | MINIZ_EXPORT mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, |
| 288 | size_t buf_len); |
| 289 | |
| 290 | #define MZ_CRC32_INIT (0) |
| 291 | /* mz_crc32() returns the initial CRC-32 value to use when called with |
| 292 | * ptr==NULL. */ |
| 293 | MINIZ_EXPORT mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, |
| 294 | size_t buf_len); |
| 295 | |
| 296 | /* Compression strategies. */ |
| 297 | enum { |
| 298 | MZ_DEFAULT_STRATEGY = 0, |
| 299 | MZ_FILTERED = 1, |
| 300 | MZ_HUFFMAN_ONLY = 2, |
| 301 | MZ_RLE = 3, |
| 302 | MZ_FIXED = 4 |
| 303 | }; |
| 304 | |
| 305 | /* Method */ |
| 306 | #define MZ_DEFLATED 8 |
| 307 | |
| 308 | /* Heap allocation callbacks. |
| 309 | Note that mz_alloc_func parameter types purposely differ from zlib's: items/size |
| 310 | is size_t, not unsigned long. */ |
| 311 | typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size); |
| 312 | typedef void (*mz_free_func)(void *opaque, void *address); |
| 313 | typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, |
| 314 | size_t size); |
| 315 | |
| 316 | /* Compression levels: 0-9 are the standard zlib-style levels, 10 is best |
| 317 | * possible compression (not zlib compatible, and may be very slow), |
| 318 | * MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL. */ |
| 319 | enum { |
| 320 | MZ_NO_COMPRESSION = 0, |
| 321 | MZ_BEST_SPEED = 1, |
| 322 | MZ_BEST_COMPRESSION = 9, |
| 323 | MZ_UBER_COMPRESSION = 10, |
| 324 | MZ_DEFAULT_LEVEL = 6, |
| 325 | MZ_DEFAULT_COMPRESSION = -1 |
| 326 | }; |
| 327 | |
| 328 | #define MZ_VERSION "11.0.2" |
| 329 | #define MZ_VERNUM 0xB002 |
| 330 | #define MZ_VER_MAJOR 11 |
| 331 | #define MZ_VER_MINOR 2 |
| 332 | #define MZ_VER_REVISION 0 |
| 333 | #define MZ_VER_SUBREVISION 0 |
| 334 | |
| 335 | #ifndef MINIZ_NO_ZLIB_APIS |
| 336 | |
| 337 | /* Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The |
| 338 | * other values are for advanced use (refer to the zlib docs). */ |
| 339 | enum { |
| 340 | MZ_NO_FLUSH = 0, |
| 341 | MZ_PARTIAL_FLUSH = 1, |
| 342 | MZ_SYNC_FLUSH = 2, |
| 343 | MZ_FULL_FLUSH = 3, |
| 344 | MZ_FINISH = 4, |
| 345 | MZ_BLOCK = 5 |
| 346 | }; |
| 347 | |
| 348 | /* Return status codes. MZ_PARAM_ERROR is non-standard. */ |
| 349 | enum { |
| 350 | MZ_OK = 0, |
| 351 | MZ_STREAM_END = 1, |
| 352 | MZ_NEED_DICT = 2, |
| 353 | MZ_ERRNO = -1, |
| 354 | MZ_STREAM_ERROR = -2, |
| 355 | MZ_DATA_ERROR = -3, |
| 356 | MZ_MEM_ERROR = -4, |
| 357 | MZ_BUF_ERROR = -5, |
| 358 | MZ_VERSION_ERROR = -6, |
| 359 | MZ_PARAM_ERROR = -10000 |
| 360 | }; |
| 361 | |
| 362 | /* Window bits */ |
| 363 | #define MZ_DEFAULT_WINDOW_BITS 15 |
| 364 | |
| 365 | struct mz_internal_state; |
| 366 | |
| 367 | /* Compression/decompression stream struct. */ |
| 368 | typedef struct mz_stream_s { |
| 369 | const unsigned char *next_in; /* pointer to next byte to read */ |
| 370 | unsigned int avail_in; /* number of bytes available at next_in */ |
| 371 | mz_ulong total_in; /* total number of bytes consumed so far */ |
| 372 | |
| 373 | unsigned char *next_out; /* pointer to next byte to write */ |
| 374 | unsigned int avail_out; /* number of bytes that can be written to next_out */ |
| 375 | mz_ulong total_out; /* total number of bytes produced so far */ |
| 376 | |
| 377 | char *msg; /* error msg (unused) */ |
| 378 | struct mz_internal_state |
| 379 | *state; /* internal state, allocated by zalloc/zfree */ |
| 380 | |
| 381 | mz_alloc_func |
| 382 | zalloc; /* optional heap allocation function (defaults to malloc) */ |
| 383 | mz_free_func zfree; /* optional heap free function (defaults to free) */ |
| 384 | void *opaque; /* heap alloc function user pointer */ |
| 385 | |
| 386 | int data_type; /* data_type (unused) */ |
| 387 | mz_ulong adler; /* adler32 of the source or uncompressed data */ |
| 388 | mz_ulong reserved; /* not used */ |
| 389 | } mz_stream; |
| 390 | |
| 391 | typedef mz_stream *mz_streamp; |
| 392 | |
| 393 | /* Returns the version string of miniz.c. */ |
| 394 | MINIZ_EXPORT const char *mz_version(void); |
| 395 | |
| 396 | #ifndef MINIZ_NO_DEFLATE_APIS |
| 397 | |
| 398 | /* mz_deflateInit() initializes a compressor with default options: */ |
| 399 | /* Parameters: */ |
| 400 | /* pStream must point to an initialized mz_stream struct. */ |
| 401 | /* level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. */ |
| 402 | /* level 1 enables a specially optimized compression function that's been |
| 403 | * optimized purely for performance, not ratio. */ |
| 404 | /* (This special func. is currently only enabled when |
| 405 | * MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.) */ |
| 406 | /* Return values: */ |
| 407 | /* MZ_OK on success. */ |
| 408 | /* MZ_STREAM_ERROR if the stream is bogus. */ |
| 409 | /* MZ_PARAM_ERROR if the input parameters are bogus. */ |
| 410 | /* MZ_MEM_ERROR on out of memory. */ |
| 411 | MINIZ_EXPORT int mz_deflateInit(mz_streamp pStream, int level); |
| 412 | |
| 413 | /* mz_deflateInit2() is like mz_deflate(), except with more control: */ |
| 414 | /* Additional parameters: */ |
| 415 | /* method must be MZ_DEFLATED */ |
| 416 | /* window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with |
| 417 | * zlib header/adler-32 footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate/no |
| 418 | * header or footer) */ |
| 419 | /* mem_level must be between [1, 9] (it's checked but ignored by miniz.c) */ |
| 420 | MINIZ_EXPORT int mz_deflateInit2(mz_streamp pStream, int level, int method, |
| 421 | int window_bits, int mem_level, int strategy); |
| 422 | |
| 423 | /* Quickly resets a compressor without having to reallocate anything. Same as |
| 424 | * calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2(). */ |
| 425 | MINIZ_EXPORT int mz_deflateReset(mz_streamp pStream); |
| 426 | |
| 427 | /* mz_deflate() compresses the input to output, consuming as much of the input |
| 428 | * and producing as much output as possible. */ |
| 429 | /* Parameters: */ |
| 430 | /* pStream is the stream to read from and write to. You must initialize/update |
| 431 | * the next_in, avail_in, next_out, and avail_out members. */ |
| 432 | /* flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or |
| 433 | * MZ_FINISH. */ |
| 434 | /* Return values: */ |
| 435 | /* MZ_OK on success (when flushing, or if more input is needed but not |
| 436 | * available, and/or there's more output to be written but the output buffer is |
| 437 | * full). */ |
| 438 | /* MZ_STREAM_END if all input has been consumed and all output bytes have been |
| 439 | * written. Don't call mz_deflate() on the stream anymore. */ |
| 440 | /* MZ_STREAM_ERROR if the stream is bogus. */ |
| 441 | /* MZ_PARAM_ERROR if one of the parameters is invalid. */ |
| 442 | /* MZ_BUF_ERROR if no forward progress is possible because the input and/or |
| 443 | * output buffers are empty. (Fill up the input buffer or free up some output |
| 444 | * space and try again.) */ |
| 445 | MINIZ_EXPORT int mz_deflate(mz_streamp pStream, int flush); |
| 446 | |
| 447 | /* mz_deflateEnd() deinitializes a compressor: */ |
| 448 | /* Return values: */ |
| 449 | /* MZ_OK on success. */ |
| 450 | /* MZ_STREAM_ERROR if the stream is bogus. */ |
| 451 | MINIZ_EXPORT int mz_deflateEnd(mz_streamp pStream); |
| 452 | |
| 453 | /* mz_deflateBound() returns a (very) conservative upper bound on the amount of |
| 454 | * data that could be generated by deflate(), assuming flush is set to only |
| 455 | * MZ_NO_FLUSH or MZ_FINISH. */ |
| 456 | MINIZ_EXPORT mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len); |
| 457 | |
| 458 | /* Single-call compression functions mz_compress() and mz_compress2(): */ |
| 459 | /* Returns MZ_OK on success, or one of the error codes from mz_deflate() on |
| 460 | * failure. */ |
| 461 | MINIZ_EXPORT int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, |
| 462 | const unsigned char *pSource, mz_ulong source_len); |
| 463 | MINIZ_EXPORT int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, |
| 464 | const unsigned char *pSource, mz_ulong source_len, |
| 465 | int level); |
| 466 | |
| 467 | /* mz_compressBound() returns a (very) conservative upper bound on the amount of |
| 468 | * data that could be generated by calling mz_compress(). */ |
| 469 | MINIZ_EXPORT mz_ulong mz_compressBound(mz_ulong source_len); |
| 470 | |
| 471 | #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ |
| 472 | |
| 473 | #ifndef MINIZ_NO_INFLATE_APIS |
| 474 | |
| 475 | /* Initializes a decompressor. */ |
| 476 | MINIZ_EXPORT int mz_inflateInit(mz_streamp pStream); |
| 477 | |
| 478 | /* mz_inflateInit2() is like mz_inflateInit() with an additional option that |
| 479 | * controls the window size and whether or not the stream has been wrapped with |
| 480 | * a zlib header/footer: */ |
| 481 | /* window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or |
| 482 | * -MZ_DEFAULT_WINDOW_BITS (raw deflate). */ |
| 483 | MINIZ_EXPORT int mz_inflateInit2(mz_streamp pStream, int window_bits); |
| 484 | |
| 485 | /* Quickly resets a compressor without having to reallocate anything. Same as |
| 486 | * calling mz_inflateEnd() followed by mz_inflateInit()/mz_inflateInit2(). */ |
| 487 | MINIZ_EXPORT int mz_inflateReset(mz_streamp pStream); |
| 488 | |
| 489 | /* Decompresses the input stream to the output, consuming only as much of the |
| 490 | * input as needed, and writing as much to the output as possible. */ |
| 491 | /* Parameters: */ |
| 492 | /* pStream is the stream to read from and write to. You must initialize/update |
| 493 | * the next_in, avail_in, next_out, and avail_out members. */ |
| 494 | /* flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. */ |
| 495 | /* On the first call, if flush is MZ_FINISH it's assumed the input and output |
| 496 | * buffers are both sized large enough to decompress the entire stream in a |
| 497 | * single call (this is slightly faster). */ |
| 498 | /* MZ_FINISH implies that there are no more source bytes available beside |
| 499 | * what's already in the input buffer, and that the output buffer is large |
| 500 | * enough to hold the rest of the decompressed data. */ |
| 501 | /* Return values: */ |
| 502 | /* MZ_OK on success. Either more input is needed but not available, and/or |
| 503 | * there's more output to be written but the output buffer is full. */ |
| 504 | /* MZ_STREAM_END if all needed input has been consumed and all output bytes |
| 505 | * have been written. For zlib streams, the adler-32 of the decompressed data |
| 506 | * has also been verified. */ |
| 507 | /* MZ_STREAM_ERROR if the stream is bogus. */ |
| 508 | /* MZ_DATA_ERROR if the deflate stream is invalid. */ |
| 509 | /* MZ_PARAM_ERROR if one of the parameters is invalid. */ |
| 510 | /* MZ_BUF_ERROR if no forward progress is possible because the input buffer is |
| 511 | * empty but the inflater needs more input to continue, or if the output buffer |
| 512 | * is not large enough. Call mz_inflate() again */ |
| 513 | /* with more input data, or with more room in the output buffer (except when |
| 514 | * using single call decompression, described above). */ |
| 515 | MINIZ_EXPORT int mz_inflate(mz_streamp pStream, int flush); |
| 516 | |
| 517 | /* Deinitializes a decompressor. */ |
| 518 | MINIZ_EXPORT int mz_inflateEnd(mz_streamp pStream); |
| 519 | |
| 520 | /* Single-call decompression. */ |
| 521 | /* Returns MZ_OK on success, or one of the error codes from mz_inflate() on |
| 522 | * failure. */ |
| 523 | MINIZ_EXPORT int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, |
| 524 | const unsigned char *pSource, |
| 525 | mz_ulong source_len); |
| 526 | MINIZ_EXPORT int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, |
| 527 | const unsigned char *pSource, |
| 528 | mz_ulong *pSource_len); |
| 529 | #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ |
| 530 | |
| 531 | /* Returns a string description of the specified error code, or NULL if the |
| 532 | * error code is invalid. */ |
| 533 | MINIZ_EXPORT const char *mz_error(int err); |
| 534 | |
| 535 | /* Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used |
| 536 | * as a drop-in replacement for the subset of zlib that miniz.c supports. */ |
| 537 | /* Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you |
| 538 | * use zlib in the same project. */ |
| 539 | #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES |
| 540 | typedef unsigned char Byte; |
| 541 | typedef unsigned int uInt; |
| 542 | typedef mz_ulong uLong; |
| 543 | typedef Byte Bytef; |
| 544 | typedef uInt uIntf; |
| 545 | typedef char charf; |
| 546 | typedef int intf; |
| 547 | typedef void *voidpf; |
| 548 | typedef uLong uLongf; |
| 549 | typedef void *voidp; |
| 550 | typedef void *const voidpc; |
| 551 | #define Z_NULL 0 |
| 552 | #define Z_NO_FLUSH MZ_NO_FLUSH |
| 553 | #define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH |
| 554 | #define Z_SYNC_FLUSH MZ_SYNC_FLUSH |
| 555 | #define Z_FULL_FLUSH MZ_FULL_FLUSH |
| 556 | #define Z_FINISH MZ_FINISH |
| 557 | #define Z_BLOCK MZ_BLOCK |
| 558 | #define Z_OK MZ_OK |
| 559 | #define Z_STREAM_END MZ_STREAM_END |
| 560 | #define Z_NEED_DICT MZ_NEED_DICT |
| 561 | #define Z_ERRNO MZ_ERRNO |
| 562 | #define Z_STREAM_ERROR MZ_STREAM_ERROR |
| 563 | #define Z_DATA_ERROR MZ_DATA_ERROR |
| 564 | #define Z_MEM_ERROR MZ_MEM_ERROR |
| 565 | #define Z_BUF_ERROR MZ_BUF_ERROR |
| 566 | #define Z_VERSION_ERROR MZ_VERSION_ERROR |
| 567 | #define Z_PARAM_ERROR MZ_PARAM_ERROR |
| 568 | #define Z_NO_COMPRESSION MZ_NO_COMPRESSION |
| 569 | #define Z_BEST_SPEED MZ_BEST_SPEED |
| 570 | #define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION |
| 571 | #define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION |
| 572 | #define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY |
| 573 | #define Z_FILTERED MZ_FILTERED |
| 574 | #define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY |
| 575 | #define Z_RLE MZ_RLE |
| 576 | #define Z_FIXED MZ_FIXED |
| 577 | #define Z_DEFLATED MZ_DEFLATED |
| 578 | #define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS |
| 579 | #define alloc_func mz_alloc_func |
| 580 | #define free_func mz_free_func |
| 581 | #define internal_state mz_internal_state |
| 582 | #define z_stream mz_stream |
| 583 | |
| 584 | #ifndef MINIZ_NO_DEFLATE_APIS |
| 585 | #define deflateInit mz_deflateInit |
| 586 | #define deflateInit2 mz_deflateInit2 |
| 587 | #define deflateReset mz_deflateReset |
| 588 | #define deflate mz_deflate |
| 589 | #define deflateEnd mz_deflateEnd |
| 590 | #define deflateBound mz_deflateBound |
| 591 | #define compress mz_compress |
| 592 | #define compress2 mz_compress2 |
| 593 | #define compressBound mz_compressBound |
| 594 | #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ |
| 595 | |
| 596 | #ifndef MINIZ_NO_INFLATE_APIS |
| 597 | #define inflateInit mz_inflateInit |
| 598 | #define inflateInit2 mz_inflateInit2 |
| 599 | #define inflateReset mz_inflateReset |
| 600 | #define inflate mz_inflate |
| 601 | #define inflateEnd mz_inflateEnd |
| 602 | #define uncompress mz_uncompress |
| 603 | #define uncompress2 mz_uncompress2 |
| 604 | #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ |
| 605 | |
| 606 | #define crc32 mz_crc32 |
| 607 | #define adler32 mz_adler32 |
| 608 | #define MAX_WBITS 15 |
| 609 | #define MAX_MEM_LEVEL 9 |
| 610 | #define zError mz_error |
| 611 | #define ZLIB_VERSION MZ_VERSION |
| 612 | #define ZLIB_VERNUM MZ_VERNUM |
| 613 | #define ZLIB_VER_MAJOR MZ_VER_MAJOR |
| 614 | #define ZLIB_VER_MINOR MZ_VER_MINOR |
| 615 | #define ZLIB_VER_REVISION MZ_VER_REVISION |
| 616 | #define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION |
| 617 | #define zlibVersion mz_version |
| 618 | #define zlib_version mz_version() |
| 619 | #endif /* #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES */ |
| 620 | |
| 621 | #endif /* MINIZ_NO_ZLIB_APIS */ |
| 622 | |
| 623 | #ifdef __cplusplus |
| 624 | } |
| 625 | #endif |
| 626 | |
| 627 | #pragma once |
| 628 | #include <assert.h> |
| 629 | #include <stdint.h> |
| 630 | #include <stdlib.h> |
| 631 | #include <string.h> |
| 632 | |
| 633 | /* ------------------- Types and macros */ |
| 634 | typedef unsigned char mz_uint8; |
| 635 | typedef signed short mz_int16; |
| 636 | typedef unsigned short mz_uint16; |
| 637 | typedef unsigned int mz_uint32; |
| 638 | typedef unsigned int mz_uint; |
| 639 | typedef int64_t mz_int64; |
| 640 | typedef uint64_t mz_uint64; |
| 641 | typedef int mz_bool; |
| 642 | |
| 643 | #define MZ_FALSE (0) |
| 644 | #define MZ_TRUE (1) |
| 645 | |
| 646 | /* Works around MSVC's spammy "warning C4127: conditional expression is |
| 647 | * constant" message. */ |
| 648 | #ifdef _MSC_VER |
| 649 | #define MZ_MACRO_END while (0, 0) |
| 650 | #else |
| 651 | #define MZ_MACRO_END while (0) |
| 652 | #endif |
| 653 | |
| 654 | #ifdef MINIZ_NO_STDIO |
| 655 | #define MZ_FILE void * |
| 656 | #else |
| 657 | #include <stdio.h> |
| 658 | #define MZ_FILE FILE |
| 659 | #endif /* #ifdef MINIZ_NO_STDIO */ |
| 660 | |
| 661 | #ifdef MINIZ_NO_TIME |
| 662 | typedef struct mz_dummy_time_t_tag { |
| 663 | mz_uint32 m_dummy1; |
| 664 | mz_uint32 m_dummy2; |
| 665 | } mz_dummy_time_t; |
| 666 | #define MZ_TIME_T mz_dummy_time_t |
| 667 | #else |
| 668 | #define MZ_TIME_T time_t |
| 669 | #endif |
| 670 | |
| 671 | #define MZ_ASSERT(x) assert(x) |
| 672 | |
| 673 | #ifdef MINIZ_NO_MALLOC |
| 674 | #define MZ_MALLOC(x) NULL |
| 675 | #define MZ_FREE(x) (void)x, ((void)0) |
| 676 | #define MZ_REALLOC(p, x) NULL |
| 677 | #else |
| 678 | #define MZ_MALLOC(x) malloc(x) |
| 679 | #define MZ_FREE(x) free(x) |
| 680 | #define MZ_REALLOC(p, x) realloc(p, x) |
| 681 | #endif |
| 682 | |
| 683 | #define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 684 | #define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 685 | #define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj)) |
| 686 | #define MZ_CLEAR_ARR(obj) memset((obj), 0, sizeof(obj)) |
| 687 | #define MZ_CLEAR_PTR(obj) memset((obj), 0, sizeof(*obj)) |
| 688 | |
| 689 | #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN |
| 690 | #define MZ_READ_LE16(p) *((const mz_uint16 *)(p)) |
| 691 | #define MZ_READ_LE32(p) *((const mz_uint32 *)(p)) |
| 692 | #else |
| 693 | #define MZ_READ_LE16(p) \ |
| 694 | ((mz_uint32)(((const mz_uint8 *)(p))[0]) | \ |
| 695 | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U)) |
| 696 | #define MZ_READ_LE32(p) \ |
| 697 | ((mz_uint32)(((const mz_uint8 *)(p))[0]) | \ |
| 698 | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | \ |
| 699 | ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | \ |
| 700 | ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U)) |
| 701 | #endif |
| 702 | |
| 703 | #define MZ_READ_LE64(p) \ |
| 704 | (((mz_uint64)MZ_READ_LE32(p)) | \ |
| 705 | (((mz_uint64)MZ_READ_LE32((const mz_uint8 *)(p) + sizeof(mz_uint32))) \ |
| 706 | << 32U)) |
| 707 | |
| 708 | #ifdef _MSC_VER |
| 709 | #define MZ_FORCEINLINE __forceinline |
| 710 | #elif defined(__GNUC__) |
| 711 | #define MZ_FORCEINLINE __inline__ __attribute__((__always_inline__)) |
| 712 | #else |
| 713 | #define MZ_FORCEINLINE inline |
| 714 | #endif |
| 715 | |
| 716 | #ifdef __cplusplus |
| 717 | extern "C" { |
| 718 | #endif |
| 719 | |
| 720 | extern MINIZ_EXPORT void *miniz_def_alloc_func(void *opaque, size_t items, |
| 721 | size_t size); |
| 722 | extern MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address); |
| 723 | extern MINIZ_EXPORT void *miniz_def_realloc_func(void *opaque, void *address, |
| 724 | size_t items, size_t size); |
| 725 | |
| 726 | #define MZ_UINT16_MAX (0xFFFFU) |
| 727 | #define MZ_UINT32_MAX (0xFFFFFFFFU) |
| 728 | |
| 729 | #ifdef __cplusplus |
| 730 | } |
| 731 | #endif |
| 732 | #pragma once |
| 733 | |
| 734 | #ifndef MINIZ_NO_DEFLATE_APIS |
| 735 | |
| 736 | #ifdef __cplusplus |
| 737 | extern "C" { |
| 738 | #endif |
| 739 | /* ------------------- Low-level Compression API Definitions */ |
| 740 | |
| 741 | /* Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly |
| 742 | * slower, and raw/dynamic blocks will be output more frequently). */ |
| 743 | #ifndef TDEFL_LESS_MEMORY |
| 744 | #define TDEFL_LESS_MEMORY 0 |
| 745 | #endif |
| 746 | |
| 747 | /* tdefl_init() compression flags logically OR'd together (low 12 bits contain |
| 748 | * the max. number of probes per dictionary search): */ |
| 749 | /* TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes |
| 750 | * per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap |
| 751 | * compression), 4095=Huffman+LZ (slowest/best compression). */ |
| 752 | enum { |
| 753 | TDEFL_HUFFMAN_ONLY = 0, |
| 754 | TDEFL_DEFAULT_MAX_PROBES = 128, |
| 755 | TDEFL_MAX_PROBES_MASK = 0xFFF |
| 756 | }; |
| 757 | |
| 758 | /* TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before |
| 759 | * the deflate data, and the Adler-32 of the source data at the end. Otherwise, |
| 760 | * you'll get raw deflate data. */ |
| 761 | /* TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even |
| 762 | * when not writing zlib headers). */ |
| 763 | /* TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more |
| 764 | * efficient lazy parsing. */ |
| 765 | /* TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's |
| 766 | * initialization time to the minimum, but the output may vary from run to run |
| 767 | * given the same input (depending on the contents of memory). */ |
| 768 | /* TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1) |
| 769 | */ |
| 770 | /* TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. */ |
| 771 | /* TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. */ |
| 772 | /* TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. */ |
| 773 | /* The low 12 bits are reserved to control the max # of hash probes per |
| 774 | * dictionary lookup (see TDEFL_MAX_PROBES_MASK). */ |
| 775 | enum { |
| 776 | TDEFL_WRITE_ZLIB_HEADER = 0x01000, |
| 777 | TDEFL_COMPUTE_ADLER32 = 0x02000, |
| 778 | TDEFL_GREEDY_PARSING_FLAG = 0x04000, |
| 779 | TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000, |
| 780 | TDEFL_RLE_MATCHES = 0x10000, |
| 781 | TDEFL_FILTER_MATCHES = 0x20000, |
| 782 | TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000, |
| 783 | TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000 |
| 784 | }; |
| 785 | |
| 786 | /* High level compression functions: */ |
| 787 | /* tdefl_compress_mem_to_heap() compresses a block in memory to a heap block |
| 788 | * allocated via malloc(). */ |
| 789 | /* On entry: */ |
| 790 | /* pSrc_buf, src_buf_len: Pointer and size of source block to compress. */ |
| 791 | /* flags: The max match finder probes (default is 128) logically OR'd against |
| 792 | * the above flags. Higher probes are slower but improve compression. */ |
| 793 | /* On return: */ |
| 794 | /* Function returns a pointer to the compressed data, or NULL on failure. */ |
| 795 | /* *pOut_len will be set to the compressed data's size, which could be larger |
| 796 | * than src_buf_len on uncompressible data. */ |
| 797 | /* The caller must free() the returned block when it's no longer needed. */ |
| 798 | MINIZ_EXPORT void *tdefl_compress_mem_to_heap(const void *pSrc_buf, |
| 799 | size_t src_buf_len, |
| 800 | size_t *pOut_len, int flags); |
| 801 | |
| 802 | /* tdefl_compress_mem_to_mem() compresses a block in memory to another block in |
| 803 | * memory. */ |
| 804 | /* Returns 0 on failure. */ |
| 805 | MINIZ_EXPORT size_t tdefl_compress_mem_to_mem(void *pOut_buf, |
| 806 | size_t out_buf_len, |
| 807 | const void *pSrc_buf, |
| 808 | size_t src_buf_len, int flags); |
| 809 | |
| 810 | /* Compresses an image to a compressed PNG file in memory. */ |
| 811 | /* On entry: */ |
| 812 | /* pImage, w, h, and num_chans describe the image to compress. num_chans may be |
| 813 | * 1, 2, 3, or 4. */ |
| 814 | /* The image pitch in bytes per scanline will be w*num_chans. The leftmost |
| 815 | * pixel on the top scanline is stored first in memory. */ |
| 816 | /* level may range from [0,10], use MZ_NO_COMPRESSION, MZ_BEST_SPEED, |
| 817 | * MZ_BEST_COMPRESSION, etc. or a decent default is MZ_DEFAULT_LEVEL */ |
| 818 | /* If flip is true, the image will be flipped on the Y axis (useful for OpenGL |
| 819 | * apps). */ |
| 820 | /* On return: */ |
| 821 | /* Function returns a pointer to the compressed data, or NULL on failure. */ |
| 822 | /* *pLen_out will be set to the size of the PNG image file. */ |
| 823 | /* The caller must mz_free() the returned heap block (which will typically be |
| 824 | * larger than *pLen_out) when it's no longer needed. */ |
| 825 | MINIZ_EXPORT void * |
| 826 | tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, |
| 827 | int num_chans, size_t *pLen_out, |
| 828 | mz_uint level, mz_bool flip); |
| 829 | MINIZ_EXPORT void *tdefl_write_image_to_png_file_in_memory(const void *pImage, |
| 830 | int w, int h, |
| 831 | int num_chans, |
| 832 | size_t *pLen_out); |
| 833 | |
| 834 | /* Output stream interface. The compressor uses this interface to write |
| 835 | * compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time. */ |
| 836 | typedef mz_bool (*tdefl_put_buf_func_ptr)(const void *pBuf, int len, |
| 837 | void *pUser); |
| 838 | |
| 839 | /* tdefl_compress_mem_to_output() compresses a block to an output stream. The |
| 840 | * above helpers use this function internally. */ |
| 841 | MINIZ_EXPORT mz_bool tdefl_compress_mem_to_output( |
| 842 | const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, |
| 843 | void *pPut_buf_user, int flags); |
| 844 | |
| 845 | enum { |
| 846 | TDEFL_MAX_HUFF_TABLES = 3, |
| 847 | TDEFL_MAX_HUFF_SYMBOLS_0 = 288, |
| 848 | TDEFL_MAX_HUFF_SYMBOLS_1 = 32, |
| 849 | TDEFL_MAX_HUFF_SYMBOLS_2 = 19, |
| 850 | TDEFL_LZ_DICT_SIZE = 32768, |
| 851 | TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1, |
| 852 | TDEFL_MIN_MATCH_LEN = 3, |
| 853 | TDEFL_MAX_MATCH_LEN = 258 |
| 854 | }; |
| 855 | |
| 856 | /* TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed |
| 857 | * output block (using static/fixed Huffman codes). */ |
| 858 | #if TDEFL_LESS_MEMORY |
| 859 | enum { |
| 860 | TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024, |
| 861 | TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, |
| 862 | TDEFL_MAX_HUFF_SYMBOLS = 288, |
| 863 | TDEFL_LZ_HASH_BITS = 12, |
| 864 | TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, |
| 865 | TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, |
| 866 | TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS |
| 867 | }; |
| 868 | #else |
| 869 | enum { |
| 870 | TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024, |
| 871 | TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, |
| 872 | TDEFL_MAX_HUFF_SYMBOLS = 288, |
| 873 | TDEFL_LZ_HASH_BITS = 15, |
| 874 | TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, |
| 875 | TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, |
| 876 | TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS |
| 877 | }; |
| 878 | #endif |
| 879 | |
| 880 | /* The low-level tdefl functions below may be used directly if the above helper |
| 881 | * functions aren't flexible enough. The low-level functions don't make any heap |
| 882 | * allocations, unlike the above helper functions. */ |
| 883 | typedef enum { |
| 884 | TDEFL_STATUS_BAD_PARAM = -2, |
| 885 | TDEFL_STATUS_PUT_BUF_FAILED = -1, |
| 886 | TDEFL_STATUS_OKAY = 0, |
| 887 | TDEFL_STATUS_DONE = 1 |
| 888 | } tdefl_status; |
| 889 | |
| 890 | /* Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums */ |
| 891 | typedef enum { |
| 892 | TDEFL_NO_FLUSH = 0, |
| 893 | TDEFL_SYNC_FLUSH = 2, |
| 894 | TDEFL_FULL_FLUSH = 3, |
| 895 | TDEFL_FINISH = 4 |
| 896 | } tdefl_flush; |
| 897 | |
| 898 | /* tdefl's compression state structure. */ |
| 899 | typedef struct { |
| 900 | tdefl_put_buf_func_ptr m_pPut_buf_func; |
| 901 | void *m_pPut_buf_user; |
| 902 | mz_uint m_flags, m_max_probes[2]; |
| 903 | int m_greedy_parsing; |
| 904 | mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size; |
| 905 | mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end; |
| 906 | mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, |
| 907 | m_bit_buffer; |
| 908 | mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit, |
| 909 | m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index, |
| 910 | m_wants_to_finish; |
| 911 | tdefl_status m_prev_return_status; |
| 912 | const void *m_pIn_buf; |
| 913 | void *m_pOut_buf; |
| 914 | size_t *m_pIn_buf_size, *m_pOut_buf_size; |
| 915 | tdefl_flush m_flush; |
| 916 | const mz_uint8 *m_pSrc; |
| 917 | size_t m_src_buf_left, m_out_buf_ofs; |
| 918 | mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1]; |
| 919 | mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; |
| 920 | mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; |
| 921 | mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; |
| 922 | mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE]; |
| 923 | mz_uint16 m_next[TDEFL_LZ_DICT_SIZE]; |
| 924 | mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE]; |
| 925 | mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE]; |
| 926 | } tdefl_compressor; |
| 927 | |
| 928 | /* Initializes the compressor. */ |
| 929 | /* There is no corresponding deinit() function because the tdefl API's do not |
| 930 | * dynamically allocate memory. */ |
| 931 | /* pBut_buf_func: If NULL, output data will be supplied to the specified |
| 932 | * callback. In this case, the user should call the tdefl_compress_buffer() API |
| 933 | * for compression. */ |
| 934 | /* If pBut_buf_func is NULL the user should always call the tdefl_compress() |
| 935 | * API. */ |
| 936 | /* flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, |
| 937 | * etc.) */ |
| 938 | MINIZ_EXPORT tdefl_status tdefl_init(tdefl_compressor *d, |
| 939 | tdefl_put_buf_func_ptr pPut_buf_func, |
| 940 | void *pPut_buf_user, int flags); |
| 941 | |
| 942 | /* Compresses a block of data, consuming as much of the specified input buffer |
| 943 | * as possible, and writing as much compressed data to the specified output |
| 944 | * buffer as possible. */ |
| 945 | MINIZ_EXPORT tdefl_status tdefl_compress(tdefl_compressor *d, |
| 946 | const void *pIn_buf, |
| 947 | size_t *pIn_buf_size, void *pOut_buf, |
| 948 | size_t *pOut_buf_size, |
| 949 | tdefl_flush flush); |
| 950 | |
| 951 | /* tdefl_compress_buffer() is only usable when the tdefl_init() is called with a |
| 952 | * non-NULL tdefl_put_buf_func_ptr. */ |
| 953 | /* tdefl_compress_buffer() always consumes the entire input buffer. */ |
| 954 | MINIZ_EXPORT tdefl_status tdefl_compress_buffer(tdefl_compressor *d, |
| 955 | const void *pIn_buf, |
| 956 | size_t in_buf_size, |
| 957 | tdefl_flush flush); |
| 958 | |
| 959 | MINIZ_EXPORT tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d); |
| 960 | MINIZ_EXPORT mz_uint32 tdefl_get_adler32(tdefl_compressor *d); |
| 961 | |
| 962 | /* Create tdefl_compress() flags given zlib-style compression parameters. */ |
| 963 | /* level may range from [0,10] (where 10 is absolute max compression, but may be |
| 964 | * much slower on some files) */ |
| 965 | /* window_bits may be -15 (raw deflate) or 15 (zlib) */ |
| 966 | /* strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, |
| 967 | * MZ_RLE, or MZ_FIXED */ |
| 968 | MINIZ_EXPORT mz_uint tdefl_create_comp_flags_from_zip_params(int level, |
| 969 | int window_bits, |
| 970 | int strategy); |
| 971 | |
| 972 | #ifndef MINIZ_NO_MALLOC |
| 973 | /* Allocate the tdefl_compressor structure in C so that */ |
| 974 | /* non-C language bindings to tdefl_ API don't need to worry about */ |
| 975 | /* structure size and allocation mechanism. */ |
| 976 | MINIZ_EXPORT tdefl_compressor *tdefl_compressor_alloc(void); |
| 977 | MINIZ_EXPORT void tdefl_compressor_free(tdefl_compressor *pComp); |
| 978 | #endif |
| 979 | |
| 980 | #ifdef __cplusplus |
| 981 | } |
| 982 | #endif |
| 983 | |
| 984 | #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ |
| 985 | #pragma once |
| 986 | |
| 987 | /* ------------------- Low-level Decompression API Definitions */ |
| 988 | |
| 989 | #ifndef MINIZ_NO_INFLATE_APIS |
| 990 | |
| 991 | #ifdef __cplusplus |
| 992 | extern "C" { |
| 993 | #endif |
| 994 | /* Decompression flags used by tinfl_decompress(). */ |
| 995 | /* TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and |
| 996 | * ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the |
| 997 | * input is a raw deflate stream. */ |
| 998 | /* TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available |
| 999 | * beyond the end of the supplied input buffer. If clear, the input buffer |
| 1000 | * contains all remaining input. */ |
| 1001 | /* TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large |
| 1002 | * enough to hold the entire decompressed stream. If clear, the output buffer is |
| 1003 | * at least the size of the dictionary (typically 32KB). */ |
| 1004 | /* TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the |
| 1005 | * decompressed bytes. */ |
| 1006 | enum { |
| 1007 | TINFL_FLAG_PARSE_ZLIB_HEADER = 1, |
| 1008 | TINFL_FLAG_HAS_MORE_INPUT = 2, |
| 1009 | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4, |
| 1010 | TINFL_FLAG_COMPUTE_ADLER32 = 8 |
| 1011 | }; |
| 1012 | |
| 1013 | /* High level decompression functions: */ |
| 1014 | /* tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block |
| 1015 | * allocated via malloc(). */ |
| 1016 | /* On entry: */ |
| 1017 | /* pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data |
| 1018 | * to decompress. */ |
| 1019 | /* On return: */ |
| 1020 | /* Function returns a pointer to the decompressed data, or NULL on failure. */ |
| 1021 | /* *pOut_len will be set to the decompressed data's size, which could be larger |
| 1022 | * than src_buf_len on uncompressible data. */ |
| 1023 | /* The caller must call mz_free() on the returned block when it's no longer |
| 1024 | * needed. */ |
| 1025 | MINIZ_EXPORT void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, |
| 1026 | size_t src_buf_len, |
| 1027 | size_t *pOut_len, int flags); |
| 1028 | |
| 1029 | /* tinfl_decompress_mem_to_mem() decompresses a block in memory to another block |
| 1030 | * in memory. */ |
| 1031 | /* Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes |
| 1032 | * written on success. */ |
| 1033 | #define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1)) |
| 1034 | MINIZ_EXPORT size_t tinfl_decompress_mem_to_mem(void *pOut_buf, |
| 1035 | size_t out_buf_len, |
| 1036 | const void *pSrc_buf, |
| 1037 | size_t src_buf_len, int flags); |
| 1038 | |
| 1039 | /* tinfl_decompress_mem_to_callback() decompresses a block in memory to an |
| 1040 | * internal 32KB buffer, and a user provided callback function will be called to |
| 1041 | * flush the buffer. */ |
| 1042 | /* Returns 1 on success or 0 on failure. */ |
| 1043 | typedef int (*tinfl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser); |
| 1044 | MINIZ_EXPORT int |
| 1045 | tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, |
| 1046 | tinfl_put_buf_func_ptr pPut_buf_func, |
| 1047 | void *pPut_buf_user, int flags); |
| 1048 | |
| 1049 | struct tinfl_decompressor_tag; |
| 1050 | typedef struct tinfl_decompressor_tag tinfl_decompressor; |
| 1051 | |
| 1052 | #ifndef MINIZ_NO_MALLOC |
| 1053 | /* Allocate the tinfl_decompressor structure in C so that */ |
| 1054 | /* non-C language bindings to tinfl_ API don't need to worry about */ |
| 1055 | /* structure size and allocation mechanism. */ |
| 1056 | MINIZ_EXPORT tinfl_decompressor *tinfl_decompressor_alloc(void); |
| 1057 | MINIZ_EXPORT void tinfl_decompressor_free(tinfl_decompressor *pDecomp); |
| 1058 | #endif |
| 1059 | |
| 1060 | /* Max size of LZ dictionary. */ |
| 1061 | #define TINFL_LZ_DICT_SIZE 32768 |
| 1062 | |
| 1063 | /* Return status. */ |
| 1064 | typedef enum { |
| 1065 | /* This flags indicates the inflator needs 1 or more input bytes to make |
| 1066 | forward progress, but the caller is indicating that no more are available. |
| 1067 | The compressed data */ |
| 1068 | /* is probably corrupted. If you call the inflator again with more bytes it'll |
| 1069 | try to continue processing the input but this is a BAD sign (either the |
| 1070 | data is corrupted or you called it incorrectly). */ |
| 1071 | /* If you call it again with no input you'll just get |
| 1072 | TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS again. */ |
| 1073 | TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4, |
| 1074 | |
| 1075 | /* This flag indicates that one or more of the input parameters was obviously |
| 1076 | bogus. (You can try calling it again, but if you get this error the calling |
| 1077 | code is wrong.) */ |
| 1078 | TINFL_STATUS_BAD_PARAM = -3, |
| 1079 | |
| 1080 | /* This flags indicate the inflator is finished but the adler32 check of the |
| 1081 | uncompressed data didn't match. If you call it again it'll return |
| 1082 | TINFL_STATUS_DONE. */ |
| 1083 | TINFL_STATUS_ADLER32_MISMATCH = -2, |
| 1084 | |
| 1085 | /* This flags indicate the inflator has somehow failed (bad code, corrupted |
| 1086 | input, etc.). If you call it again without resetting via tinfl_init() it |
| 1087 | it'll just keep on returning the same status failure code. */ |
| 1088 | TINFL_STATUS_FAILED = -1, |
| 1089 | |
| 1090 | /* Any status code less than TINFL_STATUS_DONE must indicate a failure. */ |
| 1091 | |
| 1092 | /* This flag indicates the inflator has returned every byte of uncompressed |
| 1093 | data that it can, has consumed every byte that it needed, has successfully |
| 1094 | reached the end of the deflate stream, and */ |
| 1095 | /* if zlib headers and adler32 checking enabled that it has successfully |
| 1096 | checked the uncompressed data's adler32. If you call it again you'll just |
| 1097 | get TINFL_STATUS_DONE over and over again. */ |
| 1098 | TINFL_STATUS_DONE = 0, |
| 1099 | |
| 1100 | /* This flag indicates the inflator MUST have more input data (even 1 byte) |
| 1101 | before it can make any more forward progress, or you need to clear the |
| 1102 | TINFL_FLAG_HAS_MORE_INPUT */ |
| 1103 | /* flag on the next call if you don't have any more source data. If the source |
| 1104 | data was somehow corrupted it's also possible (but unlikely) for the |
| 1105 | inflator to keep on demanding input to */ |
| 1106 | /* proceed, so be sure to properly set the TINFL_FLAG_HAS_MORE_INPUT flag. */ |
| 1107 | TINFL_STATUS_NEEDS_MORE_INPUT = 1, |
| 1108 | |
| 1109 | /* This flag indicates the inflator definitely has 1 or more bytes of |
| 1110 | uncompressed data available, but it cannot write this data into the output |
| 1111 | buffer. */ |
| 1112 | /* Note if the source compressed data was corrupted it's possible for the |
| 1113 | inflator to return a lot of uncompressed data to the caller. I've been |
| 1114 | assuming you know how much uncompressed data to expect */ |
| 1115 | /* (either exact or worst case) and will stop calling the inflator and fail |
| 1116 | after receiving too much. In pure streaming scenarios where you have no |
| 1117 | idea how many bytes to expect this may not be possible */ |
| 1118 | /* so I may need to add some code to address this. */ |
| 1119 | TINFL_STATUS_HAS_MORE_OUTPUT = 2 |
| 1120 | } tinfl_status; |
| 1121 | |
| 1122 | /* Initializes the decompressor to its initial state. */ |
| 1123 | #define tinfl_init(r) \ |
| 1124 | do { \ |
| 1125 | (r)->m_state = 0; \ |
| 1126 | } \ |
| 1127 | MZ_MACRO_END |
| 1128 | #define tinfl_get_adler32(r) (r)->m_check_adler32 |
| 1129 | |
| 1130 | /* Main low-level decompressor coroutine function. This is the only function |
| 1131 | * actually needed for decompression. All the other functions are just |
| 1132 | * high-level helpers for improved usability. */ |
| 1133 | /* This is a universal API, i.e. it can be used as a building block to build any |
| 1134 | * desired higher level decompression API. In the limit case, it can be called |
| 1135 | * once per every byte input or output. */ |
| 1136 | MINIZ_EXPORT tinfl_status tinfl_decompress( |
| 1137 | tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, |
| 1138 | mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, |
| 1139 | const mz_uint32 decomp_flags); |
| 1140 | |
| 1141 | /* Internal/private bits follow. */ |
| 1142 | enum { |
| 1143 | TINFL_MAX_HUFF_TABLES = 3, |
| 1144 | TINFL_MAX_HUFF_SYMBOLS_0 = 288, |
| 1145 | TINFL_MAX_HUFF_SYMBOLS_1 = 32, |
| 1146 | TINFL_MAX_HUFF_SYMBOLS_2 = 19, |
| 1147 | TINFL_FAST_LOOKUP_BITS = 10, |
| 1148 | TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS |
| 1149 | }; |
| 1150 | |
| 1151 | #if MINIZ_HAS_64BIT_REGISTERS |
| 1152 | #define TINFL_USE_64BIT_BITBUF 1 |
| 1153 | #else |
| 1154 | #define TINFL_USE_64BIT_BITBUF 0 |
| 1155 | #endif |
| 1156 | |
| 1157 | #if TINFL_USE_64BIT_BITBUF |
| 1158 | typedef mz_uint64 tinfl_bit_buf_t; |
| 1159 | #define TINFL_BITBUF_SIZE (64) |
| 1160 | #else |
| 1161 | typedef mz_uint32 tinfl_bit_buf_t; |
| 1162 | #define TINFL_BITBUF_SIZE (32) |
| 1163 | #endif |
| 1164 | |
| 1165 | struct tinfl_decompressor_tag { |
| 1166 | mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, |
| 1167 | m_check_adler32, m_dist, m_counter, m_num_extra, |
| 1168 | m_table_sizes[TINFL_MAX_HUFF_TABLES]; |
| 1169 | tinfl_bit_buf_t m_bit_buf; |
| 1170 | size_t m_dist_from_out_buf_start; |
| 1171 | mz_int16 m_look_up[TINFL_MAX_HUFF_TABLES][TINFL_FAST_LOOKUP_SIZE]; |
| 1172 | mz_int16 m_tree_0[TINFL_MAX_HUFF_SYMBOLS_0 * 2]; |
| 1173 | mz_int16 m_tree_1[TINFL_MAX_HUFF_SYMBOLS_1 * 2]; |
| 1174 | mz_int16 m_tree_2[TINFL_MAX_HUFF_SYMBOLS_2 * 2]; |
| 1175 | mz_uint8 m_code_size_0[TINFL_MAX_HUFF_SYMBOLS_0]; |
| 1176 | mz_uint8 m_code_size_1[TINFL_MAX_HUFF_SYMBOLS_1]; |
| 1177 | mz_uint8 m_code_size_2[TINFL_MAX_HUFF_SYMBOLS_2]; |
| 1178 | mz_uint8 m_raw_header[4], |
| 1179 | m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137]; |
| 1180 | }; |
| 1181 | |
| 1182 | #ifdef __cplusplus |
| 1183 | } |
| 1184 | #endif |
| 1185 | |
| 1186 | #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ |
| 1187 | |
| 1188 | #pragma once |
| 1189 | |
| 1190 | /* ------------------- ZIP archive reading/writing */ |
| 1191 | |
| 1192 | #ifndef MINIZ_NO_ARCHIVE_APIS |
| 1193 | |
| 1194 | #ifdef __cplusplus |
| 1195 | extern "C" { |
| 1196 | #endif |
| 1197 | |
| 1198 | enum { |
| 1199 | /* Note: These enums can be reduced as needed to save memory or stack space - |
| 1200 | they are pretty conservative. */ |
| 1201 | MZ_ZIP_MAX_IO_BUF_SIZE = 64 * 1024, |
| 1202 | MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 512, |
| 1203 | MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 512 |
| 1204 | }; |
| 1205 | |
| 1206 | typedef struct { |
| 1207 | /* Central directory file index. */ |
| 1208 | mz_uint32 m_file_index; |
| 1209 | |
| 1210 | /* Byte offset of this entry in the archive's central directory. Note we |
| 1211 | * currently only support up to UINT_MAX or less bytes in the central dir. */ |
| 1212 | mz_uint64 m_central_dir_ofs; |
| 1213 | |
| 1214 | /* These fields are copied directly from the zip's central dir. */ |
| 1215 | mz_uint16 m_version_made_by; |
| 1216 | mz_uint16 m_version_needed; |
| 1217 | mz_uint16 m_bit_flag; |
| 1218 | mz_uint16 m_method; |
| 1219 | |
| 1220 | /* CRC-32 of uncompressed data. */ |
| 1221 | mz_uint32 m_crc32; |
| 1222 | |
| 1223 | /* File's compressed size. */ |
| 1224 | mz_uint64 m_comp_size; |
| 1225 | |
| 1226 | /* File's uncompressed size. Note, I've seen some old archives where directory |
| 1227 | * entries had 512 bytes for their uncompressed sizes, but when you try to |
| 1228 | * unpack them you actually get 0 bytes. */ |
| 1229 | mz_uint64 m_uncomp_size; |
| 1230 | |
| 1231 | /* Zip internal and external file attributes. */ |
| 1232 | mz_uint16 m_internal_attr; |
| 1233 | mz_uint32 m_external_attr; |
| 1234 | |
| 1235 | /* Entry's local header file offset in bytes. */ |
| 1236 | mz_uint64 m_local_header_ofs; |
| 1237 | |
| 1238 | /* Size of comment in bytes. */ |
| 1239 | mz_uint32 m_comment_size; |
| 1240 | |
| 1241 | /* MZ_TRUE if the entry appears to be a directory. */ |
| 1242 | mz_bool m_is_directory; |
| 1243 | |
| 1244 | /* MZ_TRUE if the entry uses encryption/strong encryption (which miniz_zip |
| 1245 | * doesn't support) */ |
| 1246 | mz_bool m_is_encrypted; |
| 1247 | |
| 1248 | /* MZ_TRUE if the file is not encrypted, a patch file, and if it uses a |
| 1249 | * compression method we support. */ |
| 1250 | mz_bool m_is_supported; |
| 1251 | |
| 1252 | /* Filename. If string ends in '/' it's a subdirectory entry. */ |
| 1253 | /* Guaranteed to be zero terminated, may be truncated to fit. */ |
| 1254 | char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE]; |
| 1255 | |
| 1256 | /* Comment field. */ |
| 1257 | /* Guaranteed to be zero terminated, may be truncated to fit. */ |
| 1258 | char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE]; |
| 1259 | |
| 1260 | #ifdef MINIZ_NO_TIME |
| 1261 | MZ_TIME_T m_padding; |
| 1262 | #else |
| 1263 | MZ_TIME_T m_time; |
| 1264 | #endif |
| 1265 | } mz_zip_archive_file_stat; |
| 1266 | |
| 1267 | typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, |
| 1268 | void *pBuf, size_t n); |
| 1269 | typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, |
| 1270 | const void *pBuf, size_t n); |
| 1271 | typedef mz_bool (*mz_file_needs_keepalive)(void *pOpaque); |
| 1272 | |
| 1273 | struct mz_zip_internal_state_tag; |
| 1274 | typedef struct mz_zip_internal_state_tag mz_zip_internal_state; |
| 1275 | |
| 1276 | typedef enum { |
| 1277 | MZ_ZIP_MODE_INVALID = 0, |
| 1278 | MZ_ZIP_MODE_READING = 1, |
| 1279 | MZ_ZIP_MODE_WRITING = 2, |
| 1280 | MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3 |
| 1281 | } mz_zip_mode; |
| 1282 | |
| 1283 | typedef enum { |
| 1284 | MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100, |
| 1285 | MZ_ZIP_FLAG_IGNORE_PATH = 0x0200, |
| 1286 | MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400, |
| 1287 | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800, |
| 1288 | MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG = |
| 1289 | 0x1000, /* if enabled, mz_zip_reader_locate_file() will be called on each |
| 1290 | file as its validated to ensure the func finds the file in the |
| 1291 | central dir (intended for testing) */ |
| 1292 | MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY = |
| 1293 | 0x2000, /* validate the local headers, but don't decompress the entire |
| 1294 | file and check the crc32 */ |
| 1295 | MZ_ZIP_FLAG_WRITE_ZIP64 = |
| 1296 | 0x4000, /* always use the zip64 file format, instead of the original zip |
| 1297 | file format with automatic switch to zip64. Use as flags |
| 1298 | parameter with mz_zip_writer_init*_v2 */ |
| 1299 | MZ_ZIP_FLAG_WRITE_ALLOW_READING = 0x8000, |
| 1300 | MZ_ZIP_FLAG_ASCII_FILENAME = 0x10000, |
| 1301 | /*After adding a compressed file, seek back |
| 1302 | to local file header and set the correct sizes*/ |
| 1303 | MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE = 0x20000 |
| 1304 | } mz_zip_flags; |
| 1305 | |
| 1306 | typedef enum { |
| 1307 | MZ_ZIP_TYPE_INVALID = 0, |
| 1308 | MZ_ZIP_TYPE_USER, |
| 1309 | MZ_ZIP_TYPE_MEMORY, |
| 1310 | MZ_ZIP_TYPE_HEAP, |
| 1311 | MZ_ZIP_TYPE_FILE, |
| 1312 | MZ_ZIP_TYPE_CFILE, |
| 1313 | MZ_ZIP_TOTAL_TYPES |
| 1314 | } mz_zip_type; |
| 1315 | |
| 1316 | /* miniz error codes. Be sure to update mz_zip_get_error_string() if you add or |
| 1317 | * modify this enum. */ |
| 1318 | typedef enum { |
| 1319 | MZ_ZIP_NO_ERROR = 0, |
| 1320 | MZ_ZIP_UNDEFINED_ERROR, |
| 1321 | MZ_ZIP_TOO_MANY_FILES, |
| 1322 | MZ_ZIP_FILE_TOO_LARGE, |
| 1323 | MZ_ZIP_UNSUPPORTED_METHOD, |
| 1324 | MZ_ZIP_UNSUPPORTED_ENCRYPTION, |
| 1325 | MZ_ZIP_UNSUPPORTED_FEATURE, |
| 1326 | MZ_ZIP_FAILED_FINDING_CENTRAL_DIR, |
| 1327 | MZ_ZIP_NOT_AN_ARCHIVE, |
| 1328 | MZ_ZIP_INVALID_HEADER_OR_CORRUPTED, |
| 1329 | MZ_ZIP_UNSUPPORTED_MULTIDISK, |
| 1330 | MZ_ZIP_DECOMPRESSION_FAILED, |
| 1331 | MZ_ZIP_COMPRESSION_FAILED, |
| 1332 | MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE, |
| 1333 | MZ_ZIP_CRC_CHECK_FAILED, |
| 1334 | MZ_ZIP_UNSUPPORTED_CDIR_SIZE, |
| 1335 | MZ_ZIP_ALLOC_FAILED, |
| 1336 | MZ_ZIP_FILE_OPEN_FAILED, |
| 1337 | MZ_ZIP_FILE_CREATE_FAILED, |
| 1338 | MZ_ZIP_FILE_WRITE_FAILED, |
| 1339 | MZ_ZIP_FILE_READ_FAILED, |
| 1340 | MZ_ZIP_FILE_CLOSE_FAILED, |
| 1341 | MZ_ZIP_FILE_SEEK_FAILED, |
| 1342 | MZ_ZIP_FILE_STAT_FAILED, |
| 1343 | MZ_ZIP_INVALID_PARAMETER, |
| 1344 | MZ_ZIP_INVALID_FILENAME, |
| 1345 | MZ_ZIP_BUF_TOO_SMALL, |
| 1346 | MZ_ZIP_INTERNAL_ERROR, |
| 1347 | MZ_ZIP_FILE_NOT_FOUND, |
| 1348 | MZ_ZIP_ARCHIVE_TOO_LARGE, |
| 1349 | MZ_ZIP_VALIDATION_FAILED, |
| 1350 | MZ_ZIP_WRITE_CALLBACK_FAILED, |
| 1351 | MZ_ZIP_TOTAL_ERRORS |
| 1352 | } mz_zip_error; |
| 1353 | |
| 1354 | typedef struct { |
| 1355 | mz_uint64 m_archive_size; |
| 1356 | mz_uint64 m_central_directory_file_ofs; |
| 1357 | |
| 1358 | /* We only support up to UINT32_MAX files in zip64 mode. */ |
| 1359 | mz_uint32 m_total_files; |
| 1360 | mz_zip_mode m_zip_mode; |
| 1361 | mz_zip_type m_zip_type; |
| 1362 | mz_zip_error m_last_error; |
| 1363 | |
| 1364 | mz_uint64 m_file_offset_alignment; |
| 1365 | |
| 1366 | mz_alloc_func m_pAlloc; |
| 1367 | mz_free_func m_pFree; |
| 1368 | mz_realloc_func m_pRealloc; |
| 1369 | void *m_pAlloc_opaque; |
| 1370 | |
| 1371 | mz_file_read_func m_pRead; |
| 1372 | mz_file_write_func m_pWrite; |
| 1373 | mz_file_needs_keepalive m_pNeeds_keepalive; |
| 1374 | void *m_pIO_opaque; |
| 1375 | |
| 1376 | mz_zip_internal_state *m_pState; |
| 1377 | |
| 1378 | } mz_zip_archive; |
| 1379 | |
| 1380 | typedef struct { |
| 1381 | mz_zip_archive *pZip; |
| 1382 | mz_uint flags; |
| 1383 | |
| 1384 | int status; |
| 1385 | |
| 1386 | mz_uint64 read_buf_size, read_buf_ofs, read_buf_avail, comp_remaining, |
| 1387 | out_buf_ofs, cur_file_ofs; |
| 1388 | mz_zip_archive_file_stat file_stat; |
| 1389 | void *pRead_buf; |
| 1390 | void *pWrite_buf; |
| 1391 | |
| 1392 | size_t out_blk_remain; |
| 1393 | |
| 1394 | tinfl_decompressor inflator; |
| 1395 | |
| 1396 | #ifdef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS |
| 1397 | mz_uint padding; |
| 1398 | #else |
| 1399 | mz_uint file_crc32; |
| 1400 | #endif |
| 1401 | |
| 1402 | } mz_zip_reader_extract_iter_state; |
| 1403 | |
| 1404 | /* -------- ZIP reading */ |
| 1405 | |
| 1406 | /* Inits a ZIP archive reader. */ |
| 1407 | /* These functions read and validate the archive's central directory. */ |
| 1408 | MINIZ_EXPORT mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, |
| 1409 | mz_uint flags); |
| 1410 | |
| 1411 | MINIZ_EXPORT mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, |
| 1412 | const void *pMem, size_t size, |
| 1413 | mz_uint flags); |
| 1414 | |
| 1415 | #ifndef MINIZ_NO_STDIO |
| 1416 | /* Read a archive from a disk file. */ |
| 1417 | /* file_start_ofs is the file offset where the archive actually begins, or 0. */ |
| 1418 | /* actual_archive_size is the true total size of the archive, which may be |
| 1419 | * smaller than the file's actual size on disk. If zero the entire file is |
| 1420 | * treated as the archive. */ |
| 1421 | MINIZ_EXPORT mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, |
| 1422 | const char *pFilename, |
| 1423 | mz_uint32 flags); |
| 1424 | MINIZ_EXPORT mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip, |
| 1425 | const char *pFilename, |
| 1426 | mz_uint flags, |
| 1427 | mz_uint64 file_start_ofs, |
| 1428 | mz_uint64 archive_size); |
| 1429 | |
| 1430 | /* Read an archive from an already opened FILE, beginning at the current file |
| 1431 | * position. */ |
| 1432 | /* The archive is assumed to be archive_size bytes long. If archive_size is 0, |
| 1433 | * then the entire rest of the file is assumed to contain the archive. */ |
| 1434 | /* The FILE will NOT be closed when mz_zip_reader_end() is called. */ |
| 1435 | MINIZ_EXPORT mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, |
| 1436 | MZ_FILE *pFile, |
| 1437 | mz_uint64 archive_size, |
| 1438 | mz_uint flags); |
| 1439 | #endif |
| 1440 | |
| 1441 | /* Ends archive reading, freeing all allocations, and closing the input archive |
| 1442 | * file if mz_zip_reader_init_file() was used. */ |
| 1443 | MINIZ_EXPORT mz_bool mz_zip_reader_end(mz_zip_archive *pZip); |
| 1444 | |
| 1445 | /* -------- ZIP reading or writing */ |
| 1446 | |
| 1447 | /* Clears a mz_zip_archive struct to all zeros. */ |
| 1448 | /* Important: This must be done before passing the struct to any mz_zip |
| 1449 | * functions. */ |
| 1450 | MINIZ_EXPORT void mz_zip_zero_struct(mz_zip_archive *pZip); |
| 1451 | |
| 1452 | MINIZ_EXPORT mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip); |
| 1453 | MINIZ_EXPORT mz_zip_type mz_zip_get_type(mz_zip_archive *pZip); |
| 1454 | |
| 1455 | /* Returns the total number of files in the archive. */ |
| 1456 | MINIZ_EXPORT mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip); |
| 1457 | |
| 1458 | MINIZ_EXPORT mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip); |
| 1459 | MINIZ_EXPORT mz_uint64 |
| 1460 | mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip); |
| 1461 | MINIZ_EXPORT MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip); |
| 1462 | |
| 1463 | /* Reads n bytes of raw archive data, starting at file offset file_ofs, to pBuf. |
| 1464 | */ |
| 1465 | MINIZ_EXPORT size_t mz_zip_read_archive_data(mz_zip_archive *pZip, |
| 1466 | mz_uint64 file_ofs, void *pBuf, |
| 1467 | size_t n); |
| 1468 | |
| 1469 | /* All mz_zip funcs set the m_last_error field in the mz_zip_archive struct. |
| 1470 | * These functions retrieve/manipulate this field. */ |
| 1471 | /* Note that the m_last_error functionality is not thread safe. */ |
| 1472 | MINIZ_EXPORT mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, |
| 1473 | mz_zip_error err_num); |
| 1474 | MINIZ_EXPORT mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip); |
| 1475 | MINIZ_EXPORT mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip); |
| 1476 | MINIZ_EXPORT mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip); |
| 1477 | MINIZ_EXPORT const char *mz_zip_get_error_string(mz_zip_error mz_err); |
| 1478 | |
| 1479 | /* MZ_TRUE if the archive file entry is a directory entry. */ |
| 1480 | MINIZ_EXPORT mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, |
| 1481 | mz_uint file_index); |
| 1482 | |
| 1483 | /* MZ_TRUE if the file is encrypted/strong encrypted. */ |
| 1484 | MINIZ_EXPORT mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, |
| 1485 | mz_uint file_index); |
| 1486 | |
| 1487 | /* MZ_TRUE if the compression method is supported, and the file is not |
| 1488 | * encrypted, and the file is not a compressed patch file. */ |
| 1489 | MINIZ_EXPORT mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip, |
| 1490 | mz_uint file_index); |
| 1491 | |
| 1492 | /* Retrieves the filename of an archive file entry. */ |
| 1493 | /* Returns the number of bytes written to pFilename, or if filename_buf_size is |
| 1494 | * 0 this function returns the number of bytes needed to fully store the |
| 1495 | * filename. */ |
| 1496 | MINIZ_EXPORT mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, |
| 1497 | mz_uint file_index, |
| 1498 | char *pFilename, |
| 1499 | mz_uint filename_buf_size); |
| 1500 | |
| 1501 | /* Attempts to locates a file in the archive's central directory. */ |
| 1502 | /* Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH */ |
| 1503 | /* Returns -1 if the file cannot be found. */ |
| 1504 | MINIZ_EXPORT int mz_zip_reader_locate_file(mz_zip_archive *pZip, |
| 1505 | const char *pName, |
| 1506 | const char *pComment, mz_uint flags); |
| 1507 | MINIZ_EXPORT mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip, |
| 1508 | const char *pName, |
| 1509 | const char *pComment, |
| 1510 | mz_uint flags, |
| 1511 | mz_uint32 *file_index); |
| 1512 | |
| 1513 | /* Returns detailed information about an archive file entry. */ |
| 1514 | MINIZ_EXPORT mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, |
| 1515 | mz_uint file_index, |
| 1516 | mz_zip_archive_file_stat *pStat); |
| 1517 | |
| 1518 | /* MZ_TRUE if the file is in zip64 format. */ |
| 1519 | /* A file is considered zip64 if it contained a zip64 end of central directory |
| 1520 | * marker, or if it contained any zip64 extended file information fields in the |
| 1521 | * central directory. */ |
| 1522 | MINIZ_EXPORT mz_bool mz_zip_is_zip64(mz_zip_archive *pZip); |
| 1523 | |
| 1524 | /* Returns the total central directory size in bytes. */ |
| 1525 | /* The current max supported size is <= MZ_UINT32_MAX. */ |
| 1526 | MINIZ_EXPORT size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip); |
| 1527 | |
| 1528 | /* Extracts a archive file to a memory buffer using no memory allocation. */ |
| 1529 | /* There must be at least enough room on the stack to store the inflator's state |
| 1530 | * (~34KB or so). */ |
| 1531 | MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_mem_no_alloc( |
| 1532 | mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, |
| 1533 | mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size); |
| 1534 | MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_mem_no_alloc( |
| 1535 | mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, |
| 1536 | mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size); |
| 1537 | |
| 1538 | /* Extracts a archive file to a memory buffer. */ |
| 1539 | MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, |
| 1540 | mz_uint file_index, |
| 1541 | void *pBuf, size_t buf_size, |
| 1542 | mz_uint flags); |
| 1543 | MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, |
| 1544 | const char *pFilename, |
| 1545 | void *pBuf, |
| 1546 | size_t buf_size, |
| 1547 | mz_uint flags); |
| 1548 | |
| 1549 | /* Extracts a archive file to a dynamically allocated heap buffer. */ |
| 1550 | /* The memory will be allocated via the mz_zip_archive's alloc/realloc |
| 1551 | * functions. */ |
| 1552 | /* Returns NULL and sets the last error on failure. */ |
| 1553 | MINIZ_EXPORT void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, |
| 1554 | mz_uint file_index, |
| 1555 | size_t *pSize, mz_uint flags); |
| 1556 | MINIZ_EXPORT void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, |
| 1557 | const char *pFilename, |
| 1558 | size_t *pSize, |
| 1559 | mz_uint flags); |
| 1560 | |
| 1561 | /* Extracts a archive file using a callback function to output the file's data. |
| 1562 | */ |
| 1563 | MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_callback( |
| 1564 | mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, |
| 1565 | void *pOpaque, mz_uint flags); |
| 1566 | MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_callback( |
| 1567 | mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, |
| 1568 | void *pOpaque, mz_uint flags); |
| 1569 | |
| 1570 | /* Extract a file iteratively */ |
| 1571 | MINIZ_EXPORT mz_zip_reader_extract_iter_state * |
| 1572 | mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index, |
| 1573 | mz_uint flags); |
| 1574 | MINIZ_EXPORT mz_zip_reader_extract_iter_state * |
| 1575 | mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename, |
| 1576 | mz_uint flags); |
| 1577 | MINIZ_EXPORT size_t mz_zip_reader_extract_iter_read( |
| 1578 | mz_zip_reader_extract_iter_state *pState, void *pvBuf, size_t buf_size); |
| 1579 | MINIZ_EXPORT mz_bool |
| 1580 | mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state *pState); |
| 1581 | |
| 1582 | #ifndef MINIZ_NO_STDIO |
| 1583 | /* Extracts a archive file to a disk file and sets its last accessed and |
| 1584 | * modified times. */ |
| 1585 | /* This function only extracts files, not archive directory records. */ |
| 1586 | MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, |
| 1587 | mz_uint file_index, |
| 1588 | const char *pDst_filename, |
| 1589 | mz_uint flags); |
| 1590 | MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_file( |
| 1591 | mz_zip_archive *pZip, const char *pArchive_filename, |
| 1592 | const char *pDst_filename, mz_uint flags); |
| 1593 | |
| 1594 | /* Extracts a archive file starting at the current position in the destination |
| 1595 | * FILE stream. */ |
| 1596 | MINIZ_EXPORT mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip, |
| 1597 | mz_uint file_index, |
| 1598 | MZ_FILE *File, |
| 1599 | mz_uint flags); |
| 1600 | MINIZ_EXPORT mz_bool mz_zip_reader_extract_file_to_cfile( |
| 1601 | mz_zip_archive *pZip, const char *pArchive_filename, MZ_FILE *pFile, |
| 1602 | mz_uint flags); |
| 1603 | #endif |
| 1604 | |
| 1605 | #if 0 |
| 1606 | /* TODO */ |
| 1607 | typedef void *mz_zip_streaming_extract_state_ptr; |
| 1608 | mz_zip_streaming_extract_state_ptr mz_zip_streaming_extract_begin(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags); |
| 1609 | mz_uint64 mz_zip_streaming_extract_get_size(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); |
| 1610 | mz_uint64 mz_zip_streaming_extract_get_cur_ofs(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); |
| 1611 | mz_bool mz_zip_streaming_extract_seek(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, mz_uint64 new_ofs); |
| 1612 | size_t mz_zip_streaming_extract_read(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState, void *pBuf, size_t buf_size); |
| 1613 | mz_bool mz_zip_streaming_extract_end(mz_zip_archive *pZip, mz_zip_streaming_extract_state_ptr pState); |
| 1614 | #endif |
| 1615 | |
| 1616 | /* This function compares the archive's local headers, the optional local zip64 |
| 1617 | * extended information block, and the optional descriptor following the |
| 1618 | * compressed data vs. the data in the central directory. */ |
| 1619 | /* It also validates that each file can be successfully uncompressed unless the |
| 1620 | * MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY is specified. */ |
| 1621 | MINIZ_EXPORT mz_bool mz_zip_validate_file(mz_zip_archive *pZip, |
| 1622 | mz_uint file_index, mz_uint flags); |
| 1623 | |
| 1624 | /* Validates an entire archive by calling mz_zip_validate_file() on each file. |
| 1625 | */ |
| 1626 | MINIZ_EXPORT mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, |
| 1627 | mz_uint flags); |
| 1628 | |
| 1629 | /* Misc utils/helpers, valid for ZIP reading or writing */ |
| 1630 | MINIZ_EXPORT mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size, |
| 1631 | mz_uint flags, |
| 1632 | mz_zip_error *pErr); |
| 1633 | #ifndef MINIZ_NO_STDIO |
| 1634 | MINIZ_EXPORT mz_bool mz_zip_validate_file_archive(const char *pFilename, |
| 1635 | mz_uint flags, |
| 1636 | mz_zip_error *pErr); |
| 1637 | #endif |
| 1638 | |
| 1639 | /* Universal end function - calls either mz_zip_reader_end() or |
| 1640 | * mz_zip_writer_end(). */ |
| 1641 | MINIZ_EXPORT mz_bool mz_zip_end(mz_zip_archive *pZip); |
| 1642 | |
| 1643 | /* -------- ZIP writing */ |
| 1644 | |
| 1645 | #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS |
| 1646 | |
| 1647 | /* Inits a ZIP archive writer. */ |
| 1648 | /*Set pZip->m_pWrite (and pZip->m_pIO_opaque) before calling mz_zip_writer_init |
| 1649 | * or mz_zip_writer_init_v2*/ |
| 1650 | /*The output is streamable, i.e. file_ofs in mz_file_write_func always increases |
| 1651 | * only by n*/ |
| 1652 | MINIZ_EXPORT mz_bool mz_zip_writer_init(mz_zip_archive *pZip, |
| 1653 | mz_uint64 existing_size); |
| 1654 | MINIZ_EXPORT mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip, |
| 1655 | mz_uint64 existing_size, |
| 1656 | mz_uint flags); |
| 1657 | |
| 1658 | MINIZ_EXPORT mz_bool mz_zip_writer_init_heap( |
| 1659 | mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, |
| 1660 | size_t initial_allocation_size); |
| 1661 | MINIZ_EXPORT mz_bool mz_zip_writer_init_heap_v2( |
| 1662 | mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, |
| 1663 | size_t initial_allocation_size, mz_uint flags); |
| 1664 | |
| 1665 | #ifndef MINIZ_NO_STDIO |
| 1666 | MINIZ_EXPORT mz_bool |
| 1667 | mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, |
| 1668 | mz_uint64 size_to_reserve_at_beginning); |
| 1669 | MINIZ_EXPORT mz_bool mz_zip_writer_init_file_v2( |
| 1670 | mz_zip_archive *pZip, const char *pFilename, |
| 1671 | mz_uint64 size_to_reserve_at_beginning, mz_uint flags); |
| 1672 | MINIZ_EXPORT mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip, |
| 1673 | MZ_FILE *pFile, mz_uint flags); |
| 1674 | #endif |
| 1675 | |
| 1676 | /* Converts a ZIP archive reader object into a writer object, to allow efficient |
| 1677 | * in-place file appends to occur on an existing archive. */ |
| 1678 | /* For archives opened using mz_zip_reader_init_file, pFilename must be the |
| 1679 | * archive's filename so it can be reopened for writing. If the file can't be |
| 1680 | * reopened, mz_zip_reader_end() will be called. */ |
| 1681 | /* For archives opened using mz_zip_reader_init_mem, the memory block must be |
| 1682 | * growable using the realloc callback (which defaults to realloc unless you've |
| 1683 | * overridden it). */ |
| 1684 | /* Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's |
| 1685 | * user provided m_pWrite function cannot be NULL. */ |
| 1686 | /* Note: In-place archive modification is not recommended unless you know what |
| 1687 | * you're doing, because if execution stops or something goes wrong before */ |
| 1688 | /* the archive is finalized the file's central directory will be hosed. */ |
| 1689 | MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, |
| 1690 | const char *pFilename); |
| 1691 | MINIZ_EXPORT mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip, |
| 1692 | const char *pFilename, |
| 1693 | mz_uint flags); |
| 1694 | |
| 1695 | /* Adds the contents of a memory buffer to an archive. These functions record |
| 1696 | * the current local time into the archive. */ |
| 1697 | /* To add a directory entry, call this method with an archive name ending in a |
| 1698 | * forwardslash with an empty buffer. */ |
| 1699 | /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, |
| 1700 | * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or |
| 1701 | * just set to MZ_DEFAULT_COMPRESSION. */ |
| 1702 | MINIZ_EXPORT mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, |
| 1703 | const char *pArchive_name, |
| 1704 | const void *pBuf, size_t buf_size, |
| 1705 | mz_uint level_and_flags); |
| 1706 | |
| 1707 | /* Like mz_zip_writer_add_mem(), except you can specify a file comment field, |
| 1708 | * and optionally supply the function with already compressed data. */ |
| 1709 | /* uncomp_size/uncomp_crc32 are only used if the MZ_ZIP_FLAG_COMPRESSED_DATA |
| 1710 | * flag is specified. */ |
| 1711 | MINIZ_EXPORT mz_bool mz_zip_writer_add_mem_ex( |
| 1712 | mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, |
| 1713 | size_t buf_size, const void *pComment, mz_uint16 comment_size, |
| 1714 | mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32); |
| 1715 | |
| 1716 | MINIZ_EXPORT mz_bool mz_zip_writer_add_mem_ex_v2( |
| 1717 | mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, |
| 1718 | size_t buf_size, const void *pComment, mz_uint16 comment_size, |
| 1719 | mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32, |
| 1720 | MZ_TIME_T *last_modified, const char *user_extra_data_local, |
| 1721 | mz_uint user_extra_data_local_len, const char *user_extra_data_central, |
| 1722 | mz_uint user_extra_data_central_len); |
| 1723 | |
| 1724 | /* Adds the contents of a file to an archive. This function also records the |
| 1725 | * disk file's modified time into the archive. */ |
| 1726 | /* File data is supplied via a read callback function. User |
| 1727 | * mz_zip_writer_add_(c)file to add a file directly.*/ |
| 1728 | MINIZ_EXPORT mz_bool mz_zip_writer_add_read_buf_callback( |
| 1729 | mz_zip_archive *pZip, const char *pArchive_name, |
| 1730 | mz_file_read_func read_callback, void *callback_opaque, mz_uint64 max_size, |
| 1731 | const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, |
| 1732 | mz_uint level_and_flags, mz_uint32 ext_attributes, |
| 1733 | const char *user_extra_data_local, mz_uint user_extra_data_local_len, |
| 1734 | const char *user_extra_data_central, mz_uint user_extra_data_central_len); |
| 1735 | |
| 1736 | #ifndef MINIZ_NO_STDIO |
| 1737 | /* Adds the contents of a disk file to an archive. This function also records |
| 1738 | * the disk file's modified time into the archive. */ |
| 1739 | /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, |
| 1740 | * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or |
| 1741 | * just set to MZ_DEFAULT_COMPRESSION. */ |
| 1742 | MINIZ_EXPORT mz_bool mz_zip_writer_add_file( |
| 1743 | mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, |
| 1744 | const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, |
| 1745 | mz_uint32 ext_attributes); |
| 1746 | |
| 1747 | /* Like mz_zip_writer_add_file(), except the file data is read from the |
| 1748 | * specified FILE stream. */ |
| 1749 | MINIZ_EXPORT mz_bool mz_zip_writer_add_cfile( |
| 1750 | mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file, |
| 1751 | mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, |
| 1752 | mz_uint16 comment_size, mz_uint level_and_flags, mz_uint32 ext_attributes, |
| 1753 | const char *user_extra_data_local, mz_uint user_extra_data_local_len, |
| 1754 | const char *user_extra_data_central, mz_uint user_extra_data_central_len); |
| 1755 | #endif |
| 1756 | |
| 1757 | /* Adds a file to an archive by fully cloning the data from another archive. */ |
| 1758 | /* This function fully clones the source file's compressed data (no |
| 1759 | * recompression), along with its full filename, extra data (it may add or |
| 1760 | * modify the zip64 local header extra data field), and the optional descriptor |
| 1761 | * following the compressed data. */ |
| 1762 | MINIZ_EXPORT mz_bool mz_zip_writer_add_from_zip_reader( |
| 1763 | mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint src_file_index); |
| 1764 | |
| 1765 | /* Finalizes the archive by writing the central directory records followed by |
| 1766 | * the end of central directory record. */ |
| 1767 | /* After an archive is finalized, the only valid call on the mz_zip_archive |
| 1768 | * struct is mz_zip_writer_end(). */ |
| 1769 | /* An archive must be manually finalized by calling this function for it to be |
| 1770 | * valid. */ |
| 1771 | MINIZ_EXPORT mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip); |
| 1772 | |
| 1773 | /* Finalizes a heap archive, returning a pointer to the heap block and its size. |
| 1774 | */ |
| 1775 | /* The heap block will be allocated using the mz_zip_archive's alloc/realloc |
| 1776 | * callbacks. */ |
| 1777 | MINIZ_EXPORT mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, |
| 1778 | void **ppBuf, |
| 1779 | size_t *pSize); |
| 1780 | |
| 1781 | /* Ends archive writing, freeing all allocations, and closing the output file if |
| 1782 | * mz_zip_writer_init_file() was used. */ |
| 1783 | /* Note for the archive to be valid, it *must* have been finalized before ending |
| 1784 | * (this function will not do it for you). */ |
| 1785 | MINIZ_EXPORT mz_bool mz_zip_writer_end(mz_zip_archive *pZip); |
| 1786 | |
| 1787 | /* -------- Misc. high-level helper functions: */ |
| 1788 | |
| 1789 | /* mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) |
| 1790 | * appends a memory blob to a ZIP archive. */ |
| 1791 | /* Note this is NOT a fully safe operation. If it crashes or dies in some way |
| 1792 | * your archive can be left in a screwed up state (without a central directory). |
| 1793 | */ |
| 1794 | /* level_and_flags - compression level (0-10, see MZ_BEST_SPEED, |
| 1795 | * MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or |
| 1796 | * just set to MZ_DEFAULT_COMPRESSION. */ |
| 1797 | /* TODO: Perhaps add an option to leave the existing central dir in place in |
| 1798 | * case the add dies? We could then truncate the file (so the old central dir |
| 1799 | * would be at the end) if something goes wrong. */ |
| 1800 | MINIZ_EXPORT mz_bool mz_zip_add_mem_to_archive_file_in_place( |
| 1801 | const char *pZip_filename, const char *pArchive_name, const void *pBuf, |
| 1802 | size_t buf_size, const void *pComment, mz_uint16 comment_size, |
| 1803 | mz_uint level_and_flags); |
| 1804 | MINIZ_EXPORT mz_bool mz_zip_add_mem_to_archive_file_in_place_v2( |
| 1805 | const char *pZip_filename, const char *pArchive_name, const void *pBuf, |
| 1806 | size_t buf_size, const void *pComment, mz_uint16 comment_size, |
| 1807 | mz_uint level_and_flags, mz_zip_error *pErr); |
| 1808 | |
| 1809 | #ifndef MINIZ_NO_STDIO |
| 1810 | /* Reads a single file from an archive into a heap block. */ |
| 1811 | /* If pComment is not NULL, only the file with the specified comment will be |
| 1812 | * extracted. */ |
| 1813 | /* Returns NULL on failure. */ |
| 1814 | MINIZ_EXPORT void * |
| 1815 | mz_zip_extract_archive_file_to_heap(const char *pZip_filename, |
| 1816 | const char *pArchive_name, size_t *pSize, |
| 1817 | mz_uint flags); |
| 1818 | MINIZ_EXPORT void *mz_zip_extract_archive_file_to_heap_v2( |
| 1819 | const char *pZip_filename, const char *pArchive_name, const char *pComment, |
| 1820 | size_t *pSize, mz_uint flags, mz_zip_error *pErr); |
| 1821 | #endif |
| 1822 | |
| 1823 | #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */ |
| 1824 | |
| 1825 | #ifdef __cplusplus |
| 1826 | } |
| 1827 | #endif |
| 1828 | |
| 1829 | #endif /* MINIZ_NO_ARCHIVE_APIS */ |
| 1830 | |
| 1831 | #ifndef MINIZ_HEADER_FILE_ONLY |
| 1832 | /************************************************************************** |
| 1833 | * |
| 1834 | * Copyright 2013-2014 RAD Game Tools and Valve Software |
| 1835 | * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC |
| 1836 | * All Rights Reserved. |
| 1837 | * |
| 1838 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 1839 | * of this software and associated documentation files (the "Software"), to deal |
| 1840 | * in the Software without restriction, including without limitation the rights |
| 1841 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 1842 | * copies of the Software, and to permit persons to whom the Software is |
| 1843 | * furnished to do so, subject to the following conditions: |
| 1844 | * |
| 1845 | * The above copyright notice and this permission notice shall be included in |
| 1846 | * all copies or substantial portions of the Software. |
| 1847 | * |
| 1848 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 1849 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 1850 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 1851 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 1852 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 1853 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 1854 | * THE SOFTWARE. |
| 1855 | * |
| 1856 | **************************************************************************/ |
| 1857 | |
| 1858 | typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1]; |
| 1859 | typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1]; |
| 1860 | typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1]; |
| 1861 | |
| 1862 | #ifdef __cplusplus |
| 1863 | extern "C" { |
| 1864 | #endif |
| 1865 | |
| 1866 | /* ------------------- zlib-style API's */ |
| 1867 | |
| 1868 | mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len) { |
| 1869 | mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16); |
| 1870 | size_t block_len = buf_len % 5552; |
| 1871 | if (!ptr) |
| 1872 | return MZ_ADLER32_INIT; |
| 1873 | while (buf_len) { |
| 1874 | for (i = 0; i + 7 < block_len; i += 8, ptr += 8) { |
| 1875 | s1 += ptr[0], s2 += s1; |
| 1876 | s1 += ptr[1], s2 += s1; |
| 1877 | s1 += ptr[2], s2 += s1; |
| 1878 | s1 += ptr[3], s2 += s1; |
| 1879 | s1 += ptr[4], s2 += s1; |
| 1880 | s1 += ptr[5], s2 += s1; |
| 1881 | s1 += ptr[6], s2 += s1; |
| 1882 | s1 += ptr[7], s2 += s1; |
| 1883 | } |
| 1884 | for (; i < block_len; ++i) |
| 1885 | s1 += *ptr++, s2 += s1; |
| 1886 | s1 %= 65521U, s2 %= 65521U; |
| 1887 | buf_len -= block_len; |
| 1888 | block_len = 5552; |
| 1889 | } |
| 1890 | return (s2 << 16) + s1; |
| 1891 | } |
| 1892 | |
| 1893 | /* Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C |
| 1894 | * implementation that balances processor cache usage against speed": |
| 1895 | * http://www.geocities.com/malbrain/ */ |
| 1896 | #if 0 |
| 1897 | mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len) |
| 1898 | { |
| 1899 | static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, |
| 1900 | 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c }; |
| 1901 | mz_uint32 crcu32 = (mz_uint32)crc; |
| 1902 | if (!ptr) |
| 1903 | return MZ_CRC32_INIT; |
| 1904 | crcu32 = ~crcu32; |
| 1905 | while (buf_len--) |
| 1906 | { |
| 1907 | mz_uint8 b = *ptr++; |
| 1908 | crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)]; |
| 1909 | crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)]; |
| 1910 | } |
| 1911 | return ~crcu32; |
| 1912 | } |
| 1913 | #elif defined(USE_EXTERNAL_MZCRC) |
| 1914 | /* If USE_EXTERNAL_CRC is defined, an external module will export the |
| 1915 | * mz_crc32() symbol for us to use, e.g. an SSE-accelerated version. |
| 1916 | * Depending on the impl, it may be necessary to ~ the input/output crc values. |
| 1917 | */ |
| 1918 | mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len); |
| 1919 | #else |
| 1920 | /* Faster, but larger CPU cache footprint. |
| 1921 | */ |
| 1922 | mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len) { |
| 1923 | static const mz_uint32 s_crc_table[256] = { |
| 1924 | 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, |
| 1925 | 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, |
| 1926 | 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, |
| 1927 | 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, |
| 1928 | 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, |
| 1929 | 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, |
| 1930 | 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, |
| 1931 | 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, |
| 1932 | 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, |
| 1933 | 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, |
| 1934 | 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, |
| 1935 | 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, |
| 1936 | 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, |
| 1937 | 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, |
| 1938 | 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, |
| 1939 | 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, |
| 1940 | 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, |
| 1941 | 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, |
| 1942 | 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, |
| 1943 | 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, |
| 1944 | 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, |
| 1945 | 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, |
| 1946 | 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, |
| 1947 | 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, |
| 1948 | 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, |
| 1949 | 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, |
| 1950 | 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, |
| 1951 | 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, |
| 1952 | 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, |
| 1953 | 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, |
| 1954 | 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, |
| 1955 | 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, |
| 1956 | 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, |
| 1957 | 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, |
| 1958 | 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, |
| 1959 | 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, |
| 1960 | 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, |
| 1961 | 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, |
| 1962 | 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, |
| 1963 | 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, |
| 1964 | 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, |
| 1965 | 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, |
| 1966 | 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D}; |
| 1967 | |
| 1968 | mz_uint32 crc32 = (mz_uint32)crc ^ 0xFFFFFFFF; |
| 1969 | const mz_uint8 *pByte_buf = (const mz_uint8 *)ptr; |
| 1970 | |
| 1971 | while (buf_len >= 4) { |
| 1972 | crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF]; |
| 1973 | crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[1]) & 0xFF]; |
| 1974 | crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[2]) & 0xFF]; |
| 1975 | crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[3]) & 0xFF]; |
| 1976 | pByte_buf += 4; |
| 1977 | buf_len -= 4; |
| 1978 | } |
| 1979 | |
| 1980 | while (buf_len) { |
| 1981 | crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF]; |
| 1982 | ++pByte_buf; |
| 1983 | --buf_len; |
| 1984 | } |
| 1985 | |
| 1986 | return ~crc32; |
| 1987 | } |
| 1988 | #endif |
| 1989 | |
| 1990 | void mz_free(void *p) { MZ_FREE(p); } |
| 1991 | |
| 1992 | MINIZ_EXPORT void *miniz_def_alloc_func(void *opaque, size_t items, |
| 1993 | size_t size) { |
| 1994 | (void)opaque, (void)items, (void)size; |
| 1995 | return MZ_MALLOC(items * size); |
| 1996 | } |
| 1997 | MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address) { |
| 1998 | (void)opaque, (void)address; |
| 1999 | MZ_FREE(address); |
| 2000 | } |
| 2001 | MINIZ_EXPORT void *miniz_def_realloc_func(void *opaque, void *address, |
| 2002 | size_t items, size_t size) { |
| 2003 | (void)opaque, (void)address, (void)items, (void)size; |
| 2004 | return MZ_REALLOC(address, items * size); |
| 2005 | } |
| 2006 | |
| 2007 | const char *mz_version(void) { return MZ_VERSION; } |
| 2008 | |
| 2009 | #ifndef MINIZ_NO_ZLIB_APIS |
| 2010 | |
| 2011 | #ifndef MINIZ_NO_DEFLATE_APIS |
| 2012 | |
| 2013 | int mz_deflateInit(mz_streamp pStream, int level) { |
| 2014 | return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, |
| 2015 | MZ_DEFAULT_STRATEGY); |
| 2016 | } |
| 2017 | |
| 2018 | int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, |
| 2019 | int mem_level, int strategy) { |
| 2020 | tdefl_compressor *pComp; |
| 2021 | mz_uint comp_flags = |
| 2022 | TDEFL_COMPUTE_ADLER32 | |
| 2023 | tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy); |
| 2024 | |
| 2025 | if (!pStream) |
| 2026 | return MZ_STREAM_ERROR; |
| 2027 | if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || |
| 2028 | ((window_bits != MZ_DEFAULT_WINDOW_BITS) && |
| 2029 | (-window_bits != MZ_DEFAULT_WINDOW_BITS))) |
| 2030 | return MZ_PARAM_ERROR; |
| 2031 | |
| 2032 | pStream->data_type = 0; |
| 2033 | pStream->adler = MZ_ADLER32_INIT; |
| 2034 | pStream->msg = NULL; |
| 2035 | pStream->reserved = 0; |
| 2036 | pStream->total_in = 0; |
| 2037 | pStream->total_out = 0; |
| 2038 | if (!pStream->zalloc) |
| 2039 | pStream->zalloc = miniz_def_alloc_func; |
| 2040 | if (!pStream->zfree) |
| 2041 | pStream->zfree = miniz_def_free_func; |
| 2042 | |
| 2043 | pComp = (tdefl_compressor *)pStream->zalloc(pStream->opaque, 1, |
| 2044 | sizeof(tdefl_compressor)); |
| 2045 | if (!pComp) |
| 2046 | return MZ_MEM_ERROR; |
| 2047 | |
| 2048 | pStream->state = (struct mz_internal_state *)pComp; |
| 2049 | |
| 2050 | if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) { |
| 2051 | mz_deflateEnd(pStream); |
| 2052 | return MZ_PARAM_ERROR; |
| 2053 | } |
| 2054 | |
| 2055 | return MZ_OK; |
| 2056 | } |
| 2057 | |
| 2058 | int mz_deflateReset(mz_streamp pStream) { |
| 2059 | if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || |
| 2060 | (!pStream->zfree)) |
| 2061 | return MZ_STREAM_ERROR; |
| 2062 | pStream->total_in = pStream->total_out = 0; |
| 2063 | tdefl_init((tdefl_compressor *)pStream->state, NULL, NULL, |
| 2064 | ((tdefl_compressor *)pStream->state)->m_flags); |
| 2065 | return MZ_OK; |
| 2066 | } |
| 2067 | |
| 2068 | int mz_deflate(mz_streamp pStream, int flush) { |
| 2069 | size_t in_bytes, out_bytes; |
| 2070 | mz_ulong orig_total_in, orig_total_out; |
| 2071 | int mz_status = MZ_OK; |
| 2072 | |
| 2073 | if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || |
| 2074 | (!pStream->next_out)) |
| 2075 | return MZ_STREAM_ERROR; |
| 2076 | if (!pStream->avail_out) |
| 2077 | return MZ_BUF_ERROR; |
| 2078 | |
| 2079 | if (flush == MZ_PARTIAL_FLUSH) |
| 2080 | flush = MZ_SYNC_FLUSH; |
| 2081 | |
| 2082 | if (((tdefl_compressor *)pStream->state)->m_prev_return_status == |
| 2083 | TDEFL_STATUS_DONE) |
| 2084 | return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR; |
| 2085 | |
| 2086 | orig_total_in = pStream->total_in; |
| 2087 | orig_total_out = pStream->total_out; |
| 2088 | for (;;) { |
| 2089 | tdefl_status defl_status; |
| 2090 | in_bytes = pStream->avail_in; |
| 2091 | out_bytes = pStream->avail_out; |
| 2092 | |
| 2093 | defl_status = tdefl_compress((tdefl_compressor *)pStream->state, |
| 2094 | pStream->next_in, &in_bytes, pStream->next_out, |
| 2095 | &out_bytes, (tdefl_flush)flush); |
| 2096 | pStream->next_in += (mz_uint)in_bytes; |
| 2097 | pStream->avail_in -= (mz_uint)in_bytes; |
| 2098 | pStream->total_in += (mz_uint)in_bytes; |
| 2099 | pStream->adler = tdefl_get_adler32((tdefl_compressor *)pStream->state); |
| 2100 | |
| 2101 | pStream->next_out += (mz_uint)out_bytes; |
| 2102 | pStream->avail_out -= (mz_uint)out_bytes; |
| 2103 | pStream->total_out += (mz_uint)out_bytes; |
| 2104 | |
| 2105 | if (defl_status < 0) { |
| 2106 | mz_status = MZ_STREAM_ERROR; |
| 2107 | break; |
| 2108 | } else if (defl_status == TDEFL_STATUS_DONE) { |
| 2109 | mz_status = MZ_STREAM_END; |
| 2110 | break; |
| 2111 | } else if (!pStream->avail_out) |
| 2112 | break; |
| 2113 | else if ((!pStream->avail_in) && (flush != MZ_FINISH)) { |
| 2114 | if ((flush) || (pStream->total_in != orig_total_in) || |
| 2115 | (pStream->total_out != orig_total_out)) |
| 2116 | break; |
| 2117 | return MZ_BUF_ERROR; /* Can't make forward progress without some input. |
| 2118 | */ |
| 2119 | } |
| 2120 | } |
| 2121 | return mz_status; |
| 2122 | } |
| 2123 | |
| 2124 | int mz_deflateEnd(mz_streamp pStream) { |
| 2125 | if (!pStream) |
| 2126 | return MZ_STREAM_ERROR; |
| 2127 | if (pStream->state) { |
| 2128 | pStream->zfree(pStream->opaque, pStream->state); |
| 2129 | pStream->state = NULL; |
| 2130 | } |
| 2131 | return MZ_OK; |
| 2132 | } |
| 2133 | |
| 2134 | mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len) { |
| 2135 | (void)pStream; |
| 2136 | /* This is really over conservative. (And lame, but it's actually pretty |
| 2137 | * tricky to compute a true upper bound given the way tdefl's blocking works.) |
| 2138 | */ |
| 2139 | return MZ_MAX(128 + (source_len * 110) / 100, |
| 2140 | 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5); |
| 2141 | } |
| 2142 | |
| 2143 | int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, |
| 2144 | const unsigned char *pSource, mz_ulong source_len, int level) { |
| 2145 | int status; |
| 2146 | mz_stream stream; |
| 2147 | memset(&stream, 0, sizeof(stream)); |
| 2148 | |
| 2149 | #if defined(__MINGW32__) || defined(__MINGW64__) || defined(__WATCOMC__) |
| 2150 | /* In case mz_ulong is 64-bits (argh I hate longs). */ |
| 2151 | #else |
| 2152 | if ((mz_uint64)(source_len | *pDest_len) > 0xFFFFFFFFU) |
| 2153 | return MZ_PARAM_ERROR; |
| 2154 | #endif |
| 2155 | stream.next_in = pSource; |
| 2156 | stream.avail_in = (mz_uint32)source_len; |
| 2157 | stream.next_out = pDest; |
| 2158 | stream.avail_out = (mz_uint32)*pDest_len; |
| 2159 | |
| 2160 | status = mz_deflateInit(&stream, level); |
| 2161 | if (status != MZ_OK) |
| 2162 | return status; |
| 2163 | |
| 2164 | status = mz_deflate(&stream, MZ_FINISH); |
| 2165 | if (status != MZ_STREAM_END) { |
| 2166 | mz_deflateEnd(&stream); |
| 2167 | return (status == MZ_OK) ? MZ_BUF_ERROR : status; |
| 2168 | } |
| 2169 | |
| 2170 | *pDest_len = stream.total_out; |
| 2171 | return mz_deflateEnd(&stream); |
| 2172 | } |
| 2173 | |
| 2174 | int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, |
| 2175 | const unsigned char *pSource, mz_ulong source_len) { |
| 2176 | return mz_compress2(pDest, pDest_len, pSource, source_len, |
| 2177 | MZ_DEFAULT_COMPRESSION); |
| 2178 | } |
| 2179 | |
| 2180 | mz_ulong mz_compressBound(mz_ulong source_len) { |
| 2181 | return mz_deflateBound(NULL, source_len); |
| 2182 | } |
| 2183 | |
| 2184 | #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/ |
| 2185 | |
| 2186 | #ifndef MINIZ_NO_INFLATE_APIS |
| 2187 | |
| 2188 | typedef struct { |
| 2189 | tinfl_decompressor m_decomp; |
| 2190 | mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed; |
| 2191 | int m_window_bits; |
| 2192 | mz_uint8 m_dict[TINFL_LZ_DICT_SIZE]; |
| 2193 | tinfl_status m_last_status; |
| 2194 | } inflate_state; |
| 2195 | |
| 2196 | int mz_inflateInit2(mz_streamp pStream, int window_bits) { |
| 2197 | inflate_state *pDecomp; |
| 2198 | if (!pStream) |
| 2199 | return MZ_STREAM_ERROR; |
| 2200 | if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && |
| 2201 | (-window_bits != MZ_DEFAULT_WINDOW_BITS)) |
| 2202 | return MZ_PARAM_ERROR; |
| 2203 | |
| 2204 | pStream->data_type = 0; |
| 2205 | pStream->adler = 0; |
| 2206 | pStream->msg = NULL; |
| 2207 | pStream->total_in = 0; |
| 2208 | pStream->total_out = 0; |
| 2209 | pStream->reserved = 0; |
| 2210 | if (!pStream->zalloc) |
| 2211 | pStream->zalloc = miniz_def_alloc_func; |
| 2212 | if (!pStream->zfree) |
| 2213 | pStream->zfree = miniz_def_free_func; |
| 2214 | |
| 2215 | pDecomp = (inflate_state *)pStream->zalloc(pStream->opaque, 1, |
| 2216 | sizeof(inflate_state)); |
| 2217 | if (!pDecomp) |
| 2218 | return MZ_MEM_ERROR; |
| 2219 | |
| 2220 | pStream->state = (struct mz_internal_state *)pDecomp; |
| 2221 | |
| 2222 | tinfl_init(&pDecomp->m_decomp); |
| 2223 | pDecomp->m_dict_ofs = 0; |
| 2224 | pDecomp->m_dict_avail = 0; |
| 2225 | pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; |
| 2226 | pDecomp->m_first_call = 1; |
| 2227 | pDecomp->m_has_flushed = 0; |
| 2228 | pDecomp->m_window_bits = window_bits; |
| 2229 | |
| 2230 | return MZ_OK; |
| 2231 | } |
| 2232 | |
| 2233 | int mz_inflateInit(mz_streamp pStream) { |
| 2234 | return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS); |
| 2235 | } |
| 2236 | |
| 2237 | int mz_inflateReset(mz_streamp pStream) { |
| 2238 | inflate_state *pDecomp; |
| 2239 | if (!pStream) |
| 2240 | return MZ_STREAM_ERROR; |
| 2241 | |
| 2242 | pStream->data_type = 0; |
| 2243 | pStream->adler = 0; |
| 2244 | pStream->msg = NULL; |
| 2245 | pStream->total_in = 0; |
| 2246 | pStream->total_out = 0; |
| 2247 | pStream->reserved = 0; |
| 2248 | |
| 2249 | pDecomp = (inflate_state *)pStream->state; |
| 2250 | |
| 2251 | tinfl_init(&pDecomp->m_decomp); |
| 2252 | pDecomp->m_dict_ofs = 0; |
| 2253 | pDecomp->m_dict_avail = 0; |
| 2254 | pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT; |
| 2255 | pDecomp->m_first_call = 1; |
| 2256 | pDecomp->m_has_flushed = 0; |
| 2257 | /* pDecomp->m_window_bits = window_bits */; |
| 2258 | |
| 2259 | return MZ_OK; |
| 2260 | } |
| 2261 | |
| 2262 | int mz_inflate(mz_streamp pStream, int flush) { |
| 2263 | inflate_state *pState; |
| 2264 | mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32; |
| 2265 | size_t in_bytes, out_bytes, orig_avail_in; |
| 2266 | tinfl_status status; |
| 2267 | |
| 2268 | if ((!pStream) || (!pStream->state)) |
| 2269 | return MZ_STREAM_ERROR; |
| 2270 | if (flush == MZ_PARTIAL_FLUSH) |
| 2271 | flush = MZ_SYNC_FLUSH; |
| 2272 | if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) |
| 2273 | return MZ_STREAM_ERROR; |
| 2274 | |
| 2275 | pState = (inflate_state *)pStream->state; |
| 2276 | if (pState->m_window_bits > 0) |
| 2277 | decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER; |
| 2278 | orig_avail_in = pStream->avail_in; |
| 2279 | |
| 2280 | first_call = pState->m_first_call; |
| 2281 | pState->m_first_call = 0; |
| 2282 | if (pState->m_last_status < 0) |
| 2283 | return MZ_DATA_ERROR; |
| 2284 | |
| 2285 | if (pState->m_has_flushed && (flush != MZ_FINISH)) |
| 2286 | return MZ_STREAM_ERROR; |
| 2287 | pState->m_has_flushed |= (flush == MZ_FINISH); |
| 2288 | |
| 2289 | if ((flush == MZ_FINISH) && (first_call)) { |
| 2290 | /* MZ_FINISH on the first call implies that the input and output buffers are |
| 2291 | * large enough to hold the entire compressed/decompressed file. */ |
| 2292 | decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF; |
| 2293 | in_bytes = pStream->avail_in; |
| 2294 | out_bytes = pStream->avail_out; |
| 2295 | status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, |
| 2296 | pStream->next_out, pStream->next_out, &out_bytes, |
| 2297 | decomp_flags); |
| 2298 | pState->m_last_status = status; |
| 2299 | pStream->next_in += (mz_uint)in_bytes; |
| 2300 | pStream->avail_in -= (mz_uint)in_bytes; |
| 2301 | pStream->total_in += (mz_uint)in_bytes; |
| 2302 | pStream->adler = tinfl_get_adler32(&pState->m_decomp); |
| 2303 | pStream->next_out += (mz_uint)out_bytes; |
| 2304 | pStream->avail_out -= (mz_uint)out_bytes; |
| 2305 | pStream->total_out += (mz_uint)out_bytes; |
| 2306 | |
| 2307 | if (status < 0) |
| 2308 | return MZ_DATA_ERROR; |
| 2309 | else if (status != TINFL_STATUS_DONE) { |
| 2310 | pState->m_last_status = TINFL_STATUS_FAILED; |
| 2311 | return MZ_BUF_ERROR; |
| 2312 | } |
| 2313 | return MZ_STREAM_END; |
| 2314 | } |
| 2315 | /* flush != MZ_FINISH then we must assume there's more input. */ |
| 2316 | if (flush != MZ_FINISH) |
| 2317 | decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT; |
| 2318 | |
| 2319 | if (pState->m_dict_avail) { |
| 2320 | n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); |
| 2321 | memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); |
| 2322 | pStream->next_out += n; |
| 2323 | pStream->avail_out -= n; |
| 2324 | pStream->total_out += n; |
| 2325 | pState->m_dict_avail -= n; |
| 2326 | pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); |
| 2327 | return ((pState->m_last_status == TINFL_STATUS_DONE) && |
| 2328 | (!pState->m_dict_avail)) |
| 2329 | ? MZ_STREAM_END |
| 2330 | : MZ_OK; |
| 2331 | } |
| 2332 | |
| 2333 | for (;;) { |
| 2334 | in_bytes = pStream->avail_in; |
| 2335 | out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs; |
| 2336 | |
| 2337 | status = tinfl_decompress( |
| 2338 | &pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, |
| 2339 | pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags); |
| 2340 | pState->m_last_status = status; |
| 2341 | |
| 2342 | pStream->next_in += (mz_uint)in_bytes; |
| 2343 | pStream->avail_in -= (mz_uint)in_bytes; |
| 2344 | pStream->total_in += (mz_uint)in_bytes; |
| 2345 | pStream->adler = tinfl_get_adler32(&pState->m_decomp); |
| 2346 | |
| 2347 | pState->m_dict_avail = (mz_uint)out_bytes; |
| 2348 | |
| 2349 | n = MZ_MIN(pState->m_dict_avail, pStream->avail_out); |
| 2350 | memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n); |
| 2351 | pStream->next_out += n; |
| 2352 | pStream->avail_out -= n; |
| 2353 | pStream->total_out += n; |
| 2354 | pState->m_dict_avail -= n; |
| 2355 | pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1); |
| 2356 | |
| 2357 | if (status < 0) |
| 2358 | return MZ_DATA_ERROR; /* Stream is corrupted (there could be some |
| 2359 | uncompressed data left in the output dictionary - |
| 2360 | oh well). */ |
| 2361 | else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in)) |
| 2362 | return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress |
| 2363 | without supplying more input or by setting flush |
| 2364 | to MZ_FINISH. */ |
| 2365 | else if (flush == MZ_FINISH) { |
| 2366 | /* The output buffer MUST be large to hold the remaining uncompressed data |
| 2367 | * when flush==MZ_FINISH. */ |
| 2368 | if (status == TINFL_STATUS_DONE) |
| 2369 | return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END; |
| 2370 | /* status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's |
| 2371 | * at least 1 more byte on the way. If there's no more room left in the |
| 2372 | * output buffer then something is wrong. */ |
| 2373 | else if (!pStream->avail_out) |
| 2374 | return MZ_BUF_ERROR; |
| 2375 | } else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || |
| 2376 | (!pStream->avail_out) || (pState->m_dict_avail)) |
| 2377 | break; |
| 2378 | } |
| 2379 | |
| 2380 | return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) |
| 2381 | ? MZ_STREAM_END |
| 2382 | : MZ_OK; |
| 2383 | } |
| 2384 | |
| 2385 | int mz_inflateEnd(mz_streamp pStream) { |
| 2386 | if (!pStream) |
| 2387 | return MZ_STREAM_ERROR; |
| 2388 | if (pStream->state) { |
| 2389 | pStream->zfree(pStream->opaque, pStream->state); |
| 2390 | pStream->state = NULL; |
| 2391 | } |
| 2392 | return MZ_OK; |
| 2393 | } |
| 2394 | int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, |
| 2395 | const unsigned char *pSource, mz_ulong *pSource_len) { |
| 2396 | mz_stream stream; |
| 2397 | int status; |
| 2398 | memset(&stream, 0, sizeof(stream)); |
| 2399 | |
| 2400 | #if defined(__MINGW32__) || defined(__MINGW64__) || defined(__WATCOMC__) |
| 2401 | /* In case mz_ulong is 64-bits (argh I hate longs). */ |
| 2402 | #else |
| 2403 | if ((mz_uint64)(*pSource_len | *pDest_len) > 0xFFFFFFFFU) |
| 2404 | return MZ_PARAM_ERROR; |
| 2405 | #endif |
| 2406 | stream.next_in = pSource; |
| 2407 | stream.avail_in = (mz_uint32)*pSource_len; |
| 2408 | stream.next_out = pDest; |
| 2409 | stream.avail_out = (mz_uint32)*pDest_len; |
| 2410 | |
| 2411 | status = mz_inflateInit(&stream); |
| 2412 | if (status != MZ_OK) |
| 2413 | return status; |
| 2414 | |
| 2415 | status = mz_inflate(&stream, MZ_FINISH); |
| 2416 | *pSource_len = *pSource_len - stream.avail_in; |
| 2417 | if (status != MZ_STREAM_END) { |
| 2418 | mz_inflateEnd(&stream); |
| 2419 | return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR |
| 2420 | : status; |
| 2421 | } |
| 2422 | *pDest_len = stream.total_out; |
| 2423 | |
| 2424 | return mz_inflateEnd(&stream); |
| 2425 | } |
| 2426 | |
| 2427 | int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, |
| 2428 | const unsigned char *pSource, mz_ulong source_len) { |
| 2429 | return mz_uncompress2(pDest, pDest_len, pSource, &source_len); |
| 2430 | } |
| 2431 | |
| 2432 | #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/ |
| 2433 | |
| 2434 | const char *mz_error(int err) { |
| 2435 | static struct { |
| 2436 | int m_err; |
| 2437 | const char *m_pDesc; |
| 2438 | } s_error_descs[] = {{MZ_OK, ""}, |
| 2439 | {MZ_STREAM_END, "stream end"}, |
| 2440 | {MZ_NEED_DICT, "need dictionary"}, |
| 2441 | {MZ_ERRNO, "file error"}, |
| 2442 | {MZ_STREAM_ERROR, "stream error"}, |
| 2443 | {MZ_DATA_ERROR, "data error"}, |
| 2444 | {MZ_MEM_ERROR, "out of memory"}, |
| 2445 | {MZ_BUF_ERROR, "buf error"}, |
| 2446 | {MZ_VERSION_ERROR, "version error"}, |
| 2447 | {MZ_PARAM_ERROR, "parameter error"}}; |
| 2448 | mz_uint i; |
| 2449 | for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i) |
| 2450 | if (s_error_descs[i].m_err == err) |
| 2451 | return s_error_descs[i].m_pDesc; |
| 2452 | return NULL; |
| 2453 | } |
| 2454 | |
| 2455 | #endif /*MINIZ_NO_ZLIB_APIS */ |
| 2456 | |
| 2457 | #ifdef __cplusplus |
| 2458 | } |
| 2459 | #endif |
| 2460 | |
| 2461 | /* |
| 2462 | This is free and unencumbered software released into the public domain. |
| 2463 | |
| 2464 | Anyone is free to copy, modify, publish, use, compile, sell, or |
| 2465 | distribute this software, either in source code form or as a compiled |
| 2466 | binary, for any purpose, commercial or non-commercial, and by any |
| 2467 | means. |
| 2468 | |
| 2469 | In jurisdictions that recognize copyright laws, the author or authors |
| 2470 | of this software dedicate any and all copyright interest in the |
| 2471 | software to the public domain. We make this dedication for the benefit |
| 2472 | of the public at large and to the detriment of our heirs and |
| 2473 | successors. We intend this dedication to be an overt act of |
| 2474 | relinquishment in perpetuity of all present and future rights to this |
| 2475 | software under copyright law. |
| 2476 | |
| 2477 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 2478 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 2479 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 2480 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 2481 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 2482 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 2483 | OTHER DEALINGS IN THE SOFTWARE. |
| 2484 | |
| 2485 | For more information, please refer to <http://unlicense.org/> |
| 2486 | */ |
| 2487 | /************************************************************************** |
| 2488 | * |
| 2489 | * Copyright 2013-2014 RAD Game Tools and Valve Software |
| 2490 | * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC |
| 2491 | * All Rights Reserved. |
| 2492 | * |
| 2493 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 2494 | * of this software and associated documentation files (the "Software"), to deal |
| 2495 | * in the Software without restriction, including without limitation the rights |
| 2496 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 2497 | * copies of the Software, and to permit persons to whom the Software is |
| 2498 | * furnished to do so, subject to the following conditions: |
| 2499 | * |
| 2500 | * The above copyright notice and this permission notice shall be included in |
| 2501 | * all copies or substantial portions of the Software. |
| 2502 | * |
| 2503 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 2504 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 2505 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 2506 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 2507 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 2508 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 2509 | * THE SOFTWARE. |
| 2510 | * |
| 2511 | **************************************************************************/ |
| 2512 | |
| 2513 | #ifndef MINIZ_NO_DEFLATE_APIS |
| 2514 | |
| 2515 | #ifdef __cplusplus |
| 2516 | extern "C" { |
| 2517 | #endif |
| 2518 | |
| 2519 | /* ------------------- Low-level Compression (independent from all decompression |
| 2520 | * API's) */ |
| 2521 | |
| 2522 | /* Purposely making these tables static for faster init and thread safety. */ |
| 2523 | static const mz_uint16 s_tdefl_len_sym[256] = { |
| 2524 | 257, 258, 259, 260, 261, 262, 263, 264, 265, 265, 266, 266, 267, 267, 268, |
| 2525 | 268, 269, 269, 269, 269, 270, 270, 270, 270, 271, 271, 271, 271, 272, 272, |
| 2526 | 272, 272, 273, 273, 273, 273, 273, 273, 273, 273, 274, 274, 274, 274, 274, |
| 2527 | 274, 274, 274, 275, 275, 275, 275, 275, 275, 275, 275, 276, 276, 276, 276, |
| 2528 | 276, 276, 276, 276, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, |
| 2529 | 277, 277, 277, 277, 277, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, |
| 2530 | 278, 278, 278, 278, 278, 278, 279, 279, 279, 279, 279, 279, 279, 279, 279, |
| 2531 | 279, 279, 279, 279, 279, 279, 279, 280, 280, 280, 280, 280, 280, 280, 280, |
| 2532 | 280, 280, 280, 280, 280, 280, 280, 280, 281, 281, 281, 281, 281, 281, 281, |
| 2533 | 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, |
| 2534 | 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 282, 282, 282, 282, 282, |
| 2535 | 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, |
| 2536 | 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 283, 283, 283, |
| 2537 | 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, |
| 2538 | 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 284, |
| 2539 | 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, |
| 2540 | 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, |
| 2541 | 285}; |
| 2542 | |
| 2543 | static const mz_uint8 s_tdefl_len_extra[256] = { |
| 2544 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2545 | 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2546 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, |
| 2547 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 2548 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 2549 | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 2550 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 2551 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 2552 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 2553 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 2554 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0}; |
| 2555 | |
| 2556 | static const mz_uint8 s_tdefl_small_dist_sym[512] = { |
| 2557 | 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, |
| 2558 | 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, |
| 2559 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 2560 | 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, |
| 2561 | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, |
| 2562 | 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, |
| 2563 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, |
| 2564 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, |
| 2565 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, |
| 2566 | 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, |
| 2567 | 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 2568 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 2569 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 2570 | 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
| 2571 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
| 2572 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
| 2573 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
| 2574 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
| 2575 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
| 2576 | 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, |
| 2577 | 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, |
| 2578 | 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, |
| 2579 | 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, |
| 2580 | 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, |
| 2581 | 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, |
| 2582 | 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, |
| 2583 | 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17}; |
| 2584 | |
| 2585 | static const mz_uint8 s_tdefl_small_dist_extra[512] = { |
| 2586 | 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 2587 | 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 2588 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 2589 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 2590 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
| 2591 | 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 2592 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 2593 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 2594 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 2595 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
| 2596 | 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 2597 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 2598 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 2599 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 2600 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 2601 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 2602 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 2603 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 2604 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 2605 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 2606 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}; |
| 2607 | |
| 2608 | static const mz_uint8 s_tdefl_large_dist_sym[128] = { |
| 2609 | 0, 0, 18, 19, 20, 20, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, |
| 2610 | 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, |
| 2611 | 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 2612 | 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 2613 | 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, |
| 2614 | 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, |
| 2615 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29}; |
| 2616 | |
| 2617 | static const mz_uint8 s_tdefl_large_dist_extra[128] = { |
| 2618 | 0, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, |
| 2619 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, |
| 2620 | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, |
| 2621 | 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, |
| 2622 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, |
| 2623 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, |
| 2624 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13}; |
| 2625 | |
| 2626 | /* Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted |
| 2627 | * values. */ |
| 2628 | typedef struct { |
| 2629 | mz_uint16 m_key, m_sym_index; |
| 2630 | } tdefl_sym_freq; |
| 2631 | static tdefl_sym_freq *tdefl_radix_sort_syms(mz_uint num_syms, |
| 2632 | tdefl_sym_freq *pSyms0, |
| 2633 | tdefl_sym_freq *pSyms1) { |
| 2634 | mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2]; |
| 2635 | tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1; |
| 2636 | MZ_CLEAR_ARR(hist); |
| 2637 | for (i = 0; i < num_syms; i++) { |
| 2638 | mz_uint freq = pSyms0[i].m_key; |
| 2639 | hist[freq & 0xFF]++; |
| 2640 | hist[256 + ((freq >> 8) & 0xFF)]++; |
| 2641 | } |
| 2642 | while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256])) |
| 2643 | total_passes--; |
| 2644 | for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8) { |
| 2645 | const mz_uint32 *pHist = &hist[pass << 8]; |
| 2646 | mz_uint offsets[256], cur_ofs = 0; |
| 2647 | for (i = 0; i < 256; i++) { |
| 2648 | offsets[i] = cur_ofs; |
| 2649 | cur_ofs += pHist[i]; |
| 2650 | } |
| 2651 | for (i = 0; i < num_syms; i++) |
| 2652 | pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = |
| 2653 | pCur_syms[i]; |
| 2654 | { |
| 2655 | tdefl_sym_freq *t = pCur_syms; |
| 2656 | pCur_syms = pNew_syms; |
| 2657 | pNew_syms = t; |
| 2658 | } |
| 2659 | } |
| 2660 | return pCur_syms; |
| 2661 | } |
| 2662 | |
| 2663 | /* tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, |
| 2664 | * [email protected], Jyrki Katajainen, [email protected], November 1996. */ |
| 2665 | static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n) { |
| 2666 | int root, leaf, next, avbl, used, dpth; |
| 2667 | if (n == 0) |
| 2668 | return; |
| 2669 | else if (n == 1) { |
| 2670 | A[0].m_key = 1; |
| 2671 | return; |
| 2672 | } |
| 2673 | A[0].m_key += A[1].m_key; |
| 2674 | root = 0; |
| 2675 | leaf = 2; |
| 2676 | for (next = 1; next < n - 1; next++) { |
| 2677 | if (leaf >= n || A[root].m_key < A[leaf].m_key) { |
| 2678 | A[next].m_key = A[root].m_key; |
| 2679 | A[root++].m_key = (mz_uint16)next; |
| 2680 | } else |
| 2681 | A[next].m_key = A[leaf++].m_key; |
| 2682 | if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) { |
| 2683 | A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key); |
| 2684 | A[root++].m_key = (mz_uint16)next; |
| 2685 | } else |
| 2686 | A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key); |
| 2687 | } |
| 2688 | A[n - 2].m_key = 0; |
| 2689 | for (next = n - 3; next >= 0; next--) |
| 2690 | A[next].m_key = A[A[next].m_key].m_key + 1; |
| 2691 | avbl = 1; |
| 2692 | used = dpth = 0; |
| 2693 | root = n - 2; |
| 2694 | next = n - 1; |
| 2695 | while (avbl > 0) { |
| 2696 | while (root >= 0 && (int)A[root].m_key == dpth) { |
| 2697 | used++; |
| 2698 | root--; |
| 2699 | } |
| 2700 | while (avbl > used) { |
| 2701 | A[next--].m_key = (mz_uint16)(dpth); |
| 2702 | avbl--; |
| 2703 | } |
| 2704 | avbl = 2 * used; |
| 2705 | dpth++; |
| 2706 | used = 0; |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | /* Limits canonical Huffman code table's max code size. */ |
| 2711 | enum { TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32 }; |
| 2712 | static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, |
| 2713 | int code_list_len, |
| 2714 | int max_code_size) { |
| 2715 | int i; |
| 2716 | mz_uint32 total = 0; |
| 2717 | if (code_list_len <= 1) |
| 2718 | return; |
| 2719 | for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++) |
| 2720 | pNum_codes[max_code_size] += pNum_codes[i]; |
| 2721 | for (i = max_code_size; i > 0; i--) |
| 2722 | total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i)); |
| 2723 | while (total != (1UL << max_code_size)) { |
| 2724 | pNum_codes[max_code_size]--; |
| 2725 | for (i = max_code_size - 1; i > 0; i--) |
| 2726 | if (pNum_codes[i]) { |
| 2727 | pNum_codes[i]--; |
| 2728 | pNum_codes[i + 1] += 2; |
| 2729 | break; |
| 2730 | } |
| 2731 | total--; |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, |
| 2736 | int table_len, int code_size_limit, |
| 2737 | int static_table) { |
| 2738 | int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE]; |
| 2739 | mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1]; |
| 2740 | MZ_CLEAR_ARR(num_codes); |
| 2741 | if (static_table) { |
| 2742 | for (i = 0; i < table_len; i++) |
| 2743 | num_codes[d->m_huff_code_sizes[table_num][i]]++; |
| 2744 | } else { |
| 2745 | tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], |
| 2746 | *pSyms; |
| 2747 | int num_used_syms = 0; |
| 2748 | const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0]; |
| 2749 | for (i = 0; i < table_len; i++) |
| 2750 | if (pSym_count[i]) { |
| 2751 | syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i]; |
| 2752 | syms0[num_used_syms++].m_sym_index = (mz_uint16)i; |
| 2753 | } |
| 2754 | |
| 2755 | pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1); |
| 2756 | tdefl_calculate_minimum_redundancy(pSyms, num_used_syms); |
| 2757 | |
| 2758 | for (i = 0; i < num_used_syms; i++) |
| 2759 | num_codes[pSyms[i].m_key]++; |
| 2760 | |
| 2761 | tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, |
| 2762 | code_size_limit); |
| 2763 | |
| 2764 | MZ_CLEAR_ARR(d->m_huff_code_sizes[table_num]); |
| 2765 | MZ_CLEAR_ARR(d->m_huff_codes[table_num]); |
| 2766 | for (i = 1, j = num_used_syms; i <= code_size_limit; i++) |
| 2767 | for (l = num_codes[i]; l > 0; l--) |
| 2768 | d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i); |
| 2769 | } |
| 2770 | |
| 2771 | next_code[1] = 0; |
| 2772 | for (j = 0, i = 2; i <= code_size_limit; i++) |
| 2773 | next_code[i] = j = ((j + num_codes[i - 1]) << 1); |
| 2774 | |
| 2775 | for (i = 0; i < table_len; i++) { |
| 2776 | mz_uint rev_code = 0, code, code_size; |
| 2777 | if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0) |
| 2778 | continue; |
| 2779 | code = next_code[code_size]++; |
| 2780 | for (l = code_size; l > 0; l--, code >>= 1) |
| 2781 | rev_code = (rev_code << 1) | (code & 1); |
| 2782 | d->m_huff_codes[table_num][i] = (mz_uint16)rev_code; |
| 2783 | } |
| 2784 | } |
| 2785 | |
| 2786 | #define TDEFL_PUT_BITS(b, l) \ |
| 2787 | do { \ |
| 2788 | mz_uint bits = b; \ |
| 2789 | mz_uint len = l; \ |
| 2790 | MZ_ASSERT(bits <= ((1U << len) - 1U)); \ |
| 2791 | d->m_bit_buffer |= (bits << d->m_bits_in); \ |
| 2792 | d->m_bits_in += len; \ |
| 2793 | while (d->m_bits_in >= 8) { \ |
| 2794 | if (d->m_pOutput_buf < d->m_pOutput_buf_end) \ |
| 2795 | *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \ |
| 2796 | d->m_bit_buffer >>= 8; \ |
| 2797 | d->m_bits_in -= 8; \ |
| 2798 | } \ |
| 2799 | } \ |
| 2800 | MZ_MACRO_END |
| 2801 | |
| 2802 | #define TDEFL_RLE_PREV_CODE_SIZE() \ |
| 2803 | { \ |
| 2804 | if (rle_repeat_count) { \ |
| 2805 | if (rle_repeat_count < 3) { \ |
| 2806 | d->m_huff_count[2][prev_code_size] = \ |
| 2807 | (mz_uint16)(d->m_huff_count[2][prev_code_size] + \ |
| 2808 | rle_repeat_count); \ |
| 2809 | while (rle_repeat_count--) \ |
| 2810 | packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \ |
| 2811 | } else { \ |
| 2812 | d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \ |
| 2813 | packed_code_sizes[num_packed_code_sizes++] = 16; \ |
| 2814 | packed_code_sizes[num_packed_code_sizes++] = \ |
| 2815 | (mz_uint8)(rle_repeat_count - 3); \ |
| 2816 | } \ |
| 2817 | rle_repeat_count = 0; \ |
| 2818 | } \ |
| 2819 | } |
| 2820 | |
| 2821 | #define TDEFL_RLE_ZERO_CODE_SIZE() \ |
| 2822 | { \ |
| 2823 | if (rle_z_count) { \ |
| 2824 | if (rle_z_count < 3) { \ |
| 2825 | d->m_huff_count[2][0] = \ |
| 2826 | (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \ |
| 2827 | while (rle_z_count--) \ |
| 2828 | packed_code_sizes[num_packed_code_sizes++] = 0; \ |
| 2829 | } else if (rle_z_count <= 10) { \ |
| 2830 | d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \ |
| 2831 | packed_code_sizes[num_packed_code_sizes++] = 17; \ |
| 2832 | packed_code_sizes[num_packed_code_sizes++] = \ |
| 2833 | (mz_uint8)(rle_z_count - 3); \ |
| 2834 | } else { \ |
| 2835 | d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \ |
| 2836 | packed_code_sizes[num_packed_code_sizes++] = 18; \ |
| 2837 | packed_code_sizes[num_packed_code_sizes++] = \ |
| 2838 | (mz_uint8)(rle_z_count - 11); \ |
| 2839 | } \ |
| 2840 | rle_z_count = 0; \ |
| 2841 | } \ |
| 2842 | } |
| 2843 | |
| 2844 | static const mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = { |
| 2845 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; |
| 2846 | |
| 2847 | static void tdefl_start_dynamic_block(tdefl_compressor *d) { |
| 2848 | int num_lit_codes, num_dist_codes, num_bit_lengths; |
| 2849 | mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, |
| 2850 | rle_repeat_count, packed_code_sizes_index; |
| 2851 | mz_uint8 |
| 2852 | code_sizes_to_pack[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], |
| 2853 | packed_code_sizes[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], |
| 2854 | prev_code_size = 0xFF; |
| 2855 | |
| 2856 | d->m_huff_count[0][256] = 1; |
| 2857 | |
| 2858 | tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE); |
| 2859 | tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE); |
| 2860 | |
| 2861 | for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--) |
| 2862 | if (d->m_huff_code_sizes[0][num_lit_codes - 1]) |
| 2863 | break; |
| 2864 | for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--) |
| 2865 | if (d->m_huff_code_sizes[1][num_dist_codes - 1]) |
| 2866 | break; |
| 2867 | |
| 2868 | memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes); |
| 2869 | memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], |
| 2870 | num_dist_codes); |
| 2871 | total_code_sizes_to_pack = num_lit_codes + num_dist_codes; |
| 2872 | num_packed_code_sizes = 0; |
| 2873 | rle_z_count = 0; |
| 2874 | rle_repeat_count = 0; |
| 2875 | |
| 2876 | memset(&d->m_huff_count[2][0], 0, |
| 2877 | sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2); |
| 2878 | for (i = 0; i < total_code_sizes_to_pack; i++) { |
| 2879 | mz_uint8 code_size = code_sizes_to_pack[i]; |
| 2880 | if (!code_size) { |
| 2881 | TDEFL_RLE_PREV_CODE_SIZE(); |
| 2882 | if (++rle_z_count == 138) { |
| 2883 | TDEFL_RLE_ZERO_CODE_SIZE(); |
| 2884 | } |
| 2885 | } else { |
| 2886 | TDEFL_RLE_ZERO_CODE_SIZE(); |
| 2887 | if (code_size != prev_code_size) { |
| 2888 | TDEFL_RLE_PREV_CODE_SIZE(); |
| 2889 | d->m_huff_count[2][code_size] = |
| 2890 | (mz_uint16)(d->m_huff_count[2][code_size] + 1); |
| 2891 | packed_code_sizes[num_packed_code_sizes++] = code_size; |
| 2892 | } else if (++rle_repeat_count == 6) { |
| 2893 | TDEFL_RLE_PREV_CODE_SIZE(); |
| 2894 | } |
| 2895 | } |
| 2896 | prev_code_size = code_size; |
| 2897 | } |
| 2898 | if (rle_repeat_count) { |
| 2899 | TDEFL_RLE_PREV_CODE_SIZE(); |
| 2900 | } else { |
| 2901 | TDEFL_RLE_ZERO_CODE_SIZE(); |
| 2902 | } |
| 2903 | |
| 2904 | tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE); |
| 2905 | |
| 2906 | TDEFL_PUT_BITS(2, 2); |
| 2907 | |
| 2908 | TDEFL_PUT_BITS(num_lit_codes - 257, 5); |
| 2909 | TDEFL_PUT_BITS(num_dist_codes - 1, 5); |
| 2910 | |
| 2911 | for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--) |
| 2912 | if (d->m_huff_code_sizes |
| 2913 | [2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]]) |
| 2914 | break; |
| 2915 | num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1)); |
| 2916 | TDEFL_PUT_BITS(num_bit_lengths - 4, 4); |
| 2917 | for (i = 0; (int)i < num_bit_lengths; i++) |
| 2918 | TDEFL_PUT_BITS( |
| 2919 | d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3); |
| 2920 | |
| 2921 | for (packed_code_sizes_index = 0; |
| 2922 | packed_code_sizes_index < num_packed_code_sizes;) { |
| 2923 | mz_uint code = packed_code_sizes[packed_code_sizes_index++]; |
| 2924 | MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2); |
| 2925 | TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]); |
| 2926 | if (code >= 16) |
| 2927 | TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], |
| 2928 | "\02\03\07"[code - 16]); |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | static void tdefl_start_static_block(tdefl_compressor *d) { |
| 2933 | mz_uint i; |
| 2934 | mz_uint8 *p = &d->m_huff_code_sizes[0][0]; |
| 2935 | |
| 2936 | for (i = 0; i <= 143; ++i) |
| 2937 | *p++ = 8; |
| 2938 | for (; i <= 255; ++i) |
| 2939 | *p++ = 9; |
| 2940 | for (; i <= 279; ++i) |
| 2941 | *p++ = 7; |
| 2942 | for (; i <= 287; ++i) |
| 2943 | *p++ = 8; |
| 2944 | |
| 2945 | memset(d->m_huff_code_sizes[1], 5, 32); |
| 2946 | |
| 2947 | tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE); |
| 2948 | tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE); |
| 2949 | |
| 2950 | TDEFL_PUT_BITS(1, 2); |
| 2951 | } |
| 2952 | |
| 2953 | static const mz_uint mz_bitmasks[17] = { |
| 2954 | 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, |
| 2955 | 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF}; |
| 2956 | |
| 2957 | #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && \ |
| 2958 | MINIZ_HAS_64BIT_REGISTERS |
| 2959 | static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d) { |
| 2960 | mz_uint flags; |
| 2961 | mz_uint8 *pLZ_codes; |
| 2962 | mz_uint8 *pOutput_buf = d->m_pOutput_buf; |
| 2963 | mz_uint8 *pLZ_code_buf_end = d->m_pLZ_code_buf; |
| 2964 | mz_uint64 bit_buffer = d->m_bit_buffer; |
| 2965 | mz_uint bits_in = d->m_bits_in; |
| 2966 | |
| 2967 | #define TDEFL_PUT_BITS_FAST(b, l) \ |
| 2968 | { \ |
| 2969 | bit_buffer |= (((mz_uint64)(b)) << bits_in); \ |
| 2970 | bits_in += (l); \ |
| 2971 | } |
| 2972 | |
| 2973 | flags = 1; |
| 2974 | for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; |
| 2975 | flags >>= 1) { |
| 2976 | if (flags == 1) |
| 2977 | flags = *pLZ_codes++ | 0x100; |
| 2978 | |
| 2979 | if (flags & 1) { |
| 2980 | mz_uint s0, s1, n0, n1, sym, num_extra_bits; |
| 2981 | mz_uint match_len = pLZ_codes[0]; |
| 2982 | mz_uint match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8)); |
| 2983 | pLZ_codes += 3; |
| 2984 | |
| 2985 | MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); |
| 2986 | TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], |
| 2987 | d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); |
| 2988 | TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], |
| 2989 | s_tdefl_len_extra[match_len]); |
| 2990 | |
| 2991 | /* This sequence coaxes MSVC into using cmov's vs. jmp's. */ |
| 2992 | s0 = s_tdefl_small_dist_sym[match_dist & 511]; |
| 2993 | n0 = s_tdefl_small_dist_extra[match_dist & 511]; |
| 2994 | s1 = s_tdefl_large_dist_sym[match_dist >> 8]; |
| 2995 | n1 = s_tdefl_large_dist_extra[match_dist >> 8]; |
| 2996 | sym = (match_dist < 512) ? s0 : s1; |
| 2997 | num_extra_bits = (match_dist < 512) ? n0 : n1; |
| 2998 | |
| 2999 | MZ_ASSERT(d->m_huff_code_sizes[1][sym]); |
| 3000 | TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], |
| 3001 | d->m_huff_code_sizes[1][sym]); |
| 3002 | TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], |
| 3003 | num_extra_bits); |
| 3004 | } else { |
| 3005 | mz_uint lit = *pLZ_codes++; |
| 3006 | MZ_ASSERT(d->m_huff_code_sizes[0][lit]); |
| 3007 | TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], |
| 3008 | d->m_huff_code_sizes[0][lit]); |
| 3009 | |
| 3010 | if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) { |
| 3011 | flags >>= 1; |
| 3012 | lit = *pLZ_codes++; |
| 3013 | MZ_ASSERT(d->m_huff_code_sizes[0][lit]); |
| 3014 | TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], |
| 3015 | d->m_huff_code_sizes[0][lit]); |
| 3016 | |
| 3017 | if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) { |
| 3018 | flags >>= 1; |
| 3019 | lit = *pLZ_codes++; |
| 3020 | MZ_ASSERT(d->m_huff_code_sizes[0][lit]); |
| 3021 | TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], |
| 3022 | d->m_huff_code_sizes[0][lit]); |
| 3023 | } |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | if (pOutput_buf >= d->m_pOutput_buf_end) |
| 3028 | return MZ_FALSE; |
| 3029 | |
| 3030 | memcpy(pOutput_buf, &bit_buffer, sizeof(mz_uint64)); |
| 3031 | pOutput_buf += (bits_in >> 3); |
| 3032 | bit_buffer >>= (bits_in & ~7); |
| 3033 | bits_in &= 7; |
| 3034 | } |
| 3035 | |
| 3036 | #undef TDEFL_PUT_BITS_FAST |
| 3037 | |
| 3038 | d->m_pOutput_buf = pOutput_buf; |
| 3039 | d->m_bits_in = 0; |
| 3040 | d->m_bit_buffer = 0; |
| 3041 | |
| 3042 | while (bits_in) { |
| 3043 | mz_uint32 n = MZ_MIN(bits_in, 16); |
| 3044 | TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n); |
| 3045 | bit_buffer >>= n; |
| 3046 | bits_in -= n; |
| 3047 | } |
| 3048 | |
| 3049 | TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); |
| 3050 | |
| 3051 | return (d->m_pOutput_buf < d->m_pOutput_buf_end); |
| 3052 | } |
| 3053 | #else |
| 3054 | static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d) { |
| 3055 | mz_uint flags; |
| 3056 | mz_uint8 *pLZ_codes; |
| 3057 | |
| 3058 | flags = 1; |
| 3059 | for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; |
| 3060 | flags >>= 1) { |
| 3061 | if (flags == 1) |
| 3062 | flags = *pLZ_codes++ | 0x100; |
| 3063 | if (flags & 1) { |
| 3064 | mz_uint sym, num_extra_bits; |
| 3065 | mz_uint match_len = pLZ_codes[0], |
| 3066 | match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8)); |
| 3067 | pLZ_codes += 3; |
| 3068 | |
| 3069 | MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); |
| 3070 | TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], |
| 3071 | d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); |
| 3072 | TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], |
| 3073 | s_tdefl_len_extra[match_len]); |
| 3074 | |
| 3075 | if (match_dist < 512) { |
| 3076 | sym = s_tdefl_small_dist_sym[match_dist]; |
| 3077 | num_extra_bits = s_tdefl_small_dist_extra[match_dist]; |
| 3078 | } else { |
| 3079 | sym = s_tdefl_large_dist_sym[match_dist >> 8]; |
| 3080 | num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8]; |
| 3081 | } |
| 3082 | MZ_ASSERT(d->m_huff_code_sizes[1][sym]); |
| 3083 | TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]); |
| 3084 | TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits); |
| 3085 | } else { |
| 3086 | mz_uint lit = *pLZ_codes++; |
| 3087 | MZ_ASSERT(d->m_huff_code_sizes[0][lit]); |
| 3088 | TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); |
| 3089 | } |
| 3090 | } |
| 3091 | |
| 3092 | TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); |
| 3093 | |
| 3094 | return (d->m_pOutput_buf < d->m_pOutput_buf_end); |
| 3095 | } |
| 3096 | #endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && \ |
| 3097 | MINIZ_HAS_64BIT_REGISTERS */ |
| 3098 | |
| 3099 | static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block) { |
| 3100 | if (static_block) |
| 3101 | tdefl_start_static_block(d); |
| 3102 | else |
| 3103 | tdefl_start_dynamic_block(d); |
| 3104 | return tdefl_compress_lz_codes(d); |
| 3105 | } |
| 3106 | |
| 3107 | static const mz_uint s_tdefl_num_probes[11] = {0, 1, 6, 32, 16, 32, |
| 3108 | 128, 256, 512, 768, 1500}; |
| 3109 | |
| 3110 | static int tdefl_flush_block(tdefl_compressor *d, int flush) { |
| 3111 | mz_uint saved_bit_buf, saved_bits_in; |
| 3112 | mz_uint8 *pSaved_output_buf; |
| 3113 | mz_bool comp_block_succeeded = MZ_FALSE; |
| 3114 | int n, use_raw_block = |
| 3115 | ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && |
| 3116 | (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size; |
| 3117 | mz_uint8 *pOutput_buf_start = |
| 3118 | ((d->m_pPut_buf_func == NULL) && |
| 3119 | ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE)) |
| 3120 | ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs) |
| 3121 | : d->m_output_buf; |
| 3122 | |
| 3123 | d->m_pOutput_buf = pOutput_buf_start; |
| 3124 | d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16; |
| 3125 | |
| 3126 | MZ_ASSERT(!d->m_output_flush_remaining); |
| 3127 | d->m_output_flush_ofs = 0; |
| 3128 | d->m_output_flush_remaining = 0; |
| 3129 | |
| 3130 | *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left); |
| 3131 | d->m_pLZ_code_buf -= (d->m_num_flags_left == 8); |
| 3132 | |
| 3133 | if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) { |
| 3134 | const mz_uint8 cmf = 0x78; |
| 3135 | mz_uint8 flg, flevel = 3; |
| 3136 | mz_uint header, i, mz_un = sizeof(s_tdefl_num_probes) / sizeof(mz_uint); |
| 3137 | |
| 3138 | /* Determine compression level by reversing the process in |
| 3139 | * tdefl_create_comp_flags_from_zip_params() */ |
| 3140 | for (i = 0; i < mz_un; i++) |
| 3141 | if (s_tdefl_num_probes[i] == (d->m_flags & 0xFFF)) |
| 3142 | break; |
| 3143 | |
| 3144 | if (i < 2) |
| 3145 | flevel = 0; |
| 3146 | else if (i < 6) |
| 3147 | flevel = 1; |
| 3148 | else if (i == 6) |
| 3149 | flevel = 2; |
| 3150 | |
| 3151 | header = cmf << 8 | (flevel << 6); |
| 3152 | header += 31 - (header % 31); |
| 3153 | flg = header & 0xFF; |
| 3154 | |
| 3155 | TDEFL_PUT_BITS(cmf, 8); |
| 3156 | TDEFL_PUT_BITS(flg, 8); |
| 3157 | } |
| 3158 | |
| 3159 | TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1); |
| 3160 | |
| 3161 | pSaved_output_buf = d->m_pOutput_buf; |
| 3162 | saved_bit_buf = d->m_bit_buffer; |
| 3163 | saved_bits_in = d->m_bits_in; |
| 3164 | |
| 3165 | if (!use_raw_block) |
| 3166 | comp_block_succeeded = |
| 3167 | tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || |
| 3168 | (d->m_total_lz_bytes < 48)); |
| 3169 | |