Skip to content

more complete CRC checking#1357

Merged
adamhathcock merged 13 commits into
masterfrom
adam/crc-impl
Jun 19, 2026
Merged

more complete CRC checking#1357
adamhathcock merged 13 commits into
masterfrom
adam/crc-impl

Conversation

@adamhathcock

Copy link
Copy Markdown
Owner

This pull request introduces documentation and code changes to improve support for archive-level checksum validation, especially for the XZ and LZMA/LZMA2 formats, and to clarify the behavior and usage of the CheckCrc extraction option across the library. It adds detailed skill and reference documents for XZ/LZMA, updates extraction APIs to support checksum validation, and enhances usage documentation for end users.

Documentation and Reference Additions:

  • Added .agents/skills/xz-lzma-format/SKILL.md and .agents/skills/xz-lzma-format/references/xz-lzma-format.md to provide comprehensive guidance on the XZ container, LZMA/LZMA2 decoding, CRC64/XZ, block checks, and SharpCompress-specific implementation notes. These documents include workflow steps, upstream references, and guidance for handling corrupt files and test fixtures. [1] [2]
  • Updated docs/FORMATS.md to include a new section on XZ format, emphasizing its container structure, integrity checks, and differences from raw LZMA/LZMA2 decoding.

API and Usage Documentation Improvements:

  • Enhanced docs/API.md and docs/USAGE.md to document the CheckCrc option in ExtractionOptions. This clarifies its default behavior, when it is applicable, and how it affects checksum validation during extraction for supported formats. [1] [2] [3] [4] [5]

Codebase Enhancements for Checksum Validation:

  • Updated extraction methods in src/SharpCompress/Archives/IArchiveEntryExtensions.cs to accept and utilize the ExtractionOptions parameter, ensuring that checksum validation is performed when CheckCrc is enabled. This affects both synchronous and asynchronous extraction workflows, including file extraction. [1] [2] [3] [4] [5] [6] [7]

Rar and Ace Format Checksum Handling:

  • Refactored src/SharpCompress/Archives/Rar/RarArchiveEntry.cs to select the appropriate stream wrapper (RarCrcStream or RarBLAKE2spStream) for RAR entries based on archive version and header properties, improving checksum validation logic.
  • Implemented the Checksum property in src/SharpCompress/Common/Ace/AceEntry.cs to expose CRC32 checksums for ACE entries when applicable, supporting the new validation workflow.

Copilot AI review requested due to automatic review settings June 16, 2026 12:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR expands SharpCompress checksum validation support by introducing an explicit ExtractionOptions.CheckCrc (default true), plumbing checksum validation into common extraction paths, and improving XZ/LZMA/LZip integrity behavior, alongside new tests and documentation clarifying format-specific expectations.

Changes:

  • Add ExtractionOptions.CheckCrc (default true) and integrate checksum validation into entry/reader extraction flows.
  • Implement/extend per-format checksum exposure/validation (ZIP/7z/ARJ/ARC/ACE, GZip trailer validation, LZip trailer validation, improved XZ block checks including CRC64/XZ).
  • Add targeted regression tests and update user/API/format docs, plus new agent reference docs for XZ/LZMA.

Review Notes (issues to address)

  • Breaking public API (critical): src/SharpCompress/Compressors/Xz/XZBlock.cs changes XZBlock’s previously-public members (BlockHeaderSize, Filters, HeaderIsLoaded) to non-public/private. Since XZBlock is public, this is a source/binary breaking change for consumers who referenced these properties. Consider restoring the public surface (even as passthroughs) and, if desired, deprecating via [Obsolete] for a future major version.
  • Avoid per-byte allocations in checksum wrapper (performance/maintainability): ChecksumValidationStream.ReadByte() updates via UpdateChecksum([(byte)value]), which allocates for every byte read. Update the CRC32 seed directly via the existing single-byte CRC helper and update CRC16 similarly without allocating.
  • API completeness for CheckCrc (api_design): IArchiveEntryExtensions.WriteTo(Stream, …) / WriteToAsync(Stream, …) do not expose an ExtractionOptions overload, so callers using WriteTo cannot opt into (or out of) checksum validation even though CheckCrc is now a core extraction option. Consider adding overloads (non-breaking) that accept ExtractionOptions.

Reviewed changes

Copilot reviewed 55 out of 56 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/SharpCompress.Test/Zip/ZipCrcExtractionTests.cs New tests for ZIP CRC mismatch behavior and CheckCrc opt-out.
tests/SharpCompress.Test/Xz/XZStreamTests.cs Add test asserting corrupt XZ block checks throw.
tests/SharpCompress.Test/Xz/XZBlockTests.cs Fix assertion order; align indexed-stream check type/size.
tests/SharpCompress.Test/Xz/XZBlockAsyncTests.cs Async equivalents of XZ block test updates.
tests/SharpCompress.Test/Xz/Crc64Tests.cs Add CRC64/XZ known-vector test.
tests/SharpCompress.Test/Streams/LzmaStreamAsyncTests.cs Add async test exercising corrupt LZMA2-in-XZ behavior.
tests/SharpCompress.Test/SevenZip/SevenZipCrcExtractionTests.cs New tests for 7z CRC validation + opt-out.
tests/SharpCompress.Test/RemainingCrcExtractionTests.cs New tests covering remaining formats (ARJ/ACE/ARC reader) and LZip trailer CRC.
tests/SharpCompress.Test/Rar/RarCrcExtractionTests.cs New tests for RAR/RAR5 CRC mismatch on extraction.
tests/SharpCompress.Test/OptionsUsabilityTests.cs Assert ExtractionOptions.CheckCrc default is true.
tests/SharpCompress.Test/GZip/GZipCrcExtractionTests.cs New tests for GZip trailer CRC/size validation and non-seekable behavior.
tests/SharpCompress.Test/BZip2/BZip2StreamTests.cs New test for BZip2 checksum failure behavior.
src/SharpCompress/Readers/IReaderExtensions.cs Ensure reader extraction wraps entry stream with checksum validation when enabled.
src/SharpCompress/Readers/IAsyncReaderExtensions.cs Async reader extraction now supports checksum validation via options.
src/SharpCompress/IO/CountingStream.cs Extend counting to bytes read; add async read overrides.
src/SharpCompress/Crypto/Crc32Stream.cs Widen CRC helper visibility for internal reuse.
src/SharpCompress/Compressors/Xz/XZBlock.cs Implement XZ block check verification for CRC32/CRC64/XZ/SHA-256.
src/SharpCompress/Compressors/Xz/XZBlock.Async.cs Async parity for padding/check reading and check verification.
src/SharpCompress/Compressors/Xz/Crc64.cs Add CRC64/XZ polynomial/seed path (ComputeXz / UpdateXz).
src/SharpCompress/Compressors/Xz/Crc32.cs Add incremental update helper for CRC32.
src/SharpCompress/Compressors/LZMA/LzmaStream.cs Tighten LZMA2 corruption handling (EOPM/end-marker rejection).
src/SharpCompress/Compressors/LZMA/LzmaStream.Async.cs Async parity for LZMA2 corruption handling updates.
src/SharpCompress/Compressors/LZMA/LzmaDecoder.cs Expose internal end-marker detection signal.
src/SharpCompress/Compressors/LZMA/LZipStream.cs Add LZip trailer CRC/size/member-size validation logic.
src/SharpCompress/Compressors/LZMA/LZipStream.Async.cs Async read now validates trailer at EOF.
src/SharpCompress/Common/Zip/ZipHeaderFactory.cs Track CRC availability when parsing ZIP headers.
src/SharpCompress/Common/Zip/ZipHeaderFactory.Async.cs Async parity for CRC availability tracking.
src/SharpCompress/Common/Zip/ZipEntry.cs Provide reliable Checksum descriptor for ZIP entries (incl. WinZip AES nuance).
src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs Mark CRC availability across streaming ZIP header paths.
src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.Async.cs Async parity for streaming ZIP CRC availability.
src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs Propagate CRC availability when using post-data descriptors.
src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs Add IsCrcAvailable metadata field.
src/SharpCompress/Common/Zip/Headers/LocalEntryHeader.cs Set IsCrcAvailable based on flags (post-data descriptor).
src/SharpCompress/Common/Zip/Headers/LocalEntryHeader.Async.cs Async parity for IsCrcAvailable.
src/SharpCompress/Common/Zip/Headers/DirectoryEntryHeader.cs Ensure directory CRC is marked available.
src/SharpCompress/Common/Zip/Headers/DirectoryEntryHeader.Async.cs Async parity for directory CRC availability.
src/SharpCompress/Common/SevenZip/SevenZipEntry.cs Expose Checksum descriptor when 7z CRC metadata is present.
src/SharpCompress/Common/IEntryExtensions.cs Add helper to wrap streams with checksum validation based on options.
src/SharpCompress/Common/GZip/GZipFilePart.cs Wrap raw stream and provide checksum-validation wrapper.
src/SharpCompress/Common/GZip/GZipEntry.cs Hook gzip entry extraction into gzip trailer validation stream.
src/SharpCompress/Common/GZip/GZipChecksumValidationStream.cs New stream wrapper to validate gzip CRC/ISIZE at EOF.
src/SharpCompress/Common/ExtractionOptions.cs Add CheckCrc option (default true) with docs.
src/SharpCompress/Common/Entry.cs Introduce Checksum descriptor + default checksum-validation wrapper.
src/SharpCompress/Common/ChecksumValidationStream.cs New general-purpose entry checksum validation stream (CRC32/CRC16).
src/SharpCompress/Common/ChecksumDescriptor.cs New internal checksum descriptor types used by entries.
src/SharpCompress/Common/Arj/ArjEntry.cs Expose CRC32 checksum descriptor for ARJ entries where applicable.
src/SharpCompress/Common/Arc/ArcEntry.cs Expose CRC16/ARC checksum descriptor for ARC entries.
src/SharpCompress/Common/Ace/AceEntry.cs Expose CRC32 (no final XOR) checksum descriptor for eligible ACE entries.
src/SharpCompress/Archives/Rar/RarArchiveEntry.cs Select correct RAR checksum stream wrapper (CRC vs BLAKE2sp).
src/SharpCompress/Archives/IArchiveEntryExtensions.cs Plumb ExtractionOptions into entry extraction so CheckCrc is honored.
docs/USAGE.md Document CheckCrc usage in extraction examples.
docs/FORMATS.md Add XZ format notes emphasizing container integrity checks.
docs/API.md Document CheckCrc semantics and defaults in API docs.
.agents/skills/xz-lzma-format/SKILL.md New agent skill definition for XZ/LZMA format guidance.
.agents/skills/xz-lzma-format/references/xz-lzma-format.md New detailed XZ/LZMA/LZMA2 reference notes for maintenance.

Copilot AI review requested due to automatic review settings June 19, 2026 10:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 55 out of 56 changed files in this pull request and generated no new comments.

This was referenced Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants