-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileUnpacker.c
executable file
·148 lines (127 loc) · 2.95 KB
/
fileUnpacker.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
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
#include "header.h"
BOOL checkDirectoryExist( char* dirName )
{
struct stat st;
if( stat(dirName , &st) == -1 )
return FALSE;
return TRUE;
}
BOOL checkValidation( char* fileName , int key)
{
char *actualName;
read(inputFd , &authentication , sizeof(authentication));
//checking hardcoded data
if( strcmp(authentication.hardcoded , "MARVALLOUS" ) != 0 )
return ERR_INVALID;
//user not given key
if( ( key == 0 ) && ( authentication.type == USER_DEF) )
{
printf("Error : Give encryt key value\n");
return ERR_AUTHENTICATION;
}
//geting autal fileName
actualName = fileName;
for (int i = strlen(fileName); i >= 0; i--)
{
if( fileName[i] == '/' )
{
actualName = &fileName[i + 1] ;
break;
}
}
//check key set by application
if( ( key == 0 ) && ( authentication.type == APPLICATION_DEF ))
{
if( authentication.key != strlen(actualName) )
return ERR_AUTHENTICATION;
}
else // check userdefined key
{
if( authentication.key != ( strlen(actualName) + key ) )
return ERR_AUTHENTICATION;
}
return SUCCESS;
}
BOOL writeFile( char* fileName , int size)
{
int temp;
char * ptr = (char * ) malloc ( size * sizeof(char));
outputFd = open( fileName , O_WRONLY | O_CREAT );
if( outputFd == -1 )
return ERR_CREATE;
read( inputFd , ptr , size );
temp = size;
while(temp != 0)
{
ptr[temp] -= authentication.key;
temp--;
}
write( outputFd , ptr , size );
close(outputFd);
free(ptr);
return SUCCESS;
}
BOOL fileUnpack( char* fileName , char* dirName , int key)
{
BOOL ret;
FILEINFO obj;
relativePath = NULL;
inputFd = open(fileName, O_RDONLY );
if(inputFd == -1)
{
printf("Error: File Not Found\n");
return ERR_ACCESS;
}
//Open existing file to read
ret = checkValidation(fileName , key );
if(ret != SUCCESS ) return ret;
relativePath = dynamicConcat(relativePath,dirName);
if(checkDirectoryExist( relativePath ) == FALSE)
{
if( mkdir(dirName , 0777) == -1)
{
printf("Error: will creating directory\n");
return ERR_CREATE;
}
}
//open pack file
while( (read( inputFd , &obj , sizeof(obj) ) ) != 0 )
{
if( obj.size == DIRECTORY_OPEN )
{
relativePath = dynamicConcat(relativePath,"/");
relativePath = dynamicConcat(relativePath,obj.name);
if(checkDirectoryExist( relativePath ) == FALSE)
{
if( mkdir(relativePath , 0777) == -1)
{
printf("Error: will creating directory\n");
return ERR_CREATE;
}
}
continue;
}
else if( obj.size == DIRECTORY_CLOSE )
{
//remove file name form path
relativePath = dynamicRemoveLast(relativePath , '/');
continue;
}
else
{
relativePath = dynamicConcat(relativePath,"/");
relativePath = dynamicConcat(relativePath,obj.name);
ret = writeFile( relativePath , obj.size );
if( ret != SUCCESS )
return ret;
relativePath = dynamicRemoveLast(relativePath , '/');
continue;
}
}
if( close(inputFd) == -1 )
{
printf("Error: Will Closing the output file \n");
return FALSE;
}
return TRUE;
}