-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecover.c
60 lines (53 loc) · 1.5 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
#include <stdio.h>
#include <stdlib.h>
//setting arguments
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: ./recover image");
return 1;
}
//Using file pointers to make the call
FILE *card = fopen(argv[1], "r");
unsigned char *buffer = malloc(512);
if (buffer == NULL)
{
return 1;
}
//Allocating memory
char *filename = malloc(3 * sizeof(int));
int photoCount = 0;
//reading the raw file
while (fread(buffer, sizeof(unsigned char), 512, card) == 512)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
if (photoCount == 1)
{
sprintf(filename, "%03i.jpg", photoCount);
FILE *imageFile = fopen(filename, "w");
fwrite(buffer, 1, 512, imageFile);
fclose(imageFile);
}
else
{
sprintf(filename, "%03i.jpg", photoCount);
FILE *imageFile = fopen(filename, "w");
fwrite(buffer, 1, 512, imageFile);
fclose(imageFile);
}
photoCount++;
}
//recovering the photos
else if (photoCount != 0)
{
FILE *imageFile = fopen(filename, "a");
fwrite(buffer, 1, 512, imageFile);
fclose(imageFile);
}
}
//Freeing the allocated memory
free(buffer);
printf("contagem = %i\n", photoCount);
}