Skip to content

Add a hex wrapper #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.14
go-version: 1.16

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
5 changes: 3 additions & 2 deletions base64/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package base64

import (
"encoding/base64"

lua "github.com/yuin/gopher-lua"
)

// Preload adds yaml to the given Lua state's package.preload table. After it
// Preload adds base64 to the given Lua state's package.preload table. After it
// has been preloaded, it can be loaded using require:
//
// local yaml = require("yaml")
// local base64 = require("base64")
func Preload(L *lua.LState) {
L.PreloadModule("base64", Loader)
}
Expand Down
43 changes: 43 additions & 0 deletions hex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# hex [![GoDoc](https://godoc.org/github.com/vadv/gopher-lua-libs/hex?status.svg)](https://godoc.org/github.com/vadv/gopher-lua-libs/hex)

Lua module for [encoding/hex](https://pkg.go.dev/encoding/hex)

## Usage

### Encoding

```lua
local hex = require("hex")

s = hex.RawStdEncoding:encode_to_string("foo\01bar")
print(s)
Zm9vAWJhcg

s = hex.StdEncoding:encode_to_string("foo\01bar")
print(s)
Zm9vAWJhcg==

s = hex.RawURLEncoding:encode_to_string("this is a <tag> and should be encoded")
print(s)
dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA

s = hex.URLEncoding:encode_to_string("this is a <tag> and should be encoded")
print(s)
dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA==

```

### Decoding

```lua
local hex = require 'hex'

decoded, err = hex.decode_string("666f6f62617262617a")
assert(not err, err)
print(decoded)
foobar

encoded = hex.encode_to_string(decoded)
print(encoded)
666f6f62617262617a
```
84 changes: 84 additions & 0 deletions hex/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Package hex implements hex encode/decode functionality for lua.
package hex

import (
"encoding/hex"
"io"

lio "github.com/vadv/gopher-lua-libs/io"
lua "github.com/yuin/gopher-lua"
)

const (
hexEncoderType = "hex.Encoder"
hexDecoderType = "hex.Decoder"
)

// LVHexEncoder creates a new Lua user data for the hex encoder
func LVHexEncoder(L *lua.LState, writer io.Writer) lua.LValue {
ud := L.NewUserData()
ud.Value = writer
L.SetMetatable(ud, L.GetTypeMetatable(hexEncoderType))
return ud
}

// LVHexDecoder creates a new Lua user data for the hex decoder
func LVHexDecoder(L *lua.LState, reader io.Reader) lua.LValue {
ud := L.NewUserData()
ud.Value = reader
L.SetMetatable(ud, L.GetTypeMetatable(hexDecoderType))
return ud
}

// DecodeString decodes the encoded string with the encoding
func DecodeString(L *lua.LState) int {
encoded := L.CheckString(1)
L.Pop(L.GetTop())
decoded, err := hex.DecodeString(encoded)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LString(decoded))
return 1
}

// EncodeToString decodes the string with the encoding
func EncodeToString(L *lua.LState) int {
decoded := L.CheckString(1)
L.Pop(L.GetTop())
encoded := hex.EncodeToString([]byte(decoded))
L.Push(lua.LString(encoded))
return 1
}

// registerHexEncoder Registers the encoder type and its methods
func registerHexEncoder(L *lua.LState) {
mt := L.NewTypeMetatable(hexEncoderType)
L.SetGlobal(hexEncoderType, mt)
L.SetField(mt, "__index", lio.WriterFuncTable(L))
}

// registerHexDecoder Registers the decoder type and its methods
func registerHexDecoder(L *lua.LState) {
mt := L.NewTypeMetatable(hexDecoderType)
L.SetGlobal(hexDecoderType, mt)
L.SetField(mt, "__index", lio.ReaderFuncTable(L))
}

func NewEncoder(L *lua.LState) int {
writer := lio.CheckIOWriter(L, 1)
L.Pop(L.GetTop())
encoder := hex.NewEncoder(writer)
L.Push(LVHexEncoder(L, encoder))
return 1
}

func NewDecoder(L *lua.LState) int {
reader := lio.CheckIOReader(L, 1)
L.Pop(L.GetTop())
decoder := hex.NewDecoder(reader)
L.Push(LVHexDecoder(L, decoder))
return 1
}
14 changes: 14 additions & 0 deletions hex/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package hex

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vadv/gopher-lua-libs/strings"
"github.com/vadv/gopher-lua-libs/tests"
)

func TestApi(t *testing.T) {
preload := tests.SeveralPreloadFuncs(Preload, strings.Preload)
assert.NotZero(t, tests.RunLuaTestFile(t, preload, "./test/test_api.lua"))
}
81 changes: 81 additions & 0 deletions hex/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package hex

import (
"log"

"github.com/vadv/gopher-lua-libs/strings"
lua "github.com/yuin/gopher-lua"
)

func ExampleEncodeToString() {
state := lua.NewState()
defer state.Close()
Preload(state)
source := `
local hex = require 'hex'
s = hex.encode_to_string("foo\01bar")
print(s)
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// 666f6f01626172
}

func ExampleDecodeString() {
state := lua.NewState()
defer state.Close()
Preload(state)
source := `
local hex = require 'hex'
s, err = hex.decode_string("666f6f01626172")
assert(not err, err)
print(s)
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// foobar
}

func ExampleNewEncoder() {
state := lua.NewState()
defer state.Close()
Preload(state)
strings.Preload(state)
source := `
local hex = require 'hex'
local strings = require 'strings'
local writer = strings.new_builder()
encoder = hex.new_encoder(writer)
encoder:write("foo\01bar")
print(writer:string())
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// 666f6f01626172
}

func ExampleNewDecoder() {
state := lua.NewState()
defer state.Close()
Preload(state)
strings.Preload(state)
source := `
local hex = require 'hex'
local strings = require 'strings'
local reader = strings.new_reader('666f6f01626172')
decoder = hex.new_decoder(reader)
local s = decoder:read("*a")
print(s)
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// foobar
}
28 changes: 28 additions & 0 deletions hex/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package hex

import lua "github.com/yuin/gopher-lua"

// Preload adds hex to the given Lua state's package.preload table. After it
// has been preloaded, it can be loaded using require:
//
// local hex = require("hex")
func Preload(L *lua.LState) {
L.PreloadModule("hex", Loader)
}

// Loader is the module loader function.
func Loader(L *lua.LState) int {
registerHexDecoder(L)
registerHexEncoder(L)

// Register the encodings offered by hex go module.
t := L.NewTable()
L.SetFuncs(t, map[string]lua.LGFunction{
"decode_string": DecodeString,
"encode_to_string": EncodeToString,
"new_encoder": NewEncoder,
"new_decoder": NewDecoder,
})
L.Push(t)
return 1
}
43 changes: 43 additions & 0 deletions hex/test/test_api.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local hex = require 'hex'
local strings = require 'strings'
local assert = require 'assert'

function TestHexCodec(t)
local tests = {
{
encoded = "48656c6c6f20776f726c64", -- "Hello world" in hex
decoded = "Hello world",
},
{
encoded = "",
decoded = "",
},
}
for _, tt in ipairs(tests) do
t:Run("hex.decode_string(" .. tostring(tt.encoded) .. ")", function(t)
local got = hex.decode_string(tt.encoded)
assert:Equal(t, tt.decoded, got)
end)

t:Run("hex.encode_to_string(" .. tostring(tt.decoded) .. ")", function(t)
local got = hex.encode_to_string(tt.decoded)
assert:Equal(t, tt.encoded, got)
end)
end
end

function TestEncoder(t)
local writer = strings.new_builder()
local encoder = hex.new_encoder(writer)
encoder:write("foo", "bar", "baz")
encoder:close()
local s = writer:string()
assert:Equal(t, "666f6f62617262617a", s)
end

function TestDecoder(t)
local reader = strings.new_reader("666f6f62617262617a")
local decoder = hex.new_decoder(reader)
local s = decoder:read("*a")
assert:Equal(t, "foobarbaz", s)
end
4 changes: 3 additions & 1 deletion plugin/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/vadv/gopher-lua-libs/db"
"github.com/vadv/gopher-lua-libs/filepath"
"github.com/vadv/gopher-lua-libs/goos"
"github.com/vadv/gopher-lua-libs/hex"
"github.com/vadv/gopher-lua-libs/http"
"github.com/vadv/gopher-lua-libs/humanize"
"github.com/vadv/gopher-lua-libs/inspect"
Expand Down Expand Up @@ -44,6 +45,7 @@ func PreloadAll(L *lua.LState) {

argparse.Preload(L)
base64.Preload(L)
bit.Preload(L)
cert_util.Preload(L)
chef.Preload(L)
cloudwatch.Preload(L)
Expand All @@ -52,6 +54,7 @@ func PreloadAll(L *lua.LState) {
db.Preload(L)
filepath.Preload(L)
goos.Preload(L)
hex.Preload(L)
http.Preload(L)
humanize.Preload(L)
inspect.Preload(L)
Expand All @@ -75,5 +78,4 @@ func PreloadAll(L *lua.LState) {
xmlpath.Preload(L)
yaml.Preload(L)
zabbix.Preload(L)
bit.Preload(L)
}
2 changes: 1 addition & 1 deletion tests/assertions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function assertions:NoErrorf(t, err, fmt, ...)
if not err then
return true
end
return self:Fail(t, string.format([[
return self:Failf(t, string.format([[

Error: Received unexpected error:
%s
Expand Down