You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
4.9 KiB

  1. package mssql
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "io"
  6. )
  7. type packetType uint8
  8. type header struct {
  9. PacketType packetType
  10. Status uint8
  11. Size uint16
  12. Spid uint16
  13. PacketNo uint8
  14. Pad uint8
  15. }
  16. // tdsBuffer reads and writes TDS packets of data to the transport.
  17. // The write and read buffers are separate to make sending attn signals
  18. // possible without locks. Currently attn signals are only sent during
  19. // reads, not writes.
  20. type tdsBuffer struct {
  21. transport io.ReadWriteCloser
  22. packetSize int
  23. // Write fields.
  24. wbuf []byte
  25. wpos int
  26. wPacketSeq byte
  27. wPacketType packetType
  28. // Read fields.
  29. rbuf []byte
  30. rpos int
  31. rsize int
  32. final bool
  33. rPacketType packetType
  34. // afterFirst is assigned to right after tdsBuffer is created and
  35. // before the first use. It is executed after the first packet is
  36. // written and then removed.
  37. afterFirst func()
  38. }
  39. func newTdsBuffer(bufsize uint16, transport io.ReadWriteCloser) *tdsBuffer {
  40. return &tdsBuffer{
  41. packetSize: int(bufsize),
  42. wbuf: make([]byte, 1<<16),
  43. rbuf: make([]byte, 1<<16),
  44. rpos: 8,
  45. transport: transport,
  46. }
  47. }
  48. func (rw *tdsBuffer) ResizeBuffer(packetSize int) {
  49. rw.packetSize = packetSize
  50. }
  51. func (w *tdsBuffer) PackageSize() int {
  52. return w.packetSize
  53. }
  54. func (w *tdsBuffer) flush() (err error) {
  55. // Write packet size.
  56. w.wbuf[0] = byte(w.wPacketType)
  57. binary.BigEndian.PutUint16(w.wbuf[2:], uint16(w.wpos))
  58. w.wbuf[6] = w.wPacketSeq
  59. // Write packet into underlying transport.
  60. if _, err = w.transport.Write(w.wbuf[:w.wpos]); err != nil {
  61. return err
  62. }
  63. // It is possible to create a whole new buffer after a flush.
  64. // Useful for debugging. Normally reuse the buffer.
  65. // w.wbuf = make([]byte, 1<<16)
  66. // Execute afterFirst hook if it is set.
  67. if w.afterFirst != nil {
  68. w.afterFirst()
  69. w.afterFirst = nil
  70. }
  71. w.wpos = 8
  72. w.wPacketSeq++
  73. return nil
  74. }
  75. func (w *tdsBuffer) Write(p []byte) (total int, err error) {
  76. for {
  77. copied := copy(w.wbuf[w.wpos:w.packetSize], p)
  78. w.wpos += copied
  79. total += copied
  80. if copied == len(p) {
  81. return
  82. }
  83. if err = w.flush(); err != nil {
  84. return
  85. }
  86. p = p[copied:]
  87. }
  88. return
  89. }
  90. func (w *tdsBuffer) WriteByte(b byte) error {
  91. if int(w.wpos) == len(w.wbuf) {
  92. if err := w.flush(); err != nil {
  93. return err
  94. }
  95. }
  96. w.wbuf[w.wpos] = b
  97. w.wpos += 1
  98. return nil
  99. }
  100. func (w *tdsBuffer) BeginPacket(packetType packetType) {
  101. w.wbuf[1] = 0 // Packet is incomplete. This byte is set again in FinishPacket.
  102. w.wpos = 8
  103. w.wPacketSeq = 1
  104. w.wPacketType = packetType
  105. }
  106. func (w *tdsBuffer) FinishPacket() error {
  107. w.wbuf[1] = 1 // Mark this as the last packet in the message.
  108. return w.flush()
  109. }
  110. var headerSize = binary.Size(header{})
  111. func (r *tdsBuffer) readNextPacket() error {
  112. h := header{}
  113. var err error
  114. err = binary.Read(r.transport, binary.BigEndian, &h)
  115. if err != nil {
  116. return err
  117. }
  118. if int(h.Size) > len(r.rbuf) {
  119. return errors.New("Invalid packet size, it is longer than buffer size")
  120. }
  121. if headerSize > int(h.Size) {
  122. return errors.New("Invalid packet size, it is shorter than header size")
  123. }
  124. _, err = io.ReadFull(r.transport, r.rbuf[headerSize:h.Size])
  125. if err != nil {
  126. return err
  127. }
  128. r.rpos = headerSize
  129. r.rsize = int(h.Size)
  130. r.final = h.Status != 0
  131. r.rPacketType = h.PacketType
  132. return nil
  133. }
  134. func (r *tdsBuffer) BeginRead() (packetType, error) {
  135. err := r.readNextPacket()
  136. if err != nil {
  137. return 0, err
  138. }
  139. return r.rPacketType, nil
  140. }
  141. func (r *tdsBuffer) ReadByte() (res byte, err error) {
  142. if r.rpos == r.rsize {
  143. if r.final {
  144. return 0, io.EOF
  145. }
  146. err = r.readNextPacket()
  147. if err != nil {
  148. return 0, err
  149. }
  150. }
  151. res = r.rbuf[r.rpos]
  152. r.rpos++
  153. return res, nil
  154. }
  155. func (r *tdsBuffer) byte() byte {
  156. b, err := r.ReadByte()
  157. if err != nil {
  158. badStreamPanic(err)
  159. }
  160. return b
  161. }
  162. func (r *tdsBuffer) ReadFull(buf []byte) {
  163. _, err := io.ReadFull(r, buf[:])
  164. if err != nil {
  165. badStreamPanic(err)
  166. }
  167. }
  168. func (r *tdsBuffer) uint64() uint64 {
  169. var buf [8]byte
  170. r.ReadFull(buf[:])
  171. return binary.LittleEndian.Uint64(buf[:])
  172. }
  173. func (r *tdsBuffer) int32() int32 {
  174. return int32(r.uint32())
  175. }
  176. func (r *tdsBuffer) uint32() uint32 {
  177. var buf [4]byte
  178. r.ReadFull(buf[:])
  179. return binary.LittleEndian.Uint32(buf[:])
  180. }
  181. func (r *tdsBuffer) uint16() uint16 {
  182. var buf [2]byte
  183. r.ReadFull(buf[:])
  184. return binary.LittleEndian.Uint16(buf[:])
  185. }
  186. func (r *tdsBuffer) BVarChar() string {
  187. l := int(r.byte())
  188. return r.readUcs2(l)
  189. }
  190. func (r *tdsBuffer) UsVarChar() string {
  191. l := int(r.uint16())
  192. return r.readUcs2(l)
  193. }
  194. func (r *tdsBuffer) readUcs2(numchars int) string {
  195. b := make([]byte, numchars*2)
  196. r.ReadFull(b)
  197. res, err := ucs22str(b)
  198. if err != nil {
  199. badStreamPanic(err)
  200. }
  201. return res
  202. }
  203. func (r *tdsBuffer) Read(buf []byte) (copied int, err error) {
  204. copied = 0
  205. err = nil
  206. if r.rpos == r.rsize {
  207. if r.final {
  208. return 0, io.EOF
  209. }
  210. err = r.readNextPacket()
  211. if err != nil {
  212. return
  213. }
  214. }
  215. copied = copy(buf, r.rbuf[r.rpos:r.rsize])
  216. r.rpos += copied
  217. return
  218. }