| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package protocol
- import (
- "encoding/binary"
- "hash/crc32"
- )
- type crc32Encoder struct {
- startOffset int
- }
- func (c *crc32Encoder) saveOffset(in int) {
- c.startOffset = in
- }
- func (c *crc32Encoder) reserveLength() int {
- return 4
- }
- func (c *crc32Encoder) run(curOffset int, buf []byte) {
- crc := crc32.ChecksumIEEE(buf[c.startOffset+4 : curOffset])
- binary.BigEndian.PutUint32(buf[c.startOffset:], crc)
- }
- type crc32Decoder struct {
- startOffset int
- }
- func (c *crc32Decoder) saveOffset(in int) {
- c.startOffset = in
- }
- func (c *crc32Decoder) reserveLength() int {
- return 4
- }
- func (c *crc32Decoder) check(curOffset int, buf []byte) error {
- crc := crc32.ChecksumIEEE(buf[c.startOffset+4 : curOffset])
- if crc != binary.BigEndian.Uint32(buf[c.startOffset:]) {
- return DecodingError("CRC did not match.")
- }
- return nil
- }
|