Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
208 lines (191 loc) · 4.04 KB
/
Copy pathmain.go
File metadata and controls
208 lines (191 loc) · 4.04 KB
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
package main
import (
"bytes"
"encoding/hex"
"flag"
"fmt"
"io"
"math/big"
"os"
"path/filepath"
"strconv"
"strings"
)
type prettyFormat struct {
IsSet bool
Columns int
}
func (p *prettyFormat) String() string {
if p.Columns == 0 {
return "true"
}
return strconv.Itoa(p.Columns)
}
func (p *prettyFormat) Set(s string) error {
s = strings.ToLower(s)
if s == "true" || s == "false" {
p.IsSet = (s == "true")
return nil
}
n, err := strconv.Atoi(s)
if err != nil {
return err
}
p.IsSet = true
p.Columns = n
return nil
}
func (p *prettyFormat) IsBoolFlag() bool {
return true
}
func main() {
var decode, dump, number, uppercase bool
var base int
var pretty, goFormat prettyFormat
flag.BoolVar(&decode, "d", false, "decodes input")
flag.BoolVar(&dump, "c", false, "encodes the input as hexadecimal followed by characters")
flag.Var(&pretty, "p", "encoded using a prettier format aa:bb, pass -p=n to print using n columns")
flag.BoolVar(&uppercase, "u", false, "encoded using uppercase hexadecimal characters")
flag.Var(&goFormat, "go", "encoded using as Go's []byte, pass -go=n to print using n columns")
flag.BoolVar(&number, "n", false, "interprets input as a number")
flag.IntVar(&base, "b", 10, "base used to when -n is used")
flag.Usage = usage
flag.Parse()
var b []byte
var err error
switch len(flag.Args()) {
case 0:
b, err = io.ReadAll(os.Stdin)
if err != nil {
fatal(err)
}
case 1:
b, err = os.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 if number {
fmt.Println(new(big.Int).SetBytes(out).String())
} else {
os.Stdout.Write(out)
}
case dump:
fmt.Print(hex.Dump(b))
default:
if number {
n, ok := new(big.Int).SetString(string(bytes.TrimSpace(b)), base)
if !ok {
fatal(fmt.Errorf("error parsing %s in base %d", b, base))
}
b = n.Bytes()
}
switch {
case pretty.IsSet:
prettify(b, pretty.Columns, uppercase)
case goFormat.IsSet:
goify(b, goFormat.Columns, uppercase)
default:
if uppercase {
fmt.Printf("%X\n", b)
} else {
fmt.Printf("%x\n", b)
}
}
}
}
func usage() {
o := flag.CommandLine.Output()
fmt.Fprintf(o, "Usage: %s [<filename>]\n", filepath.Base(flag.CommandLine.Name()))
fmt.Fprintf(o, " -c %s\n", flag.Lookup("c").Usage)
fmt.Fprintf(o, " -d %s\n", flag.Lookup("d").Usage)
fmt.Fprintf(o, " -p %s\n", flag.Lookup("p").Usage)
fmt.Fprintf(o, " -u %s\n", flag.Lookup("u").Usage)
fmt.Fprintf(o, " -go %s\n", flag.Lookup("go").Usage)
fmt.Fprintf(o, " -n %s\n", flag.Lookup("n").Usage)
fmt.Fprintf(o, " -b int %s (default: %s)\n", flag.Lookup("b").Usage, flag.Lookup("b").DefValue)
}
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, cols int, uppercase bool) {
format := "%02x"
if uppercase {
format = "%02X"
}
if cols == 0 {
cols = 16
}
last := len(data) - 1
for i, b := range data {
if i != 0 && (i%cols) == 0 {
fmt.Print("\n")
}
fmt.Printf(format, b)
if i != last {
fmt.Print(":")
}
}
fmt.Println()
}
func goify(data []byte, cols int, uppercase bool) {
format := "0x%02x"
if uppercase {
format = "0x%02X"
}
if cols == 0 {
cols = 8
}
last := len(data) - 1
fmt.Println("[]byte{")
for i, b := range data {
if (i % cols) == 0 {
if i == 0 {
fmt.Print("\t")
} else {
fmt.Print("\n\t")
}
}
fmt.Printf(format, b)
if i != last {
fmt.Print(", ")
} else {
fmt.Print(",")
}
}
fmt.Println("\n}")
}