packets.go 31 KB

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