summaryrefslogtreecommitdiff
path: root/other-licenses/7zstub/src/7zip/Common/MSBFEncoder.h
blob: b141e5545956d8ea96ca2a8fb5e66416a42c49a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Stream/MSBFEncoder.h

#ifndef __STREAM_MSBFENCODER_H
#define __STREAM_MSBFENCODER_H

#include "Common/Defs.h"
#include "../IStream.h"
#include "OutBuffer.h"

namespace NStream {
namespace NMSBF {

template<class TOutByte>
class CEncoder
{
  TOutByte m_Stream;
  int m_BitPos;
  Byte m_CurByte;
public:
  bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); }
  void SetStream(ISequentialOutStream *outStream) { m_Stream.SetStream(outStream);}
  void ReleaseStream() { m_Stream.ReleaseStream(); }
  void Init()
  {
    m_Stream.Init();
    m_BitPos = 8; 
    m_CurByte = 0;
  }
  HRESULT Flush()
  {
    if(m_BitPos < 8)
      WriteBits(0, m_BitPos);
    return m_Stream.Flush();
  }

  void WriteBits(UInt32 value, int numBits)
  {
    while(numBits > 0)
    {
      if (numBits < m_BitPos)
      {
        m_CurByte |= ((Byte)value << (m_BitPos -= numBits));
        return;
      }
      numBits -= m_BitPos;
      UInt32 newBits = (value >> numBits);
      value -= (newBits << numBits);
      m_Stream.WriteByte(m_CurByte | (Byte)newBits);
      m_BitPos = 8;
      m_CurByte = 0;
    }
  }
  UInt64 GetProcessedSize() const { 
      return m_Stream.GetProcessedSize() + (8 - m_BitPos + 7) / 8; }
};

}}

#endif