lua-users home
lua-l archive

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

Thank Mauro for your explaination.
But how can I let Lua report the correct line number in its original non-encrypted file when an error occurs.
For example:
I have a lua script loadfile.lua:
-- Let's assume the above line is line 3.
When the error occurs it reports:
in function `luaL_loadfile`
But this's not what I want, I need it to point to the line number in its original non-encrypted file so that I can debug the code.
Anyone knows how to achieve that? Thanks.

Mauro Iazzi <> wrote:
luaL_loadfile uses a simple reader which reads plain files to simplify
a common operation, seems you have to use lua_load (which is
internally used by luaL_load* functions). If you use those function
you do not have access to the bytes read.

With lua_load you are responsible for the buffers you provide with the
reader, so you can either free them all at the end of the read or
inside the reader (freeing the previous block) or arrange to never
free anything. You can keep the data you need (file handle, key,
buffers) in the opaque value of lua_load.

A simple example could show better:

struct crypt_data {
FILE* fh;
char key[SIZEOFKEY];
char buff[SIZEOFCHUNK];
char *p;
int n;

const char * crypt_reader(lua_State *L, void *ud, size_t *sz) {
int n;
char *p;
struct crypt_data *data = "">if ( !data->p || data->n<=0) {
data->n = fread(data->buff, SIZEOFCHUNK, sizeof(char), fh);
data->p = data->buff;
p = data->p;
n = data->n;
data->p = NULL;
data->n = 0;
/* decrypt using p, n, data->key <-- your code */
*sz = n;
return p;

void load_crypted(lua_State*L, FILE* fh) {
struct crypt_data my_data;
my_data.fh = fh;
my_data.n = fread(my_data.buff, SIZEOFCHUNK, sizeof(char), fh);
memcpy(my_data.key, my_data.buff, SIZEOFKEY*sizeof(char));
p = &(my_data.buff[SIZEOFKEY]);
my_data.n -= SIZEOFKEY;
lua_load(L, crypt_reader, &my_data, "name of file");

this never needs any free (and should be correct, too :-P ). You can
easily change the loader to allocate and free dynamically. Hope it

On 05/06/07, Dive Florida wrote:
> I need to parse encrypted file inside luaL_loadfile(). I know it can be done
> by writing my own lua_Reader(), but there are 2 issues here:
> 1. How can I free the memory returned by lua_Reader(allocated inside
> lua_Reader)?
> 2. How to know where the first block is? I need to get the key in the first
> block to decrypt the data.
> Thanks.
> ________________________________
> Shape Yahoo! in your own image. Join our Network Research Panel today!

Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.