Rizin
unix-like reverse engineering framework and cli tools
parse_53.c File Reference
#include "luac_specs_53.h"

Go to the source code of this file.

Functions

static void lua_load_block (RzBuffer *buffer, void *dest, size_t size, ut64 offset, ut64 data_size)
 
static ut64 lua_load_integer (RzBuffer *buffer, ut64 offset)
 
static double lua_load_number (RzBuffer *buffer, ut64 offset)
 
static ut32 lua_load_int (RzBuffer *buffer, ut64 offset)
 
static ut64 lua_parse_line_defined (LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
 
static ut64 lua_parse_string (RzBuffer *buffer, ut8 **dest, int *str_len, ut64 offset, ut64 data_size)
 
static ut64 lua_parse_name (LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
 
static ut64 lua_parse_code (LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
 
static ut64 lua_parse_const_entry (LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
 
static ut64 lua_parse_consts (LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
 
static ut64 lua_parse_upvalue_entry (LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
 
static ut64 lua_parse_upvalues (LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
 
static ut64 lua_parse_debug (LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
 
static ut64 lua_parse_protos (LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
 
LuaProtolua_parse_body_53 (RzBuffer *buffer, ut64 base_offset, ut64 data_size)
 
RzBinInfolua_parse_header_53 (RzBinFile *bf, st32 major, st32 minor)
 

Function Documentation

◆ lua_load_block()

static void lua_load_block ( RzBuffer buffer,
void *  dest,
size_t  size,
ut64  offset,
ut64  data_size 
)
static

Definition at line 7 of file parse_53.c.

7  {
8  if (offset + size > data_size) {
9  RZ_LOG_ERROR("Truncated load block at 0x%llx\n", offset);
10  return;
11  }
13 }
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
char * dest
Definition: lz4.h:697
RZ_API st64 rz_buf_read_at(RZ_NONNULL RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, ut64 len)
Read len bytes of the buffer at the specified address.
Definition: buf.c:1136
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
Definition: buffer.h:15

References dest, rz_buf_read_at(), and RZ_LOG_ERROR.

Referenced by lua_parse_const_entry().

◆ lua_load_int()

static ut32 lua_load_int ( RzBuffer buffer,
ut64  offset 
)
static

Definition at line 27 of file parse_53.c.

27  {
28  ut32 x = 0;
30  return x;
31 }
uint32_t ut32
int x
Definition: mipsasm.c:20
#define rz_buf_read_le32_at(b, addr, result)
Definition: rz_buf.h:271

References rz_buf_read_le32_at, and x.

Referenced by lua_parse_code(), lua_parse_consts(), lua_parse_debug(), lua_parse_line_defined(), lua_parse_protos(), and lua_parse_upvalues().

◆ lua_load_integer()

static ut64 lua_load_integer ( RzBuffer buffer,
ut64  offset 
)
static

Definition at line 15 of file parse_53.c.

15  {
16  ut64 x = 0;
18  return x;
19 }
#define rz_buf_read_le64_at(b, addr, result)
Definition: rz_buf.h:272
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References rz_buf_read_le64_at, ut64(), and x.

Referenced by lua_parse_header_53().

◆ lua_load_number()

static double lua_load_number ( RzBuffer buffer,
ut64  offset 
)
static

Definition at line 21 of file parse_53.c.

21  {
22  double x = 0;
24  return x;
25 }

References rz_buf_read_le64_at, ut64(), and x.

Referenced by lua_parse_header_53().

◆ lua_parse_body_53()

LuaProto* lua_parse_body_53 ( RzBuffer buffer,
ut64  base_offset,
ut64  data_size 
)

Definition at line 407 of file parse_53.c.

407  {
408  ut64 offset;
409  ut64 delta_offset;
410 
411  LuaProto *ret_proto = lua_new_proto_entry();
412  if (!ret_proto) {
413  return NULL;
414  }
415 
416  // start parsing
417  offset = base_offset;
418 
419  // record offset of main proto
420  ret_proto->offset = offset;
421 
422  // parse proto name
423  delta_offset = lua_parse_name(ret_proto, buffer, offset, data_size);
424  lua_check_error_offset_proto(delta_offset, ret_proto);
425  offset += delta_offset;
426 
427  /* parse line defined info */
428  delta_offset = lua_parse_line_defined(ret_proto, buffer, offset, data_size);
429  lua_check_error_offset_proto(delta_offset, ret_proto);
430  offset += delta_offset;
431 
432  /* parse num params max_stack_size */
433  if (offset + 3 > data_size) {
434  lua_free_proto_entry(ret_proto);
435  return NULL;
436  }
437  if (!rz_buf_read8_at(buffer, offset + 0, &ret_proto->num_params) ||
438  !rz_buf_read8_at(buffer, offset + 1, &ret_proto->is_vararg) ||
439  !rz_buf_read8_at(buffer, offset + 2, &ret_proto->max_stack_size)) {
440  lua_free_proto_entry(ret_proto);
441  return NULL;
442  }
443  offset += 3;
444 
445  /* parse code */
446  ret_proto->code_offset = offset;
447  delta_offset = lua_parse_code(ret_proto, buffer, offset, data_size);
448  lua_check_error_offset_proto(delta_offset, ret_proto);
449  offset += delta_offset;
450 
451  /* parse constants */
452  ret_proto->const_offset = offset;
453  delta_offset = lua_parse_consts(ret_proto, buffer, offset, data_size);
454  lua_check_error_offset_proto(delta_offset, ret_proto);
455  offset += delta_offset;
456 
457  /* parse upvalues */
458  ret_proto->upvalue_offset = offset;
459  delta_offset = lua_parse_upvalues(ret_proto, buffer, offset, data_size);
460  lua_check_error_offset_proto(delta_offset, ret_proto);
461  offset += delta_offset;
462 
463  /* parse inner protos */
464  ret_proto->inner_proto_offset = offset;
465  delta_offset = lua_parse_protos(ret_proto, buffer, offset, data_size);
466  lua_check_error_offset_proto(delta_offset, ret_proto);
467  offset += delta_offset;
468 
469  /* specially handle recursive protos size */
470  ret_proto->inner_proto_size = offset - ret_proto->inner_proto_offset;
471 
472  /* parse debug */
473  ret_proto->debug_offset = offset;
474  delta_offset = lua_parse_debug(ret_proto, buffer, offset, data_size);
475  lua_check_error_offset_proto(delta_offset, ret_proto);
476  offset += delta_offset;
477 
478  ret_proto->size = offset - base_offset + 1;
479 
480  return ret_proto;
481 }
#define NULL
Definition: cris-opc.c:27
LuaProto * lua_new_proto_entry()
Definition: luac_common.c:38
void lua_free_proto_entry(LuaProto *proto)
Definition: luac_common.c:119
#define lua_check_error_offset_proto(offset, proto)
Definition: luac_common.h:212
static ut64 lua_parse_code(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:105
static ut64 lua_parse_name(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:101
static ut64 lua_parse_debug(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:295
static ut64 lua_parse_upvalues(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:268
static ut64 lua_parse_line_defined(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:33
static ut64 lua_parse_consts(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:213
static ut64 lua_parse_protos(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:378
RZ_API bool rz_buf_read8_at(RzBuffer *b, ut64 addr, RZ_NONNULL RZ_OUT ut8 *result)
Read a byte at the specified address in the buffer.
Definition: buf.c:876
Store valuable info when parsing. Treat luac file body as a main function.
Definition: luac_common.h:43
ut64 size
current proto size
Definition: luac_common.h:45
ut8 is_vararg
is variable arg?
Definition: luac_common.h:54
ut64 inner_proto_offset
sub proto section offset
Definition: luac_common.h:74
ut64 const_offset
const section offset
Definition: luac_common.h:64
ut8 num_params
number of parameters of this proto
Definition: luac_common.h:53
ut64 code_offset
code section offset
Definition: luac_common.h:58
ut64 upvalue_offset
upvalue section offset
Definition: luac_common.h:69
ut64 offset
proto offset in bytes
Definition: luac_common.h:44
ut64 debug_offset
debug section offset
Definition: luac_common.h:78
ut8 max_stack_size
max stack size
Definition: luac_common.h:55
ut64 inner_proto_size
sub proto section size
Definition: luac_common.h:75

References lua_proto_ex::code_offset, lua_proto_ex::const_offset, lua_proto_ex::debug_offset, lua_proto_ex::inner_proto_offset, lua_proto_ex::inner_proto_size, lua_proto_ex::is_vararg, lua_check_error_offset_proto, lua_free_proto_entry(), lua_new_proto_entry(), lua_parse_code(), lua_parse_consts(), lua_parse_debug(), lua_parse_line_defined(), lua_parse_name(), lua_parse_protos(), lua_parse_upvalues(), lua_proto_ex::max_stack_size, NULL, lua_proto_ex::num_params, lua_proto_ex::offset, rz_buf_read8_at(), lua_proto_ex::size, lua_proto_ex::upvalue_offset, and ut64().

Referenced by load_buffer(), and lua_parse_protos().

◆ lua_parse_code()

static ut64 lua_parse_code ( LuaProto proto,
RzBuffer buffer,
ut64  offset,
ut64  data_size 
)
static

Definition at line 105 of file parse_53.c.

105  {
106  ut64 size_offset;
107  ut64 total_size;
108  int code_size;
109 
110  size_offset = sizeof(LUA_INT);
111  if (size_offset + offset > data_size) {
112  return 0;
113  }
114  code_size = lua_load_int(buffer, offset);
115  total_size = code_size * 4 + size_offset;
116 
117  if (total_size + offset > data_size) {
118  RZ_LOG_ERROR("Truncated Code at [0x%llx]\n", offset);
119  return 0;
120  }
121 
122  /* Set Proto Member */
123  proto->code_size = code_size * 4;
124  proto->code_skipped = size_offset;
125 
126  return total_size;
127 }
ut32 LUA_INT
Definition: luac_specs_53.h:12
static ut32 lua_load_int(RzBuffer *buffer, ut64 offset)
Definition: parse_53.c:27
ut64 code_skipped
opcode data offset to code_offset.
Definition: luac_common.h:60
ut64 code_size
code section size
Definition: luac_common.h:59

References lua_proto_ex::code_size, lua_proto_ex::code_skipped, lua_load_int(), RZ_LOG_ERROR, and ut64().

Referenced by lua_parse_body_53().

◆ lua_parse_const_entry()

static ut64 lua_parse_const_entry ( LuaProto proto,
RzBuffer buffer,
ut64  offset,
ut64  data_size 
)
static

Definition at line 129 of file parse_53.c.

129  {
130  LuaConstEntry *current_entry;
131  ut64 base_offset;
132  ut8 *recv_data;
133  ut64 delta_offset;
134  int data_len;
135 
136  current_entry = lua_new_const_entry();
137  current_entry->offset = offset;
138  base_offset = offset;
139  delta_offset = 0;
140 
141  /* read TAG byte */
142  if (offset + 1 > data_size) {
143  return 0;
144  }
145  if (!rz_buf_read8_at(buffer, offset, &current_entry->tag)) {
146  return 0;
147  }
148  offset += 1;
149 
150  ut8 tmp;
151 
152  /* read data */
153  // TODO : check tag Macro
154  // RIGHT : 0x843
155  switch (current_entry->tag) {
156  case LUA_TNUMFLT:
157  data_len = sizeof(LUA_NUMBER);
158  recv_data = RZ_NEWS(ut8, data_len);
159  lua_load_block(buffer, recv_data, data_len, offset, data_size);
160  if (offset + data_len > data_size) {
161  return 0;
162  }
163  delta_offset = data_len;
164  current_entry->tag = LUA_VNUMFLT; // keep the same with 5.4 tag
165  break;
166  case LUA_TNUMINT:
167  data_len = sizeof(LUA_INTEGER);
168  recv_data = RZ_NEWS(ut8, data_len);
169  lua_load_block(buffer, recv_data, data_len, offset, data_size);
170  if (offset + data_len > data_size) {
171  return 0;
172  }
173  delta_offset = data_len;
174  current_entry->tag = LUA_VNUMINT; // keep the same with 5.4 tag
175  break;
176  case LUA_VSHRSTR:
177  case LUA_VLNGSTR:
178  delta_offset = lua_parse_string(buffer, &recv_data, &data_len, offset, data_size);
179  lua_check_error_offset(delta_offset);
180  break;
181  // BOOLEAN
182  case LUA_TBOOLEAN:
183  if (!rz_buf_read8_at(buffer, offset, &tmp)) {
184  return 0;
185  }
186 
187  current_entry->tag = tmp == 0 ? LUA_VFALSE : LUA_VTRUE;
188  recv_data = NULL;
189  data_len = 0;
190  delta_offset = 1;
191  break;
192  // NIL
193  case LUA_TNIL:
194  default:
195  recv_data = NULL;
196  current_entry->tag = LUA_VNIL;
197  data_len = 0;
198  delta_offset = 0;
199  break;
200  }
201 
202  offset += delta_offset;
203 
204  current_entry->data = recv_data;
205  current_entry->data_len = data_len;
206 
207  /* add to list */
208  rz_list_append(proto->const_entries, current_entry);
209 
210  return offset - base_offset;
211 }
uint8_t ut8
Definition: lh5801.h:11
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
LuaConstEntry * lua_new_const_entry()
Definition: luac_common.c:33
#define LUA_VNUMFLT
Definition: luac_common.h:35
#define LUA_TBOOLEAN
Definition: luac_common.h:27
#define LUA_VLNGSTR
Definition: luac_common.h:37
#define LUA_VNUMINT
Definition: luac_common.h:34
#define lua_check_error_offset(offset)
Definition: luac_common.h:208
#define LUA_VSHRSTR
Definition: luac_common.h:36
#define LUA_VFALSE
Definition: luac_common.h:32
#define LUA_VNIL
Definition: luac_common.h:31
#define LUA_TNIL
Definition: luac_common.h:26
#define LUA_VTRUE
Definition: luac_common.h:33
ut64 LUA_INTEGER
Definition: luac_specs_53.h:11
#define LUA_TNUMINT
Definition: luac_specs_53.h:42
double LUA_NUMBER
Definition: luac_specs_53.h:10
#define LUA_TNUMFLT
Definition: luac_specs_53.h:41
static ut64 lua_parse_string(RzBuffer *buffer, ut8 **dest, int *str_len, ut64 offset, ut64 data_size)
Definition: parse_53.c:54
static void lua_load_block(RzBuffer *buffer, void *dest, size_t size, ut64 offset, ut64 data_size)
Definition: parse_53.c:7
#define RZ_NEWS(x, y)
Definition: rz_types.h:283
Store constant type, data, and offset of this constant in luac file.
Definition: luac_common.h:93
ut64 offset
addr of this constant
Definition: luac_common.h:97
int data_len
len of data
Definition: luac_common.h:96
void * data
can be Number/Integer/String
Definition: luac_common.h:95
ut8 tag
type of this constant, see LUA_V* macros in luac_common.h
Definition: luac_common.h:94
RzList * const_entries
A list to store constant entries.
Definition: luac_common.h:63

References lua_proto_ex::const_entries, lua_constant_entry::data, lua_constant_entry::data_len, lua_check_error_offset, lua_load_block(), lua_new_const_entry(), lua_parse_string(), LUA_TBOOLEAN, LUA_TNIL, LUA_TNUMFLT, LUA_TNUMINT, LUA_VFALSE, LUA_VLNGSTR, LUA_VNIL, LUA_VNUMFLT, LUA_VNUMINT, LUA_VSHRSTR, LUA_VTRUE, NULL, lua_constant_entry::offset, rz_buf_read8_at(), rz_list_append(), RZ_NEWS, lua_constant_entry::tag, autogen_x86imm::tmp, and ut64().

Referenced by lua_parse_consts().

◆ lua_parse_consts()

static ut64 lua_parse_consts ( LuaProto proto,
RzBuffer buffer,
ut64  offset,
ut64  data_size 
)
static

Definition at line 213 of file parse_53.c.

213  {
214  int consts_cnt;
215  int i;
216  ut64 base_offset;
217  ut64 delta_offset;
218 
219  base_offset = offset;
220 
221  /* parse number of constants */
222  if (offset + sizeof(LUA_INT) > data_size) {
223  return 0;
224  }
225  consts_cnt = lua_load_int(buffer, offset);
226  delta_offset = sizeof(LUA_INT);
227  offset += delta_offset;
228 
229  for (i = 0; i < consts_cnt; ++i) {
230  // add an entry of constant
231  delta_offset = lua_parse_const_entry(proto, buffer, offset, data_size);
232  lua_check_error_offset(delta_offset);
233  offset += delta_offset;
234  }
235 
236  proto->const_size = offset - base_offset + 1;
237  return offset - base_offset;
238 }
lzma_index ** i
Definition: index.h:629
static ut64 lua_parse_const_entry(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:129
ut64 const_size
const section size
Definition: luac_common.h:65

References lua_proto_ex::const_size, i, lua_check_error_offset, lua_load_int(), lua_parse_const_entry(), and ut64().

Referenced by lua_parse_body_53().

◆ lua_parse_debug()

static ut64 lua_parse_debug ( LuaProto proto,
RzBuffer buffer,
ut64  offset,
ut64  data_size 
)
static

Definition at line 295 of file parse_53.c.

295  {
296  int entries_cnt;
297  int i;
298  ut64 base_offset;
299  ut64 delta_offset;
300 
301  base_offset = offset;
302 
303  /* parse line info */
304  if (offset + sizeof(LUA_INT) > data_size) {
305  return 0;
306  }
307  entries_cnt = lua_load_int(buffer, offset);
308  offset += sizeof(LUA_INT);
309  LuaLineinfoEntry *info_entry;
310  for (i = 0; i < entries_cnt; ++i) {
311  info_entry = lua_new_lineinfo_entry();
312  info_entry->offset = offset;
313  info_entry->info_data = lua_load_int(buffer, offset);
314  rz_list_append(proto->line_info_entries, info_entry);
315  offset += sizeof(int);
316  }
317 
318  /* no parse absline info */
319 
320  /* parse local vars */
321  if (offset + sizeof(LUA_INT) > data_size) {
322  return 0;
323  }
324  entries_cnt = lua_load_int(buffer, offset);
325  offset += sizeof(LUA_INT);
326  LuaLocalVarEntry *var_entry;
327  for (i = 0; i < entries_cnt; ++i) {
328  var_entry = lua_new_local_var_entry();
329  var_entry->offset = offset;
330 
331  /* string */
332  delta_offset = lua_parse_string(
333  buffer,
334  &var_entry->varname, &var_entry->varname_len,
335  offset, data_size);
336  lua_check_error_offset(delta_offset);
337  offset += delta_offset;
338 
339  /* start pc && end pc -- int */
340  if (offset + sizeof(LUA_INT) + sizeof(LUA_INT) > data_size) {
341  return 0;
342  }
343  var_entry->start_pc = lua_load_int(buffer, offset);
344  offset += sizeof(LUA_INT);
345 
346  /* end pc -- int */
347  var_entry->end_pc = lua_load_int(buffer, offset);
348  offset += sizeof(LUA_INT);
349 
350  rz_list_append(proto->local_var_info_entries, var_entry);
351  }
352 
353  /* parse upvalue */
354  if (offset + sizeof(LUA_INT) > data_size) {
355  return 0;
356  }
357  entries_cnt = lua_load_int(buffer, offset);
358  offset += sizeof(LUA_INT);
359  LuaDbgUpvalueEntry *dbg_upvalue_entry;
360  for (i = 0; i < entries_cnt; ++i) {
361  dbg_upvalue_entry = lua_new_dbg_upvalue_entry();
362  dbg_upvalue_entry->offset = offset;
363 
364  delta_offset = lua_parse_string(
365  buffer,
366  &dbg_upvalue_entry->upvalue_name, &dbg_upvalue_entry->name_len,
367  offset, data_size);
368  lua_check_error_offset(delta_offset);
369  offset += delta_offset;
370 
371  rz_list_append(proto->dbg_upvalue_entries, dbg_upvalue_entry);
372  }
373 
374  proto->debug_size = offset - base_offset + 1;
375  return offset - base_offset;
376 }
LuaLineinfoEntry * lua_new_lineinfo_entry()
Definition: luac_common.c:23
LuaDbgUpvalueEntry * lua_new_dbg_upvalue_entry()
Definition: luac_common.c:8
LuaLocalVarEntry * lua_new_local_var_entry()
Definition: luac_common.c:13
static int
Definition: sfsocketcall.h:114
Store upvalue's debug info.
Definition: luac_common.h:149
int name_len
length of name
Definition: luac_common.h:151
ut8 * upvalue_name
upvalue name
Definition: luac_common.h:150
ut64 offset
Definition: luac_common.h:152
Store line info attributes.
Definition: luac_common.h:118
ut32 info_data
Definition: luac_common.h:119
ut64 offset
Definition: luac_common.h:120
Store local var names and other info.
Definition: luac_common.h:137
int varname_len
length of name
Definition: luac_common.h:139
int end_pc
first deactive position
Definition: luac_common.h:141
ut64 offset
offset of this entry
Definition: luac_common.h:142
int start_pc
first active position
Definition: luac_common.h:140
ut8 * varname
name of this variable
Definition: luac_common.h:138
RzList * dbg_upvalue_entries
A list to store upvalue names.
Definition: luac_common.h:83
RzList * local_var_info_entries
A list to store local var entries.
Definition: luac_common.h:82
ut64 debug_size
debug section size
Definition: luac_common.h:79
RzList * line_info_entries
A list to store line info entries.
Definition: luac_common.h:80

References lua_proto_ex::dbg_upvalue_entries, lua_proto_ex::debug_size, lua_local_var_entry::end_pc, i, lua_lineinfo_entry::info_data, int, lua_proto_ex::line_info_entries, lua_proto_ex::local_var_info_entries, lua_check_error_offset, lua_load_int(), lua_new_dbg_upvalue_entry(), lua_new_lineinfo_entry(), lua_new_local_var_entry(), lua_parse_string(), lua_dbg_upvalue_entry::name_len, lua_lineinfo_entry::offset, lua_local_var_entry::offset, lua_dbg_upvalue_entry::offset, rz_list_append(), lua_local_var_entry::start_pc, lua_dbg_upvalue_entry::upvalue_name, ut64(), lua_local_var_entry::varname, and lua_local_var_entry::varname_len.

Referenced by lua_parse_body_53().

◆ lua_parse_header_53()

RzBinInfo* lua_parse_header_53 ( RzBinFile bf,
st32  major,
st32  minor 
)

Definition at line 483 of file parse_53.c.

483  {
484  RzBinInfo *ret = NULL;
485  RzBuffer *buffer;
486 
487  st64 reat = bf->size;
488  if (reat < LUAC_53_HDRSIZE) {
489  RZ_LOG_ERROR("Truncated header\n");
490  return NULL;
491  }
492  buffer = bf->buf;
493 
494  /* read header members from work buffer */
495  ut8 luac_format;
496  if (!rz_buf_read8_at(buffer, LUAC_53_FORMAT_OFFSET, &luac_format)) {
497  return NULL;
498  }
499  ut8 int_size;
500  if (!rz_buf_read8_at(buffer, LUAC_53_INT_SIZE_OFFSET, &int_size)) {
501  return NULL;
502  }
503  ut8 sizet_size;
504  if (!rz_buf_read8_at(buffer, LUAC_53_SIZET_SIZE_OFFSET, &sizet_size)) {
505  return NULL;
506  }
507  ut8 instruction_size;
508  if (!rz_buf_read8_at(buffer, LUAC_53_INSTRUCTION_SIZE_OFFSET, &instruction_size)) {
509  return NULL;
510  }
511  ut8 integer_size;
512  if (!rz_buf_read8_at(buffer, LUAC_53_INTEGER_SIZE_OFFSET, &integer_size)) {
513  return NULL;
514  }
515  ut8 number_size;
516  if (!rz_buf_read8_at(buffer, LUAC_53_NUMBER_SIZE_OFFSET, &number_size)) {
517  return NULL;
518  }
520  double number_valid = lua_load_number(buffer, LUAC_53_NUMBER_VALID_OFFSET);
521 
522  /* Common Ret */
523  if (!(ret = RZ_NEW0(RzBinInfo))) {
524  return NULL;
525  }
526  ret->file = rz_str_new(bf->file);
527  ret->type = rz_str_newf("Lua %c.%c compiled file", major + '0', minor + '0');
528  ret->bclass = rz_str_new("Lua compiled file");
529  ret->rclass = rz_str_new("luac");
530  ret->arch = rz_str_new("luac");
531  ret->machine = rz_str_newf("Lua %c.%c VM", major + '0', minor + '0');
532  ret->os = rz_str_newf("%c.%c", major + '0', minor + '0');
533  ret->cpu = rz_str_newf("%c.%c", major + '0', minor + '0');
534  ret->bits = 8;
535 
536  /* official format ? */
537  if (luac_format != LUAC_54_FORMAT) {
538  ret->compiler = rz_str_new("Unofficial Lua Compiler");
539  return ret;
540  }
541  ret->compiler = rz_str_new("Official Lua Compiler");
542 
543  /* Check Size */
544  // TODO : remove this check and process different compiler options
545  if ((instruction_size != sizeof(LUA_INSTRUCTION)) ||
546  (integer_size != sizeof(LUA_INTEGER)) ||
547  (number_size != sizeof(LUA_NUMBER)) ||
548  (int_size != sizeof(LUA_INT)) ||
549  (sizet_size != sizeof(size_t))) {
550  RZ_LOG_ERROR("Size definition does not match with the expected size\n");
551  return ret;
552  }
553 
554  /* Check endian */
555  if (integer_valid != LUAC_53_INT_VALIDATION) {
556  RZ_LOG_ERROR("Integer format does not match with the expected integer\n");
557  return ret;
558  } else if (number_valid != LUAC_53_NUMBER_VALIDATION) {
559  RZ_LOG_ERROR("Number format does not match with the expected number\n");
560  return ret;
561  }
562 
563  /* parse source file name */
564  char *src_file_name = NULL;
565  int name_len;
566  lua_parse_string(buffer, ((ut8 **)&(src_file_name)), &name_len, LUAC_FILENAME_OFFSET, bf->size);
567 
568  /* put source file info into GUID */
569  ret->guid = rz_str_new(src_file_name ? src_file_name : "stripped");
570  free(src_file_name);
571 
572  return ret;
573 }
struct buffer buffer
#define minor(dev)
Definition: fsmagic.c:57
#define major(dev)
Definition: fsmagic.c:56
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
ut32 LUA_INSTRUCTION
Definition: luac_common.h:13
#define LUAC_53_HDRSIZE
Definition: luac_specs_53.h:34
#define LUAC_53_INSTRUCTION_SIZE_OFFSET
Definition: luac_specs_53.h:27
#define LUAC_53_INT_SIZE_OFFSET
Definition: luac_specs_53.h:25
#define LUAC_53_NUMBER_VALIDATION
Definition: luac_specs_53.h:20
#define LUAC_54_FORMAT
Definition: luac_specs_53.h:17
#define LUAC_FILENAME_OFFSET
Definition: luac_specs_53.h:37
#define LUAC_53_NUMBER_VALID_OFFSET
Definition: luac_specs_53.h:31
#define LUAC_53_NUMBER_SIZE_OFFSET
Definition: luac_specs_53.h:29
#define LUAC_53_FORMAT_OFFSET
Definition: luac_specs_53.h:23
#define LUAC_53_INT_VALIDATION
Definition: luac_specs_53.h:19
#define LUAC_53_SIZET_SIZE_OFFSET
Definition: luac_specs_53.h:26
#define LUAC_53_INTEGER_VALID_OFFSET
Definition: luac_specs_53.h:30
#define LUAC_53_INTEGER_SIZE_OFFSET
Definition: luac_specs_53.h:28
static double lua_load_number(RzBuffer *buffer, ut64 offset)
Definition: parse_53.c:21
static ut64 lua_load_integer(RzBuffer *buffer, ut64 offset)
Definition: parse_53.c:15
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define st64
Definition: rz_types_base.h:10
char * file
Definition: rz_bin.h:299
RzBuffer * buf
Definition: rz_bin.h:303
char * type
Definition: rz_bin.h:211
char * os
Definition: rz_bin.h:219
char * machine
Definition: rz_bin.h:216
char * bclass
Definition: rz_bin.h:212
char * guid
Definition: rz_bin.h:222
char * file
Definition: rz_bin.h:210
char * cpu
Definition: rz_bin.h:215
char * rclass
Definition: rz_bin.h:213
char * arch
Definition: rz_bin.h:214
char * compiler
Definition: rz_bin.h:244

References rz_bin_info_t::arch, rz_bin_info_t::bclass, rz_bin_info_t::bits, rz_bin_file_t::buf, rz_bin_info_t::compiler, rz_bin_info_t::cpu, rz_bin_info_t::file, rz_bin_file_t::file, free(), rz_bin_info_t::guid, lua_load_integer(), lua_load_number(), lua_parse_string(), LUAC_53_FORMAT_OFFSET, LUAC_53_HDRSIZE, LUAC_53_INSTRUCTION_SIZE_OFFSET, LUAC_53_INT_SIZE_OFFSET, LUAC_53_INT_VALIDATION, LUAC_53_INTEGER_SIZE_OFFSET, LUAC_53_INTEGER_VALID_OFFSET, LUAC_53_NUMBER_SIZE_OFFSET, LUAC_53_NUMBER_VALID_OFFSET, LUAC_53_NUMBER_VALIDATION, LUAC_53_SIZET_SIZE_OFFSET, LUAC_54_FORMAT, LUAC_FILENAME_OFFSET, rz_bin_info_t::machine, major, minor, NULL, rz_bin_info_t::os, rz_bin_info_t::rclass, rz_buf_read8_at(), RZ_LOG_ERROR, RZ_NEW0, rz_str_new(), rz_str_newf(), rz_bin_file_t::size, st64, rz_bin_info_t::type, and ut64().

Referenced by load_buffer().

◆ lua_parse_line_defined()

static ut64 lua_parse_line_defined ( LuaProto proto,
RzBuffer buffer,
ut64  offset,
ut64  data_size 
)
static

Definition at line 33 of file parse_53.c.

33  {
34  ut64 size_offset;
35  int line_defined;
36  int last_line_defined;
37 
38  size_offset = sizeof(LUA_INT) + sizeof(LUA_INT);
39 
40  if (size_offset + offset > data_size) {
41  return 0;
42  }
43  line_defined = lua_load_int(buffer, offset);
44  offset += size_offset;
45  last_line_defined = lua_load_int(buffer, offset);
46 
47  /* Set Proto Member */
48  proto->line_defined = line_defined;
49  proto->lastline_defined = last_line_defined;
50 
51  return size_offset;
52 }
ut64 lastline_defined
line number of function end
Definition: luac_common.h:51
ut64 line_defined
line number of function start
Definition: luac_common.h:50

References lua_proto_ex::lastline_defined, lua_proto_ex::line_defined, lua_load_int(), and ut64().

Referenced by lua_parse_body_53().

◆ lua_parse_name()

static ut64 lua_parse_name ( LuaProto proto,
RzBuffer buffer,
ut64  offset,
ut64  data_size 
)
static

Definition at line 101 of file parse_53.c.

101  {
102  return lua_parse_string(buffer, &proto->proto_name, &proto->name_size, offset, data_size);
103 }
int name_size
size of proto name
Definition: luac_common.h:48
ut8 * proto_name
current proto name
Definition: luac_common.h:47

References lua_parse_string(), lua_proto_ex::name_size, and lua_proto_ex::proto_name.

Referenced by lua_parse_body_53().

◆ lua_parse_protos()

static ut64 lua_parse_protos ( LuaProto proto,
RzBuffer buffer,
ut64  offset,
ut64  data_size 
)
static

Definition at line 378 of file parse_53.c.

378  {
379  rz_return_val_if_fail(proto, 0);
380 
381  int proto_cnt;
382  int i;
383  ut64 base_offset;
384  ut64 delta_offset;
385 
386  base_offset = offset; // store origin offset
387 
388  delta_offset = sizeof(LUA_INT);
389  if (offset + delta_offset > data_size) {
390  return 0;
391  }
392  proto_cnt = lua_load_int(buffer, offset);
393  offset += delta_offset;
394 
395  LuaProto *current_proto;
396  for (i = 0; i < proto_cnt; ++i) {
397  current_proto = lua_parse_body_53(buffer, offset, data_size);
398  lua_return_if_null(current_proto);
399  rz_list_append(proto->proto_entries, current_proto);
400  offset += current_proto->size - 1; // update offset
401  }
402 
403  // return the delta between offset and base_offset
404  return offset - base_offset;
405 }
#define lua_return_if_null(proto)
Definition: luac_common.h:217
LuaProto * lua_parse_body_53(RzBuffer *buffer, ut64 base_offset, ut64 data_size)
Definition: parse_53.c:407
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RzList * proto_entries
A list to store sub proto entries.
Definition: luac_common.h:73

References i, lua_load_int(), lua_parse_body_53(), lua_return_if_null, lua_proto_ex::proto_entries, rz_list_append(), rz_return_val_if_fail, lua_proto_ex::size, and ut64().

Referenced by lua_parse_body_53().

◆ lua_parse_string()

static ut64 lua_parse_string ( RzBuffer buffer,
ut8 **  dest,
int str_len,
ut64  offset,
ut64  data_size 
)
static

Definition at line 54 of file parse_53.c.

54  {
55  ut8 string_buf_size;
56  if (!rz_buf_read8_at(buffer, offset, &string_buf_size)) {
57  return 0;
58  }
59 
60  int len = 0;
61  ut64 base_offset = 0;
62  ut64 size_offset = 1;
63  ut8 *ret;
64 
65  base_offset = offset;
66 
67  // Long string
68  if (string_buf_size == 0xFF) {
69  offset += size_offset;
70  if (!rz_buf_read8_at(buffer, offset, &string_buf_size)) {
71  return 0;
72  }
73  size_offset = 1;
74  }
75 
76  offset += size_offset;
77 
78  if (string_buf_size == 0 || size_offset == 0) {
79  ret = NULL;
80  len = 0;
81  } else {
82  len = string_buf_size - 1;
83  if ((ret = RZ_NEWS(ut8, string_buf_size)) == NULL) {
84  len = 0;
85  } else {
87  ret[len] = 0x00;
88  }
89  }
90 
91  if (dest && str_len) {
92  *dest = ret;
93  *str_len = len;
94  } else {
95  RZ_LOG_ERROR("Cannot store string\n");
96  }
97 
98  return offset + len - base_offset;
99 }
size_t len
Definition: 6502dis.c:15

References dest, len, NULL, rz_buf_read8_at(), rz_buf_read_at(), RZ_LOG_ERROR, RZ_NEWS, and ut64().

Referenced by lua_parse_const_entry(), lua_parse_debug(), lua_parse_header_53(), and lua_parse_name().

◆ lua_parse_upvalue_entry()

static ut64 lua_parse_upvalue_entry ( LuaProto proto,
RzBuffer buffer,
ut64  offset,
ut64  data_size 
)
static

Definition at line 240 of file parse_53.c.

240  {
241  LuaUpvalueEntry *current_entry;
242  ut64 base_offset;
243 
244  base_offset = offset;
245  current_entry = lua_new_upvalue_entry();
246  current_entry->offset = base_offset;
247 
248  if (offset + 2 > data_size) {
249  return 0;
250  }
251 
252  /* read instack/idx attr */
253  // no kind in lua 5.3
254  if (!rz_buf_read8_at(buffer, offset + 0, &current_entry->instack) ||
255  !rz_buf_read8_at(buffer, offset + 1, &current_entry->idx)) {
256  return 0;
257  }
258  current_entry->kind = 0;
259 
260  offset += 2;
261 
262  /* add to list */
263  rz_list_append(proto->upvalue_entries, current_entry);
264 
265  return offset - base_offset;
266 }
LuaUpvalueEntry * lua_new_upvalue_entry()
Definition: luac_common.c:28
RzList * upvalue_entries
A list to store upvalue entries.
Definition: luac_common.h:68
Store upvalue attributes.
Definition: luac_common.h:104
ut64 offset
offset of this upvalue
Definition: luac_common.h:109
ut8 idx
index
Definition: luac_common.h:107
ut8 kind
kind
Definition: luac_common.h:108
ut8 instack
is in stack
Definition: luac_common.h:106

References lua_upvalue_entry::idx, lua_upvalue_entry::instack, lua_upvalue_entry::kind, lua_new_upvalue_entry(), lua_upvalue_entry::offset, rz_buf_read8_at(), rz_list_append(), lua_proto_ex::upvalue_entries, and ut64().

Referenced by lua_parse_upvalues().

◆ lua_parse_upvalues()

static ut64 lua_parse_upvalues ( LuaProto proto,
RzBuffer buffer,
ut64  offset,
ut64  data_size 
)
static

Definition at line 268 of file parse_53.c.

268  {
269  int upvalues_cnt;
270  int i;
271  ut64 base_offset;
272  ut64 delta_offset;
273 
274  base_offset = offset;
275 
276  /* parse number of upvalues */
277  delta_offset = sizeof(LUA_INT);
278  if (delta_offset + offset > data_size) {
279  return 0;
280  }
281  upvalues_cnt = lua_load_int(buffer, offset);
282  offset += delta_offset;
283 
284  for (i = 0; i < upvalues_cnt; ++i) {
285  delta_offset = lua_parse_upvalue_entry(proto, buffer, offset, data_size);
286  lua_check_error_offset(delta_offset);
287  offset += delta_offset;
288  }
289 
290  proto->upvalue_size = offset - base_offset + 1;
291 
292  return offset - base_offset;
293 }
static ut64 lua_parse_upvalue_entry(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:240
ut64 upvalue_size
upvalue section size
Definition: luac_common.h:70

References i, lua_check_error_offset, lua_load_int(), lua_parse_upvalue_entry(), lua_proto_ex::upvalue_size, and ut64().

Referenced by lua_parse_body_53().