-
Notifications
You must be signed in to change notification settings - Fork 1
/
recover.c
58 lines (51 loc) · 1.23 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
//right number of arguments
if (argc != 2)
{
printf("wrong usage, usage: ./recover <file>");
return 1;
}
//Open the file input
FILE *infile = fopen(argv[1], "r");
if (infile == NULL)
{
printf("Could not open input\n");
return 1;
}
//looking beging of JPEG bytes are 0xff 0xd8 0xff 0xe*
BYTE bytes[512];
int count = 0;
char *name = malloc(8 * sizeof(char));
FILE *output = NULL;
while(fread(&bytes, 1, 512, infile))
{
if(bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (bytes[3] & 0xf0) == 0xe0)
{
if (count != 0)
{
fclose(output);
}
sprintf(name, "%03i.jpg", count);
output = fopen(name, "w");
count++;
if (output == NULL)
{
printf("Could not open output\n");
return 1;
}
}
if (output !=NULL)
{
fwrite(&bytes, 1, 512, output);
}
}
free(name);
fclose(output);
fclose(infile);
return 0;
}