Searching Binary Files for a Pattern

This is a discussion on Searching Binary Files for a Pattern within the C Programming forums, part of the General Programming Boards category; Matt, thanks for the great info. That particular implementation might be a little advanced for me. I understand what boyer-moore ...

  1. #16
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45
    Matt, thanks for the great info. That particular implementation might be a little advanced for me. I understand what boyer-moore does in concept but have never implemented it before.

    Here's an update on my code. Yes, I will remove gets and fgetc, believe me I understand when you say uniformity. Right now I just want this to function as it should and then Ill go back and alter it to make it correct. At this point, I need to output separate files according to where the user specifies. I know that there are three files in the binary and so there will be three output files on the hdd somewhere, but there could be any number of files and file sizes. My problem, I believe unless you spot it elsewhere is in one fread() and fwrite().. although I think it is the fread() because I cannot accurately the file size and then use it in the call for fwrite. Oh and speed is not an issue. It just needs to output the correct information found on the binary to the output files, that's it. There are three documents on the binary and will should readable in word processing form. As Mac guessed, .wpd

    Any assistance would be appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct FileHeader {
      unsigned long magic_number;
      unsigned long fileLength;
    } header; 
    
    void performFileIO(const char *infile)
    {
      FILE *FileIn, *FileOut;
      char inBuffer[BUFSIZ];
      char tempBuffer[4];
      int position = 0;
      // marks the header of a file found within the input file
      const unsigned char pattern[] = {0xFF, 0x57, 0x50, 0x43};
    
      // properly open the binary file for reading
      FileIn = fopen(infile, "rb");
    
      if (FileIn == NULL) {
        printf("The file could not be opened, please check your file and location before trying again.\n");
        exit(1);
      }
    
      printf("Enter the name and path for the output file: ");
      gets(inBuffer);
      FileOut = fopen(inBuffer, "w+b");
    
      // position the file marker at the beginning of file
      fseek(FileIn, 0, SEEK_SET); 
    
      while (!feof(FileIn))
      {
        position++;
    
        if (fgetc(FileIn) == pattern[0]) {
          tempBuffer[0] = pattern[0];
          fread(tempBuffer + 1, sizeof(pattern) - 1, 1, FileIn);
    
          if (memcmp(pattern, tempBuffer, sizeof(pattern)) == 0) {
            printf("Match Found.\n");
    
            // now that match is found load pattern into struct
            header.magic_number = (int)pattern;
    
            fseek(FileIn, 21, SEEK_CUR); // find the file size
            fread(tempBuffer, sizeof(pattern), 1, FileIn); // four byte size of file found
    
            // after finding filesize load it into struct
            header.fileLength = (int)tempBuffer;
    
            // retract to the pattern header
            fseek(FileIn, -25, SEEK_CUR);
    
            // read the full - whole file loaded into the structure
            fread(&header, sizeof(struct FileHeader), 1, FileIn);
    
            // present for output to user specified file
            fwrite(&header, sizeof(struct FileHeader), 1, FileOut);
            break; // just for testing one output, one iteration through
            continue;
          }
    
          fseek(FileIn, -3, SEEK_CUR);
        }
      }
    
      // close the files and at the same time flush the buffers
      fclose(FileIn);
      fclose(FileOut);
    }
    
    // program entry point
    int main(int argc, char *argv[])
    {
      char buffer[BUFSIZ];
    
      printf("Please enter the name and path of the binary file: ");
      gets(buffer);
    
      performFileIO(buffer);
    
      printf("\n Done!\n Press enter to exit\n");
      getchar();
    
      return 0;
    }

  2. #17
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45
    Here's my attempt at checking fread for the correct return values... which I might they weren't returning. However, still no correct values from binary to output.

    Code:
    // ProjectThreeSourceA.c
    // This program searches a binary file for a given hex pattern. Files are
    //  found in the binary file via this pattern. The offsets of where the files
    //  were found are output to another file, to be named by user input. Along with
    //  the number of files found and their offsets, the output file will also contain
    //  their sizes.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <limits.h>
    
    #define base UCHAR_MAX + 1
    
    struct FileHeader {
      unsigned long magic_number;
      unsigned long fileLength;
    } header;
    
    void performFileIO(const char *infile)
    {
      FILE *FileIn, *FileOut;
      char inBuffer[BUFSIZ];
      unsigned char tempBuffer[4];
      unsigned long result;
      int position = 0, i;
      // marks the header of a file found within the input file
      const unsigned char pattern[] = {0xFF, 0x57, 0x50, 0x43};
    
      // properly open the binary file for reading
      FileIn = fopen(infile, "rb");
    
      if (FileIn == NULL) {
        printf("The file could not be opened, please check your file and location before trying again.\n");
        exit(1);
      }
    
      printf("Enter the name and path for the output file: ");
      gets(inBuffer);
      FileOut = fopen(inBuffer, "w+b");
    
      // position the file marker at the beginning of file
      fseek(FileIn, 0, SEEK_SET);
    
      while (!feof(FileIn))
      {
        position++;
    
        if (fgetc(FileIn) == pattern[0]) {
          tempBuffer[0] = pattern[0];
          if (fread(tempBuffer + 1, sizeof(char), 3, FileIn) != 3) {
            printf("Error reading file.\n");
            exit(1);
          }
    
          if (memcmp(pattern, tempBuffer, sizeof(pattern)) == 0) {
            printf("Match Found.\n");
    
            // now that match is found compute and load pattern into struct
            for (i = 0, result = 1; i < 4; i++) {
              result = result * base + pattern[ i ];
            }
            header.magic_number = result;
    
            fseek(FileIn, 16, SEEK_CUR); // find the file size
            // four byte size of file found
            if (fread(tempBuffer, sizeof(char), 4, FileIn) != 4) {
              printf("Match found but fatal error reading file.\n");
              exit(1);
            }
    
            // after finding filesize compute and load it into struct
            for (i = 0, result = 1; i < 4; i++) {
              result = result * base + tempBuffer[ i ];
            }
            header.fileLength = result;
    
            // retract to the pattern header
            fseek(FileIn, -20, SEEK_CUR);
    
            // read the full - whole file loaded into the structure
            if (fread(&header, sizeof(struct FileHeader), 1, FileIn) != 1) {
              printf("Match found but fatal error reading of size struct.\n");
              exit(1);
            }
    
            // present for output to user specified file
            fwrite(&header, sizeof(struct FileHeader), 1, FileOut);
            break;
            continue;
          }
    
          fseek(FileIn, -3, SEEK_CUR);
        }
      }
    
      // close the files and at the same time flush the buffers
      fclose(FileIn);
      fclose(FileOut);
    }
    
    // program entry point
    int main(int argc, char *argv[])
    {
      char buffer[BUFSIZ];
    
      printf("Please enter the name and path of the binary file: ");
      gets(buffer);
    
      performFileIO(buffer);
    
      printf("\n Done!\n Press enter to exit\n");
      getchar();
    
      return 0;
    }
    Last edited by CaptainMorgan; 06-17-2007 at 08:20 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  2. Copying Binary Files
    By mikeman118 in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2007, 11:55 PM
  3. Processing binary or plaintext files
    By Jags in forum C Programming
    Replies: 12
    Last Post: 08-04-2006, 03:35 PM
  4. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 12:00 PM
  5. Binary Searching a Structure Array
    By Simon in forum C Programming
    Replies: 10
    Last Post: 09-03-2002, 07:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21