for(i=0;i<3;i++)//3 character could be put in 1 byt..at most..byt1=000 001 01...byt2=0 011 100 1...byt3=01 110 111...
//in 3rd byt last character will be complete..i have seen ..if 1byt=8bits...& each character could be represented into 3 bits....then in 3 bytes...8 characters may take place,,,after that same procedure take place ..another 3 bytes will have another 8 characters..so on...i said distinct characters 8 || <8 could be represented by 3 bits...i did not say maximium no. of characters 8 represented by 3 bits...size of file could be 1GB ...no matter but distinct no.of character in that file could be 8...to compress with 3 bit....compression technique...
{
//sleep(1);
if(i==2 && j==2)//before reading so that next character could be compressed....after reading in next turn...//8th character read in file will not have pending no. of bits ...so will not allow to enter in condition if(i==2)...in which pending no. of bits are collected ...
{ printf("HI IN i==2 && j==2 3rd CHAR LAST...BEFORE AGAIN START FROM START\n");
j=-1;//make j=0 again for next turn////make j=-1 again to compress..more no. of characters in file ..with same addressing of distinct no. of characters...to start the procedure again ..from starting..& continues untill...characters in the file to be compressed ..finished ...or count==0
in=in^in;//make in 0 so that in oring it with byte out of loop will give no effect in out of loop at last time i=2 & j=2...
break;//when i=2 & j=2 also 2 which is made -1..now..to start procedure again..so goto out of for loop ...to start ..procedure again in to while loop..//
}
count=read(fd,&buff,1);//read character one by one
printf("count=%d\t %c\n",count,buff);
if(count==0)//if character is null...goto OUT of loop...& put ur last byt into file...by oring with ..in... if any..but in=0 at last time.....in=in^in in if(i==2 && j==2) ..i have done this...
{
printf("IN COUNT=0...GOTO A\n");
goto a;//if count is 0...goto a will break from both of the loops...
}
indu=index_get(buff);//get index for characters...of file
// printf("indu=%d\n",indu);
//sprintf(&ch,"%c",indu);//convert index into character form
ch=indu;//put int value of index into char value so that 8 bits of that char value could be represente as ..address for distinct characters
printf("ch=%d\n",ch);
if(i==2)//every time it read the character 3rd time ...when i=2..3rd character in every byt will not complete in that byt....bcz byt=8 bits...3 characters..could be represented in 9 bits...so every time when we read 3rd character...compress that character & put into another byte...when 1st byt is written to file ...then make || (or )this byt...in which pending bits are placed..
in=ch;//character read 3rd time each time is could have pending bytes..so extract those pending no. of bytes
printf("ch=%d\n",ch);
in=in<<(7-j);//bcz for 1 character ..means for 3rd character of every byt will use in to keep its pendiing no. of bits..1 or single time..........every time...//every 3rd byt is shifted with value of j...j=0 for 1st time 3rd byt writting into byt..means 7 left shift of char type index ...means last bit of charactr we have in 1st position which is to be place in 2nd byt in future///////
//2nd time j=1 for 2nd time we come into for loop again due to while loop...for 3rd character of 2nd time in for loop..in=in<<6...j=1 so 7-1=6 6 left shift will get u 2 bits pending from 2nd byt which is to be place in 3rd byt in future in this coding
//3rd time when we came into for loop ...we will not come into this portion of code...if (i==2)..bcz..in 3rd time it came into for loop will not have pending no. of bits....
//so 1byt=000 001 01...<1 bit is pending written into 2nd byt..i.e.> 0 011 100 1 <2 bits r pending written into 3rd byt i.e.>01 110 111...now bit are complete in 3 bytes...so 8 characters are written into 3 bytes
printf("in=%d\n",in);
}
printf("SHIFTING BITS OF CHARAC\n");
ch=ch<<5;//after pending no. of bits are placed in in named byt for i=2...make actual byt..which is to be write 1st in file....make left shift with 5 to address of each character...1 chracter will have address i.e. before. is 000 00 last 3bits for address of character.i.e. 000 for 1st time 0 index..so make these all bits to left shift to remove all garbage from ..left side of these 3 bits...so make 5 left shift
a=(3*i+j);//for j=0..i=0 i=1 i=2.....for j=1 i=0 i=1 i=2........for j=2 i=0 i=1 i=2.....again j=0 ..i=0 i=1 i=2..//now make right of those bits & make them | bitwise or or i can say add those bits in a byt to prepare 1 byt...
//1st time 5 left shifted bit will have 0 right shift..& or with byt..starting 3 bits of this byt will be address of 1st character...i.e. 000
//2nd time 5 left shifted bit will have 3 right shift & make it or with byt..leaving 1st 3 positions..2nd 3 positions in that byt will have another 3 bits means address of 2nd character
printf("byt###or se just pehle######=%p\n",byt);
//3rd time 5 left shifted bit will have 6 right shift & make it or with byt..leaving 1st 6 positions..3nd 2 positions in that byt will have another 1st 2 bits of 3rd character means address of 3nd character..
//pending bits that character is placed by in=in<<7-j...made byt ...i.e. byt with pending bits
byt=byt|ch;//so i can say j as inter & i is intra to the bytes...this (byt)...& (in)which we use to present to bytes..
printf("byt#########=%p\n",byt);//byt is prepaired with no pending bits
}
printf("BEFORE LABLE a:BREAK\n");
printf("AFTER a:BREAK .....WRITING INTO FILE.... \n");
printf("*********BEFORE WRITING INTO FILE*********byt=%d\n",byt);
count=write(wfd,&byt,1);//after reading 3 characters...when 1 byt is ful ..put into file
printf("written byte count=%d\n",count);
byt=byt^byt;//make byt again..0
printf(" .....ORING BYT=BYT|IN.... \n");
byt=byt|in;//make in | byt...in or with byt...will write all pending bits into byt again....so now this byt will not be made 0 untill..it got full & written into file..file which is compressed file ...
if(buff == '\0' || count ==0)//if count is 0 means no other character left in file to compress//
a:break;//break means goto out of loop
}//again goto a ...to ++ment value of j...& start for loop from i=0..to read 3 characters again...
//printf("masterarray in check=%p\n",masterarray+i-1);
printf("masterARRAY=%c\n",*(masterarray+i-1));
return 0;
OUT:
return -1;
}
int create_masterarray(char buff)////we have to find distinct no. of characters..we have character read from the file in main send by their as a argument,..so here we have to find distinct no. of characters in from file ..& put those no. of characters in masterarray pointer..
{ int ret; if(flag==1) { masterarray=malloc(sizeof(char)*size);//1st time flag is 1..means malloc to masterarray ////bcz we have to put characters in masterarray named pointer..so we need to malloc masterarrray.. // flag=0;////once masterarray is malloced then flag=0
} else masterarray=realloc(masterarray,sizeof(char)*size);////after making flag=0..every time ..it will not malloc the mastearray..it will realloc the masterarray..every time now... //There is difference b/w malloc & realloc is this that..when we do malloc..a new memory from new block is provided to u..in masterarray..then realloc means ...everytime continue to memory that is malloced to u before .memory is provided to u. // //printf("masterarray in realloc=%p\n",masterarray+size); // if(buff != '\0')////if character that is read is not NULL...then is will send for to find distinct of characters in check function....bcz ..we do not have to increase size of our compressed file..so we will try ..not to compress this NULL character...
{ ret=check(buff);////call to check function to check the distinct no. of characters & put those characters into masterarray if they are not in masterarray named pointer
if(ret==0)///if returned value is zero means in check function ...character was put into masterarray this time ..so increse the size...to realloc size again to put character into masterarray again...
size++;////memory in which characters are being putted is masterarray+i-1..size is 1 more i...so size-2 is returned...as no. of distinct no. of charactes... } return size-2; OUT: return -1; }
int main(int argc,char* argv[])//main will pick the arguments from command line ...argc will have count of those arguments
{
int ret,j;
char buff;
count=1;
printf("argv[1]=%s\n",argv[1]);//argv[1] is 1st argument of command line///if arguments on command line are less than 2..it will demand for 2nd argument on command line..that u had not given here on command line..char* argv[] is pointer of array in which argv[0] ..argv[1]..will have name of arguments on command line...but when arguments come from command line ...on that time..ur compiler is not aware that this argument that is coming from command line ..could be a file...
//print argument that we send from command line..with %s..bcz we have send name of file
if(argc != 2)
{
printf("PLEASE GIVE ME FILE NAME\n");
}
fd=open(argv[1],O_RDONLY);///open that file ..by using argument which contains name of file...in RD_ONLY MODE..argv[0] will be the name of file ..in which we are..name of its ELF format
while(count)
{
count=read(fd,&buff,1);//read characters from file one by one
//printf("count=%d read %c\n",count,buff);
ret=create_masterarray(buff);//send these characters to find distinct no..of characters in file ...on bases of those distinct no. of character ..we wil find codelength ..means those no. of distinct characters could be reprsented in how many bits...suppose here we 8 distinct characters..so we can represent these many no. of characters in 3 bits..
//000 001 010 011 100 101 110 111...
//
}
printf("ndc=%d\n",ret);
j=codelen(ret);////make a logic to find codelength of distinct no. of characters...
printf("codelength=%d\n",j);////make a logic to find codelength of distinct no. of characters...
ret=compress();///now we have distinct no. of characters & codelen both..bcz...distinct no. characters are maximum 3 in our case ...in this file which is to be compressed ..so we wil...compress the file using 3 bit compression
if(ret==0)
printf("****compressed****\n");////if compression is succesful,. it will return 0
ret=decompress();////decompress the file again...in reverse order in which we have done compression of file.