All pastes #572991 Raw Edit

Untitled

public c v1 · immutable
·published 2007-06-17 23:26 UTC
rendered paste body
// 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 <stdint.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;        continue;      }      fseek(FileIn, -3, SEEK_CUR);    }  }  // close the files and at the same time flush the buffers  fclose(FileIn);  fclose(FileOut);}// program entry pointint 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;}