2020-06-08 03:29:51 +03:00
|
|
|
package interp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
2022-04-16 12:22:56 +03:00
|
|
|
"github.com/wader/fq/internal/ansi"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/internal/asciiwriter"
|
2022-01-24 23:21:48 +03:00
|
|
|
"github.com/wader/fq/internal/bitioextra"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/internal/columnwriter"
|
|
|
|
"github.com/wader/fq/internal/hexpairwriter"
|
2022-01-15 20:55:57 +03:00
|
|
|
"github.com/wader/fq/internal/mathextra"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2022-04-16 12:22:56 +03:00
|
|
|
// TODO: refactor this
|
|
|
|
// move more things to jq?
|
|
|
|
// select columns?
|
|
|
|
// smart line wrap instead of truncate?
|
|
|
|
// binary/octal/... dump?
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
// 0 12 34 56
|
|
|
|
// addr|hexdump|ascii|field
|
|
|
|
const (
|
|
|
|
colAddr = 0
|
|
|
|
colHex = 2
|
2021-09-27 12:01:14 +03:00
|
|
|
colASCII = 4
|
2020-06-08 03:29:51 +03:00
|
|
|
colField = 6
|
|
|
|
)
|
|
|
|
|
2021-09-06 12:06:06 +03:00
|
|
|
func isCompound(v *decode.Value) bool {
|
|
|
|
switch v.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *decode.Compound:
|
2021-09-06 12:06:06 +03:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 13:38:43 +03:00
|
|
|
type dumpCtx struct {
|
|
|
|
opts Options
|
|
|
|
buf []byte
|
|
|
|
cw *columnwriter.Writer
|
|
|
|
hexHeader string
|
|
|
|
asciiHeader string
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
// no error check as we write into buffering column
|
|
|
|
// we check for err later for Flush()
|
2022-05-20 16:10:41 +03:00
|
|
|
cprint := func(c int, a ...any) {
|
2020-06-08 03:29:51 +03:00
|
|
|
fmt.Fprint(cw.Columns[c], a...)
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
cfmt := func(c int, format string, a ...any) {
|
2020-06-08 03:29:51 +03:00
|
|
|
fmt.Fprintf(cw.Columns[c], format, a...)
|
|
|
|
}
|
|
|
|
|
|
|
|
columns := func() {
|
|
|
|
cprint(1, deco.Column, "\n")
|
|
|
|
cprint(3, deco.Column, "\n")
|
|
|
|
cprint(5, deco.Column, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
isInArray := false
|
2021-08-30 13:51:42 +03:00
|
|
|
inArrayLen := 0
|
2020-06-08 03:29:51 +03:00
|
|
|
if v.Parent != nil {
|
2021-12-03 02:06:11 +03:00
|
|
|
if dc, ok := v.Parent.V.(*decode.Compound); ok {
|
2021-11-03 19:19:33 +03:00
|
|
|
isInArray = dc.IsArray
|
2021-12-03 02:06:11 +03:00
|
|
|
inArrayLen = len(dc.Children)
|
2021-08-30 13:51:42 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nameV := v
|
|
|
|
name := nameV.Name
|
|
|
|
if isInArray {
|
|
|
|
nameV = v.Parent
|
|
|
|
name = ""
|
|
|
|
}
|
|
|
|
if depth == 0 {
|
2022-04-08 17:47:40 +03:00
|
|
|
name = valuePathExprDecorated(nameV, deco)
|
2020-06-08 03:29:51 +03:00
|
|
|
} else {
|
|
|
|
name = deco.ObjectKey.Wrap(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
rootIndent := strings.Repeat(" ", rootDepth)
|
|
|
|
indent := strings.Repeat(" ", depth)
|
|
|
|
|
2022-05-20 13:38:43 +03:00
|
|
|
if depth == 0 {
|
2021-09-16 23:40:03 +03:00
|
|
|
if !isCompound(v) {
|
2020-06-08 03:29:51 +03:00
|
|
|
columns()
|
2021-09-16 23:40:03 +03:00
|
|
|
}
|
2022-05-20 13:38:43 +03:00
|
|
|
cfmt(colHex, "%s", deco.DumpHeader.F(ctx.hexHeader))
|
|
|
|
cfmt(colASCII, "%s", deco.DumpHeader.F(ctx.asciiHeader))
|
2021-09-16 23:40:03 +03:00
|
|
|
if !isCompound(v) {
|
2020-06-08 03:29:51 +03:00
|
|
|
cw.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 13:51:42 +03:00
|
|
|
if opts.ArrayTruncate != 0 && depth != 0 && isInArray && v.Index >= opts.ArrayTruncate {
|
|
|
|
columns()
|
|
|
|
cfmt(colField, "%s%s%s:%s%s: ...",
|
|
|
|
indent,
|
|
|
|
deco.Index.F("["),
|
|
|
|
deco.Number.F(strconv.Itoa(v.Index)),
|
2021-12-08 18:20:37 +03:00
|
|
|
deco.Number.F(strconv.Itoa(inArrayLen)),
|
2021-08-30 13:51:42 +03:00
|
|
|
deco.Index.F("]"),
|
|
|
|
)
|
|
|
|
cw.Flush()
|
|
|
|
return decode.ErrWalkBreak
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
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("]"))
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:19:33 +03:00
|
|
|
var valueErr error
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
// TODO: cleanup map[string]any []any or json format
|
2021-08-30 13:51:42 +03:00
|
|
|
// dump should use some internal interface instead?
|
2021-11-03 19:19:33 +03:00
|
|
|
switch vv := v.V.(type) {
|
2021-12-03 02:06:11 +03:00
|
|
|
case *decode.Compound:
|
2021-11-03 19:19:33 +03:00
|
|
|
if vv.IsArray {
|
2021-12-08 18:20:37 +03:00
|
|
|
cfmt(colField, "%s%s:%s%s", deco.Index.F("["), deco.Number.F("0"), deco.Number.F(strconv.Itoa(len(vv.Children))), deco.Index.F("]"))
|
2021-11-03 19:19:33 +03:00
|
|
|
} else {
|
2021-12-08 18:20:37 +03:00
|
|
|
cfmt(colField, "%s", deco.Object.F("{}"))
|
|
|
|
}
|
|
|
|
cprint(colField, ":")
|
2022-04-21 18:31:34 +03:00
|
|
|
if isInArray {
|
2021-12-08 18:20:37 +03:00
|
|
|
cfmt(colField, " %s", v.Name)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
if vv.Description != "" {
|
|
|
|
cfmt(colField, " %s", deco.Value.F(vv.Description))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-12-03 02:06:11 +03:00
|
|
|
case *scalar.S:
|
2021-11-03 19:19:33 +03:00
|
|
|
switch av := vv.Actual.(type) {
|
2022-05-20 16:10:41 +03:00
|
|
|
case map[string]any:
|
2022-06-01 17:55:55 +03:00
|
|
|
cfmt(colField, ": %s", deco.Object.F("{}"))
|
2022-05-20 16:10:41 +03:00
|
|
|
case []any:
|
2022-06-01 17:55:55 +03:00
|
|
|
// TODO: format?
|
|
|
|
cfmt(colField, ": %s%s:%s%s", deco.Index.F("["), deco.Number.F("0"), deco.Number.F(strconv.Itoa(len(av))), deco.Index.F("]"))
|
2020-06-08 03:29:51 +03:00
|
|
|
default:
|
2021-12-08 18:20:37 +03:00
|
|
|
cprint(colField, ":")
|
2021-12-06 00:03:39 +03:00
|
|
|
if vv.Sym == nil {
|
|
|
|
cfmt(colField, " %s", deco.ValueColor(vv.Actual).F(previewValue(vv.Actual, vv.ActualDisplay)))
|
|
|
|
} else {
|
2022-01-24 18:57:35 +03:00
|
|
|
cfmt(colField, " %s", deco.ValueColor(vv.Sym).F(previewValue(vv.Sym, vv.SymDisplay)))
|
2021-12-06 00:03:39 +03:00
|
|
|
cfmt(colField, " (%s)", deco.ValueColor(vv.Actual).F(previewValue(vv.Actual, vv.ActualDisplay)))
|
2021-11-03 19:19:33 +03:00
|
|
|
}
|
2022-06-01 17:55:55 +03:00
|
|
|
}
|
2021-11-03 19:19:33 +03:00
|
|
|
|
2022-06-01 17:55:55 +03:00
|
|
|
if opts.Verbose && isInArray {
|
|
|
|
cfmt(colField, " %s", v.Name)
|
|
|
|
}
|
|
|
|
if vv.Description != "" {
|
|
|
|
cfmt(colField, " (%s)", deco.Value.F(vv.Description))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
default:
|
2021-12-03 02:06:11 +03:00
|
|
|
panic(fmt.Sprintf("unreachable vv %#+v", vv))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-06-01 17:55:55 +03:00
|
|
|
if v.Format != nil {
|
|
|
|
cfmt(colField, " (%s)", deco.Value.F(v.Format.Name))
|
|
|
|
}
|
|
|
|
valueErr = v.Err
|
|
|
|
|
2021-11-18 00:14:49 +03:00
|
|
|
innerRange := v.InnerRange()
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
if opts.Verbose {
|
|
|
|
cfmt(colField, " %s (%s)",
|
2022-06-20 21:27:41 +03:00
|
|
|
mathextra.BitRange(innerRange).StringByteBits(opts.Addrbase), mathextra.Bits(innerRange.Len).StringByteBits(opts.Sizebase))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cprint(colField, "\n")
|
|
|
|
|
2021-11-03 19:19:33 +03:00
|
|
|
if valueErr != nil {
|
2020-06-08 03:29:51 +03:00
|
|
|
var printErrs func(depth int, err error)
|
|
|
|
printErrs = func(depth int, err error) {
|
|
|
|
indent := strings.Repeat(" ", depth)
|
|
|
|
|
|
|
|
var formatErr decode.FormatError
|
2021-09-27 12:01:14 +03:00
|
|
|
var decodeFormatsErr decode.FormatsError
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case errors.As(err, &formatErr):
|
|
|
|
columns()
|
|
|
|
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() {
|
|
|
|
columns()
|
|
|
|
cfmt(colField, "%s %s\n", indent, f.Function)
|
|
|
|
columns()
|
|
|
|
cfmt(colField, "%s %s:%d\n", indent, f.File, f.Line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch {
|
2021-09-27 12:01:14 +03:00
|
|
|
case errors.Is(formatErr.Err, decode.FormatsError{}):
|
2020-06-08 03:29:51 +03:00
|
|
|
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:
|
|
|
|
columns()
|
2021-11-03 19:19:33 +03:00
|
|
|
cfmt(colField, "%s!%s\n", indent, deco.Error.F(err.Error()))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:19:33 +03:00
|
|
|
printErrs(depth, valueErr)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
rootBitLen, err := bitioextra.Len(rootV.RootReader)
|
2022-01-24 23:21:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferLastBit := rootBitLen - 1
|
2021-11-18 00:14:49 +03:00
|
|
|
startBit := innerRange.Start
|
|
|
|
stopBit := innerRange.Stop() - 1
|
|
|
|
sizeBits := innerRange.Len
|
2020-06-08 03:29:51 +03:00
|
|
|
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)
|
|
|
|
|
|
|
|
columns()
|
|
|
|
|
2021-09-06 12:06:06 +03:00
|
|
|
// has length and is not compound or a collapsed struct/array (max depth)
|
2021-11-18 00:14:49 +03:00
|
|
|
if innerRange.Len > 0 && (!isCompound(v) || (opts.Depth != 0 && opts.Depth == depth)) {
|
2020-06-08 03:29:51 +03:00
|
|
|
cfmt(colAddr, "%s%s\n",
|
2022-06-20 21:27:41 +03:00
|
|
|
rootIndent, deco.DumpAddr.F(mathextra.PadFormatInt(startLineByte, opts.Addrbase, true, addrWidth)))
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
vBR, err := bitioextra.Range(rootV.RootReader, startByte*8, displaySizeBits)
|
2020-06-08 03:29:51 +03:00
|
|
|
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)) }
|
|
|
|
|
2022-05-28 14:04:04 +03:00
|
|
|
hexBR, err := bitio.CloneReadSeeker(vBR)
|
2022-01-24 23:21:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := bitioextra.CopyBitsBuffer(
|
|
|
|
hexpairwriter.New(cw.Columns[colHex], opts.LineBytes, int(startLineByteOffset), hexpairFn),
|
|
|
|
hexBR,
|
|
|
|
buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-28 14:04:04 +03:00
|
|
|
asciiBR, err := bitio.CloneReadSeeker(vBR)
|
2022-01-24 23:21:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := bitioextra.CopyBitsBuffer(
|
|
|
|
asciiwriter.New(cw.Columns[colASCII], opts.LineBytes, int(startLineByteOffset), asciiFn),
|
|
|
|
asciiBR,
|
|
|
|
buf); err != nil {
|
|
|
|
return err
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := int64(1); i < addrLines; i++ {
|
|
|
|
lineStartByte := startLineByte + i*int64(opts.LineBytes)
|
|
|
|
columns()
|
2022-06-20 21:27:41 +03:00
|
|
|
cfmt(colAddr, "%s%s\n", rootIndent, deco.DumpAddr.F(mathextra.PadFormatInt(lineStartByte, opts.Addrbase, true, addrWidth)))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
// TODO: correct? should rethink columnwriter api maybe?
|
|
|
|
lastLineStopByte := startLineByte + addrLines*int64(opts.LineBytes) - 1
|
|
|
|
if lastDisplayByte == bufferLastByte && lastDisplayByte != lastLineStopByte {
|
|
|
|
// extra "|" in as EOF markers
|
|
|
|
cfmt(colHex, "%s\n", deco.Column)
|
2021-09-27 12:01:14 +03:00
|
|
|
cfmt(colASCII, "%s\n", deco.Column)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if stopByte != lastDisplayByte {
|
|
|
|
isEnd := ""
|
|
|
|
if stopBit == bufferLastBit {
|
|
|
|
isEnd = " (end)"
|
|
|
|
}
|
|
|
|
columns()
|
|
|
|
|
|
|
|
cfmt(colAddr, "%s%s\n", rootIndent, deco.DumpAddr.F("*"))
|
|
|
|
cprint(colHex, "\n")
|
2021-09-22 01:32:57 +03:00
|
|
|
// TODO: truncate if display_bytes is small?
|
2020-06-08 03:29:51 +03:00
|
|
|
cfmt(colHex, "until %s%s (%s)",
|
2022-06-20 21:27:41 +03:00
|
|
|
mathextra.Bits(stopBit).StringByteBits(opts.Addrbase),
|
2020-06-08 03:29:51 +03:00
|
|
|
isEnd,
|
2022-06-20 21:27:41 +03:00
|
|
|
mathextra.PadFormatInt(bitio.BitsByteCount(sizeBits), opts.Sizebase, true, 0))
|
2020-06-08 03:29:51 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:41:26 +03:00
|
|
|
_ = v.WalkPreOrder(makeWalkFn(func(v *decode.Value, _ *decode.Value, _ int, rootDepth int) error {
|
2022-01-15 20:55:57 +03:00
|
|
|
maxAddrIndentWidth = mathextra.MaxInt(
|
2020-06-08 03:29:51 +03:00
|
|
|
maxAddrIndentWidth,
|
2022-06-20 21:27:41 +03:00
|
|
|
rootDepth+mathextra.DigitsInBase(bitio.BitsByteCount(v.InnerRange().Stop()), true, opts.Addrbase),
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
2022-04-16 12:22:56 +03:00
|
|
|
cw := columnwriter.New(
|
|
|
|
w,
|
|
|
|
[]int{
|
|
|
|
maxAddrIndentWidth,
|
|
|
|
1,
|
|
|
|
opts.LineBytes*3 - 1,
|
|
|
|
1,
|
|
|
|
opts.LineBytes,
|
|
|
|
1,
|
2022-04-19 19:19:40 +03:00
|
|
|
-1,
|
2022-04-16 12:22:56 +03:00
|
|
|
})
|
2021-09-16 13:24:53 +03:00
|
|
|
buf := make([]byte, 32*1024)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-04-16 12:22:56 +03:00
|
|
|
if opts.Color {
|
2022-05-11 01:37:52 +03:00
|
|
|
cw.DisplayLenFn = ansi.Len
|
|
|
|
cw.DisplayTruncateFn = ansi.Truncate
|
2022-04-16 12:22:56 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 13:38:43 +03:00
|
|
|
var hexHeader string
|
|
|
|
var asciiHeader string
|
|
|
|
for i := 0; i < opts.LineBytes; i++ {
|
2022-06-20 21:27:41 +03:00
|
|
|
s := mathextra.PadFormatInt(int64(i), opts.Addrbase, false, 2)
|
2022-05-20 13:38:43 +03:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
return v.WalkPreOrder(makeWalkFn(func(v *decode.Value, rootV *decode.Value, depth int, rootDepth int) error {
|
2022-05-20 13:38:43 +03:00
|
|
|
return dumpEx(v, ctx, depth, rootV, rootDepth, maxAddrIndentWidth-rootDepth)
|
2020-06-08 03:29:51 +03:00
|
|
|
}))
|
|
|
|
}
|
2021-10-05 23:26:05 +03:00
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
func hexdump(w io.Writer, bv Binary, opts Options) error {
|
2022-04-19 16:44:10 +03:00
|
|
|
br, err := bitioextra.Range(bv.br, bv.r.Start, bv.r.Len)
|
2022-01-24 23:21:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-05 23:26:05 +03:00
|
|
|
// TODO: hack
|
|
|
|
opts.Verbose = true
|
|
|
|
return dump(
|
|
|
|
&decode.Value{
|
2021-11-03 19:19:33 +03:00
|
|
|
// TODO: hack
|
2022-01-24 23:21:48 +03:00
|
|
|
V: &scalar.S{Actual: br},
|
2021-10-05 23:26:05 +03:00
|
|
|
Range: bv.r,
|
2022-04-19 16:44:10 +03:00
|
|
|
RootReader: bv.br,
|
2021-10-05 23:26:05 +03:00
|
|
|
},
|
|
|
|
w,
|
|
|
|
opts,
|
|
|
|
)
|
|
|
|
}
|