1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/7zip/Common/LSBFDecoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,127 @@ 1.4 +// LSBFDecoder.h 1.5 + 1.6 +#ifndef __STREAM_LSBFDECODER_H 1.7 +#define __STREAM_LSBFDECODER_H 1.8 + 1.9 +#include "../IStream.h" 1.10 + 1.11 +namespace NStream { 1.12 +namespace NLSBF { 1.13 + 1.14 +const int kNumBigValueBits = 8 * 4; 1.15 + 1.16 +const int kNumValueBytes = 3; 1.17 +const int kNumValueBits = 8 * kNumValueBytes; 1.18 + 1.19 +const UInt32 kMask = (1 << kNumValueBits) - 1; 1.20 + 1.21 +extern Byte kInvertTable[256]; 1.22 +// the Least Significant Bit of byte is First 1.23 + 1.24 +template<class TInByte> 1.25 +class CBaseDecoder 1.26 +{ 1.27 +protected: 1.28 + int m_BitPos; 1.29 + UInt32 m_Value; 1.30 + TInByte m_Stream; 1.31 +public: 1.32 + UInt32 NumExtraBytes; 1.33 + bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } 1.34 + void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream); } 1.35 + void ReleaseStream() { m_Stream.ReleaseStream(); } 1.36 + void Init() 1.37 + { 1.38 + m_Stream.Init(); 1.39 + m_BitPos = kNumBigValueBits; 1.40 + m_Value = 0; 1.41 + NumExtraBytes = 0; 1.42 + } 1.43 + UInt64 GetProcessedSize() const 1.44 + { return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; } 1.45 + UInt64 GetProcessedBitsSize() const 1.46 + { return (m_Stream.GetProcessedSize() << 3) - (kNumBigValueBits - m_BitPos); } 1.47 + int GetBitPosition() const { return (m_BitPos & 7); } 1.48 + 1.49 + void Normalize() 1.50 + { 1.51 + for (;m_BitPos >= 8; m_BitPos -= 8) 1.52 + { 1.53 + Byte b; 1.54 + if (!m_Stream.ReadByte(b)) 1.55 + { 1.56 + b = 0xFF; // check it 1.57 + NumExtraBytes++; 1.58 + } 1.59 + m_Value = (b << (kNumBigValueBits - m_BitPos)) | m_Value; 1.60 + } 1.61 + } 1.62 + 1.63 + UInt32 ReadBits(int numBits) 1.64 + { 1.65 + Normalize(); 1.66 + UInt32 res = m_Value & ((1 << numBits) - 1); 1.67 + m_BitPos += numBits; 1.68 + m_Value >>= numBits; 1.69 + return res; 1.70 + } 1.71 + 1.72 + bool ExtraBitsWereRead() const 1.73 + { 1.74 + if (NumExtraBytes == 0) 1.75 + return false; 1.76 + return ((UInt32)(kNumBigValueBits - m_BitPos) < (NumExtraBytes << 3)); 1.77 + } 1.78 +}; 1.79 + 1.80 +template<class TInByte> 1.81 +class CDecoder: public CBaseDecoder<TInByte> 1.82 +{ 1.83 + UInt32 m_NormalValue; 1.84 + 1.85 +public: 1.86 + void Init() 1.87 + { 1.88 + CBaseDecoder<TInByte>::Init(); 1.89 + m_NormalValue = 0; 1.90 + } 1.91 + 1.92 + void Normalize() 1.93 + { 1.94 + for (;this->m_BitPos >= 8; this->m_BitPos -= 8) 1.95 + { 1.96 + Byte b; 1.97 + if (!this->m_Stream.ReadByte(b)) 1.98 + { 1.99 + b = 0xFF; // check it 1.100 + this->NumExtraBytes++; 1.101 + } 1.102 + m_NormalValue = (b << (kNumBigValueBits - this->m_BitPos)) | m_NormalValue; 1.103 + this->m_Value = (this->m_Value << 8) | kInvertTable[b]; 1.104 + } 1.105 + } 1.106 + 1.107 + UInt32 GetValue(int numBits) 1.108 + { 1.109 + Normalize(); 1.110 + return ((this->m_Value >> (8 - this->m_BitPos)) & kMask) >> (kNumValueBits - numBits); 1.111 + } 1.112 + 1.113 + void MovePos(int numBits) 1.114 + { 1.115 + this->m_BitPos += numBits; 1.116 + m_NormalValue >>= numBits; 1.117 + } 1.118 + 1.119 + UInt32 ReadBits(int numBits) 1.120 + { 1.121 + Normalize(); 1.122 + UInt32 res = m_NormalValue & ( (1 << numBits) - 1); 1.123 + MovePos(numBits); 1.124 + return res; 1.125 + } 1.126 +}; 1.127 + 1.128 +}} 1.129 + 1.130 +#endif