Add xxtea encrypt support for lua and update the console submodule

This commit is contained in:
samuele3hu 2014-06-03 09:55:57 +08:00
parent 36ef9d7dd7
commit b91d8255bd
7 changed files with 392 additions and 9 deletions

View File

@ -56,6 +56,10 @@
06CAAAD0186AD7FE0012A414 /* TriggerBase.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 06CAAABC186AD63B0012A414 /* TriggerBase.cpp */; };
06CAAAD1186AD8010012A414 /* ObjectFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 06CAAABB186AD63B0012A414 /* ObjectFactory.h */; };
06CAAAD2186AD8030012A414 /* ObjectFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 06CAAABB186AD63B0012A414 /* ObjectFactory.h */; };
1594D8D51935967C00F829CA /* xxtea.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1594D8D31935967C00F829CA /* xxtea.cpp */; };
1594D8D61935967C00F829CA /* xxtea.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1594D8D31935967C00F829CA /* xxtea.cpp */; };
1594D8D71935967C00F829CA /* xxtea.h in Headers */ = {isa = PBXBuildFile; fileRef = 1594D8D41935967C00F829CA /* xxtea.h */; };
1594D8D81935967C00F829CA /* xxtea.h in Headers */ = {isa = PBXBuildFile; fileRef = 1594D8D41935967C00F829CA /* xxtea.h */; };
1A01C68418F57BE800EFE3A6 /* CCArray.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A01C67618F57BE800EFE3A6 /* CCArray.cpp */; };
1A01C68518F57BE800EFE3A6 /* CCArray.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A01C67618F57BE800EFE3A6 /* CCArray.cpp */; };
1A01C68618F57BE800EFE3A6 /* CCArray.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A01C67718F57BE800EFE3A6 /* CCArray.h */; };
@ -1879,6 +1883,8 @@
06CAAAC1186AD63B0012A414 /* TriggerObj.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TriggerObj.h; sourceTree = "<group>"; };
1551A33F158F2AB200E66CFE /* libcocos2dx Mac.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libcocos2dx Mac.a"; sourceTree = BUILT_PRODUCTS_DIR; };
1551A342158F2AB200E66CFE /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1594D8D31935967C00F829CA /* xxtea.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = xxtea.cpp; path = ../base/xxtea.cpp; sourceTree = "<group>"; };
1594D8D41935967C00F829CA /* xxtea.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = xxtea.h; path = ../base/xxtea.h; sourceTree = "<group>"; };
1A01C67618F57BE800EFE3A6 /* CCArray.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCArray.cpp; sourceTree = "<group>"; };
1A01C67718F57BE800EFE3A6 /* CCArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCArray.h; sourceTree = "<group>"; };
1A01C67818F57BE800EFE3A6 /* CCBool.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCBool.h; sourceTree = "<group>"; };
@ -3043,6 +3049,8 @@
1A5700A2180BC5E60088DEC7 /* base */ = {
isa = PBXGroup;
children = (
1594D8D31935967C00F829CA /* xxtea.cpp */,
1594D8D41935967C00F829CA /* xxtea.h */,
50ABBDC11925AB6E00A911A9 /* atitc.cpp */,
50ABBDC21925AB6E00A911A9 /* atitc.h */,
50ABBDC31925AB6E00A911A9 /* base64.cpp */,
@ -4928,6 +4936,7 @@
5034CA47191D591100CE6051 /* ccShader_Label_normal.frag in Headers */,
1A5701B7180BCB5A0088DEC7 /* CCFontFreeType.h in Headers */,
1A5701BB180BCB5A0088DEC7 /* CCLabel.h in Headers */,
1594D8D71935967C00F829CA /* xxtea.h in Headers */,
1A5701BF180BCB5A0088DEC7 /* CCLabelAtlas.h in Headers */,
50ABBED91925AB6F00A911A9 /* ZipUtils.h in Headers */,
1A5701C3180BCB5A0088DEC7 /* CCLabelBMFont.h in Headers */,
@ -5580,6 +5589,7 @@
1AD71EA4180E26E600808F54 /* Atlas.h in Headers */,
1AD71EA8180E26E600808F54 /* AtlasAttachmentLoader.h in Headers */,
1ABA68B11888D700007D1BB4 /* CCFontCharMap.h in Headers */,
1594D8D81935967C00F829CA /* xxtea.h in Headers */,
50ABBD4F1925AB0000A911A9 /* MathUtil.h in Headers */,
1AD71EAC180E26E600808F54 /* Attachment.h in Headers */,
2905FA4918CF08D100240AA3 /* UIButton.h in Headers */,
@ -6248,6 +6258,7 @@
50E6D33C18E174130051CA34 /* UIVBox.cpp in Sources */,
1AD71EC9180E26E600808F54 /* RegionAttachment.cpp in Sources */,
50E6D33818E174130051CA34 /* UIRelativeBox.cpp in Sources */,
1594D8D51935967C00F829CA /* xxtea.cpp in Sources */,
1AD71ECD180E26E600808F54 /* Skeleton.cpp in Sources */,
1AD71ED1180E26E600808F54 /* SkeletonData.cpp in Sources */,
1AD71ED5180E26E600808F54 /* SkeletonJson.cpp in Sources */,
@ -6733,6 +6744,7 @@
50ABC0001926664800A911A9 /* CCFileUtilsApple.mm in Sources */,
2905FA7918CF08D100240AA3 /* UISlider.cpp in Sources */,
1A8C59A4180E930E00EF57C3 /* CCArmatureAnimation.cpp in Sources */,
1594D8D61935967C00F829CA /* xxtea.cpp in Sources */,
1A8C59A8180E930E00EF57C3 /* CCArmatureDataManager.cpp in Sources */,
2905FA8918CF08D100240AA3 /* UITextField.cpp in Sources */,
1A8C59AC180E930E00EF57C3 /* CCArmatureDefine.cpp in Sources */,

176
cocos/base/xxtea.cpp Normal file
View File

@ -0,0 +1,176 @@
/***********************************************************************
Copyright 2006-2009 Ma Bingyao
Copyright 2013 Gao Chunhui, Liu Tao
These sources is free software. Redistributions of source code must
retain the above copyright notice. Redistributions in binary form
must reproduce the above copyright notice. You can redistribute it
freely. You can use it with any free or commercial software.
These sources is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. Without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
github: https://github.com/liut/pecl-xxtea
*************************************************************************/
#include "xxtea.h"
#include <memory.h>
#include <stdlib.h>
static void xxtea_long_encrypt(xxtea_long *v, xxtea_long len, xxtea_long *k)
{
xxtea_long n = len - 1;
xxtea_long z = v[n], y = v[0], p, q = 6 + 52 / (n + 1), sum = 0, e;
if (n < 1) {
return;
}
while (0 < q--) {
sum += XXTEA_DELTA;
e = sum >> 2 & 3;
for (p = 0; p < n; p++) {
y = v[p + 1];
z = v[p] += XXTEA_MX;
}
y = v[0];
z = v[n] += XXTEA_MX;
}
}
static void xxtea_long_decrypt(xxtea_long *v, xxtea_long len, xxtea_long *k)
{
xxtea_long n = len - 1;
xxtea_long z = v[n], y = v[0], p, q = 6 + 52 / (n + 1), sum = q * XXTEA_DELTA, e;
if (n < 1) {
return;
}
while (sum != 0) {
e = sum >> 2 & 3;
for (p = n; p > 0; p--) {
z = v[p - 1];
y = v[p] -= XXTEA_MX;
}
z = v[n];
y = v[0] -= XXTEA_MX;
sum -= XXTEA_DELTA;
}
}
static unsigned char *fix_key_length(unsigned char *key, xxtea_long key_len)
{
unsigned char *tmp = (unsigned char *)malloc(16);
memcpy(tmp, key, key_len);
memset(tmp + key_len, '\0', 16 - key_len);
return tmp;
}
static xxtea_long *xxtea_to_long_array(unsigned char *data, xxtea_long len, int include_length, xxtea_long *ret_len) {
xxtea_long i, n, *result;
n = len >> 2;
n = (((len & 3) == 0) ? n : n + 1);
if (include_length) {
result = (xxtea_long *)malloc((n + 1) << 2);
result[n] = len;
*ret_len = n + 1;
} else {
result = (xxtea_long *)malloc(n << 2);
*ret_len = n;
}
memset(result, 0, n << 2);
for (i = 0; i < len; i++) {
result[i >> 2] |= (xxtea_long)data[i] << ((i & 3) << 3);
}
return result;
}
static unsigned char *xxtea_to_byte_array(xxtea_long *data, xxtea_long len, int include_length, xxtea_long *ret_len) {
xxtea_long i, n, m;
unsigned char *result;
n = len << 2;
if (include_length) {
m = data[len - 1];
if ((m < n - 7) || (m > n - 4)) return NULL;
n = m;
}
result = (unsigned char *)malloc(n + 1);
for (i = 0; i < n; i++) {
result[i] = (unsigned char)((data[i >> 2] >> ((i & 3) << 3)) & 0xff);
}
result[n] = '\0';
*ret_len = n;
return result;
}
static unsigned char *do_xxtea_encrypt(unsigned char *data, xxtea_long len, unsigned char *key, xxtea_long *ret_len) {
unsigned char *result;
xxtea_long *v, *k, v_len, k_len;
v = xxtea_to_long_array(data, len, 1, &v_len);
k = xxtea_to_long_array(key, 16, 0, &k_len);
xxtea_long_encrypt(v, v_len, k);
result = xxtea_to_byte_array(v, v_len, 0, ret_len);
free(v);
free(k);
return result;
}
static unsigned char *do_xxtea_decrypt(unsigned char *data, xxtea_long len, unsigned char *key, xxtea_long *ret_len) {
unsigned char *result;
xxtea_long *v, *k, v_len, k_len;
v = xxtea_to_long_array(data, len, 0, &v_len);
k = xxtea_to_long_array(key, 16, 0, &k_len);
xxtea_long_decrypt(v, v_len, k);
result = xxtea_to_byte_array(v, v_len, 1, ret_len);
free(v);
free(k);
return result;
}
unsigned char *xxtea_encrypt(unsigned char *data, xxtea_long data_len, unsigned char *key, xxtea_long key_len, xxtea_long *ret_length)
{
unsigned char *result;
*ret_length = 0;
if (key_len < 16) {
unsigned char *key2 = fix_key_length(key, key_len);
result = do_xxtea_encrypt(data, data_len, key2, ret_length);
free(key2);
}
else
{
result = do_xxtea_encrypt(data, data_len, key, ret_length);
}
return result;
}
unsigned char *xxtea_decrypt(unsigned char *data, xxtea_long data_len, unsigned char *key, xxtea_long key_len, xxtea_long *ret_length)
{
unsigned char *result;
*ret_length = 0;
if (key_len < 16) {
unsigned char *key2 = fix_key_length(key, key_len);
result = do_xxtea_decrypt(data, data_len, key2, ret_length);
free(key2);
}
else
{
result = do_xxtea_decrypt(data, data_len, key, ret_length);
}
return result;
}
/* }}} */

47
cocos/base/xxtea.h Normal file
View File

@ -0,0 +1,47 @@
/***********************************************************************
Copyright 2006-2009 Ma Bingyao
Copyright 2013 Gao Chunhui, Liu Tao
These sources is free software. Redistributions of source code must
retain the above copyright notice. Redistributions in binary form
must reproduce the above copyright notice. You can redistribute it
freely. You can use it with any free or commercial software.
These sources is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. Without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
github: https://github.com/liut/pecl-xxtea
*************************************************************************/
#ifndef XXTEA_H
#define XXTEA_H
#include <stddef.h> /* for size_t & NULL declarations */
#if defined(_MSC_VER)
typedef unsigned __int32 xxtea_long;
#else
#if defined(__FreeBSD__) && __FreeBSD__ < 5
/* FreeBSD 4 doesn't have stdint.h file */
#include <inttypes.h>
#else
#include <stdint.h>
#endif
typedef uint32_t xxtea_long;
#endif /* end of if defined(_MSC_VER) */
#define XXTEA_MX (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z)
#define XXTEA_DELTA 0x9e3779b9
unsigned char *xxtea_encrypt(unsigned char *data, xxtea_long data_len, unsigned char *key, xxtea_long key_len, xxtea_long *ret_length);
unsigned char *xxtea_decrypt(unsigned char *data, xxtea_long data_len, unsigned char *key, xxtea_long key_len, xxtea_long *ret_length);
#endif

View File

@ -25,6 +25,7 @@
#include "CCLuaStack.h"
#include "tolua_fix.h"
#include "base/xxtea.h"
extern "C" {
#include "lua.h"
#include "tolua++.h"
@ -685,4 +686,88 @@ int LuaStack::reload(const char* moduleFileName)
return executeString(require.c_str());
}
void LuaStack::setXXTEAKeyAndSign(const char *key, int keyLen, const char *sign, int signLen)
{
cleanupXXTEAKeyAndSign();
if (key && keyLen && sign && signLen)
{
_xxteaKey = (char*)malloc(keyLen);
memcpy(_xxteaKey, key, keyLen);
_xxteaKeyLen = keyLen;
_xxteaSign = (char*)malloc(signLen);
memcpy(_xxteaSign, sign, signLen);
_xxteaSignLen = signLen;
_xxteaEnabled = true;
}
else
{
_xxteaEnabled = false;
}
}
void LuaStack::cleanupXXTEAKeyAndSign(void)
{
if (_xxteaKey)
{
free(_xxteaKey);
_xxteaKey = nullptr;
_xxteaKeyLen = 0;
}
if (_xxteaSign)
{
free(_xxteaSign);
_xxteaSign = nullptr;
_xxteaSignLen = 0;
}
}
int LuaStack::lua_loadbuffer(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;
}
NS_CC_END

View File

@ -129,10 +129,20 @@ public:
virtual bool handleAssert(const char *msg);
virtual void setXXTEAKeyAndSign(const char *key, int keyLen, const char *sign, int signLen);
virtual void cleanupXXTEAKeyAndSign(void);
int lua_loadbuffer(lua_State *L, const char *chunk, int chunkSize, const char *chunkName);
protected:
LuaStack(void)
: _state(nullptr)
, _callFromLua(0)
, _xxteaEnabled(false)
, _xxteaKey(nullptr)
, _xxteaKeyLen(0)
, _xxteaSign(nullptr)
, _xxteaSignLen(0)
{
}
@ -141,6 +151,11 @@ protected:
lua_State *_state;
int _callFromLua;
bool _xxteaEnabled;
char* _xxteaKey;
int _xxteaKeyLen;
char* _xxteaSign;
int _xxteaSignLen;
};
NS_CC_END

View File

@ -26,14 +26,20 @@ THE SOFTWARE.
#include <string>
#include <algorithm>
#include "CCLuaStack.h"
#include "CCLuaEngine.h"
using namespace cocos2d;
extern "C"
{
int cocos2dx_lua_loader(lua_State *L)
{
static const std::string BYTE_FILE_EXT = ".luac";
static const std::string NO_BYTE_FILE_EXT = ".lua";
std::string filename(luaL_checkstring(L, 1));
size_t pos = filename.rfind(".lua");
size_t pos = filename.rfind(NO_BYTE_FILE_EXT);
if (pos != std::string::npos)
{
filename = filename.substr(0, pos);
@ -45,21 +51,63 @@ extern "C"
filename.replace(pos, 1, "/");
pos = filename.find_first_of(".");
}
filename.append(".lua");
//filename.append(".lua");
Data data = FileUtils::getInstance()->getDataFromFile(filename);
// search file in package.path
unsigned char* chunk = NULL;
ssize_t chunkSize = 0;
std::string chunkName;
FileUtils* utils = FileUtils::getInstance();
if (!data.isNull())
lua_getglobal(L, "package");
lua_getfield(L, -1, "path");
std::string searchpath(lua_tostring(L, -1));
lua_pop(L, 1);
size_t begin = 0;
size_t next = searchpath.find_first_of(";", 0);
do
{
if (luaL_loadbuffer(L, (char*)data.getBytes(), data.getSize(), filename.c_str()) != 0)
if (next == std::string::npos) next = searchpath.length();
std::string prefix = searchpath.substr(begin, next);
if (prefix[0] == '.' && prefix[1] == '/')
{
luaL_error(L, "error loading module %s from file %s :\n\t%s",
lua_tostring(L, 1), filename.c_str(), lua_tostring(L, -1));
prefix = prefix.substr(2);
}
pos = prefix.find("?.lua");
chunkName = prefix.substr(0, pos) + filename + BYTE_FILE_EXT;
chunkName = utils->fullPathForFilename(chunkName.c_str());
if (utils->isFileExist(chunkName))
{
chunk = utils->getFileData(chunkName.c_str(), "rb", &chunkSize);
break;
}
else
{
chunkName = prefix.substr(0, pos) + filename + NO_BYTE_FILE_EXT;
chunkName = utils->fullPathForFilename(chunkName.c_str());
if (utils->isFileExist(chunkName))
{
chunk = utils->getFileData(chunkName.c_str(), "rb", &chunkSize);
break;
}
}
begin = next + 1;
next = searchpath.find_first_of(";", begin);
} while (begin < (int)searchpath.length());
if (chunk)
{
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
stack->lua_loadbuffer(L, (char*)chunk, (int)chunkSize, chunkName.c_str());
delete []chunk;
}
else
{
log("can not get file data of %s", filename.c_str());
log("can not get file data of %s", chunkName.c_str());
return 0;
}
return 1;

@ -1 +1 @@
Subproject commit 29cfa00a4b85f934eefe0b2fae460bd9a55ee345
Subproject commit 8a58f5a5f93896f1a15577c76a69e57f99f9bd53