-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecover.c
68 lines (59 loc) · 1.51 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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void openNewJpgForWriting(FILE **outptr, int fileCount)
{
fclose(*outptr);
char fileName[8];
sprintf(fileName, "%03i.jpg", fileCount);
*outptr = fopen(fileName, "w");
}
bool blockStartsWithJpgHeader(unsigned char buffer[512])
{
unsigned int signature = 0;
signature |= (unsigned int) buffer[0] << 24;
signature |= (unsigned int) buffer[1] << 16;
signature |= (unsigned int) buffer[2] << 8;
signature |= (unsigned int) buffer[3];
return (signature >= 4292411360 && signature <= 4292411375);
}
void recoverJpgs(FILE **inptr)
{
unsigned char buffer[512];
int fileCount = 0;
FILE *outptr = fopen("temp", "w");
// read through the file 1 block (512 bytes) at a time
while (fread(buffer, sizeof(buffer), 1, *inptr) == 1)
{
if (blockStartsWithJpgHeader(buffer))
{
openNewJpgForWriting(&outptr, fileCount);
fileCount++;
}
fwrite(buffer, sizeof(buffer), 1, outptr);
}
// clean up
remove("temp");
fclose(outptr);
}
int main(int argc, char *argv[])
{
if (argc == 2)
{
char *infile = argv[1];
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
recoverJpgs(&inptr);
fclose(inptr);
return 0;
}
else
{
fprintf(stderr, "Usage: recover filename\n");
return 1;
}
}