utils.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. // Time Location
  112. case "loc":
  113. cfg.loc, err = time.LoadLocation(value)
  114. if err != nil {
  115. return
  116. }
  117. // Dial Timeout
  118. case "timeout":
  119. cfg.timeout, err = time.ParseDuration(value)
  120. if err != nil {
  121. return
  122. }
  123. // TLS-Encryption
  124. case "tls":
  125. if readBool(value) {
  126. cfg.tls = &tls.Config{}
  127. } else if strings.ToLower(value) == "skip-verify" {
  128. cfg.tls = &tls.Config{InsecureSkipVerify: true}
  129. }
  130. default:
  131. cfg.params[param[0]] = value
  132. }
  133. }
  134. }
  135. }
  136. // Set default network if empty
  137. if cfg.net == "" {
  138. cfg.net = "tcp"
  139. }
  140. // Set default adress if empty
  141. if cfg.addr == "" {
  142. cfg.addr = "127.0.0.1:3306"
  143. }
  144. // Set default location if not set
  145. if cfg.loc == nil {
  146. cfg.loc = time.UTC
  147. }
  148. return
  149. }
  150. // Encrypt password using 4.1+ method
  151. // http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#4.1_and_later
  152. func scramblePassword(scramble, password []byte) []byte {
  153. if len(password) == 0 {
  154. return nil
  155. }
  156. // stage1Hash = SHA1(password)
  157. crypt := sha1.New()
  158. crypt.Write(password)
  159. stage1 := crypt.Sum(nil)
  160. // scrambleHash = SHA1(scramble + SHA1(stage1Hash))
  161. // inner Hash
  162. crypt.Reset()
  163. crypt.Write(stage1)
  164. hash := crypt.Sum(nil)
  165. // outer Hash
  166. crypt.Reset()
  167. crypt.Write(scramble)
  168. crypt.Write(hash)
  169. scramble = crypt.Sum(nil)
  170. // token = scrambleHash XOR stage1Hash
  171. for i := range scramble {
  172. scramble[i] ^= stage1[i]
  173. }
  174. return scramble
  175. }
  176. func parseDateTime(str string, loc *time.Location) (t time.Time, err error) {
  177. switch len(str) {
  178. case 10: // YYYY-MM-DD
  179. if str == "0000-00-00" {
  180. return
  181. }
  182. t, err = time.Parse(timeFormat[:10], str)
  183. case 19: // YYYY-MM-DD HH:MM:SS
  184. if str == "0000-00-00 00:00:00" {
  185. return
  186. }
  187. t, err = time.Parse(timeFormat, str)
  188. default:
  189. err = fmt.Errorf("Invalid Time-String: %s", str)
  190. return
  191. }
  192. // Adjust location
  193. if err == nil && loc != time.UTC {
  194. y, mo, d := t.Date()
  195. h, mi, s := t.Clock()
  196. t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
  197. }
  198. return
  199. }
  200. func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Value, error) {
  201. switch num {
  202. case 0:
  203. return time.Time{}, nil
  204. case 4:
  205. return time.Date(
  206. int(binary.LittleEndian.Uint16(data[:2])), // year
  207. time.Month(data[2]), // month
  208. int(data[3]), // day
  209. 0, 0, 0, 0,
  210. loc,
  211. ), nil
  212. case 7:
  213. return time.Date(
  214. int(binary.LittleEndian.Uint16(data[:2])), // year
  215. time.Month(data[2]), // month
  216. int(data[3]), // day
  217. int(data[4]), // hour
  218. int(data[5]), // minutes
  219. int(data[6]), // seconds
  220. 0,
  221. loc,
  222. ), nil
  223. case 11:
  224. return time.Date(
  225. int(binary.LittleEndian.Uint16(data[:2])), // year
  226. time.Month(data[2]), // month
  227. int(data[3]), // day
  228. int(data[4]), // hour
  229. int(data[5]), // minutes
  230. int(data[6]), // seconds
  231. int(binary.LittleEndian.Uint32(data[7:11]))*1000, // nanoseconds
  232. loc,
  233. ), nil
  234. }
  235. return nil, fmt.Errorf("Invalid DATETIME-packet length %d", num)
  236. }
  237. func formatBinaryDate(num uint64, data []byte) (driver.Value, error) {
  238. switch num {
  239. case 0:
  240. return []byte("0000-00-00"), nil
  241. case 4:
  242. return []byte(fmt.Sprintf(
  243. "%04d-%02d-%02d",
  244. binary.LittleEndian.Uint16(data[:2]),
  245. data[2],
  246. data[3],
  247. )), nil
  248. }
  249. return nil, fmt.Errorf("Invalid DATE-packet length %d", num)
  250. }
  251. func formatBinaryDateTime(num uint64, data []byte) (driver.Value, error) {
  252. switch num {
  253. case 0:
  254. return []byte("0000-00-00 00:00:00"), nil
  255. case 4:
  256. return []byte(fmt.Sprintf(
  257. "%04d-%02d-%02d 00:00:00",
  258. binary.LittleEndian.Uint16(data[:2]),
  259. data[2],
  260. data[3],
  261. )), nil
  262. case 7:
  263. return []byte(fmt.Sprintf(
  264. "%04d-%02d-%02d %02d:%02d:%02d",
  265. binary.LittleEndian.Uint16(data[:2]),
  266. data[2],
  267. data[3],
  268. data[4],
  269. data[5],
  270. data[6],
  271. )), nil
  272. case 11:
  273. return []byte(fmt.Sprintf(
  274. "%04d-%02d-%02d %02d:%02d:%02d.%06d",
  275. binary.LittleEndian.Uint16(data[:2]),
  276. data[2],
  277. data[3],
  278. data[4],
  279. data[5],
  280. data[6],
  281. binary.LittleEndian.Uint32(data[7:11]),
  282. )), nil
  283. }
  284. return nil, fmt.Errorf("Invalid DATETIME-packet length %d", num)
  285. }
  286. func readBool(value string) bool {
  287. switch strings.ToLower(value) {
  288. case "true":
  289. return true
  290. case "1":
  291. return true
  292. }
  293. return false
  294. }
  295. /******************************************************************************
  296. * Convert from and to bytes *
  297. ******************************************************************************/
  298. func uint64ToBytes(n uint64) []byte {
  299. return []byte{
  300. byte(n),
  301. byte(n >> 8),
  302. byte(n >> 16),
  303. byte(n >> 24),
  304. byte(n >> 32),
  305. byte(n >> 40),
  306. byte(n >> 48),
  307. byte(n >> 56),
  308. }
  309. }
  310. func uint64ToString(n uint64) []byte {
  311. var a [20]byte
  312. i := 20
  313. // U+0030 = 0
  314. // ...
  315. // U+0039 = 9
  316. var q uint64
  317. for n >= 10 {
  318. i--
  319. q = n / 10
  320. a[i] = uint8(n-q*10) + 0x30
  321. n = q
  322. }
  323. i--
  324. a[i] = uint8(n) + 0x30
  325. return a[i:]
  326. }
  327. // treats string value as unsigned integer representation
  328. func stringToInt(b []byte) int {
  329. val := 0
  330. for i := range b {
  331. val *= 10
  332. val += int(b[i] - 0x30)
  333. }
  334. return val
  335. }
  336. func readLengthEnodedString(b []byte) ([]byte, bool, int, error) {
  337. // Get length
  338. num, isNull, n := readLengthEncodedInteger(b)
  339. if num < 1 {
  340. return nil, isNull, n, nil
  341. }
  342. n += int(num)
  343. // Check data length
  344. if len(b) >= n {
  345. return b[n-int(num) : n], false, n, nil
  346. }
  347. return nil, false, n, io.EOF
  348. }
  349. func skipLengthEnodedString(b []byte) (int, error) {
  350. // Get length
  351. num, _, n := readLengthEncodedInteger(b)
  352. if num < 1 {
  353. return n, nil
  354. }
  355. n += int(num)
  356. // Check data length
  357. if len(b) >= n {
  358. return n, nil
  359. }
  360. return n, io.EOF
  361. }
  362. func readLengthEncodedInteger(b []byte) (num uint64, isNull bool, n int) {
  363. switch b[0] {
  364. // 251: NULL
  365. case 0xfb:
  366. n = 1
  367. isNull = true
  368. return
  369. // 252: value of following 2
  370. case 0xfc:
  371. num = uint64(b[1]) | uint64(b[2])<<8
  372. n = 3
  373. return
  374. // 253: value of following 3
  375. case 0xfd:
  376. num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16
  377. n = 4
  378. return
  379. // 254: value of following 8
  380. case 0xfe:
  381. num = uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16 |
  382. uint64(b[4])<<24 | uint64(b[5])<<32 | uint64(b[6])<<40 |
  383. uint64(b[7])<<48 | uint64(b[8])<<54
  384. n = 9
  385. return
  386. }
  387. // 0-250: value of first byte
  388. num = uint64(b[0])
  389. n = 1
  390. return
  391. }
  392. func lengthEncodedIntegerToBytes(n uint64) []byte {
  393. switch {
  394. case n <= 250:
  395. return []byte{byte(n)}
  396. case n <= 0xffff:
  397. return []byte{0xfc, byte(n), byte(n >> 8)}
  398. case n <= 0xffffff:
  399. return []byte{0xfd, byte(n), byte(n >> 8), byte(n >> 16)}
  400. }
  401. return nil
  402. }