1 // BitsReader.cpp: bits reader, for Gnash.
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "BitsReader.h"
27 bool BitsReader::read_bit()
29 bool ret
= (*ptr
&(128>>usedBits
));
30 if ( ++usedBits
== 8 ) advanceToNextByte();
34 unsigned BitsReader::read_uint(unsigned short bitcount
)
36 assert(bitcount
<= 32);
38 boost::uint32_t value
= 0;
40 unsigned short bits_needed
= bitcount
;
43 int unusedMask
= 0xFF >> usedBits
;
44 int unusedBits
= 8-usedBits
;
46 if (bits_needed
== unusedBits
)
48 // Consume all the unused bits.
49 value
|= (*ptr
&unusedMask
);
54 else if (bits_needed
> unusedBits
)
56 // Consume all the unused bits.
58 bits_needed
-= unusedBits
; // assert(bits_needed>0)
60 value
|= ((*ptr
&unusedMask
) << bits_needed
);
65 assert(bits_needed
<= unusedBits
);
67 // Consume some of the unused bits.
69 unusedBits
-= bits_needed
;
71 value
|= ((*ptr
&unusedMask
) >> unusedBits
);
73 usedBits
+= bits_needed
;
74 if ( usedBits
>= 8 ) advanceToNextByte();
80 while (bits_needed
> 0);
82 //std::cerr << "Returning value: " << value << " unused bits: " << (int)m_unused_bits << std::endl;
88 boost::int32_t BitsReader::read_sint(unsigned short bitcount
)
90 boost::int32_t value
= boost::int32_t(read_uint(bitcount
));
93 if (value
& (1 << (bitcount
- 1)))
94 value
|= -1 << bitcount
;
100 } // end namespace gnash