utils.go 23 KB

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