lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Looks interesting. Anyone working on a code obfuscator to further
reduce the size?

Something I've done for an application where small size was important
but speed was not was to gzip-compress the lua code and make a
specific file reader (for lua_load) that expanded the file.  An extra
feature is that the same loader can be used for loading uncompressed
files as well, thanks to how zlib is implemented.

The source code looks as follows (well, error handling is missing, but
it's easy to add):

#include <zlib.h>

#define BUFFER_SIZE 8192

typedef struct {
  gzFile f;
  char buffer[BUFFER_SIZE];
} FileInfo;

static const char *luagz_filereader (lua_State *L, void *ud, size_t *size) {
  FileInfo *fi = (FileInfo *)ud;

  if (gzeof(fi->f)) return NULL;

  *size = gzread(fi->f, fi->buffer, BUFFER_SIZE);

  (void)L; // To remove warnings

  return (*size > 0) ? fi->buffer : NULL;
}

/* an alternative to luaL_loadfile that reads a gz-file into the code buffer */
int luagz_loadfile (lua_State *L, const char *filename) {
  FileInfo fi;
  int status;

  fi.f = gzopen(filename, "r");

  lua_pushfstring(L, "@%s", filename);
  status = lua_load(L, luagz_filereader, &fi, lua_tostring(L, -1));

  (void)gzclose(fi.f);

  return status;
}

// Victor

On 3/30/06, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
> I've updated my lstrip program, which compresses Lua programs by
> removing comments and whitespace:
>         http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/5.1/lstrip.tar.gz
>
> lstrip may be useful for embedding Lua code as a string (after running the
> output through bin2c or 'xxd -i').
>
> Enjoy.
> --lhf
>