简体   繁体   English

C#查找字节模式的偏移量,检查特定字节,更改字节,导出字节数组的一部分

[英]C# Find offset of byte pattern, check specific byte, change byte, export part of byte array

This could be long one. 这可能很长。 I do have a binary file, that contains some information. 我确实有一个二进制文件,其中包含一些信息。

What I want to do: 我想做的事:

  1. File (Binary) is read from OpenFileDialog 从OpenFileDialog中读取文件(二进制)
  2. I'm now searching for specific bytes in this file 我正在此文件中搜索特定字节
  3. I'm getting offset of that byte, and then I'm checking byte value of offset+2 我正在获取该字节的偏移量,然后正在检查offset + 2的字节值
  4. Basic if for (if offset+2 value is 0x08, then do this, if not, then do something else) 如果是,则为基本(如果offset + 2值为0x08,则执行此操作,否则,执行其他操作)
  5. Now, search for offset for another byte pattern. 现在,搜索另一个字节模式的偏移量。
  6. Copy everything from that offset till the end of file 复制从该偏移量到文件末尾的所有内容
  7. Save copied byte array to file. 将复制的字节数组保存到文件。

So, here're my codes for every step. 因此,这是我每一步的代码。 Step one : 1. 步骤一 :1。

        Byte[] bytes;
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.ShowDialog();
        path = ofd.FileName;
        bytes = File.ReadAllBytes(path);

Step two , search specific pattern in this file. 第二步 ,在该文件中搜索特定模式。 I used some help here on Stackoverflow, and end up with this: 我在Stackoverflow上使用了一些帮助,最后得到了以下结果:

VOID from stackoverflow: 来自stackoverflow的VOID:

        static public List<int> SearchBytePattern(byte[] pattern, byte[] bytes)
    {
        List<int> positions = new List<int>();
        int patternLength = pattern.Length;
        int totalLength = bytes.Length;
        byte firstMatchByte = pattern[0];
        for (int i = 0; i < totalLength; i++)
        {
            if (firstMatchByte == bytes[i] && totalLength - i >= patternLength)
            {
                byte[] match = new byte[patternLength];
                Array.Copy(bytes, i, match, 0, patternLength);
                if (match.SequenceEqual<byte>(pattern))
                {
                    positions.Add(i);
                    i += patternLength - 1;
                }
            }
        }
        return positions;
    }

My void to search for pattern: 我无法搜索模式:

        void CheckCamera()
    {
        Byte[] szukajkamera = { 0x02, 0x00, 0x08, 0x00, 0x20};
        List<int> positions = SearchBytePattern(szukajkamera, bytes);
        foreach (var item in positions){
            MessageBox.Show(item.ToString("X2"));
            IndexCamera = item;
        }
        int OffsetCameraCheck = IndexCamera + 2;
    }

Item is now my offset, where 02 00 08 00 20 is in file. 现在,项目是我的偏移量,其中文件02 00 08 00 20。 Now, how do I check, if bytes(offset=IndexCamera+2) == 0x08 ? 现在,如何检查bytes(offset = IndexCamera + 2)== 0x08? I can do array.IndexOf, but there's plenty of 08 before that 08 I'm looking for. 我可以执行array.IndexOf,但是在寻找08之前有很多08。

For step 5 I'm also doing the thing, but it gets impossible for me, when Buffer.BlockCopy ask me for length. 对于第5步,我也正在执行此操作,但是当Buffer.BlockCopy向我询问长度时,对我而言这是不可能的。 For step 5 and forward I need to search again in this same file for another pattern, get it's offset and copy from that offset till the end. 对于第5步及以后的操作,我需要在同一文件中再次搜索另一个模式,获取它的偏移量并从该偏移量复制到最后。 If I want so, then I need to buffer.blockcopy to non-empty byte array, but I just need it empty! 如果需要的话,我需要将buffer.blockcopy缓存到非空字节数组,但是我只需要将其清空即可! I totally lost it. 我完全失去了它。 Please, help me. 请帮我。 Thank you! 谢谢!

When doing pattern searching the above answer does work, however you need to adapt it to search for more of the pattern. 在进行模式搜索时,上述答案确实有效,但是您需要对其进行调整以搜索更多模式。

Eg: If you are looking for the location of 08 1D 1A AA 43 88 33 例如:如果您要查找08 1D 1A AA 43 88 33的位置

then you would need something like: 那么您将需要以下内容:

public static unsafe long IndexOf(this byte[] haystack, byte[] needle, long startOffset = 0)
{ 
    fixed (byte* h = haystack) fixed (byte* n = needle)
    {
        for (byte* hNext = h + startOffset, hEnd = h + haystack.LongLength + 1 - needle.LongLength, nEnd = n + needle.LongLength; hNext < hEnd; hNext++)
            for (byte* hInc = hNext, nInc = n; *nInc == *hInc; hInc++)
                if (++nInc == nEnd)
                    return hNext - h;
        return -1;
    }
}

Note : Credit to Dylan Nicholson who wrote this code. 注意:感谢编写此代码的Dylan Nicholson。

我该怎么做bytes(offset = IndexCamera + 2)== 0x08?

if(bytes[IndexCamera+2] == 0x08)....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM