lua-users home
lua-l archive

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


> OK, but I do not think the proposed mechanism is optimal, at the 'C' level.
> Firstly the lua_Reader function replacement has to process the internals of
> the bytecode format thus breaking containment

I'm probably missing something, but testing in a lua_Reader function
whether the input is binary is simply testing whether the first byte is
LUA_SIGNATURE[0], as luaL_loadfile does.

That said, we could write a convenience function like this:
 
typedef struct {
  int first;
  lua_Reader f;
  void *data;
} Safedata;

static const char *safe (lua_State *L, void *ud, size_t *size) {
  Safedata *d = (Safedata*) ud;
  const char *s = d->f(L,d->data,size);
  if (d->first) {
    d->first = 0;
    if (s!=NULL && *size!=0 && s[0]==LUA_SIGNATURE[0])
      luaL_error(L,"cannot load binary files");
  }
  return (*size > 0) ? s : NULL;
}

LUALIB_API int luaL_load (lua_State *L, lua_Reader f, void *data,
                      const char *chunkname, int nobinary) {
  Safedata d;
  d.first=nobinary;
  d.f=f;
  d.data=data;
  return lua_load(L, safe, &d, chunkname);
}