9 #pragma GCC system_header
145 #ifndef MINIZ_HEADER_INCLUDED
146 #define MINIZ_HEADER_INCLUDED
150 #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_APIS)
182 #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || defined(__i386) || defined(__i486__) || defined(__i486) || defined(i386) || defined(__ia64__) || defined(__x86_64__)
184 #define MINIZ_X86_OR_X64_CPU 1
187 #if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
189 #define MINIZ_LITTLE_ENDIAN 1
192 #if MINIZ_X86_OR_X64_CPU
194 #define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
197 #if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
199 #define MINIZ_HAS_64BIT_REGISTERS 1
213 typedef void *(*mz_alloc_func)(
void *opaque,
size_t items,
size_t size);
215 typedef void *(*mz_realloc_func)(
void *opaque,
void *address,
size_t items,
size_t size);
217 #define MZ_ADLER32_INIT (1)
219 mz_ulong
mz_adler32(mz_ulong adler,
const unsigned char *ptr,
size_t buf_len);
221 #define MZ_CRC32_INIT (0)
223 mz_ulong
mz_crc32(mz_ulong crc,
const unsigned char *ptr,
size_t buf_len);
229 #define MZ_DEFLATED 8
231 #ifndef MINIZ_NO_ZLIB_APIS
233 #define MZ_VERSION "9.1.14"
234 #define MZ_VERNUM 0x91E0
235 #define MZ_VER_MAJOR 9
236 #define MZ_VER_MINOR 1
237 #define MZ_VER_REVISION 14
238 #define MZ_VER_SUBREVISION 0
250 #define MZ_DEFAULT_WINDOW_BITS 15
252 struct mz_internal_state;
300 int mz_deflateInit2(mz_streamp pStream,
int level,
int method,
int window_bits,
int mem_level,
int strategy);
315 int mz_deflate(mz_streamp pStream,
int flush);
328 int mz_compress(
unsigned char *pDest, mz_ulong *pDest_len,
const unsigned char *pSource, mz_ulong source_len);
329 int mz_compress2(
unsigned char *pDest, mz_ulong *pDest_len,
const unsigned char *pSource, mz_ulong source_len,
int level);
355 int mz_inflate(mz_streamp pStream,
int flush);
362 int mz_uncompress(
unsigned char *pDest, mz_ulong *pDest_len,
const unsigned char *pSource, mz_ulong source_len);
369 #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
382 #define Z_NO_FLUSH MZ_NO_FLUSH
383 #define Z_PARTIAL_FLUSH MZ_PARTIAL_FLUSH
384 #define Z_SYNC_FLUSH MZ_SYNC_FLUSH
385 #define Z_FULL_FLUSH MZ_FULL_FLUSH
386 #define Z_FINISH MZ_FINISH
387 #define Z_BLOCK MZ_BLOCK
389 #define Z_STREAM_END MZ_STREAM_END
390 #define Z_NEED_DICT MZ_NEED_DICT
391 #define Z_ERRNO MZ_ERRNO
392 #define Z_STREAM_ERROR MZ_STREAM_ERROR
393 #define Z_DATA_ERROR MZ_DATA_ERROR
394 #define Z_MEM_ERROR MZ_MEM_ERROR
395 #define Z_BUF_ERROR MZ_BUF_ERROR
396 #define Z_VERSION_ERROR MZ_VERSION_ERROR
397 #define Z_PARAM_ERROR MZ_PARAM_ERROR
398 #define Z_NO_COMPRESSION MZ_NO_COMPRESSION
399 #define Z_BEST_SPEED MZ_BEST_SPEED
400 #define Z_BEST_COMPRESSION MZ_BEST_COMPRESSION
401 #define Z_DEFAULT_COMPRESSION MZ_DEFAULT_COMPRESSION
402 #define Z_DEFAULT_STRATEGY MZ_DEFAULT_STRATEGY
403 #define Z_FILTERED MZ_FILTERED
404 #define Z_HUFFMAN_ONLY MZ_HUFFMAN_ONLY
406 #define Z_FIXED MZ_FIXED
407 #define Z_DEFLATED MZ_DEFLATED
408 #define Z_DEFAULT_WINDOW_BITS MZ_DEFAULT_WINDOW_BITS
409 #define alloc_func mz_alloc_func
410 #define free_func mz_free_func
411 #define internal_state mz_internal_state
412 #define z_stream mz_stream
413 #define deflateInit mz_deflateInit
414 #define deflateInit2 mz_deflateInit2
415 #define deflateReset mz_deflateReset
416 #define deflate mz_deflate
417 #define deflateEnd mz_deflateEnd
418 #define deflateBound mz_deflateBound
419 #define compress mz_compress
420 #define compress2 mz_compress2
421 #define compressBound mz_compressBound
422 #define inflateInit mz_inflateInit
423 #define inflateInit2 mz_inflateInit2
424 #define inflate mz_inflate
425 #define inflateEnd mz_inflateEnd
426 #define uncompress mz_uncompress
427 #define crc32 mz_crc32
428 #define adler32 mz_adler32
430 #define MAX_MEM_LEVEL 9
431 #define zError mz_error
432 #define ZLIB_VERSION MZ_VERSION
433 #define ZLIB_VERNUM MZ_VERNUM
434 #define ZLIB_VER_MAJOR MZ_VER_MAJOR
435 #define ZLIB_VER_MINOR MZ_VER_MINOR
436 #define ZLIB_VER_REVISION MZ_VER_REVISION
437 #define ZLIB_VER_SUBREVISION MZ_VER_SUBREVISION
438 #define zlibVersion mz_version
439 #define zlib_version mz_version()
440 #endif // #ifndef MINIZ_NO_ZLIB_COMPATIBLE_NAMES
442 #endif // MINIZ_NO_ZLIB_APIS
460 #define MZ_MACRO_END while (0, 0)
462 #define MZ_MACRO_END while (0)
467 #ifndef MINIZ_NO_ARCHIVE_APIS
484 #ifndef MINIZ_NO_TIME
549 #ifndef MINIZ_NO_STDIO
588 #ifndef MINIZ_NO_STDIO
600 #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
606 #ifndef MINIZ_NO_STDIO
622 mz_bool
mz_zip_writer_add_mem_ex(
mz_zip_archive *pZip,
const char *pArchive_name,
const void *pBuf,
size_t buf_size,
const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32);
624 #ifndef MINIZ_NO_STDIO
627 mz_bool
mz_zip_writer_add_file(
mz_zip_archive *pZip,
const char *pArchive_name,
const char *pSrc_filename,
const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags);
648 mz_bool
mz_zip_add_mem_to_archive_file_in_place(
const char *pZip_filename,
const char *pArchive_name,
const void *pBuf,
size_t buf_size,
const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags);
654 #endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
656 #endif // #ifndef MINIZ_NO_ARCHIVE_APIS
685 #define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
696 #define TINFL_LZ_DICT_SIZE 32768
710 #define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END
711 #define tinfl_get_adler32(r) (r)->m_check_adler32
730 #if MINIZ_HAS_64BIT_REGISTERS
731 #define TINFL_USE_64BIT_BITBUF 1
734 #if TINFL_USE_64BIT_BITBUF
736 #define TINFL_BITBUF_SIZE (64)
739 #define TINFL_BITBUF_SIZE (32)
744 mz_uint32
m_state,
m_num_bits,
m_zhdr0,
m_zhdr1,
m_z_adler32,
m_final,
m_type,
m_check_adler32,
m_dist,
m_counter,
m_num_extra,
m_table_sizes[
TINFL_MAX_HUFF_TABLES];
754 #define TDEFL_LESS_MEMORY 0
796 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);
816 #if TDEFL_LESS_MEMORY
845 mz_uint m_flags, m_max_probes[2];
849 mz_uint m_num_flags_left,
m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer;
850 mz_uint m_saved_match_dist, m_saved_match_len, m_saved_lit, m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index,
m_wants_to_finish;
886 #ifndef MINIZ_NO_ZLIB_APIS
892 #endif // #ifndef MINIZ_NO_ZLIB_APIS
898 #endif // MINIZ_HEADER_INCLUDED
902 #ifndef MINIZ_HEADER_FILE_ONLY
911 #define MZ_ASSERT(x) assert(x)
913 #ifdef MINIZ_NO_MALLOC
914 #define MZ_MALLOC(x) NULL
915 #define MZ_FREE(x) (void)x, ((void)0)
916 #define MZ_REALLOC(p, x) NULL
918 #define MZ_MALLOC(x) malloc(x)
919 #define MZ_FREE(x) free(x)
920 #define MZ_REALLOC(p, x) realloc(p, x)
923 #define MZ_MAX(a,b) (((a)>(b))?(a):(b))
924 #define MZ_MIN(a,b) (((a)<(b))?(a):(b))
925 #define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
927 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
928 #define MZ_READ_LE16(p) *((const mz_uint16 *)(p))
929 #define MZ_READ_LE32(p) *((const mz_uint32 *)(p))
931 #define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U))
932 #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))
936 #define MZ_FORCEINLINE __forceinline
937 #elif defined(__GNUC__)
938 #define MZ_FORCEINLINE __attribute__((__always_inline__))
940 #define MZ_FORCEINLINE inline
949 mz_ulong
mz_adler32(mz_ulong adler,
const unsigned char *ptr,
size_t buf_len)
951 mz_uint32 i, s1 = (
mz_uint32)(adler & 0xffff), s2 = (
mz_uint32)(adler >> 16);
size_t block_len = buf_len % 5552;
954 for (i = 0; i + 7 < block_len; i += 8, ptr += 8) {
955 s1 += ptr[0], s2 += s1; s1 += ptr[1], s2 += s1; s1 += ptr[2], s2 += s1; s1 += ptr[3], s2 += s1;
956 s1 += ptr[4], s2 += s1; s1 += ptr[5], s2 += s1; s1 += ptr[6], s2 += s1; s1 += ptr[7], s2 += s1;
958 for ( ; i < block_len; ++i) s1 += *ptr++, s2 += s1;
959 s1 %= 65521U, s2 %= 65521U; buf_len -= block_len; block_len = 5552;
961 return (s2 << 16) + s1;
965 mz_ulong
mz_crc32(mz_ulong crc,
const mz_uint8 *ptr,
size_t buf_len)
967 static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
968 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
971 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)]; }
975 #ifndef MINIZ_NO_ZLIB_APIS
977 static void *
def_alloc_func(
void *opaque,
size_t items,
size_t size) { (void)opaque, (
void)items, (void)size;
return MZ_MALLOC(items * size); }
979 static void *
def_realloc_func(
void *opaque,
void *address,
size_t items,
size_t size) { (void)opaque, (
void)address, (void)items, (
void)size;
return MZ_REALLOC(address, items * size); }
991 int mz_deflateInit2(mz_streamp pStream,
int level,
int method,
int window_bits,
int mem_level,
int strategy)
1001 pStream->
msg = NULL;
1012 pStream->
state = (
struct mz_internal_state *)pComp;
1033 size_t in_bytes, out_bytes;
1034 mz_ulong orig_total_in, orig_total_out;
1035 int mz_status =
MZ_OK;
1058 if (defl_status < 0)
1072 if ((flush) || (pStream->
total_in != orig_total_in) || (pStream->
total_out != orig_total_out))
1086 pStream->
state = NULL;
1095 return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
1098 int mz_compress2(
unsigned char *pDest, mz_ulong *pDest_len,
const unsigned char *pSource, mz_ulong source_len,
int level)
1102 memset(&stream, 0,
sizeof(stream));
1105 if ((source_len | *pDest_len) > 0xFFFFFFFFU)
return MZ_PARAM_ERROR;
1113 if (status !=
MZ_OK)
return status;
1126 int mz_compress(
unsigned char *pDest, mz_ulong *pDest_len,
const unsigned char *pSource, mz_ulong source_len)
1139 mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed;
int m_window_bits;
1152 pStream->
msg = NULL;
1162 pStream->
state = (
struct mz_internal_state *)pDecomp;
1184 size_t in_bytes, out_bytes, orig_avail_in;
1201 if ((flush ==
MZ_FINISH) && (first_call))
1278 pStream->
state = NULL;
1283 int mz_uncompress(
unsigned char *pDest, mz_ulong *pDest_len,
const unsigned char *pSource, mz_ulong source_len)
1287 memset(&stream, 0,
sizeof(stream));
1290 if ((source_len | *pDest_len) > 0xFFFFFFFFU)
return MZ_PARAM_ERROR;
1298 if (status !=
MZ_OK)
1314 static struct {
int m_err;
const char *m_pDesc; } s_error_descs[] =
1316 {
MZ_OK,
"" }, {
MZ_STREAM_END,
"stream end" }, {
MZ_NEED_DICT,
"need dictionary" }, {
MZ_ERRNO,
"file error" }, {
MZ_STREAM_ERROR,
"stream error" },
1319 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;
1323 #endif //MINIZ_NO_ZLIB_APIS
1327 #define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
1328 #define TINFL_MEMSET(p, c, l) memset(p, c, l)
1330 #define TINFL_CR_BEGIN switch(r->m_state) { case 0:
1331 #define TINFL_CR_RETURN(state_index, result) do { status = result; r->m_state = state_index; goto common_exit; case state_index:; } MZ_MACRO_END
1332 #define TINFL_CR_RETURN_FOREVER(state_index, result) do { for ( ; ; ) { TINFL_CR_RETURN(state_index, result); } } MZ_MACRO_END
1333 #define TINFL_CR_FINISH }
1337 #define TINFL_GET_BYTE(state_index, c) do { \
1338 if (pIn_buf_cur >= pIn_buf_end) { \
1340 if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) { \
1341 TINFL_CR_RETURN(state_index, TINFL_STATUS_NEEDS_MORE_INPUT); \
1342 if (pIn_buf_cur < pIn_buf_end) { \
1343 c = *pIn_buf_cur++; \
1351 } else c = *pIn_buf_cur++; } MZ_MACRO_END
1353 #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))
1354 #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
1355 #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
1361 #define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \
1363 temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
1365 code_len = temp >> 9; \
1366 if ((code_len) && (num_bits >= code_len)) \
1368 } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \
1369 code_len = TINFL_FAST_LOOKUP_BITS; \
1371 temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
1372 } while ((temp < 0) && (num_bits >= (code_len + 1))); if (temp >= 0) break; \
1373 } TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; \
1374 } while (num_bits < 15);
1380 #define TINFL_HUFF_DECODE(state_index, sym, pHuff) do { \
1381 int temp; mz_uint code_len, c; \
1382 if (num_bits < 15) { \
1383 if ((pIn_buf_end - pIn_buf_cur) < 2) { \
1384 TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \
1386 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; \
1389 if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
1390 code_len = temp >> 9, temp &= 511; \
1392 code_len = TINFL_FAST_LOOKUP_BITS; do { temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; } while (temp < 0); \
1393 } sym = temp; bit_buf >>= code_len; num_bits -= code_len; } MZ_MACRO_END
1397 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 };
1398 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 };
1399 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};
1400 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};
1401 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 };
1402 static const int s_min_table_sizes[3] = { 257, 1, 4 };
1405 const mz_uint8 *pIn_buf_cur = pIn_buf_next, *
const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
1406 mz_uint8 *pOut_buf_cur = pOut_buf_next, *
const pOut_buf_end = pOut_buf_next + *pOut_buf_size;
1410 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; }
1432 while ((counter) && (num_bits))
1442 while (pIn_buf_cur >= pIn_buf_end)
1453 n =
MZ_MIN(
MZ_MIN((
size_t)(pOut_buf_end - pOut_buf_cur), (
size_t)(pIn_buf_end - pIn_buf_cur)), counter);
1454 TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n); pIn_buf_cur += n; pOut_buf_cur += n; counter -= (
mz_uint)n;
1467 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;
1480 used_syms = 0, total = 0; next_code[0] = next_code[1] = 0;
1481 for (i = 1; i <= 15; ++i) { used_syms += total_syms[i]; next_code[i + 1] = (total = ((total + total_syms[i]) << 1)); }
1482 if ((65536 != total) && (used_syms > 1))
1488 mz_uint rev_code = 0, l, cur_code, code_size = pTable->
m_code_size[sym_index];
if (!code_size)
continue;
1489 cur_code = next_code[code_size]++;
for (l = code_size; l > 0; l--, cur_code >>= 1) rev_code = (rev_code << 1) | (cur_code & 1);
1495 tree_cur -= ((rev_code >>= 1) & 1);
1496 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];
1498 tree_cur -= ((rev_code >>= 1) & 1); pTable->
m_tree[-tree_cur - 1] = (
mz_int16)sym_index;
1505 if ((dist == 16) && (!counter))
1509 num_extra =
"\02\03\07"[dist - 16];
TINFL_GET_BITS(18, s, num_extra); s +=
"\03\03\013"[dist - 16];
1524 if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
1530 *pOut_buf_cur++ = (
mz_uint8)counter;
1534 int sym2; mz_uint code_len;
1535 #if TINFL_USE_64BIT_BITBUF
1541 code_len = sym2 >> 9;
1546 counter = sym2; bit_buf >>= code_len; num_bits -= code_len;
1550 #if !TINFL_USE_64BIT_BITBUF
1554 code_len = sym2 >> 9;
1559 bit_buf >>= code_len; num_bits -= code_len;
1561 pOut_buf_cur[0] = (
mz_uint8)counter;
1572 if ((counter &= 511) == 256)
break;
1574 num_extra = s_length_extra[counter - 257]; counter = s_length_base[counter - 257];
1575 if (num_extra) { mz_uint extra_bits;
TINFL_GET_BITS(25, extra_bits, num_extra); counter += extra_bits; }
1578 num_extra = s_dist_extra[dist]; dist = s_dist_base[dist];
1579 if (num_extra) { mz_uint extra_bits;
TINFL_GET_BITS(27, extra_bits, num_extra); dist += extra_bits; }
1581 dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
1587 pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
1589 if ((
MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
1594 *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
1598 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
1599 else if ((counter >= 9) && (counter <= dist))
1601 const mz_uint8 *pSrc_end = pSrc + (counter & ~7);
1604 ((mz_uint32 *)pOut_buf_cur)[0] = ((
const mz_uint32 *)pSrc)[0];
1605 ((mz_uint32 *)pOut_buf_cur)[1] = ((
const mz_uint32 *)pSrc)[1];
1607 }
while ((pSrc += 8) < pSrc_end);
1608 if ((counter &= 7) < 3)
1612 pOut_buf_cur[0] = pSrc[0];
1614 pOut_buf_cur[1] = pSrc[1];
1615 pOut_buf_cur += counter;
1623 pOut_buf_cur[0] = pSrc[0];
1624 pOut_buf_cur[1] = pSrc[1];
1625 pOut_buf_cur[2] = pSrc[2];
1626 pOut_buf_cur += 3; pSrc += 3;
1627 }
while ((
int)(counter -= 3) > 2);
1628 if ((
int)counter > 0)
1630 pOut_buf_cur[0] = pSrc[0];
1631 if ((
int)counter > 1)
1632 pOut_buf_cur[1] = pSrc[1];
1633 pOut_buf_cur += counter;
1638 if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
1647 *pIn_buf_size = pIn_buf_cur - pIn_buf_next; *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
1650 const mz_uint8 *ptr = pOut_buf_next;
size_t buf_len = *pOut_buf_size;
1654 for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
1656 s1 += ptr[0], s2 += s1; s1 += ptr[1], s2 += s1; s1 += ptr[2], s2 += s1; s1 += ptr[3], s2 += s1;
1657 s1 += ptr[4], s2 += s1; s1 += ptr[5], s2 += s1; s1 += ptr[6], s2 += s1; s1 += ptr[7], s2 += s1;
1659 for ( ; i < block_len; ++i) s1 += *ptr++, s2 += s1;
1660 s1 %= 65521U, s2 %= 65521U; buf_len -= block_len; block_len = 5552;
1670 tinfl_decompressor decomp;
void *pBuf = NULL, *pNew_buf;
size_t src_buf_ofs = 0, out_buf_capacity = 0;
1675 size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity;
1676 tinfl_status status =
tinfl_decompress(&decomp, (
const mz_uint8*)pSrc_buf + src_buf_ofs, &src_buf_size, (mz_uint8*)pBuf, pBuf ? (mz_uint8*)pBuf + *pOut_len : NULL, &dst_buf_size,
1680 MZ_FREE(pBuf); *pOut_len = 0;
return NULL;
1682 src_buf_ofs += src_buf_size;
1683 *pOut_len += dst_buf_size;
1685 new_out_buf_capacity = out_buf_capacity * 2;
if (new_out_buf_capacity < 128) new_out_buf_capacity = 128;
1686 pNew_buf =
MZ_REALLOC(pBuf, new_out_buf_capacity);
1689 MZ_FREE(pBuf); *pOut_len = 0;
return NULL;
1691 pBuf = pNew_buf; out_buf_capacity = new_out_buf_capacity;
1713 size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size =
TINFL_LZ_DICT_SIZE - dict_ofs;
1714 tinfl_status status =
tinfl_decompress(&decomp, (
const mz_uint8*)pIn_buf + in_buf_ofs, &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size,
1716 in_buf_ofs += in_buf_size;
1717 if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (
int)dst_buf_size, pPut_buf_user)))
1727 *pIn_buf_size = in_buf_ofs;
1735 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,272,272,272,
1736 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,276,276,276,276,276,276,
1737 277,277,277,277,277,277,277,277,277,277,277,277,277,277,277,277,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,
1738 279,279,279,279,279,279,279,279,279,279,279,279,279,279,279,279,280,280,280,280,280,280,280,280,280,280,280,280,280,280,280,280,
1739 281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,
1740 282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,
1741 283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,
1742 284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,285 };
1745 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,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
1746 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,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
1747 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,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
1748 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,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,0 };
1751 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,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,
1752 11,11,11,11,11,11,11,11,11,11,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,13,
1753 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,14,14,14,14,14,14,14,14,14,14,14,14,
1754 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,14,14,14,14,14,14,14,14,
1755 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,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
1756 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,16,16,16,16,16,16,16,16,16,16,16,16,16,
1757 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,16,16,16,16,16,16,16,16,
1758 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,16,16,16,16,16,16,16,16,
1759 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,17,17,17,17,17,17,17,17,17,17,17,17,17,
1760 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,17,17,17,17,17,17,17,17,
1761 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,17,17,17,17,17,17,17,17,
1762 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 };
1765 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,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,
1766 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,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,6,6,6,
1767 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,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,
1768 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,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,
1769 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,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,
1770 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,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,
1771 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,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,
1775 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,26,26,26,26,26,26,26,26,26,26,26,26,
1776 26,26,26,26,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,
1777 28,28,28,28,28,28,28,28,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 };
1780 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,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
1781 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,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
1782 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 };
1788 mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];
tdefl_sym_freq* pCur_syms = pSyms0, *pNew_syms = pSyms1;
MZ_CLEAR_OBJ(hist);
1789 for (i = 0; i < num_syms; i++) { mz_uint freq = pSyms0[i].
m_key; hist[freq & 0xFF]++; hist[256 + ((freq >> 8) & 0xFF)]++; }
1790 while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256])) total_passes--;
1791 for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8)
1793 const mz_uint32* pHist = &hist[pass << 8];
1794 mz_uint offsets[256], cur_ofs = 0;
1795 for (i = 0; i < 256; i++) { offsets[i] = cur_ofs; cur_ofs += pHist[i]; }
1796 for (i = 0; i < num_syms; i++) pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];
1797 {
tdefl_sym_freq* t = pCur_syms; pCur_syms = pNew_syms; pNew_syms = t; }
1805 int root, leaf, next, avbl, used, dpth;
1806 if (n==0)
return;
else if (n==1) { A[0].
m_key = 1;
return; }
1807 A[0].
m_key += A[1].
m_key; root = 0; leaf = 2;
1808 for (next=1; next < n-1; next++)
1811 if (leaf>=n || (root<next && A[root].m_key<A[leaf].m_key)) { A[next].
m_key = (
mz_uint16)(A[next].m_key + A[root].m_key); A[root++].
m_key = (
mz_uint16)next; }
else A[next].
m_key = (
mz_uint16)(A[next].m_key + A[leaf++].m_key);
1813 A[n-2].
m_key = 0;
for (next=n-3; next>=0; next--) A[next].m_key = A[A[next].m_key].m_key+1;
1814 avbl = 1; used = dpth = 0; root = n-2; next = n-1;
1817 while (root>=0 && (
int)A[root].
m_key==dpth) { used++; root--; }
1818 while (avbl>used) { A[next--].
m_key = (
mz_uint16)(dpth); avbl--; }
1819 avbl = 2*used; dpth++; used = 0;
1827 int i; mz_uint32 total = 0;
if (code_list_len <= 1)
return;
1829 for (i = max_code_size; i > 0; i--) total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i));
1830 while (total != (1UL << max_code_size))
1832 pNum_codes[max_code_size]--;
1833 for (i = max_code_size - 1; i > 0; i--)
if (pNum_codes[i]) { pNum_codes[i]--; pNum_codes[i + 1] += 2;
break; }
1843 for (i = 0; i < table_len; i++) num_codes[d->
m_huff_code_sizes[table_num][i]]++;
1848 int num_used_syms = 0;
1849 const mz_uint16 *pSym_count = &d->
m_huff_count[table_num][0];
1854 for (i = 0; i < num_used_syms; i++) num_codes[pSyms[i].m_key]++;
1859 for (i = 1, j = num_used_syms; i <= code_size_limit; i++)
1863 next_code[1] = 0;
for (j = 0, i = 2; i <= code_size_limit; i++) next_code[i] = j = ((j + num_codes[i - 1]) << 1);
1865 for (i = 0; i < table_len; i++)
1867 mz_uint rev_code = 0, code, code_size;
if ((code_size = d->
m_huff_code_sizes[table_num][i]) == 0)
continue;
1868 code = next_code[code_size]++;
for (l = code_size; l > 0; l--, code >>= 1) rev_code = (rev_code << 1) | (code & 1);
1873 #define TDEFL_PUT_BITS(b, l) do { \
1874 mz_uint bits = b; mz_uint len = l; MZ_ASSERT(bits <= ((1U << len) - 1U)); \
1875 d->m_bit_buffer |= (bits << d->m_bits_in); d->m_bits_in += len; \
1876 while (d->m_bits_in >= 8) { \
1877 if (d->m_pOutput_buf < d->m_pOutput_buf_end) \
1878 *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \
1879 d->m_bit_buffer >>= 8; \
1880 d->m_bits_in -= 8; \
1884 #define TDEFL_RLE_PREV_CODE_SIZE() { if (rle_repeat_count) { \
1885 if (rle_repeat_count < 3) { \
1886 d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \
1887 while (rle_repeat_count--) packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \
1889 d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); packed_code_sizes[num_packed_code_sizes++] = 16; packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \
1890 } rle_repeat_count = 0; } }
1892 #define TDEFL_RLE_ZERO_CODE_SIZE() { if (rle_z_count) { \
1893 if (rle_z_count < 3) { \
1894 d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); while (rle_z_count--) packed_code_sizes[num_packed_code_sizes++] = 0; \
1895 } else if (rle_z_count <= 10) { \
1896 d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); packed_code_sizes[num_packed_code_sizes++] = 17; packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \
1898 d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); packed_code_sizes[num_packed_code_sizes++] = 18; packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \
1899 } rle_z_count = 0; } }
1901 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 };
1905 int num_lit_codes, num_dist_codes, num_bit_lengths; mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index;
1913 for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--)
if (d->
m_huff_code_sizes[0][num_lit_codes - 1])
break;
1914 for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--)
if (d->
m_huff_code_sizes[1][num_dist_codes - 1])
break;
1917 memcpy(code_sizes_to_pack + num_lit_codes, &d->
m_huff_code_sizes[1][0], num_dist_codes);
1918 total_code_sizes_to_pack = num_lit_codes + num_dist_codes; num_packed_code_sizes = 0; rle_z_count = 0; rle_repeat_count = 0;
1921 for (i = 0; i < total_code_sizes_to_pack; i++)
1923 mz_uint8 code_size = code_sizes_to_pack[i];
1932 if (code_size != prev_code_size)
1937 else if (++rle_repeat_count == 6)
1942 prev_code_size = code_size;
1953 for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)
if (d->
m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]])
break;
1957 for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes; )
1961 if (code >= 16)
TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++],
"\02\03\07"[code - 16]);
1970 for (i = 0; i <= 143; ++i) *p++ = 8;
1971 for ( ; i <= 255; ++i) *p++ = 9;
1972 for ( ; i <= 279; ++i) *p++ = 7;
1973 for ( ; i <= 287; ++i) *p++ = 8;
1983 static const mz_uint
mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
1985 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS
1989 mz_uint8 *pLZ_codes;
1995 #define TDEFL_PUT_BITS_FAST(b, l) { bit_buffer |= (((mz_uint64)(b)) << bits_in); bits_in += (l); }
1998 for (pLZ_codes = d->
m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1)
2001 flags = *pLZ_codes++ | 0x100;
2005 mz_uint s0, s1, n0, n1, sym, num_extra_bits;
2006 mz_uint match_len = pLZ_codes[0], match_dist = *(
const mz_uint16 *)(pLZ_codes + 1); pLZ_codes += 3;
2010 TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
2013 s0 = s_tdefl_small_dist_sym[match_dist & 511];
2014 n0 = s_tdefl_small_dist_extra[match_dist & 511];
2015 s1 = s_tdefl_large_dist_sym[match_dist >> 8];
2016 n1 = s_tdefl_large_dist_extra[match_dist >> 8];
2017 sym = (match_dist < 512) ? s0 : s1;
2018 num_extra_bits = (match_dist < 512) ? n0 : n1;
2022 TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
2026 mz_uint lit = *pLZ_codes++;
2030 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
2037 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
2050 *(mz_uint64*)pOutput_buf = bit_buffer;
2051 pOutput_buf += (bits_in >> 3);
2052 bit_buffer >>= (bits_in & ~7);
2056 #undef TDEFL_PUT_BITS_FAST
2064 mz_uint32 n =
MZ_MIN(bits_in, 16);
2078 mz_uint8 *pLZ_codes;
2081 for (pLZ_codes = d->
m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1)
2084 flags = *pLZ_codes++ | 0x100;
2087 mz_uint sym, num_extra_bits;
2088 mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8)); pLZ_codes += 3;
2092 TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
2094 if (match_dist < 512)
2096 sym = s_tdefl_small_dist_sym[match_dist]; num_extra_bits = s_tdefl_small_dist_extra[match_dist];
2100 sym = s_tdefl_large_dist_sym[match_dist >> 8]; num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8];
2104 TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
2108 mz_uint lit = *pLZ_codes++;
2118 #endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS
2131 mz_uint saved_bit_buf, saved_bits_in;
2132 mz_uint8 *pSaved_output_buf;
2133 mz_bool comp_block_succeeded =
MZ_FALSE;
2176 else if (!comp_block_succeeded)
2215 if ((n -= bytes_to_copy) != 0)
2230 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
2231 #define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16*)(p)
2234 mz_uint dist, pos = lookahead_pos &
TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
2235 mz_uint num_probes_left = d->
m_max_probes[match_len >= 32];
2236 const mz_uint16 *s = (
const mz_uint16*)(d->
m_dict + pos), *p, *q;
2237 mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->
m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD(s);
2243 if (--num_probes_left == 0)
return;
2244 #define TDEFL_PROBE \
2245 next_probe_pos = d->m_next[probe_pos]; \
2246 if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) return; \
2247 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
2248 if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) break;
2251 if (!dist)
break; q = (
const mz_uint16*)(d->
m_dict + probe_pos);
if (TDEFL_READ_UNALIGNED_WORD(q) != s01)
continue; p = s; probe_len = 32;
2252 do { }
while ( (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
2253 (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0) );
2258 else if ((probe_len = ((mz_uint)(p - s) * 2) + (mz_uint)(*(
const mz_uint8*)p == *(
const mz_uint8*)q)) > match_len)
2260 *pMatch_dist = dist;
if ((*pMatch_len = match_len =
MZ_MIN(max_match_len, probe_len)) == max_match_len)
break;
2261 c01 = TDEFL_READ_UNALIGNED_WORD(&d->
m_dict[pos + match_len - 1]);
2268 mz_uint dist, pos = lookahead_pos &
TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
2269 mz_uint num_probes_left = d->
m_max_probes[match_len >= 32];
2270 const mz_uint8 *s = d->
m_dict + pos, *p, *q;
2271 mz_uint8 c0 = d->
m_dict[pos + match_len], c1 = d->
m_dict[pos + match_len - 1];
2277 if (--num_probes_left == 0)
return;
2278 #define TDEFL_PROBE \
2279 next_probe_pos = d->m_next[probe_pos]; \
2280 if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) return; \
2281 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
2282 if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) break;
2285 if (!dist)
break; p = s; q = d->
m_dict + probe_pos;
for (probe_len = 0; probe_len < max_match_len; probe_len++)
if (*p++ != *q++)
break;
2286 if (probe_len > match_len)
2288 *pMatch_dist = dist;
if ((*pMatch_len = match_len = probe_len) == max_match_len)
return;
2289 c0 = d->
m_dict[pos + match_len]; c1 = d->
m_dict[pos + match_len - 1];
2293 #endif // #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
2295 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
2305 const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096;
2306 mz_uint dst_pos = (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
2309 lookahead_size += num_bytes_to_process;
2311 while (num_bytes_to_process)
2318 dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK;
2319 num_bytes_to_process -= n;
2323 if ((!d->
m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE))
break;
2325 while (lookahead_size >= 4)
2327 mz_uint cur_match_dist, cur_match_len = 1;
2328 mz_uint8 *pCur_dict = d->
m_dict + cur_pos;
2329 mz_uint first_trigram = (*(
const mz_uint32 *)pCur_dict) & 0xFFFFFF;
2331 mz_uint probe_pos = d->
m_hash[hash];
2334 if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <= dict_size) && ((*(
const mz_uint32 *)(d->
m_dict + (probe_pos &=
TDEFL_LZ_DICT_SIZE_MASK)) & 0xFFFFFF) == first_trigram))
2336 const mz_uint16 *p = (
const mz_uint16 *)pCur_dict;
2337 const mz_uint16 *q = (
const mz_uint16 *)(d->
m_dict + probe_pos);
2338 mz_uint32 probe_len = 32;
2339 do { }
while ( (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
2340 (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0) );
2341 cur_match_len = ((
mz_uint)(p - (
const mz_uint16 *)pCur_dict) * 2) + (
mz_uint)(*(
const mz_uint8 *)p == *(
const mz_uint8 *)q);
2348 *pLZ_code_buf++ = (
mz_uint8)first_trigram;
2349 *pLZ_flags = (
mz_uint8)(*pLZ_flags >> 1);
2355 cur_match_len =
MZ_MIN(cur_match_len, lookahead_size);
2362 *(mz_uint16 *)(&pLZ_code_buf[1]) = (
mz_uint16)cur_match_dist;
2364 *pLZ_flags = (
mz_uint8)((*pLZ_flags >> 1) | 0x80);
2366 s0 = s_tdefl_small_dist_sym[cur_match_dist & 511];
2367 s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8];
2368 d->
m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++;
2375 *pLZ_code_buf++ = (
mz_uint8)first_trigram;
2376 *pLZ_flags = (
mz_uint8)(*pLZ_flags >> 1);
2380 if (--num_flags_left == 0) { num_flags_left = 8; pLZ_flags = pLZ_code_buf++; }
2382 total_lz_bytes += cur_match_len;
2383 lookahead_pos += cur_match_len;
2385 cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK;
2386 MZ_ASSERT(lookahead_size >= cur_match_len);
2387 lookahead_size -= cur_match_len;
2400 while (lookahead_size)
2402 mz_uint8 lit = d->
m_dict[cur_pos];
2405 *pLZ_code_buf++ = lit;
2406 *pLZ_flags = (
mz_uint8)(*pLZ_flags >> 1);
2407 if (--num_flags_left == 0) { num_flags_left = 8; pLZ_flags = pLZ_code_buf++; }
2413 cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
2432 #endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
2458 s0 = s_tdefl_small_dist_sym[match_dist & 511]; s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127];
2471 mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;
2478 const mz_uint8 *pSrc_end = pSrc + num_bytes_to_process;
2479 src_buf_left -= num_bytes_to_process;
2481 while (pSrc != pSrc_end)
2486 dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK; ins_pos++;
2493 mz_uint8 c = *pSrc++;
2517 mz_uint8 c = d->
m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK];
2518 cur_match_len = 0;
while (cur_match_len < d->m_lookahead_size) {
if (d->
m_dict[cur_pos + cur_match_len] != c)
break; cur_match_len++; }
2528 cur_match_dist = cur_match_len = 0;
2535 if (cur_match_len >= 128)
2551 else if (!cur_match_dist)
2556 len_to_move = cur_match_len;
2607 if (pIn_buf_size) *pIn_buf_size = 0;
2608 if (pOut_buf_size) *pOut_buf_size = 0;
2619 (d->
m_wants_to_finish && (flush !=
TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf) )
2621 if (pIn_buf_size) *pIn_buf_size = 0;
2622 if (pOut_buf_size) *pOut_buf_size = 0;
2630 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
2635 if (!tdefl_compress_fast(d))
2639 #endif // #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
2668 d->
m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3;
2699 MZ_FREE(pComp);
return succeeded;
2712 size_t new_size = p->
m_size + len;
2716 do { new_capacity =
MZ_MAX(128U, new_capacity << 1U); }
while (new_size > new_capacity);
2727 if (!pOut_len)
return MZ_FALSE;
else *pOut_len = 0;
2736 if (!pOut_buf)
return 0;
2742 #ifndef MINIZ_NO_ZLIB_APIS
2743 static const mz_uint
s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
2759 #endif //MINIZ_NO_ZLIB_APIS
2762 #pragma warning (push)
2763 #pragma warning (disable:4204) // nonstandard extension used : non-constant aggregate initializer (also supported by GNU C and C99, so no big deal)
2771 if (!pComp)
return NULL;
2780 *pLen_out = out_buf.
m_size-41;
2782 mz_uint8 pnghdr[41]={0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
2783 0,0,(
mz_uint8)(w>>8),(
mz_uint8)w,0,0,(mz_uint8)(h>>8),(mz_uint8)h,8,
"\0\0\04\02\06"[num_chans],0,0,0,0,0,0,0,
2786 memcpy(out_buf.
m_pBuf, pnghdr, 41);
2796 #pragma warning (pop)
2801 #ifndef MINIZ_NO_ARCHIVE_APIS
2803 #ifdef MINIZ_NO_STDIO
2804 #define MZ_FILE void *
2807 #include <sys/stat.h>
2808 #if defined(_MSC_VER) || defined(__MINGW64__)
2809 #include <sys/utime.h>
2810 #define MZ_FILE FILE
2811 #define MZ_FOPEN fopen
2812 #define MZ_FCLOSE fclose
2813 #define MZ_FREAD fread
2814 #define MZ_FWRITE fwrite
2815 #define MZ_FTELL64 _ftelli64
2816 #define MZ_FSEEK64 _fseeki64
2817 #define MZ_FILE_STAT_STRUCT _stat
2818 #define MZ_FILE_STAT _stat
2819 #define MZ_FFLUSH fflush
2820 #define MZ_FREOPEN freopen
2821 #define MZ_DELETE_FILE remove
2822 #elif defined(__MINGW32__)
2823 #include <sys/utime.h>
2824 #define MZ_FILE FILE
2825 #define MZ_FOPEN fopen
2826 #define MZ_FCLOSE fclose
2827 #define MZ_FREAD fread
2828 #define MZ_FWRITE fwrite
2829 #define MZ_FTELL64 ftello64
2830 #define MZ_FSEEK64 fseeko64
2831 #define MZ_FILE_STAT_STRUCT _stat
2832 #define MZ_FILE_STAT _stat
2833 #define MZ_FFLUSH fflush
2834 #define MZ_FREOPEN freopen
2835 #define MZ_DELETE_FILE remove
2838 #define MZ_FILE FILE
2839 #define MZ_FOPEN fopen
2840 #define MZ_FCLOSE fclose
2841 #define MZ_FREAD fread
2842 #define MZ_FWRITE fwrite
2843 #define MZ_FTELL64 ftello
2844 #define MZ_FSEEK64 fseeko
2845 #define MZ_FILE_STAT_STRUCT stat
2846 #define MZ_FILE_STAT stat
2847 #define MZ_FFLUSH fflush
2848 #define MZ_FREOPEN freopen
2849 #define MZ_DELETE_FILE remove
2850 #endif // #ifdef _MSC_VER
2851 #endif // #ifdef MINIZ_NO_STDIO
2853 #define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
2893 #define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size
2894 #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[index]
2905 if (growing) { new_capacity =
MZ_MAX(1, pArray->
m_capacity);
while (new_capacity < min_new_capacity) new_capacity *= 2; }
2920 pArray->
m_size = new_size;
2936 #ifndef MINIZ_NO_TIME
2940 memset(&tm, 0,
sizeof(tm)); tm.tm_isdst = -1;
2941 tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900; tm.tm_mon = ((dos_date >> 5) & 15) - 1; tm.tm_mday = dos_date & 31;
2942 tm.tm_hour = (dos_time >> 11) & 31; tm.tm_min = (dos_time >> 5) & 63; tm.tm_sec = (dos_time << 1) & 62;
2948 struct tm *tm = localtime(&time);
2949 *pDOS_time = (
mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1));
2950 *pDOS_date = (
mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday);
2954 #ifndef MINIZ_NO_STDIO
2957 #ifdef MINIZ_NO_TIME
2958 (void)pFilename; *pDOS_date = *pDOS_time = 0;
2962 #endif // #ifdef MINIZ_NO_TIME
2968 #ifndef MINIZ_NO_TIME
2969 struct utimbuf t; t.actime = access_time; t.modtime = modified_time;
2970 return !utime(pFilename, &t);
2972 pFilename, access_time, modified_time;
2974 #endif // #ifndef MINIZ_NO_TIME
3007 mz_uint8 l = 0, r = 0;
3009 pE = pL +
MZ_MIN(l_len, r_len);
3016 return (pL == pE) ? (l_len < r_len) : (l < r);
3019 #define MZ_SWAP_UINT32(a, b) do { mz_uint32 t = a; a = b; b = t; } MZ_MACRO_END
3029 int start = (size - 2) >> 1, end;
3032 int child, root = start;
3035 if ((child = (root << 1) + 1) >= size)
3037 child += (((child + 1) < size) && (
mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1])));
3048 int child, root = 0;
3052 if ((child = (root << 1) + 1) >= end)
3054 child += (((child + 1) < end) &&
mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1]));
3065 mz_uint cdir_size, num_this_disk, cdir_disk_index;
3067 mz_int64 cur_file_ofs;
3069 mz_uint32 buf_u32[4096 /
sizeof(
mz_uint32)]; mz_uint8 *pBuf = (mz_uint8 *)buf_u32;
3080 for (i = n - 4; i >= 0; --i)
3090 cur_file_ofs =
MZ_MAX(cur_file_ofs - (
sizeof(buf_u32) - 3), 0);
3101 if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1)))
3128 mz_uint total_header_size, comp_size, decomp_size, disk_index;
3135 if (((!
MZ_READ_LE32(p +
MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size) || (decomp_size == 0xFFFFFFFF) || (comp_size == 0xFFFFFFFF))
3138 if ((disk_index != num_this_disk) && (disk_index != 1))
3144 n -= total_header_size; p += total_header_size;
3156 if ((!pZip) || (!pZip->
m_pRead))
3173 memcpy(pBuf, (
const mz_uint8 *)pZip->
m_pState->
m_pMem + file_ofs, s);
3194 #ifndef MINIZ_NO_STDIO
3199 if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (
MZ_FSEEK64(pZip->
m_pState->
m_pFile, (mz_int64)file_ofs, SEEK_SET))))
3206 mz_uint64 file_size;
3229 #endif // #ifndef MINIZ_NO_STDIO
3250 return (m_bit_flag & 1);
3255 mz_uint filename_len, internal_attr, external_attr;
3262 if ((!internal_attr) && ((external_attr & 0x10) != 0))
3279 if ((!p) || (!pStat))
3289 #ifndef MINIZ_NO_TIME
3314 if (!p) {
if (filename_buf_size) pFilename[0] =
'\0';
return 0; }
3316 if (filename_buf_size)
3318 n =
MZ_MIN(n, filename_buf_size - 1);
3320 pFilename[n] =
'\0';
3329 return 0 == memcmp(pA, pB, len);
3330 for (i = 0; i < len; ++i)
3340 mz_uint8 l = 0, r = 0;
3342 pE = pL +
MZ_MIN(l_len, r_len);
3349 return (pL == pE) ? (int)(l_len - r_len) : (l - r);
3359 const mz_uint filename_len = (
mz_uint)strlen(pFilename);
3360 int l = 0, h = size - 1;
3363 int m = (l + h) >> 1, file_index = pIndices[m], comp =
mz_zip_reader_filename_compare(pCentral_dir, pCentral_dir_offsets, file_index, pFilename, filename_len);
3376 mz_uint file_index;
size_t name_len, comment_len;
3381 name_len = strlen(pName);
if (name_len > 0xFFFF)
return -1;
3382 comment_len = pComment ? strlen(pComment) : 0;
if (comment_len > 0xFFFF)
return -1;
3383 for (file_index = 0; file_index < pZip->
m_total_files; file_index++)
3388 if (filename_len < name_len)
3393 const char *pFile_comment = pFilename + filename_len + file_extra_len;
3399 int ofs = filename_len - 1;
3402 if ((pFilename[ofs] ==
'/') || (pFilename[ofs] ==
'\\') || (pFilename[ofs] ==
':'))
3404 }
while (--ofs >= 0);
3406 pFilename += ofs; filename_len -= ofs;
3417 mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail;
3423 if ((buf_size) && (!pBuf))
3442 if (buf_size < needed_size)
3456 if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.
m_method))
3459 if (pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pBuf, (
size_t)needed_size) != needed_size)
3471 read_buf_size = read_buf_avail = file_stat.
m_comp_size;
3474 else if (pUser_read_buf)
3477 if (!user_read_buf_size)
3479 pRead_buf = (mz_uint8 *)pUser_read_buf;
3480 read_buf_size = user_read_buf_size;
3489 if (((0,
sizeof(
size_t) ==
sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF))
3491 if (((
sizeof(
size_t) ==
sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF))
3502 size_t in_buf_size, out_buf_size = (size_t)(file_stat.
m_uncomp_size - out_buf_ofs);
3505 read_buf_avail =
MZ_MIN(read_buf_size, comp_remaining);
3506 if (pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail)
3511 cur_file_ofs += read_buf_avail;
3512 comp_remaining -= read_buf_avail;
3515 in_buf_size = (size_t)read_buf_avail;
3517 read_buf_avail -= in_buf_size;
3518 read_buf_ofs += in_buf_size;
3519 out_buf_ofs += out_buf_size;
3555 mz_uint64 comp_size, uncomp_size, alloc_size;
3569 if (((0,
sizeof(
size_t) ==
sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF))
3571 if (((
sizeof(
size_t) ==
sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF))
3583 if (pSize) *pSize = (size_t)alloc_size;
3592 if (pSize) *pSize = 0;
3601 mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, out_buf_ofs = 0, cur_file_ofs;
3603 void *pRead_buf = NULL;
void *pWrite_buf = NULL;
3635 read_buf_size = read_buf_avail = file_stat.
m_comp_size;
3647 if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.
m_method))
3660 else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
3661 file_crc32 = (mz_uint32)
mz_crc32(file_crc32, (
const mz_uint8 *)pRead_buf, (
size_t)file_stat.
m_comp_size);
3668 while (comp_remaining)
3670 read_buf_avail =
MZ_MIN(read_buf_size, comp_remaining);
3671 if (pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail)
3677 if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
3678 file_crc32 = (mz_uint32)
mz_crc32(file_crc32, (
const mz_uint8 *)pRead_buf, (
size_t)read_buf_avail);
3680 if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail)
3685 cur_file_ofs += read_buf_avail;
3686 out_buf_ofs += read_buf_avail;
3687 comp_remaining -= read_buf_avail;
3702 mz_uint8 *pWrite_buf_cur = (mz_uint8 *)pWrite_buf + (out_buf_ofs & (
TINFL_LZ_DICT_SIZE - 1));
3706 read_buf_avail =
MZ_MIN(read_buf_size, comp_remaining);
3707 if (pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail)
3712 cur_file_ofs += read_buf_avail;
3713 comp_remaining -= read_buf_avail;
3717 in_buf_size = (size_t)read_buf_avail;
3719 read_buf_avail -= in_buf_size;
3720 read_buf_ofs += in_buf_size;
3724 if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size)
3730 if ((out_buf_ofs += out_buf_size) > file_stat.
m_uncomp_size)
3763 #ifndef MINIZ_NO_STDIO
3776 pFile =
MZ_FOPEN(pDst_filename,
"wb");
3782 #ifndef MINIZ_NO_TIME
3788 #endif // #ifndef MINIZ_NO_STDIO
3802 #ifndef MINIZ_NO_STDIO
3808 #endif // #ifndef MINIZ_NO_STDIO
3817 #ifndef MINIZ_NO_STDIO
3829 #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
3833 #define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8 *)(p), (mz_uint16)(v))
3834 #define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8 *)(p), (mz_uint32)(v))
3872 if ((!n) || ((0,
sizeof(
size_t) ==
sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF)))
3874 if ((!n) || ((
sizeof(
size_t) ==
sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF)))
3880 size_t new_capacity =
MZ_MAX(64, pState->
m_mem_capacity);
while (new_capacity < new_size) new_capacity *= 2;
3885 memcpy((mz_uint8 *)pState->
m_pMem + file_ofs, pBuf, n);
3896 if (0 != (initial_allocation_size =
MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning)))
3908 #ifndef MINIZ_NO_STDIO
3913 if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (
MZ_FSEEK64(pZip->
m_pState->
m_pFile, (mz_int64)file_ofs, SEEK_SET))))
3925 if (NULL == (pFile =
MZ_FOPEN(pFilename,
"wb")))
3931 if (size_to_reserve_at_beginning)
3933 mz_uint64 cur_ofs = 0;
char buf[4096];
MZ_CLEAR_OBJ(buf);
3936 size_t n = (size_t)
MZ_MIN(
sizeof(buf), size_to_reserve_at_beginning);
3942 cur_ofs += n; size_to_reserve_at_beginning -= n;
3943 }
while (size_to_reserve_at_beginning);
3947 #endif // #ifndef MINIZ_NO_STDIO
3962 #ifdef MINIZ_NO_STDIO
3977 #endif // #ifdef MINIZ_NO_STDIO
4021 static mz_bool
mz_zip_writer_create_local_dir_header(
mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size, mz_uint16 extra_size, mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date)
4039 static mz_bool
mz_zip_writer_create_central_dir_header(
mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size, mz_uint16 extra_size, mz_uint16 comment_size, mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date, mz_uint64 local_header_ofs, mz_uint32 ext_attributes)
4060 static mz_bool
mz_zip_writer_add_to_central_dir(
mz_zip_archive *pZip,
const char *pFilename, mz_uint16 filename_size,
const void *pExtra, mz_uint16 extra_size,
const void *pComment, mz_uint16 comment_size, mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date, mz_uint64 local_header_ofs, mz_uint32 ext_attributes)
4071 if (!
mz_zip_writer_create_central_dir_header(pZip, central_dir_header, filename_size, extra_size, comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_header_ofs, ext_attributes))
4091 if (*pArchive_name ==
'/')
4093 while (*pArchive_name)
4095 if ((*pArchive_name ==
'\\') || (*pArchive_name ==
':'))
4114 memset(buf, 0,
MZ_MIN(
sizeof(buf), n));
4117 mz_uint32 s =
MZ_MIN(
sizeof(buf), n);
4120 cur_file_ofs += s; n -= s;
4125 mz_bool
mz_zip_writer_add_mem_ex(
mz_zip_archive *pZip,
const char *pArchive_name,
const void *pBuf,
size_t buf_size,
const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32)
4127 mz_uint16 method = 0, dos_time = 0, dos_date = 0;
4128 mz_uint level, ext_attributes = 0, num_alignment_padding_bytes;
4130 size_t archive_name_size;
4133 mz_bool store_data_uncompressed;
4136 if ((
int)level_and_flags < 0)
4138 level = level_and_flags & 0xF;
4146 if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size))
4149 if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF))
4154 #ifndef MINIZ_NO_TIME
4156 time_t cur_time; time(&cur_time);
4159 #endif // #ifndef MINIZ_NO_TIME
4161 archive_name_size = strlen(pArchive_name);
4162 if (archive_name_size > 0xFFFF)
4171 if ((archive_name_size) && (pArchive_name[archive_name_size - 1] ==
'/'))
4174 ext_attributes |= 0x10;
4176 if ((buf_size) || (uncomp_size))
4184 if ((!store_data_uncompressed) && (buf_size))
4195 local_dir_header_ofs += num_alignment_padding_bytes;
4197 cur_archive_file_ofs += num_alignment_padding_bytes +
sizeof(local_dir_header);
4200 if (pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
4205 cur_archive_file_ofs += archive_name_size;
4207 if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
4210 uncomp_size = buf_size;
4211 if (uncomp_size <= 3)
4214 store_data_uncompressed =
MZ_TRUE;
4218 if (store_data_uncompressed)
4226 cur_archive_file_ofs += buf_size;
4227 comp_size = buf_size;
4229 if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)
4257 if ((comp_size > 0xFFFFFFFF) || (cur_archive_file_ofs > 0xFFFFFFFF))
4260 if (!
mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 0, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date))
4263 if (pZip->
m_pWrite(pZip->
m_pIO_opaque, local_dir_header_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
4266 if (!
mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, NULL, 0, pComment, comment_size, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date, local_dir_header_ofs, ext_attributes))
4275 #ifndef MINIZ_NO_STDIO
4278 mz_uint uncomp_crc32 =
MZ_CRC32_INIT, level, num_alignment_padding_bytes;
4279 mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0;
4281 size_t archive_name_size;
4285 if ((
int)level_and_flags < 0)
4287 level = level_and_flags & 0xF;
4296 archive_name_size = strlen(pArchive_name);
4297 if (archive_name_size > 0xFFFF)
4309 pSrc_file =
MZ_FOPEN(pSrc_filename,
"rb");
4316 if (uncomp_size > 0xFFFFFFFF)
4322 if (uncomp_size <= 3)
4327 local_dir_header_ofs += num_alignment_padding_bytes;
4329 cur_archive_file_ofs += num_alignment_padding_bytes +
sizeof(local_dir_header);
4332 if (pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
4337 cur_archive_file_ofs += archive_name_size;
4341 mz_uint64 uncomp_remaining = uncomp_size;
4351 while (uncomp_remaining)
4360 uncomp_crc32 = (
mz_uint32)
mz_crc32(uncomp_crc32, (
const mz_uint8 *)pRead_buf, n);
4361 uncomp_remaining -= n;
4362 cur_archive_file_ofs += n;
4364 comp_size = uncomp_size;
4395 if (
MZ_FREAD(pRead_buf, 1, in_buf_size, pSrc_file) != in_buf_size)
4398 uncomp_crc32 = (
mz_uint32)
mz_crc32(uncomp_crc32, (
const mz_uint8 *)pRead_buf, in_buf_size);
4399 uncomp_remaining -= in_buf_size;
4432 if ((comp_size > 0xFFFFFFFF) || (cur_archive_file_ofs > 0xFFFFFFFF))
4435 if (!
mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 0, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date))
4438 if (pZip->
m_pWrite(pZip->
m_pIO_opaque, local_dir_header_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
4441 if (!
mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, NULL, 0, pComment, comment_size, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date, local_dir_header_ofs, ext_attributes))
4449 #endif // #ifndef MINIZ_NO_STDIO
4453 mz_uint n, bit_flags, num_alignment_padding_bytes;
4454 mz_uint64 comp_bytes_remaining, local_dir_header_ofs;
4455 mz_uint64 cur_src_file_ofs, cur_dst_file_ofs;
4458 size_t orig_central_dir_size;
4460 void *pBuf;
const mz_uint8 *pSrc_central_header;
4485 cur_dst_file_ofs += num_alignment_padding_bytes;
4486 local_dir_header_ofs = cur_dst_file_ofs;
4499 while (comp_bytes_remaining)
4507 cur_src_file_ofs += n;
4514 cur_dst_file_ofs += n;
4516 comp_bytes_remaining -= n;
4536 cur_src_file_ofs += n;
4537 cur_dst_file_ofs += n;
4542 if (cur_dst_file_ofs > 0xFFFFFFFF)
4577 mz_uint64 central_dir_ofs, central_dir_size;
4589 central_dir_ofs = 0;
4590 central_dir_size = 0;
4612 #ifndef MINIZ_NO_STDIO
4615 #endif // #ifndef MINIZ_NO_STDIO
4625 if ((!pZip) || (!pZip->
m_pState) || (!pBuf) || (!pSize))
4652 #ifndef MINIZ_NO_STDIO
4658 #endif // #ifndef MINIZ_NO_STDIO
4671 #ifndef MINIZ_NO_STDIO
4674 mz_bool status, created_new_archive =
MZ_FALSE;
4678 if ((
int)level_and_flags < 0)
4680 if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment)) || ((level_and_flags & 0xF) >
MZ_UBER_COMPRESSION))
4689 created_new_archive =
MZ_TRUE;
4702 status =
mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, 0, 0);
4708 if ((!status) && (created_new_archive))
4712 (void)ignoredStatus;
4726 if ((!pZip_filename) || (!pArchive_name))
4740 #endif // #ifndef MINIZ_NO_STDIO
4742 #endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
4744 #endif // #ifndef MINIZ_NO_ARCHIVE_APIS
4750 #endif // MINIZ_HEADER_FILE_ONLY