Gnash  0.8.10
BitsReader.h
Go to the documentation of this file.
00001 // BitsReader.h:  bits reader, for Gnash.
00002 // 
00003 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00004 //   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 <cassert>
00029 #include <boost/cstdint.hpp> // for boost::uint32_t used in this file
00030 
00031 namespace gnash {
00032 
00034 //
00038 class DSOEXPORT BitsReader 
00039 {
00040 public:
00041         typedef unsigned char byte;
00042 
00044         BitsReader(const byte* input, size_t len)
00045                 :
00046                 start(input),
00047                 ptr(start),
00048                 end(start+len),
00049                 usedBits(0)
00050         {
00051         }
00052 
00054         //
00061         BitsReader(const BitsReader& from, size_t len)
00062                 :
00063                 start(from.ptr),
00064                 ptr(start),
00065                 end(start+len),
00066                 usedBits(from.usedBits) 
00067         {
00068         }
00069 
00070         ~BitsReader() {}
00071 
00072         size_t size() const
00073         {
00074                 return end-start;
00075         }
00076 
00078         void setBuffer(byte* input, size_t len)
00079         {
00080                 start = ptr = input;
00081                 end = start+len;
00082                 usedBits = 0;
00083         }
00084 
00089         unsigned read_uint(unsigned short bitcount);
00090 
00094         bool read_bit();
00095 
00100         boost::int32_t read_sint(unsigned short bitcount);
00101 
00103         boost::uint8_t  read_u8()
00104         {
00105                 align();
00106                 return *ptr++;
00107         }
00108 
00110     boost::int8_t read_s8()
00111         {
00112                 return static_cast<boost::int8_t>(read_u8());
00113         }
00114 
00116         boost::uint16_t read_u16()
00117         {
00118                 align();
00119                 assert(ptr+2 < end);
00120                 boost::uint16_t result = *ptr++;
00121                 result |= *ptr++ << 8;
00122                 return result ;
00123         }
00124 
00126         boost::int16_t  read_s16()
00127         {
00128                 return static_cast<boost::int16_t>(read_u16());
00129         }
00130 
00132         boost::uint32_t read_u32()
00133         {
00134                 align();
00135                 assert(ptr+4 < end);
00136                 boost::uint32_t result = *ptr++;
00137                 result |= *ptr++ << 8;
00138                 result |= *ptr++ << 16;
00139                 result |= *ptr++ << 24;
00140                 return(result);
00141         }
00142 
00144         boost::int32_t read_s32()
00145         {
00146                 return static_cast<boost::int32_t>(read_u32());
00147         }
00148 
00152         void align()
00153         {
00154                 if ( usedBits ) advanceToNextByte();
00155         }
00156 
00158         bool gotBits(boost::uint32_t nbits)
00159         {
00160                 boost::uint32_t gotbits = 8-usedBits +8*(end-ptr-1);
00161                 if (gotbits > nbits) return true;
00162                 else return false;
00163         }
00164 
00165 private:
00166 
00167         void advanceToNextByte()
00168         {
00169                 if ( ++ptr == end )
00170                 {
00171                         log_debug(_("Going round"));
00172                         ptr=start;
00173                 }
00174                 usedBits=0;
00175         }
00176 
00178         const byte* start;
00179 
00181         const byte* ptr;
00182 
00184         const byte* end;
00185 
00187         unsigned usedBits;
00188 
00189 };
00190 
00191 
00192 }       // end namespace gnash
00193 
00194 
00195 #endif // BITSREADER_H
00196 
00197 
00198 // Local Variables:
00199 // mode: C++
00200 // c-basic-offset: 8 
00201 // tab-width: 8
00202 // indent-tabs-mode: t
00203 // End: