-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecover.c
50 lines (44 loc) · 1019 Bytes
/
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
48
49
50
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
FILE *input = fopen(argv[1], "r");
if (input == NULL)
{
printf("Could not open file.\n");
return 1;
}
FILE *img = NULL;
BYTE buffer[512];
char filename[8];
int i = 0;
while (fread(buffer, sizeof(buffer), 1, input))
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 &&
buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
if (i > 0)
{
fclose(img);
}
sprintf(filename, "%03d.jpg", i);
img = fopen(filename, "w");
fwrite(&buffer, sizeof(buffer), 1, img);
i++;
}
else if (img != NULL)
{
fwrite(&buffer, sizeof(buffer), 1, img);
}
}
fclose(input);
fclose(img);
return 0;
}