Advertisement
Guest User

Recover image

a guest
Sep 18th, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.85 KB | None | 0 0
  1.  #include <stdio.h>
  2.  #include <stdlib.h>
  3.  #include <stdint.h>
  4.  
  5.  typedef uint8_t BYTE;
  6.  
  7.  #define BLOCKSIZE 512
  8.  
  9.  
  10.  int main(void)
  11.  {
  12.     // define 512 byte block
  13.     BYTE block[BLOCKSIZE];
  14.    
  15.     // open memory card
  16.     FILE* fp = fopen("card.raw", "r");
  17.    
  18.     // check if file opened
  19.     if (fp == NULL)
  20.     {
  21.         printf("Error opening file\n");
  22.         return 1;
  23.     }
  24.     // open output file
  25.     FILE* outfile;
  26.     outfile = NULL;
  27.     int num = 0;
  28.     char filename[14];
  29.     int x = 0;
  30.     int y = 512;
  31.    
  32.     // main block till end of file
  33.     while (y == 512)
  34.     {
  35.         if ( x == 0)
  36.         {
  37.         // read every 512 block
  38.         fread(&block, sizeof(block), 1 , fp);
  39.         }
  40.        
  41.         // if 4 first characters jpg
  42.         if ((block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && block[3] == 0xe0) || (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && block[3] == 0xe1))
  43.         {
  44.             sprintf(filename, "%03d_output.jpg", num);
  45.             outfile = fopen(filename, "a");
  46.             fwrite(&block, sizeof(block), 1, outfile);
  47.             //fseek(fp, sizeof(block), SEEK_CUR);
  48.            
  49.             fread(&block, sizeof(block), 1 , fp);
  50.             while (!((block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && block[3] == 0xe0) || (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && block[3] == 0xe1)))
  51.             {
  52.                 fwrite(&block, sizeof(block), 1, outfile);
  53.                 y = fread(&block, sizeof(block), 1, fp);
  54.             }
  55.             //fseek(fp, -(sizeof(block)), SEEK_CUR);
  56.             fclose(outfile);
  57.             num++;
  58.             x = 1;
  59.             //y = fread(&block, sizeof(block), 1, fp);
  60.         }
  61.         //printf("%s\n", block);
  62.        
  63.     }
  64.    
  65.     // close memory card
  66.     fclose(fp);
  67.    
  68.     return 0;
  69.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement