-
Notifications
You must be signed in to change notification settings - Fork 2
/
encode.go
231 lines (200 loc) · 5.09 KB
/
encode.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"errors"
"fmt"
)
const (
reverseHexTable = "" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xff\xff\xff\xff\xff\xff" +
"\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
)
var ErrLength = errors.New("encoding/hex: odd length hex string")
// InvalidByteError values describe errors resulting from an invalid byte in a hex string.
type InvalidByteError byte
func (e InvalidByteError) Error() string {
return fmt.Sprintf("encoding/hex: invalid byte: %#U", rune(e))
}
func binaryEncode(dst, src []byte) {
d := uint(0)
_, _ = src[0], dst[7]
for i := 7; i >= 0; i-- {
if src[0]&(1<<d) == 0 {
dst[i] = '0'
} else {
dst[i] = '1'
}
d++
}
}
// returns -1 on success
// returns k > -1 if space found where k is index of space byte
func binaryDecode(dst, src []byte) int {
var v, d byte
for i := 0; i < len(src); i++ {
v, d = src[i], d<<1
if isSpace(v) { // found a space, so between groups
if i == 0 {
return 1
}
return i
}
if v == '1' {
d ^= 1
} else if v != '0' {
return i // will catch issues like "000000: "
}
}
dst[0] = d
return -1
}
func cfmtEncode(dst, src []byte, hextable string) {
b := src[0]
dst[3] = hextable[b&0x0f]
dst[2] = hextable[b>>4]
dst[1] = 'x'
dst[0] = '0'
}
// copied from encoding/hex package in order to add support for uppercase hex
func hexEncode(dst, src []byte, hextable string) {
b := src[0]
dst[1] = hextable[b&0x0f]
dst[0] = hextable[b>>4]
}
// copied from encoding/hex package
// returns -1 on bad byte or space (\t \s \n)
// returns -2 on two consecutive spaces
// returns 0 on success
func hexDecode(dst, src []byte) (int, error) {
i, j := 0, 1
for ; j < len(src); j += 2 {
p := src[j-1]
q := src[j]
a := reverseHexTable[p]
b := reverseHexTable[q]
if a > 0x0f {
return i, InvalidByteError(p)
}
if b > 0x0f {
return i, InvalidByteError(q)
}
dst[i] = (a << 4) | b
i++
}
if len(src)%2 == 1 {
// Check for invalid char before reporting bad length,
// since the invalid char (if present) is an earlier problem.
if reverseHexTable[src[j-1]] > 0x0f {
return i, InvalidByteError(src[j-1])
}
return i, ErrLength
}
return i, nil
}
// copied from encoding/hex package
func fromHexChar(c byte) (byte, bool) {
switch {
case '0' <= c && c <= '9':
return c - '0', true
case 'a' <= c && c <= 'f':
return c - 'a' + 10, true
case 'A' <= c && c <= 'F':
return c - 'A' + 10, true
}
return 0, false
}
// check if entire line is full of isEmpty []byte{0} bytes (nul in C)
func isEmpty(b *[]byte) bool {
for i := 0; i < len(*b); i++ {
if (*b)[i] != 0 {
return false
}
}
return true
}
// check if filename character contains problematic characters
func isSpecial(b byte) bool {
switch b {
case '/', '!', '#', '$', '%', '^', '&', '*', '(', ')', ';', ':', '|', '{', '}', '\\', '~', '`':
return true
default:
return false
}
}
// quick binary tree check
// probably horribly written idk it's late at night
func parseSpecifier(b string) float64 {
lb := len(b)
if lb == 0 {
return 0
}
var b0, b1 byte
if lb < 2 {
b0 = b[0]
b1 = '0'
} else {
b1 = b[1]
b0 = b[0]
}
if b1 != '0' {
if b1 == 'b' { // bits, so convert bytes to bits for os.Seek()
if b0 == 'k' || b0 == 'K' {
return 0.0078125
}
if b0 == 'm' || b0 == 'M' {
return 7.62939453125e-06
}
if b0 == 'g' || b0 == 'G' {
return 7.45058059692383e-09
}
}
if b1 == 'B' { // kilo/mega/giga- bytes are assumed
if b0 == 'k' || b0 == 'K' {
return 1024
}
if b0 == 'm' || b0 == 'M' {
return 1048576
}
if b0 == 'g' || b0 == 'G' {
return 1073741824
}
}
} else { // kilo/mega/giga- bytes are assumed for single b, k, m, g
if b0 == 'k' || b0 == 'K' {
return 1024
}
if b0 == 'm' || b0 == 'M' {
return 1048576
}
if b0 == 'g' || b0 == 'G' {
return 1073741824
}
}
return 1 // assumes bytes as fallback
}
// is byte a space? (\t, \n, \s)
func isSpace(b byte) bool {
switch b {
case 32, 12, 9:
return true
default:
return false
}
}
// are the two bytes hex prefixes? (0x or 0X)
func isPrefix(b []byte) bool {
return b[0] == '0' && (b[1] == 'x' || b[1] == 'X')
}