Advertisement
Jodyone

recover

Mar 22nd, 2014
137
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.  * 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.     FILE* outptr = fopen(img_ptr,"w");
  37.    
  38.  
  39.   // while our attempt to read a block succeeds
  40.    while(fread(&buffer, sizeof(buffer), 1, inptr) != 0)
  41.    {
  42.        // if this block is the start of new JPEG  
  43.          if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff ) && (buffer[3] == 0xe0))
  44.          {
  45.              // if there is a file open, close it
  46.              if (outptr != NULL)
  47.              {
  48.                  fclose(outptr);
  49.                  jpeg_num++;
  50.                  
  51.                  // open a new file
  52.                  sprintf(img_ptr,"%03d.jpeg",jpeg_num);  
  53.                  outptr = fopen(img_ptr,"w");  
  54.              
  55.                  // write this first block into it
  56.                  fwrite(&buffer,sizeof(buffer),1,outptr);
  57.              }
  58.          }    
  59.              // else
  60.          else
  61.          {
  62.           // if there is a file open, write this block into it
  63.              if (outptr != NULL)
  64.              {
  65.                  fwrite(&buffer,sizeof(buffer),1,outptr);
  66.              }
  67.          }
  68.          
  69.     }
  70.       // Close any remaining files
  71.              fclose(outptr);
  72.              
  73.        
  74.            
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement