-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.go
126 lines (111 loc) · 2.07 KB
/
util.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
package bactract
// "Utility" functions
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
)
// toInt converts a byte array (string) of digits to its corresponding
// integer value
func toInt(b []byte) (ret int, err error) {
ret, err = strconv.Atoi(string(b))
if err != nil {
var digits = map[string]int{
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
}
ret = 0
for i := range b {
x, ok := digits[string(b[i])]
if !ok {
return 0, errors.New("toInt(): Not an integer")
}
ret = (10 * ret) + x
}
}
return ret, nil
}
// toRunes translates a [character] byte slice to the corresponding rune slice
func toRunes(b []byte) (ret []rune) {
var j int
ret = make([]rune, len(b)/2)
for i := 0; i < len(b); i = i + 2 {
z := int32(b[i])
if b[i+1] != 0x00 {
z |= int32(b[i+1]) << uint(8)
}
ret[j] = z
j++
}
return ret
}
// stripTrailingNulls removes the null bytes from the end of a byte slice
func stripTrailingNulls(b []byte) []byte {
i := len(b)
if i > 0 {
for {
if i == 1 || int(b[i-1]) != 0x00 {
break
}
i--
}
b = b[:i]
}
return b
}
func catDir(t []string) (dir string) {
dir = strings.Join(t, string(os.PathSeparator))
return dir
}
func debOut(msg string) {
if debugFlag {
fmt.Println(msg)
}
}
func debHextOut(label string, bytes []byte) {
if debugFlag {
fmt.Printf("%s: ", label)
if len(bytes) > debugLen && debugLen > 10 {
for _, b := range bytes[0 : debugLen-6] {
if int(b) == 0 {
fmt.Print("0x00 ")
} else if int(b) < 0x10 {
fmt.Printf("0x0%x ", b)
} else {
fmt.Printf("0x%x ", b)
}
}
fmt.Print(" ... ")
for _, b := range bytes[len(bytes)-4:] {
if int(b) == 0 {
fmt.Print("0x00 ")
} else if int(b) < 0x10 {
fmt.Printf("0x0%x ", b)
} else {
fmt.Printf("0x%x ", b)
}
}
} else {
for _, b := range bytes {
if int(b) == 0 {
fmt.Print("0x00 ")
} else if int(b) < 0x10 {
fmt.Printf("0x0%x ", b)
} else {
fmt.Printf("0x%x ", b)
}
}
}
fmt.Println()
}
}