MY STRENGTH IS THAT I ALWAYS DO 80% OF THINGS WHAT I THINK...my weakness is this that sometimes i find myself so feelingless..so selfish...it might be good for me..I HAVE COMPLETED MDC WITH ALL 1 TO 7 BIT COMPRESSION TECHNIQUE..FOR DIFFRENT NO. OF DISTINCT CHARACTERS..I HAD ALSO COMPLETED..PROJECT USING INTERPROCESS COMMMUNICATION..FIFO PIPES SOCKETS,THREADS,,USING SYNCHRONIZATION WITH SEMAPHORES,I HAD CREATED PROJECT WITH DIFFRENT SOCKETS..THREADS..THREADS WITH PROCESSES...PROCESS WITH PIPE..SOCKETS WITH MDC...FTP USING THREADS & SOCKETS & SO ON...I HAD DONE A LONG TIME PRACTICE ON C..BOARD BRINGUP..,CHARACTER DRIVER..MAKEFILE..ARM PROCESSOR..I HAD MADE TOOLCHAIN..I AM ALSO WORKING IN C++..I AM LEARNING PYTHON ALSO...TOUCH SCREEN DRIVER SO ON..USB DRIVER...BLOCK DRIVER...PCI DRIVER..PARRLEL PORT DRIVER....
/*THIS IS CODE FOR 6 BIT COMPRESSION & DECOMPRESSION*/
//U MAY MAKE DIFFRENT FILES FOR COMPRESSION & DECOMRESSION & EXECUTE THEM TOGETHER USING MAKEFILE...OR SEPERATELY..AS UR REQUIREMENT
int decompress()//decompression of 6 bit..means upto 64 distinct no. of characters...is reverse order of its compression
{
unsigned char cp=0,ch=0,byt=byt=byt^byt,in=in^in;
int i=0,j=0,count=1;
fd=open("desti",O_RDONLY);//open compressed file in rd_only mode
printf("fd=%d\n",fd);
while(count)//count=1 at starting
{
//sleep(1);
//printf("j=%d\n",j);
count=read(fd,&ch,1);//read byt from comressed file..which can have max.no. of 1 full & 1 half character inside it...1full character's 6 bits..& half character's 2 bits..
cp=ch;//1st of all preserve the byt..bcz.this byt contains 2 character address..so shifting to byt in 1st time will change its value...so preserve it for 2nd time use
if(count==0)//if count =0 means there is no character to decompress..so go to out of the loop
break;
for(j=8;j>0;j=j-2)//j starts from 8 every time j decreases ..8 6 4 2 ..so i will read 3 bytes & decompress them & extract 4 characters from those 3 bytes read ..
{
ch=cp;//pick value from cp...in condition if(i*j==24)..u may see that i have changed the value of ch & also cp..so new read character is decompressed this time ..that is cp is to be put into ch..
//value of cp is either old & new will give me write bytes to decompress
i=j;//i=6
if(j==6)
i=i-2;//i=4
else if(j==4)
i=i+2;//i=6
//ch=000000 00//0001 0000//10 000011 that ch have initially
if(j != 8)//1st time no left shift is made
ch=ch<<j;//make ch right shift means take the bits of required character to starting 6 bit poisition..after removing all the garbage value...means j=8 6 4 2..right shift is not for j=8
//ch=000000 00 1st remain as it is//ch=//000001 00//000010 00//000011 00//000100 00//000101 00...
ch=ch>>2;//two right shift will give me 6 bits which was at starting 6 bit positon..will become last ..6 bit positions
//means 1st 00 000000 //2nd 00 000001//3rd 00 000010 //4th 00 000011 //5th 00 000100 //6th 00 000101 so on
if((i)*(j)==24)//pending no. of bits are extracted from another byt.to make a character..but there are pending no. of bits only for j=6 & j=4..means 2nd & 3rd time ...so enter to extract pending no. of bits for these 2 vlues
{
count=read(fd,&ch,1);//read a new byt to extract pending no. of bits
if(count==0)
break;
in=ch;//copy into in
cp=ch;//preserve tht character
in=in>>(i);//i=4 for j=6 & i=6 for j=4..
//4 right shift of in for 2nd character will give me 4 pending bits of 2nd character at last positions of in variable.
byt=byt|in;//add pending no. of bits in..in/..variable ..if any..
in=in^in;//again make in=0 for further use of it..
}
printf("%c",*(masterarray+byt));//now byt is full address of one character like an index..so add that index in masterarray to get exact position of that character in ur system..
byt=byt^byt;//make byt=0 for its again use...
}//again go into while loop for further read of new character to decompress untill all the characters in file are decompressed....
}
return 0;
}
int indu(char buff)//func to index..
{
int i=0;//i=0 initially
if(buff == '\0')
goto OUT;
//i=0;
//
while(buff != *(masterarray+i))//WHOLE FILE TO BE COMPRESSED IS COMPAIRED TO MASTERARAY..AT WHAT VALUE OF i..character from file is matched to distinct character of masterarray..that value of i will be treated as identification of that character ...so that value of i is returned...
unsigned char ch=0,in=in^in,byt=byt^byt,buff=0;///byt & ch is always of unsigned char not of char..bcz.char can have -ve value .& we have to convert it in addressing...while decompression ,addition of -ve value will give wrong address ...result no character will found...so address must always be +ve...
int nd,i=0,j,count=1;
fd=open("source",O_RDONLY);//open file to be compressed
printf("fd=%d\n",fd);
wfd=open("desti",O_WRONLY|O_CREAT);//open the file in which compressed characters are to be placed
printf("wfd=%d\n",wfd);
while(count)//count=1 at starting
{
if(count == 0)//when count is 0 goto out of while loop
break;
for(j=8;j>0;j=j-2)//4 characters- 3 bytes for 6 bit compression
{
//printf("j=%d\t",j);
i=j;
if(j==6)
i=i-2;//i=4
else if(j==4)
i=i+2;//i=6
count=read(fd,&buff,1);//read character from file to be compressed
if((i)*(j)==24)//IF ANY OF i or j is 6 or 4...only then pending bits will left ...or i can say ..in place of 4 characters...mid two characters will have pending no. of bits left that we get need to put those pending no. of bits in another byt & preserve that byt for some time ....untill 1st byt is written to file ...
{
in=ch;//put character index into the in variable
in=in<<i;
//make it right shift as i..i will have difference of 2 always in this condition
//j=6 i=j; i=i-2;;;will make i=4...so j=6 i=4 means 4 left shift of in variable will give me 4 last bits of 2nd character in another byt
//j=4 i=j; i=i+2;;;will make i=6...so j=4 i=6 means 6 left shift of in variable will give me 2 last bits of 3rd character in another byt
}
ch=ch<<2;//2 left shift will give me 6 bits of character in starting 6 positions..
if(j != 8)//for 1st character...there is no right shift
ch=ch>>j;//0 6 4 2 ..1st char become ch=000000 00//first 6 bit will have address of character
//for 2nd character right shift is 6...become ch=000000 00//last two bits are of 2nd character in this ch...
//for 3rd time character
byt=byt|ch;//2nd //
if(j != 8)//for 1st character only 6 bits in byt...so first time do not write into file
{
write(wfd,&byt,1);
printf("byt=%p\t",byt);
byt=byt^byt;//1st time do not make byt 0
byt=byt|in;//when 1st byt is written to compressed file ..make empty to file..add in to byt...tht... in... contains pending no. of bits..
in=in^in;//aftr adding..in..to byt..make empty to ..in...for its further use
}
}
}
return 0;
}
int codelength(int ndc)//now we have to find codelen { int i,j=0;//j will represent no. bits 6 to represent 64 distinct characters //printf("ndc=%d\n",ndc); for(i=1;i<ndc;j++)//untill i<ndc..j++ every time... { //printf("i=%d\n",i); i=i*2;//when i=i*2 will count power of 2..j=0 2=1*2 j=1 4=2*2 j=2 8=4*2 j=3;...so on } if(ndc==0) j=0; return j;//in this case ...we have j=6 counted...for 6 bit compression.. }
int check(char buff)//character is passed further here in check function to find distinct no. of characters...
{
int i;
//printf("masterarray=%p\n",masterarray);
for(i=0;i<size;)//i=0 size=1 1st time ..so loop will operate only 1 time time in 1st iteration
{
if(*(masterarray+i)==buff)//1st time masterarray ppointer is empty that is malloced..so we will compare character in masterarray if there is any..to the character of file that has been read from the file ...
goto OUT;//if we get that character into masterarray..then we will not put that character into masterarray further...& goto OUT...WILL return -1 to check function ...which will not tend to ++ment size..or will not realloc the masterarray
else
i++;//if we had put some distinct characters in masterarray then compare those distinct no. of characters in masterarray with character read from the file...if character read from file do not match 1st character in masterarray...then ++ment the value of i to find the right match of that character in masterarray...
}
if(buff != '\0')//again if character that is not matched with any of character in masterarray..is not NULL
*(masterarray+i-1)=buff;//then ...after no match found for that character put that character into masterarray....*ptr=buff...means we are putting..value of buff into address of pointer
//ptr=&buff means we are putting address of character into ptr...& we do not have to change address of masterarray..so do not follow this assignment of character...
printf("*(masterarray+i-1)=%c\n",*(masterarray+i-1));//print distinct no. of characters
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..
{
if(flag==1)//1st time flag is 1..means malloc to masterarray
{
masterarray=malloc(sizeof(char)*size);//bcz we have to put characters in masterarray named pointer..so we need to malloc masterarrray..
//printf("masterarray in malloc=%p\n",masterarray+size);
flag=0;//once masterarray is malloced then flag=0
}
else
{
//printf("master\n");
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);
}
//printf("flag=%d\n",flag);
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
//printf("ret=%d\n",ret);
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++;
return size-2;//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...
int main(int argc,char* argv[])//MAIN WILL TAKE ITS ARGUMENTS FROM COMMAND LINE..ARGC WILL KEEP COUNT OF THOSE ARGUMENTS..
{
char buff;
if(argc <2)//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...
{
printf("PLEASE GIVE ME FILENAME\n");
}
printf("argv[1]=%s\n",argv[1]);//print argument that we send from command line..with %s..bcz we have send name of file
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
printf("fd=%d\n",fd);
while(count)
{
count=read(fd,&buff,1);//read characters from file one by one
//printf("buff=%c\n",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 64 distinct characters..so we can represent these many no. of characters in 6 bits..
}
printf("ndc=%d\n",ret);
ret=codelength(ret);//make a logic to find codelength of distinct no. of characters...
printf("codelength=%d\n",ret);//printf that codelength..means distinct no. of characters could be represented in how many bits...
ret=compress();//now we have distinct no. of characters & codelen both..bcz...distinct no. characters are maximum 64 in our case ...in this file which is to be compressed ..so we wil...compress the file using 6 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.
if(ret==0)
printf("DECOMPRESSED\n");//if decompression is succesful...again returned value will be 0;