utils.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2012 Julien Schmidt. All rights reserved.
  4. // http://www.julienschmidt.com
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public
  7. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  8. // You can obtain one at http://mozilla.org/MPL/2.0/.
  9. package mysql
  10. import (
  11. "bytes"
  12. "crypto/sha1"
  13. "io"
  14. "math"
  15. "regexp"
  16. "strconv"
  17. "strings"
  18. )
  19. var dsnPattern *regexp.Regexp
  20. func init() {
  21. dsnPattern = regexp.MustCompile(
  22. `^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
  23. `(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
  24. `\/(?P<dbname>.*?)` + // /dbname
  25. `(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
  26. }
  27. func parseDSN(dsn string) *config {
  28. cfg := new(config)
  29. cfg.params = make(map[string]string)
  30. matches := dsnPattern.FindStringSubmatch(dsn)
  31. names := dsnPattern.SubexpNames()
  32. for i, match := range matches {
  33. switch names[i] {
  34. case "user":
  35. cfg.user = match
  36. case "passwd":
  37. cfg.passwd = match
  38. case "net":
  39. cfg.net = match
  40. case "addr":
  41. cfg.addr = match
  42. case "dbname":
  43. cfg.dbname = match
  44. case "params":
  45. for _, v := range strings.Split(match, "&") {
  46. param := strings.SplitN(v, "=", 2)
  47. if len(param) != 2 {
  48. continue
  49. }
  50. cfg.params[param[0]] = param[1]
  51. }
  52. }
  53. }
  54. // Set default network if empty
  55. if cfg.net == "" {
  56. cfg.net = "tcp"
  57. }
  58. // Set default adress if empty
  59. if cfg.addr == "" {
  60. cfg.addr = "127.0.0.1:3306"
  61. }
  62. return cfg
  63. }
  64. // Encrypt password using 4.1+ method
  65. // http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#4.1_and_later
  66. func scramblePassword(scramble, password []byte) (result []byte) {
  67. if len(password) == 0 {
  68. return
  69. }
  70. // stage1Hash = SHA1(password)
  71. crypt := sha1.New()
  72. crypt.Write(password)
  73. stage1Hash := crypt.Sum(nil)
  74. // scrambleHash = SHA1(scramble + SHA1(stage1Hash))
  75. // inner Hash
  76. crypt.Reset()
  77. crypt.Write(stage1Hash)
  78. scrambleHash := crypt.Sum(nil)
  79. // outer Hash
  80. crypt.Reset()
  81. crypt.Write(scramble)
  82. crypt.Write(scrambleHash)
  83. scrambleHash = crypt.Sum(nil)
  84. // token = scrambleHash XOR stage1Hash
  85. result = make([]byte, 20)
  86. for i := range result {
  87. result[i] = scrambleHash[i] ^ stage1Hash[i]
  88. }
  89. return
  90. }
  91. /******************************************************************************
  92. * Read data-types from bytes *
  93. ******************************************************************************/
  94. // Read a slice from the data slice
  95. func readSlice(data []byte, delim byte) (slice []byte, e error) {
  96. pos := bytes.IndexByte(data, delim)
  97. if pos > -1 {
  98. slice = data[:pos]
  99. } else {
  100. slice = data
  101. e = io.EOF
  102. }
  103. return
  104. }
  105. func readLengthCodedBinary(data []byte) (b []byte, n int, isNull bool, e error) {
  106. // Get length
  107. num, n, e := bytesToLengthCodedBinary(data)
  108. if e != nil {
  109. return
  110. }
  111. // Check data length
  112. if len(data) < n+int(num) {
  113. e = io.EOF
  114. return
  115. }
  116. // Check if null
  117. if data[0] == 251 {
  118. isNull = true
  119. } else {
  120. isNull = false
  121. }
  122. // Get bytes
  123. b = data[n : n+int(num)]
  124. n += int(num)
  125. return
  126. }
  127. /******************************************************************************
  128. * Convert from and to bytes *
  129. ******************************************************************************/
  130. func byteToUint8(b byte) (n uint8) {
  131. n |= uint8(b)
  132. return
  133. }
  134. func bytesToUint16(b []byte) (n uint16) {
  135. n |= uint16(b[0])
  136. n |= uint16(b[1]) << 8
  137. return
  138. }
  139. func uint24ToBytes(n uint32) (b []byte) {
  140. b = make([]byte, 3)
  141. for i := uint8(0); i < 3; i++ {
  142. b[i] = byte(n >> (i * 8))
  143. }
  144. return
  145. }
  146. func bytesToUint32(b []byte) (n uint32) {
  147. for i := uint8(0); i < 4; i++ {
  148. n |= uint32(b[i]) << (i * 8)
  149. }
  150. return
  151. }
  152. func uint32ToBytes(n uint32) (b []byte) {
  153. b = make([]byte, 4)
  154. for i := uint8(0); i < 4; i++ {
  155. b[i] = byte(n >> (i * 8))
  156. }
  157. return
  158. }
  159. func bytesToUint64(b []byte) (n uint64) {
  160. for i := uint8(0); i < 8; i++ {
  161. n |= uint64(b[i]) << (i * 8)
  162. }
  163. return
  164. }
  165. func uint64ToBytes(n uint64) (b []byte) {
  166. b = make([]byte, 8)
  167. for i := uint8(0); i < 8; i++ {
  168. b[i] = byte(n >> (i * 8))
  169. }
  170. return
  171. }
  172. func int64ToBytes(n int64) []byte {
  173. return uint64ToBytes(uint64(n))
  174. }
  175. func bytesToFloat32(b []byte) float32 {
  176. return math.Float32frombits(bytesToUint32(b))
  177. }
  178. func bytesToFloat64(b []byte) float64 {
  179. return math.Float64frombits(bytesToUint64(b))
  180. }
  181. func bytesToLengthCodedBinary(b []byte) (length uint64, n int, e error) {
  182. switch {
  183. // 0-250: value of first byte
  184. case b[0] <= 250:
  185. length = uint64(b[0])
  186. n = 1
  187. return
  188. // 251: NULL
  189. case b[0] == 251:
  190. length = 0
  191. n = 1
  192. return
  193. // 252: value of following 2
  194. case b[0] == 252:
  195. n = 3
  196. // 253: value of following 3
  197. case b[0] == 253:
  198. n = 4
  199. // 254: value of following 8
  200. case b[0] == 254:
  201. n = 9
  202. }
  203. if len(b) < n {
  204. e = io.EOF
  205. return
  206. }
  207. // get Length
  208. tmp := make([]byte, 8)
  209. copy(tmp, b[1:n])
  210. length = bytesToUint64(tmp)
  211. return
  212. }
  213. func lengthCodedBinaryToBytes(n uint64) (b []byte) {
  214. switch {
  215. case n <= 250:
  216. b = []byte{byte(n)}
  217. case n <= 0xffff:
  218. b = []byte{0xfc, byte(n), byte(n >> 8)}
  219. case n <= 0xffffff:
  220. b = []byte{0xfd, byte(n), byte(n >> 8), byte(n >> 16)}
  221. }
  222. return
  223. }
  224. func intToByteStr(i int64) (d *[]byte) {
  225. tmp := make([]byte, 0)
  226. tmp = strconv.AppendInt(tmp, i, 10)
  227. return &tmp
  228. }
  229. func uintToByteStr(u uint64) (d *[]byte) {
  230. tmp := make([]byte, 0)
  231. tmp = strconv.AppendUint(tmp, u, 10)
  232. return &tmp
  233. }
  234. func float32ToByteStr(f float32) (d *[]byte) {
  235. tmp := make([]byte, 0)
  236. tmp = strconv.AppendFloat(tmp, float64(f), 'f', -1, 32)
  237. return &tmp
  238. }
  239. func float64ToByteStr(f float64) (d *[]byte) {
  240. tmp := make([]byte, 0)
  241. tmp = strconv.AppendFloat(tmp, f, 'f', -1, 64)
  242. return &tmp
  243. }