forked from dropbox/godropbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
91 lines (76 loc) · 1.95 KB
/
decoder.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
package binlog
import (
"bytes"
"encoding/binary"
"github.com/manilion/godropbox/errors"
)
func bytesToLEUint(valBytes []byte) uint64 {
val := uint64(0)
for i, b := range valBytes {
val += uint64(b) << (uint(i) * 8)
}
return val
}
const NullLength = uint64(^uint32(0))
// Note: this is equivalent to net_field_length in sql-common/pack.c
func readFieldLength(valBytes []byte) (
length uint64,
remaining []byte,
err error) {
if len(valBytes) == 0 {
return 0, nil, errors.New("Empty field length input")
}
val := uint64(valBytes[0])
if val < 251 {
return val, valBytes[1:], nil
} else if val == 251 {
return NullLength, valBytes[1:], nil
}
size := 9
if val == 252 {
size = 3
} else if val == 253 {
size = 4
}
if len(valBytes) < size {
return 0, nil, errors.Newf(
"Invalid field length input (expected at least %d bytes)",
size)
}
// NOTE: mysql's net_field_length implementation is somewhat broken.
// In particular, when net_store_length encode a ulong using 8 bytes,
// net_field_length will only read the first 4 bytes, and ignore the
// rest ....
return bytesToLEUint(valBytes[1:size]), valBytes[size:], nil
}
func readLittleEndian(valBytes []byte, val interface{}) ([]byte, error) {
r := bytes.NewBuffer(valBytes)
err := binary.Read(r, binary.LittleEndian, val)
if err != nil {
return nil, err
}
return r.Bytes(), nil
}
func readSlice(valBytes []byte, n int) (
slice []byte,
remaining []byte,
err error) {
if len(valBytes) < n {
return nil, nil, errors.New("not enough bytes")
}
return valBytes[:n], valBytes[n:], nil
}
func readBitArray(bytes []byte, numVals int) (
bits []bool,
remaining []byte,
err error) {
bytesUsed := ((numVals + 7) / 8)
if len(bytes) < bytesUsed {
return nil, nil, errors.New("Not enough bytes")
}
bitVector := make([]bool, numVals, numVals)
for i := 0; i < numVals; i++ {
bitVector[i] = (uint8(bytes[i/8]) & (1 << (uint(i) % 8))) != 0
}
return bitVector, bytes[bytesUsed:], nil
}