Created
February 3, 2023 15:31
-
-
Save damieng/9cb6ccc3b212a9237476731909aeecc9 to your computer and use it in GitHub Desktop.
Find matching binary parts of a file regardless of offset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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