Posted by: login November 8, 2004
Login in to Rate this Post:
0
?
Folks, this is my code for LZ77. I can't seem to find what the problem is. Can anyone help me out here? Any programmers at sajha? Has anyone had to code LZ77 before? I tell ya, it's a pain in the butt!
Hers is my compression code: I still have to work on my decompression routine. Thanks for any help guys!
Peace!
-LoGiN
*************************************************************
#include
#include
#include
/**************************************************************/
int SW, swb, LA, lab, minsearch, offset, length, inamount;
int lastrun, lastbits, alreadyin;
FILE *fin,*fout;
char *swbegin, *swend, *labegin, *bigbuf,*endfile;
int curroffset = 0;
unsigned char currbyte;
int pow(int x, int z)
{
int y;
int w = 1;
for(y=1;y<=z;y++)w*=x;
return w;
}
/*************************************************************/void ByteIntoBits(unsigned char inchar, int start, int len)
{
unsigned char mask;
int templen, j;
if (curroffset + len > 8){
templen = 8 - curroffset;
ByteIntoBits(inchar, start, templen);
ByteIntoBits(inchar, start + templen, len - templen);
return;
}
mask = (unsigned char)(pow(2, len) - 1);
if ((j = 8-start-len) > 0) mask <<= j;
mask &= inchar;
if (curroffset < start) mask <<= start - curroffset;
if (curroffset > start) mask >>= curroffset - start;
currbyte |= mask;
curroffset += len;
if (curroffset > 7) {
curroffset -= 8;
fwrite(&currbyte, 1, 1, fout);
currbyte = 0;
}}
/***************************************************************/
int Findbiggest()
{
while(length>minsearch){
while(swbegin<=swend - length){
if(!strncmp(swend, swbegin, length)){
offset = swend - swbegin;
return 1;}
else swbegin++;}
length--;
swbegin = swend - SW;
}
return 0;}
/***************************************************************/
int Code(){
int data = 1;
unsigned char *ptr;
printf("Code -- offset = %d length = %d\n", offset, length);
ByteIntoBits((unsigned char)data,7,1);
ptr=(unsigned char *)&offset;
offset--;
if(swb<=8) ByteIntoBits(*ptr,8-swb,swb);
else{
ByteIntoBits(*ptr,0,8);
ptr++;
}offset++;
ptr =(unsigned char *)&length;
length--;
if(lab<=8) ByteIntoBits(*ptr,8-lab,lab);
else{
ByteIntoBits(*ptr,0,8);
ptr++;
ByteIntoBits(*ptr,16-lab,lab-8);
}
length++;
swend += length;
ByteIntoBits(*swend, 0, 8);
swend++;
return 0;}
/****************************************************************/
int Single(){
int data = 0;
printf("Single %c\n",*swend);
ByteIntoBits((unsigned char)data,7,1);
ByteIntoBits(*swend,0,8);
swend++;
return 0;}
/***************************************************************/
void foo(){
int a=0;
swbegin = swend - SW;
while(swend<=labegin){
length = LA;
if(Findbiggest()){ Code();}
else { Single();}
swbegin = swend - SW;
}
if(lastrun){
length=endfile-swend-1;
while(length>minsearch){ if(Findbiggest()){ Code();}
else { Single();}
swbegin = swend - SW;
length=endfile-swend-1;
}
}
alreadyin=swend-labegin;
for(a=0;a