-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover.c
87 lines (69 loc) · 2.21 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
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
typedef uint8_t BYTE;
#define BLOCK_SIZE 512
#define MAX_LENGTH 100
int main(int argc, char *argv[])
{
// Open the memory card
FILE *input = fopen(argv[1], "r");
// Check command-line arguments
if (argc != 2)
{
printf("Usage: ./recover IMAGE\n");
return 1;
}
if (input != NULL)
{
int count_img = 0;
BYTE buffer[BLOCK_SIZE];
char output[MAX_LENGTH];
FILE *img;
bool found = false;
int offset = 0x00;
// fread returns 0 (which is effectively false), loop will end
while ( fread(&buffer, sizeof(BYTE), BLOCK_SIZE, input) )
{
// keep track of next offset in the memory card
offset += 0x200;
bool condition = (buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && ((buffer[3] & 0xf0) == 0xe0);
if (condition)
{
// first jpeg
if (count_img == 0)
{
sprintf(output, "%03i.jpg", count_img);
img = fopen(output, "w");
fwrite(&buffer, sizeof(BYTE), BLOCK_SIZE, img);
// found the jpegs
found = true;
count_img++;
}
//next jpegs
else
{
// close previous jpeg
fclose(img);
// write to the next jpeg
sprintf(output, "%03i.jpg", count_img);
img = fopen(output, "w");
fwrite(&buffer, sizeof(BYTE), BLOCK_SIZE, img);
count_img++;
}
}
// countinue writing the remaining blocks of a jpeg
else
{
if (count_img >= 0 && found)
fwrite(&buffer, sizeof(BYTE), BLOCK_SIZE, img);
// from the beginning of the memory card, no jpeg detected yet, then countinue detecting/reading
else
continue;
}
}
fclose(input);
}
return EXIT_SUCCESS;
}