Path: blob/master/dependencies/all/miniz/miniz.c
774 views
#include "miniz.h"1/**************************************************************************2*3* Copyright 2013-2014 RAD Game Tools and Valve Software4* Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC5* All Rights Reserved.6*7* Permission is hereby granted, free of charge, to any person obtaining a copy8* of this software and associated documentation files (the "Software"), to deal9* in the Software without restriction, including without limitation the rights10* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell11* copies of the Software, and to permit persons to whom the Software is12* furnished to do so, subject to the following conditions:13*14* The above copyright notice and this permission notice shall be included in15* all copies or substantial portions of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR18* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE20* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER21* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,22* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN23* THE SOFTWARE.24*25**************************************************************************/26272829typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1];30typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1];31typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1];3233#ifdef __cplusplus34extern "C" {35#endif3637/* ------------------- zlib-style API's */3839mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len)40{41mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16);42size_t block_len = buf_len % 5552;43if (!ptr)44return MZ_ADLER32_INIT;45while (buf_len)46{47for (i = 0; i + 7 < block_len; i += 8, ptr += 8)48{49s1 += ptr[0], s2 += s1;50s1 += ptr[1], s2 += s1;51s1 += ptr[2], s2 += s1;52s1 += ptr[3], s2 += s1;53s1 += ptr[4], s2 += s1;54s1 += ptr[5], s2 += s1;55s1 += ptr[6], s2 += s1;56s1 += ptr[7], s2 += s1;57}58for (; i < block_len; ++i)59s1 += *ptr++, s2 += s1;60s1 %= 65521U, s2 %= 65521U;61buf_len -= block_len;62block_len = 5552;63}64return (s2 << 16) + s1;65}6667/* Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C implementation that balances processor cache usage against speed": http://www.geocities.com/malbrain/ */68#if 069mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)70{71static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,720xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };73mz_uint32 crcu32 = (mz_uint32)crc;74if (!ptr)75return MZ_CRC32_INIT;76crcu32 = ~crcu32;77while (buf_len--)78{79mz_uint8 b = *ptr++;80crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)];81crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)];82}83return ~crcu32;84}85#elif defined(USE_EXTERNAL_MZCRC)86/* If USE_EXTERNAL_CRC is defined, an external module will export the87* mz_crc32() symbol for us to use, e.g. an SSE-accelerated version.88* Depending on the impl, it may be necessary to ~ the input/output crc values.89*/90mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len);91#else92/* Faster, but larger CPU cache footprint.93*/94mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)95{96static const mz_uint32 s_crc_table[256] =97{980x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535,990x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD,1000xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D,1010x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,1020x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4,1030xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,1040xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC,1050x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,1060x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB,1070xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,1080x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB,1090x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,1100x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA,1110xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE,1120xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A,1130x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,1140x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409,1150xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,1160xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739,1170x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,1180xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268,1190x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0,1200x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8,1210xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,1220xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF,1230x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703,1240x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7,1250xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,1260x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE,1270x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,1280x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6,1290xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,1300xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D,1310x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5,1320x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605,1330xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,1340xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D135};136137mz_uint32 crc32 = (mz_uint32)crc ^ 0xFFFFFFFF;138const mz_uint8 *pByte_buf = (const mz_uint8 *)ptr;139140while (buf_len >= 4)141{142crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF];143crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[1]) & 0xFF];144crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[2]) & 0xFF];145crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[3]) & 0xFF];146pByte_buf += 4;147buf_len -= 4;148}149150while (buf_len)151{152crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF];153++pByte_buf;154--buf_len;155}156157return ~crc32;158}159#endif160161void mz_free(void *p)162{163MZ_FREE(p);164}165166MINIZ_EXPORT void *miniz_def_alloc_func(void *opaque, size_t items, size_t size)167{168(void)opaque, (void)items, (void)size;169return MZ_MALLOC(items * size);170}171MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address)172{173(void)opaque, (void)address;174MZ_FREE(address);175}176MINIZ_EXPORT void *miniz_def_realloc_func(void *opaque, void *address, size_t items, size_t size)177{178(void)opaque, (void)address, (void)items, (void)size;179return MZ_REALLOC(address, items * size);180}181182const char *mz_version(void)183{184return MZ_VERSION;185}186187#ifndef MINIZ_NO_ZLIB_APIS188189int mz_deflateInit(mz_streamp pStream, int level)190{191return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, MZ_DEFAULT_STRATEGY);192}193194int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy)195{196tdefl_compressor *pComp;197mz_uint comp_flags = TDEFL_COMPUTE_ADLER32 | tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy);198199if (!pStream)200return MZ_STREAM_ERROR;201if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)))202return MZ_PARAM_ERROR;203204pStream->data_type = 0;205pStream->adler = MZ_ADLER32_INIT;206pStream->msg = NULL;207pStream->reserved = 0;208pStream->total_in = 0;209pStream->total_out = 0;210if (!pStream->zalloc)211pStream->zalloc = miniz_def_alloc_func;212if (!pStream->zfree)213pStream->zfree = miniz_def_free_func;214215pComp = (tdefl_compressor *)pStream->zalloc(pStream->opaque, 1, sizeof(tdefl_compressor));216if (!pComp)217return MZ_MEM_ERROR;218219pStream->state = (struct mz_internal_state *)pComp;220221if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY)222{223mz_deflateEnd(pStream);224return MZ_PARAM_ERROR;225}226227return MZ_OK;228}229230int mz_deflateReset(mz_streamp pStream)231{232if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree))233return MZ_STREAM_ERROR;234pStream->total_in = pStream->total_out = 0;235tdefl_init((tdefl_compressor *)pStream->state, NULL, NULL, ((tdefl_compressor *)pStream->state)->m_flags);236return MZ_OK;237}238239int mz_deflate(mz_streamp pStream, int flush)240{241size_t in_bytes, out_bytes;242mz_ulong orig_total_in, orig_total_out;243int mz_status = MZ_OK;244245if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out))246return MZ_STREAM_ERROR;247if (!pStream->avail_out)248return MZ_BUF_ERROR;249250if (flush == MZ_PARTIAL_FLUSH)251flush = MZ_SYNC_FLUSH;252253if (((tdefl_compressor *)pStream->state)->m_prev_return_status == TDEFL_STATUS_DONE)254return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR;255256orig_total_in = pStream->total_in;257orig_total_out = pStream->total_out;258for (;;)259{260tdefl_status defl_status;261in_bytes = pStream->avail_in;262out_bytes = pStream->avail_out;263264defl_status = tdefl_compress((tdefl_compressor *)pStream->state, pStream->next_in, &in_bytes, pStream->next_out, &out_bytes, (tdefl_flush)flush);265pStream->next_in += (mz_uint)in_bytes;266pStream->avail_in -= (mz_uint)in_bytes;267pStream->total_in += (mz_uint)in_bytes;268pStream->adler = tdefl_get_adler32((tdefl_compressor *)pStream->state);269270pStream->next_out += (mz_uint)out_bytes;271pStream->avail_out -= (mz_uint)out_bytes;272pStream->total_out += (mz_uint)out_bytes;273274if (defl_status < 0)275{276mz_status = MZ_STREAM_ERROR;277break;278}279else if (defl_status == TDEFL_STATUS_DONE)280{281mz_status = MZ_STREAM_END;282break;283}284else if (!pStream->avail_out)285break;286else if ((!pStream->avail_in) && (flush != MZ_FINISH))287{288if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out))289break;290return MZ_BUF_ERROR; /* Can't make forward progress without some input.291*/292}293}294return mz_status;295}296297int mz_deflateEnd(mz_streamp pStream)298{299if (!pStream)300return MZ_STREAM_ERROR;301if (pStream->state)302{303pStream->zfree(pStream->opaque, pStream->state);304pStream->state = NULL;305}306return MZ_OK;307}308309mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len)310{311(void)pStream;312/* This is really over conservative. (And lame, but it's actually pretty tricky to compute a true upper bound given the way tdefl's blocking works.) */313return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);314}315316int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level)317{318int status;319mz_stream stream;320memset(&stream, 0, sizeof(stream));321322/* In case mz_ulong is 64-bits (argh I hate longs). */323if ((source_len | *pDest_len) > 0xFFFFFFFFU)324return MZ_PARAM_ERROR;325326stream.next_in = pSource;327stream.avail_in = (mz_uint32)source_len;328stream.next_out = pDest;329stream.avail_out = (mz_uint32)*pDest_len;330331status = mz_deflateInit(&stream, level);332if (status != MZ_OK)333return status;334335status = mz_deflate(&stream, MZ_FINISH);336if (status != MZ_STREAM_END)337{338mz_deflateEnd(&stream);339return (status == MZ_OK) ? MZ_BUF_ERROR : status;340}341342*pDest_len = stream.total_out;343return mz_deflateEnd(&stream);344}345346int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)347{348return mz_compress2(pDest, pDest_len, pSource, source_len, MZ_DEFAULT_COMPRESSION);349}350351mz_ulong mz_compressBound(mz_ulong source_len)352{353return mz_deflateBound(NULL, source_len);354}355356typedef struct357{358tinfl_decompressor m_decomp;359mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed;360int m_window_bits;361mz_uint8 m_dict[TINFL_LZ_DICT_SIZE];362tinfl_status m_last_status;363} inflate_state;364365int mz_inflateInit2(mz_streamp pStream, int window_bits)366{367inflate_state *pDecomp;368if (!pStream)369return MZ_STREAM_ERROR;370if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))371return MZ_PARAM_ERROR;372373pStream->data_type = 0;374pStream->adler = 0;375pStream->msg = NULL;376pStream->total_in = 0;377pStream->total_out = 0;378pStream->reserved = 0;379if (!pStream->zalloc)380pStream->zalloc = miniz_def_alloc_func;381if (!pStream->zfree)382pStream->zfree = miniz_def_free_func;383384pDecomp = (inflate_state *)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state));385if (!pDecomp)386return MZ_MEM_ERROR;387388pStream->state = (struct mz_internal_state *)pDecomp;389390tinfl_init(&pDecomp->m_decomp);391pDecomp->m_dict_ofs = 0;392pDecomp->m_dict_avail = 0;393pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;394pDecomp->m_first_call = 1;395pDecomp->m_has_flushed = 0;396pDecomp->m_window_bits = window_bits;397398return MZ_OK;399}400401int mz_inflateInit(mz_streamp pStream)402{403return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS);404}405406int mz_inflateReset(mz_streamp pStream)407{408inflate_state *pDecomp;409if (!pStream)410return MZ_STREAM_ERROR;411412pStream->data_type = 0;413pStream->adler = 0;414pStream->msg = NULL;415pStream->total_in = 0;416pStream->total_out = 0;417pStream->reserved = 0;418419pDecomp = (inflate_state *)pStream->state;420421tinfl_init(&pDecomp->m_decomp);422pDecomp->m_dict_ofs = 0;423pDecomp->m_dict_avail = 0;424pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;425pDecomp->m_first_call = 1;426pDecomp->m_has_flushed = 0;427/* pDecomp->m_window_bits = window_bits */;428429return MZ_OK;430}431432int mz_inflate(mz_streamp pStream, int flush)433{434inflate_state *pState;435mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32;436size_t in_bytes, out_bytes, orig_avail_in;437tinfl_status status;438439if ((!pStream) || (!pStream->state))440return MZ_STREAM_ERROR;441if (flush == MZ_PARTIAL_FLUSH)442flush = MZ_SYNC_FLUSH;443if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH))444return MZ_STREAM_ERROR;445446pState = (inflate_state *)pStream->state;447if (pState->m_window_bits > 0)448decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER;449orig_avail_in = pStream->avail_in;450451first_call = pState->m_first_call;452pState->m_first_call = 0;453if (pState->m_last_status < 0)454return MZ_DATA_ERROR;455456if (pState->m_has_flushed && (flush != MZ_FINISH))457return MZ_STREAM_ERROR;458pState->m_has_flushed |= (flush == MZ_FINISH);459460if ((flush == MZ_FINISH) && (first_call))461{462/* MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file. */463decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;464in_bytes = pStream->avail_in;465out_bytes = pStream->avail_out;466status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags);467pState->m_last_status = status;468pStream->next_in += (mz_uint)in_bytes;469pStream->avail_in -= (mz_uint)in_bytes;470pStream->total_in += (mz_uint)in_bytes;471pStream->adler = tinfl_get_adler32(&pState->m_decomp);472pStream->next_out += (mz_uint)out_bytes;473pStream->avail_out -= (mz_uint)out_bytes;474pStream->total_out += (mz_uint)out_bytes;475476if (status < 0)477return MZ_DATA_ERROR;478else if (status != TINFL_STATUS_DONE)479{480pState->m_last_status = TINFL_STATUS_FAILED;481return MZ_BUF_ERROR;482}483return MZ_STREAM_END;484}485/* flush != MZ_FINISH then we must assume there's more input. */486if (flush != MZ_FINISH)487decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT;488489if (pState->m_dict_avail)490{491n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);492memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);493pStream->next_out += n;494pStream->avail_out -= n;495pStream->total_out += n;496pState->m_dict_avail -= n;497pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);498return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;499}500501for (;;)502{503in_bytes = pStream->avail_in;504out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs;505506status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags);507pState->m_last_status = status;508509pStream->next_in += (mz_uint)in_bytes;510pStream->avail_in -= (mz_uint)in_bytes;511pStream->total_in += (mz_uint)in_bytes;512pStream->adler = tinfl_get_adler32(&pState->m_decomp);513514pState->m_dict_avail = (mz_uint)out_bytes;515516n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);517memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);518pStream->next_out += n;519pStream->avail_out -= n;520pStream->total_out += n;521pState->m_dict_avail -= n;522pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);523524if (status < 0)525return MZ_DATA_ERROR; /* Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well). */526else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in))527return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH. */528else if (flush == MZ_FINISH)529{530/* The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH. */531if (status == TINFL_STATUS_DONE)532return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END;533/* status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's at least 1 more byte on the way. If there's no more room left in the output buffer then something is wrong. */534else if (!pStream->avail_out)535return MZ_BUF_ERROR;536}537else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail))538break;539}540541return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;542}543544int mz_inflateEnd(mz_streamp pStream)545{546if (!pStream)547return MZ_STREAM_ERROR;548if (pStream->state)549{550pStream->zfree(pStream->opaque, pStream->state);551pStream->state = NULL;552}553return MZ_OK;554}555int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong *pSource_len)556{557mz_stream stream;558int status;559memset(&stream, 0, sizeof(stream));560561/* In case mz_ulong is 64-bits (argh I hate longs). */562if ((*pSource_len | *pDest_len) > 0xFFFFFFFFU)563return MZ_PARAM_ERROR;564565stream.next_in = pSource;566stream.avail_in = (mz_uint32)*pSource_len;567stream.next_out = pDest;568stream.avail_out = (mz_uint32)*pDest_len;569570status = mz_inflateInit(&stream);571if (status != MZ_OK)572return status;573574status = mz_inflate(&stream, MZ_FINISH);575*pSource_len = *pSource_len - stream.avail_in;576if (status != MZ_STREAM_END)577{578mz_inflateEnd(&stream);579return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status;580}581*pDest_len = stream.total_out;582583return mz_inflateEnd(&stream);584}585586int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)587{588return mz_uncompress2(pDest, pDest_len, pSource, &source_len);589}590591const char *mz_error(int err)592{593static struct594{595int m_err;596const char *m_pDesc;597} s_error_descs[] =598{599{ MZ_OK, "" }, { MZ_STREAM_END, "stream end" }, { MZ_NEED_DICT, "need dictionary" }, { MZ_ERRNO, "file error" }, { MZ_STREAM_ERROR, "stream error" }, { MZ_DATA_ERROR, "data error" }, { MZ_MEM_ERROR, "out of memory" }, { MZ_BUF_ERROR, "buf error" }, { MZ_VERSION_ERROR, "version error" }, { MZ_PARAM_ERROR, "parameter error" }600};601mz_uint i;602for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i)603if (s_error_descs[i].m_err == err)604return s_error_descs[i].m_pDesc;605return NULL;606}607608#endif /*MINIZ_NO_ZLIB_APIS */609610#ifdef __cplusplus611}612#endif613614/*615This is free and unencumbered software released into the public domain.616617Anyone is free to copy, modify, publish, use, compile, sell, or618distribute this software, either in source code form or as a compiled619binary, for any purpose, commercial or non-commercial, and by any620means.621622In jurisdictions that recognize copyright laws, the author or authors623of this software dedicate any and all copyright interest in the624software to the public domain. We make this dedication for the benefit625of the public at large and to the detriment of our heirs and626successors. We intend this dedication to be an overt act of627relinquishment in perpetuity of all present and future rights to this628software under copyright law.629630THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,631EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF632MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.633IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR634OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,635ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR636OTHER DEALINGS IN THE SOFTWARE.637638For more information, please refer to <http://unlicense.org/>639*/640/**************************************************************************641*642* Copyright 2013-2014 RAD Game Tools and Valve Software643* Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC644* All Rights Reserved.645*646* Permission is hereby granted, free of charge, to any person obtaining a copy647* of this software and associated documentation files (the "Software"), to deal648* in the Software without restriction, including without limitation the rights649* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell650* copies of the Software, and to permit persons to whom the Software is651* furnished to do so, subject to the following conditions:652*653* The above copyright notice and this permission notice shall be included in654* all copies or substantial portions of the Software.655*656* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR657* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,658* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE659* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER660* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,661* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN662* THE SOFTWARE.663*664**************************************************************************/665666667668#ifdef __cplusplus669extern "C" {670#endif671672/* ------------------- Low-level Compression (independent from all decompression API's) */673674/* Purposely making these tables static for faster init and thread safety. */675static const mz_uint16 s_tdefl_len_sym[256] =676{677257, 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,678273, 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,679277, 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,680279, 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,681281, 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,682282, 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,683283, 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,684284, 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, 285685};686687static const mz_uint8 s_tdefl_len_extra[256] =688{6890, 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,6904, 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,6915, 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,6925, 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, 0693};694695static const mz_uint8 s_tdefl_small_dist_sym[512] =696{6970, 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,69811, 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,69913, 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,70014, 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,70114, 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,70215, 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,70316, 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,70416, 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,70516, 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,70617, 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,70717, 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,70817, 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, 17709};710711static const mz_uint8 s_tdefl_small_dist_extra[512] =712{7130, 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,7145, 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,7156, 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,7166, 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,7177, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,7187, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,7197, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,7207, 7, 7, 7, 7, 7, 7, 7721};722723static const mz_uint8 s_tdefl_large_dist_sym[128] =724{7250, 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,72626, 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,72728, 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, 29728};729730static const mz_uint8 s_tdefl_large_dist_extra[128] =731{7320, 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,73312, 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,73413, 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, 13735};736737/* Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted values. */738typedef struct739{740mz_uint16 m_key, m_sym_index;741} tdefl_sym_freq;742static tdefl_sym_freq *tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq *pSyms0, tdefl_sym_freq *pSyms1)743{744mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];745tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1;746MZ_CLEAR_OBJ(hist);747for (i = 0; i < num_syms; i++)748{749mz_uint freq = pSyms0[i].m_key;750hist[freq & 0xFF]++;751hist[256 + ((freq >> 8) & 0xFF)]++;752}753while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))754total_passes--;755for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8)756{757const mz_uint32 *pHist = &hist[pass << 8];758mz_uint offsets[256], cur_ofs = 0;759for (i = 0; i < 256; i++)760{761offsets[i] = cur_ofs;762cur_ofs += pHist[i];763}764for (i = 0; i < num_syms; i++)765pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];766{767tdefl_sym_freq *t = pCur_syms;768pCur_syms = pNew_syms;769pNew_syms = t;770}771}772return pCur_syms;773}774775/* tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, [email protected], Jyrki Katajainen, [email protected], November 1996. */776static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n)777{778int root, leaf, next, avbl, used, dpth;779if (n == 0)780return;781else if (n == 1)782{783A[0].m_key = 1;784return;785}786A[0].m_key += A[1].m_key;787root = 0;788leaf = 2;789for (next = 1; next < n - 1; next++)790{791if (leaf >= n || A[root].m_key < A[leaf].m_key)792{793A[next].m_key = A[root].m_key;794A[root++].m_key = (mz_uint16)next;795}796else797A[next].m_key = A[leaf++].m_key;798if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key))799{800A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key);801A[root++].m_key = (mz_uint16)next;802}803else804A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key);805}806A[n - 2].m_key = 0;807for (next = n - 3; next >= 0; next--)808A[next].m_key = A[A[next].m_key].m_key + 1;809avbl = 1;810used = dpth = 0;811root = n - 2;812next = n - 1;813while (avbl > 0)814{815while (root >= 0 && (int)A[root].m_key == dpth)816{817used++;818root--;819}820while (avbl > used)821{822A[next--].m_key = (mz_uint16)(dpth);823avbl--;824}825avbl = 2 * used;826dpth++;827used = 0;828}829}830831/* Limits canonical Huffman code table's max code size. */832enum833{834TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32835};836static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, int code_list_len, int max_code_size)837{838int i;839mz_uint32 total = 0;840if (code_list_len <= 1)841return;842for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++)843pNum_codes[max_code_size] += pNum_codes[i];844for (i = max_code_size; i > 0; i--)845total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i));846while (total != (1UL << max_code_size))847{848pNum_codes[max_code_size]--;849for (i = max_code_size - 1; i > 0; i--)850if (pNum_codes[i])851{852pNum_codes[i]--;853pNum_codes[i + 1] += 2;854break;855}856total--;857}858}859860static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int table_len, int code_size_limit, int static_table)861{862int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE];863mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1];864MZ_CLEAR_OBJ(num_codes);865if (static_table)866{867for (i = 0; i < table_len; i++)868num_codes[d->m_huff_code_sizes[table_num][i]]++;869}870else871{872tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], *pSyms;873int num_used_syms = 0;874const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0];875for (i = 0; i < table_len; i++)876if (pSym_count[i])877{878syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i];879syms0[num_used_syms++].m_sym_index = (mz_uint16)i;880}881882pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1);883tdefl_calculate_minimum_redundancy(pSyms, num_used_syms);884885for (i = 0; i < num_used_syms; i++)886num_codes[pSyms[i].m_key]++;887888tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit);889890MZ_CLEAR_OBJ(d->m_huff_code_sizes[table_num]);891MZ_CLEAR_OBJ(d->m_huff_codes[table_num]);892for (i = 1, j = num_used_syms; i <= code_size_limit; i++)893for (l = num_codes[i]; l > 0; l--)894d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i);895}896897next_code[1] = 0;898for (j = 0, i = 2; i <= code_size_limit; i++)899next_code[i] = j = ((j + num_codes[i - 1]) << 1);900901for (i = 0; i < table_len; i++)902{903mz_uint rev_code = 0, code, code_size;904if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0)905continue;906code = next_code[code_size]++;907for (l = code_size; l > 0; l--, code >>= 1)908rev_code = (rev_code << 1) | (code & 1);909d->m_huff_codes[table_num][i] = (mz_uint16)rev_code;910}911}912913#define TDEFL_PUT_BITS(b, l) \914do \915{ \916mz_uint bits = b; \917mz_uint len = l; \918MZ_ASSERT(bits <= ((1U << len) - 1U)); \919d->m_bit_buffer |= (bits << d->m_bits_in); \920d->m_bits_in += len; \921while (d->m_bits_in >= 8) \922{ \923if (d->m_pOutput_buf < d->m_pOutput_buf_end) \924*d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \925d->m_bit_buffer >>= 8; \926d->m_bits_in -= 8; \927} \928} \929MZ_MACRO_END930931#define TDEFL_RLE_PREV_CODE_SIZE() \932{ \933if (rle_repeat_count) \934{ \935if (rle_repeat_count < 3) \936{ \937d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \938while (rle_repeat_count--) \939packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \940} \941else \942{ \943d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \944packed_code_sizes[num_packed_code_sizes++] = 16; \945packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \946} \947rle_repeat_count = 0; \948} \949}950951#define TDEFL_RLE_ZERO_CODE_SIZE() \952{ \953if (rle_z_count) \954{ \955if (rle_z_count < 3) \956{ \957d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \958while (rle_z_count--) \959packed_code_sizes[num_packed_code_sizes++] = 0; \960} \961else if (rle_z_count <= 10) \962{ \963d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \964packed_code_sizes[num_packed_code_sizes++] = 17; \965packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \966} \967else \968{ \969d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \970packed_code_sizes[num_packed_code_sizes++] = 18; \971packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \972} \973rle_z_count = 0; \974} \975}976977static 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 };978979static void tdefl_start_dynamic_block(tdefl_compressor *d)980{981int num_lit_codes, num_dist_codes, num_bit_lengths;982mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index;983mz_uint8 code_sizes_to_pack[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], packed_code_sizes[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], prev_code_size = 0xFF;984985d->m_huff_count[0][256] = 1;986987tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE);988tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE);989990for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--)991if (d->m_huff_code_sizes[0][num_lit_codes - 1])992break;993for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--)994if (d->m_huff_code_sizes[1][num_dist_codes - 1])995break;996997memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes);998memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes);999total_code_sizes_to_pack = num_lit_codes + num_dist_codes;1000num_packed_code_sizes = 0;1001rle_z_count = 0;1002rle_repeat_count = 0;10031004memset(&d->m_huff_count[2][0], 0, sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2);1005for (i = 0; i < total_code_sizes_to_pack; i++)1006{1007mz_uint8 code_size = code_sizes_to_pack[i];1008if (!code_size)1009{1010TDEFL_RLE_PREV_CODE_SIZE();1011if (++rle_z_count == 138)1012{1013TDEFL_RLE_ZERO_CODE_SIZE();1014}1015}1016else1017{1018TDEFL_RLE_ZERO_CODE_SIZE();1019if (code_size != prev_code_size)1020{1021TDEFL_RLE_PREV_CODE_SIZE();1022d->m_huff_count[2][code_size] = (mz_uint16)(d->m_huff_count[2][code_size] + 1);1023packed_code_sizes[num_packed_code_sizes++] = code_size;1024}1025else if (++rle_repeat_count == 6)1026{1027TDEFL_RLE_PREV_CODE_SIZE();1028}1029}1030prev_code_size = code_size;1031}1032if (rle_repeat_count)1033{1034TDEFL_RLE_PREV_CODE_SIZE();1035}1036else1037{1038TDEFL_RLE_ZERO_CODE_SIZE();1039}10401041tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE);10421043TDEFL_PUT_BITS(2, 2);10441045TDEFL_PUT_BITS(num_lit_codes - 257, 5);1046TDEFL_PUT_BITS(num_dist_codes - 1, 5);10471048for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)1049if (d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]])1050break;1051num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1));1052TDEFL_PUT_BITS(num_bit_lengths - 4, 4);1053for (i = 0; (int)i < num_bit_lengths; i++)1054TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3);10551056for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;)1057{1058mz_uint code = packed_code_sizes[packed_code_sizes_index++];1059MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2);1060TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]);1061if (code >= 16)1062TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]);1063}1064}10651066static void tdefl_start_static_block(tdefl_compressor *d)1067{1068mz_uint i;1069mz_uint8 *p = &d->m_huff_code_sizes[0][0];10701071for (i = 0; i <= 143; ++i)1072*p++ = 8;1073for (; i <= 255; ++i)1074*p++ = 9;1075for (; i <= 279; ++i)1076*p++ = 7;1077for (; i <= 287; ++i)1078*p++ = 8;10791080memset(d->m_huff_code_sizes[1], 5, 32);10811082tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE);1083tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE);10841085TDEFL_PUT_BITS(1, 2);1086}10871088static const mz_uint mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };10891090#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS1091static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)1092{1093mz_uint flags;1094mz_uint8 *pLZ_codes;1095mz_uint8 *pOutput_buf = d->m_pOutput_buf;1096mz_uint8 *pLZ_code_buf_end = d->m_pLZ_code_buf;1097mz_uint64 bit_buffer = d->m_bit_buffer;1098mz_uint bits_in = d->m_bits_in;10991100#define TDEFL_PUT_BITS_FAST(b, l) \1101{ \1102bit_buffer |= (((mz_uint64)(b)) << bits_in); \1103bits_in += (l); \1104}11051106flags = 1;1107for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1)1108{1109if (flags == 1)1110flags = *pLZ_codes++ | 0x100;11111112if (flags & 1)1113{1114mz_uint s0, s1, n0, n1, sym, num_extra_bits;1115mz_uint match_len = pLZ_codes[0], match_dist = *(const mz_uint16 *)(pLZ_codes + 1);1116pLZ_codes += 3;11171118MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);1119TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);1120TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);11211122/* This sequence coaxes MSVC into using cmov's vs. jmp's. */1123s0 = s_tdefl_small_dist_sym[match_dist & 511];1124n0 = s_tdefl_small_dist_extra[match_dist & 511];1125s1 = s_tdefl_large_dist_sym[match_dist >> 8];1126n1 = s_tdefl_large_dist_extra[match_dist >> 8];1127sym = (match_dist < 512) ? s0 : s1;1128num_extra_bits = (match_dist < 512) ? n0 : n1;11291130MZ_ASSERT(d->m_huff_code_sizes[1][sym]);1131TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);1132TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);1133}1134else1135{1136mz_uint lit = *pLZ_codes++;1137MZ_ASSERT(d->m_huff_code_sizes[0][lit]);1138TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);11391140if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))1141{1142flags >>= 1;1143lit = *pLZ_codes++;1144MZ_ASSERT(d->m_huff_code_sizes[0][lit]);1145TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);11461147if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))1148{1149flags >>= 1;1150lit = *pLZ_codes++;1151MZ_ASSERT(d->m_huff_code_sizes[0][lit]);1152TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);1153}1154}1155}11561157if (pOutput_buf >= d->m_pOutput_buf_end)1158return MZ_FALSE;11591160*(mz_uint64 *)pOutput_buf = bit_buffer;1161pOutput_buf += (bits_in >> 3);1162bit_buffer >>= (bits_in & ~7);1163bits_in &= 7;1164}11651166#undef TDEFL_PUT_BITS_FAST11671168d->m_pOutput_buf = pOutput_buf;1169d->m_bits_in = 0;1170d->m_bit_buffer = 0;11711172while (bits_in)1173{1174mz_uint32 n = MZ_MIN(bits_in, 16);1175TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n);1176bit_buffer >>= n;1177bits_in -= n;1178}11791180TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);11811182return (d->m_pOutput_buf < d->m_pOutput_buf_end);1183}1184#else1185static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)1186{1187mz_uint flags;1188mz_uint8 *pLZ_codes;11891190flags = 1;1191for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1)1192{1193if (flags == 1)1194flags = *pLZ_codes++ | 0x100;1195if (flags & 1)1196{1197mz_uint sym, num_extra_bits;1198mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));1199pLZ_codes += 3;12001201MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);1202TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);1203TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);12041205if (match_dist < 512)1206{1207sym = s_tdefl_small_dist_sym[match_dist];1208num_extra_bits = s_tdefl_small_dist_extra[match_dist];1209}1210else1211{1212sym = s_tdefl_large_dist_sym[match_dist >> 8];1213num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8];1214}1215MZ_ASSERT(d->m_huff_code_sizes[1][sym]);1216TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);1217TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);1218}1219else1220{1221mz_uint lit = *pLZ_codes++;1222MZ_ASSERT(d->m_huff_code_sizes[0][lit]);1223TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);1224}1225}12261227TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);12281229return (d->m_pOutput_buf < d->m_pOutput_buf_end);1230}1231#endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS */12321233static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block)1234{1235if (static_block)1236tdefl_start_static_block(d);1237else1238tdefl_start_dynamic_block(d);1239return tdefl_compress_lz_codes(d);1240}12411242static int tdefl_flush_block(tdefl_compressor *d, int flush)1243{1244mz_uint saved_bit_buf, saved_bits_in;1245mz_uint8 *pSaved_output_buf;1246mz_bool comp_block_succeeded = MZ_FALSE;1247int n, use_raw_block = ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size;1248mz_uint8 *pOutput_buf_start = ((d->m_pPut_buf_func == NULL) && ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE)) ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs) : d->m_output_buf;12491250d->m_pOutput_buf = pOutput_buf_start;1251d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16;12521253MZ_ASSERT(!d->m_output_flush_remaining);1254d->m_output_flush_ofs = 0;1255d->m_output_flush_remaining = 0;12561257*d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left);1258d->m_pLZ_code_buf -= (d->m_num_flags_left == 8);12591260if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index))1261{1262TDEFL_PUT_BITS(0x78, 8);1263TDEFL_PUT_BITS(0x01, 8);1264}12651266TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1);12671268pSaved_output_buf = d->m_pOutput_buf;1269saved_bit_buf = d->m_bit_buffer;1270saved_bits_in = d->m_bits_in;12711272if (!use_raw_block)1273comp_block_succeeded = tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48));12741275/* If the block gets expanded, forget the current contents of the output buffer and send a raw block instead. */1276if (((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) &&1277((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size))1278{1279mz_uint i;1280d->m_pOutput_buf = pSaved_output_buf;1281d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;1282TDEFL_PUT_BITS(0, 2);1283if (d->m_bits_in)1284{1285TDEFL_PUT_BITS(0, 8 - d->m_bits_in);1286}1287for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF)1288{1289TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16);1290}1291for (i = 0; i < d->m_total_lz_bytes; ++i)1292{1293TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8);1294}1295}1296/* Check for the extremely unlikely (if not impossible) case of the compressed block not fitting into the output buffer when using dynamic codes. */1297else if (!comp_block_succeeded)1298{1299d->m_pOutput_buf = pSaved_output_buf;1300d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;1301tdefl_compress_block(d, MZ_TRUE);1302}13031304if (flush)1305{1306if (flush == TDEFL_FINISH)1307{1308if (d->m_bits_in)1309{1310TDEFL_PUT_BITS(0, 8 - d->m_bits_in);1311}1312if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER)1313{1314mz_uint i, a = d->m_adler32;1315for (i = 0; i < 4; i++)1316{1317TDEFL_PUT_BITS((a >> 24) & 0xFF, 8);1318a <<= 8;1319}1320}1321}1322else1323{1324mz_uint i, z = 0;1325TDEFL_PUT_BITS(0, 3);1326if (d->m_bits_in)1327{1328TDEFL_PUT_BITS(0, 8 - d->m_bits_in);1329}1330for (i = 2; i; --i, z ^= 0xFFFF)1331{1332TDEFL_PUT_BITS(z & 0xFFFF, 16);1333}1334}1335}13361337MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end);13381339memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);1340memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);13411342d->m_pLZ_code_buf = d->m_lz_code_buf + 1;1343d->m_pLZ_flags = d->m_lz_code_buf;1344d->m_num_flags_left = 8;1345d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes;1346d->m_total_lz_bytes = 0;1347d->m_block_index++;13481349if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0)1350{1351if (d->m_pPut_buf_func)1352{1353*d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;1354if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user))1355return (d->m_prev_return_status = TDEFL_STATUS_PUT_BUF_FAILED);1356}1357else if (pOutput_buf_start == d->m_output_buf)1358{1359int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs));1360memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy);1361d->m_out_buf_ofs += bytes_to_copy;1362if ((n -= bytes_to_copy) != 0)1363{1364d->m_output_flush_ofs = bytes_to_copy;1365d->m_output_flush_remaining = n;1366}1367}1368else1369{1370d->m_out_buf_ofs += n;1371}1372}13731374return d->m_output_flush_remaining;1375}13761377#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES1378#ifdef MINIZ_UNALIGNED_USE_MEMCPY1379static mz_uint16 TDEFL_READ_UNALIGNED_WORD(const mz_uint8* p)1380{1381mz_uint16 ret;1382memcpy(&ret, p, sizeof(mz_uint16));1383return ret;1384}1385static mz_uint16 TDEFL_READ_UNALIGNED_WORD2(const mz_uint16* p)1386{1387mz_uint16 ret;1388memcpy(&ret, p, sizeof(mz_uint16));1389return ret;1390}1391#else1392#define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16 *)(p)1393#define TDEFL_READ_UNALIGNED_WORD2(p) *(const mz_uint16 *)(p)1394#endif1395static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len)1396{1397mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;1398mz_uint num_probes_left = d->m_max_probes[match_len >= 32];1399const mz_uint16 *s = (const mz_uint16 *)(d->m_dict + pos), *p, *q;1400mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD2(s);1401MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);1402if (max_match_len <= match_len)1403return;1404for (;;)1405{1406for (;;)1407{1408if (--num_probes_left == 0)1409return;1410#define TDEFL_PROBE \1411next_probe_pos = d->m_next[probe_pos]; \1412if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \1413return; \1414probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \1415if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) \1416break;1417TDEFL_PROBE;1418TDEFL_PROBE;1419TDEFL_PROBE;1420}1421if (!dist)1422break;1423q = (const mz_uint16 *)(d->m_dict + probe_pos);1424if (TDEFL_READ_UNALIGNED_WORD2(q) != s01)1425continue;1426p = s;1427probe_len = 32;1428do1429{1430} while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&1431(TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0));1432if (!probe_len)1433{1434*pMatch_dist = dist;1435*pMatch_len = MZ_MIN(max_match_len, (mz_uint)TDEFL_MAX_MATCH_LEN);1436break;1437}1438else if ((probe_len = ((mz_uint)(p - s) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q)) > match_len)1439{1440*pMatch_dist = dist;1441if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) == max_match_len)1442break;1443c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]);1444}1445}1446}1447#else1448static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len)1449{1450mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;1451mz_uint num_probes_left = d->m_max_probes[match_len >= 32];1452const mz_uint8 *s = d->m_dict + pos, *p, *q;1453mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1];1454MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);1455if (max_match_len <= match_len)1456return;1457for (;;)1458{1459for (;;)1460{1461if (--num_probes_left == 0)1462return;1463#define TDEFL_PROBE \1464next_probe_pos = d->m_next[probe_pos]; \1465if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \1466return; \1467probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \1468if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) \1469break;1470TDEFL_PROBE;1471TDEFL_PROBE;1472TDEFL_PROBE;1473}1474if (!dist)1475break;1476p = s;1477q = d->m_dict + probe_pos;1478for (probe_len = 0; probe_len < max_match_len; probe_len++)1479if (*p++ != *q++)1480break;1481if (probe_len > match_len)1482{1483*pMatch_dist = dist;1484if ((*pMatch_len = match_len = probe_len) == max_match_len)1485return;1486c0 = d->m_dict[pos + match_len];1487c1 = d->m_dict[pos + match_len - 1];1488}1489}1490}1491#endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES */14921493#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN1494#ifdef MINIZ_UNALIGNED_USE_MEMCPY1495static mz_uint32 TDEFL_READ_UNALIGNED_WORD32(const mz_uint8* p)1496{1497mz_uint32 ret;1498memcpy(&ret, p, sizeof(mz_uint32));1499return ret;1500}1501#else1502#define TDEFL_READ_UNALIGNED_WORD32(p) *(const mz_uint32 *)(p)1503#endif1504static mz_bool tdefl_compress_fast(tdefl_compressor *d)1505{1506/* Faster, minimally featured LZRW1-style match+parse loop with better register utilization. Intended for applications where raw throughput is valued more highly than ratio. */1507mz_uint lookahead_pos = d->m_lookahead_pos, lookahead_size = d->m_lookahead_size, dict_size = d->m_dict_size, total_lz_bytes = d->m_total_lz_bytes, num_flags_left = d->m_num_flags_left;1508mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags;1509mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;15101511while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size)))1512{1513const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096;1514mz_uint dst_pos = (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;1515mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size);1516d->m_src_buf_left -= num_bytes_to_process;1517lookahead_size += num_bytes_to_process;15181519while (num_bytes_to_process)1520{1521mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process);1522memcpy(d->m_dict + dst_pos, d->m_pSrc, n);1523if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))1524memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos));1525d->m_pSrc += n;1526dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK;1527num_bytes_to_process -= n;1528}15291530dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size);1531if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE))1532break;15331534while (lookahead_size >= 4)1535{1536mz_uint cur_match_dist, cur_match_len = 1;1537mz_uint8 *pCur_dict = d->m_dict + cur_pos;1538mz_uint first_trigram = TDEFL_READ_UNALIGNED_WORD32(pCur_dict) & 0xFFFFFF;1539mz_uint hash = (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) & TDEFL_LEVEL1_HASH_SIZE_MASK;1540mz_uint probe_pos = d->m_hash[hash];1541d->m_hash[hash] = (mz_uint16)lookahead_pos;15421543if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <= dict_size) && ((TDEFL_READ_UNALIGNED_WORD32(d->m_dict + (probe_pos &= TDEFL_LZ_DICT_SIZE_MASK)) & 0xFFFFFF) == first_trigram))1544{1545const mz_uint16 *p = (const mz_uint16 *)pCur_dict;1546const mz_uint16 *q = (const mz_uint16 *)(d->m_dict + probe_pos);1547mz_uint32 probe_len = 32;1548do1549{1550} while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&1551(TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0));1552cur_match_len = ((mz_uint)(p - (const mz_uint16 *)pCur_dict) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q);1553if (!probe_len)1554cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0;15551556if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || ((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)))1557{1558cur_match_len = 1;1559*pLZ_code_buf++ = (mz_uint8)first_trigram;1560*pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);1561d->m_huff_count[0][(mz_uint8)first_trigram]++;1562}1563else1564{1565mz_uint32 s0, s1;1566cur_match_len = MZ_MIN(cur_match_len, lookahead_size);15671568MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 1) && (cur_match_dist <= TDEFL_LZ_DICT_SIZE));15691570cur_match_dist--;15711572pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN);1573#ifdef MINIZ_UNALIGNED_USE_MEMCPY1574memcpy(&pLZ_code_buf[1], &cur_match_dist, sizeof(cur_match_dist));1575#else1576*(mz_uint16 *)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist;1577#endif1578pLZ_code_buf += 3;1579*pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80);15801581s0 = s_tdefl_small_dist_sym[cur_match_dist & 511];1582s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8];1583d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++;15841585d->m_huff_count[0][s_tdefl_len_sym[cur_match_len - TDEFL_MIN_MATCH_LEN]]++;1586}1587}1588else1589{1590*pLZ_code_buf++ = (mz_uint8)first_trigram;1591*pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);1592d->m_huff_count[0][(mz_uint8)first_trigram]++;1593}15941595if (--num_flags_left == 0)1596{1597num_flags_left = 8;1598pLZ_flags = pLZ_code_buf++;1599}16001601total_lz_bytes += cur_match_len;1602lookahead_pos += cur_match_len;1603dict_size = MZ_MIN(dict_size + cur_match_len, (mz_uint)TDEFL_LZ_DICT_SIZE);1604cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK;1605MZ_ASSERT(lookahead_size >= cur_match_len);1606lookahead_size -= cur_match_len;16071608if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8])1609{1610int n;1611d->m_lookahead_pos = lookahead_pos;1612d->m_lookahead_size = lookahead_size;1613d->m_dict_size = dict_size;1614d->m_total_lz_bytes = total_lz_bytes;1615d->m_pLZ_code_buf = pLZ_code_buf;1616d->m_pLZ_flags = pLZ_flags;1617d->m_num_flags_left = num_flags_left;1618if ((n = tdefl_flush_block(d, 0)) != 0)1619return (n < 0) ? MZ_FALSE : MZ_TRUE;1620total_lz_bytes = d->m_total_lz_bytes;1621pLZ_code_buf = d->m_pLZ_code_buf;1622pLZ_flags = d->m_pLZ_flags;1623num_flags_left = d->m_num_flags_left;1624}1625}16261627while (lookahead_size)1628{1629mz_uint8 lit = d->m_dict[cur_pos];16301631total_lz_bytes++;1632*pLZ_code_buf++ = lit;1633*pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);1634if (--num_flags_left == 0)1635{1636num_flags_left = 8;1637pLZ_flags = pLZ_code_buf++;1638}16391640d->m_huff_count[0][lit]++;16411642lookahead_pos++;1643dict_size = MZ_MIN(dict_size + 1, (mz_uint)TDEFL_LZ_DICT_SIZE);1644cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;1645lookahead_size--;16461647if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8])1648{1649int n;1650d->m_lookahead_pos = lookahead_pos;1651d->m_lookahead_size = lookahead_size;1652d->m_dict_size = dict_size;1653d->m_total_lz_bytes = total_lz_bytes;1654d->m_pLZ_code_buf = pLZ_code_buf;1655d->m_pLZ_flags = pLZ_flags;1656d->m_num_flags_left = num_flags_left;1657if ((n = tdefl_flush_block(d, 0)) != 0)1658return (n < 0) ? MZ_FALSE : MZ_TRUE;1659total_lz_bytes = d->m_total_lz_bytes;1660pLZ_code_buf = d->m_pLZ_code_buf;1661pLZ_flags = d->m_pLZ_flags;1662num_flags_left = d->m_num_flags_left;1663}1664}1665}16661667d->m_lookahead_pos = lookahead_pos;1668d->m_lookahead_size = lookahead_size;1669d->m_dict_size = dict_size;1670d->m_total_lz_bytes = total_lz_bytes;1671d->m_pLZ_code_buf = pLZ_code_buf;1672d->m_pLZ_flags = pLZ_flags;1673d->m_num_flags_left = num_flags_left;1674return MZ_TRUE;1675}1676#endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */16771678static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d, mz_uint8 lit)1679{1680d->m_total_lz_bytes++;1681*d->m_pLZ_code_buf++ = lit;1682*d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1);1683if (--d->m_num_flags_left == 0)1684{1685d->m_num_flags_left = 8;1686d->m_pLZ_flags = d->m_pLZ_code_buf++;1687}1688d->m_huff_count[0][lit]++;1689}16901691static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, mz_uint match_len, mz_uint match_dist)1692{1693mz_uint32 s0, s1;16941695MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && (match_dist <= TDEFL_LZ_DICT_SIZE));16961697d->m_total_lz_bytes += match_len;16981699d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN);17001701match_dist -= 1;1702d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF);1703d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8);1704d->m_pLZ_code_buf += 3;17051706*d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80);1707if (--d->m_num_flags_left == 0)1708{1709d->m_num_flags_left = 8;1710d->m_pLZ_flags = d->m_pLZ_code_buf++;1711}17121713s0 = s_tdefl_small_dist_sym[match_dist & 511];1714s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127];1715d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++;1716d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++;1717}17181719static mz_bool tdefl_compress_normal(tdefl_compressor *d)1720{1721const mz_uint8 *pSrc = d->m_pSrc;1722size_t src_buf_left = d->m_src_buf_left;1723tdefl_flush flush = d->m_flush;17241725while ((src_buf_left) || ((flush) && (d->m_lookahead_size)))1726{1727mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;1728/* Update dictionary and hash chains. Keeps the lookahead size equal to TDEFL_MAX_MATCH_LEN. */1729if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1))1730{1731mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK, ins_pos = d->m_lookahead_pos + d->m_lookahead_size - 2;1732mz_uint hash = (d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK];1733mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size);1734const mz_uint8 *pSrc_end = pSrc + num_bytes_to_process;1735src_buf_left -= num_bytes_to_process;1736d->m_lookahead_size += num_bytes_to_process;1737while (pSrc != pSrc_end)1738{1739mz_uint8 c = *pSrc++;1740d->m_dict[dst_pos] = c;1741if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))1742d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;1743hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);1744d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];1745d->m_hash[hash] = (mz_uint16)(ins_pos);1746dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;1747ins_pos++;1748}1749}1750else1751{1752while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))1753{1754mz_uint8 c = *pSrc++;1755mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;1756src_buf_left--;1757d->m_dict[dst_pos] = c;1758if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))1759d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;1760if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN)1761{1762mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2;1763mz_uint hash = ((d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << (TDEFL_LZ_HASH_SHIFT * 2)) ^ (d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);1764d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];1765d->m_hash[hash] = (mz_uint16)(ins_pos);1766}1767}1768}1769d->m_dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size);1770if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))1771break;17721773/* Simple lazy/greedy parsing state machine. */1774len_to_move = 1;1775cur_match_dist = 0;1776cur_match_len = d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1);1777cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;1778if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS))1779{1780if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))1781{1782mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK];1783cur_match_len = 0;1784while (cur_match_len < d->m_lookahead_size)1785{1786if (d->m_dict[cur_pos + cur_match_len] != c)1787break;1788cur_match_len++;1789}1790if (cur_match_len < TDEFL_MIN_MATCH_LEN)1791cur_match_len = 0;1792else1793cur_match_dist = 1;1794}1795}1796else1797{1798tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len);1799}1800if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5)))1801{1802cur_match_dist = cur_match_len = 0;1803}1804if (d->m_saved_match_len)1805{1806if (cur_match_len > d->m_saved_match_len)1807{1808tdefl_record_literal(d, (mz_uint8)d->m_saved_lit);1809if (cur_match_len >= 128)1810{1811tdefl_record_match(d, cur_match_len, cur_match_dist);1812d->m_saved_match_len = 0;1813len_to_move = cur_match_len;1814}1815else1816{1817d->m_saved_lit = d->m_dict[cur_pos];1818d->m_saved_match_dist = cur_match_dist;1819d->m_saved_match_len = cur_match_len;1820}1821}1822else1823{1824tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist);1825len_to_move = d->m_saved_match_len - 1;1826d->m_saved_match_len = 0;1827}1828}1829else if (!cur_match_dist)1830tdefl_record_literal(d, d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]);1831else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128))1832{1833tdefl_record_match(d, cur_match_len, cur_match_dist);1834len_to_move = cur_match_len;1835}1836else1837{1838d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)];1839d->m_saved_match_dist = cur_match_dist;1840d->m_saved_match_len = cur_match_len;1841}1842/* Move the lookahead forward by len_to_move bytes. */1843d->m_lookahead_pos += len_to_move;1844MZ_ASSERT(d->m_lookahead_size >= len_to_move);1845d->m_lookahead_size -= len_to_move;1846d->m_dict_size = MZ_MIN(d->m_dict_size + len_to_move, (mz_uint)TDEFL_LZ_DICT_SIZE);1847/* Check if it's time to flush the current LZ codes to the internal output buffer. */1848if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) ||1849((d->m_total_lz_bytes > 31 * 1024) && (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= d->m_total_lz_bytes) || (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))))1850{1851int n;1852d->m_pSrc = pSrc;1853d->m_src_buf_left = src_buf_left;1854if ((n = tdefl_flush_block(d, 0)) != 0)1855return (n < 0) ? MZ_FALSE : MZ_TRUE;1856}1857}18581859d->m_pSrc = pSrc;1860d->m_src_buf_left = src_buf_left;1861return MZ_TRUE;1862}18631864static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d)1865{1866if (d->m_pIn_buf_size)1867{1868*d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;1869}18701871if (d->m_pOut_buf_size)1872{1873size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, d->m_output_flush_remaining);1874memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n);1875d->m_output_flush_ofs += (mz_uint)n;1876d->m_output_flush_remaining -= (mz_uint)n;1877d->m_out_buf_ofs += n;18781879*d->m_pOut_buf_size = d->m_out_buf_ofs;1880}18811882return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY;1883}18841885tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush)1886{1887if (!d)1888{1889if (pIn_buf_size)1890*pIn_buf_size = 0;1891if (pOut_buf_size)1892*pOut_buf_size = 0;1893return TDEFL_STATUS_BAD_PARAM;1894}18951896d->m_pIn_buf = pIn_buf;1897d->m_pIn_buf_size = pIn_buf_size;1898d->m_pOut_buf = pOut_buf;1899d->m_pOut_buf_size = pOut_buf_size;1900d->m_pSrc = (const mz_uint8 *)(pIn_buf);1901d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0;1902d->m_out_buf_ofs = 0;1903d->m_flush = flush;19041905if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) ||1906(d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf))1907{1908if (pIn_buf_size)1909*pIn_buf_size = 0;1910if (pOut_buf_size)1911*pOut_buf_size = 0;1912return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM);1913}1914d->m_wants_to_finish |= (flush == TDEFL_FINISH);19151916if ((d->m_output_flush_remaining) || (d->m_finished))1917return (d->m_prev_return_status = tdefl_flush_output_buffer(d));19181919#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN1920if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) &&1921((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) &&1922((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS | TDEFL_RLE_MATCHES)) == 0))1923{1924if (!tdefl_compress_fast(d))1925return d->m_prev_return_status;1926}1927else1928#endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */1929{1930if (!tdefl_compress_normal(d))1931return d->m_prev_return_status;1932}19331934if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf))1935d->m_adler32 = (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8 *)pIn_buf, d->m_pSrc - (const mz_uint8 *)pIn_buf);19361937if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining))1938{1939if (tdefl_flush_block(d, flush) < 0)1940return d->m_prev_return_status;1941d->m_finished = (flush == TDEFL_FINISH);1942if (flush == TDEFL_FULL_FLUSH)1943{1944MZ_CLEAR_OBJ(d->m_hash);1945MZ_CLEAR_OBJ(d->m_next);1946d->m_dict_size = 0;1947}1948}19491950return (d->m_prev_return_status = tdefl_flush_output_buffer(d));1951}19521953tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush)1954{1955MZ_ASSERT(d->m_pPut_buf_func);1956return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush);1957}19581959tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)1960{1961d->m_pPut_buf_func = pPut_buf_func;1962d->m_pPut_buf_user = pPut_buf_user;1963d->m_flags = (mz_uint)(flags);1964d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3;1965d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0;1966d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3;1967if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))1968MZ_CLEAR_OBJ(d->m_hash);1969d->m_lookahead_pos = d->m_lookahead_size = d->m_dict_size = d->m_total_lz_bytes = d->m_lz_code_buf_dict_pos = d->m_bits_in = 0;1970d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = d->m_block_index = d->m_bit_buffer = d->m_wants_to_finish = 0;1971d->m_pLZ_code_buf = d->m_lz_code_buf + 1;1972d->m_pLZ_flags = d->m_lz_code_buf;1973*d->m_pLZ_flags = 0;1974d->m_num_flags_left = 8;1975d->m_pOutput_buf = d->m_output_buf;1976d->m_pOutput_buf_end = d->m_output_buf;1977d->m_prev_return_status = TDEFL_STATUS_OKAY;1978d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0;1979d->m_adler32 = 1;1980d->m_pIn_buf = NULL;1981d->m_pOut_buf = NULL;1982d->m_pIn_buf_size = NULL;1983d->m_pOut_buf_size = NULL;1984d->m_flush = TDEFL_NO_FLUSH;1985d->m_pSrc = NULL;1986d->m_src_buf_left = 0;1987d->m_out_buf_ofs = 0;1988if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))1989MZ_CLEAR_OBJ(d->m_dict);1990memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);1991memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);1992return TDEFL_STATUS_OKAY;1993}19941995tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d)1996{1997return d->m_prev_return_status;1998}19992000mz_uint32 tdefl_get_adler32(tdefl_compressor *d)2001{2002return d->m_adler32;2003}20042005mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)2006{2007tdefl_compressor *pComp;2008mz_bool succeeded;2009if (((buf_len) && (!pBuf)) || (!pPut_buf_func))2010return MZ_FALSE;2011pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));2012if (!pComp)2013return MZ_FALSE;2014succeeded = (tdefl_init(pComp, pPut_buf_func, pPut_buf_user, flags) == TDEFL_STATUS_OKAY);2015succeeded = succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == TDEFL_STATUS_DONE);2016MZ_FREE(pComp);2017return succeeded;2018}20192020typedef struct2021{2022size_t m_size, m_capacity;2023mz_uint8 *m_pBuf;2024mz_bool m_expandable;2025} tdefl_output_buffer;20262027static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len, void *pUser)2028{2029tdefl_output_buffer *p = (tdefl_output_buffer *)pUser;2030size_t new_size = p->m_size + len;2031if (new_size > p->m_capacity)2032{2033size_t new_capacity = p->m_capacity;2034mz_uint8 *pNew_buf;2035if (!p->m_expandable)2036return MZ_FALSE;2037do2038{2039new_capacity = MZ_MAX(128U, new_capacity << 1U);2040} while (new_size > new_capacity);2041pNew_buf = (mz_uint8 *)MZ_REALLOC(p->m_pBuf, new_capacity);2042if (!pNew_buf)2043return MZ_FALSE;2044p->m_pBuf = pNew_buf;2045p->m_capacity = new_capacity;2046}2047memcpy((mz_uint8 *)p->m_pBuf + p->m_size, pBuf, len);2048p->m_size = new_size;2049return MZ_TRUE;2050}20512052void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)2053{2054tdefl_output_buffer out_buf;2055MZ_CLEAR_OBJ(out_buf);2056if (!pOut_len)2057return MZ_FALSE;2058else2059*pOut_len = 0;2060out_buf.m_expandable = MZ_TRUE;2061if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))2062return NULL;2063*pOut_len = out_buf.m_size;2064return out_buf.m_pBuf;2065}20662067size_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)2068{2069tdefl_output_buffer out_buf;2070MZ_CLEAR_OBJ(out_buf);2071if (!pOut_buf)2072return 0;2073out_buf.m_pBuf = (mz_uint8 *)pOut_buf;2074out_buf.m_capacity = out_buf_len;2075if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))2076return 0;2077return out_buf.m_size;2078}20792080static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };20812082/* level may actually range from [0,10] (10 is a "hidden" max level, where we want a bit more compression and it's fine if throughput to fall off a cliff on some files). */2083mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy)2084{2085mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0);2086if (window_bits > 0)2087comp_flags |= TDEFL_WRITE_ZLIB_HEADER;20882089if (!level)2090comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS;2091else if (strategy == MZ_FILTERED)2092comp_flags |= TDEFL_FILTER_MATCHES;2093else if (strategy == MZ_HUFFMAN_ONLY)2094comp_flags &= ~TDEFL_MAX_PROBES_MASK;2095else if (strategy == MZ_FIXED)2096comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS;2097else if (strategy == MZ_RLE)2098comp_flags |= TDEFL_RLE_MATCHES;20992100return comp_flags;2101}21022103#ifdef _MSC_VER2104#pragma warning(push)2105#pragma warning(disable : 4204) /* nonstandard extension used : non-constant aggregate initializer (also supported by GNU C and C99, so no big deal) */2106#endif21072108/* Simple PNG writer function by Alex Evans, 2011. Released into the public domain: https://gist.github.com/908299, more context at2109http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/.2110This is actually a modification of Alex's original code so PNG files generated by this function pass pngcheck. */2111void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, size_t *pLen_out, mz_uint level, mz_bool flip)2112{2113/* Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was defined. */2114static const mz_uint s_tdefl_png_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };2115tdefl_compressor *pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));2116tdefl_output_buffer out_buf;2117int i, bpl = w * num_chans, y, z;2118mz_uint32 c;2119*pLen_out = 0;2120if (!pComp)2121return NULL;2122MZ_CLEAR_OBJ(out_buf);2123out_buf.m_expandable = MZ_TRUE;2124out_buf.m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h);2125if (NULL == (out_buf.m_pBuf = (mz_uint8 *)MZ_MALLOC(out_buf.m_capacity)))2126{2127MZ_FREE(pComp);2128return NULL;2129}2130/* write dummy header */2131for (z = 41; z; --z)2132tdefl_output_buffer_putter(&z, 1, &out_buf);2133/* compress image data */2134tdefl_init(pComp, tdefl_output_buffer_putter, &out_buf, s_tdefl_png_num_probes[MZ_MIN(10, level)] | TDEFL_WRITE_ZLIB_HEADER);2135for (y = 0; y < h; ++y)2136{2137tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH);2138tdefl_compress_buffer(pComp, (mz_uint8 *)pImage + (flip ? (h - 1 - y) : y) * bpl, bpl, TDEFL_NO_FLUSH);2139}2140if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != TDEFL_STATUS_DONE)2141{2142MZ_FREE(pComp);2143MZ_FREE(out_buf.m_pBuf);2144return NULL;2145}2146/* write real header */2147*pLen_out = out_buf.m_size - 41;2148{2149static const mz_uint8 chans[] = { 0x00, 0x00, 0x04, 0x02, 0x06 };2150mz_uint8 pnghdr[41] = { 0x89, 0x50, 0x4e, 0x47, 0x0d,21510x0a, 0x1a, 0x0a, 0x00, 0x00,21520x00, 0x0d, 0x49, 0x48, 0x44,21530x52, 0x00, 0x00, 0x00, 0x00,21540x00, 0x00, 0x00, 0x00, 0x08,21550x00, 0x00, 0x00, 0x00, 0x00,21560x00, 0x00, 0x00, 0x00, 0x00,21570x00, 0x00, 0x49, 0x44, 0x41,21580x54 };2159pnghdr[18] = (mz_uint8)(w >> 8);2160pnghdr[19] = (mz_uint8)w;2161pnghdr[22] = (mz_uint8)(h >> 8);2162pnghdr[23] = (mz_uint8)h;2163pnghdr[25] = chans[num_chans];2164pnghdr[33] = (mz_uint8)(*pLen_out >> 24);2165pnghdr[34] = (mz_uint8)(*pLen_out >> 16);2166pnghdr[35] = (mz_uint8)(*pLen_out >> 8);2167pnghdr[36] = (mz_uint8)*pLen_out;2168c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17);2169for (i = 0; i < 4; ++i, c <<= 8)2170((mz_uint8 *)(pnghdr + 29))[i] = (mz_uint8)(c >> 24);2171memcpy(out_buf.m_pBuf, pnghdr, 41);2172}2173/* write footer (IDAT CRC-32, followed by IEND chunk) */2174if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf))2175{2176*pLen_out = 0;2177MZ_FREE(pComp);2178MZ_FREE(out_buf.m_pBuf);2179return NULL;2180}2181c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, out_buf.m_pBuf + 41 - 4, *pLen_out + 4);2182for (i = 0; i < 4; ++i, c <<= 8)2183(out_buf.m_pBuf + out_buf.m_size - 16)[i] = (mz_uint8)(c >> 24);2184/* compute final size of file, grab compressed data buffer and return */2185*pLen_out += 57;2186MZ_FREE(pComp);2187return out_buf.m_pBuf;2188}2189void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out)2190{2191/* Level 6 corresponds to TDEFL_DEFAULT_MAX_PROBES or MZ_DEFAULT_LEVEL (but we can't depend on MZ_DEFAULT_LEVEL being available in case the zlib API's where #defined out) */2192return tdefl_write_image_to_png_file_in_memory_ex(pImage, w, h, num_chans, pLen_out, 6, MZ_FALSE);2193}21942195#ifndef MINIZ_NO_MALLOC2196/* Allocate the tdefl_compressor and tinfl_decompressor structures in C so that */2197/* non-C language bindings to tdefL_ and tinfl_ API don't need to worry about */2198/* structure size and allocation mechanism. */2199tdefl_compressor *tdefl_compressor_alloc()2200{2201return (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));2202}22032204void tdefl_compressor_free(tdefl_compressor *pComp)2205{2206MZ_FREE(pComp);2207}2208#endif22092210#ifdef _MSC_VER2211#pragma warning(pop)2212#endif22132214#ifdef __cplusplus2215}2216#endif2217/**************************************************************************2218*2219* Copyright 2013-2014 RAD Game Tools and Valve Software2220* Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC2221* All Rights Reserved.2222*2223* Permission is hereby granted, free of charge, to any person obtaining a copy2224* of this software and associated documentation files (the "Software"), to deal2225* in the Software without restriction, including without limitation the rights2226* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell2227* copies of the Software, and to permit persons to whom the Software is2228* furnished to do so, subject to the following conditions:2229*2230* The above copyright notice and this permission notice shall be included in2231* all copies or substantial portions of the Software.2232*2233* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR2234* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,2235* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE2236* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER2237* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,2238* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN2239* THE SOFTWARE.2240*2241**************************************************************************/2242224322442245#ifdef __cplusplus2246extern "C" {2247#endif22482249/* ------------------- Low-level Decompression (completely independent from all compression API's) */22502251#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)2252#define TINFL_MEMSET(p, c, l) memset(p, c, l)22532254#define TINFL_CR_BEGIN \2255switch (r->m_state) \2256{ \2257case 0:2258#define TINFL_CR_RETURN(state_index, result) \2259do \2260{ \2261status = result; \2262r->m_state = state_index; \2263goto common_exit; \2264case state_index:; \2265} \2266MZ_MACRO_END2267#define TINFL_CR_RETURN_FOREVER(state_index, result) \2268do \2269{ \2270for (;;) \2271{ \2272TINFL_CR_RETURN(state_index, result); \2273} \2274} \2275MZ_MACRO_END2276#define TINFL_CR_FINISH }22772278#define TINFL_GET_BYTE(state_index, c) \2279do \2280{ \2281while (pIn_buf_cur >= pIn_buf_end) \2282{ \2283TINFL_CR_RETURN(state_index, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \2284} \2285c = *pIn_buf_cur++; \2286} \2287MZ_MACRO_END22882289#define TINFL_NEED_BITS(state_index, n) \2290do \2291{ \2292mz_uint c; \2293TINFL_GET_BYTE(state_index, c); \2294bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \2295num_bits += 8; \2296} while (num_bits < (mz_uint)(n))2297#define TINFL_SKIP_BITS(state_index, n) \2298do \2299{ \2300if (num_bits < (mz_uint)(n)) \2301{ \2302TINFL_NEED_BITS(state_index, n); \2303} \2304bit_buf >>= (n); \2305num_bits -= (n); \2306} \2307MZ_MACRO_END2308#define TINFL_GET_BITS(state_index, b, n) \2309do \2310{ \2311if (num_bits < (mz_uint)(n)) \2312{ \2313TINFL_NEED_BITS(state_index, n); \2314} \2315b = bit_buf & ((1 << (n)) - 1); \2316bit_buf >>= (n); \2317num_bits -= (n); \2318} \2319MZ_MACRO_END23202321/* TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. */2322/* It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no more). It works by trying to fully decode a */2323/* Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, and tries again until it succeeds or until the */2324/* bit buffer contains >=15 bits (deflate's max. Huffman code size). */2325#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \2326do \2327{ \2328temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \2329if (temp >= 0) \2330{ \2331code_len = temp >> 9; \2332if ((code_len) && (num_bits >= code_len)) \2333break; \2334} \2335else if (num_bits > TINFL_FAST_LOOKUP_BITS) \2336{ \2337code_len = TINFL_FAST_LOOKUP_BITS; \2338do \2339{ \2340temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \2341} while ((temp < 0) && (num_bits >= (code_len + 1))); \2342if (temp >= 0) \2343break; \2344} \2345TINFL_GET_BYTE(state_index, c); \2346bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \2347num_bits += 8; \2348} while (num_bits < 15);23492350/* TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because the zlib API expects the decompressor to never read */2351/* beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the input, it REALLY needs another byte in order to fully */2352/* decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, which aren't followed by a byte aligned adler-32. */2353/* The slow path is only executed at the very end of the input buffer. */2354/* v1.16: The original macro handled the case at the very end of the passed-in input buffer, but we also need to handle the case where the user passes in 1+zillion bytes */2355/* following the deflate data and our non-conservative read-ahead path won't kick in here on this code. This is much trickier. */2356#define TINFL_HUFF_DECODE(state_index, sym, pHuff) \2357do \2358{ \2359int temp; \2360mz_uint code_len, c; \2361if (num_bits < 15) \2362{ \2363if ((pIn_buf_end - pIn_buf_cur) < 2) \2364{ \2365TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \2366} \2367else \2368{ \2369bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \2370pIn_buf_cur += 2; \2371num_bits += 16; \2372} \2373} \2374if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \2375code_len = temp >> 9, temp &= 511; \2376else \2377{ \2378code_len = TINFL_FAST_LOOKUP_BITS; \2379do \2380{ \2381temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \2382} while (temp < 0); \2383} \2384sym = temp; \2385bit_buf >>= code_len; \2386num_bits -= code_len; \2387} \2388MZ_MACRO_END23892390tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags)2391{2392static 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 };2393static 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 };2394static 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 };2395static 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 };2396static 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 };2397static const int s_min_table_sizes[3] = { 257, 1, 4 };23982399tinfl_status status = TINFL_STATUS_FAILED;2400mz_uint32 num_bits, dist, counter, num_extra;2401tinfl_bit_buf_t bit_buf;2402const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;2403mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size;2404size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start;24052406/* Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter). */2407if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start))2408{2409*pIn_buf_size = *pOut_buf_size = 0;2410return TINFL_STATUS_BAD_PARAM;2411}24122413num_bits = r->m_num_bits;2414bit_buf = r->m_bit_buf;2415dist = r->m_dist;2416counter = r->m_counter;2417num_extra = r->m_num_extra;2418dist_from_out_buf_start = r->m_dist_from_out_buf_start;2419TINFL_CR_BEGIN24202421bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;2422r->m_z_adler32 = r->m_check_adler32 = 1;2423if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)2424{2425TINFL_GET_BYTE(1, r->m_zhdr0);2426TINFL_GET_BYTE(2, r->m_zhdr1);2427counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));2428if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))2429counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4)))));2430if (counter)2431{2432TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED);2433}2434}24352436do2437{2438TINFL_GET_BITS(3, r->m_final, 3);2439r->m_type = r->m_final >> 1;2440if (r->m_type == 0)2441{2442TINFL_SKIP_BITS(5, num_bits & 7);2443for (counter = 0; counter < 4; ++counter)2444{2445if (num_bits)2446TINFL_GET_BITS(6, r->m_raw_header[counter], 8);2447else2448TINFL_GET_BYTE(7, r->m_raw_header[counter]);2449}2450if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8))))2451{2452TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED);2453}2454while ((counter) && (num_bits))2455{2456TINFL_GET_BITS(51, dist, 8);2457while (pOut_buf_cur >= pOut_buf_end)2458{2459TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);2460}2461*pOut_buf_cur++ = (mz_uint8)dist;2462counter--;2463}2464while (counter)2465{2466size_t n;2467while (pOut_buf_cur >= pOut_buf_end)2468{2469TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);2470}2471while (pIn_buf_cur >= pIn_buf_end)2472{2473TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS);2474}2475n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);2476TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n);2477pIn_buf_cur += n;2478pOut_buf_cur += n;2479counter -= (mz_uint)n;2480}2481}2482else if (r->m_type == 3)2483{2484TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);2485}2486else2487{2488if (r->m_type == 1)2489{2490mz_uint8 *p = r->m_tables[0].m_code_size;2491mz_uint i;2492r->m_table_sizes[0] = 288;2493r->m_table_sizes[1] = 32;2494TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32);2495for (i = 0; i <= 143; ++i)2496*p++ = 8;2497for (; i <= 255; ++i)2498*p++ = 9;2499for (; i <= 279; ++i)2500*p++ = 7;2501for (; i <= 287; ++i)2502*p++ = 8;2503}2504else2505{2506for (counter = 0; counter < 3; counter++)2507{2508TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]);2509r->m_table_sizes[counter] += s_min_table_sizes[counter];2510}2511MZ_CLEAR_OBJ(r->m_tables[2].m_code_size);2512for (counter = 0; counter < r->m_table_sizes[2]; counter++)2513{2514mz_uint s;2515TINFL_GET_BITS(14, s, 3);2516r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s;2517}2518r->m_table_sizes[2] = 19;2519}2520for (; (int)r->m_type >= 0; r->m_type--)2521{2522int tree_next, tree_cur;2523tinfl_huff_table *pTable;2524mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];2525pTable = &r->m_tables[r->m_type];2526MZ_CLEAR_OBJ(total_syms);2527MZ_CLEAR_OBJ(pTable->m_look_up);2528MZ_CLEAR_OBJ(pTable->m_tree);2529for (i = 0; i < r->m_table_sizes[r->m_type]; ++i)2530total_syms[pTable->m_code_size[i]]++;2531used_syms = 0, total = 0;2532next_code[0] = next_code[1] = 0;2533for (i = 1; i <= 15; ++i)2534{2535used_syms += total_syms[i];2536next_code[i + 1] = (total = ((total + total_syms[i]) << 1));2537}2538if ((65536 != total) && (used_syms > 1))2539{2540TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);2541}2542for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)2543{2544mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index];2545if (!code_size)2546continue;2547cur_code = next_code[code_size]++;2548for (l = code_size; l > 0; l--, cur_code >>= 1)2549rev_code = (rev_code << 1) | (cur_code & 1);2550if (code_size <= TINFL_FAST_LOOKUP_BITS)2551{2552mz_int16 k = (mz_int16)((code_size << 9) | sym_index);2553while (rev_code < TINFL_FAST_LOOKUP_SIZE)2554{2555pTable->m_look_up[rev_code] = k;2556rev_code += (1 << code_size);2557}2558continue;2559}2560if (0 == (tree_cur = pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)]))2561{2562pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next;2563tree_cur = tree_next;2564tree_next -= 2;2565}2566rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);2567for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)2568{2569tree_cur -= ((rev_code >>= 1) & 1);2570if (!pTable->m_tree[-tree_cur - 1])2571{2572pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next;2573tree_cur = tree_next;2574tree_next -= 2;2575}2576else2577tree_cur = pTable->m_tree[-tree_cur - 1];2578}2579tree_cur -= ((rev_code >>= 1) & 1);2580pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index;2581}2582if (r->m_type == 2)2583{2584for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);)2585{2586mz_uint s;2587TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]);2588if (dist < 16)2589{2590r->m_len_codes[counter++] = (mz_uint8)dist;2591continue;2592}2593if ((dist == 16) && (!counter))2594{2595TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);2596}2597num_extra = "\02\03\07"[dist - 16];2598TINFL_GET_BITS(18, s, num_extra);2599s += "\03\03\013"[dist - 16];2600TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s);2601counter += s;2602}2603if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)2604{2605TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);2606}2607TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes, r->m_table_sizes[0]);2608TINFL_MEMCPY(r->m_tables[1].m_code_size, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);2609}2610}2611for (;;)2612{2613mz_uint8 *pSrc;2614for (;;)2615{2616if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))2617{2618TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]);2619if (counter >= 256)2620break;2621while (pOut_buf_cur >= pOut_buf_end)2622{2623TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT);2624}2625*pOut_buf_cur++ = (mz_uint8)counter;2626}2627else2628{2629int sym2;2630mz_uint code_len;2631#if TINFL_USE_64BIT_BITBUF2632if (num_bits < 30)2633{2634bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits);2635pIn_buf_cur += 4;2636num_bits += 32;2637}2638#else2639if (num_bits < 15)2640{2641bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);2642pIn_buf_cur += 2;2643num_bits += 16;2644}2645#endif2646if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)2647code_len = sym2 >> 9;2648else2649{2650code_len = TINFL_FAST_LOOKUP_BITS;2651do2652{2653sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)];2654} while (sym2 < 0);2655}2656counter = sym2;2657bit_buf >>= code_len;2658num_bits -= code_len;2659if (counter & 256)2660break;26612662#if !TINFL_USE_64BIT_BITBUF2663if (num_bits < 15)2664{2665bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);2666pIn_buf_cur += 2;2667num_bits += 16;2668}2669#endif2670if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)2671code_len = sym2 >> 9;2672else2673{2674code_len = TINFL_FAST_LOOKUP_BITS;2675do2676{2677sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)];2678} while (sym2 < 0);2679}2680bit_buf >>= code_len;2681num_bits -= code_len;26822683pOut_buf_cur[0] = (mz_uint8)counter;2684if (sym2 & 256)2685{2686pOut_buf_cur++;2687counter = sym2;2688break;2689}2690pOut_buf_cur[1] = (mz_uint8)sym2;2691pOut_buf_cur += 2;2692}2693}2694if ((counter &= 511) == 256)2695break;26962697num_extra = s_length_extra[counter - 257];2698counter = s_length_base[counter - 257];2699if (num_extra)2700{2701mz_uint extra_bits;2702TINFL_GET_BITS(25, extra_bits, num_extra);2703counter += extra_bits;2704}27052706TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]);2707num_extra = s_dist_extra[dist];2708dist = s_dist_base[dist];2709if (num_extra)2710{2711mz_uint extra_bits;2712TINFL_GET_BITS(27, extra_bits, num_extra);2713dist += extra_bits;2714}27152716dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;2717if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))2718{2719TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);2720}27212722pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);27232724if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)2725{2726while (counter--)2727{2728while (pOut_buf_cur >= pOut_buf_end)2729{2730TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT);2731}2732*pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];2733}2734continue;2735}2736#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES2737else if ((counter >= 9) && (counter <= dist))2738{2739const mz_uint8 *pSrc_end = pSrc + (counter & ~7);2740do2741{2742#ifdef MINIZ_UNALIGNED_USE_MEMCPY2743memcpy(pOut_buf_cur, pSrc, sizeof(mz_uint32)*2);2744#else2745((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0];2746((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1];2747#endif2748pOut_buf_cur += 8;2749} while ((pSrc += 8) < pSrc_end);2750if ((counter &= 7) < 3)2751{2752if (counter)2753{2754pOut_buf_cur[0] = pSrc[0];2755if (counter > 1)2756pOut_buf_cur[1] = pSrc[1];2757pOut_buf_cur += counter;2758}2759continue;2760}2761}2762#endif2763while(counter>2)2764{2765pOut_buf_cur[0] = pSrc[0];2766pOut_buf_cur[1] = pSrc[1];2767pOut_buf_cur[2] = pSrc[2];2768pOut_buf_cur += 3;2769pSrc += 3;2770counter -= 3;2771}2772if (counter > 0)2773{2774pOut_buf_cur[0] = pSrc[0];2775if (counter > 1)2776pOut_buf_cur[1] = pSrc[1];2777pOut_buf_cur += counter;2778}2779}2780}2781} while (!(r->m_final & 1));27822783/* Ensure byte alignment and put back any bytes from the bitbuf if we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */2784/* I'm being super conservative here. A number of simplifications can be made to the byte alignment part, and the Adler32 check shouldn't ever need to worry about reading from the bitbuf now. */2785TINFL_SKIP_BITS(32, num_bits & 7);2786while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))2787{2788--pIn_buf_cur;2789num_bits -= 8;2790}2791bit_buf &= (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1);2792MZ_ASSERT(!num_bits); /* if this assert fires then we've read beyond the end of non-deflate/zlib streams with following data (such as gzip streams). */27932794if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)2795{2796for (counter = 0; counter < 4; ++counter)2797{2798mz_uint s;2799if (num_bits)2800TINFL_GET_BITS(41, s, 8);2801else2802TINFL_GET_BYTE(42, s);2803r->m_z_adler32 = (r->m_z_adler32 << 8) | s;2804}2805}2806TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);28072808TINFL_CR_FINISH28092810common_exit:2811/* As long as we aren't telling the caller that we NEED more input to make forward progress: */2812/* Put back any bytes from the bitbuf in case we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */2813/* We need to be very careful here to NOT push back any bytes we definitely know we need to make forward progress, though, or we'll lock the caller up into an inf loop. */2814if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS))2815{2816while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))2817{2818--pIn_buf_cur;2819num_bits -= 8;2820}2821}2822r->m_num_bits = num_bits;2823r->m_bit_buf = bit_buf & (tinfl_bit_buf_t)((((mz_uint64)1) << num_bits) - (mz_uint64)1);2824r->m_dist = dist;2825r->m_counter = counter;2826r->m_num_extra = num_extra;2827r->m_dist_from_out_buf_start = dist_from_out_buf_start;2828*pIn_buf_size = pIn_buf_cur - pIn_buf_next;2829*pOut_buf_size = pOut_buf_cur - pOut_buf_next;2830if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))2831{2832const mz_uint8 *ptr = pOut_buf_next;2833size_t buf_len = *pOut_buf_size;2834mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16;2835size_t block_len = buf_len % 5552;2836while (buf_len)2837{2838for (i = 0; i + 7 < block_len; i += 8, ptr += 8)2839{2840s1 += ptr[0], s2 += s1;2841s1 += ptr[1], s2 += s1;2842s1 += ptr[2], s2 += s1;2843s1 += ptr[3], s2 += s1;2844s1 += ptr[4], s2 += s1;2845s1 += ptr[5], s2 += s1;2846s1 += ptr[6], s2 += s1;2847s1 += ptr[7], s2 += s1;2848}2849for (; i < block_len; ++i)2850s1 += *ptr++, s2 += s1;2851s1 %= 65521U, s2 %= 65521U;2852buf_len -= block_len;2853block_len = 5552;2854}2855r->m_check_adler32 = (s2 << 16) + s1;2856if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32))2857status = TINFL_STATUS_ADLER32_MISMATCH;2858}2859return status;2860}28612862/* Higher level helper functions. */2863void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)2864{2865tinfl_decompressor decomp;2866void *pBuf = NULL, *pNew_buf;2867size_t src_buf_ofs = 0, out_buf_capacity = 0;2868*pOut_len = 0;2869tinfl_init(&decomp);2870for (;;)2871{2872size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity;2873tinfl_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,2874(flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);2875if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT))2876{2877MZ_FREE(pBuf);2878*pOut_len = 0;2879return NULL;2880}2881src_buf_ofs += src_buf_size;2882*pOut_len += dst_buf_size;2883if (status == TINFL_STATUS_DONE)2884break;2885new_out_buf_capacity = out_buf_capacity * 2;2886if (new_out_buf_capacity < 128)2887new_out_buf_capacity = 128;2888pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity);2889if (!pNew_buf)2890{2891MZ_FREE(pBuf);2892*pOut_len = 0;2893return NULL;2894}2895pBuf = pNew_buf;2896out_buf_capacity = new_out_buf_capacity;2897}2898return pBuf;2899}29002901size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags)2902{2903tinfl_decompressor decomp;2904tinfl_status status;2905tinfl_init(&decomp);2906status = tinfl_decompress(&decomp, (const mz_uint8 *)pSrc_buf, &src_buf_len, (mz_uint8 *)pOut_buf, (mz_uint8 *)pOut_buf, &out_buf_len, (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);2907return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len;2908}29092910int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)2911{2912int result = 0;2913tinfl_decompressor decomp;2914mz_uint8 *pDict = (mz_uint8 *)MZ_MALLOC(TINFL_LZ_DICT_SIZE);2915size_t in_buf_ofs = 0, dict_ofs = 0;2916if (!pDict)2917return TINFL_STATUS_FAILED;2918tinfl_init(&decomp);2919for (;;)2920{2921size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs;2922tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8 *)pIn_buf + in_buf_ofs, &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size,2923(flags & ~(TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)));2924in_buf_ofs += in_buf_size;2925if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user)))2926break;2927if (status != TINFL_STATUS_HAS_MORE_OUTPUT)2928{2929result = (status == TINFL_STATUS_DONE);2930break;2931}2932dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1);2933}2934MZ_FREE(pDict);2935*pIn_buf_size = in_buf_ofs;2936return result;2937}29382939#ifndef MINIZ_NO_MALLOC2940tinfl_decompressor *tinfl_decompressor_alloc()2941{2942tinfl_decompressor *pDecomp = (tinfl_decompressor *)MZ_MALLOC(sizeof(tinfl_decompressor));2943if (pDecomp)2944tinfl_init(pDecomp);2945return pDecomp;2946}29472948void tinfl_decompressor_free(tinfl_decompressor *pDecomp)2949{2950MZ_FREE(pDecomp);2951}2952#endif29532954#ifdef __cplusplus2955}2956#endif2957/**************************************************************************2958*2959* Copyright 2013-2014 RAD Game Tools and Valve Software2960* Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC2961* Copyright 2016 Martin Raiber2962* All Rights Reserved.2963*2964* Permission is hereby granted, free of charge, to any person obtaining a copy2965* of this software and associated documentation files (the "Software"), to deal2966* in the Software without restriction, including without limitation the rights2967* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell2968* copies of the Software, and to permit persons to whom the Software is2969* furnished to do so, subject to the following conditions:2970*2971* The above copyright notice and this permission notice shall be included in2972* all copies or substantial portions of the Software.2973*2974* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR2975* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,2976* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE2977* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER2978* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,2979* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN2980* THE SOFTWARE.2981*2982**************************************************************************/298329842985#ifndef MINIZ_NO_ARCHIVE_APIS29862987#ifdef __cplusplus2988extern "C" {2989#endif29902991/* ------------------- .ZIP archive reading */29922993#ifdef MINIZ_NO_STDIO2994#define MZ_FILE void *2995#else2996#include <sys/stat.h>29972998#if defined(_MSC_VER) || defined(__MINGW64__)2999static FILE *mz_fopen(const char *pFilename, const char *pMode)3000{3001FILE *pFile = NULL;3002fopen_s(&pFile, pFilename, pMode);3003return pFile;3004}3005static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream)3006{3007FILE *pFile = NULL;3008if (freopen_s(&pFile, pPath, pMode, pStream))3009return NULL;3010return pFile;3011}3012#ifndef MINIZ_NO_TIME3013#include <sys/utime.h>3014#endif3015#define MZ_FOPEN mz_fopen3016#define MZ_FCLOSE fclose3017#define MZ_FREAD fread3018#define MZ_FWRITE fwrite3019#define MZ_FTELL64 _ftelli643020#define MZ_FSEEK64 _fseeki643021#define MZ_FILE_STAT_STRUCT _stat643022#define MZ_FILE_STAT _stat643023#define MZ_FFLUSH fflush3024#define MZ_FREOPEN mz_freopen3025#define MZ_DELETE_FILE remove3026#elif defined(__MINGW32__)3027#ifndef MINIZ_NO_TIME3028#include <sys/utime.h>3029#endif3030#define MZ_FOPEN(f, m) fopen(f, m)3031#define MZ_FCLOSE fclose3032#define MZ_FREAD fread3033#define MZ_FWRITE fwrite3034#define MZ_FTELL64 ftello643035#define MZ_FSEEK64 fseeko643036#define MZ_FILE_STAT_STRUCT _stat3037#define MZ_FILE_STAT _stat3038#define MZ_FFLUSH fflush3039#define MZ_FREOPEN(f, m, s) freopen(f, m, s)3040#define MZ_DELETE_FILE remove3041#elif defined(__TINYC__)3042#ifndef MINIZ_NO_TIME3043#include <sys/utime.h>3044#endif3045#define MZ_FOPEN(f, m) fopen(f, m)3046#define MZ_FCLOSE fclose3047#define MZ_FREAD fread3048#define MZ_FWRITE fwrite3049#define MZ_FTELL64 ftell3050#define MZ_FSEEK64 fseek3051#define MZ_FILE_STAT_STRUCT stat3052#define MZ_FILE_STAT stat3053#define MZ_FFLUSH fflush3054#define MZ_FREOPEN(f, m, s) freopen(f, m, s)3055#define MZ_DELETE_FILE remove3056#elif defined(__USE_LARGEFILE64) /* gcc, clang */3057#ifndef MINIZ_NO_TIME3058#include <utime.h>3059#endif3060#define MZ_FOPEN(f, m) fopen64(f, m)3061#define MZ_FCLOSE fclose3062#define MZ_FREAD fread3063#define MZ_FWRITE fwrite3064#define MZ_FTELL64 ftello643065#define MZ_FSEEK64 fseeko643066#define MZ_FILE_STAT_STRUCT stat643067#define MZ_FILE_STAT stat643068#define MZ_FFLUSH fflush3069#define MZ_FREOPEN(p, m, s) freopen64(p, m, s)3070#define MZ_DELETE_FILE remove3071#elif defined(__APPLE__)3072#ifndef MINIZ_NO_TIME3073#include <utime.h>3074#endif3075#define MZ_FOPEN(f, m) fopen(f, m)3076#define MZ_FCLOSE fclose3077#define MZ_FREAD fread3078#define MZ_FWRITE fwrite3079#define MZ_FTELL64 ftello3080#define MZ_FSEEK64 fseeko3081#define MZ_FILE_STAT_STRUCT stat3082#define MZ_FILE_STAT stat3083#define MZ_FFLUSH fflush3084#define MZ_FREOPEN(p, m, s) freopen(p, m, s)3085#define MZ_DELETE_FILE remove30863087#else3088#pragma message("Using fopen, ftello, fseeko, stat() etc. path for file I/O - this path may not support large files.")3089#ifndef MINIZ_NO_TIME3090#include <utime.h>3091#endif3092#define MZ_FOPEN(f, m) fopen(f, m)3093#define MZ_FCLOSE fclose3094#define MZ_FREAD fread3095#define MZ_FWRITE fwrite3096#ifdef __STRICT_ANSI__3097#define MZ_FTELL64 ftell3098#define MZ_FSEEK64 fseek3099#else3100#define MZ_FTELL64 ftello3101#define MZ_FSEEK64 fseeko3102#endif3103#define MZ_FILE_STAT_STRUCT stat3104#define MZ_FILE_STAT stat3105#define MZ_FFLUSH fflush3106#define MZ_FREOPEN(f, m, s) freopen(f, m, s)3107#define MZ_DELETE_FILE remove3108#endif /* #ifdef _MSC_VER */3109#endif /* #ifdef MINIZ_NO_STDIO */31103111#define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))31123113/* Various ZIP archive enums. To completely avoid cross platform compiler alignment and platform endian issues, miniz.c doesn't use structs for any of this stuff. */3114enum3115{3116/* ZIP archive identifiers and record sizes */3117MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50,3118MZ_ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50,3119MZ_ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50,3120MZ_ZIP_LOCAL_DIR_HEADER_SIZE = 30,3121MZ_ZIP_CENTRAL_DIR_HEADER_SIZE = 46,3122MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22,31233124/* ZIP64 archive identifier and record sizes */3125MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06064b50,3126MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG = 0x07064b50,3127MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE = 56,3128MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE = 20,3129MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID = 0x0001,3130MZ_ZIP_DATA_DESCRIPTOR_ID = 0x08074b50,3131MZ_ZIP_DATA_DESCRIPTER_SIZE64 = 24,3132MZ_ZIP_DATA_DESCRIPTER_SIZE32 = 16,31333134/* Central directory header record offsets */3135MZ_ZIP_CDH_SIG_OFS = 0,3136MZ_ZIP_CDH_VERSION_MADE_BY_OFS = 4,3137MZ_ZIP_CDH_VERSION_NEEDED_OFS = 6,3138MZ_ZIP_CDH_BIT_FLAG_OFS = 8,3139MZ_ZIP_CDH_METHOD_OFS = 10,3140MZ_ZIP_CDH_FILE_TIME_OFS = 12,3141MZ_ZIP_CDH_FILE_DATE_OFS = 14,3142MZ_ZIP_CDH_CRC32_OFS = 16,3143MZ_ZIP_CDH_COMPRESSED_SIZE_OFS = 20,3144MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24,3145MZ_ZIP_CDH_FILENAME_LEN_OFS = 28,3146MZ_ZIP_CDH_EXTRA_LEN_OFS = 30,3147MZ_ZIP_CDH_COMMENT_LEN_OFS = 32,3148MZ_ZIP_CDH_DISK_START_OFS = 34,3149MZ_ZIP_CDH_INTERNAL_ATTR_OFS = 36,3150MZ_ZIP_CDH_EXTERNAL_ATTR_OFS = 38,3151MZ_ZIP_CDH_LOCAL_HEADER_OFS = 42,31523153/* Local directory header offsets */3154MZ_ZIP_LDH_SIG_OFS = 0,3155MZ_ZIP_LDH_VERSION_NEEDED_OFS = 4,3156MZ_ZIP_LDH_BIT_FLAG_OFS = 6,3157MZ_ZIP_LDH_METHOD_OFS = 8,3158MZ_ZIP_LDH_FILE_TIME_OFS = 10,3159MZ_ZIP_LDH_FILE_DATE_OFS = 12,3160MZ_ZIP_LDH_CRC32_OFS = 14,3161MZ_ZIP_LDH_COMPRESSED_SIZE_OFS = 18,3162MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS = 22,3163MZ_ZIP_LDH_FILENAME_LEN_OFS = 26,3164MZ_ZIP_LDH_EXTRA_LEN_OFS = 28,3165MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR = 1 << 3,31663167/* End of central directory offsets */3168MZ_ZIP_ECDH_SIG_OFS = 0,3169MZ_ZIP_ECDH_NUM_THIS_DISK_OFS = 4,3170MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS = 6,3171MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8,3172MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10,3173MZ_ZIP_ECDH_CDIR_SIZE_OFS = 12,3174MZ_ZIP_ECDH_CDIR_OFS_OFS = 16,3175MZ_ZIP_ECDH_COMMENT_SIZE_OFS = 20,31763177/* ZIP64 End of central directory locator offsets */3178MZ_ZIP64_ECDL_SIG_OFS = 0, /* 4 bytes */3179MZ_ZIP64_ECDL_NUM_DISK_CDIR_OFS = 4, /* 4 bytes */3180MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS = 8, /* 8 bytes */3181MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS = 16, /* 4 bytes */31823183/* ZIP64 End of central directory header offsets */3184MZ_ZIP64_ECDH_SIG_OFS = 0, /* 4 bytes */3185MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS = 4, /* 8 bytes */3186MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS = 12, /* 2 bytes */3187MZ_ZIP64_ECDH_VERSION_NEEDED_OFS = 14, /* 2 bytes */3188MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS = 16, /* 4 bytes */3189MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS = 20, /* 4 bytes */3190MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 24, /* 8 bytes */3191MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS = 32, /* 8 bytes */3192MZ_ZIP64_ECDH_CDIR_SIZE_OFS = 40, /* 8 bytes */3193MZ_ZIP64_ECDH_CDIR_OFS_OFS = 48, /* 8 bytes */3194MZ_ZIP_VERSION_MADE_BY_DOS_FILESYSTEM_ID = 0,3195MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG = 0x10,3196MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED = 1,3197MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG = 32,3198MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION = 64,3199MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED = 8192,3200MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8 = 1 << 113201};32023203typedef struct3204{3205void *m_p;3206size_t m_size, m_capacity;3207mz_uint m_element_size;3208} mz_zip_array;32093210struct mz_zip_internal_state_tag3211{3212mz_zip_array m_central_dir;3213mz_zip_array m_central_dir_offsets;3214mz_zip_array m_sorted_central_dir_offsets;32153216/* The flags passed in when the archive is initially opened. */3217uint32_t m_init_flags;32183219/* MZ_TRUE if the archive has a zip64 end of central directory headers, etc. */3220mz_bool m_zip64;32213222/* MZ_TRUE if we found zip64 extended info in the central directory (m_zip64 will also be slammed to true too, even if we didn't find a zip64 end of central dir header, etc.) */3223mz_bool m_zip64_has_extended_info_fields;32243225/* These fields are used by the file, FILE, memory, and memory/heap read/write helpers. */3226MZ_FILE *m_pFile;3227mz_uint64 m_file_archive_start_ofs;32283229void *m_pMem;3230size_t m_mem_size;3231size_t m_mem_capacity;3232};32333234#define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size32353236#if defined(DEBUG) || defined(_DEBUG)3237static MZ_FORCEINLINE mz_uint mz_zip_array_range_check(const mz_zip_array *pArray, mz_uint index)3238{3239MZ_ASSERT(index < pArray->m_size);3240return index;3241}3242#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[mz_zip_array_range_check(array_ptr, index)]3243#else3244#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[index]3245#endif32463247static MZ_FORCEINLINE void mz_zip_array_init(mz_zip_array *pArray, mz_uint32 element_size)3248{3249memset(pArray, 0, sizeof(mz_zip_array));3250pArray->m_element_size = element_size;3251}32523253static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive *pZip, mz_zip_array *pArray)3254{3255pZip->m_pFree(pZip->m_pAlloc_opaque, pArray->m_p);3256memset(pArray, 0, sizeof(mz_zip_array));3257}32583259static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive *pZip, mz_zip_array *pArray, size_t min_new_capacity, mz_uint growing)3260{3261void *pNew_p;3262size_t new_capacity = min_new_capacity;3263MZ_ASSERT(pArray->m_element_size);3264if (pArray->m_capacity >= min_new_capacity)3265return MZ_TRUE;3266if (growing)3267{3268new_capacity = MZ_MAX(1, pArray->m_capacity);3269while (new_capacity < min_new_capacity)3270new_capacity *= 2;3271}3272if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->m_p, pArray->m_element_size, new_capacity)))3273return MZ_FALSE;3274pArray->m_p = pNew_p;3275pArray->m_capacity = new_capacity;3276return MZ_TRUE;3277}32783279static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_capacity, mz_uint growing)3280{3281if (new_capacity > pArray->m_capacity)3282{3283if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing))3284return MZ_FALSE;3285}3286return MZ_TRUE;3287}32883289static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_size, mz_uint growing)3290{3291if (new_size > pArray->m_capacity)3292{3293if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing))3294return MZ_FALSE;3295}3296pArray->m_size = new_size;3297return MZ_TRUE;3298}32993300static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive *pZip, mz_zip_array *pArray, size_t n)3301{3302return mz_zip_array_reserve(pZip, pArray, pArray->m_size + n, MZ_TRUE);3303}33043305static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive *pZip, mz_zip_array *pArray, const void *pElements, size_t n)3306{3307size_t orig_size = pArray->m_size;3308if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE))3309return MZ_FALSE;3310if (n > 0)3311memcpy((mz_uint8 *)pArray->m_p + orig_size * pArray->m_element_size, pElements, n * pArray->m_element_size);3312return MZ_TRUE;3313}33143315#ifndef MINIZ_NO_TIME3316static MZ_TIME_T mz_zip_dos_to_time_t(int dos_time, int dos_date)3317{3318struct tm tm;3319memset(&tm, 0, sizeof(tm));3320tm.tm_isdst = -1;3321tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900;3322tm.tm_mon = ((dos_date >> 5) & 15) - 1;3323tm.tm_mday = dos_date & 31;3324tm.tm_hour = (dos_time >> 11) & 31;3325tm.tm_min = (dos_time >> 5) & 63;3326tm.tm_sec = (dos_time << 1) & 62;3327return mktime(&tm);3328}33293330#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS3331static void mz_zip_time_t_to_dos_time(MZ_TIME_T time, mz_uint16 *pDOS_time, mz_uint16 *pDOS_date)3332{3333#ifdef _MSC_VER3334struct tm tm_struct;3335struct tm *tm = &tm_struct;3336errno_t err = localtime_s(tm, &time);3337if (err)3338{3339*pDOS_date = 0;3340*pDOS_time = 0;3341return;3342}3343#else3344struct tm *tm = localtime(&time);3345#endif /* #ifdef _MSC_VER */33463347*pDOS_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1));3348*pDOS_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday);3349}3350#endif /* MINIZ_NO_ARCHIVE_WRITING_APIS */33513352#ifndef MINIZ_NO_STDIO3353#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS3354static mz_bool mz_zip_get_file_modified_time(const char *pFilename, MZ_TIME_T *pTime)3355{3356struct MZ_FILE_STAT_STRUCT file_stat;33573358/* On Linux with x86 glibc, this call will fail on large files (I think >= 0x80000000 bytes) unless you compiled with _LARGEFILE64_SOURCE. Argh. */3359if (MZ_FILE_STAT(pFilename, &file_stat) != 0)3360return MZ_FALSE;33613362*pTime = file_stat.st_mtime;33633364return MZ_TRUE;3365}3366#endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS*/33673368static mz_bool mz_zip_set_file_times(const char *pFilename, MZ_TIME_T access_time, MZ_TIME_T modified_time)3369{3370struct utimbuf t;33713372memset(&t, 0, sizeof(t));3373t.actime = access_time;3374t.modtime = modified_time;33753376return !utime(pFilename, &t);3377}3378#endif /* #ifndef MINIZ_NO_STDIO */3379#endif /* #ifndef MINIZ_NO_TIME */33803381static MZ_FORCEINLINE mz_bool mz_zip_set_error(mz_zip_archive *pZip, mz_zip_error err_num)3382{3383if (pZip)3384pZip->m_last_error = err_num;3385return MZ_FALSE;3386}33873388static mz_bool mz_zip_reader_init_internal(mz_zip_archive *pZip, mz_uint flags)3389{3390(void)flags;3391if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))3392return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);33933394if (!pZip->m_pAlloc)3395pZip->m_pAlloc = miniz_def_alloc_func;3396if (!pZip->m_pFree)3397pZip->m_pFree = miniz_def_free_func;3398if (!pZip->m_pRealloc)3399pZip->m_pRealloc = miniz_def_realloc_func;34003401pZip->m_archive_size = 0;3402pZip->m_central_directory_file_ofs = 0;3403pZip->m_total_files = 0;3404pZip->m_last_error = MZ_ZIP_NO_ERROR;34053406if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))3407return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);34083409memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));3410MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8));3411MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32));3412MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32));3413pZip->m_pState->m_init_flags = flags;3414pZip->m_pState->m_zip64 = MZ_FALSE;3415pZip->m_pState->m_zip64_has_extended_info_fields = MZ_FALSE;34163417pZip->m_zip_mode = MZ_ZIP_MODE_READING;34183419return MZ_TRUE;3420}34213422static MZ_FORCEINLINE mz_bool mz_zip_reader_filename_less(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, mz_uint r_index)3423{3424const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE;3425const mz_uint8 *pR = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, r_index));3426mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS), r_len = MZ_READ_LE16(pR + MZ_ZIP_CDH_FILENAME_LEN_OFS);3427mz_uint8 l = 0, r = 0;3428pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;3429pR += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;3430pE = pL + MZ_MIN(l_len, r_len);3431while (pL < pE)3432{3433if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))3434break;3435pL++;3436pR++;3437}3438return (pL == pE) ? (l_len < r_len) : (l < r);3439}34403441#define MZ_SWAP_UINT32(a, b) \3442do \3443{ \3444mz_uint32 t = a; \3445a = b; \3446b = t; \3447} \3448MZ_MACRO_END34493450/* Heap sort of lowercased filenames, used to help accelerate plain central directory searches by mz_zip_reader_locate_file(). (Could also use qsort(), but it could allocate memory.) */3451static void mz_zip_reader_sort_central_dir_offsets_by_filename(mz_zip_archive *pZip)3452{3453mz_zip_internal_state *pState = pZip->m_pState;3454const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets;3455const mz_zip_array *pCentral_dir = &pState->m_central_dir;3456mz_uint32 *pIndices;3457mz_uint32 start, end;3458const mz_uint32 size = pZip->m_total_files;34593460if (size <= 1U)3461return;34623463pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0);34643465start = (size - 2U) >> 1U;3466for (;;)3467{3468mz_uint64 child, root = start;3469for (;;)3470{3471if ((child = (root << 1U) + 1U) >= size)3472break;3473child += (((child + 1U) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U])));3474if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))3475break;3476MZ_SWAP_UINT32(pIndices[root], pIndices[child]);3477root = child;3478}3479if (!start)3480break;3481start--;3482}34833484end = size - 1;3485while (end > 0)3486{3487mz_uint64 child, root = 0;3488MZ_SWAP_UINT32(pIndices[end], pIndices[0]);3489for (;;)3490{3491if ((child = (root << 1U) + 1U) >= end)3492break;3493child += (((child + 1U) < end) && mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U]));3494if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))3495break;3496MZ_SWAP_UINT32(pIndices[root], pIndices[child]);3497root = child;3498}3499end--;3500}3501}35023503static mz_bool mz_zip_reader_locate_header_sig(mz_zip_archive *pZip, mz_uint32 record_sig, mz_uint32 record_size, mz_int64 *pOfs)3504{3505mz_int64 cur_file_ofs;3506mz_uint32 buf_u32[4096 / sizeof(mz_uint32)];3507mz_uint8 *pBuf = (mz_uint8 *)buf_u32;35083509/* Basic sanity checks - reject files which are too small */3510if (pZip->m_archive_size < record_size)3511return MZ_FALSE;35123513/* Find the record by scanning the file from the end towards the beginning. */3514cur_file_ofs = MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)sizeof(buf_u32), 0);3515for (;;)3516{3517int i, n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs);35183519if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n)3520return MZ_FALSE;35213522for (i = n - 4; i >= 0; --i)3523{3524mz_uint s = MZ_READ_LE32(pBuf + i);3525if (s == record_sig)3526{3527if ((pZip->m_archive_size - (cur_file_ofs + i)) >= record_size)3528break;3529}3530}35313532if (i >= 0)3533{3534cur_file_ofs += i;3535break;3536}35373538/* Give up if we've searched the entire file, or we've gone back "too far" (~64kb) */3539if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= (MZ_UINT16_MAX + record_size)))3540return MZ_FALSE;35413542cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0);3543}35443545*pOfs = cur_file_ofs;3546return MZ_TRUE;3547}35483549static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, mz_uint flags)3550{3551mz_uint cdir_size = 0, cdir_entries_on_this_disk = 0, num_this_disk = 0, cdir_disk_index = 0;3552mz_uint64 cdir_ofs = 0;3553mz_int64 cur_file_ofs = 0;3554const mz_uint8 *p;35553556mz_uint32 buf_u32[4096 / sizeof(mz_uint32)];3557mz_uint8 *pBuf = (mz_uint8 *)buf_u32;3558mz_bool sort_central_dir = ((flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0);3559mz_uint32 zip64_end_of_central_dir_locator_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];3560mz_uint8 *pZip64_locator = (mz_uint8 *)zip64_end_of_central_dir_locator_u32;35613562mz_uint32 zip64_end_of_central_dir_header_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];3563mz_uint8 *pZip64_end_of_central_dir = (mz_uint8 *)zip64_end_of_central_dir_header_u32;35643565mz_uint64 zip64_end_of_central_dir_ofs = 0;35663567/* Basic sanity checks - reject files which are too small, and check the first 4 bytes of the file to make sure a local header is there. */3568if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)3569return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);35703571if (!mz_zip_reader_locate_header_sig(pZip, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE, &cur_file_ofs))3572return mz_zip_set_error(pZip, MZ_ZIP_FAILED_FINDING_CENTRAL_DIR);35733574/* Read and verify the end of central directory record. */3575if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)3576return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);35773578if (MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_SIG_OFS) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG)3579return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);35803581if (cur_file_ofs >= (MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE))3582{3583if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE, pZip64_locator, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE)3584{3585if (MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG)3586{3587zip64_end_of_central_dir_ofs = MZ_READ_LE64(pZip64_locator + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS);3588if (zip64_end_of_central_dir_ofs > (pZip->m_archive_size - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE))3589return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);35903591if (pZip->m_pRead(pZip->m_pIO_opaque, zip64_end_of_central_dir_ofs, pZip64_end_of_central_dir, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)3592{3593if (MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG)3594{3595pZip->m_pState->m_zip64 = MZ_TRUE;3596}3597}3598}3599}3600}36013602pZip->m_total_files = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS);3603cdir_entries_on_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS);3604num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS);3605cdir_disk_index = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS);3606cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS);3607cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS);36083609if (pZip->m_pState->m_zip64)3610{3611mz_uint32 zip64_total_num_of_disks = MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS);3612mz_uint64 zip64_cdir_total_entries = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS);3613mz_uint64 zip64_cdir_total_entries_on_this_disk = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS);3614mz_uint64 zip64_size_of_end_of_central_dir_record = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS);3615mz_uint64 zip64_size_of_central_directory = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_SIZE_OFS);36163617if (zip64_size_of_end_of_central_dir_record < (MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - 12))3618return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);36193620if (zip64_total_num_of_disks != 1U)3621return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);36223623/* Check for miniz's practical limits */3624if (zip64_cdir_total_entries > MZ_UINT32_MAX)3625return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);36263627pZip->m_total_files = (mz_uint32)zip64_cdir_total_entries;36283629if (zip64_cdir_total_entries_on_this_disk > MZ_UINT32_MAX)3630return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);36313632cdir_entries_on_this_disk = (mz_uint32)zip64_cdir_total_entries_on_this_disk;36333634/* Check for miniz's current practical limits (sorry, this should be enough for millions of files) */3635if (zip64_size_of_central_directory > MZ_UINT32_MAX)3636return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);36373638cdir_size = (mz_uint32)zip64_size_of_central_directory;36393640num_this_disk = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS);36413642cdir_disk_index = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS);36433644cdir_ofs = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_OFS_OFS);3645}36463647if (pZip->m_total_files != cdir_entries_on_this_disk)3648return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);36493650if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1)))3651return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);36523653if (cdir_size < pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)3654return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);36553656if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size)3657return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);36583659pZip->m_central_directory_file_ofs = cdir_ofs;36603661if (pZip->m_total_files)3662{3663mz_uint i, n;3664/* Read the entire central directory into a heap block, and allocate another heap block to hold the unsorted central dir file record offsets, and possibly another to hold the sorted indices. */3665if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, MZ_FALSE)) ||3666(!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, pZip->m_total_files, MZ_FALSE)))3667return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);36683669if (sort_central_dir)3670{3671if (!mz_zip_array_resize(pZip, &pZip->m_pState->m_sorted_central_dir_offsets, pZip->m_total_files, MZ_FALSE))3672return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);3673}36743675if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, pZip->m_pState->m_central_dir.m_p, cdir_size) != cdir_size)3676return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);36773678/* Now create an index into the central directory file records, do some basic sanity checking on each record */3679p = (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p;3680for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i)3681{3682mz_uint total_header_size, disk_index, bit_flags, filename_size, ext_data_size;3683mz_uint64 comp_size, decomp_size, local_header_ofs;36843685if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG))3686return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);36873688MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, i) = (mz_uint32)(p - (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p);36893690if (sort_central_dir)3691MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_sorted_central_dir_offsets, mz_uint32, i) = i;36923693comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);3694decomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);3695local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);3696filename_size = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);3697ext_data_size = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS);36983699if ((!pZip->m_pState->m_zip64_has_extended_info_fields) &&3700(ext_data_size) &&3701(MZ_MAX(MZ_MAX(comp_size, decomp_size), local_header_ofs) == MZ_UINT32_MAX))3702{3703/* Attempt to find zip64 extended information field in the entry's extra data */3704mz_uint32 extra_size_remaining = ext_data_size;37053706if (extra_size_remaining)3707{3708const mz_uint8 *pExtra_data;3709void* buf = NULL;37103711if (MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + ext_data_size > n)3712{3713buf = MZ_MALLOC(ext_data_size);3714if(buf==NULL)3715return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);37163717if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size, buf, ext_data_size) != ext_data_size)3718{3719MZ_FREE(buf);3720return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);3721}37223723pExtra_data = (mz_uint8*)buf;3724}3725else3726{3727pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size;3728}37293730do3731{3732mz_uint32 field_id;3733mz_uint32 field_data_size;37343735if (extra_size_remaining < (sizeof(mz_uint16) * 2))3736{3737MZ_FREE(buf);3738return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);3739}37403741field_id = MZ_READ_LE16(pExtra_data);3742field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));37433744if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining)3745{3746MZ_FREE(buf);3747return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);3748}37493750if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)3751{3752/* Ok, the archive didn't have any zip64 headers but it uses a zip64 extended information field so mark it as zip64 anyway (this can occur with infozip's zip util when it reads compresses files from stdin). */3753pZip->m_pState->m_zip64 = MZ_TRUE;3754pZip->m_pState->m_zip64_has_extended_info_fields = MZ_TRUE;3755break;3756}37573758pExtra_data += sizeof(mz_uint16) * 2 + field_data_size;3759extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size;3760} while (extra_size_remaining);37613762MZ_FREE(buf);3763}3764}37653766/* I've seen archives that aren't marked as zip64 that uses zip64 ext data, argh */3767if ((comp_size != MZ_UINT32_MAX) && (decomp_size != MZ_UINT32_MAX))3768{3769if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size))3770return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);3771}37723773disk_index = MZ_READ_LE16(p + MZ_ZIP_CDH_DISK_START_OFS);3774if ((disk_index == MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1)))3775return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);37763777if (comp_size != MZ_UINT32_MAX)3778{3779if (((mz_uint64)MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS) + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > pZip->m_archive_size)3780return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);3781}37823783bit_flags = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);3784if (bit_flags & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED)3785return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);37863787if ((total_header_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS)) > n)3788return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);37893790n -= total_header_size;3791p += total_header_size;3792}3793}37943795if (sort_central_dir)3796mz_zip_reader_sort_central_dir_offsets_by_filename(pZip);37973798return MZ_TRUE;3799}38003801void mz_zip_zero_struct(mz_zip_archive *pZip)3802{3803if (pZip)3804MZ_CLEAR_OBJ(*pZip);3805}38063807static mz_bool mz_zip_reader_end_internal(mz_zip_archive *pZip, mz_bool set_last_error)3808{3809mz_bool status = MZ_TRUE;38103811if (!pZip)3812return MZ_FALSE;38133814if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))3815{3816if (set_last_error)3817pZip->m_last_error = MZ_ZIP_INVALID_PARAMETER;38183819return MZ_FALSE;3820}38213822if (pZip->m_pState)3823{3824mz_zip_internal_state *pState = pZip->m_pState;3825pZip->m_pState = NULL;38263827mz_zip_array_clear(pZip, &pState->m_central_dir);3828mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);3829mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);38303831#ifndef MINIZ_NO_STDIO3832if (pState->m_pFile)3833{3834if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE)3835{3836if (MZ_FCLOSE(pState->m_pFile) == EOF)3837{3838if (set_last_error)3839pZip->m_last_error = MZ_ZIP_FILE_CLOSE_FAILED;3840status = MZ_FALSE;3841}3842}3843pState->m_pFile = NULL;3844}3845#endif /* #ifndef MINIZ_NO_STDIO */38463847pZip->m_pFree(pZip->m_pAlloc_opaque, pState);3848}3849pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;38503851return status;3852}38533854mz_bool mz_zip_reader_end(mz_zip_archive *pZip)3855{3856return mz_zip_reader_end_internal(pZip, MZ_TRUE);3857}3858mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint flags)3859{3860if ((!pZip) || (!pZip->m_pRead))3861return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);38623863if (!mz_zip_reader_init_internal(pZip, flags))3864return MZ_FALSE;38653866pZip->m_zip_type = MZ_ZIP_TYPE_USER;3867pZip->m_archive_size = size;38683869if (!mz_zip_reader_read_central_dir(pZip, flags))3870{3871mz_zip_reader_end_internal(pZip, MZ_FALSE);3872return MZ_FALSE;3873}38743875return MZ_TRUE;3876}38773878static size_t mz_zip_mem_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)3879{3880mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;3881size_t s = (file_ofs >= pZip->m_archive_size) ? 0 : (size_t)MZ_MIN(pZip->m_archive_size - file_ofs, n);3882memcpy(pBuf, (const mz_uint8 *)pZip->m_pState->m_pMem + file_ofs, s);3883return s;3884}38853886mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint flags)3887{3888if (!pMem)3889return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);38903891if (size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)3892return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);38933894if (!mz_zip_reader_init_internal(pZip, flags))3895return MZ_FALSE;38963897pZip->m_zip_type = MZ_ZIP_TYPE_MEMORY;3898pZip->m_archive_size = size;3899pZip->m_pRead = mz_zip_mem_read_func;3900pZip->m_pIO_opaque = pZip;3901pZip->m_pNeeds_keepalive = NULL;39023903#ifdef __cplusplus3904pZip->m_pState->m_pMem = const_cast<void *>(pMem);3905#else3906pZip->m_pState->m_pMem = (void *)pMem;3907#endif39083909pZip->m_pState->m_mem_size = size;39103911if (!mz_zip_reader_read_central_dir(pZip, flags))3912{3913mz_zip_reader_end_internal(pZip, MZ_FALSE);3914return MZ_FALSE;3915}39163917return MZ_TRUE;3918}39193920#ifndef MINIZ_NO_STDIO3921static size_t mz_zip_file_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)3922{3923mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;3924mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);39253926file_ofs += pZip->m_pState->m_file_archive_start_ofs;39273928if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET))))3929return 0;39303931return MZ_FREAD(pBuf, 1, n, pZip->m_pState->m_pFile);3932}39333934mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags)3935{3936return mz_zip_reader_init_file_v2(pZip, pFilename, flags, 0, 0);3937}39383939mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags, mz_uint64 file_start_ofs, mz_uint64 archive_size)3940{3941mz_uint64 file_size;3942MZ_FILE *pFile;39433944if ((!pZip) || (!pFilename) || ((archive_size) && (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)))3945return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);39463947pFile = MZ_FOPEN(pFilename, "rb");3948if (!pFile)3949return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);39503951file_size = archive_size;3952if (!file_size)3953{3954if (MZ_FSEEK64(pFile, 0, SEEK_END))3955{3956MZ_FCLOSE(pFile);3957return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);3958}39593960file_size = MZ_FTELL64(pFile);3961}39623963/* TODO: Better sanity check archive_size and the # of actual remaining bytes */39643965if (file_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)3966{3967MZ_FCLOSE(pFile);3968return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);3969}39703971if (!mz_zip_reader_init_internal(pZip, flags))3972{3973MZ_FCLOSE(pFile);3974return MZ_FALSE;3975}39763977pZip->m_zip_type = MZ_ZIP_TYPE_FILE;3978pZip->m_pRead = mz_zip_file_read_func;3979pZip->m_pIO_opaque = pZip;3980pZip->m_pState->m_pFile = pFile;3981pZip->m_archive_size = file_size;3982pZip->m_pState->m_file_archive_start_ofs = file_start_ofs;39833984if (!mz_zip_reader_read_central_dir(pZip, flags))3985{3986mz_zip_reader_end_internal(pZip, MZ_FALSE);3987return MZ_FALSE;3988}39893990return MZ_TRUE;3991}39923993mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint64 archive_size, mz_uint flags)3994{3995mz_uint64 cur_file_ofs;39963997if ((!pZip) || (!pFile))3998return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);39994000cur_file_ofs = MZ_FTELL64(pFile);40014002if (!archive_size)4003{4004if (MZ_FSEEK64(pFile, 0, SEEK_END))4005return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);40064007archive_size = MZ_FTELL64(pFile) - cur_file_ofs;40084009if (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)4010return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);4011}40124013if (!mz_zip_reader_init_internal(pZip, flags))4014return MZ_FALSE;40154016pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;4017pZip->m_pRead = mz_zip_file_read_func;40184019pZip->m_pIO_opaque = pZip;4020pZip->m_pState->m_pFile = pFile;4021pZip->m_archive_size = archive_size;4022pZip->m_pState->m_file_archive_start_ofs = cur_file_ofs;40234024if (!mz_zip_reader_read_central_dir(pZip, flags))4025{4026mz_zip_reader_end_internal(pZip, MZ_FALSE);4027return MZ_FALSE;4028}40294030return MZ_TRUE;4031}40324033#endif /* #ifndef MINIZ_NO_STDIO */40344035static MZ_FORCEINLINE const mz_uint8 *mz_zip_get_cdh(mz_zip_archive *pZip, mz_uint file_index)4036{4037if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files))4038return NULL;4039return &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index));4040}40414042mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index)4043{4044mz_uint m_bit_flag;4045const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);4046if (!p)4047{4048mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);4049return MZ_FALSE;4050}40514052m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);4053return (m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION)) != 0;4054}40554056mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip, mz_uint file_index)4057{4058mz_uint bit_flag;4059mz_uint method;40604061const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);4062if (!p)4063{4064mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);4065return MZ_FALSE;4066}40674068method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS);4069bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);40704071if ((method != 0) && (method != MZ_DEFLATED))4072{4073mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);4074return MZ_FALSE;4075}40764077if (bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION))4078{4079mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);4080return MZ_FALSE;4081}40824083if (bit_flag & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)4084{4085mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);4086return MZ_FALSE;4087}40884089return MZ_TRUE;4090}40914092mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index)4093{4094mz_uint filename_len, attribute_mapping_id, external_attr;4095const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);4096if (!p)4097{4098mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);4099return MZ_FALSE;4100}41014102filename_len = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);4103if (filename_len)4104{4105if (*(p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len - 1) == '/')4106return MZ_TRUE;4107}41084109/* Bugfix: This code was also checking if the internal attribute was non-zero, which wasn't correct. */4110/* Most/all zip writers (hopefully) set DOS file/directory attributes in the low 16-bits, so check for the DOS directory flag and ignore the source OS ID in the created by field. */4111/* FIXME: Remove this check? Is it necessary - we already check the filename. */4112attribute_mapping_id = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS) >> 8;4113(void)attribute_mapping_id;41144115external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS);4116if ((external_attr & MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG) != 0)4117{4118return MZ_TRUE;4119}41204121return MZ_FALSE;4122}41234124static mz_bool mz_zip_file_stat_internal(mz_zip_archive *pZip, mz_uint file_index, const mz_uint8 *pCentral_dir_header, mz_zip_archive_file_stat *pStat, mz_bool *pFound_zip64_extra_data)4125{4126mz_uint n;4127const mz_uint8 *p = pCentral_dir_header;41284129if (pFound_zip64_extra_data)4130*pFound_zip64_extra_data = MZ_FALSE;41314132if ((!p) || (!pStat))4133return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);41344135/* Extract fields from the central directory record. */4136pStat->m_file_index = file_index;4137pStat->m_central_dir_ofs = MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index);4138pStat->m_version_made_by = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS);4139pStat->m_version_needed = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_NEEDED_OFS);4140pStat->m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);4141pStat->m_method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS);4142#ifndef MINIZ_NO_TIME4143pStat->m_time = mz_zip_dos_to_time_t(MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_TIME_OFS), MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_DATE_OFS));4144#endif4145pStat->m_crc32 = MZ_READ_LE32(p + MZ_ZIP_CDH_CRC32_OFS);4146pStat->m_comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);4147pStat->m_uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);4148pStat->m_internal_attr = MZ_READ_LE16(p + MZ_ZIP_CDH_INTERNAL_ATTR_OFS);4149pStat->m_external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS);4150pStat->m_local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);41514152/* Copy as much of the filename and comment as possible. */4153n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);4154n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - 1);4155memcpy(pStat->m_filename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n);4156pStat->m_filename[n] = '\0';41574158n = MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS);4159n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE - 1);4160pStat->m_comment_size = n;4161memcpy(pStat->m_comment, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS), n);4162pStat->m_comment[n] = '\0';41634164/* Set some flags for convienance */4165pStat->m_is_directory = mz_zip_reader_is_file_a_directory(pZip, file_index);4166pStat->m_is_encrypted = mz_zip_reader_is_file_encrypted(pZip, file_index);4167pStat->m_is_supported = mz_zip_reader_is_file_supported(pZip, file_index);41684169/* See if we need to read any zip64 extended information fields. */4170/* Confusingly, these zip64 fields can be present even on non-zip64 archives (Debian zip on a huge files from stdin piped to stdout creates them). */4171if (MZ_MAX(MZ_MAX(pStat->m_comp_size, pStat->m_uncomp_size), pStat->m_local_header_ofs) == MZ_UINT32_MAX)4172{4173/* Attempt to find zip64 extended information field in the entry's extra data */4174mz_uint32 extra_size_remaining = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS);41754176if (extra_size_remaining)4177{4178const mz_uint8 *pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);41794180do4181{4182mz_uint32 field_id;4183mz_uint32 field_data_size;41844185if (extra_size_remaining < (sizeof(mz_uint16) * 2))4186return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);41874188field_id = MZ_READ_LE16(pExtra_data);4189field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));41904191if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining)4192return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);41934194if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)4195{4196const mz_uint8 *pField_data = pExtra_data + sizeof(mz_uint16) * 2;4197mz_uint32 field_data_remaining = field_data_size;41984199if (pFound_zip64_extra_data)4200*pFound_zip64_extra_data = MZ_TRUE;42014202if (pStat->m_uncomp_size == MZ_UINT32_MAX)4203{4204if (field_data_remaining < sizeof(mz_uint64))4205return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);42064207pStat->m_uncomp_size = MZ_READ_LE64(pField_data);4208pField_data += sizeof(mz_uint64);4209field_data_remaining -= sizeof(mz_uint64);4210}42114212if (pStat->m_comp_size == MZ_UINT32_MAX)4213{4214if (field_data_remaining < sizeof(mz_uint64))4215return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);42164217pStat->m_comp_size = MZ_READ_LE64(pField_data);4218pField_data += sizeof(mz_uint64);4219field_data_remaining -= sizeof(mz_uint64);4220}42214222if (pStat->m_local_header_ofs == MZ_UINT32_MAX)4223{4224if (field_data_remaining < sizeof(mz_uint64))4225return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);42264227pStat->m_local_header_ofs = MZ_READ_LE64(pField_data);4228pField_data += sizeof(mz_uint64);4229field_data_remaining -= sizeof(mz_uint64);4230}42314232break;4233}42344235pExtra_data += sizeof(mz_uint16) * 2 + field_data_size;4236extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size;4237} while (extra_size_remaining);4238}4239}42404241return MZ_TRUE;4242}42434244static MZ_FORCEINLINE mz_bool mz_zip_string_equal(const char *pA, const char *pB, mz_uint len, mz_uint flags)4245{4246mz_uint i;4247if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE)4248return 0 == memcmp(pA, pB, len);4249for (i = 0; i < len; ++i)4250if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i]))4251return MZ_FALSE;4252return MZ_TRUE;4253}42544255static MZ_FORCEINLINE int mz_zip_filename_compare(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, const char *pR, mz_uint r_len)4256{4257const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE;4258mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS);4259mz_uint8 l = 0, r = 0;4260pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;4261pE = pL + MZ_MIN(l_len, r_len);4262while (pL < pE)4263{4264if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))4265break;4266pL++;4267pR++;4268}4269return (pL == pE) ? (int)(l_len - r_len) : (l - r);4270}42714272static mz_bool mz_zip_locate_file_binary_search(mz_zip_archive *pZip, const char *pFilename, mz_uint32 *pIndex)4273{4274mz_zip_internal_state *pState = pZip->m_pState;4275const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets;4276const mz_zip_array *pCentral_dir = &pState->m_central_dir;4277mz_uint32 *pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0);4278const uint32_t size = pZip->m_total_files;4279const mz_uint filename_len = (mz_uint)strlen(pFilename);42804281if (pIndex)4282*pIndex = 0;42834284if (size)4285{4286/* yes I could use uint32_t's, but then we would have to add some special case checks in the loop, argh, and */4287/* honestly the major expense here on 32-bit CPU's will still be the filename compare */4288mz_int64 l = 0, h = (mz_int64)size - 1;42894290while (l <= h)4291{4292mz_int64 m = l + ((h - l) >> 1);4293uint32_t file_index = pIndices[(uint32_t)m];42944295int comp = mz_zip_filename_compare(pCentral_dir, pCentral_dir_offsets, file_index, pFilename, filename_len);4296if (!comp)4297{4298if (pIndex)4299*pIndex = file_index;4300return MZ_TRUE;4301}4302else if (comp < 0)4303l = m + 1;4304else4305h = m - 1;4306}4307}43084309return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND);4310}43114312int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags)4313{4314mz_uint32 index;4315if (!mz_zip_reader_locate_file_v2(pZip, pName, pComment, flags, &index))4316return -1;4317else4318return (int)index;4319}43204321mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags, mz_uint32 *pIndex)4322{4323mz_uint file_index;4324size_t name_len, comment_len;43254326if (pIndex)4327*pIndex = 0;43284329if ((!pZip) || (!pZip->m_pState) || (!pName))4330return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);43314332/* See if we can use a binary search */4333if (((pZip->m_pState->m_init_flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0) &&4334(pZip->m_zip_mode == MZ_ZIP_MODE_READING) &&4335((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size))4336{4337return mz_zip_locate_file_binary_search(pZip, pName, pIndex);4338}43394340/* Locate the entry by scanning the entire central directory */4341name_len = strlen(pName);4342if (name_len > MZ_UINT16_MAX)4343return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);43444345comment_len = pComment ? strlen(pComment) : 0;4346if (comment_len > MZ_UINT16_MAX)4347return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);43484349for (file_index = 0; file_index < pZip->m_total_files; file_index++)4350{4351const mz_uint8 *pHeader = &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index));4352mz_uint filename_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS);4353const char *pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;4354if (filename_len < name_len)4355continue;4356if (comment_len)4357{4358mz_uint file_extra_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_EXTRA_LEN_OFS), file_comment_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_COMMENT_LEN_OFS);4359const char *pFile_comment = pFilename + filename_len + file_extra_len;4360if ((file_comment_len != comment_len) || (!mz_zip_string_equal(pComment, pFile_comment, file_comment_len, flags)))4361continue;4362}4363if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len))4364{4365int ofs = filename_len - 1;4366do4367{4368if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || (pFilename[ofs] == ':'))4369break;4370} while (--ofs >= 0);4371ofs++;4372pFilename += ofs;4373filename_len -= ofs;4374}4375if ((filename_len == name_len) && (mz_zip_string_equal(pName, pFilename, filename_len, flags)))4376{4377if (pIndex)4378*pIndex = file_index;4379return MZ_TRUE;4380}4381}43824383return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND);4384}43854386mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size)4387{4388int status = TINFL_STATUS_DONE;4389mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail;4390mz_zip_archive_file_stat file_stat;4391void *pRead_buf;4392mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];4393mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;4394tinfl_decompressor inflator;43954396if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead))4397return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);43984399if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))4400return MZ_FALSE;44014402/* A directory or zero length file */4403if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))4404return MZ_TRUE;44054406/* Encryption and patch files are not supported. */4407if (file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))4408return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);44094410/* This function only supports decompressing stored and deflate. */4411if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))4412return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);44134414/* Ensure supplied output buffer is large enough. */4415needed_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size;4416if (buf_size < needed_size)4417return mz_zip_set_error(pZip, MZ_ZIP_BUF_TOO_SMALL);44184419/* Read and parse the local directory entry. */4420cur_file_ofs = file_stat.m_local_header_ofs;4421if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)4422return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);44234424if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)4425return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);44264427cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);4428if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)4429return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);44304431if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method))4432{4433/* The file is stored or the caller has requested the compressed data. */4434if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, (size_t)needed_size) != needed_size)4435return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);44364437#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS4438if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) == 0)4439{4440if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32)4441return mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED);4442}4443#endif44444445return MZ_TRUE;4446}44474448/* Decompress the file either directly from memory or from a file input buffer. */4449tinfl_init(&inflator);44504451if (pZip->m_pState->m_pMem)4452{4453/* Read directly from the archive in memory. */4454pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs;4455read_buf_size = read_buf_avail = file_stat.m_comp_size;4456comp_remaining = 0;4457}4458else if (pUser_read_buf)4459{4460/* Use a user provided read buffer. */4461if (!user_read_buf_size)4462return MZ_FALSE;4463pRead_buf = (mz_uint8 *)pUser_read_buf;4464read_buf_size = user_read_buf_size;4465read_buf_avail = 0;4466comp_remaining = file_stat.m_comp_size;4467}4468else4469{4470/* Temporarily allocate a read buffer. */4471read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);4472if (((sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF))4473return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);44744475if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size)))4476return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);44774478read_buf_avail = 0;4479comp_remaining = file_stat.m_comp_size;4480}44814482do4483{4484/* The size_t cast here should be OK because we've verified that the output buffer is >= file_stat.m_uncomp_size above */4485size_t in_buf_size, out_buf_size = (size_t)(file_stat.m_uncomp_size - out_buf_ofs);4486if ((!read_buf_avail) && (!pZip->m_pState->m_pMem))4487{4488read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);4489if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)4490{4491status = TINFL_STATUS_FAILED;4492mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);4493break;4494}4495cur_file_ofs += read_buf_avail;4496comp_remaining -= read_buf_avail;4497read_buf_ofs = 0;4498}4499in_buf_size = (size_t)read_buf_avail;4500status = tinfl_decompress(&inflator, (mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pBuf, (mz_uint8 *)pBuf + out_buf_ofs, &out_buf_size, TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | (comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0));4501read_buf_avail -= in_buf_size;4502read_buf_ofs += in_buf_size;4503out_buf_ofs += out_buf_size;4504} while (status == TINFL_STATUS_NEEDS_MORE_INPUT);45054506if (status == TINFL_STATUS_DONE)4507{4508/* Make sure the entire file was decompressed, and check its CRC. */4509if (out_buf_ofs != file_stat.m_uncomp_size)4510{4511mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);4512status = TINFL_STATUS_FAILED;4513}4514#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS4515else if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32)4516{4517mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED);4518status = TINFL_STATUS_FAILED;4519}4520#endif4521}45224523if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf))4524pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);45254526return status == TINFL_STATUS_DONE;4527}45284529mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size)4530{4531mz_uint32 file_index;4532if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))4533return MZ_FALSE;4534return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size);4535}45364537mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags)4538{4539return mz_zip_reader_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, NULL, 0);4540}45414542mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags)4543{4544return mz_zip_reader_extract_file_to_mem_no_alloc(pZip, pFilename, pBuf, buf_size, flags, NULL, 0);4545}45464547void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags)4548{4549mz_uint64 comp_size, uncomp_size, alloc_size;4550const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);4551void *pBuf;45524553if (pSize)4554*pSize = 0;45554556if (!p)4557{4558mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);4559return NULL;4560}45614562comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);4563uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);45644565alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? comp_size : uncomp_size;4566if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF))4567{4568mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);4569return NULL;4570}45714572if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)alloc_size)))4573{4574mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);4575return NULL;4576}45774578if (!mz_zip_reader_extract_to_mem(pZip, file_index, pBuf, (size_t)alloc_size, flags))4579{4580pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);4581return NULL;4582}45834584if (pSize)4585*pSize = (size_t)alloc_size;4586return pBuf;4587}45884589void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags)4590{4591mz_uint32 file_index;4592if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))4593{4594if (pSize)4595*pSize = 0;4596return MZ_FALSE;4597}4598return mz_zip_reader_extract_to_heap(pZip, file_index, pSize, flags);4599}46004601mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, void *pOpaque, mz_uint flags)4602{4603int status = TINFL_STATUS_DONE;4604#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS4605mz_uint file_crc32 = MZ_CRC32_INIT;4606#endif4607mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, out_buf_ofs = 0, cur_file_ofs;4608mz_zip_archive_file_stat file_stat;4609void *pRead_buf = NULL;4610void *pWrite_buf = NULL;4611mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];4612mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;46134614if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead))4615return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);46164617if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))4618return MZ_FALSE;46194620/* A directory or zero length file */4621if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))4622return MZ_TRUE;46234624/* Encryption and patch files are not supported. */4625if (file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))4626return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);46274628/* This function only supports decompressing stored and deflate. */4629if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))4630return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);46314632/* Read and do some minimal validation of the local directory entry (this doesn't crack the zip64 stuff, which we already have from the central dir) */4633cur_file_ofs = file_stat.m_local_header_ofs;4634if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)4635return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);46364637if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)4638return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);46394640cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);4641if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)4642return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);46434644/* Decompress the file either directly from memory or from a file input buffer. */4645if (pZip->m_pState->m_pMem)4646{4647pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs;4648read_buf_size = read_buf_avail = file_stat.m_comp_size;4649comp_remaining = 0;4650}4651else4652{4653read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);4654if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size)))4655return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);46564657read_buf_avail = 0;4658comp_remaining = file_stat.m_comp_size;4659}46604661if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method))4662{4663/* The file is stored or the caller has requested the compressed data. */4664if (pZip->m_pState->m_pMem)4665{4666if (((sizeof(size_t) == sizeof(mz_uint32))) && (file_stat.m_comp_size > MZ_UINT32_MAX))4667return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);46684669if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)file_stat.m_comp_size) != file_stat.m_comp_size)4670{4671mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);4672status = TINFL_STATUS_FAILED;4673}4674else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))4675{4676#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS4677file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, (size_t)file_stat.m_comp_size);4678#endif4679}46804681cur_file_ofs += file_stat.m_comp_size;4682out_buf_ofs += file_stat.m_comp_size;4683comp_remaining = 0;4684}4685else4686{4687while (comp_remaining)4688{4689read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);4690if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)4691{4692mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);4693status = TINFL_STATUS_FAILED;4694break;4695}46964697#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS4698if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))4699{4700file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, (size_t)read_buf_avail);4701}4702#endif47034704if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)4705{4706mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);4707status = TINFL_STATUS_FAILED;4708break;4709}47104711cur_file_ofs += read_buf_avail;4712out_buf_ofs += read_buf_avail;4713comp_remaining -= read_buf_avail;4714}4715}4716}4717else4718{4719tinfl_decompressor inflator;4720tinfl_init(&inflator);47214722if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE)))4723{4724mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);4725status = TINFL_STATUS_FAILED;4726}4727else4728{4729do4730{4731mz_uint8 *pWrite_buf_cur = (mz_uint8 *)pWrite_buf + (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));4732size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));4733if ((!read_buf_avail) && (!pZip->m_pState->m_pMem))4734{4735read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);4736if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)4737{4738mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);4739status = TINFL_STATUS_FAILED;4740break;4741}4742cur_file_ofs += read_buf_avail;4743comp_remaining -= read_buf_avail;4744read_buf_ofs = 0;4745}47464747in_buf_size = (size_t)read_buf_avail;4748status = tinfl_decompress(&inflator, (const mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pWrite_buf, pWrite_buf_cur, &out_buf_size, comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0);4749read_buf_avail -= in_buf_size;4750read_buf_ofs += in_buf_size;47514752if (out_buf_size)4753{4754if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size)4755{4756mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);4757status = TINFL_STATUS_FAILED;4758break;4759}47604761#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS4762file_crc32 = (mz_uint32)mz_crc32(file_crc32, pWrite_buf_cur, out_buf_size);4763#endif4764if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size)4765{4766mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);4767status = TINFL_STATUS_FAILED;4768break;4769}4770}4771} while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || (status == TINFL_STATUS_HAS_MORE_OUTPUT));4772}4773}47744775if ((status == TINFL_STATUS_DONE) && (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))4776{4777/* Make sure the entire file was decompressed, and check its CRC. */4778if (out_buf_ofs != file_stat.m_uncomp_size)4779{4780mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);4781status = TINFL_STATUS_FAILED;4782}4783#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS4784else if (file_crc32 != file_stat.m_crc32)4785{4786mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);4787status = TINFL_STATUS_FAILED;4788}4789#endif4790}47914792if (!pZip->m_pState->m_pMem)4793pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);47944795if (pWrite_buf)4796pZip->m_pFree(pZip->m_pAlloc_opaque, pWrite_buf);47974798return status == TINFL_STATUS_DONE;4799}48004801mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, void *pOpaque, mz_uint flags)4802{4803mz_uint32 file_index;4804if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))4805return MZ_FALSE;48064807return mz_zip_reader_extract_to_callback(pZip, file_index, pCallback, pOpaque, flags);4808}48094810mz_zip_reader_extract_iter_state* mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags)4811{4812mz_zip_reader_extract_iter_state *pState;4813mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];4814mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;48154816/* Argument sanity check */4817if ((!pZip) || (!pZip->m_pState))4818return NULL;48194820/* Allocate an iterator status structure */4821pState = (mz_zip_reader_extract_iter_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_reader_extract_iter_state));4822if (!pState)4823{4824mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);4825return NULL;4826}48274828/* Fetch file details */4829if (!mz_zip_reader_file_stat(pZip, file_index, &pState->file_stat))4830{4831pZip->m_pFree(pZip->m_pAlloc_opaque, pState);4832return NULL;4833}48344835/* Encryption and patch files are not supported. */4836if (pState->file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))4837{4838mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);4839pZip->m_pFree(pZip->m_pAlloc_opaque, pState);4840return NULL;4841}48424843/* This function only supports decompressing stored and deflate. */4844if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (pState->file_stat.m_method != 0) && (pState->file_stat.m_method != MZ_DEFLATED))4845{4846mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);4847pZip->m_pFree(pZip->m_pAlloc_opaque, pState);4848return NULL;4849}48504851/* Init state - save args */4852pState->pZip = pZip;4853pState->flags = flags;48544855/* Init state - reset variables to defaults */4856pState->status = TINFL_STATUS_DONE;4857#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS4858pState->file_crc32 = MZ_CRC32_INIT;4859#endif4860pState->read_buf_ofs = 0;4861pState->out_buf_ofs = 0;4862pState->pRead_buf = NULL;4863pState->pWrite_buf = NULL;4864pState->out_blk_remain = 0;48654866/* Read and parse the local directory entry. */4867pState->cur_file_ofs = pState->file_stat.m_local_header_ofs;4868if (pZip->m_pRead(pZip->m_pIO_opaque, pState->cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)4869{4870mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);4871pZip->m_pFree(pZip->m_pAlloc_opaque, pState);4872return NULL;4873}48744875if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)4876{4877mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);4878pZip->m_pFree(pZip->m_pAlloc_opaque, pState);4879return NULL;4880}48814882pState->cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);4883if ((pState->cur_file_ofs + pState->file_stat.m_comp_size) > pZip->m_archive_size)4884{4885mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);4886pZip->m_pFree(pZip->m_pAlloc_opaque, pState);4887return NULL;4888}48894890/* Decompress the file either directly from memory or from a file input buffer. */4891if (pZip->m_pState->m_pMem)4892{4893pState->pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + pState->cur_file_ofs;4894pState->read_buf_size = pState->read_buf_avail = pState->file_stat.m_comp_size;4895pState->comp_remaining = pState->file_stat.m_comp_size;4896}4897else4898{4899if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)))4900{4901/* Decompression required, therefore intermediate read buffer required */4902pState->read_buf_size = MZ_MIN(pState->file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);4903if (NULL == (pState->pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)pState->read_buf_size)))4904{4905mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);4906pZip->m_pFree(pZip->m_pAlloc_opaque, pState);4907return NULL;4908}4909}4910else4911{4912/* Decompression not required - we will be reading directly into user buffer, no temp buf required */4913pState->read_buf_size = 0;4914}4915pState->read_buf_avail = 0;4916pState->comp_remaining = pState->file_stat.m_comp_size;4917}49184919if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)))4920{4921/* Decompression required, init decompressor */4922tinfl_init( &pState->inflator );49234924/* Allocate write buffer */4925if (NULL == (pState->pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE)))4926{4927mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);4928if (pState->pRead_buf)4929pZip->m_pFree(pZip->m_pAlloc_opaque, pState->pRead_buf);4930pZip->m_pFree(pZip->m_pAlloc_opaque, pState);4931return NULL;4932}4933}49344935return pState;4936}49374938mz_zip_reader_extract_iter_state* mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)4939{4940mz_uint32 file_index;49414942/* Locate file index by name */4943if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))4944return NULL;49454946/* Construct iterator */4947return mz_zip_reader_extract_iter_new(pZip, file_index, flags);4948}49494950size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state* pState, void* pvBuf, size_t buf_size)4951{4952size_t copied_to_caller = 0;49534954/* Argument sanity check */4955if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf))4956return 0;49574958if ((pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))4959{4960/* The file is stored or the caller has requested the compressed data, calc amount to return. */4961copied_to_caller = (size_t)MZ_MIN( buf_size, pState->comp_remaining );49624963/* Zip is in memory....or requires reading from a file? */4964if (pState->pZip->m_pState->m_pMem)4965{4966/* Copy data to caller's buffer */4967memcpy( pvBuf, pState->pRead_buf, copied_to_caller );4968pState->pRead_buf = ((mz_uint8*)pState->pRead_buf) + copied_to_caller;4969}4970else4971{4972/* Read directly into caller's buffer */4973if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pvBuf, copied_to_caller) != copied_to_caller)4974{4975/* Failed to read all that was asked for, flag failure and alert user */4976mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED);4977pState->status = TINFL_STATUS_FAILED;4978copied_to_caller = 0;4979}4980}49814982#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS4983/* Compute CRC if not returning compressed data only */4984if (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))4985pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, (const mz_uint8 *)pvBuf, copied_to_caller);4986#endif49874988/* Advance offsets, dec counters */4989pState->cur_file_ofs += copied_to_caller;4990pState->out_buf_ofs += copied_to_caller;4991pState->comp_remaining -= copied_to_caller;4992}4993else4994{4995do4996{4997/* Calc ptr to write buffer - given current output pos and block size */4998mz_uint8 *pWrite_buf_cur = (mz_uint8 *)pState->pWrite_buf + (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));49995000/* Calc max output size - given current output pos and block size */5001size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));50025003if (!pState->out_blk_remain)5004{5005/* Read more data from file if none available (and reading from file) */5006if ((!pState->read_buf_avail) && (!pState->pZip->m_pState->m_pMem))5007{5008/* Calc read size */5009pState->read_buf_avail = MZ_MIN(pState->read_buf_size, pState->comp_remaining);5010if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pState->pRead_buf, (size_t)pState->read_buf_avail) != pState->read_buf_avail)5011{5012mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED);5013pState->status = TINFL_STATUS_FAILED;5014break;5015}50165017/* Advance offsets, dec counters */5018pState->cur_file_ofs += pState->read_buf_avail;5019pState->comp_remaining -= pState->read_buf_avail;5020pState->read_buf_ofs = 0;5021}50225023/* Perform decompression */5024in_buf_size = (size_t)pState->read_buf_avail;5025pState->status = tinfl_decompress(&pState->inflator, (const mz_uint8 *)pState->pRead_buf + pState->read_buf_ofs, &in_buf_size, (mz_uint8 *)pState->pWrite_buf, pWrite_buf_cur, &out_buf_size, pState->comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0);5026pState->read_buf_avail -= in_buf_size;5027pState->read_buf_ofs += in_buf_size;50285029/* Update current output block size remaining */5030pState->out_blk_remain = out_buf_size;5031}50325033if (pState->out_blk_remain)5034{5035/* Calc amount to return. */5036size_t to_copy = MZ_MIN( (buf_size - copied_to_caller), pState->out_blk_remain );50375038/* Copy data to caller's buffer */5039memcpy( (uint8_t*)pvBuf + copied_to_caller, pWrite_buf_cur, to_copy );50405041#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS5042/* Perform CRC */5043pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, pWrite_buf_cur, to_copy);5044#endif50455046/* Decrement data consumed from block */5047pState->out_blk_remain -= to_copy;50485049/* Inc output offset, while performing sanity check */5050if ((pState->out_buf_ofs += to_copy) > pState->file_stat.m_uncomp_size)5051{5052mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED);5053pState->status = TINFL_STATUS_FAILED;5054break;5055}50565057/* Increment counter of data copied to caller */5058copied_to_caller += to_copy;5059}5060} while ( (copied_to_caller < buf_size) && ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) || (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT)) );5061}50625063/* Return how many bytes were copied into user buffer */5064return copied_to_caller;5065}50665067mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state* pState)5068{5069int status;50705071/* Argument sanity check */5072if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState))5073return MZ_FALSE;50745075/* Was decompression completed and requested? */5076if ((pState->status == TINFL_STATUS_DONE) && (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))5077{5078/* Make sure the entire file was decompressed, and check its CRC. */5079if (pState->out_buf_ofs != pState->file_stat.m_uncomp_size)5080{5081mz_zip_set_error(pState->pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);5082pState->status = TINFL_STATUS_FAILED;5083}5084#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS5085else if (pState->file_crc32 != pState->file_stat.m_crc32)5086{5087mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED);5088pState->status = TINFL_STATUS_FAILED;5089}5090#endif5091}50925093/* Free buffers */5094if (!pState->pZip->m_pState->m_pMem)5095pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pRead_buf);5096if (pState->pWrite_buf)5097pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pWrite_buf);50985099/* Save status */5100status = pState->status;51015102/* Free context */5103pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState);51045105return status == TINFL_STATUS_DONE;5106}51075108#ifndef MINIZ_NO_STDIO5109static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n)5110{5111(void)ofs;51125113return MZ_FWRITE(pBuf, 1, n, (MZ_FILE *)pOpaque);5114}51155116mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags)5117{5118mz_bool status;5119mz_zip_archive_file_stat file_stat;5120MZ_FILE *pFile;51215122if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))5123return MZ_FALSE;51245125if ((file_stat.m_is_directory) || (!file_stat.m_is_supported))5126return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);51275128pFile = MZ_FOPEN(pDst_filename, "wb");5129if (!pFile)5130return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);51315132status = mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags);51335134if (MZ_FCLOSE(pFile) == EOF)5135{5136if (status)5137mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);51385139status = MZ_FALSE;5140}51415142#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)5143if (status)5144mz_zip_set_file_times(pDst_filename, file_stat.m_time, file_stat.m_time);5145#endif51465147return status;5148}51495150mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags)5151{5152mz_uint32 file_index;5153if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags, &file_index))5154return MZ_FALSE;51555156return mz_zip_reader_extract_to_file(pZip, file_index, pDst_filename, flags);5157}51585159mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip, mz_uint file_index, MZ_FILE *pFile, mz_uint flags)5160{5161mz_zip_archive_file_stat file_stat;51625163if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))5164return MZ_FALSE;51655166if ((file_stat.m_is_directory) || (!file_stat.m_is_supported))5167return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);51685169return mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags);5170}51715172mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive *pZip, const char *pArchive_filename, MZ_FILE *pFile, mz_uint flags)5173{5174mz_uint32 file_index;5175if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags, &file_index))5176return MZ_FALSE;51775178return mz_zip_reader_extract_to_cfile(pZip, file_index, pFile, flags);5179}5180#endif /* #ifndef MINIZ_NO_STDIO */51815182static size_t mz_zip_compute_crc32_callback(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)5183{5184mz_uint32 *p = (mz_uint32 *)pOpaque;5185(void)file_ofs;5186*p = (mz_uint32)mz_crc32(*p, (const mz_uint8 *)pBuf, n);5187return n;5188}51895190mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags)5191{5192mz_zip_archive_file_stat file_stat;5193mz_zip_internal_state *pState;5194const mz_uint8 *pCentral_dir_header;5195mz_bool found_zip64_ext_data_in_cdir = MZ_FALSE;5196mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE;5197mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];5198mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;5199mz_uint64 local_header_ofs = 0;5200mz_uint32 local_header_filename_len, local_header_extra_len, local_header_crc32;5201mz_uint64 local_header_comp_size, local_header_uncomp_size;5202mz_uint32 uncomp_crc32 = MZ_CRC32_INIT;5203mz_bool has_data_descriptor;5204mz_uint32 local_header_bit_flags;52055206mz_zip_array file_data_array;5207mz_zip_array_init(&file_data_array, 1);52085209if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead))5210return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);52115212if (file_index > pZip->m_total_files)5213return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);52145215pState = pZip->m_pState;52165217pCentral_dir_header = mz_zip_get_cdh(pZip, file_index);52185219if (!mz_zip_file_stat_internal(pZip, file_index, pCentral_dir_header, &file_stat, &found_zip64_ext_data_in_cdir))5220return MZ_FALSE;52215222/* A directory or zero length file */5223if ((file_stat.m_is_directory) || (!file_stat.m_uncomp_size))5224return MZ_TRUE;52255226/* Encryption and patch files are not supported. */5227if (file_stat.m_is_encrypted)5228return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);52295230/* This function only supports stored and deflate. */5231if ((file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))5232return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);52335234if (!file_stat.m_is_supported)5235return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);52365237/* Read and parse the local directory entry. */5238local_header_ofs = file_stat.m_local_header_ofs;5239if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)5240return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);52415242if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)5243return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);52445245local_header_filename_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS);5246local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);5247local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS);5248local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS);5249local_header_crc32 = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_CRC32_OFS);5250local_header_bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS);5251has_data_descriptor = (local_header_bit_flags & 8) != 0;52525253if (local_header_filename_len != strlen(file_stat.m_filename))5254return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);52555256if ((local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + file_stat.m_comp_size) > pZip->m_archive_size)5257return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);52585259if (!mz_zip_array_resize(pZip, &file_data_array, MZ_MAX(local_header_filename_len, local_header_extra_len), MZ_FALSE))5260{5261mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);5262goto handle_failure;5263}52645265if (local_header_filename_len)5266{5267if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE, file_data_array.m_p, local_header_filename_len) != local_header_filename_len)5268{5269mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);5270goto handle_failure;5271}52725273/* I've seen 1 archive that had the same pathname, but used backslashes in the local dir and forward slashes in the central dir. Do we care about this? For now, this case will fail validation. */5274if (memcmp(file_stat.m_filename, file_data_array.m_p, local_header_filename_len) != 0)5275{5276mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);5277goto handle_failure;5278}5279}52805281if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX)))5282{5283mz_uint32 extra_size_remaining = local_header_extra_len;5284const mz_uint8 *pExtra_data = (const mz_uint8 *)file_data_array.m_p;52855286if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len, file_data_array.m_p, local_header_extra_len) != local_header_extra_len)5287{5288mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);5289goto handle_failure;5290}52915292do5293{5294mz_uint32 field_id, field_data_size, field_total_size;52955296if (extra_size_remaining < (sizeof(mz_uint16) * 2))5297{5298mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);5299goto handle_failure;5300}53015302field_id = MZ_READ_LE16(pExtra_data);5303field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));5304field_total_size = field_data_size + sizeof(mz_uint16) * 2;53055306if (field_total_size > extra_size_remaining)5307{5308mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);5309goto handle_failure;5310}53115312if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)5313{5314const mz_uint8 *pSrc_field_data = pExtra_data + sizeof(mz_uint32);53155316if (field_data_size < sizeof(mz_uint64) * 2)5317{5318mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);5319goto handle_failure;5320}53215322local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data);5323local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64));53245325found_zip64_ext_data_in_ldir = MZ_TRUE;5326break;5327}53285329pExtra_data += field_total_size;5330extra_size_remaining -= field_total_size;5331} while (extra_size_remaining);5332}53335334/* TODO: parse local header extra data when local_header_comp_size is 0xFFFFFFFF! (big_descriptor.zip) */5335/* I've seen zips in the wild with the data descriptor bit set, but proper local header values and bogus data descriptors */5336if ((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32))5337{5338mz_uint8 descriptor_buf[32];5339mz_bool has_id;5340const mz_uint8 *pSrc;5341mz_uint32 file_crc32;5342mz_uint64 comp_size = 0, uncomp_size = 0;53435344mz_uint32 num_descriptor_uint32s = ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4;53455346if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + file_stat.m_comp_size, descriptor_buf, sizeof(mz_uint32) * num_descriptor_uint32s) != (sizeof(mz_uint32) * num_descriptor_uint32s))5347{5348mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);5349goto handle_failure;5350}53515352has_id = (MZ_READ_LE32(descriptor_buf) == MZ_ZIP_DATA_DESCRIPTOR_ID);5353pSrc = has_id ? (descriptor_buf + sizeof(mz_uint32)) : descriptor_buf;53545355file_crc32 = MZ_READ_LE32(pSrc);53565357if ((pState->m_zip64) || (found_zip64_ext_data_in_ldir))5358{5359comp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32));5360uncomp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32) + sizeof(mz_uint64));5361}5362else5363{5364comp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32));5365uncomp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32) + sizeof(mz_uint32));5366}53675368if ((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || (uncomp_size != file_stat.m_uncomp_size))5369{5370mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);5371goto handle_failure;5372}5373}5374else5375{5376if ((local_header_crc32 != file_stat.m_crc32) || (local_header_comp_size != file_stat.m_comp_size) || (local_header_uncomp_size != file_stat.m_uncomp_size))5377{5378mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);5379goto handle_failure;5380}5381}53825383mz_zip_array_clear(pZip, &file_data_array);53845385if ((flags & MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY) == 0)5386{5387if (!mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_compute_crc32_callback, &uncomp_crc32, 0))5388return MZ_FALSE;53895390/* 1 more check to be sure, although the extract checks too. */5391if (uncomp_crc32 != file_stat.m_crc32)5392{5393mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);5394return MZ_FALSE;5395}5396}53975398return MZ_TRUE;53995400handle_failure:5401mz_zip_array_clear(pZip, &file_data_array);5402return MZ_FALSE;5403}54045405mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags)5406{5407mz_zip_internal_state *pState;5408uint32_t i;54095410if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead))5411return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);54125413pState = pZip->m_pState;54145415/* Basic sanity checks */5416if (!pState->m_zip64)5417{5418if (pZip->m_total_files > MZ_UINT16_MAX)5419return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);54205421if (pZip->m_archive_size > MZ_UINT32_MAX)5422return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);5423}5424else5425{5426if (pZip->m_total_files >= MZ_UINT32_MAX)5427return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);54285429if (pState->m_central_dir.m_size >= MZ_UINT32_MAX)5430return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);5431}54325433for (i = 0; i < pZip->m_total_files; i++)5434{5435if (MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG & flags)5436{5437mz_uint32 found_index;5438mz_zip_archive_file_stat stat;54395440if (!mz_zip_reader_file_stat(pZip, i, &stat))5441return MZ_FALSE;54425443if (!mz_zip_reader_locate_file_v2(pZip, stat.m_filename, NULL, 0, &found_index))5444return MZ_FALSE;54455446/* This check can fail if there are duplicate filenames in the archive (which we don't check for when writing - that's up to the user) */5447if (found_index != i)5448return mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);5449}54505451if (!mz_zip_validate_file(pZip, i, flags))5452return MZ_FALSE;5453}54545455return MZ_TRUE;5456}54575458mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size, mz_uint flags, mz_zip_error *pErr)5459{5460mz_bool success = MZ_TRUE;5461mz_zip_archive zip;5462mz_zip_error actual_err = MZ_ZIP_NO_ERROR;54635464if ((!pMem) || (!size))5465{5466if (pErr)5467*pErr = MZ_ZIP_INVALID_PARAMETER;5468return MZ_FALSE;5469}54705471mz_zip_zero_struct(&zip);54725473if (!mz_zip_reader_init_mem(&zip, pMem, size, flags))5474{5475if (pErr)5476*pErr = zip.m_last_error;5477return MZ_FALSE;5478}54795480if (!mz_zip_validate_archive(&zip, flags))5481{5482actual_err = zip.m_last_error;5483success = MZ_FALSE;5484}54855486if (!mz_zip_reader_end_internal(&zip, success))5487{5488if (!actual_err)5489actual_err = zip.m_last_error;5490success = MZ_FALSE;5491}54925493if (pErr)5494*pErr = actual_err;54955496return success;5497}54985499#ifndef MINIZ_NO_STDIO5500mz_bool mz_zip_validate_file_archive(const char *pFilename, mz_uint flags, mz_zip_error *pErr)5501{5502mz_bool success = MZ_TRUE;5503mz_zip_archive zip;5504mz_zip_error actual_err = MZ_ZIP_NO_ERROR;55055506if (!pFilename)5507{5508if (pErr)5509*pErr = MZ_ZIP_INVALID_PARAMETER;5510return MZ_FALSE;5511}55125513mz_zip_zero_struct(&zip);55145515if (!mz_zip_reader_init_file_v2(&zip, pFilename, flags, 0, 0))5516{5517if (pErr)5518*pErr = zip.m_last_error;5519return MZ_FALSE;5520}55215522if (!mz_zip_validate_archive(&zip, flags))5523{5524actual_err = zip.m_last_error;5525success = MZ_FALSE;5526}55275528if (!mz_zip_reader_end_internal(&zip, success))5529{5530if (!actual_err)5531actual_err = zip.m_last_error;5532success = MZ_FALSE;5533}55345535if (pErr)5536*pErr = actual_err;55375538return success;5539}5540#endif /* #ifndef MINIZ_NO_STDIO */55415542/* ------------------- .ZIP archive writing */55435544#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS55455546static MZ_FORCEINLINE void mz_write_le16(mz_uint8 *p, mz_uint16 v)5547{5548p[0] = (mz_uint8)v;5549p[1] = (mz_uint8)(v >> 8);5550}5551static MZ_FORCEINLINE void mz_write_le32(mz_uint8 *p, mz_uint32 v)5552{5553p[0] = (mz_uint8)v;5554p[1] = (mz_uint8)(v >> 8);5555p[2] = (mz_uint8)(v >> 16);5556p[3] = (mz_uint8)(v >> 24);5557}5558static MZ_FORCEINLINE void mz_write_le64(mz_uint8 *p, mz_uint64 v)5559{5560mz_write_le32(p, (mz_uint32)v);5561mz_write_le32(p + sizeof(mz_uint32), (mz_uint32)(v >> 32));5562}55635564#define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8 *)(p), (mz_uint16)(v))5565#define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8 *)(p), (mz_uint32)(v))5566#define MZ_WRITE_LE64(p, v) mz_write_le64((mz_uint8 *)(p), (mz_uint64)(v))55675568static size_t mz_zip_heap_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)5569{5570mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;5571mz_zip_internal_state *pState = pZip->m_pState;5572mz_uint64 new_size = MZ_MAX(file_ofs + n, pState->m_mem_size);55735574if (!n)5575return 0;55765577/* An allocation this big is likely to just fail on 32-bit systems, so don't even go there. */5578if ((sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF))5579{5580mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);5581return 0;5582}55835584if (new_size > pState->m_mem_capacity)5585{5586void *pNew_block;5587size_t new_capacity = MZ_MAX(64, pState->m_mem_capacity);55885589while (new_capacity < new_size)5590new_capacity *= 2;55915592if (NULL == (pNew_block = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity)))5593{5594mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);5595return 0;5596}55975598pState->m_pMem = pNew_block;5599pState->m_mem_capacity = new_capacity;5600}5601memcpy((mz_uint8 *)pState->m_pMem + file_ofs, pBuf, n);5602pState->m_mem_size = (size_t)new_size;5603return n;5604}56055606static mz_bool mz_zip_writer_end_internal(mz_zip_archive *pZip, mz_bool set_last_error)5607{5608mz_zip_internal_state *pState;5609mz_bool status = MZ_TRUE;56105611if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED)))5612{5613if (set_last_error)5614mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);5615return MZ_FALSE;5616}56175618pState = pZip->m_pState;5619pZip->m_pState = NULL;5620mz_zip_array_clear(pZip, &pState->m_central_dir);5621mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);5622mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);56235624#ifndef MINIZ_NO_STDIO5625if (pState->m_pFile)5626{5627if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE)5628{5629if (MZ_FCLOSE(pState->m_pFile) == EOF)5630{5631if (set_last_error)5632mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);5633status = MZ_FALSE;5634}5635}56365637pState->m_pFile = NULL;5638}5639#endif /* #ifndef MINIZ_NO_STDIO */56405641if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem))5642{5643pZip->m_pFree(pZip->m_pAlloc_opaque, pState->m_pMem);5644pState->m_pMem = NULL;5645}56465647pZip->m_pFree(pZip->m_pAlloc_opaque, pState);5648pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;5649return status;5650}56515652mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip, mz_uint64 existing_size, mz_uint flags)5653{5654mz_bool zip64 = (flags & MZ_ZIP_FLAG_WRITE_ZIP64) != 0;56555656if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))5657return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);56585659if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)5660{5661if (!pZip->m_pRead)5662return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);5663}56645665if (pZip->m_file_offset_alignment)5666{5667/* Ensure user specified file offset alignment is a power of 2. */5668if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1))5669return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);5670}56715672if (!pZip->m_pAlloc)5673pZip->m_pAlloc = miniz_def_alloc_func;5674if (!pZip->m_pFree)5675pZip->m_pFree = miniz_def_free_func;5676if (!pZip->m_pRealloc)5677pZip->m_pRealloc = miniz_def_realloc_func;56785679pZip->m_archive_size = existing_size;5680pZip->m_central_directory_file_ofs = 0;5681pZip->m_total_files = 0;56825683if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))5684return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);56855686memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));56875688MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8));5689MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32));5690MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32));56915692pZip->m_pState->m_zip64 = zip64;5693pZip->m_pState->m_zip64_has_extended_info_fields = zip64;56945695pZip->m_zip_type = MZ_ZIP_TYPE_USER;5696pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;56975698return MZ_TRUE;5699}57005701mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size)5702{5703return mz_zip_writer_init_v2(pZip, existing_size, 0);5704}57055706mz_bool mz_zip_writer_init_heap_v2(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size, mz_uint flags)5707{5708pZip->m_pWrite = mz_zip_heap_write_func;5709pZip->m_pNeeds_keepalive = NULL;57105711if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)5712pZip->m_pRead = mz_zip_mem_read_func;57135714pZip->m_pIO_opaque = pZip;57155716if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags))5717return MZ_FALSE;57185719pZip->m_zip_type = MZ_ZIP_TYPE_HEAP;57205721if (0 != (initial_allocation_size = MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning)))5722{5723if (NULL == (pZip->m_pState->m_pMem = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, initial_allocation_size)))5724{5725mz_zip_writer_end_internal(pZip, MZ_FALSE);5726return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);5727}5728pZip->m_pState->m_mem_capacity = initial_allocation_size;5729}57305731return MZ_TRUE;5732}57335734mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size)5735{5736return mz_zip_writer_init_heap_v2(pZip, size_to_reserve_at_beginning, initial_allocation_size, 0);5737}57385739#ifndef MINIZ_NO_STDIO5740static size_t mz_zip_file_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)5741{5742mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;5743mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);57445745file_ofs += pZip->m_pState->m_file_archive_start_ofs;57465747if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET))))5748{5749mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);5750return 0;5751}57525753return MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile);5754}57555756mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning)5757{5758return mz_zip_writer_init_file_v2(pZip, pFilename, size_to_reserve_at_beginning, 0);5759}57605761mz_bool mz_zip_writer_init_file_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning, mz_uint flags)5762{5763MZ_FILE *pFile;57645765pZip->m_pWrite = mz_zip_file_write_func;5766pZip->m_pNeeds_keepalive = NULL;57675768if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)5769pZip->m_pRead = mz_zip_file_read_func;57705771pZip->m_pIO_opaque = pZip;57725773if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags))5774return MZ_FALSE;57755776if (NULL == (pFile = MZ_FOPEN(pFilename, (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) ? "w+b" : "wb")))5777{5778mz_zip_writer_end(pZip);5779return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);5780}57815782pZip->m_pState->m_pFile = pFile;5783pZip->m_zip_type = MZ_ZIP_TYPE_FILE;57845785if (size_to_reserve_at_beginning)5786{5787mz_uint64 cur_ofs = 0;5788char buf[4096];57895790MZ_CLEAR_OBJ(buf);57915792do5793{5794size_t n = (size_t)MZ_MIN(sizeof(buf), size_to_reserve_at_beginning);5795if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_ofs, buf, n) != n)5796{5797mz_zip_writer_end(pZip);5798return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);5799}5800cur_ofs += n;5801size_to_reserve_at_beginning -= n;5802} while (size_to_reserve_at_beginning);5803}58045805return MZ_TRUE;5806}58075808mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint flags)5809{5810pZip->m_pWrite = mz_zip_file_write_func;5811pZip->m_pNeeds_keepalive = NULL;58125813if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)5814pZip->m_pRead = mz_zip_file_read_func;58155816pZip->m_pIO_opaque = pZip;58175818if (!mz_zip_writer_init_v2(pZip, 0, flags))5819return MZ_FALSE;58205821pZip->m_pState->m_pFile = pFile;5822pZip->m_pState->m_file_archive_start_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);5823pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;58245825return MZ_TRUE;5826}5827#endif /* #ifndef MINIZ_NO_STDIO */58285829mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)5830{5831mz_zip_internal_state *pState;58325833if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))5834return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);58355836if (flags & MZ_ZIP_FLAG_WRITE_ZIP64)5837{5838/* We don't support converting a non-zip64 file to zip64 - this seems like more trouble than it's worth. (What about the existing 32-bit data descriptors that could follow the compressed data?) */5839if (!pZip->m_pState->m_zip64)5840return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);5841}58425843/* No sense in trying to write to an archive that's already at the support max size */5844if (pZip->m_pState->m_zip64)5845{5846if (pZip->m_total_files == MZ_UINT32_MAX)5847return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);5848}5849else5850{5851if (pZip->m_total_files == MZ_UINT16_MAX)5852return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);58535854if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)5855return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);5856}58575858pState = pZip->m_pState;58595860if (pState->m_pFile)5861{5862#ifdef MINIZ_NO_STDIO5863(void)pFilename;5864return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);5865#else5866if (pZip->m_pIO_opaque != pZip)5867return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);58685869if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE)5870{5871if (!pFilename)5872return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);58735874/* Archive is being read from stdio and was originally opened only for reading. Try to reopen as writable. */5875if (NULL == (pState->m_pFile = MZ_FREOPEN(pFilename, "r+b", pState->m_pFile)))5876{5877/* The mz_zip_archive is now in a bogus state because pState->m_pFile is NULL, so just close it. */5878mz_zip_reader_end_internal(pZip, MZ_FALSE);5879return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);5880}5881}58825883pZip->m_pWrite = mz_zip_file_write_func;5884pZip->m_pNeeds_keepalive = NULL;5885#endif /* #ifdef MINIZ_NO_STDIO */5886}5887else if (pState->m_pMem)5888{5889/* Archive lives in a memory block. Assume it's from the heap that we can resize using the realloc callback. */5890if (pZip->m_pIO_opaque != pZip)5891return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);58925893pState->m_mem_capacity = pState->m_mem_size;5894pZip->m_pWrite = mz_zip_heap_write_func;5895pZip->m_pNeeds_keepalive = NULL;5896}5897/* Archive is being read via a user provided read function - make sure the user has specified a write function too. */5898else if (!pZip->m_pWrite)5899return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);59005901/* Start writing new files at the archive's current central directory location. */5902/* TODO: We could add a flag that lets the user start writing immediately AFTER the existing central dir - this would be safer. */5903pZip->m_archive_size = pZip->m_central_directory_file_ofs;5904pZip->m_central_directory_file_ofs = 0;59055906/* Clear the sorted central dir offsets, they aren't useful or maintained now. */5907/* Even though we're now in write mode, files can still be extracted and verified, but file locates will be slow. */5908/* TODO: We could easily maintain the sorted central directory offsets. */5909mz_zip_array_clear(pZip, &pZip->m_pState->m_sorted_central_dir_offsets);59105911pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;59125913return MZ_TRUE;5914}59155916mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename)5917{5918return mz_zip_writer_init_from_reader_v2(pZip, pFilename, 0);5919}59205921/* TODO: pArchive_name is a terrible name here! */5922mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, mz_uint level_and_flags)5923{5924return mz_zip_writer_add_mem_ex(pZip, pArchive_name, pBuf, buf_size, NULL, 0, level_and_flags, 0, 0);5925}59265927typedef struct5928{5929mz_zip_archive *m_pZip;5930mz_uint64 m_cur_archive_file_ofs;5931mz_uint64 m_comp_size;5932} mz_zip_writer_add_state;59335934static mz_bool mz_zip_writer_add_put_buf_callback(const void *pBuf, int len, void *pUser)5935{5936mz_zip_writer_add_state *pState = (mz_zip_writer_add_state *)pUser;5937if ((int)pState->m_pZip->m_pWrite(pState->m_pZip->m_pIO_opaque, pState->m_cur_archive_file_ofs, pBuf, len) != len)5938return MZ_FALSE;59395940pState->m_cur_archive_file_ofs += len;5941pState->m_comp_size += len;5942return MZ_TRUE;5943}59445945#define MZ_ZIP64_MAX_LOCAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 2)5946#define MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 3)5947static mz_uint32 mz_zip_writer_create_zip64_extra_data(mz_uint8 *pBuf, mz_uint64 *pUncomp_size, mz_uint64 *pComp_size, mz_uint64 *pLocal_header_ofs)5948{5949mz_uint8 *pDst = pBuf;5950mz_uint32 field_size = 0;59515952MZ_WRITE_LE16(pDst + 0, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID);5953MZ_WRITE_LE16(pDst + 2, 0);5954pDst += sizeof(mz_uint16) * 2;59555956if (pUncomp_size)5957{5958MZ_WRITE_LE64(pDst, *pUncomp_size);5959pDst += sizeof(mz_uint64);5960field_size += sizeof(mz_uint64);5961}59625963if (pComp_size)5964{5965MZ_WRITE_LE64(pDst, *pComp_size);5966pDst += sizeof(mz_uint64);5967field_size += sizeof(mz_uint64);5968}59695970if (pLocal_header_ofs)5971{5972MZ_WRITE_LE64(pDst, *pLocal_header_ofs);5973pDst += sizeof(mz_uint64);5974field_size += sizeof(mz_uint64);5975}59765977MZ_WRITE_LE16(pBuf + 2, field_size);59785979return (mz_uint32)(pDst - pBuf);5980}59815982static 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)5983{5984(void)pZip;5985memset(pDst, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE);5986MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_SIG_OFS, MZ_ZIP_LOCAL_DIR_HEADER_SIG);5987MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_VERSION_NEEDED_OFS, method ? 20 : 0);5988MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_BIT_FLAG_OFS, bit_flags);5989MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_METHOD_OFS, method);5990MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_TIME_OFS, dos_time);5991MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_DATE_OFS, dos_date);5992MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_CRC32_OFS, uncomp_crc32);5993MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX));5994MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX));5995MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILENAME_LEN_OFS, filename_size);5996MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_EXTRA_LEN_OFS, extra_size);5997return MZ_TRUE;5998}59996000static mz_bool mz_zip_writer_create_central_dir_header(mz_zip_archive *pZip, mz_uint8 *pDst,6001mz_uint16 filename_size, mz_uint16 extra_size, mz_uint16 comment_size,6002mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32,6003mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date,6004mz_uint64 local_header_ofs, mz_uint32 ext_attributes)6005{6006(void)pZip;6007memset(pDst, 0, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE);6008MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_SIG_OFS, MZ_ZIP_CENTRAL_DIR_HEADER_SIG);6009MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_VERSION_NEEDED_OFS, method ? 20 : 0);6010MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_BIT_FLAG_OFS, bit_flags);6011MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_METHOD_OFS, method);6012MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_TIME_OFS, dos_time);6013MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_DATE_OFS, dos_date);6014MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_CRC32_OFS, uncomp_crc32);6015MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX));6016MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX));6017MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILENAME_LEN_OFS, filename_size);6018MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_EXTRA_LEN_OFS, extra_size);6019MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_COMMENT_LEN_OFS, comment_size);6020MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS, ext_attributes);6021MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_MIN(local_header_ofs, MZ_UINT32_MAX));6022return MZ_TRUE;6023}60246025static mz_bool mz_zip_writer_add_to_central_dir(mz_zip_archive *pZip, const char *pFilename, mz_uint16 filename_size,6026const void *pExtra, mz_uint16 extra_size, const void *pComment, mz_uint16 comment_size,6027mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32,6028mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date,6029mz_uint64 local_header_ofs, mz_uint32 ext_attributes,6030const char *user_extra_data, mz_uint user_extra_data_len)6031{6032mz_zip_internal_state *pState = pZip->m_pState;6033mz_uint32 central_dir_ofs = (mz_uint32)pState->m_central_dir.m_size;6034size_t orig_central_dir_size = pState->m_central_dir.m_size;6035mz_uint8 central_dir_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE];60366037if (!pZip->m_pState->m_zip64)6038{6039if (local_header_ofs > 0xFFFFFFFF)6040return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);6041}60426043/* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */6044if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + extra_size + user_extra_data_len + comment_size) >= MZ_UINT32_MAX)6045return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);60466047if (!mz_zip_writer_create_central_dir_header(pZip, central_dir_header, filename_size, (mz_uint16)(extra_size + user_extra_data_len), comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_header_ofs, ext_attributes))6048return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);60496050if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) ||6051(!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename, filename_size)) ||6052(!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra, extra_size)) ||6053(!mz_zip_array_push_back(pZip, &pState->m_central_dir, user_extra_data, user_extra_data_len)) ||6054(!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment, comment_size)) ||6055(!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, ¢ral_dir_ofs, 1)))6056{6057/* Try to resize the central directory array back into its original state. */6058mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);6059return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);6060}60616062return MZ_TRUE;6063}60646065static mz_bool mz_zip_writer_validate_archive_name(const char *pArchive_name)6066{6067/* Basic ZIP archive filename validity checks: Valid filenames cannot start with a forward slash, cannot contain a drive letter, and cannot use DOS-style backward slashes. */6068if (*pArchive_name == '/')6069return MZ_FALSE;60706071/* Making sure the name does not contain drive letters or DOS style backward slashes is the responsibility of the program using miniz*/60726073return MZ_TRUE;6074}60756076static mz_uint mz_zip_writer_compute_padding_needed_for_file_alignment(mz_zip_archive *pZip)6077{6078mz_uint32 n;6079if (!pZip->m_file_offset_alignment)6080return 0;6081n = (mz_uint32)(pZip->m_archive_size & (pZip->m_file_offset_alignment - 1));6082return (mz_uint)((pZip->m_file_offset_alignment - n) & (pZip->m_file_offset_alignment - 1));6083}60846085static mz_bool mz_zip_writer_write_zeros(mz_zip_archive *pZip, mz_uint64 cur_file_ofs, mz_uint32 n)6086{6087char buf[4096];6088memset(buf, 0, MZ_MIN(sizeof(buf), n));6089while (n)6090{6091mz_uint32 s = MZ_MIN(sizeof(buf), n);6092if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_file_ofs, buf, s) != s)6093return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);60946095cur_file_ofs += s;6096n -= s;6097}6098return MZ_TRUE;6099}61006101mz_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,6102mz_uint64 uncomp_size, mz_uint32 uncomp_crc32)6103{6104return mz_zip_writer_add_mem_ex_v2(pZip, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, uncomp_size, uncomp_crc32, NULL, NULL, 0, NULL, 0);6105}61066107mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size,6108mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32, MZ_TIME_T *last_modified,6109const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len)6110{6111mz_uint16 method = 0, dos_time = 0, dos_date = 0;6112mz_uint level, ext_attributes = 0, num_alignment_padding_bytes;6113mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, comp_size = 0;6114size_t archive_name_size;6115mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];6116tdefl_compressor *pComp = NULL;6117mz_bool store_data_uncompressed;6118mz_zip_internal_state *pState;6119mz_uint8 *pExtra_data = NULL;6120mz_uint32 extra_size = 0;6121mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];6122mz_uint16 bit_flags = 0;61236124if ((int)level_and_flags < 0)6125level_and_flags = MZ_DEFAULT_LEVEL;61266127if (uncomp_size || (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))6128bit_flags |= MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR;61296130if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))6131bit_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8;61326133level = level_and_flags & 0xF;6134store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA));61356136if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION))6137return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);61386139pState = pZip->m_pState;61406141if (pState->m_zip64)6142{6143if (pZip->m_total_files == MZ_UINT32_MAX)6144return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);6145}6146else6147{6148if (pZip->m_total_files == MZ_UINT16_MAX)6149{6150pState->m_zip64 = MZ_TRUE;6151/*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */6152}6153if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF))6154{6155pState->m_zip64 = MZ_TRUE;6156/*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */6157}6158}61596160if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size))6161return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);61626163if (!mz_zip_writer_validate_archive_name(pArchive_name))6164return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);61656166#ifndef MINIZ_NO_TIME6167if (last_modified != NULL)6168{6169mz_zip_time_t_to_dos_time(*last_modified, &dos_time, &dos_date);6170}6171else6172{6173MZ_TIME_T cur_time;6174time(&cur_time);6175mz_zip_time_t_to_dos_time(cur_time, &dos_time, &dos_date);6176}6177#endif /* #ifndef MINIZ_NO_TIME */61786179if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))6180{6181uncomp_crc32 = (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, buf_size);6182uncomp_size = buf_size;6183if (uncomp_size <= 3)6184{6185level = 0;6186store_data_uncompressed = MZ_TRUE;6187}6188}61896190archive_name_size = strlen(pArchive_name);6191if (archive_name_size > MZ_UINT16_MAX)6192return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);61936194num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);61956196/* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */6197if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX)6198return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);61996200if (!pState->m_zip64)6201{6202/* Bail early if the archive would obviously become too large */6203if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size6204+ MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + user_extra_data_len +6205pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + user_extra_data_central_len6206+ MZ_ZIP_DATA_DESCRIPTER_SIZE32) > 0xFFFFFFFF)6207{6208pState->m_zip64 = MZ_TRUE;6209/*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */6210}6211}62126213if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/'))6214{6215/* Set DOS Subdirectory attribute bit. */6216ext_attributes |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;62176218/* Subdirectories cannot contain data. */6219if ((buf_size) || (uncomp_size))6220return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);6221}62226223/* Try to do any allocations before writing to the archive, so if an allocation fails the file remains unmodified. (A good idea if we're doing an in-place modification.) */6224if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) || (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1)))6225return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);62266227if ((!store_data_uncompressed) && (buf_size))6228{6229if (NULL == (pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor))))6230return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);6231}62326233if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes))6234{6235pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);6236return MZ_FALSE;6237}62386239local_dir_header_ofs += num_alignment_padding_bytes;6240if (pZip->m_file_offset_alignment)6241{6242MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0);6243}6244cur_archive_file_ofs += num_alignment_padding_bytes;62456246MZ_CLEAR_OBJ(local_dir_header);62476248if (!store_data_uncompressed || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))6249{6250method = MZ_DEFLATED;6251}62526253if (pState->m_zip64)6254{6255if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX)6256{6257pExtra_data = extra_data;6258extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,6259(uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);6260}62616262if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, bit_flags, dos_time, dos_date))6263return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);62646265if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))6266return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);62676268cur_archive_file_ofs += sizeof(local_dir_header);62696270if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)6271{6272pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);6273return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);6274}6275cur_archive_file_ofs += archive_name_size;62766277if (pExtra_data != NULL)6278{6279if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size)6280return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);62816282cur_archive_file_ofs += extra_size;6283}6284}6285else6286{6287if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))6288return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);6289if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)user_extra_data_len, 0, 0, 0, method, bit_flags, dos_time, dos_date))6290return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);62916292if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))6293return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);62946295cur_archive_file_ofs += sizeof(local_dir_header);62966297if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)6298{6299pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);6300return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);6301}6302cur_archive_file_ofs += archive_name_size;6303}63046305if (user_extra_data_len > 0)6306{6307if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len)6308return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);63096310cur_archive_file_ofs += user_extra_data_len;6311}63126313if (store_data_uncompressed)6314{6315if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf, buf_size) != buf_size)6316{6317pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);6318return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);6319}63206321cur_archive_file_ofs += buf_size;6322comp_size = buf_size;6323}6324else if (buf_size)6325{6326mz_zip_writer_add_state state;63276328state.m_pZip = pZip;6329state.m_cur_archive_file_ofs = cur_archive_file_ofs;6330state.m_comp_size = 0;63316332if ((tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) ||6333(tdefl_compress_buffer(pComp, pBuf, buf_size, TDEFL_FINISH) != TDEFL_STATUS_DONE))6334{6335pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);6336return mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED);6337}63386339comp_size = state.m_comp_size;6340cur_archive_file_ofs = state.m_cur_archive_file_ofs;6341}63426343pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);6344pComp = NULL;63456346if (uncomp_size)6347{6348mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64];6349mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32;63506351MZ_ASSERT(bit_flags & MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR);63526353MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID);6354MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32);6355if (pExtra_data == NULL)6356{6357if (comp_size > MZ_UINT32_MAX)6358return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);63596360MZ_WRITE_LE32(local_dir_footer + 8, comp_size);6361MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size);6362}6363else6364{6365MZ_WRITE_LE64(local_dir_footer + 8, comp_size);6366MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size);6367local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64;6368}63696370if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size)6371return MZ_FALSE;63726373cur_archive_file_ofs += local_dir_footer_size;6374}63756376if (pExtra_data != NULL)6377{6378extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,6379(uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);6380}63816382if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, (mz_uint16)extra_size, pComment,6383comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_dir_header_ofs, ext_attributes,6384user_extra_data_central, user_extra_data_central_len))6385return MZ_FALSE;63866387pZip->m_total_files++;6388pZip->m_archive_size = cur_archive_file_ofs;63896390return MZ_TRUE;6391}63926393mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pArchive_name, mz_file_read_func read_callback, void* callback_opaque, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags,6394const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len)6395{6396mz_uint16 gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR;6397mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes;6398mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0;6399mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0;6400size_t archive_name_size;6401mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];6402mz_uint8 *pExtra_data = NULL;6403mz_uint32 extra_size = 0;6404mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];6405mz_zip_internal_state *pState;6406mz_uint64 file_ofs = 0, cur_archive_header_file_ofs;64076408if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))6409gen_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8;64106411if ((int)level_and_flags < 0)6412level_and_flags = MZ_DEFAULT_LEVEL;6413level = level_and_flags & 0xF;64146415/* Sanity checks */6416if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION))6417return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);64186419pState = pZip->m_pState;64206421if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX))6422{6423/* Source file is too large for non-zip64 */6424/*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */6425pState->m_zip64 = MZ_TRUE;6426}64276428/* We could support this, but why? */6429if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)6430return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);64316432if (!mz_zip_writer_validate_archive_name(pArchive_name))6433return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);64346435if (pState->m_zip64)6436{6437if (pZip->m_total_files == MZ_UINT32_MAX)6438return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);6439}6440else6441{6442if (pZip->m_total_files == MZ_UINT16_MAX)6443{6444pState->m_zip64 = MZ_TRUE;6445/*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */6446}6447}64486449archive_name_size = strlen(pArchive_name);6450if (archive_name_size > MZ_UINT16_MAX)6451return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);64526453num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);64546455/* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */6456if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX)6457return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);64586459if (!pState->m_zip64)6460{6461/* Bail early if the archive would obviously become too large */6462if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE6463+ archive_name_size + comment_size + user_extra_data_len + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 10246464+ MZ_ZIP_DATA_DESCRIPTER_SIZE32 + user_extra_data_central_len) > 0xFFFFFFFF)6465{6466pState->m_zip64 = MZ_TRUE;6467/*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */6468}6469}64706471#ifndef MINIZ_NO_TIME6472if (pFile_time)6473{6474mz_zip_time_t_to_dos_time(*pFile_time, &dos_time, &dos_date);6475}6476#endif64776478if (max_size <= 3)6479level = 0;64806481if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes))6482{6483return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);6484}64856486cur_archive_file_ofs += num_alignment_padding_bytes;6487local_dir_header_ofs = cur_archive_file_ofs;64886489if (pZip->m_file_offset_alignment)6490{6491MZ_ASSERT((cur_archive_file_ofs & (pZip->m_file_offset_alignment - 1)) == 0);6492}64936494if (max_size && level)6495{6496method = MZ_DEFLATED;6497}64986499MZ_CLEAR_OBJ(local_dir_header);6500if (pState->m_zip64)6501{6502if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX)6503{6504pExtra_data = extra_data;6505if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)6506extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,6507(max_size >= MZ_UINT32_MAX) ? &comp_size : NULL,6508(local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);6509else6510extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, NULL,6511NULL,6512(local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);6513}65146515if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, gen_flags, dos_time, dos_date))6516return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);65176518if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))6519return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);65206521cur_archive_file_ofs += sizeof(local_dir_header);65226523if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)6524{6525return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);6526}65276528cur_archive_file_ofs += archive_name_size;65296530if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size)6531return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);65326533cur_archive_file_ofs += extra_size;6534}6535else6536{6537if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))6538return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);6539if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)user_extra_data_len, 0, 0, 0, method, gen_flags, dos_time, dos_date))6540return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);65416542if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))6543return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);65446545cur_archive_file_ofs += sizeof(local_dir_header);65466547if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)6548{6549return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);6550}65516552cur_archive_file_ofs += archive_name_size;6553}65546555if (user_extra_data_len > 0)6556{6557if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len)6558return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);65596560cur_archive_file_ofs += user_extra_data_len;6561}65626563if (max_size)6564{6565void *pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE);6566if (!pRead_buf)6567{6568return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);6569}65706571if (!level)6572{6573while (1)6574{6575size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE);6576if (n == 0)6577break;65786579if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size))6580{6581pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);6582return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);6583}6584if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, n) != n)6585{6586pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);6587return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);6588}6589file_ofs += n;6590uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n);6591cur_archive_file_ofs += n;6592}6593uncomp_size = file_ofs;6594comp_size = uncomp_size;6595}6596else6597{6598mz_bool result = MZ_FALSE;6599mz_zip_writer_add_state state;6600tdefl_compressor *pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor));6601if (!pComp)6602{6603pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);6604return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);6605}66066607state.m_pZip = pZip;6608state.m_cur_archive_file_ofs = cur_archive_file_ofs;6609state.m_comp_size = 0;66106611if (tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY)6612{6613pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);6614pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);6615return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);6616}66176618for (;;)6619{6620tdefl_status status;6621tdefl_flush flush = TDEFL_NO_FLUSH;66226623size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE);6624if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size))6625{6626mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);6627break;6628}66296630file_ofs += n;6631uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n);66326633if (pZip->m_pNeeds_keepalive != NULL && pZip->m_pNeeds_keepalive(pZip->m_pIO_opaque))6634flush = TDEFL_FULL_FLUSH;66356636if (n == 0)6637flush = TDEFL_FINISH;66386639status = tdefl_compress_buffer(pComp, pRead_buf, n, flush);6640if (status == TDEFL_STATUS_DONE)6641{6642result = MZ_TRUE;6643break;6644}6645else if (status != TDEFL_STATUS_OKAY)6646{6647mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED);6648break;6649}6650}66516652pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);66536654if (!result)6655{6656pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);6657return MZ_FALSE;6658}66596660uncomp_size = file_ofs;6661comp_size = state.m_comp_size;6662cur_archive_file_ofs = state.m_cur_archive_file_ofs;6663}66646665pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);6666}66676668if (!(level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE))6669{6670mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64];6671mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32;66726673MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID);6674MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32);6675if (pExtra_data == NULL)6676{6677if (comp_size > MZ_UINT32_MAX)6678return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);66796680MZ_WRITE_LE32(local_dir_footer + 8, comp_size);6681MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size);6682}6683else6684{6685MZ_WRITE_LE64(local_dir_footer + 8, comp_size);6686MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size);6687local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64;6688}66896690if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size)6691return MZ_FALSE;66926693cur_archive_file_ofs += local_dir_footer_size;6694}66956696if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)6697{6698if (pExtra_data != NULL)6699{6700extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,6701(max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);6702}67036704if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header,6705(mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len),6706(max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : uncomp_size,6707(max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : comp_size,6708uncomp_crc32, method, gen_flags, dos_time, dos_date))6709return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);67106711cur_archive_header_file_ofs = local_dir_header_ofs;67126713if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))6714return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);67156716if (pExtra_data != NULL)6717{6718cur_archive_header_file_ofs += sizeof(local_dir_header);67196720if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, pArchive_name, archive_name_size) != archive_name_size)6721{6722return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);6723}67246725cur_archive_header_file_ofs += archive_name_size;67266727if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, extra_data, extra_size) != extra_size)6728return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);67296730cur_archive_header_file_ofs += extra_size;6731}6732}67336734if (pExtra_data != NULL)6735{6736extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,6737(uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);6738}67396740if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, (mz_uint16)extra_size, pComment, comment_size,6741uncomp_size, comp_size, uncomp_crc32, method, gen_flags, dos_time, dos_date, local_dir_header_ofs, ext_attributes,6742user_extra_data_central, user_extra_data_central_len))6743return MZ_FALSE;67446745pZip->m_total_files++;6746pZip->m_archive_size = cur_archive_file_ofs;67476748return MZ_TRUE;6749}67506751#ifndef MINIZ_NO_STDIO67526753static size_t mz_file_read_func_stdio(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)6754{6755MZ_FILE *pSrc_file = (MZ_FILE *)pOpaque;6756mz_int64 cur_ofs = MZ_FTELL64(pSrc_file);67576758if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pSrc_file, (mz_int64)file_ofs, SEEK_SET))))6759return 0;67606761return MZ_FREAD(pBuf, 1, n, pSrc_file);6762}67636764mz_bool mz_zip_writer_add_cfile(mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags,6765const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len)6766{6767return mz_zip_writer_add_read_buf_callback(pZip, pArchive_name, mz_file_read_func_stdio, pSrc_file, max_size, pFile_time, pComment, comment_size, level_and_flags,6768user_extra_data, user_extra_data_len, user_extra_data_central, user_extra_data_central_len);6769}67706771mz_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)6772{6773MZ_FILE *pSrc_file = NULL;6774mz_uint64 uncomp_size = 0;6775MZ_TIME_T file_modified_time;6776MZ_TIME_T *pFile_time = NULL;6777mz_bool status;67786779memset(&file_modified_time, 0, sizeof(file_modified_time));67806781#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)6782pFile_time = &file_modified_time;6783if (!mz_zip_get_file_modified_time(pSrc_filename, &file_modified_time))6784return mz_zip_set_error(pZip, MZ_ZIP_FILE_STAT_FAILED);6785#endif67866787pSrc_file = MZ_FOPEN(pSrc_filename, "rb");6788if (!pSrc_file)6789return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);67906791MZ_FSEEK64(pSrc_file, 0, SEEK_END);6792uncomp_size = MZ_FTELL64(pSrc_file);6793MZ_FSEEK64(pSrc_file, 0, SEEK_SET);67946795status = mz_zip_writer_add_cfile(pZip, pArchive_name, pSrc_file, uncomp_size, pFile_time, pComment, comment_size, level_and_flags, NULL, 0, NULL, 0);67966797MZ_FCLOSE(pSrc_file);67986799return status;6800}6801#endif /* #ifndef MINIZ_NO_STDIO */68026803static mz_bool mz_zip_writer_update_zip64_extension_block(mz_zip_array *pNew_ext, mz_zip_archive *pZip, const mz_uint8 *pExt, uint32_t ext_len, mz_uint64 *pComp_size, mz_uint64 *pUncomp_size, mz_uint64 *pLocal_header_ofs, mz_uint32 *pDisk_start)6804{6805/* + 64 should be enough for any new zip64 data */6806if (!mz_zip_array_reserve(pZip, pNew_ext, ext_len + 64, MZ_FALSE))6807return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);68086809mz_zip_array_resize(pZip, pNew_ext, 0, MZ_FALSE);68106811if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start))6812{6813mz_uint8 new_ext_block[64];6814mz_uint8 *pDst = new_ext_block;6815mz_write_le16(pDst, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID);6816mz_write_le16(pDst + sizeof(mz_uint16), 0);6817pDst += sizeof(mz_uint16) * 2;68186819if (pUncomp_size)6820{6821mz_write_le64(pDst, *pUncomp_size);6822pDst += sizeof(mz_uint64);6823}68246825if (pComp_size)6826{6827mz_write_le64(pDst, *pComp_size);6828pDst += sizeof(mz_uint64);6829}68306831if (pLocal_header_ofs)6832{6833mz_write_le64(pDst, *pLocal_header_ofs);6834pDst += sizeof(mz_uint64);6835}68366837if (pDisk_start)6838{6839mz_write_le32(pDst, *pDisk_start);6840pDst += sizeof(mz_uint32);6841}68426843mz_write_le16(new_ext_block + sizeof(mz_uint16), (mz_uint16)((pDst - new_ext_block) - sizeof(mz_uint16) * 2));68446845if (!mz_zip_array_push_back(pZip, pNew_ext, new_ext_block, pDst - new_ext_block))6846return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);6847}68486849if ((pExt) && (ext_len))6850{6851mz_uint32 extra_size_remaining = ext_len;6852const mz_uint8 *pExtra_data = pExt;68536854do6855{6856mz_uint32 field_id, field_data_size, field_total_size;68576858if (extra_size_remaining < (sizeof(mz_uint16) * 2))6859return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);68606861field_id = MZ_READ_LE16(pExtra_data);6862field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));6863field_total_size = field_data_size + sizeof(mz_uint16) * 2;68646865if (field_total_size > extra_size_remaining)6866return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);68676868if (field_id != MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)6869{6870if (!mz_zip_array_push_back(pZip, pNew_ext, pExtra_data, field_total_size))6871return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);6872}68736874pExtra_data += field_total_size;6875extra_size_remaining -= field_total_size;6876} while (extra_size_remaining);6877}68786879return MZ_TRUE;6880}68816882/* TODO: This func is now pretty freakin complex due to zip64, split it up? */6883mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint src_file_index)6884{6885mz_uint n, bit_flags, num_alignment_padding_bytes, src_central_dir_following_data_size;6886mz_uint64 src_archive_bytes_remaining, local_dir_header_ofs;6887mz_uint64 cur_src_file_ofs, cur_dst_file_ofs;6888mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];6889mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;6890mz_uint8 new_central_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE];6891size_t orig_central_dir_size;6892mz_zip_internal_state *pState;6893void *pBuf;6894const mz_uint8 *pSrc_central_header;6895mz_zip_archive_file_stat src_file_stat;6896mz_uint32 src_filename_len, src_comment_len, src_ext_len;6897mz_uint32 local_header_filename_size, local_header_extra_len;6898mz_uint64 local_header_comp_size, local_header_uncomp_size;6899mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE;69006901/* Sanity checks */6902if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead))6903return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);69046905pState = pZip->m_pState;69066907/* Don't support copying files from zip64 archives to non-zip64, even though in some cases this is possible */6908if ((pSource_zip->m_pState->m_zip64) && (!pZip->m_pState->m_zip64))6909return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);69106911/* Get pointer to the source central dir header and crack it */6912if (NULL == (pSrc_central_header = mz_zip_get_cdh(pSource_zip, src_file_index)))6913return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);69146915if (MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_SIG_OFS) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG)6916return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);69176918src_filename_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_FILENAME_LEN_OFS);6919src_comment_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_COMMENT_LEN_OFS);6920src_ext_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS);6921src_central_dir_following_data_size = src_filename_len + src_ext_len + src_comment_len;69226923/* TODO: We don't support central dir's >= MZ_UINT32_MAX bytes right now (+32 fudge factor in case we need to add more extra data) */6924if ((pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + 32) >= MZ_UINT32_MAX)6925return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);69266927num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);69286929if (!pState->m_zip64)6930{6931if (pZip->m_total_files == MZ_UINT16_MAX)6932return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);6933}6934else6935{6936/* TODO: Our zip64 support still has some 32-bit limits that may not be worth fixing. */6937if (pZip->m_total_files == MZ_UINT32_MAX)6938return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);6939}69406941if (!mz_zip_file_stat_internal(pSource_zip, src_file_index, pSrc_central_header, &src_file_stat, NULL))6942return MZ_FALSE;69436944cur_src_file_ofs = src_file_stat.m_local_header_ofs;6945cur_dst_file_ofs = pZip->m_archive_size;69466947/* Read the source archive's local dir header */6948if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)6949return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);69506951if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)6952return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);69536954cur_src_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE;69556956/* Compute the total size we need to copy (filename+extra data+compressed data) */6957local_header_filename_size = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS);6958local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);6959local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS);6960local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS);6961src_archive_bytes_remaining = local_header_filename_size + local_header_extra_len + src_file_stat.m_comp_size;69626963/* Try to find a zip64 extended information field */6964if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX)))6965{6966mz_zip_array file_data_array;6967const mz_uint8 *pExtra_data;6968mz_uint32 extra_size_remaining = local_header_extra_len;69696970mz_zip_array_init(&file_data_array, 1);6971if (!mz_zip_array_resize(pZip, &file_data_array, local_header_extra_len, MZ_FALSE))6972{6973return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);6974}69756976if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, src_file_stat.m_local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_size, file_data_array.m_p, local_header_extra_len) != local_header_extra_len)6977{6978mz_zip_array_clear(pZip, &file_data_array);6979return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);6980}69816982pExtra_data = (const mz_uint8 *)file_data_array.m_p;69836984do6985{6986mz_uint32 field_id, field_data_size, field_total_size;69876988if (extra_size_remaining < (sizeof(mz_uint16) * 2))6989{6990mz_zip_array_clear(pZip, &file_data_array);6991return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);6992}69936994field_id = MZ_READ_LE16(pExtra_data);6995field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));6996field_total_size = field_data_size + sizeof(mz_uint16) * 2;69976998if (field_total_size > extra_size_remaining)6999{7000mz_zip_array_clear(pZip, &file_data_array);7001return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);7002}70037004if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)7005{7006const mz_uint8 *pSrc_field_data = pExtra_data + sizeof(mz_uint32);70077008if (field_data_size < sizeof(mz_uint64) * 2)7009{7010mz_zip_array_clear(pZip, &file_data_array);7011return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);7012}70137014local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data);7015local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64)); /* may be 0 if there's a descriptor */70167017found_zip64_ext_data_in_ldir = MZ_TRUE;7018break;7019}70207021pExtra_data += field_total_size;7022extra_size_remaining -= field_total_size;7023} while (extra_size_remaining);70247025mz_zip_array_clear(pZip, &file_data_array);7026}70277028if (!pState->m_zip64)7029{7030/* Try to detect if the new archive will most likely wind up too big and bail early (+(sizeof(mz_uint32) * 4) is for the optional descriptor which could be present, +64 is a fudge factor). */7031/* We also check when the archive is finalized so this doesn't need to be perfect. */7032mz_uint64 approx_new_archive_size = cur_dst_file_ofs + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + src_archive_bytes_remaining + (sizeof(mz_uint32) * 4) +7033pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 64;70347035if (approx_new_archive_size >= MZ_UINT32_MAX)7036return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);7037}70387039/* Write dest archive padding */7040if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs, num_alignment_padding_bytes))7041return MZ_FALSE;70427043cur_dst_file_ofs += num_alignment_padding_bytes;70447045local_dir_header_ofs = cur_dst_file_ofs;7046if (pZip->m_file_offset_alignment)7047{7048MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0);7049}70507051/* The original zip's local header+ext block doesn't change, even with zip64, so we can just copy it over to the dest zip */7052if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)7053return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);70547055cur_dst_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE;70567057/* Copy over the source archive bytes to the dest archive, also ensure we have enough buf space to handle optional data descriptor */7058if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)MZ_MAX(32U, MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining)))))7059return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);70607061while (src_archive_bytes_remaining)7062{7063n = (mz_uint)MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining);7064if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, n) != n)7065{7066pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);7067return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);7068}7069cur_src_file_ofs += n;70707071if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n)7072{7073pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);7074return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);7075}7076cur_dst_file_ofs += n;70777078src_archive_bytes_remaining -= n;7079}70807081/* Now deal with the optional data descriptor */7082bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS);7083if (bit_flags & 8)7084{7085/* Copy data descriptor */7086if ((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir))7087{7088/* src is zip64, dest must be zip64 */70897090/* name uint32_t's */7091/* id 1 (optional in zip64?) */7092/* crc 1 */7093/* comp_size 2 */7094/* uncomp_size 2 */7095if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, (sizeof(mz_uint32) * 6)) != (sizeof(mz_uint32) * 6))7096{7097pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);7098return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);7099}71007101n = sizeof(mz_uint32) * ((MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID) ? 6 : 5);7102}7103else7104{7105/* src is NOT zip64 */7106mz_bool has_id;71077108if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, sizeof(mz_uint32) * 4) != sizeof(mz_uint32) * 4)7109{7110pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);7111return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);7112}71137114has_id = (MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID);71157116if (pZip->m_pState->m_zip64)7117{7118/* dest is zip64, so upgrade the data descriptor */7119const mz_uint32 *pSrc_descriptor = (const mz_uint32 *)((const mz_uint8 *)pBuf + (has_id ? sizeof(mz_uint32) : 0));7120const mz_uint32 src_crc32 = pSrc_descriptor[0];7121const mz_uint64 src_comp_size = pSrc_descriptor[1];7122const mz_uint64 src_uncomp_size = pSrc_descriptor[2];71237124mz_write_le32((mz_uint8 *)pBuf, MZ_ZIP_DATA_DESCRIPTOR_ID);7125mz_write_le32((mz_uint8 *)pBuf + sizeof(mz_uint32) * 1, src_crc32);7126mz_write_le64((mz_uint8 *)pBuf + sizeof(mz_uint32) * 2, src_comp_size);7127mz_write_le64((mz_uint8 *)pBuf + sizeof(mz_uint32) * 4, src_uncomp_size);71287129n = sizeof(mz_uint32) * 6;7130}7131else7132{7133/* dest is NOT zip64, just copy it as-is */7134n = sizeof(mz_uint32) * (has_id ? 4 : 3);7135}7136}71377138if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n)7139{7140pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);7141return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);7142}71437144cur_src_file_ofs += n;7145cur_dst_file_ofs += n;7146}7147pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);71487149/* Finally, add the new central dir header */7150orig_central_dir_size = pState->m_central_dir.m_size;71517152memcpy(new_central_header, pSrc_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE);71537154if (pState->m_zip64)7155{7156/* This is the painful part: We need to write a new central dir header + ext block with updated zip64 fields, and ensure the old fields (if any) are not included. */7157const mz_uint8 *pSrc_ext = pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len;7158mz_zip_array new_ext_block;71597160mz_zip_array_init(&new_ext_block, sizeof(mz_uint8));71617162MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_UINT32_MAX);7163MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_UINT32_MAX);7164MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_UINT32_MAX);71657166if (!mz_zip_writer_update_zip64_extension_block(&new_ext_block, pZip, pSrc_ext, src_ext_len, &src_file_stat.m_comp_size, &src_file_stat.m_uncomp_size, &local_dir_header_ofs, NULL))7167{7168mz_zip_array_clear(pZip, &new_ext_block);7169return MZ_FALSE;7170}71717172MZ_WRITE_LE16(new_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS, new_ext_block.m_size);71737174if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE))7175{7176mz_zip_array_clear(pZip, &new_ext_block);7177return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);7178}71797180if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_filename_len))7181{7182mz_zip_array_clear(pZip, &new_ext_block);7183mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);7184return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);7185}71867187if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_ext_block.m_p, new_ext_block.m_size))7188{7189mz_zip_array_clear(pZip, &new_ext_block);7190mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);7191return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);7192}71937194if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len + src_ext_len, src_comment_len))7195{7196mz_zip_array_clear(pZip, &new_ext_block);7197mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);7198return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);7199}72007201mz_zip_array_clear(pZip, &new_ext_block);7202}7203else7204{7205/* sanity checks */7206if (cur_dst_file_ofs > MZ_UINT32_MAX)7207return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);72087209if (local_dir_header_ofs >= MZ_UINT32_MAX)7210return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);72117212MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, local_dir_header_ofs);72137214if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE))7215return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);72167217if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_central_dir_following_data_size))7218{7219mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);7220return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);7221}7222}72237224/* This shouldn't trigger unless we screwed up during the initial sanity checks */7225if (pState->m_central_dir.m_size >= MZ_UINT32_MAX)7226{7227/* TODO: Support central dirs >= 32-bits in size */7228mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);7229return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);7230}72317232n = (mz_uint32)orig_central_dir_size;7233if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1))7234{7235mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);7236return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);7237}72387239pZip->m_total_files++;7240pZip->m_archive_size = cur_dst_file_ofs;72417242return MZ_TRUE;7243}72447245mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip)7246{7247mz_zip_internal_state *pState;7248mz_uint64 central_dir_ofs, central_dir_size;7249mz_uint8 hdr[256];72507251if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING))7252return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);72537254pState = pZip->m_pState;72557256if (pState->m_zip64)7257{7258if ((pZip->m_total_files > MZ_UINT32_MAX) || (pState->m_central_dir.m_size >= MZ_UINT32_MAX))7259return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);7260}7261else7262{7263if ((pZip->m_total_files > MZ_UINT16_MAX) || ((pZip->m_archive_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX))7264return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);7265}72667267central_dir_ofs = 0;7268central_dir_size = 0;7269if (pZip->m_total_files)7270{7271/* Write central directory */7272central_dir_ofs = pZip->m_archive_size;7273central_dir_size = pState->m_central_dir.m_size;7274pZip->m_central_directory_file_ofs = central_dir_ofs;7275if (pZip->m_pWrite(pZip->m_pIO_opaque, central_dir_ofs, pState->m_central_dir.m_p, (size_t)central_dir_size) != central_dir_size)7276return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);72777278pZip->m_archive_size += central_dir_size;7279}72807281if (pState->m_zip64)7282{7283/* Write zip64 end of central directory header */7284mz_uint64 rel_ofs_to_zip64_ecdr = pZip->m_archive_size;72857286MZ_CLEAR_OBJ(hdr);7287MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDH_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG);7288MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - sizeof(mz_uint32) - sizeof(mz_uint64));7289MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS, 0x031E); /* TODO: always Unix */7290MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_NEEDED_OFS, 0x002D);7291MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, pZip->m_total_files);7292MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS, pZip->m_total_files);7293MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_SIZE_OFS, central_dir_size);7294MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_OFS_OFS, central_dir_ofs);7295if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)7296return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);72977298pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE;72997300/* Write zip64 end of central directory locator */7301MZ_CLEAR_OBJ(hdr);7302MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG);7303MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS, rel_ofs_to_zip64_ecdr);7304MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS, 1);7305if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) != MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE)7306return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);73077308pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE;7309}73107311/* Write end of central directory record */7312MZ_CLEAR_OBJ(hdr);7313MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_SIG_OFS, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG);7314MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files));7315MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files));7316MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_SIZE_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_size));7317MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_OFS_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_ofs));73187319if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)7320return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);73217322#ifndef MINIZ_NO_STDIO7323if ((pState->m_pFile) && (MZ_FFLUSH(pState->m_pFile) == EOF))7324return mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);7325#endif /* #ifndef MINIZ_NO_STDIO */73267327pZip->m_archive_size += MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE;73287329pZip->m_zip_mode = MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED;7330return MZ_TRUE;7331}73327333mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **ppBuf, size_t *pSize)7334{7335if ((!ppBuf) || (!pSize))7336return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);73377338*ppBuf = NULL;7339*pSize = 0;73407341if ((!pZip) || (!pZip->m_pState))7342return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);73437344if (pZip->m_pWrite != mz_zip_heap_write_func)7345return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);73467347if (!mz_zip_writer_finalize_archive(pZip))7348return MZ_FALSE;73497350*ppBuf = pZip->m_pState->m_pMem;7351*pSize = pZip->m_pState->m_mem_size;7352pZip->m_pState->m_pMem = NULL;7353pZip->m_pState->m_mem_size = pZip->m_pState->m_mem_capacity = 0;73547355return MZ_TRUE;7356}73577358mz_bool mz_zip_writer_end(mz_zip_archive *pZip)7359{7360return mz_zip_writer_end_internal(pZip, MZ_TRUE);7361}73627363#ifndef MINIZ_NO_STDIO7364mz_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)7365{7366return mz_zip_add_mem_to_archive_file_in_place_v2(pZip_filename, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, NULL);7367}73687369mz_bool mz_zip_add_mem_to_archive_file_in_place_v2(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, mz_zip_error *pErr)7370{7371mz_bool status, created_new_archive = MZ_FALSE;7372mz_zip_archive zip_archive;7373struct MZ_FILE_STAT_STRUCT file_stat;7374mz_zip_error actual_err = MZ_ZIP_NO_ERROR;73757376mz_zip_zero_struct(&zip_archive);7377if ((int)level_and_flags < 0)7378level_and_flags = MZ_DEFAULT_LEVEL;73797380if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment)) || ((level_and_flags & 0xF) > MZ_UBER_COMPRESSION))7381{7382if (pErr)7383*pErr = MZ_ZIP_INVALID_PARAMETER;7384return MZ_FALSE;7385}73867387if (!mz_zip_writer_validate_archive_name(pArchive_name))7388{7389if (pErr)7390*pErr = MZ_ZIP_INVALID_FILENAME;7391return MZ_FALSE;7392}73937394/* Important: The regular non-64 bit version of stat() can fail here if the file is very large, which could cause the archive to be overwritten. */7395/* So be sure to compile with _LARGEFILE64_SOURCE 1 */7396if (MZ_FILE_STAT(pZip_filename, &file_stat) != 0)7397{7398/* Create a new archive. */7399if (!mz_zip_writer_init_file_v2(&zip_archive, pZip_filename, 0, level_and_flags))7400{7401if (pErr)7402*pErr = zip_archive.m_last_error;7403return MZ_FALSE;7404}74057406created_new_archive = MZ_TRUE;7407}7408else7409{7410/* Append to an existing archive. */7411if (!mz_zip_reader_init_file_v2(&zip_archive, pZip_filename, level_and_flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0))7412{7413if (pErr)7414*pErr = zip_archive.m_last_error;7415return MZ_FALSE;7416}74177418if (!mz_zip_writer_init_from_reader_v2(&zip_archive, pZip_filename, level_and_flags))7419{7420if (pErr)7421*pErr = zip_archive.m_last_error;74227423mz_zip_reader_end_internal(&zip_archive, MZ_FALSE);74247425return MZ_FALSE;7426}7427}74287429status = mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, 0, 0);7430actual_err = zip_archive.m_last_error;74317432/* Always finalize, even if adding failed for some reason, so we have a valid central directory. (This may not always succeed, but we can try.) */7433if (!mz_zip_writer_finalize_archive(&zip_archive))7434{7435if (!actual_err)7436actual_err = zip_archive.m_last_error;74377438status = MZ_FALSE;7439}74407441if (!mz_zip_writer_end_internal(&zip_archive, status))7442{7443if (!actual_err)7444actual_err = zip_archive.m_last_error;74457446status = MZ_FALSE;7447}74487449if ((!status) && (created_new_archive))7450{7451/* It's a new archive and something went wrong, so just delete it. */7452int ignoredStatus = MZ_DELETE_FILE(pZip_filename);7453(void)ignoredStatus;7454}74557456if (pErr)7457*pErr = actual_err;74587459return status;7460}74617462void *mz_zip_extract_archive_file_to_heap_v2(const char *pZip_filename, const char *pArchive_name, const char *pComment, size_t *pSize, mz_uint flags, mz_zip_error *pErr)7463{7464mz_uint32 file_index;7465mz_zip_archive zip_archive;7466void *p = NULL;74677468if (pSize)7469*pSize = 0;74707471if ((!pZip_filename) || (!pArchive_name))7472{7473if (pErr)7474*pErr = MZ_ZIP_INVALID_PARAMETER;74757476return NULL;7477}74787479mz_zip_zero_struct(&zip_archive);7480if (!mz_zip_reader_init_file_v2(&zip_archive, pZip_filename, flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0))7481{7482if (pErr)7483*pErr = zip_archive.m_last_error;74847485return NULL;7486}74877488if (mz_zip_reader_locate_file_v2(&zip_archive, pArchive_name, pComment, flags, &file_index))7489{7490p = mz_zip_reader_extract_to_heap(&zip_archive, file_index, pSize, flags);7491}74927493mz_zip_reader_end_internal(&zip_archive, p != NULL);74947495if (pErr)7496*pErr = zip_archive.m_last_error;74977498return p;7499}75007501void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint flags)7502{7503return mz_zip_extract_archive_file_to_heap_v2(pZip_filename, pArchive_name, NULL, pSize, flags, NULL);7504}75057506#endif /* #ifndef MINIZ_NO_STDIO */75077508#endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */75097510/* ------------------- Misc utils */75117512mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip)7513{7514return pZip ? pZip->m_zip_mode : MZ_ZIP_MODE_INVALID;7515}75167517mz_zip_type mz_zip_get_type(mz_zip_archive *pZip)7518{7519return pZip ? pZip->m_zip_type : MZ_ZIP_TYPE_INVALID;7520}75217522mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, mz_zip_error err_num)7523{7524mz_zip_error prev_err;75257526if (!pZip)7527return MZ_ZIP_INVALID_PARAMETER;75287529prev_err = pZip->m_last_error;75307531pZip->m_last_error = err_num;7532return prev_err;7533}75347535mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip)7536{7537if (!pZip)7538return MZ_ZIP_INVALID_PARAMETER;75397540return pZip->m_last_error;7541}75427543mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip)7544{7545return mz_zip_set_last_error(pZip, MZ_ZIP_NO_ERROR);7546}75477548mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip)7549{7550mz_zip_error prev_err;75517552if (!pZip)7553return MZ_ZIP_INVALID_PARAMETER;75547555prev_err = pZip->m_last_error;75567557pZip->m_last_error = MZ_ZIP_NO_ERROR;7558return prev_err;7559}75607561const char *mz_zip_get_error_string(mz_zip_error mz_err)7562{7563switch (mz_err)7564{7565case MZ_ZIP_NO_ERROR:7566return "no error";7567case MZ_ZIP_UNDEFINED_ERROR:7568return "undefined error";7569case MZ_ZIP_TOO_MANY_FILES:7570return "too many files";7571case MZ_ZIP_FILE_TOO_LARGE:7572return "file too large";7573case MZ_ZIP_UNSUPPORTED_METHOD:7574return "unsupported method";7575case MZ_ZIP_UNSUPPORTED_ENCRYPTION:7576return "unsupported encryption";7577case MZ_ZIP_UNSUPPORTED_FEATURE:7578return "unsupported feature";7579case MZ_ZIP_FAILED_FINDING_CENTRAL_DIR:7580return "failed finding central directory";7581case MZ_ZIP_NOT_AN_ARCHIVE:7582return "not a ZIP archive";7583case MZ_ZIP_INVALID_HEADER_OR_CORRUPTED:7584return "invalid header or archive is corrupted";7585case MZ_ZIP_UNSUPPORTED_MULTIDISK:7586return "unsupported multidisk archive";7587case MZ_ZIP_DECOMPRESSION_FAILED:7588return "decompression failed or archive is corrupted";7589case MZ_ZIP_COMPRESSION_FAILED:7590return "compression failed";7591case MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE:7592return "unexpected decompressed size";7593case MZ_ZIP_CRC_CHECK_FAILED:7594return "CRC-32 check failed";7595case MZ_ZIP_UNSUPPORTED_CDIR_SIZE:7596return "unsupported central directory size";7597case MZ_ZIP_ALLOC_FAILED:7598return "allocation failed";7599case MZ_ZIP_FILE_OPEN_FAILED:7600return "file open failed";7601case MZ_ZIP_FILE_CREATE_FAILED:7602return "file create failed";7603case MZ_ZIP_FILE_WRITE_FAILED:7604return "file write failed";7605case MZ_ZIP_FILE_READ_FAILED:7606return "file read failed";7607case MZ_ZIP_FILE_CLOSE_FAILED:7608return "file close failed";7609case MZ_ZIP_FILE_SEEK_FAILED:7610return "file seek failed";7611case MZ_ZIP_FILE_STAT_FAILED:7612return "file stat failed";7613case MZ_ZIP_INVALID_PARAMETER:7614return "invalid parameter";7615case MZ_ZIP_INVALID_FILENAME:7616return "invalid filename";7617case MZ_ZIP_BUF_TOO_SMALL:7618return "buffer too small";7619case MZ_ZIP_INTERNAL_ERROR:7620return "internal error";7621case MZ_ZIP_FILE_NOT_FOUND:7622return "file not found";7623case MZ_ZIP_ARCHIVE_TOO_LARGE:7624return "archive is too large";7625case MZ_ZIP_VALIDATION_FAILED:7626return "validation failed";7627case MZ_ZIP_WRITE_CALLBACK_FAILED:7628return "write calledback failed";7629default:7630break;7631}76327633return "unknown error";7634}76357636/* Note: Just because the archive is not zip64 doesn't necessarily mean it doesn't have Zip64 extended information extra field, argh. */7637mz_bool mz_zip_is_zip64(mz_zip_archive *pZip)7638{7639if ((!pZip) || (!pZip->m_pState))7640return MZ_FALSE;76417642return pZip->m_pState->m_zip64;7643}76447645size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip)7646{7647if ((!pZip) || (!pZip->m_pState))7648return 0;76497650return pZip->m_pState->m_central_dir.m_size;7651}76527653mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip)7654{7655return pZip ? pZip->m_total_files : 0;7656}76577658mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip)7659{7660if (!pZip)7661return 0;7662return pZip->m_archive_size;7663}76647665mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip)7666{7667if ((!pZip) || (!pZip->m_pState))7668return 0;7669return pZip->m_pState->m_file_archive_start_ofs;7670}76717672MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip)7673{7674if ((!pZip) || (!pZip->m_pState))7675return 0;7676return pZip->m_pState->m_pFile;7677}76787679size_t mz_zip_read_archive_data(mz_zip_archive *pZip, mz_uint64 file_ofs, void *pBuf, size_t n)7680{7681if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead))7682return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);76837684return pZip->m_pRead(pZip->m_pIO_opaque, file_ofs, pBuf, n);7685}76867687mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size)7688{7689mz_uint n;7690const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);7691if (!p)7692{7693if (filename_buf_size)7694pFilename[0] = '\0';7695mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);7696return 0;7697}7698n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);7699if (filename_buf_size)7700{7701n = MZ_MIN(n, filename_buf_size - 1);7702memcpy(pFilename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n);7703pFilename[n] = '\0';7704}7705return n + 1;7706}77077708mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat)7709{7710return mz_zip_file_stat_internal(pZip, file_index, mz_zip_get_cdh(pZip, file_index), pStat, NULL);7711}77127713mz_bool mz_zip_end(mz_zip_archive *pZip)7714{7715if (!pZip)7716return MZ_FALSE;77177718if (pZip->m_zip_mode == MZ_ZIP_MODE_READING)7719return mz_zip_reader_end(pZip);7720#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS7721else if ((pZip->m_zip_mode == MZ_ZIP_MODE_WRITING) || (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))7722return mz_zip_writer_end(pZip);7723#endif77247725return MZ_FALSE;7726}77277728#ifdef __cplusplus7729}7730#endif77317732#endif /*#ifndef MINIZ_NO_ARCHIVE_APIS*/773377347735