1 --- a/C/7zip/Compress/LZMA/LZMADecoder.cpp
2 +++ b/C/7zip/Compress/LZMA/LZMADecoder.cpp
3 @@ -274,12 +274,17 @@ STDMETHODIMP CDecoder::SetDecoderPropert
4 Byte remainder = (Byte)(properties[0] / 9);
\r
5 int lp = remainder % 5;
\r
6 int pb = remainder / 5;
\r
7 - if (pb > NLength::kNumPosStatesBitsMax)
\r
8 - return E_INVALIDARG;
\r
9 - _posStateMask = (1 << pb) - 1;
\r
10 UInt32 dictionarySize = 0;
\r
11 for (int i = 0; i < 4; i++)
\r
12 dictionarySize += ((UInt32)(properties[1 + i])) << (i * 8);
\r
13 + return SetDecoderPropertiesRaw(lc, lp, pb, dictionarySize);
\r
16 +STDMETHODIMP CDecoder::SetDecoderPropertiesRaw(int lc, int lp, int pb, UInt32 dictionarySize)
\r
18 + if (pb > NLength::kNumPosStatesBitsMax)
\r
19 + return E_INVALIDARG;
\r
20 + _posStateMask = (1 << pb) - 1;
\r
21 if (!_outWindowStream.Create(dictionarySize))
\r
22 return E_OUTOFMEMORY;
\r
23 if (!_literalDecoder.Create(lp, lc))
\r
24 --- a/C/7zip/Compress/LZMA/LZMADecoder.h
25 +++ b/C/7zip/Compress/LZMA/LZMADecoder.h
26 @@ -228,6 +228,7 @@ public:
\r
27 ICompressProgressInfo *progress);
\r
29 STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
\r
30 + STDMETHOD(SetDecoderPropertiesRaw)(int lc, int lp, int pb, UInt32 dictionarySize);
\r
32 STDMETHOD(GetInStreamProcessedSize)(UInt64 *value);
\r
35 +++ b/C/7zip/Compress/LZMA_Lib/makefile
41 +CFLAGS = -c -I ../../../
55 + CommandLineParser.o \
67 + $(AR) r $(PROG) $(OBJS)
70 + $(CXX) $(CFLAGS) ZLib.cpp
72 +LZMADecoder.o: ../LZMA/LZMADecoder.cpp
73 + $(CXX) $(CFLAGS) ../LZMA/LZMADecoder.cpp
75 +LZMAEncoder.o: ../LZMA/LZMAEncoder.cpp
76 + $(CXX) $(CFLAGS) ../LZMA/LZMAEncoder.cpp
78 +LZInWindow.o: ../LZ/LZInWindow.cpp
79 + $(CXX) $(CFLAGS) ../LZ/LZInWindow.cpp
81 +LZOutWindow.o: ../LZ/LZOutWindow.cpp
82 + $(CXX) $(CFLAGS) ../LZ/LZOutWindow.cpp
84 +RangeCoderBit.o: ../RangeCoder/RangeCoderBit.cpp
85 + $(CXX) $(CFLAGS) ../RangeCoder/RangeCoderBit.cpp
87 +InBuffer.o: ../../Common/InBuffer.cpp
88 + $(CXX) $(CFLAGS) ../../Common/InBuffer.cpp
90 +OutBuffer.o: ../../Common/OutBuffer.cpp
91 + $(CXX) $(CFLAGS) ../../Common/OutBuffer.cpp
93 +StreamUtils.o: ../../Common/StreamUtils.cpp
94 + $(CXX) $(CFLAGS) ../../Common/StreamUtils.cpp
96 +FileStreams.o: ../../Common/FileStreams.cpp
97 + $(CXX) $(CFLAGS) ../../Common/FileStreams.cpp
99 +Alloc.o: ../../../Common/Alloc.cpp
100 + $(CXX) $(CFLAGS) ../../../Common/Alloc.cpp
102 +C_FileIO.o: ../../../Common/C_FileIO.cpp
103 + $(CXX) $(CFLAGS) ../../../Common/C_FileIO.cpp
105 +CommandLineParser.o: ../../../Common/CommandLineParser.cpp
106 + $(CXX) $(CFLAGS) ../../../Common/CommandLineParser.cpp
108 +CRC.o: ../../../Common/CRC.cpp
109 + $(CXX) $(CFLAGS) ../../../Common/CRC.cpp
111 +MyWindows.o: ../../../Common/MyWindows.cpp
112 + $(CXX) $(CFLAGS) ../../../Common/MyWindows.cpp
114 +String.o: ../../../Common/String.cpp
115 + $(CXX) $(CFLAGS) ../../../Common/String.cpp
117 +StringConvert.o: ../../../Common/StringConvert.cpp
118 + $(CXX) $(CFLAGS) ../../../Common/StringConvert.cpp
120 +StringToInt.o: ../../../Common/StringToInt.cpp
121 + $(CXX) $(CFLAGS) ../../../Common/StringToInt.cpp
123 +Vector.o: ../../../Common/Vector.cpp
124 + $(CXX) $(CFLAGS) ../../../Common/Vector.cpp
127 + -$(RM) $(PROG) $(OBJS)
130 +++ b/C/7zip/Compress/LZMA_Lib/ZLib.cpp
133 + * lzma zlib simplified wrapper
\r
135 + * Copyright (c) 2005-2006 Oleg I. Vdovikin <oleg@cs.msu.su>
\r
137 + * This library is free software; you can redistribute
\r
138 + * it and/or modify it under the terms of the GNU Lesser
\r
139 + * General Public License as published by the Free Software
\r
140 + * Foundation; either version 2.1 of the License, or
\r
141 + * (at your option) any later version.
\r
143 + * This library is distributed in the hope that it will be
\r
144 + * useful, but WITHOUT ANY WARRANTY; without even the implied
\r
145 + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
146 + * PURPOSE. See the GNU Lesser General Public License
\r
147 + * for more details.
\r
149 + * You should have received a copy of the GNU Lesser General
\r
150 + * Public License along with this library; if not, write to
\r
151 + * the Free Software Foundation, Inc., 59 Temple Place,
\r
152 + * Suite 330, Boston, MA 02111-1307 USA
\r
156 + * default values for encoder/decoder used by wrapper
\r
166 +#include <initguid.h>
\r
171 +#include "../../../Common/MyWindows.h"
\r
172 +#include "../LZMA/LZMADecoder.h"
\r
173 +#include "../LZMA/LZMAEncoder.h"
\r
175 +#define STG_E_SEEKERROR ((HRESULT)0x80030019L)
\r
176 +#define STG_E_MEDIUMFULL ((HRESULT)0x80030070L)
\r
178 +class CInMemoryStream:
\r
179 + public IInStream,
\r
180 + public IStreamGetSize,
\r
181 + public CMyUnknownImp
\r
184 + CInMemoryStream(const Bytef *data, UInt64 size) :
\r
185 + m_data(data), m_size(size), m_offset(0) {}
\r
187 + virtual ~CInMemoryStream() {}
\r
189 + MY_UNKNOWN_IMP2(IInStream, IStreamGetSize)
\r
191 + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize)
\r
193 + if (size > m_size - m_offset)
\r
194 + size = m_size - m_offset;
\r
197 + memcpy(data, m_data + m_offset, size);
\r
200 + m_offset += size;
\r
202 + if (processedSize)
\r
203 + *processedSize = size;
\r
208 + STDMETHOD(ReadPart)(void *data, UInt32 size, UInt32 *processedSize)
\r
210 + return Read(data, size, processedSize);
\r
213 + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)
\r
217 + if (seekOrigin == STREAM_SEEK_SET) _offset = offset;
\r
218 + else if (seekOrigin == STREAM_SEEK_CUR) _offset = m_offset + offset;
\r
219 + else if (seekOrigin == STREAM_SEEK_END) _offset = m_size;
\r
220 + else return STG_E_INVALIDFUNCTION;
\r
222 + if (_offset < 0 || _offset > m_size)
\r
223 + return STG_E_SEEKERROR;
\r
225 + m_offset = _offset;
\r
228 + *newPosition = m_offset;
\r
233 + STDMETHOD(GetSize)(UInt64 *size)
\r
239 + const Bytef *m_data;
\r
244 +class COutMemoryStream:
\r
245 + public IOutStream,
\r
246 + public CMyUnknownImp
\r
249 + COutMemoryStream(Bytef *data, UInt64 maxsize) :
\r
250 + m_data(data), m_size(0), m_maxsize(maxsize), m_offset(0) {}
\r
251 + virtual ~COutMemoryStream() {}
\r
253 + MY_UNKNOWN_IMP1(IOutStream)
\r
255 + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize)
\r
257 + if (size > m_maxsize - m_offset)
\r
258 + size = m_maxsize - m_offset;
\r
261 + memcpy(m_data + m_offset, data, size);
\r
264 + m_offset += size;
\r
266 + if (m_offset > m_size)
\r
267 + m_size = m_offset;
\r
269 + if (processedSize)
\r
270 + *processedSize = size;
\r
275 + STDMETHOD(WritePart)(const void *data, UInt32 size, UInt32 *processedSize)
\r
277 + return Write(data, size, processedSize);
\r
280 + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)
\r
284 + if (seekOrigin == STREAM_SEEK_SET) _offset = offset;
\r
285 + else if (seekOrigin == STREAM_SEEK_CUR) _offset = m_offset + offset;
\r
286 + else if (seekOrigin == STREAM_SEEK_END) _offset = m_size;
\r
287 + else return STG_E_INVALIDFUNCTION;
\r
289 + if (_offset < 0 || _offset > m_maxsize)
\r
290 + return STG_E_SEEKERROR;
\r
292 + m_offset = _offset;
\r
295 + *newPosition = m_offset;
\r
300 + STDMETHOD(SetSize)(Int64 newSize)
\r
302 + if ((UInt64)newSize > m_maxsize)
\r
303 + return STG_E_MEDIUMFULL;
\r
310 + UInt64 m_maxsize;
\r
314 +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
\r
315 + const Bytef *source, uLong sourceLen,
\r
318 + CInMemoryStream *inStreamSpec = new CInMemoryStream(source, sourceLen);
\r
319 + CMyComPtr<ISequentialInStream> inStream = inStreamSpec;
\r
321 + COutMemoryStream *outStreamSpec = new COutMemoryStream(dest, *destLen);
\r
322 + CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;
\r
324 + NCompress::NLZMA::CEncoder *encoderSpec =
\r
325 + new NCompress::NLZMA::CEncoder;
\r
326 + CMyComPtr<ICompressCoder> encoder = encoderSpec;
\r
328 + PROPID propIDs[] =
\r
330 + NCoderPropID::kDictionarySize,
\r
331 + NCoderPropID::kPosStateBits,
\r
332 + NCoderPropID::kLitContextBits,
\r
333 + NCoderPropID::kLitPosBits,
\r
334 + NCoderPropID::kAlgorithm,
\r
335 + NCoderPropID::kNumFastBytes,
\r
336 + NCoderPropID::kMatchFinder,
\r
337 + NCoderPropID::kEndMarker
\r
339 + const int kNumProps = sizeof(propIDs) / sizeof(propIDs[0]);
\r
341 + PROPVARIANT properties[kNumProps];
\r
342 + for (int p = 0; p < 6; p++)
\r
343 + properties[p].vt = VT_UI4;
\r
344 + properties[0].ulVal = UInt32(1 << (level + 14));
\r
345 + properties[1].ulVal = UInt32(ZLIB_PB);
\r
346 + properties[2].ulVal = UInt32(ZLIB_LC); // for normal files
\r
347 + properties[3].ulVal = UInt32(ZLIB_LP); // for normal files
\r
348 + properties[4].ulVal = UInt32(2);
\r
349 + properties[5].ulVal = UInt32(128);
\r
351 + properties[6].vt = VT_BSTR;
\r
352 + properties[6].bstrVal = (BSTR)(const wchar_t *)L"BT4";
\r
354 + properties[7].vt = VT_BOOL;
\r
355 + properties[7].boolVal = VARIANT_TRUE;
\r
357 + if (encoderSpec->SetCoderProperties(propIDs, properties, kNumProps) != S_OK)
\r
358 + return Z_MEM_ERROR; // should not happen
\r
360 + HRESULT result = encoder->Code(inStream, outStream, 0, 0, 0);
\r
361 + if (result == E_OUTOFMEMORY)
\r
363 + return Z_MEM_ERROR;
\r
365 + else if (result != S_OK)
\r
367 + return Z_BUF_ERROR; // should not happen
\r
371 + outStreamSpec->Seek(0, STREAM_SEEK_END, &fileSize);
\r
372 + *destLen = fileSize;
\r
377 +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
\r
378 + const Bytef *source, uLong sourceLen))
\r
380 + CInMemoryStream *inStreamSpec = new CInMemoryStream(source, sourceLen);
\r
381 + CMyComPtr<ISequentialInStream> inStream = inStreamSpec;
\r
383 + COutMemoryStream *outStreamSpec = new COutMemoryStream(dest, *destLen);
\r
384 + CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;
\r
386 + NCompress::NLZMA::CDecoder *decoderSpec =
\r
387 + new NCompress::NLZMA::CDecoder;
\r
388 + CMyComPtr<ICompressCoder> decoder = decoderSpec;
\r
390 + if (decoderSpec->SetDecoderPropertiesRaw(ZLIB_LC,
\r
391 + ZLIB_LP, ZLIB_PB, (1 << 23)) != S_OK) return Z_DATA_ERROR;
\r
393 + UInt64 fileSize = *destLen;
\r
395 + if (decoder->Code(inStream, outStream, 0, &fileSize, 0) != S_OK)
\r
397 + return Z_DATA_ERROR;
\r
400 + outStreamSpec->Seek(0, STREAM_SEEK_END, &fileSize);
\r
401 + *destLen = fileSize;
\r