diff 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
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/BitmDecoder.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,66 @@
     1.4 +// BitmDecoder.h -- the Most Significant Bit of byte is First
     1.5 +
     1.6 +#ifndef __BITM_DECODER_H
     1.7 +#define __BITM_DECODER_H
     1.8 +
     1.9 +#include "../IStream.h"
    1.10 +
    1.11 +namespace NBitm {
    1.12 +
    1.13 +const int kNumBigValueBits = 8 * 4;
    1.14 +const int kNumValueBytes = 3;
    1.15 +const int kNumValueBits = 8  * kNumValueBytes;
    1.16 +
    1.17 +const UInt32 kMask = (1 << kNumValueBits) - 1;
    1.18 +
    1.19 +template<class TInByte>
    1.20 +class CDecoder
    1.21 +{
    1.22 +  UInt32 m_BitPos;
    1.23 +  UInt32 m_Value;
    1.24 +public:
    1.25 +  TInByte m_Stream;
    1.26 +  bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); }
    1.27 +  void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream);}
    1.28 +  void ReleaseStream() { m_Stream.ReleaseStream();}
    1.29 +
    1.30 +  void Init()
    1.31 +  {
    1.32 +    m_Stream.Init();
    1.33 +    m_BitPos = kNumBigValueBits;
    1.34 +    Normalize();
    1.35 +  }
    1.36 +  
    1.37 +  UInt64 GetProcessedSize() const
    1.38 +    { return m_Stream.GetProcessedSize() - (kNumBigValueBits - m_BitPos) / 8; }
    1.39 +  UInt32 GetBitPosition() const { return (m_BitPos & 7); }
    1.40 +  
    1.41 +  void Normalize()
    1.42 +  {
    1.43 +    for (;m_BitPos >= 8; m_BitPos -= 8)
    1.44 +      m_Value = (m_Value << 8) | m_Stream.ReadByte();
    1.45 +  }
    1.46 +
    1.47 +  UInt32 GetValue(UInt32 numBits) const
    1.48 +  {
    1.49 +    // return (m_Value << m_BitPos) >> (kNumBigValueBits - numBits);
    1.50 +    return ((m_Value >> (8 - m_BitPos)) & kMask) >> (kNumValueBits - numBits);
    1.51 +  }
    1.52 +  
    1.53 +  void MovePos(UInt32 numBits)
    1.54 +  {
    1.55 +    m_BitPos += numBits;
    1.56 +    Normalize();
    1.57 +  }
    1.58 +  
    1.59 +  UInt32 ReadBits(UInt32 numBits)
    1.60 +  {
    1.61 +    UInt32 res = GetValue(numBits);
    1.62 +    MovePos(numBits);
    1.63 +    return res;
    1.64 +  }
    1.65 +};
    1.66 +
    1.67 +}
    1.68 +
    1.69 +#endif