Posted by: login November 18, 2004
Login in to Rate this Post:
0
?
int BitsToBytes(unsigned char *out_char, int out_len)
{
unsigned char temp_char, mask ;
if(out_len >= curr_len) /* number of bits need to fill out_char will require more bits
than currently available in curr_byte */
{
*out_char = curr_byte ; /* out_char stores the bits of curr_byte for manipulation */
(*out_char) >>= (8 - out_len) ; /* shifts the remaining bit(s) from curr_byte to the
appropriate position(s) in out_char */
mask = (unsigned char)(pow(2, out_len) - 1) ; /* creates a mask that will affect the
number of bits to be added to out_char */
(*out_char) &= mask ; /* sets all other bits besides the ones to be added to out_char
to zero */
curr_byte = (unsigned char)fgetc(f_input) ; /* retrieves next byte from file and puts it in
curr_byte */
if(feof(f_input)) /* if end of the file is reached, return 0 to main */
return FAIL ;
out_len -= curr_len ; /* sets lentgh of out_char to reflect how many bits are needed to
complete out_char */
temp_char = curr_byte ; /* stores curr_byte in temp_char for manipulation */
temp_char >>= (8 - out_len) ; /* shifts bits in temp_temp char that need to be added to
out_char to the appropriate position */
mask = (unsigned char)(pow(2, out_len) - 1) ; /* creates a mask that will affect the
number of bits to be addedd to out_chat */
temp_char &= mask ; /* sets all other bits besides the ones to be added to out_char to
zero */
(*out_char) |= temp_char ; /* adds bits to be added to out_char to out_char */
curr_byte <<= out_len ; /* shifts next available bits in curr_byte to the left
end of curr_byte */
curr_len = 8 - out_len ; /* changes length of curr_byte to reflect how many bits are
still available in curr_byte */
return PASS ; /* tells main that the end of file has not been reached */
}
else /* the number of bits to be added to out_char does not empty curr_byte */
{
*out_char = curr_byte ; /* out_char stores the bits of curr_byte for manipulation */
(*out_char) >>= 8 - out_len ; /* shifts the bits needed from curr_byte to the
appropriate position in out_char */
mask = (char)(pow(2,out_len)-1) ; /* creates a mask that will affect the number of
bits added to out_char */
(*out_char) &= mask ; /* sets all other bits besides the ones added to out_char to
zero */
curr_byte <<= out_len ; /* shifts next available bits in curr_byte to the left end of
curr_byte */
curr_len -= out_len ; /* changes the length of curr_byte to reflect how many bits are
still available in curr_byte */
return PASS ;
}
}
int FindMatch(unsigned char *srch, unsigned char *target, int target_len)
{
offset = 0 ;
while(srch + target_len < target)
{
if(!(memcmp(srch, target, target_len)))
return(target_len) ;
srch++ ;
offset++ ;
}
return FAIL ;
}