-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.c
More file actions
443 lines (423 loc) · 11.7 KB
/
extract.c
File metadata and controls
443 lines (423 loc) · 11.7 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
uint8_t PNG [8]= { 137, 80, 78, 71, 13, 10, 26, 10};
unsigned char *IEND = "IEND";
char *STEGSIG = "o90xpx95";
int numOfFiles;
uint8_t f = 0; //0 for files 1 for folder
//read signature
char *slashPath(char *name){//culls string at the first /
char *folderName;
uint8_t slash = 0;
int len = strlen(name);
for (size_t i = 0; i < len; i++)
{
if (name[i] == '/') slash = i+1;
}
folderName = (char*)malloc((len-slash)+1);
int t = 0;
for (size_t i = slash; i < len; i++)
{
folderName[t] = name[i];
t++;
}
folderName[t] = '\0';
return folderName;
}
void readSig(FILE *file){//this function verifies png file
if(file == NULL){
fprintf(stderr,"ERROR: Could not open File!");
exit(1);
}
unsigned char *buffer = (unsigned char*)malloc(8);//8 bytes is the size of the png signatuer
int bytesRead = fread(buffer,1,8,file);
if(bytesRead==0){//could not read file
fprintf(stderr,"ERROR: Could not read PNG header!");
exit(1);
}
int status = memcmp(PNG,buffer,8);
if (status!=0)
{
fprintf(stderr,"ERROR: File is not a PNG file!");
exit(1);
}
free(buffer);
}
int checkIfFolderExists(char *path){
int result = 0;
struct stat statbuf;
if(stat(path, &statbuf) != 0){
result = 0;
}else{
result = 1;
}
return result;
}
int readChunk(FILE *file,int offset,char *data){//returns location of STEGSIG signature given offset
//get size
uint8_t isValidSteg = 0;
int location = -1;
fseek(file,0,SEEK_END);
int size=ftell(file);
fseek(file,offset,SEEK_SET);
int size2=size;
size=size-offset;
unsigned char *temp = (unsigned char*)malloc(8);//stores STEGSIG
for (int i = offset; i < size2; i++)
{
if(i<=(size2-8)){
for (int j = 0; j < 8; j++)
{
temp[j]=data[i+j];
}
}
//check if file has STEGSIG
if(memcmp(temp,STEGSIG,8)==0){
isValidSteg = 1;
location = i;
return location;
}
}
free(temp);
return location;
}
int checkIfFileExists(char *fileName){
FILE* file;
if (file=fopen(fileName,"r"))
{
fclose(file);
return 1;
}
return 0;
}
void السلام(char *fileName,char *data,long long int size){//receives file name and data and creates a file with the filename and writes the data to it
//first check if file exists
char yesorno;
printf("INFO: EXTRACTING %s\n",fileName);
if(checkIfFileExists(fileName)==1){
printf("The File %s Already Exists, Do you want to overwrite(y/n)\n",fileName);
scanf(" %c", &yesorno);
if(yesorno == 'n' || yesorno == 'N'){
return;//exit without writing anything
}
}
FILE *file = fopen(fileName,"wb");
if(file == NULL){
fprintf(stderr,"ERROR: Failed to create File For writing, %s",fileName);
exit(1);
}
fwrite(data,1,size,file);
fclose(file);
}
int readData(FILE* pngFile,long long int location,long long int location2){
//reads the data between the given offsets
location+=8;//exclude STEGSIG
fseek(pngFile,location,SEEK_SET);
location2 = location2 - 4;//skip ENDSIG
//
int fileNameSize=0;
fread(&fileNameSize,4,1,pngFile);
location+=4;
if (fileNameSize <= 0) {
fprintf(stderr, "ERROR: UNEXPECTED SIZE: %d\n", fileNameSize);
return -1;
}
//
char *fileName = (char*)malloc(fileNameSize+1);
long long int tf = fread(fileName,1,fileNameSize,pngFile);
fileName[fileNameSize] = '\0';
location+=fileNameSize;
if(f == 0){
fileName = slashPath(fileName);
}
if (fileName == NULL) {
perror("malloc failed for fileName");
return -1;
}
if (tf!=fileNameSize)
{//failed STEGSIG may indicate false signature
printf("Error: UNEXPECTED SIZE!\n");
free(fileName);
return -1;
}
//extract foldername
char *folderName;
int slash = 0;
for (size_t i = 0; i < fileNameSize; i++)
{
if(fileName[i] == '/')slash = i+1;//the last slash representing the complete folder structure
}
folderName = (char*)malloc(slash+2);
snprintf(folderName,slash+1,"%s",fileName);
//
for (size_t i = 0; i < strlen(folderName); i++)
{
if(folderName[i] == '/'){
char *temp = (char*)malloc(i+3);
snprintf(temp,i+1,"%s",folderName);
if( checkIfFolderExists(temp) == 0){
mkdir(temp,0777);
}else{
}
free(temp);
}
}
//if(checkIfFolderExists(folderName) == 0)mkdir(folderName,0777);
free(folderName);
//
long long int sizeOfData = location2 - location;
char *data = (char*)malloc(sizeOfData);
long long int read = fread(data,1,sizeOfData,pngFile);
if(read!=sizeOfData){//failed STEGSIG may indicate false signature
printf("ERROR: UNEXPECTED SIZE!\n");
free(data);
free(fileName);
return -1;
}
السلام(fileName,data,sizeOfData);
free(fileName);
free(data);
return 0;
}
int writeData(FILE* pngFile){
//read data to be written
//get size
fseek(pngFile,0,SEEK_END);
long long int size = ftell(pngFile);//go to end
fseek(pngFile,0,SEEK_SET);//return to start
char *buffer = (char*)malloc(size);
long long int dataRead = fread(buffer,1,size,pngFile);
if(dataRead!=size){
fprintf(stderr,"ERROR: Data Read is not Equal to file size!");
exit(1);
}
long long int location = readChunk(pngFile,0,buffer); //will give us locations of STEGSIG
//start reading from there
long long int location2 ;
unsigned char *temp = (unsigned char*)malloc(8);
long long int start = location;
char preventFalseFlag[4];
for (long long int i = start+1; i < size; i++)
{
if(i<=(size-8)){
for (int j = 0; j < 8; j++)
{
temp[j]=buffer[i+j];
}
}
if (memcmp(temp,STEGSIG,8) == 0)
{
//note the location
location2 = location; //range from location2 to location
location = i;
//signature verification --prevent false flags
char IEND[4] = {0xE7,0xF2,0x6E,0x96};
for (size_t j = 0; j < 4; j++)
{
preventFalseFlag[j] = buffer[i-(4-j)];
}
if(memcmp(IEND,preventFalseFlag,4) == 0){//verified
if(readData(pngFile,location2,location) == 0)numOfFiles++;
}else{
continue;//skip the affected file
}
}
}
if(location != -1){
readData(pngFile,location,size);
numOfFiles++;
}
free(temp);
free(buffer);
}
int readNumOfFiles(FILE* pngFile){
int result = 0;
int location;
//get size of pngFile
fseek(pngFile,0,SEEK_END);
int size=ftell(pngFile);
fseek(pngFile,0,SEEK_SET);
//read data from file in blocks for efficient memory usage ->TODO
unsigned char *buffer =(unsigned char*)malloc(size);//not good for large file sizes
int status = fread(buffer,1,size,pngFile);
if(status!=size){
printf("ERROR: UNEXPECTED SIZE\n");
exit(1);
}
//loop through buffer to check for STEGSIG signature
unsigned char *temp = (unsigned char*)malloc(8);;
for (int i = 0; i < size; i++)
{
if(i<=(size-8)){
for (int j = 0; j < 8; j++)
{
int t=i+j;
temp[j]=buffer[i+j];
//printf("%x\n",temp);
}
}
//printf("%s\n",temp);
if (memcmp(temp,STEGSIG,8) == 0)
{
//note the location
location = i;
result +=1 ;
}
}
free(temp);
return result;
}
int checkIfFileOrFolder(char *name){
struct stat fileStat;
if(stat(name,&fileStat)<0){
return -1;
}
if(S_ISDIR(fileStat.st_mode)){
return 1;//1 folder 0 for file
}else{
return 0;
}
}
char *getPath(char *name){
char *result;
int length = strlen(name);
int location = 1;
int previousLocation = 0;
result = (char*)malloc(length);
for (size_t i = 0; i < length; i++)
{
if(name[i] == '/')location = i;
}
result = (char*) malloc(location);
for (size_t i = 0; i < location; i++)
{
result[i] = name[i];
}
return result;
}
int getIEND(FILE *file){//returns location of IEND signature
//get size
int location;
fseek(file,0,SEEK_END);
long int size=ftell(file);
fseek(file,0,SEEK_SET);
unsigned char *buffer =(unsigned char*)malloc(size);
long int status = fread(buffer,1,size,file);
if(status!=size){
printf("ERROR: UNEXPECTED NUMBER OF BYTES\n",status,size);
exit(1);
}
//loop through buffer to check for IEND signature
unsigned char *temp = (unsigned char*)malloc(4);;
for (int i = 0; i < size; i++)
{
if(i<=(size-4)){
for (int j = 0; j < 4; j++)
{
temp[j]=buffer[i+j];
}
}
if (memcmp(temp,IEND,4)==0)
{
//note the location
location = i;
break;
}
}
free(temp);
free(buffer);
return location;
}
int searchArgs(char **args,char *input,int len){//searches args for input string
for (size_t i = 0; i < len; i++)
{
if(strcmp(args[i],input) == 0){
return 1;
}
}
return 0;
}
int main(int argc, char *argv[])
{
argc-=1;
char **strings = (++argv);
char clean = 'b';
//check flags
if(searchArgs(strings,"--clean",argc) == 1)clean = 'y';
//open pngFile
FILE* checkExistence = fopen(strings[0],"r");
if(checkExistence == NULL){
perror("ERROR");
exit(1);
}
fclose(checkExistence);
FILE* pngFile = fopen(strings[0],"r+b");
if(pngFile == NULL){
printf("ERROR: Could not Open PNG!");
perror("ERROR");
exit(1);
}
//verify png
readSig(pngFile);
char *folderName = getPath(strings[0]);
chdir(folderName);
long long int globalLocation = getIEND(pngFile) +4;
fseek(pngFile,0,SEEK_END);
long long int sizeOfPngFile = ftell(pngFile);
fseek(pngFile,0,SEEK_SET);
if(globalLocation+4 == sizeOfPngFile){//check if there is any embedded files
printf("NO FILES FOUND!\n");
return 1;
}
//check if simply files or folder
long long int location = getIEND(pngFile);//returns location of IEND + 4
{
char *stegHolder = malloc(8);
fseek(pngFile,location+8,SEEK_SET);
fread(stegHolder,1,8,pngFile);
fseek(pngFile,0,SEEK_SET);
if(memcmp(stegHolder,STEGSIG,8) == 0){
}else{
f = 1;
fseek(pngFile,location+8,SEEK_SET);
int folderNameSize;
char *nameOfFolder;
fread(&folderNameSize,sizeof(int),1,pngFile);
nameOfFolder = (char*)malloc(folderNameSize);
fread(nameOfFolder,1,folderNameSize,pngFile);
nameOfFolder[folderNameSize] = '\0';
printf("INFO: EXTRACTING FILES TO : %s\n",nameOfFolder);
if(checkIfFolderExists(nameOfFolder) == 1){printf("FOLDER %s ALREADY EXISTS\n"); return 1;}
else mkdir(nameOfFolder,0777);
}
free(stegHolder);
}
writeData(pngFile);
//clean pngFile
if(clean == 'y' || clean == 'Y'){
globalLocation = globalLocation + 4;
char *data = (char*)malloc(globalLocation);
fseek(pngFile,0,SEEK_SET);
fread(data,1,globalLocation,pngFile);
fseek(pngFile,0,SEEK_SET);
fclose(pngFile);
char *fileName = slashPath(strings[0]);
FILE* cleaner = fopen(fileName,"wb");
if (data == NULL)
{
printf("ERROR: COULD NOT CLEAN FILE!\n");
return 1;
}
fseek(cleaner,0,SEEK_SET);
int written = fwrite(data,1,globalLocation,cleaner);
fclose(cleaner);
free(data);
}
printf("SUCCESFULLY EXTRACTED %d FILES\n",numOfFiles);
return 0;
}