2024-01-15 23:39:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/jessevdk/go-flags"
|
|
|
|
)
|
|
|
|
|
|
|
|
var opts struct {
|
2024-01-16 00:14:42 +00:00
|
|
|
OffsetFormat string `short:"t" long:"radix" default:"x" choice:"d" choice:"o" choice:"x" description:"Print offset in [d|o|x] format"`
|
2024-02-01 21:24:06 +00:00
|
|
|
Binary bool `short:"b" long:"binary" description:"output in binary format (01010101) incompatible with plain, reverse and include"`
|
2024-01-25 22:38:27 +00:00
|
|
|
Reverse bool `short:"r" long:"reverse" description:"re-assemble hexdump output back into binary"`
|
2024-02-01 21:24:06 +00:00
|
|
|
Autoskip bool `short:"a" long:"autoskip" description:"toggle autoskip (replaces blank lines with a *)"`
|
|
|
|
Bars bool `short:"B" long:"bars" description:"delimiter bars in ascii table"`
|
|
|
|
Seek int64 `short:"s" long:"seek" description:"start at <seek> bytes"`
|
|
|
|
Len int64 `short:"l" long:"len" description:"stop after <len> octets"`
|
|
|
|
Columns int `short:"c" long:"columns" description:"column count"`
|
2024-02-02 00:12:24 +00:00
|
|
|
GroupSize int `short:"g" long:"groups" description:"group size of bytes"`
|
2024-01-25 22:38:27 +00:00
|
|
|
Plain bool `short:"p" long:"plain" description:"plain output without ascii table and offset row [often used with hexxy -r]"`
|
2024-02-01 21:24:06 +00:00
|
|
|
Upper bool `short:"u" long:"upper" description:"output hex in UPPERCASE format"`
|
|
|
|
CInclude bool `short:"i" long:"include" description:"output in C include format"`
|
|
|
|
OutputFile string `short:"o" long:"output" description:"automatically output to file instead of STDOUT"`
|
|
|
|
Separator string `long:"separator" default:"|" description:"separator character for the ascii character table"`
|
2024-01-25 22:38:27 +00:00
|
|
|
ForceColor bool `short:"F" long:"force-color" description:"color is automatically disabled if output is a pipe, this option forces color output"`
|
2024-02-01 21:24:06 +00:00
|
|
|
NoColor bool `short:"N" long:"no-color" description:"do not print output with color"`
|
2024-01-16 00:14:42 +00:00
|
|
|
Verbose bool `short:"v" long:"verbose" description:"print debugging information and verbose output"`
|
2024-01-15 23:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var Debug = func(string, ...interface{}) {}
|
2024-02-01 21:24:06 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
dumpHex = iota
|
|
|
|
dumpBinary
|
|
|
|
dumpCformat
|
|
|
|
dumpPlain
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ldigits = "0123456789abcdef"
|
|
|
|
udigits = "0123456789ABCDEF"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dumpType int
|
|
|
|
space = []byte(" ")
|
|
|
|
doubleSpace = []byte(" ")
|
|
|
|
dot = []byte(".")
|
|
|
|
newLine = []byte("\n")
|
|
|
|
zeroHeader = []byte("0000000: ")
|
|
|
|
unsignedChar = []byte("unsigned char ")
|
|
|
|
unsignedInt = []byte("};\nunsigned int ")
|
|
|
|
lenEquals = []byte("_len = ")
|
|
|
|
brackets = []byte("[] = {")
|
|
|
|
asterisk = []byte("*")
|
|
|
|
commaSpace = []byte(", ")
|
|
|
|
comma = []byte(",")
|
|
|
|
semiColonNl = []byte(";\n")
|
|
|
|
bar = []byte("|")
|
|
|
|
)
|
|
|
|
|
|
|
|
func inputIsPipe() bool {
|
2024-01-15 23:39:38 +00:00
|
|
|
stat, _ := os.Stdin.Stat()
|
2024-02-01 21:24:06 +00:00
|
|
|
return stat.Mode()&os.ModeCharDevice != os.ModeCharDevice
|
2024-01-16 00:14:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
func outputIsPipe() bool {
|
|
|
|
stat, _ := os.Stdout.Stat()
|
|
|
|
return stat.Mode()&os.ModeCharDevice != os.ModeCharDevice
|
2024-01-16 00:14:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 00:12:24 +00:00
|
|
|
func XXD(r io.Reader, w io.Writer, filename string, color *Color) error {
|
2024-02-01 21:24:06 +00:00
|
|
|
var (
|
|
|
|
lineOffset int64
|
|
|
|
hexOffset = make([]byte, 6)
|
|
|
|
groupSize int
|
|
|
|
cols int
|
|
|
|
octs int
|
|
|
|
caps = ldigits
|
|
|
|
doCheader = true
|
|
|
|
doCEnd bool
|
|
|
|
varDeclChar = make([]byte, 14+len(filename)+6) // for "unsigned char NAME_FORMAT[] = {"
|
|
|
|
varDeclInt = make([]byte, 16+len(filename)+7) // enough room for "unsigned int NAME_FORMAT = "
|
|
|
|
nulLine int64
|
|
|
|
totalOcts int64
|
|
|
|
)
|
|
|
|
|
|
|
|
if dumpType == dumpCformat {
|
|
|
|
_ = copy(varDeclChar[0:14], unsignedChar[:])
|
2024-02-02 00:12:24 +00:00
|
|
|
_ = copy(varDeclInt[0:16], unsignedInt[:])
|
2024-02-01 21:24:06 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(filename); i++ {
|
2024-02-02 00:12:24 +00:00
|
|
|
if !isSpecial(filename[i]) {
|
2024-02-01 21:24:06 +00:00
|
|
|
varDeclChar[14+i] = filename[i]
|
|
|
|
varDeclInt[16+i] = filename[i]
|
|
|
|
} else {
|
|
|
|
varDeclChar[14+i] = '_'
|
|
|
|
varDeclInt[16+i] = '_'
|
|
|
|
}
|
2024-01-15 23:39:38 +00:00
|
|
|
}
|
2024-02-01 21:24:06 +00:00
|
|
|
// copy "[] = {" and "_len = "
|
|
|
|
_ = copy(varDeclChar[14+len(filename):], brackets[:])
|
|
|
|
_ = copy(varDeclInt[16+len(filename):], lenEquals[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Upper {
|
|
|
|
caps = udigits
|
|
|
|
}
|
2024-01-15 23:39:38 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if opts.Columns == -1 {
|
|
|
|
switch dumpType {
|
|
|
|
case dumpPlain:
|
|
|
|
cols = 30
|
|
|
|
case dumpCformat:
|
|
|
|
cols = 12
|
|
|
|
case dumpBinary:
|
|
|
|
cols = 6
|
|
|
|
default:
|
|
|
|
cols = 16
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cols = opts.Columns
|
|
|
|
}
|
2024-01-16 00:14:42 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
switch dumpType {
|
|
|
|
case dumpBinary:
|
|
|
|
octs = 8
|
|
|
|
groupSize = 1
|
|
|
|
case dumpPlain:
|
|
|
|
octs = 0
|
|
|
|
case dumpCformat:
|
|
|
|
octs = 4
|
|
|
|
default:
|
|
|
|
octs = 2
|
|
|
|
groupSize = 2
|
|
|
|
}
|
2024-01-16 00:14:42 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if opts.GroupSize != -1 {
|
|
|
|
groupSize = opts.GroupSize
|
|
|
|
}
|
2024-01-16 00:14:42 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if opts.Len != -1 {
|
|
|
|
if opts.Len < int64(cols) {
|
|
|
|
cols = int(opts.Len)
|
2024-01-15 23:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if octs < 1 {
|
|
|
|
octs = cols
|
|
|
|
}
|
2024-01-16 00:14:42 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
// allocate their size based on the users specs, hence why its declared here
|
|
|
|
var (
|
|
|
|
line = make([]byte, cols)
|
|
|
|
char = make([]byte, octs)
|
|
|
|
)
|
2024-01-16 00:14:42 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
c := int64(0)
|
|
|
|
nl := int64(0)
|
|
|
|
r = bufio.NewReader(r)
|
2024-01-16 00:14:42 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
var (
|
|
|
|
n int
|
|
|
|
err error
|
|
|
|
)
|
2024-01-15 23:39:38 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
for {
|
|
|
|
n, err = io.ReadFull(r, line)
|
2024-02-02 00:12:24 +00:00
|
|
|
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
|
2024-02-01 21:24:06 +00:00
|
|
|
return fmt.Errorf("hexxy: %v", err)
|
|
|
|
}
|
2024-01-15 23:39:38 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if dumpType == dumpPlain && n != 0 {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
hexEncode(char, line[i:i+1], caps)
|
|
|
|
w.Write(char)
|
|
|
|
c++
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if n == 0 {
|
|
|
|
if dumpType == dumpPlain {
|
|
|
|
w.Write(newLine)
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if dumpType == dumpCformat {
|
|
|
|
doCEnd = true
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if opts.Len != -1 {
|
|
|
|
if totalOcts == opts.Len {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
totalOcts += opts.Len
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if opts.Autoskip && empty(&line) {
|
|
|
|
if nulLine == 1 {
|
|
|
|
w.Write(asterisk)
|
|
|
|
w.Write(newLine)
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
nulLine++
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if nulLine > 1 {
|
2024-02-02 00:12:24 +00:00
|
|
|
lineOffset++ // still increment offset while printing crunched lines with '*'
|
2024-02-01 21:24:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
// hex or binary formats only
|
|
|
|
if dumpType <= dumpBinary {
|
|
|
|
// line offset
|
|
|
|
hexOffset = strconv.AppendInt(hexOffset[0:0], lineOffset, 16)
|
|
|
|
w.Write(zeroHeader[0:(6 - len(hexOffset))])
|
|
|
|
w.Write(hexOffset)
|
|
|
|
w.Write(zeroHeader[6:])
|
|
|
|
lineOffset++
|
|
|
|
} else if doCheader {
|
|
|
|
w.Write(varDeclChar)
|
|
|
|
w.Write(newLine)
|
|
|
|
doCheader = false
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if dumpType == dumpBinary {
|
|
|
|
// binary values
|
|
|
|
for i, k := 0, octs; i < n; i, k = i+1, k+octs {
|
|
|
|
binaryEncode(char, line[i:i+1])
|
|
|
|
w.Write(char)
|
|
|
|
c++
|
|
|
|
|
|
|
|
if k == octs*groupSize {
|
|
|
|
k = 0
|
|
|
|
w.Write(space)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if dumpType == dumpCformat {
|
|
|
|
if !doCEnd {
|
|
|
|
w.Write(doubleSpace)
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
cfmtEncode(char, line[i:i+1], caps)
|
|
|
|
w.Write(char)
|
|
|
|
c++
|
|
|
|
// no space at EOL
|
|
|
|
if i != n-1 {
|
|
|
|
w.Write(commaSpace)
|
|
|
|
} else if n == cols {
|
|
|
|
w.Write(comma)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// hex values -- default
|
|
|
|
for i, k := 0, octs; i < n; i, k = i+1, k+octs {
|
|
|
|
hexEncode(char, line[i:i+1], caps)
|
2024-02-02 00:12:24 +00:00
|
|
|
|
|
|
|
// s := color.Colorize(string(char), byte(i))
|
|
|
|
// w.Write([]byte(s))
|
2024-02-01 21:24:06 +00:00
|
|
|
w.Write(char)
|
|
|
|
c++
|
|
|
|
|
|
|
|
if k == octs*groupSize {
|
|
|
|
k = 0
|
|
|
|
w.Write(space)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if doCEnd {
|
|
|
|
w.Write(varDeclInt)
|
|
|
|
w.Write([]byte(strconv.FormatInt(c, 10)))
|
|
|
|
w.Write(semiColonNl)
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if n < len(line) && dumpType <= dumpBinary {
|
|
|
|
for i := n * octs; i < len(line)*octs; i++ {
|
|
|
|
w.Write(space)
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if i%octs == 1 {
|
|
|
|
w.Write(space)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 00:14:42 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if dumpType != dumpCformat {
|
|
|
|
w.Write(space)
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if dumpType <= dumpBinary {
|
|
|
|
// character values
|
|
|
|
b := line[:n]
|
|
|
|
// |hello,.world!|
|
|
|
|
if opts.Bars {
|
|
|
|
w.Write(bar)
|
|
|
|
}
|
2024-01-16 00:14:42 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
var v byte
|
|
|
|
for i := 0; i < len(b); i++ {
|
|
|
|
v = b[i]
|
|
|
|
if v > 0x1f && v < 0x7f {
|
|
|
|
w.Write(line[i : i+1])
|
|
|
|
} else {
|
|
|
|
w.Write(dot)
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 00:14:42 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if opts.Bars {
|
|
|
|
w.Write(bar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write(newLine)
|
|
|
|
nl++
|
2024-01-16 00:14:42 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 23:39:38 +00:00
|
|
|
func Hexxy(args []string) error {
|
|
|
|
color := &Color{}
|
|
|
|
|
|
|
|
if opts.NoColor {
|
|
|
|
color.disable = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !color.disable {
|
|
|
|
color.Compute()
|
|
|
|
}
|
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
var infile, outfile *os.File
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(args) < 1 && inputIsPipe() {
|
|
|
|
infile = os.Stdin
|
|
|
|
} else {
|
|
|
|
infile, err = os.Open(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("hexxy: %v", err.Error())
|
2024-01-25 22:38:27 +00:00
|
|
|
}
|
2024-01-15 23:39:38 +00:00
|
|
|
}
|
2024-02-01 21:24:06 +00:00
|
|
|
defer infile.Close()
|
2024-01-15 23:39:38 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if opts.Seek != -1 {
|
|
|
|
_, err = infile.Seek(opts.Seek, io.SeekStart)
|
2024-01-15 23:39:38 +00:00
|
|
|
if err != nil {
|
2024-02-01 21:24:06 +00:00
|
|
|
return fmt.Errorf("hexxy: %v", err.Error())
|
2024-01-15 23:39:38 +00:00
|
|
|
}
|
2024-02-01 21:24:06 +00:00
|
|
|
}
|
2024-01-15 23:39:38 +00:00
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if opts.OutputFile != "" {
|
|
|
|
outfile, err = os.Open(opts.OutputFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("hexxy: %v", err.Error())
|
2024-01-15 23:42:30 +00:00
|
|
|
}
|
2024-02-01 21:24:06 +00:00
|
|
|
} else {
|
|
|
|
outfile = os.Stdout
|
|
|
|
}
|
|
|
|
defer outfile.Close()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case opts.Binary:
|
|
|
|
dumpType = dumpBinary
|
|
|
|
case opts.CInclude:
|
|
|
|
dumpType = dumpCformat
|
|
|
|
case opts.Plain:
|
|
|
|
dumpType = dumpPlain
|
|
|
|
default:
|
|
|
|
dumpType = dumpHex
|
|
|
|
}
|
|
|
|
|
|
|
|
out := bufio.NewWriter(outfile)
|
|
|
|
defer out.Flush()
|
|
|
|
|
|
|
|
if opts.Reverse {
|
2024-02-02 00:12:24 +00:00
|
|
|
if err := XXDReverse(infile, outfile); err != nil {
|
|
|
|
return fmt.Errorf("hexxy: %v", err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
2024-02-01 21:24:06 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 00:12:24 +00:00
|
|
|
if err := XXD(infile, out, infile.Name(), color); err != nil {
|
2024-02-01 21:24:06 +00:00
|
|
|
return fmt.Errorf("hexxy: %v", err.Error())
|
2024-01-15 23:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
const usage_msg = `
|
|
|
|
hexxy is a command line hex dumping tool
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
hexxy [OPTIONS] input-file
|
2024-02-02 00:12:24 +00:00
|
|
|
|
|
|
|
# Include a binary as a C variable
|
|
|
|
hexxy -i input-file > output.c
|
|
|
|
|
|
|
|
# Use plain non-formatted output
|
|
|
|
hexxy -p input-file
|
|
|
|
|
|
|
|
# Reverse plain non-formatted output (reverse plain)
|
|
|
|
hexxy -rp input-file
|
|
|
|
|
|
|
|
# Show output with a space in between N groups of bytes
|
|
|
|
hexxy -g1 input-file ... -> outputs: 00000000: 0f 1a ff ff 00 aa
|
|
|
|
|
|
|
|
# Seek to N bytes in an input file
|
|
|
|
hexxy -s 12546 input-file
|
2024-02-01 21:24:06 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
// extra usage examples
|
|
|
|
func usage() {
|
|
|
|
fmt.Fprint(os.Stderr, usage_msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
opts.Seek = -1 // default no-op value
|
|
|
|
opts.Columns = -1
|
|
|
|
opts.GroupSize = -1
|
|
|
|
opts.Len = -1
|
|
|
|
}
|
|
|
|
|
2024-01-15 23:39:38 +00:00
|
|
|
func main() {
|
2024-02-01 21:24:06 +00:00
|
|
|
parser := flags.NewParser(&opts, flags.Default)
|
|
|
|
args, err := parser.Parse()
|
2024-01-15 23:39:38 +00:00
|
|
|
if flags.WroteHelp(err) {
|
2024-02-02 00:12:24 +00:00
|
|
|
fmt.Print(usage_msg)
|
2024-01-15 23:39:38 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if !inputIsPipe() && len(args) == 0 {
|
|
|
|
parser.WriteHelp(os.Stderr)
|
2024-02-02 00:12:24 +00:00
|
|
|
fmt.Print(usage_msg)
|
2024-01-25 22:38:27 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-02-01 21:24:06 +00:00
|
|
|
if opts.Verbose {
|
|
|
|
Debug = log.Printf
|
2024-01-16 00:14:42 +00:00
|
|
|
}
|
2024-02-02 00:12:24 +00:00
|
|
|
|
2024-01-15 23:39:38 +00:00
|
|
|
if err := Hexxy(args); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|