-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil.go
364 lines (311 loc) · 7.53 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package binlog
import (
"crypto/sha1"
"encoding/binary"
"fmt"
"io"
"math"
"reflect"
"unsafe"
)
type Position struct {
Name string
Pos uint32
}
func byteCountFromBitCount(n int) int {
return (n + 7) / 8
}
func getBinaryInt8(b []byte) int8 {
return int8(b[0])
}
func getBinaryUint8(b []byte) uint8 {
return b[0]
}
func getBinaryInt16(data []byte) int16 {
return int16(binary.LittleEndian.Uint16(data))
}
func getBinaryUint16(data []byte) uint16 {
return binary.LittleEndian.Uint16(data)
}
func getBinaryInt24(data []byte) int32 {
u32 := uint32(getBinaryUint24(data))
if u32&0x00800000 != 0 {
u32 |= 0xFF000000
}
return int32(u32)
}
func getBinaryUint24(data []byte) uint32 {
return uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16
}
func getBinaryInt32(data []byte) int32 {
return int32(binary.LittleEndian.Uint32(data))
}
func getBinaryUint32(data []byte) uint32 {
return binary.LittleEndian.Uint32(data)
}
func getBinaryInt64(data []byte) int64 {
return int64(binary.LittleEndian.Uint64(data))
}
func getBinaryUint64(data []byte) uint64 {
return binary.LittleEndian.Uint64(data)
}
func getBinaryFloat32(data []byte) float32 {
return math.Float32frombits(binary.LittleEndian.Uint32(data))
}
func getBinaryFloat64(data []byte) float64 {
return math.Float64frombits(binary.LittleEndian.Uint64(data))
}
// little-endian
func getLittleEndianFixedLengthInt(buf []byte) uint64 {
var num uint64 = 0
for i, b := range buf {
num |= uint64(b) << (uint(i) * 8)
}
return num
}
// big-endian
func getBigEndianFixedLengthInt(buf []byte) uint64 {
var num uint64 = 0
for i, b := range buf {
num |= uint64(b) << (uint(len(buf)-i-1) * 8)
}
return num
}
// getLengthEncodedInt reads from the passed-in buffer and returns the number as
// stored in length-encoded integer format or true if it's null, plus the number
// of bytes read.
func getLengthEncodedInt(b []byte) (num uint64, isNull bool, n int) {
switch b[0] {
// 251: NULL
case 0xfb:
n = 1
isNull = true
return
// 252: value of following 2
case 0xfc:
num = uint64(b[1]) | uint64(b[2])<<8
n = 3
return
// 253: value of following 3
case 0xfd:
num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16
n = 4
return
// 254: value of following 8
case 0xfe:
num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16 |
uint64(b[4])<<24 | uint64(b[5])<<32 | uint64(b[6])<<40 |
uint64(b[7])<<48 | uint64(b[8])<<56
n = 9
return
}
// 0-250: value of first byte
num = uint64(b[0])
n = 1
return
}
func putLengthEncodedInt(n uint64) []byte {
switch {
case n <= 250:
return []byte{byte(n)}
case n <= 0xffff:
return []byte{0xfc, byte(n), byte(n >> 8)}
case n <= 0xffffff:
return []byte{0xfd, byte(n), byte(n >> 8), byte(n >> 16)}
case n <= 0xffffffffffffffff:
return []byte{0xfe, byte(n), byte(n >> 8), byte(n >> 16), byte(n >> 24),
byte(n >> 32), byte(n >> 40), byte(n >> 48), byte(n >> 56)}
}
return nil
}
func getLengthEncodedString(b []byte) ([]byte, bool, int, error) {
// Get length
num, isNull, n := getLengthEncodedInt(b)
if num < 1 {
return nil, isNull, n, nil
}
n = n + int(num)
// Check data length
if len(b) >= n {
return b[n-int(num) : n], false, n, nil
}
return nil, false, n, io.EOF
}
func skipLengthEncodedString(b []byte) (int, error) {
// Get length
num, _, n := getLengthEncodedInt(b)
if num < 1 {
return n, nil
}
n = n + int(num)
// Check data length
if len(b) >= n {
return n, nil
}
return n, io.EOF
}
var bitCountInByte = [256]uint8{
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
}
// Calculate total bit counts in a bitmap
func bitCount(bitmap []byte) int {
var n uint32 = 0
for _, bit := range bitmap {
n = n + uint32(bitCountInByte[bit])
}
return int(n)
}
// Get the bit set at offset position in bitmap
func getBit(bitmap []byte, off int) byte {
bit := bitmap[off/8]
return bit & (1 << (uint(off) & 7))
}
// scramble41() returns a scramble buffer based on the following formula:
// SHA1(password) XOR SHA1(20-byte public seed from server CONCAT SHA1(SHA1(password)))
func scramble41(scramble, password []byte) []byte {
if len(password) == 0 {
return nil
}
hash := sha1.New()
// Stage 1 hash: SHA1(password)
hash.Write(password)
stage1 := hash.Sum(nil)
// Stage 2 hash: SHA1(SHA1(password))
hash.Reset()
hash.Write(stage1)
stage2 := hash.Sum(nil)
// Scramble hash
hash.Reset()
hash.Write(scramble)
hash.Write(stage2)
result := hash.Sum(nil)
// token = scrambleHash XOR stage1Hash
for i := range result {
result[i] ^= stage1[i]
}
return result
}
func formatBinaryDate(n int, data []byte) ([]byte, error) {
switch n {
case 0:
return []byte("0000-00-00"), nil
case 4:
return []byte(fmt.Sprintf("%04d-%02d-%02d",
binary.LittleEndian.Uint16(data[:2]),
data[2],
data[3])), nil
default:
return nil, fmt.Errorf("invalid date packet length %d", n)
}
}
func formatBinaryDateTime(n int, data []byte) ([]byte, error) {
switch n {
case 0:
return []byte("0000-00-00 00:00:00"), nil
case 4:
return []byte(fmt.Sprintf("%04d-%02d-%02d 00:00:00",
binary.LittleEndian.Uint16(data[:2]),
data[2],
data[3])), nil
case 7:
return []byte(fmt.Sprintf(
"%04d-%02d-%02d %02d:%02d:%02d",
binary.LittleEndian.Uint16(data[:2]),
data[2],
data[3],
data[4],
data[5],
data[6])), nil
case 11:
return []byte(fmt.Sprintf(
"%04d-%02d-%02d %02d:%02d:%02d.%06d",
binary.LittleEndian.Uint16(data[:2]),
data[2],
data[3],
data[4],
data[5],
data[6],
binary.LittleEndian.Uint32(data[7:11]))), nil
default:
return nil, fmt.Errorf("invalid datetime packet length %d", n)
}
}
func formatBinaryTime(n int, data []byte) ([]byte, error) {
if n == 0 {
return []byte("0000-00-00"), nil
}
var sign byte
if data[0] == 1 {
sign = byte('-')
}
switch n {
case 8:
return []byte(fmt.Sprintf(
"%c%02d:%02d:%02d",
sign,
uint16(data[1])*24+uint16(data[5]),
data[6],
data[7],
)), nil
case 12:
return []byte(fmt.Sprintf(
"%c%02d:%02d:%02d.%06d",
sign,
uint16(data[1])*24+uint16(data[5]),
data[6],
data[7],
binary.LittleEndian.Uint32(data[8:12]),
)), nil
default:
return nil, fmt.Errorf("invalid time packet length %d", n)
}
}
func putBinaryUint16(n uint16) []byte {
return []byte{
byte(n),
byte(n >> 8),
}
}
func putBinaryUint32(n uint32) []byte {
return []byte{
byte(n),
byte(n >> 8),
byte(n >> 16),
byte(n >> 24),
}
}
func putBinaryUint64(n uint64) []byte {
return []byte{
byte(n),
byte(n >> 8),
byte(n >> 16),
byte(n >> 24),
byte(n >> 32),
byte(n >> 40),
byte(n >> 48),
byte(n >> 56),
}
}
// superUnsafeGetString() force-casts a []byte to a string (DANGEROUS!)
func superUnsafeGetString(b []byte) (s string) {
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
pstring.Data = pbytes.Data
pstring.Len = pbytes.Len
return
}