Skip to content

Commit 1688b5a

Browse files
committed
Parse headers
1 parent b3d9c9b commit 1688b5a

File tree

6 files changed

+244
-240
lines changed

6 files changed

+244
-240
lines changed

librz/bin/format/luac/luac_bin.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ static const char *get_tag_string(ut8 tag) {
168168

169169
/* Heap allocated string */
170170
static char *get_constant_symbol_name(char *proto_name, LuaConstEntry *entry) {
171-
rz_return_val_if_fail(entry || proto_name, NULL);
171+
rz_return_val_if_fail(entry && proto_name, NULL);
172+
// rz_return_val_if_fail(entry || proto_name, NULL);
172173
ut8 tag = entry->tag;
173174
char *ret;
174175
st64 integer_value;
@@ -186,8 +187,7 @@ static char *get_constant_symbol_name(char *proto_name, LuaConstEntry *entry) {
186187
break;
187188
case LUA_VSHRSTR:
188189
case LUA_VLNGSTR:
189-
rz_return_val_if_fail(entry->data, NULL);
190-
ret = rz_str_newf("%s_const_%s", proto_name, (char *)entry->data);
190+
ret = rz_str_newf("%s_const_%s", proto_name, entry->data_len ? (char *)entry->data : "NULL");
191191
break;
192192
case LUA_VNUMFLT:
193193
rz_return_val_if_fail(entry->data, NULL);

librz/bin/format/luac/luac_common.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,6 @@
66

77
#include "luac_common.h"
88

9-
ut8 luac_header_size(const ut8 minor) {
10-
const ut8 regular = strlen(LUAC_MAGIC) + 2 /* version */ + strlen(LUAC_DATA);
11-
switch (minor) {
12-
case 0x1:
13-
return regular;
14-
case 0x2:
15-
return regular + 6;
16-
case 0x3:
17-
return regular + 22;
18-
case 0x4:
19-
return regular + 20;
20-
case 0x5:
21-
return regular + 29;
22-
default:
23-
rz_warn_if_reached();
24-
return -1;
25-
}
26-
}
27-
289
LuaDbgUpvalueEntry *lua_new_dbg_upvalue_entry() {
2910
LuaDbgUpvalueEntry *entry = RZ_NEW0(LuaDbgUpvalueEntry);
3011
return entry;

librz/bin/format/luac/luac_common.h

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <rz_lib.h>
1212
#include <rz_list.h>
1313

14+
// #define RZ_DEBUG
1415
/* Macros for bin_luac.c */
1516
/* Macros/Typedefs used in luac */
1617
typedef double LUA_NUMBER;
@@ -88,6 +89,7 @@ typedef ut32 LUA_INT;
8889
#define LUAC_55_NUMBER_VALID_OFFSET 0x17
8990
#define LUAC_55_UPVALUES_NUMBER_OFFSET 0x1F
9091

92+
#define LUAC_MAGIC "\x1b\x4c\x75\x61" ///< "\033Lua"
9193
#define LUAC_FORMAT 0 /* this is the official format */
9294
#define LUAC_DATA "\x19\x93\r\n\x1a\n"
9395
#define LUAC_INT_VALIDATION luac_cast_int(0x5678)
@@ -103,8 +105,6 @@ typedef ut32 LUA_INSTRUCTION;
103105
#define LUAC_VERSION_OFFSET 0x04
104106
#define LUAC_VERSION_SIZE 1
105107

106-
#define LUAC_MAGIC "\x1b\x4c\x75\x61" ///< "\033Lua"
107-
108108
/* Body */
109109
#define LUAC51_FILENAME_OFFSET 0x13
110110
#define LUAC52_FILENAME_OFFSET 0x00
@@ -161,6 +161,7 @@ typedef struct lua_proto_ex {
161161
RzList /*<LuaUpvalueEntry *>*/ *upvalue_entries; ///< A list to store upvalue entries
162162
ut64 upvalue_offset; ///< upvalue section offset
163163
ut64 upvalue_size; ///< upvalue section size
164+
ut64 size_upvalues; ///< upvalue size (v5.5)
164165

165166
/* store protos defined in this proto */
166167
RzList /*<LuaProto *>*/ *proto_entries; ///< A list to store sub proto entries
@@ -179,6 +180,26 @@ typedef struct lua_proto_ex {
179180

180181
typedef LuaProtoHeavy LuaProto;
181182

183+
/**
184+
* \struct lua_header_info
185+
* \brief Store header information of luac file
186+
*/
187+
typedef struct lua_header_info {
188+
st32 major; ///< major version
189+
st32 minor; ///< minor version
190+
// ut8 version; ///< type of this constant, see LUA_V* macros in luac_common.h
191+
ut8 format; ///< type of this constant, see LUA_V* macros in luac_common.h
192+
ut8 endianness; ///< type of this constant, see LUA_V* macros in luac_common.h
193+
st32 int_size; ///< type of this constant, see LUA_V* macros in luac_common.h
194+
ut8 size_t_size; ///< type of this constant, see LUA_V* macros in luac_common.h
195+
st32 instruction_size; ///< type of this constant, see LUA_V* macros in luac_common.h
196+
st32 integer_size; ///< type of this constant, see LUA_V* macros in luac_common.h
197+
st32 number_size; ///< type of this constant, see LUA_V* macros in luac_common.h
198+
ut8 is_number_integral; ///< is lua_Number integral? (< 5.3)
199+
size_t psize; ///< Physical size of header in bytes
200+
char *src_file_name;
201+
} LuaHeaderInfo;
202+
182203
/**
183204
* \struct lua_constant_entry
184205
* \brief Store constant type, data, and offset of this constant in luac file
@@ -246,18 +267,17 @@ typedef struct lua_dbg_upvalue_entry {
246267
} LuaDbgUpvalueEntry;
247268

248269
/**
249-
* \struct lua_bin_info
270+
* \struct luac_bin_info
250271
* \brief A context info structure for luac plugin.
251272
*/
252273
typedef struct luac_bin_info {
253-
st32 major; ///< major version
254-
st32 minor; ///< minor version
255274
LuaProto *proto;
256275
RzPVector /*<RzBinSection *>*/ *section_vec; ///< list of sections
257276
RzList /*<RzBinSymbol *>*/ *symbol_list; ///< list of symbols
258277
RzPVector /*<RzBinAddr *>*/ *entry_vec; ///< list of entries
259278
RzList /*<RzBinString *>*/ *string_list; ///< list of strings
260279
RzBinInfo *general_info; ///< general binary info from luac header
280+
LuaHeaderInfo *header;
261281
} LuacBinInfo;
262282

263283
/* ========================================================
@@ -294,23 +314,18 @@ void _luac_build_info(LuaProto *proto, LuacBinInfo *info);
294314
* Export version specified Api to bin_luac.c
295315
* Implemented in bin/format/luac/v[version]/bin_[version]
296316
* ======================================================== */
297-
RzBinInfo *lua_parse_header_54(RzBinFile *bf, st8 major, st8 minor);
317+
RzBinInfo *lua_parse_header_54(RzBinFile *bf, ut8 major, ut8 minor);
298318
LuaProto *lua_parse_body_54(RzBuffer *buffer, ut64 offset, ut64 data_size);
299319

300-
RzBinInfo *lua_parse_header_53(RzBinFile *bf, st8 major, st8 minor);
320+
RzBinInfo *lua_parse_header_53(RzBinFile *bf, ut8 major, ut8 minor);
301321
LuaProto *lua_parse_body_53(RzBuffer *buffer, ut64 offset, ut64 data_size);
302322

303-
RzBinInfo *lua_parse_header_52(RzBinFile *bf, st8 major, st8 minor);
323+
RzBinInfo *lua_parse_header_52(RzBinFile *bf, ut8 major, ut8 minor);
304324
LuaProto *lua_parse_body_52(RzBuffer *buffer, ut64 offset, ut64 data_size);
305325

306-
ut8 luac_header_size(const ut8 minor);
307-
// static void lua_load_block(RzBuffer *buffer, void *dest, size_t size, ut64 offset, ut64 data_size);
308-
// static ut64 lua_load_integer(RzBuffer *buffer, ut64 offset);
309-
// static double lua_load_number(RzBuffer *buffer, ut64 offset);
310-
// static ut32 lua_load_int(RzBuffer *buffer, ut64 offset);
311-
// static ut64 lua_parse_name(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size, st8 minor);
312-
LuaProto *lua_parse_body(RzBuffer *buffer, ut64 base_offset, ut64 data_size, st8 minor);
313-
RzBinInfo *lua_parse_header(const RzBinFile *bf, st8 major, st8 minor);
326+
LuaProto *lua_parse_body(RzBuffer *buffer, LuaHeaderInfo *header, ut64 base_offset, ut64 data_size);
327+
RzBinInfo *lua_parse_header(const RzBinFile *bf, const LuaHeaderInfo *header);
328+
size_t parse_header(const RzBinFile *bf, LuaHeaderInfo *header);
314329

315330
#define lua_check_error_offset(offset) \
316331
if ((offset) == 0) { \

0 commit comments

Comments
 (0)