forked from hgfischer/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
313 lines (260 loc) · 6.1 KB
/
utils.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
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
//
// Copyright 2012 Julien Schmidt. All rights reserved.
// http://www.julienschmidt.com
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
package mysql
import (
"bytes"
"crypto/sha1"
"io"
"log"
"math"
"os"
"regexp"
"strconv"
"strings"
)
// Logger
var (
errLog *log.Logger
)
func init() {
errLog = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
dsnPattern = regexp.MustCompile(
`^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
`(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
`\/(?P<dbname>.*?)` + // /dbname
`(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1¶mN=valueN]
}
// Data Source Name Parser
var dsnPattern *regexp.Regexp
func parseDSN(dsn string) *config {
cfg := new(config)
cfg.params = make(map[string]string)
matches := dsnPattern.FindStringSubmatch(dsn)
names := dsnPattern.SubexpNames()
for i, match := range matches {
switch names[i] {
case "user":
cfg.user = match
case "passwd":
cfg.passwd = match
case "net":
cfg.net = match
case "addr":
cfg.addr = match
case "dbname":
cfg.dbname = match
case "params":
for _, v := range strings.Split(match, "&") {
param := strings.SplitN(v, "=", 2)
if len(param) != 2 {
continue
}
cfg.params[param[0]] = param[1]
}
}
}
// Set default network if empty
if cfg.net == "" {
cfg.net = "tcp"
}
// Set default adress if empty
if cfg.addr == "" {
cfg.addr = "127.0.0.1:3306"
}
return cfg
}
// Encrypt password using 4.1+ method
// http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#4.1_and_later
func scramblePassword(scramble, password []byte) (result []byte) {
if len(password) == 0 {
return
}
// stage1Hash = SHA1(password)
crypt := sha1.New()
crypt.Write(password)
stage1Hash := crypt.Sum(nil)
// scrambleHash = SHA1(scramble + SHA1(stage1Hash))
// inner Hash
crypt.Reset()
crypt.Write(stage1Hash)
scrambleHash := crypt.Sum(nil)
// outer Hash
crypt.Reset()
crypt.Write(scramble)
crypt.Write(scrambleHash)
scrambleHash = crypt.Sum(nil)
// token = scrambleHash XOR stage1Hash
result = make([]byte, 20)
for i := range result {
result[i] = scrambleHash[i] ^ stage1Hash[i]
}
return
}
/******************************************************************************
* Read data-types from bytes *
******************************************************************************/
// Read a slice from the data slice
func readSlice(data []byte, delim byte) (slice []byte, err error) {
pos := bytes.IndexByte(data, delim)
if pos > -1 {
slice = data[:pos]
} else {
slice = data
err = io.EOF
}
return
}
func readLengthCodedBinary(data []byte) (*[]byte, int, bool, error) {
// Get length
num, n, err := bytesToLengthCodedBinary(data)
if err != nil {
return nil, n, true, err
}
// Check data length
if len(data) < n+int(num) {
return nil, n, true, io.EOF
}
// Check if value is NULL
var isNull bool
if data[0] == 251 {
isNull = true
} else {
isNull = false
}
// Get bytes
b := data[n : n+int(num)]
n += int(num)
return &b, n, isNull, err
}
func readAndDropLengthCodedBinary(data []byte) (n int, err error) {
// Get length
num, n, err := bytesToLengthCodedBinary(data)
if err != nil {
return
}
// Check data length
if len(data) < n+int(num) {
err = io.EOF
return
}
n += int(num)
return
}
/******************************************************************************
* Convert from and to bytes *
******************************************************************************/
func byteToUint8(b byte) (n uint8) {
n |= uint8(b)
return
}
func bytesToUint16(b []byte) (n uint16) {
n |= uint16(b[0])
n |= uint16(b[1]) << 8
return
}
func uint24ToBytes(n uint32) (b []byte) {
b = make([]byte, 3)
for i := uint8(0); i < 3; i++ {
b[i] = byte(n >> (i << 3))
}
return
}
func bytesToUint32(b []byte) (n uint32) {
for i := uint8(0); i < 4; i++ {
n |= uint32(b[i]) << (i << 3)
}
return
}
func uint32ToBytes(n uint32) (b []byte) {
b = make([]byte, 4)
for i := uint8(0); i < 4; i++ {
b[i] = byte(n >> (i << 3))
}
return
}
func bytesToUint64(b []byte) (n uint64) {
for i := uint8(0); i < 8; i++ {
n |= uint64(b[i]) << (i << 3)
}
return
}
func uint64ToBytes(n uint64) (b []byte) {
b = make([]byte, 8)
for i := uint8(0); i < 8; i++ {
b[i] = byte(n >> (i << 3))
}
return
}
func int64ToBytes(n int64) []byte {
return uint64ToBytes(uint64(n))
}
func bytesToFloat32(b []byte) float32 {
return math.Float32frombits(bytesToUint32(b))
}
func bytesToFloat64(b []byte) float64 {
return math.Float64frombits(bytesToUint64(b))
}
func float64ToBytes(f float64) []byte {
return uint64ToBytes(math.Float64bits(f))
}
func bytesToLengthCodedBinary(b []byte) (length uint64, n int, err error) {
switch {
// 0-250: value of first byte
case b[0] <= 250:
length = uint64(b[0])
n = 1
return
// 251: NULL
case b[0] == 251:
length = 0
n = 1
return
// 252: value of following 2
case b[0] == 252:
n = 3
// 253: value of following 3
case b[0] == 253:
n = 4
// 254: value of following 8
case b[0] == 254:
n = 9
}
if len(b) < n {
err = io.EOF
return
}
// get Length
tmp := make([]byte, 8)
copy(tmp, b[1:n])
length = bytesToUint64(tmp)
return
}
func lengthCodedBinaryToBytes(n uint64) (b []byte) {
switch {
case n <= 250:
b = []byte{byte(n)}
case n <= 0xffff:
b = []byte{0xfc, byte(n), byte(n >> 8)}
case n <= 0xffffff:
b = []byte{0xfd, byte(n), byte(n >> 8), byte(n >> 16)}
}
return
}
func intToByteStr(i int64) (b []byte) {
return strconv.AppendInt(b, i, 10)
}
func uintToByteStr(u uint64) (b []byte) {
return strconv.AppendUint(b, u, 10)
}
func float32ToByteStr(f float32) (b []byte) {
return strconv.AppendFloat(b, float64(f), 'f', -1, 32)
}
func float64ToByteStr(f float64) (b []byte) {
return strconv.AppendFloat(b, f, 'f', -1, 64)
}