Ryba3310

rec

Apr 23rd, 2022 (edited)
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5.  
  6. // Prolly should be in a header file
  7. typedef uint8_t BYTE;
  8. // Size of blocks in FAT
  9. typedef struct
  10. {
  11.     BYTE bytes[512];
  12. } BLOCK_SIZE;
  13.  
  14. bool check_block(BLOCK_SIZE *block);
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     if (argc != 2)
  19.     {
  20.         printf("Invalid argument");
  21.         return 1;
  22.     }
  23.     FILE *file = fopen(argv[1], "r");
  24.     if (file == NULL)
  25.     {
  26.         printf("Cannot open file");
  27.         return 2;
  28.     }
  29.     FILE *out;
  30.     char *fname = malloc(8 * sizeof(char));
  31.  
  32.     bool if_open = false;
  33.     int files = 0;
  34.     BLOCK_SIZE *block = calloc(1, sizeof(BLOCK_SIZE));
  35.     while (fread(block, sizeof(BLOCK_SIZE), 1, file))
  36.     {
  37.         //Prolly do while loop fits better for writing
  38.         if (check_block(block)) // Check first 4 Bs in a block
  39.         {
  40.             if_open = true;
  41.             sprintf(fname, "%.3d.jpg", files);
  42.             out = fopen(fname, "w");
  43.             if (out == NULL)
  44.             {
  45.                 printf("Cannot create file\n");
  46.                 fclose(out);
  47.                 fclose(file);
  48.                 return 3;
  49.             }
  50.             files++;
  51.         }
  52.         if (if_open)
  53.         {
  54.             fwrite(block, sizeof(BLOCK_SIZE), 1, out);
  55.         }
  56.     }
  57.     fclose(file);
  58.     fclose(out);
  59.     free(fname);
  60. }
  61.  
  62. bool check_block(BLOCK_SIZE *block)
  63. {
  64.     /* //Probably better with 4 explicit if statements
  65.     short first_Bs_flags = 0;
  66.     for (int i = 0; i < 4; i++) //Check only for 4 first Bytes of a block
  67.     {
  68.         if (block->bytes[i] == 0xff || block->bytes[i] == 0xd8)
  69.         {
  70.             first_Bs_flags++;
  71.         }
  72.         else if (i == 3)
  73.         {
  74.             for (int j = 0; j < 16; j++)
  75.             {
  76.                 if (block->bytes[i] == 0xe0 + j)
  77.                 {
  78.                     first_Bs_flags++;
  79.                     break;
  80.                 }
  81.             }
  82.         }
  83.     } */
  84.     short first_Bs_flags = 0;
  85.     if (block->bytes[0] == 0xff && block->bytes[1] == 0xd8 && block->bytes[2] == 0xff)
  86.     {
  87.         first_Bs_flags += 3;
  88.     }
  89.     for (int j = 0; j < 16; j++)
  90.     {
  91.         if (block->bytes[3] == 0xe0 + j)
  92.         {
  93.             first_Bs_flags++;
  94.             break;
  95.         }
  96.     }
  97.     return first_Bs_flags == 4 ? true : false;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment