Octane v1.01.20 - The Open Compression Toolkit for C++ | http://octane.sourceforge.net/ |
#include <bitreader.hpp>
Inheritance diagram for bitreader:
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.
Definition at line 52 of file bitreader.hpp.
|
|
Destructor. Derived classes should implement a destructor which calls the finalize() member function. Definition at line 57 of file bitreader.hpp.
00057 { ; } |
|
Check for error.
Reimplemented in file_bitreader. Definition at line 62 of file bitreader.hpp.
00062 { return false; } |
|
Check if bitreader is empty.
Definition at line 73 of file bitreader.hpp. 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().
|
|
Return the number of bits left.
Definition at line 76 of file bitreader.hpp. Referenced by bytes_left().
|
|
Return the number of bytes left.
Definition at line 79 of file bitreader.hpp. References bits_left(). Referenced by OctaneTester::CheckFilesAreIdentical().
00079 { return bits_left() / 8; } |
|
Return the total number of bits available.
Definition at line 83 of file bitreader.hpp. References length. Referenced by get_byte_length().
00083 { return length; } |
|
Return the total number of bytes available.
Definition at line 87 of file bitreader.hpp. References get_bit_length().
00087 { return get_bit_length() / 8; } |
|
Return the current bit position within the bit stream.
Definition at line 91 of file bitreader.hpp. References offs. Referenced by OctaneCompressor_Statistical::DoProtectedCreateSymbolsAndModelsUsingStream(), SubstringParser::RewindAnyBufferedInput(), and tell_byte().
00091 { return offs; } |
|
Return the current byte position within the bit stream.
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; } |
|
Check if seek functions are supported.
Implemented in null_bitreader, stream_bitreader, vector_bitreader, string_bitreader, array_bitreader, and file_bitreader. |
|
Seek to a specified bit position within the bit stream.
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; } |
|
Seek to a specified byte position within the bit stream.
Definition at line 115 of file bitreader.hpp. References seek_bit(). Referenced by zleCompressor::DoProtectedCompress().
00115 { return seek_bit(8 * pos); } |
|
Seek to a specified relative bit position within the bit stream.
Definition at line 124 of file bitreader.hpp. References length, offs, and seek_bit().
|
|
Seek to a specified relative byte position within the bit stream.
Definition at line 148 of file bitreader.hpp. References seek_bit().
00149 { 00150 return seek_bit(8 * pos, rpos); 00151 } |
|
Get and return the next bit.
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 } |
|
Get and return the next byte (8-bit unsigned value).
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 } |
|
Get and return the next word (16-bit unsigned value).
Definition at line 194 of file bitreader.hpp. References get_byte(). Referenced by get_string().
|
|
Get and return the next dword (32-bit unsigned value).
Definition at line 204 of file bitreader.hpp. References get_byte(). Referenced by get_string().
|
|
Get and return the next float.
Definition at line 218 of file bitreader.hpp. References read().
00219 { 00220 float v; 00221 read((char*)&v, sizeof(float)); 00222 return v; 00223 } |
|
Get and return the next double.
Definition at line 229 of file bitreader.hpp. References read().
00230 { 00231 double v; 00232 read((char*)&v, sizeof(double)); 00233 return v; 00234 } |
|
Read and return a string.
Definition at line 238 of file bitreader.hpp. References get_byte(), get_dword(), and get_word().
|
|
Copy bytes from the bitreader to an array.
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 } |
|
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(); } |
|
Defines the method of reading a single byte of input. Derived classes must implement this function.
Implemented in null_bitreader, stream_bitreader, vector_bitreader, string_bitreader, array_bitreader, and file_bitreader. Referenced by get_bit(), get_byte(), and reposition(). |
|
Defines the method of setting the position for the next read. Derived classes should implement this function if possible.
Reimplemented in vector_bitreader, string_bitreader, array_bitreader, and file_bitreader. Definition at line 300 of file bitreader.hpp. Referenced by reposition().
00300 { ; } |