Commit cbe7ae83 by Rudolf

Extend bit-buffer library with writing total byte

This is useful to write out a full byte. Note this cannot be done using simple
fwrite, as we need to consider the bit cursor.
parent f55b32e2
Showing with 24 additions and 0 deletions
......@@ -78,6 +78,29 @@ bb_write (struct BIT_BUFFER *buffer, int *bits, int size)
}
/*
* Write byte to file, taking bit pos into account.
*/
void
bb_writebyte (struct BIT_BUFFER *buffer, byte byte)
{
int i, shift_pos;
printf("-- %x\n", byte);
for (i = 0; i < BYTE_BIT; i++)
{
if (buffer->pos == BYTE_BIT)
bb_flush (buffer);
shift_pos = BYTE_BIT - buffer->pos - 1;
if (byte & (1 << (BYTE_BIT - 1 - i)))
buffer->bits |= (1 << shift_pos);
else
buffer->bits &= ~(1 << shift_pos);
(buffer->pos)++;
}
}
/*
* Read bits from file.
*
* The return value must be freed after being used.
......
......@@ -14,4 +14,5 @@ typedef struct BIT_BUFFER
void bb_init (struct BIT_BUFFER *buffer, FILE *fp);
void bb_flush (struct BIT_BUFFER *buffer);
void bb_write (struct BIT_BUFFER *buffer, int *bits, int size);
void bb_writebyte (struct BIT_BUFFER *buffer, byte byte);
int *bb_read (struct BIT_BUFFER *buffer, int size);
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