简体   繁体   中英

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
  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
  4. Basic if for (if offset+2 value is 0x08, then do this, if not, then do something else)
  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.

        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:

VOID from stackoverflow:

        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. Now, how do I check, if bytes(offset=IndexCamera+2) == 0x08 ? I can do array.IndexOf, but there's plenty of 08 before that 08 I'm looking for.

For step 5 I'm also doing the thing, but it gets impossible for me, when Buffer.BlockCopy ask me for length. 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. If I want so, then I need to buffer.blockcopy to non-empty byte array, but I just need it empty! 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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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