-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover.c
47 lines (42 loc) · 1.01 KB
/
recover.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
char *infile = argv[1];
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
int is_open = 0;
int counter = 0;
char filename[8];
uint8_t buffer[512];
FILE *img;
while (fread(buffer, 1, 512, inptr) == 512)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
if (is_open)
{
fclose(img);
}
sprintf(filename, "%03i.jpg", counter++);
img = fopen(filename, "w");
fwrite(buffer, 1, 512, img);
is_open = 1;
}
else if (is_open)
{
fwrite(buffer, 1, 512, img);
}
}
fclose(img);
}