hexdump

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

README

hexdump build Document License Release

Dump binary content as an ASCII table in Golang.

Install

go get github.com/flier/hexdump

Examples

Dump string, slices, stream, any value or pointer to value.

String
hexdump.String("Hello, World!")
// Output:
// 00000000  48 65 6c 6c 6f 2c 20 57  6f 72 6c 64 21           |Hello, World!   |
Slices
hexdump.Slices([]uint16{0x0123, 0x4567, 0x89AB, 0xCDEF}, hexdump.TwoBytesHex, hexdump.LittleEndian)
// Output:
// 00000000     0123    4567    89ab    cdef                                  |#.gE....        |
Stream
b := fnv.New128().Sum([]byte("Hello, World!"))

hexdump.Stream(bytes.NewBuffer(b))
// Output:
// 00000000  48 65 6c 6c 6f 2c 20 57  6f 72 6c 64 21 6c 62 27  |Hello, World!lb'|
// 00000010  2e 07 bb 01 42 62 b8 21  75 62 95 c5 8d           |....Bb.!ub...   |
Value
hexdump.Value(complex128(3.14))
// Output:
// 00000000  1f 85 eb 51 b8 1e 09 40  00 00 00 00 00 00 00 00  |...Q...@........|
Structure
type UDP struct {
    SrcPort uint16
    DstPort uint16
    Length  uint16
    Chksum  uint16
}

htons := func(v uint16) uint16 {
    var b [2]byte

    binary.NativeEndian.PutUint16(b[:], v)

    return binary.BigEndian.Uint16(b[:])
}

udp := &UDP{
    SrcPort: htons(0x1234),
    DstPort: htons(0x5678),
    Length:  htons(0x9abc),
    Chksum:  htons(0xdef0),
}

hexdump.Deref(udp)
// Output:
// 00000000  12 34 56 78 9a bc de f0                           |.4Vx....        |

License

Apache License 2.0, see LICENSE for more details.

Documentation

Overview

Display binary contents in hexadecimal, decimal, octal, or ascii.

Index

Examples

Constants

View Source
const DefaultLineWidth = 16

Variables

View Source
var (
	Stdout = Output(os.Stdout) // Output to stdout.
	Stderr = Output(os.Stderr) // Output to stderr.

	AutoColor   = Color(ColorAuto)   // Auto color mode.
	AlwaysColor = Color(ColorAlways) // Always color mode.
	NeverColor  = Color(ColorNever)  // Never color mode.

	Canonical     = Style(StyleCanonical)     // Canonical hex+ASCII display.
	OneByteChar   = Style(StyleOneByteChar)   // One-byte character display.
	OneByteHex    = Style(StyleOneByteHex)    // One-byte hex display.
	OneByteOctal  = Style(StyleOneByteOctal)  // One-byte octal display.
	TwoBytesDec   = Style(StyleTwoBytesDec)   // Two-byte decimal display.
	TwoBytesHex   = Style(StyleTwoBytesHex)   // Two-byte hexadecimal display
	TwoBytesOctal = Style(StyleTwoBytesOctal) // Wwo-byte octal display

	LittleEndian = ByteOrder(binary.LittleEndian) // Little-endian byte order.
	BigEndian    = ByteOrder(binary.BigEndian)    // Big-endian byte order.
	NativeEndian = ByteOrder(binary.NativeEndian) // Native-endian byte order.
)
View Source
var DefaultTheme = ColorTheme{
	Offset:  color.New(color.Faint),
	Content: color.New(color.Reset),
	Chars:   color.New(color.Italic),
}

Functions

func AsDeref

func AsDeref[T, S any](p *S, x ...Option) (err error)

AsDeref converts the contents of the value pointed to by 'p' into a readable ASCII table using the options provided.

Example
var b [8]byte

binary.BigEndian.PutUint64(b[:], 0x123456789abcdef)

_ = hexdump.AsDeref[uint64](&b[0])
Output:

00000000  01 23 45 67 89 ab cd ef                           |.#Eg....        |

func Bytes

func Bytes(b []byte, x ...Option) (err error)

Bytes converts a byte slice into a readable ASCII table using the provided options. It writes the output to the specified io.Writer (default is os.Stdout).

The function accepts a byte slice 'b' and a variable number of options 'x'. The options can be used to customize the behavior of the dumper.

The function returns an error if any error occurs during the dumping process.

Example
_ = hexdump.Bytes(fnv.New128().Sum([]byte("Hello, World!")))
Output:

00000000  48 65 6c 6c 6f 2c 20 57  6f 72 6c 64 21 6c 62 27  |Hello, World!lb'|
00000010  2e 07 bb 01 42 62 b8 21  75 62 95 c5 8d           |....Bb.!ub...   |

func Deref

func Deref[T any](p *T, x ...Option) (err error)

Deref converts the contents of the value pointed to by 'p' into a readable ASCII table using the options provided.

Example
type UDP struct {
	SrcPort uint16
	DstPort uint16
	Length  uint16
	Chksum  uint16
}

htons := func(v uint16) uint16 {
	var b [2]byte

	binary.NativeEndian.PutUint16(b[:], v)

	return binary.BigEndian.Uint16(b[:])
}

udp := &UDP{
	SrcPort: htons(0x1234),
	DstPort: htons(0x5678),
	Length:  htons(0x9abc),
	Chksum:  htons(0xdef0),
}

_ = hexdump.Deref(udp)
Output:

00000000  12 34 56 78 9a bc de f0                           |.4Vx....        |

func Seq

func Seq[T any](i iter.Seq[T], x ...Option) (err error)

Seq reads value from iter.Seq and converts it as binary content into a readable ASCII table. The function writes the output to the specified io.Writer (default is os.Stdout).

The function accepts an iterator 'i' and a variable number of options 'x'. The options can be used to customize the behavior of the dumper.

The function returns an error if any error occurs during the dumping process.

func Slices

func Slices[T any](s []T, x ...Option) error

Slices converts a slice of T into a readable ASCII table using the provided options.

Example
_ = hexdump.Slices([]uint16{0x0123, 0x4567, 0x89AB, 0xCDEF}, hexdump.TwoBytesHex, hexdump.LittleEndian)
Output:

00000000     0123    4567    89ab    cdef                                  |#.gE....        |

func Stream

func Stream(r io.Reader, x ...Option) (err error)

Stream reads from the provided io.Reader and converts the binary content into a readable ASCII table. The function writes the output to the specified io.Writer (default is os.Stdout).

The function accepts a byte stream 'r' and a variable number of options 'x'. The options can be used to customize the behavior of the dumper.

The function returns an error if any error occurs during the dumping process.

Example
b := fnv.New128().Sum([]byte("Hello, World!"))

_ = hexdump.Stream(bytes.NewBuffer(b))
Output:

00000000  48 65 6c 6c 6f 2c 20 57  6f 72 6c 64 21 6c 62 27  |Hello, World!lb'|
00000010  2e 07 bb 01 42 62 b8 21  75 62 95 c5 8d           |....Bb.!ub...   |

func String

func String(s string, x ...Option) error

String converts a string into a readable ASCII table using the provided options.

Example
_ = hexdump.String("Hello, World!")
Output:

00000000  48 65 6c 6c 6f 2c 20 57  6f 72 6c 64 21           |Hello, World!   |

func Value

func Value[T any](v T, x ...Option) (err error)

Value converts the contents of the value 'v' into a readable ASCII table using the options provided.

Example
_ = hexdump.Value(complex128(3.14))
Output:

00000000  1f 85 eb 51 b8 1e 09 40  00 00 00 00 00 00 00 00  |...Q...@........|

Types

type ColorMode

type ColorMode int //nolint:recvcheck
const (
	ColorAuto   ColorMode = iota // auto
	ColorAlways                  // always
	ColorNever                   // never
)

func (ColorMode) MarshalText

func (m ColorMode) MarshalText() ([]byte, error)

func (ColorMode) String

func (i ColorMode) String() string

func (*ColorMode) UnmarshalText

func (m *ColorMode) UnmarshalText(text []byte) (err error)

type ColorTheme

type ColorTheme struct {
	Offset  *color.Color
	Content *color.Color
	Chars   *color.Color
}

type DisplayStyle

type DisplayStyle int

The display style of binary content.

const (
	StyleCanonical     DisplayStyle = iota // Canonical hex+ASCII display.
	StyleOneByteChar                       // One-byte character display.
	StyleOneByteHex                        // One-byte hex display.
	StyleOneByteOctal                      // One-byte octal display.
	StyleTwoBytesDec                       // Two-byte decimal display.
	StyleTwoBytesHex                       // Two-byte hexadecimal display
	StyleTwoBytesOctal                     // Wwo-byte octal display
)

type Dumper

type Dumper struct {

	// The output stream, the default is [os.Stdout].
	Output io.Writer

	// The number of bytes per line, the default is 16.
	LineWidth int

	// The color mode of the line, the default is [ColorAuto].
	Color ColorMode

	// The color theme of the line, the default is [DefaultTheme].
	Theme *ColorTheme

	// The display style of the line, the default is [StyleCanonical].
	Style DisplayStyle

	// The byte order used to read the data group, the default is [binary.NativeEndian].
	ByteOrder binary.ByteOrder

	// The start offset of the binary content.
	Start int64

	// Skip offset bytes from the beginning of the input.
	Skip int64

	// Interpret only length bytes of input.
	Length int64
	// contains filtered or unexported fields
}

Dumper converts the binary content into a readable ASCII table.

func New

func New(x ...Option) (d *Dumper)

New returns a new Dumper with the provided options.

func (*Dumper) Flush

func (d *Dumper) Flush() (err error)

Flush dump any buffered data to the underlying io.Writer.

func (*Dumper) Write

func (d *Dumper) Write(p []byte) (nn int, err error)

Write writes the contents of p into the buffer.

It returns the number of bytes written. If nn < len(p), it also returns an error explaining why the write is short.

func (*Dumper) WriteByte

func (d *Dumper) WriteByte(c byte) (err error)

WriteByte writes a single byte.

func (*Dumper) WriteRune

func (d *Dumper) WriteRune(r rune) (size int, err error)

WriteRune writes a single Unicode code point, returning the number of bytes written and any error.

func (*Dumper) WriteString

func (d *Dumper) WriteString(s string) (count int, err error)

WriteString writes a string.

It returns the number of bytes written. If the count is less than len(s), it also returns an error explaining why the write is short.

type Formatter

type Formatter struct {
	*bufio.Writer
	*ColorTheme
	DisplayStyle
	binary.ByteOrder
	LineWidth int
}

func (*Formatter) FormatLine

func (f *Formatter) FormatLine(off int64, skip int, buf []byte) (err error)

type Option

type Option func(*Dumper)

Option can be used to customize the behavior of the Dumper.

func ByteOrder

func ByteOrder(b binary.ByteOrder) Option

The byte order used to read the data group, the default is binary.NativeEndian.

func Color

func Color(c ColorMode) Option

The color mode of the line, the default is ColorAuto.

func Length

func Length(n int64) Option

Interpret only length bytes of input.

Example
_ = hexdump.String("Hello, World!", hexdump.Length(5))
Output:

00000000  48 65 6c 6c 6f                                    |Hello           |

func LineWidth

func LineWidth(n int) Option

The number of bytes per line, the default is 16.

Example
_ = hexdump.String("Hello, World!", hexdump.LineWidth(8))
Output:

00000000  48 65 6c 6c 6f 2c 20 57  |Hello, W|
00000008  6f 72 6c 64 21           |orld!   |

func Output

func Output(w io.Writer) Option

The output stream, the default is os.Stdout.

Example
var b strings.Builder

_ = hexdump.String("Hello, World!", hexdump.Output(&b))

fmt.Print(b.String())
Output:

00000000  48 65 6c 6c 6f 2c 20 57  6f 72 6c 64 21           |Hello, World!   |

func Range

func Range(start, end int64) Option

Extract the range of input from start to end.

Example
_ = hexdump.String("Hello, World!", hexdump.Range(7, 12))
Output:

00000000                       57  6f 72 6c 64              |       World    |

func Skip

func Skip(n int64) Option

Skip offset bytes from the beginning of the input.

Example
_ = hexdump.String("Hello, World!", hexdump.Skip(7))
Output:

00000000                       57  6f 72 6c 64 21           |       World!   |
Example (Length)
_ = hexdump.String("Hello, World!", hexdump.Skip(7), hexdump.Length(5))
Output:

00000000                       57  6f 72 6c 64              |       World    |

func Start

func Start(off int64) Option

The start offset of the binary content.

Example
_ = hexdump.String("Hello, World!", hexdump.Start(0x1004))
Output:

00001000              48 65 6c 6c  6f 2c 20 57 6f 72 6c 64  |    Hello, World|
00001010  21                                                |!               |

func Style

func Style(s DisplayStyle) Option

The display style of the line, the default is StyleCanonical.

func Theme

func Theme(t *ColorTheme) Option

The color theme of the line, the default is DefaultTheme.

Directories

Path Synopsis
cmd
xd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL