Skip to content
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

fix bsdiff creation for patches that don't contain bsdiff extra data #1421

Merged
merged 1 commit into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/Squirrel/BinaryPatchUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,14 @@ static void CreateInternal(byte[] oldData, byte[] newData, Stream output)
long diffEndPosition = output.Position;
WriteInt64(diffEndPosition - controlEndPosition, header, 16);

// write compressed extra data
using (WrappingStream wrappingStream = new WrappingStream(output, Ownership.None))
using (var bz2Stream = new BZip2Stream(wrappingStream, CompressionMode.Compress))
// write compressed extra data, if any
if (eblen > 0)
{
bz2Stream.Write(eb, 0, eblen);
using (WrappingStream wrappingStream = new WrappingStream(output, Ownership.None))
using (var bz2Stream = new BZip2Stream(wrappingStream, CompressionMode.Compress))
{
bz2Stream.Write(eb, 0, eblen);
}
}

// seek to the beginning, write the header, then seek back to end
Expand Down Expand Up @@ -326,10 +329,13 @@ public static void Apply(Stream input, Func<Stream> openPatchStream, Stream outp
compressedDiffStream.Seek(c_headerSize + controlLength, SeekOrigin.Current);
compressedExtraStream.Seek(c_headerSize + controlLength + diffLength, SeekOrigin.Current);

// the stream might end here if there is no extra data
var hasExtraData = compressedExtraStream.Position < compressedExtraStream.Length;

// decompress each part (to read it)
using (var controlStream = new BZip2Stream(compressedControlStream, CompressionMode.Decompress))
using (var diffStream = new BZip2Stream(compressedDiffStream, CompressionMode.Decompress))
using (var extraStream = new BZip2Stream(compressedExtraStream, CompressionMode.Decompress))
using (var extraStream = hasExtraData ? new BZip2Stream(compressedExtraStream, CompressionMode.Decompress) : null)
{
long[] control = new long[3];
byte[] buffer = new byte[8];
Expand Down
23 changes: 23 additions & 0 deletions test/DeltaPackageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,28 @@ public void WhenNewPackageDoesNotExistThrowException()
tempFiles.ForEach(File.Delete);
}
}

[Fact]
public void HandleBsDiffWithoutExtraData()
{
var baseFileData = new byte[] { 1, 1, 1, 1 };
var newFileData = new byte[] { 2, 1, 1, 1 };

byte[] patchData;

using (var patchOut = new MemoryStream())
{
Bsdiff.BinaryPatchUtility.Create(baseFileData, newFileData, patchOut);
patchData = patchOut.ToArray();
}

using (var toPatch = new MemoryStream(baseFileData))
using (var patched = new MemoryStream())
{
Bsdiff.BinaryPatchUtility.Apply(toPatch, () => new MemoryStream(patchData), patched);

Assert.Equal(newFileData, patched.ToArray());
}
}
}
}