Skip to content

Commit

Permalink
fix bsdiff creation for patches that don't contain bsdiff extra data
Browse files Browse the repository at this point in the history
  • Loading branch information
Rufus Linke committed Nov 22, 2018
1 parent 783565b commit afe47c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
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());
}
}
}
}

0 comments on commit afe47c9

Please sign in to comment.