utils.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. "crypto/tls"
  13. "database/sql/driver"
  14. "encoding/binary"
  15. "fmt"
  16. "io"
  17. "log"
  18. "os"
  19. "regexp"
  20. "strings"
  21. "time"
  22. )
  23. // NullTime represents a time.Time that may be NULL.
  24. // NullTime implements the Scanner interface so
  25. // it can be used as a scan destination:
  26. //
  27. // var nt NullTime
  28. // err := db.QueryRow("SELECT time FROM foo WHERE id=?", id).Scan(&nt)
  29. // ...
  30. // if nt.Valid {
  31. // // use nt.Time
  32. // } else {
  33. // // NULL value
  34. // }
  35. //
  36. // This NullTime implementation is not driver-specific
  37. type NullTime struct {
  38. Time time.Time
  39. Valid bool // Valid is true if Time is not NULL
  40. }
  41. // Scan implements the Scanner interface.
  42. // The value type must be time.Time or string / []byte (formatted time-string),
  43. // otherwise Scan fails.
  44. func (nt *NullTime) Scan(value interface{}) (err error) {
  45. if value == nil {
  46. nt.Time, nt.Valid = time.Time{}, false
  47. return
  48. }
  49. switch v := value.(type) {
  50. case time.Time:
  51. nt.Time, nt.Valid = v, true
  52. return
  53. case []byte:
  54. nt.Time, err = parseDateTime(string(v), time.UTC)
  55. nt.Valid = (err == nil)
  56. return
  57. case string:
  58. nt.Time, err = parseDateTime(v, time.UTC)
  59. nt.Valid = (err == nil)
  60. return
  61. }
  62. nt.Valid = false
  63. return fmt.Errorf("Can't convert %T to time.Time", value)
  64. }
  65. // Value implements the driver Valuer interface.
  66. func (nt NullTime) Value() (driver.Value, error) {
  67. if !nt.Valid {
  68. return nil, nil
  69. }
  70. return nt.Time, nil
  71. }
  72. // Logger
  73. var (
  74. errLog *log.Logger
  75. )
  76. func init() {
  77. errLog = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
  78. dsnPattern = regexp.MustCompile(
  79. `^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
  80. `(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
  81. `\/(?P<dbname>.*?)` + // /dbname
  82. `(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
  83. }
  84. // Data Source Name Parser
  85. var dsnPattern *regexp.Regexp
  86. func parseDSN(dsn string) (cfg *config, err error) {
  87. cfg = new(config)
  88. cfg.params = make(map[string]string)
  89. matches := dsnPattern.FindStringSubmatch(dsn)
  90. names := dsnPattern.SubexpNames()
  91. for i, match := range matches {
  92. switch names[i] {
  93. case "user":
  94. cfg.user = match
  95. case "passwd":
  96. cfg.passwd = match
  97. case "net":
  98. cfg.net = match
  99. case "addr":
  100. cfg.addr = match
  101. case "dbname":
  102. cfg.dbname = match
  103. case "params":
  104. for _, v := range strings.Split(match, "&") {
  105. param := strings.SplitN(v, "=", 2)
  106. if len(param) != 2 {
  107. continue
  108. }
  109. // cfg params
  110. switch value := param[1]; param[0] {
  111. // Disable INFILE whitelist / enable all files
  112. case "allowAllFiles":
  113. cfg.allowAllFiles = readBool(value)
  114. // Switch "rowsAffected" mode
  115. case "clientFoundRows":
  116. cfg.clientFoundRows = readBool(value)
  117. // Time Location
  118. case "loc":
  119. cfg.loc, err = time.LoadLocation(value)
  120. if err != nil {
  121. return
  122. }
  123. // Dial Timeout
  124. case "timeout":
  125. cfg.timeout, err = time.ParseDuration(value)
  126. if err != nil {
  127. return
  128. }
  129. // TLS-Encryption
  130. case "tls":
  131. if readBool(value) {
  132. cfg.tls = &tls.Config{}
  133. } else if strings.ToLower(value) == "skip-verify" {
  134. cfg.tls = &tls.Config{InsecureSkipVerify: true}
  135. } else if tlsConfig, ok := tlsConfigMap[value]; ok {
  136. cfg.tls = &tls.Config{}
  137. *cfg.tls = *tlsConfig
  138. }
  139. default:
  140. cfg.params[param[0]] = value
  141. }
  142. }
  143. }
  144. }
  145. // Set default network if empty
  146. if cfg.net == "" {
  147. cfg.net = "tcp"
  148. }
  149. // Set default adress if empty
  150. if cfg.addr == "" {
  151. cfg.addr = "127.0.0.1:3306"
  152. }
  153. // Set default location if not set
  154. if cfg.loc == nil {
  155. cfg.loc = time.UTC
  156. }
  157. return
  158. }
  159. // Encrypt password using 4.1+ method
  160. // http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#4.1_and_later
  161. func scramblePassword(scramble, password []byte) []byte {
  162. if len(password) == 0 {
  163. return nil
  164. }
  165. // stage1Hash = SHA1(password)
  166. crypt := sha1.New()
  167. crypt.Write(password)
  168. stage1 := crypt.Sum(nil)
  169. // scrambleHash = SHA1(scramble + SHA1(stage1Hash))
  170. // inner Hash
  171. crypt.Reset()
  172. crypt.Write(stage1)
  173. hash := crypt.Sum(nil)
  174. // outer Hash
  175. crypt.Reset()
  176. crypt.Write(scramble)
  177. crypt.Write(hash)
  178. scramble = crypt.Sum(nil)
  179. // token = scrambleHash XOR stage1Hash
  180. for i := range scramble {
  181. scramble[i] ^= stage1[i]
  182. }
  183. return scramble
  184. }
  185. func parseDateTime(str string, loc *time.Location) (t time.Time, err error) {
  186. switch len(str) {
  187. case 10: // YYYY-MM-DD
  188. if str == "0000-00-00" {
  189. return
  190. }
  191. t, err = time.Parse(timeFormat[:10], str)
  192. case 19: // YYYY-MM-DD HH:MM:SS
  193. if str == "0000-00-00 00:00:00" {
  194. return
  195. }
  196. t, err = time.Parse(timeFormat, str)
  197. default:
  198. err = fmt.Errorf("Invalid Time-String: %s", str)
  199. return
  200. }
  201. // Adjust location
  202. if err == nil && loc != time.UTC {
  203. y, mo, d := t.Date()
  204. h, mi, s := t.Clock()
  205. t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
  206. }
  207. return
  208. }
  209. func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Value, error) {
  210. switch num {
  211. case 0:
  212. return time.Time{}, nil
  213. case 4:
  214. return time.Date(
  215. int(binary.LittleEndian.Uint16(data[:2])), // year
  216. time.Month(data[2]), // month
  217. int(data[3]), // day
  218. 0, 0, 0, 0,
  219. loc,
  220. ), nil
  221. case 7:
  222. return time.Date(
  223. int(binary.LittleEndian.Uint16(data[:2])), // year
  224. time.Month(data[2]), // month
  225. int(data[3]), // day
  226. int(data[4]), // hour
  227. int(data[5]), // minutes
  228. int(data[6]), // seconds
  229. 0,
  230. loc,
  231. ), nil
  232. case 11:
  233. return time.Date(
  234. int(binary.LittleEndian.Uint16(data[:2])), // year
  235. time.Month(data[2]), // month
  236. int(data[3]), // day
  237. int(data[4]), // hour
  238. int(data[5]), // minutes
  239. int(data[6]), // seconds
  240. int(binary.LittleEndian.Uint32(data[7:11]))*1000, // nanoseconds
  241. loc,
  242. ), nil
  243. }
  244. return nil, fmt.Errorf("Invalid DATETIME-packet length %d", num)
  245. }
  246. func formatBinaryDate(num uint64, data []byte) (driver.Value, error) {
  247. switch num {
  248. case 0:
  249. return []byte("0000-00-00"), nil
  250. case 4:
  251. return []byte(fmt.Sprintf(
  252. "%04d-%02d-%02d",
  253. binary.LittleEndian.Uint16(data[:2]),
  254. data[2],
  255. data[3],
  256. )), nil
  257. }
  258. return nil, fmt.Errorf("Invalid DATE-packet length %d", num)
  259. }
  260. func formatBinaryDateTime(num uint64, data []byte) (driver.Value, error) {
  261. switch num {
  262. case 0:
  263. return []byte("0000-00-00 00:00:00"), nil
  264. case 4:
  265. return []byte(fmt.Sprintf(
  266. "%04d-%02d-%02d 00:00:00",
  267. binary.LittleEndian.Uint16(data[:2]),
  268. data[2],
  269. data[3],
  270. )), nil
  271. case 7:
  272. return []byte(fmt.Sprintf(
  273. "%04d-%02d-%02d %02d:%02d:%02d",
  274. binary.LittleEndian.Uint16(data[:2]),
  275. data[2],
  276. data[3],
  277. data[4],
  278. data[5],
  279. data[6],
  280. )), nil
  281. case 11:
  282. return []byte(fmt.Sprintf(
  283. "%04d-%02d-%02d %02d:%02d:%02d.%06d",
  284. binary.LittleEndian.Uint16(data[:2]),
  285. data[2],
  286. data[3],
  287. data[4],
  288. data[5],
  289. data[6],
  290. binary.LittleEndian.Uint32(data[7:11]),
  291. )), nil
  292. }
  293. return nil, fmt.Errorf("Invalid DATETIME-packet length %d", num)
  294. }
  295. func readBool(value string) bool {
  296. switch strings.ToLower(value) {
  297. case "true":
  298. return true
  299. case "1":
  300. return true
  301. }
  302. return false
  303. }
  304. /******************************************************************************
  305. * Convert from and to bytes *
  306. ******************************************************************************/
  307. func uint64ToBytes(n uint64) []byte {
  308. return []byte{
  309. byte(n),
  310. byte(n >> 8),
  311. byte(n >> 16),
  312. byte(n >> 24),
  313. byte(n >> 32),
  314. byte(n >> 40),
  315. byte(n >> 48),
  316. byte(n >> 56),
  317. }
  318. }
  319. func uint64ToString(n uint64) []byte {
  320. var a [20]byte
  321. i := 20
  322. // U+0030 = 0
  323. // ...
  324. // U+0039 = 9
  325. var q uint64
  326. for n >= 10 {
  327. i--
  328. q = n / 10
  329. a[i] = uint8(n-q*10) + 0x30
  330. n = q
  331. }
  332. i--
  333. a[i] = uint8(n) + 0x30
  334. return a[i:]
  335. }
  336. // treats string value as unsigned integer representation
  337. func stringToInt(b []byte) int {
  338. val := 0
  339. for i := range b {
  340. val *= 10
  341. val += int(b[i] - 0x30)
  342. }
  343. return val
  344. }
  345. func readLengthEnodedString(b []byte) ([]byte, bool, int, error) {
  346. // Get length
  347. num, isNull, n := readLengthEncodedInteger(b)
  348. if num < 1 {
  349. return nil, isNull, n, nil
  350. }
  351. n += int(num)
  352. // Check data length
  353. if len(b) >= n {
  354. return b[n-int(num) : n], false, n, nil
  355. }
  356. return nil, false, n, io.EOF
  357. }
  358. func skipLengthEnodedString(b []byte) (int, error) {
  359. // Get length
  360. num, _, n := readLengthEncodedInteger(b)
  361. if num < 1 {
  362. return n, nil
  363. }
  364. n += int(num)
  365. // Check data length
  366. if len(b) >= n {
  367. return n, nil
  368. }
  369. return n, io.EOF
  370. }
  371. func readLengthEncodedInteger(b []byte) (num uint64, isNull bool, n int) {
  372. switch b[0] {
  373. // 251: NULL
  374. case 0xfb:
  375. n = 1
  376. isNull = true
  377. return
  378. // 252: value of following 2
  379. case 0xfc:
  380. num = uint64(b[1]) | uint64(b[2])<<8
  381. n = 3
  382. return
  383. // 253: value of following 3
  384. case 0xfd:
  385. num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16
  386. n = 4
  387. return
  388. // 254: value of following 8
  389. case 0xfe:
  390. num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16 |
  391. uint64(b[4])<<24 | uint64(b[5])<<32 | uint64(b[6])<<40 |
  392. uint64(b[7])<<48 | uint64(b[8])<<54
  393. n = 9
  394. return
  395. }
  396. // 0-250: value of first byte
  397. num = uint64(b[0])
  398. n = 1
  399. return
  400. }
  401. func lengthEncodedIntegerToBytes(n uint64) []byte {
  402. switch {
  403. case n <= 250:
  404. return []byte{byte(n)}
  405. case n <= 0xffff:
  406. return []byte{0xfc, byte(n), byte(n >> 8)}
  407. case n <= 0xffffff:
  408. return []byte{0xfd, byte(n), byte(n >> 8), byte(n >> 16)}
  409. }
  410. return nil
  411. }