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

LzhDecoder.cpp « Compress « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bf4a0b6865641b2fde40e7c6a54982e4229910b0 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// LzhDecoder.cpp

#include "StdAfx.h"

#include "LzhDecoder.h"

namespace NCompress{
namespace NLzh {
namespace NDecoder {

static const UInt32 kWindowSizeMin = 1 << 16;

static bool CheckCodeLens(const Byte *lens, unsigned num)
{
  UInt32 sum = 0;
  for (unsigned i = 0; i < num; i++)
  {
    unsigned len = lens[i];
    if (len != 0)
      sum += ((UInt32)1 << (NUM_CODE_BITS - len));
  }
  return sum == ((UInt32)1 << NUM_CODE_BITS);
}

bool CCoder::ReadTP(unsigned num, unsigned numBits, int spec)
{
  _symbolT = -1;

  UInt32 n = _inBitStream.ReadBits(numBits);
  if (n == 0)
  {
    _symbolT = _inBitStream.ReadBits(numBits);
    return ((unsigned)_symbolT < num);
  }

  if (n > num)
    return false;

  {
    Byte lens[NPT];
    unsigned i;
    for (i = 0; i < NPT; i++)
      lens[i] = 0;

    i = 0;
    
    do
    {
      UInt32 val = _inBitStream.GetValue(16);
      unsigned c = val >> 13;
      
      if (c == 7)
      {
        UInt32 mask = 1 << 12;
        while (mask & val)
        {
          mask >>= 1;
          c++;
        }
        if (c > 16)
          return false;
      }
      
      _inBitStream.MovePos(c < 7 ? 3 : c - 3);
      lens[i++] = (Byte)c;
      
      if (i == (unsigned)spec)
        i += _inBitStream.ReadBits(2);
    }
    while (i < n);
    
    if (!CheckCodeLens(lens, NPT))
      return false;
    return _decoderT.SetCodeLengths(lens);
  }
}

static const unsigned NUM_C_BITS = 9;

bool CCoder::ReadC()
{
  _symbolC = -1;

  unsigned n = _inBitStream.ReadBits(NUM_C_BITS);
  
  if (n == 0)
  {
    _symbolC = _inBitStream.ReadBits(NUM_C_BITS);
    return ((unsigned)_symbolC < NC);
  }

  if (n > NC)
    return false;

  {
    Byte lens[NC];

    unsigned i = 0;
  
    do
    {
      UInt32 c = (unsigned)_symbolT;
      if (_symbolT < 0)
        c = _decoderT.DecodeSymbol(&_inBitStream);
      
      if (c <= 2)
      {
        if (c == 0)
          c = 1;
        else if (c == 1)
          c = _inBitStream.ReadBits(4) + 3;
        else
          c = _inBitStream.ReadBits(NUM_C_BITS) + 20;
    
        if (i + c > n)
          return false;
        
        do
          lens[i++] = 0;
        while (--c);
      }
      else
        lens[i++] = (Byte)(c - 2);
    }
    while (i < n);
    
    while (i < NC)
      lens[i++] = 0;
    
    if (!CheckCodeLens(lens, NC))
      return false;
    return _decoderC.SetCodeLengths(lens);
  }
}

HRESULT CCoder::CodeReal(UInt64 rem, ICompressProgressInfo *progress)
{
  unsigned pbit = (DictSize <= (1 << 14) ? 4 : 5);

  UInt32 blockSize = 0;

  while (rem != 0)
  {
    if (blockSize == 0)
    {
      if (_inBitStream.ExtraBitsWereRead())
        return S_FALSE;

      if (progress)
      {
        UInt64 packSize = _inBitStream.GetProcessedSize();
        UInt64 pos = _outWindow.GetProcessedSize();
        RINOK(progress->SetRatioInfo(&packSize, &pos));
      }
      
      blockSize = _inBitStream.ReadBits(16);
      if (blockSize == 0)
        return S_FALSE;
      
      if (!ReadTP(NT, 5, 3))
        return S_FALSE;
      if (!ReadC())
        return S_FALSE;
      if (!ReadTP(NP, pbit, -1))
        return S_FALSE;
    }
  
    blockSize--;

    UInt32 number = (unsigned)_symbolC;
    if (_symbolC < 0)
      number = _decoderC.DecodeSymbol(&_inBitStream);

    if (number < 256)
    {
      _outWindow.PutByte((Byte)number);
      rem--;
    }
    else
    {
      UInt32 len = number - 256 + kMatchMinLen;

      UInt32 dist = (unsigned)_symbolT;
      if (_symbolT < 0)
        dist = _decoderT.DecodeSymbol(&_inBitStream);
      
      if (dist > 1)
      {
        dist--;
        dist = ((UInt32)1 << dist) + _inBitStream.ReadBits((unsigned)dist);
      }
      
      if (dist >= DictSize)
        return S_FALSE;

      if (len > rem)
        len = (UInt32)rem;

      if (!_outWindow.CopyBlock(dist, len))
        return S_FALSE;
      rem -= len;
    }
  }

  if (FinishMode)
  {
    if (blockSize != 0)
      return S_FALSE;
    if (_inBitStream.ReadAlignBits() != 0)
      return S_FALSE;
  }

  if (_inBitStream.ExtraBitsWereRead())
    return S_FALSE;

  return S_OK;
}


STDMETHODIMP CCoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
    const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress)
{
  try
  {
    if (!outSize)
      return E_INVALIDARG;
    
    if (!_outWindow.Create(DictSize > kWindowSizeMin ? DictSize : kWindowSizeMin))
      return E_OUTOFMEMORY;
    if (!_inBitStream.Create(1 << 17))
      return E_OUTOFMEMORY;
    
    _outWindow.SetStream(outStream);
    _outWindow.Init(false);
    _inBitStream.SetStream(inStream);
    _inBitStream.Init();
    
    CCoderReleaser coderReleaser(this);
    
    RINOK(CodeReal(*outSize, progress));

    coderReleaser.Disable();
    return _outWindow.Flush();
  }
  catch(const CInBufferException &e) { return e.ErrorCode; }
  catch(const CLzOutWindowException &e) { return e.ErrorCode; }
  catch(...) { return S_FALSE; }
}

}}}