-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrecover.c
73 lines (62 loc) · 1.54 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define BLOCK_SIZE 512
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
printf("Unable to open file %s\n", argv[1]);
return 1;
}
int c = 0;
FILE *image = NULL;
while (1)
{
BYTE *retrieved = malloc(BLOCK_SIZE);
if (retrieved == NULL)
{
printf("Error allocating memory.\n");
return 1;
}
int read = fread(retrieved, sizeof(BYTE), BLOCK_SIZE, file);
if (read != BLOCK_SIZE)
{
break;
}
if (retrieved[0] == 0xff && retrieved[1] == 0xd8 && retrieved[2] == 0xff && (retrieved[3] & 0xf0) == 0xe0)
{
if (image != NULL)
{
fclose(image);
}
char *filename = malloc(sizeof(char) * 8);
if (filename == NULL)
{
printf("Error\n");
return 1;
}
sprintf(filename, "%03d.jpg", c++);
image = fopen(filename, "w");
if (image == NULL)
{
printf("Error opening new file.\n");
return 1;
}
free(filename);
}
if (image != NULL)
{
fwrite(retrieved, sizeof(BYTE), read, image);
}
free(retrieved);
}
return 0;
}