如何检查其他二进制文件中是否找到了二进制文件的内容? [英] How can i check if binary file's content is found in other binary file?

查看:63
本文介绍了如何检查其他二进制文件中是否找到了二进制文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查二进制文件中的内容是否在其他二进制文件中.

i need to check if content in a binary file in in other binary file.

我试图将两个文件的内容复制到带有fread的字符数组中,并使用strstr进行检查,但是即使应该在另一个文件中找到该内容,strstr始终返回NULL.

i've tried to copy both files content into a array of chars with fread and check them with strstr, but strstr is always returning NULL even if the content supposed to be found in the other file.

有什么想法吗?

谢谢.

推荐答案

由于 strstr 函数在这里不适用于任意二进制数据(它仅适用于具有 \的字符串0 .终止),我可以在此处看到三种方法:
1)天真的方法:遍历一个字节数组,并使用 memcmp 每次将另一个数组从不同位置开始.容易,但消耗 O(k * n)时间(k,n-数据大小).
2)使用 KMP算法.需要一些理解和编码方面的工作,但要提供最佳的时间复杂度 O(k + n).
3)如果性能并不重要,并且您不想弄乱任何一些非平凡的算法,则:
-将您的二进制数据转换为字符串,以每个字节的两位数字十六进制值表示.
-使用 strstr .

Since the strstr function won't work here for an arbitrary binary data (it is working only for strings with \0. termination), I can see three approaches here:
1) Naive approach: iterate over one array of bytes, and use memcmp with the other array starting at different positions each time. Easy, but consumes O(k*n) time (k, n - sizes of the data).
2) Using the KMP algorithm. Requires some work on understanding and coding, but giving the best time complexity O(k+n).
3) If the performance is not important, and you don't want to mess with ANY somewhat non-trivial algorithms:
-- Convert your binary datas to strings, representing each byte with it's two digits HEX value.
-- Use strstr.

更新:对第三种方法进行了一些思考之后,可能会出现无法正常使用的情况.考虑您要在 1A AA A1 中查找由 AA AA 表示的数据.因为它不存在,所以不应该找到它.但是,如果您将数据表示为不带分隔符的连接字符,则就像在 1AAAA1 中找到 AAAA 一样,它将成功.因此,在此处添加一些定界符将是一个好主意.

Update: After a little thinking about the third approach, there might be a case when it won't work right. Consider that you want to find the data represented by AA AA inside 1A AA A1. It shouldn't be found, since it is not there. But, if you represent the data as concatenated characters without delimiters, it will be like find AAAA in 1AAAA1, which will succeed. So adding some delimiter would be a good idea here.

这篇关于如何检查其他二进制文件中是否找到了二进制文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆