From 24db45e9253df0a6cef51bbd1d89d624dd148196 Mon Sep 17 00:00:00 2001 From: xpol Date: Mon, 18 Apr 2016 16:35:50 +0800 Subject: [PATCH] Fixes some warnings in Lua manual bindings. (#15443) 1. Use __Type for deprecated classes. 2. Refactored LuaStack::luaLoadChunksFromZIP() and use FileUtils:getDataFromFile(). 3. Fixes wrongly compare NULL with char array. --- .../lua-bindings/manual/CCLuaEngine.cpp | 4 +-- .../lua-bindings/manual/CCLuaStack.cpp | 26 ++++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/cocos/scripting/lua-bindings/manual/CCLuaEngine.cpp b/cocos/scripting/lua-bindings/manual/CCLuaEngine.cpp index 30a5e15a49..7da8359e11 100644 --- a/cocos/scripting/lua-bindings/manual/CCLuaEngine.cpp +++ b/cocos/scripting/lua-bindings/manual/CCLuaEngine.cpp @@ -439,13 +439,13 @@ int LuaEngine::handleCommonEvent(void* data) return 0; CommonScriptData* commonInfo = static_cast(data); - if (NULL == commonInfo->eventName || 0 == commonInfo->handler) + if ('\0' == commonInfo->eventName[0] || 0 == commonInfo->handler) return 0; _stack->pushString(commonInfo->eventName); if (NULL != commonInfo->eventSource) { - if (NULL != commonInfo->eventSourceClassName && strlen(commonInfo->eventSourceClassName) > 0) + if ('\0' != commonInfo->eventSourceClassName[0]) { _stack->pushObject(commonInfo->eventSource, commonInfo->eventSourceClassName); } diff --git a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp index 056e88daba..3e56bbf6bd 100644 --- a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp +++ b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp @@ -25,6 +25,7 @@ #include "scripting/lua-bindings/manual/CCLuaStack.h" #include "scripting/lua-bindings/manual/tolua_fix.h" +#include #include "external/xxtea/xxtea.h" extern "C" { #include "lua.h" @@ -623,7 +624,7 @@ int LuaStack::executeFunctionReturnArray(int handler,int numArgs,int numResults, if (lua_type(_state, -1) == LUA_TBOOLEAN) { bool value = lua_toboolean(_state, -1); - resultArray.addObject(__Bool::create(value)) ; + resultArray.addObject(__Bool::create(value)); }else if (lua_type(_state, -1) == LUA_TNUMBER) { @@ -808,29 +809,27 @@ int LuaStack::luaLoadChunksFromZIP(lua_State *L) LuaStack *stack = this; do { - ssize_t size = 0; void *buffer = nullptr; - unsigned char *zipFileData = utils->getFileData(zipFilePath.c_str(), "rb", &size); ZipFile *zip = nullptr; + Data zipFileData(utils->getDataFromFile(zipFilePath)); + unsigned char* bytes = zipFileData.getBytes(); + ssize_t size = zipFileData.getSize(); - bool isXXTEA = stack && stack->_xxteaEnabled && zipFileData; - for (int i = 0; isXXTEA && i < stack->_xxteaSignLen && i < size; ++i) { - isXXTEA = zipFileData[i] == stack->_xxteaSign[i]; - } + 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(zipFileData + stack->_xxteaSignLen, + buffer = xxtea_decrypt(bytes + 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 (size > 0) { + zip = ZipFile::createWithBuffer(bytes, (unsigned long)size); } } @@ -879,9 +878,6 @@ int LuaStack::luaLoadChunksFromZIP(lua_State *L) lua_pushboolean(L, 0); } - if (zipFileData) { - free(zipFileData); - } if (buffer) { free(buffer);