Advertisement
Guest User

Untitled

a guest
Jul 19th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. /**
  2.  * recover.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 4
  6.  *
  7.  * Recovers JPEGs from a forensic image.
  8.  */
  9.  
  10. #include <cs50.h>
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14.  
  15. typedef uint8_t BYTE;
  16.  
  17. #define BLOCK 512;
  18.  
  19. int main(int argc, char* argv[])
  20. {
  21.     // TODO
  22.  
  23.     // open card.raw to read
  24.     FILE* file = fopen("card.raw", "r");
  25.    
  26.     // check if file is NULL
  27.     if (file == NULL)
  28.     {
  29.         printf("Could not open %s.\n", "card.raw");
  30.         return 1;
  31.     }
  32.  
  33.     // 512 byte block
  34.     uint8_t block[512];
  35.    
  36.     int i = 0;
  37.     int j = 0;
  38.     FILE* img = NULL;
  39.    
  40.     // read from the file in a loop
  41.     while (fread(block, sizeof(block), 1, file) == 1)
  42.     {        
  43.         // if signature is found
  44.         if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] == 0xe0 || block[3] == 0xe1))
  45.         {
  46.             // if this is the first jpg
  47.             if (i == 0)
  48.             {
  49.                 // malloc space for title of new jpgs
  50.                 char* title = malloc(sizeof(char));
  51.                
  52.                 // print new title of jpgs
  53.                 sprintf(title, "%03d.jpg", i);
  54.                
  55.                 // open newly titled jpg
  56.                 img = fopen(title, "w");
  57.                
  58.                 if (img == NULL)
  59.                 {
  60.                     fclose(file);
  61.                     fprintf(stderr, "Could not create %s.\n", title);
  62.                     return 2;
  63.                 }
  64.  
  65.                 fwrite(&block, sizeof(block), 1, img);
  66.                 // iterate i so program knows first jpg has been found
  67.                 i++;
  68.                 j++;
  69.                 free(title);
  70.             }
  71.            
  72.             // if not first jpg
  73.             else
  74.             {
  75.                 // close previous jpg
  76.                 fclose(img);
  77.                
  78.                 // increment 'i' (keeps track of jpgs)
  79.                 // my (not so elegant) solution on how to not pre-increment after first jpg
  80.                 if (j == 1)
  81.                 {
  82.                     j++;
  83.                 }
  84.                
  85.                 else
  86.                 {
  87.                     i++;
  88.                 }
  89.                
  90.                 // malloc space for title of new jpgs
  91.                 char* title = malloc(sizeof(char));
  92.                
  93.                 // print new title of jpgs
  94.                 sprintf(title, "%03d.jpg", i);
  95.                
  96.                 // open newly titled jpg
  97.                 img = fopen(title, "w");
  98.                
  99.                 if (img == NULL)
  100.                 {
  101.                     fclose(file);
  102.                     fprintf(stderr, "Could not create %s.\n", title);
  103.                     return 2;
  104.                 }
  105.                
  106.                 // write first block to new jpg
  107.                 fwrite(&block, sizeof(block), 1, img);
  108.                 free(title);
  109.             }
  110.         }
  111.        
  112.         // if new jpg signature not found, but i > 0 (meaning first jpg has already been hit)
  113.         else if (i > 0)
  114.         {
  115.             fwrite(&block, sizeof(block), 1, img);
  116.         }
  117.        
  118.         // if first jpg has not been found, do nothing
  119.         else
  120.         {
  121.             i = i + 0;
  122.         }
  123.     }
  124.  
  125. fclose(file);
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement