You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
apphost-extract/src/apphost-extract/apphost-extract-v2/Util.cs

50 lines
1.4 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace apphost_extract_v2
{
public static class Util
{
public static int[] PatternScan(FileStream fs, int start, int length, byte[] pattern, string mask)
{
byte[] scanBuffer = fs.ReadBuffer(start, length);
List<int> scanResults = new List<int>();
for(int i = 0; i < scanBuffer.Length - pattern.Length; i++)
{
if (!IsMatch(scanBuffer, i, pattern, mask))
continue;
scanResults.Add(start + i);
}
return scanResults.ToArray();
}
//https://stackoverflow.com/a/283648/10724593
private static bool IsMatch(byte[] array, int position, byte[] candidate, string mask)
{
if (candidate.Length > (array.Length - position))
return false;
for (int i = 0; i < candidate.Length; i++)
if (mask[i] == 'x' && array[position + i] != candidate[i])
return false;
return true;
}
public static byte[] ReadBuffer(this FileStream fs, long start, int length)
{
byte[] buff = new byte[length];
fs.Seek(start, SeekOrigin.Begin);
fs.Read(buff, 0, length);
return buff;
}
}
}