mirror of
https://github.com/libretro/RetroArch
synced 2025-02-03 17:54:04 +00:00
ff49f28236
This reverts commit 9a7125b49d565c38cb2612957895e58cbf9c9933.
942 lines
43 KiB
C
942 lines
43 KiB
C
/*
|
|
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/>
|
|
*/
|
|
|
|
#include "zlib.h"
|
|
|
|
typedef unsigned char mz_validate_uint16[sizeof(mz_uint16)==2 ? 1 : -1];
|
|
typedef unsigned char mz_validate_uint32[sizeof(mz_uint32)==4 ? 1 : -1];
|
|
typedef unsigned char mz_validate_uint64[sizeof(mz_uint64)==8 ? 1 : -1];
|
|
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
|
|
#define MZ_ASSERT(x) assert(x)
|
|
|
|
#define MZ_MALLOC(x) malloc(x)
|
|
#define MZ_FREE(x) free(x)
|
|
#define MZ_REALLOC(p, x) realloc(p, x)
|
|
|
|
#define MZ_MAX(a,b) (((a)>(b))?(a):(b))
|
|
#define MZ_MIN(a,b) (((a)<(b))?(a):(b))
|
|
#define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
|
|
|
|
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
|
|
#define MZ_READ_LE16(p) *((const mz_uint16 *)(p))
|
|
#define MZ_READ_LE32(p) *((const mz_uint32 *)(p))
|
|
#else
|
|
#define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U))
|
|
#define MZ_READ_LE32(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U))
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
#define MZ_FORCEINLINE __forceinline
|
|
#elif defined(__GNUC__)
|
|
#define MZ_FORCEINLINE __attribute__((__always_inline__))
|
|
#else
|
|
#define MZ_FORCEINLINE
|
|
#endif
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
// ------------------- zlib-style API's
|
|
|
|
mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len)
|
|
{
|
|
mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16); size_t block_len = buf_len % 5552;
|
|
if (!ptr) return MZ_ADLER32_INIT;
|
|
while (buf_len) {
|
|
for (i = 0; i + 7 < block_len; i += 8, ptr += 8) {
|
|
s1 += ptr[0], s2 += s1; s1 += ptr[1], s2 += s1; s1 += ptr[2], s2 += s1; s1 += ptr[3], s2 += s1;
|
|
s1 += ptr[4], s2 += s1; s1 += ptr[5], s2 += s1; s1 += ptr[6], s2 += s1; s1 += ptr[7], s2 += s1;
|
|
}
|
|
for ( ; i < block_len; ++i) s1 += *ptr++, s2 += s1;
|
|
s1 %= 65521U, s2 %= 65521U; buf_len -= block_len; block_len = 5552;
|
|
}
|
|
return (s2 << 16) + s1;
|
|
}
|
|
|
|
// Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C implementation that balances processor cache usage against speed": http://www.geocities.com/malbrain/
|
|
mz_ulong minizip_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)
|
|
{
|
|
static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
|
|
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
|
|
mz_uint32 crcu32 = (mz_uint32)crc;
|
|
if (!ptr) return MZ_CRC32_INIT;
|
|
crcu32 = ~crcu32; while (buf_len--) { mz_uint8 b = *ptr++; crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)]; crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)]; }
|
|
return ~crcu32;
|
|
}
|
|
|
|
unsigned crc32(unsigned crc, const void *ptr_tmp, size_t buf_len)
|
|
{
|
|
const unsigned char *ptr = (const unsigned char*)ptr_tmp;
|
|
static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
|
|
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
|
|
mz_uint32 crcu32 = (mz_uint32)crc;
|
|
if (!ptr) return MZ_CRC32_INIT;
|
|
crcu32 = ~crcu32; while (buf_len--) { mz_uint8 b = *ptr++; crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)]; crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)]; }
|
|
return ~crcu32;
|
|
}
|
|
|
|
void mz_free(void *p)
|
|
{
|
|
MZ_FREE(p);
|
|
}
|
|
|
|
#ifndef MINIZ_NO_ZLIB_APIS
|
|
|
|
#define def_alloc_func(opaque, items, size) (MZ_MALLOC(items * size))
|
|
#define def_free_func(opaque, address) MZ_FREE(address)
|
|
#define def_realloc_func(opaque, address, items, size) (MZ_REALLOC(address, items * size))
|
|
|
|
const char *mz_version(void)
|
|
{
|
|
return MZ_VERSION;
|
|
}
|
|
|
|
typedef struct
|
|
{
|
|
tinfl_decompressor m_decomp;
|
|
mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed; int m_window_bits;
|
|
mz_uint8 m_dict[TINFL_LZ_DICT_SIZE];
|
|
tinfl_status m_last_status;
|
|
} inflate_state;
|
|
|
|
int mz_inflateInit2(mz_streamp pStream, int window_bits)
|
|
{
|
|
inflate_state *pDecomp;
|
|
if (!pStream) return MZ_STREAM_ERROR;
|
|
if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)) return MZ_PARAM_ERROR;
|
|
|
|
pStream->data_type = 0;
|
|
pStream->adler = 0;
|
|
pStream->msg = NULL;
|
|
pStream->total_in = 0;
|
|
pStream->total_out = 0;
|
|
pStream->reserved = 0;
|
|
|
|
pDecomp = (inflate_state*)def_alloc_func(pStream->opaque, 1, sizeof(inflate_state));
|
|
if (!pDecomp) return MZ_MEM_ERROR;
|
|
|
|
pStream->state = (struct mz_internal_state *)pDecomp;
|
|
|
|
tinfl_init(&pDecomp->m_decomp);
|
|
pDecomp->m_dict_ofs = 0;
|
|
pDecomp->m_dict_avail = 0;
|
|
pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
|
|
pDecomp->m_first_call = 1;
|
|
pDecomp->m_has_flushed = 0;
|
|
pDecomp->m_window_bits = window_bits;
|
|
|
|
return MZ_OK;
|
|
}
|
|
|
|
int mz_inflateInit(mz_streamp pStream)
|
|
{
|
|
return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS);
|
|
}
|
|
|
|
int mz_inflate(mz_streamp pStream, int flush)
|
|
{
|
|
inflate_state* pState;
|
|
mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32;
|
|
size_t in_bytes, out_bytes, orig_avail_in;
|
|
tinfl_status status;
|
|
|
|
if ((!pStream) || (!pStream->state)) return MZ_STREAM_ERROR;
|
|
if (flush == MZ_PARTIAL_FLUSH) flush = MZ_SYNC_FLUSH;
|
|
if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) return MZ_STREAM_ERROR;
|
|
|
|
pState = (inflate_state*)pStream->state;
|
|
if (pState->m_window_bits > 0) decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER;
|
|
orig_avail_in = pStream->avail_in;
|
|
|
|
first_call = pState->m_first_call; pState->m_first_call = 0;
|
|
if (pState->m_last_status < 0) return MZ_DATA_ERROR;
|
|
|
|
if (pState->m_has_flushed && (flush != MZ_FINISH)) return MZ_STREAM_ERROR;
|
|
pState->m_has_flushed |= (flush == MZ_FINISH);
|
|
|
|
if ((flush == MZ_FINISH) && (first_call))
|
|
{
|
|
// MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file.
|
|
decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
|
|
in_bytes = pStream->avail_in; out_bytes = pStream->avail_out;
|
|
status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags);
|
|
pState->m_last_status = status;
|
|
pStream->next_in += (mz_uint)in_bytes; pStream->avail_in -= (mz_uint)in_bytes; pStream->total_in += (mz_uint)in_bytes;
|
|
pStream->adler = tinfl_get_adler32(&pState->m_decomp);
|
|
pStream->next_out += (mz_uint)out_bytes; pStream->avail_out -= (mz_uint)out_bytes; pStream->total_out += (mz_uint)out_bytes;
|
|
|
|
if (status < 0)
|
|
return MZ_DATA_ERROR;
|
|
else if (status != TINFL_STATUS_DONE)
|
|
{
|
|
pState->m_last_status = TINFL_STATUS_FAILED;
|
|
return MZ_BUF_ERROR;
|
|
}
|
|
return MZ_STREAM_END;
|
|
}
|
|
// flush != MZ_FINISH then we must assume there's more input.
|
|
if (flush != MZ_FINISH) decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT;
|
|
|
|
if (pState->m_dict_avail)
|
|
{
|
|
n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
|
|
memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
|
|
pStream->next_out += n; pStream->avail_out -= n; pStream->total_out += n;
|
|
pState->m_dict_avail -= n; pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
|
|
return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
|
|
}
|
|
|
|
for (;;)
|
|
{
|
|
in_bytes = pStream->avail_in;
|
|
out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs;
|
|
|
|
status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags);
|
|
pState->m_last_status = status;
|
|
|
|
pStream->next_in += (mz_uint)in_bytes; pStream->avail_in -= (mz_uint)in_bytes;
|
|
pStream->total_in += (mz_uint)in_bytes; pStream->adler = tinfl_get_adler32(&pState->m_decomp);
|
|
|
|
pState->m_dict_avail = (mz_uint)out_bytes;
|
|
|
|
n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
|
|
memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
|
|
pStream->next_out += n; pStream->avail_out -= n; pStream->total_out += n;
|
|
pState->m_dict_avail -= n; pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
|
|
|
|
if (status < 0)
|
|
return MZ_DATA_ERROR; // Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well).
|
|
else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in))
|
|
return MZ_BUF_ERROR; // Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH.
|
|
else if (flush == MZ_FINISH)
|
|
{
|
|
// The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH.
|
|
if (status == TINFL_STATUS_DONE)
|
|
return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END;
|
|
// status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's at least 1 more byte on the way. If there's no more room left in the output buffer then something is wrong.
|
|
else if (!pStream->avail_out)
|
|
return MZ_BUF_ERROR;
|
|
}
|
|
else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail))
|
|
break;
|
|
}
|
|
|
|
return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
|
|
}
|
|
|
|
int mz_inflateEnd(mz_streamp pStream)
|
|
{
|
|
if (!pStream)
|
|
return MZ_STREAM_ERROR;
|
|
if (pStream->state)
|
|
{
|
|
def_free_func(pStream->opaque, pStream->state);
|
|
pStream->state = NULL;
|
|
}
|
|
return MZ_OK;
|
|
}
|
|
|
|
int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
|
|
{
|
|
mz_stream stream;
|
|
int status;
|
|
memset(&stream, 0, sizeof(stream));
|
|
|
|
// In case mz_ulong is 64-bits (argh I hate longs).
|
|
if ((source_len | *pDest_len) > 0xFFFFFFFFU) return MZ_PARAM_ERROR;
|
|
|
|
stream.next_in = pSource;
|
|
stream.avail_in = (mz_uint32)source_len;
|
|
stream.next_out = pDest;
|
|
stream.avail_out = (mz_uint32)*pDest_len;
|
|
|
|
status = mz_inflateInit(&stream);
|
|
if (status != MZ_OK)
|
|
return status;
|
|
|
|
status = mz_inflate(&stream, MZ_FINISH);
|
|
if (status != MZ_STREAM_END)
|
|
{
|
|
mz_inflateEnd(&stream);
|
|
return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status;
|
|
}
|
|
*pDest_len = stream.total_out;
|
|
|
|
return mz_inflateEnd(&stream);
|
|
}
|
|
|
|
const char *mz_error(int err)
|
|
{
|
|
static struct { int m_err; const char *m_pDesc; } s_error_descs[] =
|
|
{
|
|
{ MZ_OK, "" }, { MZ_STREAM_END, "stream end" }, { MZ_NEED_DICT, "need dictionary" }, { MZ_ERRNO, "file error" }, { MZ_STREAM_ERROR, "stream error" },
|
|
{ MZ_DATA_ERROR, "data error" }, { MZ_MEM_ERROR, "out of memory" }, { MZ_BUF_ERROR, "buf error" }, { MZ_VERSION_ERROR, "version error" }, { MZ_PARAM_ERROR, "parameter error" }
|
|
};
|
|
mz_uint i; for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i) if (s_error_descs[i].m_err == err) return s_error_descs[i].m_pDesc;
|
|
return NULL;
|
|
}
|
|
|
|
#endif //MINIZ_NO_ZLIB_APIS
|
|
|
|
// ------------------- Low-level Decompression (completely independent from all compression API's)
|
|
|
|
#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
|
|
#define TINFL_MEMSET(p, c, l) memset(p, c, l)
|
|
|
|
#define TINFL_CR_BEGIN switch(r->m_state) { case 0:
|
|
#define TINFL_CR_RETURN(state_index, result) do { status = result; r->m_state = state_index; goto common_exit; case state_index:; } MZ_MACRO_END
|
|
#define TINFL_CR_RETURN_FOREVER(state_index, result) do { for ( ; ; ) { TINFL_CR_RETURN(state_index, result); } } MZ_MACRO_END
|
|
#define TINFL_CR_FINISH }
|
|
|
|
// TODO: If the caller has indicated that there's no more input, and we attempt to read beyond the input buf, then something is wrong with the input because the inflator never
|
|
// reads ahead more than it needs to. Currently TINFL_GET_BYTE() pads the end of the stream with 0's in this scenario.
|
|
#define TINFL_GET_BYTE(state_index, c) do { \
|
|
if (pIn_buf_cur >= pIn_buf_end) { \
|
|
for ( ; ; ) { \
|
|
if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) { \
|
|
TINFL_CR_RETURN(state_index, TINFL_STATUS_NEEDS_MORE_INPUT); \
|
|
if (pIn_buf_cur < pIn_buf_end) { \
|
|
c = *pIn_buf_cur++; \
|
|
break; \
|
|
} \
|
|
} else { \
|
|
c = 0; \
|
|
break; \
|
|
} \
|
|
} \
|
|
} else c = *pIn_buf_cur++; } MZ_MACRO_END
|
|
|
|
#define TINFL_NEED_BITS(state_index, n) do { mz_uint c; TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; } while (num_bits < (mz_uint)(n))
|
|
#define TINFL_SKIP_BITS(state_index, n) do { if (num_bits < (mz_uint)(n)) { TINFL_NEED_BITS(state_index, n); } bit_buf >>= (n); num_bits -= (n); } MZ_MACRO_END
|
|
#define TINFL_GET_BITS(state_index, b, n) do { if (num_bits < (mz_uint)(n)) { TINFL_NEED_BITS(state_index, n); } b = bit_buf & ((1 << (n)) - 1); bit_buf >>= (n); num_bits -= (n); } MZ_MACRO_END
|
|
|
|
// TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2.
|
|
// It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no more). It works by trying to fully decode a
|
|
// Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, and tries again until it succeeds or until the
|
|
// bit buffer contains >=15 bits (deflate's max. Huffman code size).
|
|
#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \
|
|
do { \
|
|
temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
|
|
if (temp >= 0) { \
|
|
code_len = temp >> 9; \
|
|
if ((code_len) && (num_bits >= code_len)) \
|
|
break; \
|
|
} else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \
|
|
code_len = TINFL_FAST_LOOKUP_BITS; \
|
|
do { \
|
|
temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
|
|
} while ((temp < 0) && (num_bits >= (code_len + 1))); if (temp >= 0) break; \
|
|
} TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; \
|
|
} while (num_bits < 15);
|
|
|
|
// TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because the zlib API expects the decompressor to never read
|
|
// beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the input, it REALLY needs another byte in order to fully
|
|
// decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, which aren't followed by a byte aligned adler-32.
|
|
// The slow path is only executed at the very end of the input buffer.
|
|
#define TINFL_HUFF_DECODE(state_index, sym, pHuff) do { \
|
|
int temp; mz_uint code_len, c; \
|
|
if (num_bits < 15) { \
|
|
if ((pIn_buf_end - pIn_buf_cur) < 2) { \
|
|
TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \
|
|
} else { \
|
|
bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); pIn_buf_cur += 2; num_bits += 16; \
|
|
} \
|
|
} \
|
|
if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
|
|
code_len = temp >> 9, temp &= 511; \
|
|
else { \
|
|
code_len = TINFL_FAST_LOOKUP_BITS; do { temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; } while (temp < 0); \
|
|
} sym = temp; bit_buf >>= code_len; num_bits -= code_len; } MZ_MACRO_END
|
|
|
|
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)
|
|
{
|
|
static const int s_length_base[31] = { 3,4,5,6,7,8,9,10,11,13, 15,17,19,23,27,31,35,43,51,59, 67,83,99,115,131,163,195,227,258,0,0 };
|
|
static const int s_length_extra[31]= { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
|
|
static const int s_dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
|
|
static const int s_dist_extra[32] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
|
|
static const mz_uint8 s_length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
|
|
static const int s_min_table_sizes[3] = { 257, 1, 4 };
|
|
|
|
tinfl_status status = TINFL_STATUS_FAILED; mz_uint32 num_bits, dist, counter, num_extra; tinfl_bit_buf_t bit_buf;
|
|
const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
|
|
mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size;
|
|
size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start;
|
|
|
|
// Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter).
|
|
if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) { *pIn_buf_size = *pOut_buf_size = 0; return TINFL_STATUS_BAD_PARAM; }
|
|
|
|
num_bits = r->m_num_bits; bit_buf = r->m_bit_buf; dist = r->m_dist; counter = r->m_counter; num_extra = r->m_num_extra; dist_from_out_buf_start = r->m_dist_from_out_buf_start;
|
|
TINFL_CR_BEGIN
|
|
|
|
bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0; r->m_z_adler32 = r->m_check_adler32 = 1;
|
|
if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
|
|
{
|
|
TINFL_GET_BYTE(1, r->m_zhdr0); TINFL_GET_BYTE(2, r->m_zhdr1);
|
|
counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));
|
|
if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4)))));
|
|
if (counter) { TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED); }
|
|
}
|
|
|
|
do
|
|
{
|
|
TINFL_GET_BITS(3, r->m_final, 3); r->m_type = r->m_final >> 1;
|
|
if (r->m_type == 0)
|
|
{
|
|
TINFL_SKIP_BITS(5, num_bits & 7);
|
|
for (counter = 0; counter < 4; ++counter) { if (num_bits) TINFL_GET_BITS(6, r->m_raw_header[counter], 8); else TINFL_GET_BYTE(7, r->m_raw_header[counter]); }
|
|
if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) { TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED); }
|
|
while ((counter) && (num_bits))
|
|
{
|
|
TINFL_GET_BITS(51, dist, 8);
|
|
while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT); }
|
|
*pOut_buf_cur++ = (mz_uint8)dist;
|
|
counter--;
|
|
}
|
|
while (counter)
|
|
{
|
|
size_t n; while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT); }
|
|
while (pIn_buf_cur >= pIn_buf_end)
|
|
{
|
|
if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT)
|
|
{
|
|
TINFL_CR_RETURN(38, TINFL_STATUS_NEEDS_MORE_INPUT);
|
|
}
|
|
else
|
|
{
|
|
TINFL_CR_RETURN_FOREVER(40, TINFL_STATUS_FAILED);
|
|
}
|
|
}
|
|
n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
|
|
TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n); pIn_buf_cur += n; pOut_buf_cur += n; counter -= (mz_uint)n;
|
|
}
|
|
}
|
|
else if (r->m_type == 3)
|
|
{
|
|
TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
|
|
}
|
|
else
|
|
{
|
|
if (r->m_type == 1)
|
|
{
|
|
mz_uint8 *p = r->m_tables[0].m_code_size; mz_uint i;
|
|
r->m_table_sizes[0] = 288; r->m_table_sizes[1] = 32; TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32);
|
|
for ( i = 0; i <= 143; ++i) *p++ = 8; for ( ; i <= 255; ++i) *p++ = 9; for ( ; i <= 279; ++i) *p++ = 7; for ( ; i <= 287; ++i) *p++ = 8;
|
|
}
|
|
else
|
|
{
|
|
for (counter = 0; counter < 3; counter++) { TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); r->m_table_sizes[counter] += s_min_table_sizes[counter]; }
|
|
MZ_CLEAR_OBJ(r->m_tables[2].m_code_size); for (counter = 0; counter < r->m_table_sizes[2]; counter++) { mz_uint s; TINFL_GET_BITS(14, s, 3); r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s; }
|
|
r->m_table_sizes[2] = 19;
|
|
}
|
|
for ( ; (int)r->m_type >= 0; r->m_type--)
|
|
{
|
|
int tree_next, tree_cur; tinfl_huff_table *pTable;
|
|
mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16]; pTable = &r->m_tables[r->m_type]; MZ_CLEAR_OBJ(total_syms); MZ_CLEAR_OBJ(pTable->m_look_up); MZ_CLEAR_OBJ(pTable->m_tree);
|
|
for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) total_syms[pTable->m_code_size[i]]++;
|
|
used_syms = 0, total = 0; next_code[0] = next_code[1] = 0;
|
|
for (i = 1; i <= 15; ++i) { used_syms += total_syms[i]; next_code[i + 1] = (total = ((total + total_syms[i]) << 1)); }
|
|
if ((65536 != total) && (used_syms > 1))
|
|
{
|
|
TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
|
|
}
|
|
for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)
|
|
{
|
|
mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index]; if (!code_size) continue;
|
|
cur_code = next_code[code_size]++; for (l = code_size; l > 0; l--, cur_code >>= 1) rev_code = (rev_code << 1) | (cur_code & 1);
|
|
if (code_size <= TINFL_FAST_LOOKUP_BITS) { mz_int16 k = (mz_int16)((code_size << 9) | sym_index); while (rev_code < TINFL_FAST_LOOKUP_SIZE) { pTable->m_look_up[rev_code] = k; rev_code += (1 << code_size); } continue; }
|
|
if (0 == (tree_cur = pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) { pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next; tree_cur = tree_next; tree_next -= 2; }
|
|
rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
|
|
for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)
|
|
{
|
|
tree_cur -= ((rev_code >>= 1) & 1);
|
|
if (!pTable->m_tree[-tree_cur - 1]) { pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next; tree_cur = tree_next; tree_next -= 2; } else tree_cur = pTable->m_tree[-tree_cur - 1];
|
|
}
|
|
tree_cur -= ((rev_code >>= 1) & 1); pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index;
|
|
}
|
|
if (r->m_type == 2)
|
|
{
|
|
for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]); )
|
|
{
|
|
mz_uint s; TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]); if (dist < 16) { r->m_len_codes[counter++] = (mz_uint8)dist; continue; }
|
|
if ((dist == 16) && (!counter))
|
|
{
|
|
TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
|
|
}
|
|
num_extra = "\02\03\07"[dist - 16]; TINFL_GET_BITS(18, s, num_extra); s += "\03\03\013"[dist - 16];
|
|
TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s); counter += s;
|
|
}
|
|
if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)
|
|
{
|
|
TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
|
|
}
|
|
TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes, r->m_table_sizes[0]); TINFL_MEMCPY(r->m_tables[1].m_code_size, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
|
|
}
|
|
}
|
|
for ( ; ; )
|
|
{
|
|
mz_uint8 *pSrc;
|
|
for ( ; ; )
|
|
{
|
|
if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
|
|
{
|
|
TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]);
|
|
if (counter >= 256)
|
|
break;
|
|
while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); }
|
|
*pOut_buf_cur++ = (mz_uint8)counter;
|
|
}
|
|
else
|
|
{
|
|
int sym2; mz_uint code_len;
|
|
#if TINFL_USE_64BIT_BITBUF
|
|
if (num_bits < 30) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits); pIn_buf_cur += 4; num_bits += 32; }
|
|
#else
|
|
if (num_bits < 15) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); pIn_buf_cur += 2; num_bits += 16; }
|
|
#endif
|
|
if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
|
|
code_len = sym2 >> 9;
|
|
else
|
|
{
|
|
code_len = TINFL_FAST_LOOKUP_BITS; do { sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; } while (sym2 < 0);
|
|
}
|
|
counter = sym2; bit_buf >>= code_len; num_bits -= code_len;
|
|
if (counter & 256)
|
|
break;
|
|
|
|
#if !TINFL_USE_64BIT_BITBUF
|
|
if (num_bits < 15) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); pIn_buf_cur += 2; num_bits += 16; }
|
|
#endif
|
|
if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
|
|
code_len = sym2 >> 9;
|
|
else
|
|
{
|
|
code_len = TINFL_FAST_LOOKUP_BITS; do { sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; } while (sym2 < 0);
|
|
}
|
|
bit_buf >>= code_len; num_bits -= code_len;
|
|
|
|
pOut_buf_cur[0] = (mz_uint8)counter;
|
|
if (sym2 & 256)
|
|
{
|
|
pOut_buf_cur++;
|
|
counter = sym2;
|
|
break;
|
|
}
|
|
pOut_buf_cur[1] = (mz_uint8)sym2;
|
|
pOut_buf_cur += 2;
|
|
}
|
|
}
|
|
if ((counter &= 511) == 256) break;
|
|
|
|
num_extra = s_length_extra[counter - 257]; counter = s_length_base[counter - 257];
|
|
if (num_extra) { mz_uint extra_bits; TINFL_GET_BITS(25, extra_bits, num_extra); counter += extra_bits; }
|
|
|
|
TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]);
|
|
num_extra = s_dist_extra[dist]; dist = s_dist_base[dist];
|
|
if (num_extra) { mz_uint extra_bits; TINFL_GET_BITS(27, extra_bits, num_extra); dist += extra_bits; }
|
|
|
|
dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
|
|
if ((dist > dist_from_out_buf_start) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
|
|
{
|
|
TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
|
|
}
|
|
|
|
pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
|
|
|
|
if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
|
|
{
|
|
while (counter--)
|
|
{
|
|
while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT); }
|
|
*pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
|
|
}
|
|
continue;
|
|
}
|
|
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
|
|
else if ((counter >= 9) && (counter <= dist))
|
|
{
|
|
const mz_uint8 *pSrc_end = pSrc + (counter & ~7);
|
|
do
|
|
{
|
|
((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0];
|
|
((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1];
|
|
pOut_buf_cur += 8;
|
|
} while ((pSrc += 8) < pSrc_end);
|
|
if ((counter &= 7) < 3)
|
|
{
|
|
if (counter)
|
|
{
|
|
pOut_buf_cur[0] = pSrc[0];
|
|
if (counter > 1)
|
|
pOut_buf_cur[1] = pSrc[1];
|
|
pOut_buf_cur += counter;
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
#endif
|
|
do
|
|
{
|
|
pOut_buf_cur[0] = pSrc[0];
|
|
pOut_buf_cur[1] = pSrc[1];
|
|
pOut_buf_cur[2] = pSrc[2];
|
|
pOut_buf_cur += 3; pSrc += 3;
|
|
} while ((int)(counter -= 3) > 2);
|
|
if ((int)counter > 0)
|
|
{
|
|
pOut_buf_cur[0] = pSrc[0];
|
|
if ((int)counter > 1)
|
|
pOut_buf_cur[1] = pSrc[1];
|
|
pOut_buf_cur += counter;
|
|
}
|
|
}
|
|
}
|
|
} while (!(r->m_final & 1));
|
|
if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
|
|
{
|
|
TINFL_SKIP_BITS(32, num_bits & 7); for (counter = 0; counter < 4; ++counter) { mz_uint s; if (num_bits) TINFL_GET_BITS(41, s, 8); else TINFL_GET_BYTE(42, s); r->m_z_adler32 = (r->m_z_adler32 << 8) | s; }
|
|
}
|
|
TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
|
|
TINFL_CR_FINISH
|
|
|
|
common_exit:
|
|
r->m_num_bits = num_bits; r->m_bit_buf = bit_buf; r->m_dist = dist; r->m_counter = counter; r->m_num_extra = num_extra; r->m_dist_from_out_buf_start = dist_from_out_buf_start;
|
|
*pIn_buf_size = pIn_buf_cur - pIn_buf_next; *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
|
|
if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))
|
|
{
|
|
const mz_uint8 *ptr = pOut_buf_next; size_t buf_len = *pOut_buf_size;
|
|
mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16; size_t block_len = buf_len % 5552;
|
|
while (buf_len)
|
|
{
|
|
for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
|
|
{
|
|
s1 += ptr[0], s2 += s1; s1 += ptr[1], s2 += s1; s1 += ptr[2], s2 += s1; s1 += ptr[3], s2 += s1;
|
|
s1 += ptr[4], s2 += s1; s1 += ptr[5], s2 += s1; s1 += ptr[6], s2 += s1; s1 += ptr[7], s2 += s1;
|
|
}
|
|
for ( ; i < block_len; ++i) s1 += *ptr++, s2 += s1;
|
|
s1 %= 65521U, s2 %= 65521U; buf_len -= block_len; block_len = 5552;
|
|
}
|
|
r->m_check_adler32 = (s2 << 16) + s1; if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32)) status = TINFL_STATUS_ADLER32_MISMATCH;
|
|
}
|
|
return status;
|
|
}
|
|
|
|
#ifdef _MSC_VER
|
|
#pragma warning (push)
|
|
#pragma warning (disable:4204) // nonstandard extension used : non-constant aggregate initializer (also supported by GNU C and C99, so no big deal)
|
|
#endif
|
|
|
|
#ifdef _MSC_VER
|
|
#pragma warning (pop)
|
|
#endif
|
|
|
|
// ------------------- .ZIP archive reading
|
|
|
|
#ifndef MINIZ_NO_ARCHIVE_APIS
|
|
|
|
#define MZ_FILE void *
|
|
|
|
#define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
|
|
|
|
// Various ZIP archive enums. To completely avoid cross platform compiler alignment and platform endian issues, miniz.c doesn't use structs for any of this stuff.
|
|
enum
|
|
{
|
|
// ZIP archive identifiers and record sizes
|
|
MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50, MZ_ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50, MZ_ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50,
|
|
MZ_ZIP_LOCAL_DIR_HEADER_SIZE = 30, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE = 46, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22,
|
|
// Central directory header record offsets
|
|
MZ_ZIP_CDH_SIG_OFS = 0, MZ_ZIP_CDH_VERSION_MADE_BY_OFS = 4, MZ_ZIP_CDH_VERSION_NEEDED_OFS = 6, MZ_ZIP_CDH_BIT_FLAG_OFS = 8,
|
|
MZ_ZIP_CDH_METHOD_OFS = 10, MZ_ZIP_CDH_FILE_TIME_OFS = 12, MZ_ZIP_CDH_FILE_DATE_OFS = 14, MZ_ZIP_CDH_CRC32_OFS = 16,
|
|
MZ_ZIP_CDH_COMPRESSED_SIZE_OFS = 20, MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24, MZ_ZIP_CDH_FILENAME_LEN_OFS = 28, MZ_ZIP_CDH_EXTRA_LEN_OFS = 30,
|
|
MZ_ZIP_CDH_COMMENT_LEN_OFS = 32, MZ_ZIP_CDH_DISK_START_OFS = 34, MZ_ZIP_CDH_INTERNAL_ATTR_OFS = 36, MZ_ZIP_CDH_EXTERNAL_ATTR_OFS = 38, MZ_ZIP_CDH_LOCAL_HEADER_OFS = 42,
|
|
// Local directory header offsets
|
|
MZ_ZIP_LDH_SIG_OFS = 0, MZ_ZIP_LDH_VERSION_NEEDED_OFS = 4, MZ_ZIP_LDH_BIT_FLAG_OFS = 6, MZ_ZIP_LDH_METHOD_OFS = 8, MZ_ZIP_LDH_FILE_TIME_OFS = 10,
|
|
MZ_ZIP_LDH_FILE_DATE_OFS = 12, MZ_ZIP_LDH_CRC32_OFS = 14, MZ_ZIP_LDH_COMPRESSED_SIZE_OFS = 18, MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS = 22,
|
|
MZ_ZIP_LDH_FILENAME_LEN_OFS = 26, MZ_ZIP_LDH_EXTRA_LEN_OFS = 28,
|
|
// End of central directory offsets
|
|
MZ_ZIP_ECDH_SIG_OFS = 0, MZ_ZIP_ECDH_NUM_THIS_DISK_OFS = 4, MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS = 6, MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8,
|
|
MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10, MZ_ZIP_ECDH_CDIR_SIZE_OFS = 12, MZ_ZIP_ECDH_CDIR_OFS_OFS = 16, MZ_ZIP_ECDH_COMMENT_SIZE_OFS = 20,
|
|
};
|
|
|
|
typedef struct
|
|
{
|
|
void *m_p;
|
|
size_t m_size, m_capacity;
|
|
mz_uint m_element_size;
|
|
} mz_zip_array;
|
|
|
|
struct mz_zip_internal_state_tag
|
|
{
|
|
mz_zip_array m_central_dir;
|
|
mz_zip_array m_central_dir_offsets;
|
|
mz_zip_array m_sorted_central_dir_offsets;
|
|
MZ_FILE *m_pFile;
|
|
void *m_pMem;
|
|
size_t m_mem_size;
|
|
size_t m_mem_capacity;
|
|
};
|
|
|
|
#define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size
|
|
#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[index]
|
|
|
|
static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive *pZip, mz_zip_array *pArray)
|
|
{
|
|
def_free_func(pZip->m_pAlloc_opaque, pArray->m_p);
|
|
memset(pArray, 0, sizeof(mz_zip_array));
|
|
}
|
|
|
|
static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive *pZip, mz_zip_array *pArray, size_t min_new_capacity, mz_uint growing)
|
|
{
|
|
void *pNew_p; size_t new_capacity = min_new_capacity; MZ_ASSERT(pArray->m_element_size); if (pArray->m_capacity >= min_new_capacity) return MZ_TRUE;
|
|
if (growing) { new_capacity = MZ_MAX(1, pArray->m_capacity); while (new_capacity < min_new_capacity) new_capacity *= 2; }
|
|
if (NULL == (pNew_p = def_realloc_func(pZip->m_pAlloc_opaque, pArray->m_p, pArray->m_element_size, new_capacity))) return MZ_FALSE;
|
|
pArray->m_p = pNew_p; pArray->m_capacity = new_capacity;
|
|
return MZ_TRUE;
|
|
}
|
|
|
|
static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_size, mz_uint growing)
|
|
{
|
|
if (new_size > pArray->m_capacity) { if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) return MZ_FALSE; }
|
|
pArray->m_size = new_size;
|
|
return MZ_TRUE;
|
|
}
|
|
|
|
static mz_bool mz_zip_reader_init_internal(mz_zip_archive *pZip, mz_uint32 flags)
|
|
{
|
|
(void)flags;
|
|
if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
|
|
return MZ_FALSE;
|
|
|
|
pZip->m_zip_mode = MZ_ZIP_MODE_READING;
|
|
pZip->m_archive_size = 0;
|
|
pZip->m_central_directory_file_ofs = 0;
|
|
pZip->m_total_files = 0;
|
|
|
|
if (NULL == (pZip->m_pState = (mz_zip_internal_state *)def_alloc_func(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))
|
|
return MZ_FALSE;
|
|
memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));
|
|
MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8));
|
|
MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32));
|
|
MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32));
|
|
return MZ_TRUE;
|
|
}
|
|
|
|
static MZ_FORCEINLINE mz_bool mz_zip_reader_filename_less(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, mz_uint r_index)
|
|
{
|
|
const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE;
|
|
const mz_uint8 *pR = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, r_index));
|
|
mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS), r_len = MZ_READ_LE16(pR + MZ_ZIP_CDH_FILENAME_LEN_OFS);
|
|
mz_uint8 l = 0, r = 0;
|
|
pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE; pR += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
|
|
pE = pL + MZ_MIN(l_len, r_len);
|
|
while (pL < pE)
|
|
{
|
|
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))
|
|
break;
|
|
pL++; pR++;
|
|
}
|
|
return (pL == pE) ? (l_len < r_len) : (l < r);
|
|
}
|
|
|
|
#define MZ_SWAP_UINT32(a, b) do { mz_uint32 t = a; a = b; b = t; } MZ_MACRO_END
|
|
|
|
// Heap sort of lowercased filenames, used to help accelerate plain central directory searches by mz_zip_reader_locate_file(). (Could also use qsort(), but it could allocate memory.)
|
|
static void mz_zip_reader_sort_central_dir_offsets_by_filename(mz_zip_archive *pZip)
|
|
{
|
|
mz_zip_internal_state *pState = pZip->m_pState;
|
|
const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets;
|
|
const mz_zip_array *pCentral_dir = &pState->m_central_dir;
|
|
mz_uint32 *pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0);
|
|
const int size = pZip->m_total_files;
|
|
int start = (size - 2) >> 1, end;
|
|
while (start >= 0)
|
|
{
|
|
int child, root = start;
|
|
for ( ; ; )
|
|
{
|
|
if ((child = (root << 1) + 1) >= size)
|
|
break;
|
|
child += (((child + 1) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1])));
|
|
if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))
|
|
break;
|
|
MZ_SWAP_UINT32(pIndices[root], pIndices[child]); root = child;
|
|
}
|
|
start--;
|
|
}
|
|
|
|
end = size - 1;
|
|
while (end > 0)
|
|
{
|
|
int child, root = 0;
|
|
MZ_SWAP_UINT32(pIndices[end], pIndices[0]);
|
|
for ( ; ; )
|
|
{
|
|
if ((child = (root << 1) + 1) >= end)
|
|
break;
|
|
child += (((child + 1) < end) && mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1]));
|
|
if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))
|
|
break;
|
|
MZ_SWAP_UINT32(pIndices[root], pIndices[child]); root = child;
|
|
}
|
|
end--;
|
|
}
|
|
}
|
|
|
|
static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, mz_uint32 flags)
|
|
{
|
|
mz_uint cdir_size, num_this_disk, cdir_disk_index;
|
|
mz_uint64 cdir_ofs;
|
|
mz_int64 cur_file_ofs;
|
|
const mz_uint8 *p;
|
|
mz_uint32 buf_u32[4096 / sizeof(mz_uint32)]; mz_uint8 *pBuf = (mz_uint8 *)buf_u32;
|
|
// Basic sanity checks - reject files which are too small, and check the first 4 bytes of the file to make sure a local header is there.
|
|
if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
|
|
return MZ_FALSE;
|
|
// Find the end of central directory record by scanning the file from the end towards the beginning.
|
|
cur_file_ofs = MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)sizeof(buf_u32), 0);
|
|
for ( ; ; )
|
|
{
|
|
int i, n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs);
|
|
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n)
|
|
return MZ_FALSE;
|
|
for (i = n - 4; i >= 0; --i)
|
|
if (MZ_READ_LE32(pBuf + i) == MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG)
|
|
break;
|
|
if (i >= 0)
|
|
{
|
|
cur_file_ofs += i;
|
|
break;
|
|
}
|
|
if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= (0xFFFF + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)))
|
|
return MZ_FALSE;
|
|
cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0);
|
|
}
|
|
// Read and verify the end of central directory record.
|
|
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
|
|
return MZ_FALSE;
|
|
if ((MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_SIG_OFS) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG) ||
|
|
((pZip->m_total_files = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS)) != MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS)))
|
|
return MZ_FALSE;
|
|
|
|
num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS);
|
|
cdir_disk_index = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS);
|
|
if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1)))
|
|
return MZ_FALSE;
|
|
|
|
if ((cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS)) < pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)
|
|
return MZ_FALSE;
|
|
|
|
cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS);
|
|
if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size)
|
|
return MZ_FALSE;
|
|
|
|
pZip->m_central_directory_file_ofs = cdir_ofs;
|
|
|
|
if (pZip->m_total_files)
|
|
{
|
|
mz_uint i, n;
|
|
// Read the entire central directory into a heap block, and allocate another heap block to hold the unsorted central dir file record offsets, and another to hold the sorted indices.
|
|
if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, MZ_FALSE)) ||
|
|
(!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, pZip->m_total_files, MZ_FALSE)) ||
|
|
(!mz_zip_array_resize(pZip, &pZip->m_pState->m_sorted_central_dir_offsets, pZip->m_total_files, MZ_FALSE)))
|
|
return MZ_FALSE;
|
|
if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, pZip->m_pState->m_central_dir.m_p, cdir_size) != cdir_size)
|
|
return MZ_FALSE;
|
|
|
|
// Now create an index into the central directory file records, do some basic sanity checking on each record, and check for zip64 entries (which are not yet supported).
|
|
p = (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p;
|
|
for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i)
|
|
{
|
|
mz_uint total_header_size, comp_size, decomp_size, disk_index;
|
|
if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG))
|
|
return MZ_FALSE;
|
|
MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, i) = (mz_uint32)(p - (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p);
|
|
MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_sorted_central_dir_offsets, mz_uint32, i) = i;
|
|
comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
|
|
decomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);
|
|
if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size) || (decomp_size == 0xFFFFFFFF) || (comp_size == 0xFFFFFFFF))
|
|
return MZ_FALSE;
|
|
disk_index = MZ_READ_LE16(p + MZ_ZIP_CDH_DISK_START_OFS);
|
|
if ((disk_index != num_this_disk) && (disk_index != 1))
|
|
return MZ_FALSE;
|
|
if (((mz_uint64)MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS) + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > pZip->m_archive_size)
|
|
return MZ_FALSE;
|
|
if ((total_header_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS)) > n)
|
|
return MZ_FALSE;
|
|
n -= total_header_size; p += total_header_size;
|
|
}
|
|
}
|
|
|
|
if ((flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0)
|
|
mz_zip_reader_sort_central_dir_offsets_by_filename(pZip);
|
|
|
|
return MZ_TRUE;
|
|
}
|
|
|
|
mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint32 flags)
|
|
{
|
|
if ((!pZip) || (!pZip->m_pRead))
|
|
return MZ_FALSE;
|
|
if (!mz_zip_reader_init_internal(pZip, flags))
|
|
return MZ_FALSE;
|
|
pZip->m_archive_size = size;
|
|
if (!mz_zip_reader_read_central_dir(pZip, flags))
|
|
{
|
|
mz_zip_reader_end(pZip);
|
|
return MZ_FALSE;
|
|
}
|
|
return MZ_TRUE;
|
|
}
|
|
|
|
mz_bool mz_zip_reader_end(mz_zip_archive *pZip)
|
|
{
|
|
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
|
|
return MZ_FALSE;
|
|
|
|
if (pZip->m_pState)
|
|
{
|
|
mz_zip_internal_state *pState = pZip->m_pState; pZip->m_pState = NULL;
|
|
mz_zip_array_clear(pZip, &pState->m_central_dir);
|
|
mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);
|
|
mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);
|
|
|
|
def_free_func(pZip->m_pAlloc_opaque, pState);
|
|
}
|
|
pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
|
|
|
|
return MZ_TRUE;
|
|
}
|
|
|
|
#endif // #ifndef MINIZ_NO_ARCHIVE_APIS
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|