~ubuntu-branches/ubuntu/precise/p7zip/precise-updates

« back to all changes in this revision

Viewing changes to CPP/7zip/Compress/Lzx/LzxDecoder.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2009-02-14 20:12:27 UTC
  • mfrom: (1.1.11 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090214201227-go63qxm9ozfdma60
Tags: 4.65~dfsg.1-1
* New upstream release.
* Remove wx2.8 Build-Depends added by mistakes (7zG is not yet
  intended to be built).
* Use dh_clean without -k.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
        // LzxDecoder.cpp
2
 
 
3
 
#include "StdAfx.h"
4
 
 
5
 
#include "LzxDecoder.h"
6
 
 
7
 
#include "Common/Defs.h"
8
 
extern "C" 
9
 
10
 
#include "../../../../C/Alloc.h"
11
 
}
12
 
#include "Windows/Defs.h"
13
 
 
14
 
namespace NCompress {
15
 
namespace NLzx {
16
 
 
17
 
const int kLenIdNeedInit = -2;
18
 
 
19
 
CDecoder::CDecoder(bool wimMode):
20
 
  _keepHistory(false),
21
 
  _skipByte(false),
22
 
  _wimMode(wimMode)
23
 
{
24
 
  m_x86ConvertOutStreamSpec = new Cx86ConvertOutStream;
25
 
  m_x86ConvertOutStream = m_x86ConvertOutStreamSpec;
26
 
}
27
 
 
28
 
void CDecoder::ReleaseStreams()
29
 
{
30
 
  m_OutWindowStream.ReleaseStream();
31
 
  m_InBitStream.ReleaseStream();
32
 
  m_x86ConvertOutStreamSpec->ReleaseStream();
33
 
}
34
 
 
35
 
STDMETHODIMP CDecoder::Flush()
36
 
{
37
 
  RINOK(m_OutWindowStream.Flush());
38
 
  return m_x86ConvertOutStreamSpec->Flush();
39
 
}
40
 
 
41
 
UInt32 CDecoder::ReadBits(int numBits) { return m_InBitStream.ReadBits(numBits); }
42
 
 
43
 
#define RIF(x) { if (!(x)) return false; }
44
 
 
45
 
bool CDecoder::ReadTable(Byte *lastLevels, Byte *newLevels, UInt32 numSymbols)
46
 
{
47
 
  Byte levelLevels[kLevelTableSize];
48
 
  UInt32 i;
49
 
  for (i = 0; i < kLevelTableSize; i++)
50
 
    levelLevels[i] = (Byte)ReadBits(kNumBitsForPreTreeLevel);
51
 
  RIF(m_LevelDecoder.SetCodeLengths(levelLevels));
52
 
  int num = 0;
53
 
  Byte symbol = 0;
54
 
  for (i = 0; i < numSymbols;)
55
 
  {
56
 
    if (num != 0)
57
 
    {
58
 
      lastLevels[i] = newLevels[i] = symbol;
59
 
      i++;
60
 
      num--;
61
 
      continue;
62
 
    }
63
 
    UInt32 number = m_LevelDecoder.DecodeSymbol(&m_InBitStream);
64
 
    if (number == kLevelSymbolZeros)
65
 
    {
66
 
      num = kLevelSymbolZerosStartValue + (int)ReadBits(kLevelSymbolZerosNumBits);
67
 
      symbol = 0;
68
 
    }
69
 
    else if (number == kLevelSymbolZerosBig)
70
 
    {
71
 
      num = kLevelSymbolZerosBigStartValue + (int)ReadBits(kLevelSymbolZerosBigNumBits);
72
 
      symbol = 0;
73
 
    }
74
 
    else if (number == kLevelSymbolSame || number <= kNumHuffmanBits)
75
 
    {
76
 
      if (number <= kNumHuffmanBits)
77
 
        num = 1;
78
 
      else
79
 
      {
80
 
        num = kLevelSymbolSameStartValue + (int)ReadBits(kLevelSymbolSameNumBits);
81
 
        number = m_LevelDecoder.DecodeSymbol(&m_InBitStream);
82
 
        if (number > kNumHuffmanBits)
83
 
          return false;
84
 
      }
85
 
      symbol = Byte((17 + lastLevels[i] - number) % (kNumHuffmanBits + 1));
86
 
    }
87
 
    else
88
 
      return false;
89
 
  }
90
 
  return true;
91
 
}
92
 
 
93
 
bool CDecoder::ReadTables(void)
94
 
{
95
 
  Byte newLevels[kMaxTableSize];
96
 
  {
97
 
    if (_skipByte)
98
 
      m_InBitStream.DirectReadByte();
99
 
    m_InBitStream.Normalize();
100
 
 
101
 
    int blockType = (int)ReadBits(kNumBlockTypeBits);
102
 
    if (blockType > kBlockTypeUncompressed)
103
 
      return false;
104
 
    if (_wimMode)
105
 
      if (ReadBits(1) == 1)
106
 
        m_UnCompressedBlockSize = (1 << 15);
107
 
      else
108
 
        m_UnCompressedBlockSize = ReadBits(16);
109
 
    else
110
 
      m_UnCompressedBlockSize = m_InBitStream.ReadBitsBig(kUncompressedBlockSizeNumBits);
111
 
 
112
 
    m_IsUncompressedBlock = (blockType == kBlockTypeUncompressed);
113
 
 
114
 
    _skipByte = (m_IsUncompressedBlock && ((m_UnCompressedBlockSize & 1) != 0));
115
 
 
116
 
    if (m_IsUncompressedBlock)
117
 
    {
118
 
      ReadBits(16 - m_InBitStream.GetBitPosition());
119
 
      if (!m_InBitStream.ReadUInt32(m_RepDistances[0]))
120
 
        return false;
121
 
      m_RepDistances[0]--;
122
 
      for (int i = 1; i < kNumRepDistances; i++)
123
 
      {
124
 
        UInt32 rep = 0;
125
 
        for (int j = 0; j < 4; j++)
126
 
          rep |= (UInt32)m_InBitStream.DirectReadByte() << (8 * j);
127
 
        m_RepDistances[i] = rep - 1;
128
 
      }
129
 
      return true;
130
 
    }
131
 
    m_AlignIsUsed = (blockType == kBlockTypeAligned);
132
 
    if (m_AlignIsUsed)
133
 
    {
134
 
      for(int i = 0; i < kAlignTableSize; i++)
135
 
        newLevels[i] = (Byte)ReadBits(kNumBitsForAlignLevel);
136
 
      RIF(m_AlignDecoder.SetCodeLengths(newLevels));
137
 
    }
138
 
  }
139
 
 
140
 
  RIF(ReadTable(m_LastMainLevels, newLevels, 256));
141
 
  RIF(ReadTable(m_LastMainLevels + 256, newLevels + 256, m_NumPosLenSlots));
142
 
  for (UInt32 i = 256 + m_NumPosLenSlots; i < kMainTableSize; i++)
143
 
    newLevels[i] = 0;
144
 
  RIF(m_MainDecoder.SetCodeLengths(newLevels));
145
 
 
146
 
  RIF(ReadTable(m_LastLenLevels, newLevels, kNumLenSymbols));
147
 
  return m_LenDecoder.SetCodeLengths(newLevels);
148
 
}
149
 
 
150
 
class CDecoderFlusher
151
 
{
152
 
  CDecoder *m_Decoder;
153
 
public:
154
 
  bool NeedFlush;
155
 
  CDecoderFlusher(CDecoder *decoder): m_Decoder(decoder), NeedFlush(true) {}
156
 
  ~CDecoderFlusher()
157
 
  {
158
 
    if (NeedFlush)
159
 
      m_Decoder->Flush();
160
 
    m_Decoder->ReleaseStreams();
161
 
  }
162
 
};
163
 
 
164
 
 
165
 
void CDecoder::ClearPrevLevels()
166
 
{
167
 
  int i;
168
 
  for (i = 0; i < kMainTableSize; i++)
169
 
    m_LastMainLevels[i] = 0;
170
 
  for (i = 0; i < kNumLenSymbols; i++)
171
 
    m_LastLenLevels[i] = 0;
172
 
};
173
 
 
174
 
 
175
 
HRESULT CDecoder::CodeSpec(UInt32 curSize)
176
 
{
177
 
  if (_remainLen == kLenIdNeedInit)
178
 
  {
179
 
    _remainLen = 0;
180
 
    m_InBitStream.Init();
181
 
    if (!_keepHistory || !m_IsUncompressedBlock)
182
 
      m_InBitStream.Normalize();
183
 
    if (!_keepHistory)
184
 
    {
185
 
      _skipByte = false;
186
 
      m_UnCompressedBlockSize = 0;
187
 
      ClearPrevLevels();
188
 
      UInt32 i86TranslationSize = 12000000;
189
 
      bool translationMode = true;
190
 
      if (!_wimMode)
191
 
      {
192
 
        translationMode = (ReadBits(1) != 0);
193
 
        if (translationMode)
194
 
        {
195
 
          i86TranslationSize = ReadBits(16) << 16;
196
 
          i86TranslationSize |= ReadBits(16);
197
 
        }
198
 
      }
199
 
      m_x86ConvertOutStreamSpec->Init(translationMode, i86TranslationSize);
200
 
      
201
 
      for(int i = 0 ; i < kNumRepDistances; i++)
202
 
        m_RepDistances[i] = 0;
203
 
    }
204
 
  }
205
 
 
206
 
  while(_remainLen > 0 && curSize > 0)
207
 
  {
208
 
    m_OutWindowStream.PutByte(m_OutWindowStream.GetByte(m_RepDistances[0]));
209
 
    _remainLen--;
210
 
    curSize--;
211
 
  }
212
 
 
213
 
  while(curSize > 0)
214
 
  {
215
 
    if (m_UnCompressedBlockSize == 0)
216
 
      if (!ReadTables())
217
 
        return S_FALSE;
218
 
    UInt32 next = (Int32)MyMin(m_UnCompressedBlockSize, curSize);
219
 
    curSize -= next;
220
 
    m_UnCompressedBlockSize -= next;
221
 
    if (m_IsUncompressedBlock)
222
 
    {
223
 
      while(next > 0)
224
 
      {
225
 
        m_OutWindowStream.PutByte(m_InBitStream.DirectReadByte());
226
 
        next--;
227
 
      }
228
 
    }
229
 
    else while(next > 0)
230
 
    {
231
 
      UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream);
232
 
      if (number < 256)
233
 
      {
234
 
        m_OutWindowStream.PutByte((Byte)number);
235
 
        next--;
236
 
      }
237
 
      else
238
 
      {
239
 
        UInt32 posLenSlot = number - 256;
240
 
        if (posLenSlot >= m_NumPosLenSlots)
241
 
          return S_FALSE;
242
 
        UInt32 posSlot = posLenSlot / kNumLenSlots;
243
 
        UInt32 lenSlot = posLenSlot % kNumLenSlots;
244
 
        UInt32 len = kMatchMinLen + lenSlot;
245
 
        if (lenSlot == kNumLenSlots - 1)
246
 
        {
247
 
          UInt32 lenTemp = m_LenDecoder.DecodeSymbol(&m_InBitStream);
248
 
          if (lenTemp >= kNumLenSymbols)
249
 
            return S_FALSE;
250
 
          len += lenTemp;
251
 
        }
252
 
        
253
 
        if (posSlot < kNumRepDistances)
254
 
        {
255
 
          UInt32 distance = m_RepDistances[posSlot];
256
 
          m_RepDistances[posSlot] = m_RepDistances[0];
257
 
          m_RepDistances[0] = distance;
258
 
        }
259
 
        else
260
 
        {
261
 
          UInt32 distance;
262
 
          int numDirectBits;
263
 
          if (posSlot < kNumPowerPosSlots)
264
 
          {
265
 
            numDirectBits = (int)(posSlot >> 1) - 1;
266
 
            distance = ((2 | (posSlot & 1)) << numDirectBits);
267
 
          }
268
 
          else
269
 
          {
270
 
            numDirectBits = kNumLinearPosSlotBits;
271
 
            distance = ((posSlot - 0x22) << kNumLinearPosSlotBits);
272
 
          }
273
 
 
274
 
          if (m_AlignIsUsed && numDirectBits >= kNumAlignBits)
275
 
          {
276
 
            distance += (m_InBitStream.ReadBits(numDirectBits - kNumAlignBits) << kNumAlignBits);
277
 
            UInt32 alignTemp = m_AlignDecoder.DecodeSymbol(&m_InBitStream);
278
 
            if (alignTemp >= kAlignTableSize)
279
 
              return S_FALSE;
280
 
            distance += alignTemp;
281
 
          }
282
 
          else
283
 
            distance += m_InBitStream.ReadBits(numDirectBits);
284
 
          m_RepDistances[2] = m_RepDistances[1];
285
 
          m_RepDistances[1] = m_RepDistances[0];
286
 
          m_RepDistances[0] = distance - kNumRepDistances;
287
 
        }
288
 
 
289
 
        UInt32 locLen = len;
290
 
        if (locLen > next)
291
 
          locLen = next;
292
 
 
293
 
        if (!m_OutWindowStream.CopyBlock(m_RepDistances[0], locLen))
294
 
          return S_FALSE;
295
 
 
296
 
        len -= locLen;
297
 
        next -= locLen;
298
 
        if (len != 0)
299
 
        {
300
 
          _remainLen = (int)len;
301
 
          return S_OK;
302
 
        }
303
 
      }
304
 
    }
305
 
  }
306
 
  return S_OK;
307
 
}
308
 
 
309
 
HRESULT CDecoder::CodeReal(ISequentialInStream *inStream,
310
 
    ISequentialOutStream *outStream, 
311
 
    const UInt64 *, const UInt64 *outSize,
312
 
    ICompressProgressInfo *progress)
313
 
{
314
 
  if (outSize == NULL)
315
 
    return E_INVALIDARG;
316
 
  UInt64 size = *outSize;
317
 
 
318
 
  RINOK(SetInStream(inStream));
319
 
  m_x86ConvertOutStreamSpec->SetStream(outStream);
320
 
  m_OutWindowStream.SetStream(m_x86ConvertOutStream);
321
 
  RINOK(SetOutStreamSize(outSize));
322
 
 
323
 
  CDecoderFlusher flusher(this);
324
 
 
325
 
  const UInt64 start = m_OutWindowStream.GetProcessedSize();
326
 
  for (;;)
327
 
  {
328
 
    UInt32 curSize = 1 << 18;
329
 
    UInt64 rem = size - (m_OutWindowStream.GetProcessedSize() - start);
330
 
    if (curSize > rem)
331
 
      curSize = (UInt32)rem;
332
 
    if (curSize == 0)
333
 
      break;
334
 
    RINOK(CodeSpec(curSize));
335
 
    if (progress != NULL)
336
 
    {
337
 
      UInt64 inSize = m_InBitStream.GetProcessedSize();
338
 
      UInt64 nowPos64 = m_OutWindowStream.GetProcessedSize() - start;
339
 
      RINOK(progress->SetRatioInfo(&inSize, &nowPos64));
340
 
    }
341
 
  } 
342
 
  flusher.NeedFlush = false;
343
 
  return Flush();
344
 
}
345
 
 
346
 
HRESULT CDecoder::Code(ISequentialInStream *inStream,
347
 
    ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize,
348
 
    ICompressProgressInfo *progress)
349
 
{
350
 
  try { return CodeReal(inStream, outStream, inSize, outSize, progress); }
351
 
  catch(const CLZOutWindowException &e) { return e.ErrorCode; }
352
 
  catch(...) { return S_FALSE; }
353
 
}
354
 
 
355
 
STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream)
356
 
{
357
 
  m_InBitStream.SetStream(inStream);
358
 
  return S_OK;
359
 
}
360
 
 
361
 
STDMETHODIMP CDecoder::ReleaseInStream()
362
 
{
363
 
  m_InBitStream.ReleaseStream();
364
 
  return S_OK;
365
 
}
366
 
 
367
 
STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize)
368
 
{
369
 
  if (outSize == NULL)
370
 
    return E_FAIL;
371
 
  _remainLen = kLenIdNeedInit;
372
 
  m_OutWindowStream.Init(_keepHistory);
373
 
  return S_OK;
374
 
}
375
 
 
376
 
HRESULT CDecoder::SetParams(int numDictBits)
377
 
378
 
  if (numDictBits < kNumDictionaryBitsMin || numDictBits > kNumDictionaryBitsMax)
379
 
    return E_INVALIDARG;
380
 
  UInt32 numPosSlots;
381
 
  if (numDictBits < 20)
382
 
    numPosSlots = 30 + (numDictBits - 15) * 2;
383
 
  else if (numDictBits == 20)
384
 
    numPosSlots = 42;
385
 
  else
386
 
    numPosSlots = 50;
387
 
  m_NumPosLenSlots = numPosSlots * kNumLenSlots;
388
 
  if (!m_OutWindowStream.Create(kDictionarySizeMax))
389
 
    return E_OUTOFMEMORY;
390
 
  if (!m_InBitStream.Create(1 << 16))
391
 
    return E_OUTOFMEMORY;
392
 
  return S_OK;
393
 
}
394
 
 
395
 
}}