ZipUtils.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. // FIXME: hack, must be included before ziputils
  22. #ifdef MINIZIP_FROM_SYSTEM
  23. #include <minizip/unzip.h>
  24. #else // from our embedded sources
  25. #include "unzip.h"
  26. #endif
  27. #include "ZipUtils.h"
  28. #include <zlib.h>
  29. #include <assert.h>
  30. #include <stdlib.h>
  31. #include <map>
  32. // FIXME: Other platforms should use upstream minizip like mingw-w64
  33. #ifdef MINIZIP_FROM_SYSTEM
  34. #define unzGoToFirstFile64(A,B,C,D) unzGoToFirstFile2(A,B,C,D, NULL, 0, NULL, 0)
  35. #define unzGoToNextFile64(A,B,C,D) unzGoToNextFile2(A,B,C,D, NULL, 0, NULL, 0)
  36. #endif
  37. // --------------------- ZipUtils ---------------------
  38. // memory in iPhone is precious
  39. // Should buffer factor be 1.5 instead of 2 ?
  40. #define BUFFER_INC_FACTOR (2)
  41. int ZipUtils::inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t *outLength, ssize_t outLengthHint)
  42. {
  43. /* ret value */
  44. int err = Z_OK;
  45. ssize_t bufferSize = outLengthHint;
  46. *out = (unsigned char*)malloc(bufferSize);
  47. z_stream d_stream; /* decompression stream */
  48. d_stream.zalloc = (alloc_func)0;
  49. d_stream.zfree = (free_func)0;
  50. d_stream.opaque = (voidpf)0;
  51. d_stream.next_in = in;
  52. d_stream.avail_in = static_cast<unsigned int>(inLength);
  53. d_stream.next_out = *out;
  54. d_stream.avail_out = static_cast<unsigned int>(bufferSize);
  55. /* window size to hold 256k */
  56. if( (err = inflateInit2(&d_stream, 15 + 32)) != Z_OK )
  57. return err;
  58. for (;;)
  59. {
  60. err = inflate(&d_stream, Z_NO_FLUSH);
  61. if (err == Z_STREAM_END)
  62. {
  63. break;
  64. }
  65. switch (err)
  66. {
  67. case Z_NEED_DICT:
  68. err = Z_DATA_ERROR;
  69. case Z_DATA_ERROR:
  70. case Z_MEM_ERROR:
  71. inflateEnd(&d_stream);
  72. return err;
  73. }
  74. // not enough memory ?
  75. if (err != Z_STREAM_END)
  76. {
  77. *out = (unsigned char*)realloc(*out, bufferSize * BUFFER_INC_FACTOR);
  78. /* not enough memory, ouch */
  79. if (! *out )
  80. {
  81. // CCLOG("cocos2d: ZipUtils: realloc failed");
  82. inflateEnd(&d_stream);
  83. return Z_MEM_ERROR;
  84. }
  85. d_stream.next_out = *out + bufferSize;
  86. d_stream.avail_out = static_cast<unsigned int>(bufferSize);
  87. bufferSize *= BUFFER_INC_FACTOR;
  88. }
  89. }
  90. *outLength = bufferSize - d_stream.avail_out;
  91. err = inflateEnd(&d_stream);
  92. return err;
  93. }
  94. ssize_t ZipUtils::inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t outLengthHint)
  95. {
  96. ssize_t outLength = 0;
  97. int err = inflateMemoryWithHint(in, inLength, out, &outLength, outLengthHint);
  98. if (err != Z_OK || *out == nullptr) {
  99. if (err == Z_MEM_ERROR)
  100. {
  101. // CCLOG("cocos2d: ZipUtils: Out of memory while decompressing map data!");
  102. } else
  103. if (err == Z_VERSION_ERROR)
  104. {
  105. // CCLOG("cocos2d: ZipUtils: Incompatible zlib version!");
  106. } else
  107. if (err == Z_DATA_ERROR)
  108. {
  109. // CCLOG("cocos2d: ZipUtils: Incorrect zlib compressed data!");
  110. }
  111. else
  112. {
  113. // CCLOG("cocos2d: ZipUtils: Unknown error while decompressing map data!");
  114. }
  115. if(*out) {
  116. free(*out);
  117. *out = nullptr;
  118. }
  119. outLength = 0;
  120. }
  121. return outLength;
  122. }
  123. ssize_t ZipUtils::inflateMemory(unsigned char *in, ssize_t inLength, unsigned char **out)
  124. {
  125. // 256k for hint
  126. return inflateMemoryWithHint(in, inLength, out, 256 * 1024);
  127. }