-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecover.c
executable file
·84 lines (69 loc) · 2.13 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
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
BYTE buffer[512];
int file_counter = 0;
char *filename;
FILE *jpeg;
int main(int argc, char *argv[])
{
//there should be only one input file as a command-line argument
if (argc != 2)
{
printf("Usage: ./recover IMAGE\n");
return 1;
}
//open the memory card inputted
FILE *img = fopen(argv[1], "r");
if (img == NULL)
{
printf("Could not open the file\n");
return 1;
}
//read very 512 bytes into buffer
while ((fread(buffer, sizeof(BYTE), 512, img)) == 512)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3] & 0xf0) == 0xe0))
{
//check if this is the first image recovered
if (file_counter == 0)
{
filename = malloc(sizeof(BYTE) * 9);
sprintf(filename, "%03i.jpg", file_counter);
jpeg = fopen(filename, "w");
if (jpeg == NULL)
{
fclose(jpeg);
printf("Could not create image file");
return 1;
}
fwrite(buffer, sizeof(BYTE), 512, jpeg);
file_counter++;
}
//if this is not the first image recovered do the previous step but close the existing file first
else
{
fclose(jpeg);
sprintf(filename, "%03i.jpg", file_counter);
jpeg = fopen(filename, "w");
if (jpeg == NULL)
{
fclose(jpeg);
printf("Could not create image file");
return 1;
}
fwrite(buffer, sizeof(BYTE), 512, jpeg);
file_counter++;
}
}
//if there is a already opened image file keep and not start of a new image, keep writing to existing one
else if (file_counter != 0)
{
fwrite(buffer, sizeof(BYTE), 512, jpeg);
}
}
free(filename);
fclose(jpeg);
fclose(img);
}