31#define DEFAULT_BUFSIZE (128)
38 if(bstream == NULL)
return NULL;
42 if(bstream->
data == NULL) {
52BitStream *BitStream_newWithBits(
size_t size,
unsigned char *bits)
59 if(bstream == NULL)
return NULL;
61 bstream->
data = (
unsigned char *)malloc(
size);
62 if(bstream->
data == NULL) {
79 data = (
unsigned char *)realloc(bstream->
data, bstream->
datasize * 2);
97 mask = 1U << (bits - 1);
98 for(i = 0; i < bits; i++) {
116 for(i = 0; i <
size; i++) {
118 for(j = 0; j < 8; j++) {
143 if(ret < 0)
return ret;
156 if(bits == 0)
return 0;
160 if(ret < 0)
return ret;
172 if(
size == 0)
return 0;
176 if(ret < 0)
return ret;
186 size_t i, j,
size, bytes, oddbits;
187 unsigned char *data, v;
194 data = (
unsigned char *)malloc((
size + 7) / 8);
202 for(i = 0; i < bytes; i++) {
204 for(j = 0; j < 8; j++) {
205 v = (
unsigned char)(v << 1);
214 for(j = 0; j < oddbits; j++) {
215 v = (
unsigned char)(v << 1);
219 data[bytes] = (
unsigned char)(v << (8 - oddbits));
227 if(bstream != NULL) {
static void BitStream_writeNum(unsigned char *dest, size_t bits, unsigned int num)
void BitStream_free(BitStream *bstream)
BitStream * BitStream_new(void)
static int BitStream_expand(BitStream *bstream)
unsigned char * BitStream_toByte(BitStream *bstream)
int BitStream_appendNum(BitStream *bstream, size_t bits, unsigned int num)
static void BitStream_writeBytes(unsigned char *dest, size_t size, unsigned char *data)
int BitStream_append(BitStream *bstream, BitStream *arg)
int BitStream_appendBytes(BitStream *bstream, size_t size, unsigned char *data)
#define BitStream_size(__bstream__)