2024-01-15 23:39:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jessevdk/go-flags"
|
|
|
|
)
|
|
|
|
|
|
|
|
var opts struct {
|
2024-01-16 00:14:42 +00:00
|
|
|
NoColor bool `short:"N" long:"no-color" description:"do not print output with color"`
|
|
|
|
OffsetFormat string `short:"t" long:"radix" default:"x" choice:"d" choice:"o" choice:"x" description:"Print offset in [d|o|x] format"`
|
|
|
|
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-01-16 00:14:42 +00:00
|
|
|
var OffsetFormat string
|
|
|
|
var Separator string
|
|
|
|
|
|
|
|
const GREY = "\x1b[38;2;111;111;111m"
|
|
|
|
const CLR = "\x1b[0m"
|
2024-01-15 23:39:38 +00:00
|
|
|
|
|
|
|
type Color struct {
|
|
|
|
disable bool
|
|
|
|
values [256]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Color) Compute() {
|
|
|
|
const WHITEB = "\x1b[1;37m"
|
|
|
|
for i := 0; i < 256; i++ {
|
|
|
|
var fg, bg string
|
|
|
|
|
|
|
|
lowVis := i == 0 || (i >= 16 && i <= 20) || (i >= 232 && i <= 242)
|
|
|
|
|
|
|
|
if lowVis {
|
|
|
|
fg = WHITEB + "\x1b[38;5;" + "255" + "m"
|
|
|
|
bg = "\x1b[48;5;" + strconv.Itoa(int(i)) + "m"
|
|
|
|
} else {
|
|
|
|
fg = "\x1b[38;5;" + strconv.Itoa(int(i)) + "m"
|
|
|
|
bg = ""
|
|
|
|
}
|
|
|
|
c.values[i] = bg + fg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Color) Colorize(s string, clr byte) string {
|
|
|
|
const NOCOLOR = "\x1b[0m"
|
|
|
|
return c.values[clr] + s + NOCOLOR
|
|
|
|
}
|
|
|
|
|
|
|
|
func stdinOpen() bool {
|
|
|
|
stat, _ := os.Stdin.Stat()
|
|
|
|
if stat.Mode()&os.ModeCharDevice == os.ModeCharDevice {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func asciiRow(ascii []byte, clr *Color, stdout io.Writer) {
|
|
|
|
var s string
|
|
|
|
for _, b := range ascii {
|
|
|
|
if b >= 33 && b <= 126 {
|
|
|
|
s = clr.Colorize(string(b), b)
|
|
|
|
} else {
|
|
|
|
s = clr.Colorize(".", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(stdout, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 00:14:42 +00:00
|
|
|
func printOffset(offset uint64) string {
|
|
|
|
return fmt.Sprintf(OffsetFormat, offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printSeparator(writer io.Writer, newline bool) {
|
|
|
|
if newline {
|
|
|
|
fmt.Fprintln(writer, Separator)
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(writer, Separator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 23:39:38 +00:00
|
|
|
func Hexdump(file *os.File, color *Color) error {
|
|
|
|
stdout := bufio.NewWriter(os.Stdout)
|
|
|
|
stderr := os.Stderr
|
|
|
|
ascii := [16]byte{}
|
2024-01-16 00:14:42 +00:00
|
|
|
defer stdout.Flush()
|
2024-01-15 23:39:38 +00:00
|
|
|
|
|
|
|
var i uint64 = 0
|
|
|
|
reader := bufio.NewReaderSize(file, 10*1024*1024)
|
|
|
|
|
|
|
|
for {
|
|
|
|
b, err := reader.ReadByte()
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(stderr, "Failed to read %v: %v\n", file.Name(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ascii[i%16] = b
|
|
|
|
|
|
|
|
// offset
|
|
|
|
if i%16 == 0 {
|
2024-01-16 00:14:42 +00:00
|
|
|
// fmt.Fprintf(stdout, "%08x ", i)
|
|
|
|
offy := printOffset(i)
|
|
|
|
fmt.Fprint(stdout, offy)
|
2024-01-15 23:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// byte
|
|
|
|
fmt.Fprintf(stdout, color.Colorize("%02x", b)+" ", b)
|
|
|
|
|
|
|
|
// extra space every 4 bytes
|
|
|
|
if (i+1)%4 == 0 {
|
|
|
|
fmt.Fprint(stdout, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// print ascii row and newline │ | ┆
|
|
|
|
if (i+1)%16 == 0 {
|
2024-01-16 00:14:42 +00:00
|
|
|
// fmt.Fprint(stdout, "│")
|
|
|
|
printSeparator(stdout, false)
|
|
|
|
|
2024-01-15 23:39:38 +00:00
|
|
|
asciiRow(ascii[:i%16], color, stdout)
|
2024-01-16 00:14:42 +00:00
|
|
|
|
|
|
|
// fmt.Fprintln(stdout, "│")
|
|
|
|
printSeparator(stdout, true)
|
|
|
|
|
2024-01-15 23:39:38 +00:00
|
|
|
ascii = [16]byte{} // reset
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if i%16 != 0 {
|
|
|
|
left := int(16 - i%16)
|
|
|
|
spaces := 3*left + (left-1)/4 + 1
|
2024-01-16 00:14:42 +00:00
|
|
|
|
2024-01-15 23:39:38 +00:00
|
|
|
fmt.Fprint(stdout, strings.Repeat(" ", spaces))
|
2024-01-16 00:14:42 +00:00
|
|
|
printSeparator(stdout, false)
|
|
|
|
|
2024-01-15 23:39:38 +00:00
|
|
|
asciiRow(ascii[:i%16], color, stdout)
|
2024-01-16 00:14:42 +00:00
|
|
|
printSeparator(stdout, true)
|
|
|
|
|
|
|
|
offy := printOffset(i)
|
|
|
|
fmt.Fprintln(stdout, offy)
|
|
|
|
// fmt.Fprintf(stdout, "%08x\n", i)
|
2024-01-15 23:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-16 00:14:42 +00:00
|
|
|
func getOffsetFormat() error {
|
|
|
|
var prefix string
|
|
|
|
var suffix string
|
|
|
|
|
|
|
|
if !opts.NoColor {
|
|
|
|
prefix = GREY
|
|
|
|
suffix = CLR
|
|
|
|
} else {
|
|
|
|
prefix = ""
|
|
|
|
suffix = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
Separator = prefix + "│" + suffix
|
|
|
|
|
|
|
|
switch opts.OffsetFormat {
|
|
|
|
case "d":
|
|
|
|
OffsetFormat = prefix + "%08d " + suffix
|
|
|
|
case "o":
|
|
|
|
OffsetFormat = prefix + "%08o " + suffix
|
|
|
|
case "x":
|
|
|
|
OffsetFormat = prefix + "%08x " + suffix
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Offset format must be [d|o|x]")
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) < 1 && stdinOpen() {
|
|
|
|
return Hexdump(os.Stdin, color)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range args {
|
|
|
|
file, err := os.Open(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-16 00:14:42 +00:00
|
|
|
defer file.Close()
|
2024-01-15 23:39:38 +00:00
|
|
|
|
2024-01-15 23:42:30 +00:00
|
|
|
if err := Hexdump(file, color); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-15 23:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
args, err := flags.Parse(&opts)
|
|
|
|
if flags.WroteHelp(err) {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Verbose {
|
|
|
|
Debug = log.Printf
|
|
|
|
}
|
|
|
|
|
2024-01-16 00:14:42 +00:00
|
|
|
err = getOffsetFormat()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-01-15 23:39:38 +00:00
|
|
|
if err := Hexxy(args); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|