123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- /****************************************************************************
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2013-2017 Chukong Technologies Inc.
- http://www.cocos2d-x.org
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- // FIXME: hack, must be included before ziputils
- #ifdef MINIZIP_FROM_SYSTEM
- #include <minizip/unzip.h>
- #else // from our embedded sources
- #include "unzip.h"
- #endif
- #include "ZipUtils.h"
- #include <zlib.h>
- #include <assert.h>
- #include <stdlib.h>
- #include <map>
- // FIXME: Other platforms should use upstream minizip like mingw-w64
- #ifdef MINIZIP_FROM_SYSTEM
- #define unzGoToFirstFile64(A,B,C,D) unzGoToFirstFile2(A,B,C,D, NULL, 0, NULL, 0)
- #define unzGoToNextFile64(A,B,C,D) unzGoToNextFile2(A,B,C,D, NULL, 0, NULL, 0)
- #endif
- // --------------------- ZipUtils ---------------------
- // memory in iPhone is precious
- // Should buffer factor be 1.5 instead of 2 ?
- #define BUFFER_INC_FACTOR (2)
- int ZipUtils::inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t *outLength, ssize_t outLengthHint)
- {
- /* ret value */
- int err = Z_OK;
-
- ssize_t bufferSize = outLengthHint;
- *out = (unsigned char*)malloc(bufferSize);
-
- z_stream d_stream; /* decompression stream */
- d_stream.zalloc = (alloc_func)0;
- d_stream.zfree = (free_func)0;
- d_stream.opaque = (voidpf)0;
-
- d_stream.next_in = in;
- d_stream.avail_in = static_cast<unsigned int>(inLength);
- d_stream.next_out = *out;
- d_stream.avail_out = static_cast<unsigned int>(bufferSize);
-
- /* window size to hold 256k */
- if( (err = inflateInit2(&d_stream, 15 + 32)) != Z_OK )
- return err;
-
- for (;;)
- {
- err = inflate(&d_stream, Z_NO_FLUSH);
-
- if (err == Z_STREAM_END)
- {
- break;
- }
-
- switch (err)
- {
- case Z_NEED_DICT:
- err = Z_DATA_ERROR;
- case Z_DATA_ERROR:
- case Z_MEM_ERROR:
- inflateEnd(&d_stream);
- return err;
- }
-
- // not enough memory ?
- if (err != Z_STREAM_END)
- {
- *out = (unsigned char*)realloc(*out, bufferSize * BUFFER_INC_FACTOR);
-
- /* not enough memory, ouch */
- if (! *out )
- {
- // CCLOG("cocos2d: ZipUtils: realloc failed");
- inflateEnd(&d_stream);
- return Z_MEM_ERROR;
- }
-
- d_stream.next_out = *out + bufferSize;
- d_stream.avail_out = static_cast<unsigned int>(bufferSize);
- bufferSize *= BUFFER_INC_FACTOR;
- }
- }
-
- *outLength = bufferSize - d_stream.avail_out;
- err = inflateEnd(&d_stream);
- return err;
- }
- ssize_t ZipUtils::inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t outLengthHint)
- {
- ssize_t outLength = 0;
- int err = inflateMemoryWithHint(in, inLength, out, &outLength, outLengthHint);
-
- if (err != Z_OK || *out == nullptr) {
- if (err == Z_MEM_ERROR)
- {
- // CCLOG("cocos2d: ZipUtils: Out of memory while decompressing map data!");
- } else
- if (err == Z_VERSION_ERROR)
- {
- // CCLOG("cocos2d: ZipUtils: Incompatible zlib version!");
- } else
- if (err == Z_DATA_ERROR)
- {
- // CCLOG("cocos2d: ZipUtils: Incorrect zlib compressed data!");
- }
- else
- {
- // CCLOG("cocos2d: ZipUtils: Unknown error while decompressing map data!");
- }
- if(*out) {
- free(*out);
- *out = nullptr;
- }
- outLength = 0;
- }
-
- return outLength;
- }
- ssize_t ZipUtils::inflateMemory(unsigned char *in, ssize_t inLength, unsigned char **out)
- {
- // 256k for hint
- return inflateMemoryWithHint(in, inLength, out, 256 * 1024);
- }
|