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 {
LuaCode64 string
Base64LuaCode string
}
func NewLua(params LuaParams) (*Lua, error) {
luaCode, err := base64.StdEncoding.DecodeString(params.LuaCode64)
luaCode, err := base64.StdEncoding.DecodeString(params.Base64LuaCode)
if err != nil {
return nil, err
}
@ -26,7 +26,6 @@ func NewLua(params LuaParams) (*Lua, error) {
state := lua.NewState()
state.OpenLibs()
// Load and execute the Lua code
if err := state.DoString(string(luaCode)); err != nil {
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) {
// 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.PushInteger(counter)
@ -52,15 +49,12 @@ func (l *Lua) Generate(data []byte, counter int64) ([]byte, error) {
result := l.state.ToBytes(-1)
l.state.Pop(1)
fmt.Printf("Result: %s\n", string(result))
return result, nil
}
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)
if err := l.state.Call(1, 1); err != nil {
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)
l.state.Pop(1)
fmt.Printf("Result: %s\n", string(result))
return result, nil
}

View file

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