Advertisement
Jodyone

recover.c

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