real_decoder.go 6.3 KB

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