Posted by: login November 18, 2004
Login in to Rate this Post:
0
?
Hey Ya'll who were interested in the lz77 program. I finally finished it. Took a different approach and yes, I am done!!! What a relief.... This program will compress and decompress the file.
Peace!
LoGiN :)
#include
#include
#include
#include
#define SW_SIZE 4096 /* sliding window will be 4k bytes */
#define LA_SIZE 32 /* Look Ahead Buffer will be 32 bytes */
#define PASS 1
#define FAIL 0
/* function declarations */
void BytesToBits(unsigned char, int) ;
int BitsToBytes(unsigned char *, int) ;
int FindMatch(unsigned char *, unsigned char *, int) ;
int BiggestMatch(unsigned char *, unsigned char *, int) ;
void BuildZeroCode(unsigned char) ;
int BuildOneCode(int, unsigned char) ;
int ZeroCodeWrite(void) ;
int OneCodeWrite(void) ;
/* global variables */
int curr_len = 0 ;
unsigned char curr_byte = 0 ;
FILE *f_input, *f_output ;
unsigned char *p_start ;
unsigned char *p_sw ;
unsigned char *p_la ;
unsigned char *p_la_start ;
unsigned char *p_flag ;
unsigned char *p_end ;
int offset = 0 ;
int len ;
unsigned char temp[31] ;
void BytesToBits(unsigned char in_char, int in_len)
{
unsigned char temp_char, mask ;
if(in_len + curr_len > 7) /* the number bits to be added to curr_byte will at least
fill curr_byte to capacity */
{
temp_char = in_char ; /* stores in_char in temp variable for manipulation */
temp_char >>= (in_len + curr_len - 8) ; /* shifts temp variable over to the right
so the bits that we want to deal with are in
the appropriate psoition */
mask = (unsigned char)(pow(2, 8 - curr_len) - 1) ; /* creates a mask that will affect
the number of bits needed to fill
curr_byte */
temp_char &= mask ; /* sets all other bytes besides the ones that will fill curr_byte
to zero */
curr_byte |= temp_char ; /* puts the bit(s) needed to fill curr_byte in the
appropriate position(s) */
putc(curr_byte, f_output) ; /* writes curr_byte to a file */
curr_byte = 0 ; /* flushes curr_byte */
curr_len = in_len = in_len + curr_len - 8 ; /* changes curr_len and in len to reflect
how many bytes are needed to fill curr_byte */
curr_byte = (in_char <<= 8 - in_len) ; /* shifts any left over bits from in_char to the
appropriate position and puts them into curr_byte */
}
else /* the number of bits to be added to curr_byte does not fill curr_byte */
{
mask =(unsigned char)(pow(2, in_len) - 1) ; /* creates a mask that will affect the number
of bits to be added to curr_byte */
in_char &= mask ; /* sets all other bytes other bytes besides the ones to be added to
curr_byte to zero */
in_char <<= (8 - curr_len - in_len) ; /* shifts bits that are to be added to curr_byte
to the appropriate position */
curr_byte |= in_char ; /* adds bits to be added to curr_byte to curr_byte */
curr_len += in_len ; /* curr_len chnages to reflect nu,ber of free bits remaining in
curr_byte */
}
}