Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[])
- {
- // check usage
- if (argc != 2)
- {
- printf("Usage: ./recover image\n");
- return 1;
- }
- char *card = argv[1];
- // open file
- FILE *file = fopen(card, "r");
- // if file isnt a file tell the user and inform them of such return 1
- if (!file)
- {
- printf("couldn't open that file dawg, like give me a valid chunk of memory to use my dude\n");
- return 1;
- }
- fseek(file, 0, SEEK_END);
- long count = ftell(file);
- fseek(file, 0, SEEK_SET);
- int *bytes = malloc(512 * sizeof(int));
- fread(bytes, 1, 512, file);
- int jpg = 0;
- int i = 0;
- while (i < count)
- {
- if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && bytes[3] >= 0xe0 && bytes[3] <= 0xef)
- {
- char *name = malloc(sizeof(8));
- jpg++;
- sprintf(name, "%03i.jpg", jpg);
- FILE *jpeg = fopen(name, "w");
- fwrite(bytes, 512, 1, jpeg);
- fseek(file, 0, SEEK_SET);
- fseek(file, 512, SEEK_CUR);
- fclose(jpeg);
- free(name);
- }
- else
- {
- fseek(file, 512, SEEK_CUR);
- }
- i++;
- }
- free(bytes);
- fclose(file);
- free(card);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement