@@ -165,21 +165,38 @@ int exi_bitstream_write_bits(exi_bitstream_t* stream, size_t bit_count, uint32_t
165165 return EXI_ERROR__BIT_COUNT_LARGER_THAN_TYPE_SIZE;
166166 }
167167
168- int error = EXI_ERROR__NO_ERROR;
168+ const size_t remaining_bits = 8 - stream->bit_count;
169+ if (remaining_bits >= bit_count)
170+ {
171+ stream->data[stream->byte_pos] |= (uint8_t)((value << (remaining_bits - bit_count)));
172+ stream->bit_count += bit_count;
173+ return EXI_ERROR__NO_ERROR;
174+ }
175+ stream->data[stream->byte_pos] |=
176+ ((uint8_t)(value >> (bit_count - remaining_bits)) & (0xff >> stream->bit_count));
169177
170- for (size_t n = 0; n < bit_count; n++)
178+ bit_count = (bit_count - (8 - stream->bit_count));
179+ if ((stream->byte_pos) >= stream->data_size)
171180 {
172- uint8_t bit;
173- bit = (value & (1u << (bit_count - n - 1))) > 0;
181+ return EXI_ERROR__BYTE_BUFFER_TOO_SMALL;
182+ }
183+ stream->byte_pos++;
184+ stream->data[stream->byte_pos] = 0;
174185
175- error = exi_bitstream_write_bit(stream, bit);
176- if (error != EXI_ERROR__NO_ERROR)
186+ while (bit_count >= EXI_BITSTREAM_MAX_BIT_COUNT)
187+ {
188+ bit_count = (bit_count - EXI_BITSTREAM_MAX_BIT_COUNT);
189+ if ((stream->byte_pos) >= stream->data_size)
177190 {
178- break ;
191+ return EXI_ERROR__BYTE_BUFFER_TOO_SMALL ;
179192 }
193+ stream->data[(stream->byte_pos)++] = (uint8_t)(value >> (bit_count));
180194 }
181195
182- return error;
196+ stream->data[stream->byte_pos] = (uint8_t)value << (8 - bit_count);
197+ stream->bit_count = bit_count;
198+
199+ return EXI_ERROR__NO_ERROR;
183200}
184201
185202int exi_bitstream_write_octet(exi_bitstream_t* stream, uint8_t value)
@@ -196,27 +213,45 @@ int exi_bitstream_read_bits(exi_bitstream_t* stream, size_t bit_count, uint32_t*
196213 return EXI_ERROR__BIT_COUNT_LARGER_THAN_TYPE_SIZE;
197214 }
198215
199- int error = EXI_ERROR__NO_ERROR;
216+ const size_t remaining_bits = 8 - stream->bit_count;
217+ if (remaining_bits >= bit_count)
218+ {
219+ *value = (uint32_t)((stream->data[stream->byte_pos] >> (remaining_bits - bit_count)) & ((1 << bit_count) - 1));
220+ stream->bit_count += bit_count;
221+ return EXI_ERROR__NO_ERROR;
222+ }
223+ *value = (uint32_t)(stream->data[stream->byte_pos] & ((1 << remaining_bits) - 1));
224+ bit_count = (bit_count - remaining_bits);
225+ stream->bit_count = 8;
226+ if (stream->byte_pos == stream->data_size)
227+ {
228+ return EXI_ERROR__BITSTREAM_OVERFLOW;
229+ }
230+ stream->byte_pos++;
231+ stream->bit_count = 0;
200232
201- for (size_t n = 0; n < bit_count; n++ )
233+ while (bit_count >= 8 )
202234 {
203- uint8_t bit ;
204- error = exi_bitstream_read_bit(stream, &bit) ;
205- if (error != EXI_ERROR__NO_ERROR )
235+ *value = (*value << 8) | stream->data[stream->byte_pos] ;
236+ bit_count -= 8 ;
237+ if (stream->byte_pos >= stream->data_size )
206238 {
207- break ;
239+ return EXI_ERROR__BITSTREAM_OVERFLOW ;
208240 }
209-
210- *value = (*value << 1u) | bit;
241+ stream->byte_pos++;
211242 }
243+
244+ *value = (*value << bit_count) | (stream->data[stream->byte_pos] >> (8 - bit_count));
245+ stream->bit_count = bit_count;
212246{% - if add_debug_code == 1 %}
213247
214248 if (stream->status_callback)
215249 {
216250 stream->status_callback(EXI_DEBUG__BITSTREAM_READ_BITS, 0, bit_count, *value);
217251 }
218252{% endif %}
219- return error;
253+
254+ return EXI_ERROR__NO_ERROR;
220255}
221256
222257int exi_bitstream_read_octet(exi_bitstream_t* stream, uint8_t* value)
0 commit comments