utils.go 9.8 KB

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