-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh3x.go
71 lines (60 loc) · 1.29 KB
/
h3x.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
// Imports and globals
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func hexdump(s string) ([]byte, error) {
content, err := ioutil.ReadFile(s)
if err != nil {
panic("Cannot open file")
}
return content, nil
}
func print_hd(arr []byte) {
row_width := 16
curr_row_width := 16
seen_bytes := 0
for i := 0; i < len(arr); i += row_width {
if (len(arr) - seen_bytes) < row_width { // check if our file-size is multiple of 16
curr_row_width = (len(arr) - seen_bytes)
}
row := arr[i:(seen_bytes + curr_row_width)]
// print hex values
for i := 0; i < row_width; i++ {
if i < curr_row_width {
fmt.Printf("%02x ", row[i])
} else {
// if our file is no multiple of 16, pad it to align the "|" symbols
fmt.Print(strings.Repeat(" ", 3))
}
}
fmt.Print("|")
fmt.Print(" ")
// print ascii representation
for i := 0; i < row_width; i++ {
if i < curr_row_width {
if row[i] >= 0x20 && row[i] < 0x7f {
fmt.Print(string(row[i]))
} else {
fmt.Print(".")
}
} else {
fmt.Print(strings.Repeat(" ", 3))
}
}
fmt.Print("|")
fmt.Print("\n")
seen_bytes += row_width
}
}
func main() {
if len(os.Args) <= 1 {
fmt.Println("Usage: h3x <FILEPATH>")
os.Exit(1)
}
var content, _ = hexdump(os.Args[1])
print_hd(content)
}