Fossil SCM

fossil-scm / src / miniz.h
Blame History Raw 3076 lines
1
#ifndef MINIZ_HEADER_INCLUDED
2
#define MINIZ_HEADER_INCLUDED
3
4
#include <stdlib.h>
5
6
// Defines to completely disable specific portions of miniz.c:
7
// If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl.
8
9
// Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O.
10
#define MINIZ_NO_STDIO
11
12
// If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able to get the current time, or
13
// get/set file times, and the C run-time funcs that get/set times won't be called.
14
// The current downside is the times written to your archives will be from 1979.
15
#define MINIZ_NO_TIME
16
17
// Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's.
18
#define MINIZ_NO_ARCHIVE_APIS
19
20
// Define MINIZ_NO_ARCHIVE_APIS to disable all writing related ZIP archive API's.
21
#define MINIZ_NO_ARCHIVE_WRITING_APIS
22
23
// Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's.
24
//#define MINIZ_NO_ZLIB_APIS
25
26
// Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib.
27
//#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
28
29
// Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc.
30
// Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc
31
// callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user
32
// functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work.
33
//#define MINIZ_NO_MALLOC
34
35
#if defined(__TINYC__) && (defined(__linux) || defined(__linux__))
36
// TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux
37
#define MINIZ_NO_TIME
38
#endif
39
40
#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS)
41
#include <time.h>
42
#endif
43
44
#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__)
45
// MINIZ_X86_OR_X64_CPU is only used to help set the below macros.
46
#define MINIZ_X86_OR_X64_CPU 1
47
#endif
48
49
#if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
50
// Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian.
51
#define MINIZ_LITTLE_ENDIAN 1
52
#endif
53
54
#if MINIZ_X86_OR_X64_CPU
55
// Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses.
56
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
57
#endif
58
59
#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
60
// 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).
61
#define MINIZ_HAS_64BIT_REGISTERS 1
62
#endif
63
64
#ifdef __cplusplus
65
extern "C" {
66
#endif
67
68
// ------------------- zlib-style API Definitions.
69
70
// 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!
71
typedef unsigned long mz_ulong;
72
73
// 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.
74
void mz_free(void *p);
75
76
#define MZ_ADLER32_INIT (1)
77
// mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL.
78
mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len);
79
80
#define MZ_CRC32_INIT (0)
81
// mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL.
82
mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, size_t buf_len);
83
84
// Compression strategies.
85
enum { MZ_DEFAULT_STRATEGY = 0, MZ_FILTERED = 1, MZ_HUFFMAN_ONLY = 2, MZ_RLE = 3, MZ_FIXED = 4 };
86
87
// Method
88
#define MZ_DEFLATED 8
89
90
#ifndef MINIZ_NO_ZLIB_APIS
91
92
// Heap allocation callbacks.
93
// Note that mz_alloc_func parameter types purpsosely differ from zlib's: items/size is size_t, not unsigned long.
94
typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size);
95
typedef void (*mz_free_func)(void *opaque, void *address);
96
typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size);
97
98
#define MZ_VERSION "9.1.15"
99
#define MZ_VERNUM 0x91F0
100
#define MZ_VER_MAJOR 9
101
#define MZ_VER_MINOR 1
102
#define MZ_VER_REVISION 15
103
#define MZ_VER_SUBREVISION 0
104
105
// 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).
106
enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 };
107
108
// Return status codes. MZ_PARAM_ERROR is non-standard.
109
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 };
110
111
// 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.
112
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 };
113
114
// Window bits
115
#define MZ_DEFAULT_WINDOW_BITS 15
116
117
struct mz_internal_state;
118
119
// Compression/decompression stream struct.
120
typedef struct mz_stream_s
121
{
122
const unsigned char *next_in; // pointer to next byte to read
123
unsigned int avail_in; // number of bytes available at next_in
124
mz_ulong total_in; // total number of bytes consumed so far
125
126
unsigned char *next_out; // pointer to next byte to write
127
unsigned int avail_out; // number of bytes that can be written to next_out
128
mz_ulong total_out; // total number of bytes produced so far
129
130
char *msg; // error msg (unused)
131
struct mz_internal_state *state; // internal state, allocated by zalloc/zfree
132
133
mz_alloc_func zalloc; // optional heap allocation function (defaults to malloc)
134
mz_free_func zfree; // optional heap free function (defaults to free)
135
void *opaque; // heap alloc function user pointer
136
137
int data_type; // data_type (unused)
138
mz_ulong adler; // adler32 of the source or uncompressed data
139
mz_ulong reserved; // not used
140
} mz_stream;
141
142
typedef mz_stream *mz_streamp;
143
144
// Returns the version string of miniz.c.
145
const char *mz_version(void);
146
147
// mz_deflateInit() initializes a compressor with default options:
148
// Parameters:
149
// pStream must point to an initialized mz_stream struct.
150
// level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION].
151
// level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio.
152
// (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.)
153
// Return values:
154
// MZ_OK on success.
155
// MZ_STREAM_ERROR if the stream is bogus.
156
// MZ_PARAM_ERROR if the input parameters are bogus.
157
// MZ_MEM_ERROR on out of memory.
158
int mz_deflateInit(mz_streamp pStream, int level);
159
160
// mz_deflateInit2() is like mz_deflate(), except with more control:
161
// Additional parameters:
162
// method must be MZ_DEFLATED
163
// 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)
164
// mem_level must be between [1, 9] (it's checked but ignored by miniz.c)
165
int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy);
166
167
// Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2().
168
int mz_deflateReset(mz_streamp pStream);
169
170
// mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible.
171
// Parameters:
172
// 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.
173
// flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH.
174
// Return values:
175
// 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).
176
// 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.
177
// MZ_STREAM_ERROR if the stream is bogus.
178
// MZ_PARAM_ERROR if one of the parameters is invalid.
179
// 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.)
180
int mz_deflate(mz_streamp pStream, int flush);
181
182
// mz_deflateEnd() deinitializes a compressor:
183
// Return values:
184
// MZ_OK on success.
185
// MZ_STREAM_ERROR if the stream is bogus.
186
int mz_deflateEnd(mz_streamp pStream);
187
188
// 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.
189
mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len);
190
191
// Single-call compression functions mz_compress() and mz_compress2():
192
// Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure.
193
int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
194
int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level);
195
196
// mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress().
197
mz_ulong mz_compressBound(mz_ulong source_len);
198
199
// Initializes a decompressor.
200
int mz_inflateInit(mz_streamp pStream);
201
202
// 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:
203
// window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate).
204
int mz_inflateInit2(mz_streamp pStream, int window_bits);
205
206
// 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.
207
// Parameters:
208
// 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.
209
// flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH.
210
// 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).
211
// 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.
212
// Return values:
213
// 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.
214
// 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.
215
// MZ_STREAM_ERROR if the stream is bogus.
216
// MZ_DATA_ERROR if the deflate stream is invalid.
217
// MZ_PARAM_ERROR if one of the parameters is invalid.
218
// 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
219
// with more input data, or with more room in the output buffer (except when using single call decompression, described above).
220
int mz_inflate(mz_streamp pStream, int flush);
221
222
// Deinitializes a decompressor.
223
int mz_inflateEnd(mz_streamp pStream);
224
225
// Single-call decompression.
226
// Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure.
227
int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
228
229
// Returns a string description of the specified error code, or NULL if the error code is invalid.
230
const char *mz_error(int err);
231
232
// 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.
233
// Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project.
234
#ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
235
typedef unsigned char Byte;
236
typedef unsigned int uInt;
237
typedef mz_ulong uLong;
238
typedef Byte Bytef;
239
typedef uInt uIntf;
240
typedef char charf;
241
typedef int intf;
242
typedef void *voidpf;
243
typedef uLong uLongf;
244
typedef void *voidp;
245
typedef void *const voidpc;
246
#define Z_NULL 0
247
#define Z_NO_FLUSH MZ_NO_FLUSH
248
#define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH
249
#define Z_SYNC_FLUSH MZ_SYNC_FLUSH
250
#define Z_FULL_FLUSH MZ_FULL_FLUSH
251
#define Z_FINISH MZ_FINISH
252
#define Z_BLOCK MZ_BLOCK
253
#define Z_OK MZ_OK
254
#define Z_STREAM_END MZ_STREAM_END
255
#define Z_NEED_DICT MZ_NEED_DICT
256
#define Z_ERRNO MZ_ERRNO
257
#define Z_STREAM_ERROR MZ_STREAM_ERROR
258
#define Z_DATA_ERROR MZ_DATA_ERROR
259
#define Z_MEM_ERROR MZ_MEM_ERROR
260
#define Z_BUF_ERROR MZ_BUF_ERROR
261
#define Z_VERSION_ERROR MZ_VERSION_ERROR
262
#define Z_PARAM_ERROR MZ_PARAM_ERROR
263
#define Z_NO_COMPRESSION MZ_NO_COMPRESSION
264
#define Z_BEST_SPEED MZ_BEST_SPEED
265
#define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION
266
#define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION
267
#define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY
268
#define Z_FILTERED MZ_FILTERED
269
#define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY
270
#define Z_RLE MZ_RLE
271
#define Z_FIXED MZ_FIXED
272
#define Z_DEFLATED MZ_DEFLATED
273
#define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS
274
#define alloc_func mz_alloc_func
275
#define free_func mz_free_func
276
#define internal_state mz_internal_state
277
#define z_stream mz_stream
278
#define deflateInit mz_deflateInit
279
#define deflateInit2 mz_deflateInit2
280
#define deflateReset mz_deflateReset
281
#define deflate mz_deflate
282
#define deflateEnd mz_deflateEnd
283
#define deflateBound mz_deflateBound
284
#define compress mz_compress
285
#define compress2 mz_compress2
286
#define compressBound mz_compressBound
287
#define inflateInit mz_inflateInit
288
#define inflateInit2 mz_inflateInit2
289
#define inflate mz_inflate
290
#define inflateEnd mz_inflateEnd
291
#define uncompress mz_uncompress
292
#define crc32 mz_crc32
293
#define adler32 mz_adler32
294
#define MAX_WBITS 15
295
#define MAX_MEM_LEVEL 9
296
#define zError mz_error
297
#define ZLIB_VERSION MZ_VERSION
298
#define ZLIB_VERNUM MZ_VERNUM
299
#define ZLIB_VER_MAJOR MZ_VER_MAJOR
300
#define ZLIB_VER_MINOR MZ_VER_MINOR
301
#define ZLIB_VER_REVISION MZ_VER_REVISION
302
#define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION
303
#define zlibVersion mz_version
304
#define zlib_version mz_version()
305
#endif // #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
306
307
#endif // MINIZ_NO_ZLIB_APIS
308
309
// ------------------- Types and macros
310
311
typedef unsigned char mz_uint8;
312
typedef signed short mz_int16;
313
typedef unsigned short mz_uint16;
314
typedef unsigned int mz_uint32;
315
typedef unsigned int mz_uint;
316
typedef long long mz_int64;
317
typedef unsigned long long mz_uint64;
318
typedef int mz_bool;
319
320
#define MZ_FALSE (0)
321
#define MZ_TRUE (1)
322
323
// An attempt to work around MSVC's spammy "warning C4127: conditional expression is constant" message.
324
#ifdef _MSC_VER
325
#define MZ_MACRO_END while (0, 0)
326
#else
327
#define MZ_MACRO_END while (0)
328
#endif
329
330
// ------------------- ZIP archive reading/writing
331
332
#ifndef MINIZ_NO_ARCHIVE_APIS
333
334
enum
335
{
336
MZ_ZIP_MAX_IO_BUF_SIZE = 64*1024,
337
MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 260,
338
MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 256
339
};
340
341
typedef struct
342
{
343
mz_uint32 m_file_index;
344
mz_uint32 m_central_dir_ofs;
345
mz_uint16 m_version_made_by;
346
mz_uint16 m_version_needed;
347
mz_uint16 m_bit_flag;
348
mz_uint16 m_method;
349
#ifndef MINIZ_NO_TIME
350
time_t m_time;
351
#endif
352
mz_uint32 m_crc32;
353
mz_uint64 m_comp_size;
354
mz_uint64 m_uncomp_size;
355
mz_uint16 m_internal_attr;
356
mz_uint32 m_external_attr;
357
mz_uint64 m_local_header_ofs;
358
mz_uint32 m_comment_size;
359
char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
360
char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
361
} mz_zip_archive_file_stat;
362
363
typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n);
364
typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n);
365
366
struct mz_zip_internal_state_tag;
367
typedef struct mz_zip_internal_state_tag mz_zip_internal_state;
368
369
typedef enum
370
{
371
MZ_ZIP_MODE_INVALID = 0,
372
MZ_ZIP_MODE_READING = 1,
373
MZ_ZIP_MODE_WRITING = 2,
374
MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3
375
} mz_zip_mode;
376
377
typedef struct mz_zip_archive_tag
378
{
379
mz_uint64 m_archive_size;
380
mz_uint64 m_central_directory_file_ofs;
381
mz_uint m_total_files;
382
mz_zip_mode m_zip_mode;
383
384
mz_uint m_file_offset_alignment;
385
386
mz_alloc_func m_pAlloc;
387
mz_free_func m_pFree;
388
mz_realloc_func m_pRealloc;
389
void *m_pAlloc_opaque;
390
391
mz_file_read_func m_pRead;
392
mz_file_write_func m_pWrite;
393
void *m_pIO_opaque;
394
395
mz_zip_internal_state *m_pState;
396
397
} mz_zip_archive;
398
399
typedef enum
400
{
401
MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100,
402
MZ_ZIP_FLAG_IGNORE_PATH = 0x0200,
403
MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400,
404
MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800
405
} mz_zip_flags;
406
407
// ZIP archive reading
408
409
// Inits a ZIP archive reader.
410
// These functions read and validate the archive's central directory.
411
mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint32 flags);
412
mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint32 flags);
413
414
#ifndef MINIZ_NO_STDIO
415
mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags);
416
#endif
417
418
// Returns the total number of files in the archive.
419
mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip);
420
421
// Returns detailed information about an archive file entry.
422
mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat);
423
424
// Determines if an archive file entry is a directory entry.
425
mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index);
426
mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index);
427
428
// Retrieves the filename of an archive file entry.
429
// 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.
430
mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size);
431
432
// Attempts to locates a file in the archive's central directory.
433
// Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH
434
// Returns -1 if the file cannot be found.
435
int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags);
436
437
// Extracts a archive file to a memory buffer using no memory allocation.
438
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);
439
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);
440
441
// Extracts a archive file to a memory buffer.
442
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);
443
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);
444
445
// Extracts a archive file to a dynamically allocated heap buffer.
446
void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags);
447
void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags);
448
449
// Extracts a archive file using a callback function to output the file's data.
450
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);
451
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);
452
453
#ifndef MINIZ_NO_STDIO
454
// Extracts a archive file to a disk file and sets its last accessed and modified times.
455
// This function only extracts files, not archive directory records.
456
mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags);
457
mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags);
458
#endif
459
460
// Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was used.
461
mz_bool mz_zip_reader_end(mz_zip_archive *pZip);
462
463
// ZIP archive writing
464
465
#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
466
467
// Inits a ZIP archive writer.
468
mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size);
469
mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size);
470
471
#ifndef MINIZ_NO_STDIO
472
mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning);
473
#endif
474
475
// Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an existing archive.
476
// 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.
477
// 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).
478
// Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be NULL.
479
// Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops or something goes wrong before
480
// the archive is finalized the file's central directory will be hosed.
481
mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename);
482
483
// Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive.
484
// To add a directory entry, call this method with an archive name ending in a forwardslash with empty buffer.
485
// 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.
486
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);
487
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);
488
489
#ifndef MINIZ_NO_STDIO
490
// Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive.
491
// 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.
492
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);
493
#endif
494
495
// Adds a file to an archive by fully cloning the data from another archive.
496
// This function fully clones the source file's compressed data (no recompression), along with its full filename, extra data, and comment fields.
497
mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint file_index);
498
499
// Finalizes the archive by writing the central directory records followed by the end of central directory record.
500
// After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end().
501
// An archive must be manually finalized by calling this function for it to be valid.
502
mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip);
503
mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **pBuf, size_t *pSize);
504
505
// Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used.
506
// Note for the archive to be valid, it must have been finalized before ending.
507
mz_bool mz_zip_writer_end(mz_zip_archive *pZip);
508
509
// Misc. high-level helper functions:
510
511
// mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive.
512
// 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.
513
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);
514
515
// Reads a single file from an archive into a heap block.
516
// Returns NULL on failure.
517
void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint zip_flags);
518
519
#endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
520
521
#endif // #ifndef MINIZ_NO_ARCHIVE_APIS
522
523
// ------------------- Low-level Decompression API Definitions
524
525
// Decompression flags used by tinfl_decompress().
526
// 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.
527
// 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.
528
// 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).
529
// TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes.
530
enum
531
{
532
TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
533
TINFL_FLAG_HAS_MORE_INPUT = 2,
534
TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
535
TINFL_FLAG_COMPUTE_ADLER32 = 8
536
};
537
538
// High level decompression functions:
539
// tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc().
540
// On entry:
541
// pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress.
542
// On return:
543
// Function returns a pointer to the decompressed data, or NULL on failure.
544
// *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data.
545
// The caller must call mz_free() on the returned block when it's no longer needed.
546
void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
547
548
// tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory.
549
// Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success.
550
#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
551
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);
552
553
// 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.
554
// Returns 1 on success or 0 on failure.
555
typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
556
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);
557
558
struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor;
559
560
// Max size of LZ dictionary.
561
#define TINFL_LZ_DICT_SIZE 32768
562
563
// Return status.
564
typedef enum
565
{
566
TINFL_STATUS_BAD_PARAM = -3,
567
TINFL_STATUS_ADLER32_MISMATCH = -2,
568
TINFL_STATUS_FAILED = -1,
569
TINFL_STATUS_DONE = 0,
570
TINFL_STATUS_NEEDS_MORE_INPUT = 1,
571
TINFL_STATUS_HAS_MORE_OUTPUT = 2
572
} tinfl_status;
573
574
// Initializes the decompressor to its initial state.
575
#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END
576
#define tinfl_get_adler32(r) (r)->m_check_adler32
577
578
// 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.
579
// 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.
580
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);
581
582
// Internal/private bits follow.
583
enum
584
{
585
TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19,
586
TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
587
};
588
589
typedef struct
590
{
591
mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
592
mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
593
} tinfl_huff_table;
594
595
#if MINIZ_HAS_64BIT_REGISTERS
596
#define TINFL_USE_64BIT_BITBUF 1
597
#endif
598
599
#if TINFL_USE_64BIT_BITBUF
600
typedef mz_uint64 tinfl_bit_buf_t;
601
#define TINFL_BITBUF_SIZE (64)
602
#else
603
typedef mz_uint32 tinfl_bit_buf_t;
604
#define TINFL_BITBUF_SIZE (32)
605
#endif
606
607
struct tinfl_decompressor_tag
608
{
609
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];
610
tinfl_bit_buf_t m_bit_buf;
611
size_t m_dist_from_out_buf_start;
612
tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
613
mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
614
};
615
616
// ------------------- Low-level Compression API Definitions
617
618
// Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently).
619
#define TDEFL_LESS_MEMORY 0
620
621
// tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search):
622
// 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).
623
enum
624
{
625
TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF
626
};
627
628
// 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.
629
// TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers).
630
// TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing.
631
// 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).
632
// TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1)
633
// TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled.
634
// TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables.
635
// TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks.
636
// The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK).
637
enum
638
{
639
TDEFL_WRITE_ZLIB_HEADER = 0x01000,
640
TDEFL_COMPUTE_ADLER32 = 0x02000,
641
TDEFL_GREEDY_PARSING_FLAG = 0x04000,
642
TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000,
643
TDEFL_RLE_MATCHES = 0x10000,
644
TDEFL_FILTER_MATCHES = 0x20000,
645
TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000,
646
TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
647
};
648
649
// High level compression functions:
650
// tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc().
651
// On entry:
652
// pSrc_buf, src_buf_len: Pointer and size of source block to compress.
653
// flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower but improve compression.
654
// On return:
655
// Function returns a pointer to the compressed data, or NULL on failure.
656
// *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data.
657
// The caller must free() the returned block when it's no longer needed.
658
void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
659
660
// tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory.
661
// Returns 0 on failure.
662
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);
663
664
// Compresses an image to a compressed PNG file in memory.
665
// On entry:
666
// pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4.
667
// The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in memory.
668
// 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
669
// If flip is true, the image will be flipped on the Y axis (useful for OpenGL apps).
670
// On return:
671
// Function returns a pointer to the compressed data, or NULL on failure.
672
// *pLen_out will be set to the size of the PNG image file.
673
// The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no longer needed.
674
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);
675
void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out);
676
677
// Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time.
678
typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
679
680
// tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally.
681
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);
682
683
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 };
684
685
// TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes).
686
#if TDEFL_LESS_MEMORY
687
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 };
688
#else
689
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 };
690
#endif
691
692
// 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.
693
typedef enum
694
{
695
TDEFL_STATUS_BAD_PARAM = -2,
696
TDEFL_STATUS_PUT_BUF_FAILED = -1,
697
TDEFL_STATUS_OKAY = 0,
698
TDEFL_STATUS_DONE = 1,
699
} tdefl_status;
700
701
// Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums
702
typedef enum
703
{
704
TDEFL_NO_FLUSH = 0,
705
TDEFL_SYNC_FLUSH = 2,
706
TDEFL_FULL_FLUSH = 3,
707
TDEFL_FINISH = 4
708
} tdefl_flush;
709
710
// tdefl's compression state structure.
711
typedef struct
712
{
713
tdefl_put_buf_func_ptr m_pPut_buf_func;
714
void *m_pPut_buf_user;
715
mz_uint m_flags, m_max_probes[2];
716
int m_greedy_parsing;
717
mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
718
mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
719
mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer;
720
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;
721
tdefl_status m_prev_return_status;
722
const void *m_pIn_buf;
723
void *m_pOut_buf;
724
size_t *m_pIn_buf_size, *m_pOut_buf_size;
725
tdefl_flush m_flush;
726
const mz_uint8 *m_pSrc;
727
size_t m_src_buf_left, m_out_buf_ofs;
728
mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
729
mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
730
mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
731
mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
732
mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
733
mz_uint16 m_next[TDEFL_LZ_DICT_SIZE];
734
mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
735
mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
736
} tdefl_compressor;
737
738
// Initializes the compressor.
739
// There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory.
740
// 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.
741
// If pBut_buf_func is NULL the user should always call the tdefl_compress() API.
742
// flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.)
743
tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
744
745
// 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.
746
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);
747
748
// tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr.
749
// tdefl_compress_buffer() always consumes the entire input buffer.
750
tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush);
751
752
tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d);
753
mz_uint32 tdefl_get_adler32(tdefl_compressor *d);
754
755
// 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.
756
#ifndef MINIZ_NO_ZLIB_APIS
757
// Create tdefl_compress() flags given zlib-style compression parameters.
758
// level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files)
759
// window_bits may be -15 (raw deflate) or 15 (zlib)
760
// strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED
761
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy);
762
#endif // #ifndef MINIZ_NO_ZLIB_APIS
763
764
#ifdef __cplusplus
765
}
766
#endif
767
768
#endif // MINIZ_HEADER_INCLUDED
769
770
#ifndef MINIZ_HEADER_INCLUDED
771
#define MINIZ_HEADER_INCLUDED
772
773
#include <stdlib.h>
774
775
// Defines to completely disable specific portions of miniz.c:
776
// If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl.
777
778
// Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O.
779
#define MINIZ_NO_STDIO
780
781
// If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able to get the current time, or
782
// get/set file times, and the C run-time funcs that get/set times won't be called.
783
// The current downside is the times written to your archives will be from 1979.
784
#define MINIZ_NO_TIME
785
786
// Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's.
787
#define MINIZ_NO_ARCHIVE_APIS
788
789
// Define MINIZ_NO_ARCHIVE_APIS to disable all writing related ZIP archive API's.
790
#define MINIZ_NO_ARCHIVE_WRITING_APIS
791
792
// Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's.
793
//#define MINIZ_NO_ZLIB_APIS
794
795
// Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib.
796
//#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
797
798
// Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc.
799
// Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc
800
// callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user
801
// functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work.
802
//#define MINIZ_NO_MALLOC
803
804
#if defined(__TINYC__) && (defined(__linux) || defined(__linux__))
805
// TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux
806
#define MINIZ_NO_TIME
807
#endif
808
809
#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS)
810
#include <time.h>
811
#endif
812
813
#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__)
814
// MINIZ_X86_OR_X64_CPU is only used to help set the below macros.
815
#define MINIZ_X86_OR_X64_CPU 1
816
#endif
817
818
#if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
819
// Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian.
820
#define MINIZ_LITTLE_ENDIAN 1
821
#endif
822
823
#if MINIZ_X86_OR_X64_CPU
824
// Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses.
825
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
826
#endif
827
828
#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
829
// 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).
830
#define MINIZ_HAS_64BIT_REGISTERS 1
831
#endif
832
833
#ifdef __cplusplus
834
extern "C" {
835
#endif
836
837
// ------------------- zlib-style API Definitions.
838
839
// 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!
840
typedef unsigned long mz_ulong;
841
842
// 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.
843
void mz_free(void *p);
844
845
#define MZ_ADLER32_INIT (1)
846
// mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL.
847
mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len);
848
849
#define MZ_CRC32_INIT (0)
850
// mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL.
851
mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, size_t buf_len);
852
853
// Compression strategies.
854
enum { MZ_DEFAULT_STRATEGY = 0, MZ_FILTERED = 1, MZ_HUFFMAN_ONLY = 2, MZ_RLE = 3, MZ_FIXED = 4 };
855
856
// Method
857
#define MZ_DEFLATED 8
858
859
#ifndef MINIZ_NO_ZLIB_APIS
860
861
// Heap allocation callbacks.
862
// Note that mz_alloc_func parameter types purpsosely differ from zlib's: items/size is size_t, not unsigned long.
863
typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size);
864
typedef void (*mz_free_func)(void *opaque, void *address);
865
typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size);
866
867
#define MZ_VERSION "9.1.15"
868
#define MZ_VERNUM 0x91F0
869
#define MZ_VER_MAJOR 9
870
#define MZ_VER_MINOR 1
871
#define MZ_VER_REVISION 15
872
#define MZ_VER_SUBREVISION 0
873
874
// 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).
875
enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 };
876
877
// Return status codes. MZ_PARAM_ERROR is non-standard.
878
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 };
879
880
// 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.
881
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 };
882
883
// Window bits
884
#define MZ_DEFAULT_WINDOW_BITS 15
885
886
struct mz_internal_state;
887
888
// Compression/decompression stream struct.
889
typedef struct mz_stream_s
890
{
891
const unsigned char *next_in; // pointer to next byte to read
892
unsigned int avail_in; // number of bytes available at next_in
893
mz_ulong total_in; // total number of bytes consumed so far
894
895
unsigned char *next_out; // pointer to next byte to write
896
unsigned int avail_out; // number of bytes that can be written to next_out
897
mz_ulong total_out; // total number of bytes produced so far
898
899
char *msg; // error msg (unused)
900
struct mz_internal_state *state; // internal state, allocated by zalloc/zfree
901
902
mz_alloc_func zalloc; // optional heap allocation function (defaults to malloc)
903
mz_free_func zfree; // optional heap free function (defaults to free)
904
void *opaque; // heap alloc function user pointer
905
906
int data_type; // data_type (unused)
907
mz_ulong adler; // adler32 of the source or uncompressed data
908
mz_ulong reserved; // not used
909
} mz_stream;
910
911
typedef mz_stream *mz_streamp;
912
913
// Returns the version string of miniz.c.
914
const char *mz_version(void);
915
916
// mz_deflateInit() initializes a compressor with default options:
917
// Parameters:
918
// pStream must point to an initialized mz_stream struct.
919
// level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION].
920
// level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio.
921
// (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.)
922
// Return values:
923
// MZ_OK on success.
924
// MZ_STREAM_ERROR if the stream is bogus.
925
// MZ_PARAM_ERROR if the input parameters are bogus.
926
// MZ_MEM_ERROR on out of memory.
927
int mz_deflateInit(mz_streamp pStream, int level);
928
929
// mz_deflateInit2() is like mz_deflate(), except with more control:
930
// Additional parameters:
931
// method must be MZ_DEFLATED
932
// 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)
933
// mem_level must be between [1, 9] (it's checked but ignored by miniz.c)
934
int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy);
935
936
// Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2().
937
int mz_deflateReset(mz_streamp pStream);
938
939
// mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible.
940
// Parameters:
941
// 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.
942
// flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH.
943
// Return values:
944
// 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).
945
// 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.
946
// MZ_STREAM_ERROR if the stream is bogus.
947
// MZ_PARAM_ERROR if one of the parameters is invalid.
948
// 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.)
949
int mz_deflate(mz_streamp pStream, int flush);
950
951
// mz_deflateEnd() deinitializes a compressor:
952
// Return values:
953
// MZ_OK on success.
954
// MZ_STREAM_ERROR if the stream is bogus.
955
int mz_deflateEnd(mz_streamp pStream);
956
957
// 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.
958
mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len);
959
960
// Single-call compression functions mz_compress() and mz_compress2():
961
// Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure.
962
int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
963
int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level);
964
965
// mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress().
966
mz_ulong mz_compressBound(mz_ulong source_len);
967
968
// Initializes a decompressor.
969
int mz_inflateInit(mz_streamp pStream);
970
971
// 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:
972
// window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate).
973
int mz_inflateInit2(mz_streamp pStream, int window_bits);
974
975
// 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.
976
// Parameters:
977
// 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.
978
// flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH.
979
// 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).
980
// 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.
981
// Return values:
982
// 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.
983
// 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.
984
// MZ_STREAM_ERROR if the stream is bogus.
985
// MZ_DATA_ERROR if the deflate stream is invalid.
986
// MZ_PARAM_ERROR if one of the parameters is invalid.
987
// 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
988
// with more input data, or with more room in the output buffer (except when using single call decompression, described above).
989
int mz_inflate(mz_streamp pStream, int flush);
990
991
// Deinitializes a decompressor.
992
int mz_inflateEnd(mz_streamp pStream);
993
994
// Single-call decompression.
995
// Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure.
996
int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
997
998
// Returns a string description of the specified error code, or NULL if the error code is invalid.
999
const char *mz_error(int err);
1000
1001
// 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.
1002
// Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project.
1003
#ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
1004
typedef unsigned char Byte;
1005
typedef unsigned int uInt;
1006
typedef mz_ulong uLong;
1007
typedef Byte Bytef;
1008
typedef uInt uIntf;
1009
typedef char charf;
1010
typedef int intf;
1011
typedef void *voidpf;
1012
typedef uLong uLongf;
1013
typedef void *voidp;
1014
typedef void *const voidpc;
1015
#define Z_NULL 0
1016
#define Z_NO_FLUSH MZ_NO_FLUSH
1017
#define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH
1018
#define Z_SYNC_FLUSH MZ_SYNC_FLUSH
1019
#define Z_FULL_FLUSH MZ_FULL_FLUSH
1020
#define Z_FINISH MZ_FINISH
1021
#define Z_BLOCK MZ_BLOCK
1022
#define Z_OK MZ_OK
1023
#define Z_STREAM_END MZ_STREAM_END
1024
#define Z_NEED_DICT MZ_NEED_DICT
1025
#define Z_ERRNO MZ_ERRNO
1026
#define Z_STREAM_ERROR MZ_STREAM_ERROR
1027
#define Z_DATA_ERROR MZ_DATA_ERROR
1028
#define Z_MEM_ERROR MZ_MEM_ERROR
1029
#define Z_BUF_ERROR MZ_BUF_ERROR
1030
#define Z_VERSION_ERROR MZ_VERSION_ERROR
1031
#define Z_PARAM_ERROR MZ_PARAM_ERROR
1032
#define Z_NO_COMPRESSION MZ_NO_COMPRESSION
1033
#define Z_BEST_SPEED MZ_BEST_SPEED
1034
#define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION
1035
#define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION
1036
#define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY
1037
#define Z_FILTERED MZ_FILTERED
1038
#define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY
1039
#define Z_RLE MZ_RLE
1040
#define Z_FIXED MZ_FIXED
1041
#define Z_DEFLATED MZ_DEFLATED
1042
#define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS
1043
#define alloc_func mz_alloc_func
1044
#define free_func mz_free_func
1045
#define internal_state mz_internal_state
1046
#define z_stream mz_stream
1047
#define deflateInit mz_deflateInit
1048
#define deflateInit2 mz_deflateInit2
1049
#define deflateReset mz_deflateReset
1050
#define deflate mz_deflate
1051
#define deflateEnd mz_deflateEnd
1052
#define deflateBound mz_deflateBound
1053
#define compress mz_compress
1054
#define compress2 mz_compress2
1055
#define compressBound mz_compressBound
1056
#define inflateInit mz_inflateInit
1057
#define inflateInit2 mz_inflateInit2
1058
#define inflate mz_inflate
1059
#define inflateEnd mz_inflateEnd
1060
#define uncompress mz_uncompress
1061
#define crc32 mz_crc32
1062
#define adler32 mz_adler32
1063
#define MAX_WBITS 15
1064
#define MAX_MEM_LEVEL 9
1065
#define zError mz_error
1066
#define ZLIB_VERSION MZ_VERSION
1067
#define ZLIB_VERNUM MZ_VERNUM
1068
#define ZLIB_VER_MAJOR MZ_VER_MAJOR
1069
#define ZLIB_VER_MINOR MZ_VER_MINOR
1070
#define ZLIB_VER_REVISION MZ_VER_REVISION
1071
#define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION
1072
#define zlibVersion mz_version
1073
#define zlib_version mz_version()
1074
#endif // #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
1075
1076
#endif // MINIZ_NO_ZLIB_APIS
1077
1078
// ------------------- Types and macros
1079
1080
typedef unsigned char mz_uint8;
1081
typedef signed short mz_int16;
1082
typedef unsigned short mz_uint16;
1083
typedef unsigned int mz_uint32;
1084
typedef unsigned int mz_uint;
1085
typedef long long mz_int64;
1086
typedef unsigned long long mz_uint64;
1087
typedef int mz_bool;
1088
1089
#define MZ_FALSE (0)
1090
#define MZ_TRUE (1)
1091
1092
// An attempt to work around MSVC's spammy "warning C4127: conditional expression is constant" message.
1093
#ifdef _MSC_VER
1094
#define MZ_MACRO_END while (0, 0)
1095
#else
1096
#define MZ_MACRO_END while (0)
1097
#endif
1098
1099
// ------------------- ZIP archive reading/writing
1100
1101
#ifndef MINIZ_NO_ARCHIVE_APIS
1102
1103
enum
1104
{
1105
MZ_ZIP_MAX_IO_BUF_SIZE = 64*1024,
1106
MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 260,
1107
MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 256
1108
};
1109
1110
typedef struct
1111
{
1112
mz_uint32 m_file_index;
1113
mz_uint32 m_central_dir_ofs;
1114
mz_uint16 m_version_made_by;
1115
mz_uint16 m_version_needed;
1116
mz_uint16 m_bit_flag;
1117
mz_uint16 m_method;
1118
#ifndef MINIZ_NO_TIME
1119
time_t m_time;
1120
#endif
1121
mz_uint32 m_crc32;
1122
mz_uint64 m_comp_size;
1123
mz_uint64 m_uncomp_size;
1124
mz_uint16 m_internal_attr;
1125
mz_uint32 m_external_attr;
1126
mz_uint64 m_local_header_ofs;
1127
mz_uint32 m_comment_size;
1128
char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
1129
char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
1130
} mz_zip_archive_file_stat;
1131
1132
typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n);
1133
typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n);
1134
1135
struct mz_zip_internal_state_tag;
1136
typedef struct mz_zip_internal_state_tag mz_zip_internal_state;
1137
1138
typedef enum
1139
{
1140
MZ_ZIP_MODE_INVALID = 0,
1141
MZ_ZIP_MODE_READING = 1,
1142
MZ_ZIP_MODE_WRITING = 2,
1143
MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3
1144
} mz_zip_mode;
1145
1146
typedef struct mz_zip_archive_tag
1147
{
1148
mz_uint64 m_archive_size;
1149
mz_uint64 m_central_directory_file_ofs;
1150
mz_uint m_total_files;
1151
mz_zip_mode m_zip_mode;
1152
1153
mz_uint m_file_offset_alignment;
1154
1155
mz_alloc_func m_pAlloc;
1156
mz_free_func m_pFree;
1157
mz_realloc_func m_pRealloc;
1158
void *m_pAlloc_opaque;
1159
1160
mz_file_read_func m_pRead;
1161
mz_file_write_func m_pWrite;
1162
void *m_pIO_opaque;
1163
1164
mz_zip_internal_state *m_pState;
1165
1166
} mz_zip_archive;
1167
1168
typedef enum
1169
{
1170
MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100,
1171
MZ_ZIP_FLAG_IGNORE_PATH = 0x0200,
1172
MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400,
1173
MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800
1174
} mz_zip_flags;
1175
1176
// ZIP archive reading
1177
1178
// Inits a ZIP archive reader.
1179
// These functions read and validate the archive's central directory.
1180
mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint32 flags);
1181
mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint32 flags);
1182
1183
#ifndef MINIZ_NO_STDIO
1184
mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags);
1185
#endif
1186
1187
// Returns the total number of files in the archive.
1188
mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip);
1189
1190
// Returns detailed information about an archive file entry.
1191
mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat);
1192
1193
// Determines if an archive file entry is a directory entry.
1194
mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index);
1195
mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index);
1196
1197
// Retrieves the filename of an archive file entry.
1198
// 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.
1199
mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size);
1200
1201
// Attempts to locates a file in the archive's central directory.
1202
// Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH
1203
// Returns -1 if the file cannot be found.
1204
int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags);
1205
1206
// Extracts a archive file to a memory buffer using no memory allocation.
1207
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);
1208
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);
1209
1210
// Extracts a archive file to a memory buffer.
1211
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);
1212
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);
1213
1214
// Extracts a archive file to a dynamically allocated heap buffer.
1215
void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags);
1216
void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags);
1217
1218
// Extracts a archive file using a callback function to output the file's data.
1219
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);
1220
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);
1221
1222
#ifndef MINIZ_NO_STDIO
1223
// Extracts a archive file to a disk file and sets its last accessed and modified times.
1224
// This function only extracts files, not archive directory records.
1225
mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags);
1226
mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags);
1227
#endif
1228
1229
// Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was used.
1230
mz_bool mz_zip_reader_end(mz_zip_archive *pZip);
1231
1232
// ZIP archive writing
1233
1234
#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
1235
1236
// Inits a ZIP archive writer.
1237
mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size);
1238
mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size);
1239
1240
#ifndef MINIZ_NO_STDIO
1241
mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning);
1242
#endif
1243
1244
// Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an existing archive.
1245
// 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.
1246
// 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).
1247
// Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be NULL.
1248
// Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops or something goes wrong before
1249
// the archive is finalized the file's central directory will be hosed.
1250
mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename);
1251
1252
// Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive.
1253
// To add a directory entry, call this method with an archive name ending in a forwardslash with empty buffer.
1254
// 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.
1255
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);
1256
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);
1257
1258
#ifndef MINIZ_NO_STDIO
1259
// Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive.
1260
// 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.
1261
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);
1262
#endif
1263
1264
// Adds a file to an archive by fully cloning the data from another archive.
1265
// This function fully clones the source file's compressed data (no recompression), along with its full filename, extra data, and comment fields.
1266
mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint file_index);
1267
1268
// Finalizes the archive by writing the central directory records followed by the end of central directory record.
1269
// After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end().
1270
// An archive must be manually finalized by calling this function for it to be valid.
1271
mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip);
1272
mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **pBuf, size_t *pSize);
1273
1274
// Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used.
1275
// Note for the archive to be valid, it must have been finalized before ending.
1276
mz_bool mz_zip_writer_end(mz_zip_archive *pZip);
1277
1278
// Misc. high-level helper functions:
1279
1280
// mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive.
1281
// 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.
1282
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);
1283
1284
// Reads a single file from an archive into a heap block.
1285
// Returns NULL on failure.
1286
void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint zip_flags);
1287
1288
#endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
1289
1290
#endif // #ifndef MINIZ_NO_ARCHIVE_APIS
1291
1292
// ------------------- Low-level Decompression API Definitions
1293
1294
// Decompression flags used by tinfl_decompress().
1295
// 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.
1296
// 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.
1297
// 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).
1298
// TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes.
1299
enum
1300
{
1301
TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
1302
TINFL_FLAG_HAS_MORE_INPUT = 2,
1303
TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
1304
TINFL_FLAG_COMPUTE_ADLER32 = 8
1305
};
1306
1307
// High level decompression functions:
1308
// tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc().
1309
// On entry:
1310
// pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress.
1311
// On return:
1312
// Function returns a pointer to the decompressed data, or NULL on failure.
1313
// *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data.
1314
// The caller must call mz_free() on the returned block when it's no longer needed.
1315
void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
1316
1317
// tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory.
1318
// Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success.
1319
#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
1320
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);
1321
1322
// 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.
1323
// Returns 1 on success or 0 on failure.
1324
typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
1325
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);
1326
1327
struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor;
1328
1329
// Max size of LZ dictionary.
1330
#define TINFL_LZ_DICT_SIZE 32768
1331
1332
// Return status.
1333
typedef enum
1334
{
1335
TINFL_STATUS_BAD_PARAM = -3,
1336
TINFL_STATUS_ADLER32_MISMATCH = -2,
1337
TINFL_STATUS_FAILED = -1,
1338
TINFL_STATUS_DONE = 0,
1339
TINFL_STATUS_NEEDS_MORE_INPUT = 1,
1340
TINFL_STATUS_HAS_MORE_OUTPUT = 2
1341
} tinfl_status;
1342
1343
// Initializes the decompressor to its initial state.
1344
#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END
1345
#define tinfl_get_adler32(r) (r)->m_check_adler32
1346
1347
// 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.
1348
// 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.
1349
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);
1350
1351
// Internal/private bits follow.
1352
enum
1353
{
1354
TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19,
1355
TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
1356
};
1357
1358
typedef struct
1359
{
1360
mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
1361
mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
1362
} tinfl_huff_table;
1363
1364
#if MINIZ_HAS_64BIT_REGISTERS
1365
#define TINFL_USE_64BIT_BITBUF 1
1366
#endif
1367
1368
#if TINFL_USE_64BIT_BITBUF
1369
typedef mz_uint64 tinfl_bit_buf_t;
1370
#define TINFL_BITBUF_SIZE (64)
1371
#else
1372
typedef mz_uint32 tinfl_bit_buf_t;
1373
#define TINFL_BITBUF_SIZE (32)
1374
#endif
1375
1376
struct tinfl_decompressor_tag
1377
{
1378
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];
1379
tinfl_bit_buf_t m_bit_buf;
1380
size_t m_dist_from_out_buf_start;
1381
tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
1382
mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
1383
};
1384
1385
// ------------------- Low-level Compression API Definitions
1386
1387
// Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently).
1388
#define TDEFL_LESS_MEMORY 0
1389
1390
// tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search):
1391
// 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).
1392
enum
1393
{
1394
TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF
1395
};
1396
1397
// 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.
1398
// TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers).
1399
// TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing.
1400
// 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).
1401
// TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1)
1402
// TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled.
1403
// TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables.
1404
// TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks.
1405
// The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK).
1406
enum
1407
{
1408
TDEFL_WRITE_ZLIB_HEADER = 0x01000,
1409
TDEFL_COMPUTE_ADLER32 = 0x02000,
1410
TDEFL_GREEDY_PARSING_FLAG = 0x04000,
1411
TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000,
1412
TDEFL_RLE_MATCHES = 0x10000,
1413
TDEFL_FILTER_MATCHES = 0x20000,
1414
TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000,
1415
TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
1416
};
1417
1418
// High level compression functions:
1419
// tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc().
1420
// On entry:
1421
// pSrc_buf, src_buf_len: Pointer and size of source block to compress.
1422
// flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower but improve compression.
1423
// On return:
1424
// Function returns a pointer to the compressed data, or NULL on failure.
1425
// *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data.
1426
// The caller must free() the returned block when it's no longer needed.
1427
void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
1428
1429
// tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory.
1430
// Returns 0 on failure.
1431
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);
1432
1433
// Compresses an image to a compressed PNG file in memory.
1434
// On entry:
1435
// pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4.
1436
// The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in memory.
1437
// 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
1438
// If flip is true, the image will be flipped on the Y axis (useful for OpenGL apps).
1439
// On return:
1440
// Function returns a pointer to the compressed data, or NULL on failure.
1441
// *pLen_out will be set to the size of the PNG image file.
1442
// The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no longer needed.
1443
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);
1444
void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out);
1445
1446
// Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time.
1447
typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
1448
1449
// tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally.
1450
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);
1451
1452
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 };
1453
1454
// TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes).
1455
#if TDEFL_LESS_MEMORY
1456
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 };
1457
#else
1458
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 };
1459
#endif
1460
1461
// 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.
1462
typedef enum
1463
{
1464
TDEFL_STATUS_BAD_PARAM = -2,
1465
TDEFL_STATUS_PUT_BUF_FAILED = -1,
1466
TDEFL_STATUS_OKAY = 0,
1467
TDEFL_STATUS_DONE = 1,
1468
} tdefl_status;
1469
1470
// Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums
1471
typedef enum
1472
{
1473
TDEFL_NO_FLUSH = 0,
1474
TDEFL_SYNC_FLUSH = 2,
1475
TDEFL_FULL_FLUSH = 3,
1476
TDEFL_FINISH = 4
1477
} tdefl_flush;
1478
1479
// tdefl's compression state structure.
1480
typedef struct
1481
{
1482
tdefl_put_buf_func_ptr m_pPut_buf_func;
1483
void *m_pPut_buf_user;
1484
mz_uint m_flags, m_max_probes[2];
1485
int m_greedy_parsing;
1486
mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
1487
mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
1488
mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer;
1489
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;
1490
tdefl_status m_prev_return_status;
1491
const void *m_pIn_buf;
1492
void *m_pOut_buf;
1493
size_t *m_pIn_buf_size, *m_pOut_buf_size;
1494
tdefl_flush m_flush;
1495
const mz_uint8 *m_pSrc;
1496
size_t m_src_buf_left, m_out_buf_ofs;
1497
mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
1498
mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
1499
mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
1500
mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
1501
mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
1502
mz_uint16 m_next[TDEFL_LZ_DICT_SIZE];
1503
mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
1504
mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
1505
} tdefl_compressor;
1506
1507
// Initializes the compressor.
1508
// There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory.
1509
// 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.
1510
// If pBut_buf_func is NULL the user should always call the tdefl_compress() API.
1511
// flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.)
1512
tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
1513
1514
// 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.
1515
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);
1516
1517
// tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr.
1518
// tdefl_compress_buffer() always consumes the entire input buffer.
1519
tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush);
1520
1521
tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d);
1522
mz_uint32 tdefl_get_adler32(tdefl_compressor *d);
1523
1524
// 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.
1525
#ifndef MINIZ_NO_ZLIB_APIS
1526
// Create tdefl_compress() flags given zlib-style compression parameters.
1527
// level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files)
1528
// window_bits may be -15 (raw deflate) or 15 (zlib)
1529
// strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED
1530
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy);
1531
#endif // #ifndef MINIZ_NO_ZLIB_APIS
1532
1533
#ifdef __cplusplus
1534
}
1535
#endif
1536
1537
#endif // MINIZ_HEADER_INCLUDED
1538
1539
#ifndef MINIZ_HEADER_INCLUDED
1540
#define MINIZ_HEADER_INCLUDED
1541
1542
#include <stdlib.h>
1543
1544
// Defines to completely disable specific portions of miniz.c:
1545
// If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl.
1546
1547
// Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O.
1548
#define MINIZ_NO_STDIO
1549
1550
// If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able to get the current time, or
1551
// get/set file times, and the C run-time funcs that get/set times won't be called.
1552
// The current downside is the times written to your archives will be from 1979.
1553
#define MINIZ_NO_TIME
1554
1555
// Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's.
1556
#define MINIZ_NO_ARCHIVE_APIS
1557
1558
// Define MINIZ_NO_ARCHIVE_APIS to disable all writing related ZIP archive API's.
1559
#define MINIZ_NO_ARCHIVE_WRITING_APIS
1560
1561
// Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's.
1562
//#define MINIZ_NO_ZLIB_APIS
1563
1564
// Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib.
1565
//#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
1566
1567
// Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc.
1568
// Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc
1569
// callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user
1570
// functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work.
1571
//#define MINIZ_NO_MALLOC
1572
1573
#if defined(__TINYC__) && (defined(__linux) || defined(__linux__))
1574
// TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux
1575
#define MINIZ_NO_TIME
1576
#endif
1577
1578
#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS)
1579
#include <time.h>
1580
#endif
1581
1582
#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__)
1583
// MINIZ_X86_OR_X64_CPU is only used to help set the below macros.
1584
#define MINIZ_X86_OR_X64_CPU 1
1585
#endif
1586
1587
#if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
1588
// Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian.
1589
#define MINIZ_LITTLE_ENDIAN 1
1590
#endif
1591
1592
#if MINIZ_X86_OR_X64_CPU
1593
// Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses.
1594
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
1595
#endif
1596
1597
#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
1598
// 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).
1599
#define MINIZ_HAS_64BIT_REGISTERS 1
1600
#endif
1601
1602
#ifdef __cplusplus
1603
extern "C" {
1604
#endif
1605
1606
// ------------------- zlib-style API Definitions.
1607
1608
// 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!
1609
typedef unsigned long mz_ulong;
1610
1611
// 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.
1612
void mz_free(void *p);
1613
1614
#define MZ_ADLER32_INIT (1)
1615
// mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL.
1616
mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len);
1617
1618
#define MZ_CRC32_INIT (0)
1619
// mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL.
1620
mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, size_t buf_len);
1621
1622
// Compression strategies.
1623
enum { MZ_DEFAULT_STRATEGY = 0, MZ_FILTERED = 1, MZ_HUFFMAN_ONLY = 2, MZ_RLE = 3, MZ_FIXED = 4 };
1624
1625
// Method
1626
#define MZ_DEFLATED 8
1627
1628
#ifndef MINIZ_NO_ZLIB_APIS
1629
1630
// Heap allocation callbacks.
1631
// Note that mz_alloc_func parameter types purpsosely differ from zlib's: items/size is size_t, not unsigned long.
1632
typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size);
1633
typedef void (*mz_free_func)(void *opaque, void *address);
1634
typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size);
1635
1636
#define MZ_VERSION "9.1.15"
1637
#define MZ_VERNUM 0x91F0
1638
#define MZ_VER_MAJOR 9
1639
#define MZ_VER_MINOR 1
1640
#define MZ_VER_REVISION 15
1641
#define MZ_VER_SUBREVISION 0
1642
1643
// 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).
1644
enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 };
1645
1646
// Return status codes. MZ_PARAM_ERROR is non-standard.
1647
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 };
1648
1649
// 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.
1650
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 };
1651
1652
// Window bits
1653
#define MZ_DEFAULT_WINDOW_BITS 15
1654
1655
struct mz_internal_state;
1656
1657
// Compression/decompression stream struct.
1658
typedef struct mz_stream_s
1659
{
1660
const unsigned char *next_in; // pointer to next byte to read
1661
unsigned int avail_in; // number of bytes available at next_in
1662
mz_ulong total_in; // total number of bytes consumed so far
1663
1664
unsigned char *next_out; // pointer to next byte to write
1665
unsigned int avail_out; // number of bytes that can be written to next_out
1666
mz_ulong total_out; // total number of bytes produced so far
1667
1668
char *msg; // error msg (unused)
1669
struct mz_internal_state *state; // internal state, allocated by zalloc/zfree
1670
1671
mz_alloc_func zalloc; // optional heap allocation function (defaults to malloc)
1672
mz_free_func zfree; // optional heap free function (defaults to free)
1673
void *opaque; // heap alloc function user pointer
1674
1675
int data_type; // data_type (unused)
1676
mz_ulong adler; // adler32 of the source or uncompressed data
1677
mz_ulong reserved; // not used
1678
} mz_stream;
1679
1680
typedef mz_stream *mz_streamp;
1681
1682
// Returns the version string of miniz.c.
1683
const char *mz_version(void);
1684
1685
// mz_deflateInit() initializes a compressor with default options:
1686
// Parameters:
1687
// pStream must point to an initialized mz_stream struct.
1688
// level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION].
1689
// level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio.
1690
// (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.)
1691
// Return values:
1692
// MZ_OK on success.
1693
// MZ_STREAM_ERROR if the stream is bogus.
1694
// MZ_PARAM_ERROR if the input parameters are bogus.
1695
// MZ_MEM_ERROR on out of memory.
1696
int mz_deflateInit(mz_streamp pStream, int level);
1697
1698
// mz_deflateInit2() is like mz_deflate(), except with more control:
1699
// Additional parameters:
1700
// method must be MZ_DEFLATED
1701
// 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)
1702
// mem_level must be between [1, 9] (it's checked but ignored by miniz.c)
1703
int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy);
1704
1705
// Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2().
1706
int mz_deflateReset(mz_streamp pStream);
1707
1708
// mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible.
1709
// Parameters:
1710
// 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.
1711
// flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH.
1712
// Return values:
1713
// 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).
1714
// 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.
1715
// MZ_STREAM_ERROR if the stream is bogus.
1716
// MZ_PARAM_ERROR if one of the parameters is invalid.
1717
// 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.)
1718
int mz_deflate(mz_streamp pStream, int flush);
1719
1720
// mz_deflateEnd() deinitializes a compressor:
1721
// Return values:
1722
// MZ_OK on success.
1723
// MZ_STREAM_ERROR if the stream is bogus.
1724
int mz_deflateEnd(mz_streamp pStream);
1725
1726
// 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.
1727
mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len);
1728
1729
// Single-call compression functions mz_compress() and mz_compress2():
1730
// Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure.
1731
int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
1732
int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level);
1733
1734
// mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress().
1735
mz_ulong mz_compressBound(mz_ulong source_len);
1736
1737
// Initializes a decompressor.
1738
int mz_inflateInit(mz_streamp pStream);
1739
1740
// 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:
1741
// window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate).
1742
int mz_inflateInit2(mz_streamp pStream, int window_bits);
1743
1744
// 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.
1745
// Parameters:
1746
// 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.
1747
// flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH.
1748
// 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).
1749
// 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.
1750
// Return values:
1751
// 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.
1752
// 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.
1753
// MZ_STREAM_ERROR if the stream is bogus.
1754
// MZ_DATA_ERROR if the deflate stream is invalid.
1755
// MZ_PARAM_ERROR if one of the parameters is invalid.
1756
// 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
1757
// with more input data, or with more room in the output buffer (except when using single call decompression, described above).
1758
int mz_inflate(mz_streamp pStream, int flush);
1759
1760
// Deinitializes a decompressor.
1761
int mz_inflateEnd(mz_streamp pStream);
1762
1763
// Single-call decompression.
1764
// Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure.
1765
int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
1766
1767
// Returns a string description of the specified error code, or NULL if the error code is invalid.
1768
const char *mz_error(int err);
1769
1770
// 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.
1771
// Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project.
1772
#ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
1773
typedef unsigned char Byte;
1774
typedef unsigned int uInt;
1775
typedef mz_ulong uLong;
1776
typedef Byte Bytef;
1777
typedef uInt uIntf;
1778
typedef char charf;
1779
typedef int intf;
1780
typedef void *voidpf;
1781
typedef uLong uLongf;
1782
typedef void *voidp;
1783
typedef void *const voidpc;
1784
#define Z_NULL 0
1785
#define Z_NO_FLUSH MZ_NO_FLUSH
1786
#define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH
1787
#define Z_SYNC_FLUSH MZ_SYNC_FLUSH
1788
#define Z_FULL_FLUSH MZ_FULL_FLUSH
1789
#define Z_FINISH MZ_FINISH
1790
#define Z_BLOCK MZ_BLOCK
1791
#define Z_OK MZ_OK
1792
#define Z_STREAM_END MZ_STREAM_END
1793
#define Z_NEED_DICT MZ_NEED_DICT
1794
#define Z_ERRNO MZ_ERRNO
1795
#define Z_STREAM_ERROR MZ_STREAM_ERROR
1796
#define Z_DATA_ERROR MZ_DATA_ERROR
1797
#define Z_MEM_ERROR MZ_MEM_ERROR
1798
#define Z_BUF_ERROR MZ_BUF_ERROR
1799
#define Z_VERSION_ERROR MZ_VERSION_ERROR
1800
#define Z_PARAM_ERROR MZ_PARAM_ERROR
1801
#define Z_NO_COMPRESSION MZ_NO_COMPRESSION
1802
#define Z_BEST_SPEED MZ_BEST_SPEED
1803
#define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION
1804
#define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION
1805
#define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY
1806
#define Z_FILTERED MZ_FILTERED
1807
#define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY
1808
#define Z_RLE MZ_RLE
1809
#define Z_FIXED MZ_FIXED
1810
#define Z_DEFLATED MZ_DEFLATED
1811
#define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS
1812
#define alloc_func mz_alloc_func
1813
#define free_func mz_free_func
1814
#define internal_state mz_internal_state
1815
#define z_stream mz_stream
1816
#define deflateInit mz_deflateInit
1817
#define deflateInit2 mz_deflateInit2
1818
#define deflateReset mz_deflateReset
1819
#define deflate mz_deflate
1820
#define deflateEnd mz_deflateEnd
1821
#define deflateBound mz_deflateBound
1822
#define compress mz_compress
1823
#define compress2 mz_compress2
1824
#define compressBound mz_compressBound
1825
#define inflateInit mz_inflateInit
1826
#define inflateInit2 mz_inflateInit2
1827
#define inflate mz_inflate
1828
#define inflateEnd mz_inflateEnd
1829
#define uncompress mz_uncompress
1830
#define crc32 mz_crc32
1831
#define adler32 mz_adler32
1832
#define MAX_WBITS 15
1833
#define MAX_MEM_LEVEL 9
1834
#define zError mz_error
1835
#define ZLIB_VERSION MZ_VERSION
1836
#define ZLIB_VERNUM MZ_VERNUM
1837
#define ZLIB_VER_MAJOR MZ_VER_MAJOR
1838
#define ZLIB_VER_MINOR MZ_VER_MINOR
1839
#define ZLIB_VER_REVISION MZ_VER_REVISION
1840
#define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION
1841
#define zlibVersion mz_version
1842
#define zlib_version mz_version()
1843
#endif // #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
1844
1845
#endif // MINIZ_NO_ZLIB_APIS
1846
1847
// ------------------- Types and macros
1848
1849
typedef unsigned char mz_uint8;
1850
typedef signed short mz_int16;
1851
typedef unsigned short mz_uint16;
1852
typedef unsigned int mz_uint32;
1853
typedef unsigned int mz_uint;
1854
typedef long long mz_int64;
1855
typedef unsigned long long mz_uint64;
1856
typedef int mz_bool;
1857
1858
#define MZ_FALSE (0)
1859
#define MZ_TRUE (1)
1860
1861
// An attempt to work around MSVC's spammy "warning C4127: conditional expression is constant" message.
1862
#ifdef _MSC_VER
1863
#define MZ_MACRO_END while (0, 0)
1864
#else
1865
#define MZ_MACRO_END while (0)
1866
#endif
1867
1868
// ------------------- ZIP archive reading/writing
1869
1870
#ifndef MINIZ_NO_ARCHIVE_APIS
1871
1872
enum
1873
{
1874
MZ_ZIP_MAX_IO_BUF_SIZE = 64*1024,
1875
MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 260,
1876
MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 256
1877
};
1878
1879
typedef struct
1880
{
1881
mz_uint32 m_file_index;
1882
mz_uint32 m_central_dir_ofs;
1883
mz_uint16 m_version_made_by;
1884
mz_uint16 m_version_needed;
1885
mz_uint16 m_bit_flag;
1886
mz_uint16 m_method;
1887
#ifndef MINIZ_NO_TIME
1888
time_t m_time;
1889
#endif
1890
mz_uint32 m_crc32;
1891
mz_uint64 m_comp_size;
1892
mz_uint64 m_uncomp_size;
1893
mz_uint16 m_internal_attr;
1894
mz_uint32 m_external_attr;
1895
mz_uint64 m_local_header_ofs;
1896
mz_uint32 m_comment_size;
1897
char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
1898
char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
1899
} mz_zip_archive_file_stat;
1900
1901
typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n);
1902
typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n);
1903
1904
struct mz_zip_internal_state_tag;
1905
typedef struct mz_zip_internal_state_tag mz_zip_internal_state;
1906
1907
typedef enum
1908
{
1909
MZ_ZIP_MODE_INVALID = 0,
1910
MZ_ZIP_MODE_READING = 1,
1911
MZ_ZIP_MODE_WRITING = 2,
1912
MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3
1913
} mz_zip_mode;
1914
1915
typedef struct mz_zip_archive_tag
1916
{
1917
mz_uint64 m_archive_size;
1918
mz_uint64 m_central_directory_file_ofs;
1919
mz_uint m_total_files;
1920
mz_zip_mode m_zip_mode;
1921
1922
mz_uint m_file_offset_alignment;
1923
1924
mz_alloc_func m_pAlloc;
1925
mz_free_func m_pFree;
1926
mz_realloc_func m_pRealloc;
1927
void *m_pAlloc_opaque;
1928
1929
mz_file_read_func m_pRead;
1930
mz_file_write_func m_pWrite;
1931
void *m_pIO_opaque;
1932
1933
mz_zip_internal_state *m_pState;
1934
1935
} mz_zip_archive;
1936
1937
typedef enum
1938
{
1939
MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100,
1940
MZ_ZIP_FLAG_IGNORE_PATH = 0x0200,
1941
MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400,
1942
MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800
1943
} mz_zip_flags;
1944
1945
// ZIP archive reading
1946
1947
// Inits a ZIP archive reader.
1948
// These functions read and validate the archive's central directory.
1949
mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint32 flags);
1950
mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint32 flags);
1951
1952
#ifndef MINIZ_NO_STDIO
1953
mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags);
1954
#endif
1955
1956
// Returns the total number of files in the archive.
1957
mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip);
1958
1959
// Returns detailed information about an archive file entry.
1960
mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat);
1961
1962
// Determines if an archive file entry is a directory entry.
1963
mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index);
1964
mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index);
1965
1966
// Retrieves the filename of an archive file entry.
1967
// 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.
1968
mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size);
1969
1970
// Attempts to locates a file in the archive's central directory.
1971
// Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH
1972
// Returns -1 if the file cannot be found.
1973
int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags);
1974
1975
// Extracts a archive file to a memory buffer using no memory allocation.
1976
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);
1977
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);
1978
1979
// Extracts a archive file to a memory buffer.
1980
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);
1981
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);
1982
1983
// Extracts a archive file to a dynamically allocated heap buffer.
1984
void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags);
1985
void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags);
1986
1987
// Extracts a archive file using a callback function to output the file's data.
1988
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);
1989
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);
1990
1991
#ifndef MINIZ_NO_STDIO
1992
// Extracts a archive file to a disk file and sets its last accessed and modified times.
1993
// This function only extracts files, not archive directory records.
1994
mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags);
1995
mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags);
1996
#endif
1997
1998
// Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was used.
1999
mz_bool mz_zip_reader_end(mz_zip_archive *pZip);
2000
2001
// ZIP archive writing
2002
2003
#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
2004
2005
// Inits a ZIP archive writer.
2006
mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size);
2007
mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size);
2008
2009
#ifndef MINIZ_NO_STDIO
2010
mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning);
2011
#endif
2012
2013
// Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an existing archive.
2014
// 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.
2015
// 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).
2016
// Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be NULL.
2017
// Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops or something goes wrong before
2018
// the archive is finalized the file's central directory will be hosed.
2019
mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename);
2020
2021
// Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive.
2022
// To add a directory entry, call this method with an archive name ending in a forwardslash with empty buffer.
2023
// 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.
2024
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);
2025
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);
2026
2027
#ifndef MINIZ_NO_STDIO
2028
// Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive.
2029
// 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.
2030
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);
2031
#endif
2032
2033
// Adds a file to an archive by fully cloning the data from another archive.
2034
// This function fully clones the source file's compressed data (no recompression), along with its full filename, extra data, and comment fields.
2035
mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint file_index);
2036
2037
// Finalizes the archive by writing the central directory records followed by the end of central directory record.
2038
// After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end().
2039
// An archive must be manually finalized by calling this function for it to be valid.
2040
mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip);
2041
mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **pBuf, size_t *pSize);
2042
2043
// Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used.
2044
// Note for the archive to be valid, it must have been finalized before ending.
2045
mz_bool mz_zip_writer_end(mz_zip_archive *pZip);
2046
2047
// Misc. high-level helper functions:
2048
2049
// mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive.
2050
// 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.
2051
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);
2052
2053
// Reads a single file from an archive into a heap block.
2054
// Returns NULL on failure.
2055
void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint zip_flags);
2056
2057
#endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
2058
2059
#endif // #ifndef MINIZ_NO_ARCHIVE_APIS
2060
2061
// ------------------- Low-level Decompression API Definitions
2062
2063
// Decompression flags used by tinfl_decompress().
2064
// 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.
2065
// 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.
2066
// 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).
2067
// TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes.
2068
enum
2069
{
2070
TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
2071
TINFL_FLAG_HAS_MORE_INPUT = 2,
2072
TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
2073
TINFL_FLAG_COMPUTE_ADLER32 = 8
2074
};
2075
2076
// High level decompression functions:
2077
// tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc().
2078
// On entry:
2079
// pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress.
2080
// On return:
2081
// Function returns a pointer to the decompressed data, or NULL on failure.
2082
// *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data.
2083
// The caller must call mz_free() on the returned block when it's no longer needed.
2084
void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
2085
2086
// tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory.
2087
// Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success.
2088
#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
2089
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);
2090
2091
// 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.
2092
// Returns 1 on success or 0 on failure.
2093
typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
2094
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);
2095
2096
struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor;
2097
2098
// Max size of LZ dictionary.
2099
#define TINFL_LZ_DICT_SIZE 32768
2100
2101
// Return status.
2102
typedef enum
2103
{
2104
TINFL_STATUS_BAD_PARAM = -3,
2105
TINFL_STATUS_ADLER32_MISMATCH = -2,
2106
TINFL_STATUS_FAILED = -1,
2107
TINFL_STATUS_DONE = 0,
2108
TINFL_STATUS_NEEDS_MORE_INPUT = 1,
2109
TINFL_STATUS_HAS_MORE_OUTPUT = 2
2110
} tinfl_status;
2111
2112
// Initializes the decompressor to its initial state.
2113
#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END
2114
#define tinfl_get_adler32(r) (r)->m_check_adler32
2115
2116
// 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.
2117
// 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.
2118
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);
2119
2120
// Internal/private bits follow.
2121
enum
2122
{
2123
TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19,
2124
TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
2125
};
2126
2127
typedef struct
2128
{
2129
mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
2130
mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
2131
} tinfl_huff_table;
2132
2133
#if MINIZ_HAS_64BIT_REGISTERS
2134
#define TINFL_USE_64BIT_BITBUF 1
2135
#endif
2136
2137
#if TINFL_USE_64BIT_BITBUF
2138
typedef mz_uint64 tinfl_bit_buf_t;
2139
#define TINFL_BITBUF_SIZE (64)
2140
#else
2141
typedef mz_uint32 tinfl_bit_buf_t;
2142
#define TINFL_BITBUF_SIZE (32)
2143
#endif
2144
2145
struct tinfl_decompressor_tag
2146
{
2147
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];
2148
tinfl_bit_buf_t m_bit_buf;
2149
size_t m_dist_from_out_buf_start;
2150
tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
2151
mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
2152
};
2153
2154
// ------------------- Low-level Compression API Definitions
2155
2156
// Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently).
2157
#define TDEFL_LESS_MEMORY 0
2158
2159
// tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search):
2160
// 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).
2161
enum
2162
{
2163
TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF
2164
};
2165
2166
// 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.
2167
// TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers).
2168
// TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing.
2169
// 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).
2170
// TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1)
2171
// TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled.
2172
// TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables.
2173
// TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks.
2174
// The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK).
2175
enum
2176
{
2177
TDEFL_WRITE_ZLIB_HEADER = 0x01000,
2178
TDEFL_COMPUTE_ADLER32 = 0x02000,
2179
TDEFL_GREEDY_PARSING_FLAG = 0x04000,
2180
TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000,
2181
TDEFL_RLE_MATCHES = 0x10000,
2182
TDEFL_FILTER_MATCHES = 0x20000,
2183
TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000,
2184
TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
2185
};
2186
2187
// High level compression functions:
2188
// tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc().
2189
// On entry:
2190
// pSrc_buf, src_buf_len: Pointer and size of source block to compress.
2191
// flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower but improve compression.
2192
// On return:
2193
// Function returns a pointer to the compressed data, or NULL on failure.
2194
// *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data.
2195
// The caller must free() the returned block when it's no longer needed.
2196
void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
2197
2198
// tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory.
2199
// Returns 0 on failure.
2200
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);
2201
2202
// Compresses an image to a compressed PNG file in memory.
2203
// On entry:
2204
// pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4.
2205
// The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in memory.
2206
// 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
2207
// If flip is true, the image will be flipped on the Y axis (useful for OpenGL apps).
2208
// On return:
2209
// Function returns a pointer to the compressed data, or NULL on failure.
2210
// *pLen_out will be set to the size of the PNG image file.
2211
// The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no longer needed.
2212
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);
2213
void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out);
2214
2215
// Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time.
2216
typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
2217
2218
// tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally.
2219
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);
2220
2221
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 };
2222
2223
// TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes).
2224
#if TDEFL_LESS_MEMORY
2225
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 };
2226
#else
2227
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 };
2228
#endif
2229
2230
// 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.
2231
typedef enum
2232
{
2233
TDEFL_STATUS_BAD_PARAM = -2,
2234
TDEFL_STATUS_PUT_BUF_FAILED = -1,
2235
TDEFL_STATUS_OKAY = 0,
2236
TDEFL_STATUS_DONE = 1,
2237
} tdefl_status;
2238
2239
// Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums
2240
typedef enum
2241
{
2242
TDEFL_NO_FLUSH = 0,
2243
TDEFL_SYNC_FLUSH = 2,
2244
TDEFL_FULL_FLUSH = 3,
2245
TDEFL_FINISH = 4
2246
} tdefl_flush;
2247
2248
// tdefl's compression state structure.
2249
typedef struct
2250
{
2251
tdefl_put_buf_func_ptr m_pPut_buf_func;
2252
void *m_pPut_buf_user;
2253
mz_uint m_flags, m_max_probes[2];
2254
int m_greedy_parsing;
2255
mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
2256
mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
2257
mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer;
2258
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;
2259
tdefl_status m_prev_return_status;
2260
const void *m_pIn_buf;
2261
void *m_pOut_buf;
2262
size_t *m_pIn_buf_size, *m_pOut_buf_size;
2263
tdefl_flush m_flush;
2264
const mz_uint8 *m_pSrc;
2265
size_t m_src_buf_left, m_out_buf_ofs;
2266
mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
2267
mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
2268
mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
2269
mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
2270
mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
2271
mz_uint16 m_next[TDEFL_LZ_DICT_SIZE];
2272
mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
2273
mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
2274
} tdefl_compressor;
2275
2276
// Initializes the compressor.
2277
// There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory.
2278
// 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.
2279
// If pBut_buf_func is NULL the user should always call the tdefl_compress() API.
2280
// flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.)
2281
tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
2282
2283
// 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.
2284
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);
2285
2286
// tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr.
2287
// tdefl_compress_buffer() always consumes the entire input buffer.
2288
tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush);
2289
2290
tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d);
2291
mz_uint32 tdefl_get_adler32(tdefl_compressor *d);
2292
2293
// 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.
2294
#ifndef MINIZ_NO_ZLIB_APIS
2295
// Create tdefl_compress() flags given zlib-style compression parameters.
2296
// level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files)
2297
// window_bits may be -15 (raw deflate) or 15 (zlib)
2298
// strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED
2299
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy);
2300
#endif // #ifndef MINIZ_NO_ZLIB_APIS
2301
2302
#ifdef __cplusplus
2303
}
2304
#endif
2305
2306
#endif // MINIZ_HEADER_INCLUDED
2307
2308
#ifndef MINIZ_HEADER_INCLUDED
2309
#define MINIZ_HEADER_INCLUDED
2310
2311
#include <stdlib.h>
2312
2313
// Defines to completely disable specific portions of miniz.c:
2314
// If all macros here are defined the only functionality remaining will be CRC-32, adler-32, tinfl, and tdefl.
2315
2316
// Define MINIZ_NO_STDIO to disable all usage and any functions which rely on stdio for file I/O.
2317
#define MINIZ_NO_STDIO
2318
2319
// If MINIZ_NO_TIME is specified then the ZIP archive functions will not be able to get the current time, or
2320
// get/set file times, and the C run-time funcs that get/set times won't be called.
2321
// The current downside is the times written to your archives will be from 1979.
2322
#define MINIZ_NO_TIME
2323
2324
// Define MINIZ_NO_ARCHIVE_APIS to disable all ZIP archive API's.
2325
#define MINIZ_NO_ARCHIVE_APIS
2326
2327
// Define MINIZ_NO_ARCHIVE_APIS to disable all writing related ZIP archive API's.
2328
#define MINIZ_NO_ARCHIVE_WRITING_APIS
2329
2330
// Define MINIZ_NO_ZLIB_APIS to remove all ZLIB-style compression/decompression API's.
2331
//#define MINIZ_NO_ZLIB_APIS
2332
2333
// Define MINIZ_NO_ZLIB_COMPATIBLE_NAME to disable zlib names, to prevent conflicts against stock zlib.
2334
//#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
2335
2336
// Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc.
2337
// Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc
2338
// callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user
2339
// functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work.
2340
//#define MINIZ_NO_MALLOC
2341
2342
#if defined(__TINYC__) && (defined(__linux) || defined(__linux__))
2343
// TODO: Work around "error: include file 'sys\utime.h' when compiling with tcc on Linux
2344
#define MINIZ_NO_TIME
2345
#endif
2346
2347
#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS)
2348
#include <time.h>
2349
#endif
2350
2351
#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__)
2352
// MINIZ_X86_OR_X64_CPU is only used to help set the below macros.
2353
#define MINIZ_X86_OR_X64_CPU 1
2354
#endif
2355
2356
#if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
2357
// Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian.
2358
#define MINIZ_LITTLE_ENDIAN 1
2359
#endif
2360
2361
#if MINIZ_X86_OR_X64_CPU
2362
// Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 on CPU's that permit efficient integer loads and stores from unaligned addresses.
2363
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
2364
#endif
2365
2366
#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
2367
// 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).
2368
#define MINIZ_HAS_64BIT_REGISTERS 1
2369
#endif
2370
2371
#ifdef __cplusplus
2372
extern "C" {
2373
#endif
2374
2375
// ------------------- zlib-style API Definitions.
2376
2377
// 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!
2378
typedef unsigned long mz_ulong;
2379
2380
// 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.
2381
void mz_free(void *p);
2382
2383
#define MZ_ADLER32_INIT (1)
2384
// mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL.
2385
mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len);
2386
2387
#define MZ_CRC32_INIT (0)
2388
// mz_crc32() returns the initial CRC-32 value to use when called with ptr==NULL.
2389
mz_ulong mz_crc32(mz_ulong crc, const unsigned char *ptr, size_t buf_len);
2390
2391
// Compression strategies.
2392
enum { MZ_DEFAULT_STRATEGY = 0, MZ_FILTERED = 1, MZ_HUFFMAN_ONLY = 2, MZ_RLE = 3, MZ_FIXED = 4 };
2393
2394
// Method
2395
#define MZ_DEFLATED 8
2396
2397
#ifndef MINIZ_NO_ZLIB_APIS
2398
2399
// Heap allocation callbacks.
2400
// Note that mz_alloc_func parameter types purpsosely differ from zlib's: items/size is size_t, not unsigned long.
2401
typedef void *(*mz_alloc_func)(void *opaque, size_t items, size_t size);
2402
typedef void (*mz_free_func)(void *opaque, void *address);
2403
typedef void *(*mz_realloc_func)(void *opaque, void *address, size_t items, size_t size);
2404
2405
#define MZ_VERSION "9.1.15"
2406
#define MZ_VERNUM 0x91F0
2407
#define MZ_VER_MAJOR 9
2408
#define MZ_VER_MINOR 1
2409
#define MZ_VER_REVISION 15
2410
#define MZ_VER_SUBREVISION 0
2411
2412
// 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).
2413
enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 };
2414
2415
// Return status codes. MZ_PARAM_ERROR is non-standard.
2416
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 };
2417
2418
// 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.
2419
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 };
2420
2421
// Window bits
2422
#define MZ_DEFAULT_WINDOW_BITS 15
2423
2424
struct mz_internal_state;
2425
2426
// Compression/decompression stream struct.
2427
typedef struct mz_stream_s
2428
{
2429
const unsigned char *next_in; // pointer to next byte to read
2430
unsigned int avail_in; // number of bytes available at next_in
2431
mz_ulong total_in; // total number of bytes consumed so far
2432
2433
unsigned char *next_out; // pointer to next byte to write
2434
unsigned int avail_out; // number of bytes that can be written to next_out
2435
mz_ulong total_out; // total number of bytes produced so far
2436
2437
char *msg; // error msg (unused)
2438
struct mz_internal_state *state; // internal state, allocated by zalloc/zfree
2439
2440
mz_alloc_func zalloc; // optional heap allocation function (defaults to malloc)
2441
mz_free_func zfree; // optional heap free function (defaults to free)
2442
void *opaque; // heap alloc function user pointer
2443
2444
int data_type; // data_type (unused)
2445
mz_ulong adler; // adler32 of the source or uncompressed data
2446
mz_ulong reserved; // not used
2447
} mz_stream;
2448
2449
typedef mz_stream *mz_streamp;
2450
2451
// Returns the version string of miniz.c.
2452
const char *mz_version(void);
2453
2454
// mz_deflateInit() initializes a compressor with default options:
2455
// Parameters:
2456
// pStream must point to an initialized mz_stream struct.
2457
// level must be between [MZ_NO_COMPRESSION, MZ_BEST_COMPRESSION].
2458
// level 1 enables a specially optimized compression function that's been optimized purely for performance, not ratio.
2459
// (This special func. is currently only enabled when MINIZ_USE_UNALIGNED_LOADS_AND_STORES and MINIZ_LITTLE_ENDIAN are defined.)
2460
// Return values:
2461
// MZ_OK on success.
2462
// MZ_STREAM_ERROR if the stream is bogus.
2463
// MZ_PARAM_ERROR if the input parameters are bogus.
2464
// MZ_MEM_ERROR on out of memory.
2465
int mz_deflateInit(mz_streamp pStream, int level);
2466
2467
// mz_deflateInit2() is like mz_deflate(), except with more control:
2468
// Additional parameters:
2469
// method must be MZ_DEFLATED
2470
// 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)
2471
// mem_level must be between [1, 9] (it's checked but ignored by miniz.c)
2472
int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy);
2473
2474
// Quickly resets a compressor without having to reallocate anything. Same as calling mz_deflateEnd() followed by mz_deflateInit()/mz_deflateInit2().
2475
int mz_deflateReset(mz_streamp pStream);
2476
2477
// mz_deflate() compresses the input to output, consuming as much of the input and producing as much output as possible.
2478
// Parameters:
2479
// 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.
2480
// flush may be MZ_NO_FLUSH, MZ_PARTIAL_FLUSH/MZ_SYNC_FLUSH, MZ_FULL_FLUSH, or MZ_FINISH.
2481
// Return values:
2482
// 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).
2483
// 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.
2484
// MZ_STREAM_ERROR if the stream is bogus.
2485
// MZ_PARAM_ERROR if one of the parameters is invalid.
2486
// 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.)
2487
int mz_deflate(mz_streamp pStream, int flush);
2488
2489
// mz_deflateEnd() deinitializes a compressor:
2490
// Return values:
2491
// MZ_OK on success.
2492
// MZ_STREAM_ERROR if the stream is bogus.
2493
int mz_deflateEnd(mz_streamp pStream);
2494
2495
// 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.
2496
mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len);
2497
2498
// Single-call compression functions mz_compress() and mz_compress2():
2499
// Returns MZ_OK on success, or one of the error codes from mz_deflate() on failure.
2500
int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
2501
int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level);
2502
2503
// mz_compressBound() returns a (very) conservative upper bound on the amount of data that could be generated by calling mz_compress().
2504
mz_ulong mz_compressBound(mz_ulong source_len);
2505
2506
// Initializes a decompressor.
2507
int mz_inflateInit(mz_streamp pStream);
2508
2509
// 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:
2510
// window_bits must be MZ_DEFAULT_WINDOW_BITS (to parse zlib header/footer) or -MZ_DEFAULT_WINDOW_BITS (raw deflate).
2511
int mz_inflateInit2(mz_streamp pStream, int window_bits);
2512
2513
// 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.
2514
// Parameters:
2515
// 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.
2516
// flush may be MZ_NO_FLUSH, MZ_SYNC_FLUSH, or MZ_FINISH.
2517
// 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).
2518
// 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.
2519
// Return values:
2520
// 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.
2521
// 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.
2522
// MZ_STREAM_ERROR if the stream is bogus.
2523
// MZ_DATA_ERROR if the deflate stream is invalid.
2524
// MZ_PARAM_ERROR if one of the parameters is invalid.
2525
// 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
2526
// with more input data, or with more room in the output buffer (except when using single call decompression, described above).
2527
int mz_inflate(mz_streamp pStream, int flush);
2528
2529
// Deinitializes a decompressor.
2530
int mz_inflateEnd(mz_streamp pStream);
2531
2532
// Single-call decompression.
2533
// Returns MZ_OK on success, or one of the error codes from mz_inflate() on failure.
2534
int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len);
2535
2536
// Returns a string description of the specified error code, or NULL if the error code is invalid.
2537
const char *mz_error(int err);
2538
2539
// 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.
2540
// Define MINIZ_NO_ZLIB_COMPATIBLE_NAMES to disable zlib-compatibility if you use zlib in the same project.
2541
#ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
2542
typedef unsigned char Byte;
2543
typedef unsigned int uInt;
2544
typedef mz_ulong uLong;
2545
typedef Byte Bytef;
2546
typedef uInt uIntf;
2547
typedef char charf;
2548
typedef int intf;
2549
typedef void *voidpf;
2550
typedef uLong uLongf;
2551
typedef void *voidp;
2552
typedef void *const voidpc;
2553
#define Z_NULL 0
2554
#define Z_NO_FLUSH MZ_NO_FLUSH
2555
#define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH
2556
#define Z_SYNC_FLUSH MZ_SYNC_FLUSH
2557
#define Z_FULL_FLUSH MZ_FULL_FLUSH
2558
#define Z_FINISH MZ_FINISH
2559
#define Z_BLOCK MZ_BLOCK
2560
#define Z_OK MZ_OK
2561
#define Z_STREAM_END MZ_STREAM_END
2562
#define Z_NEED_DICT MZ_NEED_DICT
2563
#define Z_ERRNO MZ_ERRNO
2564
#define Z_STREAM_ERROR MZ_STREAM_ERROR
2565
#define Z_DATA_ERROR MZ_DATA_ERROR
2566
#define Z_MEM_ERROR MZ_MEM_ERROR
2567
#define Z_BUF_ERROR MZ_BUF_ERROR
2568
#define Z_VERSION_ERROR MZ_VERSION_ERROR
2569
#define Z_PARAM_ERROR MZ_PARAM_ERROR
2570
#define Z_NO_COMPRESSION MZ_NO_COMPRESSION
2571
#define Z_BEST_SPEED MZ_BEST_SPEED
2572
#define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION
2573
#define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION
2574
#define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY
2575
#define Z_FILTERED MZ_FILTERED
2576
#define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY
2577
#define Z_RLE MZ_RLE
2578
#define Z_FIXED MZ_FIXED
2579
#define Z_DEFLATED MZ_DEFLATED
2580
#define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS
2581
#define alloc_func mz_alloc_func
2582
#define free_func mz_free_func
2583
#define internal_state mz_internal_state
2584
#define z_stream mz_stream
2585
#define deflateInit mz_deflateInit
2586
#define deflateInit2 mz_deflateInit2
2587
#define deflateReset mz_deflateReset
2588
#define deflate mz_deflate
2589
#define deflateEnd mz_deflateEnd
2590
#define deflateBound mz_deflateBound
2591
#define compress mz_compress
2592
#define compress2 mz_compress2
2593
#define compressBound mz_compressBound
2594
#define inflateInit mz_inflateInit
2595
#define inflateInit2 mz_inflateInit2
2596
#define inflate mz_inflate
2597
#define inflateEnd mz_inflateEnd
2598
#define uncompress mz_uncompress
2599
#define crc32 mz_crc32
2600
#define adler32 mz_adler32
2601
#define MAX_WBITS 15
2602
#define MAX_MEM_LEVEL 9
2603
#define zError mz_error
2604
#define ZLIB_VERSION MZ_VERSION
2605
#define ZLIB_VERNUM MZ_VERNUM
2606
#define ZLIB_VER_MAJOR MZ_VER_MAJOR
2607
#define ZLIB_VER_MINOR MZ_VER_MINOR
2608
#define ZLIB_VER_REVISION MZ_VER_REVISION
2609
#define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION
2610
#define zlibVersion mz_version
2611
#define zlib_version mz_version()
2612
#endif // #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
2613
2614
#endif // MINIZ_NO_ZLIB_APIS
2615
2616
// ------------------- Types and macros
2617
2618
typedef unsigned char mz_uint8;
2619
typedef signed short mz_int16;
2620
typedef unsigned short mz_uint16;
2621
typedef unsigned int mz_uint32;
2622
typedef unsigned int mz_uint;
2623
typedef long long mz_int64;
2624
typedef unsigned long long mz_uint64;
2625
typedef int mz_bool;
2626
2627
#define MZ_FALSE (0)
2628
#define MZ_TRUE (1)
2629
2630
// An attempt to work around MSVC's spammy "warning C4127: conditional expression is constant" message.
2631
#ifdef _MSC_VER
2632
#define MZ_MACRO_END while (0, 0)
2633
#else
2634
#define MZ_MACRO_END while (0)
2635
#endif
2636
2637
// ------------------- ZIP archive reading/writing
2638
2639
#ifndef MINIZ_NO_ARCHIVE_APIS
2640
2641
enum
2642
{
2643
MZ_ZIP_MAX_IO_BUF_SIZE = 64*1024,
2644
MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 260,
2645
MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 256
2646
};
2647
2648
typedef struct
2649
{
2650
mz_uint32 m_file_index;
2651
mz_uint32 m_central_dir_ofs;
2652
mz_uint16 m_version_made_by;
2653
mz_uint16 m_version_needed;
2654
mz_uint16 m_bit_flag;
2655
mz_uint16 m_method;
2656
#ifndef MINIZ_NO_TIME
2657
time_t m_time;
2658
#endif
2659
mz_uint32 m_crc32;
2660
mz_uint64 m_comp_size;
2661
mz_uint64 m_uncomp_size;
2662
mz_uint16 m_internal_attr;
2663
mz_uint32 m_external_attr;
2664
mz_uint64 m_local_header_ofs;
2665
mz_uint32 m_comment_size;
2666
char m_filename[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
2667
char m_comment[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
2668
} mz_zip_archive_file_stat;
2669
2670
typedef size_t (*mz_file_read_func)(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n);
2671
typedef size_t (*mz_file_write_func)(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n);
2672
2673
struct mz_zip_internal_state_tag;
2674
typedef struct mz_zip_internal_state_tag mz_zip_internal_state;
2675
2676
typedef enum
2677
{
2678
MZ_ZIP_MODE_INVALID = 0,
2679
MZ_ZIP_MODE_READING = 1,
2680
MZ_ZIP_MODE_WRITING = 2,
2681
MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3
2682
} mz_zip_mode;
2683
2684
typedef struct mz_zip_archive_tag
2685
{
2686
mz_uint64 m_archive_size;
2687
mz_uint64 m_central_directory_file_ofs;
2688
mz_uint m_total_files;
2689
mz_zip_mode m_zip_mode;
2690
2691
mz_uint m_file_offset_alignment;
2692
2693
mz_alloc_func m_pAlloc;
2694
mz_free_func m_pFree;
2695
mz_realloc_func m_pRealloc;
2696
void *m_pAlloc_opaque;
2697
2698
mz_file_read_func m_pRead;
2699
mz_file_write_func m_pWrite;
2700
void *m_pIO_opaque;
2701
2702
mz_zip_internal_state *m_pState;
2703
2704
} mz_zip_archive;
2705
2706
typedef enum
2707
{
2708
MZ_ZIP_FLAG_CASE_SENSITIVE = 0x0100,
2709
MZ_ZIP_FLAG_IGNORE_PATH = 0x0200,
2710
MZ_ZIP_FLAG_COMPRESSED_DATA = 0x0400,
2711
MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800
2712
} mz_zip_flags;
2713
2714
// ZIP archive reading
2715
2716
// Inits a ZIP archive reader.
2717
// These functions read and validate the archive's central directory.
2718
mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint32 flags);
2719
mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint32 flags);
2720
2721
#ifndef MINIZ_NO_STDIO
2722
mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags);
2723
#endif
2724
2725
// Returns the total number of files in the archive.
2726
mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip);
2727
2728
// Returns detailed information about an archive file entry.
2729
mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat);
2730
2731
// Determines if an archive file entry is a directory entry.
2732
mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index);
2733
mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index);
2734
2735
// Retrieves the filename of an archive file entry.
2736
// 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.
2737
mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size);
2738
2739
// Attempts to locates a file in the archive's central directory.
2740
// Valid flags: MZ_ZIP_FLAG_CASE_SENSITIVE, MZ_ZIP_FLAG_IGNORE_PATH
2741
// Returns -1 if the file cannot be found.
2742
int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags);
2743
2744
// Extracts a archive file to a memory buffer using no memory allocation.
2745
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);
2746
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);
2747
2748
// Extracts a archive file to a memory buffer.
2749
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);
2750
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);
2751
2752
// Extracts a archive file to a dynamically allocated heap buffer.
2753
void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags);
2754
void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags);
2755
2756
// Extracts a archive file using a callback function to output the file's data.
2757
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);
2758
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);
2759
2760
#ifndef MINIZ_NO_STDIO
2761
// Extracts a archive file to a disk file and sets its last accessed and modified times.
2762
// This function only extracts files, not archive directory records.
2763
mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags);
2764
mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags);
2765
#endif
2766
2767
// Ends archive reading, freeing all allocations, and closing the input archive file if mz_zip_reader_init_file() was used.
2768
mz_bool mz_zip_reader_end(mz_zip_archive *pZip);
2769
2770
// ZIP archive writing
2771
2772
#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
2773
2774
// Inits a ZIP archive writer.
2775
mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size);
2776
mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size);
2777
2778
#ifndef MINIZ_NO_STDIO
2779
mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning);
2780
#endif
2781
2782
// Converts a ZIP archive reader object into a writer object, to allow efficient in-place file appends to occur on an existing archive.
2783
// 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.
2784
// 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).
2785
// Finally, for archives opened using mz_zip_reader_init, the mz_zip_archive's user provided m_pWrite function cannot be NULL.
2786
// Note: In-place archive modification is not recommended unless you know what you're doing, because if execution stops or something goes wrong before
2787
// the archive is finalized the file's central directory will be hosed.
2788
mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename);
2789
2790
// Adds the contents of a memory buffer to an archive. These functions record the current local time into the archive.
2791
// To add a directory entry, call this method with an archive name ending in a forwardslash with empty buffer.
2792
// 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.
2793
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);
2794
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);
2795
2796
#ifndef MINIZ_NO_STDIO
2797
// Adds the contents of a disk file to an archive. This function also records the disk file's modified time into the archive.
2798
// 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.
2799
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);
2800
#endif
2801
2802
// Adds a file to an archive by fully cloning the data from another archive.
2803
// This function fully clones the source file's compressed data (no recompression), along with its full filename, extra data, and comment fields.
2804
mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint file_index);
2805
2806
// Finalizes the archive by writing the central directory records followed by the end of central directory record.
2807
// After an archive is finalized, the only valid call on the mz_zip_archive struct is mz_zip_writer_end().
2808
// An archive must be manually finalized by calling this function for it to be valid.
2809
mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip);
2810
mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **pBuf, size_t *pSize);
2811
2812
// Ends archive writing, freeing all allocations, and closing the output file if mz_zip_writer_init_file() was used.
2813
// Note for the archive to be valid, it must have been finalized before ending.
2814
mz_bool mz_zip_writer_end(mz_zip_archive *pZip);
2815
2816
// Misc. high-level helper functions:
2817
2818
// mz_zip_add_mem_to_archive_file_in_place() efficiently (but not atomically) appends a memory blob to a ZIP archive.
2819
// 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.
2820
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);
2821
2822
// Reads a single file from an archive into a heap block.
2823
// Returns NULL on failure.
2824
void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint zip_flags);
2825
2826
#endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
2827
2828
#endif // #ifndef MINIZ_NO_ARCHIVE_APIS
2829
2830
// ------------------- Low-level Decompression API Definitions
2831
2832
// Decompression flags used by tinfl_decompress().
2833
// 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.
2834
// 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.
2835
// 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).
2836
// TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes.
2837
enum
2838
{
2839
TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
2840
TINFL_FLAG_HAS_MORE_INPUT = 2,
2841
TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
2842
TINFL_FLAG_COMPUTE_ADLER32 = 8
2843
};
2844
2845
// High level decompression functions:
2846
// tinfl_decompress_mem_to_heap() decompresses a block in memory to a heap block allocated via malloc().
2847
// On entry:
2848
// pSrc_buf, src_buf_len: Pointer and size of the Deflate or zlib source data to decompress.
2849
// On return:
2850
// Function returns a pointer to the decompressed data, or NULL on failure.
2851
// *pOut_len will be set to the decompressed data's size, which could be larger than src_buf_len on uncompressible data.
2852
// The caller must call mz_free() on the returned block when it's no longer needed.
2853
void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
2854
2855
// tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory.
2856
// Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success.
2857
#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
2858
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);
2859
2860
// 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.
2861
// Returns 1 on success or 0 on failure.
2862
typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
2863
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);
2864
2865
struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor;
2866
2867
// Max size of LZ dictionary.
2868
#define TINFL_LZ_DICT_SIZE 32768
2869
2870
// Return status.
2871
typedef enum
2872
{
2873
TINFL_STATUS_BAD_PARAM = -3,
2874
TINFL_STATUS_ADLER32_MISMATCH = -2,
2875
TINFL_STATUS_FAILED = -1,
2876
TINFL_STATUS_DONE = 0,
2877
TINFL_STATUS_NEEDS_MORE_INPUT = 1,
2878
TINFL_STATUS_HAS_MORE_OUTPUT = 2
2879
} tinfl_status;
2880
2881
// Initializes the decompressor to its initial state.
2882
#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END
2883
#define tinfl_get_adler32(r) (r)->m_check_adler32
2884
2885
// 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.
2886
// 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.
2887
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);
2888
2889
// Internal/private bits follow.
2890
enum
2891
{
2892
TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19,
2893
TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
2894
};
2895
2896
typedef struct
2897
{
2898
mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
2899
mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
2900
} tinfl_huff_table;
2901
2902
#if MINIZ_HAS_64BIT_REGISTERS
2903
#define TINFL_USE_64BIT_BITBUF 1
2904
#endif
2905
2906
#if TINFL_USE_64BIT_BITBUF
2907
typedef mz_uint64 tinfl_bit_buf_t;
2908
#define TINFL_BITBUF_SIZE (64)
2909
#else
2910
typedef mz_uint32 tinfl_bit_buf_t;
2911
#define TINFL_BITBUF_SIZE (32)
2912
#endif
2913
2914
struct tinfl_decompressor_tag
2915
{
2916
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];
2917
tinfl_bit_buf_t m_bit_buf;
2918
size_t m_dist_from_out_buf_start;
2919
tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
2920
mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
2921
};
2922
2923
// ------------------- Low-level Compression API Definitions
2924
2925
// Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently).
2926
#define TDEFL_LESS_MEMORY 0
2927
2928
// tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search):
2929
// 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).
2930
enum
2931
{
2932
TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF
2933
};
2934
2935
// 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.
2936
// TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers).
2937
// TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing.
2938
// 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).
2939
// TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1)
2940
// TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled.
2941
// TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables.
2942
// TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks.
2943
// The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK).
2944
enum
2945
{
2946
TDEFL_WRITE_ZLIB_HEADER = 0x01000,
2947
TDEFL_COMPUTE_ADLER32 = 0x02000,
2948
TDEFL_GREEDY_PARSING_FLAG = 0x04000,
2949
TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000,
2950
TDEFL_RLE_MATCHES = 0x10000,
2951
TDEFL_FILTER_MATCHES = 0x20000,
2952
TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000,
2953
TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
2954
};
2955
2956
// High level compression functions:
2957
// tdefl_compress_mem_to_heap() compresses a block in memory to a heap block allocated via malloc().
2958
// On entry:
2959
// pSrc_buf, src_buf_len: Pointer and size of source block to compress.
2960
// flags: The max match finder probes (default is 128) logically OR'd against the above flags. Higher probes are slower but improve compression.
2961
// On return:
2962
// Function returns a pointer to the compressed data, or NULL on failure.
2963
// *pOut_len will be set to the compressed data's size, which could be larger than src_buf_len on uncompressible data.
2964
// The caller must free() the returned block when it's no longer needed.
2965
void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
2966
2967
// tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory.
2968
// Returns 0 on failure.
2969
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);
2970
2971
// Compresses an image to a compressed PNG file in memory.
2972
// On entry:
2973
// pImage, w, h, and num_chans describe the image to compress. num_chans may be 1, 2, 3, or 4.
2974
// The image pitch in bytes per scanline will be w*num_chans. The leftmost pixel on the top scanline is stored first in memory.
2975
// 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
2976
// If flip is true, the image will be flipped on the Y axis (useful for OpenGL apps).
2977
// On return:
2978
// Function returns a pointer to the compressed data, or NULL on failure.
2979
// *pLen_out will be set to the size of the PNG image file.
2980
// The caller must mz_free() the returned heap block (which will typically be larger than *pLen_out) when it's no longer needed.
2981
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);
2982
void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out);
2983
2984
// Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time.
2985
typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
2986
2987
// tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally.
2988
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);
2989
2990
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 };
2991
2992
// TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes).
2993
#if TDEFL_LESS_MEMORY
2994
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 };
2995
#else
2996
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 };
2997
#endif
2998
2999
// 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.
3000
typedef enum
3001
{
3002
TDEFL_STATUS_BAD_PARAM = -2,
3003
TDEFL_STATUS_PUT_BUF_FAILED = -1,
3004
TDEFL_STATUS_OKAY = 0,
3005
TDEFL_STATUS_DONE = 1,
3006
} tdefl_status;
3007
3008
// Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums
3009
typedef enum
3010
{
3011
TDEFL_NO_FLUSH = 0,
3012
TDEFL_SYNC_FLUSH = 2,
3013
TDEFL_FULL_FLUSH = 3,
3014
TDEFL_FINISH = 4
3015
} tdefl_flush;
3016
3017
// tdefl's compression state structure.
3018
typedef struct
3019
{
3020
tdefl_put_buf_func_ptr m_pPut_buf_func;
3021
void *m_pPut_buf_user;
3022
mz_uint m_flags, m_max_probes[2];
3023
int m_greedy_parsing;
3024
mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
3025
mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
3026
mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer;
3027
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;
3028
tdefl_status m_prev_return_status;
3029
const void *m_pIn_buf;
3030
void *m_pOut_buf;
3031
size_t *m_pIn_buf_size, *m_pOut_buf_size;
3032
tdefl_flush m_flush;
3033
const mz_uint8 *m_pSrc;
3034
size_t m_src_buf_left, m_out_buf_ofs;
3035
mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
3036
mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
3037
mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
3038
mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
3039
mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
3040
mz_uint16 m_next[TDEFL_LZ_DICT_SIZE];
3041
mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
3042
mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
3043
} tdefl_compressor;
3044
3045
// Initializes the compressor.
3046
// There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory.
3047
// 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.
3048
// If pBut_buf_func is NULL the user should always call the tdefl_compress() API.
3049
// flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.)
3050
tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
3051
3052
// 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.
3053
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);
3054
3055
// tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr.
3056
// tdefl_compress_buffer() always consumes the entire input buffer.
3057
tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush);
3058
3059
tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d);
3060
mz_uint32 tdefl_get_adler32(tdefl_compressor *d);
3061
3062
// 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.
3063
#ifndef MINIZ_NO_ZLIB_APIS
3064
// Create tdefl_compress() flags given zlib-style compression parameters.
3065
// level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files)
3066
// window_bits may be -15 (raw deflate) or 15 (zlib)
3067
// strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED
3068
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy);
3069
#endif // #ifndef MINIZ_NO_ZLIB_APIS
3070
3071
#ifdef __cplusplus
3072
}
3073
#endif
3074
3075
#endif // MINIZ_HEADER_INCLUDED
3076

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button