I'm having some problems with my code, the code does compile but when I test the code, I find that if the signature is at the end of the file(or after the middle of the file), the program doesn't work.
int normalScan(char * fileName, char * sign){
int toReturn = 0;
FILE * toScan = fopen(fileName,"rb");
FILE * signature = fopen(sign, "rb");
char * buffer = NULL;
char * secondBuffer = NULL;
int size = 0;
int secondSize = 0;
int elements = 0;
int i = 0;
fseek(signature, 0, SEEK_END);
size = ftell(signature);
rewind(signature);
fseek(toScan, 0, SEEK_END);
secondSize = ftell(toScan);
rewind(toScan);
buffer = malloc(sizeof(char)*size);
elements = fread(buffer, 1, size, signature);
secondBuffer = malloc(sizeof(char)*size);
elements = fread(secondBuffer, 1, size, toScan);
if (strcmp(secondBuffer, buffer) == 0){
toReturn = 1;
}
else{
while (fgetc(toScan) != EOF && fgetc(signature) != EOF){
rewind(signature);
fseek(toScan, -1, SEEK_CUR);
if (fgetc(toScan) == fgetc(signature)){
rewind(signature);
fseek(toScan, -1, SEEK_CUR);
elements = fread(secondBuffer, 1, size, toScan);
if (strcmp(secondBuffer, buffer) == 0){
toReturn = 1;
}
}
rewind(signature);
strncpy(secondBuffer, "", sizeof(secondBuffer));
}
}
free(secondBuffer);
fclose(toScan);
fclose(signature);
free(buffer);
return toReturn;
}