-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.go
70 lines (58 loc) · 1.42 KB
/
utils.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
package binary
import (
"crypto/ed25519"
"encoding/binary"
)
func PutKey32(dst []byte, src []byte, offset *int) {
copy(dst, src)
*offset += ed25519.PublicKeySize
}
func PutOptionalKey32(dst []byte, src []byte, offset *int) {
if len(src) > 0 {
dst[0] = 1
copy(dst[4:], src)
}
*offset += 4 + ed25519.PublicKeySize
}
func PutUint64(dst []byte, v uint64, offset *int) {
binary.LittleEndian.PutUint64(dst, v)
*offset += 8
}
func PutUint32(dst []byte, v uint32, offset *int) {
binary.LittleEndian.PutUint32(dst, v)
*offset += 4
}
func PutOptionalUint64(dst []byte, v *uint64, offset *int) {
if v != nil {
dst[0] = 1
binary.LittleEndian.PutUint64(dst[4:], *v)
}
*offset += 4 + 8
}
func GetKey32(src []byte, dst *ed25519.PublicKey, offset *int) {
*dst = make([]byte, ed25519.PublicKeySize)
copy(*dst, src)
*offset += ed25519.PublicKeySize
}
func GetOptionalKey32(src []byte, dst *ed25519.PublicKey, offset *int) {
if src[0] == 1 {
*dst = make([]byte, ed25519.PublicKeySize)
copy(*dst, src[4:])
}
*offset += 4 + ed25519.PublicKeySize
}
func GetUint64(src []byte, dst *uint64, offset *int) {
*dst = binary.LittleEndian.Uint64(src)
*offset += 8
}
func GetUint32(src []byte, dst *uint32, offset *int) {
*dst = binary.LittleEndian.Uint32(src)
*offset += 4
}
func GetOptionalUint64(src []byte, dst **uint64, offset *int) {
if src[0] == 1 {
val := binary.LittleEndian.Uint64(src[4:])
*dst = &val
}
*offset += 4 + 8
}