Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef BITSREADER_H
00023 #define BITSREADER_H
00024
00025 #include "dsodefs.h"
00026 #include "log.h"
00027
00028 #include <sstream>
00029 #include <cassert>
00030 #include <boost/cstdint.hpp>
00031
00032 namespace gnash {
00033
00035
00039 class DSOEXPORT BitsReader
00040 {
00041 public:
00042 typedef unsigned char byte;
00043
00045 BitsReader(const byte* input, size_t len)
00046 :
00047 start(input),
00048 ptr(start),
00049 end(start+len),
00050 usedBits(0)
00051 {
00052 }
00053
00055
00062 BitsReader(const BitsReader& from, size_t len)
00063 :
00064 start(from.ptr),
00065 ptr(start),
00066 end(start+len),
00067 usedBits(from.usedBits)
00068 {
00069 }
00070
00071 ~BitsReader() {}
00072
00073 size_t size() const
00074 {
00075 return end-start;
00076 }
00077
00079 void setBuffer(byte* input, size_t len)
00080 {
00081 start = ptr = input;
00082 end = start+len;
00083 usedBits = 0;
00084 }
00085
00090 unsigned read_uint(unsigned short bitcount);
00091
00095 bool read_bit();
00096
00101 boost::int32_t read_sint(unsigned short bitcount);
00102
00104 boost::uint8_t read_u8()
00105 {
00106 align();
00107 return *ptr++;
00108 }
00109
00111 boost::int8_t read_s8()
00112 {
00113 return static_cast<boost::int8_t>(read_u8());
00114 }
00115
00117 boost::uint16_t read_u16()
00118 {
00119 align();
00120 assert(ptr+2 < end);
00121 boost::uint16_t result = *ptr++;
00122 result |= *ptr++ << 8;
00123 return result ;
00124 }
00125
00127 boost::int16_t read_s16()
00128 {
00129 return static_cast<boost::int16_t>(read_u16());
00130 }
00131
00133 boost::uint32_t read_u32()
00134 {
00135 align();
00136 assert(ptr+4 < end);
00137 boost::uint32_t result = *ptr++;
00138 result |= *ptr++ << 8;
00139 result |= *ptr++ << 16;
00140 result |= *ptr++ << 24;
00141 return(result);
00142 }
00143
00145 boost::int32_t read_s32()
00146 {
00147 return static_cast<boost::int32_t>(read_u32());
00148 }
00149
00153 void align()
00154 {
00155 if ( usedBits ) advanceToNextByte();
00156 }
00157
00159 bool gotBits(boost::uint32_t nbits)
00160 {
00161 boost::uint32_t gotbits = 8-usedBits +8*(end-ptr-1);
00162 if (gotbits > nbits) return true;
00163 else return false;
00164 }
00165
00166 private:
00167
00168 void advanceToNextByte()
00169 {
00170 if ( ++ptr == end )
00171 {
00172 log_debug("Going round");
00173 ptr=start;
00174 }
00175 usedBits=0;
00176 }
00177
00179 const byte* start;
00180
00182 const byte* ptr;
00183
00185 const byte* end;
00186
00188 unsigned usedBits;
00189
00190 };
00191
00192
00193 }
00194
00195
00196 #endif // BITSREADER_H
00197
00198
00199
00200
00201
00202
00203
00204