29 #define DEFAULT_BUFSIZE (128)
36 if(bstream == NULL)
return NULL;
40 if(bstream->
data == NULL) {
50 BitStream *BitStream_newWithBits(
size_t size,
unsigned char *bits)
57 if(bstream == NULL)
return NULL;
60 if(bstream->
data == NULL) {
67 memcpy(bstream->
data, bits, size);
77 data = (
unsigned char *)realloc(bstream->
data, bstream->
datasize * 2);
95 mask = 1U << (bits - 1);
96 for(i = 0; i < bits; i++) {
114 for(i = 0; i <
size; i++) {
116 for(j = 0; j < 8; j++) {
141 if(ret < 0)
return ret;
154 if(bits == 0)
return 0;
158 if(ret < 0)
return ret;
170 if(size == 0)
return 0;
174 if(ret < 0)
return ret;
177 bstream->
length += size * 8;
184 size_t i, j,
size, bytes, oddbits;
185 unsigned char *data, v;
192 data = (
unsigned char *)
malloc((size + 7) / 8);
200 for(i = 0; i < bytes; i++) {
202 for(j = 0; j < 8; j++) {
203 v = (
unsigned char)(v << 1);
212 for(j = 0; j < oddbits; j++) {
213 v = (
unsigned char)(v << 1);
217 data[bytes] = (
unsigned char)(v << (8 - oddbits));
225 if(bstream != NULL) {
static int BitStream_expand(BitStream *bstream)
int BitStream_append(BitStream *bstream, BitStream *arg)
int BitStream_appendBytes(BitStream *bstream, size_t size, unsigned char *data)
unsigned char * BitStream_toByte(BitStream *bstream)
void BitStream_free(BitStream *bstream)
static void BitStream_writeNum(unsigned char *dest, size_t bits, unsigned int num)
#define BitStream_size(__bstream__)
BitStream * BitStream_new(void)
static void BitStream_writeBytes(unsigned char *dest, size_t size, unsigned char *data)
int BitStream_appendNum(BitStream *bstream, size_t bits, unsigned int num)