Rizin
unix-like reverse engineering framework and cli tools
luac_common.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 // SPDX-FileCopyrightText: 2021 Heersin <teablearcher@gmail.com>
3 
4 // Implement Functions declared in luac_common.h
5 
6 #include "luac_common.h"
7 
10  return entry;
11 }
12 
15  return entry;
16 }
17 
20  return entry;
21 }
22 
25  return entry;
26 }
27 
30  return entry;
31 }
32 
35  return entry;
36 }
37 
39  LuaProto *proto = RZ_NEW0(LuaProto);
40  if (!proto) {
41  RZ_LOG_ERROR("Cannot allocate LuaProto\n");
42  return NULL;
43  }
44 
46  if (!proto->const_entries) {
47  RZ_LOG_ERROR("Cannot allocate Const Entry List\n");
48  goto fail;
49  }
50 
52  if (!proto->upvalue_entries) {
53  RZ_LOG_ERROR("Cannot allocate Upvalue Entry List\n");
54  goto fail;
55  }
56 
58  if (!proto->proto_entries) {
59  RZ_LOG_ERROR("Cannot allocate Proto Entry List\n");
60  goto fail;
61  }
62 
64  if (!proto->line_info_entries) {
65  RZ_LOG_ERROR("Cannot allocate Debug Line Info\n");
66  goto fail;
67  }
68 
70  if (!proto->abs_line_info_entries) {
71  RZ_LOG_ERROR("Cannot allocate Abs Line Info\n");
72  goto fail;
73  }
74 
76  if (!proto->local_var_info_entries) {
77  RZ_LOG_ERROR("Cannot allocate Local Var\n");
78  goto fail;
79  }
80 
82  if (!proto->dbg_upvalue_entries) {
83  RZ_LOG_ERROR("Cannot allocate Debug Upvalues\n");
84  goto fail;
85  }
86 
87  return proto;
88 
89 fail:
90  lua_free_proto_entry(proto);
91  return NULL;
92 }
93 
95  if (!entry) {
96  return;
97  }
98  free(entry->upvalue_name);
99  // leave entry to rz_list_free
100  free(entry);
101 }
102 
104  if (!entry) {
105  return;
106  }
107  free(entry->varname);
108  free(entry);
109 }
110 
112  if (!entry) {
113  return;
114  }
115  free(entry->data);
116  free(entry);
117 }
118 
120  if (!proto) {
121  return;
122  }
123 
124  /* free constants entries */
125  rz_list_free(proto->const_entries);
126 
127  /* free upvalue entries */
129 
130  /* free debug */
135 
136  /* recursively free protos */
137  rz_list_free(proto->proto_entries);
138 
139  free(proto->proto_name);
140  free(proto);
141 }
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
LuaLineinfoEntry * lua_new_lineinfo_entry()
Definition: luac_common.c:23
LuaConstEntry * lua_new_const_entry()
Definition: luac_common.c:33
LuaProto * lua_new_proto_entry()
Definition: luac_common.c:38
void lua_free_const_entry(LuaConstEntry *entry)
Definition: luac_common.c:111
void lua_free_local_var_entry(LuaLocalVarEntry *entry)
Definition: luac_common.c:103
LuaDbgUpvalueEntry * lua_new_dbg_upvalue_entry()
Definition: luac_common.c:8
LuaLocalVarEntry * lua_new_local_var_entry()
Definition: luac_common.c:13
LuaUpvalueEntry * lua_new_upvalue_entry()
Definition: luac_common.c:28
void lua_free_dbg_upvalue_entry(LuaDbgUpvalueEntry *entry)
Definition: luac_common.c:94
void lua_free_proto_entry(LuaProto *proto)
Definition: luac_common.c:119
LuaAbsLineinfoEntry * lua_new_abs_lineinfo_entry()
Definition: luac_common.c:18
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW0(x)
Definition: rz_types.h:284
Definition: zipcmp.c:77
Store line info attributes.
Definition: luac_common.h:127
Store constant type, data, and offset of this constant in luac file.
Definition: luac_common.h:93
Store upvalue's debug info.
Definition: luac_common.h:149
Store line info attributes.
Definition: luac_common.h:118
Store local var names and other info.
Definition: luac_common.h:137
Store valuable info when parsing. Treat luac file body as a main function.
Definition: luac_common.h:43
RzList * dbg_upvalue_entries
A list to store upvalue names.
Definition: luac_common.h:83
RzList * upvalue_entries
A list to store upvalue entries.
Definition: luac_common.h:68
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
RzList * const_entries
A list to store constant entries.
Definition: luac_common.h:63
ut8 * proto_name
current proto name
Definition: luac_common.h:47
RzList * proto_entries
A list to store sub proto entries.
Definition: luac_common.h:73
RzList * line_info_entries
A list to store line info entries.
Definition: luac_common.h:80
Store upvalue attributes.
Definition: luac_common.h:104
#define fail(test)
Definition: tests.h:29