在文件中搜索字节序列 (C#)
我正在编写一个 C# 应用程序,需要在文件(可能非常大)中搜索字节序列,但我无法使用任何库来执行此操作。因此,我需要一个函数,它将字节数组作为参数并返回给定序列后面的字节的位置。该功能不必非常快,只需能够工作即可。任何帮助将不胜感激:)
I'm writing a C# application in which I need to search a file (could be very big) for a sequence of bytes, and I can't use any libraries to do so. So, I need a function that takes a byte array as an argument and returns the position of the byte following the given sequence. The function doesn't have to be fast, it simply has to work. Any help would be greatly appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果不需要很快,你可以使用这个:
但我真的建议你使用 Knuth-Morris-Pratt 算法,该算法主要用作字符串 IndexOf 方法的基础。除了小数组和小模式之外,上面的算法执行速度非常慢。
If it doesn't have to be fast you could use this:
But I really would recommend you to use Knuth-Morris-Pratt algorithm, it's the algorithm mostly used as a base of IndexOf methods for strings. The algorithm above will perform really slow, exept for small arrays and small patterns.
Turrau 指出的直接方法是有效的,并且对于您的目的来说可能已经足够好了,因为您说它不必很快 - 特别是因为对于大多数实际目的,该算法比最坏情况 O( n*m)。 (我猜这取决于你的模式)。
要获得最佳解决方案,您还可以查看 Knuth-Morris -Pratt 算法,它利用部分匹配,最终的复杂度为 O(n+m)。
The straight-forward approach as pointed out by Turrau works, and for your purposes is probably good enough, since you say it doesn't have to be fast - especially since for most practical purposes the algorithm is much faster than the worst case O(n*m). (Depending on your pattern I guess).
For an optimal solution you can also check out the Knuth-Morris-Pratt algorithm, which makes use of partial matches which in the end is O(n+m).
这是我用来进行 boyer-moore 类型搜索的一些代码的摘录。它适用于 pcap 文件,因此它会逐条记录地进行操作,但应该很容易修改以适合搜索长二进制文件。它是从一些测试代码中提取的,所以我希望我已经掌握了所有内容供您遵循。还要在维基百科上查找 boyer-moore 搜索,因为这就是它的基础。
Here's an extract of some code I used to do a boyer-moore type search. It's mean to work on pcap files, so it operates record by record, but should be easy enough to modify to suit just searching a long binary file. It's sort of extracted from some test code, so I hope I got everything for you to follow along. Also look up boyer-moore searching on wikipedia, since that is what it's based off of.