Skip to content

Commit

Permalink
zstd: Shorten checksum code (#795)
Browse files Browse the repository at this point in the history
frameDec.consumeCRC is now inlineable. Its callers already check
for ignoreChecksum. frameDec.checkCRC's callers already check for
HasCheckSum and ignoreChecksum.

xxhash.Digest.Write is documented as always returning len(b), nil,
so no need to check its return values.
  • Loading branch information
greatroar committed Mar 26, 2023
1 parent 6c7dd07 commit 5753c12
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
7 changes: 1 addition & 6 deletions zstd/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,7 @@ func (d *Decoder) nextBlock(blocking bool) (ok bool) {
}

if len(next.b) > 0 {
n, err := d.current.crc.Write(next.b)
if err == nil {
if n != len(next.b) {
d.current.err = io.ErrShortWrite
}
}
d.current.crc.Write(next.b)
}
if next.err == nil && next.d != nil && next.d.hasCRC {
got := uint32(d.current.crc.Sum64())
Expand Down
35 changes: 8 additions & 27 deletions zstd/framedec.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,24 +293,16 @@ func (d *frameDec) next(block *blockDec) error {
return nil
}

// checkCRC will check the checksum if the frame has one.
// checkCRC will check the checksum, assuming the frame has one.
// Will return ErrCRCMismatch if crc check failed, otherwise nil.
func (d *frameDec) checkCRC() error {
if !d.HasCheckSum {
return nil
}

// We can overwrite upper tmp now
buf, err := d.rawInput.readSmall(4)
if err != nil {
println("CRC missing?", err)
return err
}

if d.o.ignoreChecksum {
return nil
}

want := binary.LittleEndian.Uint32(buf[:4])
got := uint32(d.crc.Sum64())

Expand All @@ -326,17 +318,13 @@ func (d *frameDec) checkCRC() error {
return nil
}

// consumeCRC reads the checksum data if the frame has one.
// consumeCRC skips over the checksum, assuming the frame has one.
func (d *frameDec) consumeCRC() error {
if d.HasCheckSum {
_, err := d.rawInput.readSmall(4)
if err != nil {
println("CRC missing?", err)
return err
}
_, err := d.rawInput.readSmall(4)
if err != nil {
println("CRC missing?", err)
}

return nil
return err
}

// runDecoder will run the decoder for the remainder of the frame.
Expand Down Expand Up @@ -415,15 +403,8 @@ func (d *frameDec) runDecoder(dst []byte, dec *blockDec) ([]byte, error) {
if d.o.ignoreChecksum {
err = d.consumeCRC()
} else {
var n int
n, err = d.crc.Write(dst[crcStart:])
if err == nil {
if n != len(dst)-crcStart {
err = io.ErrShortWrite
} else {
err = d.checkCRC()
}
}
d.crc.Write(dst[crcStart:])
err = d.checkCRC()
}
}
}
Expand Down

0 comments on commit 5753c12

Please sign in to comment.