-+/*
-+ * lzma zlib simplified wrapper
-+ *
-+ * Copyright (c) 2005 Oleg I. Vdovikin <oleg@cs.msu.su>
-+ *
-+ * This library is free software; you can redistribute
-+ * it and/or modify it under the terms of the GNU Lesser
-+ * General Public License as published by the Free Software
-+ * Foundation; either version 2.1 of the License, or
-+ * (at your option) any later version.
-+ *
-+ * This library is distributed in the hope that it will be
-+ * useful, but WITHOUT ANY WARRANTY; without even the implied
-+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-+ * PURPOSE. See the GNU Lesser General Public License
-+ * for more details.
-+ *
-+ * You should have received a copy of the GNU Lesser General
-+ * Public License along with this library; if not, write to
-+ * the Free Software Foundation, Inc., 59 Temple Place,
-+ * Suite 330, Boston, MA 02111-1307 USA
-+ */
-+
-+/*
-+ * default values for encoder/decoder used by wrapper
-+ */
-+
-+#include <zlib.h>
-+
-+#define ZLIB_LC 3
-+#define ZLIB_LP 0
-+#define ZLIB_PB 2
-+
-+#ifdef WIN32
-+#include <initguid.h>
-+#else
-+#define INITGUID
-+#endif
-+
-+#include "../../../Common/MyWindows.h"
-+#include "../LZMA/LZMADecoder.h"
-+#include "../LZMA/LZMAEncoder.h"
-+
-+#define STG_E_SEEKERROR ((HRESULT)0x80030019L)
-+#define STG_E_MEDIUMFULL ((HRESULT)0x80030070L)
-+
-+class CInMemoryStream:
-+ public IInStream,
-+ public IStreamGetSize,
-+ public CMyUnknownImp
-+{
-+public:
-+ CInMemoryStream(const Bytef *data, UInt64 size) :
-+ m_data(data), m_size(size), m_offset(0) {}
-+
-+ virtual ~CInMemoryStream() {}
-+
-+ MY_UNKNOWN_IMP2(IInStream, IStreamGetSize)
-+
-+ STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize)
-+ {
-+ if (size > m_size - m_offset)
-+ size = m_size - m_offset;
-+
-+ if (size) {
-+ memcpy(data, m_data + m_offset, size);
-+ }
-+
-+ m_offset += size;
-+
-+ if (processedSize)
-+ *processedSize = size;
-+
-+ return S_OK;
-+ }
-+
-+ STDMETHOD(ReadPart)(void *data, UInt32 size, UInt32 *processedSize)
-+ {
-+ return Read(data, size, processedSize);
-+ }
-+
-+ STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)
-+ {
-+ UInt64 _offset;
-+
-+ if (seekOrigin == STREAM_SEEK_SET) _offset = offset;
-+ else if (seekOrigin == STREAM_SEEK_CUR) _offset = m_offset + offset;
-+ else if (seekOrigin == STREAM_SEEK_END) _offset = m_size;
-+ else return STG_E_INVALIDFUNCTION;
-+
-+ if (_offset < 0 || _offset > m_size)
-+ return STG_E_SEEKERROR;
-+
-+ m_offset = _offset;
-+
-+ if (newPosition)
-+ *newPosition = m_offset;
-+
-+ return S_OK;
-+ }
-+
-+ STDMETHOD(GetSize)(UInt64 *size)
-+ {
-+ *size = m_size;
-+ return S_OK;
-+ }
-+protected:
-+ const Bytef *m_data;
-+ UInt64 m_size;
-+ UInt64 m_offset;
-+};
-+
-+class COutMemoryStream:
-+ public IOutStream,
-+ public CMyUnknownImp
-+{
-+public:
-+ COutMemoryStream(Bytef *data, UInt64 maxsize) :
-+ m_data(data), m_size(0), m_maxsize(maxsize), m_offset(0) {}
-+ virtual ~COutMemoryStream() {}
-+
-+ MY_UNKNOWN_IMP1(IOutStream)
-+
-+ STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize)
-+ {
-+ if (size > m_maxsize - m_offset)
-+ size = m_maxsize - m_offset;
-+
-+ if (size) {
-+ memcpy(m_data + m_offset, data, size);
-+ }
-+
-+ m_offset += size;
-+
-+ if (m_offset > m_size)
-+ m_size = m_offset;
-+
-+ if (processedSize)
-+ *processedSize = size;
-+
-+ return S_OK;
-+ }
-+
-+ STDMETHOD(WritePart)(const void *data, UInt32 size, UInt32 *processedSize)
-+ {
-+ return Write(data, size, processedSize);
-+ }
-+
-+ STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)
-+ {
-+ UInt64 _offset;
-+
-+ if (seekOrigin == STREAM_SEEK_SET) _offset = offset;
-+ else if (seekOrigin == STREAM_SEEK_CUR) _offset = m_offset + offset;
-+ else if (seekOrigin == STREAM_SEEK_END) _offset = m_size;
-+ else return STG_E_INVALIDFUNCTION;
-+
-+ if (_offset < 0 || _offset > m_maxsize)
-+ return STG_E_SEEKERROR;
-+
-+ m_offset = _offset;
-+
-+ if (newPosition)
-+ *newPosition = m_offset;
-+
-+ return S_OK;
-+ }
-+
-+ STDMETHOD(SetSize)(Int64 newSize)
-+ {
-+ if ((UInt64)newSize > m_maxsize)
-+ return STG_E_MEDIUMFULL;
-+
-+ return S_OK;
-+ }
-+protected:
-+ Bytef *m_data;
-+ UInt64 m_size;
-+ UInt64 m_maxsize;
-+ UInt64 m_offset;
-+};
-+
-+ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
-+ const Bytef *source, uLong sourceLen,
-+ int level))
-+{
-+ CInMemoryStream *inStreamSpec = new CInMemoryStream(source, sourceLen);
-+ CMyComPtr<ISequentialInStream> inStream = inStreamSpec;
-+
-+ COutMemoryStream *outStreamSpec = new COutMemoryStream(dest, *destLen);
-+ CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;
-+
-+ NCompress::NLZMA::CEncoder *encoderSpec =
-+ new NCompress::NLZMA::CEncoder;
-+ CMyComPtr<ICompressCoder> encoder = encoderSpec;
-+
-+ PROPID propIDs[] =
-+ {
-+ NCoderPropID::kDictionarySize,
-+ NCoderPropID::kPosStateBits,
-+ NCoderPropID::kLitContextBits,
-+ NCoderPropID::kLitPosBits,
-+ NCoderPropID::kAlgorithm,
-+ NCoderPropID::kNumFastBytes,
-+ NCoderPropID::kMatchFinder,
-+ NCoderPropID::kEndMarker
-+ };
-+ const int kNumProps = sizeof(propIDs) / sizeof(propIDs[0]);
-+
-+ PROPVARIANT properties[kNumProps];
-+ for (int p = 0; p < 6; p++)
-+ properties[p].vt = VT_UI4;
-+ properties[0].ulVal = UInt32(1 << (level + 14));
-+ properties[1].ulVal = UInt32(ZLIB_PB);
-+ properties[2].ulVal = UInt32(ZLIB_LC); // for normal files
-+ properties[3].ulVal = UInt32(ZLIB_LP); // for normal files
-+ properties[4].ulVal = UInt32(2);
-+ properties[5].ulVal = UInt32(128);
-+
-+ properties[6].vt = VT_BSTR;
-+ properties[6].bstrVal = (BSTR)(const wchar_t *)L"BT4";
-+
-+ properties[7].vt = VT_BOOL;
-+ properties[7].boolVal = VARIANT_TRUE;
-+
-+ if (encoderSpec->SetCoderProperties(propIDs, properties, kNumProps) != S_OK)
-+ return Z_MEM_ERROR; // should not happen
-+
-+ HRESULT result = encoder->Code(inStream, outStream, 0, 0, 0);
-+ if (result == E_OUTOFMEMORY)
-+ {
-+ return Z_MEM_ERROR;
-+ }
-+ else if (result != S_OK)
-+ {
-+ return Z_BUF_ERROR; // should not happen
-+ }
-+
-+ UInt64 fileSize;
-+ outStreamSpec->Seek(0, STREAM_SEEK_END, &fileSize);
-+ *destLen = fileSize;
-+
-+ return Z_OK;
-+}
-+
-+ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
-+ const Bytef *source, uLong sourceLen))
-+{
-+ CInMemoryStream *inStreamSpec = new CInMemoryStream(source, sourceLen);
-+ CMyComPtr<ISequentialInStream> inStream = inStreamSpec;
-+
-+ COutMemoryStream *outStreamSpec = new COutMemoryStream(dest, *destLen);
-+ CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;
-+
-+ NCompress::NLZMA::CDecoder *decoderSpec =
-+ new NCompress::NLZMA::CDecoder;
-+ CMyComPtr<ICompressCoder> decoder = decoderSpec;
-+
-+ if (decoderSpec->SetDecoderPropertiesRaw(ZLIB_LC,
-+ ZLIB_LP, ZLIB_PB, (1 << 23)) != S_OK) return Z_DATA_ERROR;
-+
-+ UInt64 fileSize = *destLen;
-+
-+ if (decoder->Code(inStream, outStream, 0, &fileSize, 0) != S_OK)
-+ {
-+ return Z_DATA_ERROR;
-+ }
-+
-+ outStreamSpec->Seek(0, STREAM_SEEK_END, &fileSize);
-+ *destLen = fileSize;
-+
-+ return Z_OK;
-+}
++/*\r
++ * lzma zlib simplified wrapper\r
++ *\r
++ * Copyright (c) 2005 Oleg I. Vdovikin <oleg@cs.msu.su>\r
++ *\r
++ * This library is free software; you can redistribute \r
++ * it and/or modify it under the terms of the GNU Lesser \r
++ * General Public License as published by the Free Software \r
++ * Foundation; either version 2.1 of the License, or \r
++ * (at your option) any later version.\r
++ *\r
++ * This library is distributed in the hope that it will be \r
++ * useful, but WITHOUT ANY WARRANTY; without even the implied \r
++ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
++ * PURPOSE. See the GNU Lesser General Public License \r
++ * for more details.\r
++ *\r
++ * You should have received a copy of the GNU Lesser General \r
++ * Public License along with this library; if not, write to \r
++ * the Free Software Foundation, Inc., 59 Temple Place, \r
++ * Suite 330, Boston, MA 02111-1307 USA \r
++ */\r
++\r
++/*\r
++ * default values for encoder/decoder used by wrapper\r
++ */\r
++\r
++#include <zlib.h>\r
++\r
++#define ZLIB_LC 3\r
++#define ZLIB_LP 0\r
++#define ZLIB_PB 2\r
++\r
++#ifdef WIN32\r
++#include <initguid.h>\r
++#else\r
++#define INITGUID\r
++#endif\r
++\r
++#include "../../../Common/MyWindows.h"\r
++#include "../LZMA/LZMADecoder.h"\r
++#include "../LZMA/LZMAEncoder.h"\r
++\r
++#define STG_E_SEEKERROR ((HRESULT)0x80030019L)\r
++#define STG_E_MEDIUMFULL ((HRESULT)0x80030070L)\r
++\r
++class CInMemoryStream: \r
++ public IInStream,\r
++ public IStreamGetSize,\r
++ public CMyUnknownImp\r
++{\r
++public:\r
++ CInMemoryStream(const Bytef *data, UInt64 size) : \r
++ m_data(data), m_size(size), m_offset(0) {}\r
++\r
++ virtual ~CInMemoryStream() {}\r
++\r
++ MY_UNKNOWN_IMP2(IInStream, IStreamGetSize)\r
++\r
++ STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize)\r
++ {\r
++ if (size > m_size - m_offset) \r
++ size = m_size - m_offset;\r
++\r
++ if (size) {\r
++ memcpy(data, m_data + m_offset, size);\r
++ }\r
++\r
++ m_offset += size;\r
++\r
++ if (processedSize) \r
++ *processedSize = size;\r
++\r
++ return S_OK;\r
++ }\r
++\r
++ STDMETHOD(ReadPart)(void *data, UInt32 size, UInt32 *processedSize)\r
++ {\r
++ return Read(data, size, processedSize);\r
++ }\r
++\r
++ STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)\r
++ {\r
++ UInt64 _offset;\r
++\r
++ if (seekOrigin == STREAM_SEEK_SET) _offset = offset;\r
++ else if (seekOrigin == STREAM_SEEK_CUR) _offset = m_offset + offset; \r
++ else if (seekOrigin == STREAM_SEEK_END) _offset = m_size;\r
++ else return STG_E_INVALIDFUNCTION;\r
++\r
++ if (_offset < 0 || _offset > m_size)\r
++ return STG_E_SEEKERROR;\r
++\r
++ m_offset = _offset;\r
++\r
++ if (newPosition)\r
++ *newPosition = m_offset;\r
++\r
++ return S_OK;\r
++ }\r
++\r
++ STDMETHOD(GetSize)(UInt64 *size)\r
++ {\r
++ *size = m_size;\r
++ return S_OK;\r
++ }\r
++protected:\r
++ const Bytef *m_data;\r
++ UInt64 m_size;\r
++ UInt64 m_offset;\r
++};\r
++\r
++class COutMemoryStream: \r
++ public IOutStream,\r
++ public CMyUnknownImp\r
++{\r
++public:\r
++ COutMemoryStream(Bytef *data, UInt64 maxsize) : \r
++ m_data(data), m_size(0), m_maxsize(maxsize), m_offset(0) {}\r
++ virtual ~COutMemoryStream() {}\r
++ \r
++ MY_UNKNOWN_IMP1(IOutStream)\r
++\r
++ STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize)\r
++ {\r
++ if (size > m_maxsize - m_offset) \r
++ size = m_maxsize - m_offset;\r
++\r
++ if (size) {\r
++ memcpy(m_data + m_offset, data, size);\r
++ }\r
++\r
++ m_offset += size;\r
++\r
++ if (m_offset > m_size)\r
++ m_size = m_offset;\r
++\r
++ if (processedSize) \r
++ *processedSize = size;\r
++\r
++ return S_OK;\r
++ }\r
++ \r
++ STDMETHOD(WritePart)(const void *data, UInt32 size, UInt32 *processedSize)\r
++ {\r
++ return Write(data, size, processedSize);\r
++ }\r
++\r
++ STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)\r
++ {\r
++ UInt64 _offset;\r
++\r
++ if (seekOrigin == STREAM_SEEK_SET) _offset = offset;\r
++ else if (seekOrigin == STREAM_SEEK_CUR) _offset = m_offset + offset; \r
++ else if (seekOrigin == STREAM_SEEK_END) _offset = m_size;\r
++ else return STG_E_INVALIDFUNCTION;\r
++\r
++ if (_offset < 0 || _offset > m_maxsize)\r
++ return STG_E_SEEKERROR;\r
++\r
++ m_offset = _offset;\r
++\r
++ if (newPosition)\r
++ *newPosition = m_offset;\r
++\r
++ return S_OK;\r
++ }\r
++ \r
++ STDMETHOD(SetSize)(Int64 newSize)\r
++ {\r
++ if ((UInt64)newSize > m_maxsize) \r
++ return STG_E_MEDIUMFULL;\r
++\r
++ return S_OK;\r
++ }\r
++protected:\r
++ Bytef *m_data;\r
++ UInt64 m_size;\r
++ UInt64 m_maxsize;\r
++ UInt64 m_offset;\r
++};\r
++\r
++ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,\r
++ const Bytef *source, uLong sourceLen,\r
++ int level))\r
++{\r
++ CInMemoryStream *inStreamSpec = new CInMemoryStream(source, sourceLen);\r
++ CMyComPtr<ISequentialInStream> inStream = inStreamSpec;\r
++ \r
++ COutMemoryStream *outStreamSpec = new COutMemoryStream(dest, *destLen);\r
++ CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;\r
++ \r
++ NCompress::NLZMA::CEncoder *encoderSpec = \r
++ new NCompress::NLZMA::CEncoder;\r
++ CMyComPtr<ICompressCoder> encoder = encoderSpec;\r
++ \r
++ PROPID propIDs[] = \r
++ {\r
++ NCoderPropID::kDictionarySize,\r
++ NCoderPropID::kPosStateBits,\r
++ NCoderPropID::kLitContextBits,\r
++ NCoderPropID::kLitPosBits,\r
++ NCoderPropID::kAlgorithm,\r
++ NCoderPropID::kNumFastBytes,\r
++ NCoderPropID::kMatchFinder,\r
++ NCoderPropID::kEndMarker\r
++ };\r
++ const int kNumProps = sizeof(propIDs) / sizeof(propIDs[0]);\r
++ \r
++ PROPVARIANT properties[kNumProps];\r
++ for (int p = 0; p < 6; p++)\r
++ properties[p].vt = VT_UI4;\r
++ properties[0].ulVal = UInt32(1 << (level + 14));\r
++ properties[1].ulVal = UInt32(ZLIB_PB);\r
++ properties[2].ulVal = UInt32(ZLIB_LC); // for normal files\r
++ properties[3].ulVal = UInt32(ZLIB_LP); // for normal files\r
++ properties[4].ulVal = UInt32(2);\r
++ properties[5].ulVal = UInt32(128);\r
++ \r
++ properties[6].vt = VT_BSTR;\r
++ properties[6].bstrVal = (BSTR)(const wchar_t *)L"BT4";\r
++ \r
++ properties[7].vt = VT_BOOL;\r
++ properties[7].boolVal = VARIANT_TRUE;\r
++ \r
++ if (encoderSpec->SetCoderProperties(propIDs, properties, kNumProps) != S_OK)\r
++ return Z_MEM_ERROR; // should not happen\r
++ \r
++ HRESULT result = encoder->Code(inStream, outStream, 0, 0, 0);\r
++ if (result == E_OUTOFMEMORY)\r
++ {\r
++ return Z_MEM_ERROR;\r
++ } \r
++ else if (result != S_OK)\r
++ {\r
++ return Z_BUF_ERROR; // should not happen\r
++ } \r
++ \r
++ UInt64 fileSize;\r
++ outStreamSpec->Seek(0, STREAM_SEEK_END, &fileSize);\r
++ *destLen = fileSize;\r
++ \r
++ return Z_OK;\r
++}\r
++\r
++ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,\r
++ const Bytef *source, uLong sourceLen))\r
++{\r
++ CInMemoryStream *inStreamSpec = new CInMemoryStream(source, sourceLen);\r
++ CMyComPtr<ISequentialInStream> inStream = inStreamSpec;\r
++ \r
++ COutMemoryStream *outStreamSpec = new COutMemoryStream(dest, *destLen);\r
++ CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;\r
++ \r
++ NCompress::NLZMA::CDecoder *decoderSpec = \r
++ new NCompress::NLZMA::CDecoder;\r
++ CMyComPtr<ICompressCoder> decoder = decoderSpec;\r
++ \r
++ if (decoderSpec->SetDecoderPropertiesRaw(ZLIB_LC, \r
++ ZLIB_LP, ZLIB_PB, (1 << 23)) != S_OK) return Z_DATA_ERROR;\r
++ \r
++ UInt64 fileSize = *destLen;\r
++ \r
++ if (decoder->Code(inStream, outStream, 0, &fileSize, 0) != S_OK)\r
++ {\r
++ return Z_DATA_ERROR;\r
++ }\r
++ \r
++ outStreamSpec->Seek(0, STREAM_SEEK_END, &fileSize);\r
++ *destLen = fileSize;\r
++ \r
++ return Z_OK;\r
++}\r