-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathlua_modules.c
243 lines (222 loc) · 6.66 KB
/
lua_modules.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* Zenroom (DECODE project)
*
* (c) Copyright 2017-2019 Dyne.org foundation
* designed, written and maintained by Denis Roio <jaromil@dyne.org>
*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Public License as published
* by the Free Software Foundation; either version 3 of the License,
* or (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Please refer to the GNU Public License for more details.
*
* You should have received a copy of the GNU Public License along with
* this source code; if not, write to:
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <errno.h>
#include <strings.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <lua_functions.h>
#include <zenroom.h>
#include <zen_error.h>
// defined in lua_shims.c
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
#endif
extern int lualibs_load_all_detected(lua_State *L);
// from lualibs_detected (generated by make embed-lua)
extern zen_extension_t zen_extensions[];
// extern unsigned char zen_lua_init[];
// extern unsigned int zen_lua_init_len;
// extern int luaopen_crypto(lua_State *L);
extern int luaopen_octet(lua_State *L);
// extern int luaopen_rsa();
extern int luaopen_ecdh(lua_State *L);
extern int luaopen_aes(lua_State *L);
extern int luaopen_ecp(lua_State *L);
extern int luaopen_ecp2(lua_State *L);
extern int luaopen_fp12(lua_State *L);
extern int luaopen_big(lua_State *L);
extern int luaopen_hash(lua_State *L);
extern int luaopen_float(lua_State *L);
extern int luaopen_qp(lua_State *L);
extern int luaopen_ed(lua_State *L);
extern int luaopen_p256(lua_State *L);
// really loaded in lib/lua54/linit.c
// always align here for correct reference
static const luaL_Reg loadedlibs[] = {
{LUA_GNAME, luaopen_base},
{LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_UTF8LIBNAME, luaopen_utf8},
{LUA_DBLIBNAME, luaopen_debug},
{NULL, NULL}
};
int zen_load_string(lua_State *L, const char *code,
size_t size, const char *name) {
int res;
#ifdef LUA_COMPILED
res = luaL_loadbufferx(L,code,size,name,"b");
#else
res = luaL_loadbufferx(L,code,size,name,NULL);
#endif
switch (res) {
case LUA_OK: { // func(L, "%s OK %s",__func__,name);
break; }
case LUA_ERRSYNTAX: { zerror(L, "%s syntax error: %s", __func__, name); break; }
case LUA_ERRMEM: { zerror(L, "%s out of memory: %s", __func__, name); break; }
// removed in lua 5.4
#if 0
case LUA_ERRGCMM: {
zerror(L, "%s garbage collection error: %s", __func__, name);
break;
}
#endif
}
return(res);
}
int zen_exec_extension(lua_State *L, zen_extension_t *p) {
SAFE(p); // HEREs(p->name);
#ifdef __EMSCRIPTEN__
if(p->code) {
// HEREs(p->code);
if(luaL_loadfile(L, p->code)==0) {
if(lua_pcall(L, 0, LUA_MULTRET, 0) == LUA_OK) {
func(L,"loaded %s", p->name);
return 1;
}
}
}
#else
if(zen_load_string(L, p->code, *p->size, p->name)
==LUA_OK) {
// func(L,"%s %s", __func__, p->name);
// HEREn(*p->size);
// HEREp(p->code);
lua_call(L,0,1);
func(L,"loaded %s", p->name);
return 1;
}
#endif
zerror(L, "%s", lua_tostring(L, -1));
lerror(L, "%s %s", __func__, p->name); // quits with SIGABRT
fflush(stderr);
return 0;
}
int nop(lua_State *L) {
lerror(L,"illegal instruction: require");
return 0; }
#ifndef S_SPLINT_S
// src/lua_modules.c:139:15: Parse Error. Attempting to continue.
int zen_require(lua_State *L) {
SAFE(L);
size_t len;
const char *s = lua_tolstring(L, 1, &len);
// HEREs(s);
if(!s) return 0;
// require classic lua libs
const luaL_Reg *ll;
for (ll = loadedlibs; ll->name != NULL; ++ll) {
if (strcmp(ll->name, s) == 0) {
luaL_requiref(L, ll->name, ll->func, 1);
return 1;
}
}
// SAFE(zen_extensions);
zen_extension_t *p;
// require our own lua extensions (generated by embed-lua)
for (p = zen_extensions;
p->name != NULL; ++p) {
// skip init (called as last)
if (strcasecmp(p->name, s) == 0) {
return zen_exec_extension(L,p);
}
}
// require our own C to lua extensions
if(strcasecmp(s, "octet") ==0) {
luaL_requiref(L, s, luaopen_octet, 1); }
// else if(strcmp(s, "rsa") ==0) {
// luaL_requiref(L, s, luaopen_rsa, 1); return 1; }
else if(strcasecmp(s, "ecdh") ==0) {
luaL_requiref(L, s, luaopen_ecdh, 1); }
else if(strcasecmp(s, "aes") ==0) {
luaL_requiref(L, s, luaopen_aes, 1); }
else if(strcasecmp(s, "ecp") ==0) {
luaL_requiref(L, s, luaopen_ecp, 1); }
else if(strcasecmp(s, "ecp2") ==0) {
luaL_requiref(L, s, luaopen_ecp2, 1); }
else if(strcasecmp(s, "big") ==0) {
luaL_requiref(L, s, luaopen_big, 1); }
else if(strcasecmp(s, "float") ==0) {
luaL_requiref(L, s, luaopen_float, 1); }
else if(strcasecmp(s, "fp12") ==0) {
luaL_requiref(L, s, luaopen_fp12, 1); }
else if(strcasecmp(s, "hash") ==0) {
luaL_requiref(L, s, luaopen_hash, 1); }
else if(strcasecmp(s, "qp") ==0) {
luaL_requiref(L, s, luaopen_qp, 1); }
else if(strcasecmp(s, "ed") ==0) {
luaL_requiref(L, s, luaopen_ed, 1); }
else if(strcasecmp(s, "p256") ==0) {
luaL_requiref(L, s, luaopen_p256, 1); }
else {
// shall we bail out and abort execution here?
warning(L, "required extension not found: %s", s);
return 0; }
func(L,"loaded %s",s);
return 1;
}
int zen_exitcode(lua_State *L) {
int tn;
lua_Number n = lua_tonumberx(L,1,&tn);
Z(L);
if(tn)
Z->exitcode = (int)n;
else
Z->exitcode = ERR_GENERIC;
return 0;
}
int zen_require_override(lua_State *L, const int restricted) {
static const struct luaL_Reg custom_require [] =
{ {"exitcode", zen_exitcode },
{"require", zen_require },
{NULL, NULL} };
static const struct luaL_Reg custom_require_restricted [] =
{ {"exitcode", zen_exitcode },
{"require", nop },
{NULL, NULL} };
lua_getglobal(L, "_G");
if(restricted)
luaL_setfuncs(L, custom_require_restricted, 0);
else
luaL_setfuncs(L, custom_require, 0);
lua_pop(L, 1);
return 1;
}
#endif
// load the src/lua/init.lua
int zen_lua_init(lua_State *L) {
func(L, "loading lua initialisation");
zen_extension_t *p;
for (p = zen_extensions;
p->name != NULL; ++p) {
if (strcasecmp(p->name, "init") == 0)
return zen_exec_extension(L,p);
}
lua_gc(L, LUA_GCCOLLECT, 0);
lerror(L,"Error loading lua init script");
return 0;
}