-
Notifications
You must be signed in to change notification settings - Fork 15
Add LZMA Decompression Support #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chyyran
merged 9 commits into
SnowflakePowered:master
from
evandixon:xz-secondary-decompression
Mar 3, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
61f4853
Add lzma decompression support
evandixon 27c8a8c
Fix code quality issues
evandixon f92c383
Fix byte buffer not advancing offset in ReadBytesToSpan
evandixon 8da8ba4
Reduce allocations of compressors when not necessary
evandixon af30e83
Extract decompression to separate class
evandixon 7ce0686
Dispose of compression streams when done
evandixon 193e7a5
Fix accessibilty error
evandixon c9a4edc
Add test
evandixon a6a03d8
Use try/finally for secondary compressor disposal
evandixon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using VCDiff.Includes; | ||
| using VCDiff.Shared; | ||
|
|
||
| namespace VCDiff.Compressors | ||
| { | ||
| /// <summary> | ||
| /// A compression method for secondary compression, for use when <see cref="VCDiffCodeFlags.VCDDECOMPRESS"/> is enabled in the header and the appropriate <see cref="VCDiffCompressFlags"/> flag is enabled for the section in the window. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Implementations are stateful, and a single instance must be used for the entire file for a single operation (compression or decompression). | ||
| /// </remarks> | ||
| internal interface ICompressor | ||
| { | ||
| PinnedArrayRental Decompress(WindowSectionType windowSectionType, PinnedArrayRental sectionData); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| using SharpCompress.Compressors.Xz; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Text; | ||
| using VCDiff.Shared; | ||
|
|
||
| namespace VCDiff.Compressors | ||
| { | ||
| internal class XzCompressor : ICompressor, IDisposable | ||
| { | ||
| public XzCompressor() | ||
| { | ||
| addRunCompressedBuffer = new(); | ||
| instructionsCompressedBuffer = new(); | ||
| addressesCompressedBuffer = new(); | ||
|
|
||
| addRunDecompressor = new(addRunCompressedBuffer); | ||
| instructionsDecompressor = new(instructionsCompressedBuffer); | ||
| addressesDecompressor = new(addressesCompressedBuffer); | ||
| } | ||
|
|
||
| private readonly MemoryStream addRunCompressedBuffer; | ||
| private readonly MemoryStream instructionsCompressedBuffer; | ||
| private readonly MemoryStream addressesCompressedBuffer; | ||
| private readonly XZStream addRunDecompressor; | ||
| private readonly XZStream instructionsDecompressor; | ||
| private readonly XZStream addressesDecompressor; | ||
|
|
||
| public PinnedArrayRental Decompress(WindowSectionType windowSectionType, PinnedArrayRental sectionData) | ||
| { | ||
| if (sectionData.Data == null) | ||
| { | ||
| throw new ArgumentException("Cannot decompress null data"); | ||
| } | ||
|
|
||
| MemoryStream memoryStream; | ||
| XZStream xzStream; | ||
| switch (windowSectionType) | ||
| { | ||
| case WindowSectionType.AddRunData: | ||
| memoryStream = addRunCompressedBuffer; | ||
| xzStream = addRunDecompressor; | ||
| break; | ||
| case WindowSectionType.InstructionsAndSizes: | ||
| memoryStream = instructionsCompressedBuffer; | ||
| xzStream = instructionsDecompressor; | ||
| break; | ||
| case WindowSectionType.AddressForCopy: | ||
| memoryStream = addressesCompressedBuffer; | ||
| xzStream = addressesDecompressor; | ||
| break; | ||
| default: | ||
| throw new ArgumentOutOfRangeException(nameof(windowSectionType)); | ||
| } | ||
|
|
||
| var uncompressedLength = VarIntBE.ParseInt32(sectionData.AsSpan(), out int uncompressedLengthByteCount); | ||
| var compressedData = sectionData.AsSpan().Slice(uncompressedLengthByteCount); | ||
|
|
||
| // Each section in a window uses the same compression stream throughout the file | ||
| // If this is not the first window, reuse the same stream from before, just using different data | ||
| memoryStream.SetLength(compressedData.Length); | ||
| memoryStream.Position = 0; | ||
| memoryStream.Write(compressedData); | ||
| memoryStream.Position = 0; | ||
|
|
||
| var decompressedData = new PinnedArrayRental(uncompressedLength); | ||
| xzStream.ReadExactly(decompressedData.AsSpan()); | ||
|
|
||
| return decompressedData; | ||
| } | ||
| public void Dispose() | ||
| { | ||
| addressesCompressedBuffer?.Dispose(); | ||
| instructionsCompressedBuffer?.Dispose(); | ||
| addressesCompressedBuffer?.Dispose(); | ||
| addRunDecompressor?.Dispose(); | ||
| instructionsDecompressor?.Dispose(); | ||
| addressesDecompressor?.Dispose(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using SharpCompress.Compressors.Xz; | ||
| using System.IO; | ||
|
|
||
| namespace VCDiff.Decoders | ||
| { | ||
| internal class SharedDecompressors | ||
| { | ||
| public XZStream? AddRunDecompressor; | ||
| public XZStream? InstructionsDecompressor; | ||
| public XZStream? AddressesDecompressor; | ||
|
|
||
| public MemoryStream? AddRunCompressedBuffer; | ||
| public MemoryStream? InstructionsCompressedBuffer; | ||
|
Check warning on line 13 in src/VCDiff/Decoders/SharedDecompressors.cs
|
||
| public MemoryStream? AddressesCompressedBuffer; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.