Advertisement
geneviedube

Untitled

Aug 25th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.22 KB | None | 0 0
  1. /**
  2.  * Make a program to copy all JPEG images of memory card to recover all thoses deleted images
  3.    by looking for JPEG signature.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9.  
  10.  
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.  
  15.     // one command-line argument
  16.     if (argc != 2)
  17.     {
  18.         fprintf(stderr, "Usage: enter name of forensic image\n");
  19.         return 1;
  20.     }
  21.  
  22.  
  23.     //name of the memroy card file
  24.     char *card_raw = argv[1];
  25.  
  26.     // open memory card file that is the input // cardfile is a file and card_raw is the name of the file
  27.     FILE *cardfile  = fopen(card_raw, "r");
  28.     if (cardfile == NULL)
  29.     {
  30.         fprintf(stderr, "Could not open %s\n", card_raw);
  31.         return 2;
  32.     }
  33.  
  34.  
  35.  
  36.     //define data structure for having exactly 1 byte
  37.     typedef uint8_t BYTE;
  38.  
  39.  
  40.     //to store bytes of the size of an array = 512 =block size long  (size of an array of 512 which is 1 byte long) => store memory which bunch of 512 bytes called buffer
  41.     BYTE buffer[512];
  42.  
  43.     //count for number of JPEG file - should get total of 50 with raw
  44.     int count = 0;
  45.     FILE *image = NULL;
  46.  
  47.     //read the memory card until end of file (end of memory card) so no more 512 block left, where size of block is 512 from FAT
  48.     while (fread(buffer, 512, 1, cardfile) == 1)
  49.     {
  50.         //check the first 4 bytes pattern to find the JPEG signature (fourth byte 0Xe (0 à 7 ou a à f))
  51.         if (buffer [0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && (buffer [3] == 0xe0 || buffer[3] == 0xe1))
  52.         {
  53.  
  54.             //if an outfile is open, close it to than start a new file
  55.             if (count > 0)
  56.             {
  57.                 fclose(image);
  58.             }
  59.  
  60.  
  61.             //if JPEG signature start a new file so create the file to be opened to be able to copy the JPEG image from memory card
  62.  
  63.             //create name of the file with char for jpeg filames with 3 digits => 000.jpg (7 caracters) + "\ 0" (+1 caracter) = total of 8caracters
  64.             char filename[8];
  65.             //create a name for each file
  66.             sprintf(filename,"%03i.jpg", count);
  67.             //increase number of filename for next jpg found => 000.jpg, 001.jpg. 002.jpg etc...
  68.  
  69.             //open the file
  70.             image = fopen(filename, "w");
  71.             count ++;
  72.  
  73.             if (image == NULL);
  74.             {
  75.                 fprintf(stderr, "Could not open new file\n");
  76.                 return 3;
  77.             }
  78.  
  79.              //write the new file so copy the memory card ofr one image at a time
  80.             fwrite(buffer, 512, 1, image);
  81.         }
  82.  
  83.         // write image by image // write the 0 as 0 if blank space
  84.         //if ( size != 1) //stop reading because it s
  85.         // fin c'est le début d'un autre JPEG
  86.  
  87.         //if no JPEG signature, just copy on the file until closed or end of file (when found a new signature file will be closed)
  88.         if (count > 0)
  89.         {
  90.             //write all the blocks after the JPEG signature
  91.             fwrite(buffer, 512, 1, image);
  92.         }
  93.  
  94.     }
  95.     // close outfile each one at the time ( when encounter another signature)
  96.     fclose(image);
  97.  
  98.     // close memory file
  99.     fclose(cardfile);
  100.  
  101.  
  102.     // success
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement