satya5

recover

Sep 10th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5.  
  6. typedef uint8_t  BYTE;
  7.  
  8. typedef struct {
  9.     BYTE out;
  10. }__attribute__((__packed__))
  11. outf;
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.     if(argc != 2) {
  16.         fprintf(stderr, "Usage: %s forensic_file \n", argv[0]);
  17.         return 1;
  18.     }
  19.  
  20.     FILE *img = fopen(argv[1], "r");
  21.  
  22.     if(img == NULL) {
  23.         fprintf(stderr, "Could not open\n");
  24.         return 2;
  25.     }
  26.  
  27.     FILE *out;
  28.     int count = 0;
  29.  
  30.     while(1) {
  31.  
  32.         outf * C = malloc(sizeof(outf) * 512);
  33.         size_t n = fread(C, sizeof(BYTE), 512, img);
  34.  
  35.         char filename[10];
  36.  
  37.         if((int)C[0].out == 0xff && (int)C[1].out == 0xd8 && (int)C[2].out == 0xff && ((int)C[3].out & 0xf0) == 0xe0) {
  38.             if(count == 0) {
  39.                 sprintf(filename, "%03i.jpg", count);
  40.                 out = fopen(filename,"w");
  41.                 fwrite(C, 1, 512, out);
  42.                 count ++;
  43.               //  free(C);
  44.             } else {
  45.                 fclose(out);
  46.                 sprintf(filename, "%03i.jpg", count);
  47.                 out = fopen(filename,"w");
  48.                 fwrite(C, 1, 512, out);
  49.                 count ++;
  50.              //   free(C);
  51.             }
  52.         } else if(count != 0) {
  53.             fwrite(C, 512, 1, out);
  54.            // free(C);
  55.         }
  56.         if(n != 512) {
  57.             fclose (out);
  58.             fclose(img);
  59.             free(C);
  60.             break;
  61.         }
  62.         free(C);
  63.     }
  64.     return  0;
  65. }
Add Comment
Please, Sign In to add comment