Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MSBFEncoder.h « Common « 7zip - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b87bdea310dbd11e892b3d63dbdec388d413e67e (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
60
61
62
63
64
65
66
67
68
69
70
// Stream/MSBFEncoder.h
// the Most Significant Bit of byte is First

#pragma once

#ifndef __STREAM_MSBFENCODER_H
#define __STREAM_MSBFENCODER_H

#include "Common/Defs.h"
#include "Interface/IInOutStreams.h"

namespace NStream {
namespace NMSBF {

template<class TOutByte>
class CEncoder
{
  TOutByte m_Stream;
  UINT32 m_BitPos;
  BYTE m_CurByte;
public:
  void Init(ISequentialOutStream *aStream)
  {
    m_Stream.Init(aStream);
    m_BitPos = 8; 
    m_CurByte = 0;
  }
  HRESULT Flush()
  {
    if(m_BitPos < 8)
      WriteBits(0, m_BitPos);
    return m_Stream.Flush();
  }

  void ReleaseStream()
  {
    m_Stream.ReleaseStream();
  }

  void WriteBits(UINT32 aValue, UINT32 aNumBits)
  {
    while(aNumBits > 0)
    {
      UINT32 aNumNewBits = MyMin(aNumBits, m_BitPos);
      aNumBits -= aNumNewBits;
      
      
      m_CurByte <<= aNumNewBits;
      UINT32 aNewBits = aValue >> aNumBits;
      m_CurByte |= BYTE(aNewBits);
      aValue -= (aNewBits << aNumBits);
      
      
      m_BitPos -= aNumNewBits;
      
      if (m_BitPos == 0)
      {
        m_Stream.WriteByte(m_CurByte);
        m_BitPos = 8;
      }
    }
  }
  UINT64 GetProcessedSize() const { 
      return m_Stream.GetProcessedSize() + (8 - m_BitPos + 7) /8; }
};

}}

#endif