-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdecode.go
284 lines (258 loc) · 5.81 KB
/
decode.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
package sproto
import (
"fmt"
"os"
"reflect"
)
type Tag struct {
Tag uint16
Val *uint16
}
func readChunk(chunk []byte) (int, []byte, error) {
if len(chunk) < 4 {
return 0, nil, ErrDecode
}
sz := int(readUint32(chunk))
expected := 4 + sz
if len(chunk) < expected {
return 0, nil, ErrDecode
}
return expected, chunk[4:expected], nil
}
func readUint16(buf []byte) uint16 {
var n uint16
n = uint16(buf[1]) << 8
n |= uint16(buf[0])
return n
}
func readUint32(buf []byte) uint32 {
var n uint32
n = uint32(buf[3]) << 24
n |= uint32(buf[2]) << 16
n |= uint32(buf[1]) << 8
n |= uint32(buf[0])
return n
}
func readUint64(buf []byte) uint64 {
var n uint64
n = uint64(buf[7]) << 56
n |= uint64(buf[6]) << 48
n |= uint64(buf[5]) << 40
n |= uint64(buf[4]) << 32
n |= uint64(buf[3]) << 24
n |= uint64(buf[2]) << 16
n |= uint64(buf[1]) << 8
n |= uint64(buf[0])
return n
}
func decodeBool(val *uint16, data []byte, sf *SprotoField, v reflect.Value) error {
if *val == 0 {
v.SetBool(false)
} else {
v.SetBool(true)
}
return nil
}
func decodeInt(val *uint16, data []byte, sf *SprotoField, v reflect.Value) error {
var n uint64
if val != nil {
n = uint64(*val)
} else {
switch len(data) {
case 0:
n = 0
case 4:
n = uint64(readUint32(data))
case 8:
n = readUint64(data)
default:
return fmt.Errorf("sproto: malformed integer data for field %s", sf.Name)
}
}
e := v.Type()
//v.Set(reflect.New(e).Elem())
switch e.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
v.SetInt(int64(n))
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
v.SetUint(n)
}
return nil
}
func decodeString(val *uint16, data []byte, sf *SprotoField, v reflect.Value) error {
str := string(data)
v.SetString(str)
return nil
}
func decodeBytes(val *uint16, data []byte, sf *SprotoField, v reflect.Value) error {
buf := make([]byte, len(data))
copy(buf, data)
v.SetBytes(buf)
return nil
}
func decodeBoolSlice(val *uint16, data []byte, sf *SprotoField, v reflect.Value) error {
vals := make([]bool, len(data))
for i, v := range data {
if v == 0 {
vals[i] = false
} else {
vals[i] = true
}
}
v.Set(reflect.ValueOf(vals))
return nil
}
func decodeIntSlice(val *uint16, data []byte, sf *SprotoField, v reflect.Value) error {
dataLen := len(data)
if dataLen < 1 {
return ErrDecode
}
intLen := int(data[0])
if (dataLen-1)%intLen != 0 {
return fmt.Errorf("sproto: malformed integer data for field %s", sf.Name)
}
sz := (dataLen - 1) / intLen
vals := reflect.MakeSlice(v.Type(), sz, sz)
data = data[1:]
var n uint64
for i := 0; i < sz; i++ {
if intLen == 4 {
n = uint64(readUint32(data[i*intLen:]))
} else {
n = readUint64(data[i*intLen:])
}
val := vals.Index(i)
switch e := v.Type().Elem(); e.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
val.SetInt(int64(n))
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
val.SetUint(n)
}
}
v.Set(vals)
return nil
}
func decodeStringSlice(val *uint16, data []byte, sf *SprotoField, v reflect.Value) error {
vals := make([]string, 0, 16)
for len(data) > 0 {
expected, val, err := readChunk(data)
if err != nil {
return err
}
vals = append(vals, string(val))
data = data[expected:]
}
v.Set(reflect.ValueOf(vals))
return nil
}
func decodeStruct(val *uint16, data []byte, sf *SprotoField, v reflect.Value) error {
// v1: pointer to struct
v1 := reflect.New(v.Type().Elem())
used, err := decodeMessage(data, sf.st, v1)
if err != nil {
return err
}
if used != len(data) {
return fmt.Errorf("sproto: malformed struct data for field %s", sf.Name)
}
v.Addr().Elem().Set(v1)
return nil
}
func decodeStructSlice(val *uint16, data []byte, sf *SprotoField, v reflect.Value) error {
vals := reflect.MakeSlice(v.Type(), 0, 16)
for len(data) > 0 {
expected, buf, err := readChunk(data)
if err != nil {
return err
}
// v1: pointer to struct
v1 := reflect.New(v.Type().Elem().Elem())
used, err := decodeMessage(buf, sf.st, v1)
if err != nil {
return err
}
if used != len(buf) {
return fmt.Errorf("sproto: malformed struct data for field %s", sf.Name)
}
vals = reflect.Append(vals, v1)
data = data[expected:]
}
v.Set(vals)
return nil
}
func decodeHeader(chunk []byte) (int, []Tag, error) {
if len(chunk) < 2 {
return 0, nil, ErrDecode
}
fn := int(readUint16(chunk))
expected := 2 + fn*2
if len(chunk) < expected {
return 0, nil, ErrDecode
}
tags := make([]Tag, fn)
n := 0
var tag uint16 = 0
for i := 0; i < fn; i++ {
v := readUint16(chunk[(i+1)*2:])
if v%2 != 0 { //skip tag
tag += (v + 1) / 2
continue
}
var val *uint16
if v != 0 { // value
v1 := v/2 - 1
val = &v1
}
tags[n] = Tag{
Tag: tag,
Val: val,
}
tag++
n++
}
return expected, tags[:n], nil
}
func decodeMessage(chunk []byte, st *SprotoType, v reflect.Value) (int, error) {
var total int
var tags []Tag
var err error
if total, tags, err = decodeHeader(chunk); err != nil {
return 0, err
}
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
for _, tag := range tags {
var used int
var data []byte
if tag.Val == nil {
if used, data, err = readChunk(chunk[total:]); err != nil {
return 0, err
}
total += used
}
sf := st.FieldByTag(int(tag.Tag))
if sf == nil {
fmt.Fprintf(os.Stderr, "sproto<%s>: unknown tag %d\n", st.Name, tag.Tag)
continue
}
v1 := v.FieldByIndex(sf.index)
if err = sf.dec(tag.Val, data, sf, v1); err != nil {
return 0, err
}
}
return total, nil
}
func Decode(data []byte, sp interface{}) (int, error) {
t, v, err := getbase(sp)
if err != nil {
return 0, err
}
// clear sp
v.Elem().Set(reflect.Zero(t.Elem()))
st, err := GetSprotoType(t.Elem())
if err != nil {
return 0, err
}
return decodeMessage(data, st, v)
}