Advertisement
infmega

W3 Recover

Sep 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     if(argc != 2)
  7.     {
  8.         fprintf(stderr, "Usage: ./recover image\n");
  9.         return 1;
  10.     }
  11.  
  12.     char *infile = argv[1];
  13.  
  14.     FILE *inptr = fopen(infile, "r");
  15.     if (inptr == NULL)
  16.     {
  17.         fprintf(stderr, "Could not open %s.\n", infile);
  18.         return 2;
  19.     }
  20.  
  21.     //open new outfile
  22.     FILE *outptr = NULL;
  23.  
  24.     //create a buffer
  25.     char buffer[512];
  26.  
  27.     //sprintf variables
  28.     char name[10];
  29.     int a = 0;
  30.     int q = 1;
  31.     int w = 1;
  32.  
  33.     //iterate
  34.     while(fread(&buffer, 512, 1, inptr))
  35.     {
  36.  
  37.         //find jpeg header
  38.         if((buffer[0]&0xff) == 0xff && (buffer[1]&0xff) == 0xd8 && (buffer[2]&0xff) == 0xff && (buffer[3] & 0xf0) == 0xe0)
  39.         {
  40.             //printf("%ith head read, 1: %x, 2: %x, 3: %x, 4: %x\n", w, buffer[0],buffer[1],buffer[2],buffer[3]);
  41.             w++;
  42.  
  43.             //create a numbered file
  44.             sprintf(name,"%03d.jpg",a);
  45.  
  46.             //open outfile
  47.             outptr = fopen(name,"w");
  48.  
  49.             //write head chunk
  50.             fwrite(&buffer, 512, 1, outptr);
  51.             //printf("%ith head write, 1: %x, 2: %x, 3: %x, 4: %x\n", q, buffer[0],buffer[1],buffer[2],buffer[3]);
  52.             q++;
  53.  
  54.             //identify next chunk
  55.             while(fread(&buffer, 512, 1, inptr))
  56.             {
  57.                 //printf("%ith body read, 1: %x, 2: %x, 3: %x, 4: %x\n", w, buffer[0],buffer[1],buffer[2],buffer[3]);
  58.                 w++;
  59.  
  60.                 if((buffer[0]&0xff) == 0xff && (buffer[1]&0xff) == 0xd8 && (buffer[2]&0xff) == 0xff && (buffer[3] & 0xf0) == 0xe0)
  61.                 {
  62.                     //printf("2st head, 1: %x, 2: %x, 3: %x, 4: %x\n", buffer[0],buffer[1],buffer[2],buffer[3]);
  63.                     //close previous jpg file
  64.                     fclose(outptr);
  65.                     printf("%s is closed\n",name);
  66.  
  67.                     //update variables
  68.                     a++;
  69.  
  70.                     //create a numbered file
  71.                     sprintf(name,"%03d.jpg",a);
  72.  
  73.                     //open new outfile
  74.                     outptr = fopen(name,"w");
  75.  
  76.                     //write body chunk
  77.                     fwrite(&buffer, 512, 1, outptr);
  78.                 }
  79.                 else
  80.                 {
  81.                     //write body chunk
  82.                     fwrite(&buffer, 512, 1, outptr);
  83.                     //printf("%ith body write, 1: %x, 2: %x, 3: %x, 4: %x\n", q, buffer[0],buffer[1],buffer[2],buffer[3]);
  84.                     q++;
  85.                 }
  86.             }
  87.  
  88.         }
  89.  
  90.     }
  91.  
  92.     fclose(inptr);
  93.     fclose(outptr);
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement