159 #ifndef MINIZ_HEADER_INCLUDED
160 # define MINIZ_HEADER_INCLUDED
193 # if defined(__TINYC__) && (defined(__linux) || defined(__linux__))
195 # define MINIZ_NO_TIME
198 # if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS)
202 # if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) \
203 || defined(__ia64__) || defined(__x86_64__)
205 # define MINIZ_X86_OR_X64_CPU 1
208 # if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
210 # define MINIZ_LITTLE_ENDIAN 1
213 # if MINIZ_X86_OR_X64_CPU
215 # define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
218 # if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
220 # define MINIZ_HAS_64BIT_REGISTERS 1
236 # define MZ_ADLER32_INIT (1)
240 # define MZ_CRC32_INIT (0)
255 # define MZ_DEFLATED 8
257 # ifndef MINIZ_NO_ZLIB_APIS
261 typedef void* (*mz_alloc_func)(
void* opaque,
size_t items,
size_t size);
263 typedef void* (*mz_realloc_func)(
void* opaque,
void* address,
size_t items,
size_t size);
265 # define MZ_VERSION "9.1.15"
266 # define MZ_VERNUM 0x91F0
267 # define MZ_VER_MAJOR 9
268 # define MZ_VER_MINOR 1
269 # define MZ_VER_REVISION 15
270 # define MZ_VER_SUBREVISION 0
310 # define MZ_DEFAULT_WINDOW_BITS 15
312 struct mz_internal_state;
429 # ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
442 # define Z_NO_FLUSH MZ_NO_FLUSH
443 # define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH
444 # define Z_SYNC_FLUSH MZ_SYNC_FLUSH
445 # define Z_FULL_FLUSH MZ_FULL_FLUSH
446 # define Z_FINISH MZ_FINISH
447 # define Z_BLOCK MZ_BLOCK
449 # define Z_STREAM_END MZ_STREAM_END
450 # define Z_NEED_DICT MZ_NEED_DICT
451 # define Z_ERRNO MZ_ERRNO
452 # define Z_STREAM_ERROR MZ_STREAM_ERROR
453 # define Z_DATA_ERROR MZ_DATA_ERROR
454 # define Z_MEM_ERROR MZ_MEM_ERROR
455 # define Z_BUF_ERROR MZ_BUF_ERROR
456 # define Z_VERSION_ERROR MZ_VERSION_ERROR
457 # define Z_PARAM_ERROR MZ_PARAM_ERROR
458 # define Z_NO_COMPRESSION MZ_NO_COMPRESSION
459 # define Z_BEST_SPEED MZ_BEST_SPEED
460 # define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION
461 # define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION
462 # define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY
463 # define Z_FILTERED MZ_FILTERED
464 # define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY
465 # define Z_RLE MZ_RLE
466 # define Z_FIXED MZ_FIXED
467 # define Z_DEFLATED MZ_DEFLATED
468 # define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS
469 # define alloc_func mz_alloc_func
470 # define free_func mz_free_func
471 # define internal_state mz_internal_state
472 # define z_stream mz_stream
473 # define deflateInit mz_deflateInit
474 # define deflateInit2 mz_deflateInit2
475 # define deflateReset mz_deflateReset
476 # define deflate mz_deflate
477 # define deflateEnd mz_deflateEnd
478 # define deflateBound mz_deflateBound
479 # define compress mz_compress
480 # define compress2 mz_compress2
481 # define compressBound mz_compressBound
482 # define inflateInit mz_inflateInit
483 # define inflateInit2 mz_inflateInit2
484 # define inflate mz_inflate
485 # define inflateEnd mz_inflateEnd
486 # define uncompress mz_uncompress
487 # define crc32 mz_crc32
488 # define adler32 mz_adler32
489 # define MAX_WBITS 15
490 # define MAX_MEM_LEVEL 9
491 # define zError mz_error
492 # define ZLIB_VERSION MZ_VERSION
493 # define ZLIB_VERNUM MZ_VERNUM
494 # define ZLIB_VER_MAJOR MZ_VER_MAJOR
495 # define ZLIB_VER_MINOR MZ_VER_MINOR
496 # define ZLIB_VER_REVISION MZ_VER_REVISION
497 # define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION
498 # define zlibVersion mz_version
499 # define zlib_version mz_version()
515 # define MZ_FALSE (0)
520 # define MZ_MACRO_END while (0, 0)
522 # define MZ_MACRO_END while (0)
527 # ifndef MINIZ_NO_ARCHIVE_APIS
544 # ifndef MINIZ_NO_TIME
609 # ifndef MINIZ_NO_STDIO
634 void* pUser_read_buf,
size_t user_read_buf_size);
636 void* pUser_read_buf,
size_t user_read_buf_size);
651 # ifndef MINIZ_NO_STDIO
663 # ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
669 # ifndef MINIZ_NO_STDIO
688 # ifndef MINIZ_NO_STDIO
751 # define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
764 # define TINFL_LZ_DICT_SIZE 32768
778 # define tinfl_init(r) \
783 # define tinfl_get_adler32(r) (r)->m_check_adler32
807 # if MINIZ_HAS_64BIT_REGISTERS
808 # define TINFL_USE_64BIT_BITBUF 1
811 # if TINFL_USE_64BIT_BITBUF
813 # define TINFL_BITBUF_SIZE (64)
816 # define TINFL_BITBUF_SIZE (32)
821 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,
832 # define TDEFL_LESS_MEMORY 0
877 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);
911 # if TDEFL_LESS_MEMORY
963 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,
1001 # ifndef MINIZ_NO_ZLIB_APIS
1017 #ifndef MINIZ_HEADER_FILE_ONLY
1023 # include <string.h>
1024 # include <assert.h>
1026 # define MZ_ASSERT(x) assert(x)
1028 # ifdef MINIZ_NO_MALLOC
1029 # define MZ_MALLOC(x) NULL
1030 # define MZ_FREE(x) (void)x, ((void)0)
1031 # define MZ_REALLOC(p, x) NULL
1033 # define MZ_MALLOC(x) malloc(x)
1034 # define MZ_FREE(x) free(x)
1035 # define MZ_REALLOC(p, x) realloc(p, x)
1038 # define MZ_MAX(a, b) (((a) > (b)) ? (a) : (b))
1039 # define MZ_MIN(a, b) (((a) < (b)) ? (a) : (b))
1040 # define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
1042 # if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
1043 # define MZ_READ_LE16(p) *((const mz_uint16*)(p))
1044 # define MZ_READ_LE32(p) *((const mz_uint32*)(p))
1046 # define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8*)(p))[0]) | ((mz_uint32)(((const mz_uint8*)(p))[1]) << 8U))
1047 # define MZ_READ_LE32(p) \
1048 ((mz_uint32)(((const mz_uint8*)(p))[0]) | ((mz_uint32)(((const mz_uint8*)(p))[1]) << 8U) \
1049 | ((mz_uint32)(((const mz_uint8*)(p))[2]) << 16U) | ((mz_uint32)(((const mz_uint8*)(p))[3]) << 24U))
1053 # define MZ_FORCEINLINE __forceinline
1054 # elif defined(__GNUC__)
1055 # define MZ_FORCEINLINE inline __attribute__((__always_inline__))
1057 # define MZ_FORCEINLINE inline
1069 size_t block_len = buf_len % 5552;
1072 for (i = 0; i + 7 < block_len; i += 8, ptr += 8) {
1073 s1 += ptr[0], s2 += s1;
1074 s1 += ptr[1], s2 += s1;
1075 s1 += ptr[2], s2 += s1;
1076 s1 += ptr[3], s2 += s1;
1077 s1 += ptr[4], s2 += s1;
1078 s1 += ptr[5], s2 += s1;
1079 s1 += ptr[6], s2 += s1;
1080 s1 += ptr[7], s2 += s1;
1082 for (; i < block_len; ++i)
1083 s1 += *ptr++, s2 += s1;
1084 s1 %= 65521U, s2 %= 65521U;
1085 buf_len -= block_len;
1088 return (s2 << 16) + s1;
1093 static const mz_uint32 s_crc32[16] = {0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1094 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c};
1100 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)];
1101 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)];
1110 # ifndef MINIZ_NO_ZLIB_APIS
1112 static void* def_alloc_func(
void* opaque,
size_t items,
size_t size) {
1113 (void)opaque, (
void)items, (void)size;
1116 static void def_free_func(
void* opaque,
void* address) {
1117 (void)opaque, (
void)address;
1120 static void* def_realloc_func(
void* opaque,
void* address,
size_t items,
size_t size) {
1121 (void)opaque, (
void)address, (void)items, (
void)size;
1138 if ((method !=
MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9))
1144 pStream->
msg = NULL;
1148 if (!pStream->
zalloc) pStream->
zalloc = def_alloc_func;
1149 if (!pStream->
zfree) pStream->
zfree = def_free_func;
1154 pStream->
state = (
struct mz_internal_state*)pComp;
1172 size_t in_bytes, out_bytes;
1173 mz_ulong orig_total_in, orig_total_out;
1174 int mz_status =
MZ_OK;
1202 if (defl_status < 0) {
1211 if ((flush) || (pStream->
total_in != orig_total_in) || (pStream->
total_out != orig_total_out))
break;
1220 if (pStream->
state) {
1222 pStream->
state = NULL;
1230 return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
1236 memset(&stream, 0,
sizeof(stream));
1239 if ((source_len | *pDest_len) > 0xFFFFFFFFU)
return MZ_PARAM_ERROR;
1247 if (status !=
MZ_OK)
return status;
1283 pStream->
msg = NULL;
1287 if (!pStream->
zalloc) pStream->
zalloc = def_alloc_func;
1288 if (!pStream->
zfree) pStream->
zfree = def_free_func;
1293 pStream->
state = (
struct mz_internal_state*)pDecomp;
1313 size_t in_bytes, out_bytes, orig_avail_in;
1331 if ((flush ==
MZ_FINISH) && (first_call)) {
1410 if (pStream->
state) {
1412 pStream->
state = NULL;
1420 memset(&stream, 0,
sizeof(stream));
1423 if ((source_len | *pDest_len) > 0xFFFFFFFFU)
return MZ_PARAM_ERROR;
1431 if (status !=
MZ_OK)
return status;
1447 const char* m_pDesc;
1448 } s_error_descs[] = {{
MZ_OK,
""},
1459 for (i = 0; i <
sizeof(s_error_descs) /
sizeof(s_error_descs[0]); ++i)
1460 if (s_error_descs[i].m_err == err)
return s_error_descs[i].m_pDesc;
1468 # define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
1469 # define TINFL_MEMSET(p, c, l) memset(p, c, l)
1471 # define TINFL_CR_BEGIN \
1472 switch (r->m_state) { \
1474 # define TINFL_CR_RETURN(state_index, result) \
1477 r->m_state = state_index; \
1479 case state_index:; \
1482 # define TINFL_CR_RETURN_FOREVER(state_index, result) \
1485 TINFL_CR_RETURN(state_index, result); \
1489 # define TINFL_CR_FINISH }
1493 # define TINFL_GET_BYTE(state_index, c) \
1495 if (pIn_buf_cur >= pIn_buf_end) { \
1497 if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) { \
1498 TINFL_CR_RETURN(state_index, TINFL_STATUS_NEEDS_MORE_INPUT); \
1499 if (pIn_buf_cur < pIn_buf_end) { \
1500 c = *pIn_buf_cur++; \
1509 c = *pIn_buf_cur++; \
1513 # define TINFL_NEED_BITS(state_index, n) \
1516 TINFL_GET_BYTE(state_index, c); \
1517 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
1519 } while (num_bits < (mz_uint)(n))
1520 # define TINFL_SKIP_BITS(state_index, n) \
1522 if (num_bits < (mz_uint)(n)) { \
1523 TINFL_NEED_BITS(state_index, n); \
1529 # define TINFL_GET_BITS(state_index, b, n) \
1531 if (num_bits < (mz_uint)(n)) { \
1532 TINFL_NEED_BITS(state_index, n); \
1534 b = bit_buf & ((1 << (n)) - 1); \
1544 # define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \
1546 temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
1548 code_len = temp >> 9; \
1549 if ((code_len) && (num_bits >= code_len)) break; \
1550 } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \
1551 code_len = TINFL_FAST_LOOKUP_BITS; \
1553 temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
1554 } while ((temp < 0) && (num_bits >= (code_len + 1))); \
1555 if (temp >= 0) break; \
1557 TINFL_GET_BYTE(state_index, c); \
1558 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
1560 } while (num_bits < 15);
1566 # define TINFL_HUFF_DECODE(state_index, sym, pHuff) \
1569 mz_uint code_len, c; \
1570 if (num_bits < 15) { \
1571 if ((pIn_buf_end - pIn_buf_cur) < 2) { \
1572 TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \
1574 bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
1579 if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
1580 code_len = temp >> 9, temp &= 511; \
1582 code_len = TINFL_FAST_LOOKUP_BITS; \
1584 temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
1585 } while (temp < 0); \
1588 bit_buf >>= code_len; \
1589 num_bits -= code_len; \
1595 static const int s_length_base[31] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1596 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
1597 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};
1598 static const int s_dist_base[32] = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1599 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0};
1600 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};
1601 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};
1602 static const int s_min_table_sizes[3] = {257, 1, 4};
1605 mz_uint32 num_bits, dist, counter, num_extra;
1607 const mz_uint8 *pIn_buf_cur = pIn_buf_next, *
const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
1608 mz_uint8 *pOut_buf_cur = pOut_buf_next, *
const pOut_buf_end = pOut_buf_next + *pOut_buf_size;
1609 size_t out_buf_size_mask =
1611 dist_from_out_buf_start;
1614 if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) {
1615 *pIn_buf_size = *pOut_buf_size = 0;
1627 bit_buf = num_bits = dist = counter = num_extra = r->
m_zhdr0 = r->
m_zhdr1 = 0;
1634 counter |= (((1U << (8U + (r->
m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->
m_zhdr0 >> 4)))));
1645 for (counter = 0; counter < 4; ++counter) {
1655 while ((counter) && (num_bits)) {
1657 while (pOut_buf_cur >= pOut_buf_end) {
1665 while (pOut_buf_cur >= pOut_buf_end) {
1668 while (pIn_buf_cur >= pIn_buf_end) {
1675 n =
MZ_MIN(
MZ_MIN((
size_t)(pOut_buf_end - pOut_buf_cur), (
size_t)(pIn_buf_end - pIn_buf_cur)), counter);
1681 }
else if (r->
m_type == 3) {
1690 for (i = 0; i <= 143; ++i)
1692 for (; i <= 255; ++i)
1694 for (; i <= 279; ++i)
1696 for (; i <= 287; ++i)
1699 for (counter = 0; counter < 3; counter++) {
1704 for (counter = 0; counter < r->
m_table_sizes[2]; counter++) {
1712 int tree_next, tree_cur;
1714 mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];
1721 used_syms = 0, total = 0;
1722 next_code[0] = next_code[1] = 0;
1723 for (i = 1; i <= 15; ++i) {
1724 used_syms += total_syms[i];
1725 next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
1727 if ((65536 != total) && (used_syms > 1)) {
1730 for (tree_next = -1, sym_index = 0; sym_index < r->
m_table_sizes[r->
m_type]; ++sym_index) {
1732 if (!code_size)
continue;
1733 cur_code = next_code[code_size]++;
1734 for (l = code_size; l > 0; l--, cur_code >>= 1)
1735 rev_code = (rev_code << 1) | (cur_code & 1);
1740 rev_code += (1 << code_size);
1746 tree_cur = tree_next;
1751 tree_cur -= ((rev_code >>= 1) & 1);
1752 if (!pTable->
m_tree[-tree_cur - 1]) {
1754 tree_cur = tree_next;
1757 tree_cur = pTable->
m_tree[-tree_cur - 1];
1759 tree_cur -= ((rev_code >>= 1) & 1);
1770 if ((dist == 16) && (!counter)) {
1773 num_extra =
"\02\03\07"[dist - 16];
1775 s +=
"\03\03\013"[dist - 16];
1789 if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) {
1791 if (counter >= 256)
break;
1792 while (pOut_buf_cur >= pOut_buf_end) {
1795 *pOut_buf_cur++ = (
mz_uint8)counter;
1799 # if TINFL_USE_64BIT_BITBUF
1800 if (num_bits < 30) {
1806 if (num_bits < 15) {
1813 code_len = sym2 >> 9;
1817 sym2 = r->
m_tables[0].
m_tree[~sym2 + ((bit_buf >> code_len++) & 1)];
1821 bit_buf >>= code_len;
1822 num_bits -= code_len;
1823 if (counter & 256)
break;
1825 # if !TINFL_USE_64BIT_BITBUF
1826 if (num_bits < 15) {
1833 code_len = sym2 >> 9;
1837 sym2 = r->
m_tables[0].
m_tree[~sym2 + ((bit_buf >> code_len++) & 1)];
1840 bit_buf >>= code_len;
1841 num_bits -= code_len;
1843 pOut_buf_cur[0] = (
mz_uint8)counter;
1853 if ((counter &= 511) == 256)
break;
1855 num_extra = s_length_extra[counter - 257];
1856 counter = s_length_base[counter - 257];
1860 counter += extra_bits;
1864 num_extra = s_dist_extra[dist];
1865 dist = s_dist_base[dist];
1872 dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
1877 pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
1879 if ((
MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) {
1881 while (pOut_buf_cur >= pOut_buf_end) {
1884 *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
1888 # if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
1889 else if ((counter >= 9) && (counter <= dist)) {
1890 const mz_uint8* pSrc_end = pSrc + (counter & ~7);
1895 }
while ((pSrc += 8) < pSrc_end);
1896 if ((counter &= 7) < 3) {
1898 pOut_buf_cur[0] = pSrc[0];
1899 if (counter > 1) pOut_buf_cur[1] = pSrc[1];
1900 pOut_buf_cur += counter;
1907 pOut_buf_cur[0] = pSrc[0];
1908 pOut_buf_cur[1] = pSrc[1];
1909 pOut_buf_cur[2] = pSrc[2];
1912 }
while ((
int)(counter -= 3) > 2);
1913 if ((
int)counter > 0) {
1914 pOut_buf_cur[0] = pSrc[0];
1915 if ((
int)counter > 1) pOut_buf_cur[1] = pSrc[1];
1916 pOut_buf_cur += counter;
1923 for (counter = 0; counter < 4; ++counter) {
1942 *pIn_buf_size = pIn_buf_cur - pIn_buf_next;
1943 *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
1945 const mz_uint8* ptr = pOut_buf_next;
1946 size_t buf_len = *pOut_buf_size;
1948 size_t block_len = buf_len % 5552;
1950 for (i = 0; i + 7 < block_len; i += 8, ptr += 8) {
1951 s1 += ptr[0], s2 += s1;
1952 s1 += ptr[1], s2 += s1;
1953 s1 += ptr[2], s2 += s1;
1954 s1 += ptr[3], s2 += s1;
1955 s1 += ptr[4], s2 += s1;
1956 s1 += ptr[5], s2 += s1;
1957 s1 += ptr[6], s2 += s1;
1958 s1 += ptr[7], s2 += s1;
1960 for (; i < block_len; ++i)
1961 s1 += *ptr++, s2 += s1;
1962 s1 %= 65521U, s2 %= 65521U;
1963 buf_len -= block_len;
1976 void *pBuf = NULL, *pNew_buf;
1977 size_t src_buf_ofs = 0, out_buf_capacity = 0;
1981 size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity;
1983 pBuf ? (
mz_uint8*)pBuf + *pOut_len : NULL, &dst_buf_size,
1990 src_buf_ofs += src_buf_size;
1991 *pOut_len += dst_buf_size;
1993 new_out_buf_capacity = out_buf_capacity * 2;
1994 if (new_out_buf_capacity < 128) new_out_buf_capacity = 128;
1995 pNew_buf =
MZ_REALLOC(pBuf, new_out_buf_capacity);
2002 out_buf_capacity = new_out_buf_capacity;
2021 size_t in_buf_ofs = 0, dict_ofs = 0;
2025 size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size =
TINFL_LZ_DICT_SIZE - dict_ofs;
2028 in_buf_ofs += in_buf_size;
2029 if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (
int)dst_buf_size, pPut_buf_user)))
break;
2037 *pIn_buf_size = in_buf_ofs;
2044 static const mz_uint16 s_tdefl_len_sym[256] = {
2045 257, 258, 259, 260, 261, 262, 263, 264, 265, 265, 266, 266, 267, 267, 268, 268, 269, 269, 269, 269, 270, 270, 270, 270, 271, 271, 271, 271, 272,
2046 272, 272, 272, 273, 273, 273, 273, 273, 273, 273, 273, 274, 274, 274, 274, 274, 274, 274, 274, 275, 275, 275, 275, 275, 275, 275, 275, 276, 276,
2047 276, 276, 276, 276, 276, 276, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 278, 278, 278, 278, 278, 278, 278,
2048 278, 278, 278, 278, 278, 278, 278, 278, 278, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 280, 280, 280, 280,
2049 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281,
2050 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282,
2051 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283,
2052 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 284, 284, 284, 284, 284, 284, 284, 284,
2053 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 285};
2055 static const mz_uint8 s_tdefl_len_extra[256] = {
2056 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2057 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
2058 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5,
2059 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
2060 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
2061 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0};
2063 static const mz_uint8 s_tdefl_small_dist_sym[512] = {
2064 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10,
2065 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12,
2066 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13,
2067 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
2068 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
2069 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
2070 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
2071 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
2072 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
2073 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
2074 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17,
2075 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
2076 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
2077 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
2078 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17};
2080 static const mz_uint8 s_tdefl_small_dist_extra[512] = {
2081 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
2082 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
2083 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
2084 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
2085 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
2086 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
2087 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
2088 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
2089 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
2090 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
2091 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
2093 static const mz_uint8 s_tdefl_large_dist_sym[128] = {
2094 0, 0, 18, 19, 20, 20, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25,
2095 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
2096 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
2097 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29};
2099 static const mz_uint8 s_tdefl_large_dist_extra[128] = {
2100 0, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
2101 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
2102 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
2103 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13};
2111 mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];
2114 for (i = 0; i < num_syms; i++) {
2116 hist[freq & 0xFF]++;
2117 hist[256 + ((freq >> 8) & 0xFF)]++;
2119 while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))
2121 for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8) {
2122 const mz_uint32* pHist = &hist[pass << 8];
2123 mz_uint offsets[256], cur_ofs = 0;
2124 for (i = 0; i < 256; i++) {
2125 offsets[i] = cur_ofs;
2126 cur_ofs += pHist[i];
2128 for (i = 0; i < num_syms; i++)
2129 pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];
2132 pCur_syms = pNew_syms;
2140 static void tdefl_calculate_minimum_redundancy(
tdefl_sym_freq* A,
int n) {
2141 int root, leaf, next, avbl, used, dpth;
2151 for (next = 1; next < n - 1; next++) {
2152 if (leaf >= n || A[root].m_key < A[leaf].m_key) {
2157 if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) {
2164 for (next = n - 3; next >= 0; next--)
2165 A[next].m_key = A[A[next].m_key].m_key + 1;
2171 while (root >= 0 && (
int)A[root].
m_key == dpth) {
2175 while (avbl > used) {
2190 static void tdefl_huffman_enforce_max_code_size(
int* pNum_codes,
int code_list_len,
int max_code_size) {
2193 if (code_list_len <= 1)
return;
2195 pNum_codes[max_code_size] += pNum_codes[i];
2196 for (i = max_code_size; i > 0; i--)
2197 total += (((
mz_uint32)pNum_codes[i]) << (max_code_size - i));
2198 while (total != (1UL << max_code_size)) {
2199 pNum_codes[max_code_size]--;
2200 for (i = max_code_size - 1; i > 0; i--)
2201 if (pNum_codes[i]) {
2203 pNum_codes[i + 1] += 2;
2210 static void tdefl_optimize_huffman_table(
tdefl_compressor* d,
int table_num,
int table_len,
int code_size_limit,
int static_table) {
2215 for (i = 0; i < table_len; i++)
2219 int num_used_syms = 0;
2221 for (i = 0; i < table_len; i++)
2222 if (pSym_count[i]) {
2227 pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1);
2228 tdefl_calculate_minimum_redundancy(pSyms, num_used_syms);
2230 for (i = 0; i < num_used_syms; i++)
2231 num_codes[pSyms[i].m_key]++;
2233 tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit);
2237 for (i = 1, j = num_used_syms; i <= code_size_limit; i++)
2238 for (l = num_codes[i]; l > 0; l--)
2243 for (j = 0, i = 2; i <= code_size_limit; i++)
2244 next_code[i] = j = ((j + num_codes[i - 1]) << 1);
2246 for (i = 0; i < table_len; i++) {
2247 mz_uint rev_code = 0, code, code_size;
2249 code = next_code[code_size]++;
2250 for (l = code_size; l > 0; l--, code >>= 1)
2251 rev_code = (rev_code << 1) | (code & 1);
2256 # define TDEFL_PUT_BITS(b, l) \
2260 MZ_ASSERT(bits <= ((1U << len) - 1U)); \
2261 d->m_bit_buffer |= (bits << d->m_bits_in); \
2262 d->m_bits_in += len; \
2263 while (d->m_bits_in >= 8) { \
2264 if (d->m_pOutput_buf < d->m_pOutput_buf_end) *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \
2265 d->m_bit_buffer >>= 8; \
2266 d->m_bits_in -= 8; \
2271 # define TDEFL_RLE_PREV_CODE_SIZE() \
2273 if (rle_repeat_count) { \
2274 if (rle_repeat_count < 3) { \
2275 d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \
2276 while (rle_repeat_count--) \
2277 packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \
2279 d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \
2280 packed_code_sizes[num_packed_code_sizes++] = 16; \
2281 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \
2283 rle_repeat_count = 0; \
2287 # define TDEFL_RLE_ZERO_CODE_SIZE() \
2289 if (rle_z_count) { \
2290 if (rle_z_count < 3) { \
2291 d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \
2292 while (rle_z_count--) \
2293 packed_code_sizes[num_packed_code_sizes++] = 0; \
2294 } else if (rle_z_count <= 10) { \
2295 d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \
2296 packed_code_sizes[num_packed_code_sizes++] = 17; \
2297 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \
2299 d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \
2300 packed_code_sizes[num_packed_code_sizes++] = 18; \
2301 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \
2307 static mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
2310 int num_lit_codes, num_dist_codes, num_bit_lengths;
2311 mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index;
2320 for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--)
2322 for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--)
2326 memcpy(code_sizes_to_pack + num_lit_codes, &d->
m_huff_code_sizes[1][0], num_dist_codes);
2327 total_code_sizes_to_pack = num_lit_codes + num_dist_codes;
2328 num_packed_code_sizes = 0;
2330 rle_repeat_count = 0;
2333 for (i = 0; i < total_code_sizes_to_pack; i++) {
2334 mz_uint8 code_size = code_sizes_to_pack[i];
2337 if (++rle_z_count == 138) {
2342 if (code_size != prev_code_size) {
2345 packed_code_sizes[num_packed_code_sizes++] = code_size;
2346 }
else if (++rle_repeat_count == 6) {
2350 prev_code_size = code_size;
2352 if (rle_repeat_count) {
2365 for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)
2366 if (d->
m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]])
break;
2367 num_bit_lengths =
MZ_MAX(4, (num_bit_lengths + 1));
2369 for (i = 0; (int)i < num_bit_lengths; i++)
2372 for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;) {
2373 mz_uint code = packed_code_sizes[packed_code_sizes_index++];
2376 if (code >= 16)
TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++],
"\02\03\07"[code - 16]);
2384 for (i = 0; i <= 143; ++i)
2386 for (; i <= 255; ++i)
2388 for (; i <= 279; ++i)
2390 for (; i <= 287; ++i)
2395 tdefl_optimize_huffman_table(d, 0, 288, 15,
MZ_TRUE);
2396 tdefl_optimize_huffman_table(d, 1, 32, 15,
MZ_TRUE);
2401 static const mz_uint mz_bitmasks[17] = {0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF,
2402 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF};
2404 # if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS
2413 # define TDEFL_PUT_BITS_FAST(b, l) \
2415 bit_buffer |= (((mz_uint64)(b)) << bits_in); \
2420 for (pLZ_codes = d->
m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1) {
2421 if (flags == 1) flags = *pLZ_codes++ | 0x100;
2424 mz_uint s0, s1, n0, n1, sym, num_extra_bits;
2425 mz_uint match_len = pLZ_codes[0], match_dist = *(
const mz_uint16*)(pLZ_codes + 1);
2430 TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
2433 s0 = s_tdefl_small_dist_sym[match_dist & 511];
2434 n0 = s_tdefl_small_dist_extra[match_dist & 511];
2435 s1 = s_tdefl_large_dist_sym[match_dist >> 8];
2436 n1 = s_tdefl_large_dist_extra[match_dist >> 8];
2437 sym = (match_dist < 512) ? s0 : s1;
2438 num_extra_bits = (match_dist < 512) ? n0 : n1;
2442 TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
2448 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) {
2454 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) {
2466 pOutput_buf += (bits_in >> 3);
2467 bit_buffer >>= (bits_in & ~7);
2471 # undef TDEFL_PUT_BITS_FAST
2494 for (pLZ_codes = d->
m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1) {
2495 if (flags == 1) flags = *pLZ_codes++ | 0x100;
2498 mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
2503 TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
2505 if (match_dist < 512) {
2506 sym = s_tdefl_small_dist_sym[match_dist];
2507 num_extra_bits = s_tdefl_small_dist_extra[match_dist];
2509 sym = s_tdefl_large_dist_sym[match_dist >> 8];
2510 num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8];
2514 TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
2530 tdefl_start_static_block(d);
2532 tdefl_start_dynamic_block(d);
2533 return tdefl_compress_lz_codes(d);
2537 mz_uint saved_bit_buf, saved_bits_in;
2587 else if (!comp_block_succeeded) {
2590 tdefl_compress_block(d,
MZ_TRUE);
2600 for (i = 0; i < 4; i++) {
2611 for (i = 2; i; --i, z ^= 0xFFFF) {
2629 if ((n = (
int)(d->
m_pOutput_buf - pOutput_buf_start)) != 0) {
2637 if ((n -= bytes_to_copy) != 0) {
2649 # if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
2650 # define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16*)(p)
2656 mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->
m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD(s);
2658 if (max_match_len <= match_len)
return;
2661 if (--num_probes_left == 0)
return;
2662 # define TDEFL_PROBE \
2663 next_probe_pos = d->m_next[probe_pos]; \
2664 if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) return; \
2665 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
2666 if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) break;
2673 if (TDEFL_READ_UNALIGNED_WORD(q) != s01)
continue;
2677 }
while ((TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q))
2678 && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q))
2679 && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q))
2680 && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0));
2682 *pMatch_dist = dist;
2686 *pMatch_dist = dist;
2687 if ((*pMatch_len = match_len =
MZ_MIN(max_match_len, probe_len)) == max_match_len)
break;
2688 c01 = TDEFL_READ_UNALIGNED_WORD(&d->
m_dict[pos + match_len - 1]);
2700 if (max_match_len <= match_len)
return;
2703 if (--num_probes_left == 0)
return;
2704 # define TDEFL_PROBE \
2705 next_probe_pos = d->m_next[probe_pos]; \
2706 if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) return; \
2707 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
2708 if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) break;
2715 q = d->
m_dict + probe_pos;
2716 for (probe_len = 0; probe_len < max_match_len; probe_len++)
2717 if (*p++ != *q++)
break;
2718 if (probe_len > match_len) {
2719 *pMatch_dist = dist;
2720 if ((*pMatch_len = match_len = probe_len) == max_match_len)
return;
2721 c0 = d->
m_dict[pos + match_len];
2722 c1 = d->
m_dict[pos + match_len - 1];
2728 # if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
2737 const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096;
2741 lookahead_size += num_bytes_to_process;
2743 while (num_bytes_to_process) {
2750 num_bytes_to_process -= n;
2754 if ((!d->
m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE))
break;
2756 while (lookahead_size >= 4) {
2757 mz_uint cur_match_dist, cur_match_len = 1;
2764 if (((cur_match_dist = (
mz_uint16)(lookahead_pos - probe_pos)) <= dict_size)
2770 }
while ((TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q))
2771 && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q))
2772 && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q))
2773 && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0));
2779 *pLZ_code_buf++ = (
mz_uint8)first_trigram;
2780 *pLZ_flags = (
mz_uint8)(*pLZ_flags >> 1);
2784 cur_match_len =
MZ_MIN(cur_match_len, lookahead_size);
2793 *pLZ_flags = (
mz_uint8)((*pLZ_flags >> 1) | 0x80);
2795 s0 = s_tdefl_small_dist_sym[cur_match_dist & 511];
2796 s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8];
2797 d->
m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++;
2802 *pLZ_code_buf++ = (
mz_uint8)first_trigram;
2803 *pLZ_flags = (
mz_uint8)(*pLZ_flags >> 1);
2807 if (--num_flags_left == 0) {
2809 pLZ_flags = pLZ_code_buf++;
2812 total_lz_bytes += cur_match_len;
2813 lookahead_pos += cur_match_len;
2816 MZ_ASSERT(lookahead_size >= cur_match_len);
2817 lookahead_size -= cur_match_len;
2828 if ((n = tdefl_flush_block(d, 0)) != 0)
return (n < 0) ?
MZ_FALSE :
MZ_TRUE;
2836 while (lookahead_size) {
2840 *pLZ_code_buf++ = lit;
2841 *pLZ_flags = (
mz_uint8)(*pLZ_flags >> 1);
2842 if (--num_flags_left == 0) {
2844 pLZ_flags = pLZ_code_buf++;
2863 if ((n = tdefl_flush_block(d, 0)) != 0)
return (n < 0) ?
MZ_FALSE :
MZ_TRUE;
2914 s0 = s_tdefl_small_dist_sym[match_dist & 511];
2915 s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127];
2927 mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;
2935 const mz_uint8* pSrc_end = pSrc + num_bytes_to_process;
2936 src_buf_left -= num_bytes_to_process;
2938 while (pSrc != pSrc_end) {
2977 while (cur_match_len < d->m_lookahead_size) {
2978 if (d->
m_dict[cur_pos + cur_match_len] != c)
break;
2989 if (((cur_match_len ==
TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist)
2991 cur_match_dist = cur_match_len = 0;
2996 if (cur_match_len >= 128) {
2997 tdefl_record_match(d, cur_match_len, cur_match_dist);
2999 len_to_move = cur_match_len;
3010 }
else if (!cur_match_dist)
3013 tdefl_record_match(d, cur_match_len, cur_match_dist);
3014 len_to_move = cur_match_len;
3033 if ((n = tdefl_flush_block(d, 0)) != 0)
return (n < 0) ?
MZ_FALSE :
MZ_TRUE;
3063 if (pIn_buf_size) *pIn_buf_size = 0;
3064 if (pOut_buf_size) *pOut_buf_size = 0;
3079 || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) {
3080 if (pIn_buf_size) *pIn_buf_size = 0;
3081 if (pOut_buf_size) *pOut_buf_size = 0;
3088 # if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
3116 return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush);
3125 d->
m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3;
3161 if (((buf_len) && (!pBuf)) || (!pPut_buf_func))
return MZ_FALSE;
3177 static mz_bool tdefl_output_buffer_putter(
const void* pBuf,
int len,
void* pUser) {
3179 size_t new_size = p->
m_size + len;
3185 new_capacity =
MZ_MAX(128U, new_capacity << 1U);
3186 }
while (new_size > new_capacity);
3206 *pOut_len = out_buf.
m_size;
3213 if (!pOut_buf)
return 0;
3220 # ifndef MINIZ_NO_ZLIB_APIS
3221 static const mz_uint s_tdefl_num_probes[11] = {0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500};
3236 else if (strategy ==
MZ_RLE)
3244 # pragma warning(push)
3254 static const mz_uint s_tdefl_png_num_probes[11] = {0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500};
3257 int i, bpl = w * num_chans, y, z;
3260 if (!pComp)
return NULL;
3269 for (z = 41; z; --z)
3270 tdefl_output_buffer_putter(&z, 1, &out_buf);
3273 for (y = 0; y < h; ++y) {
3283 *pLen_out = out_buf.
m_size - 41;
3285 static const mz_uint8 chans[] = {0x00, 0x00, 0x04, 0x02, 0x06};
3328 for (i = 0; i < 4; ++i, c <<= 8)
3330 memcpy(out_buf.
m_pBuf, pnghdr, 41);
3333 if (!tdefl_output_buffer_putter(
"\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf)) {
3340 for (i = 0; i < 4; ++i, c <<= 8)
3353 # pragma warning(pop)
3358 # ifndef MINIZ_NO_ARCHIVE_APIS
3360 # ifdef MINIZ_NO_STDIO
3361 # define MZ_FILE void*
3364 # include <sys/stat.h>
3366 # if defined(_MSC_VER)
3367 static FILE* mz_fopen(
const char* pFilename,
const char* pMode) {
3369 fopen_s(&pFile, pFilename, pMode);
3372 static FILE* mz_freopen(
const char* pPath,
const char* pMode, FILE* pStream) {
3374 if (freopen_s(&pFile, pPath, pMode, pStream))
return NULL;
3377 # ifndef MINIZ_NO_TIME
3378 # include <sys/utime.h>
3380 # define MZ_FILE FILE
3381 # define MZ_FOPEN mz_fopen
3382 # define MZ_FCLOSE fclose
3383 # define MZ_FREAD fread
3384 # define MZ_FWRITE fwrite
3385 # define MZ_FTELL64 _ftelli64
3386 # define MZ_FSEEK64 _fseeki64
3387 # define MZ_FILE_STAT_STRUCT _stat
3388 # define MZ_FILE_STAT _stat
3389 # define MZ_FFLUSH fflush
3390 # define MZ_FREOPEN mz_freopen
3391 # define MZ_DELETE_FILE remove
3392 # elif defined(__MINGW32__)
3393 # ifndef MINIZ_NO_TIME
3394 # include <sys/utime.h>
3396 # define MZ_FILE FILE
3397 # define MZ_FOPEN(f, m) fopen(f, m)
3398 # define MZ_FCLOSE fclose
3399 # define MZ_FREAD fread
3400 # define MZ_FWRITE fwrite
3401 # define MZ_FTELL64 ftello64
3402 # define MZ_FSEEK64 fseeko64
3403 # define MZ_FILE_STAT_STRUCT _stat
3404 # define MZ_FILE_STAT _stat
3405 # define MZ_FFLUSH fflush
3406 # define MZ_FREOPEN(f, m, s) freopen(f, m, s)
3407 # define MZ_DELETE_FILE remove
3408 # elif defined(__TINYC__)
3409 # ifndef MINIZ_NO_TIME
3410 # include <sys/utime.h>
3412 # define MZ_FILE FILE
3413 # define MZ_FOPEN(f, m) fopen(f, m)
3414 # define MZ_FCLOSE fclose
3415 # define MZ_FREAD fread
3416 # define MZ_FWRITE fwrite
3417 # define MZ_FTELL64 ftell
3418 # define MZ_FSEEK64 fseek
3419 # define MZ_FILE_STAT_STRUCT stat
3420 # define MZ_FILE_STAT stat
3421 # define MZ_FFLUSH fflush
3422 # define MZ_FREOPEN(f, m, s) freopen(f, m, s)
3423 # define MZ_DELETE_FILE remove
3424 # elif defined(__GNUC__) && _LARGEFILE64_SOURCE
3425 # ifndef MINIZ_NO_TIME
3428 # define MZ_FILE FILE
3429 # define MZ_FOPEN(f, m) fopen64(f, m)
3430 # define MZ_FCLOSE fclose
3431 # define MZ_FREAD fread
3432 # define MZ_FWRITE fwrite
3433 # define MZ_FTELL64 ftello64
3434 # define MZ_FSEEK64 fseeko64
3435 # define MZ_FILE_STAT_STRUCT stat64
3436 # define MZ_FILE_STAT stat64
3437 # define MZ_FFLUSH fflush
3438 # define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
3439 # define MZ_DELETE_FILE remove
3441 # ifndef MINIZ_NO_TIME
3444 # define MZ_FILE FILE
3445 # define MZ_FOPEN(f, m) fopen(f, m)
3446 # define MZ_FCLOSE fclose
3447 # define MZ_FREAD fread
3448 # define MZ_FWRITE fwrite
3449 # define MZ_FTELL64 ftello
3450 # define MZ_FSEEK64 fseeko
3451 # define MZ_FILE_STAT_STRUCT stat
3452 # define MZ_FILE_STAT stat
3453 # define MZ_FFLUSH fflush
3454 # define MZ_FREOPEN(f, m, s) freopen(f, m, s)
3455 # define MZ_DELETE_FILE remove
3459 # define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
3530 # define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size
3531 # define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type*)((array_ptr)->m_p))[index]
3540 size_t new_capacity = min_new_capacity;
3545 while (new_capacity < min_new_capacity)
3549 pArray->
m_p = pNew_p;
3556 if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing))
return MZ_FALSE;
3563 if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing))
return MZ_FALSE;
3565 pArray->
m_size = new_size;
3570 return mz_zip_array_reserve(pZip, pArray, pArray->
m_size + n,
MZ_TRUE);
3574 size_t orig_size = pArray->
m_size;
3575 if (!mz_zip_array_resize(pZip, pArray, orig_size + n,
MZ_TRUE))
return MZ_FALSE;
3580 # ifndef MINIZ_NO_TIME
3581 static time_t mz_zip_dos_to_time_t(
int dos_time,
int dos_date) {
3583 memset(&tm, 0,
sizeof(tm));
3585 tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900;
3586 tm.tm_mon = ((dos_date >> 5) & 15) - 1;
3587 tm.tm_mday = dos_date & 31;
3588 tm.tm_hour = (dos_time >> 11) & 31;
3589 tm.tm_min = (dos_time >> 5) & 63;
3590 tm.tm_sec = (dos_time << 1) & 62;
3594 static void mz_zip_time_to_dos_time(time_t time,
mz_uint16* pDOS_time,
mz_uint16* pDOS_date) {
3596 struct tm tm_struct;
3597 struct tm* tm = &tm_struct;
3598 errno_t err = localtime_s(tm, &time);
3605 struct tm* tm = localtime(&time);
3607 *pDOS_time = (
mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1));
3608 *pDOS_date = (
mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday);
3612 # ifndef MINIZ_NO_STDIO
3614 # ifdef MINIZ_NO_TIME
3616 *pDOS_date = *pDOS_time = 0;
3621 mz_zip_time_to_dos_time(file_stat.st_mtime, pDOS_time, pDOS_date);
3626 # ifndef MINIZ_NO_TIME
3627 static mz_bool mz_zip_set_file_times(
const char* pFilename, time_t access_time, time_t modified_time) {
3629 t.actime = access_time;
3630 t.modtime = modified_time;
3631 return !utime(pFilename, &t);
3666 pE = pL +
MZ_MIN(l_len, r_len);
3672 return (pL == pE) ? (l_len < r_len) : (l < r);
3675 # define MZ_SWAP_UINT32(a, b) \
3684 static void mz_zip_reader_sort_central_dir_offsets_by_filename(
mz_zip_archive* pZip) {
3690 int start = (size - 2) >> 1, end;
3691 while (start >= 0) {
3692 int child, root = start;
3694 if ((child = (root << 1) + 1) >= size)
break;
3696 (((child + 1) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1])));
3697 if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))
break;
3706 int child, root = 0;
3709 if ((child = (root << 1) + 1) >= end)
break;
3711 (((child + 1) < end) && mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1]));
3712 if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))
break;
3721 mz_uint cdir_size, num_this_disk, cdir_disk_index;
3735 for (i = n - 4; i >= 0; --i)
3742 cur_file_ofs =
MZ_MAX(cur_file_ofs - (
sizeof(buf_u32) - 3), 0);
3754 if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1)))
return MZ_FALSE;
3771 if (sort_central_dir) {
3780 mz_uint total_header_size, comp_size, decomp_size, disk_index;
3788 || (decomp_size == 0xFFFFFFFF) || (comp_size == 0xFFFFFFFF))
3791 if ((disk_index != num_this_disk) && (disk_index != 1))
return MZ_FALSE;
3798 n -= total_header_size;
3799 p += total_header_size;
3803 if (sort_central_dir) mz_zip_reader_sort_central_dir_offsets_by_filename(pZip);
3810 if (!mz_zip_reader_init_internal(pZip, flags))
return MZ_FALSE;
3812 if (!mz_zip_reader_read_central_dir(pZip, flags)) {
3819 static size_t mz_zip_mem_read_func(
void* pOpaque,
mz_uint64 file_ofs,
void* pBuf,
size_t n) {
3827 if (!mz_zip_reader_init_internal(pZip, flags))
return MZ_FALSE;
3829 pZip->
m_pRead = mz_zip_mem_read_func;
3837 if (!mz_zip_reader_read_central_dir(pZip, flags)) {
3844 # ifndef MINIZ_NO_STDIO
3845 static size_t mz_zip_file_read_func(
void* pOpaque,
mz_uint64 file_ofs,
void* pBuf,
size_t n) {
3862 if (!mz_zip_reader_init_internal(pZip, flags)) {
3866 pZip->
m_pRead = mz_zip_file_read_func;
3870 if (!mz_zip_reader_read_central_dir(pZip, flags)) {
3890 const mz_uint8* p = mz_zip_reader_get_cdh(pZip, file_index);
3893 return (m_bit_flag & 1);
3897 mz_uint filename_len, external_attr;
3898 const mz_uint8* p = mz_zip_reader_get_cdh(pZip, file_index);
3911 if ((external_attr & 0x10) != 0)
return MZ_TRUE;
3918 const mz_uint8* p = mz_zip_reader_get_cdh(pZip, file_index);
3919 if ((!p) || (!pStat))
return MZ_FALSE;
3928 # ifndef MINIZ_NO_TIME
3956 const mz_uint8* p = mz_zip_reader_get_cdh(pZip, file_index);
3958 if (filename_buf_size) pFilename[0] =
'\0';
3962 if (filename_buf_size) {
3963 n =
MZ_MIN(n, filename_buf_size - 1);
3965 pFilename[n] =
'\0';
3973 for (i = 0; i < len; ++i)
3984 pE = pL +
MZ_MIN(l_len, r_len);
3990 return (pL == pE) ? (int)(l_len - r_len) : (l - r);
3993 static int mz_zip_reader_locate_file_binary_search(
mz_zip_archive* pZip,
const char* pFilename) {
4000 int l = 0, h = size - 1;
4002 int m = (l + h) >> 1, file_index = pIndices[m],
4003 comp = mz_zip_reader_filename_compare(pCentral_dir, pCentral_dir_offsets, file_index, pFilename, filename_len);
4016 size_t name_len, comment_len;
4020 return mz_zip_reader_locate_file_binary_search(pZip, pName);
4021 name_len = strlen(pName);
4022 if (name_len > 0xFFFF)
return -1;
4023 comment_len = pComment ? strlen(pComment) : 0;
4024 if (comment_len > 0xFFFF)
return -1;
4025 for (file_index = 0; file_index < pZip->
m_total_files; file_index++) {
4030 if (filename_len < name_len)
continue;
4034 const char* pFile_comment = pFilename + filename_len + file_extra_len;
4035 if ((file_comment_len != comment_len) || (!mz_zip_reader_string_equal(pComment, pFile_comment, file_comment_len, flags)))
continue;
4038 int ofs = filename_len - 1;
4040 if ((pFilename[ofs] ==
'/') || (pFilename[ofs] ==
'\\') || (pFilename[ofs] ==
':'))
break;
4041 }
while (--ofs >= 0);
4044 filename_len -= ofs;
4046 if ((filename_len == name_len) && (mz_zip_reader_string_equal(pName, pFilename, filename_len, flags)))
return file_index;
4052 void* pUser_read_buf,
size_t user_read_buf_size) {
4054 mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail;
4061 if ((buf_size) && (!pBuf))
return MZ_FALSE;
4080 if (buf_size < needed_size)
return MZ_FALSE;
4105 read_buf_size = read_buf_avail = file_stat.
m_comp_size;
4107 }
else if (pUser_read_buf) {
4109 if (!user_read_buf_size)
return MZ_FALSE;
4110 pRead_buf = (
mz_uint8*)pUser_read_buf;
4111 read_buf_size = user_read_buf_size;
4118 if (((0,
sizeof(
size_t) ==
sizeof(
mz_uint32))) && (read_buf_size > 0x7FFFFFFF))
4120 if (((
sizeof(
size_t) ==
sizeof(
mz_uint32))) && (read_buf_size > 0x7FFFFFFF))
4129 size_t in_buf_size, out_buf_size = (size_t)(file_stat.
m_uncomp_size - out_buf_ofs);
4131 read_buf_avail =
MZ_MIN(read_buf_size, comp_remaining);
4132 if (pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail) {
4136 cur_file_ofs += read_buf_avail;
4137 comp_remaining -= read_buf_avail;
4140 in_buf_size = (size_t)read_buf_avail;
4143 read_buf_avail -= in_buf_size;
4144 read_buf_ofs += in_buf_size;
4145 out_buf_ofs += out_buf_size;
4161 void* pUser_read_buf,
size_t user_read_buf_size) {
4163 if (file_index < 0)
return MZ_FALSE;
4176 mz_uint64 comp_size, uncomp_size, alloc_size;
4177 const mz_uint8* p = mz_zip_reader_get_cdh(pZip, file_index);
4180 if (pSize) *pSize = 0;
4181 if (!p)
return NULL;
4188 if (((0,
sizeof(
size_t) ==
sizeof(
mz_uint32))) && (alloc_size > 0x7FFFFFFF))
4190 if (((
sizeof(
size_t) ==
sizeof(
mz_uint32))) && (alloc_size > 0x7FFFFFFF))
4200 if (pSize) *pSize = (size_t)alloc_size;
4206 if (file_index < 0) {
4207 if (pSize) *pSize = 0;
4216 mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, out_buf_ofs = 0, cur_file_ofs;
4218 void* pRead_buf = NULL;
4219 void* pWrite_buf = NULL;
4251 read_buf_size = read_buf_avail = file_stat.
m_comp_size;
4277 while (comp_remaining) {
4278 read_buf_avail =
MZ_MIN(read_buf_size, comp_remaining);
4279 if (pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail) {
4287 if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail) {
4291 cur_file_ofs += read_buf_avail;
4292 out_buf_ofs += read_buf_avail;
4293 comp_remaining -= read_buf_avail;
4307 read_buf_avail =
MZ_MIN(read_buf_size, comp_remaining);
4308 if (pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail) {
4312 cur_file_ofs += read_buf_avail;
4313 comp_remaining -= read_buf_avail;
4317 in_buf_size = (size_t)read_buf_avail;
4320 read_buf_avail -= in_buf_size;
4321 read_buf_ofs += in_buf_size;
4324 if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size) {
4329 if ((out_buf_ofs += out_buf_size) > file_stat.
m_uncomp_size) {
4352 if (file_index < 0)
return MZ_FALSE;
4356 # ifndef MINIZ_NO_STDIO
4357 static size_t mz_zip_file_write_callback(
void* pOpaque,
mz_uint64 ofs,
const void* pBuf,
size_t n) {
4367 pFile =
MZ_FOPEN(pDst_filename,
"wb");
4371 # ifndef MINIZ_NO_TIME
4372 if (status) mz_zip_set_file_times(pDst_filename, file_stat.
m_time, file_stat.
m_time);
4388 # ifndef MINIZ_NO_STDIO
4402 # ifndef MINIZ_NO_STDIO
4405 if (file_index < 0)
return MZ_FALSE;
4412 # ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
4424 # define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8*)(p), (mz_uint16)(v))
4425 # define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8*)(p), (mz_uint32)(v))
4453 static size_t mz_zip_heap_write_func(
void* pOpaque,
mz_uint64 file_ofs,
const void* pBuf,
size_t n) {
4458 if ((!n) || ((0,
sizeof(
size_t) ==
sizeof(
mz_uint32)) && (new_size > 0x7FFFFFFF)))
4460 if ((!n) || ((
sizeof(
size_t) ==
sizeof(
mz_uint32)) && (new_size > 0x7FFFFFFF)))
4466 while (new_capacity < new_size)
4469 pState->
m_pMem = pNew_block;
4478 pZip->
m_pWrite = mz_zip_heap_write_func;
4481 if (0 != (initial_allocation_size =
MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning))) {
4491 # ifndef MINIZ_NO_STDIO
4492 static size_t mz_zip_file_write_func(
void* pOpaque,
mz_uint64 file_ofs,
const void* pBuf,
size_t n) {
4502 pZip->
m_pWrite = mz_zip_file_write_func;
4505 if (NULL == (pFile =
MZ_FOPEN(pFilename,
"wb"))) {
4510 if (size_to_reserve_at_beginning) {
4515 size_t n = (size_t)
MZ_MIN(
sizeof(buf), size_to_reserve_at_beginning);
4521 size_to_reserve_at_beginning -= n;
4522 }
while (size_to_reserve_at_beginning);
4538 # ifdef MINIZ_NO_STDIO
4545 pZip->
m_pWrite = mz_zip_file_write_func;
4552 }
else if (pState->
m_pMem) {
4556 pZip->
m_pWrite = mz_zip_heap_write_func;
4581 static mz_bool mz_zip_writer_add_put_buf_callback(
const void* pBuf,
int len,
void* pUser) {
4641 if ((local_header_ofs > 0xFFFFFFFF)
4645 if (!mz_zip_writer_create_central_dir_header(pZip, central_dir_header, filename_size, extra_size, comment_size, uncomp_size, comp_size,
4646 uncomp_crc32, method, bit_flags, dos_time, dos_date, local_header_ofs, ext_attributes))
4650 || (!mz_zip_array_push_back(pZip, &pState->
m_central_dir, pFilename, filename_size))
4651 || (!mz_zip_array_push_back(pZip, &pState->
m_central_dir, pExtra, extra_size))
4652 || (!mz_zip_array_push_back(pZip, &pState->
m_central_dir, pComment, comment_size))
4662 static mz_bool mz_zip_writer_validate_archive_name(
const char* pArchive_name) {
4664 if (*pArchive_name ==
'/')
return MZ_FALSE;
4665 while (*pArchive_name) {
4666 if ((*pArchive_name ==
'\\') || (*pArchive_name ==
':'))
return MZ_FALSE;
4681 memset(buf, 0,
MZ_MIN(
sizeof(buf), n));
4693 mz_uint16 method = 0, dos_time = 0, dos_date = 0;
4694 mz_uint level, ext_attributes = 0, num_alignment_padding_bytes;
4696 size_t archive_name_size;
4699 mz_bool store_data_uncompressed;
4703 level = level_and_flags & 0xF;
4714 if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF))
return MZ_FALSE;
4715 if (!mz_zip_writer_validate_archive_name(pArchive_name))
return MZ_FALSE;
4717 # ifndef MINIZ_NO_TIME
4721 mz_zip_time_to_dos_time(cur_time, &dos_time, &dos_date);
4725 archive_name_size = strlen(pArchive_name);
4726 if (archive_name_size > 0xFFFF)
return MZ_FALSE;
4728 num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
4733 + archive_name_size)
4737 if ((archive_name_size) && (pArchive_name[archive_name_size - 1] ==
'/')) {
4739 ext_attributes |= 0x10;
4741 if ((buf_size) || (uncomp_size))
return MZ_FALSE;
4749 if ((!store_data_uncompressed) && (buf_size)) {
4753 if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes +
sizeof(local_dir_header))) {
4757 local_dir_header_ofs += num_alignment_padding_bytes;
4761 cur_archive_file_ofs += num_alignment_padding_bytes +
sizeof(local_dir_header);
4764 if (pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) {
4768 cur_archive_file_ofs += archive_name_size;
4772 uncomp_size = buf_size;
4773 if (uncomp_size <= 3) {
4775 store_data_uncompressed =
MZ_TRUE;
4779 if (store_data_uncompressed) {
4785 cur_archive_file_ofs += buf_size;
4786 comp_size = buf_size;
4789 }
else if (buf_size) {
4796 if ((
tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state,
4814 if ((comp_size > 0xFFFFFFFF) || (cur_archive_file_ofs > 0xFFFFFFFF))
return MZ_FALSE;
4816 if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (
mz_uint16)archive_name_size, 0, uncomp_size, comp_size, uncomp_crc32,
4817 method, 0, dos_time, dos_date))
4820 if (pZip->
m_pWrite(pZip->
m_pIO_opaque, local_dir_header_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
4823 if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (
mz_uint16)archive_name_size, NULL, 0, pComment, comment_size, uncomp_size,
4824 comp_size, uncomp_crc32, method, 0, dos_time, dos_date, local_dir_header_ofs, ext_attributes))
4833 # ifndef MINIZ_NO_STDIO
4837 mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0;
4839 size_t archive_name_size;
4844 level = level_and_flags & 0xF;
4850 if (!mz_zip_writer_validate_archive_name(pArchive_name))
return MZ_FALSE;
4852 archive_name_size = strlen(pArchive_name);
4853 if (archive_name_size > 0xFFFF)
return MZ_FALSE;
4855 num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
4860 + archive_name_size)
4864 if (!mz_zip_get_file_modified_time(pSrc_filename, &dos_time, &dos_date))
return MZ_FALSE;
4866 pSrc_file =
MZ_FOPEN(pSrc_filename,
"rb");
4872 if (uncomp_size > 0xFFFFFFFF) {
4877 if (uncomp_size <= 3) level = 0;
4879 if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes +
sizeof(local_dir_header))) {
4883 local_dir_header_ofs += num_alignment_padding_bytes;
4887 cur_archive_file_ofs += num_alignment_padding_bytes +
sizeof(local_dir_header);
4890 if (pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) {
4894 cur_archive_file_ofs += archive_name_size;
4897 mz_uint64 uncomp_remaining = uncomp_size;
4905 while (uncomp_remaining) {
4907 if ((
MZ_FREAD(pRead_buf, 1, n, pSrc_file) != n)
4914 uncomp_remaining -= n;
4915 cur_archive_file_ofs += n;
4917 comp_size = uncomp_size;
4932 if (
tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state,
4945 if (
MZ_FREAD(pRead_buf, 1, in_buf_size, pSrc_file) != in_buf_size)
break;
4948 uncomp_remaining -= in_buf_size;
4979 if ((comp_size > 0xFFFFFFFF) || (cur_archive_file_ofs > 0xFFFFFFFF))
return MZ_FALSE;
4981 if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (
mz_uint16)archive_name_size, 0, uncomp_size, comp_size, uncomp_crc32,
4982 method, 0, dos_time, dos_date))
4985 if (pZip->
m_pWrite(pZip->
m_pIO_opaque, local_dir_header_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
4988 if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (
mz_uint16)archive_name_size, NULL, 0, pComment, comment_size, uncomp_size,
4989 comp_size, uncomp_crc32, method, 0, dos_time, dos_date, local_dir_header_ofs, ext_attributes))
5000 mz_uint n, bit_flags, num_alignment_padding_bytes;
5001 mz_uint64 comp_bytes_remaining, local_dir_header_ofs;
5002 mz_uint64 cur_src_file_ofs, cur_dst_file_ofs;
5006 size_t orig_central_dir_size;
5009 const mz_uint8* pSrc_central_header;
5012 if (NULL == (pSrc_central_header = mz_zip_reader_get_cdh(pSource_zip, file_index)))
return MZ_FALSE;
5015 num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
5031 if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs, num_alignment_padding_bytes))
return MZ_FALSE;
5032 cur_dst_file_ofs += num_alignment_padding_bytes;
5033 local_dir_header_ofs = cur_dst_file_ofs;
5050 while (comp_bytes_remaining) {
5056 cur_src_file_ofs += n;
5062 cur_dst_file_ofs += n;
5064 comp_bytes_remaining -= n;
5068 if (bit_flags & 8) {
5081 cur_src_file_ofs += n;
5082 cur_dst_file_ofs += n;
5087 if (cur_dst_file_ofs > 0xFFFFFFFF)
return MZ_FALSE;
5117 mz_uint64 central_dir_ofs, central_dir_size;
5129 central_dir_ofs = 0;
5130 central_dir_size = 0;
5150 # ifndef MINIZ_NO_STDIO
5185 # ifndef MINIZ_NO_STDIO
5192 if ((pZip->
m_pWrite == mz_zip_heap_write_func) && (pState->
m_pMem)) {
5202 # ifndef MINIZ_NO_STDIO
5210 if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment))
5213 if (!mz_zip_writer_validate_archive_name(pArchive_name))
return MZ_FALSE;
5217 created_new_archive =
MZ_TRUE;
5226 status =
mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, 0, 0);
5230 if ((!status) && (created_new_archive)) {
5233 (void)ignoredStatus;
5243 if (pSize) *pSize = 0;
5245 if ((!pZip_filename) || (!pArchive_name))
return NULL;