Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <cs50.h>
- bool check(FILE *fp);
- typedef uint8_t BYTE;
- int main(int argc, char *argv[])
- {
- int w = 000;
- if (argc == 2)
- {
- FILE *ptr = fopen(argv[1], "r");
- while(ftell(ptr) != EOF)
- {
- while(check(ptr) == 0)
- {
- char name[64];
- sprintf(name, "%i.jpeg", w);
- FILE *img = fopen(name, "w");
- while(check(ptr) == 0)
- {
- BYTE B[512];
- fread(B, 512, 1, ptr);
- fwrite(B, 512, 1, img);
- }
- w++;
- fclose(img);
- }
- }
- fclose(ptr);
- }
- else
- {
- printf("Usage: ./recover image\n");
- return 1;
- }
- }
- bool check(FILE *fp)
- {
- BYTE b[4];
- fread (&b, 4, 1, fp);
- bool jpg = b[0] == 0xff && b[1] == 0xd8 && b[2] == 0xff && (b[3] == 0xe0 || b[3] == 0xe1 || b[3] == 0xe2 || b[3] == 0xe3 || b[3] == 0xe4 || b[3] == 0xe5 || b[3] == 0xe6 || b[3] == 0xe7 || b[3] == 0xe8 || b[3] == 0xe9 || b[3] == 0xea || b[3] == 0xeb || b[3] == 0xec || b[3] == 0xed || b[3] == 0xee || b[3] == 0xef);
- if (jpg == 0)
- {
- fseek(fp, -4L, SEEK_CUR);
- return 0;
- }
- else
- {
- fseek(fp, 508L, SEEK_CUR);
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement