view src/win32/7zip/7z/CPP/7zip/Compress/LzmaDecoder.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line source
1 // LzmaDecoder.cpp
3 #include "StdAfx.h"
5 extern "C"
6 {
7 #include "../../../C/Alloc.h"
8 }
10 #include "../Common/StreamUtils.h"
12 #include "LzmaDecoder.h"
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 }
28 namespace NCompress {
29 namespace NLzma {
31 static const UInt32 kInBufSize = 1 << 20;
33 CDecoder::CDecoder(): _inBuf(0), _outSizeDefined(false), FinishStream(false)
34 {
35 LzmaDec_Construct(&_state);
36 }
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 };
42 CDecoder::~CDecoder()
43 {
44 LzmaDec_Free(&_state, &g_Alloc);
45 MyFree(_inBuf);
46 }
48 STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *prop, UInt32 size)
49 {
50 RINOK(SResToHRESULT(LzmaDec_Allocate(&_state, prop, size, &g_Alloc)));
52 if (_inBuf == 0)
53 {
54 _inBuf = (Byte *)MyAlloc(kInBufSize);
55 if (_inBuf == 0)
56 return E_OUTOFMEMORY;
57 }
59 return S_OK;
60 }
62 STDMETHODIMP CDecoder::GetInStreamProcessedSize(UInt64 *value) { *value = _inSizeProcessed; return S_OK; }
63 STDMETHODIMP CDecoder::SetInStream(ISequentialInStream *inStream) { _inStream = inStream; return S_OK; }
64 STDMETHODIMP CDecoder::ReleaseInStream() { _inStream.Release(); return S_OK; }
66 STDMETHODIMP CDecoder::SetOutStreamSize(const UInt64 *outSize)
67 {
68 _outSizeDefined = (outSize != NULL);
69 if (_outSizeDefined)
70 _outSize = *outSize;
72 LzmaDec_Init(&_state);
74 _inPos = _inSize = 0;
75 _inSizeProcessed = _outSizeProcessed = 0;
76 return S_OK;
77 }
79 STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
80 const UInt64 * /* inSize */, const UInt64 *outSize, ICompressProgressInfo *progress)
81 {
82 if (_inBuf == 0)
83 return S_FALSE;
84 SetOutStreamSize(outSize);
86 for (;;)
87 {
88 if (_inPos == _inSize)
89 {
90 _inPos = _inSize = 0;
91 RINOK(inStream->Read(_inBuf, kInBufSize, &_inSize));
92 }
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;
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 }
112 SizeT inSizeProcessed = _inSize - _inPos;
113 ELzmaStatus status;
114 SRes res = LzmaDec_DecodeToDic(&_state, dicPos + curSize, _inBuf + _inPos, &inSizeProcessed, finishMode, &status);
116 _inPos += (UInt32)inSizeProcessed;
117 _inSizeProcessed += inSizeProcessed;
118 SizeT outSizeProcessed = _state.dicPos - dicPos;
119 _outSizeProcessed += outSizeProcessed;
121 bool finished = (inSizeProcessed == 0 && outSizeProcessed == 0);
122 bool stopDecoding = (_outSizeDefined && _outSizeProcessed >= _outSize);
124 if (res != 0 || _state.dicPos == _state.dicBufSize || finished || stopDecoding)
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;
138 if (progress != NULL)
139 {
140 RINOK(progress->SetRatioInfo(&_inSizeProcessed, &_outSizeProcessed));
141 }
142 }
143 }
145 #ifndef NO_READ_FROM_CODER
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;
161 if (_outSizeDefined)
162 {
163 const UInt64 rem = _outSize - _outSizeProcessed;
164 if (rem < size)
165 size = (UInt32)rem;
166 }
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 }
188 #endif
190 }}