real_decoder.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 errUVarintOverflow = PacketDecodingError{"uvarint overflow"}
  11. var errInvalidBool = PacketDecodingError{"invalid bool"}
  12. var errUnsupportedTaggedFields = PacketDecodingError{"non-empty tagged fields are not supported yet"}
  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) getUVarint() (uint64, error) {
  69. tmp, n := binary.Uvarint(rd.raw[rd.off:])
  70. if n == 0 {
  71. rd.off = len(rd.raw)
  72. return 0, ErrInsufficientData
  73. }
  74. if n < 0 {
  75. rd.off -= n
  76. return 0, errUVarintOverflow
  77. }
  78. rd.off += n
  79. return tmp, nil
  80. }
  81. func (rd *realDecoder) getArrayLength() (int, error) {
  82. if rd.remaining() < 4 {
  83. rd.off = len(rd.raw)
  84. return -1, ErrInsufficientData
  85. }
  86. tmp := int(int32(binary.BigEndian.Uint32(rd.raw[rd.off:])))
  87. rd.off += 4
  88. if tmp > rd.remaining() {
  89. rd.off = len(rd.raw)
  90. return -1, ErrInsufficientData
  91. } else if tmp > 2*math.MaxUint16 {
  92. return -1, errInvalidArrayLength
  93. }
  94. return tmp, nil
  95. }
  96. func (rd *realDecoder) getCompactArrayLength() (int, error) {
  97. n, err := rd.getUVarint()
  98. if err != nil {
  99. return 0, err
  100. }
  101. if n == 0 {
  102. return 0, nil
  103. }
  104. return int(n) - 1, nil
  105. }
  106. func (rd *realDecoder) getBool() (bool, error) {
  107. b, err := rd.getInt8()
  108. if err != nil || b == 0 {
  109. return false, err
  110. }
  111. if b != 1 {
  112. return false, errInvalidBool
  113. }
  114. return true, nil
  115. }
  116. func (rd *realDecoder) getEmptyTaggedFieldArray() (int, error) {
  117. tagCount, err := rd.getUVarint()
  118. if err != nil {
  119. return 0, err
  120. }
  121. if tagCount != 0 {
  122. return 0, errUnsupportedTaggedFields
  123. }
  124. return 0, nil
  125. }
  126. // collections
  127. func (rd *realDecoder) getBytes() ([]byte, error) {
  128. tmp, err := rd.getInt32()
  129. if err != nil {
  130. return nil, err
  131. }
  132. if tmp == -1 {
  133. return nil, nil
  134. }
  135. return rd.getRawBytes(int(tmp))
  136. }
  137. func (rd *realDecoder) getVarintBytes() ([]byte, error) {
  138. tmp, err := rd.getVarint()
  139. if err != nil {
  140. return nil, err
  141. }
  142. if tmp == -1 {
  143. return nil, nil
  144. }
  145. return rd.getRawBytes(int(tmp))
  146. }
  147. func (rd *realDecoder) getStringLength() (int, error) {
  148. length, err := rd.getInt16()
  149. if err != nil {
  150. return 0, err
  151. }
  152. n := int(length)
  153. switch {
  154. case n < -1:
  155. return 0, errInvalidStringLength
  156. case n > rd.remaining():
  157. rd.off = len(rd.raw)
  158. return 0, ErrInsufficientData
  159. }
  160. return n, nil
  161. }
  162. func (rd *realDecoder) getString() (string, error) {
  163. n, err := rd.getStringLength()
  164. if err != nil || n == -1 {
  165. return "", err
  166. }
  167. tmpStr := string(rd.raw[rd.off : rd.off+n])
  168. rd.off += n
  169. return tmpStr, nil
  170. }
  171. func (rd *realDecoder) getNullableString() (*string, error) {
  172. n, err := rd.getStringLength()
  173. if err != nil || n == -1 {
  174. return nil, err
  175. }
  176. tmpStr := string(rd.raw[rd.off : rd.off+n])
  177. rd.off += n
  178. return &tmpStr, err
  179. }
  180. func (rd *realDecoder) getCompactString() (string, error) {
  181. n, err := rd.getUVarint()
  182. if err != nil {
  183. return "", err
  184. }
  185. var length = int(n - 1)
  186. tmpStr := string(rd.raw[rd.off : rd.off+length])
  187. rd.off += length
  188. return tmpStr, nil
  189. }
  190. func (rd *realDecoder) getCompactNullableString() (*string, error) {
  191. n, err := rd.getUVarint()
  192. if err != nil {
  193. return nil, err
  194. }
  195. var length = int(n - 1)
  196. if length < 0 {
  197. return nil, err
  198. }
  199. tmpStr := string(rd.raw[rd.off : rd.off+length])
  200. rd.off += length
  201. return &tmpStr, err
  202. }
  203. func (rd *realDecoder) getCompactInt32Array() ([]int32, error) {
  204. n, err := rd.getUVarint()
  205. if err != nil {
  206. return nil, err
  207. }
  208. if n == 0 {
  209. return nil, nil
  210. }
  211. arrayLength := int(n) - 1
  212. ret := make([]int32, arrayLength)
  213. for i := range ret {
  214. ret[i] = int32(binary.BigEndian.Uint32(rd.raw[rd.off:]))
  215. rd.off += 4
  216. }
  217. return ret, nil
  218. }
  219. func (rd *realDecoder) getInt32Array() ([]int32, error) {
  220. if rd.remaining() < 4 {
  221. rd.off = len(rd.raw)
  222. return nil, ErrInsufficientData
  223. }
  224. n := int(binary.BigEndian.Uint32(rd.raw[rd.off:]))
  225. rd.off += 4
  226. if rd.remaining() < 4*n {
  227. rd.off = len(rd.raw)
  228. return nil, ErrInsufficientData
  229. }
  230. if n == 0 {
  231. return nil, nil
  232. }
  233. if n < 0 {
  234. return nil, errInvalidArrayLength
  235. }
  236. ret := make([]int32, n)
  237. for i := range ret {
  238. ret[i] = int32(binary.BigEndian.Uint32(rd.raw[rd.off:]))
  239. rd.off += 4
  240. }
  241. return ret, nil
  242. }
  243. func (rd *realDecoder) getInt64Array() ([]int64, error) {
  244. if rd.remaining() < 4 {
  245. rd.off = len(rd.raw)
  246. return nil, ErrInsufficientData
  247. }
  248. n := int(binary.BigEndian.Uint32(rd.raw[rd.off:]))
  249. rd.off += 4
  250. if rd.remaining() < 8*n {
  251. rd.off = len(rd.raw)
  252. return nil, ErrInsufficientData
  253. }
  254. if n == 0 {
  255. return nil, nil
  256. }
  257. if n < 0 {
  258. return nil, errInvalidArrayLength
  259. }
  260. ret := make([]int64, n)
  261. for i := range ret {
  262. ret[i] = int64(binary.BigEndian.Uint64(rd.raw[rd.off:]))
  263. rd.off += 8
  264. }
  265. return ret, nil
  266. }
  267. func (rd *realDecoder) getStringArray() ([]string, error) {
  268. if rd.remaining() < 4 {
  269. rd.off = len(rd.raw)
  270. return nil, ErrInsufficientData
  271. }
  272. n := int(binary.BigEndian.Uint32(rd.raw[rd.off:]))
  273. rd.off += 4
  274. if n == 0 {
  275. return nil, nil
  276. }
  277. if n < 0 {
  278. return nil, errInvalidArrayLength
  279. }
  280. ret := make([]string, n)
  281. for i := range ret {
  282. str, err := rd.getString()
  283. if err != nil {
  284. return nil, err
  285. }
  286. ret[i] = str
  287. }
  288. return ret, nil
  289. }
  290. // subsets
  291. func (rd *realDecoder) remaining() int {
  292. return len(rd.raw) - rd.off
  293. }
  294. func (rd *realDecoder) getSubset(length int) (packetDecoder, error) {
  295. buf, err := rd.getRawBytes(length)
  296. if err != nil {
  297. return nil, err
  298. }
  299. return &realDecoder{raw: buf}, nil
  300. }
  301. func (rd *realDecoder) getRawBytes(length int) ([]byte, error) {
  302. if length < 0 {
  303. return nil, errInvalidByteSliceLength
  304. } else if length > rd.remaining() {
  305. rd.off = len(rd.raw)
  306. return nil, ErrInsufficientData
  307. }
  308. start := rd.off
  309. rd.off += length
  310. return rd.raw[start:rd.off], nil
  311. }
  312. func (rd *realDecoder) peek(offset, length int) (packetDecoder, error) {
  313. if rd.remaining() < offset+length {
  314. return nil, ErrInsufficientData
  315. }
  316. off := rd.off + offset
  317. return &realDecoder{raw: rd.raw[off : off+length]}, nil
  318. }
  319. func (rd *realDecoder) peekInt8(offset int) (int8, error) {
  320. const byteLen = 1
  321. if rd.remaining() < offset+byteLen {
  322. return -1, ErrInsufficientData
  323. }
  324. return int8(rd.raw[rd.off+offset]), nil
  325. }
  326. // stacks
  327. func (rd *realDecoder) push(in pushDecoder) error {
  328. in.saveOffset(rd.off)
  329. var reserve int
  330. if dpd, ok := in.(dynamicPushDecoder); ok {
  331. if err := dpd.decode(rd); err != nil {
  332. return err
  333. }
  334. } else {
  335. reserve = in.reserveLength()
  336. if rd.remaining() < reserve {
  337. rd.off = len(rd.raw)
  338. return ErrInsufficientData
  339. }
  340. }
  341. rd.stack = append(rd.stack, in)
  342. rd.off += reserve
  343. return nil
  344. }
  345. func (rd *realDecoder) pop() error {
  346. // this is go's ugly pop pattern (the inverse of append)
  347. in := rd.stack[len(rd.stack)-1]
  348. rd.stack = rd.stack[:len(rd.stack)-1]
  349. return in.check(rd.off, rd.raw)
  350. }