如果我有2个二进制文件,则一个是4460个字符,第二个是42个。
如何检查较大的文件是否连续包含较小的文件?
int checkIfInfected(char* virusSignature, char* currentFileString, int len, int virusLen)
{
int flag = FALSE;
int counter = 0;
int i = 0;
for (i = 0; i < len && !flag; i++)
{
if (currentFileString[i] == virusSignature[counter])
{
counter++;
if (counter == virusLen)
{
flag = TRUE;
}
}
else
{
counter = 0;
}
}
return flag;
}
currentFileString
是一个包含长文件的数组
virusSignature
是一个包含短文件的数组......
即使它不对,它也会返回true ...
任何人都有修复?
答案 0 :(得分:1)
如果您不想使用memmem
#include <stdio.h>
#include <stdint.h>
const uint8_t *binSearch(const uint8_t *haystack, const uint8_t *needle, size_t haystackLength, size_t needleLength)
{
size_t nP = 0;
while(haystackLength)
{
while(nP < needleLength)
{
if(*haystack == needle[nP])
{
haystack++;
haystackLength--;
nP++;
}
else
{
nP = 0;
break;
}
}
if(nP) return haystack - needleLength;
haystack++;
haystackLength--;
}
return NULL;
}
int main(void) {
uint8_t arr[] = {1,2,3,4,5,6,7,8,9,10,56,123,87,98,104,3,1};
uint8_t needle[] = {104,3,2};
printf("%p\n", binSearch(arr, needle, sizeof(arr), sizeof(needle)));
return 0;
}
或者您的数据不一定是uint8_t
const void *binSearch(const void *hs, const void *ndl, size_t haystackLength, size_t needleLength)
{
size_t nP = 0;
const uint8_t *needle = ndl;
const uint8_t *haystack = hs;