forked from tendermint/go-amino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aminoscan.go
314 lines (292 loc) · 7.2 KB
/
aminoscan.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
package main
import (
"encoding/binary"
"encoding/hex"
"errors"
"flag"
"fmt"
"os"
"github.com/tendermint/go-amino"
)
func main() {
// Print help.
if len(os.Args) == 1 {
fmt.Println(`Usage: aminoscan <STRUCT HEXBYTES> or --help`)
return
}
// Parse flags...
var colorize bool
var concreteName string
flgs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flgs.BoolVar(&colorize, "color", false, "Just print the colored bytes and exit.")
flgs.StringVar(&concreteName, "concrete-name", "", "Just print the concrete bytes for a concrete name and exit.")
err := flgs.Parse(os.Args[1:])
if err == flag.ErrHelp {
fmt.Println(`Usage: aminoscan <STRUCT HEXBYTES> or --help
You can also use aminoscan to print "colored" bytes. This view will
try to display bytes in ascii in a different color if it happens to be
a printable character.
> aminoscan --color <HEXBYTES>`)
return
} else if err != nil {
fmt.Println(err)
return
}
// If we just want to show colored bytes...
if colorize {
if flgs.Arg(0) == "" {
fmt.Println(`Usage: aminoscan --color <HEXBYTES>`)
return
}
bz := hexDecode(flgs.Arg(0))
fmt.Println(ColoredBytes(bz, Green, Blue))
return
}
// If we just want to print the concrete bytes...
if concreteName != "" {
db, pb := amino.NameToDisfix(concreteName)
fmt.Printf("Disamb bytes: %X\nPrefix bytes: %X\n", db, pb)
return
}
// Parse struct Amino bytes.
bz := hexDecode(os.Args[1]) // Read input hex bytes.
fmt.Println(Yellow("## Root Struct (assumed)"))
s, n, err := scanStruct(bz, "", true) // Assume that it's struct.
s += Red(fmt.Sprintf("%X", bz[n:])) // Bytes remaining are red.
fmt.Println(Yellow("## Root Struct END"))
fmt.Println(s, n, err) // Print color-encoded bytes s.
}
func scanAny(typ amino.Typ3, bz []byte, indent string) (s string, n int, err error) {
switch typ {
case amino.Typ3_Varint:
s, n, err = scanVarint(bz, indent)
case amino.Typ3_8Byte:
s, n, err = scan8Byte(bz, indent)
case amino.Typ3_ByteLength:
s, n, err = scanByteLength(bz, indent)
case amino.Typ3_4Byte:
s, n, err = scan4Byte(bz, indent)
default:
panic("should not happen")
}
return
}
func scanVarint(bz []byte, indent string) (s string, n int, err error) {
if len(bz) == 0 {
err = fmt.Errorf("EOF while reading (U)Varint")
}
// First try Varint.
var i64, okI64 = int64(0), true
i64, n = binary.Varint(bz)
if n <= 0 {
n = 0
okI64 = false
}
// Then try Uvarint.
var u64, okU64, _n = uint64(0), true, int(0)
u64, _n = binary.Uvarint(bz)
if n != _n {
n = 0
okU64 = false
}
// If neither work, return error.
if !okI64 && !okU64 {
err = fmt.Errorf("Invalid (u)varint")
return
}
// s is the same either way.
s = Cyan(fmt.Sprintf("%X", bz[:n]))
fmt.Printf("%s%s (", indent, s)
if okI64 {
fmt.Printf("i64:%v ", i64)
}
if okU64 {
fmt.Printf("u64:%v", u64)
}
fmt.Print(")\n")
return
}
func scan8Byte(bz []byte, indent string) (s string, n int, err error) {
if len(bz) < 8 {
err = errors.New("EOF while reading 8byte field.")
return
}
n = 8
s = Blue(fmt.Sprintf("%X", bz[:8]))
fmt.Printf("%s%s\n", indent, s)
return
}
func scanByteLength(bz []byte, indent string) (s string, n int, err error) {
// Read the length.
var length, l64, _n = int(0), uint64(0), int(0)
l64, _n = binary.Uvarint(bz)
if n < 0 {
n = 0
err = errors.New("error decoding uvarint")
return
}
length = int(l64)
if length >= len(bz) {
err = errors.New("EOF while reading byte-length delimited.")
return
}
s = Cyan(fmt.Sprintf("%X", bz[:_n]))
slide(&bz, &n, _n)
// Read the remaining bytes.
s += Green(fmt.Sprintf("%X", bz[:length]))
slide(&bz, &n, length)
fmt.Printf("%s%s (%v bytes)\n", indent, s, length)
return
}
func scanStruct(bz []byte, indent string, isRoot bool) (s string, n int, err error) {
var _s, _n, typ = string(""), int(0), amino.Typ3(0x00)
for {
if isRoot && len(bz) == 0 {
return
}
_s, typ, _n, err = scanFieldKey(bz, indent+" ")
if slide(&bz, &n, _n) && concat(&s, _s) && err != nil {
return
}
_s, _n, err = scanAny(typ, bz, indent+" ")
if slide(&bz, &n, _n) && concat(&s, _s) && err != nil {
return
}
}
return
}
func scanFieldKey(bz []byte, indent string) (s string, typ amino.Typ3, n int, err error) {
var u64 uint64
u64, n = binary.Uvarint(bz)
if n < 0 {
n = 0
err = errors.New("error decoding uvarint")
return
}
typ = amino.Typ3(u64 & 0x07)
var number uint32 = uint32(u64 >> 3)
s = fmt.Sprintf("%X", bz[:n])
fmt.Printf("%s%s @%v %v\n", indent, s, number, typ)
return
}
func scan4Byte(bz []byte, indent string) (s string, n int, err error) {
if len(bz) < 4 {
err = errors.New("EOF while reading 4byte field.")
return
}
n = 4
s = Blue(fmt.Sprintf("%X", bz[:4]))
fmt.Printf("%s%s\n", indent, s)
return
}
/*
func scanList(bz []byte, indent string) (s string, n int, err error) {
// Read element Typ4.
if len(bz) < 1 {
err = errors.New("EOF while reading list element typ4.")
return
}
var typ = amino.Typ4(bz[0])
if typ&0xF0 > 0 {
err = errors.New("Invalid list element typ4 byte")
}
s = fmt.Sprintf("%X", bz[:1])
if slide(&bz, &n, 1) && err != nil {
return
}
// Read number of elements.
var num, _n = uint64(0), int(0)
num, _n = binary.Uvarint(bz)
if _n < 0 {
_n = 0
err = errors.New("error decoding list length (uvarint)")
}
s += Cyan(fmt.Sprintf("%X", bz[:_n]))
if slide(&bz, &n, _n) && err != nil {
return
}
fmt.Printf("%s%s of %v with %v items\n", indent, s, typ, num)
// Read elements.
var _s string
for i := 0; i < int(num); i++ {
// Maybe read nil byte.
if typ&0x08 != 0 {
if len(bz) == 0 {
err = errors.New("EOF while reading list nil byte")
return
}
var nb = bz[0]
slide(&bz, &n, 1)
switch nb {
case 0x00:
s += "00"
fmt.Printf("%s00 (not nil)\n", indent)
case 0x01:
s += "01" // Is nil (NOTE: reverse logic)
fmt.Printf("%s01 (is nil)\n", indent)
continue
default:
err = fmt.Errorf("Unexpected nil pointer byte %X", nb)
return
}
}
// Read element.
_s, _n, err = scanAny(typ.Typ3(), bz, indent+" ")
if slide(&bz, &n, _n) && concat(&s, _s) && err != nil {
return
}
}
return
}
*/
/*
func scanInterface(bz []byte, indent string) (s string, n int, err error) {
db, hasDb, pb, typ, _, isNil, _n, err := amino.DecodeDisambPrefixBytes(bz)
if slide(&bz, &n, _n) && err != nil {
return
}
pb3 := pb
if isNil {
s = Magenta("0000")
} else if hasDb {
s = Magenta(fmt.Sprintf("%X%X", db.Bytes(), pb3.Bytes()))
} else {
s = Magenta(fmt.Sprintf("%X", pb3.Bytes()))
}
if isNil {
fmt.Printf("%s%s (nil interface)\n", indent, s)
} else if hasDb {
fmt.Printf("%s%s (disamb: %X, prefix: %X, typ: %v)\n",
indent, s, db.Bytes(), pb.Bytes(), typ)
} else {
fmt.Printf("%s%s (prefix: %X, typ: %v)\n",
indent, s, pb.Bytes(), typ)
}
_s, _n, err := scanAny(typ, bz, indent)
if slide(&bz, &n, _n) && concat(&s, _s) && err != nil {
return
}
return
}
*/
//----------------------------------------
// Misc.
func slide(bzPtr *[]byte, n *int, _n int) bool {
if len(*bzPtr) < _n {
panic("eof")
}
*bzPtr = (*bzPtr)[_n:]
*n += _n
return true
}
func concat(sPtr *string, _s string) bool {
*sPtr += _s
return true
}
func hexDecode(s string) []byte {
bz, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return bz
}