Advertisement
jrhauser11

Recover.c

Jun 11th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.    // check usage
  9.     if (argc != 2)
  10.     {
  11.         printf("Usage: ./recover image\n");
  12.         return 1;
  13.     }
  14.     char *card = argv[1];
  15.     // open file
  16.      FILE *file = fopen(card, "r");
  17.  
  18.     // if file isnt a file tell the user and inform them of such return 1
  19.     if (!file)
  20.     {
  21.         printf("couldn't open that file dawg, like give me a valid chunk of memory to use my dude\n");
  22.         return 1;
  23.     }
  24.     fseek(file, 0, SEEK_END);
  25.     long count = ftell(file);
  26.     fseek(file, 0, SEEK_SET);
  27.    
  28.     int *bytes = malloc(512 * sizeof(int));
  29.     fread(bytes, 1, 512, file);
  30.     int jpg = 0;
  31.     int i  = 0;
  32.     while (i < count)
  33.     {
  34.         if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && bytes[3] >= 0xe0 && bytes[3] <= 0xef)
  35.         {
  36.             char *name = malloc(sizeof(8));
  37.             jpg++;
  38.             sprintf(name, "%03i.jpg", jpg);
  39.             FILE *jpeg = fopen(name, "w");
  40.             fwrite(bytes, 512, 1, jpeg);
  41.             fseek(file, 0, SEEK_SET);
  42.             fseek(file, 512, SEEK_CUR);
  43.             fclose(jpeg);
  44.             free(name);
  45.         }
  46.         else
  47.         {
  48.             fseek(file, 512, SEEK_CUR);
  49.         }
  50.         i++;
  51.     }
  52.     free(bytes);
  53.     fclose(file);
  54.     free(card);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement