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

BitlDecoder.h « Compress « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3bee413ae4716e19562a9c82d4d99617667d6264 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// BitlDecoder.h -- the Least Significant Bit of byte is First

#ifndef __BITL_DECODER_H
#define __BITL_DECODER_H

#include "../IStream.h"

namespace NBitl {

const int kNumBigValueBits = 8 * 4;

const int kNumValueBytes = 3;
const int kNumValueBits = 8  * kNumValueBytes;

const UInt32 kMask = (1 << kNumValueBits) - 1;

extern Byte kInvertTable[256];

template<class TInByte>
class CBaseDecoder
{
protected:
  int m_BitPos;
  UInt32 m_Value;
  TInByte m_Stream;
public:
  UInt32 NumExtraBytes;
  bool Create(UInt32 bufferSize) { return m_Stream.Create(bufferSize); }
  void SetStream(ISequentialInStream *inStream) { m_Stream.SetStream(inStream); }
  void ReleaseStream() { m_Stream.ReleaseStream(); }
  void Init()
  {
    m_Stream.Init();
    m_BitPos = kNumBigValueBits;
    m_Value = 0;
    NumExtraBytes = 0;
  }
  UInt64 GetProcessedSize() const { return m_Stream.GetProcessedSize() + NumExtraBytes - (kNumBigValueBits - m_BitPos) / 8; }

  void Normalize()
  {
    for (; m_BitPos >= 8; m_BitPos -= 8)
    {
      Byte b = 0;
      if (!m_Stream.ReadByte(b))
      {
        b = 0xFF; // check it
        NumExtraBytes++;
      }
      m_Value = (b << (kNumBigValueBits - m_BitPos)) | m_Value;
    }
  }
  
  UInt32 ReadBits(int numBits)
  {
    Normalize();
    UInt32 res = m_Value & ((1 << numBits) - 1);
    m_BitPos += numBits;
    m_Value >>= numBits;
    return res;
  }

  bool ExtraBitsWereRead() const
  {
    if (NumExtraBytes == 0)
      return false;
    return ((UInt32)(kNumBigValueBits - m_BitPos) < (NumExtraBytes << 3));
  }
};

template<class TInByte>
class CDecoder: public CBaseDecoder<TInByte>
{
  UInt32 m_NormalValue;

public:
  void Init()
  {
    CBaseDecoder<TInByte>::Init();
    m_NormalValue = 0;
  }

  void Normalize()
  {
    for (; this->m_BitPos >= 8; this->m_BitPos -= 8)
    {
      Byte b = 0;
      if (!this->m_Stream.ReadByte(b))
      {
        b = 0xFF; // check it
        this->NumExtraBytes++;
      }
      m_NormalValue = (b << (kNumBigValueBits - this->m_BitPos)) | m_NormalValue;
      this->m_Value = (this->m_Value << 8) | kInvertTable[b];
    }
  }
  
  UInt32 GetValue(int numBits)
  {
    Normalize();
    return ((this->m_Value >> (8 - this->m_BitPos)) & kMask) >> (kNumValueBits - numBits);
  }

  void MovePos(int numBits)
  {
    this->m_BitPos += numBits;
    m_NormalValue >>= numBits;
  }
  
  UInt32 ReadBits(int numBits)
  {
    Normalize();
    UInt32 res = m_NormalValue & ((1 << numBits) - 1);
    MovePos(numBits);
    return res;
  }

  void AlignToByte() { MovePos((32 - this->m_BitPos) & 7); }

  Byte ReadByte()
  {
    if (this->m_BitPos == kNumBigValueBits)
    {
      Byte b = 0;
      if (!this->m_Stream.ReadByte(b))
      {
        b = 0xFF;
        this->NumExtraBytes++;
      }
      return b;
    }
    {
      Byte b = (Byte)(m_NormalValue & 0xFF);
      MovePos(8);
      return b;
    }
  }
};

}

#endif