lua 代码加密方案

require 实现*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, ; (lua_isstring(L, -2)) { /* searcher returned error message? */lua_pop(L, 1); /* remove extra return */luaL_addvalue(&msg); /* concatenate error message */}elselua_pop(L, 2); /* remove both returns */ }}修改lua源代码方案在searcher_Lua中最终是调用lua_load(L, getF, &lf, lua_tostring(L, -1), mode)加载源文件,该函数的第二个参数getF是一个lua_Reader函数,所以这里可以重写该函数以实现解密,也可以向外部暴露一个接口用来将自定义的文件读取函数作为参数传给lua_load。下面是原版的getF实现*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 tablelua_getglobal(m_state, “package”);/* L: package */lua_getfield(m_state, -1, “loaders”);/* L: package, loaders */// insert loader into index 2lua_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 -3lua_rawseti(m_state, -3, i);/* L: package, loaders, func */}lua_rawseti(m_state, -2, 2);/* L: package, loaders */// set loaders into packagelua_setfield(m_state, -2, “loaders”);/* L: package */lua_pop(m_state, 1);}加载器函数实现根据传入的文件名,逐个匹配的package.path中的内容,存在文件后,然后读取文件内容,,解密,最后再将解出的内容调用load加载并返回(c中为luaL_loadbufferx),实现可以参照lua源码中的searcher_Lua实现

不是每个人都一定快乐,不是每种痛都一定要述说。

lua 代码加密方案

相关文章:

你感兴趣的文章:

标签云: