Skip to content

Instantly share code, notes, and snippets.

@zapirkon
Created May 5, 2018 06:14
Show Gist options
  • Save zapirkon/d885964dc4ce2362d804877bccccf760 to your computer and use it in GitHub Desktop.
Save zapirkon/d885964dc4ce2362d804877bccccf760 to your computer and use it in GitHub Desktop.
using System.IO;
namespace XorFilesMerger
{
static class XorFilesMerger
{
/// <summary>
/// merge file1 and file2 into file3, where bytes == 0 should be the ones where the files differ
/// </summary>
/// <param name="file1"></param>
/// <param name="file2"></param>
/// <param name="file3"></param>
public static void merge(string file1, string file2, string file3)
{
//no checks, just go for it
var stream1 = new FileStream(file1, FileMode.Open);
var stream2 = new FileStream(file2, FileMode.Open);
var stream3 = new FileStream(file3, FileMode.Create);
var binary1 = new BinaryReader(stream1);
var binary2 = new BinaryReader(stream2);
var binary3 = new BinaryWriter(stream3);
var bufferLength = 1024;
var buffer1 = new byte[bufferLength];
var buffer2 = new byte[bufferLength];
var read1 = 0;
var read2 = 0;
while (true)
{
read1 = binary1.Read(buffer1, 0, bufferLength);
read2 = binary2.Read(buffer2, 0, bufferLength);
if (read1 != read2)
{
System.Diagnostics.Debugger.Break();
}
for (int i = 0; i < bufferLength; i++)
{
if (buffer1[i] != buffer2[i])
{
if (buffer1[i] > 0 && buffer2[i] > 0)
{
if (buffer1[i] != buffer2[i])
{
System.Diagnostics.Debugger.Break();
}
}
if (buffer2[i] > 0)
{
buffer1[i] = buffer2[i];
}
}
}
binary3.Write(buffer1, 0, read1);
if (read1 < bufferLength)
{
break;
}
}
binary1.Close();
binary2.Close();
binary3.Close();
stream1.Close();
stream2.Close();
stream3.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment