forked from ferranbt/fastssz
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdecode.go
More file actions
253 lines (217 loc) · 5.94 KB
/
Copy pathdecode.go
File metadata and controls
253 lines (217 loc) · 5.94 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package ssz
import (
"encoding/binary"
"errors"
"fmt"
"math/bits"
"unsafe"
)
const bytesPerLengthOffset = 4
type unmarshallUints interface {
~uint8 | ~uint16 | ~uint32 | ~uint64
}
// UnmarshallUint unmarshals a little endian uint8, uint16, uint32, or uint64 from the src input.
func UnmarshallUint[T unmarshallUints](src []byte) T {
var zero T
switch unsafe.Sizeof(zero) {
case 1:
return T(src[0])
case 2:
return T(binary.LittleEndian.Uint16(src[:2]))
case 4:
return T(binary.LittleEndian.Uint32(src[:4]))
case 8:
return T(binary.LittleEndian.Uint64(src[:8]))
default:
panic("unsupported uint size")
}
}
// UnmarshallUint64 unmarshals a little endian uint64 from the src input.
//
// Deprecated: use UnmarshallUint instead.
func UnmarshallUint64(src []byte) uint64 {
return UnmarshallUint[uint64](src)
}
// UnmarshallUint32 unmarshals a little endian uint32 from the src input.
//
// Deprecated: use UnmarshallUint instead.
func UnmarshallUint32(src []byte) uint32 {
return UnmarshallUint[uint32](src)
}
// UnmarshallUint16 unmarshals a little endian uint16 from the src input.
//
// Deprecated: use UnmarshallUint instead.
func UnmarshallUint16(src []byte) uint16 {
return UnmarshallUint[uint16](src)
}
// UnmarshallUint8 unmarshals a little endian uint8 from the src input.
//
// Deprecated: use UnmarshallUint instead.
func UnmarshallUint8(src []byte) uint8 {
return UnmarshallUint[uint8](src)
}
// UnmarshalBool unmarshals a boolean from the src input
func UnmarshalBool(src []byte) bool {
if src[0] == 1 {
return true
}
return false
}
var (
ErrOffsetExceedsSize = errors.New("offset exceeds size of buffer")
ErrOffsetOrdering = errors.New("offset is less than previous offset")
ErrDynamicLengthTooShort = errors.New("buffer too small to hold an offset")
ErrDynamicLengthNotOffsetSized = errors.New("list offsets must be multiples of the offset size (4)")
ErrDynamicLengthExceedsMax = errors.New("list length longer than ssz max length for the type")
ErrInvalidEncoding = errors.New("invalid encoding")
)
// ValidateBitlist validates that the bitlist is correct
func ValidateBitlist(buf []byte, bitLimit uint64) error {
byteLen := len(buf)
if byteLen == 0 {
return fmt.Errorf("bitlist empty, it does not have length bit")
}
// Maximum possible bytes in a bitlist with provided bitlimit.
maxBytes := (bitLimit >> 3) + 1
if byteLen > int(maxBytes) {
return fmt.Errorf("unexpected number of bytes, got %d but found %d", byteLen, maxBytes)
}
// The most significant bit is present in the last byte in the array.
last := buf[byteLen-1]
if last == 0 {
return fmt.Errorf("trailing byte is zero")
}
// Determine the position of the most significant bit.
msb := bits.Len8(last)
// The absolute position of the most significant bit will be the number of
// bits in the preceding bytes plus the position of the most significant
// bit. Subtract this value by 1 to determine the length of the bitlist.
numOfBits := uint64(8*(byteLen-1) + msb - 1)
if numOfBits > bitLimit {
return fmt.Errorf("too many bits")
}
return nil
}
// DecodeDynamicLength decodes the length from the dynamic input
func DecodeDynamicLength(buf []byte, maxSize int) (int, error) {
if len(buf) == 0 {
return 0, nil
}
if len(buf) < 4 {
return 0, ErrDynamicLengthTooShort
}
o := int(binary.LittleEndian.Uint32(buf))
if o%bytesPerLengthOffset != 0 || o == 0 {
return 0, ErrDynamicLengthNotOffsetSized
}
length := o / bytesPerLengthOffset
if length > maxSize {
return 0, ErrDynamicLengthExceedsMax
}
return length, nil
}
// UnmarshalDynamic unmarshals the dynamic items from the input
func UnmarshalDynamic(src []byte, length int, f func(indx int, b []byte) error) error {
var err error
size := uint64(len(src))
if length == 0 {
if size != 0 && size != 4 {
return ErrSize
}
return nil
}
indx := 0
dst := src
var offset, endOffset uint64
offset, dst = ReadOffset(src), dst[4:]
for {
if length != 1 {
endOffset, dst, err = safeReadOffset(dst)
if err != nil {
return err
}
} else {
endOffset = uint64(len(src))
}
if offset > endOffset {
return ErrOffsetOrdering
}
if endOffset > size {
return ErrOffsetExceedsSize
}
err := f(indx, src[offset:endOffset])
if err != nil {
return err
}
indx++
offset = endOffset
if length == 1 {
break
}
length--
}
return nil
}
func DivideInt2(a, b, max int) (int, error) {
num, ok := DivideInt(a, b)
if !ok {
return 0, fmt.Errorf("a is not evenly divisble by b")
}
if num > max {
return 0, fmt.Errorf("a/b is greater than max")
}
return num, nil
}
// DivideInt divides the int fully
func DivideInt(a, b int) (int, bool) {
return a / b, a%b == 0
}
type uints interface {
~uint8 | ~uint16 | ~uint64
}
// ExtendUint extends an unsigned integer buffer to a given size.
func ExtendUint[T uints](b []T, needLen int) []T {
b = b[:cap(b)]
if n := needLen - cap(b); n > 0 {
b = append(b, make([]T, n)...)
}
return b[:needLen]
}
// ExtendUint64 extends a uint64 buffer to a given size.
//
// Deprecated: use ExtendUint instead.
func ExtendUint64(b []uint64, needLen int) []uint64 {
return ExtendUint(b, needLen)
}
// ExtendUint16 extends a uint16 buffer to a given size.
//
// Deprecated: use ExtendUint instead.
func ExtendUint16(b []uint16, needLen int) []uint16 {
return ExtendUint(b, needLen)
}
// ExtendUint8 extends a uint8 buffer to a given size.
//
// Deprecated: use ExtendUint instead.
func ExtendUint8(b []uint8, needLen int) []uint8 {
return ExtendUint(b, needLen)
}
// ReadOffset reads an offset from buf
func ReadOffset(buf []byte) uint64 {
return uint64(binary.LittleEndian.Uint32(buf))
}
func safeReadOffset(buf []byte) (uint64, []byte, error) {
if len(buf) < 4 {
return 0, nil, fmt.Errorf("")
}
offset := ReadOffset(buf)
return offset, buf[4:], nil
}
func DecodeBool(src []byte) (bool, error) {
if src[0] == 1 {
return true, nil
}
if src[0] == 0 {
return false, nil
}
return false, ErrInvalidEncoding
}