-
Notifications
You must be signed in to change notification settings - Fork 0
/
compression6.c
executable file
·147 lines (127 loc) · 2.64 KB
/
compression6.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
//this is compression of 3 byte
#include"header.h"
#include"prototype.h"
int compression6(char *m,int l,char *str)
{
// printf("in function------>%s\n",__func__);
char encr1[]=".encr";
char* encr;
encr=malloc(strlen(str) + strlen(encr1) + 1);
strcpy(encr, str);
strcat(encr, encr1);
// printf("encryption file name is---> %s\n",encr);
// printf("kry file name is---> %s\n",key);
int i=0,fd4,fd2,w,g;
unsigned int n,count,val,new;
int loc,d,x,eof;
unsigned char ch1;
unsigned char byte;
char *buff;
buff = (char *)malloc(sizeof(char)*l);
if(!buff)
{
perror("malloc");
exit(EXIT_FAILURE);
}
// printf("the master arraay is -----> %s\n",m);
// printf("the final legth of masterarray is---------------------->%d\n",l);
preserve_masterarray(m,l,str);
fd2 = open(str,O_RDONLY);
count=lseek(fd2,0,SEEK_END);
lseek(fd2,0,SEEK_SET);
val=(count*0.75);
if(count%8 != 0)
val = val+1;
// printf("the no. of characters in actual file is & the encrypted file will be---------------------->%d & %d\n",count,val);
fd4 = open(encr,O_WRONLY | O_CREAT);
d=0,w=0;
while(1)
{
if(i==0 || x==0)
{
byte= byte^byte;
}
if(i==0 || i==1 || i==3 || i==5 )
{
eof=read(fd2,&ch1,1);
// printf("the ch1----->%c\n",ch1);
loc = findloc(ch1,m,l);
// printf("the %cth location is--------->%d\n",ch1,loc);
n = loc;
if(eof==1)
{
w++;
// printf(" the %d th character is read from file\n",w);
}
}
if( eof == 0)
{
if(i>0 && d<val)
{
write(fd4,&byte,1);
// printf("the last byte is written to the file is-------------------->%c && %d\n",byte,byte);
d++;
}
break;
}
if(i==0)
{
n = n << 2;
byte = byte | n;
}
if(i==1)
{
new = n;
n = n << 2;
n = n >> 6;
byte = byte | n;
}
if(i==2)
{
new = new << 4;
byte = byte | new;
}
if(i==3)
{
new = n;
n = n << 2;
n = n >> 4;
byte = byte | n;
}
if(i==4)
{
new = new << 6;
byte = byte | new;
}
if(i==5)
{
n = n << 2;
n = n >> 2;
byte = byte | n;
}
if(i==1 || i==3 || i==5)
{
if(d<val)
{
write(fd4,&byte,1);
x=0;
d++;
// printf("the byte is written to the file is-------------------->%c && %d && %d\n",byte,byte,d);
}
}
else
x=1;
// printf("value of i ----------------------------------->>>>>>>>%d\n",i);
i++;
if(i==6)
i=0;
}
// printf("fd2-----> %d\n fd4-------->%d\n",fd2,fd4);
lseek(fd4,0,SEEK_SET);
read(fd4,buff,l);
// printf("the encryption code is -------->%s\n",buff);
close(fd4);
close(fd2);
printf("\n.\n..\n...\n.....\n........Encrypted file is generated using 6-Bit Compression named %s\n\n",encr);
return 0;
}