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