forked from RaduStoian/Harvard-CS50-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
(week3) - recover.c
96 lines (70 loc) · 1.93 KB
/
(week3) - 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
88
89
90
91
92
93
94
95
96
// Recover 50 deleted jpgs from an image of a memory card named card.raw
//
// Program accepts 1 argument - the input file
//
// Searches through card.raw in blocks of 512 bytes.
//
// Looks for the 4 bytes signifying the start of a jpg
//
// If found, it opens a new jpg file and writes all next bytes to that file until it finds another start of jpg.
// Then it closes the previous jpg file and opens another.
//
// Process continues until the end of the memory card, when it cannot find another full block of 512 bytes.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *infile = argv[1];
// Accept only 1 arguments
if (argc != 2)
{
fprintf(stderr, "usage: ./recover file\n");
return 1;
}
// Make sure you can open file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
// 1 Block of 512 bytes
unsigned char buffer[512];
// file counter
int jpg = 0;
int count = 0;
FILE *picture = NULL;
// Reading from Card till the end
while (fread(buffer, 512, 1, inptr) == 1)
{
// look for first 4 bytes
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xe0) == 0xe0)
{
// to close picture
if (jpg == 1)
{
fclose(picture);
}
else
{
jpg = 1;
}
// open picture
if (jpg == 1)
{
char filename[8];
sprintf(filename, "%03i.jpg", count);
picture = fopen(filename, "a");
count++;
}
}
if (jpg == 1)
{
// write to picture
fwrite(&buffer, 512, 1, picture);
}
}
fclose(inptr);
fclose(picture);
return 0;
}