[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: turnning lua code to executable in a C program
- From: Amir Ramezani <amir.ramezani1370@...>
- Date: Mon, 24 Aug 2015 10:14:56 +0430
hello,
i want to turn a lua code in my application to executable!
i've wrote a code from
http://github.com/qtnc/lua2exe
but it doesn't work
this is my compiler class which bind's the script:
class compiler
{
private:
//compression function for compress the lua scripts
const char* compress (const char* in, int inlen, int* outlen)
{
char* buffer = (char*) malloc(inlen+8);
if (buffer==nullptr)
{
return nullptr;
}
z_stream z;
memset(&z, 0, sizeof(z));
z.next_in = in;
z.avail_in = inlen;
z.next_out = buffer;
z.avail_out = inlen+8;
if (deflateInit(&z,9)!=Z_OK)
{
return nullptr;
}
if (deflate(&z,Z_FINISH)!=Z_STREAM_END)
{
return nullptr;
}
if (outlen)
{
*outlen = inlen +8 -z.avail_out;
}
return buffer;
}
//encryption function in order to prevent the executable from accessed
and cracked by others
char* encrypt(char* in, int len)
{
BLOWFISH_CTX c;
Blowfish_Init(&c, encryption_code, len);
char* out;
Blowfish_Encrypt(&c, (int*)in, (int*)out);
return out;
}
//this function writes the executable VM and the script into executable file
int writechunk (lua_State* l, chunkinfo* i, FILE* out, const char*
chunkname, bool dbg)
{
int n=0, t=0, length=0, clength=0;
lua_pushboolean(l, !dbg);
lua_call(l, 2, 1);
const char* data = lua_tolstring(l, -1, &length);
const char*cdata = compress(data, length, &clength);
if (cdata==nullptr)
{
return 0;
}
char* encdata=encrypt(cdata, length);
if(encdata==nullptr)
{
return 0;
}
i->offset =ftell(out);
i->length = clength;
while (t<clength && (n=fwrite(encdata+t, clength-t, 1, out))>0)
{
t+=n;
}
free(encdata);
free(cdata);
if (t<clength)
{
return 0;
}
lua_pop(l,1);
return 0;
}
//this function prepares the executable name, in order for it to run
void preparename (char* s)
{
char* c = strrchr(s,'.');
if (c)
{
*c=0;
}
while (*(++s))
{
if (*s=='/' || *s=='\\')
{
*s='.';
}
}
}
//this function save's the executable into disk
int output(const char* scr, bool dbg)
{
lua_pushvalue(l, -1);
luaL_loadfile(l, scr);
pkginfo.count++;
preparename(scr);
e.namelength=strlen(scr)+1;
if(writechunk(l, &e, out, scr, dbg))
{
lua_pushstring(l, "unable to write the executable to the disk");
lua_error(l);
return 1;
}
if (bufferpos+e.namelength+sizeof(e)+8 >= bufferlen)
{
buffer=realloc(buffer, bufferlen=max<int>(bufferlen*3/2 +1,
bufferpos+e.namelength+sizeof(e)+16));
}
memcpy(buffer+bufferpos, &e, sizeof(e));
memcpy(buffer + bufferpos + sizeof(e), scr, e.namelength);
bufferpos += e.namelength + sizeof(e);
}
FILE *in;
FILE *out;
int n = 0, t=0;
GlobalChunkInfo pkginfo = { 0, 0, 0, MAGIC };
chunkinfo e;
int bufferlen = 4096, bufferpos = 0;
char* buffer;
public:
//compile function which compile's the script files
int compile(const char *scr, const char* outfile, bool debug)
{
buffer=malloc(bufferlen);
if(buffer==nullptr)
{
lua_pushstring(l, "unable to compile the script into executable!");
lua_error(l);
return 1;
}
in=fopen("exec.vm", "rb");
out=fopen(outfile, "wb");
if(!in)
{
lua_pushstring(l, "unable to initialize executable compiler:
executable embedder not found");
lua_error(l);
return 1;
}
if(!out)
{
lua_pushstring(l, "unable to initialize executable compiler: couldnt
access output file");
lua_error(l);
return 1;
}
while(n=fread(buffer, bufferlen, 1, in)>0)
{
fwrite(buffer, n, 1, out);
}
fclose(in);
lua_getglobal(l, "string");
lua_getfield(l, -1, "dump");
unsigned long length=0;
const char* data=nullptr;
if(output(scr, debug))
{
return 1;
}
pkginfo.offset=ftell(out);
pkginfo.length = bufferpos;
n=0;
t=0;
while(t<bufferpos&&(n=fwrite(buffer+t, bufferpos, 1, out))>0)
{
t+=n;
}
if(t<bufferpos)
{
lua_pushstring(l, "unable to write into executable file!");
lua_error(l);
return 1;
}
fflush(out);
fclose(out);
}
};
what is the problem here? and how can i fix it