-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfield.go
121 lines (96 loc) · 1.9 KB
/
field.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
package binlog
import (
"encoding/binary"
"errors"
)
type FieldData []byte
type Field struct {
Data FieldData
Schema []byte
Table []byte
OrgTable []byte
Name []byte
OrgName []byte
Charset uint16
ColumnLength uint32
Type uint8
Flag uint16
Decimal uint8
DefaultValueLength uint64
DefaultValue []byte
}
func (p FieldData) parse() (f *Field, err error) {
f = new(Field)
f.Data = p
var n int
i := 0
// Skip catalog (always def)
n, err = skipLengthEncodedString(p)
if err != nil {
return
}
i = i + n
// Schema
f.Schema, _, n, err = getLengthEncodedString(p[i:])
if err != nil {
return
}
i = i + n
// Table
f.Table, _, n, err = getLengthEncodedString(p[i:])
if err != nil {
return
}
i = i + n
// Org table
f.OrgTable, _, n, err = getLengthEncodedString(p[i:])
if err != nil {
return
}
i = i + n
// Name
f.Name, _, n, err = getLengthEncodedString(p[i:])
if err != nil {
return
}
i = i + n
// Org name
f.OrgName, _, n, err = getLengthEncodedString(p[i:])
if err != nil {
return
}
i = i + n
// Skip oc
i = i + 1
// Charset
f.Charset = binary.LittleEndian.Uint16(p[i:])
i = i + 2
// Column length
f.ColumnLength = binary.LittleEndian.Uint32(p[i:])
i = i + 4
// Type
f.Type = p[i]
i = i + 1
// Flag
f.Flag = binary.LittleEndian.Uint16(p[i:])
i = i + 2
// Decimals
f.Decimal = p[i]
i = i + 1
// Filter [0x00][0x00]
i = i + 2
f.DefaultValue = nil
// If more data, command was field list
if len(p) > i {
// Length of default value (lenenc-int)
f.DefaultValueLength, _, n = getLengthEncodedInt(p[i:])
i = i + n
if i+int(f.DefaultValueLength) > len(p) {
err = errors.New("malformed packet")
return
}
// Default value (string[len])
f.DefaultValue = p[i:(i + int(f.DefaultValueLength))]
}
return
}