Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- typedef uint8_t BYTE;
- int main(int argc, char *argv[])
- {
- FILE *mem = NULL;
- FILE *file;
- if (argc != 2)
- {
- printf("Usage: ./recover image\n");
- return 1;
- }
- file = fopen(argv[1] , "r");
- if (!file)
- {
- return 1;
- }
- int count = 0;
- char img[8];
- int i = 1;
- BYTE bytes[512];
- // This reads the file front to back
- while (fread(bytes, 512, 1, file))
- {
- // we check for a Jpeg
- if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (bytes[3] & 0xf0) == 0xe0)
- {
- // we check what number has the jpeg file
- if(count == 0)
- {
- // we print in a string
- sprintf(img, "%03i.jpg", count);
- // create a file
- mem = fopen(img , "w");
- //we write the bytes from bytes to the actual file
- fwrite(bytes, 512, 1, mem);
- //we start counting the jpg files
- count++;
- }
- // If this is another jpeg file
- else
- {
- fclose(mem);
- sprintf(img, "%03i.jpg", count);
- mem = fopen(img , "w");
- fwrite(bytes, 512, 1, mem);
- count++;
- }
- i = 1;
- }
- else
- {
- // if we already found a jpeg we continue to write to it.
- if(mem != NULL)
- {
- fwrite(bytes, 512, 1, mem);
- }
- }
- }
- fclose(mem);
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement