void searchfile(const char *filename, int fd, const unsigned char *value, const unsigned char *mask, int len)
{
off_t offset = 0;
unsigned char buf[1024];
len--;
while (1)
{
int r;
memmove(buf, buf + sizeof(buf) - len, len);
r = read(fd, buf + len, sizeof(buf) - len);
if (r < 0)
{
perror("read");
return;
} else if (!r)
return;
int o, i;
for (o = offset ? 0 : len; o < r; ++o)
{
for (i = 0; i <= len; ++i)
if ((buf[o + i] & mask[i]) != value[i])
break;
if (i > len)
{
unsigned long long pos = (unsigned long long)(offset + o - len);
printf("%s: %08llx\n", filename, pos);
if (bytes_before || bytes_after)
dump_context(fd, pos);
}
}
offset += r;
}
}
|