-
Notifications
You must be signed in to change notification settings - Fork 1
/
hex.go
95 lines (78 loc) · 1.7 KB
/
hex.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
package main
import (
"fmt"
"io"
"os"
)
func hexDumpAround(filename string, offset, context int64) error {
const bytesPerLine = 16
// Open binary file
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
// Calculate start position, ensure it's not negative
start := offset - context
if start < 0 {
start = 0
}
end := offset + context
// Seek to start position
_, err = file.Seek(start, io.SeekStart)
if err != nil {
return err
}
data := make([]byte, end-start)
_, err = file.Read(data)
if err != nil {
return err
}
mid := int64(len(data)) / 2
// Print hex dump from start to end
for i := int64(0); i < int64(len(data)); i += bytesPerLine {
chunk := data[i:min(i+bytesPerLine, int64(len(data)))]
// Print the offset in the data stream
fmt.Printf("%08x ", start+i)
// Print the hex codes
for j, b := range chunk {
fmt.Printf("%02x ", b)
if j%2 == 1 {
fmt.Print(" ")
}
}
// Pad the line with spaces if it's shorter than bytesPerLine
if len(chunk) < bytesPerLine {
for j := len(chunk); j < bytesPerLine; j++ {
fmt.Print(" ")
if j%2 == 1 {
fmt.Print(" ")
}
}
}
colored := (i == mid-bytesPerLine || i == mid || i == mid+bytesPerLine)
if colored {
fmt.Print("\033[1;31m")
}
// Print the ASCII representation
for _, b := range chunk {
if b >= 32 && b <= 126 {
fmt.Printf("%c", b)
} else {
fmt.Print("·")
}
}
if colored {
fmt.Print("\033[0m")
}
fmt.Println()
}
return nil
}
// min returns the smaller of x or y.
func min(x, y int64) int64 {
if x < y {
return x
}
return y
}