This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
amino.go
366 lines (324 loc) · 8.99 KB
/
amino.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
package amino
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
)
//----------------------------------------
// Typ3 and Typ4
type Typ3 uint8
type Typ4 uint8 // Typ3 | 0x08 (pointer bit)
const (
// Typ3 types
Typ3_Varint = Typ3(0)
Typ3_8Byte = Typ3(1)
Typ3_ByteLength = Typ3(2)
Typ3_Struct = Typ3(3)
Typ3_StructTerm = Typ3(4)
Typ3_4Byte = Typ3(5)
Typ3_List = Typ3(6)
Typ3_Interface = Typ3(7)
// Typ4 bit
Typ4_Pointer = Typ4(0x08)
)
func (typ Typ3) String() string {
switch typ {
case Typ3_Varint:
return "Varint"
case Typ3_8Byte:
return "8Byte"
case Typ3_ByteLength:
return "ByteLength"
case Typ3_Struct:
return "Struct"
case Typ3_StructTerm:
return "StructTerm"
case Typ3_4Byte:
return "4Byte"
case Typ3_List:
return "List"
case Typ3_Interface:
return "Interface"
default:
return fmt.Sprintf("<Invalid Typ3 %X>", byte(typ))
}
}
func (typ Typ4) Typ3() Typ3 { return Typ3(typ & 0x07) }
func (typ Typ4) IsPointer() bool { return (typ & 0x08) > 0 }
func (typ Typ4) String() string {
if typ&0xF0 != 0 {
return fmt.Sprintf("<Invalid Typ4 %X>", byte(typ))
}
if typ&0x08 != 0 {
return "*" + Typ3(typ&0x07).String()
} else {
return Typ3(typ).String()
}
}
//----------------------------------------
// *Codec methods
// MarshalBinary encodes the object o according to the Amino spec,
// but prefixed by a uvarint encoding of the object to encode.
// Use MarshalBinaryBare if you don't want byte-length prefixing.
//
// For consistency, MarshalBinary will first dereference pointers
// before encoding. MarshalBinary will panic if o is a nil-pointer,
// or if o is invalid.
func (cdc *Codec) MarshalBinary(o interface{}) ([]byte, error) {
// Write the bytes here.
var buf = new(bytes.Buffer)
// Write the bz without length-prefixing.
bz, err := cdc.MarshalBinaryBare(o)
if err != nil {
return nil, err
}
// Write uvarint(len(bz)).
err = EncodeUvarint(buf, uint64(len(bz)))
if err != nil {
return nil, err
}
// Write bz.
_, err = buf.Write(bz)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// MarshalBinaryWriter writes the bytes as would be returned from
// MarshalBinary to the writer w.
func (cdc *Codec) MarshalBinaryWriter(w io.Writer, o interface{}) (n int64, err error) {
var bz, _n = []byte(nil), int(0)
bz, err = cdc.MarshalBinary(o)
if err != nil {
return 0, err
}
_n, err = w.Write(bz) // TODO: handle overflow in 32-bit systems.
n = int64(_n)
return
}
// Panics if error.
func (cdc *Codec) MustMarshalBinary(o interface{}) []byte {
bz, err := cdc.MarshalBinary(o)
if err != nil {
panic(err)
}
return bz
}
// MarshalBinaryBare encodes the object o according to the Amino spec.
// MarshalBinaryBare doesn't prefix the byte-length of the encoding,
// so the caller must handle framing.
func (cdc *Codec) MarshalBinaryBare(o interface{}) ([]byte, error) {
// Dereference value if pointer.
var rv, _, isNilPtr = derefPointers(reflect.ValueOf(o))
if isNilPtr {
// NOTE: You can still do so by calling
// `.MarshalBinary(struct{ *SomeType })` or so on.
panic("MarshalBinary cannot marshal a nil pointer directly. Try wrapping in a struct?")
}
// Encode Amino:binary bytes.
var bz []byte
buf := new(bytes.Buffer)
rt := rv.Type()
info, err := cdc.getTypeInfo_wlock(rt)
if err != nil {
return nil, err
}
err = cdc.encodeReflectBinary(buf, info, rv, FieldOptions{})
if err != nil {
return nil, err
}
bz = buf.Bytes()
return bz, nil
}
// Panics if error.
func (cdc *Codec) MustMarshalBinaryBare(o interface{}) []byte {
bz, err := cdc.MarshalBinaryBare(o)
if err != nil {
panic(err)
}
return bz
}
// Like UnmarshalBinaryBare, but will first decode the byte-length prefix.
// UnmarshalBinary will panic if ptr is a nil-pointer.
// Returns an error if not all of bz is consumed.
func (cdc *Codec) UnmarshalBinary(bz []byte, ptr interface{}) error {
if len(bz) == 0 {
return errors.New("UnmarshalBinary cannot decode empty bytes")
}
// Read byte-length prefix.
u64, n := binary.Uvarint(bz)
if n < 0 {
return fmt.Errorf("Error reading msg byte-length prefix: got code %v", n)
}
if u64 > uint64(len(bz)-n) {
return fmt.Errorf("Not enough bytes to read in UnmarshalBinary, want %v more bytes but only have %v",
u64, len(bz)-n)
} else if u64 < uint64(len(bz)-n) {
return fmt.Errorf("Bytes left over in UnmarshalBinary, should read %v more bytes but have %v",
u64, len(bz)-n)
}
bz = bz[n:]
// Decode.
return cdc.UnmarshalBinaryBare(bz, ptr)
}
// Like UnmarshalBinaryBare, but will first read the byte-length prefix.
// UnmarshalBinaryReader will panic if ptr is a nil-pointer.
// If maxSize is 0, there is no limit (not recommended).
func (cdc *Codec) UnmarshalBinaryReader(r io.Reader, ptr interface{}, maxSize int64) (n int64, err error) {
if maxSize < 0 {
panic("maxSize cannot be negative.")
}
// Read byte-length prefix.
var l int64
var buf [binary.MaxVarintLen64]byte
for i := 0; i < len(buf); i++ {
_, err = r.Read(buf[i : i+1])
if err != nil {
return
}
n += 1
if buf[i]&0x80 == 0 {
break
}
if n >= maxSize {
err = fmt.Errorf("Read overflow, maxSize is %v but uvarint(length-prefix) is itself greater than maxSize.", maxSize)
}
}
u64, _ := binary.Uvarint(buf[:])
if err != nil {
return
}
if maxSize > 0 {
if uint64(maxSize) < u64 {
err = fmt.Errorf("Read overflow, maxSize is %v but this amino binary object is %v bytes.", maxSize, u64)
return
}
if (maxSize - n) < int64(u64) {
err = fmt.Errorf("Read overflow, maxSize is %v but this length-prefixed amino binary object is %v+%v bytes.", maxSize, n, u64)
return
}
}
l = int64(u64)
if l < 0 {
err = fmt.Errorf("Read overflow, this implementation can't read this because, why would anyone have this much data? Hello from 2018.")
}
// Read that many bytes.
var bz = make([]byte, l, l)
_, err = io.ReadFull(r, bz)
if err != nil {
return
}
n += l
// Decode.
err = cdc.UnmarshalBinaryBare(bz, ptr)
return
}
// Panics if error.
func (cdc *Codec) MustUnmarshalBinary(bz []byte, ptr interface{}) {
err := cdc.UnmarshalBinary(bz, ptr)
if err != nil {
panic(err)
}
}
// UnmarshalBinaryBare will panic if ptr is a nil-pointer.
func (cdc *Codec) UnmarshalBinaryBare(bz []byte, ptr interface{}) error {
if len(bz) == 0 {
return errors.New("UnmarshalBinaryBare cannot decode empty bytes")
}
rv, rt := reflect.ValueOf(ptr), reflect.TypeOf(ptr)
if rv.Kind() != reflect.Ptr {
panic("Unmarshal expects a pointer")
}
rv, rt = rv.Elem(), rt.Elem()
info, err := cdc.getTypeInfo_wlock(rt)
if err != nil {
return err
}
n, err := cdc.decodeReflectBinary(bz, info, rv, FieldOptions{})
if err != nil {
return err
}
if n != len(bz) {
return fmt.Errorf("Unmarshal didn't read all bytes. Expected to read %v, only read %v", len(bz), n)
}
return nil
}
// Panics if error.
func (cdc *Codec) MustUnmarshalBinaryBare(bz []byte, ptr interface{}) {
err := cdc.UnmarshalBinaryBare(bz, ptr)
if err != nil {
panic(err)
}
}
func (cdc *Codec) MarshalJSON(o interface{}) ([]byte, error) {
rv := reflect.ValueOf(o)
if rv.Kind() == reflect.Invalid {
return []byte("null"), nil
}
rt := rv.Type()
// Note that we can't yet skip directly
// to checking if a type implements
// json.Marshaler because in some cases
// var s GenericInterface = t1(v1)
// var t GenericInterface = t2(v1)
// but we need to be able to encode
// both s and t disambiguated, so:
// {"type":<disfix>, "value":<data>}
// for the above case.
w := new(bytes.Buffer)
info, err := cdc.getTypeInfo_wlock(rt)
if err != nil {
return nil, err
}
if err := cdc.encodeReflectJSON(w, info, rv, FieldOptions{}); err != nil {
return nil, err
}
return w.Bytes(), nil
}
func (cdc *Codec) UnmarshalJSON(bz []byte, ptr interface{}) error {
if len(bz) == 0 {
return errors.New("UnmarshalJSON cannot decode empty bytes")
}
rv := reflect.ValueOf(ptr)
if rv.Kind() != reflect.Ptr {
return errors.New("UnmarshalJSON expects a pointer")
}
// If the type implements json.Unmarshaler, just
// automatically respect that and skip to it.
// if rv.Type().Implements(jsonUnmarshalerType) {
// return rv.Interface().(json.Unmarshaler).UnmarshalJSON(bz)
// }
// 1. Dereference until we find the first addressable type.
rv = rv.Elem()
rt := rv.Type()
info, err := cdc.getTypeInfo_wlock(rt)
if err != nil {
return err
}
return cdc.decodeReflectJSON(bz, info, rv, FieldOptions{})
}
// MarshalJSONIndent calls json.Indent on the output of cdc.MarshalJSON
// using the given prefix and indent string.
func (cdc *Codec) MarshalJSONIndent(o interface{}, prefix, indent string) ([]byte, error) {
bz, err := cdc.MarshalJSON(o)
if err != nil {
return nil, err
}
var out bytes.Buffer
err = json.Indent(&out, bz, prefix, indent)
if err != nil {
return nil, err
}
return out.Bytes(), nil
}
//----------------------------------------
// Misc.
var (
jsonMarshalerType = reflect.TypeOf(new(json.Marshaler)).Elem()
jsonUnmarshalerType = reflect.TypeOf(new(json.Unmarshaler)).Elem()
errorType = reflect.TypeOf(new(error)).Elem()
)