Advertisement
rupek1995

Untitled

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