Advertisement
rupek1995

recover.c

Mar 7th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. typedef uint8_t  BYTE;
  6.  
  7. int header_is_jpeg(FILE *memcard);
  8. void create_new_outfile(char **outfile, FILE **outptr, int *filename_counter);
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     // check proper usage
  13.     if (argc != 2)
  14.     {
  15.         fprintf(stderr, "usage: ./recover filename\n");
  16.         return 1;
  17.     }
  18.  
  19.     // keep filenames
  20.     char    *infile = argv[1],
  21.             *outfile = NULL;
  22.     FILE    *inptr = fopen(infile, "r"),
  23.             *outptr = NULL;
  24.     BYTE    *byte_array = malloc(512 * sizeof(BYTE));
  25.     int     filename_counter = 0;
  26.  
  27.     // program works only with initialized outfile
  28.     outfile = malloc(sizeof("000.jpg") + 1);
  29.     sprintf(outfile, "%03i.jpg", filename_counter);
  30.     outptr = fopen(outfile, "w");
  31.  
  32.     // check if files open properly
  33.     if (inptr == NULL)
  34.     {
  35.         fprintf(stderr, "Could not open %s\n", infile);
  36.         return 2;
  37.     }
  38.  
  39.     // skip memory blocks until jpeg header is found
  40.     while (!header_is_jpeg(inptr))
  41.     {
  42.         fseek(inptr, 512, SEEK_CUR);
  43.     }
  44.  
  45.     if (header_is_jpeg(inptr))
  46.     {
  47.         create_new_outfile(&outfile, &outptr, &filename_counter);
  48.     }
  49.  
  50.     if (outptr == NULL)
  51.     {
  52.         fprintf(stderr, "Could not write in %s file\n", outfile);
  53.         return 3;
  54.     }
  55.  
  56.     // only keep reading if block is proper size (512)
  57.     while (fread(outptr, 1, 512, inptr) == 512)
  58.     {
  59.         // rewind seek
  60.         fseek(inptr, -512, SEEK_CUR);
  61.  
  62.         // read and write 512 bytes, 1 byte at a time
  63.         for(int i = 0, arr_size = sizeof(byte_array); i < arr_size; i++)
  64.         {
  65.             fread(&byte_array[i], 1, 1, inptr);
  66.             fwrite(&byte_array[i], 1, 1, outptr);
  67.         }
  68.  
  69.         // if next block has a new jpeg header - create new file
  70.         if (header_is_jpeg(inptr))
  71.         {
  72.             create_new_outfile(&outfile, &outptr, &filename_counter);
  73.         }
  74.     }
  75.  
  76.     // close files
  77.     fclose(inptr);
  78.     fclose(outptr);
  79.     return 0;
  80. }
  81.  
  82.  
  83. int header_is_jpeg(FILE *memcard)
  84. {
  85.     BYTE *BLOCKHEADER = malloc(4 * sizeof(BYTE));
  86.     // read first 4 bytes of current memory block
  87.     for (int i = 0; i < 4; i++)
  88.     {
  89.         fread(&BLOCKHEADER[i], 1, 1, memcard);
  90.     }
  91.  
  92.     // rewind seek back to beginning of block
  93.     fseek(memcard, -4, SEEK_CUR);
  94.  
  95.     // check if header is the same as JPEG header, return true if it is
  96.     if (BLOCKHEADER[0] == 0xff &&
  97.         BLOCKHEADER[1] == 0xd8 &&
  98.         BLOCKHEADER[2] == 0xff &&
  99.         (BLOCKHEADER[3] & 0xf0) == 0xe0)
  100.     {
  101.         printf("JPEG Header found!\n");
  102.         free(BLOCKHEADER);
  103.         return 1;
  104.     }
  105.     else
  106.     {
  107.         free(BLOCKHEADER);
  108.         return 0;
  109.     }
  110. }
  111.  
  112. void create_new_outfile(char **outfile, FILE **outptr, int *filename_counter)
  113. {
  114.     // if a file is open - close the old one before creating new one
  115.     if (*outptr != NULL)
  116.     {
  117.         printf("successfuly recovered image: %s\n", *outfile);
  118.         fclose(*outptr);
  119.         free(*outfile);
  120.     }
  121.  
  122.     // create new output file
  123.     *outfile = malloc(sizeof("000.jpg") + 1);
  124.     sprintf(*outfile, "%03i.jpg", *filename_counter);
  125.     *outptr = fopen(*outfile, "w");
  126.     (*filename_counter)++;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement