-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
213 lines (163 loc) · 3.67 KB
/
dump.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
package jq
import (
"fmt"
"io"
"nikand.dev/go/cbor"
)
type (
Dumper struct {
Writer io.Writer
Decoder Decoder
Base int
b []byte
arr []Off
}
)
func Dump(b []byte) string {
return (&Dumper{Base: -1}).DumpBytes(b)
}
func NewDumper(w io.Writer) *Dumper { return &Dumper{Writer: w, Base: -1} }
func (d *Dumper) ApplyTo(b *Buffer, off Off, next bool) (Off, bool, error) {
if next {
return None, false, nil
}
if d.Writer == nil {
return off, false, nil
}
d.dumpBuffer(b)
_, err := d.Writer.Write(d.b)
if err != nil {
return off, false, err
}
return off, false, nil
}
func (d *Dumper) DumpBytes(b []byte) string {
d.b = d.b[:0]
d.dump(b, 0)
if d.Base >= 0 {
d.Base += len(b) // TODO: base
d.b = fmt.Appendf(d.b, "%06x ", d.Base)
}
d.b = fmt.Appendf(d.b, "%06x\n", len(b))
return string(d.b)
}
func (d *Dumper) Dump(b *Buffer) string {
if b == nil {
return "<nil>"
}
d.dumpBuffer(b)
return string(d.b)
}
func (d *Dumper) dumpBuffer(b *Buffer) {
d.dump(b.B, 0)
if d.Base >= 0 {
d.Base += len(b.B)
d.b = fmt.Appendf(d.b, "%06x ", d.Base)
}
d.b = fmt.Appendf(d.b, "%06x\n", len(b.B))
}
func (d *Dumper) dump(b []byte, depth int) {
const spaces = " "
defer func() {
p := recover()
if p == nil {
return
}
defer panic(p)
d.b = fmt.Appendf(d.b, "panic: %v\n", p)
}()
for i := 0; i >= 0 && i < len(b); {
st := i
tag := d.Decoder.TagOnly(b, i)
// log.Printf("dump loop %x", i)
if d.Base >= 0 {
d.b = fmt.Appendf(d.b, "%06x ", d.Base+i)
}
d.b = fmt.Appendf(d.b, "%06x %s", i, spaces[:depth])
switch tag {
case cbor.Int:
var v uint64
v, i = d.Decoder.Unsigned(b, i)
d.b = fmt.Appendf(d.b, "% 02x %d\n", b[st:i], v)
case cbor.Neg:
var v uint64
v, i = d.Decoder.Unsigned(b, i)
d.b = fmt.Appendf(d.b, "% 02x -%d\n", b[st:i], v)
case cbor.Bytes:
_, i = d.Decoder.Bytes(b, i)
d.b = fmt.Appendf(d.b, "% 02x\n", b[st:i])
case cbor.String:
var v []byte
v, i = d.Decoder.Bytes(b, i)
d.b = fmt.Appendf(d.b, "% 02x ", b[st:i])
d.b = d.appendQStr(d.b, v)
case cbor.Simple:
i = d.Decoder.Skip(b, i)
d.b = fmt.Appendf(d.b, "% 02x\n", b[st:i])
case cbor.Labeled:
_, _, i = d.Decoder.CBOR.Tag(b, i)
d.b = fmt.Appendf(d.b, "% 02x\n", b[st:i])
depth += 4
continue
case cbor.Array, cbor.Map:
_, l, s, end := d.Decoder.TagArrayMap(b, i)
d.arr, i = d.Decoder.ArrayMap(b, i, d.arr[:0])
d.b = fmt.Appendf(d.b, "% 02x l %2d s %d\n", b[st:end], l, s)
for j, off := range d.arr {
if d.Base >= 0 {
d.b = fmt.Appendf(d.b, "%s", spaces[:8])
}
d.b = fmt.Appendf(d.b, "%s", spaces[:8+depth+4])
d.b = fmt.Appendf(d.b, "%3x: %6x\n", j, int64(off))
}
default:
panic(tag)
}
if depth > 0 {
depth -= 4
}
}
}
func (d *Dumper) encodeString(w []byte, b *Buffer, off Off) ([]byte, error) {
tag := b.Reader().Tag(off)
r, st := b.Buf(off)
w = append(w, '"')
w, i := d.encStr(w, r, st)
w = append(w, '"')
if i < 0 {
return w, NewTypeError(tag, cbor.Bytes, cbor.String)
}
return w, nil
}
func (d *Dumper) encStr(w, r []byte, i int) ([]byte, int) {
tag, sub, i := d.Decoder.CBOR.Tag(r, i)
l := int(sub)
if tag != cbor.Bytes && tag != cbor.String {
return w, -1
}
if l >= 0 {
return d.appendQStr(w, r[i:i+l]), i + l
}
for !d.Decoder.CBOR.Break(r, &i) {
w, i = d.encStr(w, r, i)
if i < 0 {
return w, i
}
}
return w, i
}
func (d *Dumper) appendQStr(w, v []byte) []byte {
var qq, bq bool
for _, c := range v {
qq = qq || c == '"'
bq = bq || c == '`'
}
if qq && !bq {
w = append(w, '`')
w = append(w, v...)
w = append(w, '`', '\n')
} else {
w = fmt.Appendf(w, "%q\n", v)
}
return w
}