utils.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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
  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 uint64ToString(n uint64) []byte {
  112. var a [20]byte
  113. i := 20
  114. // U+0030 = 0
  115. // ...
  116. // U+0039 = 9
  117. var q uint64
  118. for n >= 10 {
  119. i--
  120. q = n / 10
  121. a[i] = uint8(n-q*10) + 0x30
  122. n = q
  123. }
  124. i--
  125. a[i] = uint8(n) + 0x30
  126. return a[i:]
  127. }
  128. func readLengthEnodedString(b []byte) ([]byte, bool, int, error) {
  129. // Get length
  130. num, isNull, n := readLengthEncodedInteger(b)
  131. if num < 1 {
  132. return nil, isNull, n, nil
  133. }
  134. n += int(num)
  135. // Check data length
  136. if len(b) >= n {
  137. return b[n-int(num) : n], false, n, nil
  138. }
  139. return nil, false, n, io.EOF
  140. }
  141. func skipLengthEnodedString(b []byte) (int, error) {
  142. // Get length
  143. num, _, n := readLengthEncodedInteger(b)
  144. if num < 1 {
  145. return n, nil
  146. }
  147. n += int(num)
  148. // Check data length
  149. if len(b) >= n {
  150. return n, nil
  151. }
  152. return n, io.EOF
  153. }
  154. func readLengthEncodedInteger(b []byte) (num uint64, isNull bool, n int) {
  155. switch b[0] {
  156. // 251: NULL
  157. case 0xfb:
  158. n = 1
  159. isNull = true
  160. return
  161. // 252: value of following 2
  162. case 0xfc:
  163. n = 3
  164. // 253: value of following 3
  165. case 0xfd:
  166. n = 4
  167. // 254: value of following 8
  168. case 0xfe:
  169. n = 9
  170. // 0-250: value of first byte
  171. default:
  172. num = uint64(b[0])
  173. n = 1
  174. return
  175. }
  176. switch n - 1 {
  177. case 2:
  178. num = uint64(b[1]) | uint64(b[2])<<8
  179. return
  180. case 3:
  181. num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16
  182. return
  183. default:
  184. num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16 |
  185. uint64(b[4])<<24 | uint64(b[5])<<32 | uint64(b[6])<<40 |
  186. uint64(b[7])<<48 | uint64(b[8])<<54
  187. }
  188. return
  189. }
  190. func lengthEncodedIntegerToBytes(n uint64) []byte {
  191. switch {
  192. case n <= 250:
  193. return []byte{byte(n)}
  194. case n <= 0xffff:
  195. return []byte{0xfc, byte(n), byte(n >> 8)}
  196. case n <= 0xffffff:
  197. return []byte{0xfd, byte(n), byte(n >> 8), byte(n >> 16)}
  198. }
  199. return nil
  200. }