Rizin
unix-like reverse engineering framework and cli tools
parse_54.c File Reference
#include "luac_specs_54.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 ut64 lua_parse_szint (RzBuffer *buffer, int *size, 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_line_defined (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_54 (RzBuffer *buffer, ut64 base_offset, ut64 data_size)
 
RzBinInfolua_parse_header_54 (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 6 of file parse_54.c.

6  {
7  if (offset + size > data_size) {
8  RZ_LOG_ERROR("Truncated load block at 0x%llx\n", offset);
9  return;
10  }
12 }
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_integer()

static ut64 lua_load_integer ( RzBuffer buffer,
ut64  offset 
)
static

Definition at line 14 of file parse_54.c.

14  {
15  ut64 x = 0;
17  return x;
18 }
int x
Definition: mipsasm.c:20
#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_54().

◆ lua_load_number()

static double lua_load_number ( RzBuffer buffer,
ut64  offset 
)
static

Definition at line 20 of file parse_54.c.

20  {
21  double x = 0;
23  return x;
24 }

References rz_buf_read_le64_at, ut64(), and x.

Referenced by lua_parse_header_54().

◆ lua_parse_body_54()

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

Definition at line 410 of file parse_54.c.

410  {
411  LuaProto *ret_proto; /* construted proto for return */
412  ut64 offset; /* record offset */
413  ut64 delta_offset;
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 of main proto */
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 
438  if (!rz_buf_read8_at(buffer, offset + 0, &ret_proto->num_params) ||
439  !rz_buf_read8_at(buffer, offset + 1, &ret_proto->is_vararg) ||
440  !rz_buf_read8_at(buffer, offset + 2, &ret_proto->max_stack_size)) {
441  lua_free_proto_entry(ret_proto);
442  return NULL;
443  }
444  offset += 3;
445 
446  /* parse code */
447  ret_proto->code_offset = offset;
448  delta_offset = lua_parse_code(ret_proto, buffer, offset, data_size);
449  lua_check_error_offset_proto(delta_offset, ret_proto);
450  offset += delta_offset;
451 
452  /* parse constants */
453  ret_proto->const_offset = offset;
454  delta_offset = lua_parse_consts(ret_proto, buffer, offset, data_size);
455  lua_check_error_offset_proto(delta_offset, ret_proto);
456  offset += delta_offset;
457 
458  /* parse upvalues */
459  ret_proto->upvalue_offset = offset;
460  delta_offset = lua_parse_upvalues(ret_proto, buffer, offset, data_size);
461  lua_check_error_offset_proto(delta_offset, ret_proto);
462  offset += delta_offset;
463 
464  /* parse inner protos */
465  ret_proto->inner_proto_offset = offset;
466  delta_offset = lua_parse_protos(ret_proto, buffer, offset, data_size);
467  lua_check_error_offset_proto(delta_offset, ret_proto);
468  offset += delta_offset;
469 
470  /* specially handle recursive protos size */
471  ret_proto->inner_proto_size = offset - ret_proto->inner_proto_offset;
472 
473  /* parse debug */
474  ret_proto->debug_offset = offset;
475  delta_offset = lua_parse_debug(ret_proto, buffer, offset, data_size);
476  lua_check_error_offset_proto(delta_offset, ret_proto);
477  offset += delta_offset;
478 
479  ret_proto->size = offset - base_offset + 1;
480 
481  return ret_proto;
482 }
#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_54.c:123
static ut64 lua_parse_name(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:98
static ut64 lua_parse_debug(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:286
static ut64 lua_parse_upvalues(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:263
static ut64 lua_parse_line_defined(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:102
static ut64 lua_parse_consts(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:206
static ut64 lua_parse_protos(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:385
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
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(), rz_return_val_if_fail, 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 123 of file parse_54.c.

123  {
124  ut64 size_offset;
125  ut64 total_size;
126  int code_size;
127 
128  size_offset = lua_parse_szint(buffer, &code_size, offset, data_size);
129  lua_check_error_offset(size_offset);
130  total_size = code_size * 4 + size_offset;
131 
132  if (total_size + offset > data_size) {
133  RZ_LOG_ERROR("Truncated Code at [0x%llx]\n", offset);
134  return 0;
135  }
136 
137  /* Set Proto Member */
138  proto->code_size = code_size * 4;
139  proto->code_skipped = size_offset;
140 
141  return total_size;
142 }
#define lua_check_error_offset(offset)
Definition: luac_common.h:208
static ut64 lua_parse_szint(RzBuffer *buffer, int *size, ut64 offset, ut64 data_size)
Definition: parse_54.c:28
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_check_error_offset, lua_parse_szint(), RZ_LOG_ERROR, and ut64().

Referenced by lua_parse_body_54().

◆ lua_parse_const_entry()

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

Definition at line 144 of file parse_54.c.

144  {
145  LuaConstEntry *current_entry;
146  ut64 base_offset;
147  ut8 *recv_data;
148  ut64 delta_offset;
149  int data_len;
150 
151  current_entry = lua_new_const_entry();
152  current_entry->offset = offset;
153  base_offset = offset;
154  delta_offset = 0;
155 
156  /* read TAG byte */
157  if (offset + 1 > data_size) {
158  return 0;
159  }
160 
161  if (!rz_buf_read8_at(buffer, offset, &current_entry->tag)) {
162  return 0;
163  }
164 
165  offset += 1;
166 
167  /* read data */
168  // TODO : replace 8 with Macro
169  switch (current_entry->tag) {
170  case LUA_VNUMFLT:
171  case LUA_VNUMINT:
172  data_len = 8;
173  recv_data = RZ_NEWS(ut8, data_len);
174  lua_load_block(buffer, recv_data, data_len, offset, data_size);
175  if (offset + data_len > data_size) {
176  return 0;
177  }
178  delta_offset = data_len;
179  break;
180  case LUA_VSHRSTR:
181  case LUA_VLNGSTR:
182  delta_offset = lua_parse_string(buffer, &recv_data, &data_len, offset, data_size);
183  lua_check_error_offset(delta_offset);
184  break;
185  case LUA_VNIL:
186  case LUA_VFALSE:
187  case LUA_VTRUE:
188  default:
189  recv_data = NULL;
190  data_len = 0;
191  delta_offset = 0;
192  break;
193  }
194 
195  offset += delta_offset;
196 
197  current_entry->data = recv_data;
198  current_entry->data_len = data_len;
199 
200  /* add to list */
201  rz_list_append(proto->const_entries, current_entry);
202 
203  return offset - base_offset;
204 }
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_VLNGSTR
Definition: luac_common.h:37
#define LUA_VNUMINT
Definition: luac_common.h:34
#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_VTRUE
Definition: luac_common.h:33
static ut64 lua_parse_string(RzBuffer *buffer, ut8 **dest, int *str_len, ut64 offset, ut64 data_size)
Definition: parse_54.c:61
static void lua_load_block(RzBuffer *buffer, void *dest, size_t size, ut64 offset, ut64 data_size)
Definition: parse_54.c:6
#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_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, 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 206 of file parse_54.c.

206  {
207  int consts_cnt;
208  int i;
209  ut64 base_offset;
210  ut64 delta_offset;
211 
212  base_offset = offset;
213 
214  /* parse number of constants */
215  delta_offset = lua_parse_szint(buffer, &consts_cnt, offset, data_size);
216  lua_check_error_offset(delta_offset);
217  offset += delta_offset;
218 
219  for (i = 0; i < consts_cnt; ++i) {
220  // add an entry of constant
221  delta_offset = lua_parse_const_entry(proto, buffer, offset, data_size);
222  lua_check_error_offset(delta_offset);
223  offset += delta_offset;
224  }
225 
226  proto->const_size = offset - base_offset + 1;
227  return offset - base_offset;
228 }
lzma_index ** i
Definition: index.h:629
static ut64 lua_parse_const_entry(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:144
ut64 const_size
const section size
Definition: luac_common.h:65

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

Referenced by lua_parse_body_54().

◆ lua_parse_debug()

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

Definition at line 286 of file parse_54.c.

286  {
287  int entries_cnt;
288  int i;
289  ut64 base_offset;
290  ut64 delta_offset;
291 
292  base_offset = offset;
293 
294  /* parse line info */
295  delta_offset = lua_parse_szint(buffer, &entries_cnt, offset, data_size);
296  lua_check_error_offset(delta_offset);
297  offset += delta_offset;
298  LuaLineinfoEntry *info_entry;
299  for (i = 0; i < entries_cnt; ++i) {
300  info_entry = lua_new_lineinfo_entry();
301  info_entry->offset = offset;
302  ut8 tmp;
303  if (!rz_buf_read8_at(buffer, offset, &tmp)) {
304  free(info_entry);
305  return 0;
306  }
307  info_entry->info_data = tmp;
308  rz_list_append(proto->line_info_entries, info_entry);
309  offset += 1;
310  }
311 
312  /* parse absline info */
313  delta_offset = lua_parse_szint(buffer, &entries_cnt, offset, data_size);
314  lua_check_error_offset(delta_offset);
315  offset += delta_offset;
316  LuaAbsLineinfoEntry *abs_info_entry;
317  for (i = 0; i < entries_cnt; ++i) {
318  abs_info_entry = lua_new_abs_lineinfo_entry();
319  abs_info_entry->offset = offset;
320 
321  delta_offset = lua_parse_szint(buffer, &abs_info_entry->pc, offset, data_size);
322  lua_check_error_offset(delta_offset);
323  offset += delta_offset;
324 
325  delta_offset = lua_parse_szint(buffer, &abs_info_entry->line, offset, data_size);
326  lua_check_error_offset(delta_offset);
327  offset += delta_offset;
328 
329  rz_list_append(proto->abs_line_info_entries, abs_info_entry);
330  }
331 
332  /* parse local vars */
333  delta_offset = lua_parse_szint(buffer, &entries_cnt, offset, data_size);
334  lua_check_error_offset(delta_offset);
335  offset += delta_offset;
336  LuaLocalVarEntry *var_entry;
337  for (i = 0; i < entries_cnt; ++i) {
338  var_entry = lua_new_local_var_entry();
339  var_entry->offset = offset;
340 
341  /* string */
342  delta_offset = lua_parse_string(
343  buffer,
344  &var_entry->varname, &var_entry->varname_len,
345  offset, data_size);
346  lua_check_error_offset(delta_offset);
347  offset += delta_offset;
348 
349  /* start pc -- int */
350  delta_offset = lua_parse_szint(buffer, &var_entry->start_pc, offset, data_size);
351  lua_check_error_offset(delta_offset);
352  offset += delta_offset;
353 
354  /* end pc -- int */
355  delta_offset = lua_parse_szint(buffer, &var_entry->end_pc, offset, data_size);
356  lua_check_error_offset(delta_offset);
357  offset += delta_offset;
358 
359  rz_list_append(proto->local_var_info_entries, var_entry);
360  }
361 
362  /* parse upvalue */
363  delta_offset = lua_parse_szint(buffer, &entries_cnt, offset, data_size);
364  lua_check_error_offset(delta_offset);
365  offset += delta_offset;
366  LuaDbgUpvalueEntry *dbg_upvalue_entry;
367  for (i = 0; i < entries_cnt; ++i) {
368  dbg_upvalue_entry = lua_new_dbg_upvalue_entry();
369  dbg_upvalue_entry->offset = offset;
370 
371  delta_offset = lua_parse_string(
372  buffer,
373  &dbg_upvalue_entry->upvalue_name, &dbg_upvalue_entry->name_len,
374  offset, data_size);
375  lua_check_error_offset(delta_offset);
376  offset += delta_offset;
377 
378  rz_list_append(proto->dbg_upvalue_entries, dbg_upvalue_entry);
379  }
380 
381  proto->debug_size = offset - base_offset + 1;
382  return offset - base_offset;
383 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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
LuaAbsLineinfoEntry * lua_new_abs_lineinfo_entry()
Definition: luac_common.c:18
Store line info attributes.
Definition: luac_common.h:127
int line
line number in source file
Definition: luac_common.h:129
ut64 offset
Definition: luac_common.h:130
int pc
pc value of lua
Definition: luac_common.h:128
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 * abs_line_info_entries
A list to store absolutely line info entries.
Definition: luac_common.h:81
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::abs_line_info_entries, lua_proto_ex::dbg_upvalue_entries, lua_proto_ex::debug_size, lua_local_var_entry::end_pc, free(), i, lua_lineinfo_entry::info_data, lua_abs_lineinfo_entry::line, lua_proto_ex::line_info_entries, lua_proto_ex::local_var_info_entries, lua_check_error_offset, lua_new_abs_lineinfo_entry(), lua_new_dbg_upvalue_entry(), lua_new_lineinfo_entry(), lua_new_local_var_entry(), lua_parse_string(), lua_parse_szint(), lua_dbg_upvalue_entry::name_len, lua_lineinfo_entry::offset, lua_abs_lineinfo_entry::offset, lua_local_var_entry::offset, lua_dbg_upvalue_entry::offset, lua_abs_lineinfo_entry::pc, rz_buf_read8_at(), rz_list_append(), lua_local_var_entry::start_pc, autogen_x86imm::tmp, lua_dbg_upvalue_entry::upvalue_name, ut64(), lua_local_var_entry::varname, and lua_local_var_entry::varname_len.

Referenced by lua_parse_body_54().

◆ lua_parse_header_54()

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

Definition at line 484 of file parse_54.c.

484  {
485  RzBinInfo *ret = NULL;
486  RzBuffer *buffer;
487 
488  st64 reat = bf->size;
489  if (reat < LUAC_54_HDRSIZE) {
490  RZ_LOG_ERROR("Truncated Header\n");
491  return NULL;
492  }
493  buffer = bf->buf;
494 
495  /* read header members from work buffer */
496  ut8 luac_format;
497  if (!rz_buf_read8_at(buffer, LUAC_54_FORMAT_OFFSET, &luac_format)) {
498  return NULL;
499  }
500 
501  ut8 instruction_size;
502  if (!rz_buf_read8_at(buffer, LUAC_54_INSTRUCTION_SIZE_OFFSET, &instruction_size)) {
503  return NULL;
504  }
505 
506  ut8 integer_size;
507  if (!rz_buf_read8_at(buffer, LUAC_54_INTEGER_SIZE_OFFSET, &integer_size)) {
508  return NULL;
509  }
510 
511  ut8 number_size;
512  if (!rz_buf_read8_at(buffer, LUAC_54_NUMBER_SIZE_OFFSET, &number_size)) {
513  return NULL;
514  }
515 
517  double number_valid = lua_load_number(buffer, LUAC_54_NUMBER_VALID_OFFSET);
518 
519  /* Common Ret */
520  if (!(ret = RZ_NEW0(RzBinInfo))) {
521  return NULL;
522  }
523  ret->file = rz_str_new(bf->file);
524  ret->type = rz_str_newf("Lua %c.%c compiled file", major + '0', minor + '0');
525  ret->bclass = rz_str_new("Lua compiled file");
526  ret->rclass = rz_str_new("luac");
527  ret->arch = rz_str_new("luac");
528  ret->machine = rz_str_newf("Lua %c.%c VM", major + '0', minor + '0');
529  ret->os = rz_str_newf("%c.%c", major + '0', minor + '0');
530  ret->cpu = rz_str_newf("%c.%c", major + '0', minor + '0');
531  ret->bits = 8;
532 
533  /* official format ? */
534  if (luac_format != LUAC_54_FORMAT) {
535  ret->compiler = rz_str_new("Unofficial Lua Compiler");
536  return ret;
537  }
538  ret->compiler = rz_str_new("Official Lua Compiler");
539 
540  /* Check Size */
541  if ((instruction_size != sizeof(LUA_INSTRUCTION)) ||
542  (integer_size != sizeof(LUA_INTEGER)) ||
543  (number_size != sizeof(LUA_NUMBER))) {
544  RZ_LOG_ERROR("Size definition does not match with the expected size\n");
545  return ret;
546  }
547 
548  /* Check endian */
549  if (int_valid != LUAC_54_INT_VALIDATION) {
550  RZ_LOG_ERROR("Integer format does not match with the expected integer\n");
551  return ret;
552  } else if (number_valid != LUAC_54_NUMBER_VALIDATION) {
553  RZ_LOG_ERROR("Number format does not match with the expected number\n");
554  return ret;
555  }
556 
557  /* parse source file name */
558  char *src_file_name = NULL;
559  int name_len;
560  lua_parse_string(buffer, ((ut8 **)&(src_file_name)), &name_len, LUAC_FILENAME_OFFSET, bf->size);
561 
562  /* put source file info into GUID */
563  ret->guid = rz_str_new(src_file_name ? src_file_name : "stripped");
564  free(src_file_name);
565 
566  return ret;
567 }
struct buffer buffer
#define minor(dev)
Definition: fsmagic.c:57
#define major(dev)
Definition: fsmagic.c:56
ut32 LUA_INSTRUCTION
Definition: luac_common.h:13
ut64 LUA_INTEGER
Definition: luac_specs_53.h:11
#define LUAC_54_FORMAT
Definition: luac_specs_53.h:17
double LUA_NUMBER
Definition: luac_specs_53.h:10
#define LUAC_FILENAME_OFFSET
Definition: luac_specs_53.h:37
#define LUAC_54_INTEGER_SIZE_OFFSET
Definition: luac_specs_54.h:25
#define LUAC_54_HDRSIZE
Definition: luac_specs_54.h:47
#define LUAC_54_NUMBER_SIZE_OFFSET
Definition: luac_specs_54.h:26
#define LUAC_54_INT_VALIDATION
Definition: luac_specs_54.h:44
#define LUAC_54_FORMAT_OFFSET
Definition: luac_specs_54.h:22
#define LUAC_54_NUMBER_VALIDATION
Definition: luac_specs_54.h:45
#define LUAC_54_INTEGER_VALID_OFFSET
Definition: luac_specs_54.h:27
#define LUAC_54_NUMBER_VALID_OFFSET
Definition: luac_specs_54.h:28
#define LUAC_54_INSTRUCTION_SIZE_OFFSET
Definition: luac_specs_54.h:24
static double lua_load_number(RzBuffer *buffer, ut64 offset)
Definition: parse_54.c:20
static ut64 lua_load_integer(RzBuffer *buffer, ut64 offset)
Definition: parse_54.c:14
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_54_FORMAT, LUAC_54_FORMAT_OFFSET, LUAC_54_HDRSIZE, LUAC_54_INSTRUCTION_SIZE_OFFSET, LUAC_54_INT_VALIDATION, LUAC_54_INTEGER_SIZE_OFFSET, LUAC_54_INTEGER_VALID_OFFSET, LUAC_54_NUMBER_SIZE_OFFSET, LUAC_54_NUMBER_VALID_OFFSET, LUAC_54_NUMBER_VALIDATION, 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 102 of file parse_54.c.

102  {
103  ut64 size_offset;
104  ut64 delta_offset;
105  int line_defined;
106  int last_line_defined;
107 
108  size_offset = lua_parse_szint(buffer, &line_defined, offset, data_size);
109  lua_check_error_offset(size_offset);
110 
111  delta_offset = lua_parse_szint(buffer, &last_line_defined, offset + size_offset, data_size);
112  lua_check_error_offset(delta_offset);
113 
114  size_offset += delta_offset;
115 
116  /* Set Proto Member */
117  proto->line_defined = line_defined;
118  proto->lastline_defined = last_line_defined;
119 
120  return size_offset;
121 }
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_check_error_offset, lua_parse_szint(), and ut64().

Referenced by lua_parse_body_54().

◆ lua_parse_name()

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

Definition at line 98 of file parse_54.c.

98  {
99  return lua_parse_string(buffer, &proto->proto_name, &proto->name_size, offset, data_size);
100 }
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_54().

◆ lua_parse_protos()

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

Definition at line 385 of file parse_54.c.

385  {
386  rz_return_val_if_fail(proto, 0);
387 
388  int proto_cnt;
389  int i;
390  ut64 base_offset;
391  ut64 delta_offset;
392 
393  base_offset = offset; // store origin offset
394  delta_offset = lua_parse_szint(buffer, &proto_cnt, offset, data_size); // skip size bytes
395  lua_check_error_offset(delta_offset);
396  offset += delta_offset;
397 
398  LuaProto *current_proto;
399  for (i = 0; i < proto_cnt; ++i) {
400  current_proto = lua_parse_body_54(buffer, offset, data_size);
401  lua_return_if_null(current_proto);
402  rz_list_append(proto->proto_entries, current_proto);
403  offset += current_proto->size - 1; // update offset
404  }
405 
406  // return the delta between offset and base_offset
407  return offset - base_offset;
408 }
#define lua_return_if_null(proto)
Definition: luac_common.h:217
LuaProto * lua_parse_body_54(RzBuffer *buffer, ut64 base_offset, ut64 data_size)
Definition: parse_54.c:410
RzList * proto_entries
A list to store sub proto entries.
Definition: luac_common.h:73

References i, lua_check_error_offset, lua_parse_body_54(), lua_parse_szint(), 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_54().

◆ lua_parse_string()

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

Definition at line 61 of file parse_54.c.

61  {
62  ut64 size_offset;
63  ut64 total_offset;
64  int ret_buf_size;
65  int string_len;
66  ut8 *ret;
67 
68  size_offset = lua_parse_szint(buffer, &ret_buf_size, offset, data_size);
69  lua_check_error_offset(size_offset);
70 
71  /* no string */
72  if (ret_buf_size == 0) {
73  ret = NULL;
74  string_len = 0;
75  } else {
76  /* skip size byte */
77  string_len = ret_buf_size - 1;
78  if ((ret = RZ_NEWS(ut8, ret_buf_size)) == NULL) {
79  string_len = 0;
80  } else {
81  rz_buf_read_at(buffer, offset + size_offset, ret, string_len);
82  ret[string_len] = 0x00;
83  }
84  }
85 
86  /* set to outside vars */
87  if (dest && str_len) {
88  *dest = ret;
89  *str_len = string_len;
90  } else {
91  RZ_LOG_ERROR("cannot store string\n");
92  }
93 
94  total_offset = size_offset + string_len;
95  return total_offset;
96 }

References dest, lua_check_error_offset, lua_parse_szint(), NULL, rz_buf_read_at(), RZ_LOG_ERROR, RZ_NEWS, and ut64().

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

◆ lua_parse_szint()

static ut64 lua_parse_szint ( RzBuffer buffer,
int size,
ut64  offset,
ut64  data_size 
)
static

Definition at line 28 of file parse_54.c.

28  {
29  int x = 0;
30  int b;
31  int i = 0;
32  ut32 limit = (~(ut32)0);
33  limit >>= 7;
34 
35  // 1 byte at least
36  if (offset + 1 > data_size) {
37  RZ_LOG_ERROR("Truncated integer size at 0x%llx\n", offset);
38  return 0;
39  }
40 
41  do {
42  ut8 tmp;
43  if (!rz_buf_read8_at(buffer, offset + i, &tmp)) {
44  return 0;
45  }
46 
47  b = tmp;
48 
49  i += 1;
50  if (x >= limit) {
51  RZ_LOG_ERROR("integer overflow while decoding integer size\n");
52  return 0;
53  }
54  x = (x << 7) | (b & 0x7f);
55  } while (((b & 0x80) == 0) && (i + offset < data_size));
56 
57  *size = x;
58  return i;
59 }
uint32_t ut32
static uint32_t const uint8_t uint32_t uint32_t limit
Definition: memcmplen.h:45
#define b(i)
Definition: sha256.c:42

References b, i, limit, rz_buf_read8_at(), RZ_LOG_ERROR, autogen_x86imm::tmp, and x.

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

◆ lua_parse_upvalue_entry()

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

Definition at line 230 of file parse_54.c.

230  {
231  LuaUpvalueEntry *current_entry;
232  ut64 base_offset;
233 
234  base_offset = offset;
235  current_entry = lua_new_upvalue_entry();
236  current_entry->offset = base_offset;
237 
238  if (offset + 3 > data_size) {
239  return 0;
240  }
241 
242  /* read instack/idx/kind attr */
243  if (!rz_buf_read8_at(buffer, offset + 0, &current_entry->instack)) {
244  return 0;
245  }
246 
247  if (!rz_buf_read8_at(buffer, offset + 1, &current_entry->idx)) {
248  return 0;
249  }
250 
251  if (!rz_buf_read8_at(buffer, offset + 2, &current_entry->kind)) {
252  return 0;
253  }
254 
255  offset += 3;
256 
257  /* add to list */
258  rz_list_append(proto->upvalue_entries, current_entry);
259 
260  return offset - base_offset;
261 }
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 263 of file parse_54.c.

263  {
264  int upvalues_cnt;
265  int i;
266  ut64 base_offset;
267  ut64 delta_offset;
268 
269  base_offset = offset;
270 
271  /* parse number of upvalues */
272  delta_offset = lua_parse_szint(buffer, &upvalues_cnt, offset, data_size);
273  lua_check_error_offset(delta_offset);
274  offset += delta_offset;
275 
276  for (i = 0; i < upvalues_cnt; ++i) {
277  delta_offset = lua_parse_upvalue_entry(proto, buffer, offset, data_size);
278  lua_check_error_offset(delta_offset);
279  offset += delta_offset;
280  }
281 
282  proto->upvalue_size = offset - base_offset + 1;
283 
284  return offset - base_offset;
285 }
static ut64 lua_parse_upvalue_entry(LuaProto *proto, RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:230
ut64 upvalue_size
upvalue section size
Definition: luac_common.h:70

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

Referenced by lua_parse_body_54().