-
Notifications
You must be signed in to change notification settings - Fork 45
/
encode.go
319 lines (270 loc) · 7.73 KB
/
encode.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package ssz
import (
"encoding/binary"
"fmt"
"math/bits"
"time"
)
// MarshalSSZ marshals an object
func MarshalSSZ(m Marshaler) ([]byte, error) {
buf := make([]byte, m.SizeSSZ())
return m.MarshalSSZTo(buf[:0])
}
// Errors
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")
)
func ErrBytesLengthFn(name string, found, expected int) error {
return fmt.Errorf("%s (%v): expected %d and %d found", name, ErrBytesLength, expected, found)
}
func ErrVectorLengthFn(name string, found, expected int) error {
return fmt.Errorf("%s (%v): expected %d and %d found", name, ErrBytesLength, expected, found)
}
func ErrListTooBigFn(name string, found, max int) error {
return fmt.Errorf("%s (%v): max expected %d and %d found", name, ErrListTooBig, max, found)
}
// ---- 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))
}
func safeReadOffset(buf []byte) (uint64, []byte, error) {
if len(buf) < 4 {
return 0, nil, fmt.Errorf("")
}
offset := ReadOffset(buf)
return offset, buf[4:], nil
}
// ---- extend functions ----
func extendByteSlice(b []byte, needLen int) []byte {
b = b[:cap(b)]
if n := needLen - cap(b); n > 0 {
b = append(b, make([]byte, n)...)
}
return b[:needLen]
}
// ExtendUint64 extends a uint64 buffer to a given size
func ExtendUint64(b []uint64, needLen int) []uint64 {
if b == nil {
b = []uint64{}
}
b = b[:cap(b)]
if n := needLen - cap(b); n > 0 {
b = append(b, make([]uint64, n)...)
}
return b[:needLen]
}
// ExtendUint32 extends a uint32 buffer to a given size
func ExtendUint32(b []uint32, needLen int) []uint32 {
if b == nil {
b = []uint32{}
}
b = b[:cap(b)]
if n := needLen - cap(b); n > 0 {
b = append(b, make([]uint32, n)...)
}
return b[:needLen]
}
// ExtendUint16 extends a uint16 buffer to a given size
func ExtendUint16(b []uint16, needLen int) []uint16 {
if b == nil {
b = []uint16{}
}
b = b[:cap(b)]
if n := needLen - cap(b); n > 0 {
b = append(b, make([]uint16, n)...)
}
return b[:needLen]
}
// ExtendUint16 extends a uint16 buffer to a given size
func ExtendUint8(b []uint8, needLen int) []uint8 {
if b == nil {
b = []uint8{}
}
b = b[:cap(b)]
if n := needLen - cap(b); n > 0 {
b = append(b, make([]uint8, n)...)
}
return b[:needLen]
}
// ---- unmarshal dynamic content ----
const bytesPerLengthOffset = 4
// 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, fmt.Errorf("not enough data")
}
offset := binary.LittleEndian.Uint32(buf[:4])
length, ok := DivideInt(int(offset), bytesPerLengthOffset)
if !ok {
return 0, fmt.Errorf("incorrect length division")
}
if length > maxSize {
return 0, fmt.Errorf("too big for the list")
}
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 fmt.Errorf("incorrect end of offset: %d %d", offset, endOffset)
}
if endOffset > size {
return fmt.Errorf("incorrect end of offset size: %d %d", endOffset, size)
}
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("failed to divide int %d by %d", a, b)
}
if num > max {
return 0, fmt.Errorf("num %d is greater than max %d", num, max)
}
return num, nil
}
// DivideInt divides the int fully
func DivideInt(a, b int) (int, bool) {
return a / b, a%b == 0
}