real_decoder.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package sarama
  2. import (
  3. "encoding/binary"
  4. "math"
  5. )
  6. var errInvalidArrayLength = PacketDecodingError{"invalid array length"}
  7. var errInvalidByteSliceLength = PacketDecodingError{"invalid byteslice length"}
  8. var errInvalidByteSliceLengthType = PacketDecodingError{"invalid byteslice length type"}
  9. var errInvalidStringLength = PacketDecodingError{"invalid string length"}
  10. var errInvalidSubsetSize = PacketDecodingError{"invalid subset size"}
  11. var errVarintOverflow = PacketDecodingError{"varint overflow"}
  12. var errInvalidBool = PacketDecodingError{"invalid bool"}
  13. type realDecoder struct {
  14. raw []byte
  15. off int
  16. stack []pushDecoder
  17. }
  18. // primitives
  19. func (rd *realDecoder) getInt8() (int8, error) {
  20. if rd.remaining() < 1 {
  21. rd.off = len(rd.raw)
  22. return -1, ErrInsufficientData
  23. }
  24. tmp := int8(rd.raw[rd.off])
  25. rd.off++
  26. return tmp, nil
  27. }
  28. func (rd *realDecoder) getInt16() (int16, error) {
  29. if rd.remaining() < 2 {
  30. rd.off = len(rd.raw)
  31. return -1, ErrInsufficientData
  32. }
  33. tmp := int16(binary.BigEndian.Uint16(rd.raw[rd.off:]))
  34. rd.off += 2
  35. return tmp, nil
  36. }
  37. func (rd *realDecoder) getInt32() (int32, error) {
  38. if rd.remaining() < 4 {
  39. rd.off = len(rd.raw)
  40. return -1, ErrInsufficientData
  41. }
  42. tmp := int32(binary.BigEndian.Uint32(rd.raw[rd.off:]))
  43. rd.off += 4
  44. return tmp, nil
  45. }
  46. func (rd *realDecoder) getInt64() (int64, error) {
  47. if rd.remaining() < 8 {
  48. rd.off = len(rd.raw)
  49. return -1, ErrInsufficientData
  50. }
  51. tmp := int64(binary.BigEndian.Uint64(rd.raw[rd.off:]))
  52. rd.off += 8
  53. return tmp, nil
  54. }
  55. func (rd *realDecoder) getVarint() (int64, error) {
  56. tmp, n := binary.Varint(rd.raw[rd.off:])
  57. if n == 0 {
  58. rd.off = len(rd.raw)
  59. return -1, ErrInsufficientData
  60. }
  61. if n < 0 {
  62. rd.off -= n
  63. return -1, errVarintOverflow
  64. }
  65. rd.off += n
  66. return tmp, nil
  67. }
  68. func (rd *realDecoder) getArrayLength() (int, error) {
  69. if rd.remaining() < 4 {
  70. rd.off = len(rd.raw)
  71. return -1, ErrInsufficientData
  72. }
  73. tmp := int(int32(binary.BigEndian.Uint32(rd.raw[rd.off:])))
  74. rd.off += 4
  75. if tmp > rd.remaining() {
  76. rd.off = len(rd.raw)
  77. return -1, ErrInsufficientData
  78. } else if tmp > 2*math.MaxUint16 {
  79. return -1, errInvalidArrayLength
  80. }
  81. return tmp, nil
  82. }
  83. func (rd *realDecoder) getBool() (bool, error) {
  84. b, err := rd.getInt8()
  85. if err != nil || b == 0 {
  86. return false, err
  87. }
  88. if b != 1 {
  89. return false, errInvalidBool
  90. }
  91. return true, nil
  92. }
  93. // collections
  94. func (rd *realDecoder) getBytes() ([]byte, error) {
  95. tmp, err := rd.getInt32()
  96. if err != nil {
  97. return nil, err
  98. }
  99. if tmp == -1 {
  100. return nil, nil
  101. }
  102. return rd.getRawBytes(int(tmp))
  103. }
  104. func (rd *realDecoder) getVarintBytes() ([]byte, error) {
  105. tmp, err := rd.getVarint()
  106. if err != nil {
  107. return nil, err
  108. }
  109. if tmp == -1 {
  110. return nil, nil
  111. }
  112. return rd.getRawBytes(int(tmp))
  113. }
  114. func (rd *realDecoder) getStringLength() (int, error) {
  115. length, err := rd.getInt16()
  116. if err != nil {
  117. return 0, err
  118. }
  119. n := int(length)
  120. switch {
  121. case n < -1:
  122. return 0, errInvalidStringLength
  123. case n > rd.remaining():
  124. rd.off = len(rd.raw)
  125. return 0, ErrInsufficientData
  126. }
  127. return n, nil
  128. }
  129. func (rd *realDecoder) getString() (string, error) {
  130. n, err := rd.getStringLength()
  131. if err != nil || n == -1 {
  132. return "", err
  133. }
  134. tmpStr := string(rd.raw[rd.off : rd.off+n])
  135. rd.off += n
  136. return tmpStr, nil
  137. }
  138. func (rd *realDecoder) getNullableString() (*string, error) {
  139. n, err := rd.getStringLength()
  140. if err != nil || n == -1 {
  141. return nil, err
  142. }
  143. tmpStr := string(rd.raw[rd.off : rd.off+n])
  144. rd.off += n
  145. return &tmpStr, err
  146. }
  147. func (rd *realDecoder) getInt32Array() ([]int32, error) {
  148. if rd.remaining() < 4 {
  149. rd.off = len(rd.raw)
  150. return nil, ErrInsufficientData
  151. }
  152. n := int(binary.BigEndian.Uint32(rd.raw[rd.off:]))
  153. rd.off += 4
  154. if rd.remaining() < 4*n {
  155. rd.off = len(rd.raw)
  156. return nil, ErrInsufficientData
  157. }
  158. if n == 0 {
  159. return nil, nil
  160. }
  161. if n < 0 {
  162. return nil, errInvalidArrayLength
  163. }
  164. ret := make([]int32, n)
  165. for i := range ret {
  166. ret[i] = int32(binary.BigEndian.Uint32(rd.raw[rd.off:]))
  167. rd.off += 4
  168. }
  169. return ret, nil
  170. }
  171. func (rd *realDecoder) getInt64Array() ([]int64, error) {
  172. if rd.remaining() < 4 {
  173. rd.off = len(rd.raw)
  174. return nil, ErrInsufficientData
  175. }
  176. n := int(binary.BigEndian.Uint32(rd.raw[rd.off:]))
  177. rd.off += 4
  178. if rd.remaining() < 8*n {
  179. rd.off = len(rd.raw)
  180. return nil, ErrInsufficientData
  181. }
  182. if n == 0 {
  183. return nil, nil
  184. }
  185. if n < 0 {
  186. return nil, errInvalidArrayLength
  187. }
  188. ret := make([]int64, n)
  189. for i := range ret {
  190. ret[i] = int64(binary.BigEndian.Uint64(rd.raw[rd.off:]))
  191. rd.off += 8
  192. }
  193. return ret, nil
  194. }
  195. func (rd *realDecoder) getStringArray() ([]string, error) {
  196. if rd.remaining() < 4 {
  197. rd.off = len(rd.raw)
  198. return nil, ErrInsufficientData
  199. }
  200. n := int(binary.BigEndian.Uint32(rd.raw[rd.off:]))
  201. rd.off += 4
  202. if n == 0 {
  203. return nil, nil
  204. }
  205. if n < 0 {
  206. return nil, errInvalidArrayLength
  207. }
  208. ret := make([]string, n)
  209. for i := range ret {
  210. str, err := rd.getString()
  211. if err != nil {
  212. return nil, err
  213. }
  214. ret[i] = str
  215. }
  216. return ret, nil
  217. }
  218. // subsets
  219. func (rd *realDecoder) remaining() int {
  220. return len(rd.raw) - rd.off
  221. }
  222. func (rd *realDecoder) getSubset(length int) (packetDecoder, error) {
  223. buf, err := rd.getRawBytes(length)
  224. if err != nil {
  225. return nil, err
  226. }
  227. return &realDecoder{raw: buf}, nil
  228. }
  229. func (rd *realDecoder) getRawBytes(length int) ([]byte, error) {
  230. if length < 0 {
  231. return nil, errInvalidByteSliceLength
  232. } else if length > rd.remaining() {
  233. rd.off = len(rd.raw)
  234. return nil, ErrInsufficientData
  235. }
  236. start := rd.off
  237. rd.off += length
  238. return rd.raw[start:rd.off], nil
  239. }
  240. func (rd *realDecoder) peek(offset, length int) (packetDecoder, error) {
  241. if rd.remaining() < offset+length {
  242. return nil, ErrInsufficientData
  243. }
  244. off := rd.off + offset
  245. return &realDecoder{raw: rd.raw[off : off+length]}, nil
  246. }
  247. // stacks
  248. func (rd *realDecoder) push(in pushDecoder) error {
  249. in.saveOffset(rd.off)
  250. var reserve int
  251. if dpd, ok := in.(dynamicPushDecoder); ok {
  252. if err := dpd.decode(rd); err != nil {
  253. return err
  254. }
  255. } else {
  256. reserve = in.reserveLength()
  257. if rd.remaining() < reserve {
  258. rd.off = len(rd.raw)
  259. return ErrInsufficientData
  260. }
  261. }
  262. rd.stack = append(rd.stack, in)
  263. rd.off += reserve
  264. return nil
  265. }
  266. func (rd *realDecoder) pop() error {
  267. // this is go's ugly pop pattern (the inverse of append)
  268. in := rd.stack[len(rd.stack)-1]
  269. rd.stack = rd.stack[:len(rd.stack)-1]
  270. return in.check(rd.off, rd.raw)
  271. }