add modified b44 fixes based on #205 to brcm-2.6
[openwrt/svn-archive/archive.git] / openwrt / target / lzma / lzma-406-zlib-stream.patch
index 822655f45ae7a18a9ae147b0b5e7ab71e52ab134..5af976372cf9318dc6f100888582269e9c4e2259 100644 (file)
@@ -2,404 +2,404 @@ diff -BurN lzma406/SRC/7zip/Compress/LZMA/LZMADecoder.cpp lzma/SRC/7zip/Compress
 --- lzma406/SRC/7zip/Compress/LZMA/LZMADecoder.cpp     2004-08-25 01:40:48.000000000 +0400
 +++ lzma/SRC/7zip/Compress/LZMA/LZMADecoder.cpp        2005-01-20 14:47:45.789801629 +0300
 @@ -288,12 +288,17 @@
-   Byte remainder = (Byte)(properties[0] / 9);
-   int lp = remainder % 5;
-   int pb = remainder / 5;
--  if (pb > NLength::kNumPosStatesBitsMax)
--    return E_INVALIDARG;
--  _posStateMask = (1 << pb) - 1;
-   UInt32 dictionarySize = 0;
-   for (int i = 0; i < 4; i++)
-     dictionarySize += ((UInt32)(properties[1 + i])) << (i * 8);
-+  return SetDecoderPropertiesRaw(lc, lp, pb, dictionarySize);
-+}
-+
-+STDMETHODIMP CDecoder::SetDecoderPropertiesRaw(int lc, int lp, int pb, UInt32 dictionarySize)
-+{
-+  if (pb > NLength::kNumPosStatesBitsMax)
-+    return E_INVALIDARG;
-+  _posStateMask = (1 << pb) - 1;
-   _dictionarySizeCheck = MyMax(dictionarySize, UInt32(1));
-   UInt32 blockSize = MyMax(_dictionarySizeCheck, UInt32(1 << 12));
-   if (!_outWindowStream.Create(blockSize))
+   Byte remainder = (Byte)(properties[0] / 9);\r
+   int lp = remainder % 5;\r
+   int pb = remainder / 5;\r
+-  if (pb > NLength::kNumPosStatesBitsMax)\r
+-    return E_INVALIDARG;\r
+-  _posStateMask = (1 << pb) - 1;\r
+   UInt32 dictionarySize = 0;\r
+   for (int i = 0; i < 4; i++)\r
+     dictionarySize += ((UInt32)(properties[1 + i])) << (i * 8);\r
++  return SetDecoderPropertiesRaw(lc, lp, pb, dictionarySize);\r
++}\r
++\r
++STDMETHODIMP CDecoder::SetDecoderPropertiesRaw(int lc, int lp, int pb, UInt32 dictionarySize)\r
++{\r
++  if (pb > NLength::kNumPosStatesBitsMax)\r
++    return E_INVALIDARG;\r
++  _posStateMask = (1 << pb) - 1;\r
+   _dictionarySizeCheck = MyMax(dictionarySize, UInt32(1));\r
+   UInt32 blockSize = MyMax(_dictionarySizeCheck, UInt32(1 << 12));\r
+   if (!_outWindowStream.Create(blockSize))\r
 diff -BurN lzma406/SRC/7zip/Compress/LZMA/LZMADecoder.h lzma/SRC/7zip/Compress/LZMA/LZMADecoder.h
 --- lzma406/SRC/7zip/Compress/LZMA/LZMADecoder.h       2004-08-25 01:40:08.000000000 +0400
 +++ lzma/SRC/7zip/Compress/LZMA/LZMADecoder.h  2005-01-20 13:06:59.081297916 +0300
 @@ -234,6 +234,8 @@
-   STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
-   STDMETHOD(ReadPart)(void *data, UInt32 size, UInt32 *processedSize);
-+  STDMETHOD(SetDecoderPropertiesRaw)(int lc, int lp, int pb, UInt32 dictionarySize);
-+
-   virtual ~CDecoder() {}
- };
+   STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);\r
+   STDMETHOD(ReadPart)(void *data, UInt32 size, UInt32 *processedSize);\r
\r
++  STDMETHOD(SetDecoderPropertiesRaw)(int lc, int lp, int pb, UInt32 dictionarySize);\r
++\r
+   virtual ~CDecoder() {}\r
+ };\r
\r
 diff -BurN lzma406/SRC/7zip/Compress/LZMA_Lib/makefile lzma/SRC/7zip/Compress/LZMA_Lib/makefile
 --- lzma406/SRC/7zip/Compress/LZMA_Lib/makefile        1970-01-01 03:00:00.000000000 +0300
 +++ lzma/SRC/7zip/Compress/LZMA_Lib/makefile   2005-01-19 13:49:20.000000000 +0300
 @@ -0,0 +1,88 @@
-+PROG = liblzma.a
-+CXX = g++ -O3 -Wall
++PROG = liblzma.a\r
++CXX = g++ -O3 -Wall\r
 +AR = ar
-+RM = rm -f
-+CFLAGS = -c
-+
-+OBJS = \
-+  ZLib.o \
-+  LZMADecoder.o \
-+  LZMAEncoder.o \
-+  LZInWindow.o \
-+  LZOutWindow.o \
-+  RangeCoderBit.o \
-+  InBuffer.o \
-+  OutBuffer.o \
-+  FileStreams.o \
-+  Alloc.o \
-+  C_FileIO.o \
-+  CommandLineParser.o \
-+  CRC.o \
-+  String.o \
-+  StringConvert.o \
-+  StringToInt.o \
-+  Vector.o \
-+
-+
-+all: $(PROG)
-+
-+$(PROG): $(OBJS)
-+      $(AR) r $(PROG) $(OBJS)
-+
-+ZLib.o: ZLib.cpp
-+      $(CXX) $(CFLAGS) ZLib.cpp
-+
-+LZMADecoder.o: ../LZMA/LZMADecoder.cpp
-+      $(CXX) $(CFLAGS) ../LZMA/LZMADecoder.cpp
-+
-+LZMAEncoder.o: ../LZMA/LZMAEncoder.cpp
-+      $(CXX) $(CFLAGS) ../LZMA/LZMAEncoder.cpp
-+
-+LZInWindow.o: ../LZ/LZInWindow.cpp
-+      $(CXX) $(CFLAGS) ../LZ/LZInWindow.cpp
-+
-+LZOutWindow.o: ../LZ/LZOutWindow.cpp
-+      $(CXX) $(CFLAGS) ../LZ/LZOutWindow.cpp
-+
-+RangeCoderBit.o: ../RangeCoder/RangeCoderBit.cpp
-+      $(CXX) $(CFLAGS) ../RangeCoder/RangeCoderBit.cpp
-+
-+InBuffer.o: ../../Common/InBuffer.cpp
-+      $(CXX) $(CFLAGS) ../../Common/InBuffer.cpp
-+
-+OutBuffer.o: ../../Common/OutBuffer.cpp
-+      $(CXX) $(CFLAGS) ../../Common/OutBuffer.cpp
-+
-+FileStreams.o: ../../Common/FileStreams.cpp
-+      $(CXX) $(CFLAGS) ../../Common/FileStreams.cpp
-+
-+Alloc.o: ../../../Common/Alloc.cpp
-+      $(CXX) $(CFLAGS) ../../../Common/Alloc.cpp
-+
-+C_FileIO.o: ../../../Common/C_FileIO.cpp
-+      $(CXX) $(CFLAGS) ../../../Common/C_FileIO.cpp
-+
-+CommandLineParser.o: ../../../Common/CommandLineParser.cpp
-+      $(CXX) $(CFLAGS) ../../../Common/CommandLineParser.cpp
-+
-+CRC.o: ../../../Common/CRC.cpp
-+      $(CXX) $(CFLAGS) ../../../Common/CRC.cpp
-+
-+MyWindows.o: ../../../Common/MyWindows.cpp
-+      $(CXX) $(CFLAGS) ../../../Common/MyWindows.cpp
-+
-+String.o: ../../../Common/String.cpp
-+      $(CXX) $(CFLAGS) ../../../Common/String.cpp
-+
-+StringConvert.o: ../../../Common/StringConvert.cpp
-+      $(CXX) $(CFLAGS) ../../../Common/StringConvert.cpp
-+
-+StringToInt.o: ../../../Common/StringToInt.cpp
-+      $(CXX) $(CFLAGS) ../../../Common/StringToInt.cpp
-+
-+Vector.o: ../../../Common/Vector.cpp
-+      $(CXX) $(CFLAGS) ../../../Common/Vector.cpp
-+
-+clean:
-+      -$(RM) $(PROG) $(OBJS)
-+
++RM = rm -f\r
++CFLAGS = -c\r
++\r
++OBJS = \\r
++  ZLib.o \\r
++  LZMADecoder.o \\r
++  LZMAEncoder.o \\r
++  LZInWindow.o \\r
++  LZOutWindow.o \\r
++  RangeCoderBit.o \\r
++  InBuffer.o \\r
++  OutBuffer.o \\r
++  FileStreams.o \\r
++  Alloc.o \\r
++  C_FileIO.o \\r
++  CommandLineParser.o \\r
++  CRC.o \\r
++  String.o \\r
++  StringConvert.o \\r
++  StringToInt.o \\r
++  Vector.o \\r
++\r
++\r
++all: $(PROG)\r
++\r
++$(PROG): $(OBJS)\r
++      $(AR) r $(PROG) $(OBJS)\r
++\r
++ZLib.o: ZLib.cpp\r
++      $(CXX) $(CFLAGS) ZLib.cpp\r
++\r
++LZMADecoder.o: ../LZMA/LZMADecoder.cpp\r
++      $(CXX) $(CFLAGS) ../LZMA/LZMADecoder.cpp\r
++\r
++LZMAEncoder.o: ../LZMA/LZMAEncoder.cpp\r
++      $(CXX) $(CFLAGS) ../LZMA/LZMAEncoder.cpp\r
++\r
++LZInWindow.o: ../LZ/LZInWindow.cpp\r
++      $(CXX) $(CFLAGS) ../LZ/LZInWindow.cpp\r
++\r
++LZOutWindow.o: ../LZ/LZOutWindow.cpp\r
++      $(CXX) $(CFLAGS) ../LZ/LZOutWindow.cpp\r
++\r
++RangeCoderBit.o: ../RangeCoder/RangeCoderBit.cpp\r
++      $(CXX) $(CFLAGS) ../RangeCoder/RangeCoderBit.cpp\r
++\r
++InBuffer.o: ../../Common/InBuffer.cpp\r
++      $(CXX) $(CFLAGS) ../../Common/InBuffer.cpp\r
++\r
++OutBuffer.o: ../../Common/OutBuffer.cpp\r
++      $(CXX) $(CFLAGS) ../../Common/OutBuffer.cpp\r
++\r
++FileStreams.o: ../../Common/FileStreams.cpp\r
++      $(CXX) $(CFLAGS) ../../Common/FileStreams.cpp\r
++\r
++Alloc.o: ../../../Common/Alloc.cpp\r
++      $(CXX) $(CFLAGS) ../../../Common/Alloc.cpp\r
++\r
++C_FileIO.o: ../../../Common/C_FileIO.cpp\r
++      $(CXX) $(CFLAGS) ../../../Common/C_FileIO.cpp\r
++\r
++CommandLineParser.o: ../../../Common/CommandLineParser.cpp\r
++      $(CXX) $(CFLAGS) ../../../Common/CommandLineParser.cpp\r
++\r
++CRC.o: ../../../Common/CRC.cpp\r
++      $(CXX) $(CFLAGS) ../../../Common/CRC.cpp\r
++\r
++MyWindows.o: ../../../Common/MyWindows.cpp\r
++      $(CXX) $(CFLAGS) ../../../Common/MyWindows.cpp\r
++\r
++String.o: ../../../Common/String.cpp\r
++      $(CXX) $(CFLAGS) ../../../Common/String.cpp\r
++\r
++StringConvert.o: ../../../Common/StringConvert.cpp\r
++      $(CXX) $(CFLAGS) ../../../Common/StringConvert.cpp\r
++\r
++StringToInt.o: ../../../Common/StringToInt.cpp\r
++      $(CXX) $(CFLAGS) ../../../Common/StringToInt.cpp\r
++\r
++Vector.o: ../../../Common/Vector.cpp\r
++      $(CXX) $(CFLAGS) ../../../Common/Vector.cpp\r
++\r
++clean:\r
++      -$(RM) $(PROG) $(OBJS)\r
++\r
 diff -BurN lzma406/SRC/7zip/Compress/LZMA_Lib/ZLib.cpp lzma/SRC/7zip/Compress/LZMA_Lib/ZLib.cpp
 --- lzma406/SRC/7zip/Compress/LZMA_Lib/ZLib.cpp        1970-01-01 03:00:00.000000000 +0300
 +++ lzma/SRC/7zip/Compress/LZMA_Lib/ZLib.cpp   2005-01-20 15:01:18.439221129 +0300
 @@ -0,0 +1,273 @@
-+/*
-+ * 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