Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.84 KB | None | 0 0
  1. /*
  2.  * Gumshoe_NLA.cpp
  3.  *
  4.  * Guidelines:
  5.  * To open the file for reading: FILE *fp = fopen(filename, "r"); the mystery.raw file is only going to be read,
  6.  *      so it only needs the "r" flag.
  7.  * Extracted files should follow Name_ImageX.jpg, where X starts at 0 and increments and Name is your last name.
  8.  *
  9.  *
  10.  *  Created on: Nov 5, 2010
  11.  *      Author: NLA
  12.  */
  13.  
  14. #include "Gumshoe_NLA.h"
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20.  
  21. #define BUFFSIZE 512
  22. #define SIGLENGTH 4
  23.  
  24. void printUsage() { // Print out a message for the user to indicate proper program usage.
  25.     printf("\nValid usage: Gumshoe_NLA.exe [File] , where");
  26.     printf("\n\t[File] is the name of the file (in the same directory as Gumshoe_NLA) that will be searched for JPEGs.");
  27.     printf("\n\nPlease re-run the program and try again.");
  28. }
  29.  
  30. // Checks to see if an improper number of arguments was passed.
  31. void processCommandLine(int argc, char *argv[]) {
  32.     if (argc != 2) {
  33.         printf("\n");
  34.         fprintf(stderr, "Improper number of parameters passed. ");
  35.         printf("Printing program syntax information.\n");
  36.         printUsage();
  37.         exit(1);
  38.     }
  39. }
  40.  
  41. int compareFirstFourBytes (char first[], char second[]) {
  42.     int i = 0;
  43.     for (i = 0; i < SIGLENGTH; i++) {
  44.         if (first[i] != second [i])
  45.             return 0;
  46.     }
  47.  
  48.     return 1;
  49.  
  50. }
  51.  
  52. int main(int argc, char *argv[]) {
  53.  
  54.     int numRead; // Int number of bytes read from the rawFile.
  55.     int numExtracted = 0; // Number of extracted JPEGs from file.
  56.     char rawFileBuffer[BUFFSIZE]; // Buffer that holds incremental 512 bytes from the rawFile.
  57.     char rawFileFirstFourBytes[SIGLENGTH]; // The first four bytes of the incremental 512 bytes buffer.
  58.  
  59.     // Signatures of JPEG files: 0xFF 0xD8 0XFF 0xE0 or 0xFF 0xD8 0xFF 0XE1
  60.  
  61.     // Char arrays that hold the signatures of essentially all JPEG files.
  62.     char jpegSigOne[SIGLENGTH];
  63.     (jpegSigOne[0] = 0xff); (jpegSigOne[1] = 0xd8); (jpegSigOne[2] = 0xff); (jpegSigOne[3] = 0xe0);
  64.     char jpegSigTwo[SIGLENGTH];
  65.     (jpegSigTwo[0] = 0xff); (jpegSigTwo[1] = 0xd8); (jpegSigTwo[2] = 0xff); (jpegSigTwo[3] = 0xe1);
  66.  
  67.     FILE *extractedFile;
  68.  
  69.     // Processes the command line input and checks for obvious errors; makes sure the program should run.
  70.     processCommandLine(argc, argv);
  71.  
  72.     FILE *rawFile; // Begin opening the file, rawFile.
  73.     if ((rawFile = fopen(argv[1], "rb")) == 0) { // Handles the case that the file could not be opened for a variety of reasons.
  74.         printf("\n");
  75.         fprintf(stderr, "File %s was not able to be opened! ", argv[1]);
  76.         printf("Please re-run the program and try again.\n");
  77.         exit(1); // Terminates with non-0 int.
  78.     }
  79.  
  80.     // File is now opened for read-only binary reading from the current directory.
  81.  
  82.     printf("Searching %s for JPEGs ...", argv[1]);
  83.  
  84.     // Routine for looking through blocks of 512 for JPEG signatures here
  85.     while ((numRead = fread(&rawFileBuffer, sizeof(char), BUFFSIZE, rawFile)) > 0) {
  86.  
  87.         char fileName[20];
  88.         sprintf(fileName, "NLA_Image%d.jpg", numExtracted);
  89.  
  90.         fread(&rawFileFirstFourBytes, sizeof(char), SIGLENGTH, rawFile);
  91.  
  92.         if (compareFirstFourBytes(rawFileFirstFourBytes, jpegSigOne) || compareFirstFourBytes(rawFileFirstFourBytes, jpegSigTwo)) {
  93.             fclose(extractedFile);
  94.             extractedFile = fopen(fileName, "a");
  95.             numExtracted++;
  96.         }
  97.  
  98.             // Write the 512 bytes from rawFile buffer into extractedFile; TO BE WRITTEN.
  99.  
  100.     }
  101.  
  102.     // If no JPEGs are found:
  103.     if (numExtracted == 0) {
  104.         printf("\n");
  105.         fprintf(stderr, "No JPEGs found in file! ");
  106.         printf("Program exiting!");
  107.         exit(1);
  108.     }
  109.  
  110.     if (fclose(rawFile) < 0) {
  111.         printf("\n");
  112.         fprintf(stderr, "Unable to close %s! ", argv[1]);
  113.         printf("Program exiting!");
  114.         exit(1);
  115.     }
  116.  
  117.     // Tells how many JPEGs were successfully extracted from the passed rawFile, and exits.
  118.     printf("%d JPEGs successfully extracted. Program now successfully terminating.", numExtracted);
  119.  
  120.     return EXIT_SUCCESS;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement