forked from tendermint/go-amino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-encode.go
425 lines (363 loc) · 10.3 KB
/
binary-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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package amino
import (
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
"time"
"github.com/davecgh/go-spew/spew"
)
//----------------------------------------
// cdc.encodeReflectBinary
// This is the main entrypoint for encoding all types in binary form. This
// function calls encodeReflectBinary*, and generally those functions should
// only call this one, for the prefix bytes are only written here.
// The value may be a nil interface, but not a nil pointer.
// The following contracts apply to all similar encode methods.
// CONTRACT: rv is not a pointer
// CONTRACT: rv is valid.
func (cdc *Codec) encodeReflectBinary(w io.Writer, info *TypeInfo, rv reflect.Value, opts FieldOptions) (err error) {
if rv.Kind() == reflect.Ptr {
panic("should not happen")
}
if !rv.IsValid() {
panic("should not happen")
}
if printLog {
spew.Printf("(E) encodeReflectBinary(info: %v, rv: %#v (%v), opts: %v)\n",
info, rv.Interface(), rv.Type(), opts)
defer func() {
fmt.Printf("(E) -> err: %v\n", err)
}()
}
// Maybe write prefix+typ3 bytes.
if info.Registered {
var typ = typeToTyp4(info.Type, opts).Typ3()
_, err = w.Write(info.Prefix.WithTyp3(typ).Bytes())
if err != nil {
return
}
}
err = cdc._encodeReflectBinary(w, info, rv, opts)
return
}
// CONTRACT: any disamb/prefix+typ3 bytes have already been written.
func (cdc *Codec) _encodeReflectBinary(w io.Writer, info *TypeInfo, rv reflect.Value, opts FieldOptions) (err error) {
if rv.Kind() == reflect.Ptr {
panic("should not happen")
}
if !rv.IsValid() {
panic("should not happen")
}
if printLog {
spew.Printf("(_) _encodeReflectBinary(info: %v, rv: %#v (%v), opts: %v)\n",
info, rv.Interface(), rv.Type(), opts)
defer func() {
fmt.Printf("(_) -> err: %v\n", err)
}()
}
// Handle override if rv implements json.Marshaler.
if info.IsAminoMarshaler {
// First, encode rv into repr instance.
var rrv, rinfo = reflect.Value{}, (*TypeInfo)(nil)
rrv, err = toReprObject(rv)
if err != nil {
return
}
rinfo, err = cdc.getTypeInfo_wlock(info.AminoMarshalReprType)
if err != nil {
return
}
// Then, encode the repr instance.
err = cdc._encodeReflectBinary(w, rinfo, rrv, opts)
return
}
switch info.Type.Kind() {
//----------------------------------------
// Complex
case reflect.Interface:
err = cdc.encodeReflectBinaryInterface(w, info, rv, opts)
case reflect.Array:
if info.Type.Elem().Kind() == reflect.Uint8 {
err = cdc.encodeReflectBinaryByteArray(w, info, rv, opts)
} else {
err = cdc.encodeReflectBinaryList(w, info, rv, opts)
}
case reflect.Slice:
if info.Type.Elem().Kind() == reflect.Uint8 {
err = cdc.encodeReflectBinaryByteSlice(w, info, rv, opts)
} else {
err = cdc.encodeReflectBinaryList(w, info, rv, opts)
}
case reflect.Struct:
err = cdc.encodeReflectBinaryStruct(w, info, rv, opts)
//----------------------------------------
// Signed
case reflect.Int64:
if opts.BinVarint {
err = EncodeVarint(w, rv.Int())
} else {
err = EncodeInt64(w, rv.Int())
}
case reflect.Int32:
err = EncodeInt32(w, int32(rv.Int()))
case reflect.Int16:
err = EncodeInt16(w, int16(rv.Int()))
case reflect.Int8:
err = EncodeInt8(w, int8(rv.Int()))
case reflect.Int:
err = EncodeVarint(w, rv.Int())
//----------------------------------------
// Unsigned
case reflect.Uint64:
if opts.BinVarint {
err = EncodeUvarint(w, rv.Uint())
} else {
err = EncodeUint64(w, rv.Uint())
}
case reflect.Uint32:
err = EncodeUint32(w, uint32(rv.Uint()))
case reflect.Uint16:
err = EncodeUint16(w, uint16(rv.Uint()))
case reflect.Uint8:
err = EncodeUint8(w, uint8(rv.Uint()))
case reflect.Uint:
err = EncodeUvarint(w, rv.Uint())
//----------------------------------------
// Misc
case reflect.Bool:
err = EncodeBool(w, rv.Bool())
case reflect.Float64:
if !opts.Unsafe {
err = errors.New("Amino float* support requires `amino:\"unsafe\"`.")
return
}
err = EncodeFloat64(w, rv.Float())
case reflect.Float32:
if !opts.Unsafe {
err = errors.New("Amino float* support requires `amino:\"unsafe\"`.")
return
}
err = EncodeFloat32(w, float32(rv.Float()))
case reflect.String:
err = EncodeString(w, rv.String())
//----------------------------------------
// Default
default:
panic(fmt.Sprintf("unsupported type %v", info.Type.Kind()))
}
return
}
func (cdc *Codec) encodeReflectBinaryInterface(w io.Writer, iinfo *TypeInfo, rv reflect.Value, opts FieldOptions) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryInterface")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
// Special case when rv is nil, write 0x0000.
if rv.IsNil() {
_, err = w.Write([]byte{0x00, 0x00})
return
}
// Get concrete non-pointer reflect value & type.
var crv, isPtr, isNilPtr = derefPointers(rv.Elem())
if isPtr && crv.Kind() == reflect.Interface {
// See "MARKER: No interface-pointers" in codec.go
panic("should not happen")
}
if isNilPtr {
panic(fmt.Sprintf("Illegal nil-pointer of type %v for registered interface %v. "+
"For compatibility with other languages, nil-pointer interface values are forbidden.", crv.Type(), iinfo.Type))
}
var crt = crv.Type()
// Get *TypeInfo for concrete type.
var cinfo *TypeInfo
cinfo, err = cdc.getTypeInfo_wlock(crt)
if err != nil {
return
}
if !cinfo.Registered {
err = fmt.Errorf("Cannot encode unregistered concrete type %v.", crt)
return
}
// Write disambiguation bytes if needed.
var needDisamb bool = false
if iinfo.AlwaysDisambiguate {
needDisamb = true
} else if len(iinfo.Implementers[cinfo.Prefix]) > 1 {
needDisamb = true
}
if needDisamb {
_, err = w.Write(append([]byte{0x00}, cinfo.Disamb[:]...))
if err != nil {
return
}
}
// Write prefix+typ3 bytes.
var typ = typeToTyp3(crt, opts)
_, err = w.Write(cinfo.Prefix.WithTyp3(typ).Bytes())
if err != nil {
return
}
// Write actual concrete value.
err = cdc._encodeReflectBinary(w, cinfo, crv, opts)
return
}
func (cdc *Codec) encodeReflectBinaryByteArray(w io.Writer, info *TypeInfo, rv reflect.Value, opts FieldOptions) (err error) {
ert := info.Type.Elem()
if ert.Kind() != reflect.Uint8 {
panic("should not happen")
}
length := info.Type.Len()
// Get byteslice.
var byteslice = []byte(nil)
if rv.CanAddr() {
byteslice = rv.Slice(0, length).Bytes()
} else {
byteslice = make([]byte, length)
reflect.Copy(reflect.ValueOf(byteslice), rv) // XXX: looks expensive!
}
// Write byte-length prefixed byteslice.
err = EncodeByteSlice(w, byteslice)
return
}
func (cdc *Codec) encodeReflectBinaryList(w io.Writer, info *TypeInfo, rv reflect.Value, opts FieldOptions) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryList")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
ert := info.Type.Elem()
if ert.Kind() == reflect.Uint8 {
panic("should not happen")
}
// Write element Typ4 byte.
var typ = typeToTyp4(ert, opts)
err = EncodeByte(w, byte(typ))
if err != nil {
return
}
// Write length.
err = EncodeUvarint(w, uint64(rv.Len()))
if err != nil {
return
}
// Write elems.
var einfo *TypeInfo
einfo, err = cdc.getTypeInfo_wlock(ert)
if err != nil {
return
}
for i := 0; i < rv.Len(); i++ {
// Get dereferenced element value and info.
var erv, void = isVoid(rv.Index(i))
if typ.IsPointer() {
// We must write a byte to denote whether element is nil.
if void {
// Value is nil or empty.
// e.g. nil pointer, nil/empty slice, pointer to nil/empty slice, pointer
// to nil pointer. Write 0x01 for "is nil".
// NOTE: Do not use a pointer to nil/empty slices to denote
// existence or not. We have to make a design choice here, and
// here we discourage using pointers to denote existence.
_, err = w.Write([]byte{0x01})
continue
} else {
// Value is not nil or empty. Write 0x00 for "not nil/empty".
_, err = w.Write([]byte{0x00})
}
}
// Write the element value.
// It may be a nil interface, but not a nil pointer.
err = cdc.encodeReflectBinary(w, einfo, erv, opts)
if err != nil {
return
}
}
return
}
// CONTRACT: info.Type.Elem().Kind() == reflect.Uint8
func (cdc *Codec) encodeReflectBinaryByteSlice(w io.Writer, info *TypeInfo, rv reflect.Value, opts FieldOptions) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryByteSlice")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
ert := info.Type.Elem()
if ert.Kind() != reflect.Uint8 {
panic("should not happen")
}
// Write byte-length prefixed byte-slice.
var byteslice = rv.Bytes()
err = EncodeByteSlice(w, byteslice)
return
}
func (cdc *Codec) encodeReflectBinaryStruct(w io.Writer, info *TypeInfo, rv reflect.Value, opts FieldOptions) (err error) {
if printLog {
fmt.Println("(e) encodeReflectBinaryBinaryStruct")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
// The "Struct" Typ3 doesn't get written here.
// It's already implied, either by struct-key or list-element-type-byte.
switch info.Type {
case timeType:
// Special case: time.Time
err = EncodeTime(w, rv.Interface().(time.Time))
return
default:
for _, field := range info.Fields {
// Get dereferenced field value and info.
var frv, void = isVoid(rv.Field(field.Index))
if void {
// Do not encode nil or empty fields.
continue
}
var finfo *TypeInfo
finfo, err = cdc.getTypeInfo_wlock(field.Type)
if err != nil {
return
}
// TODO Maybe allow omitempty somehow.
// Write field key (number and type).
err = encodeFieldNumberAndTyp3(w, field.BinFieldNum, field.BinTyp3)
if err != nil {
return
}
// Write field from rv.
err = cdc.encodeReflectBinary(w, finfo, frv, field.FieldOptions)
if err != nil {
return
}
}
// Write "StructTerm".
err = EncodeByte(w, byte(Typ3_StructTerm))
if err != nil {
return
}
return
}
}
//----------------------------------------
// Misc.
// Write field key.
func encodeFieldNumberAndTyp3(w io.Writer, num uint32, typ Typ3) (err error) {
if (typ & 0xF8) != 0 {
panic(fmt.Sprintf("invalid Typ3 byte %X", typ))
}
if num < 0 || num > (1<<29-1) {
panic(fmt.Sprintf("invalid field number %v", num))
}
// Pack Typ3 and field number.
var value64 = (uint64(num) << 3) | uint64(typ)
// Write uvarint value for field and Typ3.
var buf [10]byte
n := binary.PutUvarint(buf[:], value64)
_, err = w.Write(buf[0:n])
return
}