Lucciefr
Lua code caving, injection and exploration framework
luautils.h
1 // luautils.h
2 
3 #ifndef LUAUTILS_H
4 #define LUAUTILS_H
5 
6 #include "bool.h"
7 #include "lua.h"
8 #include "lualib.h"
9 #include "lauxlib.h"
10 #include "lj_obj.h" // LUA_TCDATA (luajit FFI type identifier for <cdata>)
11 
12 #define MAX_TRACE_DEPTH 20 /* maximum depth for stack traces */
13 
14 // helper macros
15 #include "luahelpers.h"
16 
17 // Lua table key->value helpers
18 #define LUAUTILS_KV_HEADER_ONLY
19 #include "luautils_kv.inc"
20 
21 // native/'natural' CPU register type
22 // TODO: I moved these here for luautils_get(), but they probably belong into another .h?
23 #if BITS == 32
24  typedef uint32_t cpureg_t;
25 #elif BITS == 64
26  typedef uint64_t cpureg_t;
27 #endif
28 
29 // retrieve (and push) function via module and name
30 bool luautils_getfunction(lua_State *L, const char *module,
31  char const *function, bool propagate);
32 
33 // create (Lua registry) reference for a function, via module and name
34 int luautils_getfuncref(lua_State *L, const char *module,
35  char const *function);
36 
37 // safeguarded Lua execution
38 
39 // protect against exceptions during Lua execution, similar to lua_pcall()
40 int lua_guarded_pcall(lua_State *L, int nargs, int nresults, int errfunc);
41 
42 // protected CFunction execution, similar to lua_cpcall()
43 bool luautils_cpcall(lua_State *L, lua_CFunction func, const char *fname, int nargs);
44 
45 // helper macro to pass the function name automatically
46 #define LUA_CPCALL(L, func, nargs) luautils_cpcall(L, func, #func, nargs)
47 
48 // check luaL_dostring result, will print error message
49 bool luautils_dostring (lua_State *L, const char *str);
50 
51 
52 /* LUA ADDITIONS */
53 
54 // numeric conversions that support / work with int32_t and uint32_t ranges
55 int32_t luautils_toint32(lua_State *L, int idx);
56 #define luautils_touint32(L, idx) (uint32_t)luautils_toint32(L, idx)
57 
58 uint32_t luautils_asuint32(lua_State *L, int idx); // 'implicit' conversion
59 
60 // utility functions (for C)
61 
62 void luautils_stackclean(lua_State *L, int basepointer); // stack cleanup
63 
64 void luautils_pushptr(lua_State *L, const void *ptr); // push pointer to Lua
65 void luautils_pushwstring(lua_State *L, const wchar_t *s, int len); // push wide string
66 
67 // push unsigned 32-bit integer. DON'T use this for pointers, unless you're asking for 64-bit trouble
68 // #define luautils_pushuint32(L, number) lua_pushnumber(L, (uint32_t)(number))
69 // "push pointer numeric" - achieves something similar, while staying safe on pointer conversions
70 #define luautils_pushptrnum(L, value) lua_pushnumber(L, (uintptr_t)(value))
71 
72 void *lua_getBuffer(lua_State *L, int idx, size_t *len); // retrieve address and length for a buffer
73 
74 // checks whether the lua object is convertible to a C void*. If so returns it. If not throws error.
75 void *luautils_checkptr(lua_State *L, int idx);
76 bool luautils_isptr(lua_State *L, int idx, void* *value);
77 void *luautils_toptr(lua_State *L, int idx);
78 cpureg_t luautils_tocpu(lua_State *L, int idx); // also converts bools, strings... to register arguments
79 
80 // pointer to number (pushes result to Lua stack)
81 void luautils_ptrtonumber(lua_State *L, int idx, int offset, bool nil_is_zero);
82 // pointer to string (pushes result to Lua stack)
83 void luautils_ptrtostring(lua_State *L, int idx, int format);
84 
85 /* DEPRECATED
86 // returns the value of a global Lua variable in "CPU format"
87 cpureg_t luautils_get(lua_State *L, const char *global);
88 */
89 
90 // a lua_equal() counterpart that handles <cdata> types
91 bool luautils_equal(lua_State *L, int index1, int index2);
92 
93 bool luautils_isEmpty(lua_State *L, int idx); // test for "empty" value
94 
95 // run .lua script with support for compiled-in "fallbacks"
96 int luautils_dofile(lua_State *L, const char *filename, bool stacktrace);
97 
98 // tables
99 int luautils_table_keys(lua_State *L, int table, int filter); // push table with keys of a given table
100 bool luautils_table_keyof(lua_State *L, int table); // find key of value on stack (for given table index)
101 size_t luautils_table_count(lua_State *L, int idx, int *maxn); // a low-level table count / maxn
102 bool luautils_table_issequential(lua_State *L, int idx);
103 void luautils_table_append(lua_State *L, int idx, int pos, int pop); // append value to table
104 void luautils_table_merge(lua_State *L, int from, int to, int dest); // merge associative tables
105 // luautils_table_concat // merge non-associative tables (concat "arrays")
106 
107 int luautils_xpack(lua_State *L, int from, int to); // table "pack", [x]unpack counterpart
108 int luautils_xunpack(lua_State *L, int table, int from, int to); // table unpack, considers t[0]
109 
110 // an extended version of lua_pushfstring (with full vsnprintf capabilities)
111 const char *luautils_pushfstring(lua_State *L, const char *fmt, ...);
112 
113 // helper function for library initialization / setup of bindings
114 void libopen(lua_State *L, lua_CFunction func, const char *fname,
115  int expect_args, int pop_args);
116 
117 // C modules
118 bool luautils_require(lua_State *L, const char *module_name);
119 //void luautils_setloaded(lua_State *L, int index, const char *name);
120 void luautils_cmodule(lua_State *L, const char *module_name);
121 
122 // Lua debug functions
123 
124 LUA_CFUNC(luaStackTrace_C);
125 LUA_CFUNC(luaStackDump_C);
126 bool luaListVars(lua_State *L, int level);
127 LUA_CFUNC(luaListVars_C);
128 
129 char *lua_callerPosition(lua_State *L, int level);
130 bool luautils_pushinfo(lua_State *L, const char *what, lua_Debug *ar);
131 
132 #define luaL_addliteral(B, s) luaL_addlstring(B, "" s, sizeof(s)-1)
133 void luaL_addfmt(luaL_Buffer *B, const char *fmt, ...);
134 void luautils_checktype(lua_State *L, int index, int type, const char *where);
135 
136 #endif // LUAUTILS_H
#define LUA_CFUNC(fname)
a macro for proper Lua CFunction declarations
Definition: luahelpers.h:15
Lua helpers declared as macros.
boolean data type and constants/macros