forked from menees/Libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryDiff.cs
More file actions
343 lines (293 loc) · 8.86 KB
/
Copy pathBinaryDiff.cs
File metadata and controls
343 lines (293 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
namespace Menees.Diffs
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
#endregion
/// <summary>
/// This class implements a binary differencing algorithm to return a
/// near-optimal set of differences. It uses the algorithm from "A
/// Linear Time, Constant Space Differencing Algorithm" by Randal C.
/// Burns and Darrell D. E. Long. It is based on the pseudo-code from
/// Randal C. Burns's master thesis entitled "Differential Compression:
/// A Generalized Solution For Binary Files".
///
/// Finding an optimal set of differences requires quadratic time
/// relative to the input size, so it becomes unusable very quickly.
/// This near-optimal linear algorithm is good enough for most cases.
///
/// As the Burns/Long paper suggested, this implementation uses a
/// Karp-Rabin hashing scheme so that sequential footprints can be
/// easily determined based on the previous hash and the next byte.
///
/// The base implementation of this class returns the diffs in an
/// AddCopyCollection. If for performance reasons you need the diffs in a
/// different format, then inherit from BinaryDiff and provide
/// overrides for EmitAdd and EmitCopy. Then you can dump the diffs
/// out however you need (e.g. to a member Stream called this.DiffFile).
/// </summary>
public class BinaryDiff
{
#region Private Data Members
private int footprintLength = 8;
private int tableSize = 1009;
private uint powerD = 128;
#endregion
#region Constructors
public BinaryDiff()
{
}
#endregion
#region Public Methods and Properties
/// <summary>
/// Whether the first or last match is favored if two segments
/// hash to the same entry in the hashtable. This defaults to
/// false.
/// </summary>
public bool FavorLastMatch { get; set; }
/// <summary>
/// The length of bytes to hash together. This defaults to 8
/// and must be between 1 and 31.
/// </summary>
public int FootprintLength
{
get
{
return this.footprintLength;
}
set
{
const int MaxFootprintLength = 31;
if (value >= 1 && value <= MaxFootprintLength)
{
this.footprintLength = value;
// Computes d = 2^(m-1) with the left-shift operator
this.powerD = 1;
for (int i = 1; i < this.footprintLength; i++)
{
this.powerD <<= 1;
}
}
else
{
throw new ArgumentOutOfRangeException(nameof(value), value, $"The value must be between 1 and {MaxFootprintLength}.");
}
}
}
/// <summary>
/// Sets the size of the hashtable to use. This defaults to 1009.
/// </summary>
/// <remarks>
/// The hash table size should be a prime number.
/// </remarks>
public int TableSize
{
get
{
return this.tableSize;
}
set
{
if (value >= 1)
{
this.tableSize = value;
}
else
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be greater than or equal to one.");
}
}
}
/// <summary>
/// Does a binary diff on the two streams and returns an <see cref="AddCopyCollection"/>
/// of the differences.
/// </summary>
/// <param name="baseFile">The base file.</param>
/// <param name="versionFile">The version file.</param>
/// <returns>An AddCopyCollection that can be used later to construct the version file from the base file.</returns>
public AddCopyCollection Execute(Stream baseFile, Stream versionFile)
{
if (!baseFile.CanSeek || !versionFile.CanSeek)
{
throw new ArgumentException("The Base and Version streams must support seeking.");
}
TableEntry?[] table = new TableEntry?[this.tableSize];
List<IAddCopy> list = new();
AddCopyCollection result = new(list);
baseFile.Seek(0, SeekOrigin.Begin);
versionFile.Seek(0, SeekOrigin.End);
int verPos = 0;
int basePos = 0;
int verStart = 0;
bool isBaseActive = true;
uint verHash = 0;
uint baseHash = 0;
int lastVerHashPos = 0;
int lastBaseHashPos = 0;
while (verPos <= (versionFile.Length - this.footprintLength))
{
// The GetTableEntry procedure will add the entry if it isn't already there.
// This gives us a default behavior of favoring the first match.
verHash = this.Footprint(versionFile, verPos, verHash, ref lastVerHashPos);
TableEntry verEntry = GetTableEntry(table, verHash, versionFile, verPos);
TableEntry? baseEntry = null;
if (isBaseActive)
{
baseHash = this.Footprint(baseFile, basePos, baseHash, ref lastBaseHashPos);
baseEntry = GetTableEntry(table, baseHash, baseFile, basePos);
}
if (baseFile == verEntry.File && Verify(baseFile, verEntry.Offset, versionFile, verPos))
{
int length = this.EmitCodes(verEntry.Offset, verPos, verStart, baseFile, versionFile, list);
basePos = verEntry.Offset + length;
verPos += length;
verStart = verPos;
FlushTable(table);
continue;
}
else if (this.FavorLastMatch)
{
verEntry.Offset = verPos;
verEntry.File = versionFile;
}
isBaseActive = isBaseActive && (basePos <= (baseFile.Length - this.footprintLength));
if (isBaseActive && baseEntry != null)
{
if (versionFile == baseEntry.File && Verify(versionFile, baseEntry.Offset, baseFile, basePos)
&& verStart <= baseEntry.Offset)
{
int length = this.EmitCodes(basePos, baseEntry.Offset, verStart, baseFile, versionFile, list);
verPos = baseEntry.Offset + length;
basePos += length;
verStart = verPos;
FlushTable(table);
continue;
}
else if (this.FavorLastMatch)
{
baseEntry.Offset = basePos;
baseEntry.File = baseFile;
}
}
verPos++;
basePos++;
}
this.EmitCodes((int)baseFile.Length, (int)versionFile.Length, verStart, baseFile, versionFile, list);
Debug.Assert(
result.TotalByteLength == (int)versionFile.Length,
"The total byte length of the AddCopyCollection MUST equal the length of the version file!");
return result;
}
#endregion
#region Protected Methods
protected virtual void EmitAdd(int versionStart, int length, Stream versionFile, IList<IAddCopy> list)
{
versionFile.Seek(versionStart, SeekOrigin.Begin);
byte[] bytes = new byte[length];
versionFile.Read(bytes, 0, length);
Addition add = new(bytes);
list.Add(add);
}
protected virtual int EmitCopy(int basePosition, int length, Stream baseFile, IList<IAddCopy> list)
{
Copy copy = new(basePosition, length);
list.Add(copy);
return length;
}
#endregion
#region Private Methods
private static int ExtendMatch(Stream baseFile, int basePos, Stream verFile, int verPos)
{
baseFile.Seek(basePos, SeekOrigin.Begin);
verFile.Seek(verPos, SeekOrigin.Begin);
int length = 0;
int byteValue;
while ((byteValue = baseFile.ReadByte()) == verFile.ReadByte() && byteValue != -1)
{
length++;
}
return length;
}
private static void FlushTable(TableEntry?[] table)
{
for (int i = 0; i < table.Length; i++)
{
table[i] = null;
}
}
private static TableEntry GetTableEntry(TableEntry?[] table, uint hash, Stream file, int pos)
{
int index = (int)(hash % table.Length);
TableEntry? result = table[index];
if (result == null)
{
result = new TableEntry
{
File = file,
Offset = pos,
};
table[index] = result;
}
return result;
}
private static bool Verify(Stream baseFile, int basePos, Stream verFile, int verPos)
{
baseFile.Seek(basePos, SeekOrigin.Begin);
verFile.Seek(verPos, SeekOrigin.Begin);
return baseFile.ReadByte() == verFile.ReadByte();
}
private int EmitCodes(int basePos, int verPos, int verStart, Stream baseFile, Stream verFile, IList<IAddCopy> list)
{
if (verPos > verStart)
{
this.EmitAdd(verStart, verPos - verStart, verFile, list);
}
int copyLength = ExtendMatch(baseFile, basePos, verFile, verPos);
if (copyLength > 0)
{
this.EmitCopy(basePos, copyLength, baseFile, list);
}
return copyLength;
}
private uint Footprint(Stream file, int pos, uint lastHash, ref int lastPos)
{
uint result;
// We must allow rollovers
unchecked
{
if (pos == lastPos + 1)
{
// Rehash using a Karp-Rabin rehashing scheme.
file.Seek(lastPos, SeekOrigin.Begin);
int prevByte = file.ReadByte();
file.Seek(pos + this.footprintLength - 1, SeekOrigin.Begin);
int nextByte = file.ReadByte();
result = (uint)(((lastHash - (prevByte * this.powerD)) << 1) + nextByte);
}
else
{
// Generate a new hash
file.Seek(pos, SeekOrigin.Begin);
uint hash = 0;
for (int i = 0; i < this.footprintLength; i++)
{
hash = (uint)((hash << 1) + file.ReadByte());
}
lastPos = pos;
result = hash;
}
}
return result;
}
#endregion
#region Helper Classes
private class TableEntry
{
public Stream? File { get; set; }
public int Offset { get; set; }
}
#endregion
}
}