Octane v1.01.20 - The Open Compression Toolkit for C++ http://octane.sourceforge.net/
Homepage | Main | Modules | Class Hierarchy | Compound List | File List | Compound Members | Related Pages

bitreader Class Reference
[bitio]

#include <bitreader.hpp>

Inheritance diagram for bitreader:

array_bitreader file_bitreader null_bitreader stream_bitreader string_bitreader vector_bitreader List of all members.

Detailed Description

Base class for bit input wrappers.

The bitreader class is a wrapper which provides bit input functionality to anything supporting input of 8-bit chars (bytes). It attempts to be as clean and transparent as possible, while maintaining a fairly decent efficiency.

The base class contains functions for handling the input by holding bits in a temporary store (a byte), reading more when the store is empty.

It is used by creating a sub-class and implementing the read_next_byte() and supports_seek() member functions.

The constructor of a derived class should initialize length (either directly or through the base constructor). The destructor of the derived class should call finalize().

Definition at line 47 of file bitreader.hpp.

Public Member Functions

 bitreader (size_t size_in_bits=0)
 Constructor.

virtual ~bitreader ()
 Destructor.

virtual bool error () const
 Check for error.

void align_byte ()
 Align bitreader on a byte boundary.

Size Functions
bool empty () const
 Check if bitreader is empty.

size_t bits_left () const
 Return the number of bits left.

size_t bytes_left () const
 Return the number of bytes left.

size_t get_bit_length () const
 Return the total number of bits available.

size_t get_byte_length () const
 Return the total number of bytes available.

size_t tell_bit () const
 Return the current bit position within the bit stream.

size_t tell_byte () const
 Return the current byte position within the bit stream.

Seek Functions
virtual bool supports_seek () const=0
 Check if seek functions are supported.

size_t seek_bit (size_t pos)
 Seek to a specified bit position within the bit stream.

size_t seek_byte (size_t pos)
 Seek to a specified byte position within the bit stream.

size_t seek_bit (int pos, std::ios::seekdir rpos)
 Seek to a specified relative bit position within the bit stream.

size_t seek_byte (int pos, std::ios::seekdir rpos)
 Seek to a specified relative byte position within the bit stream.

Input Functions
bool get_bit ()
 Get and return the next bit.

unsigned char get_byte ()
 Get and return the next byte (8-bit unsigned value).

unsigned short get_word ()
 Get and return the next word (16-bit unsigned value).

unsigned long get_dword ()
 Get and return the next dword (32-bit unsigned value).

float get_float ()
 Get and return the next float.

double get_double ()
 Get and return the next double.

std::string get_string ()
 Read and return a string.

void read (char *buffer, size_t len)
 Copy bytes from the bitreader to an array.

Polymorphic Input Function Wrappers
void get (bool &v)
void get (char &v)
void get (unsigned char &v)
void get (short &v)
void get (unsigned short &v)
void get (int &v)
void get (unsigned int &v)
void get (long &v)
void get (unsigned long &v)
void get (float &v)
void get (double &v)
void get (std::string &s)

Protected Member Functions

void finalize ()
 Perform cleanup.

virtual char read_next_byte ()=0
 Defines the method of reading a single byte of input.

virtual void set_position (size_t pos)
 Defines the method of setting the position for the next read.

void reposition ()
 Helper function for reinitializing current and mask after a seek operation.


Protected Attributes

size_t offs
 Current bit position within the bit stream.

size_t length
 Total number of bits available.


Constructor & Destructor Documentation

bitreader::bitreader size_t  size_in_bits = 0  )  [inline]
 

Constructor.

Parameters:
size_in_bits - the number of bits available.

Definition at line 52 of file bitreader.hpp.

References length, and offs.

00053         : offs(0), length(size_in_bits), current(0), mask(0) { ; }

virtual bitreader::~bitreader  )  [inline, virtual]
 

Destructor.

Derived classes should implement a destructor which calls the finalize() member function.

Definition at line 57 of file bitreader.hpp.

00057 { ; }


Member Function Documentation

virtual bool bitreader::error  )  const [inline, virtual]
 

Check for error.

Returns:
true if an error occurred.

Reimplemented in file_bitreader.

Definition at line 62 of file bitreader.hpp.

00062 { return false; }

bool bitreader::empty  )  const [inline]
 

Check if bitreader is empty.

Returns:
true if bitreader is empty, otherwise false.

Definition at line 73 of file bitreader.hpp.

References length, and offs.

Referenced by SampleCoder::DecodeSymbolFromInput(), HuffmanCoder::DecodeSymbolFromInput(), zleCompressor::DoProtectedCompress(), SampleCompressor::DoProtectedCompress(), mtfllCompressor::DoProtectedCompress(), SampleCompressor::DoProtectedCreateSymbolsAndModelsUsingStream(), zleCompressor::DoProtectedDecompress(), SampleCompressor::DoProtectedDecompress(), mtfllCompressor::DoProtectedDecompress(), SampleParser::ParseNextSymbolFromInput(), and BitParser::ParseNextSymbolFromInput().

00073 { return offs >= length; }

size_t bitreader::bits_left  )  const [inline]
 

Return the number of bits left.

Returns:
the number of bits left in the bitreader.

Definition at line 76 of file bitreader.hpp.

References length, and offs.

Referenced by bytes_left().

00076 { return length - offs; }

size_t bitreader::bytes_left  )  const [inline]
 

Return the number of bytes left.

Returns:
the number of bytes left in the bitreader.

Definition at line 79 of file bitreader.hpp.

References bits_left().

Referenced by OctaneTester::CheckFilesAreIdentical().

00079 { return bits_left() / 8; }

size_t bitreader::get_bit_length  )  const [inline]
 

Return the total number of bits available.

Returns:
the number of bits available.

Definition at line 83 of file bitreader.hpp.

References length.

Referenced by get_byte_length().

00083 { return length; }

size_t bitreader::get_byte_length  )  const [inline]
 

Return the total number of bytes available.

Returns:
the number of bytes available.

Definition at line 87 of file bitreader.hpp.

References get_bit_length().

00087 { return get_bit_length() / 8; }

size_t bitreader::tell_bit  )  const [inline]
 

Return the current bit position within the bit stream.

Returns:
the current bit position.

Definition at line 91 of file bitreader.hpp.

References offs.

Referenced by OctaneCompressor_Statistical::DoProtectedCreateSymbolsAndModelsUsingStream(), SubstringParser::RewindAnyBufferedInput(), and tell_byte().

00091 { return offs; }

size_t bitreader::tell_byte  )  const [inline]
 

Return the current byte position within the bit stream.

Returns:
the current byte position.

Definition at line 95 of file bitreader.hpp.

References tell_bit().

Referenced by OctaneCompressor::Compress(), OctaneCompressor::Decompress(), OctaneCompressor::LoadState(), and OctaneTester::RunCommand_Decompress().

00095 { return tell_bit() / 8; }

virtual bool bitreader::supports_seek  )  const [pure virtual]
 

Check if seek functions are supported.

Returns:
true if seek functions are supported.

Implemented in null_bitreader, stream_bitreader, vector_bitreader, string_bitreader, array_bitreader, and file_bitreader.

size_t bitreader::seek_bit size_t  pos  )  [inline]
 

Seek to a specified bit position within the bit stream.

Parameters:
pos - the bit position to seek to.
Returns:
the new position.

Definition at line 110 of file bitreader.hpp.

References offs, and reposition().

Referenced by OctaneCompressor_Statistical::DoProtectedCreateSymbolsAndModelsUsingStream(), SubstringParser::RewindAnyBufferedInput(), seek_bit(), and seek_byte().

00110 { offs = pos; reposition(); return offs; }

size_t bitreader::seek_byte size_t  pos  )  [inline]
 

Seek to a specified byte position within the bit stream.

Parameters:
pos - the byte position to seek to.
Returns:
the new position.

Definition at line 115 of file bitreader.hpp.

References seek_bit().

Referenced by zleCompressor::DoProtectedCompress().

00115 { return seek_bit(8 * pos); }

size_t bitreader::seek_bit int  pos,
std::ios::seekdir  rpos
[inline]
 

Seek to a specified relative bit position within the bit stream.

Parameters:
pos - the relative bit position to seek to.
rpos - the position to seek relative to,
  • std::ios::beg - beginning of stream
  • std::ios::cur - current position
  • std::ios::end - end of stream
Returns:
the new position.

Definition at line 124 of file bitreader.hpp.

References length, offs, and seek_bit().

00125     {
00126         switch (rpos)
00127         {
00128         case std::ios::beg:
00129             return seek_bit(pos);
00130             break;
00131         case std::ios::cur:
00132             return seek_bit(offs + pos);
00133             break;
00134         case std::ios::end:
00135             return seek_bit(length - pos);
00136             break;
00137         }
00138         return 0;
00139     }

size_t bitreader::seek_byte int  pos,
std::ios::seekdir  rpos
[inline]
 

Seek to a specified relative byte position within the bit stream.

Parameters:
pos - the relative byte position to seek to.
rpos - the position to seek relative to,
  • std::ios::beg - beginning of stream
  • std::ios::cur - current position
  • std::ios::end - end of stream
Returns:
the new position.

Definition at line 148 of file bitreader.hpp.

References seek_bit().

00149     {
00150         return seek_bit(8 * pos, rpos);
00151     }

bool bitreader::get_bit  )  [inline]
 

Get and return the next bit.

Returns:
the next bit (true for 1, false for 0).

Definition at line 161 of file bitreader.hpp.

References offs, and read_next_byte().

Referenced by HuffmanCoder::DecodeSymbolFromInput().

00162     {
00163         ++offs;
00164 
00165         if (!mask)
00166         {
00167             current = read_next_byte();
00168             mask = 0x80;
00169         }
00170 
00171         const bool bit = (bool)(current & mask);
00172         mask >>= 1;
00173 
00174         return bit;
00175     }

unsigned char bitreader::get_byte  )  [inline]
 

Get and return the next byte (8-bit unsigned value).

Returns:
the next 8 bits.

Definition at line 179 of file bitreader.hpp.

References offs, and read_next_byte().

Referenced by OctaneTester::CheckFilesAreIdentical(), zleCompressor::DoProtectedCompress(), SampleCompressor::DoProtectedCompress(), mtfllCompressor::DoProtectedCompress(), SampleCompressor::DoProtectedCreateSymbolsAndModelsUsingStream(), zleCompressor::DoProtectedDecompress(), SampleCompressor::DoProtectedDecompress(), mtfllCompressor::DoProtectedDecompress(), get_dword(), get_string(), get_word(), OctaneCompressor::LoadGUID(), SubstringParser::ParseNextSymbolFromInput(), and read().

00180     {
00181         offs += 8;
00182 
00183         if (!mask) return read_next_byte();
00184 
00185         size_t bit_offs = offs & 0x0007;
00186         unsigned char tmp = current << (unsigned char)bit_offs;
00187         current = read_next_byte();
00188 
00189         return tmp | (current >> (8 - (unsigned char)bit_offs));
00190     }

unsigned short bitreader::get_word  )  [inline]
 

Get and return the next word (16-bit unsigned value).

Returns:
the next 16 bits.

Definition at line 194 of file bitreader.hpp.

References get_byte().

Referenced by get_string().

00195     {
00196         unsigned short v = get_byte();
00197         v = (v << 8) | get_byte();
00198 
00199         return v;
00200     }

unsigned long bitreader::get_dword  )  [inline]
 

Get and return the next dword (32-bit unsigned value).

Returns:
the next 32 bits.

Definition at line 204 of file bitreader.hpp.

References get_byte().

Referenced by get_string().

00205     {
00206         unsigned long v = get_byte();
00207         v = (v << 8) | get_byte();
00208         v = (v << 8) | get_byte();
00209         v = (v << 8) | get_byte();
00210 
00211         return v;
00212     }

float bitreader::get_float  )  [inline]
 

Get and return the next float.

Returns:
the next float.
Warning:
Experimental, not portable.
Todo:
Experimental, not portable.

Definition at line 218 of file bitreader.hpp.

References read().

00219     {
00220         float v;
00221         read((char*)&v, sizeof(float));
00222         return v;
00223     }

double bitreader::get_double  )  [inline]
 

Get and return the next double.

Returns:
the next double.
Warning:
Experimental, not portable.
Todo:
Experimental, not portable.

Definition at line 229 of file bitreader.hpp.

References read().

00230     {
00231         double v;
00232         read((char*)&v, sizeof(double));
00233         return v;
00234     }

std::string bitreader::get_string  )  [inline]
 

Read and return a string.

Returns:
the string.

Definition at line 238 of file bitreader.hpp.

References get_byte(), get_dword(), and get_word().

00239     {
00240         size_t len = get_word();
00241 
00242         if (len == 0) len = get_dword();
00243 
00244         std::string s;
00245 
00246         s.reserve(len);
00247         for ( ; len; --len) s += get_byte();
00248 
00249         return s;
00250     }

void bitreader::read char *  buffer,
size_t  len
[inline]
 

Copy bytes from the bitreader to an array.

Parameters:
buffer - pointer to where the bytes should be stored.
len - the number of bytes to copy.

Definition at line 255 of file bitreader.hpp.

References get_byte().

Referenced by get_double(), and get_float().

00256     {
00257         for ( ; len; --len) *buffer++ = get_byte();
00258     }

void bitreader::finalize  )  [inline, protected]
 

Perform cleanup.

Derived classes should call this method from their destructor.

Definition at line 290 of file bitreader.hpp.

References align_byte().

00290 { align_byte(); }

virtual char bitreader::read_next_byte  )  [protected, pure virtual]
 

Defines the method of reading a single byte of input.

Derived classes must implement this function.

Returns:
the next byte (8 bits) from the input.

Implemented in null_bitreader, stream_bitreader, vector_bitreader, string_bitreader, array_bitreader, and file_bitreader.

Referenced by get_bit(), get_byte(), and reposition().

virtual void bitreader::set_position size_t  pos  )  [inline, protected, virtual]
 

Defines the method of setting the position for the next read.

Derived classes should implement this function if possible.

Parameters:
pos - the position for the next read.

Reimplemented in vector_bitreader, string_bitreader, array_bitreader, and file_bitreader.

Definition at line 300 of file bitreader.hpp.

Referenced by reposition().

00300 { ; }


The documentation for this class was generated from the following file:  
Generated on 20 May 2004 by doxygen 1.3.3