299 lines
15 KiB
C
299 lines
15 KiB
C
/* This is miniz.c with removal of all zlib/zip like functionality - only tdefl/tinfl APIs are left
|
|
For maximum compatibility unaligned load/store and 64-bit register paths have been removed so this is slower than miniz.c
|
|
|
|
miniz.c v1.15 - public domain deflate/inflate, zlib-subset, ZIP reading/writing/appending, PNG writing
|
|
See "unlicense" statement at the end of this file.
|
|
Rich Geldreich <richgel99@gmail.com>, last updated Oct. 13, 2013
|
|
Implements RFC 1950: http://www.ietf.org/rfc/rfc1950.txt and RFC 1951: http://www.ietf.org/rfc/rfc1951.txt
|
|
*/
|
|
|
|
#ifndef MINIZ_HEADER_INCLUDED
|
|
#define MINIZ_HEADER_INCLUDED
|
|
|
|
#include <stdlib.h>
|
|
|
|
// Define MINIZ_NO_MALLOC to disable all calls to malloc, free, and realloc.
|
|
// Note if MINIZ_NO_MALLOC is defined then the user must always provide custom user alloc/free/realloc
|
|
// callbacks to the zlib and archive API's, and a few stand-alone helper API's which don't provide custom user
|
|
// functions (such as tdefl_compress_mem_to_heap() and tinfl_decompress_mem_to_heap()) won't work.
|
|
//#define MINIZ_NO_MALLOC
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
// 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.
|
|
void mz_free(void *p);
|
|
|
|
// Compression strategies.
|
|
enum { MZ_DEFAULT_STRATEGY = 0, MZ_FILTERED = 1, MZ_HUFFMAN_ONLY = 2, MZ_RLE = 3, MZ_FIXED = 4 };
|
|
|
|
// 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.
|
|
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 };
|
|
|
|
// Window bits
|
|
#define MZ_DEFAULT_WINDOW_BITS 15
|
|
|
|
// Method
|
|
#define MZ_DEFLATED 8
|
|
|
|
// ------------------- Types and macros
|
|
|
|
typedef unsigned char mz_uint8;
|
|
typedef signed short mz_int16;
|
|
typedef unsigned short mz_uint16;
|
|
typedef unsigned int mz_uint32;
|
|
typedef unsigned int mz_uint;
|
|
typedef long long mz_int64;
|
|
typedef unsigned long long mz_uint64;
|
|
typedef int mz_bool;
|
|
|
|
#define MZ_FALSE (0)
|
|
#define MZ_TRUE (1)
|
|
|
|
// An attempt to work around MSVC's spammy "warning C4127: conditional expression is constant" message.
|
|
#ifdef _MSC_VER
|
|
#define MZ_MACRO_END while (0, 0)
|
|
#else
|
|
#define MZ_MACRO_END while (0)
|
|
#endif
|
|
|
|
#define MZ_ADLER32_INIT (1)
|
|
// mz_adler32() returns the initial adler-32 value to use when called with ptr==NULL.
|
|
mz_uint32 mz_adler32(mz_uint32 adler, const unsigned char *ptr, size_t buf_len);
|
|
|
|
// ------------------- Low-level Decompression API Definitions
|
|
|
|
// Decompression flags used by tinfl_decompress().
|
|
// 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.
|
|
// 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.
|
|
// 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).
|
|
// TINFL_FLAG_COMPUTE_ADLER32: Force adler-32 checksum computation of the decompressed bytes.
|
|
enum
|
|
{
|
|
TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
|
|
TINFL_FLAG_HAS_MORE_INPUT = 2,
|
|
TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
|
|
TINFL_FLAG_COMPUTE_ADLER32 = 8
|
|
};
|
|
|
|
// High level decompression functions:
|
|
// tinfl_decompress_mem_to_mem() decompresses a block in memory to another block in memory.
|
|
// Returns TINFL_DECOMPRESS_MEM_TO_MEM_FAILED on failure, or the number of bytes written on success.
|
|
#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
|
|
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);
|
|
|
|
// 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.
|
|
// Returns 1 on success or 0 on failure.
|
|
typedef int (*tinfl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
|
|
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);
|
|
|
|
struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor;
|
|
|
|
// Max size of LZ dictionary.
|
|
#define TINFL_LZ_DICT_SIZE 32768
|
|
|
|
// Return status.
|
|
typedef enum
|
|
{
|
|
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;
|
|
|
|
// Initializes the decompressor to its initial state.
|
|
#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END
|
|
#define tinfl_get_adler32(r) (r)->m_check_adler32
|
|
|
|
// 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.
|
|
// 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.
|
|
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);
|
|
|
|
// Internal/private bits follow.
|
|
enum
|
|
{
|
|
TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19,
|
|
TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
|
|
};
|
|
|
|
typedef struct
|
|
{
|
|
mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
|
|
mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
|
|
} tinfl_huff_table;
|
|
|
|
typedef mz_uint32 tinfl_bit_buf_t;
|
|
#define TINFL_BITBUF_SIZE (32)
|
|
|
|
struct tinfl_decompressor_tag
|
|
{
|
|
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];
|
|
tinfl_bit_buf_t m_bit_buf;
|
|
size_t m_dist_from_out_buf_start;
|
|
tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
|
|
mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
|
|
};
|
|
|
|
// ------------------- Low-level Compression API Definitions
|
|
|
|
// Set TDEFL_LESS_MEMORY to 1 to use less memory (compression will be slightly slower, and raw/dynamic blocks will be output more frequently).
|
|
#define TDEFL_LESS_MEMORY 0
|
|
|
|
// tdefl_init() compression flags logically OR'd together (low 12 bits contain the max. number of probes per dictionary search):
|
|
// 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).
|
|
enum
|
|
{
|
|
TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF
|
|
};
|
|
|
|
// 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.
|
|
// TDEFL_COMPUTE_ADLER32: Always compute the adler-32 of the input data (even when not writing zlib headers).
|
|
// TDEFL_GREEDY_PARSING_FLAG: Set to use faster greedy parsing, instead of more efficient lazy parsing.
|
|
// 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).
|
|
// TDEFL_RLE_MATCHES: Only look for RLE matches (matches with a distance of 1)
|
|
// TDEFL_FILTER_MATCHES: Discards matches <= 5 chars if enabled.
|
|
// TDEFL_FORCE_ALL_STATIC_BLOCKS: Disable usage of optimized Huffman tables.
|
|
// TDEFL_FORCE_ALL_RAW_BLOCKS: Only use raw (uncompressed) deflate blocks.
|
|
// The low 12 bits are reserved to control the max # of hash probes per dictionary lookup (see TDEFL_MAX_PROBES_MASK).
|
|
enum
|
|
{
|
|
TDEFL_WRITE_ZLIB_HEADER = 0x01000,
|
|
TDEFL_COMPUTE_ADLER32 = 0x02000,
|
|
TDEFL_GREEDY_PARSING_FLAG = 0x04000,
|
|
TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000,
|
|
TDEFL_RLE_MATCHES = 0x10000,
|
|
TDEFL_FILTER_MATCHES = 0x20000,
|
|
TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000,
|
|
TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
|
|
};
|
|
|
|
// High level compression functions:
|
|
|
|
|
|
// tdefl_compress_bound() returns a (very) conservative upper bound on the amount of data that could be generated by calling tdefl_compress_*().
|
|
size_t tdefl_compress_bound(size_t source_len);
|
|
|
|
// tdefl_compress_mem_to_mem() compresses a block in memory to another block in memory.
|
|
// Returns 0 on failure.
|
|
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);
|
|
|
|
// Output stream interface. The compressor uses this interface to write compressed data. It'll typically be called TDEFL_OUT_BUF_SIZE at a time.
|
|
typedef mz_bool (*tdefl_put_buf_func_ptr)(const void* pBuf, int len, void *pUser);
|
|
|
|
// tdefl_compress_mem_to_output() compresses a block to an output stream. The above helpers use this function internally.
|
|
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);
|
|
|
|
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 };
|
|
|
|
// TDEFL_OUT_BUF_SIZE MUST be large enough to hold a single entire compressed output block (using static/fixed Huffman codes).
|
|
#if TDEFL_LESS_MEMORY
|
|
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 };
|
|
#else
|
|
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 };
|
|
#endif
|
|
|
|
// 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.
|
|
typedef enum
|
|
{
|
|
TDEFL_STATUS_BAD_PARAM = -2,
|
|
TDEFL_STATUS_PUT_BUF_FAILED = -1,
|
|
TDEFL_STATUS_OKAY = 0,
|
|
TDEFL_STATUS_DONE = 1,
|
|
} tdefl_status;
|
|
|
|
// Must map to MZ_NO_FLUSH, MZ_SYNC_FLUSH, etc. enums
|
|
typedef enum
|
|
{
|
|
TDEFL_NO_FLUSH = 0,
|
|
TDEFL_SYNC_FLUSH = 2,
|
|
TDEFL_FULL_FLUSH = 3,
|
|
TDEFL_FINISH = 4
|
|
} tdefl_flush;
|
|
|
|
// tdefl's compression state structure.
|
|
typedef struct
|
|
{
|
|
tdefl_put_buf_func_ptr m_pPut_buf_func;
|
|
void *m_pPut_buf_user;
|
|
mz_uint m_flags, m_max_probes[2];
|
|
int m_greedy_parsing;
|
|
mz_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
|
|
mz_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
|
|
mz_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer;
|
|
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;
|
|
tdefl_status m_prev_return_status;
|
|
const void *m_pIn_buf;
|
|
void *m_pOut_buf;
|
|
size_t *m_pIn_buf_size, *m_pOut_buf_size;
|
|
tdefl_flush m_flush;
|
|
const mz_uint8 *m_pSrc;
|
|
size_t m_src_buf_left, m_out_buf_ofs;
|
|
mz_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
|
|
mz_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
|
|
mz_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
|
|
mz_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
|
|
mz_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
|
|
mz_uint16 m_next[TDEFL_LZ_DICT_SIZE];
|
|
mz_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
|
|
mz_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
|
|
} tdefl_compressor;
|
|
|
|
// Initializes the compressor.
|
|
// There is no corresponding deinit() function because the tdefl API's do not dynamically allocate memory.
|
|
// 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.
|
|
// If pBut_buf_func is NULL the user should always call the tdefl_compress() API.
|
|
// flags: See the above enums (TDEFL_HUFFMAN_ONLY, TDEFL_WRITE_ZLIB_HEADER, etc.)
|
|
tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
|
|
|
|
// 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.
|
|
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);
|
|
|
|
// tdefl_compress_buffer() is only usable when the tdefl_init() is called with a non-NULL tdefl_put_buf_func_ptr.
|
|
// tdefl_compress_buffer() always consumes the entire input buffer.
|
|
tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush);
|
|
|
|
tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d);
|
|
mz_uint32 tdefl_get_adler32(tdefl_compressor *d);
|
|
|
|
// Create tdefl_compress() flags given zlib-style compression parameters.
|
|
// level may range from [0,10] (where 10 is absolute max compression, but may be much slower on some files)
|
|
// window_bits may be -15 (raw deflate) or 15 (zlib)
|
|
// strategy may be either MZ_DEFAULT_STRATEGY, MZ_FILTERED, MZ_HUFFMAN_ONLY, MZ_RLE, or MZ_FIXED
|
|
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif // MINIZ_HEADER_INCLUDED
|
|
|
|
/*
|
|
This is free and unencumbered software released into the public domain.
|
|
|
|
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
distribute this software, either in source code form or as a compiled
|
|
binary, for any purpose, commercial or non-commercial, and by any
|
|
means.
|
|
|
|
In jurisdictions that recognize copyright laws, the author or authors
|
|
of this software dedicate any and all copyright interest in the
|
|
software to the public domain. We make this dedication for the benefit
|
|
of the public at large and to the detriment of our heirs and
|
|
successors. We intend this dedication to be an overt act of
|
|
relinquishment in perpetuity of all present and future rights to this
|
|
software under copyright law.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
For more information, please refer to <http://unlicense.org/>
|
|
*/
|