10 #ifndef MSPACK_READBITS_H
11 #define MSPACK_READBITS_H 1
82 # error "define BITS_VAR as the state structure poiner variable name"
85 # error "define BITS_TYPE as the state structure type"
87 #if defined(BITS_ORDER_MSB) && defined(BITS_ORDER_LSB)
88 # error "you must define either BITS_ORDER_MSB or BITS_ORDER_LSB"
90 # if !(defined(BITS_ORDER_MSB) || defined(BITS_ORDER_LSB))
91 # error "you must define BITS_ORDER_MSB or BITS_ORDER_LSB"
101 #define BITBUF_WIDTH (sizeof(bit_buffer) * CHAR_BIT)
103 #define INIT_BITS do { \
104 BITS_VAR->i_ptr = &BITS_VAR->inbuf[0]; \
105 BITS_VAR->i_end = &BITS_VAR->inbuf[0]; \
106 BITS_VAR->bit_buffer = 0; \
107 BITS_VAR->bits_left = 0; \
108 BITS_VAR->input_end = 0; \
111 #define STORE_BITS do { \
112 BITS_VAR->i_ptr = i_ptr; \
113 BITS_VAR->i_end = i_end; \
114 BITS_VAR->bit_buffer = bit_buffer; \
115 BITS_VAR->bits_left = bits_left; \
118 #define RESTORE_BITS do { \
119 i_ptr = BITS_VAR->i_ptr; \
120 i_end = BITS_VAR->i_end; \
121 bit_buffer = BITS_VAR->bit_buffer; \
122 bits_left = BITS_VAR->bits_left; \
125 #define ENSURE_BITS(nbits) do { \
126 while (bits_left < (nbits)) READ_BYTES; \
129 #define READ_BITS(val, nbits) do { \
130 ENSURE_BITS(nbits); \
131 (val) = PEEK_BITS(nbits); \
132 REMOVE_BITS(nbits); \
135 #define READ_MANY_BITS(val, bits) do { \
136 unsigned char needed = (bits), bitrun; \
138 while (needed > 0) { \
139 if (bits_left <= (BITBUF_WIDTH - 16)) READ_BYTES; \
140 bitrun = (bits_left < needed) ? bits_left : needed; \
141 (val) = ((val) << bitrun) | PEEK_BITS(bitrun); \
142 REMOVE_BITS(bitrun); \
147 #ifdef BITS_ORDER_MSB
148 # define PEEK_BITS(nbits) (bit_buffer >> (BITBUF_WIDTH - (nbits)))
149 # define REMOVE_BITS(nbits) ((bit_buffer <<= (nbits)), (bits_left -= (nbits)))
150 # define INJECT_BITS(bitdata,nbits) ((bit_buffer |= \
151 (bitdata) << (BITBUF_WIDTH - (nbits) - bits_left)), (bits_left += (nbits)))
153 # define PEEK_BITS(nbits) (bit_buffer & ((1 << (nbits))-1))
154 # define REMOVE_BITS(nbits) ((bit_buffer >>= (nbits)), (bits_left -= (nbits)))
155 # define INJECT_BITS(bitdata,nbits) ((bit_buffer |= \
156 (bitdata) << bits_left), (bits_left += (nbits)))
159 #ifdef BITS_LSB_TABLE
161 static const unsigned short lsb_bit_mask[17] = {
162 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
163 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
165 # define PEEK_BITS_T(nbits) (bit_buffer & lsb_bit_mask[(nbits)])
166 # define READ_BITS_T(val, nbits) do { \
167 ENSURE_BITS(nbits); \
168 (val) = PEEK_BITS_T(nbits); \
169 REMOVE_BITS(nbits); \
173 #ifndef BITS_NO_READ_INPUT
174 # define READ_IF_NEEDED do { \
175 if (i_ptr >= i_end) { \
176 if (read_input(BITS_VAR)) \
177 return BITS_VAR->error; \
178 i_ptr = BITS_VAR->i_ptr; \
179 i_end = BITS_VAR->i_end; \
184 int read =
p->sys->read(
p->input, &
p->inbuf[0], (
int)
p->inbuf_size);
191 D((
"out of input bytes"))
196 p->inbuf[0] =
p->inbuf[1] = 0;
202 p->i_ptr = &
p->inbuf[0];
203 p->i_end = &
p->inbuf[
read];
int read(izstream &zs, T *x, Items items)