1 Index: linux-2.6.22.19/include/linux/LzmaDecode.h
2 ===================================================================
4 +++ linux-2.6.22.19/include/linux/LzmaDecode.h
8 + LZMA Decoder interface
10 + LZMA SDK 4.05 Copyright (c) 1999-2004 Igor Pavlov (2004-08-25)
11 + http://www.7-zip.org/
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.
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.
27 +#ifndef __LZMADECODE_H
28 +#define __LZMADECODE_H
30 +/* #define _LZMA_IN_CB */
31 +/* Use callback for input data */
33 +/* #define _LZMA_OUT_READ */
34 +/* Use read function for output data */
36 +/* #define _LZMA_PROB32 */
37 +/* It can increase speed on some 32-bit CPUs,
38 + but memory usage will be doubled in that case */
40 +/* #define _LZMA_LOC_OPT */
41 +/* Enable local speed optimizations inside code */
44 +#ifdef _LZMA_UINT32_IS_ULONG
45 +#define UInt32 unsigned long
47 +#define UInt32 unsigned int
54 +#define CProb unsigned short
57 +#define LZMA_RESULT_OK 0
58 +#define LZMA_RESULT_DATA_ERROR 1
59 +#define LZMA_RESULT_NOT_ENOUGH_MEM 2
62 +typedef struct _ILzmaInCallback
64 + int (*Read)(void *object, unsigned char **buffer, UInt32 *bufferSize);
68 +#define LZMA_BASE_SIZE 1846
69 +#define LZMA_LIT_SIZE 768
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)
78 +#ifdef _LZMA_OUT_READ
80 + unsigned char *buffer, UInt32 bufferSize,
81 + int lc, int lp, int pb,
82 + unsigned char *dictionary, UInt32 dictionarySize,
84 + ILzmaInCallback *inCallback
86 + unsigned char *inStream, UInt32 inSize
92 + unsigned char *buffer,
93 + #ifndef _LZMA_OUT_READ
95 + int lc, int lp, int pb,
97 + ILzmaInCallback *inCallback,
99 + unsigned char *inStream, UInt32 inSize,
102 + unsigned char *outStream, UInt32 outSize,
103 + UInt32 *outSizeProcessed);
106 Index: linux-2.6.22.19/lib/LzmaDecode.c
107 ===================================================================
109 +++ linux-2.6.22.19/lib/LzmaDecode.c
115 + LZMA SDK 4.05 Copyright (c) 1999-2004 Igor Pavlov (2004-08-25)
116 + http://www.7-zip.org/
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.
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.
132 +#include <linux/LzmaDecode.h>
135 +#define Byte unsigned char
138 +#define kNumTopBits 24
139 +#define kTopValue ((UInt32)1 << kNumTopBits)
141 +#define kNumBitModelTotalBits 11
142 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
143 +#define kNumMoveBits 5
145 +typedef struct _CRangeDecoder
152 + ILzmaInCallback *InCallback;
158 +Byte RangeDecoderReadByte(CRangeDecoder *rd)
160 + if (rd->Buffer == rd->BufferLim)
164 + rd->Result = rd->InCallback->Read(rd->InCallback, &rd->Buffer, &size);
165 + rd->BufferLim = rd->Buffer + size;
169 + rd->ExtraBytes = 1;
173 + return (*rd->Buffer++);
176 +/* #define ReadByte (*rd->Buffer++) */
177 +#define ReadByte (RangeDecoderReadByte(rd))
179 +void RangeDecoderInit(CRangeDecoder *rd,
181 + ILzmaInCallback *inCallback
183 + Byte *stream, UInt32 bufferSize
189 + rd->InCallback = inCallback;
190 + rd->Buffer = rd->BufferLim = 0;
192 + rd->Buffer = stream;
193 + rd->BufferLim = stream + bufferSize;
195 + rd->ExtraBytes = 0;
197 + rd->Range = (0xFFFFFFFF);
198 + for(i = 0; i < 5; i++)
199 + rd->Code = (rd->Code << 8) | ReadByte;
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; }
206 +UInt32 RangeDecoderDecodeDirectBits(CRangeDecoder *rd, int numTotalBits)
211 + for (i = numTotalBits; i > 0; i--)
223 + t = (code - range) >> 31;
225 + code -= range & (t - 1);
226 + result = (result + result) | (1 - t);
234 +int RangeDecoderBitDecode(CProb *prob, CRangeDecoder *rd)
236 + UInt32 bound = (rd->Range >> kNumBitModelTotalBits) * *prob;
237 + if (rd->Code < bound)
240 + *prob += (kBitModelTotal - *prob) >> kNumMoveBits;
241 + if (rd->Range < kTopValue)
243 + rd->Code = (rd->Code << 8) | ReadByte;
250 + rd->Range -= bound;
252 + *prob -= (*prob) >> kNumMoveBits;
253 + if (rd->Range < kTopValue)
255 + rd->Code = (rd->Code << 8) | ReadByte;
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; } \
267 + { A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
270 +#define RC_GET_BIT(prob, mi) RC_GET_BIT2(prob, mi, ; , ;)
272 +int RangeDecoderBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
276 + #ifdef _LZMA_LOC_OPT
279 + for(i = numLevels; i > 0; i--)
281 + #ifdef _LZMA_LOC_OPT
282 + CProb *prob = probs + mi;
283 + RC_GET_BIT(prob, mi)
285 + mi = (mi + mi) + RangeDecoderBitDecode(probs + mi, rd);
288 + #ifdef _LZMA_LOC_OPT
291 + return mi - (1 << numLevels);
294 +int RangeDecoderReverseBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
299 + #ifdef _LZMA_LOC_OPT
302 + for(i = 0; i < numLevels; i++)
304 + #ifdef _LZMA_LOC_OPT
305 + CProb *prob = probs + mi;
306 + RC_GET_BIT2(prob, mi, ; , symbol |= (1 << i))
308 + int bit = RangeDecoderBitDecode(probs + mi, rd);
309 + mi = mi + mi + bit;
310 + symbol |= (bit << i);
313 + #ifdef _LZMA_LOC_OPT
319 +Byte LzmaLiteralDecode(CProb *probs, CRangeDecoder *rd)
322 + #ifdef _LZMA_LOC_OPT
327 + #ifdef _LZMA_LOC_OPT
328 + CProb *prob = probs + symbol;
329 + RC_GET_BIT(prob, symbol)
331 + symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
334 + while (symbol < 0x100);
335 + #ifdef _LZMA_LOC_OPT
341 +Byte LzmaLiteralDecodeMatch(CProb *probs, CRangeDecoder *rd, Byte matchByte)
344 + #ifdef _LZMA_LOC_OPT
350 + int matchBit = (matchByte >> 7) & 1;
352 + #ifdef _LZMA_LOC_OPT
354 + CProb *prob = probs + ((1 + matchBit) << 8) + symbol;
355 + RC_GET_BIT2(prob, symbol, bit = 0, bit = 1)
358 + bit = RangeDecoderBitDecode(probs + ((1 + matchBit) << 8) + symbol, rd);
359 + symbol = (symbol << 1) | bit;
361 + if (matchBit != bit)
363 + while (symbol < 0x100)
365 + #ifdef _LZMA_LOC_OPT
366 + CProb *prob = probs + symbol;
367 + RC_GET_BIT(prob, symbol)
369 + symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
375 + while (symbol < 0x100);
376 + #ifdef _LZMA_LOC_OPT
382 +#define kNumPosBitsMax 4
383 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
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)
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)
399 +int LzmaLenDecode(CProb *p, CRangeDecoder *rd, int posState)
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);
411 +#define kNumStates 12
413 +#define kStartPosModelIndex 4
414 +#define kEndPosModelIndex 14
415 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
417 +#define kNumPosSlotBits 6
418 +#define kNumLenToPosStates 4
420 +#define kNumAlignBits 4
421 +#define kAlignTableSize (1 << kNumAlignBits)
423 +#define kMatchMinLen 2
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)
438 +#if Literal != LZMA_BASE_SIZE
442 +#ifdef _LZMA_OUT_READ
444 +typedef struct _LzmaVarState
446 + CRangeDecoder RangeDecoder;
448 + UInt32 DictionarySize;
449 + UInt32 DictionaryPos;
456 + int PreviousIsMatch;
460 +int LzmaDecoderInit(
461 + unsigned char *buffer, UInt32 bufferSize,
462 + int lc, int lp, int pb,
463 + unsigned char *dictionary, UInt32 dictionarySize,
465 + ILzmaInCallback *inCallback
467 + unsigned char *inStream, UInt32 inSize
471 + LzmaVarState *vs = (LzmaVarState *)buffer;
472 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
473 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
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;
481 + vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
486 + vs->PreviousIsMatch = 0;
488 + dictionary[dictionarySize - 1] = 0;
489 + for (i = 0; i < numProbs; i++)
490 + p[i] = kBitModelTotal >> 1;
491 + RangeDecoderInit(&vs->RangeDecoder,
498 + return LZMA_RESULT_OK;
501 +int LzmaDecode(unsigned char *buffer,
502 + unsigned char *outStream, UInt32 outSize,
503 + UInt32 *outSizeProcessed)
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;
511 + UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
513 + UInt32 posStateMask = (1 << (vs->pb)) - 1;
514 + UInt32 literalPosMask = (1 << (vs->lp)) - 1;
516 + int len = vs->RemainLen;
517 + UInt32 globalPos = vs->GlobalPos;
519 + Byte *dictionary = vs->Dictionary;
520 + UInt32 dictionarySize = vs->DictionarySize;
521 + UInt32 dictionaryPos = vs->DictionaryPos;
525 + *outSizeProcessed = 0;
526 + return LZMA_RESULT_OK;
529 + while(len > 0 && nowPos < outSize)
531 + UInt32 pos = dictionaryPos - rep0;
532 + if (pos >= dictionarySize)
533 + pos += dictionarySize;
534 + outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
535 + if (++dictionaryPos == dictionarySize)
539 + if (dictionaryPos == 0)
540 + previousByte = dictionary[dictionarySize - 1];
542 + previousByte = dictionary[dictionaryPos - 1];
546 + Byte *buffer, UInt32 bufferSize,
547 + int lc, int lp, int pb,
549 + ILzmaInCallback *inCallback,
551 + unsigned char *inStream, UInt32 inSize,
553 + unsigned char *outStream, UInt32 outSize,
554 + UInt32 *outSizeProcessed)
556 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
557 + CProb *p = (CProb *)buffer;
561 + int previousIsMatch = 0;
562 + Byte previousByte = 0;
563 + UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
565 + UInt32 posStateMask = (1 << pb) - 1;
566 + UInt32 literalPosMask = (1 << lp) - 1;
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,
581 + *outSizeProcessed = 0;
582 + while(nowPos < outSize)
584 + int posState = (int)(
586 + #ifdef _LZMA_OUT_READ
592 + if (rd.Result != LZMA_RESULT_OK)
595 + if (rd.ExtraBytes != 0)
596 + return LZMA_RESULT_DATA_ERROR;
597 + if (RangeDecoderBitDecode(p + IsMatch + (state << kNumPosBitsMax) + posState, &rd) == 0)
599 + CProb *probs = p + Literal + (LZMA_LIT_SIZE *
602 + #ifdef _LZMA_OUT_READ
606 + & literalPosMask) << lc) + (previousByte >> (8 - lc))));
608 + if (state < 4) state = 0;
609 + else if (state < 10) state -= 3;
611 + if (previousIsMatch)
614 + #ifdef _LZMA_OUT_READ
615 + UInt32 pos = dictionaryPos - rep0;
616 + if (pos >= dictionarySize)
617 + pos += dictionarySize;
618 + matchByte = dictionary[pos];
620 + matchByte = outStream[nowPos - rep0];
622 + previousByte = LzmaLiteralDecodeMatch(probs, &rd, matchByte);
623 + previousIsMatch = 0;
626 + previousByte = LzmaLiteralDecode(probs, &rd);
627 + outStream[nowPos++] = previousByte;
628 + #ifdef _LZMA_OUT_READ
629 + dictionary[dictionaryPos] = previousByte;
630 + if (++dictionaryPos == dictionarySize)
636 + previousIsMatch = 1;
637 + if (RangeDecoderBitDecode(p + IsRep + state, &rd) == 1)
639 + if (RangeDecoderBitDecode(p + IsRepG0 + state, &rd) == 0)
641 + if (RangeDecoderBitDecode(p + IsRep0Long + (state << kNumPosBitsMax) + posState, &rd) == 0)
643 + #ifdef _LZMA_OUT_READ
648 + #ifdef _LZMA_OUT_READ
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)
664 + previousByte = outStream[nowPos - rep0];
666 + outStream[nowPos++] = previousByte;
673 + if(RangeDecoderBitDecode(p + IsRepG1 + state, &rd) == 0)
677 + if(RangeDecoderBitDecode(p + IsRepG2 + state, &rd) == 0)
689 + len = LzmaLenDecode(p + RepLenCoder, &rd, posState);
690 + state = state < 7 ? 8 : 11;
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)
705 + int numDirectBits = ((posSlot >> 1) - 1);
706 + rep0 = ((2 | ((UInt32)posSlot & 1)) << numDirectBits);
707 + if (posSlot < kEndPosModelIndex)
709 + rep0 += RangeDecoderReverseBitTreeDecode(
710 + p + SpecPos + rep0 - posSlot - 1, numDirectBits, &rd);
714 + rep0 += RangeDecoderDecodeDirectBits(&rd,
715 + numDirectBits - kNumAlignBits) << kNumAlignBits;
716 + rep0 += RangeDecoderReverseBitTreeDecode(p + Align, kNumAlignBits, &rd);
723 + if (rep0 == (UInt32)(0))
725 + /* it's for stream version */
730 + #ifdef _LZMA_OUT_READ
735 + return LZMA_RESULT_DATA_ERROR;
737 + len += kMatchMinLen;
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)
749 + previousByte = outStream[nowPos - rep0];
751 + outStream[nowPos++] = previousByte;
754 + while(len > 0 && nowPos < outSize);
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;
767 + vs->PreviousIsMatch = previousIsMatch;
768 + vs->RemainLen = len;
771 + *outSizeProcessed = nowPos;
772 + return LZMA_RESULT_OK;
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
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
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
790 obj-$(CONFIG_SWIOTLB) += swiotlb.o
792 obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
794 lib-$(CONFIG_GENERIC_BUG) += bug.o