2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2022-04-25 19:15:46 +08:00
|
|
|
Copyright (c) 2020 C4games Ltd
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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__
|
|
|
|
/// @cond DO_NOT_SHOW
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/PlatformMacros.h"
|
|
|
|
#include "platform/FileUtils.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include <string>
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2023-06-11 13:08:08 +08:00
|
|
|
# include "platform/android/FileUtils-android.h"
|
2022-07-16 10:43:05 +08:00
|
|
|
#elif (AX_TARGET_PLATFORM == AX_PLATFORM_WIN32)
|
2019-11-23 20:27:39 +08:00
|
|
|
// for import ssize_t on win32 platform
|
2023-06-11 13:08:08 +08:00
|
|
|
# include "platform/StdC.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
2020-02-28 13:55:25 +08:00
|
|
|
#ifndef _unz64_H
|
2021-06-18 11:31:07 +08:00
|
|
|
struct unz_file_info_s;
|
2020-02-28 13:55:25 +08:00
|
|
|
#endif
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup base
|
|
|
|
* @{
|
|
|
|
*/
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2020-02-28 13:55:25 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/** XXX: pragma pack ???
|
|
|
|
* @struct CCZHeader
|
|
|
|
*/
|
|
|
|
struct CCZHeader
|
|
|
|
{
|
|
|
|
unsigned char sig[4]; /** Signature. Should be 'CCZ!' 4 bytes. */
|
|
|
|
unsigned short compression_type; /** Should be 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. */
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
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). */
|
|
|
|
};
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
class AX_DLL ZipUtils
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Inflates either zlib or gzip deflated memory. The inflated memory is expected to be freed by the caller.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return The length of the deflated buffer.
|
|
|
|
* @since v0.8.1
|
|
|
|
*/
|
|
|
|
static ssize_t inflateMemory(unsigned char* in, ssize_t inLength, unsigned char** out);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inflates either zlib or gzip deflated memory. The inflated memory is expected to be freed by the caller.
|
|
|
|
*
|
|
|
|
* @param outLengthHint It is assumed to be the needed room to allocate the inflated buffer.
|
|
|
|
*
|
|
|
|
* @return The length of the deflated buffer.
|
|
|
|
* @since v1.0.0
|
|
|
|
*/
|
|
|
|
static ssize_t inflateMemoryWithHint(unsigned char* in,
|
|
|
|
ssize_t inLength,
|
|
|
|
unsigned char** out,
|
|
|
|
ssize_t outLengthHint);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inflates a GZip file into memory.
|
|
|
|
*
|
|
|
|
* @return The length of the deflated buffer.
|
|
|
|
* @since v0.99.5
|
|
|
|
*/
|
|
|
|
static int inflateGZipFile(const char* filename, unsigned char** out);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a file is a GZip format file or not.
|
|
|
|
*
|
|
|
|
* @return True is a GZip format file. false is not.
|
|
|
|
* @since v3.0
|
|
|
|
*/
|
|
|
|
static bool isGZipFile(const char* filename);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the buffer is GZip format or not.
|
|
|
|
*
|
|
|
|
* @return True is GZip format. false is not.
|
|
|
|
* @since v3.0
|
2019-11-23 20:27:39 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
static bool isGZipBuffer(const unsigned char* buffer, ssize_t len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inflates a CCZ file into memory.
|
|
|
|
*
|
|
|
|
* @return The length of the deflated buffer.
|
|
|
|
* @since v0.99.5
|
|
|
|
*/
|
|
|
|
static int inflateCCZFile(const char* filename, unsigned char** out);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inflates a buffer with CCZ format into memory.
|
|
|
|
*
|
|
|
|
* @return The length of the deflated buffer.
|
|
|
|
* @since v3.0
|
|
|
|
*/
|
|
|
|
static int inflateCCZBuffer(const unsigned char* buffer, ssize_t len, unsigned char** out);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a file is a CCZ format file or not.
|
|
|
|
*
|
|
|
|
* @return True is a CCZ format file. false is not.
|
|
|
|
* @since v3.0
|
|
|
|
*/
|
|
|
|
static bool isCCZFile(const char* filename);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the buffer is CCZ format or not.
|
|
|
|
*
|
|
|
|
* @return True is CCZ format. false is not.
|
|
|
|
* @since v3.0
|
|
|
|
*/
|
|
|
|
static bool isCCZBuffer(const unsigned char* buffer, ssize_t len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the pvr.ccz encryption key parts separately for added security.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* ZipUtils::setPvrEncryptionKeyPart(0, 0xaaaaaaaa);
|
|
|
|
* ZipUtils::setPvrEncryptionKeyPart(1, 0xbbbbbbbb);
|
|
|
|
* ZipUtils::setPvrEncryptionKeyPart(2, 0xcccccccc);
|
|
|
|
* ZipUtils::setPvrEncryptionKeyPart(3, 0xdddddddd);
|
|
|
|
*
|
|
|
|
* 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 encryption is *never* 100% secure and the key code
|
|
|
|
* can be cracked by knowledgable persons.
|
|
|
|
*
|
|
|
|
* IMPORTANT: Be sure to call setPvrEncryptionKey or
|
|
|
|
* setPvrEncryptionKeyPart with all of the key parts *before* loading
|
|
|
|
* the sprite sheet or decryption will fail and the sprite sheet
|
|
|
|
* will fail to load.
|
|
|
|
*
|
|
|
|
* @param index Part of the key [0..3].
|
|
|
|
* @param value Value of the key part.
|
|
|
|
*/
|
|
|
|
static void setPvrEncryptionKeyPart(int index, unsigned int value);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +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
|
|
|
|
*
|
|
|
|
* ZipUtils::setPvrEncryptionKey(0xaaaaaaaa, 0xbbbbbbbb, 0xcccccccc, 0xdddddddd);
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* IMPORTANT: Be sure to call setPvrEncryptionKey or setPvrEncryptionKeyPart
|
|
|
|
* with all of the key parts *before* loading the spritesheet or decryption
|
|
|
|
* will fail and the sprite sheet 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.
|
|
|
|
*/
|
|
|
|
static void setPvrEncryptionKey(unsigned int keyPart1,
|
|
|
|
unsigned int keyPart2,
|
|
|
|
unsigned int keyPart3,
|
|
|
|
unsigned int keyPart4);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static int inflateMemoryWithHint(unsigned char* in,
|
|
|
|
ssize_t inLength,
|
|
|
|
unsigned char** out,
|
|
|
|
ssize_t* outLength,
|
|
|
|
ssize_t outLengthHint);
|
|
|
|
static inline void decodeEncodedPvr(unsigned int* data, ssize_t len);
|
|
|
|
static inline unsigned int checksumPvr(const unsigned int* data, ssize_t len);
|
|
|
|
|
|
|
|
static unsigned int s_uEncryptedPvrKeyParts[4];
|
|
|
|
static unsigned int s_uEncryptionKey[1024];
|
|
|
|
static bool s_bEncryptionKeyIsValid;
|
|
|
|
};
|
|
|
|
|
|
|
|
// forward declaration
|
|
|
|
struct ZipEntryInfo;
|
|
|
|
struct 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 existence.
|
|
|
|
*
|
|
|
|
* @since v2.0.5
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
class AX_DLL ZipFile
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
public:
|
2023-08-13 22:09:02 +08:00
|
|
|
static ZipFile* createFromFile(std::string_view zipFile, std::string_view filter = ""sv);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual ~ZipFile();
|
|
|
|
|
2023-08-13 22:09:02 +08:00
|
|
|
bool initWithFile(std::string_view zipFile, std::string_view filter = ""sv);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
bool setFilter(std::string_view filter);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check does a file exists or not in zip file
|
|
|
|
*
|
|
|
|
* @param fileName File to be checked on existence
|
|
|
|
* @return true whenever file exists, false otherwise
|
|
|
|
*
|
|
|
|
* @since v2.0.5
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
bool fileExists(std::string_view fileName) const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get files and folders in pathname
|
|
|
|
*
|
|
|
|
* @param dirname
|
|
|
|
* @return
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
std::vector<std::string> listFiles(std::string_view pathname) const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get resource file data from a zip file.
|
|
|
|
* @param fileName File name
|
|
|
|
* @param[out] buffer If the file read operation succeeds, if will contain the file data.
|
|
|
|
* @return True if successful.
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
bool getFileData(std::string_view fileName, ResizableBuffer* buffer);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
std::string getFirstFilename();
|
|
|
|
std::string getNextFilename();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* zipFile Streaming support, !!!important, the file in zip must no compress level, otherwise
|
|
|
|
* stream seek doesn't work.
|
|
|
|
*/
|
2023-08-13 22:09:02 +08:00
|
|
|
ZipEntryInfo* vopen(std::string_view fileName);
|
|
|
|
int vread(ZipEntryInfo*, void* buf, unsigned int size);
|
|
|
|
int64_t vseek(ZipEntryInfo*, int64_t offset, int origin);
|
|
|
|
void vclose(ZipEntryInfo*);
|
|
|
|
int64_t vsize(ZipEntryInfo*);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
ZipFile();
|
|
|
|
|
|
|
|
int getCurrentFileInfo(std::string* filename, unz_file_info_s* info);
|
|
|
|
|
|
|
|
/** Internal data like zip file pointer / file list array and so on */
|
|
|
|
ZipFilePrivate* _data;
|
|
|
|
};
|
2022-07-11 17:50:21 +08:00
|
|
|
|
2022-08-29 20:51:22 +08:00
|
|
|
NS_AX_END // end of namespace ax
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// end group
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// @endcond
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // __SUPPORT_ZIPUTILS_H__
|