Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <stdint.h>
- // Prolly should be in a header file
- typedef uint8_t BYTE;
- // Size of blocks in FAT
- typedef struct
- {
- BYTE bytes[512];
- } BLOCK_SIZE;
- bool check_block(BLOCK_SIZE *block);
- int main(int argc, char *argv[])
- {
- if (argc != 2)
- {
- printf("Invalid argument");
- return 1;
- }
- FILE *file = fopen(argv[1], "r");
- if (file == NULL)
- {
- printf("Cannot open file");
- return 2;
- }
- FILE *out;
- char *fname = malloc(8 * sizeof(char));
- bool if_open = false;
- int files = 0;
- BLOCK_SIZE *block = calloc(1, sizeof(BLOCK_SIZE));
- while (fread(block, sizeof(BLOCK_SIZE), 1, file))
- {
- //Prolly do while loop fits better for writing
- if (check_block(block)) // Check first 4 Bs in a block
- {
- if_open = true;
- sprintf(fname, "%.3d.jpg", files);
- out = fopen(fname, "w");
- if (out == NULL)
- {
- printf("Cannot create file\n");
- fclose(out);
- fclose(file);
- return 3;
- }
- files++;
- }
- if (if_open)
- {
- fwrite(block, sizeof(BLOCK_SIZE), 1, out);
- }
- }
- fclose(file);
- fclose(out);
- free(fname);
- }
- bool check_block(BLOCK_SIZE *block)
- {
- /* //Probably better with 4 explicit if statements
- short first_Bs_flags = 0;
- for (int i = 0; i < 4; i++) //Check only for 4 first Bytes of a block
- {
- if (block->bytes[i] == 0xff || block->bytes[i] == 0xd8)
- {
- first_Bs_flags++;
- }
- else if (i == 3)
- {
- for (int j = 0; j < 16; j++)
- {
- if (block->bytes[i] == 0xe0 + j)
- {
- first_Bs_flags++;
- break;
- }
- }
- }
- } */
- short first_Bs_flags = 0;
- if (block->bytes[0] == 0xff && block->bytes[1] == 0xd8 && block->bytes[2] == 0xff)
- {
- first_Bs_flags += 3;
- }
- for (int j = 0; j < 16; j++)
- {
- if (block->bytes[3] == 0xe0 + j)
- {
- first_Bs_flags++;
- break;
- }
- }
- return first_Bs_flags == 4 ? true : false;
- }
Advertisement
Add Comment
Please, Sign In to add comment