Skip to content

Instantly share code, notes, and snippets.

@damieng
Created February 3, 2023 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damieng/9cb6ccc3b212a9237476731909aeecc9 to your computer and use it in GitHub Desktop.
Save damieng/9cb6ccc3b212a9237476731909aeecc9 to your computer and use it in GitHub Desktop.
Find matching binary parts of a file regardless of offset
namespace BinarySubset
{
internal class Program
{
static void Main(string[] args)
{
const int minBytes = 15;
const int minUniques = 5;
var uniqueBytes = new bool[256];
var aFile = File.ReadAllBytes(args[0]);
var bFile = File.ReadAllBytes(args[1]);
for (var aIndex = 0; aIndex < aFile.Length; aIndex++)
{
for (var bIndex = 0; bIndex < bFile.Length; bIndex++)
{
if (bFile[bIndex] == aFile[aIndex])
{
// Start matching
Array.Clear(uniqueBytes);
var aMatchIndex = aIndex;
var bMatchIndex = bIndex;
var length = 0;
while (aMatchIndex < aFile.Length && bMatchIndex < bFile.Length && aFile[aMatchIndex] == bFile[bMatchIndex])
{
uniqueBytes[aFile[aMatchIndex]] = true;
aMatchIndex++;
bMatchIndex++;
length++;
}
if (length >= minBytes)
{
var foundUniques = uniqueBytes.Count(b => b == true);
if (foundUniques > minUniques)
Console.WriteLine($"Matched {length} bytes with {foundUniques} unique bytes at {aIndex} (A) and {bIndex} (B)");
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment