initialize validbounds on ::init, log_debug sizes
[gnash.git] / libbase / BitsReader.cpp
blob69b8bed5d6539d24cfa35a540ee450919a3d3b31
1 // BitsReader.cpp: bits reader, for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // Foundation, Inc
5 //
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.
10 //
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.
15 //
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"
23 #include "log.h"
25 namespace gnash {
27 bool BitsReader::read_bit()
29 bool ret = (*ptr&(128>>usedBits));
30 if ( ++usedBits == 8 ) advanceToNextByte();
31 return ret;
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);
50 advanceToNextByte();
51 break;
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);
61 advanceToNextByte();
63 else
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();
76 // We're done.
77 break;
80 while (bits_needed > 0);
82 //std::cerr << "Returning value: " << value << " unused bits: " << (int)m_unused_bits << std::endl;
83 return value;
88 boost::int32_t BitsReader::read_sint(unsigned short bitcount)
90 boost::int32_t value = boost::int32_t(read_uint(bitcount));
92 // Sign extend...
93 if (value & (1 << (bitcount - 1)))
94 value |= -1 << bitcount;
96 return value;
100 } // end namespace gnash