forked from go-sql-driver/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
279 lines (232 loc) · 5.83 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
// 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"
"encoding/binary"
"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 readLengthEnodedString(data []byte) ([]byte, bool, int, error) {
// Get length
num, isNull, n, err := bytesToLengthEncodedInteger(data)
if err != nil || isNull {
return nil, isNull, n, err
}
// Check data length
if len(data) < n+int(num) {
return nil, true, n, io.EOF
}
return data[n : n+int(num)], isNull, n + int(num), err
}
func readAndDropLengthEnodedString(data []byte) (n int, err error) {
// Get length
num, _, n, err := bytesToLengthEncodedInteger(data)
if err != nil || num < 1 {
return n, err
}
// Check data length
if len(data) < n+int(num) {
return n, io.EOF
}
return n + int(num), err
}
/******************************************************************************
* Convert from and to bytes *
******************************************************************************/
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 uint32ToBytes(n uint32) (b []byte) {
b = make([]byte, 4)
for i := uint8(0); i < 4; i++ {
b[i] = byte(n >> (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(binary.LittleEndian.Uint32(b))
}
func bytesToFloat64(b []byte) float64 {
return math.Float64frombits(binary.LittleEndian.Uint64(b))
}
func float64ToBytes(f float64) []byte {
return uint64ToBytes(math.Float64bits(f))
}
func bytesToLengthEncodedInteger(b []byte) (num uint64, isNull bool, n int, err error) {
switch b[0] {
// 251: NULL
case 0xfb:
n = 1
isNull = true
return
// 252: value of following 2
case 0xfc:
n = 3
// 253: value of following 3
case 0xfd:
n = 4
// 254: value of following 8
case 0xfe:
n = 9
// 0-250: value of first byte
default:
num = uint64(b[0])
n = 1
return
}
switch n - 1 {
case 2:
num = uint64(b[0]) | uint64(b[1])<<8
return
case 3:
num = uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16
return
default:
num = uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 |
uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 |
uint64(b[6])<<48 | uint64(b[7])<<54
}
return
}
func lengthEncodedIntegerToBytes(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)
}