other-licenses/7zstub/src/7zip/Compress/LZMA/LZMADecoder.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 // LZMADecoder.cpp
michael@0 2
michael@0 3 #include "StdAfx.h"
michael@0 4
michael@0 5 #include "LZMADecoder.h"
michael@0 6 #include "../../../Common/Defs.h"
michael@0 7
michael@0 8 namespace NCompress {
michael@0 9 namespace NLZMA {
michael@0 10
michael@0 11 const int kLenIdFinished = -1;
michael@0 12 const int kLenIdNeedInit = -2;
michael@0 13
michael@0 14 void CDecoder::Init()
michael@0 15 {
michael@0 16 {
michael@0 17 for(int i = 0; i < kNumStates; i++)
michael@0 18 {
michael@0 19 for (UInt32 j = 0; j <= _posStateMask; j++)
michael@0 20 {
michael@0 21 _isMatch[i][j].Init();
michael@0 22 _isRep0Long[i][j].Init();
michael@0 23 }
michael@0 24 _isRep[i].Init();
michael@0 25 _isRepG0[i].Init();
michael@0 26 _isRepG1[i].Init();
michael@0 27 _isRepG2[i].Init();
michael@0 28 }
michael@0 29 }
michael@0 30 {
michael@0 31 for (UInt32 i = 0; i < kNumLenToPosStates; i++)
michael@0 32 _posSlotDecoder[i].Init();
michael@0 33 }
michael@0 34 {
michael@0 35 for(UInt32 i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
michael@0 36 _posDecoders[i].Init();
michael@0 37 }
michael@0 38 _posAlignDecoder.Init();
michael@0 39 _lenDecoder.Init(_posStateMask + 1);
michael@0 40 _repMatchLenDecoder.Init(_posStateMask + 1);
michael@0 41 _literalDecoder.Init();
michael@0 42
michael@0 43 _state.Init();
michael@0 44 _reps[0] = _reps[1] = _reps[2] = _reps[3] = 0;
michael@0 45 }
michael@0 46
michael@0 47 HRESULT CDecoder::CodeSpec(UInt32 curSize)
michael@0 48 {
michael@0 49 if (_outSizeDefined)
michael@0 50 {
michael@0 51 const UInt64 rem = _outSize - _outWindowStream.GetProcessedSize();
michael@0 52 if (curSize > rem)
michael@0 53 curSize = (UInt32)rem;
michael@0 54 }
michael@0 55
michael@0 56 if (_remainLen == kLenIdFinished)
michael@0 57 return S_OK;
michael@0 58 if (_remainLen == kLenIdNeedInit)
michael@0 59 {
michael@0 60 _rangeDecoder.Init();
michael@0 61 Init();
michael@0 62 _remainLen = 0;
michael@0 63 }
michael@0 64 if (curSize == 0)
michael@0 65 return S_OK;
michael@0 66
michael@0 67 UInt32 rep0 = _reps[0];
michael@0 68 UInt32 rep1 = _reps[1];
michael@0 69 UInt32 rep2 = _reps[2];
michael@0 70 UInt32 rep3 = _reps[3];
michael@0 71 CState state = _state;
michael@0 72 Byte previousByte;
michael@0 73
michael@0 74 while(_remainLen > 0 && curSize > 0)
michael@0 75 {
michael@0 76 previousByte = _outWindowStream.GetByte(rep0);
michael@0 77 _outWindowStream.PutByte(previousByte);
michael@0 78 _remainLen--;
michael@0 79 curSize--;
michael@0 80 }
michael@0 81 UInt64 nowPos64 = _outWindowStream.GetProcessedSize();
michael@0 82 if (nowPos64 == 0)
michael@0 83 previousByte = 0;
michael@0 84 else
michael@0 85 previousByte = _outWindowStream.GetByte(0);
michael@0 86
michael@0 87 while(curSize > 0)
michael@0 88 {
michael@0 89 {
michael@0 90 #ifdef _NO_EXCEPTIONS
michael@0 91 if (_rangeDecoder.Stream.ErrorCode != S_OK)
michael@0 92 return _rangeDecoder.Stream.ErrorCode;
michael@0 93 #endif
michael@0 94 if (_rangeDecoder.Stream.WasFinished())
michael@0 95 return S_FALSE;
michael@0 96 UInt32 posState = UInt32(nowPos64) & _posStateMask;
michael@0 97 if (_isMatch[state.Index][posState].Decode(&_rangeDecoder) == 0)
michael@0 98 {
michael@0 99 if(!state.IsCharState())
michael@0 100 previousByte = _literalDecoder.DecodeWithMatchByte(&_rangeDecoder,
michael@0 101 (UInt32)nowPos64, previousByte, _outWindowStream.GetByte(rep0));
michael@0 102 else
michael@0 103 previousByte = _literalDecoder.DecodeNormal(&_rangeDecoder,
michael@0 104 (UInt32)nowPos64, previousByte);
michael@0 105 _outWindowStream.PutByte(previousByte);
michael@0 106 state.UpdateChar();
michael@0 107 curSize--;
michael@0 108 nowPos64++;
michael@0 109 }
michael@0 110 else
michael@0 111 {
michael@0 112 UInt32 len;
michael@0 113 if(_isRep[state.Index].Decode(&_rangeDecoder) == 1)
michael@0 114 {
michael@0 115 len = 0;
michael@0 116 if(_isRepG0[state.Index].Decode(&_rangeDecoder) == 0)
michael@0 117 {
michael@0 118 if(_isRep0Long[state.Index][posState].Decode(&_rangeDecoder) == 0)
michael@0 119 {
michael@0 120 state.UpdateShortRep();
michael@0 121 len = 1;
michael@0 122 }
michael@0 123 }
michael@0 124 else
michael@0 125 {
michael@0 126 UInt32 distance;
michael@0 127 if(_isRepG1[state.Index].Decode(&_rangeDecoder) == 0)
michael@0 128 distance = rep1;
michael@0 129 else
michael@0 130 {
michael@0 131 if (_isRepG2[state.Index].Decode(&_rangeDecoder) == 0)
michael@0 132 distance = rep2;
michael@0 133 else
michael@0 134 {
michael@0 135 distance = rep3;
michael@0 136 rep3 = rep2;
michael@0 137 }
michael@0 138 rep2 = rep1;
michael@0 139 }
michael@0 140 rep1 = rep0;
michael@0 141 rep0 = distance;
michael@0 142 }
michael@0 143 if (len == 0)
michael@0 144 {
michael@0 145 len = _repMatchLenDecoder.Decode(&_rangeDecoder, posState) + kMatchMinLen;
michael@0 146 state.UpdateRep();
michael@0 147 }
michael@0 148 }
michael@0 149 else
michael@0 150 {
michael@0 151 rep3 = rep2;
michael@0 152 rep2 = rep1;
michael@0 153 rep1 = rep0;
michael@0 154 len = kMatchMinLen + _lenDecoder.Decode(&_rangeDecoder, posState);
michael@0 155 state.UpdateMatch();
michael@0 156 UInt32 posSlot = _posSlotDecoder[GetLenToPosState(len)].Decode(&_rangeDecoder);
michael@0 157 if (posSlot >= kStartPosModelIndex)
michael@0 158 {
michael@0 159 UInt32 numDirectBits = (posSlot >> 1) - 1;
michael@0 160 rep0 = ((2 | (posSlot & 1)) << numDirectBits);
michael@0 161
michael@0 162 if (posSlot < kEndPosModelIndex)
michael@0 163 rep0 += NRangeCoder::ReverseBitTreeDecode(_posDecoders +
michael@0 164 rep0 - posSlot - 1, &_rangeDecoder, numDirectBits);
michael@0 165 else
michael@0 166 {
michael@0 167 rep0 += (_rangeDecoder.DecodeDirectBits(
michael@0 168 numDirectBits - kNumAlignBits) << kNumAlignBits);
michael@0 169 rep0 += _posAlignDecoder.ReverseDecode(&_rangeDecoder);
michael@0 170 if (rep0 == 0xFFFFFFFF)
michael@0 171 {
michael@0 172 _remainLen = kLenIdFinished;
michael@0 173 return S_OK;
michael@0 174 }
michael@0 175 }
michael@0 176 }
michael@0 177 else
michael@0 178 rep0 = posSlot;
michael@0 179 }
michael@0 180 UInt32 locLen = len;
michael@0 181 if (len > curSize)
michael@0 182 locLen = (UInt32)curSize;
michael@0 183 if (!_outWindowStream.CopyBlock(rep0, locLen))
michael@0 184 return S_FALSE;
michael@0 185 previousByte = _outWindowStream.GetByte(0);
michael@0 186 curSize -= locLen;
michael@0 187 nowPos64 += locLen;
michael@0 188 len -= locLen;
michael@0 189 if (len != 0)
michael@0 190 {
michael@0 191 _remainLen = (Int32)len;
michael@0 192 break;
michael@0 193 }
michael@0 194
michael@0 195 #ifdef _NO_EXCEPTIONS
michael@0 196 if (_outWindowStream.ErrorCode != S_OK)
michael@0 197 return _outWindowStream.ErrorCode;
michael@0 198 #endif
michael@0 199 }
michael@0 200 }
michael@0 201 }
michael@0 202 if (_rangeDecoder.Stream.WasFinished())
michael@0 203 return S_FALSE;
michael@0 204 _reps[0] = rep0;
michael@0 205 _reps[1] = rep1;
michael@0 206 _reps[2] = rep2;
michael@0 207 _reps[3] = rep3;
michael@0 208 _state = state;
michael@0 209
michael@0 210 return S_OK;
michael@0 211 }
michael@0 212
michael@0 213 STDMETHODIMP CDecoder::CodeReal(ISequentialInStream *inStream,
michael@0 214 ISequentialOutStream *outStream,
michael@0 215 const UInt64 *, const UInt64 *outSize,
michael@0 216 ICompressProgressInfo *progress)
michael@0 217 {
michael@0 218 SetInStream(inStream);
michael@0 219 _outWindowStream.SetStream(outStream);
michael@0 220 SetOutStreamSize(outSize);
michael@0 221 CDecoderFlusher flusher(this);
michael@0 222
michael@0 223 while (true)
michael@0 224 {
michael@0 225 UInt32 curSize = 1 << 18;
michael@0 226 RINOK(CodeSpec(curSize));
michael@0 227 if (_remainLen == kLenIdFinished)
michael@0 228 break;
michael@0 229 if (progress != NULL)
michael@0 230 {
michael@0 231 UInt64 inSize = _rangeDecoder.GetProcessedSize();
michael@0 232 UInt64 nowPos64 = _outWindowStream.GetProcessedSize();
michael@0 233 RINOK(progress->SetRatioInfo(&inSize, &nowPos64));
michael@0 234 }
michael@0 235 if (_outSizeDefined)
michael@0 236 if (_outWindowStream.GetProcessedSize() >= _outSize)
michael@0 237 break;
michael@0 238 }
michael@0 239 flusher.NeedFlush = false;
michael@0 240 return Flush();
michael@0 241 }
michael@0 242
michael@0 243
michael@0 244 #ifdef _NO_EXCEPTIONS
michael@0 245
michael@0 246 #define LZMA_TRY_BEGIN
michael@0 247 #define LZMA_TRY_END
michael@0 248
michael@0 249 #else
michael@0 250
michael@0 251 #define LZMA_TRY_BEGIN try {
michael@0 252 #define LZMA_TRY_END } \
michael@0 253 catch(const CInBufferException &e) { return e.ErrorCode; } \
michael@0 254 catch(const CLZOutWindowException &e) { return e.ErrorCode; } \
michael@0 255 catch(...) { return S_FALSE; }
michael@0 256
michael@0 257 #endif
michael@0 258
michael@0 259
michael@0 260 STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream,
michael@0 261 ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize,
michael@0 262 ICompressProgressInfo *progress)
michael@0 263 {
michael@0 264 LZMA_TRY_BEGIN
michael@0 265 return CodeReal(inStream, outStream, inSize, outSize, progress);
michael@0 266 LZMA_TRY_END
michael@0 267 }
michael@0 268
michael@0 269 STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *properties, UInt32 size)
michael@0 270 {
michael@0 271 if (size < 5)
michael@0 272 return E_INVALIDARG;
michael@0 273 int lc = properties[0] % 9;
michael@0 274 Byte remainder = (Byte)(properties[0] / 9);
michael@0 275 int lp = remainder % 5;
michael@0 276 int pb = remainder / 5;
michael@0 277 if (pb > NLength::kNumPosStatesBitsMax)
michael@0 278 return E_INVALIDARG;
michael@0 279 _posStateMask = (1 << pb) - 1;
michael@0 280 UInt32 dictionarySize = 0;
michael@0 281 for (int i = 0; i < 4; i++)
michael@0 282 dictionarySize += ((UInt32)(properties[1 + i])) << (i * 8);
michael@0 283 if (!_outWindowStream.Create(dictionarySize))
michael@0 284 return E_OUTOFMEMORY;
michael@0 285 if (!_literalDecoder.Create(lp, lc))
michael@0 286 return E_OUTOFMEMORY;
michael@0 287 if (!_rangeDecoder.Create(1 << 20))
michael@0 288 return E_OUTOFMEMORY;
michael@0 289 return S_OK;
michael@0 290 }
michael@0 291
michael@0 292 STDMETHODIMP CDecoder::GetInStreamProcessedSize(UInt64 *value)
michael@0 293 {
michael@0 294 *value = _rangeDecoder.GetProcessedSize();
michael@0 295 return S_OK;
michael@0 296 }
michael@0 297
michael@0 298 STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream)
michael@0 299 {
michael@0 300 _rangeDecoder.SetStream(inStream);
michael@0 301 return S_OK;
michael@0 302 }
michael@0 303
michael@0 304 STDMETHODIMP CDecoder::ReleaseInStream()
michael@0 305 {
michael@0 306 _rangeDecoder.ReleaseStream();
michael@0 307 return S_OK;
michael@0 308 }
michael@0 309
michael@0 310 STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize)
michael@0 311 {
michael@0 312 if (_outSizeDefined = (outSize != NULL))
michael@0 313 _outSize = *outSize;
michael@0 314 _remainLen = kLenIdNeedInit;
michael@0 315 _outWindowStream.Init();
michael@0 316 return S_OK;
michael@0 317 }
michael@0 318
michael@0 319 #ifdef _ST_MODE
michael@0 320
michael@0 321 STDMETHODIMP CDecoder::Read(void *data, UInt32 size, UInt32 *processedSize)
michael@0 322 {
michael@0 323 LZMA_TRY_BEGIN
michael@0 324 if (processedSize)
michael@0 325 *processedSize = 0;
michael@0 326 const UInt64 startPos = _outWindowStream.GetProcessedSize();
michael@0 327 _outWindowStream.SetMemStream((Byte *)data);
michael@0 328 RINOK(CodeSpec(size));
michael@0 329 if (processedSize)
michael@0 330 *processedSize = (UInt32)(_outWindowStream.GetProcessedSize() - startPos);
michael@0 331 return Flush();
michael@0 332 LZMA_TRY_END
michael@0 333 }
michael@0 334
michael@0 335 #endif
michael@0 336
michael@0 337 }}

mercurial