Searching Binary Files for a Pattern
I am having much trouble trying to figure this out. I am trying to search for the hex pattern(which you can find in the below program source) in a binary file and output to another file the files found by matching the pattern. The header(hex pattern) signifies the beginning of the file found. 7 bytes from where the header was found, the next 4 bytes should found signify the file size. The output file should contain a list of the files found listed by file number, its offset within the file and the size of the file.
I've been at this for a while now and I'm starting to go blind and pull my hair out. The most difficulty I have is with implementing fseek and fread since these appear, after much research, to find the pattern I am looking for. I believe I understand how they work in concept and theory but am having trouble trying to make them work. You'll notice I have left some commented sections out... simply because I am at my wits end and commenting things left and right.
Any help is appreciated.
EDIT: I know about gets() and such.. for now I am just trying to make it function correctly. I would then go back and change gets(). If it's a mess, please just locate the while loop and have a looksee. Thanks.
EDIT 2: Oh, let me just mention what it's doing wrong. When I did have it working it was producing an output file with only two files found when fact there are three. Further, the offsets were incorrect from what I located in my hex editor.
Code:
// The first three lines make the functions within
// the declared libraries available to the program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// program entry point
int main(int argc, char *argv[])
{
// used to store the user input which represent
// the input and output file name and location
char cFileInName[255], cFileOutName[255];
// pointers to input and output files
FILE *FileIn, *FileOut;
char outStringData[256]; // Character variable to hold data
// char outFilename[256]; // Another character variable to hold data*/
long int position = 0; // position counter
int currentFile = 1; // file counter
// hex pattern to search for
int pattern = 0xFF575043;
char *patternArray = (char *)&pattern; // Create an array called pattern to hold the bytes read in from the input file
long int tempInteger = 0; // file size accumulator
char *tempBuffer = (char *)&tempInteger;
printf("Please enter the name and path of the binary file: ");
gets(cFileInName); // Get the user input and store it in the character array
if ( (FileIn = fopen(cFileInName, "rb") ) == NULL)
{ // error reading the file
printf("The file could not be opened, please check your file and location before trying again.\n");
return 0;
}
printf("Enter the name and path for the output file: ");
gets(cFileOutName);
// create a file and open it in append mode ready for output
FileOut = fopen(cFileOutName, "a");
// while the end of file has not been reached
while (!feof(FileIn))
{
position++; // increase the character counter
// Check the next byte in the file to see if it matches the
// file header, fgetc takes the file pointer and returns
// the byte that was read from the file.
if (fgetc(FileIn) == patternArray[0])
{
// When it finds the first byte in patternArray. Set the first
// byte in tempBuffer to the first byte in patternArray.
tempBuffer[0] = patternArray[0];
// read the next 3 bytes into the rest of tempBuffer
fread(tempBuffer + 1, sizeof(char), 3, FileIn);
// Check if the 4 bytes read from the file are the pattern.
/*if(tempInteger != pattern)
{ printf("Hello Mofo\n");
// Skip back three bytes so that it doesnt miss any headers
// due to a flase positive being read in
fseek(FileIn, -3, SEEK_CUR);
continue; // Go back to the top of the loop
}*/
//fseek(FileIn, 7, SEEK_CUR); // Seek ahead 7 bytes and then extract 4
fread(tempBuffer, sizeof(char), 4, FileIn);
// create the string so we can write to the output file, store it in outStringData
sprintf(outStringData, "%-5i\tOffset: %-12ld\tSize: %ld\n", currentFile++, position - 1, tempInteger);
// write the string to the output file
fwrite(outStringData, sizeof(char), strlen(outStringData), FileOut);
}
}
// close the files and at the same time flush the buffers
fclose(FileIn);
fclose(FileOut);
printf("\n Done!\n Press enter to exit\n");
getchar();
return 0;
}