4 unsigned char byt=byt^byt,ch=0,cp=0;//always take byt as unsigned not signed bcz..it ma y get -ve value of address..results..adding those address..results no found of perticul ar character
5 int i=0,set=0;count=1;
6 fd=open("desti",O_RDONLY);//open compressed file in wronly mode.....
19 ch=ch<<i;//decompression is done..left shift to perticular bit...make it as 1st bit of byt..so that all garbage value could be removed after that bit
20 ch=ch>>7;//again ...7 right shift will..make ch ..last bit as address of our character. .
21 printf("ch=%c\n",*(masterarray+ch));
22 }
23 //set++;
24 }
25 return 0;
26 }
27 int indu(char buff)
28 {
29 int i=0;
30 printf("buff=%c\n",buff);
31 //for(i=0;i<2;i++)
32 //printf("master__+___=%s\n",masterarray);
33 while(buff != *(masterarray+i))//compare that perticular character to distinct characte rs of masterarray...when it got same
34 {
35 if(buff==*(masterarray+i))
36 break;//it will go out of loop
37 else
38 i++;
39 }
40 return i;//return that perticular position of character...that position will be the ide ntification of that character ..so position will be used as ..address for that characte r
41 }
42 int compress()//in compression
43 {
44 unsigned char byt=byt^byt,ch=0;
45 char buff;
46 int i,ret;
47 count=1;
48 fd=open("source",O_RDONLY);//i had opened file which is to be compressed in rdonly mode
49 wfd=open("desti",O_WRONLY|O_CREAT);//open file in which compressed file is placed ...in writeonly mode
50 while(count)//count =1 at starting
51 {
52 for(i=0;i<=7;i++)//for bcz..this is 1 bit compression ...1 bit may represent 2 characte rs at leasteither it is 0 or it is 1///so 8 bits...are used to represent 8 characters . .for each 0 or 1 value...
53 {
54 count=read(fd,&buff,1);
55 if(count==0 || buff=='\0')
56 break;
57 ret=indu(buff);//index of characters is need to find 1st...
58 //this index is int value .../so convert it into character value 1st or put int value i nto char value...bcz int value is representing ...16 bits...but we need 8 bit ..to set addresssings ....
59 //we will use this character value of index to convert..it into address,,
60 //we will put address bit by bit to make a character to write into compressed file that is desti file...
int codelen(int ndc)//SO TO compress file...or addresses....we need to find no. of nits pe character///
77 {
78 int i,j=0;
79 for(i=1;i<ndc;j++)//as loop moves j ++ments which will represent as code further
80 i=i*2;//i=i*2...will give power of 2,,,multiplyed again &agan 2 4 8 16 32 64 128..acc. to that j ++ments
81 if(ndc==0)
82 j=0;//codelength is 0 for no distinct characters ..or no. chatracters in file
83 return j;//return codelength
84 }
85 int check(char buff)//when masterarray is allocated ..//then...distinct no. of characte rs could be put in that masterarray....by bfollowing logic
86 {
87 int i=0;
88 while(i<size)//i=0 size=1////i<size....loop will execute...0<1 for starting loop
89 {
90 if(*(masterarray+i)==buff)//masterarray is empty at starting,...now check masterarray 1
91 //by 1 ...//further on after putting a character into masterarray..if i get character i n masterarray....then goto out...to return -1
92 goto OUT;
93 else
94 i++;//if i did not get character in masterarray..then ++ments masterarray..to find agai n character in msterarray..
95 }
96 //NOW i=1...
97 *(masterarray+i-1)=buff;//put masterarray character in 0th position//when whole the tim e in loop..i did not get character in masterarray////then put that character into the m asterarray....//
98 printf("masterarray=%c\n",*(masterarray+i-1));
99
100 return 0;//if ret=0 then size will ++ments otherwise not...see return arg of create_masterarray...
101 OUT:
102 return -1;
103 }
104 int create_masterarray(char buff)
105 {
106 //int flag=0;;suppose i have done flag=0...at this point than we know this function is called by main again & again....so flag become 0 again & again..so it will malloc again & again...provide diffrent addresses for masterarray again & again ...so next step ,'w ill give u error ...bcz...u could not be able to find address in masterarray..
107 if(flag==1)
108 {
109 masterarray=malloc(sizeof(char)*size);//1st we need to provide address to masterarray p ointer ...if we do not provide address to poiter it may had pointed to some other addre ss which could not be allocated...or used by some other process...
110 //when we do malloc first time..it will provide me a block of memory
111 flag=0;
112 }
113 else
114 masterarray=realloc(masterarray,sizeof(char)*size);//when flag is 0 then next time ...i malloced memory is reallocated 1 by 1 ...as per requirement of masterarray..
124 if(argc<2)//if arguments are passed by main()....through the command line...it will not sure untill now that ....if that argument ..it has picked ...is file or a string...arg c is of int type so it will keep record of this int value
125 {
126 printf("PLEASE GIVE ME FILENAME\n");
127 }
128 fd=open(argv[1],O_RDONLY);//open the file
129 printf("fd=%d\n",fd);
130 while(count)//bcz count is globally declared..so its value can be used in condition of while loop////
131 {
132 count=read(fd,&buff,1);//read will return no. of bytes read//
133 if(count==0 || buff=='\0')//if it does not read any no. of byt///or read byt is NULL,,T HEN IT WILL GO OUT OF THE LOOP...
134 break;
135 //printf("buff=%c\n",buff);
136 ret=create_masterarray(buff);//masterarray is a globally declared pointer in which we w ill keep ....all distinct no. of characters....from the file...bcz...to those distinct no. of characters...we will give...diffrent address further on....
137 }
138 printf("ndc=%d\n",ret);
139 ret=codelen(ret);//after getting dsistinct no. of characters..we will get codelength of those distinct no. of characters in the file ....
140 //codelength means...if there are 4 distinct no. of characters..then those 4 characters could be represented in 2 bits...power(2)=4 ...
141 //if there are 8 || <8 distinct no. of characters..3 pow(2)=8..so..3 bits are okey to r epresent code of 8 characters
142 //if we have 16 distinct characters in file ....then those no. of characters could be r epresented in 4 bits...
143 //if there are 32 distinct characters in file could be represented in 5 bits
144 //if there are 64 distinct characters could be represented in 6 bits
145 //if there are 128 distinct characters could be represented in 7 bits
146 //so allover.....character normally have 8 bits...but acoording to ur file ..i may ...g ive temporary addresses to those characters...for that file...and representation of cod e in unknown characters present at address made by us.
147 printf("codelength=%d\n",ret);
148 ret=compress();//compression of code is done & wriiten to a file named desti
149 if(ret==0)
150 printf("COMPRESSED\n");
151 ret=decompress();//decompression of code is done..character read from compressed file & decompressed...
1 #! /bin/bash 2 myapp:mdc.o 3 gcc -o myapp mdc.o 4 mdc.o:mdc.c header.h 5 gcc -c mdc.c 6 #dependent files are written to compile a perticular file.. 7 #-o means giving a new name to compiled file 8 #whenever we do <make> command execution,...it always 1st find name of file starting wi th 'M' ..if it does not get...then it find nama of file starting with 'm'....& compile whole module using that file....again..if it does not find it again...it will ..try to find name of file ...started with any name ...& watches if there are any commands in th at file ...then it executes those commands & treat that file as makefile ~ ~ ~