Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在文件中搜索字节序列(C)_C#_Algorithm_File Io_Pattern Matching - Fatal编程技术网

C# 在文件中搜索字节序列(C)

C# 在文件中搜索字节序列(C),c#,algorithm,file-io,pattern-matching,C#,Algorithm,File Io,Pattern Matching,我正在编写一个C应用程序,我需要在其中搜索一个文件,这个文件对于一个字节序列来说可能非常大,而我不能使用任何库来这样做。因此,我需要一个函数,它将字节数组作为参数,并返回给定序列后面字节的位置。功能不一定要快,它只需工作即可。任何帮助都将不胜感激:如果不需要很快,您可以使用: int GetPositionAfterMatch(byte[] data, byte[]pattern) { for (int i = 0; i < data.Length - pattern.Length;

我正在编写一个C应用程序,我需要在其中搜索一个文件,这个文件对于一个字节序列来说可能非常大,而我不能使用任何库来这样做。因此,我需要一个函数,它将字节数组作为参数,并返回给定序列后面字节的位置。功能不一定要快,它只需工作即可。任何帮助都将不胜感激:

如果不需要很快,您可以使用:

int GetPositionAfterMatch(byte[] data, byte[]pattern)
{
  for (int i = 0; i < data.Length - pattern.Length; i++)
  {
    bool match = true;
    for (int k = 0; k < pattern.Length; k++)
    {
      if (data[i + k] != pattern[k])
      {
        match = false;
        break;
      }
    }
    if (match)
    {
      return i + pattern.Length;
    }
  }
}

但我真的建议你使用Knuth-Morris-Pratt算法,这是一种主要用作字符串索引方法基础的算法。上面的算法执行速度非常慢,除了小阵列和小模式。

Turrau指出的直接方法是有效的,而且对于您来说可能已经足够好了,因为您说它不一定要快——特别是对于大多数实际目的,算法要比*m上的最坏情况快得多。我想这取决于你的模式


为了获得最佳解决方案,您还可以查看,它利用了部分匹配,最后是+m。

这里是我用来进行boyer-moore类型搜索的一些代码的摘录。这意味着要处理pcap文件,所以它可以逐个记录地操作,但应该很容易修改,以便只搜索一个长的二进制文件。它是从一些测试代码中提取出来的,所以我希望我能为您提供一切。还可以在维基百科上查找boyer moore搜索,因为这是它的基础

int[] badMatch = new int[256];
byte[] pattern;  //the pattern we are searching for

//badMath is an array of every possible byte value (defined as static later).
//we use this as a jump table to know how many characters we can skip comparison on
//so first, we prefill every possibility with the length of our search string
for (int i = 0; i < badMatch.Length; i++)
{
  badMatch[i] = pattern.Length;
}

//Now we need to calculate the individual maximum jump length for each byte that appears in my search string
for (int i = 0; i < pattern.Length - 1; i++)
{
  badMatch[pattern[i] & 0xff] = pattern.Length - i - 1;
}

// Place the bytes you want to run the search against in the payload variable
byte[] payload = <bytes>

// search the packet starting at offset, and try to match the last character
// if we loop, we increment by whatever our jump value is
for (i = offset + pattern.Length - 1; i < end && cont; i += badMatch[payload[i] & 0xff])
{
  // if our payload character equals our search string character, continue matching counting backwards
  for (j = pattern.Length - 1, k = i; (j >= 0) && (payload[k] == pattern[j]) && cont; j--)
  {
    k--;
  }
// if we matched every character, then we have a match, add it to the packet list, and exit the search (cont = false)
  if (j == -1)
  {
    //we MATCHED!!!
    //i = end;
    cont = false;
  }
}

应该是i