-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua2bin.c
More file actions
258 lines (220 loc) · 7.79 KB
/
lua2bin.c
File metadata and controls
258 lines (220 loc) · 7.79 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// lua2bin - Convert lua files into binaries
// Copyright (c) 2025 Ben Daws
// MIT License
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#define VERSION "1.0.0"
#define TEMP_DIR "/tmp"
static void printhelp(const char *program_name) {
printf("lua2bin v%s - Convert Lua scripts to standalone executables\n\n", VERSION);
printf("Usage: %s [options] <input.lua> <output>\n\n", program_name);
printf("Options:\n");
printf(" -h, --help Show this help message\n");
printf(" -v, --version Show version information\n\n");
printf("Example:\n");
printf(" %s script.lua script\n", program_name);
printf(" ./script\n");
}
static void printver(void) {
printf("lua2bin v%s\n", VERSION);
printf("Using Lua ver: %s\n", LUA_VERSION);
}
static int writebytecode(lua_State *L, const void *p, size_t sz, void *ud) {
FILE *f = (FILE *)ud;
return (fwrite(p, 1, sz, f) != sz);
}
static int getbytecode(const char *input_file, const char *output_file) {
lua_State *L = luaL_newstate();
if (!L) {
fprintf(stderr, "error: Failed to create Lua state\n");
return 1;
}
if (luaL_loadfilex(L, input_file, NULL) != LUA_OK) {
fprintf(stderr, "error loading Lua file: %s\n", lua_tostring(L, -1));
lua_close(L);
return 1;
}
FILE *f = fopen(output_file, "wb");
if (!f) {
fprintf(stderr, "error: Cannot open output file: %s\n", output_file);
lua_close(L);
return 1;
}
if (lua_dump(L, writebytecode, f, 0) != 0) {
fprintf(stderr, "error: Failed to dump bytecode\n");
fclose(f);
lua_close(L);
return 1;
}
fclose(f);
lua_close(L);
return 0;
}
static int createstub(const char *bytecode_file, const char *stub_file) {
FILE *bytecode = fopen(bytecode_file, "rb");
if (!bytecode) {
fprintf(stderr, "Error: Cannot open bytecode file: %s\n", bytecode_file);
return 1;
}
fseek(bytecode, 0, SEEK_END);
long bytecode_size = ftell(bytecode);
fseek(bytecode, 0, SEEK_SET);
unsigned char *bytecode_data = malloc(bytecode_size);
if (!bytecode_data) {
fprintf(stderr, "Error: Memory allocation failed\n");
fclose(bytecode);
return 1;
}
if (fread(bytecode_data, 1, bytecode_size, bytecode) != (size_t)bytecode_size) {
fprintf(stderr, "Error: Failed to read bytecode file\n");
free(bytecode_data);
fclose(bytecode);
return 1;
}
fclose(bytecode);
FILE *stub = fopen(stub_file, "w");
if (!stub) {
fprintf(stderr, "Error: Cannot create stub file: %s\n", stub_file);
free(bytecode_data);
return 1;
}
fprintf(stub, "#include <lua.h>\n");
fprintf(stub, "#include <lualib.h>\n");
fprintf(stub, "#include <lauxlib.h>\n");
fprintf(stub, "#include <stdlib.h>\n");
fprintf(stub, "#include <string.h>\n\n");
fprintf(stub, "static const unsigned char bytecode[] = {\n");
for (long i = 0; i < bytecode_size; i++) {
if (i % 12 == 0) fprintf(stub, " ");
fprintf(stub, "0x%02x", bytecode_data[i]);
if (i < bytecode_size - 1) fprintf(stub, ",");
if ((i + 1) % 12 == 0 || i == bytecode_size - 1) fprintf(stub, "\n");
else fprintf(stub, " ");
}
fprintf(stub, "};\n\n");
fprintf(stub, "static int bytecode_size = %ld;\n\n", bytecode_size);
fprintf(stub, "static const char *lua_reader(lua_State *L, void *data, size_t *size) {\n");
fprintf(stub, " static int read = 0;\n");
fprintf(stub, " if (read) {\n");
fprintf(stub, " *size = 0;\n");
fprintf(stub, " return NULL;\n");
fprintf(stub, " }\n");
fprintf(stub, " read = 1;\n");
fprintf(stub, " *size = bytecode_size;\n");
fprintf(stub, " return (const char *)bytecode;\n");
fprintf(stub, "}\n\n");
fprintf(stub, "int main(int argc, char *argv[]) {\n");
fprintf(stub, " lua_State *L = luaL_newstate();\n");
fprintf(stub, " if (!L) return 1;\n\n");
fprintf(stub, " luaL_openlibs(L);\n\n");
fprintf(stub, " /* Create arg table */\n");
fprintf(stub, " lua_newtable(L);\n");
fprintf(stub, " for (int i = 0; i < argc; i++) {\n");
fprintf(stub, " lua_pushstring(L, argv[i]);\n");
fprintf(stub, " lua_rawseti(L, -2, i);\n");
fprintf(stub, " }\n");
fprintf(stub, " lua_setglobal(L, \"arg\");\n\n");
fprintf(stub, " /* Load and run bytecode */\n");
fprintf(stub, " if (lua_load(L, lua_reader, NULL, \"main\", \"b\") != LUA_OK) {\n");
fprintf(stub, " fprintf(stderr, \"Error loading bytecode: %%s\\n\", lua_tostring(L, -1));\n");
fprintf(stub, " lua_close(L);\n");
fprintf(stub, " return 1;\n");
fprintf(stub, " }\n\n");
fprintf(stub, " if (lua_pcall(L, 0, 0, 0) != LUA_OK) {\n");
fprintf(stub, " fprintf(stderr, \"Error: %%s\\n\", lua_tostring(L, -1));\n");
fprintf(stub, " lua_close(L);\n");
fprintf(stub, " return 1;\n");
fprintf(stub, " }\n\n");
fprintf(stub, " lua_close(L);\n");
fprintf(stub, " return 0;\n");
fprintf(stub, "}\n");
fclose(stub);
free(bytecode_data);
return 0;
}
static int makestub(const char *stub_file, const char *output_file) {
char cmd[2048];
snprintf(cmd, sizeof(cmd),
"gcc -o '%s' '%s' -I/usr/include/lua5.4 -llua5.4 -lm -ldl 2>&1",
output_file, stub_file);
FILE *proc = popen(cmd, "r");
if (!proc) {
fprintf(stderr, "error: Failed to run gcc\n");
return 1;
}
char buffer[256];
int has_error = 0;
while (fgets(buffer, sizeof(buffer), proc)) {
fprintf(stderr, "%s", buffer);
has_error = 1;
}
int status = pclose(proc);
if (status != 0 || has_error) {
fprintf(stderr, "error: Compilation failed\n");
return 1;
}
chmod(output_file, 0755);
return 0;
}
int main(int argc, char *argv[]) {
char *input_file = NULL;
char *output_file = NULL;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
printhelp(argv[0]);
return 0;
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
printver();
return 0;
} else if (argv[i][0] == '-') {
fprintf(stderr, "Error: Unknown option: %s\n", argv[i]);
printhelp(argv[0]);
return 1;
} else if (!input_file) {
input_file = argv[i];
} else if (!output_file) {
output_file = argv[i];
} else {
fprintf(stderr, "Error: Too many arguments\n");
printhelp(argv[0]);
return 1;
}
}
if (!input_file || !output_file) {
fprintf(stderr, "Error: Missing required arguments\n\n");
print_usage(argv[0]);
return 1;
}
if (access(input_file, R_OK) != 0) {
fprintf(stderr, "Error: Cannot read input file: %s\n", input_file);
return 1;
}
char bytecode_file[256];
char stub_file[256];
snprintf(bytecode_file, sizeof(bytecode_file), "%s/lua2bin_bytecode_%d.luac", TEMP_DIR, getpid());
snprintf(stub_file, sizeof(stub_file), "%s/lua2bin_stub_%d.c", TEMP_DIR, getpid());
int result = 0;
if (getbytecode(input_file, bytecode_file) != 0) {
result = 1;
goto cleanup;
}
if (createstub(bytecode_file, stub_file) != 0) {
result = 1;
goto cleanup;
}
if (makestub(stub_file, output_file) != 0) {
result = 1;
goto cleanup;
}
cleanup:
/* Clean up temp files */
unlink(bytecode_file);
unlink(stub_file);
return result;
}