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