How to find bytes in binary file C

How to find bytes in binary file. I read the binary file to a string and try to do "strstr()" It doesn't work. if I print the string in loop as %c the string is dont look the same but if I print as %x or %02hhX it looks the same. I open two binary file and try to see if one file is hiding in the other file. to kind of anti virus code.

my code:

    FILE* file = fopen(filePath, "rb");
    FILE* signature = fopen(signaturePath, "rb"); 

    fread(fileStr, fileSize, 1, file);

    fread(signatureStr, signatureSize, 1, signature);

    int in = strstr(fileStr, signatureStr);

    if (in)
    {
        printf("bytes found.\n");
    }
    else
    {
        printf("sorry...\n");
    }

Upvotes: -1

Views: 638

Answers (1)

David Schwartz
David Schwartz

Reputation: 182883

I read the binary file to a string

You don't though. You just read it into an array of characters. How could you? The end of a string is marked by a zero byte, and a binary file may contain zero bytes inside it. So unless you perform some kind of conversion that escapes any zero bytes, you don't have a string.

The str* functions are only useful on strings. They aren't useful for arbitrary binary data. Instead, use the mem* functions. If you don't have memmem, it can easily be implemented. There's one written by an engineer at Apple that's open source.

Upvotes: 2

Related Questions