100-lzma_zlib.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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);
  5. int lp = remainder % 5;
  6. int pb = remainder / 5;
  7. - if (pb > NLength::kNumPosStatesBitsMax)
  8. - return E_INVALIDARG;
  9. - _posStateMask = (1 << pb) - 1;
  10. UInt32 dictionarySize = 0;
  11. for (int i = 0; i < 4; i++)
  12. dictionarySize += ((UInt32)(properties[1 + i])) << (i * 8);
  13. + return SetDecoderPropertiesRaw(lc, lp, pb, dictionarySize);
  14. +}
  15. +
  16. +STDMETHODIMP CDecoder::SetDecoderPropertiesRaw(int lc, int lp, int pb, UInt32 dictionarySize)
  17. +{
  18. + if (pb > NLength::kNumPosStatesBitsMax)
  19. + return E_INVALIDARG;
  20. + _posStateMask = (1 << pb) - 1;
  21. if (!_outWindowStream.Create(dictionarySize))
  22. return E_OUTOFMEMORY;
  23. if (!_literalDecoder.Create(lp, lc))
  24. --- a/C/7zip/Compress/LZMA/LZMADecoder.h
  25. +++ b/C/7zip/Compress/LZMA/LZMADecoder.h
  26. @@ -228,6 +228,7 @@ public:
  27. ICompressProgressInfo *progress);
  28. STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
  29. + STDMETHOD(SetDecoderPropertiesRaw)(int lc, int lp, int pb, UInt32 dictionarySize);
  30. STDMETHOD(GetInStreamProcessedSize)(UInt64 *value);
  31. --- /dev/null
  32. +++ b/C/7zip/Compress/LZMA_Lib/makefile
  33. @@ -0,0 +1,92 @@
  34. +PROG = liblzma.a
  35. +CXX = g++ -O3 -Wall
  36. +AR = ar
  37. +RM = rm -f
  38. +CFLAGS = -c -I ../../../
  39. +
  40. +OBJS = \
  41. + ZLib.o \
  42. + LZMADecoder.o \
  43. + LZMAEncoder.o \
  44. + LZInWindow.o \
  45. + LZOutWindow.o \
  46. + RangeCoderBit.o \
  47. + InBuffer.o \
  48. + OutBuffer.o \
  49. + FileStreams.o \
  50. + Alloc.o \
  51. + C_FileIO.o \
  52. + CommandLineParser.o \
  53. + CRC.o \
  54. + StreamUtils.o \
  55. + String.o \
  56. + StringConvert.o \
  57. + StringToInt.o \
  58. + Vector.o \
  59. +
  60. +
  61. +all: $(PROG)
  62. +
  63. +$(PROG): $(OBJS)
  64. + $(AR) r $(PROG) $(OBJS)
  65. +
  66. +ZLib.o: ZLib.cpp
  67. + $(CXX) $(CFLAGS) ZLib.cpp
  68. +
  69. +LZMADecoder.o: ../LZMA/LZMADecoder.cpp
  70. + $(CXX) $(CFLAGS) ../LZMA/LZMADecoder.cpp
  71. +
  72. +LZMAEncoder.o: ../LZMA/LZMAEncoder.cpp
  73. + $(CXX) $(CFLAGS) ../LZMA/LZMAEncoder.cpp
  74. +
  75. +LZInWindow.o: ../LZ/LZInWindow.cpp
  76. + $(CXX) $(CFLAGS) ../LZ/LZInWindow.cpp
  77. +
  78. +LZOutWindow.o: ../LZ/LZOutWindow.cpp
  79. + $(CXX) $(CFLAGS) ../LZ/LZOutWindow.cpp
  80. +
  81. +RangeCoderBit.o: ../RangeCoder/RangeCoderBit.cpp
  82. + $(CXX) $(CFLAGS) ../RangeCoder/RangeCoderBit.cpp
  83. +
  84. +InBuffer.o: ../../Common/InBuffer.cpp
  85. + $(CXX) $(CFLAGS) ../../Common/InBuffer.cpp
  86. +
  87. +OutBuffer.o: ../../Common/OutBuffer.cpp
  88. + $(CXX) $(CFLAGS) ../../Common/OutBuffer.cpp
  89. +
  90. +StreamUtils.o: ../../Common/StreamUtils.cpp
  91. + $(CXX) $(CFLAGS) ../../Common/StreamUtils.cpp
  92. +
  93. +FileStreams.o: ../../Common/FileStreams.cpp
  94. + $(CXX) $(CFLAGS) ../../Common/FileStreams.cpp
  95. +
  96. +Alloc.o: ../../../Common/Alloc.cpp
  97. + $(CXX) $(CFLAGS) ../../../Common/Alloc.cpp
  98. +
  99. +C_FileIO.o: ../../../Common/C_FileIO.cpp
  100. + $(CXX) $(CFLAGS) ../../../Common/C_FileIO.cpp
  101. +
  102. +CommandLineParser.o: ../../../Common/CommandLineParser.cpp
  103. + $(CXX) $(CFLAGS) ../../../Common/CommandLineParser.cpp
  104. +
  105. +CRC.o: ../../../Common/CRC.cpp
  106. + $(CXX) $(CFLAGS) ../../../Common/CRC.cpp
  107. +
  108. +MyWindows.o: ../../../Common/MyWindows.cpp
  109. + $(CXX) $(CFLAGS) ../../../Common/MyWindows.cpp
  110. +
  111. +String.o: ../../../Common/String.cpp
  112. + $(CXX) $(CFLAGS) ../../../Common/String.cpp
  113. +
  114. +StringConvert.o: ../../../Common/StringConvert.cpp
  115. + $(CXX) $(CFLAGS) ../../../Common/StringConvert.cpp
  116. +
  117. +StringToInt.o: ../../../Common/StringToInt.cpp
  118. + $(CXX) $(CFLAGS) ../../../Common/StringToInt.cpp
  119. +
  120. +Vector.o: ../../../Common/Vector.cpp
  121. + $(CXX) $(CFLAGS) ../../../Common/Vector.cpp
  122. +
  123. +clean:
  124. + -$(RM) $(PROG) $(OBJS)
  125. +
  126. --- /dev/null
  127. +++ b/C/7zip/Compress/LZMA_Lib/ZLib.cpp
  128. @@ -0,0 +1,273 @@
  129. +/*
  130. + * lzma zlib simplified wrapper
  131. + *
  132. + * Copyright (c) 2005-2006 Oleg I. Vdovikin <oleg@cs.msu.su>
  133. + *
  134. + * This library is free software; you can redistribute
  135. + * it and/or modify it under the terms of the GNU Lesser
  136. + * General Public License as published by the Free Software
  137. + * Foundation; either version 2.1 of the License, or
  138. + * (at your option) any later version.
  139. + *
  140. + * This library is distributed in the hope that it will be
  141. + * useful, but WITHOUT ANY WARRANTY; without even the implied
  142. + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  143. + * PURPOSE. See the GNU Lesser General Public License
  144. + * for more details.
  145. + *
  146. + * You should have received a copy of the GNU Lesser General
  147. + * Public License along with this library; if not, write to
  148. + * the Free Software Foundation, Inc., 59 Temple Place,
  149. + * Suite 330, Boston, MA 02111-1307 USA
  150. + */
  151. +
  152. +/*
  153. + * default values for encoder/decoder used by wrapper
  154. + */
  155. +
  156. +#include <zlib.h>
  157. +
  158. +#define ZLIB_LC 3
  159. +#define ZLIB_LP 0
  160. +#define ZLIB_PB 2
  161. +
  162. +#ifdef WIN32
  163. +#include <initguid.h>
  164. +#else
  165. +#define INITGUID
  166. +#endif
  167. +
  168. +#include "../../../Common/MyWindows.h"
  169. +#include "../LZMA/LZMADecoder.h"
  170. +#include "../LZMA/LZMAEncoder.h"
  171. +
  172. +#define STG_E_SEEKERROR ((HRESULT)0x80030019L)
  173. +#define STG_E_MEDIUMFULL ((HRESULT)0x80030070L)
  174. +
  175. +class CInMemoryStream:
  176. + public IInStream,
  177. + public IStreamGetSize,
  178. + public CMyUnknownImp
  179. +{
  180. +public:
  181. + CInMemoryStream(const Bytef *data, UInt64 size) :
  182. + m_data(data), m_size(size), m_offset(0) {}
  183. +
  184. + virtual ~CInMemoryStream() {}
  185. +
  186. + MY_UNKNOWN_IMP2(IInStream, IStreamGetSize)
  187. +
  188. + STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize)
  189. + {
  190. + if (size > m_size - m_offset)
  191. + size = m_size - m_offset;
  192. +
  193. + if (size) {
  194. + memcpy(data, m_data + m_offset, size);
  195. + }
  196. +
  197. + m_offset += size;
  198. +
  199. + if (processedSize)
  200. + *processedSize = size;
  201. +
  202. + return S_OK;
  203. + }
  204. +
  205. + STDMETHOD(ReadPart)(void *data, UInt32 size, UInt32 *processedSize)
  206. + {
  207. + return Read(data, size, processedSize);
  208. + }
  209. +
  210. + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)
  211. + {
  212. + UInt64 _offset;
  213. +
  214. + if (seekOrigin == STREAM_SEEK_SET) _offset = offset;
  215. + else if (seekOrigin == STREAM_SEEK_CUR) _offset = m_offset + offset;
  216. + else if (seekOrigin == STREAM_SEEK_END) _offset = m_size;
  217. + else return STG_E_INVALIDFUNCTION;
  218. +
  219. + if (_offset < 0 || _offset > m_size)
  220. + return STG_E_SEEKERROR;
  221. +
  222. + m_offset = _offset;
  223. +
  224. + if (newPosition)
  225. + *newPosition = m_offset;
  226. +
  227. + return S_OK;
  228. + }
  229. +
  230. + STDMETHOD(GetSize)(UInt64 *size)
  231. + {
  232. + *size = m_size;
  233. + return S_OK;
  234. + }
  235. +protected:
  236. + const Bytef *m_data;
  237. + UInt64 m_size;
  238. + UInt64 m_offset;
  239. +};
  240. +
  241. +class COutMemoryStream:
  242. + public IOutStream,
  243. + public CMyUnknownImp
  244. +{
  245. +public:
  246. + COutMemoryStream(Bytef *data, UInt64 maxsize) :
  247. + m_data(data), m_size(0), m_maxsize(maxsize), m_offset(0) {}
  248. + virtual ~COutMemoryStream() {}
  249. +
  250. + MY_UNKNOWN_IMP1(IOutStream)
  251. +
  252. + STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize)
  253. + {
  254. + if (size > m_maxsize - m_offset)
  255. + size = m_maxsize - m_offset;
  256. +
  257. + if (size) {
  258. + memcpy(m_data + m_offset, data, size);
  259. + }
  260. +
  261. + m_offset += size;
  262. +
  263. + if (m_offset > m_size)
  264. + m_size = m_offset;
  265. +
  266. + if (processedSize)
  267. + *processedSize = size;
  268. +
  269. + return S_OK;
  270. + }
  271. +
  272. + STDMETHOD(WritePart)(const void *data, UInt32 size, UInt32 *processedSize)
  273. + {
  274. + return Write(data, size, processedSize);
  275. + }
  276. +
  277. + STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)
  278. + {
  279. + UInt64 _offset;
  280. +
  281. + if (seekOrigin == STREAM_SEEK_SET) _offset = offset;
  282. + else if (seekOrigin == STREAM_SEEK_CUR) _offset = m_offset + offset;
  283. + else if (seekOrigin == STREAM_SEEK_END) _offset = m_size;
  284. + else return STG_E_INVALIDFUNCTION;
  285. +
  286. + if (_offset < 0 || _offset > m_maxsize)
  287. + return STG_E_SEEKERROR;
  288. +
  289. + m_offset = _offset;
  290. +
  291. + if (newPosition)
  292. + *newPosition = m_offset;
  293. +
  294. + return S_OK;
  295. + }
  296. +
  297. + STDMETHOD(SetSize)(Int64 newSize)
  298. + {
  299. + if ((UInt64)newSize > m_maxsize)
  300. + return STG_E_MEDIUMFULL;
  301. +
  302. + return S_OK;
  303. + }
  304. +protected:
  305. + Bytef *m_data;
  306. + UInt64 m_size;
  307. + UInt64 m_maxsize;
  308. + UInt64 m_offset;
  309. +};
  310. +
  311. +ZEXTERN int ZEXPORT compress2 (Bytef *dest, uLongf *destLen,
  312. + const Bytef *source, uLong sourceLen,
  313. + int level)
  314. +{
  315. + CInMemoryStream *inStreamSpec = new CInMemoryStream(source, sourceLen);
  316. + CMyComPtr<ISequentialInStream> inStream = inStreamSpec;
  317. +
  318. + COutMemoryStream *outStreamSpec = new COutMemoryStream(dest, *destLen);
  319. + CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;
  320. +
  321. + NCompress::NLZMA::CEncoder *encoderSpec =
  322. + new NCompress::NLZMA::CEncoder;
  323. + CMyComPtr<ICompressCoder> encoder = encoderSpec;
  324. +
  325. + PROPID propIDs[] =
  326. + {
  327. + NCoderPropID::kDictionarySize,
  328. + NCoderPropID::kPosStateBits,
  329. + NCoderPropID::kLitContextBits,
  330. + NCoderPropID::kLitPosBits,
  331. + NCoderPropID::kAlgorithm,
  332. + NCoderPropID::kNumFastBytes,
  333. + NCoderPropID::kMatchFinder,
  334. + NCoderPropID::kEndMarker
  335. + };
  336. + const int kNumProps = sizeof(propIDs) / sizeof(propIDs[0]);
  337. +
  338. + PROPVARIANT properties[kNumProps];
  339. + for (int p = 0; p < 6; p++)
  340. + properties[p].vt = VT_UI4;
  341. + properties[0].ulVal = UInt32(1 << (level + 14));
  342. + properties[1].ulVal = UInt32(ZLIB_PB);
  343. + properties[2].ulVal = UInt32(ZLIB_LC); // for normal files
  344. + properties[3].ulVal = UInt32(ZLIB_LP); // for normal files
  345. + properties[4].ulVal = UInt32(2);
  346. + properties[5].ulVal = UInt32(128);
  347. +
  348. + properties[6].vt = VT_BSTR;
  349. + properties[6].bstrVal = (BSTR)(const wchar_t *)L"BT4";
  350. +
  351. + properties[7].vt = VT_BOOL;
  352. + properties[7].boolVal = VARIANT_TRUE;
  353. +
  354. + if (encoderSpec->SetCoderProperties(propIDs, properties, kNumProps) != S_OK)
  355. + return Z_MEM_ERROR; // should not happen
  356. +
  357. + HRESULT result = encoder->Code(inStream, outStream, 0, 0, 0);
  358. + if (result == E_OUTOFMEMORY)
  359. + {
  360. + return Z_MEM_ERROR;
  361. + }
  362. + else if (result != S_OK)
  363. + {
  364. + return Z_BUF_ERROR; // should not happen
  365. + }
  366. +
  367. + UInt64 fileSize;
  368. + outStreamSpec->Seek(0, STREAM_SEEK_END, &fileSize);
  369. + *destLen = fileSize;
  370. +
  371. + return Z_OK;
  372. +}
  373. +
  374. +ZEXTERN int ZEXPORT uncompress (Bytef *dest, uLongf *destLen,
  375. + const Bytef *source, uLong sourceLen)
  376. +{
  377. + CInMemoryStream *inStreamSpec = new CInMemoryStream(source, sourceLen);
  378. + CMyComPtr<ISequentialInStream> inStream = inStreamSpec;
  379. +
  380. + COutMemoryStream *outStreamSpec = new COutMemoryStream(dest, *destLen);
  381. + CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;
  382. +
  383. + NCompress::NLZMA::CDecoder *decoderSpec =
  384. + new NCompress::NLZMA::CDecoder;
  385. + CMyComPtr<ICompressCoder> decoder = decoderSpec;
  386. +
  387. + if (decoderSpec->SetDecoderPropertiesRaw(ZLIB_LC,
  388. + ZLIB_LP, ZLIB_PB, (1 << 23)) != S_OK) return Z_DATA_ERROR;
  389. +
  390. + UInt64 fileSize = *destLen;
  391. +
  392. + if (decoder->Code(inStream, outStream, 0, &fileSize, 0) != S_OK)
  393. + {
  394. + return Z_DATA_ERROR;
  395. + }
  396. +
  397. + outStreamSpec->Seek(0, STREAM_SEEK_END, &fileSize);
  398. + *destLen = fileSize;
  399. +
  400. + return Z_OK;
  401. +}