-
Notifications
You must be signed in to change notification settings - Fork 50
/
miniz.h
512 lines (426 loc) · 14.9 KB
/
miniz.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* This library (excluding the miniz C code used for tests) is licensed under the MIT license. The library is based on the miniz C library, of which the parts used are dual-licensed under the MIT license and also the unlicense. The parts of miniz that are not covered by the unlicense is some Zip64 code which is only MIT licensed. This and other Zip functionality in miniz is not part of the miniz_oxidde and miniz_oxide_c_api rust libraries.*/
#pragma once
/* Generated with cbindgen:0.20.0 */
/* DO NOT MODIFY THIS MANUALLY! This file was generated using cbindgen.
* To generate this file:
* 1. Get the latest cbindgen using `cargo install --force cbindgen`
* a. Alternatively, you can clone `https://github.com/eqrion/cbindgen` and use a tagged release
* 2. Run `rustup run nightly cbindgen toolkit/library/rust/ --lockfile Cargo.lock --crate miniz_oxide_c_api -o miniz_h/miniz.h`
*/
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "miniz_extra_defs.h"
#define MZ_DEFLATED 8
#define MZ_CRC32_INIT 0
/**
* Size of the buffer of lz77 encoded data.
*/
#define LZ_CODE_BUF_SIZE (64 * 1024)
/**
* Size of the output buffer.
*/
#define OUT_BUF_SIZE ((LZ_CODE_BUF_SIZE * 13) / 10)
#define LZ_DICT_FULL_SIZE (((LZ_DICT_SIZE + MAX_MATCH_LEN) - 1) + 1)
/**
* Size of hash values in the hash chains.
*/
#define LZ_HASH_BITS 15
/**
* How many bits to shift when updating the current hash value.
*/
#define LZ_HASH_SHIFT ((LZ_HASH_BITS + 2) / 3)
/**
* Size of the chained hash tables.
*/
#define LZ_HASH_SIZE (1 << LZ_HASH_BITS)
/**
* Whether to use a zlib wrapper.
*/
#define TDEFL_WRITE_ZLIB_HEADER 4096
/**
* Should we compute the adler32 checksum.
*/
#define TDEFL_COMPUTE_ADLER32 8192
/**
* Should we use greedy parsing (as opposed to lazy parsing where look ahead one or more
* bytes to check for better matches.)
*/
#define TDEFL_GREEDY_PARSING_FLAG 16384
/**
* Used in miniz to skip zero-initializing hash and dict. We don't do this here, so
* this flag is ignored.
*/
#define TDEFL_NONDETERMINISTIC_PARSING_FLAG 32768
/**
* Only look for matches with a distance of 0.
*/
#define TDEFL_RLE_MATCHES 65536
/**
* Only use matches that are at least 6 bytes long.
*/
#define TDEFL_FILTER_MATCHES 131072
/**
* Force the compressor to only output static blocks. (Blocks using the default huffman codes
* specified in the deflate specification.)
*/
#define TDEFL_FORCE_ALL_STATIC_BLOCKS 262144
/**
* Force the compressor to only output raw/uncompressed blocks.
*/
#define TDEFL_FORCE_ALL_RAW_BLOCKS 524288
#define TINFL_LZ_DICT_SIZE 32768
/**
* Should we try to parse a zlib header?
*
* If unset, [`decompress()`] will expect an RFC1951 deflate stream. If set, it will expect an
* RFC1950 zlib wrapper around the deflate stream.
*/
#define TINFL_FLAG_PARSE_ZLIB_HEADER 1
/**
* There will be more input that hasn't been given to the decompressor yet.
*
* This is useful when you want to decompress what you have so far,
* even if you know there is probably more input that hasn't gotten here yet (_e.g._, over a
* network connection). When [`decompress()`][super::decompress] reaches the end of the input
* without finding the end of the compressed stream, it will return
* [`TINFLStatus::NeedsMoreInput`][super::TINFLStatus::NeedsMoreInput] if this is set,
* indicating that you should get more data before calling again. If not set, it will return
* [`TINFLStatus::FailedCannotMakeProgress`][super::TINFLStatus::FailedCannotMakeProgress]
* suggesting the stream is corrupt, since you claimed it was all there.
*/
#define TINFL_FLAG_HAS_MORE_INPUT 2
/**
* The output buffer should not wrap around.
*/
#define TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF 4
/**
* Calculate the adler32 checksum of the output data even if we're not inflating a zlib stream.
*
* If [`TINFL_FLAG_IGNORE_ADLER32`] is specified, it will override this.
*
* NOTE: Enabling/disabling this between calls to decompress will result in an incorect
* checksum.
*/
#define TINFL_FLAG_COMPUTE_ADLER32 8
/**
* Ignore adler32 checksum even if we are inflating a zlib stream.
*
* Overrides [`TINFL_FLAG_COMPUTE_ADLER32`] if both are enabled.
*
* NOTE: This flag does not exist in miniz as it does not support this and is a
* custom addition for miniz_oxide.
*
* NOTE: Should not be changed from enabled to disabled after decompression has started,
* this will result in checksum failure (outside the unlikely event where the checksum happens
* to match anyway).
*/
#define TINFL_FLAG_IGNORE_ADLER32 64
#define MZ_ADLER32_INIT 1
#define MZ_DEFAULT_WINDOW_BITS 15
typedef enum CAPICompressionLevel {
MZ_NO_COMPRESSION = 0,
MZ_BEST_SPEED = 1,
MZ_BEST_COMPRESSION = 9,
MZ_UBER_COMPRESSION = 10,
MZ_DEFAULT_LEVEL = 6,
MZ_DEFAULT_COMPRESSION = -1,
} CAPICompressionLevel;
typedef enum CAPICompressionStrategy {
MZ_DEFAULT_STRATEGY = 0,
MZ_FILTERED = 1,
MZ_HUFFMAN_ONLY = 2,
MZ_RLE = 3,
MZ_FIXED = 4,
} CAPICompressionStrategy;
/**
* Deflate flush modes.
*/
typedef enum CAPIFlush {
MZ_NO_FLUSH = 0,
MZ_PARTIAL_FLUSH = 1,
MZ_SYNC_FLUSH = 2,
MZ_FULL_FLUSH = 3,
MZ_FINISH = 4,
MZ_BLOCK = 5,
} CAPIFlush;
typedef enum CAPIReturnStatus {
MZ_PARAM_ERROR = -10000,
MZ_VERSION_ERROR = -6,
MZ_BUF_ERROR = -5,
MZ_MEM_ERROR = -4,
MZ_DATA_ERROR = -3,
MZ_STREAM_ERROR = -2,
MZ_ERRNO = -1,
MZ_OK = 0,
MZ_STREAM_END = 1,
MZ_NEED_DICT = 2,
} CAPIReturnStatus;
/**
* Enum to keep track of what type the internal state is when moving over the C API boundary.
*/
typedef enum StateTypeEnum {
None = 0,
InflateType,
DeflateType,
} StateTypeEnum;
typedef enum tdefl_flush {
TDEFL_NO_FLUSH = 0,
TDEFL_SYNC_FLUSH = 2,
TDEFL_FULL_FLUSH = 3,
TDEFL_FINISH = 4,
} tdefl_flush;
typedef enum tdefl_status {
TDEFL_STATUS_BAD_PARAM = -2,
TDEFL_STATUS_PUT_BUF_FAILED = -1,
TDEFL_STATUS_OKAY = 0,
TDEFL_STATUS_DONE = 1,
} tdefl_status;
typedef enum tinfl_status {
TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4,
TINFL_STATUS_BAD_PARAM = -3,
TINFL_STATUS_ADLER32_MISMATCH = -2,
TINFL_STATUS_FAILED = -1,
TINFL_STATUS_DONE = 0,
TINFL_STATUS_NEEDS_MORE_INPUT = 1,
TINFL_STATUS_HAS_MORE_OUTPUT = 2,
} tinfl_status;
/**
* Main compression struct. Not the same as `CompressorOxide`
* #[repr(C)]
*/
typedef struct tdefl_compressor tdefl_compressor;
/**
* Main decompression struct.
*
*/
typedef struct DecompressorOxide DecompressorOxide;
typedef struct InternalState InternalState;
typedef void *(*mz_alloc_callback)(void*, size_t, size_t);
typedef void (*mz_free_callback)(void*, void*);
/**
* Inner stream state containing pointers to the used buffers and internal state.
*/
typedef struct mz_stream {
/**
* Pointer to the current start of the input buffer.
*/
const uint8_t *next_in;
/**
* Length of the input buffer.
*/
unsigned int avail_in;
/**
* The total number of input bytes consumed so far.
*/
unsigned long total_in;
/**
* Pointer to the current start of the output buffer.
*/
uint8_t *next_out;
/**
* Space in the output buffer.
*/
unsigned int avail_out;
/**
* The total number of bytes output so far.
*/
unsigned long total_out;
const char *msg;
/**
* Compressor or decompressor, if it exists.
* This is boxed to work with the current C API.
*/
struct InternalState *state;
/**
* Allocation function to use for allocating the internal compressor/decompressor.
* Uses `mz_default_alloc_func` if set to `None`.
*/
mz_alloc_callback zalloc;
/**
* Free function to use for allocating the internal compressor/decompressor.
* Uses `mz_default_free_func` if `None`.
*/
mz_free_callback zfree;
/**
* Extra data to provide the allocation/deallocation functions.
* (Not used for the default ones)
*/
void *opaque;
/**
* Whether the stream contains a compressor or decompressor.
*/
enum StateTypeEnum data_type;
/**
* Adler32 checksum of the data that has been compressed or uncompressed.
*/
unsigned long adler;
/**
* Reserved
*/
unsigned long reserved;
} mz_stream;
typedef int32_t (*tdefl_put_buf_func_ptr)(const void*, int, void*);
typedef struct tinfl_decompressor {
struct DecompressorOxide *inner;
} tinfl_decompressor;
/**
* Signature of function used to allocate the compressor/decompressor structs.
*/
typedef void *(*mz_alloc_func)(void*, size_t, size_t);
/**
* Signature of function used to free the compressor/decompressor structs.
*/
typedef void (*mz_free_func)(void*, void*);
typedef void *(*mz_realloc_func)(void*, void*, size_t, size_t);
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
int mz_deflate(struct mz_stream *stream, int flush);
int mz_deflateEnd(struct mz_stream *stream);
int mz_deflateReset(struct mz_stream *stream);
int mz_inflate(struct mz_stream *stream, int flush);
int mz_inflateEnd(struct mz_stream *stream);
int mz_deflateInit(struct mz_stream *stream, int level);
int mz_deflateInit2(struct mz_stream *stream,
int level,
int method,
int window_bits,
int mem_level,
int strategy);
int mz_inflateInit2(struct mz_stream *stream, int window_bits);
int mz_compress(uint8_t *dest,
unsigned long *dest_len,
const uint8_t *source,
unsigned long source_len);
int mz_compress2(uint8_t *dest,
unsigned long *dest_len,
const uint8_t *source,
unsigned long source_len,
int level);
unsigned long mz_deflateBound(struct mz_stream *_stream, unsigned long source_len);
int mz_inflateInit(struct mz_stream *stream);
int mz_uncompress(uint8_t *dest,
unsigned long *dest_len,
const uint8_t *source,
unsigned long source_len);
unsigned long mz_compressBound(unsigned long source_len);
enum tdefl_status tdefl_compress(struct tdefl_compressor *d,
const void *in_buf,
uintptr_t *in_size,
void *out_buf,
uintptr_t *out_size,
enum tdefl_flush flush);
enum tdefl_status tdefl_compress_buffer(struct tdefl_compressor *d,
const void *in_buf,
uintptr_t in_size,
enum tdefl_flush flush);
/**
* Allocate a compressor.
*
* This does initialize the struct, but not the inner constructor,
* tdefl_init has to be called before doing anything with it.
*/
struct tdefl_compressor *tdefl_allocate(void);
/**
* Deallocate the compressor. (Does nothing if the argument is null).
*
* This also calles the compressor's destructor, freeing the internal memory
* allocated by it.
*/
void tdefl_deallocate(struct tdefl_compressor *c);
/**
* Initialize the compressor struct in the space pointed to by `d`.
* if d is null, an error is returned.
*
* Deinitialization is handled by tdefl_deallocate, and thus
* Compressor should not be allocated or freed manually, but only through
* tdefl_allocate and tdefl_deallocate
*/
enum tdefl_status tdefl_init(struct tdefl_compressor *d,
tdefl_put_buf_func_ptr put_buf_func,
void *put_buf_user,
int flags);
enum tdefl_status tdefl_get_prev_return_status(struct tdefl_compressor *d);
unsigned int tdefl_get_adler32(struct tdefl_compressor *d);
int tdefl_compress_mem_to_output(const void *buf,
uintptr_t buf_len,
tdefl_put_buf_func_ptr put_buf_func,
void *put_buf_user,
int flags);
void *tdefl_compress_mem_to_heap(const void *src_buf,
uintptr_t src_buf_len,
uintptr_t *out_len,
int flags);
uintptr_t tdefl_compress_mem_to_mem(void *out_buf,
uintptr_t out_buf_len,
const void *src_buf,
uintptr_t src_buf_len,
int flags);
unsigned int tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy);
int32_t tinfl_decompress(struct tinfl_decompressor *r,
const uint8_t *in_buf,
uintptr_t *in_buf_size,
uint8_t *out_buf_start,
uint8_t *out_buf_next,
uintptr_t *out_buf_size,
uint32_t flags);
size_t tinfl_decompress_mem_to_mem(void *p_out_buf,
size_t out_buf_len,
const void *p_src_buf,
size_t src_buf_len,
int flags);
/**
* Decompress data from `p_src_buf` to a continuously growing heap-allocated buffer.
*
* Sets `p_out_len` to the length of the returned buffer.
* Returns `ptr::null()` if decompression or allocation fails.
* The buffer should be freed with `miniz_def_free_func`.
*/
void *tinfl_decompress_mem_to_heap(const void *p_src_buf,
size_t src_buf_len,
size_t *p_out_len,
int flags);
/**
* Allocate a compressor.
*
* This does initialize the struct, but not the inner constructor,
* tdefl_init has to be called before doing anything with it.
*/
struct tinfl_decompressor *tinfl_decompressor_alloc(void);
/**
* Deallocate the compressor. (Does nothing if the argument is null).
*
* This also calles the compressor's destructor, freeing the internal memory
* allocated by it.
*/
void tinfl_decompressor_free(struct tinfl_decompressor *c);
void tinfl_init(struct tinfl_decompressor *c);
int tinfl_get_adler32(struct tinfl_decompressor *c);
/**
* Default allocation function using `malloc`.
*/
void *miniz_def_alloc_func(void *_opaque, size_t items, size_t size);
/**
* Default free function using `free`.
*/
void miniz_def_free_func(void *_opaque, void *address);
void *miniz_def_realloc_func(void *_opaque, void *address, size_t items, size_t size);
/**
* Calculate adler32 checksum of the provided buffer with the initial adler32 checksum of `adler`.
* If c_ulong is wider than 32 bits, only the lower 32 bits will be used.
*
* Returns MZ_ADLER32_INIT if ptr is `ptr::null`.
*/
unsigned long mz_adler32(unsigned long adler, const uint8_t *ptr, uintptr_t buf_len);
/**
* Calculate crc-32 of the provided buffer with the initial CRC32 checksum of `crc`.
* If c_ulong is wider than 32 bits, only the lower 32 bits will be used.
*
* Returns MZ_CRC32_INIT if ptr is `ptr::null`.
*/
unsigned long mz_crc32(unsigned long crc, const uint8_t *ptr, size_t buf_len);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus