Is it possible to change the lines (content and size) in the Lua bytecode so that it is right anyway?

Is it possible to change the lines (content and size) in the Lua bytecode so that it is right anyway? This is about line feeds in Lua bytecode. Of course, not every language has the same size for every word ...

+2
source share
3 answers

Yes, if you know what you are doing. Lines have a prefix of their size stored as an int. The size and nature of this int is platform dependent. But why do you need to edit the bytecode? Have you lost the sources?

+3
source

After some diving through the Lua source code, I found this solution:

#include "lua.h"
#include "lauxlib.h"

#include "lopcodes.h"
#include "lobject.h"
#include "lundump.h"

/* Definition from luac.c: */
#define toproto(L,i) (clvalue(L->top+(i))->l.p)

writer_function(lua_State* L, const void* p, size_t size, void* u)
{
    UNUSED(L);
    return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
}

static void
lua_bytecode_change_const(lua_State *l, Proto *f_proto,
                   int const_index, const char *new_const)
{
    TValue *tmp_tv = NULL;
    const TString *tmp_ts = NULL;

    tmp_ts = luaS_newlstr(l, new_const, strlen(new_const));
    tmp_tv = &f_proto->k[INDEXK(const_index)];
    setsvalue(l, tmp_tv, tmp_ts);

    return;
}

int main(void)
{
    lua_State *l = NULL;
    Proto *lua_function_prototype = NULL;
    FILE *output_file_hnd = NULL;

    l = lua_open();
    luaL_loadfile(l, "some_input_file.lua");
    lua_proto = toproto(l, -1);
    output_file_hnd = fopen("some_output_file.luac", "w");

    lua_bytecode_change_const(l, lua_function_prototype, some_const_index, "some_new_const");
    lua_lock(l);
    luaU_dump(l, lua_function_prototype, writer_function, output_file_hnd, 0);
    lua_unlock(l);

    return 0;
}

-, Lua VM script, . , . Lua, . Dump Prototype .

, .

+1
0

All Articles