static LUA_FUNCTION(openssl_cipher_encrypt_new) { const EVP_CIPHER* cipher = get_cipher(L, 1, NULL); if (cipher) { size_t key_len = 0; const char *key = luaL_optlstring(L, 2, NULL, &key_len); /* can be NULL */ size_t iv_len = 0; const char *iv = luaL_optlstring(L, 3, NULL, &iv_len); /* can be NULL */ int pad = lua_isnoneornil(L, 4) ? 1 : lua_toboolean(L, 4); ENGINE *e = lua_isnoneornil(L, 5) ? NULL : CHECK_OBJECT(5, ENGINE, "openssl.engine"); EVP_CIPHER_CTX *c = NULL; char evp_key[EVP_MAX_KEY_LENGTH] = {0}; char evp_iv[EVP_MAX_IV_LENGTH] = {0}; if (key) { key_len = EVP_MAX_KEY_LENGTH > key_len ? key_len : EVP_MAX_KEY_LENGTH; memcpy(evp_key, key, key_len); } if (iv_len > 0 && iv) { iv_len = EVP_MAX_IV_LENGTH > iv_len ? iv_len : EVP_MAX_IV_LENGTH; memcpy(evp_iv, iv, iv_len); } c = EVP_CIPHER_CTX_new(); EVP_CIPHER_CTX_init(c); if (!EVP_EncryptInit_ex(c, cipher, e, key ? (const byte*)evp_key : NULL, iv_len > 0 ? (const byte*)evp_iv : NULL)) { EVP_CIPHER_CTX_set_padding(c, pad); luaL_error(L, "EVP_CipherInit_ex failed, please check openssl error"); } PUSH_OBJECT(c, "openssl.evp_cipher_ctx"); lua_pushinteger(L, DO_ENCRYPT); lua_rawsetp(L, LUA_REGISTRYINDEX, c); } else luaL_error(L, "argument #1 is not a valid cipher algorithm or openssl.evp_cipher object"); return 1; }
static int db_sethook (lua_State *L) { int arg, mask, count; lua_Hook func; lua_State *L1 = getthread(L, &arg); if (lua_isnoneornil(L, arg+1)) { lua_settop(L, arg+1); func = NULL; mask = 0; count = 0; /* turn off hooks */ } else { const char *smask = luaL_checkstring(L, arg+2); luaL_checktype(L, arg+1, LUA_TFUNCTION); count = luaL_optint(L, arg+3, 0); func = hookf; mask = makemask(smask, count); } gethooktable(L); lua_pushvalue(L, arg+1); lua_rawsetp(L, -2, L1); /* set new hook */ lua_pop(L, 1); /* remove hook table */ lua_sethook(L1, func, mask, count); /* set hooks */ return 0; }
static int lcurl_mime_reset(lua_State *L, lcurl_mime_t *p){ lcurl_mime_part_t *ptr; /* reset all parts*/ for(ptr = p->parts; ptr; ptr=ptr->next){ lcurl_mime_part_reset(L, ptr); } if(LUA_NOREF != p->storage){ p->storage = lcurl_storage_free(L, p->storage); } p->parts = p->parent = NULL; p->mime = NULL; /* remove weak reference to easy */ lua_pushnil(L); lua_rawsetp(L, LCURL_MIME_EASY, p); return 0; }
int lcurl_mime_create(lua_State *L, int error_mode){ //! @todo make this function as method of easy handle lcurl_easy_t *e = lcurl_geteasy(L); lcurl_mime_t *p = lutil_newudatap(L, lcurl_mime_t, LCURL_MIME); p->mime = curl_mime_init(e->curl); //! @todo return more accurate error category/code if(!p->mime) return lcurl_fail_ex(L, error_mode, LCURL_ERROR_EASY, CURLE_FAILED_INIT); p->storage = lcurl_storage_init(L); p->err_mode = error_mode; p->parts = p->parent = NULL; /* weak reference from mime to easy handle */ lua_pushvalue(L, 1); lua_rawsetp(L, LCURL_MIME_EASY, (void*)p); return 1; }
static void mark_thread(lua_State *L, lua_State *dL, const void * parent, const char *desc) { const void * t = readobject(L, dL, parent, desc); if (t == NULL) return; int level = 0; lua_State *cL = lua_tothread(L,-1); if (cL == L) { level = 1; } lua_Debug ar; luaL_Buffer b; luaL_buffinit(dL, &b); while (lua_getstack(cL, level, &ar)) { char tmp[128]; lua_getinfo(cL, "Sl", &ar); luaL_addstring(&b, ar.short_src); if (ar.currentline >=0) { char tmp[16]; sprintf(tmp,":%d ",ar.currentline); luaL_addstring(&b, tmp); } int i,j; for (j=1;j>-1;j-=2) { for (i=j;;i+=j) { const char * name = lua_getlocal(cL, &ar, i); if (name == NULL) break; snprintf(tmp, sizeof(tmp), "%s : %s:%d",name,ar.short_src,ar.currentline); mark_object(cL, dL, t, tmp); } } ++level; } luaL_pushresult(&b); lua_rawsetp(dL, SOURCE, t); lua_pop(L,1); }
int luaopen_socket_c(lua_State *L) { luaL_Reg l[] = { { "open", _open }, { "close", _close }, { "write", _write }, { "new", _new }, { "push", _push }, { "read", _read }, { "readline", _readline }, { "readblock", _readblock }, { "writeblock", _writeblock }, { NULL, NULL }, }; luaL_checkversion(L); luaL_newlib(L,l); lua_createtable(L,0,1); lua_pushcfunction(L, _delete); lua_setfield(L, -2, "__gc"); lua_rawsetp(L, LUA_REGISTRYINDEX, _delete); return 1; }
//设置消息处理回调函数 static int _callback(lua_State *L) { struct server_context * context = lua_touserdata(L, lua_upvalueindex(1));//lua_upvalueindex 返回当前运行的函数的第i个上值的伪索引 int forward = lua_toboolean(L, 2); luaL_checktype(L,1,LUA_TFUNCTION); lua_settop(L,1); lua_rawsetp(L, LUA_REGISTRYINDEX, _cb); /* 获取lua 创建时 main thread 的 L http://blog.codingnow.com/2012/07/lua_c_callback.html */ lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); lua_State *gL = lua_tothread(L,-1); if (forward) { server_callback(context, gL, forward_cb); } else { server_callback(context, gL, _cb); } return 0; }
static void thread_createmeta (lua_State *L) { const struct meta_s { const char *tname; luaL_Reg *meth; } meta[] = { {THREAD_TYPENAME, thread_meth}, {DPOOL_TYPENAME, dpool_meth}, {PIPE_TYPENAME, pipe_meth}, {SCHED_TYPENAME, sched_meth}, }; int i; /* already created? */ luaL_getmetatable(L, THREAD_TYPENAME); { const int created = !lua_isnil(L, -1); lua_pop(L, 1); if (created) return; } for (i = 0; i < (int) (sizeof(meta) / sizeof(struct meta_s)); ++i) { luaL_newmetatable(L, meta[i].tname); lua_pushvalue(L, -1); /* push metatable */ lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ luaL_setfuncs(L, meta[i].meth, 0); lua_pop(L, 1); } /* create threads table */ lua_newtable(L); lua_pushlightuserdata(L, THREAD_KEY_ADDRESS); /* Thread Interrupt Error */ lua_rawseti(L, -2, THREAD_TABLE_EINTR); lua_rawsetp(L, LUA_REGISTRYINDEX, THREAD_KEY_ADDRESS); }
void lua::LuaStack::RegistryPtrSet( const void* ptr ) { lua_rawsetp( m_state, LUA_REGISTRYINDEX, ptr ); }
static int lluv_push_fs_result(lua_State* L, lluv_fs_request_t* lreq) { uv_fs_t *req = &lreq->req; /*lluv_loop_t *loop = req->loop->data;*/ switch (req->fs_type) { case UV_FS_RENAME: case UV_FS_UNLINK: case UV_FS_RMDIR: case UV_FS_MKDIR: case UV_FS_MKDTEMP: case UV_FS_UTIME: case UV_FS_CHMOD: case UV_FS_LINK: case UV_FS_SYMLINK: case UV_FS_CHOWN: lua_pushstring(L, req->path); return 1; case UV_FS_CLOSE: case UV_FS_FTRUNCATE: case UV_FS_FSYNC: case UV_FS_FDATASYNC: case UV_FS_FUTIME: case UV_FS_FCHMOD: case UV_FS_FCHOWN: case UV_FS_OPEN: if(req->path) lua_pushstring(L, req->path); else lua_pushboolean(L, 1); return 1; case UV_FS_SENDFILE: lutil_pushint64(L, req->result); return 1; case UV_FS_STAT: case UV_FS_LSTAT: lluv_push_stat(L, &req->statbuf); lua_pushstring(L, req->path); return 2; case UV_FS_FSTAT: lluv_push_stat(L, &req->statbuf); return 1; case UV_FS_READLINK: #if LLUV_UV_VER_GE(1,8,0) case UV_FS_REALPATH: #endif lua_pushstring(L, (char*)req->ptr); return 1; case UV_FS_WRITE: case UV_FS_READ: lua_rawgetp(L, LLUV_LUA_REGISTRY, req); lua_pushnil(L); lua_rawsetp(L, LLUV_LUA_REGISTRY, req); lutil_pushint64(L, req->result); return 2; case UV_FS_SCANDIR:{ uv_dirent_t ent; int i = 0, err; lua_createtable(L, (int)req->result, 0); lua_createtable(L, (int)req->result, 0); while((err = uv_fs_scandir_next(req, &ent)) >= 0){ lua_pushstring (L, ent.name); lua_rawseti(L, -3, ++i); #define XX(C,S) case S: lua_pushliteral(L, C); lua_rawseti(L, -2, i); break; switch(ent.type){ LLUV_DIRENT_MAP(XX) default: lua_pushstring(L, "unknown"); lua_rawseti(L, -2, i); } #undef XX } lua_pushstring(L, req->path); lua_insert(L, -2); return 3; } case UV_FS_ACCESS:{ lua_pushboolean(L, req->result == 0); lua_pushstring(L, req->path); return 2; } #if LLUV_UV_VER_GE(1,14,0) case UV_FS_COPYFILE: lua_pushboolean(L, req->result == 0); return 1; #endif default: fprintf(stderr, "UNKNOWN FS TYPE %d\n", req->fs_type); return 0; } }
static int hookInitialize(lua_State* L) { luaL_checktype(L, 1, LUA_TFUNCTION); lua_settop(L, 1); lua_rawsetp(L, lua_upvalueindex(1), &FFI_NEW); return 0; }
void LuaInterface::deleteObject(LuaObjGlue *glue) { lua_pushnil(_lua); lua_rawsetp(_lua, LUA_REGISTRYINDEX, glue->obj); glue->obj = NULL; }
LB_API void lbind_setlightuservalue(lua_State *L, const void *p) { lbind_getudtypebox(L); lua_pushvalue(L, -2); lua_rawsetp(L, -2, p); lua_pop(L, 1); }
static void marked(lua_State *L, const void *p, int len) { lua_pushnumber(L, len); lua_rawsetp(L, MARKED_TABLE, p); }
void hive_createenv(lua_State *L) { lua_newtable(L); lua_rawsetp(L, LUA_REGISTRYINDEX, HIVE_TAG); }
void deleteUserdata(lua_State *L, void *ptr) { lua_pushnil(L); lua_rawsetp(L, LUA_REGISTRYINDEX, ptr); }