├── 2.lua ├── 1.lua ├── lua2exe.c ├── readme.txt ├── .gitignore ├── 1 - Copie.lua ├── loader.lua ├── lua2exe.h ├── c.bat ├── loader.c ├── bin2c.lua └── lua.c /2.lua: -------------------------------------------------------------------------------- 1 | print('Ca marche pas !') -------------------------------------------------------------------------------- /1.lua: -------------------------------------------------------------------------------- 1 | print("Bonjour!") 2 | print('Ca marche !') 3 | require '2' 4 | -------------------------------------------------------------------------------- /lua2exe.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qtnc/lua2exe/HEAD/lua2exe.c -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qtnc/lua2exe/HEAD/readme.txt -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .svn/ 2 | obj/ 3 | *.o 4 | *.exe 5 | *.dll 6 | *.tmp 7 | *.log 8 | *.zip 9 | *.lvm 10 | *.lc -------------------------------------------------------------------------------- /1 - Copie.lua: -------------------------------------------------------------------------------- 1 | local ffi = require('ffi') 2 | ffi.cdef[[ 3 | int __stdcall MessageBoxA (void* hwnd, const char* msg, const char* title, int options) ; 4 | ]] 5 | ffi.C.MessageBoxA(nil, 'Niark niark niark !', 'Hello world !', 48) 6 | -------------------------------------------------------------------------------- /loader.lua: -------------------------------------------------------------------------------- 1 | -- This script is part of lua2exe 2 | -- Loader for embedded scripts 3 | local load2 = loadembed 4 | local names = embeddedfiles 5 | local function loader (name) 6 | if names[name] then 7 | return load2(name, names[name]) 8 | else return 'No embedded module \''..name ..'\'' 9 | end end 10 | loadembed = nil 11 | embeddedfiles = nil 12 | table.insert(package.loaders, 2, loader) 13 | -------------------------------------------------------------------------------- /lua2exe.h: -------------------------------------------------------------------------------- 1 | // This code is part of lua2exe 2 | // Lua2exe common header file 3 | #ifndef LUA2EXE 4 | #define LUA2EXE 5 | 6 | // Lua2exe magic number = ascii "lua2exe\0" 7 | #define MAGIC 0x6578653261756C00LL 8 | 9 | typedef union { 10 | struct { unsigned long n1, n2; }; 11 | double d; 12 | } varnum; 13 | 14 | typedef struct { 15 | unsigned long offset; 16 | unsigned long length :22; 17 | unsigned long namelength :10; 18 | } EmbeddedChunkInfo; 19 | 20 | typedef struct { 21 | unsigned long offset, length, count; 22 | unsigned long long magic; 23 | } GlobalChunkInfo; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /c.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | luajit -b loader.lua loader.lc 3 | luajit bin2c.lua loader.lc >loader.c 4 | gcc -w -c lua2exe.c -o obj\lua2exe.o -s -O3 -Os 5 | gcc obj\lua2exe.o -o lua2exe.exe -lluajit -lzlib -s -O3 -Os 6 | gcc -w -c lua.c -o obj\lua.o -s -O3 -Os 7 | gcc obj\lua.o -o lua.lvm -lluajit -lzlib -s -O3 -Os 8 | upx -9 -q lua.lvm |null 9 | gcc obj\lua.o -o luaw.lvm -lluajit -lzlib -mwindows -s -O3 -Os 10 | upx -9 -q luaw.lvm |null 11 | zip -9 -u -q lua2exe.zip lua2exe.exe *.dll *.lvm readme.txt 12 | zip -9 -u -q lua2exe-src.zip lua2exe.exe *.dll *.lvm *.c *.h bin2c.lua loader.lc loader.lua readme.txt -------------------------------------------------------------------------------- /loader.c: -------------------------------------------------------------------------------- 1 | /* code automatically generated by bin2c -- DO NOT EDIT */ 2 | { 3 | /* #include'ing this file in a C program is equivalent to calling 4 | if (luaL_loadfile(L,"loader.lc")==0) lua_pcall(L, 0, 0, 0); 5 | */ 6 | /* loader.lc */ 7 | static const unsigned char B1[]={ 8 | 27, 76, 74, 1, 2, 98, 0, 1, 4, 2, 2, 0, 16, 43, 1, 0, 0, 54, 1, 0, 9 | 1, 15, 0, 1, 0, 84, 2, 6,128, 43, 1, 1, 0, 16, 2, 0, 0, 43, 3, 0, 10 | 0, 54, 3, 0, 3, 64, 1, 3, 0, 84, 1, 5,128, 37, 1, 0, 0, 16, 2, 0, 11 | 0, 37, 3, 1, 0, 36, 1, 3, 1, 72, 1, 2, 0, 71, 0, 1, 0, 1,192, 0, 12 | 192, 6, 39, 25, 78,111, 32,101,109, 98,101,100,100,101,100, 32,109,111,100,117, 13 | 108,101, 32, 39,125, 3, 0, 7, 0, 7, 0, 16, 52, 0, 0, 0, 52, 1, 1, 0, 14 | 49, 2, 2, 0, 41, 3, 0, 0, 53, 3, 0, 0, 41, 3, 0, 0, 53, 3, 1, 0, 15 | 52, 3, 3, 0, 55, 3, 4, 3, 52, 4, 5, 0, 55, 4, 6, 4, 39, 5, 2, 0, 16 | 16, 6, 2, 0, 62, 3, 4, 1, 48, 0, 0,128, 71, 0, 1, 0, 12,108,111, 97, 17 | 100,101,114,115, 12,112, 97, 99,107, 97,103,101, 11,105,110,115,101,114,116, 10, 18 | 116, 97, 98,108,101, 0, 18,101,109, 98,101,100,100,101,100,102,105,108,101,115, 19 | 14,108,111, 97,100,101,109, 98,101,100, 0, 20 | }; 21 | 22 | if (luaL_loadbuffer(L,(const char*)B1,sizeof(B1),"loader.lc")==0) lua_pcall(L, 0, 0, 0); 23 | } 24 | -------------------------------------------------------------------------------- /bin2c.lua: -------------------------------------------------------------------------------- 1 | local description = [=[ 2 | Usage: lua bin2c.lua [+]filename [status] 3 | 4 | Write a C source file to standard output. When this C source file is 5 | included in another C source file, it has the effect of loading and 6 | running the specified file at that point in the program. 7 | 8 | The file named by 'filename' contains either Lua byte code or Lua source. 9 | Its contents are used to generate the C output. If + is used, then the 10 | contents of 'filename' are first compiled before being used to generate 11 | the C output. If given, 'status' names a C variable used to store the 12 | return value of either luaL_loadbuffer() or lua_pcall(). Otherwise, 13 | the return values of these functions will be unavailable. 14 | 15 | This program is (overly) careful to generate output identical to the 16 | output generated by bin2c5.1 from LuaBinaries. 17 | 18 | http://lua-users.org/wiki/BinTwoCee 19 | 20 | Original author: Mark Edgar 21 | Licensed under the same terms as Lua (MIT license). 22 | ]=] 23 | 24 | if not arg or not arg[1] then 25 | io.stderr:write(description) 26 | return 27 | end 28 | 29 | local compile, filename = arg[1]:match"^(+?)(.*)" 30 | local status = arg[2] 31 | 32 | local content = compile=="+" 33 | and string.dump(assert(loadfile(filename))) 34 | or assert(io.open(filename,"rb")):read"*a" 35 | 36 | local function boilerplate(fmt) 37 | return string.format(fmt, 38 | status and "("..status.."=" or "", 39 | filename, 40 | status and ")" or "", 41 | status and status.."=" or "", 42 | filename) 43 | end 44 | 45 | local dump do 46 | local numtab={}; for i=0,255 do numtab[string.char(i)]=("%3d,"):format(i) end 47 | function dump(str) 48 | return (str:gsub(".", numtab):gsub(("."):rep(80), "%0\n")) 49 | end 50 | end 51 | 52 | io.write(boilerplate[=[ 53 | /* code automatically generated by bin2c -- DO NOT EDIT */ 54 | { 55 | /* #include'ing this file in a C program is equivalent to calling 56 | if (%sluaL_loadfile(L,%q)%s==0) %slua_pcall(L, 0, 0, 0); 57 | */ 58 | /* %s */ 59 | static const unsigned char B1[]={ 60 | ]=], dump(content), boilerplate[=[ 61 | 62 | }; 63 | 64 | if (%sluaL_loadbuffer(L,(const char*)B1,sizeof(B1),%q)%s==0) %slua_pcall(L, 0, 0, 0); 65 | } 66 | ]=]) 67 | -------------------------------------------------------------------------------- /lua.c: -------------------------------------------------------------------------------- 1 | // This code is part of lua2exe 2 | // Base executable VM 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "lua2exe.h" 9 | 10 | lua_State* l = NULL; 11 | GlobalChunkInfo ginfo = {0, 0, 0}; 12 | char selfname[300] = {0}; 13 | 14 | #if defined __WIN32 || defined __WIN64 15 | #include 16 | static BOOL getSelfName (char* buf, int len) { 17 | return GetModuleFileNameA(NULL, buf,len); 18 | } 19 | #else 20 | #error Platform currently unsupported 21 | #endif 22 | 23 | const char* doDecompress (const char* in, int inlen, int* outlen) { 24 | int buflen = inlen; 25 | char* buf = malloc(buflen); 26 | if (!buf) return NULL; 27 | z_stream z; 28 | memset(&z, 0, sizeof(z)); 29 | z.next_in = in; 30 | z.avail_in = inlen; 31 | z.next_out = buf; 32 | z.avail_out = buflen; 33 | if (Z_OK!=inflateInit(&z)) return NULL; 34 | while (Z_OK==inflate(&z,0)) { 35 | int newlen = buflen*3/2+1; 36 | buf = realloc(buf, newlen); 37 | z.next_out = buf + buflen; 38 | z.avail_out = newlen -buflen; 39 | buflen = newlen; 40 | } 41 | if (outlen) *outlen = buflen -z.avail_out; 42 | return buf; 43 | } 44 | 45 | 46 | static int loadembedded (lua_State* l, const char* name, unsigned long offset, unsigned long length) { 47 | char* buf = malloc(length); 48 | if (!buf) return LUA_ERRMEM; 49 | FILE* fp = fopen(selfname, "rb"); 50 | if (!fp) { lua_pushstring(l, "Unable to read into the executable file"); return -3; } 51 | fseek(fp, offset, SEEK_SET); 52 | int n=0, total=0; 53 | while (total0) total+=n; 54 | fclose(fp); 55 | if (total=0) n+=m; 101 | fclose(fp); 102 | 103 | char firstname[1024]={0}; 104 | unsigned long firstoffset=0, firstlength=0; 105 | lua_newtable(l); 106 | m=n=0; 107 | EmbeddedChunkInfo e; 108 | for(n=0; n=1 && lua_isnumber(l,-1)) return lua_tointeger(l,-1); 142 | else return 0; 143 | 144 | luaerror: 145 | fprintf(stderr, "%s", lua_tostring(l,-1)); 146 | return 3; 147 | 148 | error: 149 | fprintf(stderr, "Unable to run this program; corrupted executable, permission problem or insufficient memory\r\n"); 150 | return 2; 151 | } 152 | --------------------------------------------------------------------------------