Lucciefr
Lua code caving, injection and exploration framework
luahelpers.h
Go to the documentation of this file.
1 
7 #ifndef LUAHELPERS_H
8 #define LUAHELPERS_H
9 
11 #ifndef LUA_CFUNC
12 # if BITS == 32
13 # define LUA_CFUNC(fname) int __attribute__((cdecl)) fname(lua_State *L)
14 # else // cdecl convention is inappropriate for 64 bits
15 # define LUA_CFUNC(fname) int fname(lua_State *L)
16 # endif
17 #endif
18 
19 
21 #define LREG_NAME(L, func, name) \
22 do { \
23  lua_pushcfunction(L, (lua_CFunction)func); \
24  lua_setglobal(L, name); \
25 } while (0)
26 
28 #define LREG(L, func) LREG_NAME(L, func, #func)
29 
31 #define LENUM(L, value) lua_setglobal_int(L, #value, value)
32 
35 #define luaL_error_fname(L, ...) \
36 do { \
37  lua_pushstring(L, __func__); \
38  lua_pushfstring(L, "() " __VA_ARGS__); \
39  lua_concat(L, 2); \
40  lua_error(L); \
41 } while (0)
42 
44 #define LUA_CHECKIDX(L, idx) \
45 do { \
46  if (idx == 0) \
47  luaL_error(L, "%s() Lua state %p, error: " \
48  "zero is not an acceptable stack index!", __func__, L); \
49 } while (0)
50 
53 #define LUA_ABSIDX(L, idx) \
54 do { \
55  if (idx < 0) { idx += lua_gettop(L); idx++; } \
56 } while (0)
57 
59 #define LUA_CHKABSIDX(L, idx) \
60 do { \
61  if (idx == 0) \
62  luaL_error(L, "%s() Lua state %p, error: " \
63  "zero is not an acceptable stack index!", __func__, L); \
64  if (idx < 0) { idx += lua_gettop(L); idx++; } \
65 } while (0)
66 
68 #define lua_iscdata(L, index) (lua_type(L, index) == LUA_TCDATA)
69 
71 #define lua_setglobal_int(L, name, value) \
72 do { \
73  lua_pushinteger(L, value); \
74  lua_setglobal(L, name); \
75 } while (0)
76 
78 #define lua_setglobal_str(L, name, value) \
79 do { \
80  lua_pushstring(L, value); \
81  lua_setglobal(L, name); \
82 } while (0)
83 
85 #define lua_setglobal_ptr(L, name, value) \
86 do { \
87  lua_pushlightuserdata(L, value); \
88  lua_setglobal(L, name); \
89 } while (0)
90 
91 #endif // LUAHELPERS_H