2323#include <stdlib.h>
2424#include <string.h>
2525#include <inttypes.h>
26+ #include <assert.h>
2627
2728#include "dvdread/bitreader.h"
2829
29- int dvdread_getbits_init (getbits_state_t * state , uint8_t * start ) {
30+ int dvdread_getbits_init (getbits_state_t * state , const uint8_t * start ) {
3031 if ((state == NULL ) || (start == NULL )) return 0 ;
3132 state -> start = start ;
3233 state -> bit_position = 0 ;
3334 state -> byte_position = 0 ;
34- state -> byte = start [0 ];
3535 return 1 ;
3636}
3737
@@ -47,37 +47,32 @@ uint32_t dvdread_getbits(getbits_state_t *state, uint32_t number_of_bits) {
4747
4848 if ((state -> bit_position ) > 0 ) { /* Last getbits left us in the middle of a byte. */
4949 if (number_of_bits > (8 - state -> bit_position )) { /* this getbits will span 2 or more bytes. */
50- byte = state -> byte ;
50+ byte = state -> start [ state -> byte_position ] << state -> bit_position ;
5151 byte = byte >> (state -> bit_position );
5252 result = byte ;
5353 number_of_bits -= (8 - state -> bit_position );
5454 state -> bit_position = 0 ;
5555 state -> byte_position ++ ;
56- state -> byte = state -> start [state -> byte_position ];
5756 } else {
58- byte = state -> byte ;
59- state -> byte = state -> byte << number_of_bits ;
57+ byte = state -> start [state -> byte_position ] << state -> bit_position ;
6058 byte = byte >> (8 - number_of_bits );
6159 result = byte ;
6260 state -> bit_position += number_of_bits ; /* Here it is impossible for bit_position > 8 */
6361 if (state -> bit_position == 8 ) {
6462 state -> bit_position = 0 ;
6563 state -> byte_position ++ ;
66- state -> byte = state -> start [state -> byte_position ];
6764 }
6865 number_of_bits = 0 ;
6966 }
7067 }
7168 if ((state -> bit_position ) == 0 ) {
7269 while (number_of_bits > 7 ) {
73- result = (result << 8 ) + state -> byte ;
70+ result = (result << 8 ) + state -> start [ state -> byte_position ] ;
7471 state -> byte_position ++ ;
75- state -> byte = state -> start [state -> byte_position ];
7672 number_of_bits -= 8 ;
7773 }
7874 if (number_of_bits > 0 ) { /* number_of_bits < 8 */
79- byte = state -> byte ;
80- state -> byte = state -> byte << number_of_bits ;
75+ byte = state -> start [state -> byte_position ] << state -> bit_position ;
8176 state -> bit_position += number_of_bits ; /* Here it is impossible for bit_position > 7 */
8277 byte = byte >> (8 - number_of_bits );
8378 result = (result << number_of_bits ) + byte ;
@@ -115,3 +110,54 @@ uint32_t dvdread_get32bits(getbits_state_t *state) {
115110}
116111
117112#endif
113+
114+ #ifdef BITREADER_TESTS
115+
116+ int main ()
117+ {
118+ uint8_t buff [2 ] = {
119+ 0x6E , 0xC2
120+ // 0b 01101110 11000010
121+ };
122+ getbits_state_t state ;
123+ dvdread_getbits_init (& state , buff );
124+
125+ uint32_t bits = dvdread_getbits (& state , 3 );
126+ assert (bits == 3 );
127+
128+ bits = dvdread_getbits (& state , 3 );
129+ assert (bits == 3 );
130+
131+ bits = dvdread_getbits (& state , 4 );
132+ assert (bits == 11 );
133+
134+ bits = dvdread_getbits (& state , 6 );
135+ assert (bits == 2 );
136+
137+ dvdread_getbits_init (& state , buff );
138+ bits = dvdread_getbits (& state , 10 );
139+ assert (bits == 443 );
140+
141+ bits = dvdread_getbits (& state , 6 );
142+ assert (bits == 2 );
143+
144+ dvdread_getbits_init (& state , buff );
145+ bits = dvdread_getbits (& state , 16 );
146+ assert (bits == 28354 );
147+
148+ buff [0 ] = buff [1 ] = 0xFF ;
149+ dvdread_getbits_init (& state , buff );
150+ bits = dvdread_getbits (& state , 16 );
151+ assert (bits == 0xFFFF );
152+
153+ uint8_t large [5 ] = { 0xFF , 0xFF , 0xFF , 0xFF , 0xFF };
154+ dvdread_getbits_init (& state , large );
155+ bits = dvdread_getbits (& state , 8 );
156+ assert (bits == 0xFF );
157+ bits = dvdread_getbits (& state , 32 );
158+ assert (bits == 0xFFFFFFFF );
159+
160+ return 0 ;
161+ }
162+
163+ #endif
0 commit comments