Gnash 0.8.9
|
00001 // BitsReader.h: bits reader, for Gnash. 00002 // 00003 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 00004 // 2011 Free Software Foundation, Inc 00005 // 00006 // This program is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 3 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // This program is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with this program; if not, write to the Free Software 00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 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> // for boost::uint32_t used in this file 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 } // end namespace gnash 00194 00195 00196 #endif // BITSREADER_H 00197 00198 00199 // Local Variables: 00200 // mode: C++ 00201 // c-basic-offset: 8 00202 // tab-width: 8 00203 // indent-tabs-mode: t 00204 // End: