Skip to content

Commit

Permalink
Create utility function CRC32Equal()
Browse files Browse the repository at this point in the history
  • Loading branch information
bodgit committed Apr 30, 2022
1 parent e4d5ed4 commit 5766fbc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions internal/util/checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package util

import "bytes"

// CRC32Equal compares CRC32 checksums.
func CRC32Equal(b []byte, c uint32) bool {
return bytes.Equal(b, []byte{byte(0xff & (c >> 24)), byte(0xff & (c >> 16)), byte(0xff & (c >> 8)), byte(0xff & c)})
}
6 changes: 3 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
}

// CRC of the start header should match
if crc32Compare(h.Sum(nil), sh.CRC) != 0 {
if !util.CRC32Equal(h.Sum(nil), sh.CRC) {
return errChecksum
}

Expand Down Expand Up @@ -1048,7 +1048,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
}

// CRC should match the one from the start header
if crc32Compare(h.Sum(nil), start.CRC) != 0 {
if !util.CRC32Equal(h.Sum(nil), start.CRC) {
return errChecksum
}

Expand Down Expand Up @@ -1080,7 +1080,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
}

if cr, ok := fr.(checksumReadCloser); ok && crc != 0 {
if crc32Compare(cr.Checksum(), crc) != 0 {
if !util.CRC32Equal(cr.Checksum(), crc) {
return errChecksum
}
}
Expand Down

0 comments on commit 5766fbc

Please sign in to comment.