-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecover.c
More file actions
56 lines (48 loc) · 1.83 KB
/
Copy pathrecover.c
File metadata and controls
56 lines (48 loc) · 1.83 KB
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
#include <stdio.h>
#include <stdint.h>
const int BLOCK_SIZE = 512;
int main(int argc, char* argv[])
{
if (argc != 2)
{
fprintf(stderr, "Should give and only give one arg.\n");
return 1;
}
FILE* f; //create a pointer to file
f = fopen(argv[1], "r"); //initiate the pointer to point at inputfile
if (f == NULL) //Check if the pointer is null
{
fprintf (stderr, "Error opening the file.");
return 2;
}
FILE* fw; //create a pointer to another file;
fw = NULL; //initialize the pointer to Null
int count = 0; // count the JPEGS for the naming use. Initialize to 0
uint8_t buffer[512]; //create a buffer containing 512 unit8_t type data
while(fread(buffer, BLOCK_SIZE, 1, f)) // read from f to buffer, return 1 if block_size is 512
{
if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) // check if the first four bytes is JPEG signature
&& (buffer[2] == 0xff) &&
((buffer[3] & 0xf0) == 0xe0))
{
if (fw != NULL) // close the file if it is opened
{
fclose(fw);
}
char filename[8];
sprintf(filename, "%03d.jpg", count);
fw = fopen(filename, "w");
count ++;
}
if (fw != NULL)
{
fwrite(buffer, BLOCK_SIZE, 1, fw);
}
}
if (fw != NULL)
{
fclose(fw);
}
fclose(f);
return 0;
}