real_decoder.go 8.5 KB

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