real_decoder.go 5.4 KB

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