forked from pk910/dynamic-ssz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
116 lines (96 loc) · 3.01 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package dynssz
import (
"encoding/binary"
"fmt"
"time"
)
var (
ErrOffset = fmt.Errorf("incorrect offset")
ErrSize = fmt.Errorf("incorrect size")
ErrBytesLength = fmt.Errorf("bytes array does not have the correct length")
ErrVectorLength = fmt.Errorf("vector does not have the correct length")
ErrListTooBig = fmt.Errorf("list length is higher than max value")
ErrEmptyBitlist = fmt.Errorf("bitlist is empty")
ErrInvalidVariableOffset = fmt.Errorf("invalid ssz encoding. first variable element offset indexes into fixed value data")
)
// ---- Unmarshal functions ----
// unmarshallUint64 unmarshals a little endian uint64 from the src input
func unmarshallUint64(src []byte) uint64 {
return binary.LittleEndian.Uint64(src)
}
// unmarshallUint32 unmarshals a little endian uint32 from the src input
func unmarshallUint32(src []byte) uint32 {
return binary.LittleEndian.Uint32(src[:4])
}
// unmarshallUint16 unmarshals a little endian uint16 from the src input
func unmarshallUint16(src []byte) uint16 {
return binary.LittleEndian.Uint16(src[:2])
}
// unmarshallUint8 unmarshals a little endian uint8 from the src input
func unmarshallUint8(src []byte) uint8 {
return uint8(src[0])
}
// unmarshalBool unmarshals a boolean from the src input
func unmarshalBool(src []byte) bool {
if src[0] == 1 {
return true
}
return false
}
// unmarshalTime unmarshals a time.Time from the src input
func unmarshalTime(src []byte) time.Time {
return time.Unix(int64(unmarshallUint64(src)), 0).UTC()
}
// ---- Marshal functions ----
// marshalUint64 marshals a little endian uint64 to dst
func marshalUint64(dst []byte, i uint64) []byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, i)
dst = append(dst, buf...)
return dst
}
// marshalUint32 marshals a little endian uint32 to dst
func marshalUint32(dst []byte, i uint32) []byte {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, i)
dst = append(dst, buf...)
return dst
}
// marshalUint16 marshals a little endian uint16 to dst
func marshalUint16(dst []byte, i uint16) []byte {
buf := make([]byte, 2)
binary.LittleEndian.PutUint16(buf, i)
dst = append(dst, buf...)
return dst
}
// marshalUint8 marshals a little endian uint8 to dst
func marshalUint8(dst []byte, i uint8) []byte {
dst = append(dst, byte(i))
return dst
}
// marshalBool marshals a boolean to dst
func marshalBool(dst []byte, b bool) []byte {
if b {
dst = append(dst, 1)
} else {
dst = append(dst, 0)
}
return dst
}
// marshalTime marshals a time to dst
func marshalTime(dst []byte, t time.Time) []byte {
return marshalUint64(dst, uint64(t.Unix()))
}
// ---- offset functions ----
// WriteOffset writes an offset to dst
func writeOffset(dst []byte, i int) []byte {
return marshalUint32(dst, uint32(i))
}
// ReadOffset reads an offset from buf
func readOffset(buf []byte) uint64 {
return uint64(binary.LittleEndian.Uint32(buf))
}
// DivideInt divides the int fully
func divideInt(a, b int) (int, bool) {
return a / b, a%b == 0
}