Skip to content

Commit

Permalink
s2: Add ReaderIgnoreCRC (#609)
Browse files Browse the repository at this point in the history
Will skip all CRC calculations when decoding frames.
  • Loading branch information
klauspost committed Jun 1, 2022
1 parent 3122d7a commit 30a82ca
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions s2/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ func ReaderSkippableCB(id uint8, fn func(r io.Reader) error) ReaderOption {
}
}

// ReaderIgnoreCRC will make the reader skip CRC calculation and checks.
func ReaderIgnoreCRC() ReaderOption {
return func(r *Reader) error {
r.ignoreCRC = true
return nil
}
}

// Reader is an io.Reader that can read Snappy-compressed bytes.
type Reader struct {
r io.Reader
Expand All @@ -193,6 +201,7 @@ type Reader struct {
paramsOK bool
snappyFrame bool
ignoreStreamID bool
ignoreCRC bool
}

// ensureBufferSize will ensure that the buffer can take at least n bytes.
Expand Down Expand Up @@ -347,7 +356,7 @@ func (r *Reader) Read(p []byte) (int, error) {
r.err = err
return 0, r.err
}
if crc(r.decoded[:n]) != checksum {
if !r.ignoreCRC && crc(r.decoded[:n]) != checksum {
r.err = ErrCRC
return 0, r.err
}
Expand Down Expand Up @@ -388,7 +397,7 @@ func (r *Reader) Read(p []byte) (int, error) {
if !r.readFull(r.decoded[:n], false) {
return 0, r.err
}
if crc(r.decoded[:n]) != checksum {
if !r.ignoreCRC && crc(r.decoded[:n]) != checksum {
r.err = ErrCRC
return 0, r.err
}
Expand Down Expand Up @@ -594,7 +603,7 @@ func (r *Reader) DecodeConcurrent(w io.Writer, concurrent int) (written int64, e
setErr(err)
return
}
if crc(decoded) != checksum {
if !r.ignoreCRC && crc(decoded) != checksum {
writtenBlocks <- decoded
setErr(ErrCRC)
return
Expand Down Expand Up @@ -638,7 +647,7 @@ func (r *Reader) DecodeConcurrent(w io.Writer, concurrent int) (written int64, e
return 0, r.err
}

if crc(buf) != checksum {
if !r.ignoreCRC && crc(buf) != checksum {
r.err = ErrCRC
return 0, r.err
}
Expand Down

0 comments on commit 30a82ca

Please sign in to comment.