File: dump.go

package info (click to toggle)
fq 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 106,624 kB
  • sloc: xml: 2,835; makefile: 250; sh: 241; exp: 57; ansic: 21
file content (432 lines) | stat: -rw-r--r-- 11,758 bytes parent folder | download
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
426
427
428
429
430
431
432
package interp

import (
	"errors"
	"fmt"
	"io"
	"strconv"
	"strings"

	"github.com/wader/fq/internal/ansi"
	"github.com/wader/fq/internal/asciiwriter"
	"github.com/wader/fq/internal/bitioex"
	"github.com/wader/fq/internal/columnwriter"
	"github.com/wader/fq/internal/hexpairwriter"
	"github.com/wader/fq/internal/mathex"
	"github.com/wader/fq/pkg/bitio"
	"github.com/wader/fq/pkg/decode"
	"github.com/wader/fq/pkg/scalar"
)

// TODO: refactor this
// move more things to jq?
// select columns?
// smart line wrap instead of truncate?
// binary/octal/... dump?

// 0   12      34    56
// addr|hexdump|ascii|field
const (
	colAddr  = 0
	colHex   = 2
	colASCII = 4
	colField = 6
)

const rootIndentWidth = 2
const treeIndentWidth = 2

func isCompound(v *decode.Value) bool {
	switch v.V.(type) {
	case *decode.Compound:
		return true
	default:
		return false
	}
}

type dumpCtx struct {
	opts        *Options
	buf         []byte
	cw          *columnwriter.Writer
	hexHeader   string
	asciiHeader string
}

func indentStr(n int) string {
	const spaces = "                                                                "
	for n > len(spaces) {
		return strings.Repeat(" ", n)
	}
	return spaces[0:n]
}

func dumpEx(v *decode.Value, ctx *dumpCtx, depth int, rootV *decode.Value, rootDepth int, addrWidth int) error {
	opts := ctx.opts
	cw := ctx.cw
	buf := ctx.buf
	deco := ctx.opts.Decorator

	// no error check as we write into buffering column
	// we check for err later for Flush()
	cprint := func(c int, a ...any) {
		fmt.Fprint(cw.Columns[c], a...)
	}
	cfmt := func(c int, format string, a ...any) {
		fmt.Fprintf(cw.Columns[c], format, a...)
	}

	isInArray := false
	isCompound := isCompound(v)
	inArrayLen := 0
	if v.Parent != nil {
		if dc, ok := v.Parent.V.(*decode.Compound); ok {
			isInArray = dc.IsArray
			inArrayLen = len(dc.Children)
		}
	}

	nameV := v
	name := nameV.Name
	if isInArray {
		nameV = v.Parent
		name = ""
	}
	if depth == 0 {
		name = valuePathExprDecorated(nameV, deco)
	} else {
		name = deco.ObjectKey.Wrap(name)
	}

	rootIndent := indentStr(rootIndentWidth * rootDepth)
	indent := indentStr(treeIndentWidth * depth)

	if opts.ArrayTruncate != 0 && depth != 0 && isInArray && v.Index >= opts.ArrayTruncate {
		cfmt(colField, "%s%s%s:%s%s: ...",
			indent,
			deco.Index.F("["),
			deco.Number.F(strconv.Itoa(v.Index)),
			deco.Number.F(strconv.Itoa(inArrayLen)),
			deco.Index.F("]"),
		)
		cw.Flush()
		return decode.ErrWalkBreak
	}

	innerRange := v.InnerRange()
	willDisplayData := innerRange.Len > 0 && (!isCompound || (opts.Depth != 0 && opts.Depth == depth))

	// show address bar on root, nested root and format change
	if depth == 0 || v.IsRoot || v.Format != nil {
		cfmt(colHex, "%s", deco.DumpHeader.F(ctx.hexHeader))
		cfmt(colASCII, "%s", deco.DumpHeader.F(ctx.asciiHeader))

		if willDisplayData {
			cw.Flush()
		}
	}

	cfmt(colField, "%s%s", indent, name)
	if isInArray {
		cfmt(colField, "%s%s%s", deco.Index.F("["), deco.Number.F(strconv.Itoa(v.Index)), deco.Index.F("]"))
	}

	var desc string
	isSynthetic := false

	switch vv := v.V.(type) {
	case *decode.Compound:
		if vv.IsArray {
			cfmt(colField, "%s%s:%s%s", deco.Index.F("["), deco.Number.F("0"), deco.Number.F(strconv.Itoa(len(vv.Children))), deco.Index.F("]"))
		} else {
			cfmt(colField, "%s", deco.Object.F("{}"))
		}
		cprint(colField, ":")
		desc = vv.Description

	case scalar.Scalarable:
		cprint(colField, ":")
		actual := vv.ScalarActual()
		sym := vv.ScalarSym()
		df := vv.ScalarDisplayFormat()
		if sym == nil {
			cfmt(colField, " %s", deco.ValueColor(actual).F(previewValue(actual, df)))
		} else {
			cfmt(colField, " %s", deco.ValueColor(sym).F(previewValue(sym, scalar.NumberDecimal)))
			cfmt(colField, " (%s)", deco.ValueColor(actual).F(previewValue(actual, df)))
		}
		desc = vv.ScalarDescription()
		isSynthetic = vv.ScalarFlags().IsSynthetic()

	default:
		panic(fmt.Sprintf("unreachable vv %#+v", vv))
	}

	if isCompound {
		if isInArray {
			cfmt(colField, " %s", v.Name)
		}
		if desc != "" {
			cfmt(colField, " %s", deco.Value.F(desc))
		}
	} else {
		if opts.Verbose && isInArray {
			cfmt(colField, " %s", v.Name)
		}
		if desc != "" {
			cfmt(colField, " (%s)", deco.Value.F(desc))
		}
	}

	if v.Format != nil {
		cfmt(colField, " (%s)", deco.Value.F(v.Format.Name))
	}
	valueErr := v.Err

	if opts.Verbose && !isSynthetic {
		cfmt(colField, " %s (%s)",
			mathex.BitRange(innerRange).StringByteBits(opts.Addrbase), mathex.Bits(innerRange.Len).StringByteBits(opts.Sizebase))
	}

	cprint(colField, "\n")

	if valueErr != nil {
		var printErrs func(depth int, err error)
		printErrs = func(depth int, err error) {
			indent := indentStr(treeIndentWidth * depth)

			var formatErr decode.FormatError
			var decodeFormatsErr decode.FormatsError

			switch {
			case errors.As(err, &formatErr):
				cfmt(colField, "%s  %s: %s: %s\n", indent, deco.Error.F("error"), formatErr.Format.Name, formatErr.Err.Error())

				if opts.Verbose {
					for _, f := range formatErr.Stacktrace.Frames() {
						cfmt(colField, "%s    %s\n", indent, f.Function)
						cfmt(colField, "%s      %s:%d\n", indent, f.File, f.Line)
					}
				}
				switch {
				case errors.Is(formatErr.Err, decode.FormatsError{}):
					printErrs(depth+1, formatErr.Err)
				}
			case errors.As(err, &decodeFormatsErr):
				cfmt(colField, "%s  %s\n", indent, err)
				for _, e := range decodeFormatsErr.Errs {
					printErrs(depth+1, e)
				}
			default:
				cfmt(colField, "%s!%s\n", indent, deco.Error.F(err.Error()))
			}
		}

		printErrs(depth, valueErr)
	}

	rootBitLen, err := bitioex.Len(rootV.RootReader)
	if err != nil {
		return err
	}

	bufferLastBit := rootBitLen - 1
	startBit := innerRange.Start
	stopBit := innerRange.Stop() - 1
	sizeBits := innerRange.Len
	lastDisplayBit := stopBit

	if opts.DisplayBytes > 0 && sizeBits > int64(opts.DisplayBytes)*8 {
		lastDisplayBit = startBit + (int64(opts.DisplayBytes)*8 - 1)
		if lastDisplayBit%(int64(opts.LineBytes)*8) != 0 {
			lastDisplayBit += (int64(opts.LineBytes) * 8) - lastDisplayBit%(int64(opts.LineBytes)*8) - 1
		}

		if lastDisplayBit > stopBit || stopBit-lastDisplayBit <= int64(opts.LineBytes)*8 {
			lastDisplayBit = stopBit
		}
	}

	bufferLastByte := bufferLastBit / 8
	startByte := startBit / 8
	stopByte := stopBit / 8
	lastDisplayByte := lastDisplayBit / 8
	displaySizeBytes := lastDisplayByte - startByte + 1
	displaySizeBits := displaySizeBytes * 8
	maxDisplaySizeBits := bufferLastBit - startByte*8 + 1
	if sizeBits == 0 {
		displaySizeBits = 0
	}
	if displaySizeBits > maxDisplaySizeBits {
		displaySizeBits = maxDisplaySizeBits
	}

	startLine := startByte / int64(opts.LineBytes)
	startLineByteOffset := startByte % int64(opts.LineBytes)
	startLineByte := startLine * int64(opts.LineBytes)
	lastDisplayLine := lastDisplayByte / int64(opts.LineBytes)

	// has length and is not compound or a collapsed struct/array (max depth)
	if willDisplayData {
		cfmt(colAddr, "%s%s\n",
			rootIndent, deco.DumpAddr.F(mathex.PadFormatInt(startLineByte, opts.Addrbase, true, addrWidth)))

		vBR, err := bitioex.Range(rootV.RootReader, startByte*8, displaySizeBits)
		if err != nil {
			return err
		}

		addrLines := lastDisplayLine - startLine + 1
		hexpairFn := func(b byte) string { return deco.ByteColor(b).Wrap(hexpairwriter.Pair(b)) }
		asciiFn := func(b byte) string { return deco.ByteColor(b).Wrap(asciiwriter.SafeASCII(b)) }

		hexBR, err := bitio.CloneReadSeeker(vBR)
		if err != nil {
			return err
		}
		if _, err := bitioex.CopyBitsBuffer(
			hexpairwriter.New(cw.Columns[colHex], opts.LineBytes, int(startLineByteOffset), hexpairFn),
			hexBR,
			buf); err != nil {
			return err
		}

		asciiBR, err := bitio.CloneReadSeeker(vBR)
		if err != nil {
			return err
		}
		if _, err := bitioex.CopyBitsBuffer(
			asciiwriter.New(cw.Columns[colASCII], opts.LineBytes, int(startLineByteOffset), asciiFn),
			asciiBR,
			buf); err != nil {
			return err
		}

		for i := int64(1); i < addrLines; i++ {
			lineStartByte := startLineByte + i*int64(opts.LineBytes)
			cfmt(colAddr, "%s%s\n", rootIndent, deco.DumpAddr.F(mathex.PadFormatInt(lineStartByte, opts.Addrbase, true, addrWidth)))
		}
		// TODO: correct? should rethink columnwriter api maybe?
		lastLineStopByte := startLineByte + addrLines*int64(opts.LineBytes) - 1
		if lastDisplayByte == bufferLastByte && lastDisplayByte != lastLineStopByte {
			// extra "|" as end markers
			cfmt(colHex, "%s\n", deco.Column)
			cfmt(colASCII, "%s\n", deco.Column)
		}

		if stopByte != lastDisplayByte {
			isEnd := ""
			if stopBit == bufferLastBit {
				isEnd = " (end)"
			}

			cfmt(colAddr, "%s%s\n", rootIndent, deco.DumpAddr.F("*"))
			cprint(colHex, "\n")
			// TODO: truncate if display_bytes is small?
			cfmt(colHex, "until %s%s (%s)",
				mathex.Bits(stopBit).StringByteBits(opts.Addrbase),
				isEnd,
				mathex.PadFormatInt(bitio.BitsByteCount(sizeBits), opts.Sizebase, true, 0))
			// TODO: dump last line?
		}
	}

	if err := cw.Flush(); err != nil {
		return err
	}

	return nil
}

func dump(v *decode.Value, w io.Writer, opts *Options) error {
	maxAddrIndentWidth := 0
	makeWalkFn := func(fn decode.WalkFn) decode.WalkFn {
		return func(v *decode.Value, rootV *decode.Value, depth int, rootDepth int) error {
			if opts.Depth != 0 && depth > opts.Depth {
				return decode.ErrWalkSkipChildren
			}

			return fn(v, rootV, depth, rootDepth)
		}
	}

	_ = v.WalkPreOrder(makeWalkFn(func(v *decode.Value, _ *decode.Value, _ int, rootDepth int) error {
		maxAddrIndentWidth = mathex.Max(
			maxAddrIndentWidth,
			rootIndentWidth*rootDepth+mathex.DigitsInBase(bitio.BitsByteCount(v.InnerRange().Stop()), true, opts.Addrbase),
		)
		return nil
	}))

	var displayLenFn func(s string) int
	var displayTruncateFn func(s string, start, stop int) string
	if opts.Color {
		displayLenFn = ansi.Len
		displayTruncateFn = ansi.Slice
	}

	addrColumnWidth := maxAddrIndentWidth
	hexColumnWidth := opts.LineBytes*3 - 1
	asciiColumnWidth := opts.LineBytes
	treeColumnWidth := -1
	// TODO: set with and truncate/wrap properly
	// if opts.Width != 0 {
	// 	treeColumnWidth = mathex.Max(0, opts.Width-(addrColumnWidth+hexColumnWidth+asciiColumnWidth+3 /* bars */))
	// }

	cw := columnwriter.New(
		w,
		&columnwriter.MultiLineColumn{Width: addrColumnWidth, LenFn: displayLenFn, SliceFn: displayTruncateFn},
		columnwriter.BarColumn(opts.Decorator.Column),
		&columnwriter.MultiLineColumn{Width: hexColumnWidth, LenFn: displayLenFn, SliceFn: displayTruncateFn},
		columnwriter.BarColumn(opts.Decorator.Column),
		&columnwriter.MultiLineColumn{Width: asciiColumnWidth, LenFn: displayLenFn, SliceFn: displayTruncateFn},
		columnwriter.BarColumn(opts.Decorator.Column),
		&columnwriter.MultiLineColumn{Width: treeColumnWidth, Wrap: false, LenFn: displayLenFn, SliceFn: displayTruncateFn},
	)

	buf := make([]byte, 32*1024)

	var hexHeader string
	var asciiHeader string
	for i := 0; i < opts.LineBytes; i++ {
		s := mathex.PadFormatInt(int64(i), opts.Addrbase, false, 2)
		hexHeader += s
		if i < opts.LineBytes-1 {
			hexHeader += " "
		}
		asciiHeader += s[len(s)-1:]
	}

	ctx := &dumpCtx{
		opts:        opts,
		buf:         buf,
		cw:          cw,
		hexHeader:   hexHeader,
		asciiHeader: asciiHeader,
	}

	return v.WalkPreOrder(makeWalkFn(func(v *decode.Value, rootV *decode.Value, depth int, rootDepth int) error {
		return dumpEx(v, ctx, depth, rootV, rootDepth, maxAddrIndentWidth-rootDepth)
	}))
}

func hexdump(w io.Writer, bv Binary, opts *Options) error {
	br, err := bitioex.Range(bv.br, bv.r.Start, bv.r.Len)
	if err != nil {
		return err
	}

	// TODO: hack
	opts.Verbose = true
	return dump(
		&decode.Value{
			// TODO: hack
			V:          &scalar.BitBuf{Actual: br},
			Range:      bv.r,
			RootReader: bv.br,
		},
		w,
		opts,
	)
}