Advertisement
rupek1995

Untitled

Mar 6th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. int main(int argc, char *argv[])
  2. {
  3.     // keep filenames
  4.     char    *infile = argv[1],
  5.             *outfile = NULL;
  6.     FILE    *inptr = fopen(infile, "r"),
  7.             *outptr = NULL;
  8.     BYTE    *byte_array = malloc(512 * sizeof(BYTE));
  9.     int     filename_counter = 0;
  10.  
  11.  
  12.     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  13.     // program works only with initialized outfile - remove this part and it crashes on sprintf
  14.     outfile = malloc(sizeof("000.jpg") + 1);
  15.     sprintf(outfile, "%03i.jpg", filename_counter);
  16.     outptr = fopen(outfile, "w");
  17.     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  18.  
  19.  
  20.     /**
  21.     * CODE
  22.     * CODE
  23.     * CODE
  24.     **/
  25.  
  26.     if (header_is_jpeg(inptr))
  27.     {
  28.         create_new_outfile(&outfile, &outptr, &filename_counter);
  29.     }
  30.  
  31.     // only keep reading if block is proper size (512)
  32.     while (fread(outptr, 1, 512, inptr) == 512)
  33.     {
  34.         // rewind seek
  35.         fseek(inptr, -512, SEEK_CUR);
  36.  
  37.         // read and write 512 bytes, 1 byte at a time
  38.         for(int i = 0, arr_size = sizeof(byte_array); i < arr_size; i++)
  39.         {
  40.             fread(&byte_array[i], 1, 1, inptr);
  41.                 /**
  42.                 * !!!!!!!!!!!!!!!!!!!!!!!
  43.                 * CODE CRASHES HERE
  44.                 * !!!!!!!!!!!!!!!!!!!!!!!
  45.                 **/
  46.            
  47.             fwrite(&byte_array[i], 1, 1, outptr);
  48.         }
  49.        
  50.     }
  51.  
  52.     // close files
  53.     fclose(inptr);
  54.     fclose(outptr);
  55.     return 0;
  56. }
  57.  
  58.  
  59. int header_is_jpeg(FILE *memcard)
  60. {
  61.     /**
  62.     * CODE
  63.     * CODE
  64.     * CODE
  65.     **/
  66. }
  67.  
  68. void create_new_outfile(char **outfile, FILE **outptr, int *filename_counter)
  69. {
  70.     // if a file is open - close the old one before creating new one
  71.     if (*outptr != NULL)
  72.     {
  73.         printf("Recovered image: %s\n", *outfile);
  74.         fclose(*outptr);
  75.         free(*outfile);
  76.     }
  77.  
  78.     // create new output file
  79.     *outfile = malloc(sizeof("000.jpg") + 1);
  80.     sprintf(*outfile, "%03i.jpg", *filename_counter+3);
  81.     *outptr = fopen(*outfile, "w");
  82.     (*filename_counter)++;
  83.    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement