-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecover.c
62 lines (46 loc) · 1.27 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
#include <stdio.h>
#include <stdint.h>
#include <cs50.h>
const int BLOCK_SIZE = 512;
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage:");
return 1;
}
string fileName = argv[1];
FILE *file;
file = fopen(fileName, "r");
if (file == NULL)
{
fprintf(stderr, "Cannot open the file.\n");
return 1;
}
uint8_t buf[512];
int counter = 0;
FILE *file_w = NULL;
// Loop through the file
while (fread(buf, BLOCK_SIZE, 1, file))
{
// Check if the first four bytes are JPEG signature
if (buf[0] == 0xff && buf[1] == 0xd8 && buf[2] == 0xff
&& (buf[3] == 0xe0 || buf[3] == 0xe1))
{
// Close the file if it is opened
if (file_w != NULL)
fclose(file_w);
char filename[3];
sprintf(filename, "%03d.jpg", counter);
// Open a new JPEG file for writing
file_w = fopen(filename, "w");
counter++;
}
if (file_w != NULL)
fwrite(buf, BLOCK_SIZE, 1, file_w);
}
if (file_w != NULL)
fclose(file_w);
fclose(file);
return 0;
}