Beispiel #1
0
static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r)
{
  void **reg = ll_register(L, path);
  if (*reg == NULL) *reg = ll_load(L, path, (*name == '*'));
  if (*reg == NULL) {
    return PACKAGE_ERR_LIB;  /* Unable to load library. */
  } else if (*name == '*') {  /* Only load library into global namespace. */
    lua_pushboolean(L, 1);
    return 0;
  } else {
    const char *sym = r ? name : mksymname(L, name, SYMPREFIX_CF);
    lua_CFunction f = ll_sym(L, *reg, sym);
    if (f) {
      lua_pushcfunction(L, f);
      return 0;
    }
    if (!r) {
      const char *bcdata = ll_bcsym(*reg, mksymname(L, name, SYMPREFIX_BC));
      lua_pop(L, 1);
      if (bcdata) {
	if (luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0)
	  return PACKAGE_ERR_LOAD;
	return 0;
      }
    }
    return PACKAGE_ERR_FUNC;  /* Unable to find function. */
  }
}
Beispiel #2
0
static int ll_loadfunc (lua_State *L, const char* path, const char* sym) {
  void** reg = ll_register(L, path);
  if (*reg == nullptr) *reg = ll_load(L, path);
  if (*reg == nullptr)
    return ERRLIB;  /* unable to load library */
  else {
    lua_CFunction f = ll_sym(L, *reg, sym);
    if (f == nullptr)
      return ERRFUNC;  /* unable to find function */
    lua_pushcfunction(L, f);
    return 0;  /* return function */
  }
}
static int ll_loadfunc(lua_State *L, const char *path, const char *sym)
{
  void **reg = ll_register(L, path);
  if (*reg == NULL) *reg = ll_load(L, path);
  if (*reg == NULL) {
    return PACKAGE_ERR_LIB;  /* unable to load library */
  } else {
    lua_CFunction f = ll_sym(L, *reg, sym);
    if (f == NULL)
      return PACKAGE_ERR_FUNC;  /* unable to find function */
    lua_pushcfunction(L, f);
    return 0;  /* return function */
  }
}
Beispiel #4
0
static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
  void **reg = ll_register(L, path);
  if (*reg == NULL) *reg = ll_load(L, path, *sym == '*');
  if (*reg == NULL) return ERRLIB;  /* unable to load library */
  if (*sym == '*') {  /* loading only library (no function)? */
    lua_pushboolean(L, 1);  /* return 'true' */
    return 0;  /* no errors */
  }
  else {
    lua_CFunction f = ll_sym(L, *reg, sym);
    if (f == NULL)
      return ERRFUNC;  /* unable to find function */
    lua_pushcfunction(L, f);  /* else create new function */
    return 0;  /* no errors */
  }
}
Beispiel #5
0
static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
  void *reg = ll_checkclib(L, path);  /* check loaded C libraries */
  if (reg == NULL) {  /* must load library? */
    reg = ll_load(L, path, *sym == '*');
    if (reg == NULL) return ERRLIB;  /* unable to load library */
    ll_addtoclib(L, path, reg);
  }
  if (*sym == '*') {  /* loading only library (no function)? */
    lua_pushboolean(L, 1);  /* return 'true' */
    return 0;  /* no errors */
  }
  else {
    lua_CFunction f = ll_sym(L, reg, sym);
    if (f == NULL)
      return ERRFUNC;  /* unable to find function */
    lua_pushcfunction(L, f);  /* else create new function */
    return 0;  /* no errors */
  }
}
Beispiel #6
0
int main(int argc, char *argv[]) {
  lb_idtype lb_id;
  lzo_uint  lb_overhead;
  lzo_uint  lb_offset;
  lzo_bytep lb_data;
  lzo_bytep lb_temp;

  /* start up the stuff */
#ifdef _WIN32
  char selfname[MAX_PATH]; /* Flawfinder: ignore */
  if (GetModuleFileName(NULL,selfname,sizeof(selfname))==0)
    lb_error("cannot locate this executable");
  argv[0] = selfname;
#endif /* not _WIN32 */
  lb_progname = lb_fnname(argv[0]);
  const char *lb_path = lb_tmppath();
  if (lb_path == NULL) lb_cannot("find","tmp dir",NULL);

  if (lzo_init() != LZO_E_OK)
    lb_error("internal LZO error");
  if ((L = luaL_newstate()) == NULL) lb_error("not enough memory");
  luaL_openlibs(L);
  LBCONF_USERFUNC_INIT(L);

  /* open and load */
  FILE *lb_file = fopen(argv[0],"rb"); /* Flawfinder: ignore */
  if (lb_file == NULL)
    lb_cannot("open",argv[0],NULL);
  if (fseek(lb_file,-sizeof(lb_id),SEEK_END)!=0)
    lb_cannot("seek",argv[0],NULL);
  if (fread(&lb_id,sizeof(lb_id),1,lb_file)!=1)
    lb_cannot("read",argv[0],NULL);
  if (memcmp(lb_id.id,"LB02",4)!=0)
    lb_error("missing overlay");
  if (fseek(lb_file,-(sizeof(lb_id)+lb_id.nlen),SEEK_END)!=0)
    lb_cannot("seek",argv[0],NULL);
  if (lb_id.dlen != 0) {
    lb_overhead = lb_id.dlen / 16 + 64 + 3;
    lb_offset = lb_id.dlen + lb_overhead - lb_id.nlen;
    lb_temp = (lzo_bytep)alloca(lb_id.dlen + lb_overhead);
    lb_data = lb_temp + lb_offset;
  } else
    lb_data = (lzo_bytep)alloca(lb_id.nlen);
  if (fread(lb_data,lb_id.nlen,1,lb_file)!=1)
    lb_cannot("read",argv[0],NULL);
  fclose(lb_file);

  /* checksum */
  if (lzo_adler32(0,(lzo_bytep)lb_data,lb_id.nlen)!=lb_id.adler32)
    lb_error("bad checksum");

  /* decompress */
  if (lb_id.dlen != 0) {
    lzo_uint new_len;
    int r = lzo1x_decompress(lb_data,lb_id.nlen,lb_temp,&new_len,NULL);
    if (r != LZO_E_OK || new_len != lb_id.dlen)
      lb_error("overlapping decompression failed");
    lb_data = lb_temp;
  } else 
    lb_id.dlen = lb_id.nlen;

  /* set parameters (arg = argv) */
  lua_newtable(L);
  { int i;
    for (i=0;i <= argc; i++) {
      lua_pushstring(L,argv[i]);
      lua_rawseti(L,-2,i);
    }
  }
  lua_setglobal(L,"arg");

  /* setup the clean up */
  lua_newtable(L);
  lua_setfield(L,LUA_REGISTRYINDEX,"_LBCM");
  lua_getfield(L,LUA_REGISTRYINDEX,"_LBCM");   /* _LBCM is at index 1   */
  atexit(lb_quit);

  /* link, load and run loop */
  lua_getfield(L,LUA_REGISTRYINDEX,"_LOADED"); /* _LOADED is at index 2 */
  char *buf = lb_data;
  uint32_t ptr = 0;
  do {
    char  _type = *buf++;
    char  _nlen = *buf++;
    char *_name = alloca((size_t)_nlen+1);
    memcpy(_name,buf,_nlen); /* Flawfinder: ignore */
    _name[_nlen] = '\0';
    buf += _nlen;
    uint32_t _size;
    memcpy(&_size,buf,4); /* Flawfinder: ignore */
    buf += 4;
    switch (_type) {
      case LB_LUAMAIN: /* run */
        if (luaL_loadbuffer(L,buf,_size,_name))
          lb_error(lua_tostring(L,-1));
        int i = 1;
        for(;i <= argc;i++)
          lua_pushstring(L,argv[i]);
        if (lua_pcall(L,i-1,0,0))
          lb_error(lua_tostring(L,-1));
        return 0;
      case LB_LMODULE: /* load */
      case LB_CMODULE: /* link and load */
        lua_getfield(L,2,_name); /* _LOADED[name] */
        if (lua_toboolean(L,-1)) { /* is it there? */
          if (lua_touserdata(L,-1) == sentinel) { /* check loops */
            lua_pushfstring(L,"loop or previous error loading module '%s'",_name);
            lb_error(lua_tostring(L,-1));
          }
          lua_pop(L,2);
          break; /* package is already loaded */
        } else lua_pop(L,1);
        /* else must load it */
        if (_type == LB_LMODULE) {
          if (luaL_loadbuffer(L,buf,_size,_name))
            lb_error(lua_tostring(L,-1));
        } else {
          lua_pushfstring(L,"%s/%s%s",lb_path,_name,LB_DLEXT);
          lb_libcreate(lua_tostring(L,-1),buf,_size);
          void *_lib = ll_load(L,lua_tostring(L,-1));
          if (_lib == NULL) lb_cannot("load library",_name,lua_tostring(L,-1));
          lua_CFunction _fun = ll_sym(L,_lib,mkfuncname(L,_name));
          if (_fun == NULL) lb_cannot("find function",lua_tostring(L,-2),lua_tostring(L,-1));
          lua_pop(L,1); /* pop value from mkfuncname() */
          lua_pushlightuserdata(L,_lib);
          lua_settable(L,1); /* _LBCM[libname] = _lib */
          lua_pushcfunction(L,_fun);
        }
        lua_pushlightuserdata(L,sentinel);
        lua_setfield(L,2,_name); /* _LOADED[name] = sentinel */
        lua_pushstring(L,_name); /* pass name as argument to module */
        if (lua_pcall(L,1,1,0)) /* run loaded module */
          lb_error(lua_tostring(L,-1)); 
        if (!lua_isnil(L,-1)) /* non-nil return ? */
          lua_setfield(L,2,_name); /* _LOADED[name] = returned value */
        lua_getfield(L,2,_name);
        if (lua_touserdata(L,-1)==sentinel){ /* module did not set a value? */
          lua_pushboolean(L,1); /* use true as result */
          lua_setfield(L,2,_name); /* _LOADED[name] = true */
        }
        break;
      case LB_LIBRARY: /* link */
        lua_pushfstring(L,"%s/%s%s",lb_path,_name,LB_DLEXT);
        lb_libcreate(lua_tostring(L,-1),buf,_size);
        void *_lib = ll_load(L,lua_tostring(L,-1));
        if (_lib == NULL) lb_cannot("load library",_name,lua_tostring(L,-1));
        lua_pushlightuserdata(L,_lib);
        lua_settable(L,1); /* _LBCM[libname] = _lib */
        break;
    }
    buf += _size;
    ptr += 2 + _nlen + 4 + _size;
  } while (ptr < lb_id.dlen);
  return 0;
}