-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
62 lines (52 loc) · 1.08 KB
/
util.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
package asn1
import (
"bytes"
"reflect"
"strings"
)
func cut(s, sep string) (before, after string, found bool) {
if i := strings.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}
func reverseBytes(b []byte) []byte {
for i, j := 0, len(b)-1; i < len(b)/2; i++ {
b[i], b[j-i] = b[j-i], b[i]
}
return b
}
func encodeUint(n uint64) []byte {
length := uintLength(n)
buf := make([]byte, length)
for i := 0; i < length; i++ {
shift := uint((length - 1 - i) * 8)
buf[i] = byte(n >> int(shift))
}
return buf
}
func uintLength(i uint64) (length int) {
length = 1
for i > 255 {
length++
i >>= 8
}
return
}
// https://en.wikipedia.org/wiki/Variable-length_quantity
func encodeBase128(num uint64) []byte {
buf := new(bytes.Buffer)
for num != 0 {
i := num & 0x7f
num >>= 7
if len(buf.Bytes()) != 0 {
i |= 0x80
}
buf.WriteByte(byte(i))
}
return reverseBytes(buf.Bytes())
}
func empty(value reflect.Value) bool {
defaultValue := reflect.Zero(value.Type())
return reflect.DeepEqual(value.Interface(), defaultValue.Interface())
}