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
|
// QuantumDecoder.cpp
#include "StdAfx.h"
#include "../../Common/Defs.h"
#include "QuantumDecoder.h"
namespace NCompress {
namespace NQuantum {
static const int kLenIdNeedInit = -2;
static const unsigned kNumLenSymbols = 27;
static const unsigned kMatchMinLen = 3;
static const unsigned kNumSimplePosSlots = 4;
static const unsigned kNumSimpleLenSlots = 6;
void CDecoder::Init()
{
m_Selector.Init(kNumSelectors);
unsigned i;
for (i = 0; i < kNumLitSelectors; i++)
m_Literals[i].Init(kNumLitSymbols);
unsigned numItems = (_numDictBits == 0 ? 1 : (_numDictBits << 1));
const unsigned kNumPosSymbolsMax[kNumMatchSelectors] = { 24, 36, 42 };
for (i = 0; i < kNumMatchSelectors; i++)
m_PosSlot[i].Init(MyMin(numItems, kNumPosSymbolsMax[i]));
m_LenSlot.Init(kNumLenSymbols);
}
HRESULT CDecoder::CodeSpec(UInt32 curSize)
{
if (_remainLen == kLenIdNeedInit)
{
if (!_keepHistory)
{
if (!_outWindowStream.Create((UInt32)1 << _numDictBits))
return E_OUTOFMEMORY;
Init();
}
if (!_rangeDecoder.Create(1 << 20))
return E_OUTOFMEMORY;
_rangeDecoder.Init();
_remainLen = 0;
}
if (curSize == 0)
return S_OK;
while (_remainLen > 0 && curSize > 0)
{
_remainLen--;
Byte b = _outWindowStream.GetByte(_rep0);
_outWindowStream.PutByte(b);
curSize--;
}
while (curSize > 0)
{
if (_rangeDecoder.Stream.WasFinished())
return S_FALSE;
unsigned selector = m_Selector.Decode(&_rangeDecoder);
if (selector < kNumLitSelectors)
{
Byte b = (Byte)((selector << (8 - kNumLitSelectorBits)) + m_Literals[selector].Decode(&_rangeDecoder));
_outWindowStream.PutByte(b);
curSize--;
}
else
{
selector -= kNumLitSelectors;
unsigned len = selector + kMatchMinLen;
if (selector == 2)
{
unsigned lenSlot = m_LenSlot.Decode(&_rangeDecoder);
if (lenSlot >= kNumSimpleLenSlots)
{
lenSlot -= 2;
int numDirectBits = (int)(lenSlot >> 2);
len += ((4 | (lenSlot & 3)) << numDirectBits) - 2;
if (numDirectBits < 6)
len += _rangeDecoder.Stream.ReadBits(numDirectBits);
}
else
len += lenSlot;
}
UInt32 rep0 = m_PosSlot[selector].Decode(&_rangeDecoder);
if (rep0 >= kNumSimplePosSlots)
{
int numDirectBits = (int)((rep0 >> 1) - 1);
rep0 = ((2 | (rep0 & 1)) << numDirectBits) + _rangeDecoder.Stream.ReadBits(numDirectBits);
}
unsigned locLen = len;
if (len > curSize)
locLen = (unsigned)curSize;
if (!_outWindowStream.CopyBlock(rep0, locLen))
return S_FALSE;
curSize -= locLen;
len -= locLen;
if (len != 0)
{
_remainLen = (int)len;
_rep0 = rep0;
break;
}
}
}
return _rangeDecoder.Stream.WasFinished() ? S_FALSE : S_OK;
}
HRESULT CDecoder::CodeReal(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *, const UInt64 *outSize, ICompressProgressInfo *progress)
{
if (outSize == NULL)
return E_INVALIDARG;
UInt64 size = *outSize;
SetInStream(inStream);
_outWindowStream.SetStream(outStream);
SetOutStreamSize(outSize);
CDecoderFlusher flusher(this);
const UInt64 start = _outWindowStream.GetProcessedSize();
for (;;)
{
UInt32 curSize = 1 << 18;
UInt64 rem = size - (_outWindowStream.GetProcessedSize() - start);
if (curSize > rem)
curSize = (UInt32)rem;
if (curSize == 0)
break;
RINOK(CodeSpec(curSize));
if (progress != NULL)
{
UInt64 inSize = _rangeDecoder.GetProcessedSize();
UInt64 nowPos64 = _outWindowStream.GetProcessedSize() - start;
RINOK(progress->SetRatioInfo(&inSize, &nowPos64));
}
}
flusher.NeedFlush = false;
return Flush();
}
STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress)
{
try { return CodeReal(inStream, outStream, inSize, outSize, progress); }
catch(const CInBufferException &e) { return e.ErrorCode; }
catch(const CLzOutWindowException &e) { return e.ErrorCode; }
catch(...) { return S_FALSE; }
}
STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream)
{
_rangeDecoder.SetStream(inStream);
return S_OK;
}
STDMETHODIMP CDecoder::ReleaseInStream()
{
_rangeDecoder.ReleaseStream();
return S_OK;
}
STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize)
{
if (outSize == NULL)
return E_FAIL;
_remainLen = kLenIdNeedInit;
_outWindowStream.Init(_keepHistory);
return S_OK;
}
}}
|