Advertisement
rupek1995

recover.c 512-byte-write

Mar 8th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 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.     // create first outfile
  41.     create_new_outfile(&outfile, &outptr, &filename_counter);
  42.  
  43.     // keep reading memory blocks while they're 512 bytes each
  44.     while (fread(byte_array, 1, 512, inptr) == 512)
  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
  62.         fwrite(byte_array, 1, 512, outptr);
  63.  
  64.     }
  65.  
  66.     // close files
  67.     free(byte_array);
  68.     free(outfile);
  69.     fclose(inptr);
  70.     fclose(outptr);
  71.     return 0;
  72. }
  73.  
  74. int header_is_jpeg(FILE *memcard)
  75. {
  76.     BYTE *BLOCKHEADER = malloc(4 * sizeof(BYTE));
  77.     // read first 4 bytes of current memory block
  78.     for (int i = 0; i < 4; i++)
  79.     {
  80.         fread(&BLOCKHEADER[i], 1, 1, memcard);
  81.     }
  82.  
  83.     // rewind seek back to beginning of block
  84.     fseek(memcard, -4, SEEK_CUR);
  85.  
  86.     // check if header is the same as JPEG header, return true if it is
  87.     if (BLOCKHEADER[0] == 0xff && BLOCKHEADER[1] == 0xd8 &&
  88.         BLOCKHEADER[2] == 0xff && (BLOCKHEADER[3] & 0xf0) == 0xe0)
  89.     {
  90.         free(BLOCKHEADER);
  91.         return 1;
  92.     }
  93.     else
  94.     {
  95.         free(BLOCKHEADER);
  96.         return 0;
  97.     }
  98. }
  99.  
  100. void create_new_outfile(char **outfile, FILE **outptr, int *filename_counter)
  101. {
  102.     // if a file is open - close the old one before creating new one
  103.     if (*outptr != NULL)
  104.     {
  105.         fclose(*outptr);
  106.         free(*outfile);
  107.     }
  108.  
  109.     // create new output file using syntax "000.jpg"
  110.     *outfile = malloc(7 * sizeof(char) + 1);
  111.     sprintf(*outfile, "%03i.jpg", *filename_counter);
  112.     *outptr = fopen(*outfile, "w");
  113.     (*filename_counter)++;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement