-
Notifications
You must be signed in to change notification settings - Fork 2
/
hexxy.go
543 lines (465 loc) · 11.2 KB
/
hexxy.go
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package main
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"github.com/jessevdk/go-flags"
)
var opts struct {
OffsetFormat string `short:"t" long:"radix" default:"x" choice:"d" choice:"o" choice:"x" description:"Print offset in [d|o|x] format"`
Binary bool `short:"b" long:"binary" description:"output in binary format (01010101) incompatible with plain, reverse and include"`
Reverse bool `short:"r" long:"reverse" description:"re-assemble hexdump output back into binary"`
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"`
GroupSize int `short:"g" long:"groups" description:"group size of bytes"`
Plain bool `short:"p" long:"plain" description:"plain output without ascii table and offset row [often used with hexxy -r]"`
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"`
HasColor string `short:"C" long:"color" default:"auto" choice:"always" choice:"auto" choice:"never" description:"this option forces color output [always|auto|never]"`
NoColor bool `short:"n" long:"no-color" description:"do not print output with color"`
Verbose bool `short:"v" long:"verbose" description:"print debugging information and verbose output"`
}
var Debug = func(string, ...interface{}) {}
const (
dumpHex = iota
dumpBinary
dumpCformat
dumpPlain
)
const (
udigits = "0123456789ABCDEF"
ldigits = "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("|")
)
var (
USE_COLOR bool
)
func inputIsPipe() bool {
stat, _ := os.Stdin.Stat()
return stat.Mode()&os.ModeCharDevice != os.ModeCharDevice
}
func outputIsPipe() bool {
stat, _ := os.Stdout.Stat()
return stat.Mode()&os.ModeCharDevice != os.ModeCharDevice
}
func HexxyDump(r io.Reader, w io.Writer, filename string, color *Color) error {
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
colFmt int
)
if dumpType == dumpCformat {
_ = copy(varDeclChar[0:14], unsignedChar[:])
_ = copy(varDeclInt[0:16], unsignedInt[:])
for i := 0; i < len(filename); i++ {
if !isSpecial(filename[i]) {
varDeclChar[14+i] = filename[i]
varDeclInt[16+i] = filename[i]
} else {
varDeclChar[14+i] = '_'
varDeclInt[16+i] = '_'
}
}
// copy "[] = {" and "_len = "
_ = copy(varDeclChar[14+len(filename):], brackets[:])
_ = copy(varDeclInt[16+len(filename):], lenEquals[:])
}
if opts.Upper {
caps = udigits
}
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
}
switch dumpType {
case dumpBinary:
octs = 8
groupSize = 1
case dumpPlain:
octs = 0
case dumpCformat:
octs = 4
default:
octs = 2
groupSize = 2
}
if opts.GroupSize != -1 {
groupSize = opts.GroupSize
}
if opts.Len != -1 {
if opts.Len < int64(cols) {
cols = int(opts.Len)
}
}
if octs < 1 {
octs = cols
}
switch opts.OffsetFormat {
case "d":
colFmt = 10
case "o":
colFmt = 8
case "x":
fallthrough
default:
colFmt = 16
}
// allocate their size based on the users specs, hence why its declared here
var (
line = make([]byte, cols)
char = make([]byte, octs)
)
c := int64(0)
nl := int64(0)
r = bufio.NewReader(r)
var (
n int
err error
)
for {
n, err = io.ReadFull(r, line)
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
return fmt.Errorf("hexxy: %v", err)
}
// we check early on for if the dump type is "plain" (no formatting, its just a stream of bytes)
// and we don't have to do any hard work
if dumpType == dumpPlain && n != 0 {
for i := 0; i < n; i++ {
hexEncode(char, line[i:i+1], caps)
w.Write(char)
c++
}
continue
}
// write the line ending based on the dump "mode"
if n == 0 {
if dumpType == dumpPlain {
w.Write(newLine)
}
if dumpType == dumpCformat {
doCEnd = true
} else {
return nil
}
}
if opts.Len != -1 {
if totalOcts == opts.Len {
break
}
totalOcts += opts.Len
}
if opts.Autoskip && isEmpty(&line) {
if nulLine == 1 {
w.Write(asterisk)
w.Write(newLine)
}
nulLine++
if nulLine > 1 {
lineOffset++ // still increment offset while printing crunched lines with '*'
continue
}
}
// hex or binary formats only
// writing the 0000000: part
if dumpType <= dumpBinary {
// create line offset
hexOffset = strconv.AppendInt(hexOffset[0:0], lineOffset, colFmt)
// confusing looking but we are just "slicing" our zero padding buffer and our offset byte buffer together
// ie zeroHeader = 0000000: and hexOffset = 10 -- we are just inserting that 10 in this position '0000010:'
// based on the offsets length
if USE_COLOR {
w.Write([]byte(GREY))
w.Write(zeroHeader[0:(6 - len(hexOffset))])
w.Write(hexOffset)
w.Write(zeroHeader[6:])
w.Write([]byte(CLEAR))
} else {
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
}
if dumpType == dumpBinary {
// dump binary values
for i, k := 0, octs; i < n; i, k = i+1, k+octs {
binaryEncode(char, line[i:i+1])
if USE_COLOR {
for _, b := range char {
if b == '1' {
w.Write([]byte("\x1b[32m"))
w.Write([]byte{b})
w.Write([]byte(CLEAR))
} else {
w.Write([]byte("\x1b[34m"))
w.Write([]byte{b})
w.Write([]byte(CLEAR))
}
}
// w.Write(char)
c++
} else {
w.Write(char)
c++
}
if k == octs*groupSize {
k = 0
w.Write(space)
}
}
} else if dumpType == dumpCformat {
// dump C format
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)
if USE_COLOR {
i := line[i : i+1][0]
b, c := color.Colorize2(i)
w.Write(b)
w.Write(char)
w.Write(c)
} else {
w.Write(char)
}
c++
if k == octs*groupSize {
k = 0
w.Write(space)
}
}
}
if doCEnd {
w.Write(varDeclInt)
w.Write([]byte(strconv.FormatInt(c, 10)))
w.Write(semiColonNl)
return nil
}
if n < len(line) && dumpType <= dumpBinary {
for i := n * octs; i < len(line)*octs; i++ {
w.Write(space)
if i%octs == 1 {
w.Write(space)
}
}
}
if dumpType != dumpCformat {
w.Write(space)
}
if dumpType <= dumpBinary {
// character values
b := line[:n]
// |hello,.world!|
if opts.Bars {
w.Write(bar)
}
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)
}
}
if opts.Bars {
w.Write(bar)
}
}
w.Write(newLine)
nl++
}
return nil
}
func Hexxy(args []string) error {
color := &Color{}
if opts.NoColor || !USE_COLOR {
color.disable = true
}
if !color.disable {
color.Compute()
}
var (
infile *os.File
outfile *os.File
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())
}
}
defer infile.Close()
if opts.Seek != -1 {
_, err = infile.Seek(opts.Seek, io.SeekStart)
if err != nil {
return fmt.Errorf("hexxy: %v", err.Error())
}
}
if opts.OutputFile != "" {
outfile, err = os.Open(opts.OutputFile)
if err != nil {
return fmt.Errorf("hexxy: %v", err.Error())
}
} 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 {
if err := HexxyReverse(infile, outfile); err != nil {
return fmt.Errorf("hexxy: %v", err.Error())
}
return nil
}
if err := HexxyDump(infile, out, infile.Name(), color); err != nil {
return fmt.Errorf("hexxy: %v", err.Error())
}
return nil
}
const usage_msg = `
hexxy is a command line hex dumping tool
Examples:
hexxy [OPTIONS] input-file
# 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
`
// extra usage examples
func usage() {
fmt.Fprint(os.Stderr, usage_msg)
}
// parses the color flag and decides whether color is appropriate or not
func useColor() bool {
// NO_COLOR spec compliance
if HasNoColorEnvVar() {
return false
}
switch strings.ToLower(opts.HasColor) {
case "always":
return true
case "auto":
if opts.NoColor {
return false
}
return !outputIsPipe()
case "never":
return false
}
return false
}
func init() {
opts.Seek = -1 // default no-op values
opts.Columns = -1
opts.GroupSize = -1
opts.Len = -1
}
func main() {
parser := flags.NewParser(&opts, flags.Default)
args, err := parser.Parse()
if flags.WroteHelp(err) {
usage()
os.Exit(0)
}
if err != nil {
log.Fatal(err)
}
// set color based on flags or default to off
USE_COLOR = useColor()
if !inputIsPipe() && len(args) == 0 {
parser.WriteHelp(os.Stderr)
fmt.Print(usage_msg)
os.Exit(0)
}
if opts.Verbose {
Debug = log.Printf
}
if err := Hexxy(args); err != nil {
log.Fatal(err)
}
}