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