2

i am making a program that gets two binary files, and checks if the second file (string) is in the first file. I tried to use strstr function but it doesnt work. This is that part of my code: Am i reading the files right?

    fseek(fileToCheckv, 0 , SEEK_END);
    size = ftell(fileToCheckv);
    rewind(fileToCheckv);
    fseek(virusSignit, 0L, SEEK_END);
    vsize = ftell(virusSignit);
    rewind(virusSignit);
    buffer = (char*)realloc(buffer, size+1 * sizeof(char));
    virusSig = (char*)realloc(virusSig, vsize+1 * sizeof(char));
    buffer[size] = 0;
    virusSig[vsize] = 0;
    fread(buffer,1 , size, fileToCheckv);
    fread(virusSig,1 ,vsize, virusSignit);
    result = strstr(buffer, virusSig);
    if (result != NULL)
    {
        printf("\nVirus was found in file: %s\n", fileToOpen);
    }
    else
    {
        printf("The virus was not found\n");
    }
user8097385
  • 109
  • 2
  • 7
  • _size+1 * sizeof(char)_ == size + sizeof(char)... I guess that is not what you meant – CIsForCookies Jun 01 '17 at 11:36
  • Of course `strstr` wont work because it operates on NUL terminated strings. You need to write your own "binbin" function having e.g. this signature: `char *binbin(const char *needle, const char *haystack, int length)`. I didn't check for other issues though. – Jabberwocky Jun 01 '17 at 11:41
  • Since the fread will copy the data into a char array that has a 0 in it's end, won't this be just as NUL terminated strings? – CIsForCookies Jun 01 '17 at 11:43
  • @CIsForCookies the binary data itself could contain NUL bytes anywhere. – Jabberwocky Jun 01 '17 at 11:44
  • @MichaelWalz I see... but in case with strings not containing NUL bytes, this will work, wouldn't it? – CIsForCookies Jun 01 '17 at 11:46
  • 1
    @CIsForCookies yes. – Jabberwocky Jun 01 '17 at 11:57
  • @MichaelWalz There is the memmem() function, which is basically strstr(), but allowing NULs in the "strings". It has **four** arguments, two pointers and two sizes. NOTE: *This function is a GNU extension.* – joop Jun 01 '17 at 12:02
  • Thank you! Can i use the memmem() function in windows? – user8097385 Jun 01 '17 at 12:08
  • 1
    @joop `memmem` is a gnu extension. It doesn't exist on Windows, but it's not very hard to rewrite. – Jabberwocky Jun 01 '17 at 12:11
  • @user8097385 [this SO question](https://stackoverflow.com/questions/2188914/c-searching-for-a-string-in-a-file) may help. – Jabberwocky Jun 01 '17 at 12:23
  • In C a _string_ is a sequence of characters up to and including a ending _null chracter_. With _binary_ files, "checks if the second file is in the first file." does not make sense as the second files contents may not constitute a _string_. – chux - Reinstate Monica Jun 01 '17 at 15:44

1 Answers1

0

You are opening the files correctly, but you have some other minor issues:

  • buffer = (char*)realloc(buffer, size+1 * sizeof(char));. You probably meant to do (size+1) * sizeof(char) of simply size+1 as sizeof(char) will be always 1. This problem occurs twice in your code
  • In the same line' you use realloc without checking if the pointer is NULL. That could prove problematic if you allocation fails
  • As @Michael Walz said, strstr() is for NUL-terminated strings, so for binary you should either create your own strstr-like function for binary, or verify there aren't NUL-bytes in your strings
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124