-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecover.c
51 lines (46 loc) · 1.15 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
typedef uint8_t BYTE;
const int BLOCK_SIZE = 512;
BYTE block[BLOCK_SIZE];
char filename[8];
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("ERROR: Must include a memory card image to scan.\n");
return 1;
}
FILE *input = fopen(argv[1], "r");
if (input == NULL) {
printf("ERROR: File cannot be opened.\n");
return 2;
}
int fnum = 0;
FILE *img = NULL;
while (fread(block, sizeof(BYTE), BLOCK_SIZE, input) == 512) {
if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
{
if (fnum == 0) {
sprintf(filename, "%03i.jpg", fnum);
img = fopen(filename, "w");
fwrite(block, sizeof(BYTE), BLOCK_SIZE, img);
fnum++;
}
else {
fclose(img);
sprintf(filename, "%03i.jpg", fnum);
img = fopen(filename, "w");
fwrite(block, sizeof(BYTE), BLOCK_SIZE, img);
fnum++;
}
}
else if (fnum > 0) {
fwrite(block, sizeof(BYTE), BLOCK_SIZE, img);
}
}
fclose(img);
fclose(input);
return 0;
}