other-licenses/7zstub/src/7zip/Common/MSBFDecoder.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/7zip/Common/MSBFDecoder.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,69 @@
     1.4 +// MSBFDecoder.h
     1.5 +// the Most Significant Bit of byte is First
     1.6 +
     1.7 +#ifndef __STREAM_MSBFDECODER_H
     1.8 +#define __STREAM_MSBFDECODER_H
     1.9 +
    1.10 +#include "../../Common/Types.h"
    1.11 +#include "../IStream.h"
    1.12 +
    1.13 +namespace NStream {
    1.14 +namespace NMSBF {
    1.15 +
    1.16 +const int kNumBigValueBits = 8 * 4;
    1.17 +const int kNumValueBytes = 3;
    1.18 +const int kNumValueBits = 8  * kNumValueBytes;
    1.19 +
    1.20 +const UInt32 kMask = (1 << kNumValueBits) - 1;
    1.21 +
    1.22 +template<class TInByte>
    1.23 +class CDecoder
    1.24 +{
    1.25 +  UInt32 m_BitPos;
    1.26 +  UInt32 m_Value;
    1.27 +public:
    1.28 +  TInByte m_Stream;
    1.29 +  bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); }
    1.30 +  void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream);}
    1.31 +  void ReleaseStream() { m_Stream.ReleaseStream();}
    1.32 +
    1.33 +  void Init()
    1.34 +  {
    1.35 +    m_Stream.Init();
    1.36 +    m_BitPos = kNumBigValueBits; 
    1.37 +    Normalize();
    1.38 +  }
    1.39 +  
    1.40 +  UInt64 GetProcessedSize() const 
    1.41 +    { return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; }
    1.42 +  UInt32 GetBitPosition() const { return (m_BitPos & 7); }
    1.43 +  
    1.44 +  void Normalize()
    1.45 +  {
    1.46 +    for (;m_BitPos >= 8; m_BitPos -= 8)
    1.47 +      m_Value = (m_Value << 8) | m_Stream.ReadByte();
    1.48 +  }
    1.49 +
    1.50 +  UInt32 GetValue(UInt32 numBits) const
    1.51 +  {
    1.52 +    // return (m_Value << m_BitPos) >> (kNumBigValueBits - numBits);
    1.53 +    return ((m_Value >> (8 - m_BitPos)) & kMask) >> (kNumValueBits - numBits);
    1.54 +  }
    1.55 +  
    1.56 +  void MovePos(UInt32 numBits)
    1.57 +  {
    1.58 +    m_BitPos += numBits;
    1.59 +    Normalize();
    1.60 +  }
    1.61 +  
    1.62 +  UInt32 ReadBits(UInt32 numBits)
    1.63 +  {
    1.64 +    UInt32 res = GetValue(numBits);
    1.65 +    MovePos(numBits);
    1.66 +    return res;
    1.67 +  }
    1.68 +};
    1.69 +
    1.70 +}}
    1.71 +
    1.72 +#endif

mercurial