-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJpegCarver.c
122 lines (99 loc) · 3.47 KB
/
JpegCarver.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void checkHostName(int hostname)
{
if (hostname == -1)
{
perror("gethostname");
exit(1);
}
}
int main (int argc, char **argv)
{
//Declarations
FILE *file = fopen( argv[1], "rb");
unsigned char buffer;
long position;
long possiblefooters[1000];
long verifiedheaders[1000];
long verifiedfooters[1000];
long subverfooters[1000];
long finalposition;
int i = 0, j = 0, k;
int switcher;
//Finding out Headers and Footers.
do {
fread(&buffer, 1, 1, file);
if (buffer == 0xFF && i != 1000){
fread(&buffer,1,1,file);
if (buffer == 0xD8){
fread(&buffer, 1, 1, file);
if (buffer == 0xFF){
fread(&buffer, 1, 1, file);
if (buffer == 0xE0){
fread(&buffer, 1, 1, file);
if (buffer == 0x00){
fread(&buffer, 1, 1, file);
if (buffer == 0x10){
finalposition = ftell(file);
printf("HEADER%d found at Position; %ld\n", i+1, finalposition-6);
verifiedheaders[i] = finalposition-6;
switcher = 1;
i++;
}
}
}
}
}
// Below statement contains a switcher variable to filter out unwanted footers, since footers that come immediately after a verified header is considered valid, switcher is implemented
if (buffer == 0xD9 && switcher){
printf("FOOTER%d found at Position: %ld\n", j+1, ftell(file)-2);
verifiedfooters[j] = ftell(file)-2;
j++;
switcher=0;
}
};
}while (!feof(file));
fclose(file); //Closing the file.
//Listing Start and End of Possible Jpeg Files.
for (k=0; k<i; k++){
printf("FILE%d : %ld to %ld\n", k, verifiedheaders[k], verifiedfooters[k]);
}
//CARVING OUT FILES
// Declaration For hostname
char hostbuffer[256];
int hostname;
// Retrieve Hostname
hostname = gethostname(hostbuffer, sizeof(hostbuffer));
checkHostName(hostname);
//Declarations for Extraction Part
FILE *fileread = fopen(argv[1], "r"); //reopening the same file in simple read mode.
FILE *filewrite;
long pos, end;
unsigned char wbuffer;
int act, m;
char filename_format[] = "%s_%d.jpg";
char filename[sizeof(filename_format) + 3]; // for up to 4 digit numbers
for (m = 0; m < 2; m++) {
act = 1;
pos = verifiedheaders[m];
end = verifiedfooters[m];
snprintf(filename, sizeof(filename), filename_format, hostbuffer,m); //for creating filename.
filewrite = fopen(filename, "wb");
rewind(fileread);
fseek(fileread, pos, SEEK_SET);
while (!feof(fileread) && act){
fread(&wbuffer, 1, 1, fileread);
fwrite(&wbuffer, 1, 1, filewrite);
if(ftell(fileread)==end+1){
act = 0;
}
}
printf("%s successfully extracted\n", filename);
fclose(filewrite);
}
printf("Extracted the files with Log-in-Name %s on host %s \n", getlogin(), hostbuffer);
return 0;
}