Advertisement
rupek1995

Untitled

Mar 7th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 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.         free(byte_array);
  36.         free(outfile);
  37.         fprintf(stderr, "Could not open %s\n", infile);
  38.         return 2;
  39.     }
  40.  
  41.     // skip memory blocks until jpeg header is found
  42.     while (!header_is_jpeg(inptr))
  43.     {
  44.         fseek(inptr, 512, SEEK_CUR);
  45.     }
  46.  
  47.     if (header_is_jpeg(inptr))
  48.     {
  49.         create_new_outfile(&outfile, &outptr, &filename_counter);
  50.     }
  51.  
  52.     if (outptr == NULL)
  53.     {
  54.         free(byte_array);
  55.         free(outfile);
  56.         fprintf(stderr, "Could not write in %s file\n", outfile);
  57.         return 3;
  58.     }
  59.  
  60.     // only keep reading if block is proper size (512)
  61.     while (fread(outptr, 1, 512, inptr) == 512)
  62.     {
  63.         // rewind seek
  64.         fseek(inptr, -512, SEEK_CUR);
  65.  
  66.         // read and write 512 bytes, 1 byte at a time
  67.         for(int i = 0, arr_size = sizeof(byte_array); i < arr_size; i++)
  68.         {
  69.             fread(&byte_array[i], 1, 1, inptr);
  70.             fwrite(&byte_array[i], 1, 1, outptr);
  71.         }
  72.  
  73.         // if next block has a new jpeg header - create new file
  74.         if (header_is_jpeg(inptr))
  75.         {
  76.             create_new_outfile(&outfile, &outptr, &filename_counter);
  77.         }
  78.     }
  79.  
  80.     // close files
  81.     free(byte_array);
  82.     free(outfile);
  83.     fclose(inptr);
  84.     fclose(outptr);
  85.     return 0;
  86. }
  87.  
  88.  
  89. int header_is_jpeg(FILE *memcard)
  90. {
  91.     BYTE *BLOCKHEADER = malloc(4 * sizeof(BYTE));
  92.     // read first 4 bytes of current memory block
  93.     for (int i = 0; i < 4; i++)
  94.     {
  95.         fread(&BLOCKHEADER[i], 1, 1, memcard);
  96.     }
  97.  
  98.     // rewind seek back to beginning of block
  99.     fseek(memcard, -4, SEEK_CUR);
  100.  
  101.     // check if header is the same as JPEG header, return true if it is
  102.     if (BLOCKHEADER[0] == 0xff &&
  103.         BLOCKHEADER[1] == 0xd8 &&
  104.         BLOCKHEADER[2] == 0xff &&
  105.         (BLOCKHEADER[3] & 0xf0) == 0xe0)
  106.     {
  107.         printf("JPEG Header found!\n");
  108.         free(BLOCKHEADER);
  109.         return 1;
  110.     }
  111.     else
  112.     {
  113.         free(BLOCKHEADER);
  114.         return 0;
  115.     }
  116. }
  117.  
  118. void create_new_outfile(char **outfile, FILE **outptr, int *filename_counter)
  119. {
  120.     // if a file is open - close the old one before creating new one
  121.     if (*outptr != NULL)
  122.     {
  123.         printf("successfuly recovered image: %s\n", *outfile);
  124.         fclose(*outptr);
  125.         free(*outfile);
  126.     }
  127.  
  128.     // create new output file
  129.     *outfile = malloc(sizeof("000.jpg") + 1);
  130.     sprintf(*outfile, "%03i.jpg", *filename_counter);
  131.     *outptr = fopen(*outfile, "w");
  132.     (*filename_counter)++;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement