-
Notifications
You must be signed in to change notification settings - Fork 504
/
encoding.go
80 lines (65 loc) · 1.53 KB
/
encoding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package itrie
// hasTerminator checks if hex is ending
// with a terminator flag.
func hasTerminator(hex []byte) bool {
if len(hex) == 0 {
return false
}
return hex[len(hex)-1] == 16
}
// encodeCompact packs a hex sequence (of nibbles)
// into compact encoding.
func encodeCompact(hex []byte) []byte {
var terminator int
if hasTerminator(hex) {
// remove terminator flag
hex = hex[:len(hex)-1]
terminator = 1
} else {
terminator = 0
}
// determine prefix flag
oddLen := len(hex) % 2
flag := 2*terminator + oddLen
// insert flag
if oddLen == 1 {
hex = append([]byte{byte(flag)}, hex...)
} else {
hex = append([]byte{byte(flag), byte(0)}, hex...)
}
// hex slice is of even length now - pack nibbles
result := make([]byte, len(hex)/2)
for i := 0; i < cap(result); i++ {
result[i] = hex[2*i]<<4 | hex[2*i+1]
}
return result
}
// bytesToHexNibbles splits bytes into nibbles
// (with terminator flag). Prefix flag is not removed.
func bytesToHexNibbles(bytes []byte) []byte {
nibbles := make([]byte, len(bytes)*2+1)
for i, b := range bytes {
nibbles[i*2] = b / 16
nibbles[i*2+1] = b % 16
}
nibbles[len(nibbles)-1] = 16
return nibbles
}
// decodeCompact unpacks compact encoding
// into a hex sequence of nibbles.
func decodeCompact(compact []byte) []byte {
base := bytesToHexNibbles(compact)
// remove the terminator flag
if base[0] < 2 {
base = base[:len(base)-1]
}
// remove prefix flag
if base[0]&1 == 1 {
// odd length
base = base[1:]
} else {
// even length
base = base[2:]
}
return base
}