Advertisement
Theo107

recover v0

Apr 11th, 2024
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. // jpegs always have a header 0xFF 0xD8 0xFF 0xEn where n can equal 0 through F
  7.  
  8. typedef uint8_t BYTE;
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     // Accept a single command-line argument
  13.     if (argc != 2)
  14.     {
  15.         printf("Usage: ./recover FILE\n");
  16.         return 1;
  17.     }
  18.  
  19.     // Open the memory card
  20.     FILE *card = fopen(argv[1], "r");
  21.  
  22.     // Create a buffer for a block of data
  23.     BYTE buffer[512];
  24.  
  25.     int count = 0;
  26.     char *filename = "000.jpg";
  27.     FILE *img = fopen(filename, "w");
  28.     printf("Initialized\n");
  29.  
  30.  
  31.     // While there's still data left to read from the memory card
  32.     while (fread(buffer, 1, 512, card) == 512)
  33.     {
  34.  
  35.  
  36.         // if start of jpeg
  37.         if (buffer[0] == 0xFF && buffer[1] == 0xD8 && buffer[2] == 0xFF && (buffer[3] & 0xF0) == 0xE0)
  38.         {
  39.             printf("line 39\n");
  40.             // if not first jpeg
  41.             if (count)
  42.             {
  43.                 // close current file
  44.                 printf("entered count if\n");
  45.                 fclose(img);
  46.                 printf("closed img\n");
  47.                 // open new file "###.jpg"
  48.                 sprintf(filename, "%03i.jpg", count);
  49.                 printf("printed to filename %s\n", filename);
  50.                 img = fopen(filename, "w");
  51.                 printf("opened %s\n", filename);
  52.             }
  53.             printf("line 53\n");
  54.             count++;
  55.         }
  56.         // else
  57.         else
  58.         {
  59.             printf("line 59\n");
  60.             // if already found jpeg
  61.             if (count)
  62.             {
  63.                 //keep writing to it
  64.                 fwrite(buffer, 1, 512, img);
  65.             }
  66.         }
  67.     }
  68.     // close remaining files
  69.     fclose(img);
  70.     fclose(card);
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement