diff --git a/src/bsdiff/BinaryPatchUtility.cs b/src/bsdiff/BinaryPatchUtility.cs index 6f1cd01..76121ae 100644 --- a/src/bsdiff/BinaryPatchUtility.cs +++ b/src/bsdiff/BinaryPatchUtility.cs @@ -37,14 +37,20 @@ POSSIBILITY OF SUCH DAMAGE. */ class BinaryPatchUtility { - /// + public enum SuffixSortAlgorithm + { + qsufsort, + SAIS + } + + /// /// Creates a binary patch (in bsdiff format) that can be used /// (by ) to transform into . /// /// The original binary data. /// The new binary data. /// A to which the patch will be written. - 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) @@ -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]; + 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; @@ -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."); @@ -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; diff --git a/src/bsdiff/BsDiff.csproj b/src/bsdiff/BsDiff.csproj index f7b5d93..6eb8fd3 100644 --- a/src/bsdiff/BsDiff.csproj +++ b/src/bsdiff/BsDiff.csproj @@ -86,6 +86,7 @@ +