📄 bitfile.c
字号:
* Effects : Calls a function that reads bits from the bit buffer and* file stream. The bit buffer will be modified as necessary.* the bits will be written to "bits" from least significant* byte to most significant byte.* Returned : EOF for failure, otherwise the number of bits read by the* called function.***************************************************************************/int BitFileGetBitsInt(bit_file_t *stream, void *bits, const unsigned int count, const size_t size){ int returnValue; if ((stream == NULL) || (bits == NULL)) { return(EOF); } if (stream->endian == BF_LITTLE_ENDIAN) { returnValue = BitFileGetBitsLE(stream, bits, count); } else if (stream->endian == BF_BIG_ENDIAN) { returnValue = BitFileGetBitsBE(stream, bits, count, size); } else { returnValue = EOF; } return returnValue;}/**************************************************************************** Function : BitFileGetBitsLE (Little Endian)* Description: This function reads the specified number of bits from the* file passed as a parameter and writes them to the* requested memory location (LSB to MSB).* 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. bits is treated as* a little endian integer of length >= (count/8) + 1.* 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 sucessful read.***************************************************************************/int BitFileGetBitsLE(bit_file_t *stream, void *bits, const unsigned int count){ unsigned char *bytes, shifts; int offset, remaining, returnValue; bytes = (unsigned char *)bits; 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--; } } return count;}/**************************************************************************** Function : BitFileGetBitsBE (Big Endian)* Description: This function reads the specified number of bits from the* file passed as a parameter and writes them to the* requested memory location (LSB to MSB).* Parameters : stream - pointer to bit file stream to read from* bits - address to store bits read* count - number of bits to read* size - sizeof type containing "bits"* Effects : Reads bits from the bit buffer and file stream. The bit* buffer will be modified as necessary. bits is treated as* a big endian integer of length size.* 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 sucessful read.***************************************************************************/int BitFileGetBitsBE(bit_file_t *stream, void *bits, const unsigned int count, const size_t size){ unsigned char *bytes, shifts; int offset, remaining, returnValue; if (count > (size * 8)) { /* too many bits to read */ return EOF; } bytes = (unsigned char *)bits; offset = size - 1; 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--; } } return count;}/**************************************************************************** Function : BitFilePutBitsInt* Description: This function provides a machine independant layer that* allows a single function call to write an arbitrary number* of bits from an integer type variable into a file.* Parameters : stream - pointer to bit file stream to write to* bits - pointer to bits to write* count - number of bits to write* size - sizeof type containing "bits"* Effects : Calls a function that writes bits to the bit buffer and* file stream. The bit buffer will be modified as necessary.* the bits will be written to the file stream from least* significant byte to most significant byte.* 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 BitFilePutBitsInt(bit_file_t *stream, void *bits, const unsigned int count, const size_t size){ int returnValue; if ((stream == NULL) || (bits == NULL)) { return(EOF); } if (stream->endian == BF_LITTLE_ENDIAN) { returnValue = BitFilePutBitsLE(stream, bits, count); } else if (stream->endian == BF_BIG_ENDIAN) { returnValue = BitFilePutBitsBE(stream, bits, count, size); } else { returnValue = EOF; } return returnValue;}/**************************************************************************** Function : BitFilePutBitsLE (Little Endian)* 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 LSB to MSB.* 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. bits is treated as* a little endian integer of length >= (count/8) + 1.* 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 BitFilePutBitsLE(bit_file_t *stream, void *bits, const unsigned int count){ unsigned char *bytes, tmp; int offset, remaining, returnValue; bytes = (unsigned char *)bits; 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]; tmp <<= (8 - remaining); while (remaining > 0) { returnValue = BitFilePutBit((tmp & 0x80), stream); if (returnValue == EOF) { return EOF; } tmp <<= 1; remaining--; } } return count;}/**************************************************************************** Function : BitFilePutBitsBE (Big Endian)* 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 LSB to MSB.* 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. bits is treated as* a big endian integer of length size.* 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 BitFilePutBitsBE(bit_file_t *stream, void *bits, const unsigned int count, const size_t size){ unsigned char *bytes, tmp; int offset, remaining, returnValue; if (count > (size * 8)) { /* too many bits to write */ return EOF; } bytes = (unsigned char *)bits; offset = size - 1; 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]; tmp <<= (8 - remaining); 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 + -