mea culpa, I was too busy with the Atheros port and broke squashfs-lzma. Oleg sent...
[openwrt.git] / openwrt / target / lzma / lzma-zlib.patch
1 diff -urN lzma432/C/7zip/Compress/LZMA/LZMADecoder.cpp lzma/C/7zip/Compress/LZMA/LZMADecoder.cpp
2 --- lzma432/C/7zip/Compress/LZMA/LZMADecoder.cpp        2005-09-22 12:55:34.000000000 +0400
3 +++ lzma/C/7zip/Compress/LZMA/LZMADecoder.cpp   2006-03-24 13:23:09.634551840 +0300
4 @@ -274,12 +274,17 @@
5    Byte remainder = (Byte)(properties[0] / 9);\r
6    int lp = remainder % 5;\r
7    int pb = remainder / 5;\r
8 -  if (pb > NLength::kNumPosStatesBitsMax)\r
9 -    return E_INVALIDARG;\r
10 -  _posStateMask = (1 << pb) - 1;\r
11    UInt32 dictionarySize = 0;\r
12    for (int i = 0; i < 4; i++)\r
13      dictionarySize += ((UInt32)(properties[1 + i])) << (i * 8);\r
14 +  return SetDecoderPropertiesRaw(lc, lp, pb, dictionarySize);\r
15 +}\r
16 +\r
17 +STDMETHODIMP CDecoder::SetDecoderPropertiesRaw(int lc, int lp, int pb, UInt32 dictionarySize)\r
18 +{\r
19 +  if (pb > NLength::kNumPosStatesBitsMax)\r
20 +    return E_INVALIDARG;\r
21 +  _posStateMask = (1 << pb) - 1;\r
22    if (!_outWindowStream.Create(dictionarySize))\r
23      return E_OUTOFMEMORY;\r
24    if (!_literalDecoder.Create(lp, lc))\r
25 diff -urN lzma432/C/7zip/Compress/LZMA/LZMADecoder.h lzma/C/7zip/Compress/LZMA/LZMADecoder.h
26 --- lzma432/C/7zip/Compress/LZMA/LZMADecoder.h  2005-09-19 10:10:06.000000000 +0400
27 +++ lzma/C/7zip/Compress/LZMA/LZMADecoder.h     2006-03-24 13:23:09.635551552 +0300
28 @@ -228,6 +228,7 @@
29        ICompressProgressInfo *progress);\r
30  \r
31    STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);\r
32 +  STDMETHOD(SetDecoderPropertiesRaw)(int lc, int lp, int pb, UInt32 dictionarySize);\r
33  \r
34    STDMETHOD(GetInStreamProcessedSize)(UInt64 *value);\r
35  \r
36 diff -urN lzma432/C/7zip/Compress/LZMA_Lib/makefile lzma/C/7zip/Compress/LZMA_Lib/makefile
37 --- lzma432/C/7zip/Compress/LZMA_Lib/makefile   1970-01-01 03:00:00.000000000 +0300
38 +++ lzma/C/7zip/Compress/LZMA_Lib/makefile      2006-03-24 13:26:58.254841791 +0300
39 @@ -0,0 +1,88 @@
40 +PROG = liblzma.a\r
41 +CXX = g++ -O3 -Wall\r
42 +AR = ar
43 +RM = rm -f\r
44 +CFLAGS = -c  -I ../../../\r
45 +\r
46 +OBJS = \\r
47 +  ZLib.o \\r
48 +  LZMADecoder.o \\r
49 +  LZMAEncoder.o \\r
50 +  LZInWindow.o \\r
51 +  LZOutWindow.o \\r
52 +  RangeCoderBit.o \\r
53 +  InBuffer.o \\r
54 +  OutBuffer.o \\r
55 +  FileStreams.o \\r
56 +  Alloc.o \\r
57 +  C_FileIO.o \\r
58 +  CommandLineParser.o \\r
59 +  CRC.o \\r
60 +  String.o \\r
61 +  StringConvert.o \\r
62 +  StringToInt.o \\r
63 +  Vector.o \\r
64 +\r
65 +\r
66 +all: $(PROG)\r
67 +\r
68 +$(PROG): $(OBJS)\r
69 +       $(AR) r $(PROG) $(OBJS)\r
70 +\r
71 +ZLib.o: ZLib.cpp\r
72 +       $(CXX) $(CFLAGS) ZLib.cpp\r
73 +\r
74 +LZMADecoder.o: ../LZMA/LZMADecoder.cpp\r
75 +       $(CXX) $(CFLAGS) ../LZMA/LZMADecoder.cpp\r
76 +\r
77 +LZMAEncoder.o: ../LZMA/LZMAEncoder.cpp\r
78 +       $(CXX) $(CFLAGS) ../LZMA/LZMAEncoder.cpp\r
79 +\r
80 +LZInWindow.o: ../LZ/LZInWindow.cpp\r
81 +       $(CXX) $(CFLAGS) ../LZ/LZInWindow.cpp\r
82 +\r
83 +LZOutWindow.o: ../LZ/LZOutWindow.cpp\r
84 +       $(CXX) $(CFLAGS) ../LZ/LZOutWindow.cpp\r
85 +\r
86 +RangeCoderBit.o: ../RangeCoder/RangeCoderBit.cpp\r
87 +       $(CXX) $(CFLAGS) ../RangeCoder/RangeCoderBit.cpp\r
88 +\r
89 +InBuffer.o: ../../Common/InBuffer.cpp\r
90 +       $(CXX) $(CFLAGS) ../../Common/InBuffer.cpp\r
91 +\r
92 +OutBuffer.o: ../../Common/OutBuffer.cpp\r
93 +       $(CXX) $(CFLAGS) ../../Common/OutBuffer.cpp\r
94 +\r
95 +FileStreams.o: ../../Common/FileStreams.cpp\r
96 +       $(CXX) $(CFLAGS) ../../Common/FileStreams.cpp\r
97 +\r
98 +Alloc.o: ../../../Common/Alloc.cpp\r
99 +       $(CXX) $(CFLAGS) ../../../Common/Alloc.cpp\r
100 +\r
101 +C_FileIO.o: ../../../Common/C_FileIO.cpp\r
102 +       $(CXX) $(CFLAGS) ../../../Common/C_FileIO.cpp\r
103 +\r
104 +CommandLineParser.o: ../../../Common/CommandLineParser.cpp\r
105 +       $(CXX) $(CFLAGS) ../../../Common/CommandLineParser.cpp\r
106 +\r
107 +CRC.o: ../../../Common/CRC.cpp\r
108 +       $(CXX) $(CFLAGS) ../../../Common/CRC.cpp\r
109 +\r
110 +MyWindows.o: ../../../Common/MyWindows.cpp\r
111 +       $(CXX) $(CFLAGS) ../../../Common/MyWindows.cpp\r
112 +\r
113 +String.o: ../../../Common/String.cpp\r
114 +       $(CXX) $(CFLAGS) ../../../Common/String.cpp\r
115 +\r
116 +StringConvert.o: ../../../Common/StringConvert.cpp\r
117 +       $(CXX) $(CFLAGS) ../../../Common/StringConvert.cpp\r
118 +\r
119 +StringToInt.o: ../../../Common/StringToInt.cpp\r
120 +       $(CXX) $(CFLAGS) ../../../Common/StringToInt.cpp\r
121 +\r
122 +Vector.o: ../../../Common/Vector.cpp\r
123 +       $(CXX) $(CFLAGS) ../../../Common/Vector.cpp\r
124 +\r
125 +clean:\r
126 +       -$(RM) $(PROG) $(OBJS)\r
127 +\r
128 diff -urN lzma432/C/7zip/Compress/LZMA_Lib/ZLib.cpp lzma/C/7zip/Compress/LZMA_Lib/ZLib.cpp
129 --- lzma432/C/7zip/Compress/LZMA_Lib/ZLib.cpp   1970-01-01 03:00:00.000000000 +0300
130 +++ lzma/C/7zip/Compress/LZMA_Lib/ZLib.cpp      2006-03-24 13:23:09.638550690 +0300
131 @@ -0,0 +1,273 @@
132 +/*\r
133 + * lzma zlib simplified wrapper\r
134 + *\r
135 + * Copyright (c) 2005-2006 Oleg I. Vdovikin <oleg@cs.msu.su>\r
136 + *\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
142 + *\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
148 + *\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
153 + */\r
154 +\r
155 +/*\r
156 + * default values for encoder/decoder used by wrapper\r
157 + */\r
158 +\r
159 +#include <zlib.h>\r
160 +\r
161 +#define ZLIB_LC 3\r
162 +#define ZLIB_LP 0\r
163 +#define ZLIB_PB 2\r
164 +\r
165 +#ifdef WIN32\r
166 +#include <initguid.h>\r
167 +#else\r
168 +#define INITGUID\r
169 +#endif\r
170 +\r
171 +#include "../../../Common/MyWindows.h"\r
172 +#include "../LZMA/LZMADecoder.h"\r
173 +#include "../LZMA/LZMAEncoder.h"\r
174 +\r
175 +#define STG_E_SEEKERROR                  ((HRESULT)0x80030019L)\r
176 +#define STG_E_MEDIUMFULL                 ((HRESULT)0x80030070L)\r
177 +\r
178 +class CInMemoryStream: \r
179 +  public IInStream,\r
180 +  public IStreamGetSize,\r
181 +  public CMyUnknownImp\r
182 +{\r
183 +public:\r
184 +  CInMemoryStream(const Bytef *data, UInt64 size) : \r
185 +         m_data(data), m_size(size), m_offset(0) {}\r
186 +\r
187 +  virtual ~CInMemoryStream() {}\r
188 +\r
189 +  MY_UNKNOWN_IMP2(IInStream, IStreamGetSize)\r
190 +\r
191 +  STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize)\r
192 +  {\r
193 +         if (size > m_size - m_offset) \r
194 +                 size = m_size - m_offset;\r
195 +\r
196 +         if (size) {\r
197 +                 memcpy(data, m_data + m_offset, size);\r
198 +         }\r
199 +\r
200 +         m_offset += size;\r
201 +\r
202 +         if (processedSize) \r
203 +                 *processedSize = size;\r
204 +\r
205 +         return S_OK;\r
206 +  }\r
207 +\r
208 +  STDMETHOD(ReadPart)(void *data, UInt32 size, UInt32 *processedSize)\r
209 +  {\r
210 +       return Read(data, size, processedSize);\r
211 +  }\r
212 +\r
213 +  STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)\r
214 +  {\r
215 +         UInt64 _offset;\r
216 +\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
221 +\r
222 +         if (_offset < 0 || _offset > m_size)\r
223 +                 return STG_E_SEEKERROR;\r
224 +\r
225 +         m_offset = _offset;\r
226 +\r
227 +         if (newPosition)\r
228 +                 *newPosition = m_offset;\r
229 +\r
230 +         return S_OK;\r
231 +  }\r
232 +\r
233 +  STDMETHOD(GetSize)(UInt64 *size)\r
234 +  {\r
235 +         *size = m_size;\r
236 +         return S_OK;\r
237 +  }\r
238 +protected:\r
239 +       const Bytef *m_data;\r
240 +       UInt64 m_size;\r
241 +       UInt64 m_offset;\r
242 +};\r
243 +\r
244 +class COutMemoryStream: \r
245 +  public IOutStream,\r
246 +  public CMyUnknownImp\r
247 +{\r
248 +public:\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
252 +  \r
253 +  MY_UNKNOWN_IMP1(IOutStream)\r
254 +\r
255 +  STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize)\r
256 +  {\r
257 +         if (size > m_maxsize - m_offset) \r
258 +                 size = m_maxsize - m_offset;\r
259 +\r
260 +         if (size) {\r
261 +                 memcpy(m_data + m_offset, data, size);\r
262 +         }\r
263 +\r
264 +         m_offset += size;\r
265 +\r
266 +         if (m_offset > m_size)\r
267 +               m_size = m_offset;\r
268 +\r
269 +         if (processedSize) \r
270 +                 *processedSize = size;\r
271 +\r
272 +         return S_OK;\r
273 +  }\r
274 +  \r
275 +  STDMETHOD(WritePart)(const void *data, UInt32 size, UInt32 *processedSize)\r
276 +  {\r
277 +         return Write(data, size, processedSize);\r
278 +  }\r
279 +\r
280 +  STDMETHOD(Seek)(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition)\r
281 +  {\r
282 +         UInt64 _offset;\r
283 +\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
288 +\r
289 +         if (_offset < 0 || _offset > m_maxsize)\r
290 +                 return STG_E_SEEKERROR;\r
291 +\r
292 +         m_offset = _offset;\r
293 +\r
294 +         if (newPosition)\r
295 +                 *newPosition = m_offset;\r
296 +\r
297 +         return S_OK;\r
298 +  }\r
299 +  \r
300 +  STDMETHOD(SetSize)(Int64 newSize)\r
301 +  {\r
302 +         if ((UInt64)newSize > m_maxsize) \r
303 +                 return STG_E_MEDIUMFULL;\r
304 +\r
305 +         return S_OK;\r
306 +  }\r
307 +protected:\r
308 +       Bytef *m_data;\r
309 +       UInt64 m_size;\r
310 +       UInt64 m_maxsize;\r
311 +       UInt64 m_offset;\r
312 +};\r
313 +\r
314 +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,\r
315 +                                  const Bytef *source, uLong sourceLen,\r
316 +                                  int level))\r
317 +{\r
318 +       CInMemoryStream *inStreamSpec = new CInMemoryStream(source, sourceLen);\r
319 +       CMyComPtr<ISequentialInStream> inStream = inStreamSpec;\r
320 +       \r
321 +       COutMemoryStream *outStreamSpec = new COutMemoryStream(dest, *destLen);\r
322 +       CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;\r
323 +       \r
324 +       NCompress::NLZMA::CEncoder *encoderSpec = \r
325 +               new NCompress::NLZMA::CEncoder;\r
326 +       CMyComPtr<ICompressCoder> encoder = encoderSpec;\r
327 +       \r
328 +       PROPID propIDs[] = \r
329 +       {\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
338 +       };\r
339 +       const int kNumProps = sizeof(propIDs) / sizeof(propIDs[0]);\r
340 +       \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
350 +       \r
351 +       properties[6].vt = VT_BSTR;\r
352 +       properties[6].bstrVal = (BSTR)(const wchar_t *)L"BT4";\r
353 +       \r
354 +       properties[7].vt = VT_BOOL;\r
355 +       properties[7].boolVal = VARIANT_TRUE;\r
356 +       \r
357 +       if (encoderSpec->SetCoderProperties(propIDs, properties, kNumProps) != S_OK)\r
358 +               return Z_MEM_ERROR; // should not happen\r
359 +       \r
360 +       HRESULT result = encoder->Code(inStream, outStream, 0, 0, 0);\r
361 +       if (result == E_OUTOFMEMORY)\r
362 +       {\r
363 +               return Z_MEM_ERROR;\r
364 +       }   \r
365 +       else if (result != S_OK)\r
366 +       {\r
367 +               return Z_BUF_ERROR;     // should not happen\r
368 +       }   \r
369 +       \r
370 +       UInt64 fileSize;\r
371 +       outStreamSpec->Seek(0, STREAM_SEEK_END, &fileSize);\r
372 +       *destLen = fileSize;\r
373 +       \r
374 +       return Z_OK;\r
375 +}\r
376 +\r
377 +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,\r
378 +                                   const Bytef *source, uLong sourceLen))\r
379 +{\r
380 +       CInMemoryStream *inStreamSpec = new CInMemoryStream(source, sourceLen);\r
381 +       CMyComPtr<ISequentialInStream> inStream = inStreamSpec;\r
382 +       \r
383 +       COutMemoryStream *outStreamSpec = new COutMemoryStream(dest, *destLen);\r
384 +       CMyComPtr<ISequentialOutStream> outStream = outStreamSpec;\r
385 +       \r
386 +       NCompress::NLZMA::CDecoder *decoderSpec = \r
387 +               new NCompress::NLZMA::CDecoder;\r
388 +       CMyComPtr<ICompressCoder> decoder = decoderSpec;\r
389 +       \r
390 +       if (decoderSpec->SetDecoderPropertiesRaw(ZLIB_LC, \r
391 +               ZLIB_LP, ZLIB_PB, (1 << 23)) != S_OK) return Z_DATA_ERROR;\r
392 +       \r
393 +       UInt64 fileSize = *destLen;\r
394 +       \r
395 +       if (decoder->Code(inStream, outStream, 0, &fileSize, 0) != S_OK)\r
396 +       {\r
397 +               return Z_DATA_ERROR;\r
398 +       }\r
399 +       \r
400 +       outStreamSpec->Seek(0, STREAM_SEEK_END, &fileSize);\r
401 +       *destLen = fileSize;\r
402 +       \r
403 +       return Z_OK;\r
404 +}\r