-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmysql_util.go
305 lines (282 loc) · 6.07 KB
/
mysql_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
/*
Utility functions for decoding mysql packets
*/
package mysql
import (
"bufio"
"crypto/sha1"
"encoding/binary"
"errors"
"fmt"
)
// Reads full slice or retutns error
func readFull(rd *bufio.Reader, p []byte) error {
for nn := 0; nn < len(p); {
kk, err := rd.Read(p[nn:])
/*if kk != len(p) {
fmt.Printf("DEBUG: readed %d/%d\n", kk, len(p))
}*/
if err != nil {
return err
}
nn += kk
}
return nil
}
//Read mysql packet header
func readHeader(br *bufio.Reader) (*PacketHeader, error) {
ph := new(PacketHeader)
i24seq := make([]byte, 4)
err := readFull(br, i24seq)
if err != nil {
return nil, errors.New(fmt.Sprintf("readHeader: %s", err))
}
ph.Len = unpackNumber(i24seq, 3)
ph.Seq = i24seq[3]
return ph, nil
}
//Decode length encoded number
func unpackLength(br *bufio.Reader) (val uint64, null bool, num int, err error) {
var bl uint8
err = binary.Read(br, binary.LittleEndian, &bl)
if err != nil {
return
}
num = 1
if bl == 251 {
null = true
} else if bl == 252 {
b := make([]byte, 2)
num += 2
err = readFull(br, b)
val = unpackNumber(b, 2)
} else if bl == 253 {
b := make([]byte, 3)
num += 3
err = readFull(br, b)
val = unpackNumber(b, 3)
} else if bl == 254 {
b := make([]byte, 8)
num += 8
err = readFull(br, b)
val = unpackNumber(b, 8)
} else {
val = uint64(bl)
}
return
}
//Special case of unpackLength where 0xfe == EOF
func unpackFieldCount(br *bufio.Reader) (val uint64, null bool, err error) {
var eof bool
eof, err = peekEOF(br)
if err != nil {
return
}
if eof {
err = ignoreBytes(br, 1)
val = uint64(0xfe)
return
}
val, null, _, err = unpackLength(br)
return
}
func packUint8(bw *bufio.Writer, u uint8) error {
return binary.Write(bw, binary.LittleEndian, u)
}
func packUint16(bw *bufio.Writer, u uint16) error {
return binary.Write(bw, binary.LittleEndian, u)
}
func packUint24(bw *bufio.Writer, u uint32) error {
b := make([]byte, 3)
b[0] = byte(u)
b[1] = byte(u >> 8)
b[2] = byte(u >> 16)
n, err := bw.Write(b)
if n != 3 {
return err
}
return nil
}
func packUint32(bw *bufio.Writer, u uint32) error {
return binary.Write(bw, binary.LittleEndian, u)
}
func packUint64(bw *bufio.Writer, u uint64) error {
return binary.Write(bw, binary.LittleEndian, u)
}
//Decode length encoded string
func unpackString(br *bufio.Reader) (string, bool, error) {
length, isnull, _, err := unpackLength(br)
if err != nil {
return "", false, err
}
b := make([]byte, length)
err = readFull(br, b)
return string(b), isnull, err
}
func packString(s string) []byte {
sb := []byte(s)
size := make([]byte, 1)
v := len(sb)
if v < 250 {
size[0] = uint8(v)
return append(size, sb...)
}
size = make([]byte, 9)
size[0] = 254
size[1] = byte(v)
size[2] = byte(v >> 8)
size[3] = byte(v >> 16)
size[4] = byte(v >> 24)
size[5] = byte(v >> 32)
size[6] = byte(v >> 40)
size[7] = byte(v >> 48)
size[8] = byte(v >> 56)
return append(size, sb...)
}
//Peek and check if packet is EOF
func peekEOF(br *bufio.Reader) (bool, error) {
b, err := br.ReadByte()
if err != nil {
return false, err
}
err = br.UnreadByte()
if b == 0xfe {
return true, err
}
return false, err
}
//Convert n bytes to uint64 (Little Endian)
func unpackNumber(b []byte, n uint8) uint64 {
if n < 1 {
return 0
}
var r uint64 = 0
for i := uint8(0); i < n; i++ {
r |= (uint64(b[i]) << (i * 8))
}
return r
}
//Read the field data from mysql ResultSet packet
func readFieldPacket(br *bufio.Reader) (f *MySQLField, err error) {
f = new(MySQLField)
f.Catalog, _, err = unpackString(br)
if err != nil {
return
}
f.Db, _, err = unpackString(br)
if err != nil {
return
}
f.Table, _, err = unpackString(br)
if err != nil {
return
}
f.OrgTable, _, err = unpackString(br)
if err != nil {
return
}
f.Name, _, err = unpackString(br)
if err != nil {
return
}
f.OrgName, _, err = unpackString(br)
if err != nil {
return
}
err = ignoreBytes(br, 1)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &f.Charset)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &f.Length)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &f.Type)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &f.Flags)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &f.Decimals)
if err != nil {
return
}
err = ignoreBytes(br, 1)
if err != nil {
return
}
f.Default, _, _, err = unpackLength(br)
return
}
//Read error packet
func readErrorPacket(br *bufio.Reader, pkt_len int) error {
var errcode uint16
err := binary.Read(br, binary.LittleEndian, &errcode)
if err != nil {
return err
}
status := make([]byte, 6)
err = readFull(br, status)
if err != nil {
return err
}
msg := make([]byte, pkt_len-9)
err = readFull(br, msg)
if err != nil {
return err
}
return errors.New(fmt.Sprintf("MySQL Error: (Code: %d) (Status: %s) %s", errcode, string(status), string(msg)))
}
//Read EOF packet.
//TODO: Return something useful?
func readEOFPacket(br *bufio.Reader) (err error) {
_, err = readHeader(br)
if err != nil {
return
}
response := new(MySQLResponse)
response.FieldCount, _, err = unpackFieldCount(br)
if err != nil {
return
}
if response.FieldCount != 0xfe {
fmt.Printf("Warning: Expected EOF! Got %#v\n", response.FieldCount)
}
err = binary.Read(br, binary.LittleEndian, &response.WarningCount)
if err != nil {
return
}
err = binary.Read(br, binary.LittleEndian, &response.ServerStatus)
return
}
//Ignores n bytes in the buffer
func ignoreBytes(br *bufio.Reader, n uint64) error {
buf := make([]byte, n)
return readFull(br, buf)
}
//Generate scrabled password using password and scramble buffer.
func mysqlPassword(password []byte, scrambleBuffer []byte) []byte {
ctx := sha1.New()
ctx.Write(password)
stage1 := ctx.Sum(nil)
ctx.Reset()
ctx.Write(stage1)
stage2 := ctx.Sum(nil)
ctx.Reset()
ctx.Write(scrambleBuffer)
ctx.Write(stage2)
result := ctx.Sum(nil)
token := make([]byte, 20)
for i := range token {
token[i] = result[i] ^ stage1[i]
}
tlen := make([]byte, 1)
tlen[0] = byte(len(token))
return append(tlen, token...)
}