packets.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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. "bytes"
  11. "crypto/tls"
  12. "database/sql/driver"
  13. "encoding/binary"
  14. "errors"
  15. "fmt"
  16. "io"
  17. "math"
  18. "os"
  19. "path"
  20. "runtime"
  21. "time"
  22. )
  23. // Packets documentation:
  24. // http://dev.mysql.com/doc/internals/en/client-server-protocol.html
  25. // Read packet to buffer 'data'
  26. func (mc *mysqlConn) readPacket() ([]byte, error) {
  27. var payload []byte
  28. for {
  29. // Read packet header
  30. data, err := mc.buf.readNext(4)
  31. if err != nil {
  32. errLog.Print(err)
  33. mc.Close()
  34. return nil, driver.ErrBadConn
  35. }
  36. // Packet Length [24 bit]
  37. pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16)
  38. if pktLen < 1 {
  39. errLog.Print(ErrMalformPkt)
  40. mc.Close()
  41. return nil, driver.ErrBadConn
  42. }
  43. // Check Packet Sync [8 bit]
  44. if data[3] != mc.sequence {
  45. if data[3] > mc.sequence {
  46. return nil, ErrPktSyncMul
  47. }
  48. return nil, ErrPktSync
  49. }
  50. mc.sequence++
  51. // Read packet body [pktLen bytes]
  52. data, err = mc.buf.readNext(pktLen)
  53. if err != nil {
  54. errLog.Print(err)
  55. mc.Close()
  56. return nil, driver.ErrBadConn
  57. }
  58. isLastPacket := (pktLen < maxPacketSize)
  59. // Zero allocations for non-splitting packets
  60. if isLastPacket && payload == nil {
  61. return data, nil
  62. }
  63. payload = append(payload, data...)
  64. if isLastPacket {
  65. return payload, nil
  66. }
  67. }
  68. }
  69. // Write packet buffer 'data'
  70. func (mc *mysqlConn) writePacket(data []byte) error {
  71. pktLen := len(data) - 4
  72. if pktLen > mc.maxPacketAllowed {
  73. return ErrPktTooLarge
  74. }
  75. for {
  76. var size int
  77. if pktLen >= maxPacketSize {
  78. data[0] = 0xff
  79. data[1] = 0xff
  80. data[2] = 0xff
  81. size = maxPacketSize
  82. } else {
  83. data[0] = byte(pktLen)
  84. data[1] = byte(pktLen >> 8)
  85. data[2] = byte(pktLen >> 16)
  86. size = pktLen
  87. }
  88. data[3] = mc.sequence
  89. // Write packet
  90. if mc.writeTimeout > 0 {
  91. if err := mc.netConn.SetWriteDeadline(time.Now().Add(mc.writeTimeout)); err != nil {
  92. return err
  93. }
  94. }
  95. n, err := mc.netConn.Write(data[:4+size])
  96. if err == nil && n == 4+size {
  97. mc.sequence++
  98. if size != maxPacketSize {
  99. return nil
  100. }
  101. pktLen -= size
  102. data = data[size:]
  103. continue
  104. }
  105. // Handle error
  106. if err == nil { // n != len(data)
  107. errLog.Print(ErrMalformPkt)
  108. } else {
  109. errLog.Print(err)
  110. }
  111. return driver.ErrBadConn
  112. }
  113. }
  114. /******************************************************************************
  115. * Initialisation Process *
  116. ******************************************************************************/
  117. // Handshake Initialization Packet
  118. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::Handshake
  119. func (mc *mysqlConn) readInitPacket() ([]byte, error) {
  120. data, err := mc.readPacket()
  121. if err != nil {
  122. return nil, err
  123. }
  124. if data[0] == iERR {
  125. return nil, mc.handleErrorPacket(data)
  126. }
  127. // protocol version [1 byte]
  128. if data[0] < minProtocolVersion {
  129. return nil, fmt.Errorf(
  130. "unsupported protocol version %d. Version %d or higher is required",
  131. data[0],
  132. minProtocolVersion,
  133. )
  134. }
  135. // server version [null terminated string]
  136. // connection id [4 bytes]
  137. pos := 1 + bytes.IndexByte(data[1:], 0x00) + 1 + 4
  138. // first part of the password cipher [8 bytes]
  139. cipher := data[pos : pos+8]
  140. // (filler) always 0x00 [1 byte]
  141. pos += 8 + 1
  142. // capability flags (lower 2 bytes) [2 bytes]
  143. mc.flags = clientFlag(binary.LittleEndian.Uint16(data[pos : pos+2]))
  144. if mc.flags&clientProtocol41 == 0 {
  145. return nil, ErrOldProtocol
  146. }
  147. if mc.flags&clientSSL == 0 && mc.cfg.tls != nil {
  148. return nil, ErrNoTLS
  149. }
  150. pos += 2
  151. if len(data) > pos {
  152. // character set [1 byte]
  153. // status flags [2 bytes]
  154. pos += 1 + 2
  155. // capability flags (upper 2 bytes) [2 bytes]
  156. mc.flags |= (clientFlag(binary.LittleEndian.Uint16(data[pos:pos+2])) << 16)
  157. pos += 2
  158. // length of auth-plugin-data [1 byte]
  159. // reserved (all [00]) [10 bytes]
  160. pos += 1 + 10
  161. // second part of the password cipher [mininum 13 bytes],
  162. // where len=MAX(13, length of auth-plugin-data - 8)
  163. //
  164. // The web documentation is ambiguous about the length. However,
  165. // according to mysql-5.7/sql/auth/sql_authentication.cc line 538,
  166. // the 13th byte is "\0 byte, terminating the second part of
  167. // a scramble". So the second part of the password cipher is
  168. // a NULL terminated string that's at least 13 bytes with the
  169. // last byte being NULL.
  170. //
  171. // The official Python library uses the fixed length 12
  172. // which seems to work but technically could have a hidden bug.
  173. cipher = append(cipher, data[pos:pos+12]...)
  174. // TODO: Verify string termination
  175. // EOF if version (>= 5.5.7 and < 5.5.10) or (>= 5.6.0 and < 5.6.2)
  176. // \NUL otherwise
  177. //
  178. //if data[len(data)-1] == 0 {
  179. // return
  180. //}
  181. //return ErrMalformPkt
  182. // make a memory safe copy of the cipher slice
  183. var b [20]byte
  184. copy(b[:], cipher)
  185. return b[:], nil
  186. }
  187. // make a memory safe copy of the cipher slice
  188. var b [8]byte
  189. copy(b[:], cipher)
  190. return b[:], nil
  191. }
  192. // Client Authentication Packet
  193. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
  194. func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
  195. // Adjust client flags based on server support
  196. clientFlags := clientProtocol41 |
  197. clientSecureConn |
  198. clientLongPassword |
  199. clientTransactions |
  200. clientLocalFiles |
  201. clientPluginAuth |
  202. clientMultiResults |
  203. mc.flags&clientLongFlag
  204. if mc.cfg.ClientFoundRows {
  205. clientFlags |= clientFoundRows
  206. }
  207. // To enable TLS / SSL
  208. if mc.cfg.tls != nil {
  209. clientFlags |= clientSSL
  210. }
  211. if mc.cfg.MultiStatements {
  212. clientFlags |= clientMultiStatements
  213. }
  214. // User Password
  215. scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.Passwd))
  216. pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1 + 1 + len(scrambleBuff) + 21 + 1
  217. // Default connection attributes
  218. attrlen := 0
  219. var attrs map[string]string
  220. if mc.flags&clientConnectAttrs != 0 {
  221. clientFlags |= clientConnectAttrs
  222. attrs = map[string]string{
  223. "_os": runtime.GOOS,
  224. "_client_name": "Go-MySQL-Driver",
  225. "_pid": pid,
  226. "_platform": runtime.GOARCH,
  227. "program_name": path.Base(os.Args[0]),
  228. }
  229. if len(os_user_full) > 0 {
  230. attrs["_os_user_full"] = os_user_full
  231. }
  232. if len(os_user) > 0 {
  233. attrs["_os_user"] = os_user
  234. }
  235. // Merge the custom attributes and the default attributes
  236. for cfganame, cfgaval := range mc.cfg.ConnAttrs {
  237. attrs[cfganame] = cfgaval
  238. }
  239. for attrname, attrvalue := range attrs {
  240. attrlen += len(attrname) + len(attrvalue)
  241. // one byte to store attrname length and one byte to store attrvalue length
  242. attrlen += 2
  243. }
  244. pktLen += attrlen + 1 // one byte to store the total length of attrs
  245. }
  246. // To specify a db name
  247. if n := len(mc.cfg.DBName); n > 0 {
  248. clientFlags |= clientConnectWithDB
  249. pktLen += n + 1
  250. }
  251. // Calculate packet length and get buffer with that size
  252. data := mc.buf.takeSmallBuffer(pktLen + 4)
  253. if data == nil {
  254. // can not take the buffer. Something must be wrong with the connection
  255. errLog.Print(ErrBusyBuffer)
  256. return driver.ErrBadConn
  257. }
  258. // ClientFlags [32 bit]
  259. data[4] = byte(clientFlags)
  260. data[5] = byte(clientFlags >> 8)
  261. data[6] = byte(clientFlags >> 16)
  262. data[7] = byte(clientFlags >> 24)
  263. // MaxPacketSize [32 bit] (none)
  264. data[8] = 0x00
  265. data[9] = 0x00
  266. data[10] = 0x00
  267. data[11] = 0x00
  268. // Charset [1 byte]
  269. var found bool
  270. data[12], found = collations[mc.cfg.Collation]
  271. if !found {
  272. // Note possibility for false negatives:
  273. // could be triggered although the collation is valid if the
  274. // collations map does not contain entries the server supports.
  275. return errors.New("unknown collation")
  276. }
  277. // SSL Connection Request Packet
  278. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::SSLRequest
  279. if mc.cfg.tls != nil {
  280. // Send TLS / SSL request packet
  281. if err := mc.writePacket(data[:(4+4+1+23)+4]); err != nil {
  282. return err
  283. }
  284. // Switch to TLS
  285. tlsConn := tls.Client(mc.netConn, mc.cfg.tls)
  286. if err := tlsConn.Handshake(); err != nil {
  287. return err
  288. }
  289. mc.netConn = tlsConn
  290. mc.buf.nc = tlsConn
  291. }
  292. // Filler [23 bytes] (all 0x00)
  293. pos := 13
  294. for ; pos < 13+23; pos++ {
  295. data[pos] = 0
  296. }
  297. // User [null terminated string]
  298. if len(mc.cfg.User) > 0 {
  299. pos += copy(data[pos:], mc.cfg.User)
  300. }
  301. data[pos] = 0x00
  302. pos++
  303. // ScrambleBuffer [length encoded integer]
  304. data[pos] = byte(len(scrambleBuff))
  305. pos += 1 + copy(data[pos+1:], scrambleBuff)
  306. // Databasename [null terminated string]
  307. if len(mc.cfg.DBName) > 0 {
  308. pos += copy(data[pos:], mc.cfg.DBName)
  309. data[pos] = 0x00
  310. pos++
  311. }
  312. // Assume native client during response
  313. pos += copy(data[pos:], "mysql_native_password")
  314. data[pos] = 0x00
  315. pos++
  316. // Connection attributes
  317. if attrlen > 0 {
  318. data[pos] = byte(attrlen)
  319. pos++
  320. for attrname, attrvalue := range attrs {
  321. data[pos] = byte(len(attrname))
  322. pos += 1 + copy(data[pos+1:], attrname)
  323. data[pos] = byte(len(attrvalue))
  324. pos += 1 + copy(data[pos+1:], attrvalue)
  325. }
  326. }
  327. // Send Auth packet
  328. return mc.writePacket(data)
  329. }
  330. // Client old authentication packet
  331. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
  332. func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
  333. // User password
  334. scrambleBuff := scrambleOldPassword(cipher, []byte(mc.cfg.Passwd))
  335. // Calculate the packet length and add a tailing 0
  336. pktLen := len(scrambleBuff) + 1
  337. data := mc.buf.takeSmallBuffer(4 + pktLen)
  338. if data == nil {
  339. // can not take the buffer. Something must be wrong with the connection
  340. errLog.Print(ErrBusyBuffer)
  341. return driver.ErrBadConn
  342. }
  343. // Add the scrambled password [null terminated string]
  344. copy(data[4:], scrambleBuff)
  345. data[4+pktLen-1] = 0x00
  346. return mc.writePacket(data)
  347. }
  348. // Client clear text authentication packet
  349. // http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
  350. func (mc *mysqlConn) writeClearAuthPacket() error {
  351. // Calculate the packet length and add a tailing 0
  352. pktLen := len(mc.cfg.Passwd) + 1
  353. data := mc.buf.takeSmallBuffer(4 + pktLen)
  354. if data == nil {
  355. // can not take the buffer. Something must be wrong with the connection
  356. errLog.Print(ErrBusyBuffer)
  357. return driver.ErrBadConn
  358. }
  359. // Add the clear password [null terminated string]
  360. copy(data[4:], mc.cfg.Passwd)
  361. data[4+pktLen-1] = 0x00
  362. return mc.writePacket(data)
  363. }
  364. /******************************************************************************
  365. * Command Packets *
  366. ******************************************************************************/
  367. func (mc *mysqlConn) writeCommandPacket(command byte) error {
  368. // Reset Packet Sequence
  369. mc.sequence = 0
  370. data := mc.buf.takeSmallBuffer(4 + 1)
  371. if data == nil {
  372. // can not take the buffer. Something must be wrong with the connection
  373. errLog.Print(ErrBusyBuffer)
  374. return driver.ErrBadConn
  375. }
  376. // Add command byte
  377. data[4] = command
  378. // Send CMD packet
  379. return mc.writePacket(data)
  380. }
  381. func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
  382. // Reset Packet Sequence
  383. mc.sequence = 0
  384. pktLen := 1 + len(arg)
  385. data := mc.buf.takeBuffer(pktLen + 4)
  386. if data == nil {
  387. // can not take the buffer. Something must be wrong with the connection
  388. errLog.Print(ErrBusyBuffer)
  389. return driver.ErrBadConn
  390. }
  391. // Add command byte
  392. data[4] = command
  393. // Add arg
  394. copy(data[5:], arg)
  395. // Send CMD packet
  396. return mc.writePacket(data)
  397. }
  398. func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
  399. // Reset Packet Sequence
  400. mc.sequence = 0
  401. data := mc.buf.takeSmallBuffer(4 + 1 + 4)
  402. if data == nil {
  403. // can not take the buffer. Something must be wrong with the connection
  404. errLog.Print(ErrBusyBuffer)
  405. return driver.ErrBadConn
  406. }
  407. // Add command byte
  408. data[4] = command
  409. // Add arg [32 bit]
  410. data[5] = byte(arg)
  411. data[6] = byte(arg >> 8)
  412. data[7] = byte(arg >> 16)
  413. data[8] = byte(arg >> 24)
  414. // Send CMD packet
  415. return mc.writePacket(data)
  416. }
  417. /******************************************************************************
  418. * Result Packets *
  419. ******************************************************************************/
  420. // Returns error if Packet is not an 'Result OK'-Packet
  421. func (mc *mysqlConn) readResultOK() error {
  422. data, err := mc.readPacket()
  423. if err == nil {
  424. // packet indicator
  425. switch data[0] {
  426. case iOK:
  427. return mc.handleOkPacket(data)
  428. case iEOF:
  429. if len(data) > 1 {
  430. plugin := string(data[1:bytes.IndexByte(data, 0x00)])
  431. if plugin == "mysql_old_password" {
  432. // using old_passwords
  433. return ErrOldPassword
  434. } else if plugin == "mysql_clear_password" {
  435. // using clear text password
  436. return ErrCleartextPassword
  437. } else {
  438. return ErrUnknownPlugin
  439. }
  440. } else {
  441. return ErrOldPassword
  442. }
  443. default: // Error otherwise
  444. return mc.handleErrorPacket(data)
  445. }
  446. }
  447. return err
  448. }
  449. // Result Set Header Packet
  450. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::Resultset
  451. func (mc *mysqlConn) readResultSetHeaderPacket() (int, error) {
  452. data, err := mc.readPacket()
  453. if err == nil {
  454. switch data[0] {
  455. case iOK:
  456. return 0, mc.handleOkPacket(data)
  457. case iERR:
  458. return 0, mc.handleErrorPacket(data)
  459. case iLocalInFile:
  460. return 0, mc.handleInFileRequest(string(data[1:]))
  461. }
  462. // column count
  463. num, _, n := readLengthEncodedInteger(data)
  464. if n-len(data) == 0 {
  465. return int(num), nil
  466. }
  467. return 0, ErrMalformPkt
  468. }
  469. return 0, err
  470. }
  471. // Error Packet
  472. // http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-ERR_Packet
  473. func (mc *mysqlConn) handleErrorPacket(data []byte) error {
  474. if data[0] != iERR {
  475. return ErrMalformPkt
  476. }
  477. // 0xff [1 byte]
  478. // Error Number [16 bit uint]
  479. errno := binary.LittleEndian.Uint16(data[1:3])
  480. pos := 3
  481. // SQL State [optional: # + 5bytes string]
  482. if data[3] == 0x23 {
  483. //sqlstate := string(data[4 : 4+5])
  484. pos = 9
  485. }
  486. // Error Message [string]
  487. return &MySQLError{
  488. Number: errno,
  489. Message: string(data[pos:]),
  490. }
  491. }
  492. func readStatus(b []byte) statusFlag {
  493. return statusFlag(b[0]) | statusFlag(b[1])<<8
  494. }
  495. // Ok Packet
  496. // http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-OK_Packet
  497. func (mc *mysqlConn) handleOkPacket(data []byte) error {
  498. var n, m int
  499. // 0x00 [1 byte]
  500. // Affected rows [Length Coded Binary]
  501. mc.affectedRows, _, n = readLengthEncodedInteger(data[1:])
  502. // Insert id [Length Coded Binary]
  503. mc.insertId, _, m = readLengthEncodedInteger(data[1+n:])
  504. // server_status [2 bytes]
  505. mc.status = readStatus(data[1+n+m : 1+n+m+2])
  506. if err := mc.discardResults(); err != nil {
  507. return err
  508. }
  509. // warning count [2 bytes]
  510. if !mc.strict {
  511. return nil
  512. }
  513. pos := 1 + n + m + 2
  514. if binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
  515. return mc.getWarnings()
  516. }
  517. return nil
  518. }
  519. // Read Packets as Field Packets until EOF-Packet or an Error appears
  520. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnDefinition41
  521. func (mc *mysqlConn) readColumns(count int) ([]mysqlField, error) {
  522. columns := make([]mysqlField, count)
  523. for i := 0; ; i++ {
  524. data, err := mc.readPacket()
  525. if err != nil {
  526. return nil, err
  527. }
  528. // EOF Packet
  529. if data[0] == iEOF && (len(data) == 5 || len(data) == 1) {
  530. if i == count {
  531. return columns, nil
  532. }
  533. return nil, fmt.Errorf("column count mismatch n:%d len:%d", count, len(columns))
  534. }
  535. // Catalog
  536. pos, err := skipLengthEncodedString(data)
  537. if err != nil {
  538. return nil, err
  539. }
  540. // Database [len coded string]
  541. n, err := skipLengthEncodedString(data[pos:])
  542. if err != nil {
  543. return nil, err
  544. }
  545. pos += n
  546. // Table [len coded string]
  547. if mc.cfg.ColumnsWithAlias {
  548. tableName, _, n, err := readLengthEncodedString(data[pos:])
  549. if err != nil {
  550. return nil, err
  551. }
  552. pos += n
  553. columns[i].tableName = string(tableName)
  554. } else {
  555. n, err = skipLengthEncodedString(data[pos:])
  556. if err != nil {
  557. return nil, err
  558. }
  559. pos += n
  560. }
  561. // Original table [len coded string]
  562. n, err = skipLengthEncodedString(data[pos:])
  563. if err != nil {
  564. return nil, err
  565. }
  566. pos += n
  567. // Name [len coded string]
  568. name, _, n, err := readLengthEncodedString(data[pos:])
  569. if err != nil {
  570. return nil, err
  571. }
  572. columns[i].name = string(name)
  573. pos += n
  574. // Original name [len coded string]
  575. n, err = skipLengthEncodedString(data[pos:])
  576. if err != nil {
  577. return nil, err
  578. }
  579. // Filler [uint8]
  580. // Charset [charset, collation uint8]
  581. // Length [uint32]
  582. pos += n + 1 + 2 + 4
  583. // Field type [uint8]
  584. columns[i].fieldType = data[pos]
  585. pos++
  586. // Flags [uint16]
  587. columns[i].flags = fieldFlag(binary.LittleEndian.Uint16(data[pos : pos+2]))
  588. pos += 2
  589. // Decimals [uint8]
  590. columns[i].decimals = data[pos]
  591. //pos++
  592. // Default value [len coded binary]
  593. //if pos < len(data) {
  594. // defaultVal, _, err = bytesToLengthCodedBinary(data[pos:])
  595. //}
  596. }
  597. }
  598. // Read Packets as Field Packets until EOF-Packet or an Error appears
  599. // http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::ResultsetRow
  600. func (rows *textRows) readRow(dest []driver.Value) error {
  601. mc := rows.mc
  602. data, err := mc.readPacket()
  603. if err != nil {
  604. return err
  605. }
  606. // EOF Packet
  607. if data[0] == iEOF && len(data) == 5 {
  608. // server_status [2 bytes]
  609. rows.mc.status = readStatus(data[3:])
  610. if err := rows.mc.discardResults(); err != nil {
  611. return err
  612. }
  613. rows.mc = nil
  614. return io.EOF
  615. }
  616. if data[0] == iERR {
  617. rows.mc = nil
  618. return mc.handleErrorPacket(data)
  619. }
  620. // RowSet Packet
  621. var n int
  622. var isNull bool
  623. pos := 0
  624. for i := range dest {
  625. // Read bytes and convert to string
  626. dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
  627. pos += n
  628. if err == nil {
  629. if !isNull {
  630. if !mc.parseTime {
  631. continue
  632. } else {
  633. switch rows.columns[i].fieldType {
  634. case fieldTypeTimestamp, fieldTypeDateTime,
  635. fieldTypeDate, fieldTypeNewDate:
  636. dest[i], err = parseDateTime(
  637. string(dest[i].([]byte)),
  638. mc.cfg.Loc,
  639. )
  640. if err == nil {
  641. continue
  642. }
  643. default:
  644. continue
  645. }
  646. }
  647. } else {
  648. dest[i] = nil
  649. continue
  650. }
  651. }
  652. return err // err != nil
  653. }
  654. return nil
  655. }
  656. // Reads Packets until EOF-Packet or an Error appears. Returns count of Packets read
  657. func (mc *mysqlConn) readUntilEOF() error {
  658. for {
  659. data, err := mc.readPacket()
  660. // No Err and no EOF Packet
  661. if err == nil && data[0] != iEOF {
  662. continue
  663. }
  664. if err == nil && data[0] == iEOF && len(data) == 5 {
  665. mc.status = readStatus(data[3:])
  666. }
  667. return err // Err or EOF
  668. }
  669. }
  670. /******************************************************************************
  671. * Prepared Statements *
  672. ******************************************************************************/
  673. // Prepare Result Packets
  674. // http://dev.mysql.com/doc/internals/en/com-stmt-prepare-response.html
  675. func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
  676. data, err := stmt.mc.readPacket()
  677. if err == nil {
  678. // packet indicator [1 byte]
  679. if data[0] != iOK {
  680. return 0, stmt.mc.handleErrorPacket(data)
  681. }
  682. // statement id [4 bytes]
  683. stmt.id = binary.LittleEndian.Uint32(data[1:5])
  684. // Column count [16 bit uint]
  685. columnCount := binary.LittleEndian.Uint16(data[5:7])
  686. // Param count [16 bit uint]
  687. stmt.paramCount = int(binary.LittleEndian.Uint16(data[7:9]))
  688. // Reserved [8 bit]
  689. // Warning count [16 bit uint]
  690. if !stmt.mc.strict {
  691. return columnCount, nil
  692. }
  693. // Check for warnings count > 0, only available in MySQL > 4.1
  694. if len(data) >= 12 && binary.LittleEndian.Uint16(data[10:12]) > 0 {
  695. return columnCount, stmt.mc.getWarnings()
  696. }
  697. return columnCount, nil
  698. }
  699. return 0, err
  700. }
  701. // http://dev.mysql.com/doc/internals/en/com-stmt-send-long-data.html
  702. func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
  703. maxLen := stmt.mc.maxPacketAllowed - 1
  704. pktLen := maxLen
  705. // After the header (bytes 0-3) follows before the data:
  706. // 1 byte command
  707. // 4 bytes stmtID
  708. // 2 bytes paramID
  709. const dataOffset = 1 + 4 + 2
  710. // Can not use the write buffer since
  711. // a) the buffer is too small
  712. // b) it is in use
  713. data := make([]byte, 4+1+4+2+len(arg))
  714. copy(data[4+dataOffset:], arg)
  715. for argLen := len(arg); argLen > 0; argLen -= pktLen - dataOffset {
  716. if dataOffset+argLen < maxLen {
  717. pktLen = dataOffset + argLen
  718. }
  719. stmt.mc.sequence = 0
  720. // Add command byte [1 byte]
  721. data[4] = comStmtSendLongData
  722. // Add stmtID [32 bit]
  723. data[5] = byte(stmt.id)
  724. data[6] = byte(stmt.id >> 8)
  725. data[7] = byte(stmt.id >> 16)
  726. data[8] = byte(stmt.id >> 24)
  727. // Add paramID [16 bit]
  728. data[9] = byte(paramID)
  729. data[10] = byte(paramID >> 8)
  730. // Send CMD packet
  731. err := stmt.mc.writePacket(data[:4+pktLen])
  732. if err == nil {
  733. data = data[pktLen-dataOffset:]
  734. continue
  735. }
  736. return err
  737. }
  738. // Reset Packet Sequence
  739. stmt.mc.sequence = 0
  740. return nil
  741. }
  742. // Execute Prepared Statement
  743. // http://dev.mysql.com/doc/internals/en/com-stmt-execute.html
  744. func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
  745. if len(args) != stmt.paramCount {
  746. return fmt.Errorf(
  747. "argument count mismatch (got: %d; has: %d)",
  748. len(args),
  749. stmt.paramCount,
  750. )
  751. }
  752. const minPktLen = 4 + 1 + 4 + 1 + 4
  753. mc := stmt.mc
  754. // Reset packet-sequence
  755. mc.sequence = 0
  756. var data []byte
  757. if len(args) == 0 {
  758. data = mc.buf.takeBuffer(minPktLen)
  759. } else {
  760. data = mc.buf.takeCompleteBuffer()
  761. }
  762. if data == nil {
  763. // can not take the buffer. Something must be wrong with the connection
  764. errLog.Print(ErrBusyBuffer)
  765. return driver.ErrBadConn
  766. }
  767. // command [1 byte]
  768. data[4] = comStmtExecute
  769. // statement_id [4 bytes]
  770. data[5] = byte(stmt.id)
  771. data[6] = byte(stmt.id >> 8)
  772. data[7] = byte(stmt.id >> 16)
  773. data[8] = byte(stmt.id >> 24)
  774. // flags (0: CURSOR_TYPE_NO_CURSOR) [1 byte]
  775. data[9] = 0x00
  776. // iteration_count (uint32(1)) [4 bytes]
  777. data[10] = 0x01
  778. data[11] = 0x00
  779. data[12] = 0x00
  780. data[13] = 0x00
  781. if len(args) > 0 {
  782. pos := minPktLen
  783. var nullMask []byte
  784. if maskLen, typesLen := (len(args)+7)/8, 1+2*len(args); pos+maskLen+typesLen >= len(data) {
  785. // buffer has to be extended but we don't know by how much so
  786. // we depend on append after all data with known sizes fit.
  787. // We stop at that because we deal with a lot of columns here
  788. // which makes the required allocation size hard to guess.
  789. tmp := make([]byte, pos+maskLen+typesLen)
  790. copy(tmp[:pos], data[:pos])
  791. data = tmp
  792. nullMask = data[pos : pos+maskLen]
  793. pos += maskLen
  794. } else {
  795. nullMask = data[pos : pos+maskLen]
  796. for i := 0; i < maskLen; i++ {
  797. nullMask[i] = 0
  798. }
  799. pos += maskLen
  800. }
  801. // newParameterBoundFlag 1 [1 byte]
  802. data[pos] = 0x01
  803. pos++
  804. // type of each parameter [len(args)*2 bytes]
  805. paramTypes := data[pos:]
  806. pos += len(args) * 2
  807. // value of each parameter [n bytes]
  808. paramValues := data[pos:pos]
  809. valuesCap := cap(paramValues)
  810. for i, arg := range args {
  811. // build NULL-bitmap
  812. if arg == nil {
  813. nullMask[i/8] |= 1 << (uint(i) & 7)
  814. paramTypes[i+i] = fieldTypeNULL
  815. paramTypes[i+i+1] = 0x00
  816. continue
  817. }
  818. // cache types and values
  819. switch v := arg.(type) {
  820. case int64:
  821. paramTypes[i+i] = fieldTypeLongLong
  822. paramTypes[i+i+1] = 0x00
  823. if cap(paramValues)-len(paramValues)-8 >= 0 {
  824. paramValues = paramValues[:len(paramValues)+8]
  825. binary.LittleEndian.PutUint64(
  826. paramValues[len(paramValues)-8:],
  827. uint64(v),
  828. )
  829. } else {
  830. paramValues = append(paramValues,
  831. uint64ToBytes(uint64(v))...,
  832. )
  833. }
  834. case float64:
  835. paramTypes[i+i] = fieldTypeDouble
  836. paramTypes[i+i+1] = 0x00
  837. if cap(paramValues)-len(paramValues)-8 >= 0 {
  838. paramValues = paramValues[:len(paramValues)+8]
  839. binary.LittleEndian.PutUint64(
  840. paramValues[len(paramValues)-8:],
  841. math.Float64bits(v),
  842. )
  843. } else {
  844. paramValues = append(paramValues,
  845. uint64ToBytes(math.Float64bits(v))...,
  846. )
  847. }
  848. case bool:
  849. paramTypes[i+i] = fieldTypeTiny
  850. paramTypes[i+i+1] = 0x00
  851. if v {
  852. paramValues = append(paramValues, 0x01)
  853. } else {
  854. paramValues = append(paramValues, 0x00)
  855. }
  856. case []byte:
  857. // Common case (non-nil value) first
  858. if v != nil {
  859. paramTypes[i+i] = fieldTypeString
  860. paramTypes[i+i+1] = 0x00
  861. if len(v) < mc.maxPacketAllowed-pos-len(paramValues)-(len(args)-(i+1))*64 {
  862. paramValues = appendLengthEncodedInteger(paramValues,
  863. uint64(len(v)),
  864. )
  865. paramValues = append(paramValues, v...)
  866. } else {
  867. if err := stmt.writeCommandLongData(i, v); err != nil {
  868. return err
  869. }
  870. }
  871. continue
  872. }
  873. // Handle []byte(nil) as a NULL value
  874. nullMask[i/8] |= 1 << (uint(i) & 7)
  875. paramTypes[i+i] = fieldTypeNULL
  876. paramTypes[i+i+1] = 0x00
  877. case string:
  878. paramTypes[i+i] = fieldTypeString
  879. paramTypes[i+i+1] = 0x00
  880. if len(v) < mc.maxPacketAllowed-pos-len(paramValues)-(len(args)-(i+1))*64 {
  881. paramValues = appendLengthEncodedInteger(paramValues,
  882. uint64(len(v)),
  883. )
  884. paramValues = append(paramValues, v...)
  885. } else {
  886. if err := stmt.writeCommandLongData(i, []byte(v)); err != nil {
  887. return err
  888. }
  889. }
  890. case time.Time:
  891. paramTypes[i+i] = fieldTypeString
  892. paramTypes[i+i+1] = 0x00
  893. var val []byte
  894. if v.IsZero() {
  895. val = []byte("0000-00-00")
  896. } else {
  897. val = []byte(v.In(mc.cfg.Loc).Format(timeFormat))
  898. }
  899. paramValues = appendLengthEncodedInteger(paramValues,
  900. uint64(len(val)),
  901. )
  902. paramValues = append(paramValues, val...)
  903. default:
  904. return fmt.Errorf("can not convert type: %T", arg)
  905. }
  906. }
  907. // Check if param values exceeded the available buffer
  908. // In that case we must build the data packet with the new values buffer
  909. if valuesCap != cap(paramValues) {
  910. data = append(data[:pos], paramValues...)
  911. mc.buf.buf = data
  912. }
  913. pos += len(paramValues)
  914. data = data[:pos]
  915. }
  916. return mc.writePacket(data)
  917. }
  918. func (mc *mysqlConn) discardResults() error {
  919. for mc.status&statusMoreResultsExists != 0 {
  920. resLen, err := mc.readResultSetHeaderPacket()
  921. if err != nil {
  922. return err
  923. }
  924. if resLen > 0 {
  925. // columns
  926. if err := mc.readUntilEOF(); err != nil {
  927. return err
  928. }
  929. // rows
  930. if err := mc.readUntilEOF(); err != nil {
  931. return err
  932. }
  933. } else {
  934. mc.status &^= statusMoreResultsExists
  935. }
  936. }
  937. return nil
  938. }
  939. // http://dev.mysql.com/doc/internals/en/binary-protocol-resultset-row.html
  940. func (rows *binaryRows) readRow(dest []driver.Value) error {
  941. data, err := rows.mc.readPacket()
  942. if err != nil {
  943. return err
  944. }
  945. // packet indicator [1 byte]
  946. if data[0] != iOK {
  947. // EOF Packet
  948. if data[0] == iEOF && len(data) == 5 {
  949. rows.mc.status = readStatus(data[3:])
  950. if err := rows.mc.discardResults(); err != nil {
  951. return err
  952. }
  953. rows.mc = nil
  954. return io.EOF
  955. }
  956. rows.mc = nil
  957. // Error otherwise
  958. return rows.mc.handleErrorPacket(data)
  959. }
  960. // NULL-bitmap, [(column-count + 7 + 2) / 8 bytes]
  961. pos := 1 + (len(dest)+7+2)>>3
  962. nullMask := data[1:pos]
  963. for i := range dest {
  964. // Field is NULL
  965. // (byte >> bit-pos) % 2 == 1
  966. if ((nullMask[(i+2)>>3] >> uint((i+2)&7)) & 1) == 1 {
  967. dest[i] = nil
  968. continue
  969. }
  970. // Convert to byte-coded string
  971. switch rows.columns[i].fieldType {
  972. case fieldTypeNULL:
  973. dest[i] = nil
  974. continue
  975. // Numeric Types
  976. case fieldTypeTiny:
  977. if rows.columns[i].flags&flagUnsigned != 0 {
  978. dest[i] = int64(data[pos])
  979. } else {
  980. dest[i] = int64(int8(data[pos]))
  981. }
  982. pos++
  983. continue
  984. case fieldTypeShort, fieldTypeYear:
  985. if rows.columns[i].flags&flagUnsigned != 0 {
  986. dest[i] = int64(binary.LittleEndian.Uint16(data[pos : pos+2]))
  987. } else {
  988. dest[i] = int64(int16(binary.LittleEndian.Uint16(data[pos : pos+2])))
  989. }
  990. pos += 2
  991. continue
  992. case fieldTypeInt24, fieldTypeLong:
  993. if rows.columns[i].flags&flagUnsigned != 0 {
  994. dest[i] = int64(binary.LittleEndian.Uint32(data[pos : pos+4]))
  995. } else {
  996. dest[i] = int64(int32(binary.LittleEndian.Uint32(data[pos : pos+4])))
  997. }
  998. pos += 4
  999. continue
  1000. case fieldTypeLongLong:
  1001. if rows.columns[i].flags&flagUnsigned != 0 {
  1002. val := binary.LittleEndian.Uint64(data[pos : pos+8])
  1003. if val > math.MaxInt64 {
  1004. dest[i] = uint64ToString(val)
  1005. } else {
  1006. dest[i] = int64(val)
  1007. }
  1008. } else {
  1009. dest[i] = int64(binary.LittleEndian.Uint64(data[pos : pos+8]))
  1010. }
  1011. pos += 8
  1012. continue
  1013. case fieldTypeFloat:
  1014. dest[i] = float64(math.Float32frombits(binary.LittleEndian.Uint32(data[pos : pos+4])))
  1015. pos += 4
  1016. continue
  1017. case fieldTypeDouble:
  1018. dest[i] = math.Float64frombits(binary.LittleEndian.Uint64(data[pos : pos+8]))
  1019. pos += 8
  1020. continue
  1021. // Length coded Binary Strings
  1022. case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar,
  1023. fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB,
  1024. fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB,
  1025. fieldTypeVarString, fieldTypeString, fieldTypeGeometry, fieldTypeJSON:
  1026. var isNull bool
  1027. var n int
  1028. dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
  1029. pos += n
  1030. if err == nil {
  1031. if !isNull {
  1032. continue
  1033. } else {
  1034. dest[i] = nil
  1035. continue
  1036. }
  1037. }
  1038. return err
  1039. case
  1040. fieldTypeDate, fieldTypeNewDate, // Date YYYY-MM-DD
  1041. fieldTypeTime, // Time [-][H]HH:MM:SS[.fractal]
  1042. fieldTypeTimestamp, fieldTypeDateTime: // Timestamp YYYY-MM-DD HH:MM:SS[.fractal]
  1043. num, isNull, n := readLengthEncodedInteger(data[pos:])
  1044. pos += n
  1045. switch {
  1046. case isNull:
  1047. dest[i] = nil
  1048. continue
  1049. case rows.columns[i].fieldType == fieldTypeTime:
  1050. // database/sql does not support an equivalent to TIME, return a string
  1051. var dstlen uint8
  1052. switch decimals := rows.columns[i].decimals; decimals {
  1053. case 0x00, 0x1f:
  1054. dstlen = 8
  1055. case 1, 2, 3, 4, 5, 6:
  1056. dstlen = 8 + 1 + decimals
  1057. default:
  1058. return fmt.Errorf(
  1059. "protocol error, illegal decimals value %d",
  1060. rows.columns[i].decimals,
  1061. )
  1062. }
  1063. dest[i], err = formatBinaryDateTime(data[pos:pos+int(num)], dstlen, true)
  1064. case rows.mc.parseTime:
  1065. dest[i], err = parseBinaryDateTime(num, data[pos:], rows.mc.cfg.Loc)
  1066. default:
  1067. var dstlen uint8
  1068. if rows.columns[i].fieldType == fieldTypeDate {
  1069. dstlen = 10
  1070. } else {
  1071. switch decimals := rows.columns[i].decimals; decimals {
  1072. case 0x00, 0x1f:
  1073. dstlen = 19
  1074. case 1, 2, 3, 4, 5, 6:
  1075. dstlen = 19 + 1 + decimals
  1076. default:
  1077. return fmt.Errorf(
  1078. "protocol error, illegal decimals value %d",
  1079. rows.columns[i].decimals,
  1080. )
  1081. }
  1082. }
  1083. dest[i], err = formatBinaryDateTime(data[pos:pos+int(num)], dstlen, false)
  1084. }
  1085. if err == nil {
  1086. pos += int(num)
  1087. continue
  1088. } else {
  1089. return err
  1090. }
  1091. // Please report if this happens!
  1092. default:
  1093. return fmt.Errorf("unknown field type %d", rows.columns[i].fieldType)
  1094. }
  1095. }
  1096. return nil
  1097. }