ZipUtils.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __SUPPORT_ZIPUTILS_H__
  23. #define __SUPPORT_ZIPUTILS_H__
  24. /// @cond DO_NOT_SHOW
  25. //#include <string>
  26. //#include "platform/CCPlatformConfig.h"
  27. #include "platform/CCPlatformMacros.h"
  28. //#include "platform/CCPlatformDefine.h"
  29. #include "platform/CCFileUtils.h"
  30. #include <string>
  31. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  32. #include "platform/android/CCFileUtils-android.h"
  33. #elif(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  34. // for import ssize_t on win32 platform
  35. #include "platform/CCStdC.h"
  36. #endif
  37. /**
  38. * @addtogroup base
  39. * @{
  40. */
  41. namespace cocos2d
  42. {
  43. #ifndef _unz64_H
  44. typedef struct unz_file_info_s unz_file_info;
  45. #endif
  46. /** XXX: pragma pack ???
  47. * @struct CCZHeader
  48. */
  49. struct CCZHeader {
  50. unsigned char sig[4]; /** Signature. Should be 'CCZ!' 4 bytes. */
  51. unsigned short compression_type; /** Should be 0. */
  52. unsigned short version; /** Should be 2 (although version type==1 is also supported). */
  53. unsigned int reserved; /** Reserved for users. */
  54. unsigned int len; /** Size of the uncompressed file. */
  55. };
  56. enum {
  57. CCZ_COMPRESSION_ZLIB, /** zlib format. */
  58. CCZ_COMPRESSION_BZIP2, /** bzip2 format (not supported yet). */
  59. CCZ_COMPRESSION_GZIP, /** gzip format (not supported yet). */
  60. CCZ_COMPRESSION_NONE, /** plain (not supported yet). */
  61. };
  62. class CC_DLL ZipUtils
  63. {
  64. public:
  65. /**
  66. * Inflates either zlib or gzip deflated memory. The inflated memory is expected to be freed by the caller.
  67. *
  68. * It will allocate 256k for the destination buffer. If it is not enough it will multiply the previous buffer size per 2, until there is enough memory.
  69. *
  70. * @return The length of the deflated buffer.
  71. * @since v0.8.1
  72. */
  73. CC_DEPRECATED_ATTRIBUTE static ssize_t ccInflateMemory(unsigned char *in, ssize_t inLength, unsigned char **out) { return inflateMemory(in, inLength, out); }
  74. static ssize_t inflateMemory(unsigned char *in, ssize_t inLength, unsigned char **out);
  75. /**
  76. * Inflates either zlib or gzip deflated memory. The inflated memory is expected to be freed by the caller.
  77. *
  78. * @param outLengthHint It is assumed to be the needed room to allocate the inflated buffer.
  79. *
  80. * @return The length of the deflated buffer.
  81. * @since v1.0.0
  82. */
  83. CC_DEPRECATED_ATTRIBUTE static ssize_t ccInflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t outLengthHint) { return inflateMemoryWithHint(in, inLength, out, outLengthHint); }
  84. static ssize_t inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t outLengthHint);
  85. /**
  86. * Inflates a GZip file into memory.
  87. *
  88. * @return The length of the deflated buffer.
  89. * @since v0.99.5
  90. */
  91. CC_DEPRECATED_ATTRIBUTE static int ccInflateGZipFile(const char *filename, unsigned char **out) { return inflateGZipFile(filename, out); }
  92. static int inflateGZipFile(const char *filename, unsigned char **out);
  93. /**
  94. * Test a file is a GZip format file or not.
  95. *
  96. * @return True is a GZip format file. false is not.
  97. * @since v3.0
  98. */
  99. CC_DEPRECATED_ATTRIBUTE static bool ccIsGZipFile(const char *filename) { return isGZipFile(filename); }
  100. static bool isGZipFile(const char *filename);
  101. /**
  102. * Test the buffer is GZip format or not.
  103. *
  104. * @return True is GZip format. false is not.
  105. * @since v3.0
  106. */
  107. CC_DEPRECATED_ATTRIBUTE static bool ccIsGZipBuffer(const unsigned char *buffer, ssize_t len) { return isGZipBuffer(buffer, len); }
  108. static bool isGZipBuffer(const unsigned char *buffer, ssize_t len);
  109. /**
  110. * Inflates a CCZ file into memory.
  111. *
  112. * @return The length of the deflated buffer.
  113. * @since v0.99.5
  114. */
  115. CC_DEPRECATED_ATTRIBUTE static int ccInflateCCZFile(const char *filename, unsigned char **out) { return inflateCCZFile(filename, out); }
  116. static int inflateCCZFile(const char *filename, unsigned char **out);
  117. /**
  118. * Inflates a buffer with CCZ format into memory.
  119. *
  120. * @return The length of the deflated buffer.
  121. * @since v3.0
  122. */
  123. CC_DEPRECATED_ATTRIBUTE static int ccInflateCCZBuffer(const unsigned char *buffer, ssize_t len, unsigned char **out) { return inflateCCZBuffer(buffer, len, out); }
  124. static int inflateCCZBuffer(const unsigned char *buffer, ssize_t len, unsigned char **out);
  125. /**
  126. * Test a file is a CCZ format file or not.
  127. *
  128. * @return True is a CCZ format file. false is not.
  129. * @since v3.0
  130. */
  131. CC_DEPRECATED_ATTRIBUTE static bool ccIsCCZFile(const char *filename) { return isCCZFile(filename); }
  132. static bool isCCZFile(const char *filename);
  133. /**
  134. * Test the buffer is CCZ format or not.
  135. *
  136. * @return True is CCZ format. false is not.
  137. * @since v3.0
  138. */
  139. CC_DEPRECATED_ATTRIBUTE static bool ccIsCCZBuffer(const unsigned char *buffer, ssize_t len) { return isCCZBuffer(buffer, len); }
  140. static bool isCCZBuffer(const unsigned char *buffer, ssize_t len);
  141. /**
  142. * Sets the pvr.ccz encryption key parts separately for added security.
  143. *
  144. * Example: If the key used to encrypt the pvr.ccz file is
  145. * 0xaaaaaaaabbbbbbbbccccccccdddddddd you will call this function 4
  146. * different times, preferably from 4 different source files, as follows
  147. *
  148. * ZipUtils::setPvrEncryptionKeyPart(0, 0xaaaaaaaa);
  149. * ZipUtils::setPvrEncryptionKeyPart(1, 0xbbbbbbbb);
  150. * ZipUtils::setPvrEncryptionKeyPart(2, 0xcccccccc);
  151. * ZipUtils::setPvrEncryptionKeyPart(3, 0xdddddddd);
  152. *
  153. * Splitting the key into 4 parts and calling the function from 4 different source
  154. * files increases the difficulty to reverse engineer the encryption key.
  155. * Be aware that encryption is *never* 100% secure and the key code
  156. * can be cracked by knowledgable persons.
  157. *
  158. * IMPORTANT: Be sure to call setPvrEncryptionKey or
  159. * setPvrEncryptionKeyPart with all of the key parts *before* loading
  160. * the sprite sheet or decryption will fail and the sprite sheet
  161. * will fail to load.
  162. *
  163. * @param index Part of the key [0..3].
  164. * @param value Value of the key part.
  165. */
  166. CC_DEPRECATED_ATTRIBUTE static void ccSetPvrEncryptionKeyPart(int index, unsigned int value) { setPvrEncryptionKeyPart(index, value); }
  167. static void setPvrEncryptionKeyPart(int index, unsigned int value);
  168. /**
  169. * Sets the pvr.ccz encryption key.
  170. *
  171. * Example: If the key used to encrypt the pvr.ccz file is
  172. * 0xaaaaaaaabbbbbbbbccccccccdddddddd you will call this function with
  173. * the key split into 4 parts as follows
  174. *
  175. * ZipUtils::setPvrEncryptionKey(0xaaaaaaaa, 0xbbbbbbbb, 0xcccccccc, 0xdddddddd);
  176. *
  177. * Note that using this function makes it easier to reverse engineer and discover
  178. * the complete key because the key parts are present in one function call.
  179. *
  180. * IMPORTANT: Be sure to call setPvrEncryptionKey or setPvrEncryptionKeyPart
  181. * with all of the key parts *before* loading the spritesheet or decryption
  182. * will fail and the sprite sheet will fail to load.
  183. *
  184. * @param keyPart1 The key value part 1.
  185. * @param keyPart2 The key value part 2.
  186. * @param keyPart3 The key value part 3.
  187. * @param keyPart4 The key value part 4.
  188. */
  189. CC_DEPRECATED_ATTRIBUTE static void ccSetPvrEncryptionKey(unsigned int keyPart1, unsigned int keyPart2, unsigned int keyPart3, unsigned int keyPart4) { setPvrEncryptionKey(keyPart1, keyPart2, keyPart3, keyPart4); }
  190. static void setPvrEncryptionKey(unsigned int keyPart1, unsigned int keyPart2, unsigned int keyPart3, unsigned int keyPart4);
  191. static bool isEpngBuffer(const unsigned char *buffer, ssize_t len);
  192. static int inflateEpngBuffer(unsigned char *buffer, ssize_t len, unsigned char **out);
  193. private:
  194. static int inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t *outLength, ssize_t outLengthHint);
  195. static inline void decodeEncodedPvr (unsigned int *data, ssize_t len);
  196. static inline unsigned int checksumPvr(const unsigned int *data, ssize_t len);
  197. static unsigned int s_uEncryptedPvrKeyParts[4];
  198. static unsigned int s_uEncryptionKey[1024];
  199. static bool s_bEncryptionKeyIsValid;
  200. };
  201. // forward declaration
  202. class ZipFilePrivate;
  203. struct unz_file_info_s;
  204. /**
  205. * Zip file - reader helper class.
  206. *
  207. * It will cache the file list of a particular zip file with positions inside an archive,
  208. * so it would be much faster to read some particular files or to check their existence.
  209. *
  210. * @since v2.0.5
  211. */
  212. class CC_DLL ZipFile
  213. {
  214. public:
  215. /**
  216. * Constructor, open zip file and store file list.
  217. *
  218. * @param zipFile Zip file name
  219. * @param filter The first part of file names, which should be accessible.
  220. * For example, "assets/". Other files will be missed.
  221. *
  222. * @since v2.0.5
  223. */
  224. ZipFile(const std::string &zipFile, const std::string &filter = std::string());
  225. virtual ~ZipFile();
  226. /**
  227. * Regenerate accessible file list based on a new filter string.
  228. *
  229. * @param filter New filter string (first part of files names)
  230. * @return true whenever zip file is open successfully and it is possible to locate
  231. * at least the first file, false otherwise
  232. *
  233. * @since v2.0.5
  234. */
  235. bool setFilter(const std::string &filter);
  236. /**
  237. * Check does a file exists or not in zip file
  238. *
  239. * @param fileName File to be checked on existence
  240. * @return true whenever file exists, false otherwise
  241. *
  242. * @since v2.0.5
  243. */
  244. bool fileExists(const std::string &fileName) const;
  245. /**
  246. * Get resource file data from a zip file.
  247. * @param fileName File name
  248. * @param[out] size If the file read operation succeeds, it will be the data size, otherwise 0.
  249. * @return Upon success, a pointer to the data is returned, otherwise nullptr.
  250. * @warning Recall: you are responsible for calling free() on any Non-nullptr pointer returned.
  251. *
  252. * @since v2.0.5
  253. */
  254. unsigned char *getFileData(const std::string &fileName, ssize_t *size);
  255. /**
  256. * Get resource file data from a zip file.
  257. * @param fileName File name
  258. * @param[out] buffer If the file read operation succeeds, if will contain the file data.
  259. * @return True if successful.
  260. */
  261. bool getFileData(const std::string &fileName, ResizableBuffer* buffer);
  262. std::string getFirstFilename();
  263. std::string getNextFilename();
  264. static ZipFile *createWithBuffer(const void* buffer, unsigned long size);
  265. private:
  266. /* Only used internal for createWithBuffer() */
  267. ZipFile();
  268. bool initWithBuffer(const void *buffer, unsigned long size);
  269. int getCurrentFileInfo(std::string *filename, unz_file_info *info);
  270. /** Internal data like zip file pointer / file list array and so on */
  271. ZipFilePrivate *_data;
  272. };
  273. } // end of namespace cocos2d
  274. // end group
  275. /// @}
  276. /// @endcond
  277. #endif // __SUPPORT_ZIPUTILS_H__