change function initials

This commit is contained in:
Mark Puha 2025-02-08 14:35:20 +01:00
parent 55f4715a50
commit 3015f3ea20
2 changed files with 7 additions and 14 deletions

View file

@ -13,11 +13,11 @@ type Lua struct {
} }
type LuaParams struct { type LuaParams struct {
LuaCode64 string Base64LuaCode string
} }
func NewLua(params LuaParams) (*Lua, error) { func NewLua(params LuaParams) (*Lua, error) {
luaCode, err := base64.StdEncoding.DecodeString(params.LuaCode64) luaCode, err := base64.StdEncoding.DecodeString(params.Base64LuaCode)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -26,7 +26,6 @@ func NewLua(params LuaParams) (*Lua, error) {
state := lua.NewState() state := lua.NewState()
state.OpenLibs() state.OpenLibs()
// Load and execute the Lua code
if err := state.DoString(string(luaCode)); err != nil { if err := state.DoString(string(luaCode)); err != nil {
return nil, fmt.Errorf("Error loading Lua code: %v\n", err) return nil, fmt.Errorf("Error loading Lua code: %v\n", err)
} }
@ -38,10 +37,8 @@ func (l *Lua) Close() {
} }
func (l *Lua) Generate(data []byte, counter int64) ([]byte, error) { func (l *Lua) Generate(data []byte, counter int64) ([]byte, error) {
// Push the function onto the stack l.state.GetGlobal("d_gen")
l.state.GetGlobal("D_gen")
// Push the argument
l.state.PushBytes(data) l.state.PushBytes(data)
l.state.PushInteger(counter) l.state.PushInteger(counter)
@ -52,15 +49,12 @@ func (l *Lua) Generate(data []byte, counter int64) ([]byte, error) {
result := l.state.ToBytes(-1) result := l.state.ToBytes(-1)
l.state.Pop(1) l.state.Pop(1)
fmt.Printf("Result: %s\n", string(result))
return result, nil return result, nil
} }
func (l *Lua) Parse(data []byte) ([]byte, error) { func (l *Lua) Parse(data []byte) ([]byte, error) {
// Push the function onto the stack l.state.GetGlobal("d_parse")
l.state.GetGlobal("D_parse")
// Push the argument
l.state.PushBytes(data) l.state.PushBytes(data)
if err := l.state.Call(1, 1); err != nil { if err := l.state.Call(1, 1); err != nil {
return nil, fmt.Errorf("Error calling Lua function: %v\n", err) return nil, fmt.Errorf("Error calling Lua function: %v\n", err)
@ -69,6 +63,5 @@ func (l *Lua) Parse(data []byte) ([]byte, error) {
result := l.state.ToBytes(-1) result := l.state.ToBytes(-1)
l.state.Pop(1) l.state.Pop(1)
fmt.Printf("Result: %s\n", string(result))
return result, nil return result, nil
} }

View file

@ -7,17 +7,17 @@ import (
func newLua() *Lua { func newLua() *Lua {
lua, _ := NewLua(LuaParams{ lua, _ := NewLua(LuaParams{
/* /*
function D_gen(data, counter) function d_gen(data, counter)
local header = "header" local header = "header"
return counter .. header .. data return counter .. header .. data
end end
function D_parse(data) function d_parse(data)
local header = "10header" local header = "10header"
return string.sub(data, #header+1) return string.sub(data, #header+1)
end end
*/ */
LuaCode64: "ZnVuY3Rpb24gRF9nZW4oZGF0YSwgY291bnRlcikKCWxvY2FsIGhlYWRlciA9ICJoZWFkZXIiCglyZXR1cm4gY291bnRlciAuLiBoZWFkZXIgLi4gZGF0YQplbmQKCmZ1bmN0aW9uIERfcGFyc2UoZGF0YSkKCWxvY2FsIGhlYWRlciA9ICIxMGhlYWRlciIKCXJldHVybiBzdHJpbmcuc3ViKGRhdGEsICNoZWFkZXIrMSkKZW5kCg==", Base64LuaCode: "ZnVuY3Rpb24gZF9nZW4oZGF0YSwgY291bnRlcikKCWxvY2FsIGhlYWRlciA9ICJoZWFkZXIiCglyZXR1cm4gY291bnRlciAuLiBoZWFkZXIgLi4gZGF0YQplbmQKCmZ1bmN0aW9uIGRfcGFyc2UoZGF0YSkKCWxvY2FsIGhlYWRlciA9ICIxMGhlYWRlciIKCXJldHVybiBzdHJpbmcuc3ViKGRhdGEsICNoZWFkZXIrMSkKZW5kCg==",
}) })
return lua return lua
} }