DityaSen

Recover PSET4

Mar 25th, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     if (argc != 2)
  9.     {
  10.         printf("Usage : ./recover [IMAGE]");
  11.         return 1;
  12.     }
  13.  
  14.     //Open File
  15.     FILE *input = fopen (argv[1], "r");
  16.  
  17.     if (input == NULL)
  18.     {
  19.         printf("Could not open file.");
  20.         return 2;
  21.     }
  22.  
  23.     FILE *output = NULL;
  24.     unsigned char buffer[512];
  25.     int count = 0;
  26.  
  27.     char *filename = malloc ( 8 * sizeof(char));
  28.  
  29.     while (fread(buffer, sizeof(char), 512, input))
  30.     {
  31.         if (buffer[0] == 0xff && buffer[1] == 0xd8 && (buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0))
  32.         {
  33.             sprintf(filename, "%03i.jpg", count);
  34.  
  35.             output = fopen(filename, "w");
  36.  
  37.             count++;
  38.         }
  39.  
  40.         if (output != NULL)
  41.         {
  42.             fwrite(buffer, sizeof(char), 512, output);
  43.         }
  44.     }
  45.  
  46.     free(filename);
  47.     fclose (output);
  48.     fclose (input);
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment