diff options
author | Rasmus Dahlberg <rasmus@mullvad.net> | 2021-12-20 14:37:25 +0100 |
---|---|---|
committer | Rasmus Dahlberg <rasmus@mullvad.net> | 2021-12-20 14:38:49 +0100 |
commit | 79aa7d7a0318db9913d7cec5473ef51ef2e04593 (patch) | |
tree | 5b1668b39d3905e177925bc66f7f11219c2e23b8 /pkg/hex/hex.go | |
parent | 9878949b5af289490ad6922f76f5b5fcedce8d7d (diff) |
hex: Add lower-case hex parser and tests
Diffstat (limited to 'pkg/hex/hex.go')
-rw-r--r-- | pkg/hex/hex.go | 54 |
1 files changed, 54 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 +} |