EmbLogic's Blog

2 bit & 3 bit compression- decompression . a different approach.

the approach is to stuff all the 24(3bit lcm of 3 and 8) bits into an integer variable, and then writing into the file from that int variable taking 1 byte at a time.**

in 2 bit compression the 4  index codes(2 bit each) is stuffed into a byte using iterations and then written into the file.

similar strategies are used at the time of decompression..

// 2 bit compression**********************************************************************

int compress2(int *fd,char *ma,int *cl)
{
int cfd,j;
unsigned char i1,ch,byt;
cfd =open(“encry”,O_CREAT | O_RDWR,0666);
lseek(*fd, 0,  SEEK_SET);
while(1)
{
byt = byt ^ byt;
for( j = 6; j >= 0; j -= 2)
{
if(!(read(*fd, &ch, 1)) && ch == 10)
goto o;
i1 =(char) findl( ch, ma);
i1 = i1<<(j);
byt = byt | i1;
}

write(cfd, &byt, 1);
}

o:        write(cfd, &byt, 1);
return cfd;
}

// 3bit compression************************************************************************
int compress3(int *fd,char *ma,int *cl)
{
int cfd,i,j,m,flag=0;
unsigned int k,p;
unsigned char ch,b;
cfd =open(“encry”,O_CREAT | O_RDWR,0666);
lseek(*fd, 0,  SEEK_SET);
while(1)
{    k=0;j=0,m=1;
for(i=8 ; i <= 29; i+=3)
{
p=0;
if(!(read(*fd, &ch, 1)) && ch == 10 )
{flag=1;
goto o;}
p = findl( ch, ma ) ;
p = p << 29;
p = p >> i;
k = k|p;
if(j++ == 2)
{j=0;m++;}
}
o:        printf(“\nm:%d”,m);
for( i = 8 ; i <= 8*m ;i+=8 )
{
b = b^b;
p = k;
p= p << i;
p= p >> 24;
b =(char)p;
if(b==0)break;
//    printf(“\nhey:%d p: %d\n”, b,p);
write(cfd, &b, 1);
}

if (flag == 1) break;
}

return cfd;
}

//2 bit decompression*******************************************************************************

int decompress2(int *cfd, char *ma)
{
unsigned char ch,i1,i2;
int rfd,l=0,m=0;
rfd = open(“dec”,O_CREAT|O_RDWR,0666);
lseek(*cfd, 0, SEEK_SET);
while(read(*cfd, &ch, 1) && ch != 10)
{
l = 0;
while(l<=6 && m<=len)
{
i1 = ch;
i1 = i1 << l;
i1 = i1 >> 6;
l+=2;
m++;
printf(” %d = %c\n”,i1, *(ma+i1));
write(rfd, (ma+i1), 1);
}
}
}

//3 bit decompression************************************************************************
int decompress3(int *cfd, char *ma)
{
unsigned char ch,i1,i2;
int rfd,i,h;
unsigned int m,n,p;
rfd = open(“dec”,O_CREAT|O_RDWR,0666);
lseek(*cfd, 0, SEEK_SET);
while(1)
{
m = 0;
for(i=16; i >= 0; i -= 8)
{
ch = 0;p=0;
if(!(read(*cfd, &ch, 1)) || ch == 10)
goto o;
p = ch<<i;
m = m | p;
printf(“\n%d %d %d\n”,i,m,ch);
}
o:    for(i=8 ; i<=29; i+=3)
{
n = 0;
n = m;
n = n << i;
n = n >> 29;
if(n == 0 && ++h  == len-2)goto l;
printf(“\n %d = %c\n”,i,*(ma+n));
write(rfd , (ma+n),1);
}

}
l:
return rfd;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>