require 实现

  • require函数在实现上是依次调用package.searchers(lua51中是package.loaders)中的载入函数,成功后返回。在loadlib.c文件里有四个载入函数的实现,分别为searcher_preload, searcher_Lua, searcher_C, searcher_Croot。
  • searcher_preload是从读取LUA_REGISTRYINDEX的_PRELOAD字段。已经require过的文件会写入到该表中
  • searcher_Lua是依据文件名称查找package.path中的全部路径的lua文件。存在文件则返回
  • searcher_C是搜索package.cpath中的全部路径下的库文件
  • searcher_Croot是对require(“a.b.c”)的情况,读取a库。然后查找函数名为lua_a_b_c的lua_CFunction函数
static void findloader (lua_State *L, const char *name) {
int i;
luaL_Buffer msg; /* to build error message */
luaL_buffinit(L, &msg);
lua_getfield(L, lua_upvalueindex(1), "searchers"); /* will be at index 3 */
if (!lua_istable(L, 3))
luaL_error(L, LUA_QL("package.searchers") " must be a table");
/* iterate over available searchers to find a loader */
for (i = 1; ; i++) {
lua_rawgeti(L, 3, i); /* get a searcher */
if (lua_isnil(L, -1)) { /* no more searchers? */
lua_pop(L, 1); /* remove nil */
luaL_pushresult(&msg); /* create error message */
luaL_error(L, "module " LUA_QS " not found:%s",
name, lua_tostring(L, -1));
}
lua_pushstring(L, name);
lua_call(L, 1, 2); /* call it */
if (lua_isfunction(L, -2)) /* did it find a loader? */
return; /* module loader found */
else if (lua_isstring(L, -2)) { /* searcher returned error message? */
lua_pop(L, 1); /* remove extra return */
luaL_addvalue(&msg); /* concatenate error message */
}
else
lua_pop(L, 2); /* remove both returns */
}
}
  • 如今要实如今require时能读取加密文件,有两种办法,一种是直接改动源码。即改动第二个载入函数,又一次实现当中读取文件内容的函数,另外一种办法是在lua中改动package.searchers表。在载入器的第一和另外一种之间加入一个载入器函数,该载入器模拟searcher_Lua函数。搜索path路径,然后逐个匹配文件。然后读取文件内容,解密。然后调用load载入并返回(c中为luaL_loadbufferx),这里在载入时最好传入文件名称作为来源參数。方便在调试信息中定位.
  • 加密方案可使用相似xxtea轻量级的加密算法
  • 在对lua文件进行加密打包时。能够在文件头写入指定的签名内容。以方便在解密前预先推断是否为有效的加密文件

改动lua源码方案

  • 在searcher_Lua中终于是调用lua_load(L, getF, &lf, lua_tostring(L, -1), mode)载入源文件,该函数的第二个參数getF是一个lua_Reader函数,所以这里能够重写该函数以实现解密,也能够向外部暴露一个接口用来将自己定义的文件读取函数作为參数传给lua_load。以下是原版的getF实现
static const char *getF (lua_State *L, void *ud, size_t *size) {
LoadF *lf = (LoadF *)ud;
(void)L; /* not used */
if (lf->n > 0) { /* are there pre-read characters to be read? */
*size = lf->n; /* return them (chars already in buffer) */
lf->n = 0; /* no more pre-read characters */
}
else { /* read a block from file */
/* 'fread' can return > 0 *and* set the EOF flag. If next call to
'getF' called 'fread', it might still wait for user input.
The next check avoids this problem. */
if (feof(lf->f)) return NULL;
*size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */
}
return lf->buff;
}

外部改动载入器方案

  • 直接改动package.searchers表,向当中加入载入器,c版实现例如以下
void addLuaSearcher(lua_CFunction func)
{
if (!func) return; // stack content after the invoking of the function
// get loader table
lua_getglobal(m_state, "package"); /* L: package */
lua_getfield(m_state, -1, "loaders"); /* L: package, loaders */
// insert loader into index 2
lua_pushcfunction(m_state, func); /* L: package, loaders, func */
for (int i = lua_objlen(m_state, -2) + 1; i > 2; --i)
{
lua_rawgeti(m_state, -2, i - 1); /* L: package, loaders, func, function */
// we call lua_rawgeti, so the loader table now is at -3
lua_rawseti(m_state, -3, i); /* L: package, loaders, func */
}
lua_rawseti(m_state, -2, 2); /* L: package, loaders */
// set loaders into package
lua_setfield(m_state, -2, "loaders"); /* L: package */
lua_pop(m_state, 1);
}
  • 载入器函数实现依据传入的文件名称。逐个匹配的package.path中的内容,存在文件后,然后读取文件内容,解密,最后再将解出的内容调用load载入并返回(c中为luaL_loadbufferx),实现能够參照lua源码中的searcher_Lua实现

lua 代码加密方案的更多相关文章

  1. cocos对lua代码加密

    1.0 cocos luacompile 用法 我用的普通的cocos2d lua,没用quick,quick好像可以对整个资源包括图像和音频都加密,打包成zip.但我没用quick.看了下luaco ...

  2. cocos2d 3.3 lua 代码加密 luac

    1.0 cocos luacompile 使用方法 我用的普通的cocos2d lua,没用quick,quick好像能够对整个资源包含图像和音频都加密,打包成zip.我看了下luacompile 的 ...

  3. 【Web技术】399- 浅谈前端代码加密

    作者简介:于航,PayPal Senior Software Engineer,在 PayPal 上海负责 Global GRT 平台相关的技术研发工作.曾任职于阿里巴巴.Tapatalk 等企业.f ...

  4. cocos lua 加密方案

    cocos2d使用的是luajit,lua原生编译出来的bytecode和luajit是不兼容的,所以直接用luac法编译出来的bytecode脚本无法在cocos2d中使用. 目前所指的解决方案有2 ...

  5. 如何保护你的 Python 代码 (一)—— 现有加密方案

    https://zhuanlan.zhihu.com/p/54296517 0 前言 去年11月在PyCon China 2018 杭州站分享了 Python 源码加密,讲述了如何通过修改 Pytho ...

  6. [转帖]如何保护你的 Python 代码 (一)—— 现有加密方案

    如何保护你的 Python 代码 (一)—— 现有加密方案 Prodesire Python猫 1周前

  7. [自动化-脚本]002.cocos2dx-lua lua代码windows加密批处理

    在开发软件的时候,我们都会在项目上线时候对代码进行加密,用来防止被不法分子盗走牟利.不同的语言有不同的加密方式,比较出名的有加壳,代码混淆等.在Lua开发cocos2dx的时候,框架会有提供加密的脚本 ...

  8. 关于cocos2dx手游lua文件加密的解决方式

    非常多使用cocos2dx+lua做游戏的同学.都会想到一个问题,我的游戏一旦公布,如何才干保证的我脚本代码不被破解.不泄露代码.尽管这和开源.共享的原则不合.可是代码也是coder的劳动成果,理应得 ...

  9. iOS代码加密常用加密方式

    iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...

随机推荐

  1. [翻译] LASIImageView - 显示进度指示并异步下载图片

      LASIImageView – download image with progress indicator 翻译原网址:http://lukagabric.com/lasiimageview-d ...

  2. JAVA常见算法题(三十三)---求子串在字符串中出现的次数

    计算某字符串中子串出现的次数. public static void main(String[] args) { String s1 = "adcdcjncdfbcdcdcd"; ...

  3. C# 编程指南

    此部分详细介绍了 C# 语言主要功能,以及通过 .NET Framework 可以在 C# 中使用的功能. 阅读此部分的大部分内容的前提是,你已对 C# 和一般编程概念有一定的了解. 如果完全没有接触 ...

  4. 用Python语言写Hadoop MapReduce程序Writing an Hadoop MapReduce Program in Python

    In this tutorial I will describe how to write a simple MapReduce program for Hadoop in the Python pr ...

  5. 计算Fisher vector和VLAD

    This short tutorial shows how to compute Fisher vector and VLAD encodings with VLFeat MATLAB interfa ...

  6. 认识Mac中的那些符号

    今天看到这篇文章,讲了Mac中的各种符号,还是很不错的. http://www.cnblogs.com/jimcheng/articles/4156268.html

  7. Tomcat安装笔记(on Mac)

    1. 官网 http://tomcat.apache.org/ 下载apache包,我下的8.5 注意要下core包的tgz版本,我开始下了full doc. 2. 拷贝解压到 /Library, 然 ...

  8. 第十四章 springboot + profile(不同环境读取不同配置)

    具体做法: 不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中:prod环境下的配置配置在application-prod.prope ...

  9. AngularJs 阻止事件运行,防止冒泡穿透事件

    ng-click 低啊用方法后 添加语句$event.stopPropagation(); <button type="button" ng-click="doSo ...

  10. android 中的 window,view,activity具体关系

    通过讨论这个问题,我们能够见识到google是对面向对象模式的理解,能够理解android底层的一些调用.这也是一道很常见的面试题. 我们这篇文章就来解决这四个问题: Android  中view的显 ...