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

Features #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions src/bsdiff/BinaryPatchUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,20 @@ POSSIBILITY OF SUCH DAMAGE.
*/
class BinaryPatchUtility
{
/// <summary>
public enum SuffixSortAlgorithm
{
qsufsort,
SAIS
}

/// <summary>
/// Creates a binary patch (in <a href="http://www.daemonology.net/bsdiff/">bsdiff</a> format) that can be used
/// (by <see cref="Apply"/>) to transform <paramref name="oldData"/> into <paramref name="newData"/>.
/// </summary>
/// <param name="oldData">The original binary data.</param>
/// <param name="newData">The new binary data.</param>
/// <param name="output">A <see cref="Stream"/> to which the patch will be written.</param>
public static void Create(byte[] oldData, byte[] newData, Stream output)
public static void Create(byte[] oldData, byte[] newData, Stream output, SuffixSortAlgorithm algorithm = SuffixSortAlgorithm.qsufsort)
{
// check arguments
if (oldData == null)
Expand Down Expand Up @@ -77,10 +83,22 @@ 0 32 Header
long startPosition = output.Position;
output.Write(header, 0, header.Length);

int[] I = SuffixSort(oldData);

byte[] db = new byte[newData.Length + 1];
byte[] eb = new byte[newData.Length + 1];
int[] I;
switch (algorithm)
{
case SuffixSortAlgorithm.qsufsort:
I = SuffixSort(oldData);
break;
case SuffixSortAlgorithm.SAIS:
I = new int[oldData.Length];
Copy link
Member

Choose a reason for hiding this comment

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

I believe you need new int[oldData.Length + 1] here to avoid an IndexOutOfRangeException (under certain input conditions) in Search.

SAIS.sufsort(oldData, I, oldData.Length);
break;
default:
throw new ArgumentException("Unknown suffix sort algorithm.", "algorithm");
}

byte[] db = new byte[newData.Length];
byte[] eb = new byte[newData.Length];

int dblen = 0;
int eblen = 0;
Expand Down Expand Up @@ -348,7 +366,10 @@ with control block a set of triples (x,y,z) meaning "add x bytes
bytesToCopy -= actualBytesToCopy;
}

// sanity-check
if (newPosition == newSize)
return;

// sanity-check
if (newPosition + control[1] > newSize)
throw new InvalidOperationException("Corrupt patch.");

Expand Down Expand Up @@ -595,18 +616,19 @@ private static long ReadInt64(byte[] buf, int offset)

private static void WriteInt64(long value, byte[] buf, int offset)
{
long valueToWrite = value < 0 ? -value : value;

for (int byteIndex = 0; byteIndex < 8; byteIndex++)
{
buf[offset + byteIndex] = (byte) (valueToWrite % 256);
valueToWrite -= buf[offset + byteIndex];
valueToWrite /= 256;
}

if (value < 0)
buf[offset + 7] |= 0x80;
}
if (value < 0)
{
value = -value;
for (int i = -1; ++i < 8; value >>= 8)
buf[offset + i] = (byte)value;
buf[offset + 7] |= 0x80;
}
else
{
for (int i = -1; ++i < 8; value >>= 8)
buf[offset + i] = (byte)value;
}
}

const long c_fileSignature = 0x3034464649445342L;
const int c_headerSize = 32;
Expand Down
1 change: 1 addition & 0 deletions src/bsdiff/BsDiff.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SAIS.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Loading