utils.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. "crypto/sha1"
  12. "io"
  13. "log"
  14. "os"
  15. "regexp"
  16. "strings"
  17. )
  18. // Logger
  19. var (
  20. errLog *log.Logger
  21. )
  22. func init() {
  23. errLog = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
  24. dsnPattern = regexp.MustCompile(
  25. `^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
  26. `(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
  27. `\/(?P<dbname>.*?)` + // /dbname
  28. `(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
  29. }
  30. // Data Source Name Parser
  31. var dsnPattern *regexp.Regexp
  32. func parseDSN(dsn string) *config {
  33. cfg := new(config)
  34. cfg.params = make(map[string]string)
  35. matches := dsnPattern.FindStringSubmatch(dsn)
  36. names := dsnPattern.SubexpNames()
  37. for i, match := range matches {
  38. switch names[i] {
  39. case "user":
  40. cfg.user = match
  41. case "passwd":
  42. cfg.passwd = match
  43. case "net":
  44. cfg.net = match
  45. case "addr":
  46. cfg.addr = match
  47. case "dbname":
  48. cfg.dbname = match
  49. case "params":
  50. for _, v := range strings.Split(match, "&") {
  51. param := strings.SplitN(v, "=", 2)
  52. if len(param) != 2 {
  53. continue
  54. }
  55. cfg.params[param[0]] = param[1]
  56. }
  57. }
  58. }
  59. // Set default network if empty
  60. if cfg.net == "" {
  61. cfg.net = "tcp"
  62. }
  63. // Set default adress if empty
  64. if cfg.addr == "" {
  65. cfg.addr = "127.0.0.1:3306"
  66. }
  67. return cfg
  68. }
  69. // Encrypt password using 4.1+ method
  70. // http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#4.1_and_later
  71. func scramblePassword(scramble, password []byte) []byte {
  72. if len(password) == 0 {
  73. return nil
  74. }
  75. // stage1Hash = SHA1(password)
  76. crypt := sha1.New()
  77. crypt.Write(password)
  78. stage1Hash := crypt.Sum(nil)
  79. // scrambleHash = SHA1(scramble + SHA1(stage1Hash))
  80. // inner Hash
  81. crypt.Reset()
  82. crypt.Write(stage1Hash)
  83. scrambleHash := crypt.Sum(nil)
  84. // outer Hash
  85. crypt.Reset()
  86. crypt.Write(scramble)
  87. crypt.Write(scrambleHash)
  88. scrambleHash = crypt.Sum(nil)
  89. // token = scrambleHash XOR stage1Hash
  90. result := make([]byte, 20)
  91. for i := range result {
  92. result[i] = scrambleHash[i] ^ stage1Hash[i]
  93. }
  94. return result[0:]
  95. }
  96. /******************************************************************************
  97. * Convert from and to bytes *
  98. ******************************************************************************/
  99. func uint64ToBytes(n uint64) []byte {
  100. return []byte{
  101. byte(n),
  102. byte(n >> 8),
  103. byte(n >> 16),
  104. byte(n >> 24),
  105. byte(n >> 32),
  106. byte(n >> 40),
  107. byte(n >> 48),
  108. byte(n >> 56),
  109. }
  110. }
  111. func readLengthEnodedString(b []byte) ([]byte, int, error) {
  112. // Get length
  113. num, _, n := readLengthEncodedInteger(b)
  114. if num < 1 {
  115. return nil, n, nil
  116. }
  117. n += int(num)
  118. // Check data length
  119. if len(b) >= n {
  120. return b[n-int(num) : n], n, nil
  121. }
  122. return nil, n, io.EOF
  123. }
  124. func skipLengthEnodedString(b []byte) (int, error) {
  125. // Get length
  126. num, _, n := readLengthEncodedInteger(b)
  127. if num < 1 {
  128. return n, nil
  129. }
  130. n += int(num)
  131. // Check data length
  132. if len(b) >= n {
  133. return n, nil
  134. }
  135. return n, io.EOF
  136. }
  137. func readLengthEncodedInteger(b []byte) (num uint64, isNull bool, n int) {
  138. switch (b)[0] {
  139. // 251: NULL
  140. case 0xfb:
  141. n = 1
  142. isNull = true
  143. return
  144. // 252: value of following 2
  145. case 0xfc:
  146. n = 3
  147. // 253: value of following 3
  148. case 0xfd:
  149. n = 4
  150. // 254: value of following 8
  151. case 0xfe:
  152. n = 9
  153. // 0-250: value of first byte
  154. default:
  155. num = uint64(b[0])
  156. n = 1
  157. return
  158. }
  159. switch n - 1 {
  160. case 2:
  161. num = uint64(b[0]) | uint64(b[1])<<8
  162. return
  163. case 3:
  164. num = uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16
  165. return
  166. default:
  167. num = uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 |
  168. uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 |
  169. uint64(b[6])<<48 | uint64(b[7])<<54
  170. }
  171. return
  172. }
  173. func lengthEncodedIntegerToBytes(n uint64) []byte {
  174. switch {
  175. case n <= 250:
  176. return []byte{byte(n)}
  177. case n <= 0xffff:
  178. return []byte{0xfc, byte(n), byte(n >> 8)}
  179. case n <= 0xffffff:
  180. return []byte{0xfd, byte(n), byte(n >> 8), byte(n >> 16)}
  181. }
  182. return nil
  183. }