crc32_field.go 775 B

123456789101112131415161718192021222324252627282930313233343536
  1. package sarama
  2. import (
  3. "encoding/binary"
  4. "github.com/klauspost/crc32"
  5. )
  6. // crc32Field implements the pushEncoder and pushDecoder interfaces for calculating CRC32s.
  7. type crc32Field struct {
  8. startOffset int
  9. }
  10. func (c *crc32Field) saveOffset(in int) {
  11. c.startOffset = in
  12. }
  13. func (c *crc32Field) reserveLength() int {
  14. return 4
  15. }
  16. func (c *crc32Field) run(curOffset int, buf []byte) error {
  17. crc := crc32.ChecksumIEEE(buf[c.startOffset+4 : curOffset])
  18. binary.BigEndian.PutUint32(buf[c.startOffset:], crc)
  19. return nil
  20. }
  21. func (c *crc32Field) check(curOffset int, buf []byte) error {
  22. crc := crc32.ChecksumIEEE(buf[c.startOffset+4 : curOffset])
  23. if crc != binary.BigEndian.Uint32(buf[c.startOffset:]) {
  24. return PacketDecodingError{"CRC didn't match"}
  25. }
  26. return nil
  27. }