From 74bc45116aa1c5d0e549f522dccefc58356c1410 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 29 Dec 2013 23:50:43 +0100 Subject: [PATCH] Encoding helpers with tests --- encoding.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ encoding_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 encoding.go create mode 100644 encoding_test.go diff --git a/encoding.go b/encoding.go new file mode 100644 index 000000000..ca30b47c9 --- /dev/null +++ b/encoding.go @@ -0,0 +1,46 @@ +package main + +import ( + "bytes" + "encoding/hex" + "strings" +) + +func CompactEncode(hexSlice []int) string { + terminator := 0 + if hexSlice[len(hexSlice)-1] == 16 { + terminator = 1 + } + + if terminator == 1 { + hexSlice = hexSlice[:len(hexSlice)-1] + } + + oddlen := len(hexSlice) % 2 + flags := 2 * terminator + oddlen + if oddlen != 0 { + hexSlice = append([]int{flags}, hexSlice...) + } else { + hexSlice = append([]int{flags, 0}, hexSlice...) + } + + var buff bytes.Buffer + for i := 0; i < len(hexSlice); i+=2 { + buff.WriteByte(byte(16 * hexSlice[i] + hexSlice[i+1])) + } + + return buff.String() +} + +func CompactHexDecode(str string) []int { + base := "0123456789abcdef" + hexSlice := make([]int, 0) + + enc := hex.EncodeToString([]byte(str)) + for _, v := range enc { + hexSlice = append(hexSlice, strings.IndexByte(base, byte(v))) + } + hexSlice = append(hexSlice, 16) + + return hexSlice +} diff --git a/encoding_test.go b/encoding_test.go new file mode 100644 index 000000000..63f7878bf --- /dev/null +++ b/encoding_test.go @@ -0,0 +1,50 @@ +package main + +import ( + "testing" + "fmt" +) + +func TestCompactEncode(t *testing.T) { + test1 := []int{1,2,3,4,5} + if res := CompactEncode(test1); res != "\x11\x23\x45" { + t.Error(fmt.Sprintf("even compact encode failed. Got: %q", res)) + } + + test2 := []int{0, 1, 2, 3, 4, 5} + if res := CompactEncode(test2); res != "\x00\x01\x23\x45" { + t.Error(fmt.Sprintf("odd compact encode failed. Got: %q", res)) + } + + test3 := []int{0, 15, 1, 12, 11, 8, /*term*/16} + if res := CompactEncode(test3); res != "\x20\x0f\x1c\xb8" { + t.Error(fmt.Sprintf("odd terminated compact encode failed. Got: %q", res)) + } + + test4 := []int{15, 1, 12, 11, 8, /*term*/16} + if res := CompactEncode(test4); res != "\x3f\x1c\xb8" { + t.Error(fmt.Sprintf("even terminated compact encode failed. Got: %q", res)) + } +} + +// Helper function for comparing slices +func CompareIntSlice(a, b []int) bool { + if len(a) != len(b) { + return false + } + for i, v := range a { + if v != b[i] { + return false + } + } + return true +} + +func TestCompactHexDecode(t *testing.T) { + exp := []int{7, 6, 6, 5, 7, 2, 6, 2, 16} + res := CompactHexDecode("verb") + + if !CompareIntSlice(res, exp) { + t.Error("Error compact hex decode. Expected", exp, "got", res) + } +}