Created
May 14, 2020 11:17
-
-
Save wx5162839/3eeda9238649a7cee51100bbfaa161b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdint.h> | |
| typedef uint8_t BYTE; | |
| int main(int argc, char *argv[]) | |
| { | |
| if (argc != 2) | |
| { | |
| printf("Usage: ./recover image"); | |
| return 1; | |
| } | |
| // Original file | |
| FILE *file = fopen(argv[1], "r"); | |
| if (file == NULL) | |
| { | |
| printf("Usage: ./recover image"); | |
| return 1; | |
| } | |
| // file will be written into | |
| FILE *img = NULL; | |
| char filename[8]; | |
| int counter = 0; | |
| int i = 0; | |
| BYTE buffer[512]; | |
| while(fread(buffer, 512, 1, file) == 1) | |
| { | |
| if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0) | |
| { | |
| i = 1; | |
| if (counter == 0) | |
| { | |
| sprintf(filename, "%03i.jpg", counter); | |
| img = fopen(filename, "w"); | |
| fwrite(buffer, 512, 1, img); | |
| counter++; | |
| } | |
| else | |
| { | |
| fclose(img); | |
| sprintf(filename, "%03i.jpg", counter); | |
| img = fopen(filename, "w"); | |
| fwrite(buffer, 512, 1, img); | |
| counter++; | |
| } | |
| } | |
| else | |
| { | |
| if (i == 0) | |
| { | |
| continue; | |
| } | |
| else | |
| { | |
| fwrite(buffer, 512, 1, img); | |
| } | |
| } | |
| } | |
| fclose(img); | |
| fclose(file); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment