# LuaLuac
LuaLuaAPP
Lua5.15.25.35.1LuaLuaJitLuaLua5.25.3LuaLua5.2LuacLua
## LuaLuac
LuaPythonPythonpycLua-luacLuaLuaLuacLuacluacLuacLua
LuacLuacLuaLuaLuacLuaLuacLuaLuac
`luadec``unlua`LuacC++JavaLuacLuaC`010 Editor`Luac.bt`010 Editor`C`luadec`
## Luac
Luac
```
typedef struct {
char signature[4]; //".lua"
uchar version;
uchar format;
uchar endian;
uchar size_int;
uchar size_size_t;
uchar size_Instruction;
uchar size_lua_Number;
uchar lua_num_valid;
uchar luac_tail[0x6];
} GlobalHeader;
```
`signature`lua.h`LUA_SIGNATURE`\033Lua"`\033````LUA_SIGNATURE`Luac4LuacMagic NumberLuacMagic Number
`version`LuacLua5.2LuaLuac0x52
`format`0official0LuacLua
`endian`Luac`LittleEndian``BigEndian`1`LittleEndian`0`BigEndian`
`size_int`int`size_size_t`size_tPC3264
`size_Instruction`Luac`Instruction`4LuacLuac
`size_lua_Number``lua_Number``lua_Number`Lua`Number`Lua`LUA_NUMBER`Lua`LUA_NUMBER`float32double64macOSLua64
`lua_num_valid`0`lua_Number`
`luac_tail`Lua`LUAC_TAIL`"\x19\x93\r\n\x1a\n"
LuacUpvalue
Lua`Proto`
```
typedef struct {
//header
ProtoHeader header;
//code
Code code;
// constants
Constants constants;
// functions
Protos protos;
// upvalues
Upvaldescs upvaldescs;
// string
SourceName src_name;
// lines
Lines lines;
// locals
LocVars loc_vars;
// upvalue names
UpValueNames names;
} Proto;
```
`ProtoHeader``Proto`
```
typedef struct {
uint32 linedefined;
uint32 lastlinedefined;
uchar numparams;
uchar is_vararg;
uchar maxstacksize;
} ProtoHeader;
```
`ProtoHeader`Lua`lua_Debug``lua_Debug`LuacLuac`linedefined``lastlinedefined``numparams``is_vararg`
```
function f1(a1, a2, ...)
......
end
```
C...`f1()``numparams`2`is_vararg`1
`maxstacksize`Lua2
`ProtoHeader``Code``Code`Luac32`Code`
```
struct Code {
uint32 sizecode;
uint32 inst[];
} code;
```
`sizecode``inst`Lua`Instruction`
```
#define LUAI_UINT32 unsigned int
typedef LUAI_UINT32 lu_int32;
typedef lu_int32 Instruction;
```
`LUAI_BITSINT`32`LUAI_UINT32`unsigned intunsigned long`lu_int32`32
`Constants`
```
typedef struct {
uint32 sizek;
Constant constant[];
} Constants;
```
`sizek``Constant``constant``Constant`
```
typedef struct {
LUA_DATATYPE const_type;
TValue val;
} Constant;
```
`LUA_DATATYPE`Lua`LUA_TBOOLEAN`bool`lua_Val``LUA_TNUMBER``lua_Integer``lua_Number``LUA_TSTRING``const_type`1
`TValue`
```
typedef struct {
union Value {
//GCObject *gc; /* collectable objects */
//void *p; /* light userdata */
lua_Val val; /* booleans */
//lua_CFunction f; /* light C functions */
lua_Integer i; /* integer numbers */
lua_Number n; /* float numbers */
} value_;
} TValue;
```
`LUA_TBOOLEAN`Lua`bvalue`
`LUA_TNUMBER``nvalue`Lua5.3`nvalue``ivalue``fltvalue`
`LUA_TSTRING``rawtsvalue`Luac64
`Protos`
```
typedef struct(string level) {
uint32 sizep;
Proto proto[];
} Protos
```
`sizep`
```
function Create(n)
local function foo1()
print(n)
end
local function foo2()
n = n + 10
end
return foo1,foo2
end
```
`Create()``foo1()``foo2()``sizep`2`proto`Lua
`Upvaldescs``UpValueNames`LuaUpValueUpValue`Create()``foo1()``foo2()``n`UpValuen
`Upvaldesc`
```
typedef struct {
uchar instack;
uchar idx;
} Upvaldesc;
```
`instack`UpValue10`idx`UpValueUpValue0
`UpValueNames`UpValue
```
typedef struct {
uint32 size_upvalue_names;
UpValueName upvalue_name[];
} UpValueNames;
```
`size_upvalue_names``UpValueName``UpValueName`UpValue
```
typedef struct {
uint64 name_size;
char var_str[];
} UpValueName;
```
`name_size``var_str`
`SourceName`Luac
```
typedef struct {
uint64 src_string_size;
char str[];
} SourceName
```
`SourceName``UpValueName`
`Lines`
```
typedef struct {
uint32 sizelineinfo;
uint32 line[];
} Lines;
```
`sizelineinfo``line`
`LocVars`
```
typedef struct {
uint32 sizelocvars;
LocVar local_var[];
} LocVars;
```
`sizelocvars``local_var``LocVar`
```
typedef struct {
uint64 varname_size;
char varname[];
uint32 startpc;
uint32 endpc;
} LocVar;
```
`varname_size``varname``startpc``endpc`
Luac
## 010 Editor
Luac`010 Editor``010 Editor`Luac
`010 Editor`C`010 Editor`
`010 Editor`Templates->New Template
```
//------------------------------------------------
//--- 010 Editor v8.0 Binary Template
//
// File:
// Authors:
// Version:
// Purpose:
// Category:
// File Mask:
// ID Bytes:
// History:
//------------------------------------------------
```
`File``010 Editor`.btluac.bt
`Authors`
`Version``010 Editor``010 Editor`
`Purpose`
`Category``010 Editor``Programming`
`File Mask`*.luac, *.lua
`ID Bytes`Magic Number1B 4c 75 61
`History`
Luac.bt
```
//------------------------------------------------
//--- 010 Editor v8.0 Binary Template
//
// File: luac.bt
// Authors: fei_cong(346345565@qq.com)
// Version: 1.0
// Purpose:
// Category: Programming
// File Mask: *.luac, *.lua
// ID Bytes: 1B 4c 75 61
// History:
// 1.0 fei_cong: Initial version, support lua 5.2.
//
// License: This file is released into the public domain. People may
// use it for any purpose, commercial or otherwise.
//------------------------------------------------
```
`010 Editor`CCC
```
- 8-Bit Signed Integer - char, byte, CHAR, BYTE
- 8-Bit Unsigned Integer - uchar, ubyte, UCHAR, UBYTE
- 16-Bit Signed Integer - short, int16, SHORT, INT16
- 16-Bit Unsigned Integer - ushort, uint16, USHORT, UINT16, WORD
- 32-Bit Signed Integer - int, int32, long, INT, INT32, LONG
- 32-Bit Unsigned Integer - uint, uint32, ulong, UINT, UINT32, ULONG, DWORD
- 64-Bit Signed Integer - int64, quad, QUAD, INT64, __int64
- 64-Bit Unsigned Integer - uint64, uquad, UQUAD, UINT64, QWORD, __uint64
- 32-Bit Floating Point Number - float, FLOAT
- 64-Bit Floating Point Number - double, DOUBLE
- 16-Bit Floating Point Number - hfloat, HFLOAT
- Date Types - DOSDATE, DOSTIME, FILETIME, OLETIME, time_t (for more information on date types see Using the Inspector)
```
```
local int a;
local int32 a;
local long a;
```
32`local``local``010 Editor`
```
typedef struct {
GlobalHeader header;
Proto proto;
} Luac;
Luac luac;
```
`010 Editor``Luac``GlobalHeader``Proto``local``010 Editor`
Cstruct`010 Editor`
```
typedef struct {
uint64 varname_size ;
char varname[varname_size];
uint32 startpc ;
uint32 endpc ;
} LocVar ;
```
`LocVar``010 Editor``format`hex10`comment``read``write``read``write`
```
string LocVarRead(LocVar &val) {
return val.varname;
}
```
`read``write`string`010 Editor`
`010 Editor``string`C`char[]`
```
local string str = "world";
local string str2 = "hello " + str + "!\n";
```
`010 Editor``BITRK``ISK`
```
#define SIZE_B 9
#define BITRK (1 0) {
SPrintf(s_level, "%s_%d", level, i++);
Proto proto(s_level);
};
} Protos ;
```
`string``level`0function level
`Constant``TValue``luadec``luadec``DecompileConstant()`
char* DecompileConstant(const Proto* f, int i) {
const TValue* o = &f->k[i];
switch (ttype(o)) {
case LUA_TBOOLEAN:
return strdup(bvalue(o)?"true":"false");
case LUA_TNIL:
return strdup("nil");
#if LUA_VERSION_NUM == 501 || LUA_VERSION_NUM == 502
case LUA_TNUMBER:
{
char* ret = (char*)calloc(128, sizeof(char));
sprintf(ret, LUA_NUMBER_FMT, nvalue(o));
return ret;
}
case LUA_TSTRING:
return DecompileString(o);
default:
return strdup("Unknown_Type_Error");
}
}
`bvalue``nvalue`LuaC`nvalue`
```
((((((o))->tt_) == ((3 | (1 value_).i)))) : (((o)->value_).n))));
```
`number2str`
```
string number2str(TValue &o) {
local string ret;
local string fmt;
if (get_inst_sz() == 4) {
fmt = "(=%.7g)";
} else if (get_inst_sz() == 8) {
fmt = "(=%.14g)";
} else {
Warning("error inst size.\n");
}
local int tt = o.value_.val.tt_;
//Printf("tt:%x\n", tt);
local lua_Integer i = o.value_.i;
local lua_Number n = o.value_.n;
SPrintf(ret, "%.14g", ((tt == (3 | (1