-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (98 loc) · 1.77 KB
/
main.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
package main
import (
"bytes"
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
func main() {
var decode, dump, pretty bool
flag.BoolVar(&decode, "d", false, "decodes input")
flag.BoolVar(&dump, "c", false, "encodes the input as hexadecimal followed by characters")
flag.BoolVar(&pretty, "p", false, "encoded using a prettier format aa:bb")
flag.Usage = usage
flag.Parse()
var b []byte
var err error
switch len(flag.Args()) {
case 0:
b, err = ioutil.ReadAll(os.Stdin)
if err != nil {
fatal(err)
}
case 1:
b, err = ioutil.ReadFile(flag.Arg(0))
if err != nil {
fatal(err)
}
default:
flag.Usage()
os.Exit(1)
}
switch {
case decode:
var bb bytes.Buffer
// Remove 0x prefix
if len(b) >= 2 && bytes.EqualFold(b[:2], []byte("0x")) {
b = b[2:]
}
for _, c := range b {
if isHexChar(c) {
bb.WriteByte(c)
}
}
out := make([]byte, bb.Len()/2)
if _, err = hex.Decode(out, bb.Bytes()); err != nil {
fatal(err)
}
if dump {
fmt.Print(hex.Dump(out))
} else {
os.Stdout.Write(out)
}
case dump:
fmt.Print(hex.Dump(b))
default:
if pretty {
prettify(b)
} else {
fmt.Printf("%x\n", b)
}
}
}
func usage() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [<filename>]\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
func fatal(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func isHexChar(c byte) bool {
switch {
case '0' <= c && c <= '9':
return true
case 'a' <= c && c <= 'f':
return true
case 'A' <= c && c <= 'F':
return true
default:
return false
}
}
func prettify(data []byte) {
last := len(data) - 1
for i, b := range data {
if i != 0 && (i%16) == 0 {
fmt.Print("\n")
}
fmt.Printf("%02x", b)
if i != last {
fmt.Print(":")
}
}
fmt.Println()
}