Rizin
unix-like reverse engineering framework and cli tools
bin_luac.c File Reference
#include <rz_bin.h>
#include <rz_lib.h>
#include "librz/bin/format/luac/luac_common.h"

Go to the source code of this file.

Macros

#define GET_INTERNAL_BIN_INFO_OBJ(bf)   ((LuacBinInfo *)(bf)->o->bin_obj)
 

Functions

static bool check_buffer (RzBuffer *buff)
 
static bool load_buffer (RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
 
static RzBinInfoinfo (RzBinFile *bf)
 
static RzListsections (RzBinFile *bf)
 
static RzListsymbols (RzBinFile *bf)
 
static RzListentries (RzBinFile *bf)
 
static RzListstrings (RzBinFile *bf)
 

Variables

RzBinPlugin rz_bin_plugin_luac
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ GET_INTERNAL_BIN_INFO_OBJ

#define GET_INTERNAL_BIN_INFO_OBJ (   bf)    ((LuacBinInfo *)(bf)->o->bin_obj)

Definition at line 8 of file bin_luac.c.

Function Documentation

◆ check_buffer()

static bool check_buffer ( RzBuffer buff)
static

Definition at line 10 of file bin_luac.c.

10  {
11  if (rz_buf_size(buff) > 4) {
14  return !memcmp(buf, LUAC_MAGIC, LUAC_MAGIC_SIZE);
15  }
16  return false;
17 }
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
#define LUAC_MAGIC_OFFSET
Definition: luac_common.h:16
#define LUAC_MAGIC_SIZE
Definition: luac_common.h:17
#define LUAC_MAGIC
Definition: luac_common.h:21
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
RZ_API ut64 rz_buf_size(RZ_NONNULL RzBuffer *b)
Return the size of the buffer.
Definition: buf.c:1225

References LUAC_MAGIC, LUAC_MAGIC_OFFSET, LUAC_MAGIC_SIZE, rz_buf_read_at(), and rz_buf_size().

◆ entries()

static RzList* entries ( RzBinFile bf)
static

Definition at line 106 of file bin_luac.c.

106  {
107  if (!bf) {
108  return NULL;
109  }
110  LuacBinInfo *bin_info_obj = GET_INTERNAL_BIN_INFO_OBJ(bf);
111  if (!bin_info_obj) {
112  return NULL;
113  }
114 
115  return bin_info_obj->entry_list;
116 }
#define GET_INTERNAL_BIN_INFO_OBJ(bf)
Definition: bin_luac.c:8
#define NULL
Definition: cris-opc.c:27
RzList * entry_list
list of entries
Definition: luac_common.h:164

References luac_bin_info::entry_list, GET_INTERNAL_BIN_INFO_OBJ, and NULL.

◆ info()

static RzBinInfo* info ( RzBinFile bf)
static

Definition at line 70 of file bin_luac.c.

70  {
71  if (!bf) {
72  return NULL;
73  }
74  LuacBinInfo *bin_info_obj = GET_INTERNAL_BIN_INFO_OBJ(bf);
75  if (!bin_info_obj) {
76  return NULL;
77  }
78 
79  return bin_info_obj->general_info;
80 }
RzBinInfo * general_info
general binary info from luac header
Definition: luac_common.h:166

References luac_bin_info::general_info, GET_INTERNAL_BIN_INFO_OBJ, and NULL.

◆ load_buffer()

static bool load_buffer ( RzBinFile bf,
RzBinObject obj,
RzBuffer buf,
Sdb sdb 
)
static

Definition at line 19 of file bin_luac.c.

19  {
20  ut8 MAJOR_MINOR_VERSION;
21  LuacBinInfo *bin_info_obj = NULL;
22  LuaProto *proto = NULL;
23  RzBinInfo *general_info = NULL;
24  st32 major;
25  st32 minor;
26 
27  rz_buf_read_at(buf, LUAC_VERSION_OFFSET, &MAJOR_MINOR_VERSION, sizeof(MAJOR_MINOR_VERSION)); /* 1-byte in fact */
28  if ((bin_info_obj = RZ_NEW(LuacBinInfo)) == NULL) {
29  return false;
30  }
31  major = (MAJOR_MINOR_VERSION & 0xF0) >> 4;
32  minor = (MAJOR_MINOR_VERSION & 0x0F);
33 
34  if (major != 5) {
35  RZ_LOG_ERROR("currently support lua 5.x only\n");
36  return false;
37  }
38 
39  switch (minor) {
40  case 4:
41  proto = lua_parse_body_54(buf, 0x20, bf->size);
42  general_info = lua_parse_header_54(bf, major, minor);
43  break;
44  case 3:
45  proto = lua_parse_body_53(buf, 0x22, bf->size);
46  general_info = lua_parse_header_53(bf, major, minor);
47  break;
48  default:
49  RZ_LOG_ERROR("lua 5.%c not support now\n", minor + '0');
50  return false;
51  }
52 
53  bin_info_obj = luac_build_info(proto);
54  if (bin_info_obj == NULL) {
55  lua_free_proto_entry(proto);
56  rz_bin_info_free(general_info);
57  return false;
58  }
59  bin_info_obj->general_info = general_info;
60  bin_info_obj->major = major;
61  bin_info_obj->minor = minor;
62 
63  lua_free_proto_entry(proto);
64  proto = NULL;
65 
66  obj->bin_obj = bin_info_obj;
67  return true;
68 }
RZ_API void rz_bin_info_free(RzBinInfo *rb)
Definition: bin.c:97
#define minor(dev)
Definition: fsmagic.c:57
#define major(dev)
Definition: fsmagic.c:56
LuacBinInfo * luac_build_info(LuaProto *proto)
Definition: luac_bin.c:114
void lua_free_proto_entry(LuaProto *proto)
Definition: luac_common.c:119
RzBinInfo * lua_parse_header_54(RzBinFile *bf, st32 major, st32 minor)
Definition: parse_54.c:484
RzBinInfo * lua_parse_header_53(RzBinFile *bf, st32 major, st32 minor)
Definition: parse_53.c:483
LuaProto * lua_parse_body_54(RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_54.c:410
#define LUAC_VERSION_OFFSET
Definition: luac_common.h:18
LuaProto * lua_parse_body_53(RzBuffer *buffer, ut64 offset, ut64 data_size)
Definition: parse_53.c:407
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW(x)
Definition: rz_types.h:285
#define st32
Definition: rz_types_base.h:12
Store valuable info when parsing. Treat luac file body as a main function.
Definition: luac_common.h:43
st32 minor
minor version
Definition: luac_common.h:161
st32 major
major version
Definition: luac_common.h:160
void * bin_obj
Definition: rz_bin.h:293

References rz_bin_object_t::bin_obj, luac_bin_info::general_info, lua_free_proto_entry(), lua_parse_body_53(), lua_parse_body_54(), lua_parse_header_53(), lua_parse_header_54(), luac_build_info(), LUAC_VERSION_OFFSET, luac_bin_info::major, major, luac_bin_info::minor, minor, NULL, rz_bin_info_free(), rz_buf_read_at(), RZ_LOG_ERROR, RZ_NEW, rz_bin_file_t::size, and st32.

◆ sections()

static RzList* sections ( RzBinFile bf)
static

Definition at line 82 of file bin_luac.c.

82  {
83  if (!bf) {
84  return NULL;
85  }
86  LuacBinInfo *bin_info_obj = GET_INTERNAL_BIN_INFO_OBJ(bf);
87  if (!bin_info_obj) {
88  return NULL;
89  }
90 
91  return rz_list_clone(bin_info_obj->section_list);
92 }
RZ_API RZ_OWN RzList * rz_list_clone(RZ_NONNULL const RzList *list)
Shallow copies of the list (but doesn't free its elements)
Definition: list.c:496
RzList * section_list
list of sections
Definition: luac_common.h:162

References GET_INTERNAL_BIN_INFO_OBJ, NULL, rz_list_clone(), and luac_bin_info::section_list.

◆ strings()

static RzList* strings ( RzBinFile bf)
static

Definition at line 118 of file bin_luac.c.

118  {
119  if (!bf) {
120  return NULL;
121  }
122  LuacBinInfo *bin_info_obj = GET_INTERNAL_BIN_INFO_OBJ(bf);
123  if (!bin_info_obj) {
124  return NULL;
125  }
126 
127  return bin_info_obj->string_list;
128 }
RzList * string_list
list of strings
Definition: luac_common.h:165

References GET_INTERNAL_BIN_INFO_OBJ, NULL, and luac_bin_info::string_list.

◆ symbols()

static RzList* symbols ( RzBinFile bf)
static

Definition at line 94 of file bin_luac.c.

94  {
95  if (!bf) {
96  return NULL;
97  }
98  LuacBinInfo *bin_info_obj = GET_INTERNAL_BIN_INFO_OBJ(bf);
99  if (!bin_info_obj) {
100  return NULL;
101  }
102 
103  return bin_info_obj->symbol_list;
104 }
RzList * symbol_list
list of symbols
Definition: luac_common.h:163

References GET_INTERNAL_BIN_INFO_OBJ, NULL, and luac_bin_info::symbol_list.

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_BIN,
}
RzBinPlugin rz_bin_plugin_luac
Definition: bin_luac.c:130
@ RZ_LIB_TYPE_BIN
Definition: rz_lib.h:75
#define RZ_VERSION
Definition: rz_version.h:8
char * version
Definition: rz_bin.h:512

Definition at line 147 of file bin_luac.c.

◆ rz_bin_plugin_luac

RzBinPlugin rz_bin_plugin_luac
Initial value:
= {
.name = "luac",
.desc = "LUA Compiled File",
.license = "LGPL3",
.get_sdb = NULL,
.load_buffer = &load_buffer,
.check_buffer = &check_buffer,
.baddr = NULL,
.entries = &entries,
.sections = &sections,
.symbols = &symbols,
.info = &info,
.strings = &strings,
}
RZ_API RZ_OWN RzList * rz_bin_maps_of_file_sections(RZ_NONNULL RzBinFile *binfile)
Create a list of RzBinMap from RzBinSections queried from the given file.
Definition: bin.c:1040
static bool load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb)
Definition: bin_luac.c:19
static RzList * symbols(RzBinFile *bf)
Definition: bin_luac.c:94
static RzList * strings(RzBinFile *bf)
Definition: bin_luac.c:118
static RzBinInfo * info(RzBinFile *bf)
Definition: bin_luac.c:70
static bool check_buffer(RzBuffer *buff)
Definition: bin_luac.c:10
static RzList * entries(RzBinFile *bf)
Definition: bin_luac.c:106
static RzList * sections(RzBinFile *bf)
Definition: bin_luac.c:82

Definition at line 130 of file bin_luac.c.