utils.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2012 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "crypto/sha1"
  11. "crypto/tls"
  12. "database/sql/driver"
  13. "encoding/binary"
  14. "errors"
  15. "fmt"
  16. "io"
  17. "net"
  18. "net/url"
  19. "strings"
  20. "time"
  21. )
  22. var (
  23. tlsConfigRegister map[string]*tls.Config // Register for custom tls.Configs
  24. errInvalidDSNUnescaped = errors.New("Invalid DSN: Did you forget to escape a param value?")
  25. errInvalidDSNAddr = errors.New("Invalid DSN: Network Address not terminated (missing closing brace)")
  26. errInvalidDSNNoSlash = errors.New("Invalid DSN: Missing the slash separating the database name")
  27. errInvalidDSNUnsafeCollation = errors.New("Invalid DSN: interpolateParams can be used with ascii, latin1, utf8 and utf8mb4 charset")
  28. )
  29. func init() {
  30. tlsConfigRegister = make(map[string]*tls.Config)
  31. }
  32. // RegisterTLSConfig registers a custom tls.Config to be used with sql.Open.
  33. // Use the key as a value in the DSN where tls=value.
  34. //
  35. // rootCertPool := x509.NewCertPool()
  36. // pem, err := ioutil.ReadFile("/path/ca-cert.pem")
  37. // if err != nil {
  38. // log.Fatal(err)
  39. // }
  40. // if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
  41. // log.Fatal("Failed to append PEM.")
  42. // }
  43. // clientCert := make([]tls.Certificate, 0, 1)
  44. // certs, err := tls.LoadX509KeyPair("/path/client-cert.pem", "/path/client-key.pem")
  45. // if err != nil {
  46. // log.Fatal(err)
  47. // }
  48. // clientCert = append(clientCert, certs)
  49. // mysql.RegisterTLSConfig("custom", &tls.Config{
  50. // RootCAs: rootCertPool,
  51. // Certificates: clientCert,
  52. // })
  53. // db, err := sql.Open("mysql", "user@tcp(localhost:3306)/test?tls=custom")
  54. //
  55. func RegisterTLSConfig(key string, config *tls.Config) error {
  56. if _, isBool := readBool(key); isBool || strings.ToLower(key) == "skip-verify" {
  57. return fmt.Errorf("Key '%s' is reserved", key)
  58. }
  59. tlsConfigRegister[key] = config
  60. return nil
  61. }
  62. // DeregisterTLSConfig removes the tls.Config associated with key.
  63. func DeregisterTLSConfig(key string) {
  64. delete(tlsConfigRegister, key)
  65. }
  66. // parseDSN parses the DSN string to a config
  67. func parseDSN(dsn string) (cfg *config, err error) {
  68. // New config with some default values
  69. cfg = &config{
  70. loc: time.UTC,
  71. collation: defaultCollation,
  72. }
  73. // TODO: use strings.IndexByte when we can depend on Go 1.2
  74. // [user[:password]@][net[(addr)]]/dbname[?param1=value1&paramN=valueN]
  75. // Find the last '/' (since the password or the net addr might contain a '/')
  76. foundSlash := false
  77. for i := len(dsn) - 1; i >= 0; i-- {
  78. if dsn[i] == '/' {
  79. foundSlash = true
  80. var j, k int
  81. // left part is empty if i <= 0
  82. if i > 0 {
  83. // [username[:password]@][protocol[(address)]]
  84. // Find the last '@' in dsn[:i]
  85. for j = i; j >= 0; j-- {
  86. if dsn[j] == '@' {
  87. // username[:password]
  88. // Find the first ':' in dsn[:j]
  89. for k = 0; k < j; k++ {
  90. if dsn[k] == ':' {
  91. cfg.passwd = dsn[k+1 : j]
  92. break
  93. }
  94. }
  95. cfg.user = dsn[:k]
  96. break
  97. }
  98. }
  99. // [protocol[(address)]]
  100. // Find the first '(' in dsn[j+1:i]
  101. for k = j + 1; k < i; k++ {
  102. if dsn[k] == '(' {
  103. // dsn[i-1] must be == ')' if an address is specified
  104. if dsn[i-1] != ')' {
  105. if strings.ContainsRune(dsn[k+1:i], ')') {
  106. return nil, errInvalidDSNUnescaped
  107. }
  108. return nil, errInvalidDSNAddr
  109. }
  110. cfg.addr = dsn[k+1 : i-1]
  111. break
  112. }
  113. }
  114. cfg.net = dsn[j+1 : k]
  115. }
  116. // dbname[?param1=value1&...&paramN=valueN]
  117. // Find the first '?' in dsn[i+1:]
  118. for j = i + 1; j < len(dsn); j++ {
  119. if dsn[j] == '?' {
  120. if err = parseDSNParams(cfg, dsn[j+1:]); err != nil {
  121. return
  122. }
  123. break
  124. }
  125. }
  126. cfg.dbname = dsn[i+1 : j]
  127. break
  128. }
  129. }
  130. if !foundSlash && len(dsn) > 0 {
  131. return nil, errInvalidDSNNoSlash
  132. }
  133. if cfg.interpolateParams && unsafeCollations[cfg.collation] {
  134. return nil, errInvalidDSNUnsafeCollation
  135. }
  136. // Set default network if empty
  137. if cfg.net == "" {
  138. cfg.net = "tcp"
  139. }
  140. // Set default address if empty
  141. if cfg.addr == "" {
  142. switch cfg.net {
  143. case "tcp":
  144. cfg.addr = "127.0.0.1:3306"
  145. case "unix":
  146. cfg.addr = "/tmp/mysql.sock"
  147. default:
  148. return nil, errors.New("Default addr for network '" + cfg.net + "' unknown")
  149. }
  150. }
  151. return
  152. }
  153. // parseDSNParams parses the DSN "query string"
  154. // Values must be url.QueryEscape'ed
  155. func parseDSNParams(cfg *config, params string) (err error) {
  156. for _, v := range strings.Split(params, "&") {
  157. param := strings.SplitN(v, "=", 2)
  158. if len(param) != 2 {
  159. continue
  160. }
  161. // cfg params
  162. switch value := param[1]; param[0] {
  163. // Enable client side placeholder substitution
  164. case "interpolateParams":
  165. var isBool bool
  166. cfg.interpolateParams, isBool = readBool(value)
  167. if !isBool {
  168. return fmt.Errorf("Invalid Bool value: %s", value)
  169. }
  170. // Disable INFILE whitelist / enable all files
  171. case "allowAllFiles":
  172. var isBool bool
  173. cfg.allowAllFiles, isBool = readBool(value)
  174. if !isBool {
  175. return fmt.Errorf("Invalid Bool value: %s", value)
  176. }
  177. // Use cleartext authentication mode (MySQL 5.5.10+)
  178. case "allowCleartextPasswords":
  179. var isBool bool
  180. cfg.allowCleartextPasswords, isBool = readBool(value)
  181. if !isBool {
  182. return fmt.Errorf("Invalid Bool value: %s", value)
  183. }
  184. // Use old authentication mode (pre MySQL 4.1)
  185. case "allowOldPasswords":
  186. var isBool bool
  187. cfg.allowOldPasswords, isBool = readBool(value)
  188. if !isBool {
  189. return fmt.Errorf("Invalid Bool value: %s", value)
  190. }
  191. // Switch "rowsAffected" mode
  192. case "clientFoundRows":
  193. var isBool bool
  194. cfg.clientFoundRows, isBool = readBool(value)
  195. if !isBool {
  196. return fmt.Errorf("Invalid Bool value: %s", value)
  197. }
  198. // Collation
  199. case "collation":
  200. collation, ok := collations[value]
  201. if !ok {
  202. // Note possibility for false negatives:
  203. // could be triggered although the collation is valid if the
  204. // collations map does not contain entries the server supports.
  205. err = errors.New("unknown collation")
  206. return
  207. }
  208. cfg.collation = collation
  209. break
  210. case "columnsWithAlias":
  211. var isBool bool
  212. cfg.columnsWithAlias, isBool = readBool(value)
  213. if !isBool {
  214. return fmt.Errorf("Invalid Bool value: %s", value)
  215. }
  216. // Time Location
  217. case "loc":
  218. if value, err = url.QueryUnescape(value); err != nil {
  219. return
  220. }
  221. cfg.loc, err = time.LoadLocation(value)
  222. if err != nil {
  223. return
  224. }
  225. // Dial Timeout
  226. case "timeout":
  227. cfg.timeout, err = time.ParseDuration(value)
  228. if err != nil {
  229. return
  230. }
  231. // TLS-Encryption
  232. case "tls":
  233. boolValue, isBool := readBool(value)
  234. if isBool {
  235. if boolValue {
  236. cfg.tls = &tls.Config{}
  237. }
  238. } else {
  239. if strings.ToLower(value) == "skip-verify" {
  240. cfg.tls = &tls.Config{InsecureSkipVerify: true}
  241. } else if tlsConfig, ok := tlsConfigRegister[value]; ok {
  242. if len(tlsConfig.ServerName) == 0 && !tlsConfig.InsecureSkipVerify {
  243. host, _, err := net.SplitHostPort(cfg.addr)
  244. if err == nil {
  245. tlsConfig.ServerName = host
  246. }
  247. }
  248. cfg.tls = tlsConfig
  249. } else {
  250. return fmt.Errorf("Invalid value / unknown config name: %s", value)
  251. }
  252. }
  253. default:
  254. // lazy init
  255. if cfg.params == nil {
  256. cfg.params = make(map[string]string)
  257. }
  258. if cfg.params[param[0]], err = url.QueryUnescape(value); err != nil {
  259. return
  260. }
  261. }
  262. }
  263. return
  264. }
  265. // Returns the bool value of the input.
  266. // The 2nd return value indicates if the input was a valid bool value
  267. func readBool(input string) (value bool, valid bool) {
  268. switch input {
  269. case "1", "true", "TRUE", "True":
  270. return true, true
  271. case "0", "false", "FALSE", "False":
  272. return false, true
  273. }
  274. // Not a valid bool value
  275. return
  276. }
  277. /******************************************************************************
  278. * Authentication *
  279. ******************************************************************************/
  280. // Encrypt password using 4.1+ method
  281. func scramblePassword(scramble, password []byte) []byte {
  282. if len(password) == 0 {
  283. return nil
  284. }
  285. // stage1Hash = SHA1(password)
  286. crypt := sha1.New()
  287. crypt.Write(password)
  288. stage1 := crypt.Sum(nil)
  289. // scrambleHash = SHA1(scramble + SHA1(stage1Hash))
  290. // inner Hash
  291. crypt.Reset()
  292. crypt.Write(stage1)
  293. hash := crypt.Sum(nil)
  294. // outer Hash
  295. crypt.Reset()
  296. crypt.Write(scramble)
  297. crypt.Write(hash)
  298. scramble = crypt.Sum(nil)
  299. // token = scrambleHash XOR stage1Hash
  300. for i := range scramble {
  301. scramble[i] ^= stage1[i]
  302. }
  303. return scramble
  304. }
  305. // Encrypt password using pre 4.1 (old password) method
  306. // https://github.com/atcurtis/mariadb/blob/master/mysys/my_rnd.c
  307. type myRnd struct {
  308. seed1, seed2 uint32
  309. }
  310. const myRndMaxVal = 0x3FFFFFFF
  311. // Pseudo random number generator
  312. func newMyRnd(seed1, seed2 uint32) *myRnd {
  313. return &myRnd{
  314. seed1: seed1 % myRndMaxVal,
  315. seed2: seed2 % myRndMaxVal,
  316. }
  317. }
  318. // Tested to be equivalent to MariaDB's floating point variant
  319. // http://play.golang.org/p/QHvhd4qved
  320. // http://play.golang.org/p/RG0q4ElWDx
  321. func (r *myRnd) NextByte() byte {
  322. r.seed1 = (r.seed1*3 + r.seed2) % myRndMaxVal
  323. r.seed2 = (r.seed1 + r.seed2 + 33) % myRndMaxVal
  324. return byte(uint64(r.seed1) * 31 / myRndMaxVal)
  325. }
  326. // Generate binary hash from byte string using insecure pre 4.1 method
  327. func pwHash(password []byte) (result [2]uint32) {
  328. var add uint32 = 7
  329. var tmp uint32
  330. result[0] = 1345345333
  331. result[1] = 0x12345671
  332. for _, c := range password {
  333. // skip spaces and tabs in password
  334. if c == ' ' || c == '\t' {
  335. continue
  336. }
  337. tmp = uint32(c)
  338. result[0] ^= (((result[0] & 63) + add) * tmp) + (result[0] << 8)
  339. result[1] += (result[1] << 8) ^ result[0]
  340. add += tmp
  341. }
  342. // Remove sign bit (1<<31)-1)
  343. result[0] &= 0x7FFFFFFF
  344. result[1] &= 0x7FFFFFFF
  345. return
  346. }
  347. // Encrypt password using insecure pre 4.1 method
  348. func scrambleOldPassword(scramble, password []byte) []byte {
  349. if len(password) == 0 {
  350. return nil
  351. }
  352. scramble = scramble[:8]
  353. hashPw := pwHash(password)
  354. hashSc := pwHash(scramble)
  355. r := newMyRnd(hashPw[0]^hashSc[0], hashPw[1]^hashSc[1])
  356. var out [8]byte
  357. for i := range out {
  358. out[i] = r.NextByte() + 64
  359. }
  360. mask := r.NextByte()
  361. for i := range out {
  362. out[i] ^= mask
  363. }
  364. return out[:]
  365. }
  366. /******************************************************************************
  367. * Time related utils *
  368. ******************************************************************************/
  369. // NullTime represents a time.Time that may be NULL.
  370. // NullTime implements the Scanner interface so
  371. // it can be used as a scan destination:
  372. //
  373. // var nt NullTime
  374. // err := db.QueryRow("SELECT time FROM foo WHERE id=?", id).Scan(&nt)
  375. // ...
  376. // if nt.Valid {
  377. // // use nt.Time
  378. // } else {
  379. // // NULL value
  380. // }
  381. //
  382. // This NullTime implementation is not driver-specific
  383. type NullTime struct {
  384. Time time.Time
  385. Valid bool // Valid is true if Time is not NULL
  386. }
  387. // Scan implements the Scanner interface.
  388. // The value type must be time.Time or string / []byte (formatted time-string),
  389. // otherwise Scan fails.
  390. func (nt *NullTime) Scan(value interface{}) (err error) {
  391. if value == nil {
  392. nt.Time, nt.Valid = time.Time{}, false
  393. return
  394. }
  395. switch v := value.(type) {
  396. case time.Time:
  397. nt.Time, nt.Valid = v, true
  398. return
  399. case []byte:
  400. nt.Time, err = parseDateTime(string(v), time.UTC)
  401. nt.Valid = (err == nil)
  402. return
  403. case string:
  404. nt.Time, err = parseDateTime(v, time.UTC)
  405. nt.Valid = (err == nil)
  406. return
  407. }
  408. nt.Valid = false
  409. return fmt.Errorf("Can't convert %T to time.Time", value)
  410. }
  411. // Value implements the driver Valuer interface.
  412. func (nt NullTime) Value() (driver.Value, error) {
  413. if !nt.Valid {
  414. return nil, nil
  415. }
  416. return nt.Time, nil
  417. }
  418. func parseDateTime(str string, loc *time.Location) (t time.Time, err error) {
  419. base := "0000-00-00 00:00:00.0000000"
  420. switch len(str) {
  421. case 10, 19, 21, 22, 23, 24, 25, 26: // up to "YYYY-MM-DD HH:MM:SS.MMMMMM"
  422. if str == base[:len(str)] {
  423. return
  424. }
  425. t, err = time.Parse(timeFormat[:len(str)], str)
  426. default:
  427. err = fmt.Errorf("Invalid Time-String: %s", str)
  428. return
  429. }
  430. // Adjust location
  431. if err == nil && loc != time.UTC {
  432. y, mo, d := t.Date()
  433. h, mi, s := t.Clock()
  434. t, err = time.Date(y, mo, d, h, mi, s, t.Nanosecond(), loc), nil
  435. }
  436. return
  437. }
  438. func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Value, error) {
  439. switch num {
  440. case 0:
  441. return time.Time{}, nil
  442. case 4:
  443. return time.Date(
  444. int(binary.LittleEndian.Uint16(data[:2])), // year
  445. time.Month(data[2]), // month
  446. int(data[3]), // day
  447. 0, 0, 0, 0,
  448. loc,
  449. ), nil
  450. case 7:
  451. return time.Date(
  452. int(binary.LittleEndian.Uint16(data[:2])), // year
  453. time.Month(data[2]), // month
  454. int(data[3]), // day
  455. int(data[4]), // hour
  456. int(data[5]), // minutes
  457. int(data[6]), // seconds
  458. 0,
  459. loc,
  460. ), nil
  461. case 11:
  462. return time.Date(
  463. int(binary.LittleEndian.Uint16(data[:2])), // year
  464. time.Month(data[2]), // month
  465. int(data[3]), // day
  466. int(data[4]), // hour
  467. int(data[5]), // minutes
  468. int(data[6]), // seconds
  469. int(binary.LittleEndian.Uint32(data[7:11]))*1000, // nanoseconds
  470. loc,
  471. ), nil
  472. }
  473. return nil, fmt.Errorf("Invalid DATETIME-packet length %d", num)
  474. }
  475. // zeroDateTime is used in formatBinaryDateTime to avoid an allocation
  476. // if the DATE or DATETIME has the zero value.
  477. // It must never be changed.
  478. // The current behavior depends on database/sql copying the result.
  479. var zeroDateTime = []byte("0000-00-00 00:00:00.000000")
  480. const digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  481. const digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
  482. func formatBinaryDateTime(src []byte, length uint8, justTime bool) (driver.Value, error) {
  483. // length expects the deterministic length of the zero value,
  484. // negative time and 100+ hours are automatically added if needed
  485. if len(src) == 0 {
  486. if justTime {
  487. return zeroDateTime[11 : 11+length], nil
  488. }
  489. return zeroDateTime[:length], nil
  490. }
  491. var dst []byte // return value
  492. var pt, p1, p2, p3 byte // current digit pair
  493. var zOffs byte // offset of value in zeroDateTime
  494. if justTime {
  495. switch length {
  496. case
  497. 8, // time (can be up to 10 when negative and 100+ hours)
  498. 10, 11, 12, 13, 14, 15: // time with fractional seconds
  499. default:
  500. return nil, fmt.Errorf("illegal TIME length %d", length)
  501. }
  502. switch len(src) {
  503. case 8, 12:
  504. default:
  505. return nil, fmt.Errorf("Invalid TIME-packet length %d", len(src))
  506. }
  507. // +2 to enable negative time and 100+ hours
  508. dst = make([]byte, 0, length+2)
  509. if src[0] == 1 {
  510. dst = append(dst, '-')
  511. }
  512. if src[1] != 0 {
  513. hour := uint16(src[1])*24 + uint16(src[5])
  514. pt = byte(hour / 100)
  515. p1 = byte(hour - 100*uint16(pt))
  516. dst = append(dst, digits01[pt])
  517. } else {
  518. p1 = src[5]
  519. }
  520. zOffs = 11
  521. src = src[6:]
  522. } else {
  523. switch length {
  524. case 10, 19, 21, 22, 23, 24, 25, 26:
  525. default:
  526. t := "DATE"
  527. if length > 10 {
  528. t += "TIME"
  529. }
  530. return nil, fmt.Errorf("illegal %s length %d", t, length)
  531. }
  532. switch len(src) {
  533. case 4, 7, 11:
  534. default:
  535. t := "DATE"
  536. if length > 10 {
  537. t += "TIME"
  538. }
  539. return nil, fmt.Errorf("illegal %s-packet length %d", t, len(src))
  540. }
  541. dst = make([]byte, 0, length)
  542. // start with the date
  543. year := binary.LittleEndian.Uint16(src[:2])
  544. pt = byte(year / 100)
  545. p1 = byte(year - 100*uint16(pt))
  546. p2, p3 = src[2], src[3]
  547. dst = append(dst,
  548. digits10[pt], digits01[pt],
  549. digits10[p1], digits01[p1], '-',
  550. digits10[p2], digits01[p2], '-',
  551. digits10[p3], digits01[p3],
  552. )
  553. if length == 10 {
  554. return dst, nil
  555. }
  556. if len(src) == 4 {
  557. return append(dst, zeroDateTime[10:length]...), nil
  558. }
  559. dst = append(dst, ' ')
  560. p1 = src[4] // hour
  561. src = src[5:]
  562. }
  563. // p1 is 2-digit hour, src is after hour
  564. p2, p3 = src[0], src[1]
  565. dst = append(dst,
  566. digits10[p1], digits01[p1], ':',
  567. digits10[p2], digits01[p2], ':',
  568. digits10[p3], digits01[p3],
  569. )
  570. if length <= byte(len(dst)) {
  571. return dst, nil
  572. }
  573. src = src[2:]
  574. if len(src) == 0 {
  575. return append(dst, zeroDateTime[19:zOffs+length]...), nil
  576. }
  577. microsecs := binary.LittleEndian.Uint32(src[:4])
  578. p1 = byte(microsecs / 10000)
  579. microsecs -= 10000 * uint32(p1)
  580. p2 = byte(microsecs / 100)
  581. microsecs -= 100 * uint32(p2)
  582. p3 = byte(microsecs)
  583. switch decimals := zOffs + length - 20; decimals {
  584. default:
  585. return append(dst, '.',
  586. digits10[p1], digits01[p1],
  587. digits10[p2], digits01[p2],
  588. digits10[p3], digits01[p3],
  589. ), nil
  590. case 1:
  591. return append(dst, '.',
  592. digits10[p1],
  593. ), nil
  594. case 2:
  595. return append(dst, '.',
  596. digits10[p1], digits01[p1],
  597. ), nil
  598. case 3:
  599. return append(dst, '.',
  600. digits10[p1], digits01[p1],
  601. digits10[p2],
  602. ), nil
  603. case 4:
  604. return append(dst, '.',
  605. digits10[p1], digits01[p1],
  606. digits10[p2], digits01[p2],
  607. ), nil
  608. case 5:
  609. return append(dst, '.',
  610. digits10[p1], digits01[p1],
  611. digits10[p2], digits01[p2],
  612. digits10[p3],
  613. ), nil
  614. }
  615. }
  616. /******************************************************************************
  617. * Convert from and to bytes *
  618. ******************************************************************************/
  619. func uint64ToBytes(n uint64) []byte {
  620. return []byte{
  621. byte(n),
  622. byte(n >> 8),
  623. byte(n >> 16),
  624. byte(n >> 24),
  625. byte(n >> 32),
  626. byte(n >> 40),
  627. byte(n >> 48),
  628. byte(n >> 56),
  629. }
  630. }
  631. func uint64ToString(n uint64) []byte {
  632. var a [20]byte
  633. i := 20
  634. // U+0030 = 0
  635. // ...
  636. // U+0039 = 9
  637. var q uint64
  638. for n >= 10 {
  639. i--
  640. q = n / 10
  641. a[i] = uint8(n-q*10) + 0x30
  642. n = q
  643. }
  644. i--
  645. a[i] = uint8(n) + 0x30
  646. return a[i:]
  647. }
  648. // treats string value as unsigned integer representation
  649. func stringToInt(b []byte) int {
  650. val := 0
  651. for i := range b {
  652. val *= 10
  653. val += int(b[i] - 0x30)
  654. }
  655. return val
  656. }
  657. // returns the string read as a bytes slice, wheter the value is NULL,
  658. // the number of bytes read and an error, in case the string is longer than
  659. // the input slice
  660. func readLengthEncodedString(b []byte) ([]byte, bool, int, error) {
  661. // Get length
  662. num, isNull, n := readLengthEncodedInteger(b)
  663. if num < 1 {
  664. return b[n:n], isNull, n, nil
  665. }
  666. n += int(num)
  667. // Check data length
  668. if len(b) >= n {
  669. return b[n-int(num) : n], false, n, nil
  670. }
  671. return nil, false, n, io.EOF
  672. }
  673. // returns the number of bytes skipped and an error, in case the string is
  674. // longer than the input slice
  675. func skipLengthEncodedString(b []byte) (int, error) {
  676. // Get length
  677. num, _, n := readLengthEncodedInteger(b)
  678. if num < 1 {
  679. return n, nil
  680. }
  681. n += int(num)
  682. // Check data length
  683. if len(b) >= n {
  684. return n, nil
  685. }
  686. return n, io.EOF
  687. }
  688. // returns the number read, whether the value is NULL and the number of bytes read
  689. func readLengthEncodedInteger(b []byte) (uint64, bool, int) {
  690. switch b[0] {
  691. // 251: NULL
  692. case 0xfb:
  693. return 0, true, 1
  694. // 252: value of following 2
  695. case 0xfc:
  696. return uint64(b[1]) | uint64(b[2])<<8, false, 3
  697. // 253: value of following 3
  698. case 0xfd:
  699. return uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16, false, 4
  700. // 254: value of following 8
  701. case 0xfe:
  702. return uint64(b[1]) | uint64(b[2])<<8 | uint64(b[3])<<16 |
  703. uint64(b[4])<<24 | uint64(b[5])<<32 | uint64(b[6])<<40 |
  704. uint64(b[7])<<48 | uint64(b[8])<<56,
  705. false, 9
  706. }
  707. // 0-250: value of first byte
  708. return uint64(b[0]), false, 1
  709. }
  710. // encodes a uint64 value and appends it to the given bytes slice
  711. func appendLengthEncodedInteger(b []byte, n uint64) []byte {
  712. switch {
  713. case n <= 250:
  714. return append(b, byte(n))
  715. case n <= 0xffff:
  716. return append(b, 0xfc, byte(n), byte(n>>8))
  717. case n <= 0xffffff:
  718. return append(b, 0xfd, byte(n), byte(n>>8), byte(n>>16))
  719. }
  720. return append(b, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
  721. byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
  722. }
  723. // reserveBuffer checks cap(buf) and expand buffer to len(buf) + appendSize.
  724. // If cap(buf) is not enough, reallocate new buffer.
  725. func reserveBuffer(buf []byte, appendSize int) []byte {
  726. newSize := len(buf) + appendSize
  727. if cap(buf) < newSize {
  728. // Grow buffer exponentially
  729. newBuf := make([]byte, len(buf)*2+appendSize)
  730. copy(newBuf, buf)
  731. buf = newBuf
  732. }
  733. return buf[:newSize]
  734. }
  735. // escapeBytesBackslash escapes []byte with backslashes (\)
  736. // This escapes the contents of a string (provided as []byte) by adding backslashes before special
  737. // characters, and turning others into specific escape sequences, such as
  738. // turning newlines into \n and null bytes into \0.
  739. // https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L823-L932
  740. func escapeBytesBackslash(buf, v []byte) []byte {
  741. pos := len(buf)
  742. buf = reserveBuffer(buf, len(v)*2)
  743. for _, c := range v {
  744. switch c {
  745. case '\x00':
  746. buf[pos] = '\\'
  747. buf[pos+1] = '0'
  748. pos += 2
  749. case '\n':
  750. buf[pos] = '\\'
  751. buf[pos+1] = 'n'
  752. pos += 2
  753. case '\r':
  754. buf[pos] = '\\'
  755. buf[pos+1] = 'r'
  756. pos += 2
  757. case '\x1a':
  758. buf[pos] = '\\'
  759. buf[pos+1] = 'Z'
  760. pos += 2
  761. case '\'':
  762. buf[pos] = '\\'
  763. buf[pos+1] = '\''
  764. pos += 2
  765. case '"':
  766. buf[pos] = '\\'
  767. buf[pos+1] = '"'
  768. pos += 2
  769. case '\\':
  770. buf[pos] = '\\'
  771. buf[pos+1] = '\\'
  772. pos += 2
  773. default:
  774. buf[pos] = c
  775. pos += 1
  776. }
  777. }
  778. return buf[:pos]
  779. }
  780. // escapeStringBackslash is similar to escapeBytesBackslash but for string.
  781. func escapeStringBackslash(buf []byte, v string) []byte {
  782. pos := len(buf)
  783. buf = reserveBuffer(buf, len(v)*2)
  784. for i := 0; i < len(v); i++ {
  785. c := v[i]
  786. switch c {
  787. case '\x00':
  788. buf[pos] = '\\'
  789. buf[pos+1] = '0'
  790. pos += 2
  791. case '\n':
  792. buf[pos] = '\\'
  793. buf[pos+1] = 'n'
  794. pos += 2
  795. case '\r':
  796. buf[pos] = '\\'
  797. buf[pos+1] = 'r'
  798. pos += 2
  799. case '\x1a':
  800. buf[pos] = '\\'
  801. buf[pos+1] = 'Z'
  802. pos += 2
  803. case '\'':
  804. buf[pos] = '\\'
  805. buf[pos+1] = '\''
  806. pos += 2
  807. case '"':
  808. buf[pos] = '\\'
  809. buf[pos+1] = '"'
  810. pos += 2
  811. case '\\':
  812. buf[pos] = '\\'
  813. buf[pos+1] = '\\'
  814. pos += 2
  815. default:
  816. buf[pos] = c
  817. pos += 1
  818. }
  819. }
  820. return buf[:pos]
  821. }
  822. // escapeBytesQuotes escapes apostrophes in []byte by doubling them up.
  823. // This escapes the contents of a string by doubling up any apostrophes that
  824. // it contains. This is used when the NO_BACKSLASH_ESCAPES SQL_MODE is in
  825. // effect on the server.
  826. // https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L963-L1038
  827. func escapeBytesQuotes(buf, v []byte) []byte {
  828. pos := len(buf)
  829. buf = reserveBuffer(buf, len(v)*2)
  830. for _, c := range v {
  831. if c == '\'' {
  832. buf[pos] = '\''
  833. buf[pos+1] = '\''
  834. pos += 2
  835. } else {
  836. buf[pos] = c
  837. pos++
  838. }
  839. }
  840. return buf[:pos]
  841. }
  842. // escapeStringQuotes is similar to escapeBytesQuotes but for string.
  843. func escapeStringQuotes(buf []byte, v string) []byte {
  844. pos := len(buf)
  845. buf = reserveBuffer(buf, len(v)*2)
  846. for i := 0; i < len(v); i++ {
  847. c := v[i]
  848. if c == '\'' {
  849. buf[pos] = '\''
  850. buf[pos+1] = '\''
  851. pos += 2
  852. } else {
  853. buf[pos] = c
  854. pos++
  855. }
  856. }
  857. return buf[:pos]
  858. }