-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbd.go
155 lines (136 loc) · 3.72 KB
/
bd.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
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
/*
* Copyright (c) 2013, Chris Anderson
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
"fmt"
"io/ioutil"
"os"
)
type FileArr [][]uint8
var maxFileSize int
func isGraph(char uint8) bool {
return (char > 0x20 && char <= 0x7E)
}
/* Mirrors output of the hd invocation of the linux hexdump */
func hexdump(f []uint8) {
for offset := 0; offset < len(f); offset += 16 {
fmt.Printf("%08x ", offset)
for i := 0; i < 16; i++ {
fmt.Printf("%s ", outputHex(offset + i, f))
if i == 7 { fmt.Print(" ") }
}
fmt.Print(" |")
for i := 0; i < 16; i++ {
if offset + i < len(f) {
tmp := f[offset+i]
if isGraph(tmp) {
fmt.Printf("%c", tmp)
} else {
fmt.Print(".")
}
} else {
fmt.Print(".")
}
}
fmt.Print("|\n")
}
}
/* Determines whether if a 16 byte row has a mismatch in any supplied file */
func lineDiffers(offset int, base []uint8, remote []uint8) [16]bool {
var diffMask [16]bool
var target uint8
for i := 0; i < 16; i++ {
// No reason to check beyond the largest of the files
if offset+i >= maxFileSize {
return diffMask
}
// If the lengths don't match then it's an obvious diff, otherwise check target
if offset+i < len(base) {
target = base[offset+i]
if offset+i >= len(remote) {
diffMask[i] = true
} else if remote[offset+i] != target {
diffMask[i] = true
}
} else {
diffMask[i] = true
}
}
return diffMask
}
func outputHex(offset int, file []uint8) string {
if offset >= len(file) {
return " "
}
return fmt.Sprintf("%02x", file[offset])
}
func printHexLine(offset int, fileData FileArr) {
for f := range fileData {
diffMask := lineDiffers(offset, fileData[0], fileData[f])
fmt.Printf("%08x ", offset)
for i := 0; i < 16; i++ {
tmp := outputHex(offset+i, fileData[f])
if diffMask[i] {
fmt.Printf("\x1b[31;1m%s\x1b[0m ", tmp)
} else {
fmt.Printf("%s ", tmp)
}
if i == 7 {
fmt.Print(" ")
}
}
fmt.Print(" ")
}
fmt.Print("\n")
}
func getMaxSize(f FileArr) {
for i := range f {
if len(f[i]) > maxFileSize {
maxFileSize = len(f[i])
}
}
}
func main() {
var err error
var paths = os.Args[1:]
if len(paths) < 1 {
println("usage:", os.Args[0], "<files>")
os.Exit(1)
}
fileData := make(FileArr, len(paths))
for i := 0; i < len(paths); i++ {
fileData[i], err = ioutil.ReadFile(paths[i])
if err != nil {
println("couldn't open", paths[i], ":", err.Error())
os.Exit(1)
}
}
/* If only one file is provided then act like hd */
if len(paths) == 1 {
hexdump(fileData[0])
} else {
getMaxSize(fileData)
for offset := 0; offset < maxFileSize; offset += 16 {
printHexLine(offset, fileData)
}
}
}
/* vim: set noexpandtab:ts=4:sw=4:sts:4 */