G
Ginrai
Dear all,
I would like to write a file compress and decompress program in C,
I tried to find some in internet and I figured out the below (File I/O part)
My question is why there are so many shift in bits? And I actually don't
quite understand what it is doing.
Is there any simple method just output the code calculated to a text file,
and read it again without error?
Is there so many bit shift just due to the codeword is variable length?
Thank you very much.
Ginrai
==============================
Compress:
void write_code(FILE *output,unsigned int code)
{
static int output_bit_count=0;
static unsigned long output_bit_buffer=0L;
output_bit_buffer |= (unsigned long) code <<
(32-MAXBITS-output_bit_count);
output_bit_count += MAXBITS;
while (output_bit_count >= 8)
{
putc(output_bit_buffer >> 24,output);
output_bit_buffer <<= 8;
output_bit_count -= 8;
}
}
==============================
Decompress:
unsigned int read_code(FILE *input)
{
unsigned int return_value;
static int input_bit_count=0;
static unsigned long input_bit_buffer=0L;
unsigned char ch;
while (input_bit_count <= 24)
{
ch = getc(input);
input_bit_buffer |= ch << (24-input_bit_count);
input_bit_count += 8;
}
return_value=input_bit_buffer >> (32-MAXBITS);
input_bit_buffer <<= MAXBITS;
input_bit_count -= MAXBITS;
return(return_value);
}
==============================
I would like to write a file compress and decompress program in C,
I tried to find some in internet and I figured out the below (File I/O part)
My question is why there are so many shift in bits? And I actually don't
quite understand what it is doing.
Is there any simple method just output the code calculated to a text file,
and read it again without error?
Is there so many bit shift just due to the codeword is variable length?
Thank you very much.
Ginrai
==============================
Compress:
void write_code(FILE *output,unsigned int code)
{
static int output_bit_count=0;
static unsigned long output_bit_buffer=0L;
output_bit_buffer |= (unsigned long) code <<
(32-MAXBITS-output_bit_count);
output_bit_count += MAXBITS;
while (output_bit_count >= 8)
{
putc(output_bit_buffer >> 24,output);
output_bit_buffer <<= 8;
output_bit_count -= 8;
}
}
==============================
Decompress:
unsigned int read_code(FILE *input)
{
unsigned int return_value;
static int input_bit_count=0;
static unsigned long input_bit_buffer=0L;
unsigned char ch;
while (input_bit_count <= 24)
{
ch = getc(input);
input_bit_buffer |= ch << (24-input_bit_count);
input_bit_count += 8;
}
return_value=input_bit_buffer >> (32-MAXBITS);
input_bit_buffer <<= MAXBITS;
input_bit_count -= MAXBITS;
return(return_value);
}
==============================