[kernel] refresh generic 2.6.22 patches
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.22 / 002-lzma_decompress.patch
1 Index: linux-2.6.22.19/include/linux/LzmaDecode.h
2 ===================================================================
3 --- /dev/null
4 +++ linux-2.6.22.19/include/linux/LzmaDecode.h
5 @@ -0,0 +1,100 @@
6 +/*
7 +  LzmaDecode.h
8 +  LZMA Decoder interface
9 +
10 +  LZMA SDK 4.05 Copyright (c) 1999-2004 Igor Pavlov (2004-08-25)
11 +  http://www.7-zip.org/
12 +
13 +  LZMA SDK is licensed under two licenses:
14 +  1) GNU Lesser General Public License (GNU LGPL)
15 +  2) Common Public License (CPL)
16 +  It means that you can select one of these two licenses and
17 +  follow rules of that license.
18 +
19 +  SPECIAL EXCEPTION:
20 +  Igor Pavlov, as the author of this code, expressly permits you to
21 +  statically or dynamically link your code (or bind by name) to the
22 +  interfaces of this file without subjecting your linked code to the
23 +  terms of the CPL or GNU LGPL. Any modifications or additions
24 +  to this file, however, are subject to the LGPL or CPL terms.
25 +*/
26 +
27 +#ifndef __LZMADECODE_H
28 +#define __LZMADECODE_H
29 +
30 +/* #define _LZMA_IN_CB */
31 +/* Use callback for input data */
32 +
33 +/* #define _LZMA_OUT_READ */
34 +/* Use read function for output data */
35 +
36 +/* #define _LZMA_PROB32 */
37 +/* It can increase speed on some 32-bit CPUs,
38 +   but memory usage will be doubled in that case */
39 +
40 +/* #define _LZMA_LOC_OPT */
41 +/* Enable local speed optimizations inside code */
42 +
43 +#ifndef UInt32
44 +#ifdef _LZMA_UINT32_IS_ULONG
45 +#define UInt32 unsigned long
46 +#else
47 +#define UInt32 unsigned int
48 +#endif
49 +#endif
50 +
51 +#ifdef _LZMA_PROB32
52 +#define CProb UInt32
53 +#else
54 +#define CProb unsigned short
55 +#endif
56 +
57 +#define LZMA_RESULT_OK 0
58 +#define LZMA_RESULT_DATA_ERROR 1
59 +#define LZMA_RESULT_NOT_ENOUGH_MEM 2
60 +
61 +#ifdef _LZMA_IN_CB
62 +typedef struct _ILzmaInCallback
63 +{
64 +  int (*Read)(void *object, unsigned char **buffer, UInt32 *bufferSize);
65 +} ILzmaInCallback;
66 +#endif
67 +
68 +#define LZMA_BASE_SIZE 1846
69 +#define LZMA_LIT_SIZE 768
70 +
71 +/*
72 +bufferSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb)
73 +bufferSize += 100 in case of _LZMA_OUT_READ
74 +by default CProb is unsigned short,
75 +but if specify _LZMA_PROB_32, CProb will be UInt32(unsigned int)
76 +*/
77 +
78 +#ifdef _LZMA_OUT_READ
79 +int LzmaDecoderInit(
80 +    unsigned char *buffer, UInt32 bufferSize,
81 +    int lc, int lp, int pb,
82 +    unsigned char *dictionary, UInt32 dictionarySize,
83 +  #ifdef _LZMA_IN_CB
84 +    ILzmaInCallback *inCallback
85 +  #else
86 +    unsigned char *inStream, UInt32 inSize
87 +  #endif
88 +);
89 +#endif
90 +
91 +int LzmaDecode(
92 +    unsigned char *buffer,
93 +  #ifndef _LZMA_OUT_READ
94 +    UInt32 bufferSize,
95 +    int lc, int lp, int pb,
96 +  #ifdef _LZMA_IN_CB
97 +    ILzmaInCallback *inCallback,
98 +  #else
99 +    unsigned char *inStream, UInt32 inSize,
100 +  #endif
101 +  #endif
102 +    unsigned char *outStream, UInt32 outSize,
103 +    UInt32 *outSizeProcessed);
104 +
105 +#endif
106 Index: linux-2.6.22.19/lib/LzmaDecode.c
107 ===================================================================
108 --- /dev/null
109 +++ linux-2.6.22.19/lib/LzmaDecode.c
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 <linux/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 Index: linux-2.6.22.19/lib/Makefile
775 ===================================================================
776 --- linux-2.6.22.19.orig/lib/Makefile
777 +++ linux-2.6.22.19/lib/Makefile
778 @@ -13,7 +13,7 @@ lib-$(CONFIG_SMP) += cpumask.o
779  lib-y  += kobject.o kref.o kobject_uevent.o klist.o
780  
781  obj-y += div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
782 -        bust_spinlocks.o hexdump.o
783 +        bust_spinlocks.o hexdump.o LzmaDecode.o
784  
785  ifeq ($(CONFIG_DEBUG_KOBJECT),y)
786  CFLAGS_kobject.o += -DDEBUG
787 @@ -58,6 +58,7 @@ obj-$(CONFIG_SMP) += percpu_counter.o
788  obj-$(CONFIG_AUDIT_GENERIC) += audit.o
789  
790  obj-$(CONFIG_SWIOTLB) += swiotlb.o
791 +
792  obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
793  
794  lib-$(CONFIG_GENERIC_BUG) += bug.o