将接下来的32个字节复制到文件中特定位置的左侧

时间:2017-03-07 13:38:47

标签: c#

我试图找到一个查找特定十六进制的查找器,一旦找到它,它就会停止,因为我不知道接下来该做什么。

我基于我制作的修补程序,它将用其他东西替换找到的字节。现在,我不需要替换它们,而是需要读取我发现的左边的下一个32字节。我不知道如何做到这一点,我已经搜索过,但没有找到具体的答案。

这是我在字节数组中搜索模式的代码。现在我保留了BinaryWriter但删除了bw.Write所在的部分,因为我不需要写任何东西,也不需要替换。我只想将接下来的32个字节复制到所述位置的左边。

IEnumerable<int> positions = FindPattern(fileBytes, searchPattern);
if (positions.Count() == 0)
{
    Console.WriteLine();
    Console.WriteLine("[UCS] Pattern not found.");
    Console.Read();
    return;
}

foreach (int pos in positions)
{
    using (BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Open, FileAccess.Write)))
    {
        bw.BaseStream.Seek(pos, SeekOrigin.Begin);
        //What??
    }
    Console.WriteLine("[UCS] File: {0} patched", fileName);
}
Console.Read();

1 个答案:

答案 0 :(得分:0)

您希望在要读取的数据的左侧寻找32个字节,然后只读取32个字节。

using (BinaryWriter bw = new BinaryWriter(File.Open(fileName, FileMode.Open, FileAccess.Write)))
{
    foreach (int pos in positions)
    {
        byte[] buffer = new byte[32];
        bw.BaseStream.Seek(pos - 32, SeekOrigin.Begin);
        bw.BaseStream.Read(buffer, 0, 32);
        // now buffer will have the desired 32 bytes.
    }
}