-
Notifications
You must be signed in to change notification settings - Fork 3
/
bin2hex.go
82 lines (71 loc) · 1.73 KB
/
bin2hex.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
package main
import (
"bytes"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
flag "github.com/spf13/pflag"
)
var inFilePtr, outFilePtr *string
var lineBreakPtr *bool
var hexBytes []byte
func insertNth(s string, n int) string {
var buffer bytes.Buffer
var n1 = n - 1
var l1 = len(s) - 1
buffer.WriteRune('\\')
buffer.WriteRune('x')
for i, rune := range s {
if *lineBreakPtr && i%400 == 0 && i != 0 && i != l1 {
buffer.WriteRune('\r')
buffer.WriteRune('\n')
} else {
buffer.WriteRune(rune)
}
if i%n == n1 && i != l1 {
buffer.WriteRune('\\')
buffer.WriteRune('x')
}
}
return buffer.String()
}
func parseArgs() {
inFilePtr = flag.StringP("InFile", "i", "", "Path to the input file.")
outFilePtr = flag.StringP("OutFile", "o", "", "Path to the output file.")
lineBreakPtr = flag.BoolP("linebreak", "l", false, "Multiline output.")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "\r\n"+
"Bin2Hex is a command line tool that will parse a binary file and write out a a hex file in an escaped format (e.g. \"\\x90\\41\").\r\n\r\n"+
"For example .\\Bin2Hex.exe -i MyBytes.bin -o MyHex.hex\r\n\r\n"+
"Usage: \r\n")
flag.PrintDefaults()
os.Exit(0)
}
flag.Parse()
}
func readInputfile() {
// Get the args and work with them.
bytes, err := ioutil.ReadFile(*inFilePtr)
if err != nil {
fmt.Println("[!] Error reading file")
fmt.Println(err)
os.Exit(0)
}
binStr := hex.EncodeToString(bytes)
hexBytes = []byte(insertNth(binStr, 2))
}
func writeOutputFile() {
err := ioutil.WriteFile(*outFilePtr, hexBytes, 0644)
if err != nil {
fmt.Println("[!] Error writing file")
fmt.Println(err)
os.Exit(0)
}
fmt.Println("[+] Successfully wrote out file")
}
func main() {
parseArgs()
readInputfile()
writeOutputFile()
}