Commit 1df42cd1 by Rudolf

Extend bit-buffer library with reading total byte

This is useful to read in a full byte. Note this cannot be done using simple
fread, as we need to consider the bit cursor.
parent cbe7ae83
Showing with 34 additions and 0 deletions
...@@ -132,3 +132,36 @@ bb_read (struct BIT_BUFFER *buffer, int size) ...@@ -132,3 +132,36 @@ bb_read (struct BIT_BUFFER *buffer, int size)
return bin; return bin;
} }
/*
* Read byte from file, taking bit pos into account.
*
* The return value must be freed after being used.
*/
int *
bb_readbyte (struct BIT_BUFFER *buffer)
{
int i, shift_pos;
int *bin = malloc(sizeof(int));
*bin = 0;
for (i = 0; i < BYTE_BIT; i++)
{
if (!buffer->first_read || buffer->pos == BYTE_BIT)
{
/* When we reach EOF, that mens any read bit was just fillers from
the bb_flush function. We actually don't have any more data */
if (!(fread (&(buffer->bits), sizeof (byte), 1, buffer->fp)))
return NULL;
buffer->first_read = 1;
buffer->pos = 0;
}
shift_pos = BYTE_BIT - buffer->pos - 1;
*bin |= ((buffer->bits >> shift_pos) & 1) << (BYTE_BIT - 1 - i);
(buffer->pos)++;
}
return bin;
}
...@@ -16,3 +16,4 @@ void bb_flush (struct BIT_BUFFER *buffer); ...@@ -16,3 +16,4 @@ void bb_flush (struct BIT_BUFFER *buffer);
void bb_write (struct BIT_BUFFER *buffer, int *bits, int size); void bb_write (struct BIT_BUFFER *buffer, int *bits, int size);
void bb_writebyte (struct BIT_BUFFER *buffer, byte byte); void bb_writebyte (struct BIT_BUFFER *buffer, byte byte);
int *bb_read (struct BIT_BUFFER *buffer, int size); int *bb_read (struct BIT_BUFFER *buffer, int size);
int *bb_readbyte (struct BIT_BUFFER *buffer);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment