utils.go 23 KB

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