Mercurial > vba-linux
annotate src/win32/7zip/7z/CPP/7zip/Compress/BitmDecoder.h @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
rev | line source |
---|---|
rlm@1 | 1 // BitmDecoder.h -- the Most Significant Bit of byte is First |
rlm@1 | 2 |
rlm@1 | 3 #ifndef __BITM_DECODER_H |
rlm@1 | 4 #define __BITM_DECODER_H |
rlm@1 | 5 |
rlm@1 | 6 #include "../IStream.h" |
rlm@1 | 7 |
rlm@1 | 8 namespace NBitm { |
rlm@1 | 9 |
rlm@1 | 10 const int kNumBigValueBits = 8 * 4; |
rlm@1 | 11 const int kNumValueBytes = 3; |
rlm@1 | 12 const int kNumValueBits = 8 * kNumValueBytes; |
rlm@1 | 13 |
rlm@1 | 14 const UInt32 kMask = (1 << kNumValueBits) - 1; |
rlm@1 | 15 |
rlm@1 | 16 template<class TInByte> |
rlm@1 | 17 class CDecoder |
rlm@1 | 18 { |
rlm@1 | 19 UInt32 m_BitPos; |
rlm@1 | 20 UInt32 m_Value; |
rlm@1 | 21 public: |
rlm@1 | 22 TInByte m_Stream; |
rlm@1 | 23 bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); } |
rlm@1 | 24 void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream);} |
rlm@1 | 25 void ReleaseStream() { m_Stream.ReleaseStream();} |
rlm@1 | 26 |
rlm@1 | 27 void Init() |
rlm@1 | 28 { |
rlm@1 | 29 m_Stream.Init(); |
rlm@1 | 30 m_BitPos = kNumBigValueBits; |
rlm@1 | 31 Normalize(); |
rlm@1 | 32 } |
rlm@1 | 33 |
rlm@1 | 34 UInt64 GetProcessedSize() const |
rlm@1 | 35 { return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; } |
rlm@1 | 36 UInt32 GetBitPosition() const { return (m_BitPos & 7); } |
rlm@1 | 37 |
rlm@1 | 38 void Normalize() |
rlm@1 | 39 { |
rlm@1 | 40 for (;m_BitPos >= 8; m_BitPos -= 8) |
rlm@1 | 41 m_Value = (m_Value << 8) | m_Stream.ReadByte(); |
rlm@1 | 42 } |
rlm@1 | 43 |
rlm@1 | 44 UInt32 GetValue(UInt32 numBits) const |
rlm@1 | 45 { |
rlm@1 | 46 // return (m_Value << m_BitPos) >> (kNumBigValueBits - numBits); |
rlm@1 | 47 return ((m_Value >> (8 - m_BitPos)) & kMask) >> (kNumValueBits - numBits); |
rlm@1 | 48 } |
rlm@1 | 49 |
rlm@1 | 50 void MovePos(UInt32 numBits) |
rlm@1 | 51 { |
rlm@1 | 52 m_BitPos += numBits; |
rlm@1 | 53 Normalize(); |
rlm@1 | 54 } |
rlm@1 | 55 |
rlm@1 | 56 UInt32 ReadBits(UInt32 numBits) |
rlm@1 | 57 { |
rlm@1 | 58 UInt32 res = GetValue(numBits); |
rlm@1 | 59 MovePos(numBits); |
rlm@1 | 60 return res; |
rlm@1 | 61 } |
rlm@1 | 62 }; |
rlm@1 | 63 |
rlm@1 | 64 } |
rlm@1 | 65 |
rlm@1 | 66 #endif |