-
Notifications
You must be signed in to change notification settings - Fork 0
/
disasm_line_components.go
executable file
·67 lines (57 loc) · 1.47 KB
/
disasm_line_components.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
package wax
import (
"fmt"
"strings"
)
type disasmTree []*disasmLineComponents
type disasmLineComponents struct {
binary []byte
mnemonic string
indent int
nest disasmTree
}
func (dt disasmTree) Join(delim string) string {
indent := []string{}
binaryLines := []string{}
mnemonicLines := []string{}
maxHexLen := 0
flat := flattenDisasmTree(dt, 0)
for _, node := range flat {
h := hexString(node.binary)
if len(h)+(node.indent*2) > maxHexLen {
maxHexLen = len(h) + (node.indent * 2)
}
indent = append(indent, nspace(node.indent*2))
binaryLines = append(binaryLines, h)
mnemonicLines = append(mnemonicLines, node.mnemonic)
}
f := fmt.Sprintf("%%s%%-%ds %%s%%s", maxHexLen)
result := []string{}
for i := range binaryLines {
result = append(result, fmt.Sprintf(f, indent[i], binaryLines[i], indent[i], mnemonicLines[i]))
}
return strings.Join(result, delim)
}
func nspace(n int) string {
f := fmt.Sprintf("%%%ds", n)
return fmt.Sprintf(f, "")
}
func flattenDisasmTree(dt disasmTree, indent int) []*disasmLineComponents {
result := []*disasmLineComponents{}
for _, node := range dt {
node.indent = indent
result = append(result, node)
if node.nest != nil {
children := flattenDisasmTree(node.nest, indent+1)
result = append(result, children...)
}
}
return result
}
func hexString(bs []byte) string {
result := []string{}
for _, b := range bs {
result = append(result, fmt.Sprintf("%02x", b))
}
return strings.Join(result, " ")
}