utils.go 23 KB

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