Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Gumshoe_NLA.cpp
- *
- * Guidelines:
- * To open the file for reading: FILE *fp = fopen(filename, "r"); the mystery.raw file is only going to be read,
- * so it only needs the "r" flag.
- * Extracted files should follow Name_ImageX.jpg, where X starts at 0 and increments and Name is your last name.
- *
- *
- * Created on: Nov 5, 2010
- * Author: NLA
- */
- #include "Gumshoe_NLA.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #define BUFFSIZE 512
- #define SIGLENGTH 4
- void printUsage() { // Print out a message for the user to indicate proper program usage.
- printf("\nValid usage: Gumshoe_NLA.exe [File] , where");
- printf("\n\t[File] is the name of the file (in the same directory as Gumshoe_NLA) that will be searched for JPEGs.");
- printf("\n\nPlease re-run the program and try again.");
- }
- // Checks to see if an improper number of arguments was passed.
- void processCommandLine(int argc, char *argv[]) {
- if (argc != 2) {
- printf("\n");
- fprintf(stderr, "Improper number of parameters passed. ");
- printf("Printing program syntax information.\n");
- printUsage();
- exit(1);
- }
- }
- int compareFirstFourBytes (char first[], char second[]) {
- int i = 0;
- for (i = 0; i < SIGLENGTH; i++) {
- if (first[i] != second [i])
- return 0;
- }
- return 1;
- }
- int main(int argc, char *argv[]) {
- int numRead; // Int number of bytes read from the rawFile.
- int numExtracted = 0; // Number of extracted JPEGs from file.
- char rawFileBuffer[BUFFSIZE]; // Buffer that holds incremental 512 bytes from the rawFile.
- char rawFileFirstFourBytes[SIGLENGTH]; // The first four bytes of the incremental 512 bytes buffer.
- // Signatures of JPEG files: 0xFF 0xD8 0XFF 0xE0 or 0xFF 0xD8 0xFF 0XE1
- // Char arrays that hold the signatures of essentially all JPEG files.
- char jpegSigOne[SIGLENGTH];
- (jpegSigOne[0] = 0xff); (jpegSigOne[1] = 0xd8); (jpegSigOne[2] = 0xff); (jpegSigOne[3] = 0xe0);
- char jpegSigTwo[SIGLENGTH];
- (jpegSigTwo[0] = 0xff); (jpegSigTwo[1] = 0xd8); (jpegSigTwo[2] = 0xff); (jpegSigTwo[3] = 0xe1);
- FILE *extractedFile;
- // Processes the command line input and checks for obvious errors; makes sure the program should run.
- processCommandLine(argc, argv);
- FILE *rawFile; // Begin opening the file, rawFile.
- if ((rawFile = fopen(argv[1], "rb")) == 0) { // Handles the case that the file could not be opened for a variety of reasons.
- printf("\n");
- fprintf(stderr, "File %s was not able to be opened! ", argv[1]);
- printf("Please re-run the program and try again.\n");
- exit(1); // Terminates with non-0 int.
- }
- // File is now opened for read-only binary reading from the current directory.
- printf("Searching %s for JPEGs ...", argv[1]);
- // Routine for looking through blocks of 512 for JPEG signatures here
- while ((numRead = fread(&rawFileBuffer, sizeof(char), BUFFSIZE, rawFile)) > 0) {
- char fileName[20];
- sprintf(fileName, "NLA_Image%d.jpg", numExtracted);
- fread(&rawFileFirstFourBytes, sizeof(char), SIGLENGTH, rawFile);
- if (compareFirstFourBytes(rawFileFirstFourBytes, jpegSigOne) || compareFirstFourBytes(rawFileFirstFourBytes, jpegSigTwo)) {
- fclose(extractedFile);
- extractedFile = fopen(fileName, "a");
- numExtracted++;
- }
- // Write the 512 bytes from rawFile buffer into extractedFile; TO BE WRITTEN.
- }
- // If no JPEGs are found:
- if (numExtracted == 0) {
- printf("\n");
- fprintf(stderr, "No JPEGs found in file! ");
- printf("Program exiting!");
- exit(1);
- }
- if (fclose(rawFile) < 0) {
- printf("\n");
- fprintf(stderr, "Unable to close %s! ", argv[1]);
- printf("Program exiting!");
- exit(1);
- }
- // Tells how many JPEGs were successfully extracted from the passed rawFile, and exits.
- printf("%d JPEGs successfully extracted. Program now successfully terminating.", numExtracted);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement