Advertisement
Dhaia_v

Recover

Feb 28th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     if (argc != 2)
  8.     {
  9.         printf("Incorrect usage\n");
  10.         return 1;
  11.     }
  12.  
  13.     FILE *file = fopen(argv[1], "r");
  14.  
  15.     int buffer[512];
  16.     char filename[8];
  17.     int jpg_counter = 0;
  18.     FILE *img = NULL;
  19.  
  20.     while (fread(buffer, 512, 1, file) == 1)
  21.     {
  22.         if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
  23.         {
  24.             if (jpg_counter == 0) //If this is the first jpg, then start writting the first file 000.jpg
  25.             {
  26.                 sprintf(filename, "%03i.jpg", jpg_counter);
  27.  
  28.                 img = fopen(filename, "w");
  29.  
  30.                 fwrite (filename, 512, 1, img);
  31.  
  32.             }
  33.             else  //If i already found a jpg, then, close the previous file, increment jpg_counter and open a new file
  34.             {
  35.                 fclose (img);
  36.                 jpg_counter ++;
  37.  
  38.                 sprintf(filename, "%03i.jpg", jpg_counter);
  39.  
  40.                 img = fopen(filename, "w");
  41.                 fwrite (filename, 512, 1, img);
  42.             }
  43.         }
  44.  
  45.         else
  46.         {
  47.             if (jpg_counter != 0 && img != NULL) //If it's not a start of a jpeg, then, If I already found a jpg and the image is open, keep writing
  48.             {
  49.                 img = fopen(filename, "w");
  50.                 fwrite (filename, 512, 1, img);
  51.                 fclose (img);
  52.             }
  53.  
  54.         }
  55.     }
  56.  
  57.     fclose (file);
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement