Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     // check if arg count is 2
  8.     if (argc != 2)
  9.     {
  10.         printf("Usage: ./recover image\n");
  11.         return 1;
  12.     }
  13.  
  14.     // create a pointer to open the memory card file
  15.     FILE *inptr = fopen("card.raw", "r");
  16.     // check if pointer is Null, if it is then return 1
  17.     if (inptr == NULL)
  18.     {
  19.         printf("this file is empty\n");
  20.         return 1;
  21.     }
  22.  
  23.     FILE *imgOut = NULL;
  24.  
  25.     char filename[10];
  26.  
  27.     int count = 0;
  28.     // create buffer array
  29.     unsigned char buffer[512];
  30.  
  31.  
  32.     do
  33.     {
  34.         // read in buffer
  35.         fread(&buffer, 512, 1, inptr);
  36.  
  37.  
  38.        // printf("this is it\n");
  39.         // check if the start of new jpeg image
  40.         if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
  41.         {
  42.             printf("this is a jpeg\n");
  43.             sprintf(filename, "test.jpg");
  44.             imgOut = fopen(filename, "w");
  45.  
  46.             if (imgOut == NULL)
  47.             {
  48.                 printf("img is NULL\n");
  49.                 fclose(inptr);
  50.                 return 1;
  51.             }
  52.  
  53.             // if it's the very first jpeg
  54.             if (count == 0)
  55.             {
  56.                 //img = fopen(filename, "w");
  57.                 fwrite(&buffer, 512, 1, imgOut);
  58.                 //count++;
  59.  
  60.             }
  61.  
  62.  
  63.  
  64.         }
  65.         else if (imgOut)
  66.         {
  67.  
  68.             fwrite(&buffer, 512, 1, imgOut);
  69.         }
  70.  
  71.  
  72.  
  73.  
  74.     }
  75.     while (!feof(inptr));
  76.  
  77.     fclose(inptr);
  78.     fclose(imgOut);
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement