real_decoder.go 4.8 KB

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