Advertisement
rupek1995

recover.c byte-by-byte

Mar 8th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 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.     int     filename_counter = 0;
  21.     char    *infile = argv[1], *outfile = NULL;
  22.     FILE    *inptr = fopen(infile, "r"), *outptr = NULL;
  23.     BYTE    *byte_array = malloc(512 * sizeof(BYTE));
  24.  
  25.     // check if input file opens properly
  26.     if (inptr == NULL)
  27.     {
  28.         free(byte_array);
  29.         free(outfile);
  30.         fprintf(stderr, "Could not open %s\n", infile);
  31.         return 2;
  32.     }
  33.  
  34.     // skip memory blocks until a block with a JPEG is found
  35.     while (!header_is_jpeg(inptr))
  36.     {
  37.         fseek(inptr, 512, SEEK_CUR);
  38.     }
  39.  
  40.     // keep reading memory blocks while they're 512 bytes each
  41.     while (fread(byte_array, 1, 512, inptr) == 512)
  42.     {
  43.         // rewind seek
  44.         fseek(inptr, -512, SEEK_CUR);
  45.  
  46.         // if next block has a new JPEG - create new file
  47.         if (header_is_jpeg(inptr))
  48.         {
  49.             create_new_outfile(&outfile, &outptr, &filename_counter);
  50.         }
  51.  
  52.         // if not possible to write outfile - exit
  53.         if (outptr == NULL)
  54.         {
  55.             free(byte_array);
  56.             free(outfile);
  57.             fprintf(stderr, "Could not write in %s file\n", outfile);
  58.             return 3;
  59.         }
  60.  
  61.         // read and write 512 bytes, 1 byte at a time
  62.         for(int i = 0, arr_size = sizeof(byte_array); i < arr_size; i++)
  63.         {
  64.             fread(&byte_array[i], 1, 1, inptr);
  65.             fwrite(&byte_array[i], 1, 1, outptr);
  66.         }
  67.  
  68.     }
  69.  
  70.     // close files
  71.     free(byte_array);
  72.     free(outfile);
  73.     fclose(inptr);
  74.     fclose(outptr);
  75.     return 0;
  76. }
  77.  
  78. int 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 && BLOCKHEADER[1] == 0xd8 &&
  92.         BLOCKHEADER[2] == 0xff && (BLOCKHEADER[3] & 0xf0) == 0xe0)
  93.     {
  94.         free(BLOCKHEADER);
  95.         return 1;
  96.     }
  97.     else
  98.     {
  99.         free(BLOCKHEADER);
  100.         return 0;
  101.     }
  102. }
  103.  
  104. void create_new_outfile(char **outfile, FILE **outptr, int *filename_counter)
  105. {
  106.     // if a file is open - close the old one before creating new one
  107.     if (*outptr != NULL)
  108.     {
  109.         fclose(*outptr);
  110.         free(*outfile);
  111.     }
  112.  
  113.     // create new output file using syntax "000.jpg"
  114.     *outfile = malloc(7 * sizeof(char) + 1);
  115.     sprintf(*outfile, "%03i.jpg", *filename_counter);
  116.     *outptr = fopen(*outfile, "w");
  117.     (*filename_counter)++;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement