⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitfile.c

📁 Huffman 压缩/解压算法的ANSI C实现 This archive contains a simple and readable ANSI C implementation of Huffm
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************   Function   : BitFileGetChar*   Description: This function returns the next byte from the file passed as*                a parameter.*   Parameters : stream - pointer to bit file stream to read from*   Effects    : Reads next byte from file and updates buffer accordingly.*   Returned   : EOF if a whole byte cannot be obtained.  Otherwise,*                the character read.***************************************************************************/int BitFileGetChar(bit_file_t *stream){    int returnValue;    unsigned char tmp;    if (stream == NULL)    {        return(EOF);    }    returnValue = fgetc(stream->fp);    if (stream->bitCount == 0)    {        /* we can just get byte from file */        return returnValue;    }    /* we have some buffered bits to return too */    if (returnValue != EOF)    {        /* figure out what to return */        tmp = ((unsigned char)returnValue) >> (stream->bitCount);        tmp |= ((stream->bitBuffer) << (8 - (stream->bitCount)));        /* put remaining in buffer. count shouldn't change. */        stream->bitBuffer = returnValue;        returnValue = tmp;    }    return returnValue;}/****************************************************************************   Function   : BitFilePutChar*   Description: This function writes the byte passed as a parameter to the*                file passed a parameter.*   Parameters : c - the character to be written*                stream - pointer to bit file stream to write to*   Effects    : Writes a byte to the file and updates buffer accordingly.*   Returned   : On success, the character written, otherwise EOF.***************************************************************************/int BitFilePutChar(const int c, bit_file_t *stream){    unsigned char tmp;    if (stream == NULL)    {        return(EOF);    }    if (stream->bitCount == 0)    {        /* we can just put byte from file */        return fputc(c, stream->fp);    }    /* figure out what to write */    tmp = ((unsigned char)c) >> (stream->bitCount);    tmp = tmp | ((stream->bitBuffer) << (8 - stream->bitCount));    if (fputc(tmp, stream->fp) != EOF)    {        /* put remaining in buffer. count shouldn't change. */        stream->bitBuffer = c;    }    else    {        return EOF;    }    return tmp;}/****************************************************************************   Function   : BitFileGetBit*   Description: This function returns the next bit from the file passed as*                a parameter.  The bit value returned is the msb in the*                bit buffer.*   Parameters : stream - pointer to bit file stream to read from*   Effects    : Reads next bit from bit buffer.  If the buffer is empty,*                a new byte will be read from the file.*   Returned   : 0 if bit == 0, 1 if bit == 1, and EOF if operation fails.***************************************************************************/int BitFileGetBit(bit_file_t *stream){    int returnValue;    if (stream == NULL)    {        return(EOF);    }    if (stream->bitCount == 0)    {        /* buffer is empty, read another character */        if ((returnValue = fgetc(stream->fp)) == EOF)        {            return EOF;        }        else        {            stream->bitCount = 8;            stream->bitBuffer = returnValue;        }    }    /* bit to return is msb in buffer */    stream->bitCount--;    returnValue = (stream->bitBuffer) >> (stream->bitCount);    return (returnValue & 0x01);}/****************************************************************************   Function   : BitFilePutBit*   Description: This function writes the bit passed as a parameter to the*                file passed a parameter.*   Parameters : c - the bit value to be written*                stream - pointer to bit file stream to write to*   Effects    : Writes a bit to the bit buffer.  If the buffer has a byte,*                the buffer is written to the file and cleared.*   Returned   : On success, the bit value written, otherwise EOF.***************************************************************************/int BitFilePutBit(const int c, bit_file_t *stream){    int returnValue = c;    if (stream == NULL)    {        return(EOF);    }    stream->bitCount++;    stream->bitBuffer <<= 1;    if (c != 0)    {        stream->bitBuffer |= 1;    }    /* write bit buffer if we have 8 bits */    if (stream->bitCount == 8)    {        if (fputc(stream->bitBuffer, stream->fp) == EOF)        {            returnValue = EOF;        }        /* reset buffer */        stream->bitCount = 0;        stream->bitBuffer = 0;    }    return returnValue;}/****************************************************************************   Function   : BitFileGetBits*   Description: This function reads the specified number of bits from the*                file passed as a parameter and writes them to the*                requested memory location (msb to lsb).*   Parameters : stream - pointer to bit file stream to read from*                bits - address to store bits read*                count - number of bits to read*   Effects    : Reads bits from the bit buffer and file stream.  The bit*                buffer will be modified as necessary.*   Returned   : EOF for failure, otherwise the number of bits read.  If*                an EOF is reached before all the bits are read, bits*                will contain every bit through the last complete byte.***************************************************************************/int BitFileGetBits(bit_file_t *stream, void *bits, const unsigned int count){    unsigned char *bytes, shifts;    int offset, remaining, returnValue;    bytes = (unsigned char *)bits;    if ((stream == NULL) || (bits == NULL))    {        return(EOF);    }    offset = 0;    remaining = count;    /* read whole bytes */    while (remaining >= 8)    {        returnValue = BitFileGetChar(stream);        if (returnValue == EOF)        {            return EOF;        }        bytes[offset] = (unsigned char)returnValue;        remaining -= 8;        offset++;    }    if (remaining != 0)    {        /* read remaining bits */        shifts = 8 - remaining;        while (remaining > 0)        {            returnValue = BitFileGetBit(stream);            if (returnValue == EOF)            {                return EOF;            }            bytes[offset] <<= 1;            bytes[offset] |= (returnValue & 0x01);            remaining--;        }        /* shift last bits into position */        bytes[offset] <<= shifts;    }    return count;}/****************************************************************************   Function   : BitFilePutBits*   Description: This function writes the specified number of bits from the*                memory location passed as a parameter to the file passed*                as a parameter.   Bits are written msb to lsb.*   Parameters : stream - pointer to bit file stream to write to*                bits - pointer to bits to write*                count - number of bits to write*   Effects    : Writes bits to the bit buffer and file stream.  The bit*                buffer will be modified as necessary.*   Returned   : EOF for failure, otherwise the number of bits written.  If*                an error occurs after a partial write, the partially*                written bits will not be unwritten.***************************************************************************/int BitFilePutBits(bit_file_t *stream, void *bits, const unsigned int count){    unsigned char *bytes, tmp;    int offset, remaining, returnValue;    bytes = (unsigned char *)bits;    if ((stream == NULL) || (bits == NULL))    {        return(EOF);    }    offset = 0;    remaining = count;    /* write whole bytes */    while (remaining >= 8)    {        returnValue = BitFilePutChar(bytes[offset], stream);        if (returnValue == EOF)        {            return EOF;        }        remaining -= 8;        offset++;    }    if (remaining != 0)    {        /* write remaining bits */        tmp = bytes[offset];        while (remaining > 0)        {            returnValue = BitFilePutBit((tmp & 0x80), stream);            if (returnValue == EOF)            {                return EOF;            }            tmp <<= 1;            remaining--;        }    }    return count;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -