Ejemplo n.º 1
0
unsigned char *EncryptXXTea::decode()
{
    UserData user_d = {10,20,30};
    unsigned char *t_data = (unsigned char *)&user_d;
    const  char *key_s = "YuanfeiTestXXTea";
    xxtea_long key_len = strlen(key_s);
    xxtea_long ret_len;
    unsigned char *bufs=xxtea_encrypt(t_data, sizeof(UserData), (unsigned char*)key_s, key_len, &ret_len);
    
    char t_buf[512];
    memset(&t_buf, 0, 512);
    memcpy(&t_buf, bufs, ret_len+1);
    
    key_s = "YuanfeiTestXXTea";
    key_len = strlen(key_s);
    xxtea_long de_len;
    unsigned char *de_data=xxtea_decrypt(bufs, ret_len, (unsigned char*)key_s, key_len, &de_len);
    memset(&t_buf, 0, 512);
    memcpy(&t_buf, de_data, ret_len+1);
    
    UserData decod;
    memset(&decod, 0, sizeof(UserData));
    memcpy(&decod, de_data, sizeof(UserData));
    return nullptr;

}
Ejemplo n.º 2
0
extern Data xxtea_file_get_data(const std::string& filename)
{
	Data data = FileUtils::getInstance()->getDataFromFile(filename);

    if (!data.isNull())
    {
        bool isXXTEA = true;
		unsigned char *buf = data.getBytes();
		ssize_t size = data.getSize();
        for (int i = 0; isXXTEA && i<xxteaSignLen && i<size; ++i)
        {
            isXXTEA = buf[i] == xxteaSign[i];
        }

        if (isXXTEA)
        { // decrypt XXTEA
            xxtea_long len = 0;
			unsigned char* buffer = xxtea_decrypt(
                                   buf + xxteaSignLen,
                                   (xxtea_long)size - (xxtea_long)xxteaSignLen,
                                   (unsigned char*)xxteaKey.c_str(),
                                   (xxtea_long)xxteaKeyLen,
                                   &len);
			data.clear();
			data.fastSet(buffer, len);
        }
    }

	return data;
}
Ejemplo n.º 3
0
unsigned char* CACrypto::decryptXXTEA(unsigned char* ciphertext,
                                      int ciphertextLength,
                                      unsigned char* key,
                                      int keyLength,
                                      int* resultLength)
{
    xxtea_long len;
    unsigned char* result = xxtea_decrypt(ciphertext, (xxtea_long)ciphertextLength, key, (xxtea_long)keyLength, &len);
    *resultLength = (int)len;
    return result;
}
unsigned char* HelperFunc::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{
    ssize_t size;
    unsigned char* buf = FileUtils::getInstance()->getFileData(pszFileName, pszMode, &size);
    if (NULL==buf || size<1) return NULL;

#ifdef LUASTACK_USED_FOR_QUICK_COCOS2DX
    LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
    if (NULL==stack) {
        return NULL;
    }
    int xxteaKeyLen = 0;
    const char *xxteaKey = stack->getXXTEAKey(&xxteaKeyLen);
    int xxteaSignLen = 0;
    const char *xxteaSign = stack->getXXTEASign(&xxteaSignLen);
    unsigned char* buffer = NULL;
    if (xxteaKey && xxteaSign) {
        bool isXXTEA = true;
        for (int i = 0; isXXTEA && i<xxteaSignLen && i<size; ++i) {
            isXXTEA = buf[i] == xxteaSign[i];
        }

        if (isXXTEA) { // decrypt XXTEA
            xxtea_long len = 0;
            buffer = xxtea_decrypt(
                                   buf + xxteaSignLen,
                                   (xxtea_long)size - (xxtea_long)xxteaSignLen,
                                   (unsigned char*)xxteaKey,
                                   (xxtea_long)xxteaKeyLen,
                                   &len);
            delete []buf;
            buf = NULL;
            size = len;
        } else {
            buffer = buf;
        }
    }
    else {
        buffer = buf;
    }


    if (pSize) *pSize = size;
    return buffer;
#else
    if (pSize) *pSize = size;
    return buf;
#endif
}
Ejemplo n.º 5
0
bool lua_msg_handler(lua_State *state, SockMsg* msg)
{
	int functionIndex = -3;
	Worker *worker = &g_workers[msg->sock->epoll_idx];
	lua_getglobal(worker->state, worker->msg_handler);
	lua_pushinteger(state, (int)msg->sock->fd);

#if ENABLE_XXTEA_ZLIB
	uLong buf_len = MAX_LUA_MSG_LEN;
	uncompress((Bytef*)msg_buf, &buf_len, (Bytef*)MSG_MSG(msg), MSG_LEN(msg));
	xxtea_long xxlen = 0;
	char* xxresult = (char*)xxtea_decrypt((unsigned char*)msg_buf, buf_len, (unsigned char*)xxtea_key, strlen(xxtea_key), &xxlen);
	lua_pushlstring(state, xxresult, xxlen);
	free(xxresult);
#else
	//printf("lua_msg_handler: %d %s\n", MSG_MSG(msg), MSG_LEN(msg));
	lua_pushlstring(state, MSG_MSG(msg), MSG_LEN(msg));
#endif
	int traceback = 0;
    lua_getglobal(worker->state, worker->trace_handler); 
    if (!lua_isfunction(worker->state, -1))
    {
        lua_pop(worker->state, 1);                                            /* L: ... func arg1 arg2 ... */
    }
    else
    {
        lua_insert(worker->state, functionIndex - 1);                         /* L: ... G func arg1 arg2 ... */
        traceback = functionIndex - 1;
    }

	bool ret = lua_pcall(worker->state, 2, 0, traceback);
	if(ret != 0)
	{
		Log();
		printf("lua_pcall failed: %s\n", lua_tostring(worker->state, -1));
		lua_pop(worker->state, 2);
		lua_settop(worker->state, 0);
		return false;
	}
	ret = lua_toboolean(state, -1);

	lua_pop(worker->state, 3);
	lua_settop(worker->state, 0);

    return ret;
}
Ejemplo n.º 6
0
int LuaStack::luaLoadBuffer(lua_State *L, const char *chunk, int chunkSize, const char *chunkName)
{
    int r = 0;
    
    if (_xxteaEnabled && strncmp(chunk, _xxteaSign, _xxteaSignLen) == 0)
    {
        // decrypt XXTEA
        xxtea_long len = 0;
        unsigned char* result = xxtea_decrypt((unsigned char*)chunk + _xxteaSignLen,
                                              (xxtea_long)chunkSize - _xxteaSignLen,
                                              (unsigned char*)_xxteaKey,
                                              (xxtea_long)_xxteaKeyLen,
                                              &len);
        r = luaL_loadbuffer(L, (char*)result, len, chunkName);
        free(result);
    }
    else
    {
        r = luaL_loadbuffer(L, chunk, chunkSize, chunkName);
    }
    
#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0
    if (r)
    {
        switch (r)
        {
            case LUA_ERRSYNTAX:
                CCLOG("[LUA ERROR] load \"%s\", error: syntax error during pre-compilation.", chunkName);
                break;
                
            case LUA_ERRMEM:
                CCLOG("[LUA ERROR] load \"%s\", error: memory allocation error.", chunkName);
                break;
                
            case LUA_ERRFILE:
                CCLOG("[LUA ERROR] load \"%s\", error: cannot open/read file.", chunkName);
                break;
                
            default:
                CCLOG("[LUA ERROR] load \"%s\", error: unknown.", chunkName);
        }
    }
#endif
    return r;
}
Ejemplo n.º 7
0
Archivo: test.c Proyecto: anley/xxtea-c
int main() {
    const char *text = "Hello World! 你好,中国!";
    const char *key = "1234567890";
    size_t len;
    unsigned char *encrypt_data = xxtea_encrypt(text, strlen(text), key, &len);
    char * base64_data = base64_encode(encrypt_data, len);
    printf("%s\n", base64_data);
    char *decrypt_data = xxtea_decrypt(encrypt_data, len, key, &len);
    if (strncmp(text, decrypt_data, len) == 0) {
        printf("success!\n");
    }
    else {
        printf("fail!\n");
    }
    free(encrypt_data);
    free(decrypt_data);
    free(base64_data);
    return 0;
}
unsigned char* FileUtils::getDecryptFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{
    ssize_t size;
    unsigned char* buf = FileUtils::getInstance()->getFileData(pszFileName, pszMode, &size);
    if (NULL == buf || size<1) return NULL;


    const char *xxteaKey = "ilovecocos2dx";
    int xxteaKeyLen = strlen(xxteaKey);
    const char *xxteaSign = "XXTEA";
    int xxteaSignLen = strlen(xxteaSign);
    unsigned char* buffer = NULL;

    bool isXXTEA = true;
    for (int i = 0; isXXTEA && i<xxteaSignLen && i<size; ++i) {
        isXXTEA = buf[i] == xxteaSign[i];
    }

    if (isXXTEA) { // decrypt XXTEA
        xxtea_long len = 0;
        buffer = xxtea_decrypt(
                     buf + xxteaSignLen,
                     (xxtea_long)size - (xxtea_long)xxteaSignLen,
                     (unsigned char*)xxteaKey,
                     (xxtea_long)xxteaKeyLen,
                     &len);
        delete[]buf;
        buf = NULL;
        size = len;
    }
    else {
        buffer = buf;
    }


    if (pSize) *pSize = size;
    return buffer;
}
Ejemplo n.º 9
0
unsigned char* CZHelperFunc::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{
    unsigned long size;
    unsigned char* buf = CCFileUtils::sharedFileUtils()->getFileData(pszFileName, pszMode, &size);
    if (NULL==buf || size<1) return NULL;
    
    CCLuaStack* stack = CCLuaEngine::defaultEngine()->getLuaStack();
	unsigned char* buffer = NULL;

        bool isXXTEA = stack && stack->m_xxteaEnabled;
        for (unsigned int i = 0; isXXTEA && ((int)i) < stack->m_xxteaSignLen && i < size; ++i)
        {
            isXXTEA = buf[i] == stack->m_xxteaSign[i];
        }

        if (isXXTEA)
        {
            // decrypt XXTEA
            xxtea_long len = 0;
            buffer = xxtea_decrypt(buf + stack->m_xxteaSignLen,
                                   (xxtea_long)size - (xxtea_long)stack->m_xxteaSignLen,
                                   (unsigned char*)stack->m_xxteaKey,
                                   (xxtea_long)stack->m_xxteaKeyLen,
                                   &len);
            delete []buf;
            buf = NULL;
			size = len;
        }
        else
        {
			buffer = buf;
        }

		if (pSize) *pSize = size;
		return buffer;
}
Ejemplo n.º 10
0
int LuaStack::luaLoadChunksFromZIP(lua_State *L)
{
    if (lua_gettop(L) < 1) {
        CCLOG("luaLoadChunksFromZIP() - invalid arguments");
        return 0;
    }
    
    const char *zipFilename = lua_tostring(L, -1);
    lua_settop(L, 0);
    FileUtils *utils = FileUtils::getInstance();
    std::string zipFilePath = utils->fullPathForFilename(zipFilename);
    
    LuaStack *stack = this;
    
    do {
        ssize_t size = 0;
        void *buffer = nullptr;
        unsigned char *zipFileData = utils->getFileData(zipFilePath.c_str(), "rb", &size);
        ZipFile *zip = nullptr;
        
        bool isXXTEA = stack && stack->_xxteaEnabled && zipFileData;
        for (int i = 0; isXXTEA && i < stack->_xxteaSignLen && i < size; ++i) {
            isXXTEA = zipFileData[i] == stack->_xxteaSign[i];
        }
        
        if (isXXTEA) { // decrypt XXTEA
            xxtea_long len = 0;
            buffer = xxtea_decrypt(zipFileData + stack->_xxteaSignLen,
                                   (xxtea_long)size - (xxtea_long)stack->_xxteaSignLen,
                                   (unsigned char*)stack->_xxteaKey,
                                   (xxtea_long)stack->_xxteaKeyLen,
                                   &len);
            free(zipFileData);
            zipFileData = nullptr;
            zip = ZipFile::createWithBuffer(buffer, len);
        } else {
            if (zipFileData) {
                zip = ZipFile::createWithBuffer(zipFileData, size);
            }
        }
        
        if (zip) {
            CCLOG("lua_loadChunksFromZIP() - load zip file: %s%s", zipFilePath.c_str(), isXXTEA ? "*" : "");
            lua_getglobal(L, "package");
            lua_getfield(L, -1, "preload");
            
            int count = 0;
            std::string filename = zip->getFirstFilename();
            while (filename.length()) {
                ssize_t bufferSize = 0;
                unsigned char *zbuffer = zip->getFileData(filename.c_str(), &bufferSize);
                if (bufferSize) {
                    // remove extension
                    std::size_t found = filename.rfind(".lua");
                    if (found != std::string::npos)
                    {
                        filename.erase(found);
                    }
                    // replace path seperator '/' '\' to '.'
                    for (int i=0; i<filename.size(); i++) {
                        if (filename[i] == '/' || filename[i] == '\\') {
                            filename[i] = '.';
                        }
                    }
                    CCLOG("[luaLoadChunksFromZIP] add %s to preload", filename.c_str());
                    if (stack->luaLoadBuffer(L, (char*)zbuffer, (int)bufferSize, filename.c_str()) == 0) {
                        lua_setfield(L, -2, filename.c_str());
                        ++count;
                    }
                    free(zbuffer);
                }
                filename = zip->getNextFilename();
            }
            CCLOG("lua_loadChunksFromZIP() - loaded chunks count: %d", count);
            lua_pop(L, 2);
            lua_pushboolean(L, 1);
            
            delete zip;
        } else {
            CCLOG("lua_loadChunksFromZIP() - not found or invalid zip file: %s", zipFilePath.c_str());
            lua_pushboolean(L, 0);
        }
        
        if (zipFileData) {
            free(zipFileData);
        }
        
        if (buffer) {
            free(buffer);
        }
    } while (0);
    
    return 1;
}
Ejemplo n.º 11
0
int LuaStack::luaLoadBuffer(lua_State *L, const char *chunk, int chunkSize, const char *chunkName)
{
    int r = 0;
    
    auto skipBOM = [](const char*& chunk, int& chunkSize){
        // UTF-8 BOM? skip
        if (static_cast<unsigned char>(chunk[0]) == 0xEF &&
            static_cast<unsigned char>(chunk[1]) == 0xBB &&
            static_cast<unsigned char>(chunk[2]) == 0xBF)
        {
            chunk += 3;
            chunkSize -= 3;
        }
    };

    if (_xxteaEnabled && strncmp(chunk, _xxteaSign, _xxteaSignLen) == 0)
    {
        // decrypt XXTEA
        xxtea_long len = 0;
        unsigned char* result = xxtea_decrypt((unsigned char*)chunk + _xxteaSignLen,
                                              (xxtea_long)chunkSize - _xxteaSignLen,
                                              (unsigned char*)_xxteaKey,
                                              (xxtea_long)_xxteaKeyLen,
                                              &len);
        skipBOM((const char*&)result, (int&)len);
        r = luaL_loadbuffer(L, (char*)result, len, chunkName);
        free(result);
    }
    else
    {
        skipBOM(chunk, chunkSize);
        r = luaL_loadbuffer(L, chunk, chunkSize, chunkName);
    }

#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0
    if (r)
    {
        switch (r)
        {
            case LUA_ERRSYNTAX:
                CCLOG("[LUA ERROR] load \"%s\", error: syntax error during pre-compilation.", chunkName);
                break;

            case LUA_ERRMEM:
                CCLOG("[LUA ERROR] load \"%s\", error: memory allocation error.", chunkName);
                break;

            case LUA_ERRFILE:
                CCLOG("[LUA ERROR] load \"%s\", error: cannot open/read file.", chunkName);
                break;

            default:
                CCLOG("[LUA ERROR] load \"%s\", error: unknown.", chunkName);
        }
        
        const char* error = lua_tostring(L, -1);
        CCLOG("[LUA_ERROR:] error result: %s", error);
        lua_pop(L, 1);
    }
#endif
    return r;
}
Ejemplo n.º 12
0
int LuaStack::luaLoadChunksFromZIP(lua_State *L)
{
    if (lua_gettop(L) < 1) {
        CCLOG("luaLoadChunksFromZIP() - invalid arguments");
        return 0;
    }

    const char *zipFilename = lua_tostring(L, -1);
    lua_settop(L, 0);
    FileUtils *utils = FileUtils::getInstance();
    std::string zipFilePath = utils->fullPathForFilename(zipFilename);

    LuaStack *stack = this;

    do {
        void *buffer = nullptr;
        ZipFile *zip = nullptr;
        Data zipFileData(utils->getDataFromFile(zipFilePath));
        unsigned char* bytes = zipFileData.getBytes();
        ssize_t size = zipFileData.getSize();

        bool isXXTEA = stack && stack->_xxteaEnabled && size >= stack->_xxteaSignLen
            && memcmp(stack->_xxteaSign, bytes, stack->_xxteaSignLen) == 0;


        if (isXXTEA) { // decrypt XXTEA
            xxtea_long len = 0;
            buffer = xxtea_decrypt(bytes + stack->_xxteaSignLen,
                                   (xxtea_long)size - (xxtea_long)stack->_xxteaSignLen,
                                   (unsigned char*)stack->_xxteaKey,
                                   (xxtea_long)stack->_xxteaKeyLen,
                                   &len);
            zip = ZipFile::createWithBuffer(buffer, len);
        } else {
            if (size > 0) {
                zip = ZipFile::createWithBuffer(bytes, (unsigned long)size);
            }
        }

        if (zip) {
            CCLOG("lua_loadChunksFromZIP() - load zip file: %s%s", zipFilePath.c_str(), isXXTEA ? "*" : "");
            lua_getglobal(L, "package");
            lua_getfield(L, -1, "preload");

            int count = 0;
            std::string filename = zip->getFirstFilename();
            while (filename.length()) {
                ssize_t bufferSize = 0;
                unsigned char *zbuffer = zip->getFileData(filename.c_str(), &bufferSize);
                if (bufferSize) {
                    // remove .lua or .luac extension
                    size_t pos = filename.find_last_of('.');
                    if (pos != std::string::npos)
                    {
                        std::string suffix = filename.substr(pos, filename.length());
                        if (suffix == NOT_BYTECODE_FILE_EXT || suffix == BYTECODE_FILE_EXT) {
                            filename.erase(pos);
                        }
                    }
                    // replace path separator '/' '\' to '.'
                    for (int i=0; i<filename.size(); i++) {
                        if (filename[i] == '/' || filename[i] == '\\') {
                            filename[i] = '.';
                        }
                    }
                    CCLOG("[luaLoadChunksFromZIP] add %s to preload", filename.c_str());
                    if (stack->luaLoadBuffer(L, (char*)zbuffer, (int)bufferSize, filename.c_str()) == 0) {
                        lua_setfield(L, -2, filename.c_str());
                        ++count;
                    }
                    free(zbuffer);
                }
                filename = zip->getNextFilename();
            }
            CCLOG("lua_loadChunksFromZIP() - loaded chunks count: %d", count);
            lua_pop(L, 2);
            lua_pushboolean(L, 1);

            delete zip;
        } else {
            CCLOG("lua_loadChunksFromZIP() - not found or invalid zip file: %s", zipFilePath.c_str());
            lua_pushboolean(L, 0);
        }


        if (buffer) {
            free(buffer);
        }
    } while (0);

    return 1;
}
Ejemplo n.º 13
0
SSAPMessage KPMqtt::sendEncrypt(char* msgJson, unsigned char* key, int keyLength){
	
	//The server does not need a topic
	//char* msgJson=msg->toJson();
	//Serial.println(msgJson);
	

	xxtea_long len=strlen(msgJson);
	xxtea_long encryptedLength=0;
	
	unsigned char *result=new unsigned char[690];
	
	//Serial.println("Antes de encriptar");
	unsigned char* encryptedMsg = xxtea_encrypt((unsigned char *)msgJson, len, key, keyLength, &encryptedLength, result);
	//Serial.print("Despues de encriptar, Longitud: ");
	//Serial.println(encryptedLength);
	//Codificado en Base64, ya no es necesario con los cambios hechos en la libreria
	
	delete[] msgJson;
	
	//Serial.print("Memoria antes de codificar a base64: ");
	//Serial.println(memoryTest());
	
	char* encodedOutput=new char[690];
	int codedLength = base64_encode(encodedOutput, reinterpret_cast<char*>(encryptedMsg), encryptedLength);
	//Serial.println("Despues de codificar, Longitud: ");
	//Serial.println(codedLength);
	
	//Serial.print("Memoria despues de codificar a base64: ");
	//Serial.println(memoryTest());
	
	delete[] encryptedMsg;
	
	char* adjustedEncodedOutput=new char[codedLength+1];
	for(int i=0;i<codedLength;i++){
		adjustedEncodedOutput[i]=encodedOutput[i];
	}
	adjustedEncodedOutput[codedLength]='\0';
	
	delete[] encodedOutput;
	
	//Serial.println("Antes de publicar");
	client.publish("", (uint8_t*)adjustedEncodedOutput, codedLength, false);
	//Serial.println("Despues de publicar");
	delete[] adjustedEncodedOutput;
	
	publishResponse=NULL;
	
	
	for(int i=0; i<MAX_RESPONSE_LOOP_RETRIES && publishResponse==NULL;i++){
		delay(1000);
		client.loop();
		//Serial.println("Esperando respuesta");
	}
	
	//Serial.println("Antes de comprobar el nulo:");
	//Serial.println(publishResponse);
	
	//Hasta aqui no se pierde memoria en memory leaks
	if(publishResponse!=NULL){
		char* decodedOutput=new char[690];
		int decodedLenght = base64_decode(decodedOutput, publishResponse, strlen(publishResponse));
		delete[] publishResponse;
		
		
		Serial.print("Longitud decodificado");
		Serial.println(decodedLenght);
		Serial.println("##################################");
		unsigned char* adjustedDecodedOutput=new unsigned char[decodedLenght];
		for(int i=0;i<decodedLenght;i++){
			adjustedDecodedOutput[i]=(unsigned char)decodedOutput[i];
			Serial.print((int) adjustedDecodedOutput[i]);
		}
		Serial.println("\n##################################");
	
		delete[] decodedOutput;
		
		
		xxtea_long *ret_len;
		Serial.println("Antes de descifrar");
		//intentar eso, pasar el argumento del resultado por parametros, y que se declare antes para que no lo haga el malloc
		unsigned char *result2=new unsigned char[690];
		unsigned char * decryptedMsg = xxtea_decrypt(adjustedDecodedOutput, decodedLenght, key, keyLength, ret_len, result2);
		/*Serial.println("Parte del desencriptado");
		for(int i=0;i<100;i++){
			Serial.print((char)result2[i]);
		}
		Serial.println("Parte del desencriptado");*/
		
	/*	Serial.println("Despues de descifrar");

		Serial.println("Longitud Respuesta descifrada: ");
		Serial.println((int)ret_len);
		
		Serial.println("Respuesta descifrada: ");
		Serial.println((char*)decryptedMsg);
		
		delete[] adjustedDecodedOutput;*/
		
		/*
		SSAPMessage response = SSAPMessage::fromJSonToSSAPMessage((char*)decryptedMsg);
		delete[] decryptedMsg;
		
		if(response.getMessageType()==JOIN){
			char* responseBody=response.getBody();//Body lo borro en la funcion principal del programa
			SSAPBodyReturnMessage bodyMessage=SSAPBodyReturnMessage::fromJSonToSSAPMessage(responseBody);
			if(bodyMessage.isOk()){
				delete[] sessionKey;
				sessionKey=new char[strlen(bodyMessage.getData())+1];
				strcpy(sessionKey, bodyMessage.getData());
			}
			
			delete[] bodyMessage.getData();
			delete[] bodyMessage.getError();
		}
		return response;*/
	}

}
Ejemplo n.º 14
0
int LuaStack::lua_loadChunksFromZIP(lua_State *L)
{
    if (lua_gettop(L) < 1) {
        CCLOG("lua_loadChunksFromZIP() - invalid arguments");
        return 0;
    }
    
    const char *zipFilename = lua_tostring(L, -1);
    lua_settop(L, 0);
    FileUtils *utils = FileUtils::getInstance();
    std::string zipFilePath = utils->fullPathForFilename(zipFilename);
    
    LuaStack *stack = curStack;
    
    do {
        ssize_t size = 0;
        void *buffer = NULL;
        unsigned char *zipFileData = utils->getFileData(zipFilePath.c_str(), "rb", &size);
        ZipFile *zip = NULL;
        
        bool isXXTEA = stack && stack->_xxteaEnabled && zipFileData;
        for (int i = 0; isXXTEA && i < stack->_xxteaSignLen && i < size; ++i) {
            isXXTEA = zipFileData[i] == stack->_xxteaSign[i];
        }
        
        if (isXXTEA) { // decrypt XXTEA
            xxtea_long len = 0;
            buffer = xxtea_decrypt(zipFileData + stack->_xxteaSignLen,
                                   (xxtea_long)size - (xxtea_long)stack->_xxteaSignLen,
                                   (unsigned char*)stack->_xxteaKey,
                                   (xxtea_long)stack->_xxteaKeyLen,
                                   &len);
            delete []zipFileData;
            zipFileData = NULL;
            zip = ZipFile::createWithBuffer(buffer, len);
        } else {
            if (zipFileData) {
                zip = ZipFile::createWithBuffer(zipFileData, size);
            }
        }
        
        if (zip) {
            CCLOG("lua_loadChunksFromZIP() - load zip file: %s%s", zipFilePath.c_str(), isXXTEA ? "*" : "");
            lua_getglobal(L, "package");
            lua_getfield(L, -1, "preload");
            
            int count = 0;
            std::string filename = zip->getFirstFilename();
            while (filename.length()) {
                ssize_t bufferSize = 0;
                unsigned char *zbuffer = zip->getFileData(filename.c_str(), &bufferSize);
                if (bufferSize) {
                    if (stack->luaLoadBuffer(L, (char*)zbuffer, (int)bufferSize, filename.c_str()) == 0) {
                        lua_setfield(L, -2, filename.c_str());
                        ++count;
                    }
                    delete []zbuffer;
                }
                filename = zip->getNextFilename();
            }
            CCLOG("lua_loadChunksFromZIP() - loaded chunks count: %d", count);
            lua_pop(L, 2);
            lua_pushboolean(L, 1);
            
            delete zip;
        } else {
            CCLOG("lua_loadChunksFromZIP() - not found or invalid zip file: %s", zipFilePath.c_str());
            lua_pushboolean(L, 0);
        }
        
        if (zipFileData) {
            delete []zipFileData;
        }
        
        if (buffer) {
            free(buffer);
        }
    } while (0);
    
    return 1;
}