-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecover.c
77 lines (64 loc) · 2.14 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
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
// number of block to read at a time
#define BLOCK_SIZE 512
bool isJPG(uint8_t buffer[]);
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s image\n", argv[0]);
return 1;
}
// remember the input file
char *infile = argv[1];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
uint8_t buffer[512]; // create a buffer of 512 bytes to read in sectors
int fileNumber = 0;
char fileName[8]; // array to hold chars for the file name XXX.jpg
FILE *outptr = NULL; // create out file pointer and set to NULL
int ret = 0; // to hold fread() return value
while ((fread(&buffer, BLOCK_SIZE, 1, inptr)) == 1)
{
if (isJPG(buffer))
{
sprintf(fileName, "%03i.jpg", fileNumber); // create the file name to write to
outptr = fopen(fileName, "w"); // open pointer to output file
// if unable to open print error and exit program
if (outptr == NULL)
{
fprintf(stderr, "Could not open %s for writing.\n", fileName);
return 2;
}
do // need to do this at least one time
{
fwrite(&buffer, BLOCK_SIZE, 1, outptr); // write the buffer to the out file
ret = fread(&buffer, BLOCK_SIZE, 1, inptr); // read the next block of 512 bytes
}
while (!isJPG(buffer) && ret != 0); // while we're not at the next jpg file
fseek(inptr, -(BLOCK_SIZE), SEEK_CUR); // go back one sector
fileNumber++; // update file number
fclose(outptr); // close the output file
}
}
fclose(inptr); // at this point done with input file, so close it
}
// check if we are at the start of a jpg file or not
bool isJPG(uint8_t buffer[])
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
return true;
}
else
{
return false;
}
}