Skip to content

Commit

Permalink
Add actual/expected crc32 in mismatch exception
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Dec 31, 2023
1 parent 088ded3 commit e1d5486
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion ValvePak/ValvePak.Test/PackageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ public void ThrowsOnInvalidCRC32()
file.CRC32 = 0xDEADBEEF;

Assert.Throws<InvalidDataException>(() => package.ReadEntry(file, out _));
Assert.Throws<InvalidDataException>(() => package.ReadEntry(file, out _, true));
var ex = Assert.Throws<InvalidDataException>(() => package.ReadEntry(file, out _, true));
Assert.That(ex.Message, Is.EqualTo("CRC32 mismatch for read data (expected DEADBEEF, got 32CFF012)."));
Assert.DoesNotThrow(() => package.ReadEntry(file, out _, false));
}

Expand Down
11 changes: 9 additions & 2 deletions ValvePak/ValvePak/Package.Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,16 @@ public void ReadEntry(PackageEntry entry, byte[] output, bool validateCrc = true
}
}

if (validateCrc && entry.CRC32 != Crc32.Compute(output, totalLength))
if (!validateCrc)
{
throw new InvalidDataException("CRC32 mismatch for read data.");
return;
}

var actualChecksum = Crc32.Compute(output, totalLength);

if (entry.CRC32 != actualChecksum)
{
throw new InvalidDataException($"CRC32 mismatch for read data (expected {entry.CRC32:X2}, got {actualChecksum:X2}).");
}
}

Expand Down

0 comments on commit e1d5486

Please sign in to comment.