aboutsummaryrefslogtreecommitdiff
path: root/pkg/hex
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/hex')
-rw-r--r--pkg/hex/hex.go54
-rw-r--r--pkg/hex/hex_test.go66
2 files changed, 120 insertions, 0 deletions
diff --git a/pkg/hex/hex.go b/pkg/hex/hex.go
new file mode 100644
index 0000000..1b5e324
--- /dev/null
+++ b/pkg/hex/hex.go
@@ -0,0 +1,54 @@
+// package hex implements a lower-case hex parser.
+package hex
+
+import (
+ "fmt"
+)
+
+const (
+ language = "0123456789abcdef"
+)
+
+// Serialize serializes a buffer as lower-case hex
+func Serialize(buf []byte) string {
+ out := make([]byte, len(buf)*2)
+ for i, b := range buf {
+ offset := i * 2
+ out[offset] = language[b>>4]
+ out[offset+1] = language[b&0x0f]
+ }
+ return string(out)
+}
+
+// Deserialize tries to deserialize a lower-case hex string
+func Deserialize(str string) ([]byte, error) {
+ if len(str)%2 != 0 {
+ return nil, fmt.Errorf("hex: string must have even length")
+ }
+
+ buf := make([]byte, len(str)/2)
+ for i := 0; i < len(buf); i++ {
+ offset := i * 2
+ first, ok := deserializeOne(str[offset])
+ if !ok {
+ return nil, fmt.Errorf("hex: invalid character at index %d: %d", i, first)
+ }
+ second, ok := deserializeOne(str[offset+1])
+ if !ok {
+ return nil, fmt.Errorf("hex: invalid character at index %d: %d", i, second)
+ }
+ buf[i] = first << 4
+ buf[i] = buf[i] | second
+ }
+ return buf, nil
+}
+
+func deserializeOne(b byte) (byte, bool) {
+ if b >= '0' && b <= '9' {
+ return b - '0', true
+ }
+ if b >= 'a' && b <= 'f' {
+ return b - 'a' + 10, true
+ }
+ return 0, false
+}
diff --git a/pkg/hex/hex_test.go b/pkg/hex/hex_test.go
new file mode 100644
index 0000000..222bd5e
--- /dev/null
+++ b/pkg/hex/hex_test.go
@@ -0,0 +1,66 @@
+package hex
+
+import (
+ "bytes"
+ "testing"
+)
+
+func TestSerialize(t *testing.T) {
+ for _, table := range []struct {
+ desc string
+ input []byte
+ want string
+ }{
+ {
+ desc: "valid",
+ input: []byte{0, 9, 10, 15, 16, 17, 254, 255},
+ want: "00090a0f1011feff",
+ },
+ } {
+ str := Serialize(table.input)
+ if got, want := str, table.want; got != want {
+ t.Errorf("got %q but wanted %q in test %q", got, want, table.desc)
+ }
+ }
+}
+
+func TestDeserialize(t *testing.T) {
+ for _, table := range []struct {
+ desc string
+ input string
+ want []byte
+ err bool
+ }{
+ {
+ desc: "invalid: length is odd",
+ input: "0",
+ err: true,
+ },
+ {
+ desc: "invalid: even index has invalid character",
+ input: "A0",
+ err: true,
+ },
+ {
+ desc: "invalid: odd index has invalid character",
+ input: "0A",
+ err: true,
+ },
+ {
+ desc: "valid",
+ input: "00090a0f1011feff",
+ want: []byte{0, 9, 10, 15, 16, 17, 254, 255},
+ },
+ } {
+ buf, err := Deserialize(table.input)
+ if got, want := err != nil, table.err; got != want {
+ t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.desc, err)
+ }
+ if err != nil {
+ continue
+ }
+ if got, want := buf, table.want; !bytes.Equal(got, want) {
+ t.Errorf("got %v but wanted %v in test %q", got, want, table.desc)
+ }
+ }
+}