2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef __SUPPORT_ZIPUTILS_H__
|
|
|
|
#define __SUPPORT_ZIPUTILS_H__
|
|
|
|
|
2012-11-08 19:46:53 +08:00
|
|
|
#include <string>
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCPlatformConfig.h"
|
2013-05-08 11:45:27 +08:00
|
|
|
#include "CCPlatformDefine.h"
|
2013-11-14 13:35:17 +08:00
|
|
|
#include "CCPlatformMacros.h"
|
2012-11-08 19:46:53 +08:00
|
|
|
|
2013-06-18 17:10:54 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
#include "platform/android/CCFileUtilsAndroid.h"
|
2013-12-10 21:20:52 +08:00
|
|
|
#elif(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
2013-12-10 23:13:21 +08:00
|
|
|
// for import ssize_t on win32 platform
|
2013-12-10 21:20:52 +08:00
|
|
|
#include "CCStdC.h"
|
2013-06-18 17:10:54 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
namespace cocos2d
|
|
|
|
{
|
|
|
|
/* XXX: pragma pack ??? */
|
|
|
|
/** @struct CCZHeader
|
|
|
|
*/
|
|
|
|
struct CCZHeader {
|
2013-05-06 22:04:45 +08:00
|
|
|
unsigned char sig[4]; // signature. Should be 'CCZ!' 4 bytes
|
|
|
|
unsigned short compression_type; // should 0
|
|
|
|
unsigned short version; // should be 2 (although version type==1 is also supported)
|
|
|
|
unsigned int reserved; // Reserved for users.
|
|
|
|
unsigned int len; // size of the uncompressed file
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2013-05-06 22:04:45 +08:00
|
|
|
CCZ_COMPRESSION_ZLIB, // zlib format.
|
|
|
|
CCZ_COMPRESSION_BZIP2, // bzip2 format (not supported yet)
|
|
|
|
CCZ_COMPRESSION_GZIP, // gzip format (not supported yet)
|
|
|
|
CCZ_COMPRESSION_NONE, // plain (not supported yet)
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2013-05-08 11:45:27 +08:00
|
|
|
class CC_DLL ZipUtils
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Inflates either zlib or gzip deflated memory. The inflated memory is
|
|
|
|
* expected to be freed by the caller.
|
|
|
|
*
|
2012-09-17 15:02:24 +08:00
|
|
|
* 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.
|
2012-04-19 14:35:52 +08:00
|
|
|
* @returns the length of the deflated buffer
|
|
|
|
*
|
|
|
|
@since v0.8.1
|
|
|
|
*/
|
2013-12-06 16:32:06 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static ssize_t ccInflateMemory(unsigned char *in, ssize_t inLength, unsigned char **out) { return inflateMemory(in, inLength, out); }
|
|
|
|
static ssize_t inflateMemory(unsigned char *in, ssize_t inLength, unsigned char **out);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inflates either zlib or gzip deflated memory. The inflated memory is
|
|
|
|
* expected to be freed by the caller.
|
|
|
|
*
|
|
|
|
* outLenghtHint is assumed to be the needed room to allocate the inflated buffer.
|
|
|
|
*
|
|
|
|
* @returns the length of the deflated buffer
|
|
|
|
*
|
|
|
|
@since v1.0.0
|
|
|
|
*/
|
2013-12-06 16:32:06 +08:00
|
|
|
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); }
|
|
|
|
static ssize_t inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t outLengthHint);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** inflates a GZip file into memory
|
|
|
|
*
|
|
|
|
* @returns the length of the deflated buffer
|
|
|
|
*
|
|
|
|
* @since v0.99.5
|
|
|
|
*/
|
2013-11-14 13:35:17 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static int ccInflateGZipFile(const char *filename, unsigned char **out) { return inflateGZipFile(filename, out); }
|
2013-11-12 10:09:47 +08:00
|
|
|
static int inflateGZipFile(const char *filename, unsigned char **out);
|
2013-07-25 21:35:00 +08:00
|
|
|
|
|
|
|
/** test a file is a GZip format file or not
|
|
|
|
*
|
|
|
|
* @returns true is a GZip format file. false is not
|
|
|
|
*
|
|
|
|
* @since v3.0
|
|
|
|
*/
|
2013-11-14 13:35:17 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static bool ccIsGZipFile(const char *filename) { return isGZipFile(filename); }
|
2013-11-12 10:09:47 +08:00
|
|
|
static bool isGZipFile(const char *filename);
|
2013-07-25 21:35:00 +08:00
|
|
|
|
|
|
|
/** test the buffer is GZip format or not
|
|
|
|
*
|
|
|
|
* @returns true is GZip format. false is not
|
|
|
|
*
|
|
|
|
* @since v3.0
|
|
|
|
*/
|
2013-12-06 16:32:06 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static bool ccIsGZipBuffer(const unsigned char *buffer, ssize_t len) { return isGZipBuffer(buffer, len); }
|
|
|
|
static bool isGZipBuffer(const unsigned char *buffer, ssize_t len);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** inflates a CCZ file into memory
|
|
|
|
*
|
|
|
|
* @returns the length of the deflated buffer
|
|
|
|
*
|
|
|
|
* @since v0.99.5
|
|
|
|
*/
|
2013-11-14 13:35:17 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static int ccInflateCCZFile(const char *filename, unsigned char **out) { return inflateCCZFile(filename, out); }
|
2013-11-12 10:09:47 +08:00
|
|
|
static int inflateCCZFile(const char *filename, unsigned char **out);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-25 21:35:00 +08:00
|
|
|
/** inflates a buffer with CCZ format into memory
|
|
|
|
*
|
|
|
|
* @returns the length of the deflated buffer
|
|
|
|
*
|
|
|
|
* @since v3.0
|
|
|
|
*/
|
2013-12-06 16:32:06 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static int ccInflateCCZBuffer(const unsigned char *buffer, ssize_t len, unsigned char **out) { return inflateCCZBuffer(buffer, len, out); }
|
|
|
|
static int inflateCCZBuffer(const unsigned char *buffer, ssize_t len, unsigned char **out);
|
2013-07-25 21:35:00 +08:00
|
|
|
|
|
|
|
/** test a file is a CCZ format file or not
|
|
|
|
*
|
|
|
|
* @returns true is a CCZ format file. false is not
|
|
|
|
*
|
|
|
|
* @since v3.0
|
|
|
|
*/
|
2013-11-14 13:35:17 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static bool ccIsCCZFile(const char *filename) { return isCCZFile(filename); }
|
2013-11-12 10:09:47 +08:00
|
|
|
static bool isCCZFile(const char *filename);
|
2013-07-25 21:35:00 +08:00
|
|
|
|
|
|
|
/** test the buffer is CCZ format or not
|
|
|
|
*
|
|
|
|
* @returns true is CCZ format. false is not
|
|
|
|
*
|
|
|
|
* @since v3.0
|
|
|
|
*/
|
2013-12-06 16:32:06 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static bool ccIsCCZBuffer(const unsigned char *buffer, ssize_t len) { return isCCZBuffer(buffer, len); }
|
|
|
|
static bool isCCZBuffer(const unsigned char *buffer, ssize_t len);
|
2013-07-25 21:35:00 +08:00
|
|
|
|
2013-05-07 22:14:38 +08:00
|
|
|
/** Sets the pvr.ccz encryption key parts separately for added
|
|
|
|
* security.
|
2013-05-06 22:04:45 +08:00
|
|
|
*
|
2013-05-07 22:14:38 +08:00
|
|
|
* Example: If the key used to encrypt the pvr.ccz file is
|
|
|
|
* 0xaaaaaaaabbbbbbbbccccccccdddddddd you will call this function 4
|
|
|
|
* different times, preferably from 4 different source files, as follows
|
2013-05-06 22:04:45 +08:00
|
|
|
*
|
2013-11-12 10:09:47 +08:00
|
|
|
* ZipUtils::setPvrEncryptionKeyPart(0, 0xaaaaaaaa);
|
|
|
|
* ZipUtils::setPvrEncryptionKeyPart(1, 0xbbbbbbbb);
|
|
|
|
* ZipUtils::setPvrEncryptionKeyPart(2, 0xcccccccc);
|
|
|
|
* ZipUtils::setPvrEncryptionKeyPart(3, 0xdddddddd);
|
2013-05-06 22:04:45 +08:00
|
|
|
*
|
2013-05-07 22:14:38 +08:00
|
|
|
* Splitting the key into 4 parts and calling the function
|
|
|
|
* from 4 different source files increases the difficulty to
|
|
|
|
* reverse engineer the encryption key. Be aware that encrpytion
|
|
|
|
* is *never* 100% secure and the key code can be cracked by
|
|
|
|
* knowledgable persons.
|
|
|
|
*
|
2013-11-12 10:09:47 +08:00
|
|
|
* IMPORTANT: Be sure to call setPvrEncryptionKey or
|
|
|
|
* setPvrEncryptionKeyPart with all of the key parts *before* loading
|
2013-05-07 22:14:38 +08:00
|
|
|
* the spritesheet or decryption will fail and the spritesheet
|
|
|
|
* will fail to load.
|
2013-05-06 22:04:45 +08:00
|
|
|
*
|
|
|
|
* @param index part of the key [0..3]
|
|
|
|
* @param value value of the key part
|
|
|
|
*/
|
2013-11-14 13:35:17 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static void ccSetPvrEncryptionKeyPart(int index, unsigned int value) { setPvrEncryptionKeyPart(index, value); }
|
2013-11-12 10:09:47 +08:00
|
|
|
static void setPvrEncryptionKeyPart(int index, unsigned int value);
|
2013-05-07 22:14:38 +08:00
|
|
|
|
|
|
|
/** Sets the pvr.ccz encryption key.
|
|
|
|
*
|
|
|
|
* Example: If the key used to encrypt the pvr.ccz file is
|
|
|
|
* 0xaaaaaaaabbbbbbbbccccccccdddddddd you will call this function with
|
|
|
|
* the key split into 4 parts as follows
|
|
|
|
*
|
2013-11-12 10:09:47 +08:00
|
|
|
* ZipUtils::setPvrEncryptionKey(0xaaaaaaaa, 0xbbbbbbbb, 0xcccccccc, 0xdddddddd);
|
2013-05-07 22:14:38 +08:00
|
|
|
*
|
|
|
|
* Note that using this function makes it easier to reverse engineer and
|
|
|
|
* discover the complete key because the key parts are present in one
|
|
|
|
* function call.
|
|
|
|
*
|
2013-11-12 10:09:47 +08:00
|
|
|
* IMPORTANT: Be sure to call setPvrEncryptionKey or
|
|
|
|
* setPvrEncryptionKeyPart with all of the key parts *before* loading
|
2013-05-07 22:14:38 +08:00
|
|
|
* the spritesheet or decryption will fail and the spritesheet
|
|
|
|
* will fail to load.
|
|
|
|
*
|
|
|
|
* @param keyPart1 the key value part 1.
|
|
|
|
* @param keyPart2 the key value part 2.
|
|
|
|
* @param keyPart3 the key value part 3.
|
|
|
|
* @param keyPart4 the key value part 4.
|
|
|
|
*/
|
2013-11-14 13:35:17 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE static void ccSetPvrEncryptionKey(unsigned int keyPart1, unsigned int keyPart2, unsigned int keyPart3, unsigned int keyPart4) { setPvrEncryptionKey(keyPart1, keyPart2, keyPart3, keyPart4); }
|
2013-11-12 10:09:47 +08:00
|
|
|
static void setPvrEncryptionKey(unsigned int keyPart1, unsigned int keyPart2, unsigned int keyPart3, unsigned int keyPart4);
|
2013-05-06 22:04:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
private:
|
2013-12-06 16:32:06 +08:00
|
|
|
static int inflateMemoryWithHint(unsigned char *in, ssize_t inLength, unsigned char **out, ssize_t *outLength, ssize_t outLenghtHint);
|
|
|
|
static inline void decodeEncodedPvr (unsigned int *data, ssize_t len);
|
|
|
|
static inline unsigned int checksumPvr(const unsigned int *data, ssize_t len);
|
2013-05-06 22:04:45 +08:00
|
|
|
|
|
|
|
static unsigned int s_uEncryptedPvrKeyParts[4];
|
|
|
|
static unsigned int s_uEncryptionKey[1024];
|
|
|
|
static bool s_bEncryptionKeyIsValid;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2012-11-08 19:46:53 +08:00
|
|
|
// forward declaration
|
|
|
|
class ZipFilePrivate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Zip file - reader helper class.
|
|
|
|
*
|
|
|
|
* It will cache the file list of a particular zip file with positions inside an archive,
|
|
|
|
* so it would be much faster to read some particular files or to check their existance.
|
|
|
|
*
|
|
|
|
* @since v2.0.5
|
|
|
|
*/
|
|
|
|
class ZipFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor, open zip file and store file list.
|
|
|
|
*
|
|
|
|
* @param zipFile Zip file name
|
|
|
|
* @param filter The first part of file names, which should be accessible.
|
|
|
|
* For example, "assets/". Other files will be missed.
|
|
|
|
*
|
|
|
|
* @since v2.0.5
|
|
|
|
*/
|
|
|
|
ZipFile(const std::string &zipFile, const std::string &filter = std::string());
|
|
|
|
virtual ~ZipFile();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerate accessible file list based on a new filter string.
|
|
|
|
*
|
|
|
|
* @param filter New filter string (first part of files names)
|
|
|
|
* @return true whenever zip file is open successfully and it is possible to locate
|
|
|
|
* at least the first file, false otherwise
|
|
|
|
*
|
|
|
|
* @since v2.0.5
|
|
|
|
*/
|
|
|
|
bool setFilter(const std::string &filter);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check does a file exists or not in zip file
|
|
|
|
*
|
|
|
|
* @param fileName File to be checked on existance
|
|
|
|
* @return true whenever file exists, false otherwise
|
|
|
|
*
|
|
|
|
* @since v2.0.5
|
|
|
|
*/
|
|
|
|
bool fileExists(const std::string &fileName) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get resource file data from a zip file.
|
|
|
|
* @param fileName File name
|
|
|
|
* @param[out] pSize If the file read operation succeeds, it will be the data size, otherwise 0.
|
2013-12-18 17:47:20 +08:00
|
|
|
* @return Upon success, a pointer to the data is returned, otherwise nullptr.
|
|
|
|
* @warning Recall: you are responsible for calling free() on any Non-nullptr pointer returned.
|
2012-11-08 19:46:53 +08:00
|
|
|
*
|
|
|
|
* @since v2.0.5
|
|
|
|
*/
|
2013-12-05 17:19:01 +08:00
|
|
|
unsigned char *getFileData(const std::string &fileName, ssize_t *size);
|
2012-11-08 19:46:53 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/** Internal data like zip file pointer / file list array and so on */
|
2013-06-15 14:03:30 +08:00
|
|
|
ZipFilePrivate *_data;
|
2012-11-08 19:46:53 +08:00
|
|
|
};
|
2012-04-19 14:35:52 +08:00
|
|
|
} // end of namespace cocos2d
|
2012-11-08 19:46:53 +08:00
|
|
|
#endif // __SUPPORT_ZIPUTILS_H__
|
2012-04-19 14:35:52 +08:00
|
|
|
|