utils.go 23 KB

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