Gnash  0.8.11dev
BitsReader.h
Go to the documentation of this file.
1 // BitsReader.h: bits reader, for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software 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
19 //
20 
21 
22 #ifndef BITSREADER_H
23 #define BITSREADER_H
24 
25 #include "dsodefs.h"
26 #include "log.h"
27 
28 #include <cassert>
29 #include <cstdint> // for std::uint32_t used in this file
30 
31 namespace gnash {
32 
34 //
39 {
40 public:
41  typedef unsigned char byte;
42 
44  BitsReader(const byte* input, size_t len)
45  :
46  start(input),
47  ptr(start),
48  end(start+len),
49  usedBits(0)
50  {
51  }
52 
54  //
61  BitsReader(const BitsReader& from, size_t len)
62  :
63  start(from.ptr),
64  ptr(start),
65  end(start+len),
66  usedBits(from.usedBits)
67  {
68  }
69 
71 
72  size_t size() const
73  {
74  return end-start;
75  }
76 
78  void setBuffer(byte* input, size_t len)
79  {
80  start = ptr = input;
81  end = start+len;
82  usedBits = 0;
83  }
84 
89  unsigned read_uint(unsigned short bitcount);
90 
94  bool read_bit();
95 
100  std::int32_t read_sint(unsigned short bitcount);
101 
103  std::uint8_t read_u8()
104  {
105  align();
106  return *ptr++;
107  }
108 
110  std::int8_t read_s8()
111  {
112  return static_cast<std::int8_t>(read_u8());
113  }
114 
116  std::uint16_t read_u16()
117  {
118  align();
119  assert(ptr+2 < end);
120  std::uint16_t result = *ptr++;
121  result |= *ptr++ << 8;
122  return result ;
123  }
124 
126  std::int16_t read_s16()
127  {
128  return static_cast<std::int16_t>(read_u16());
129  }
130 
132  std::uint32_t read_u32()
133  {
134  align();
135  assert(ptr+4 < end);
136  std::uint32_t result = *ptr++;
137  result |= *ptr++ << 8;
138  result |= *ptr++ << 16;
139  result |= *ptr++ << 24;
140  return(result);
141  }
142 
144  std::int32_t read_s32()
145  {
146  return static_cast<std::int32_t>(read_u32());
147  }
148 
152  void align()
153  {
154  if ( usedBits ) advanceToNextByte();
155  }
156 
158  bool gotBits(std::uint32_t nbits) const
159  {
160  std::uint32_t gotbits = 8-usedBits +8*(end-ptr-1);
161  if (gotbits > nbits) return true;
162  else return false;
163  }
164 
165 private:
166 
167  void advanceToNextByte()
168  {
169  if ( ++ptr == end )
170  {
171  log_debug(_("Going round"));
172  ptr=start;
173  }
174  usedBits=0;
175  }
176 
178  const byte* start;
179 
181  const byte* ptr;
182 
184  const byte* end;
185 
187  unsigned usedBits;
188 
189 };
190 
191 
192 } // end namespace gnash
193 
194 
195 #endif // BITSREADER_H
196 
197 
198 // Local Variables:
199 // mode: C++
200 // c-basic-offset: 8
201 // tab-width: 8
202 // indent-tabs-mode: t
203 // End:
size_t size() const
Definition: BitsReader.h:72
std::uint8_t read_u8()
Read a byte as an unsigned int (aligned)
Definition: BitsReader.h:103
Definition: klash_part.cpp:330
std::uint32_t read_u32()
Read four bytes as an unsigned int (aligned)
Definition: BitsReader.h:132
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
void align()
Discard any unused bit in the current byte and advance to next.
Definition: BitsReader.h:152
BitsReader(const BitsReader &from, size_t len)
Create a BitsReader reading a subset of another.
Definition: BitsReader.h:61
void setBuffer(byte *input, size_t len)
Set a new buffer to work with.
Definition: BitsReader.h:78
#define _(String)
Definition: log.h:44
BitsReader(const byte *input, size_t len)
Ownership of buffer left to caller.
Definition: BitsReader.h:44
std::int8_t read_s8()
Read one bytes as a signed int (aligned)
Definition: BitsReader.h:110
#define DSOEXPORT
Definition: dsodefs.h:55
bool gotBits(std::uint32_t nbits) const
Checks if the stream contains X bits.
Definition: BitsReader.h:158
~BitsReader()
Definition: BitsReader.h:70
std::int16_t read_s16()
Read two bytes as a signed int (aligned)
Definition: BitsReader.h:126
std::int32_t read_s32()
Read four bytes as an signed int (aligned)
Definition: BitsReader.h:144
BitsReader is used to encapsulate bit-packed buffer reads.
Definition: BitsReader.h:38
std::uint16_t read_u16()
Read two bytes as an unsigned int (aligned)
Definition: BitsReader.h:116
unsigned char byte
Definition: BitsReader.h:41
pixel_iterator< T > end(GnashImage &im)
Definition: ImageIterators.h:198
void log_debug(StringType msg, Args... args)
Definition: log.h:301