Advertisement
Jodyone

recover

Mar 24th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. /**
  2.  * recover.c
  3.  *
  4.  * Jody W Moore
  5.  *
  6.  * Computer Science 50
  7.  * Problem Set 5
  8.  *
  9.  * Recovers JPEGs from a forensic image.
  10.  */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #define CHUNK 512
  15.  
  16. int main(int argc, char* argv[])
  17. {
  18.     // TODO
  19.     typedef unsigned char BYTE;
  20.    
  21.     // remember filenames
  22.     char* infile = "card.raw";
  23.    
  24.       // open card file
  25.      
  26.     // open input file
  27.     FILE* inptr = fopen("card.raw" ,"r");
  28.     if (inptr == NULL)
  29.     {
  30.         printf("Could not open %s.\n", infile);
  31.         return 2;
  32.     }
  33.     BYTE buffer[CHUNK];
  34.     int jpeg_num = 0;
  35.     char img_ptr[20];
  36.     sprintf(img_ptr,"%03d.jpeg",jpeg_num);
  37.     FILE* outptr = fopen(img_ptr,"w");
  38.    
  39.  
  40.   // while our attempt to read a block succeeds
  41.     while(fread(&buffer, sizeof(buffer), 1, inptr) != 0)
  42.     {
  43.         // if this block is the start of new JPEG  
  44.         if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff ) && (buffer[3] == 0xe0))
  45.         {
  46.             // if there is a file open, close it
  47.             if (outptr != NULL)
  48.             {
  49.                 fclose(outptr);
  50.                  jpeg_num++;
  51.                  
  52.                 // open a new file
  53.                 sprintf(img_ptr,"%03d.jpeg",jpeg_num);  
  54.                 outptr = fopen(img_ptr,"w");
  55.                  
  56.                  
  57.              
  58.                 // write this first block into it
  59.                 fwrite(&buffer,sizeof(buffer),1,outptr);
  60.             }
  61.         }    
  62.          // else
  63.         else
  64.         {
  65.             // if there is a file open, write this block into it
  66.             if (outptr != NULL)
  67.             {
  68.                 fwrite(&buffer,sizeof(buffer),1,outptr);
  69.             }
  70.         }
  71.          
  72.     }
  73.     // Close any remaining files
  74.     fclose(outptr);        
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement