| OLD | NEW |
| (Empty) |
| 1 // LzmaDecoder.cpp | |
| 2 | |
| 3 #include "StdAfx.h" | |
| 4 | |
| 5 extern "C" | |
| 6 { | |
| 7 #include "../../../C/Alloc.h" | |
| 8 } | |
| 9 | |
| 10 #include "../Common/StreamUtils.h" | |
| 11 | |
| 12 #include "LzmaDecoder.h" | |
| 13 | |
| 14 static HRESULT SResToHRESULT(SRes res) | |
| 15 { | |
| 16 switch(res) | |
| 17 { | |
| 18 case SZ_OK: return S_OK; | |
| 19 case SZ_ERROR_MEM: return E_OUTOFMEMORY; | |
| 20 case SZ_ERROR_PARAM: return E_INVALIDARG; | |
| 21 case SZ_ERROR_UNSUPPORTED: return E_NOTIMPL; | |
| 22 // case SZ_ERROR_PROGRESS: return E_ABORT; | |
| 23 case SZ_ERROR_DATA: return S_FALSE; | |
| 24 } | |
| 25 return E_FAIL; | |
| 26 } | |
| 27 | |
| 28 namespace NCompress { | |
| 29 namespace NLzma { | |
| 30 | |
| 31 static const UInt32 kInBufSize = 1 << 20; | |
| 32 | |
| 33 CDecoder::CDecoder(): _inBuf(0), _outSizeDefined(false), FinishStream(false) | |
| 34 { | |
| 35 LzmaDec_Construct(&_state); | |
| 36 } | |
| 37 | |
| 38 static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); } | |
| 39 static void SzFree(void *p, void *address) { p = p; MyFree(address); } | |
| 40 static ISzAlloc g_Alloc = { SzAlloc, SzFree }; | |
| 41 | |
| 42 CDecoder::~CDecoder() | |
| 43 { | |
| 44 LzmaDec_Free(&_state, &g_Alloc); | |
| 45 MyFree(_inBuf); | |
| 46 } | |
| 47 | |
| 48 STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *prop, UInt32 size) | |
| 49 { | |
| 50 RINOK(SResToHRESULT(LzmaDec_Allocate(&_state, prop, size, &g_Alloc))); | |
| 51 | |
| 52 if (_inBuf == 0) | |
| 53 { | |
| 54 _inBuf = (Byte *)MyAlloc(kInBufSize); | |
| 55 if (_inBuf == 0) | |
| 56 return E_OUTOFMEMORY; | |
| 57 } | |
| 58 | |
| 59 return S_OK; | |
| 60 } | |
| 61 | |
| 62 STDMETHODIMP CDecoder::GetInStreamProcessedSize(UInt64 *value) { *value = _inSiz
eProcessed; return S_OK; } | |
| 63 STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream) { _inStream =
inStream; return S_OK; } | |
| 64 STDMETHODIMP CDecoder::ReleaseInStream() { _inStream.Release(); return S_OK; } | |
| 65 | |
| 66 STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize) | |
| 67 { | |
| 68 _outSizeDefined = (outSize != NULL); | |
| 69 if (_outSizeDefined) | |
| 70 _outSize = *outSize; | |
| 71 | |
| 72 LzmaDec_Init(&_state); | |
| 73 | |
| 74 _inPos = _inSize = 0; | |
| 75 _inSizeProcessed = _outSizeProcessed = 0; | |
| 76 return S_OK; | |
| 77 } | |
| 78 | |
| 79 STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream
*outStream, | |
| 80 const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *p
rogress) | |
| 81 { | |
| 82 if (_inBuf == 0) | |
| 83 return S_FALSE; | |
| 84 SetOutStreamSize(outSize); | |
| 85 | |
| 86 for (;;) | |
| 87 { | |
| 88 if (_inPos == _inSize) | |
| 89 { | |
| 90 _inPos = _inSize = 0; | |
| 91 RINOK(inStream->Read(_inBuf, kInBufSize, &_inSize)); | |
| 92 } | |
| 93 | |
| 94 SizeT dicPos = _state.dicPos; | |
| 95 SizeT curSize = _state.dicBufSize - dicPos; | |
| 96 const UInt32 kStepSize = ((UInt32)1 << 22); | |
| 97 if (curSize > kStepSize) | |
| 98 curSize = (SizeT)kStepSize; | |
| 99 | |
| 100 ELzmaFinishMode finishMode = LZMA_FINISH_ANY; | |
| 101 if (_outSizeDefined) | |
| 102 { | |
| 103 const UInt64 rem = _outSize - _outSizeProcessed; | |
| 104 if (rem < curSize) | |
| 105 { | |
| 106 curSize = (SizeT)rem; | |
| 107 if (FinishStream) | |
| 108 finishMode = LZMA_FINISH_END; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 SizeT inSizeProcessed = _inSize - _inPos; | |
| 113 ELzmaStatus status; | |
| 114 SRes res = LzmaDec_DecodeToDic(&_state, dicPos + curSize, _inBuf + _inPos, &
inSizeProcessed, finishMode, &status); | |
| 115 | |
| 116 _inPos += (UInt32)inSizeProcessed; | |
| 117 _inSizeProcessed += inSizeProcessed; | |
| 118 SizeT outSizeProcessed = _state.dicPos - dicPos; | |
| 119 _outSizeProcessed += outSizeProcessed; | |
| 120 | |
| 121 bool finished = (inSizeProcessed == 0 && outSizeProcessed == 0); | |
| 122 bool stopDecoding = (_outSizeDefined && _outSizeProcessed >= _outSize); | |
| 123 | |
| 124 if (res != 0 || _state.dicPos == _state.dicBufSize || finished || stopDecodi
ng) | |
| 125 { | |
| 126 HRESULT res2 = WriteStream(outStream, _state.dic, _state.dicPos); | |
| 127 if (res != 0) | |
| 128 return S_FALSE; | |
| 129 RINOK(res2); | |
| 130 if (stopDecoding) | |
| 131 return S_OK; | |
| 132 if (finished) | |
| 133 return (status == LZMA_STATUS_FINISHED_WITH_MARK ? S_OK : S_FALSE); | |
| 134 } | |
| 135 if (_state.dicPos == _state.dicBufSize) | |
| 136 _state.dicPos = 0; | |
| 137 | |
| 138 if (progress != NULL) | |
| 139 { | |
| 140 RINOK(progress->SetRatioInfo(&_inSizeProcessed, &_outSizeProcessed)); | |
| 141 } | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 #ifndef NO_READ_FROM_CODER | |
| 146 | |
| 147 STDMETHODIMP CDecoder::Read(void *data, UInt32 size, UInt32 *processedSize) | |
| 148 { | |
| 149 if (processedSize) | |
| 150 *processedSize = 0; | |
| 151 do | |
| 152 { | |
| 153 if (_inPos == _inSize) | |
| 154 { | |
| 155 _inPos = _inSize = 0; | |
| 156 RINOK(_inStream->Read(_inBuf, kInBufSize, &_inSize)); | |
| 157 } | |
| 158 { | |
| 159 SizeT inProcessed = _inSize - _inPos; | |
| 160 | |
| 161 if (_outSizeDefined) | |
| 162 { | |
| 163 const UInt64 rem = _outSize - _outSizeProcessed; | |
| 164 if (rem < size) | |
| 165 size = (UInt32)rem; | |
| 166 } | |
| 167 | |
| 168 SizeT outProcessed = size; | |
| 169 ELzmaStatus status; | |
| 170 SRes res = LzmaDec_DecodeToBuf(&_state, (Byte *)data, &outProcessed, | |
| 171 _inBuf + _inPos, &inProcessed, LZMA_FINISH_ANY, &status); | |
| 172 _inPos += (UInt32)inProcessed; | |
| 173 _inSizeProcessed += inProcessed; | |
| 174 _outSizeProcessed += outProcessed; | |
| 175 size -= (UInt32)outProcessed; | |
| 176 data = (Byte *)data + outProcessed; | |
| 177 if (processedSize) | |
| 178 *processedSize += (UInt32)outProcessed; | |
| 179 RINOK(SResToHRESULT(res)); | |
| 180 if (inProcessed == 0 && outProcessed == 0) | |
| 181 return S_OK; | |
| 182 } | |
| 183 } | |
| 184 while (size != 0); | |
| 185 return S_OK; | |
| 186 } | |
| 187 | |
| 188 #endif | |
| 189 | |
| 190 }} | |
| OLD | NEW |