├── nim.cfg ├── tests ├── nim.cfg └── miniz_test.nim ├── .gitignore ├── README.md ├── nim_miniz.nimble ├── LICENSE └── src ├── nim_miniz.nim └── miniz.h /nim.cfg: -------------------------------------------------------------------------------- 1 | path = "$projectPath/src" 2 | -------------------------------------------------------------------------------- /tests/nim.cfg: -------------------------------------------------------------------------------- 1 | path = "$projectPath/../src" 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | nimcache/ 2 | miniz 3 | miniz_test 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nim-miniz 2 | Nim wrapper for the miniz library. 3 | -------------------------------------------------------------------------------- /nim_miniz.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "1.1.0" 4 | author = "Fabio Cevasco" 5 | description = "Nim wrapper for miniz" 6 | license = "MIT" 7 | 8 | installExt = @["nim", "c", "h"] 9 | 10 | srcDir = "src" 11 | skipDirs = @["tests"] 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Fabio Cevasco 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /tests/miniz_test.nim: -------------------------------------------------------------------------------- 1 | import ../src/nim_miniz 2 | import unittest 3 | import os 4 | 5 | const a_txt = "hello A from a.txt\n" 6 | const b_txt = "hello B from b.txt\n" 7 | 8 | suite "Zip Suite": 9 | test "that creating and a zip file works": 10 | var zip:Zip 11 | check open(zip, "testing.zip", fmWrite) 12 | 13 | var tmp:File 14 | check open(tmp, "a.txt", fmWrite) 15 | tmp.write(a_txt) 16 | tmp.close() 17 | check open(tmp, "B.txt", fmWrite) 18 | tmp.write(b_txt) 19 | tmp.close() 20 | 21 | check zip.len == 0 22 | zip.add_file("a.txt", archivePath="ooo/a.txt") 23 | check zip.len == 1 24 | zip.add_file("B.txt", archivePath="ooo/B.txt") 25 | check zip.len == 2 26 | 27 | removeFile("a.txt") 28 | removeFile("B.txt") 29 | 30 | zip.close() 31 | 32 | 33 | check open(zip, "testing.zip", fmRead) 34 | check zip.len == 2 35 | 36 | expect KeyError: 37 | discard zip.extract_file("AA.txt") 38 | 39 | expect KeyError: 40 | # default is case sensitive. 41 | discard zip.extract_file("A.txt") 42 | 43 | check zip.extract_file("a.txt") == "ooo/a.txt" 44 | check fileExists("ooo/a.txt") 45 | check $("ooo/a.txt".readFile()) == a_txt 46 | 47 | check zip.extract_file("a.txt", destDir="ooo/ttt/") == "ooo/ttt/ooo/a.txt" 48 | check fileExists("ooo/ttt/ooo/a.txt") 49 | check $("ooo/ttt/ooo/a.txt".readFile()) == a_txt 50 | check zip.extract_file_to_string("a.txt") == a_txt 51 | 52 | check zip.extract_file("B.txt", destDir="ooo/bbb/").readFile() == b_txt 53 | check fileExists("ooo/bbb/ooo/B.txt") 54 | check zip.extract_file_to_string("B.txt") == b_txt 55 | 56 | removeDir("ooo") 57 | removeFile("testing.zip") 58 | 59 | -------------------------------------------------------------------------------- /src/nim_miniz.nim: -------------------------------------------------------------------------------- 1 | {.compile: "libminiz.c".} 2 | 3 | import strutils 4 | 5 | when defined(i386) or defined(ia64): 6 | const 7 | MINIZ_X86_OR_X64_CPU* = 1 8 | # when little endian... 9 | #const 10 | # MINIZ_LITTLE_ENDIAN* = 1 11 | when defined(MINIZ_X86_OR_X64_CPU): 12 | # Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses. 13 | const 14 | MINIZ_USE_UNALIGNED_LOADS_AND_STORES* = 1 15 | when defined(ia64): 16 | # Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are reasonably fast (and don't involve compiler generated calls to helper functions). 17 | const 18 | MINIZ_HAS_64BIT_REGISTERS* = 1 19 | # ------------------- zlib-style API Definitions. 20 | # For more compatibility with zlib, miniz.c uses unsigned long for some parameters/struct members. Beware: mz_ulong can be either 32 or 64-bits! 21 | 22 | type 23 | mz_ulong* = culong 24 | 25 | # mz_free() internally uses the MZ_FREE() macro (which by default calls free() unless you've modified the MZ_MALLOC macro) to release a block allocated from the heap. 26 | 27 | proc mz_free*(p: pointer) {.importc.} 28 | const 29 | MZ_ADLER32_INIT* = (1) 30 | 31 | # mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL. 32 | 33 | proc mz_adler32*(adler: mz_ulong, pointr: ptr cuchar, buf_len: csize): mz_ulong {.importc.} 34 | const 35 | MZ_CRC32_INIT* = (0) 36 | 37 | # mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL. 38 | 39 | proc mz_crc32*(crc: mz_ulong; pointr: ptr cuchar; buf_len: csize): mz_ulong {.importc.} 40 | # Compression strategies. 41 | 42 | const 43 | MZ_DEFAULT_STRATEGY* = 0 44 | MZ_FILTERED* = 1 45 | MZ_HUFFMAN_ONLY* = 2 46 | MZ_RLE* = 3 47 | MZ_FIXED* = 4 48 | 49 | # Method 50 | 51 | const 52 | MZ_DEFLATED* = 8 53 | 54 | when not(defined(MINIZ_NO_ZLIB_APIS)): 55 | # Heap allocation callbacks. 56 | # Note that mz_alloc_func parameter types purpsosely differ from zlib's: items/size is size_t, not unsigned long. 57 | type 58 | mz_alloc_func* = proc (opaque: pointer; items: csize; size: csize): pointer {.noconv.} 59 | mz_free_func* = proc (opaque: pointer; address: pointer) {.noconv.} 60 | mz_realloc_func* = proc (opaque: pointer; address: pointer; items: csize; 61 | size: csize): pointer {.noconv.} 62 | const 63 | MZ_VERSION* = "9.1.15" 64 | MZ_VERNUM* = 0x000091F0 65 | MZ_VER_MAJOR* = 9 66 | MZ_VER_MINOR* = 1 67 | MZ_VER_REVISION* = 15 68 | MZ_VER_SUBREVISION* = 0 69 | # Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The other values are for advanced use (refer to the zlib docs). 70 | const 71 | MZ_NO_FLUSH* = 0 72 | MZ_PARTIAL_FLUSH* = 1 73 | MZ_SYNC_FLUSH* = 2 74 | MZ_FULL_FLUSH* = 3 75 | MZ_FINISH* = 4 76 | MZ_BLOCK* = 5 77 | # Return status codes. MZ_PARAM_ERROR is non-standard. 78 | const 79 | MZ_OK* = 0 80 | MZ_STREAM_END* = 1 81 | MZ_NEED_DICT* = 2 82 | MZ_ERRNO* = - 1 83 | MZ_STREAM_ERROR* = - 2 84 | MZ_DATA_ERROR* = - 3 85 | MZ_MEM_ERROR* = - 4 86 | MZ_BUF_ERROR* = - 5 87 | MZ_VERSION_ERROR* = - 6 88 | MZ_PARAM_ERROR* = - 10000 89 | # Compression levels: 0-9 are the standard zlib-style levels, 10 is best possible compression (not zlib compatible, and may be very slow), MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL. 90 | const 91 | MZ_NO_COMPRESSION* = 0 92 | MZ_BEST_SPEED* = 1 93 | MZ_BEST_COMPRESSION* = 9 94 | MZ_UBER_COMPRESSION* = 10 95 | MZ_DEFAULT_LEVEL* = 6 96 | MZ_DEFAULT_COMPRESSION* = - 1 97 | # Window bits 98 | const 99 | MZ_DEFAULT_WINDOW_BITS* = 15 100 | type 101 | mz_internal_state* = object 102 | 103 | # Compression/decompression stream struct. 104 | type 105 | mz_stream* = object 106 | next_in*: ptr cuchar # pointer to next byte to read 107 | avail_in*: cuint # number of bytes available at next_in 108 | total_in*: mz_ulong # total number of bytes consumed so far 109 | next_out*: ptr cuchar # pointer to next byte to write 110 | avail_out*: cuint # number of bytes that can be written to next_out 111 | total_out*: mz_ulong # total number of bytes produced so far 112 | msg*: cstring # error msg (unused) 113 | state*: ptr mz_internal_state # internal state, allocated by zalloc/zfree 114 | zalloc*: mz_alloc_func # optional heap allocation function (defaults to malloc) 115 | zfree*: mz_free_func # optional heap free function (defaults to free) 116 | opaque*: pointer # heap alloc function user pointer 117 | data_type*: cint # data_type (unused) 118 | adler*: mz_ulong # adler32 of the source or uncompressed data 119 | reserved*: mz_ulong # not used 120 | 121 | mz_streamp* = ptr mz_stream 122 | # Returns the version string of miniz.c. 123 | proc mz_version*(): cstring {.importc.} 124 | # mz_deflateInit() initializes a compressor with default options: 125 | # Parameters: 126 | # pStream must point to an initialized mz_stream struct. 127 | # level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. 128 | # level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio. 129 | # (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.) 130 | # Return values: 131 | # MZ_OK on success. 132 | # MZ_STREAM_ERROR if the stream is bogus. 133 | # MZ_PARAM_ERROR if the input parameters are bogus. 134 | # MZ_MEM_ERROR on out of memory. 135 | proc mz_deflateInit*(pStream: mz_streamp; level: cint): cint {.importc.} 136 | # mz_deflateInit2() is like mz_deflate(), except with more control: 137 | # Additional parameters: 138 | # method must be MZ_DEFLATED 139 | # window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with zlib header/adler-32 footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate/no header or footer) 140 | # mem_level must be between [1, 9] (it's checked but ignored by miniz.c) 141 | proc mz_deflateInit2*(pStream: mz_streamp; level: cint; meth: cint; 142 | window_bits: cint; mem_level: cint; strategy: cint): cint {.importc.} 143 | # Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2(). 144 | proc mz_deflateReset*(pStream: mz_streamp): cint {.importc.} 145 | # mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible. 146 | # Parameters: 147 | # pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members. 148 | # flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH. 149 | # Return values: 150 | # MZ_OK on success (when flushing, or if more input is needed but not available, and/or there's more output to be written but the output buffer is full). 151 | # MZ_STREAM_END if all input has been consumed and all output bytes have been written. Don't call mz_deflate() on the stream anymore. 152 | # MZ_STREAM_ERROR if the stream is bogus. 153 | # MZ_PARAM_ERROR if one of the parameters is invalid. 154 | # MZ_BUF_ERROR if no forward progress is possible because the input and/or output buffers are empty. (Fill up the input buffer or free up some output space and try again.) 155 | proc mz_deflate*(pStream: mz_streamp; flush: cint): cint {.importc.} 156 | # mz_deflateEnd() deinitializes a compressor: 157 | # Return values: 158 | # MZ_OK on success. 159 | # MZ_STREAM_ERROR if the stream is bogus. 160 | proc mz_deflateEnd*(pStream: mz_streamp): cint {.importc.} 161 | # mz_deflateBound() returns a (very) conservative upper bound on the amount of data that could be generated by deflate(), assuming flush is set to only MZ_NO_FLUSH or MZ_FINISH. 162 | proc mz_deflateBound*(pStream: mz_streamp; source_len: mz_ulong): mz_ulong {.importc.} 163 | # Single-call compression functions mz_compress() and mz_compress2(): 164 | # Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure. 165 | proc mz_compress*(pDest: ptr cuchar; pDest_len: ptr mz_ulong; 166 | pSource: ptr cuchar; source_len: mz_ulong): cint {.importc.} 167 | proc mz_compress2*(pDest: ptr cuchar; pDest_len: ptr mz_ulong; 168 | pSource: ptr cuchar; source_len: mz_ulong; level: cint): cint {.importc.} 169 | # mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress(). 170 | proc mz_compressBound*(source_len: mz_ulong): mz_ulong {.importc.} 171 | # Initializes a decompressor. 172 | proc mz_inflateInit*(pStream: mz_streamp): cint {.importc.} 173 | # mz_inflateInit2() is like mz_inflateInit() with an additional option that controls the window size and whether or not the stream has been wrapped with a zlib header/footer: 174 | # window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate). 175 | proc mz_inflateInit2*(pStream: mz_streamp; window_bits: cint): cint {.importc.} 176 | # Decompresses the input stream to the output, consuming only as much of the input as needed, and writing as much to the output as possible. 177 | # Parameters: 178 | # pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members. 179 | # flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. 180 | # On the first call, if flush is MZ_FINISH it's assumed the input and output buffers are both sized large enough to decompress the entire stream in a single call (this is slightly faster). 181 | # MZ_FINISH implies that there are no more source bytes available beside what's already in the input buffer, and that the output buffer is large enough to hold the rest of the decompressed data. 182 | # Return values: 183 | # MZ_OK on success. Either more input is needed but not available, and/or there's more output to be written but the output buffer is full. 184 | # MZ_STREAM_END if all needed input has been consumed and all output bytes have been written. For zlib streams, the adler-32 of the decompressed data has also been verified. 185 | # MZ_STREAM_ERROR if the stream is bogus. 186 | # MZ_DATA_ERROR if the deflate stream is invalid. 187 | # MZ_PARAM_ERROR if one of the parameters is invalid. 188 | # MZ_BUF_ERROR if no forward progress is possible because the input buffer is empty but the inflater needs more input to continue, or if the output buffer is not large enough. Call mz_inflate() again 189 | # with more input data, or with more room in the output buffer (except when using single call decompression, described above). 190 | proc mz_inflate*(pStream: mz_streamp; flush: cint): cint {.importc.} 191 | # Deinitializes a decompressor. 192 | proc mz_inflateEnd*(pStream: mz_streamp): cint {.importc.} 193 | # Single-call decompression. 194 | # Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure. 195 | proc mz_uncompress*(pDest: ptr cuchar; pDest_len: ptr mz_ulong; 196 | pSource: ptr cuchar; source_len: mz_ulong): cint {.importc.} 197 | # Returns a string description of the specified error code, or NULL if the error code is invalid. 198 | proc mz_error*(err: cint): cstring {.importc.} 199 | # Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used as a drop-in replacement for the subset of zlib that miniz.c supports. 200 | # Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project. 201 | when not(defined(MINIZ_NO_ZLIB_COMPATIBLE_NAMES)): 202 | type 203 | Byte* = cuchar 204 | uInt* = cuint 205 | uLong* = mz_ulong 206 | Bytef* = Byte 207 | uIntf* = uInt 208 | charf* = char 209 | intf* = cint 210 | voidpf* = pointer 211 | uLongf* = uLong 212 | voidp* = pointer 213 | voidpc* = pointer 214 | const 215 | Z_NULL* = 0 216 | Z_NO_FLUSH* = MZ_NO_FLUSH 217 | Z_PARTIAL_FLUSH* = MZ_PARTIAL_FLUSH 218 | Z_SYNC_FLUSH* = MZ_SYNC_FLUSH 219 | Z_FULL_FLUSH* = MZ_FULL_FLUSH 220 | Z_FINISH* = MZ_FINISH 221 | Z_BLOCK* = MZ_BLOCK 222 | Z_OK* = MZ_OK 223 | Z_STREAM_END* = MZ_STREAM_END 224 | Z_NEED_DICT* = MZ_NEED_DICT 225 | Z_ERRNO* = MZ_ERRNO 226 | Z_STREAM_ERROR* = MZ_STREAM_ERROR 227 | Z_DATA_ERROR* = MZ_DATA_ERROR 228 | Z_MEM_ERROR* = MZ_MEM_ERROR 229 | Z_BUF_ERROR* = MZ_BUF_ERROR 230 | Z_VERSION_ERROR* = MZ_VERSION_ERROR 231 | Z_PARAM_ERROR* = MZ_PARAM_ERROR 232 | Z_NO_COMPRESSION* = MZ_NO_COMPRESSION 233 | Z_BEST_SPEED* = MZ_BEST_SPEED 234 | Z_BEST_COMPRESSION* = MZ_BEST_COMPRESSION 235 | Z_DEFAULT_COMPRESSION* = MZ_DEFAULT_COMPRESSION 236 | Z_DEFAULT_STRATEGY* = MZ_DEFAULT_STRATEGY 237 | Z_FILTERED* = MZ_FILTERED 238 | Z_HUFFMAN_ONLY* = MZ_HUFFMAN_ONLY 239 | Z_RLE* = MZ_RLE 240 | Z_FIXED* = MZ_FIXED 241 | Z_DEFLATED* = MZ_DEFLATED 242 | Z_DEFAULT_WINDOW_BITS* = MZ_DEFAULT_WINDOW_BITS 243 | type 244 | alloc_func* = mz_alloc_func 245 | free_func* = mz_free_func 246 | internal_state* = mz_internal_state 247 | z_stream* = mz_stream 248 | const 249 | deflateInit* = mz_deflateInit 250 | deflateInit2* = mz_deflateInit2 251 | deflateReset* = mz_deflateReset 252 | deflate* = mz_deflate 253 | deflateEnd* = mz_deflateEnd 254 | deflateBound* = mz_deflateBound 255 | compress* = mz_compress 256 | compress2* = mz_compress2 257 | compressBound* = mz_compressBound 258 | inflateInit* = mz_inflateInit 259 | inflateInit2* = mz_inflateInit2 260 | inflate* = mz_inflate 261 | inflateEnd* = mz_inflateEnd 262 | uncompress* = mz_uncompress 263 | crc32* = mz_crc32 264 | adler32* = mz_adler32 265 | MAX_WBITS* = 15 266 | MAX_MEM_LEVEL* = 9 267 | zError* = mz_error 268 | ZLIB_VERSION* = MZ_VERSION 269 | ZLIB_VERNUM* = MZ_VERNUM 270 | ZLIB_VER_MAJOR* = MZ_VER_MAJOR 271 | ZLIB_VER_MINOR* = MZ_VER_MINOR 272 | ZLIB_VER_REVISION* = MZ_VER_REVISION 273 | ZLIB_VER_SUBREVISION* = MZ_VER_SUBREVISION 274 | zlibVersion* = mz_version 275 | #zlib_version* = mz_version() 276 | # ------------------- Types and macros 277 | 278 | type 279 | mz_uint8* = cuchar 280 | mz_int16* = cshort 281 | mz_uint16* = cushort 282 | mz_uint32* = cuint 283 | mz_uint* = cuint 284 | mz_int64* = clonglong 285 | mz_uint64* = culonglong 286 | mz_bool* = cint 287 | 288 | const 289 | MZ_FALSE* = (0) 290 | MZ_TRUE* = (1) 291 | 292 | # An attempt to work around MSVC's spammy "warning C4127: conditional expression is constant" message. 293 | 294 | #when defined(_MSC_VER): 295 | # const 296 | # MZ_MACRO_END* = while(0, 0) 297 | #else: 298 | # const 299 | # MZ_MACRO_END* = while(0) 300 | # ------------------- ZIP archive reading/writing 301 | 302 | type #FC defininf missing types 303 | time_t = cint 304 | size_t = cint 305 | 306 | when not(defined(MINIZ_NO_ARCHIVE_APIS)): 307 | const 308 | MZ_ZIP_MAX_IO_BUF_SIZE* = 64 * 1024 309 | MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE* = 260 310 | MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE* = 256 311 | type 312 | mz_zip_archive_file_stat* {.bycopy.} = object 313 | m_file_index*: mz_uint32 314 | m_central_dir_ofs*: mz_uint32 315 | m_version_made_by*: mz_uint16 316 | m_version_needed*: mz_uint16 317 | m_bit_flag*: mz_uint16 318 | m_method*: mz_uint16 ##ifndef MINIZ_NO_TIME 319 | m_time*: time_t ##endif 320 | m_crc32*: mz_uint32 321 | m_comp_size*: mz_uint64 322 | m_uncomp_size*: mz_uint64 323 | m_internal_attr*: mz_uint16 324 | m_external_attr*: mz_uint32 325 | m_local_header_ofs*: mz_uint64 326 | m_comment_size*: mz_uint32 327 | m_filename*: array[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE, char] 328 | m_comment*: array[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE, char] 329 | 330 | mz_file_read_func* = proc (pOpaque: pointer; file_ofs: mz_uint64; 331 | pBuf: pointer; n: csize): csize {.noconv.} 332 | mz_file_write_func* = proc (pOpaque: pointer; file_ofs: mz_uint64; 333 | pBuf: pointer; n: csize): csize {.noconv.} 334 | type 335 | mz_zip_internal_state_tag* = object 336 | 337 | type 338 | mz_zip_internal_state* = mz_zip_internal_state_tag 339 | mz_zip_mode* = enum 340 | MZ_ZIP_MODE_INVALID = 0, MZ_ZIP_MODE_READING = 1, MZ_ZIP_MODE_WRITING = 2, 341 | MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3 342 | mz_zip_archive* = object 343 | m_archive_size*: mz_uint64 344 | m_central_directory_file_ofs*: mz_uint64 345 | m_total_files*: mz_uint 346 | m_zip_mode*: mz_zip_mode 347 | m_file_offset_alignment*: mz_uint 348 | m_pAlloc*: mz_alloc_func 349 | m_pFree*: mz_free_func 350 | m_pRealloc*: mz_realloc_func 351 | m_pAlloc_opaque*: pointer 352 | m_pRead*: mz_file_read_func 353 | m_pWrite*: mz_file_write_func 354 | m_pIO_opaque*: pointer 355 | m_pState*: ptr mz_zip_internal_state 356 | 357 | mz_zip_flags* {.size: sizeof(cint).} = enum 358 | MZ_ZIP_FLAG_CASE_SENSITIVE = 0x00000100, 359 | MZ_ZIP_FLAG_IGNORE_PATH = 0x00000200, 360 | MZ_ZIP_FLAG_COMPRESSED_DATA = 0x00000400, 361 | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x00000800 362 | # ZIP archive reading 363 | # Inits a ZIP archive reader. 364 | # These functions read and validate the archive's central directory. 365 | proc mz_zip_reader_init*(pZip: ptr mz_zip_archive; size: mz_uint64; 366 | flags: mz_uint32): mz_bool {.importc.} 367 | proc mz_zip_reader_init_mem*(pZip: ptr mz_zip_archive; pMem: pointer; 368 | size: csize; flags: mz_uint32): mz_bool {.importc.} 369 | when not(defined(MINIZ_NO_STDIO)): 370 | proc mz_zip_reader_init_file*(pZip: ptr mz_zip_archive; pFilename: cstring; 371 | flags: mz_uint32): mz_bool {.importc, cdecl.} 372 | # Returns the total number of files in the archive. 373 | proc mz_zip_reader_get_num_files*(pZip: ptr mz_zip_archive): mz_uint {.importc, cdecl.} 374 | # Returns detailed information about an archive file entry. 375 | proc mz_zip_reader_file_stat*(pZip: ptr mz_zip_archive; file_index: mz_uint; 376 | pStat: ptr mz_zip_archive_file_stat): mz_bool {.importc.} 377 | # Determines if an archive file entry is a directory entry. 378 | proc mz_zip_reader_is_file_a_directory*(pZip: ptr mz_zip_archive; 379 | file_index: mz_uint): mz_bool {.importc, cdecl.} 380 | proc mz_zip_reader_is_file_encrypted*(pZip: ptr mz_zip_archive; 381 | file_index: mz_uint): mz_bool {.importc.} 382 | # Retrieves the filename of an archive file entry. 383 | # Returns the number of bytes written to pFilename, or if filename_buf_size is 0 this function returns the number of bytes needed to fully store the filename. 384 | proc mz_zip_reader_get_filename*(pZip: ptr mz_zip_archive; 385 | file_index: mz_uint; pFilename: cstring; 386 | filename_buf_size: mz_uint): mz_uint {.importc, cdecl.} 387 | # Attempts to locates a file in the archive's central directory. 388 | # Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH 389 | # Returns -1 if the file cannot be found. 390 | proc mz_zip_reader_locate_file*(pZip: ptr mz_zip_archive; pName: cstring; 391 | pComment: cstring; flags: mz_uint): cint {.importc, cdecl.} 392 | # Extracts a archive file to a memory buffer using no memory allocation. 393 | proc mz_zip_reader_extract_to_mem_no_alloc*(pZip: ptr mz_zip_archive; 394 | file_index: mz_uint; pBuf: pointer; buf_size: csize; flags: mz_uint; 395 | pUser_read_buf: pointer; user_read_buf_size: csize): mz_bool {.importc.} 396 | proc mz_zip_reader_extract_file_to_mem_no_alloc*(pZip: ptr mz_zip_archive; 397 | pFilename: cstring; pBuf: pointer; buf_size: csize; flags: mz_uint; 398 | pUser_read_buf: pointer; user_read_buf_size: csize): mz_bool {.importc.} 399 | # Extracts a archive file to a memory buffer. 400 | proc mz_zip_reader_extract_to_mem*(pZip: ptr mz_zip_archive; 401 | file_index: mz_uint; pBuf: pointer; 402 | buf_size: csize; flags: mz_uint): mz_bool {.importc.} 403 | proc mz_zip_reader_extract_file_to_mem*(pZip: ptr mz_zip_archive; 404 | pFilename: cstring; pBuf: pointer; buf_size: csize; flags: mz_uint): mz_bool {.importc.} 405 | # Extracts a archive file to a dynamically allocated heap buffer. 406 | proc mz_zip_reader_extract_to_heap*(pZip: ptr mz_zip_archive; 407 | file_index: mz_uint; pSize: ptr csize; 408 | flags: mz_uint): pointer {.importc.} 409 | proc mz_zip_reader_extract_file_to_heap*(pZip: ptr mz_zip_archive; 410 | pFilename: cstring; pSize: ptr csize; flags: mz_uint): pointer {.importc.} 411 | # Extracts a archive file using a callback function to output the file's data. 412 | proc mz_zip_reader_extract_to_callback*(pZip: ptr mz_zip_archive; 413 | file_index: mz_uint; pCallback: mz_file_write_func; pOpaque: pointer; 414 | flags: mz_uint): mz_bool {.importc.} 415 | proc mz_zip_reader_extract_file_to_callback*(pZip: ptr mz_zip_archive; 416 | pFilename: cstring; pCallback: mz_file_write_func; pOpaque: pointer; 417 | flags: mz_uint): mz_bool {.importc.} 418 | when not(defined(MINIZ_NO_STDIO)): 419 | # Extracts a archive file to a disk file and sets its last accessed and modified times. 420 | # This function only extracts files, not archive directory records. 421 | proc mz_zip_reader_extract_to_file*(pZip: ptr mz_zip_archive; 422 | file_index: mz_uint; 423 | pDst_filename: cstring; flags: mz_uint): mz_bool {.importc, cdecl.} 424 | proc mz_zip_reader_extract_file_to_file*(pZip: ptr mz_zip_archive; 425 | pArchive_filename: cstring; pDst_filename: cstring; flags: mz_uint): mz_bool {.importc.} 426 | # Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was used. 427 | proc mz_zip_reader_end*(pZip: ptr mz_zip_archive): mz_bool {.importc, cdecl.} 428 | # ZIP archive writing 429 | when not(defined(MINIZ_NO_ARCHIVE_WRITING_APIS)): 430 | # Inits a ZIP archive writer. 431 | proc mz_zip_writer_init*(pZip: ptr mz_zip_archive; existing_size: mz_uint64): mz_bool {.importc.} 432 | proc mz_zip_writer_init_heap*(pZip: ptr mz_zip_archive; 433 | size_to_reserve_at_beginning: csize; 434 | initial_allocation_size: csize): mz_bool {.importc.} 435 | when not(defined(MINIZ_NO_STDIO)): 436 | proc mz_zip_writer_init_file*(pZip: ptr mz_zip_archive; 437 | pFilename: cstring; 438 | size_to_reserve_at_beginning: mz_uint64): mz_bool {.importc, cdecl.} 439 | # Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an existing archive. 440 | # For archives opened using mz_zip_reader_init_file, pFilename must be the archive's filename so it can be reopened for writing. If the file can't be reopened, mz_zip_reader_end() will be called. 441 | # For archives opened using mz_zip_reader_init_mem, the memory block must be growable using the realloc callback (which defaults to realloc unless you've overridden it). 442 | # Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be NULL. 443 | # Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops or something goes wrong before 444 | # the archive is finalized the file's central directory will be hosed. 445 | proc mz_zip_writer_init_from_reader*(pZip: ptr mz_zip_archive; 446 | pFilename: cstring): mz_bool {.importc, cdecl.} 447 | # Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive. 448 | # To add a directory entry, call this method with an archive name ending in a forwardslash with empty buffer. 449 | # level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. 450 | proc mz_zip_writer_add_mem*(pZip: ptr mz_zip_archive; 451 | pArchive_name: cstring; pBuf: pointer; 452 | buf_size: csize; level_and_flags: mz_uint): mz_bool {.importc.} 453 | proc mz_zip_writer_add_mem_ex*(pZip: ptr mz_zip_archive; 454 | pArchive_name: cstring; pBuf: pointer; 455 | buf_size: csize; pComment: pointer; 456 | comment_size: mz_uint16; 457 | level_and_flags: mz_uint; 458 | uncomp_size: mz_uint64; 459 | uncomp_crc32: mz_uint32): mz_bool {.importc.} 460 | when not(defined(MINIZ_NO_STDIO)): 461 | # Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive. 462 | # level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. 463 | proc mz_zip_writer_add_file*(pZip: ptr mz_zip_archive; 464 | pArchive_name: cstring; 465 | pSrc_filename: cstring; pComment: pointer; 466 | comment_size: mz_uint16; 467 | level_and_flags: mz_uint): mz_bool {.importc, cdecl.} 468 | # Adds a file to an archive by fully cloning the data from another archive. 469 | # This function fully clones the source file's compressed data (no recompression), along with its full filename, extra data, and comment fields. 470 | proc mz_zip_writer_add_from_zip_reader*(pZip: ptr mz_zip_archive; 471 | pSource_zip: ptr mz_zip_archive; file_index: mz_uint): mz_bool {.importc.} 472 | # Finalizes the archive by writing the central directory records followed by the end of central directory record. 473 | # After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end(). 474 | # An archive must be manually finalized by calling this function for it to be valid. 475 | proc mz_zip_writer_finalize_archive*(pZip: ptr mz_zip_archive): mz_bool {.importc, cdecl.} 476 | proc mz_zip_writer_finalize_heap_archive*(pZip: ptr mz_zip_archive; 477 | pBuf: ptr pointer; pSize: ptr csize): mz_bool {.importc, cdecl.} 478 | # Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used. 479 | # Note for the archive to be valid, it must have been finalized before ending. 480 | proc mz_zip_writer_end*(pZip: ptr mz_zip_archive): mz_bool {.importc, cdecl.} 481 | # Misc. high-level helper functions: 482 | # mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive. 483 | # level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. 484 | proc mz_zip_add_mem_to_archive_file_in_place*(pZip_filename: cstring; 485 | pArchive_name: cstring; pBuf: pointer; buf_size: csize; 486 | pComment: pointer; comment_size: mz_uint16; level_and_flags: mz_uint): mz_bool {.importc.} 487 | # Reads a single file from an archive into a heap block. 488 | # Returns NULL on failure. 489 | proc mz_zip_extract_archive_file_to_heap*(pZip_filename: cstring; 490 | pArchive_name: cstring; pSize: ptr csize; zip_flags: mz_uint): pointer {.importc.} 491 | # ------------------- Low-level Decompression API Definitions 492 | # Decompression flags used by tinfl_decompress(). 493 | # TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the input is a raw deflate stream. 494 | # TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available beyond the end of the supplied input buffer. If clear, the input buffer contains all remaining input. 495 | # TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large enough to hold the entire decompressed stream. If clear, the output buffer is at least the size of the dictionary (typically 32KB). 496 | # TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes. 497 | 498 | const 499 | TINFL_FLAG_PARSE_ZLIB_HEADER* = 1 500 | TINFL_FLAG_HAS_MORE_INPUT* = 2 501 | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF* = 4 502 | TINFL_FLAG_COMPUTE_ADLER32* = 8 503 | 504 | # High level decompression functions: 505 | # tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc(). 506 | # On entry: 507 | # pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress. 508 | # On return: 509 | # Function returns a pointer to the decompressed data, or NULL on failure. 510 | # *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data. 511 | # The caller must call mz_free() on the returned block when it's no longer needed. 512 | 513 | proc tinfl_decompress_mem_to_heap*(pSrc_buf: pointer; src_buf_len: csize; 514 | pOut_len: ptr csize; flags: cint): pointer {.importc.} 515 | # tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory. 516 | # Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success. 517 | 518 | const 519 | TINFL_DECOMPRESS_MEM_TO_MEM_FAILED* = ((size_t)(- 1)) 520 | 521 | proc tinfl_decompress_mem_to_mem*(pOut_buf: pointer; out_buf_len: csize; 522 | pSrc_buf: pointer; src_buf_len: csize; 523 | flags: cint): csize {.importc.} 524 | # tinfl_decompress_mem_to_callback() decompresses a block in memory to an internal 32KB buffer, and a user provided callback function will be called to flush the buffer. 525 | # Returns 1 on success or 0 on failure. 526 | 527 | type 528 | tinfl_put_buf_func_ptr* = proc (pBuf: pointer; len: cint; pUser: pointer): cint {.noconv.} 529 | 530 | proc tinfl_decompress_mem_to_callback*(pIn_buf: pointer; 531 | pIn_buf_size: ptr csize; 532 | pPut_buf_func: tinfl_put_buf_func_ptr; 533 | pPut_buf_user: pointer; flags: cint): cint {.importc.} 534 | type 535 | tinfl_decompressor_tag* = object 536 | 537 | tinfl_decompressor* = tinfl_decompressor_tag 538 | 539 | # Max size of LZ dictionary. 540 | 541 | const 542 | TINFL_LZ_DICT_SIZE* = 32768 543 | 544 | # Return status. 545 | 546 | type 547 | tinfl_status* {.size: sizeof(cint).} = enum 548 | TINFL_STATUS_BAD_PARAM = - 3, TINFL_STATUS_ADLER32_MISMATCH = - 2, 549 | TINFL_STATUS_FAILED = - 1, TINFL_STATUS_DONE = 0, 550 | TINFL_STATUS_NEEDS_MORE_INPUT = 1, TINFL_STATUS_HAS_MORE_OUTPUT = 2 551 | 552 | 553 | # Initializes the decompressor to its initial state. 554 | ##define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END 555 | template tinfl_init*(r: typed): void= 556 | r.m_state = 0 557 | 558 | template tinfl_get_adler32*(r: typed): void= 559 | (r).m_check_adler32 560 | 561 | # Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the other functions are just high-level helpers for improved usability. 562 | # This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. In the limit case, it can be called once per every byte input or output. 563 | 564 | proc tinfl_decompress*(r: ptr tinfl_decompressor; pIn_buf_next: ptr mz_uint8; 565 | pIn_buf_size: ptr csize; pOut_buf_start: ptr mz_uint8; 566 | pOut_buf_next: ptr mz_uint8; pOut_buf_size: ptr csize; 567 | decomp_flags: mz_uint32): tinfl_status {.importc.} 568 | # Internal/private bits follow. 569 | 570 | const 571 | TINFL_MAX_HUFF_TABLES* = 3 572 | TINFL_MAX_HUFF_SYMBOLS_0* = 288 573 | TINFL_MAX_HUFF_SYMBOLS_1* = 32 574 | TINFL_MAX_HUFF_SYMBOLS_2* = 19 575 | TINFL_FAST_LOOKUP_BITS* = 10 576 | TINFL_FAST_LOOKUP_SIZE* = 1 shl TINFL_FAST_LOOKUP_BITS 577 | 578 | type 579 | tinfl_huff_table* = object 580 | m_code_size*: array[TINFL_MAX_HUFF_SYMBOLS_0, mz_uint8] 581 | m_look_up*: array[TINFL_FAST_LOOKUP_SIZE, mz_int16] 582 | m_tree*: array[TINFL_MAX_HUFF_SYMBOLS_0 * 2, mz_int16] 583 | 584 | 585 | when defined(MINIZ_HAS_64BIT_REGISTERS): 586 | const 587 | TINFL_USE_64BIT_BITBUF* = 1 588 | when defined(TINFL_USE_64BIT_BITBUF): 589 | type 590 | tinfl_bit_buf_t* = mz_uint64 591 | const 592 | TINFL_BITBUF_SIZE* = (64) 593 | else: 594 | type 595 | tinfl_bit_buf_t* = mz_uint32 596 | const 597 | TINFL_BITBUF_SIZE* = (32) 598 | type 599 | tinfl_decompressor_tag_obj* = object 600 | m_state*: mz_uint32 601 | m_num_bits*: mz_uint32 602 | m_zhdr0*: mz_uint32 603 | m_zhdr1*: mz_uint32 604 | m_z_adler32*: mz_uint32 605 | m_final*: mz_uint32 606 | m_type*: mz_uint32 607 | m_check_adler32*: mz_uint32 608 | m_dist*: mz_uint32 609 | m_counter*: mz_uint32 610 | m_num_extra*: mz_uint32 611 | m_table_sizes*: array[TINFL_MAX_HUFF_TABLES, mz_uint32] 612 | m_bit_buf*: tinfl_bit_buf_t 613 | m_dist_from_out_buf_start*: csize 614 | m_tables*: array[TINFL_MAX_HUFF_TABLES, tinfl_huff_table] 615 | m_raw_header*: array[4, mz_uint8] 616 | m_len_codes*: array[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 617 | 137, mz_uint8] 618 | 619 | 620 | # ------------------- Low-level Compression API Definitions 621 | # Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently). 622 | 623 | const 624 | TDEFL_LESS_MEMORY* = 0 625 | 626 | # tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search): 627 | # TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap compression), 4095=Huffman+LZ (slowest/best compression). 628 | 629 | const 630 | TDEFL_HUFFMAN_ONLY* = 0 631 | TDEFL_DEFAULT_MAX_PROBES* = 128 632 | TDEFL_MAX_PROBES_MASK* = 0x00000FFF 633 | 634 | # TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before the deflate data, and the Adler-32 of the source data at the end. Otherwise, you'll get raw deflate data. 635 | # TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers). 636 | # TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing. 637 | # TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's initialization time to the minimum, but the output may vary from run to run given the same input (depending on the contents of memory). 638 | # TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1) 639 | # TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. 640 | # TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. 641 | # TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. 642 | # The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK). 643 | 644 | const 645 | TDEFL_WRITE_ZLIB_HEADER* = 0x00001000 646 | TDEFL_COMPUTE_ADLER32* = 0x00002000 647 | TDEFL_GREEDY_PARSING_FLAG* = 0x00004000 648 | TDEFL_NONDETERMINISTIC_PARSING_FLAG* = 0x00008000 649 | TDEFL_RLE_MATCHES* = 0x00010000 650 | TDEFL_FILTER_MATCHES* = 0x00020000 651 | TDEFL_FORCE_ALL_STATIC_BLOCKS* = 0x00040000 652 | TDEFL_FORCE_ALL_RAW_BLOCKS* = 0x00080000 653 | 654 | # High level compression functions: 655 | # tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc(). 656 | # On entry: 657 | # pSrc_buf, src_buf_len: Pointer and size of source block to compress. 658 | # flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower but improve compression. 659 | # On return: 660 | # Function returns a pointer to the compressed data, or NULL on failure. 661 | # *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data. 662 | # The caller must free() the returned block when it's no longer needed. 663 | 664 | proc tdefl_compress_mem_to_heap*(pSrc_buf: pointer; src_buf_len: csize; 665 | pOut_len: ptr csize; flags: cint): pointer {.importc.} 666 | # tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory. 667 | # Returns 0 on failure. 668 | 669 | proc tdefl_compress_mem_to_mem*(pOut_buf: pointer; out_buf_len: csize; 670 | pSrc_buf: pointer; src_buf_len: csize; 671 | flags: cint): csize {.importc.} 672 | # Compresses an image to a compressed PNG file in memory. 673 | # On entry: 674 | # pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4. 675 | # The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in memory. 676 | # level may range from [0,10], use MZ_NO_COMPRESSION, MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc. or a decent default is MZ_DEFAULT_LEVEL 677 | # If flip is true, the image will be flipped on the Y axis (useful for OpenGL apps). 678 | # On return: 679 | # Function returns a pointer to the compressed data, or NULL on failure. 680 | # *pLen_out will be set to the size of the PNG image file. 681 | # The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no longer needed. 682 | 683 | proc tdefl_write_image_to_png_file_in_memory_ex*(pImage: pointer; w: cint; 684 | h: cint; num_chans: cint; pLen_out: ptr csize; level: mz_uint; flip: mz_bool): pointer {.importc.} 685 | proc tdefl_write_image_to_png_file_in_memory*(pImage: pointer; w: cint; h: cint; 686 | num_chans: cint; pLen_out: ptr csize): pointer {.importc.} 687 | # Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time. 688 | 689 | type 690 | tdefl_put_buf_func_ptr* = proc (pBuf: pointer; len: cint; pUser: pointer): mz_bool {.noconv.} 691 | 692 | # tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally. 693 | 694 | proc tdefl_compress_mem_to_output*(pBuf: pointer; buf_len: csize; 695 | pPut_buf_func: tdefl_put_buf_func_ptr; 696 | pPut_buf_user: pointer; flags: cint): mz_bool {.importc.} 697 | const 698 | TDEFL_MAX_HUFF_TABLES* = 3 699 | TDEFL_MAX_HUFF_SYMBOLS_0* = 288 700 | TDEFL_MAX_HUFF_SYMBOLS_1* = 32 701 | TDEFL_MAX_HUFF_SYMBOLS_2* = 19 702 | TDEFL_LZ_DICT_SIZE* = 32768 703 | TDEFL_LZ_DICT_SIZE_MASK* = TDEFL_LZ_DICT_SIZE - 1 704 | TDEFL_MIN_MATCH_LEN* = 3 705 | TDEFL_MAX_MATCH_LEN* = 258 706 | 707 | # TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes). 708 | 709 | when TDEFL_LESS_MEMORY > 0: 710 | const 711 | TDEFL_LZ_CODE_BUF_SIZE* = 24 * 1024 712 | TDEFL_OUT_BUF_SIZE* = (TDEFL_LZ_CODE_BUF_SIZE * 13) div 10 713 | TDEFL_MAX_HUFF_SYMBOLS* = 288 714 | TDEFL_LZ_HASH_BITS* = 12 715 | TDEFL_LEVEL1_HASH_SIZE_MASK* = 4095 716 | TDEFL_LZ_HASH_SHIFT* = (TDEFL_LZ_HASH_BITS + 2) div 3 717 | TDEFL_LZ_HASH_SIZE* = 1 shl TDEFL_LZ_HASH_BITS 718 | else: 719 | const 720 | TDEFL_LZ_CODE_BUF_SIZE* = 64 * 1024 721 | TDEFL_OUT_BUF_SIZE* = (TDEFL_LZ_CODE_BUF_SIZE * 13) div 10 722 | TDEFL_MAX_HUFF_SYMBOLS* = 288 723 | TDEFL_LZ_HASH_BITS* = 15 724 | TDEFL_LEVEL1_HASH_SIZE_MASK* = 4095 725 | TDEFL_LZ_HASH_SHIFT* = (TDEFL_LZ_HASH_BITS + 2) div 3 726 | TDEFL_LZ_HASH_SIZE* = 1 shl TDEFL_LZ_HASH_BITS 727 | # The low-level tdefl functions below may be used directly if the above helper functions aren't flexible enough. The low-level functions don't make any heap allocations, unlike the above helper functions. 728 | 729 | type 730 | tdefl_status* {.size: sizeof(cint).} = enum 731 | TDEFL_STATUS_BAD_PARAM = - 2, TDEFL_STATUS_PUT_BUF_FAILED = - 1, 732 | TDEFL_STATUS_OKAY = 0, TDEFL_STATUS_DONE = 1 733 | 734 | 735 | # Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums 736 | 737 | type 738 | tdefl_flush* {.size: sizeof(cint).} = enum 739 | TDEFL_NO_FLUSH = 0, TDEFL_SYNC_FLUSH = 2, TDEFL_FULL_FLUSH = 3, 740 | TDEFL_FINISH = 4 741 | 742 | 743 | # tdefl's compression state structure. 744 | 745 | type 746 | tdefl_compressor* = object 747 | m_pPut_buf_func*: tdefl_put_buf_func_ptr 748 | m_pPut_buf_user*: pointer 749 | m_flags*: mz_uint 750 | m_max_probes*: array[2, mz_uint] 751 | m_greedy_parsing*: cint 752 | m_adler32*: mz_uint 753 | m_lookahead_pos*: mz_uint 754 | m_lookahead_size*: mz_uint 755 | m_dict_size*: mz_uint 756 | m_pLZ_code_buf*: ptr mz_uint8 757 | m_pLZ_flags*: ptr mz_uint8 758 | m_pOutput_buf*: ptr mz_uint8 759 | m_pOutput_buf_end*: ptr mz_uint8 760 | m_num_flags_left*: mz_uint 761 | m_total_lz_bytes*: mz_uint 762 | m_lz_code_buf_dict_pos*: mz_uint 763 | m_bits_in*: mz_uint 764 | m_bit_buffer*: mz_uint 765 | m_saved_match_dist*: mz_uint 766 | m_saved_match_len*: mz_uint 767 | m_saved_lit*: mz_uint 768 | m_output_flush_ofs*: mz_uint 769 | m_output_flush_remaining*: mz_uint 770 | m_finished*: mz_uint 771 | m_block_index*: mz_uint 772 | m_wants_to_finish*: mz_uint 773 | m_prev_return_status*: tdefl_status 774 | m_pIn_buf*: pointer 775 | m_pOut_buf*: pointer 776 | m_pIn_buf_size*: ptr csize 777 | m_pOut_buf_size*: ptr csize 778 | m_flush*: tdefl_flush 779 | m_pSrc*: ptr mz_uint8 780 | m_src_buf_left*: csize 781 | m_out_buf_ofs*: csize 782 | m_dict*: array[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1, mz_uint8] 783 | m_huff_count*: array[TDEFL_MAX_HUFF_SYMBOLS, 784 | array[TDEFL_MAX_HUFF_TABLES, mz_uint16]] 785 | m_huff_codes*: array[TDEFL_MAX_HUFF_SYMBOLS, 786 | array[TDEFL_MAX_HUFF_TABLES, mz_uint16]] 787 | m_huff_code_sizes*: array[TDEFL_MAX_HUFF_SYMBOLS, 788 | array[TDEFL_MAX_HUFF_TABLES, mz_uint8]] 789 | m_lz_code_buf*: array[TDEFL_LZ_CODE_BUF_SIZE, mz_uint8] 790 | m_next*: array[TDEFL_LZ_DICT_SIZE, mz_uint16] 791 | m_hash*: array[TDEFL_LZ_HASH_SIZE, mz_uint16] 792 | m_output_buf*: array[TDEFL_OUT_BUF_SIZE, mz_uint8] 793 | 794 | 795 | # Initializes the compressor. 796 | # There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory. 797 | # pBut_buf_func: If NULL, output data will be supplied to the specified callback. In this case, the user should call the tdefl_compress_buffer() API for compression. 798 | # If pBut_buf_func is NULL the user should always call the tdefl_compress() API. 799 | # flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.) 800 | 801 | proc tdefl_init*(d: ptr tdefl_compressor; pPut_buf_func: tdefl_put_buf_func_ptr; 802 | pPut_buf_user: pointer; flags: cint): tdefl_status {.importc.} 803 | # Compresses a block of data, consuming as much of the specified input buffer as possible, and writing as much compressed data to the specified output buffer as possible. 804 | 805 | proc tdefl_compress*(d: ptr tdefl_compressor; pIn_buf: pointer; 806 | pIn_buf_size: ptr csize; pOut_buf: pointer; 807 | pOut_buf_size: ptr csize; flush: tdefl_flush): tdefl_status {.importc.} 808 | # tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr. 809 | # tdefl_compress_buffer() always consumes the entire input buffer. 810 | 811 | proc tdefl_compress_buffer*(d: ptr tdefl_compressor; pIn_buf: pointer; 812 | in_buf_size: csize; flush: tdefl_flush): tdefl_status {.importc.} 813 | proc tdefl_get_prev_return_status*(d: ptr tdefl_compressor): tdefl_status {.importc.} 814 | proc tdefl_get_adler32*(d: ptr tdefl_compressor): mz_uint32 {.importc.} 815 | # Can't use tdefl_create_comp_flags_from_zip_params if MINIZ_NO_ZLIB_APIS isn't defined, because it uses some of its macros. 816 | 817 | when not(defined(MINIZ_NO_ZLIB_APIS)): 818 | # Create tdefl_compress() flags given zlib-style compression parameters. 819 | # level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files) 820 | # window_bits may be -15 (raw deflate) or 15 (zlib) 821 | # strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED 822 | proc tdefl_create_comp_flags_from_zip_params*(level: cint; window_bits: cint; 823 | strategy: cint): mz_uint {.importc.} 824 | 825 | ### Public Library 826 | 827 | import 828 | os 829 | 830 | proc zip*(files: seq[string], filepath: string) = 831 | var pZip: ptr mz_zip_archive = cast[ptr mz_zip_archive](alloc0(sizeof(mz_zip_archive))) 832 | discard pZip.mz_zip_writer_init_file(filepath.cstring, 0) 833 | var comment: pointer 834 | for f in files: 835 | discard pZip.mz_zip_writer_add_file(f.extractFileName.cstring, f.extractFileName.cstring, comment, 0, cast[mz_uint](MZ_DEFAULT_COMPRESSION)) 836 | discard pZip.mz_zip_writer_finalize_archive() 837 | discard pZip.mz_zip_writer_end() 838 | dealloc(pZip) 839 | 840 | proc unzip*(src, dst: string) = 841 | var pZip: ptr mz_zip_archive = cast[ptr mz_zip_archive](alloc0(sizeof(mz_zip_archive))) 842 | discard pZip.mz_zip_reader_init_file(src.cstring, 0) 843 | let total = pZip.mz_zip_reader_get_num_files() 844 | if total == 0: 845 | return 846 | for i in 0..total-1: 847 | let isDir = pZip.mz_zip_reader_is_file_a_directory(i) 848 | if isDir == 0: 849 | # Extract file 850 | let size = pZip.mz_zip_reader_get_filename(i, nil, 0) 851 | var filename: cstring = cast[cstring](alloc(size)) 852 | discard pZip.mz_zip_reader_get_filename(i, filename, size) 853 | let dest = dst / $filename 854 | dest.parentDir.createDir() 855 | dest.writeFile("") 856 | discard pZip.mz_zip_reader_extract_to_file(i, dest, 0) 857 | discard pZip.mz_zip_reader_end() 858 | dealloc(pZip) 859 | 860 | type Zip* = object 861 | c : mz_zip_archive 862 | mode: FileMode 863 | 864 | template check_mode(zip: Zip, mode: mz_zip_mode, operation: string) = 865 | if zip.c.addr.m_zip_mode != mode: 866 | raise newException(IOError, "must be opened in another mode to " & operation) 867 | 868 | proc len*(zip: var Zip): int = 869 | return zip.c.addr.mz_zip_reader_get_num_files().int 870 | 871 | proc open*(zip: var Zip, path: string, mode:FileMode=fmRead): bool {.discardable.} = 872 | zip.mode = mode 873 | if mode == fmWrite: 874 | return zip.c.addr.mz_zip_writer_init_file(path.cstring, 0) == 1 875 | elif mode == fmRead: 876 | return zip.c.addr.mz_zip_reader_init_file(path.cstring, 0) == 1 877 | else: 878 | quit "unsupported mode for zip" 879 | 880 | proc add_file*(zip: var Zip, path: string, archivePath:string="") = 881 | check_mode(zip, MZ_ZIP_MODE_WRITING, "add_file") 882 | var comment:pointer 883 | if not fileExists(path): 884 | raise newException(ValueError, "no file found at:" & path) 885 | var arcPath = path.cstring 886 | if archivePath != "": 887 | arcPath = archivePath.cstring 888 | doAssert zip.c.addr.mz_zip_writer_add_file(archivePath, path.cstring, comment, 0, cast[mz_uint](3'u16 or MZ_ZIP_FLAG_CASE_SENSITIVE.uint16)) == MZ_TRUE 889 | 890 | proc close*(zip: var Zip) = 891 | if zip.mode == fmWrite: 892 | doAssert zip.c.addr.mz_zip_writer_finalize_archive() == MZ_TRUE 893 | doAssert zip.c.addr.mz_zip_writer_end() == MZ_TRUE 894 | elif zip.mode == fmRead: 895 | doAssert zip.c.addr.mz_zip_reader_end() == MZ_TRUE 896 | 897 | proc get_file_name(zip: var Zip, i:int): string {.inline.} = 898 | var size = zip.c.addr.mz_zip_reader_get_filename(i.mz_uint, result, 0) 899 | result.setLen(size.int) 900 | doAssert zip.c.addr.mz_zip_reader_get_filename(i.mz_uint, result, size) > 0.mz_uint 901 | # drop trailing byte. 902 | result = result[0.., last updated Oct. 13, 2013 4 | Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt 5 | 6 | Most API's defined in miniz.c are optional. For example, to disable the archive related functions just define 7 | MINIZ_NO_ARCHIVE_APIS, or to get rid of all stdio usage define MINIZ_NO_STDIO (see the list below for more macros). 8 | 9 | * Change History 10 | 10/13/13 v1.15 r4 - Interim bugfix release while I work on the next major release with Zip64 support (almost there!): 11 | - Critical fix for the MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY bug (thanks kahmyong.moon@hp.com) which could cause locate files to not find files. This bug 12 | would only have occured in earlier versions if you explicitly used this flag, OR if you used mz_zip_extract_archive_file_to_heap() or mz_zip_add_mem_to_archive_file_in_place() 13 | (which used this flag). If you can't switch to v1.15 but want to fix this bug, just remove the uses of this flag from both helper funcs (and of course don't use the flag). 14 | - Bugfix in mz_zip_reader_extract_to_mem_no_alloc() from kymoon when pUser_read_buf is not NULL and compressed size is > uncompressed size 15 | - Fixing mz_zip_reader_extract_*() funcs so they don't try to extract compressed data from directory entries, to account for weird zipfiles which contain zero-size compressed data on dir entries. 16 | Hopefully this fix won't cause any issues on weird zip archives, because it assumes the low 16-bits of zip external attributes are DOS attributes (which I believe they always are in practice). 17 | - Fixing mz_zip_reader_is_file_a_directory() so it doesn't check the internal attributes, just the filename and external attributes 18 | - mz_zip_reader_init_file() - missing MZ_FCLOSE() call if the seek failed 19 | - Added cmake support for Linux builds which builds all the examples, tested with clang v3.3 and gcc v4.6. 20 | - Clang fix for tdefl_write_image_to_png_file_in_memory() from toffaletti 21 | - Merged MZ_FORCEINLINE fix from hdeanclark 22 | - Fix include before config #ifdef, thanks emil.brink 23 | - Added tdefl_write_image_to_png_file_in_memory_ex(): supports Y flipping (super useful for OpenGL apps), and explicit control over the compression level (so you can 24 | set it to 1 for real-time compression). 25 | - Merged in some compiler fixes from paulharris's github repro. 26 | - Retested this build under Windows (VS 2010, including static analysis), tcc 0.9.26, gcc v4.6 and clang v3.3. 27 | - Added example6.c, which dumps an image of the mandelbrot set to a PNG file. 28 | - Modified example2 to help test the MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY flag more. 29 | - In r3: Bugfix to mz_zip_writer_add_file() found during merge: Fix possible src file fclose() leak if alignment bytes+local header file write faiiled 30 | - In r4: Minor bugfix to mz_zip_writer_add_from_zip_reader(): Was pushing the wrong central dir header offset, appears harmless in this release, but it became a problem in the zip64 branch 31 | 5/20/12 v1.14 - MinGW32/64 GCC 4.6.1 compiler fixes: added MZ_FORCEINLINE, #include (thanks fermtect). 32 | 5/19/12 v1.13 - From jason@cornsyrup.org and kelwert@mtu.edu - Fix mz_crc32() so it doesn't compute the wrong CRC-32's when mz_ulong is 64-bit. 33 | - Temporarily/locally slammed in "typedef unsigned long mz_ulong" and re-ran a randomized regression test on ~500k files. 34 | - Eliminated a bunch of warnings when compiling with GCC 32-bit/64. 35 | - Ran all examples, miniz.c, and tinfl.c through MSVC 2008's /analyze (static analysis) option and fixed all warnings (except for the silly 36 | "Use of the comma-operator in a tested expression.." analysis warning, which I purposely use to work around a MSVC compiler warning). 37 | - Created 32-bit and 64-bit Codeblocks projects/workspace. Built and tested Linux executables. The codeblocks workspace is compatible with Linux+Win32/x64. 38 | - Added miniz_tester solution/project, which is a useful little app derived from LZHAM's tester app that I use as part of the regression test. 39 | - Ran miniz.c and tinfl.c through another series of regression testing on ~500,000 files and archives. 40 | - Modified example5.c so it purposely disables a bunch of high-level functionality (MINIZ_NO_STDIO, etc.). (Thanks to corysama for the MINIZ_NO_STDIO bug report.) 41 | - Fix ftell() usage in examples so they exit with an error on files which are too large (a limitation of the examples, not miniz itself). 42 | 4/12/12 v1.12 - More comments, added low-level example5.c, fixed a couple minor level_and_flags issues in the archive API's. 43 | level_and_flags can now be set to MZ_DEFAULT_COMPRESSION. Thanks to Bruce Dawson for the feedback/bug report. 44 | 5/28/11 v1.11 - Added statement from unlicense.org 45 | 5/27/11 v1.10 - Substantial compressor optimizations: 46 | - Level 1 is now ~4x faster than before. The L1 compressor's throughput now varies between 70-110MB/sec. on a 47 | - Core i7 (actual throughput varies depending on the type of data, and x64 vs. x86). 48 | - Improved baseline L2-L9 compression perf. Also, greatly improved compression perf. issues on some file types. 49 | - Refactored the compression code for better readability and maintainability. 50 | - Added level 10 compression level (L10 has slightly better ratio than level 9, but could have a potentially large 51 | drop in throughput on some files). 52 | 5/15/11 v1.09 - Initial stable release. 53 | 54 | * Low-level Deflate/Inflate implementation notes: 55 | 56 | Compression: Use the "tdefl" API's. The compressor supports raw, static, and dynamic blocks, lazy or 57 | greedy parsing, match length filtering, RLE-only, and Huffman-only streams. It performs and compresses 58 | approximately as well as zlib. 59 | 60 | Decompression: Use the "tinfl" API's. The entire decompressor is implemented as a single function 61 | coroutine: see tinfl_decompress(). It supports decompression into a 32KB (or larger power of 2) wrapping buffer, or into a memory 62 | block large enough to hold the entire file. 63 | 64 | The low-level tdefl/tinfl API's do not make any use of dynamic memory allocation. 65 | 66 | * zlib-style API notes: 67 | 68 | miniz.c implements a fairly large subset of zlib. There's enough functionality present for it to be a drop-in 69 | zlib replacement in many apps: 70 | The z_stream struct, optional memory allocation callbacks 71 | deflateInit/deflateInit2/deflate/deflateReset/deflateEnd/deflateBound 72 | inflateInit/inflateInit2/inflate/inflateEnd 73 | compress, compress2, compressBound, uncompress 74 | CRC-32, Adler-32 - Using modern, minimal code size, CPU cache friendly routines. 75 | Supports raw deflate streams or standard zlib streams with adler-32 checking. 76 | 77 | Limitations: 78 | The callback API's are not implemented yet. No support for gzip headers or zlib static dictionaries. 79 | I've tried to closely emulate zlib's various flavors of stream flushing and return status codes, but 80 | there are no guarantees that miniz.c pulls this off perfectly. 81 | 82 | * PNG writing: See the tdefl_write_image_to_png_file_in_memory() function, originally written by 83 | Alex Evans. Supports 1-4 bytes/pixel images. 84 | 85 | * ZIP archive API notes: 86 | 87 | The ZIP archive API's where designed with simplicity and efficiency in mind, with just enough abstraction to 88 | get the job done with minimal fuss. There are simple API's to retrieve file information, read files from 89 | existing archives, create new archives, append new files to existing archives, or clone archive data from 90 | one archive to another. It supports archives located in memory or the heap, on disk (using stdio.h), 91 | or you can specify custom file read/write callbacks. 92 | 93 | - Archive reading: Just call this function to read a single file from a disk archive: 94 | 95 | void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, 96 | size_t *pSize, mz_uint zip_flags); 97 | 98 | For more complex cases, use the "mz_zip_reader" functions. Upon opening an archive, the entire central 99 | directory is located and read as-is into memory, and subsequent file access only occurs when reading individual files. 100 | 101 | - Archives file scanning: The simple way is to use this function to scan a loaded archive for a specific file: 102 | 103 | int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags); 104 | 105 | The locate operation can optionally check file comments too, which (as one example) can be used to identify 106 | multiple versions of the same file in an archive. This function uses a simple linear search through the central 107 | directory, so it's not very fast. 108 | 109 | Alternately, you can iterate through all the files in an archive (using mz_zip_reader_get_num_files()) and 110 | retrieve detailed info on each file by calling mz_zip_reader_file_stat(). 111 | 112 | - Archive creation: Use the "mz_zip_writer" functions. The ZIP writer immediately writes compressed file data 113 | to disk and builds an exact image of the central directory in memory. The central directory image is written 114 | all at once at the end of the archive file when the archive is finalized. 115 | 116 | The archive writer can optionally align each file's local header and file data to any power of 2 alignment, 117 | which can be useful when the archive will be read from optical media. Also, the writer supports placing 118 | arbitrary data blobs at the very beginning of ZIP archives. Archives written using either feature are still 119 | readable by any ZIP tool. 120 | 121 | - Archive appending: The simple way to add a single file to an archive is to call this function: 122 | 123 | mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, 124 | const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); 125 | 126 | The archive will be created if it doesn't already exist, otherwise it'll be appended to. 127 | Note the appending is done in-place and is not an atomic operation, so if something goes wrong 128 | during the operation it's possible the archive could be left without a central directory (although the local 129 | file headers and file data will be fine, so the archive will be recoverable). 130 | 131 | For more complex archive modification scenarios: 132 | 1. The safest way is to use a mz_zip_reader to read the existing archive, cloning only those bits you want to 133 | preserve into a new archive using using the mz_zip_writer_add_from_zip_reader() function (which compiles the 134 | compressed file data as-is). When you're done, delete the old archive and rename the newly written archive, and 135 | you're done. This is safe but requires a bunch of temporary disk space or heap memory. 136 | 137 | 2. Or, you can convert an mz_zip_reader in-place to an mz_zip_writer using mz_zip_writer_init_from_reader(), 138 | append new files as needed, then finalize the archive which will write an updated central directory to the 139 | original archive. (This is basically what mz_zip_add_mem_to_archive_file_in_place() does.) There's a 140 | possibility that the archive's central directory could be lost with this method if anything goes wrong, though. 141 | 142 | - ZIP archive support limitations: 143 | No zip64 or spanning support. Extraction functions can only handle unencrypted, stored or deflated files. 144 | Requires streams capable of seeking. 145 | 146 | * This is a header file library, like stb_image.c. To get only a header file, either cut and paste the 147 | below header, or create miniz.h, #define MINIZ_HEADER_FILE_ONLY, and then include miniz.c from it. 148 | 149 | * Important: For best perf. be sure to customize the below macros for your target platform: 150 | #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 151 | #define MINIZ_LITTLE_ENDIAN 1 152 | #define MINIZ_HAS_64BIT_REGISTERS 1 153 | 154 | * On platforms using glibc, Be sure to "#define _LARGEFILE64_SOURCE 1" before including miniz.c to ensure miniz 155 | uses the 64-bit variants: fopen64(), stat64(), etc. Otherwise you won't be able to process large files 156 | (i.e. 32-bit stat() fails for me on files > 0x7FFFFFFF bytes). 157 | */ 158 | 159 | #include 160 | 161 | // Defines to completely disable specific portions of miniz.c: 162 | // If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl. 163 | 164 | // Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O. 165 | //#define MINIZ_NO_STDIO 166 | 167 | // If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able to get the current time, or 168 | // get/set file times, and the C run-time funcs that get/set times won't be called. 169 | // The current downside is the times written to your archives will be from 1979. 170 | //#define MINIZ_NO_TIME 171 | 172 | // Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's. 173 | //#define MINIZ_NO_ARCHIVE_APIS 174 | 175 | // Define MINIZ_NO_ARCHIVE_APIS to disable all writing related ZIP archive API's. 176 | //#define MINIZ_NO_ARCHIVE_WRITING_APIS 177 | 178 | // Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's. 179 | //#define MINIZ_NO_ZLIB_APIS 180 | 181 | // Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib. 182 | //#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES 183 | 184 | // Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc. 185 | // Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc 186 | // callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user 187 | // functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work. 188 | //#define MINIZ_NO_MALLOC 189 | 190 | #if defined(__TINYC__) && (defined(__linux) || defined(__linux__)) 191 | // TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux 192 | #define MINIZ_NO_TIME 193 | #endif 194 | 195 | #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS) 196 | #include 197 | #endif 198 | 199 | #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__) 200 | // MINIZ_X86_OR_X64_CPU is only used to help set the below macros. 201 | #define MINIZ_X86_OR_X64_CPU 1 202 | #endif 203 | 204 | #if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU 205 | // Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. 206 | #define MINIZ_LITTLE_ENDIAN 1 207 | #endif 208 | 209 | #if MINIZ_X86_OR_X64_CPU 210 | // Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses. 211 | #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1 212 | #endif 213 | 214 | #if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__) 215 | // Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are reasonably fast (and don't involve compiler generated calls to helper functions). 216 | #define MINIZ_HAS_64BIT_REGISTERS 1 217 | #endif 218 | 219 | 220 | // ------------------- zlib-style API Definitions. 221 | 222 | // For more compatibility with zlib, miniz.c uses unsigned long for some parameters/struct members. Beware: mz_ulong can be either 32 or 64-bits! 223 | typedef unsigned long mz_ulong; 224 | 225 | // mz_free() internally uses the MZ_FREE() macro (which by default calls free() unless you've modified the MZ_MALLOC macro) to release a block allocated from the heap. 226 | void mz_free(void *p); 227 | 228 | #define MZ_ADLER32_INIT (1) 229 | // mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL. 230 | mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len); 231 | 232 | #define MZ_CRC32_INIT (0) 233 | // mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL. 234 | mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, size_t buf_len); 235 | 236 | // Compression strategies. 237 | enum { MZ_DEFAULT_STRATEGY = 0, MZ_FILTERED = 1, MZ_HUFFMAN_ONLY = 2, MZ_RLE = 3, MZ_FIXED = 4 }; 238 | 239 | // Method 240 | #define MZ_DEFLATED 8 241 | 242 | #ifndef MINIZ_NO_ZLIB_APIS 243 | 244 | // Heap allocation callbacks. 245 | // Note that mz_alloc_func parameter types purpsosely differ from zlib's: items/size is size_t, not unsigned long. 246 | typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size); 247 | typedef void (*mz_free_func)(void *opaque, void *address); 248 | typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size); 249 | 250 | #define MZ_VERSION "9.1.15" 251 | #define MZ_VERNUM 0x91F0 252 | #define MZ_VER_MAJOR 9 253 | #define MZ_VER_MINOR 1 254 | #define MZ_VER_REVISION 15 255 | #define MZ_VER_SUBREVISION 0 256 | 257 | // Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The other values are for advanced use (refer to the zlib docs). 258 | enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 }; 259 | 260 | // Return status codes. MZ_PARAM_ERROR is non-standard. 261 | enum { MZ_OK = 0, MZ_STREAM_END = 1, MZ_NEED_DICT = 2, MZ_ERRNO = -1, MZ_STREAM_ERROR = -2, MZ_DATA_ERROR = -3, MZ_MEM_ERROR = -4, MZ_BUF_ERROR = -5, MZ_VERSION_ERROR = -6, MZ_PARAM_ERROR = -10000 }; 262 | 263 | // Compression levels: 0-9 are the standard zlib-style levels, 10 is best possible compression (not zlib compatible, and may be very slow), MZ_DEFAULT_COMPRESSION=MZ_DEFAULT_LEVEL. 264 | enum { MZ_NO_COMPRESSION = 0, MZ_BEST_SPEED = 1, MZ_BEST_COMPRESSION = 9, MZ_UBER_COMPRESSION = 10, MZ_DEFAULT_LEVEL = 6, MZ_DEFAULT_COMPRESSION = -1 }; 265 | 266 | // Window bits 267 | #define MZ_DEFAULT_WINDOW_BITS 15 268 | 269 | struct mz_internal_state; 270 | 271 | // Compression/decompression stream struct. 272 | typedef struct mz_stream_s 273 | { 274 | const unsigned char *next_in; // pointer to next byte to read 275 | unsigned int avail_in; // number of bytes available at next_in 276 | mz_ulong total_in; // total number of bytes consumed so far 277 | 278 | unsigned char *next_out; // pointer to next byte to write 279 | unsigned int avail_out; // number of bytes that can be written to next_out 280 | mz_ulong total_out; // total number of bytes produced so far 281 | 282 | char *msg; // error msg (unused) 283 | struct mz_internal_state *state; // internal state, allocated by zalloc/zfree 284 | 285 | mz_alloc_func zalloc; // optional heap allocation function (defaults to malloc) 286 | mz_free_func zfree; // optional heap free function (defaults to free) 287 | void *opaque; // heap alloc function user pointer 288 | 289 | int data_type; // data_type (unused) 290 | mz_ulong adler; // adler32 of the source or uncompressed data 291 | mz_ulong reserved; // not used 292 | } mz_stream; 293 | 294 | typedef mz_stream *mz_streamp; 295 | 296 | // Returns the version string of miniz.c. 297 | const char *mz_version(void); 298 | 299 | // mz_deflateInit() initializes a compressor with default options: 300 | // Parameters: 301 | // pStream must point to an initialized mz_stream struct. 302 | // level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION]. 303 | // level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio. 304 | // (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.) 305 | // Return values: 306 | // MZ_OK on success. 307 | // MZ_STREAM_ERROR if the stream is bogus. 308 | // MZ_PARAM_ERROR if the input parameters are bogus. 309 | // MZ_MEM_ERROR on out of memory. 310 | int mz_deflateInit(mz_streamp pStream, int level); 311 | 312 | // mz_deflateInit2() is like mz_deflate(), except with more control: 313 | // Additional parameters: 314 | // method must be MZ_DEFLATED 315 | // window_bits must be MZ_DEFAULT_WINDOW_BITS (to wrap the deflate stream with zlib header/adler-32 footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate/no header or footer) 316 | // mem_level must be between [1, 9] (it's checked but ignored by miniz.c) 317 | int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy); 318 | 319 | // Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2(). 320 | int mz_deflateReset(mz_streamp pStream); 321 | 322 | // mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible. 323 | // Parameters: 324 | // pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members. 325 | // flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH. 326 | // Return values: 327 | // MZ_OK on success (when flushing, or if more input is needed but not available, and/or there's more output to be written but the output buffer is full). 328 | // MZ_STREAM_END if all input has been consumed and all output bytes have been written. Don't call mz_deflate() on the stream anymore. 329 | // MZ_STREAM_ERROR if the stream is bogus. 330 | // MZ_PARAM_ERROR if one of the parameters is invalid. 331 | // MZ_BUF_ERROR if no forward progress is possible because the input and/or output buffers are empty. (Fill up the input buffer or free up some output space and try again.) 332 | int mz_deflate(mz_streamp pStream, int flush); 333 | 334 | // mz_deflateEnd() deinitializes a compressor: 335 | // Return values: 336 | // MZ_OK on success. 337 | // MZ_STREAM_ERROR if the stream is bogus. 338 | int mz_deflateEnd(mz_streamp pStream); 339 | 340 | // mz_deflateBound() returns a (very) conservative upper bound on the amount of data that could be generated by deflate(), assuming flush is set to only MZ_NO_FLUSH or MZ_FINISH. 341 | mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len); 342 | 343 | // Single-call compression functions mz_compress() and mz_compress2(): 344 | // Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure. 345 | int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len); 346 | int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level); 347 | 348 | // mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress(). 349 | mz_ulong mz_compressBound(mz_ulong source_len); 350 | 351 | // Initializes a decompressor. 352 | int mz_inflateInit(mz_streamp pStream); 353 | 354 | // mz_inflateInit2() is like mz_inflateInit() with an additional option that controls the window size and whether or not the stream has been wrapped with a zlib header/footer: 355 | // window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate). 356 | int mz_inflateInit2(mz_streamp pStream, int window_bits); 357 | 358 | // Decompresses the input stream to the output, consuming only as much of the input as needed, and writing as much to the output as possible. 359 | // Parameters: 360 | // pStream is the stream to read from and write to. You must initialize/update the next_in, avail_in, next_out, and avail_out members. 361 | // flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH. 362 | // On the first call, if flush is MZ_FINISH it's assumed the input and output buffers are both sized large enough to decompress the entire stream in a single call (this is slightly faster). 363 | // MZ_FINISH implies that there are no more source bytes available beside what's already in the input buffer, and that the output buffer is large enough to hold the rest of the decompressed data. 364 | // Return values: 365 | // MZ_OK on success. Either more input is needed but not available, and/or there's more output to be written but the output buffer is full. 366 | // MZ_STREAM_END if all needed input has been consumed and all output bytes have been written. For zlib streams, the adler-32 of the decompressed data has also been verified. 367 | // MZ_STREAM_ERROR if the stream is bogus. 368 | // MZ_DATA_ERROR if the deflate stream is invalid. 369 | // MZ_PARAM_ERROR if one of the parameters is invalid. 370 | // MZ_BUF_ERROR if no forward progress is possible because the input buffer is empty but the inflater needs more input to continue, or if the output buffer is not large enough. Call mz_inflate() again 371 | // with more input data, or with more room in the output buffer (except when using single call decompression, described above). 372 | int mz_inflate(mz_streamp pStream, int flush); 373 | 374 | // Deinitializes a decompressor. 375 | int mz_inflateEnd(mz_streamp pStream); 376 | 377 | // Single-call decompression. 378 | // Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure. 379 | int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len); 380 | 381 | // Returns a string description of the specified error code, or NULL if the error code is invalid. 382 | const char *mz_error(int err); 383 | 384 | // Redefine zlib-compatible names to miniz equivalents, so miniz.c can be used as a drop-in replacement for the subset of zlib that miniz.c supports. 385 | // Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project. 386 | #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES 387 | typedef unsigned char Byte; 388 | typedef unsigned int uInt; 389 | typedef mz_ulong uLong; 390 | typedef Byte Bytef; 391 | typedef uInt uIntf; 392 | typedef char charf; 393 | typedef int intf; 394 | typedef void *voidpf; 395 | typedef uLong uLongf; 396 | typedef void *voidp; 397 | typedef void *const voidpc; 398 | #define Z_NULL 0 399 | #define Z_NO_FLUSH MZ_NO_FLUSH 400 | #define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH 401 | #define Z_SYNC_FLUSH MZ_SYNC_FLUSH 402 | #define Z_FULL_FLUSH MZ_FULL_FLUSH 403 | #define Z_FINISH MZ_FINISH 404 | #define Z_BLOCK MZ_BLOCK 405 | #define Z_OK MZ_OK 406 | #define Z_STREAM_END MZ_STREAM_END 407 | #define Z_NEED_DICT MZ_NEED_DICT 408 | #define Z_ERRNO MZ_ERRNO 409 | #define Z_STREAM_ERROR MZ_STREAM_ERROR 410 | #define Z_DATA_ERROR MZ_DATA_ERROR 411 | #define Z_MEM_ERROR MZ_MEM_ERROR 412 | #define Z_BUF_ERROR MZ_BUF_ERROR 413 | #define Z_VERSION_ERROR MZ_VERSION_ERROR 414 | #define Z_PARAM_ERROR MZ_PARAM_ERROR 415 | #define Z_NO_COMPRESSION MZ_NO_COMPRESSION 416 | #define Z_BEST_SPEED MZ_BEST_SPEED 417 | #define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION 418 | #define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION 419 | #define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY 420 | #define Z_FILTERED MZ_FILTERED 421 | #define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY 422 | #define Z_RLE MZ_RLE 423 | #define Z_FIXED MZ_FIXED 424 | #define Z_DEFLATED MZ_DEFLATED 425 | #define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS 426 | #define alloc_func mz_alloc_func 427 | #define free_func mz_free_func 428 | #define internal_state mz_internal_state 429 | #define z_stream mz_stream 430 | #define deflateInit mz_deflateInit 431 | #define deflateInit2 mz_deflateInit2 432 | #define deflateReset mz_deflateReset 433 | #define deflate mz_deflate 434 | #define deflateEnd mz_deflateEnd 435 | #define deflateBound mz_deflateBound 436 | #define compress mz_compress 437 | #define compress2 mz_compress2 438 | #define compressBound mz_compressBound 439 | #define inflateInit mz_inflateInit 440 | #define inflateInit2 mz_inflateInit2 441 | #define inflate mz_inflate 442 | #define inflateEnd mz_inflateEnd 443 | #define uncompress mz_uncompress 444 | #define crc32 mz_crc32 445 | #define adler32 mz_adler32 446 | #define MAX_WBITS 15 447 | #define MAX_MEM_LEVEL 9 448 | #define zError mz_error 449 | #define ZLIB_VERSION MZ_VERSION 450 | #define ZLIB_VERNUM MZ_VERNUM 451 | #define ZLIB_VER_MAJOR MZ_VER_MAJOR 452 | #define ZLIB_VER_MINOR MZ_VER_MINOR 453 | #define ZLIB_VER_REVISION MZ_VER_REVISION 454 | #define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION 455 | #define zlibVersion mz_version 456 | #define zlib_version mz_version() 457 | #endif // #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES 458 | 459 | #endif // MINIZ_NO_ZLIB_APIS 460 | 461 | // ------------------- Types and macros 462 | 463 | typedef unsigned char mz_uint8; 464 | typedef signed short mz_int16; 465 | typedef unsigned short mz_uint16; 466 | typedef unsigned int mz_uint32; 467 | typedef unsigned int mz_uint; 468 | typedef long long mz_int64; 469 | typedef unsigned long long mz_uint64; 470 | typedef int mz_bool; 471 | 472 | #define MZ_FALSE (0) 473 | #define MZ_TRUE (1) 474 | 475 | // An attempt to work around MSVC's spammy "warning C4127: conditional expression is constant" message. 476 | #ifdef _MSC_VER 477 | #define MZ_MACRO_END while (0, 0) 478 | #else 479 | #define MZ_MACRO_END while (0) 480 | #endif 481 | 482 | // ------------------- ZIP archive reading/writing 483 | 484 | #ifndef MINIZ_NO_ARCHIVE_APIS 485 | 486 | enum 487 | { 488 | MZ_ZIP_MAX_IO_BUF_SIZE = 64*1024, 489 | MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 260, 490 | MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 256 491 | }; 492 | 493 | typedef struct 494 | { 495 | mz_uint32 m_file_index; 496 | mz_uint32 m_central_dir_ofs; 497 | mz_uint16 m_version_made_by; 498 | mz_uint16 m_version_needed; 499 | mz_uint16 m_bit_flag; 500 | mz_uint16 m_method; 501 | //#ifndef MINIZ_NO_TIME 502 | time_t m_time; 503 | //#endif 504 | mz_uint32 m_crc32; 505 | mz_uint64 m_comp_size; 506 | mz_uint64 m_uncomp_size; 507 | mz_uint16 m_internal_attr; 508 | mz_uint32 m_external_attr; 509 | mz_uint64 m_local_header_ofs; 510 | mz_uint32 m_comment_size; 511 | char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE]; 512 | char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE]; 513 | } mz_zip_archive_file_stat; 514 | 515 | typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n); 516 | typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n); 517 | 518 | struct mz_zip_internal_state_tag; 519 | typedef struct mz_zip_internal_state_tag mz_zip_internal_state; 520 | 521 | typedef enum 522 | { 523 | MZ_ZIP_MODE_INVALID = 0, 524 | MZ_ZIP_MODE_READING = 1, 525 | MZ_ZIP_MODE_WRITING = 2, 526 | MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3 527 | } mz_zip_mode; 528 | 529 | typedef struct mz_zip_archive_tag 530 | { 531 | mz_uint64 m_archive_size; 532 | mz_uint64 m_central_directory_file_ofs; 533 | mz_uint m_total_files; 534 | mz_zip_mode m_zip_mode; 535 | 536 | mz_uint m_file_offset_alignment; 537 | 538 | mz_alloc_func m_pAlloc; 539 | mz_free_func m_pFree; 540 | mz_realloc_func m_pRealloc; 541 | void *m_pAlloc_opaque; 542 | 543 | mz_file_read_func m_pRead; 544 | mz_file_write_func m_pWrite; 545 | void *m_pIO_opaque; 546 | 547 | mz_zip_internal_state *m_pState; 548 | 549 | } mz_zip_archive; 550 | 551 | typedef enum 552 | { 553 | MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100, 554 | MZ_ZIP_FLAG_IGNORE_PATH = 0x0200, 555 | MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400, 556 | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800 557 | } mz_zip_flags; 558 | 559 | // ZIP archive reading 560 | 561 | // Inits a ZIP archive reader. 562 | // These functions read and validate the archive's central directory. 563 | mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint32 flags); 564 | mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint32 flags); 565 | 566 | #ifndef MINIZ_NO_STDIO 567 | mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags); 568 | #endif 569 | 570 | // Returns the total number of files in the archive. 571 | mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip); 572 | 573 | // Returns detailed information about an archive file entry. 574 | mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat); 575 | 576 | // Determines if an archive file entry is a directory entry. 577 | mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index); 578 | mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index); 579 | 580 | // Retrieves the filename of an archive file entry. 581 | // Returns the number of bytes written to pFilename, or if filename_buf_size is 0 this function returns the number of bytes needed to fully store the filename. 582 | mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size); 583 | 584 | // Attempts to locates a file in the archive's central directory. 585 | // Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH 586 | // Returns -1 if the file cannot be found. 587 | int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags); 588 | 589 | // Extracts a archive file to a memory buffer using no memory allocation. 590 | mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size); 591 | mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size); 592 | 593 | // Extracts a archive file to a memory buffer. 594 | mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags); 595 | mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags); 596 | 597 | // Extracts a archive file to a dynamically allocated heap buffer. 598 | void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags); 599 | void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags); 600 | 601 | // Extracts a archive file using a callback function to output the file's data. 602 | mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, void *pOpaque, mz_uint flags); 603 | mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, void *pOpaque, mz_uint flags); 604 | 605 | #ifndef MINIZ_NO_STDIO 606 | // Extracts a archive file to a disk file and sets its last accessed and modified times. 607 | // This function only extracts files, not archive directory records. 608 | mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags); 609 | mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags); 610 | #endif 611 | 612 | // Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was used. 613 | mz_bool mz_zip_reader_end(mz_zip_archive *pZip); 614 | 615 | // ZIP archive writing 616 | 617 | #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS 618 | 619 | // Inits a ZIP archive writer. 620 | mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size); 621 | mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size); 622 | 623 | #ifndef MINIZ_NO_STDIO 624 | mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning); 625 | #endif 626 | 627 | // Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an existing archive. 628 | // For archives opened using mz_zip_reader_init_file, pFilename must be the archive's filename so it can be reopened for writing. If the file can't be reopened, mz_zip_reader_end() will be called. 629 | // For archives opened using mz_zip_reader_init_mem, the memory block must be growable using the realloc callback (which defaults to realloc unless you've overridden it). 630 | // Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be NULL. 631 | // Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops or something goes wrong before 632 | // the archive is finalized the file's central directory will be hosed. 633 | mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename); 634 | 635 | // Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive. 636 | // To add a directory entry, call this method with an archive name ending in a forwardslash with empty buffer. 637 | // level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. 638 | mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, mz_uint level_and_flags); 639 | mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32); 640 | 641 | #ifndef MINIZ_NO_STDIO 642 | // Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive. 643 | // level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. 644 | mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); 645 | #endif 646 | 647 | // Adds a file to an archive by fully cloning the data from another archive. 648 | // This function fully clones the source file's compressed data (no recompression), along with its full filename, extra data, and comment fields. 649 | mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint file_index); 650 | 651 | // Finalizes the archive by writing the central directory records followed by the end of central directory record. 652 | // After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end(). 653 | // An archive must be manually finalized by calling this function for it to be valid. 654 | mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip); 655 | mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **pBuf, size_t *pSize); 656 | 657 | // Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used. 658 | // Note for the archive to be valid, it must have been finalized before ending. 659 | mz_bool mz_zip_writer_end(mz_zip_archive *pZip); 660 | 661 | // Misc. high-level helper functions: 662 | 663 | // mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive. 664 | // level_and_flags - compression level (0-10, see MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc.) logically OR'd with zero or more mz_zip_flags, or just set to MZ_DEFAULT_COMPRESSION. 665 | mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags); 666 | 667 | // Reads a single file from an archive into a heap block. 668 | // Returns NULL on failure. 669 | void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint zip_flags); 670 | 671 | #endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS 672 | 673 | #endif // #ifndef MINIZ_NO_ARCHIVE_APIS 674 | 675 | // ------------------- Low-level Decompression API Definitions 676 | 677 | // Decompression flags used by tinfl_decompress(). 678 | // TINFL_FLAG_PARSE_ZLIB_HEADER: If set, the input has a valid zlib header and ends with an adler32 checksum (it's a valid zlib stream). Otherwise, the input is a raw deflate stream. 679 | // TINFL_FLAG_HAS_MORE_INPUT: If set, there are more input bytes available beyond the end of the supplied input buffer. If clear, the input buffer contains all remaining input. 680 | // TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF: If set, the output buffer is large enough to hold the entire decompressed stream. If clear, the output buffer is at least the size of the dictionary (typically 32KB). 681 | // TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes. 682 | enum 683 | { 684 | TINFL_FLAG_PARSE_ZLIB_HEADER = 1, 685 | TINFL_FLAG_HAS_MORE_INPUT = 2, 686 | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4, 687 | TINFL_FLAG_COMPUTE_ADLER32 = 8 688 | }; 689 | 690 | // High level decompression functions: 691 | // tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc(). 692 | // On entry: 693 | // pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress. 694 | // On return: 695 | // Function returns a pointer to the decompressed data, or NULL on failure. 696 | // *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data. 697 | // The caller must call mz_free() on the returned block when it's no longer needed. 698 | void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags); 699 | 700 | // tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory. 701 | // Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success. 702 | #define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1)) 703 | size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags); 704 | 705 | // tinfl_decompress_mem_to_callback() decompresses a block in memory to an internal 32KB buffer, and a user provided callback function will be called to flush the buffer. 706 | // Returns 1 on success or 0 on failure. 707 | typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser); 708 | int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); 709 | 710 | struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor; 711 | 712 | // Max size of LZ dictionary. 713 | #define TINFL_LZ_DICT_SIZE 32768 714 | 715 | // Return status. 716 | typedef enum 717 | { 718 | TINFL_STATUS_BAD_PARAM = -3, 719 | TINFL_STATUS_ADLER32_MISMATCH = -2, 720 | TINFL_STATUS_FAILED = -1, 721 | TINFL_STATUS_DONE = 0, 722 | TINFL_STATUS_NEEDS_MORE_INPUT = 1, 723 | TINFL_STATUS_HAS_MORE_OUTPUT = 2 724 | } tinfl_status; 725 | 726 | // Initializes the decompressor to its initial state. 727 | //#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END 728 | #define tinfl_get_adler32(r) (r)->m_check_adler32 729 | 730 | // Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the other functions are just high-level helpers for improved usability. 731 | // This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. In the limit case, it can be called once per every byte input or output. 732 | tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags); 733 | 734 | // Internal/private bits follow. 735 | enum 736 | { 737 | TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19, 738 | TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS 739 | }; 740 | 741 | typedef struct 742 | { 743 | mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0]; 744 | mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2]; 745 | } tinfl_huff_table; 746 | 747 | #if MINIZ_HAS_64BIT_REGISTERS 748 | #define TINFL_USE_64BIT_BITBUF 1 749 | #endif 750 | 751 | #if TINFL_USE_64BIT_BITBUF 752 | typedef mz_uint64 tinfl_bit_buf_t; 753 | #define TINFL_BITBUF_SIZE (64) 754 | #else 755 | typedef mz_uint32 tinfl_bit_buf_t; 756 | #define TINFL_BITBUF_SIZE (32) 757 | #endif 758 | 759 | struct tinfl_decompressor_tag 760 | { 761 | mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES]; 762 | tinfl_bit_buf_t m_bit_buf; 763 | size_t m_dist_from_out_buf_start; 764 | tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES]; 765 | mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137]; 766 | }; 767 | 768 | // ------------------- Low-level Compression API Definitions 769 | 770 | // Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently). 771 | #define TDEFL_LESS_MEMORY 0 772 | 773 | // tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search): 774 | // TDEFL_DEFAULT_MAX_PROBES: The compressor defaults to 128 dictionary probes per dictionary search. 0=Huffman only, 1=Huffman+LZ (fastest/crap compression), 4095=Huffman+LZ (slowest/best compression). 775 | enum 776 | { 777 | TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF 778 | }; 779 | 780 | // TDEFL_WRITE_ZLIB_HEADER: If set, the compressor outputs a zlib header before the deflate data, and the Adler-32 of the source data at the end. Otherwise, you'll get raw deflate data. 781 | // TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers). 782 | // TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing. 783 | // TDEFL_NONDETERMINISTIC_PARSING_FLAG: Enable to decrease the compressor's initialization time to the minimum, but the output may vary from run to run given the same input (depending on the contents of memory). 784 | // TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1) 785 | // TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled. 786 | // TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables. 787 | // TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks. 788 | // The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK). 789 | enum 790 | { 791 | TDEFL_WRITE_ZLIB_HEADER = 0x01000, 792 | TDEFL_COMPUTE_ADLER32 = 0x02000, 793 | TDEFL_GREEDY_PARSING_FLAG = 0x04000, 794 | TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000, 795 | TDEFL_RLE_MATCHES = 0x10000, 796 | TDEFL_FILTER_MATCHES = 0x20000, 797 | TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000, 798 | TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000 799 | }; 800 | 801 | // High level compression functions: 802 | // tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc(). 803 | // On entry: 804 | // pSrc_buf, src_buf_len: Pointer and size of source block to compress. 805 | // flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower but improve compression. 806 | // On return: 807 | // Function returns a pointer to the compressed data, or NULL on failure. 808 | // *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data. 809 | // The caller must free() the returned block when it's no longer needed. 810 | void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags); 811 | 812 | // tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory. 813 | // Returns 0 on failure. 814 | size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags); 815 | 816 | // Compresses an image to a compressed PNG file in memory. 817 | // On entry: 818 | // pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4. 819 | // The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in memory. 820 | // level may range from [0,10], use MZ_NO_COMPRESSION, MZ_BEST_SPEED, MZ_BEST_COMPRESSION, etc. or a decent default is MZ_DEFAULT_LEVEL 821 | // If flip is true, the image will be flipped on the Y axis (useful for OpenGL apps). 822 | // On return: 823 | // Function returns a pointer to the compressed data, or NULL on failure. 824 | // *pLen_out will be set to the size of the PNG image file. 825 | // The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no longer needed. 826 | void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, size_t *pLen_out, mz_uint level, mz_bool flip); 827 | void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out); 828 | 829 | // Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time. 830 | typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser); 831 | 832 | // tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally. 833 | mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); 834 | 835 | enum { TDEFL_MAX_HUFF_TABLES = 3, TDEFL_MAX_HUFF_SYMBOLS_0 = 288, TDEFL_MAX_HUFF_SYMBOLS_1 = 32, TDEFL_MAX_HUFF_SYMBOLS_2 = 19, TDEFL_LZ_DICT_SIZE = 32768, TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1, TDEFL_MIN_MATCH_LEN = 3, TDEFL_MAX_MATCH_LEN = 258 }; 836 | 837 | // TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes). 838 | #if TDEFL_LESS_MEMORY 839 | enum { TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024, TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13 ) / 10, TDEFL_MAX_HUFF_SYMBOLS = 288, TDEFL_LZ_HASH_BITS = 12, TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS }; 840 | #else 841 | enum { TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024, TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13 ) / 10, TDEFL_MAX_HUFF_SYMBOLS = 288, TDEFL_LZ_HASH_BITS = 15, TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS }; 842 | #endif 843 | 844 | // The low-level tdefl functions below may be used directly if the above helper functions aren't flexible enough. The low-level functions don't make any heap allocations, unlike the above helper functions. 845 | typedef enum 846 | { 847 | TDEFL_STATUS_BAD_PARAM = -2, 848 | TDEFL_STATUS_PUT_BUF_FAILED = -1, 849 | TDEFL_STATUS_OKAY = 0, 850 | TDEFL_STATUS_DONE = 1, 851 | } tdefl_status; 852 | 853 | // Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums 854 | typedef enum 855 | { 856 | TDEFL_NO_FLUSH = 0, 857 | TDEFL_SYNC_FLUSH = 2, 858 | TDEFL_FULL_FLUSH = 3, 859 | TDEFL_FINISH = 4 860 | } tdefl_flush; 861 | 862 | // tdefl's compression state structure. 863 | typedef struct 864 | { 865 | tdefl_put_buf_func_ptr m_pPut_buf_func; 866 | void *m_pPut_buf_user; 867 | mz_uint m_flags, m_max_probes[2]; 868 | int m_greedy_parsing; 869 | mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size; 870 | mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end; 871 | mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer; 872 | mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit, m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index, m_wants_to_finish; 873 | tdefl_status m_prev_return_status; 874 | const void *m_pIn_buf; 875 | void *m_pOut_buf; 876 | size_t *m_pIn_buf_size, *m_pOut_buf_size; 877 | tdefl_flush m_flush; 878 | const mz_uint8 *m_pSrc; 879 | size_t m_src_buf_left, m_out_buf_ofs; 880 | mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1]; 881 | mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; 882 | mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; 883 | mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS]; 884 | mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE]; 885 | mz_uint16 m_next[TDEFL_LZ_DICT_SIZE]; 886 | mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE]; 887 | mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE]; 888 | } tdefl_compressor; 889 | 890 | // Initializes the compressor. 891 | // There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory. 892 | // pBut_buf_func: If NULL, output data will be supplied to the specified callback. In this case, the user should call the tdefl_compress_buffer() API for compression. 893 | // If pBut_buf_func is NULL the user should always call the tdefl_compress() API. 894 | // flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.) 895 | tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags); 896 | 897 | // Compresses a block of data, consuming as much of the specified input buffer as possible, and writing as much compressed data to the specified output buffer as possible. 898 | tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush); 899 | 900 | // tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr. 901 | // tdefl_compress_buffer() always consumes the entire input buffer. 902 | tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush); 903 | 904 | tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d); 905 | mz_uint32 tdefl_get_adler32(tdefl_compressor *d); 906 | 907 | // Can't use tdefl_create_comp_flags_from_zip_params if MINIZ_NO_ZLIB_APIS isn't defined, because it uses some of its macros. 908 | #ifndef MINIZ_NO_ZLIB_APIS 909 | // Create tdefl_compress() flags given zlib-style compression parameters. 910 | // level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files) 911 | // window_bits may be -15 (raw deflate) or 15 (zlib) 912 | // strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED 913 | mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy); 914 | #endif // #ifndef MINIZ_NO_ZLIB_APIS 915 | --------------------------------------------------------------------------------