aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus@mullvad.net>2021-12-20 14:37:25 +0100
committerRasmus Dahlberg <rasmus@mullvad.net>2021-12-20 14:38:49 +0100
commit79aa7d7a0318db9913d7cec5473ef51ef2e04593 (patch)
tree5b1668b39d3905e177925bc66f7f11219c2e23b8
parent9878949b5af289490ad6922f76f5b5fcedce8d7d (diff)
hex: Add lower-case hex parser and tests
-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)
+ }
+ }
+}