简体   繁体   中英

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");
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM