forked from smallnest/gosercomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsky_group_skyencoder.go
148 lines (113 loc) · 2.75 KB
/
sky_group_skyencoder.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
// Code generated by github.com/skycoin/skyencoder. DO NOT EDIT.
package gosercomp
import (
"errors"
math "math"
"github.com/skycoin/skycoin/src/cipher/encoder"
)
// EncodeSizeSkyGroup computes the size of an encoded object of type SkyGroup
func EncodeSizeSkyGroup(obj *SkyGroup) int {
i0 := 0
// obj.Id
i0 += 4
// obj.Name
i0 += 4 + len(obj.Name)
// obj.Colors
i0 += 4
for _, x := range obj.Colors {
i1 := 0
// x
i1 += 4 + len(x)
i0 += i1
}
return i0
}
// EncodeSkyGroup encodes an object of type SkyGroup to the buffer in encoder.Encoder.
// The buffer must be large enough to encode the object, otherwise an error is returned.
func EncodeSkyGroup(buf []byte, obj *SkyGroup) error {
e := &encoder.Encoder{
Buffer: buf[:],
}
// obj.Id
e.Int32(obj.Id)
// obj.Name length check
if len(obj.Name) > math.MaxUint32 {
return errors.New("obj.Name length exceeds math.MaxUint32")
}
// obj.Name
e.ByteSlice([]byte(obj.Name))
// obj.Colors length check
if len(obj.Colors) > math.MaxUint32 {
return errors.New("obj.Colors length exceeds math.MaxUint32")
}
// obj.Colors length
e.Uint32(uint32(len(obj.Colors)))
// obj.Colors
for _, x := range obj.Colors {
// x length check
if len(x) > math.MaxUint32 {
return errors.New("x length exceeds math.MaxUint32")
}
// x
e.ByteSlice([]byte(x))
}
return nil
}
// DecodeSkyGroup decodes an object of type SkyGroup from the buffer in encoder.Decoder.
// Returns the number of bytes used from the buffer to decode the object.
func DecodeSkyGroup(buf []byte, obj *SkyGroup) (int, error) {
d := &encoder.Decoder{
Buffer: buf[:],
}
{
// obj.Id
i, err := d.Int32()
if err != nil {
return len(buf) - len(d.Buffer), err
}
obj.Id = i
}
{
// obj.Name
ul, err := d.Uint32()
if err != nil {
return len(buf) - len(d.Buffer), err
}
length := int(ul)
if length < 0 || length > len(d.Buffer) {
return len(buf) - len(d.Buffer), encoder.ErrBufferUnderflow
}
obj.Name = string(d.Buffer[:length])
d.Buffer = d.Buffer[length:]
}
{
// obj.Colors
ul, err := d.Uint32()
if err != nil {
return len(buf) - len(d.Buffer), err
}
length := int(ul)
if length < 0 || length > len(d.Buffer) {
return len(buf) - len(d.Buffer), encoder.ErrBufferUnderflow
}
if length != 0 {
obj.Colors = make([]string, length)
for z1 := range obj.Colors {
{
// obj.Colors[z1]
ul, err := d.Uint32()
if err != nil {
return len(buf) - len(d.Buffer), err
}
length := int(ul)
if length < 0 || length > len(d.Buffer) {
return len(buf) - len(d.Buffer), encoder.ErrBufferUnderflow
}
obj.Colors[z1] = string(d.Buffer[:length])
d.Buffer = d.Buffer[length:]
}
}
}
}
return len(buf) - len(d.Buffer), nil
}