-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlbase.c
More file actions
193 lines (171 loc) · 5 KB
/
Copy pathlbase.c
File metadata and controls
193 lines (171 loc) · 5 KB
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
// This code is part of lua2exe
// Base executable VM
#include<lua/lua.h>
#include<lua/lualib.h>
#include<lua/lauxlib.h>
#include<stdlib.h>
#include<stdio.h>
#include<stdint.h>
#ifndef NO_COMPRESSION
#include<lz4.h>
#endif
#pragma pack(1)
#define ENTRIES_KEY "$L2E"
typedef struct {
int32_t fileTableOffset;
char magic[8];
} trailer;
typedef struct {
uint32_t compressedLen;
uint32_t uncompressedLen;
char compressed;
} entry;
static const char* const ENTRY_PATHS[] = {
LUA_PATH_MARK ".lua",
LUA_PATH_MARK ".lc",
LUA_PATH_MARK LUA_DIRSEP "init.lua",
LUA_PATH_MARK LUA_DIRSEP "init.lc",
NULL
};
#if defined __WIN32 || defined __WIN64
#include<windows.h>
static const char* getSelfName (const char* argv0) {
char buf[512];
if (!GetModuleFileNameA(NULL, buf, 511)) return NULL;
return strdup(buf);
}
#else
#error Platform currently unsupported
#endif
static int loadentry (lua_State* L, FILE* fpSelf, const char* entryName) {
static char* buf1 = NULL, *buf2 = NULL;
static uint32_t buflen1=0, buflen2=0;
if (LUA_TTABLE != lua_getfield(L, LUA_REGISTRYINDEX, ENTRIES_KEY)) {
lua_pop(L, 1);
return LUA_ERRERR;
}
if (LUA_TNUMBER != lua_getfield(L, -1, entryName)) {
lua_pop(L, 2);
return LUA_ERRFILE;
}
int32_t offset = lua_tointeger(L, -1);
lua_pop(L, 2);
if (fseek(fpSelf, offset, SEEK_SET)) return LUA_ERRERR;
entry e = { 0, 0, 0 };
if (1!=fread(&e, sizeof(e), 1, fpSelf)) return LUA_ERRERR;
if (!buf1 || buflen1<e.compressedLen) buf1 = realloc(buf1, buflen1=e.compressedLen);
if (! buf2 || buflen2<e.uncompressedLen) buf2 = realloc(buf2, buflen2=e.uncompressedLen);
if (!buf1 || !buf2) return LUA_ERRMEM;
if (1!=fread(buf1, e.compressedLen, 1, fpSelf)) return LUA_ERRERR;
char namebuf[512]={0};
snprintf(namebuf, 511, "@%s", entryName);
char* buf=buf1; int len=buflen1;
#ifndef NO_COMPRESSION
if (e.compressed) {
buf=buf2;
len = LZ4_decompress_safe(buf1, buf2, e.compressedLen, e.uncompressedLen);
if (len<0) return luaL_error(L, "decompression error %d", len);
}
#endif
buf[len]=0;
return luaL_loadbufferx(L, buf, len, namebuf, "bt");
}
static int lua_loadentry (lua_State* L) {
FILE* fpSelf = (FILE*) lua_topointer(L, lua_upvalueindex(1));
const char* name = luaL_gsub(L, luaL_checkstring(L, 1), ".", LUA_DIRSEP);
int ntop = lua_gettop(L);
for (const char* const* path = ENTRY_PATHS; *path; path++) {
const char* entryName = luaL_gsub(L, *path, LUA_PATH_MARK, name);
if (LUA_OK == loadentry(L, fpSelf, entryName)) {
lua_pushfstring(L, "Resource %s", entryName);
return 2;
}}
int etop = lua_gettop(L);
luaL_Buffer b;
luaL_buffinit(L, &b);
for (int i=ntop+1; i<=etop; i++) {
luaL_addstring(&b, "No resource ");
lua_pushvalue(L, i);
luaL_addvalue(&b);
if (i<etop) luaL_addstring(&b, "\n");
}
luaL_pushresult(&b);
return 1;
}
int lua_getbacktrace (lua_State* l) {
lua_getglobal(l, "debug");
lua_getfield(l, -1, "traceback");
lua_pushvalue(l,1);
lua_pushinteger(l,2);
if (lua_pcall(l, 2, 1, 0)) {
printf("Error in error handling !\r\n");
}
return 1;
}
int main (int argc, char** argv) {
lua_State* L = NULL;
FILE* fpSelf = NULL;
const char* fnSelf = NULL;
trailer trail = {0};
#define fail(E) { fprintf(stderr, E "\n", fnSelf); goto end; }
L = luaL_newstate();
if (!L) fail("Couldn't allocate lua state")
luaL_openlibs(L);
fnSelf = getSelfName(argv[0]);
if (!fnSelf || !*fnSelf) fail("Unable to determine self executable path")
fpSelf = fopen(fnSelf, "rb");
if (!fpSelf) fail("Couldn't open self executable %s")
if (fseek(fpSelf, (size_t)-sizeof(trailer), SEEK_END)) fail("Couldn't seek to trailer")
if (!fread(&trail, sizeof(trailer), 1, fpSelf)) fail("Couldn't read trailer")
if (strncmp(trail.magic, "lua2exe1", 8)) fail("magic value error")
if (fseek(fpSelf, trail.fileTableOffset, SEEK_SET)) fail("Couldn't seek to file table")
int32_t nameLen, entryPos, entryCount=0;
char entryName[512] = {0};
lua_settop(L, 0);
lua_newtable(L);
while (
fread(&nameLen, 4, 1, fpSelf)
&& nameLen!=-1
&& fread(entryName, nameLen, 1, fpSelf)
&& fread(&entryPos, 4, 1, fpSelf)
&& entryPos!=-1 && entryPos>0
) {
entryName[nameLen] = 0;
lua_pushinteger(L, entryPos);
lua_setfield(L, -2, entryName);
lua_pushstring(L, entryName);
lua_rawseti(L, -2, ++entryCount);
}
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, ENTRIES_KEY);
lua_newtable(L);
for (int i=0; i<argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i);
}
lua_setglobal(L, "arg");
// Put loader into package.searchers
lua_getglobal(L, "package");
lua_getglobal(L, "table");
lua_getfield(L, -1, "insert");
lua_getfield(L, -3, "searchers");
lua_pushinteger(L, 2);
lua_pushlightuserdata(L, fpSelf);
lua_pushcclosure(L, &lua_loadentry, 1);
lua_call(L, 3, 0);
lua_pop(L, 2);
// Load the first entry
lua_pushcclosure(L, lua_getbacktrace, 0);
lua_rawgeti(L, -2, 1);
int re = loadentry(L, fpSelf, lua_tostring(L, -1));
if (re) fprintf(stderr, "%d! %s\n", re, lua_tostring(L, -1));
for (int i=1; i<argc; i++) lua_pushstring(L, argv[i]);
if (lua_pcall(L, argc -1, 1, 1-argc)) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
}
else if (lua_gettop(L)>=1 && lua_isnumber(L,-1)) return lua_tointeger(L,-1);
else return 0;
end:
return 1;
#undef fail
}