Last active
August 29, 2015 14:19
-
-
Save bellini666/366ccf1cead1ba14049f to your computer and use it in GitHub Desktop.
c-bit-buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (C) 2015 Thiago Bellini <[email protected]> | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include <limits.h> | |
#include <stdio.h> | |
#define BYTE_BIT CHAR_BIT | |
typedef char byte; | |
typedef struct BIT_BUFFER | |
{ | |
FILE *fp; | |
int pos; | |
byte bits; | |
int first_read; | |
} BIT_BUFFER; | |
/* | |
* Initialize the bit buffer. | |
*/ | |
void | |
bb_init (struct BIT_BUFFER *buffer, FILE *fp) | |
{ | |
buffer->pos = 0; | |
buffer->bits = 0; | |
buffer->first_read = 0; | |
buffer->fp = fp; | |
} | |
/* | |
* Flush bit buffer to file. | |
*/ | |
void | |
bb_flush (struct BIT_BUFFER *buffer) | |
{ | |
if (buffer->pos == 0) | |
return; | |
/* Fill the rest of the byte with 0s */ | |
while (buffer->pos < BYTE_BIT) | |
{ | |
buffer->bits &= ~(1 << (BYTE_BIT - buffer->pos - 1)); | |
(buffer->pos)++; | |
} | |
fwrite (&(buffer->bits), sizeof (byte), 1, buffer->fp); | |
buffer->pos = 0; | |
buffer->bits = 0; | |
} | |
/* | |
* Write bits to file. | |
*/ | |
void | |
bb_write (struct BIT_BUFFER *buffer, int *bits, int size) | |
{ | |
int i, shift_pos; | |
for (i = 0; i < size; i++) | |
{ | |
if (buffer->pos == BYTE_BIT) | |
bb_flush (buffer); | |
shift_pos = BYTE_BIT - buffer->pos - 1; | |
if (bits[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. | |
*/ | |
int * | |
bb_read (struct BIT_BUFFER *buffer, int size) | |
{ | |
int i, shift_pos; | |
int *bin; | |
bin = malloc (size * sizeof (int)); | |
for (i = 0; i < size; 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[i] = (buffer->bits >> shift_pos) & 1; | |
(buffer->pos)++; | |
} | |
return bin; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment