简体   繁体   English

C# 如何在特定偏移处找到 byte[] 模式

[英]C# How do I find a byte[] pattern at a specific offset

I have this code to return a long of a byte pattern.我有这段代码可以返回一个长字节模式。 How would I add it so it searches the byte at a specific offset?我将如何添加它以便它在特定偏移量处搜索字节?

static public long 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;
                    }
                }
            }
            try
            {
                return positions[0];
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("none found");
                return 0;
            }
            
        }

For example I want to do this: I want the SearchBytePattern to have a way to start to search from a specific offset, eg: start from offset 3 (which is offset 47).例如,我想这样做:我希望 SearchBytePattern 有一种方法可以从特定偏移量开始搜索,例如:从偏移量 3(即偏移量 47)开始。

          byte[] needle = new byte[]
            {
                111, 111, 111
            };
            byte[] haystack = new byte[]
            {
               111, 111, 111, 47,  111, 111, 111, 47,  111, 111, 111, 47
            };

SearchBytePattern(needle, haystack)

You will need a third parameter offset .您将需要第三个参数offset

  • Start searching at offsetoffset开始搜索
  • You can short-circuit the search as shown below, if the needle is too long compared to the haystack and offset .如果needlehaystackoffset相比太长,您可以将搜索短路,如下所示。
  • Instead of copying arrays, which will take up more memory and probably also time, just search inside the arrays themselves.不要复制 arrays,这将占用更多的 memory 并且可能还需要时间,只需在 arrays 本身内部搜索即可。
  • This is still O(m * n) worst-case time complexity.这仍然是O(m * n)最坏情况的时间复杂度。 You can make it O(m) by using a more sophisticated algorithm like Knuth-Morris-Pratt (but it is much more complicated and the overhead will not be worth it, for small inputs).您可以通过使用更复杂的算法(如Knuth-Morris-Pratt )来实现O(m) (但它要复杂得多,而且对于小输入来说开销不值得)。
        public static int Search(byte[] needle, byte[] haystack, int offset){
            if (haystack.Length - offset < needle.Length) {
                return -1;   // can't possibly find b/c needle is too long
            }
            for (int i = offset; i < haystack.Length; ++i) {
                for (int j = 0; j < needle.Length; ++j) {
                    if ((i + j) >= haystack.Length || needle[j] != haystack[i + j]) {
                        break;       // found non-match for attempt
                    }
                    if (j == needle.Length - 1) {
                        return i;    // matched all
                    }
                }
            }
            return -1;
        }

The above returns 4 for your example input.以上为您的示例输入返回4

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

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