Library call in Lua

I created a Wireshark dissector in Lua for an application over TCP. I am trying to use zlib compression and base64 decryption. How to create or call an existing c library in Lua?

The documentation I saw just says that you can get the libraries and use either a call require()or a call luaopen_, but not how to actually make the program find and recognize the actual library. All this is done on Windows.

+4
source share
3 answers

You cannot load an existing C library that was not created for Lua with plain Lua. This is not trivial, at least.

*.so/*.dll , Lua # 26.2 lua-users wiki, . .

:

@lua-users wiki

/ base64. , Lua. @wiki wiki.

. Lua, LuaRocks LuaDist .

, Lua :

local zlib = require("zlib")

, Lua luaconf.h.

5.1 :

#if defined(_WIN32)
/*
** In Windows, any exclamation mark ('!') in the path is replaced by the
** path of the directory of the executable file of the current process.
*/
#define LUA_LDIR        "!\\lua\\"
#define LUA_CDIR        "!\\"
#define LUA_PATH_DEFAULT  \
            ".\\?.lua;"  LUA_LDIR"?.lua;"  LUA_LDIR"?\\init.lua;" \
                         LUA_CDIR"?.lua;"  LUA_CDIR"?\\init.lua"
#define LUA_CPATH_DEFAULT \
    ".\\?.dll;"  LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"

#else
+2

c Lua?

, Lua? .

"" Lua Lua API - , , Lua5.1.dll Wireshark, C- , lua_CFunction. Lua , Lua API Lua.

zlib / base64 Lua. Lua FFI, FFI Lua, , , , , , . Lua , Lua.

zlib base64 C, , , , , Lua script MessageBox user32.dll Windows.

#include <windows.h>
#include "lauxlib.h"

static int luaMessageBox (lua_State* L) {
    const char* message = luaL_checkstring(L,1);
    MessageBox(NULL, message, "", MB_OK);
    return 0;
}

int __declspec(dllexport) __cdecl luaopen_messagebox (lua_State* L) {
    lua_register(L, "msgbox", luaMessageBox);
    return 0;
}

, user32.dll ( MessageBox) lua5.1.dll ( Lua API). Lua5.1.lib Wireshark. , Microsoft messagebox.dll:

cl /LD /Ilua-5.1.4/src messagebox.c user32.lib lua5.1.lib

Lua :

require "messagebox"

msgbox("Hello, World!")
+1

- , ​​ alien. . Lua Scripting FFI.

0

Source: https://habr.com/ru/post/1532157/


All Articles