finally move buildroot-ng to trunk
[openwrt/staging/stintel.git] / target / linux / generic-2.6 / patches / 002-squashfs_lzma.patch
1 diff -Nur linux-2.6.16/fs/squashfs/inode.c linux-2.6.16-owrt/fs/squashfs/inode.c
2 --- linux-2.6.16/fs/squashfs/inode.c 2006-03-21 10:55:59.000000000 +0100
3 +++ linux-2.6.16-owrt/fs/squashfs/inode.c 2006-03-21 12:24:37.000000000 +0100
4 @@ -4,6 +4,9 @@
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006
6 * Phillip Lougher <phillip@lougher.org.uk>
7 *
8 + * LZMA decompressor support added by Oleg I. Vdovikin
9 + * Copyright (c) 2005 Oleg I.Vdovikin <oleg@cs.msu.su>
10 + *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2,
14 @@ -21,6 +24,7 @@
15 * inode.c
16 */
17
18 +#define SQUASHFS_LZMA
19 #include <linux/types.h>
20 #include <linux/squashfs_fs.h>
21 #include <linux/module.h>
22 @@ -44,6 +48,19 @@
23
24 #include "squashfs.h"
25
26 +#ifdef SQUASHFS_LZMA
27 +#include "LzmaDecode.h"
28 +
29 +/* default LZMA settings, should be in sync with mksquashfs */
30 +#define LZMA_LC 3
31 +#define LZMA_LP 0
32 +#define LZMA_PB 2
33 +
34 +#define LZMA_WORKSPACE_SIZE ((LZMA_BASE_SIZE + \
35 + (LZMA_LIT_SIZE << (LZMA_LC + LZMA_LP))) * sizeof(CProb))
36 +
37 +#endif
38 +
39 static void squashfs_put_super(struct super_block *);
40 static int squashfs_statfs(struct super_block *, struct kstatfs *);
41 static int squashfs_symlink_readpage(struct file *file, struct page *page);
42 @@ -64,7 +81,11 @@
43 const char *, void *);
44
45
46 +#ifdef SQUASHFS_LZMA
47 +static unsigned char lzma_workspace[LZMA_WORKSPACE_SIZE];
48 +#else
49 static z_stream stream;
50 +#endif
51
52 static struct file_system_type squashfs_fs_type = {
53 .owner = THIS_MODULE,
54 @@ -249,6 +270,15 @@
55 if (compressed) {
56 int zlib_err;
57
58 +#ifdef SQUASHFS_LZMA
59 + if ((zlib_err = LzmaDecode(lzma_workspace,
60 + LZMA_WORKSPACE_SIZE, LZMA_LC, LZMA_LP, LZMA_PB,
61 + c_buffer, c_byte, buffer, msblk->read_size, &bytes)) != LZMA_RESULT_OK)
62 + {
63 + ERROR("lzma returned unexpected result 0x%x\n", zlib_err);
64 + bytes = 0;
65 + }
66 +#else
67 stream.next_in = c_buffer;
68 stream.avail_in = c_byte;
69 stream.next_out = buffer;
70 @@ -263,6 +293,7 @@
71 bytes = 0;
72 } else
73 bytes = stream.total_out;
74 +#endif
75
76 up(&msblk->read_data_mutex);
77 }
78 @@ -2046,15 +2077,19 @@
79 printk(KERN_INFO "squashfs: version 3.0 (2006/03/15) "
80 "Phillip Lougher\n");
81
82 +#ifndef SQUASHFS_LZMA
83 if (!(stream.workspace = vmalloc(zlib_inflate_workspacesize()))) {
84 ERROR("Failed to allocate zlib workspace\n");
85 destroy_inodecache();
86 err = -ENOMEM;
87 goto out;
88 }
89 +#endif
90
91 if ((err = register_filesystem(&squashfs_fs_type))) {
92 +#ifndef SQUASHFS_LZMA
93 vfree(stream.workspace);
94 +#endif
95 destroy_inodecache();
96 }
97
98 @@ -2065,7 +2100,9 @@
99
100 static void __exit exit_squashfs_fs(void)
101 {
102 +#ifndef SQUASHFS_LZMA
103 vfree(stream.workspace);
104 +#endif
105 unregister_filesystem(&squashfs_fs_type);
106 destroy_inodecache();
107 }
108 diff -Nur linux-2.6.16/fs/squashfs/LzmaDecode.c linux-2.6.16-owrt/fs/squashfs/LzmaDecode.c
109 --- linux-2.6.16/fs/squashfs/LzmaDecode.c 1970-01-01 01:00:00.000000000 +0100
110 +++ linux-2.6.16-owrt/fs/squashfs/LzmaDecode.c 2006-03-21 10:56:57.000000000 +0100
111 @@ -0,0 +1,663 @@
112 +/*
113 + LzmaDecode.c
114 + LZMA Decoder
115 +
116 + LZMA SDK 4.05 Copyright (c) 1999-2004 Igor Pavlov (2004-08-25)
117 + http://www.7-zip.org/
118 +
119 + LZMA SDK is licensed under two licenses:
120 + 1) GNU Lesser General Public License (GNU LGPL)
121 + 2) Common Public License (CPL)
122 + It means that you can select one of these two licenses and
123 + follow rules of that license.
124 +
125 + SPECIAL EXCEPTION:
126 + Igor Pavlov, as the author of this code, expressly permits you to
127 + statically or dynamically link your code (or bind by name) to the
128 + interfaces of this file without subjecting your linked code to the
129 + terms of the CPL or GNU LGPL. Any modifications or additions
130 + to this file, however, are subject to the LGPL or CPL terms.
131 +*/
132 +
133 +#include "LzmaDecode.h"
134 +
135 +#ifndef Byte
136 +#define Byte unsigned char
137 +#endif
138 +
139 +#define kNumTopBits 24
140 +#define kTopValue ((UInt32)1 << kNumTopBits)
141 +
142 +#define kNumBitModelTotalBits 11
143 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
144 +#define kNumMoveBits 5
145 +
146 +typedef struct _CRangeDecoder
147 +{
148 + Byte *Buffer;
149 + Byte *BufferLim;
150 + UInt32 Range;
151 + UInt32 Code;
152 + #ifdef _LZMA_IN_CB
153 + ILzmaInCallback *InCallback;
154 + int Result;
155 + #endif
156 + int ExtraBytes;
157 +} CRangeDecoder;
158 +
159 +Byte RangeDecoderReadByte(CRangeDecoder *rd)
160 +{
161 + if (rd->Buffer == rd->BufferLim)
162 + {
163 + #ifdef _LZMA_IN_CB
164 + UInt32 size;
165 + rd->Result = rd->InCallback->Read(rd->InCallback, &rd->Buffer, &size);
166 + rd->BufferLim = rd->Buffer + size;
167 + if (size == 0)
168 + #endif
169 + {
170 + rd->ExtraBytes = 1;
171 + return 0xFF;
172 + }
173 + }
174 + return (*rd->Buffer++);
175 +}
176 +
177 +/* #define ReadByte (*rd->Buffer++) */
178 +#define ReadByte (RangeDecoderReadByte(rd))
179 +
180 +void RangeDecoderInit(CRangeDecoder *rd,
181 + #ifdef _LZMA_IN_CB
182 + ILzmaInCallback *inCallback
183 + #else
184 + Byte *stream, UInt32 bufferSize
185 + #endif
186 + )
187 +{
188 + int i;
189 + #ifdef _LZMA_IN_CB
190 + rd->InCallback = inCallback;
191 + rd->Buffer = rd->BufferLim = 0;
192 + #else
193 + rd->Buffer = stream;
194 + rd->BufferLim = stream + bufferSize;
195 + #endif
196 + rd->ExtraBytes = 0;
197 + rd->Code = 0;
198 + rd->Range = (0xFFFFFFFF);
199 + for(i = 0; i < 5; i++)
200 + rd->Code = (rd->Code << 8) | ReadByte;
201 +}
202 +
203 +#define RC_INIT_VAR UInt32 range = rd->Range; UInt32 code = rd->Code;
204 +#define RC_FLUSH_VAR rd->Range = range; rd->Code = code;
205 +#define RC_NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | ReadByte; }
206 +
207 +UInt32 RangeDecoderDecodeDirectBits(CRangeDecoder *rd, int numTotalBits)
208 +{
209 + RC_INIT_VAR
210 + UInt32 result = 0;
211 + int i;
212 + for (i = numTotalBits; i > 0; i--)
213 + {
214 + /* UInt32 t; */
215 + range >>= 1;
216 +
217 + result <<= 1;
218 + if (code >= range)
219 + {
220 + code -= range;
221 + result |= 1;
222 + }
223 + /*
224 + t = (code - range) >> 31;
225 + t &= 1;
226 + code -= range & (t - 1);
227 + result = (result + result) | (1 - t);
228 + */
229 + RC_NORMALIZE
230 + }
231 + RC_FLUSH_VAR
232 + return result;
233 +}
234 +
235 +int RangeDecoderBitDecode(CProb *prob, CRangeDecoder *rd)
236 +{
237 + UInt32 bound = (rd->Range >> kNumBitModelTotalBits) * *prob;
238 + if (rd->Code < bound)
239 + {
240 + rd->Range = bound;
241 + *prob += (kBitModelTotal - *prob) >> kNumMoveBits;
242 + if (rd->Range < kTopValue)
243 + {
244 + rd->Code = (rd->Code << 8) | ReadByte;
245 + rd->Range <<= 8;
246 + }
247 + return 0;
248 + }
249 + else
250 + {
251 + rd->Range -= bound;
252 + rd->Code -= bound;
253 + *prob -= (*prob) >> kNumMoveBits;
254 + if (rd->Range < kTopValue)
255 + {
256 + rd->Code = (rd->Code << 8) | ReadByte;
257 + rd->Range <<= 8;
258 + }
259 + return 1;
260 + }
261 +}
262 +
263 +#define RC_GET_BIT2(prob, mi, A0, A1) \
264 + UInt32 bound = (range >> kNumBitModelTotalBits) * *prob; \
265 + if (code < bound) \
266 + { A0; range = bound; *prob += (kBitModelTotal - *prob) >> kNumMoveBits; mi <<= 1; } \
267 + else \
268 + { A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
269 + RC_NORMALIZE
270 +
271 +#define RC_GET_BIT(prob, mi) RC_GET_BIT2(prob, mi, ; , ;)
272 +
273 +int RangeDecoderBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
274 +{
275 + int mi = 1;
276 + int i;
277 + #ifdef _LZMA_LOC_OPT
278 + RC_INIT_VAR
279 + #endif
280 + for(i = numLevels; i > 0; i--)
281 + {
282 + #ifdef _LZMA_LOC_OPT
283 + CProb *prob = probs + mi;
284 + RC_GET_BIT(prob, mi)
285 + #else
286 + mi = (mi + mi) + RangeDecoderBitDecode(probs + mi, rd);
287 + #endif
288 + }
289 + #ifdef _LZMA_LOC_OPT
290 + RC_FLUSH_VAR
291 + #endif
292 + return mi - (1 << numLevels);
293 +}
294 +
295 +int RangeDecoderReverseBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
296 +{
297 + int mi = 1;
298 + int i;
299 + int symbol = 0;
300 + #ifdef _LZMA_LOC_OPT
301 + RC_INIT_VAR
302 + #endif
303 + for(i = 0; i < numLevels; i++)
304 + {
305 + #ifdef _LZMA_LOC_OPT
306 + CProb *prob = probs + mi;
307 + RC_GET_BIT2(prob, mi, ; , symbol |= (1 << i))
308 + #else
309 + int bit = RangeDecoderBitDecode(probs + mi, rd);
310 + mi = mi + mi + bit;
311 + symbol |= (bit << i);
312 + #endif
313 + }
314 + #ifdef _LZMA_LOC_OPT
315 + RC_FLUSH_VAR
316 + #endif
317 + return symbol;
318 +}
319 +
320 +Byte LzmaLiteralDecode(CProb *probs, CRangeDecoder *rd)
321 +{
322 + int symbol = 1;
323 + #ifdef _LZMA_LOC_OPT
324 + RC_INIT_VAR
325 + #endif
326 + do
327 + {
328 + #ifdef _LZMA_LOC_OPT
329 + CProb *prob = probs + symbol;
330 + RC_GET_BIT(prob, symbol)
331 + #else
332 + symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
333 + #endif
334 + }
335 + while (symbol < 0x100);
336 + #ifdef _LZMA_LOC_OPT
337 + RC_FLUSH_VAR
338 + #endif
339 + return symbol;
340 +}
341 +
342 +Byte LzmaLiteralDecodeMatch(CProb *probs, CRangeDecoder *rd, Byte matchByte)
343 +{
344 + int symbol = 1;
345 + #ifdef _LZMA_LOC_OPT
346 + RC_INIT_VAR
347 + #endif
348 + do
349 + {
350 + int bit;
351 + int matchBit = (matchByte >> 7) & 1;
352 + matchByte <<= 1;
353 + #ifdef _LZMA_LOC_OPT
354 + {
355 + CProb *prob = probs + ((1 + matchBit) << 8) + symbol;
356 + RC_GET_BIT2(prob, symbol, bit = 0, bit = 1)
357 + }
358 + #else
359 + bit = RangeDecoderBitDecode(probs + ((1 + matchBit) << 8) + symbol, rd);
360 + symbol = (symbol << 1) | bit;
361 + #endif
362 + if (matchBit != bit)
363 + {
364 + while (symbol < 0x100)
365 + {
366 + #ifdef _LZMA_LOC_OPT
367 + CProb *prob = probs + symbol;
368 + RC_GET_BIT(prob, symbol)
369 + #else
370 + symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
371 + #endif
372 + }
373 + break;
374 + }
375 + }
376 + while (symbol < 0x100);
377 + #ifdef _LZMA_LOC_OPT
378 + RC_FLUSH_VAR
379 + #endif
380 + return symbol;
381 +}
382 +
383 +#define kNumPosBitsMax 4
384 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
385 +
386 +#define kLenNumLowBits 3
387 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
388 +#define kLenNumMidBits 3
389 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
390 +#define kLenNumHighBits 8
391 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
392 +
393 +#define LenChoice 0
394 +#define LenChoice2 (LenChoice + 1)
395 +#define LenLow (LenChoice2 + 1)
396 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
397 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
398 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
399 +
400 +int LzmaLenDecode(CProb *p, CRangeDecoder *rd, int posState)
401 +{
402 + if(RangeDecoderBitDecode(p + LenChoice, rd) == 0)
403 + return RangeDecoderBitTreeDecode(p + LenLow +
404 + (posState << kLenNumLowBits), kLenNumLowBits, rd);
405 + if(RangeDecoderBitDecode(p + LenChoice2, rd) == 0)
406 + return kLenNumLowSymbols + RangeDecoderBitTreeDecode(p + LenMid +
407 + (posState << kLenNumMidBits), kLenNumMidBits, rd);
408 + return kLenNumLowSymbols + kLenNumMidSymbols +
409 + RangeDecoderBitTreeDecode(p + LenHigh, kLenNumHighBits, rd);
410 +}
411 +
412 +#define kNumStates 12
413 +
414 +#define kStartPosModelIndex 4
415 +#define kEndPosModelIndex 14
416 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
417 +
418 +#define kNumPosSlotBits 6
419 +#define kNumLenToPosStates 4
420 +
421 +#define kNumAlignBits 4
422 +#define kAlignTableSize (1 << kNumAlignBits)
423 +
424 +#define kMatchMinLen 2
425 +
426 +#define IsMatch 0
427 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
428 +#define IsRepG0 (IsRep + kNumStates)
429 +#define IsRepG1 (IsRepG0 + kNumStates)
430 +#define IsRepG2 (IsRepG1 + kNumStates)
431 +#define IsRep0Long (IsRepG2 + kNumStates)
432 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
433 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
434 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
435 +#define LenCoder (Align + kAlignTableSize)
436 +#define RepLenCoder (LenCoder + kNumLenProbs)
437 +#define Literal (RepLenCoder + kNumLenProbs)
438 +
439 +#if Literal != LZMA_BASE_SIZE
440 +StopCompilingDueBUG
441 +#endif
442 +
443 +#ifdef _LZMA_OUT_READ
444 +
445 +typedef struct _LzmaVarState
446 +{
447 + CRangeDecoder RangeDecoder;
448 + Byte *Dictionary;
449 + UInt32 DictionarySize;
450 + UInt32 DictionaryPos;
451 + UInt32 GlobalPos;
452 + UInt32 Reps[4];
453 + int lc;
454 + int lp;
455 + int pb;
456 + int State;
457 + int PreviousIsMatch;
458 + int RemainLen;
459 +} LzmaVarState;
460 +
461 +int LzmaDecoderInit(
462 + unsigned char *buffer, UInt32 bufferSize,
463 + int lc, int lp, int pb,
464 + unsigned char *dictionary, UInt32 dictionarySize,
465 + #ifdef _LZMA_IN_CB
466 + ILzmaInCallback *inCallback
467 + #else
468 + unsigned char *inStream, UInt32 inSize
469 + #endif
470 + )
471 +{
472 + LzmaVarState *vs = (LzmaVarState *)buffer;
473 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
474 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
475 + UInt32 i;
476 + if (bufferSize < numProbs * sizeof(CProb) + sizeof(LzmaVarState))
477 + return LZMA_RESULT_NOT_ENOUGH_MEM;
478 + vs->Dictionary = dictionary;
479 + vs->DictionarySize = dictionarySize;
480 + vs->DictionaryPos = 0;
481 + vs->GlobalPos = 0;
482 + vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
483 + vs->lc = lc;
484 + vs->lp = lp;
485 + vs->pb = pb;
486 + vs->State = 0;
487 + vs->PreviousIsMatch = 0;
488 + vs->RemainLen = 0;
489 + dictionary[dictionarySize - 1] = 0;
490 + for (i = 0; i < numProbs; i++)
491 + p[i] = kBitModelTotal >> 1;
492 + RangeDecoderInit(&vs->RangeDecoder,
493 + #ifdef _LZMA_IN_CB
494 + inCallback
495 + #else
496 + inStream, inSize
497 + #endif
498 + );
499 + return LZMA_RESULT_OK;
500 +}
501 +
502 +int LzmaDecode(unsigned char *buffer,
503 + unsigned char *outStream, UInt32 outSize,
504 + UInt32 *outSizeProcessed)
505 +{
506 + LzmaVarState *vs = (LzmaVarState *)buffer;
507 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
508 + CRangeDecoder rd = vs->RangeDecoder;
509 + int state = vs->State;
510 + int previousIsMatch = vs->PreviousIsMatch;
511 + Byte previousByte;
512 + UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
513 + UInt32 nowPos = 0;
514 + UInt32 posStateMask = (1 << (vs->pb)) - 1;
515 + UInt32 literalPosMask = (1 << (vs->lp)) - 1;
516 + int lc = vs->lc;
517 + int len = vs->RemainLen;
518 + UInt32 globalPos = vs->GlobalPos;
519 +
520 + Byte *dictionary = vs->Dictionary;
521 + UInt32 dictionarySize = vs->DictionarySize;
522 + UInt32 dictionaryPos = vs->DictionaryPos;
523 +
524 + if (len == -1)
525 + {
526 + *outSizeProcessed = 0;
527 + return LZMA_RESULT_OK;
528 + }
529 +
530 + while(len > 0 && nowPos < outSize)
531 + {
532 + UInt32 pos = dictionaryPos - rep0;
533 + if (pos >= dictionarySize)
534 + pos += dictionarySize;
535 + outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
536 + if (++dictionaryPos == dictionarySize)
537 + dictionaryPos = 0;
538 + len--;
539 + }
540 + if (dictionaryPos == 0)
541 + previousByte = dictionary[dictionarySize - 1];
542 + else
543 + previousByte = dictionary[dictionaryPos - 1];
544 +#else
545 +
546 +int LzmaDecode(
547 + Byte *buffer, UInt32 bufferSize,
548 + int lc, int lp, int pb,
549 + #ifdef _LZMA_IN_CB
550 + ILzmaInCallback *inCallback,
551 + #else
552 + unsigned char *inStream, UInt32 inSize,
553 + #endif
554 + unsigned char *outStream, UInt32 outSize,
555 + UInt32 *outSizeProcessed)
556 +{
557 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
558 + CProb *p = (CProb *)buffer;
559 + CRangeDecoder rd;
560 + UInt32 i;
561 + int state = 0;
562 + int previousIsMatch = 0;
563 + Byte previousByte = 0;
564 + UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
565 + UInt32 nowPos = 0;
566 + UInt32 posStateMask = (1 << pb) - 1;
567 + UInt32 literalPosMask = (1 << lp) - 1;
568 + int len = 0;
569 + if (bufferSize < numProbs * sizeof(CProb))
570 + return LZMA_RESULT_NOT_ENOUGH_MEM;
571 + for (i = 0; i < numProbs; i++)
572 + p[i] = kBitModelTotal >> 1;
573 + RangeDecoderInit(&rd,
574 + #ifdef _LZMA_IN_CB
575 + inCallback
576 + #else
577 + inStream, inSize
578 + #endif
579 + );
580 +#endif
581 +
582 + *outSizeProcessed = 0;
583 + while(nowPos < outSize)
584 + {
585 + int posState = (int)(
586 + (nowPos
587 + #ifdef _LZMA_OUT_READ
588 + + globalPos
589 + #endif
590 + )
591 + & posStateMask);
592 + #ifdef _LZMA_IN_CB
593 + if (rd.Result != LZMA_RESULT_OK)
594 + return rd.Result;
595 + #endif
596 + if (rd.ExtraBytes != 0)
597 + return LZMA_RESULT_DATA_ERROR;
598 + if (RangeDecoderBitDecode(p + IsMatch + (state << kNumPosBitsMax) + posState, &rd) == 0)
599 + {
600 + CProb *probs = p + Literal + (LZMA_LIT_SIZE *
601 + (((
602 + (nowPos
603 + #ifdef _LZMA_OUT_READ
604 + + globalPos
605 + #endif
606 + )
607 + & literalPosMask) << lc) + (previousByte >> (8 - lc))));
608 +
609 + if (state < 4) state = 0;
610 + else if (state < 10) state -= 3;
611 + else state -= 6;
612 + if (previousIsMatch)
613 + {
614 + Byte matchByte;
615 + #ifdef _LZMA_OUT_READ
616 + UInt32 pos = dictionaryPos - rep0;
617 + if (pos >= dictionarySize)
618 + pos += dictionarySize;
619 + matchByte = dictionary[pos];
620 + #else
621 + matchByte = outStream[nowPos - rep0];
622 + #endif
623 + previousByte = LzmaLiteralDecodeMatch(probs, &rd, matchByte);
624 + previousIsMatch = 0;
625 + }
626 + else
627 + previousByte = LzmaLiteralDecode(probs, &rd);
628 + outStream[nowPos++] = previousByte;
629 + #ifdef _LZMA_OUT_READ
630 + dictionary[dictionaryPos] = previousByte;
631 + if (++dictionaryPos == dictionarySize)
632 + dictionaryPos = 0;
633 + #endif
634 + }
635 + else
636 + {
637 + previousIsMatch = 1;
638 + if (RangeDecoderBitDecode(p + IsRep + state, &rd) == 1)
639 + {
640 + if (RangeDecoderBitDecode(p + IsRepG0 + state, &rd) == 0)
641 + {
642 + if (RangeDecoderBitDecode(p + IsRep0Long + (state << kNumPosBitsMax) + posState, &rd) == 0)
643 + {
644 + #ifdef _LZMA_OUT_READ
645 + UInt32 pos;
646 + #endif
647 + if (
648 + (nowPos
649 + #ifdef _LZMA_OUT_READ
650 + + globalPos
651 + #endif
652 + )
653 + == 0)
654 + return LZMA_RESULT_DATA_ERROR;
655 + state = state < 7 ? 9 : 11;
656 + #ifdef _LZMA_OUT_READ
657 + pos = dictionaryPos - rep0;
658 + if (pos >= dictionarySize)
659 + pos += dictionarySize;
660 + previousByte = dictionary[pos];
661 + dictionary[dictionaryPos] = previousByte;
662 + if (++dictionaryPos == dictionarySize)
663 + dictionaryPos = 0;
664 + #else
665 + previousByte = outStream[nowPos - rep0];
666 + #endif
667 + outStream[nowPos++] = previousByte;
668 + continue;
669 + }
670 + }
671 + else
672 + {
673 + UInt32 distance;
674 + if(RangeDecoderBitDecode(p + IsRepG1 + state, &rd) == 0)
675 + distance = rep1;
676 + else
677 + {
678 + if(RangeDecoderBitDecode(p + IsRepG2 + state, &rd) == 0)
679 + distance = rep2;
680 + else
681 + {
682 + distance = rep3;
683 + rep3 = rep2;
684 + }
685 + rep2 = rep1;
686 + }
687 + rep1 = rep0;
688 + rep0 = distance;
689 + }
690 + len = LzmaLenDecode(p + RepLenCoder, &rd, posState);
691 + state = state < 7 ? 8 : 11;
692 + }
693 + else
694 + {
695 + int posSlot;
696 + rep3 = rep2;
697 + rep2 = rep1;
698 + rep1 = rep0;
699 + state = state < 7 ? 7 : 10;
700 + len = LzmaLenDecode(p + LenCoder, &rd, posState);
701 + posSlot = RangeDecoderBitTreeDecode(p + PosSlot +
702 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
703 + kNumPosSlotBits), kNumPosSlotBits, &rd);
704 + if (posSlot >= kStartPosModelIndex)
705 + {
706 + int numDirectBits = ((posSlot >> 1) - 1);
707 + rep0 = ((2 | ((UInt32)posSlot & 1)) << numDirectBits);
708 + if (posSlot < kEndPosModelIndex)
709 + {
710 + rep0 += RangeDecoderReverseBitTreeDecode(
711 + p + SpecPos + rep0 - posSlot - 1, numDirectBits, &rd);
712 + }
713 + else
714 + {
715 + rep0 += RangeDecoderDecodeDirectBits(&rd,
716 + numDirectBits - kNumAlignBits) << kNumAlignBits;
717 + rep0 += RangeDecoderReverseBitTreeDecode(p + Align, kNumAlignBits, &rd);
718 + }
719 + }
720 + else
721 + rep0 = posSlot;
722 + rep0++;
723 + }
724 + if (rep0 == (UInt32)(0))
725 + {
726 + /* it's for stream version */
727 + len = -1;
728 + break;
729 + }
730 + if (rep0 > nowPos
731 + #ifdef _LZMA_OUT_READ
732 + + globalPos
733 + #endif
734 + )
735 + {
736 + return LZMA_RESULT_DATA_ERROR;
737 + }
738 + len += kMatchMinLen;
739 + do
740 + {
741 + #ifdef _LZMA_OUT_READ
742 + UInt32 pos = dictionaryPos - rep0;
743 + if (pos >= dictionarySize)
744 + pos += dictionarySize;
745 + previousByte = dictionary[pos];
746 + dictionary[dictionaryPos] = previousByte;
747 + if (++dictionaryPos == dictionarySize)
748 + dictionaryPos = 0;
749 + #else
750 + previousByte = outStream[nowPos - rep0];
751 + #endif
752 + outStream[nowPos++] = previousByte;
753 + len--;
754 + }
755 + while(len > 0 && nowPos < outSize);
756 + }
757 + }
758 +
759 + #ifdef _LZMA_OUT_READ
760 + vs->RangeDecoder = rd;
761 + vs->DictionaryPos = dictionaryPos;
762 + vs->GlobalPos = globalPos + nowPos;
763 + vs->Reps[0] = rep0;
764 + vs->Reps[1] = rep1;
765 + vs->Reps[2] = rep2;
766 + vs->Reps[3] = rep3;
767 + vs->State = state;
768 + vs->PreviousIsMatch = previousIsMatch;
769 + vs->RemainLen = len;
770 + #endif
771 +
772 + *outSizeProcessed = nowPos;
773 + return LZMA_RESULT_OK;
774 +}
775 diff -Nur linux-2.6.16/fs/squashfs/LzmaDecode.h linux-2.6.16-owrt/fs/squashfs/LzmaDecode.h
776 --- linux-2.6.16/fs/squashfs/LzmaDecode.h 1970-01-01 01:00:00.000000000 +0100
777 +++ linux-2.6.16-owrt/fs/squashfs/LzmaDecode.h 2006-03-21 10:56:57.000000000 +0100
778 @@ -0,0 +1,100 @@
779 +/*
780 + LzmaDecode.h
781 + LZMA Decoder interface
782 +
783 + LZMA SDK 4.05 Copyright (c) 1999-2004 Igor Pavlov (2004-08-25)
784 + http://www.7-zip.org/
785 +
786 + LZMA SDK is licensed under two licenses:
787 + 1) GNU Lesser General Public License (GNU LGPL)
788 + 2) Common Public License (CPL)
789 + It means that you can select one of these two licenses and
790 + follow rules of that license.
791 +
792 + SPECIAL EXCEPTION:
793 + Igor Pavlov, as the author of this code, expressly permits you to
794 + statically or dynamically link your code (or bind by name) to the
795 + interfaces of this file without subjecting your linked code to the
796 + terms of the CPL or GNU LGPL. Any modifications or additions
797 + to this file, however, are subject to the LGPL or CPL terms.
798 +*/
799 +
800 +#ifndef __LZMADECODE_H
801 +#define __LZMADECODE_H
802 +
803 +/* #define _LZMA_IN_CB */
804 +/* Use callback for input data */
805 +
806 +/* #define _LZMA_OUT_READ */
807 +/* Use read function for output data */
808 +
809 +/* #define _LZMA_PROB32 */
810 +/* It can increase speed on some 32-bit CPUs,
811 + but memory usage will be doubled in that case */
812 +
813 +/* #define _LZMA_LOC_OPT */
814 +/* Enable local speed optimizations inside code */
815 +
816 +#ifndef UInt32
817 +#ifdef _LZMA_UINT32_IS_ULONG
818 +#define UInt32 unsigned long
819 +#else
820 +#define UInt32 unsigned int
821 +#endif
822 +#endif
823 +
824 +#ifdef _LZMA_PROB32
825 +#define CProb UInt32
826 +#else
827 +#define CProb unsigned short
828 +#endif
829 +
830 +#define LZMA_RESULT_OK 0
831 +#define LZMA_RESULT_DATA_ERROR 1
832 +#define LZMA_RESULT_NOT_ENOUGH_MEM 2
833 +
834 +#ifdef _LZMA_IN_CB
835 +typedef struct _ILzmaInCallback
836 +{
837 + int (*Read)(void *object, unsigned char **buffer, UInt32 *bufferSize);
838 +} ILzmaInCallback;
839 +#endif
840 +
841 +#define LZMA_BASE_SIZE 1846
842 +#define LZMA_LIT_SIZE 768
843 +
844 +/*
845 +bufferSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb)
846 +bufferSize += 100 in case of _LZMA_OUT_READ
847 +by default CProb is unsigned short,
848 +but if specify _LZMA_PROB_32, CProb will be UInt32(unsigned int)
849 +*/
850 +
851 +#ifdef _LZMA_OUT_READ
852 +int LzmaDecoderInit(
853 + unsigned char *buffer, UInt32 bufferSize,
854 + int lc, int lp, int pb,
855 + unsigned char *dictionary, UInt32 dictionarySize,
856 + #ifdef _LZMA_IN_CB
857 + ILzmaInCallback *inCallback
858 + #else
859 + unsigned char *inStream, UInt32 inSize
860 + #endif
861 +);
862 +#endif
863 +
864 +int LzmaDecode(
865 + unsigned char *buffer,
866 + #ifndef _LZMA_OUT_READ
867 + UInt32 bufferSize,
868 + int lc, int lp, int pb,
869 + #ifdef _LZMA_IN_CB
870 + ILzmaInCallback *inCallback,
871 + #else
872 + unsigned char *inStream, UInt32 inSize,
873 + #endif
874 + #endif
875 + unsigned char *outStream, UInt32 outSize,
876 + UInt32 *outSizeProcessed);
877 +
878 +#endif
879 diff -Nur linux-2.6.16/fs/squashfs/Makefile linux-2.6.16-owrt/fs/squashfs/Makefile
880 --- linux-2.6.16/fs/squashfs/Makefile 2006-03-21 10:55:59.000000000 +0100
881 +++ linux-2.6.16-owrt/fs/squashfs/Makefile 2006-03-21 10:57:08.000000000 +0100
882 @@ -5,3 +5,4 @@
883 obj-$(CONFIG_SQUASHFS) += squashfs.o
884 squashfs-y += inode.o
885 squashfs-y += squashfs2_0.o
886 +squashfs-y += LzmaDecode.o