forked from Sahil-4555/fastssz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
96 lines (82 loc) · 2.7 KB
/
encode.go
File metadata and controls
96 lines (82 loc) · 2.7 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
package ssz
import (
"encoding/binary"
"fmt"
"unsafe"
)
// MarshalSSZ marshals an object
func MarshalSSZ(m Marshaler) ([]byte, error) {
buf := make([]byte, m.SizeSSZ())
return m.MarshalSSZTo(buf[:0])
}
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)
}
type marshalUints interface {
~uint8 | ~uint16 | ~uint32 | ~uint64
}
// MarshalUint marshals a little endian uint8, uint16, uint32, or uint64 to dst.
func MarshalUint[T marshalUints](dst []byte, i T) []byte {
switch unsafe.Sizeof(i) {
case 1:
return append(dst, byte(i))
case 2:
return binary.LittleEndian.AppendUint16(dst, uint16(i))
case 4:
return binary.LittleEndian.AppendUint32(dst, uint32(i))
case 8:
return binary.LittleEndian.AppendUint64(dst, uint64(i))
default:
panic("unsupported uint size")
}
}
// MarshalUint64 marshals a little endian uint64 to dst.
//
// Deprecated: use MarshalUint instead.
func MarshalUint64(dst []byte, i uint64) []byte {
return MarshalUint(dst, i)
}
// MarshalUint32 marshals a little endian uint32 to dst.
//
// Deprecated: use MarshalUint instead.
func MarshalUint32(dst []byte, i uint32) []byte {
return MarshalUint(dst, i)
}
// MarshalUint16 marshals a little endian uint16 to dst.
//
// Deprecated: use MarshalUint instead.
func MarshalUint16(dst []byte, i uint16) []byte {
return MarshalUint(dst, i)
}
// MarshalUint8 marshals a little endian uint8 to dst.
//
// Deprecated: use MarshalUint instead.
func MarshalUint8(dst []byte, i uint8) []byte {
return MarshalUint(dst, i)
}
// MarshalBool marshals a boolean to dst
func MarshalBool(dst []byte, b bool) []byte {
if b {
return append(dst, 1)
}
return append(dst, 0)
}
// WriteOffset writes an offset to dst
func WriteOffset(dst []byte, i int) []byte {
return MarshalUint(dst, uint32(i))
}