1 // BitsReader.h: 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
30 #include <boost/cstdint.hpp> // for boost::uint32_t used in this file
34 /// BitsReader is used to encapsulate bit-packed buffer reads.
36 /// TODO: implement boundary checking, maybe by throwing an exception
37 /// when attempts are made to read past end of buffer
39 class DSOEXPORT BitsReader
42 typedef unsigned char byte
;
44 /// Ownership of buffer left to caller
45 BitsReader(const byte
* input
, size_t len
)
54 /// Create a BitsReader reading a subset of another
56 /// The starting pointer will be taken from the parent
57 /// reader cursor, including used bits, use align() if
58 /// need to discard the left over ones.
60 /// length will be given explicitly.
62 BitsReader(const BitsReader
& from
, size_t len
)
67 usedBits(from
.usedBits
)
78 /// Set a new buffer to work with
79 void setBuffer(byte
* input
, size_t len
)
87 /// Reads a bit-packed unsigned integer from the stream
88 /// and returns it. The given bitcount determines the
89 /// number of bits to read.
90 unsigned read_uint(unsigned short bitcount
);
93 /// Reads a single bit off the stream
98 /// Reads a bit-packed little-endian signed integer
99 /// from the stream. The given bitcount determines the
100 /// number of bits to read.
101 boost::int32_t read_sint(unsigned short bitcount
);
103 /// Read a byte as an unsigned int (aligned)
104 boost::uint8_t read_u8()
110 /// Read one bytes as a signed int (aligned)
111 boost::int8_t read_s8()
113 return static_cast<boost::int8_t>(read_u8());
116 /// Read two bytes as an unsigned int (aligned)
117 boost::uint16_t read_u16()
121 boost::uint16_t result
= *ptr
++;
122 result
|= *ptr
++ << 8;
126 /// Read two bytes as a signed int (aligned)
127 boost::int16_t read_s16()
129 return static_cast<boost::int16_t>(read_u16());
132 /// Read four bytes as an unsigned int (aligned)
133 boost::uint32_t read_u32()
137 boost::uint32_t result
= *ptr
++;
138 result
|= *ptr
++ << 8;
139 result
|= *ptr
++ << 16;
140 result
|= *ptr
++ << 24;
144 /// Read four bytes as an signed int (aligned)
145 boost::int32_t read_s32()
147 return static_cast<boost::int32_t>(read_u32());
151 /// Discard any unused bit in the current byte
152 /// and advance to next
155 if ( usedBits
) advanceToNextByte();
158 /// Checks if the stream contains X bits
159 bool gotBits(boost::uint32_t nbits
)
161 boost::uint32_t gotbits
= 8-usedBits
+8*(end
-ptr
-1);
162 if (gotbits
> nbits
) return true;
168 void advanceToNextByte()
172 log_debug("Going round");
178 /// Pointer to first byte
181 /// Pointer to current byte
184 /// Pointer to one past last byte
187 /// Number of used bits in current byte
193 } // end namespace gnash
196 #endif // BITSREADER_H
203 // indent-tabs-mode: t