1 diff -urN linux-2.6.21.1.old/include/linux/LzmaDecode.h linux-2.6.21.1.dev/include/linux/LzmaDecode.h
2 --- linux-2.6.21.1.old/include/linux/LzmaDecode.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.21.1.dev/include/linux/LzmaDecode.h 2007-05-26 19:03:45.705682584 +0200
7 + LZMA Decoder interface
9 + LZMA SDK 4.05 Copyright (c) 1999-2004 Igor Pavlov (2004-08-25)
10 + http://www.7-zip.org/
12 + LZMA SDK is licensed under two licenses:
13 + 1) GNU Lesser General Public License (GNU LGPL)
14 + 2) Common Public License (CPL)
15 + It means that you can select one of these two licenses and
16 + follow rules of that license.
19 + Igor Pavlov, as the author of this code, expressly permits you to
20 + statically or dynamically link your code (or bind by name) to the
21 + interfaces of this file without subjecting your linked code to the
22 + terms of the CPL or GNU LGPL. Any modifications or additions
23 + to this file, however, are subject to the LGPL or CPL terms.
26 +#ifndef __LZMADECODE_H
27 +#define __LZMADECODE_H
29 +/* #define _LZMA_IN_CB */
30 +/* Use callback for input data */
32 +/* #define _LZMA_OUT_READ */
33 +/* Use read function for output data */
35 +/* #define _LZMA_PROB32 */
36 +/* It can increase speed on some 32-bit CPUs,
37 + but memory usage will be doubled in that case */
39 +/* #define _LZMA_LOC_OPT */
40 +/* Enable local speed optimizations inside code */
43 +#ifdef _LZMA_UINT32_IS_ULONG
44 +#define UInt32 unsigned long
46 +#define UInt32 unsigned int
53 +#define CProb unsigned short
56 +#define LZMA_RESULT_OK 0
57 +#define LZMA_RESULT_DATA_ERROR 1
58 +#define LZMA_RESULT_NOT_ENOUGH_MEM 2
61 +typedef struct _ILzmaInCallback
63 + int (*Read)(void *object, unsigned char **buffer, UInt32 *bufferSize);
67 +#define LZMA_BASE_SIZE 1846
68 +#define LZMA_LIT_SIZE 768
71 +bufferSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb)
72 +bufferSize += 100 in case of _LZMA_OUT_READ
73 +by default CProb is unsigned short,
74 +but if specify _LZMA_PROB_32, CProb will be UInt32(unsigned int)
77 +#ifdef _LZMA_OUT_READ
79 + unsigned char *buffer, UInt32 bufferSize,
80 + int lc, int lp, int pb,
81 + unsigned char *dictionary, UInt32 dictionarySize,
83 + ILzmaInCallback *inCallback
85 + unsigned char *inStream, UInt32 inSize
91 + unsigned char *buffer,
92 + #ifndef _LZMA_OUT_READ
94 + int lc, int lp, int pb,
96 + ILzmaInCallback *inCallback,
98 + unsigned char *inStream, UInt32 inSize,
101 + unsigned char *outStream, UInt32 outSize,
102 + UInt32 *outSizeProcessed);
105 diff -urN linux-2.6.21.1.old/lib/LzmaDecode.c linux-2.6.21.1.dev/lib/LzmaDecode.c
106 --- linux-2.6.21.1.old/lib/LzmaDecode.c 1970-01-01 01:00:00.000000000 +0100
107 +++ linux-2.6.21.1.dev/lib/LzmaDecode.c 2007-05-26 19:03:45.706682432 +0200
113 + LZMA SDK 4.05 Copyright (c) 1999-2004 Igor Pavlov (2004-08-25)
114 + http://www.7-zip.org/
116 + LZMA SDK is licensed under two licenses:
117 + 1) GNU Lesser General Public License (GNU LGPL)
118 + 2) Common Public License (CPL)
119 + It means that you can select one of these two licenses and
120 + follow rules of that license.
123 + Igor Pavlov, as the author of this code, expressly permits you to
124 + statically or dynamically link your code (or bind by name) to the
125 + interfaces of this file without subjecting your linked code to the
126 + terms of the CPL or GNU LGPL. Any modifications or additions
127 + to this file, however, are subject to the LGPL or CPL terms.
130 +#include <linux/LzmaDecode.h>
133 +#define Byte unsigned char
136 +#define kNumTopBits 24
137 +#define kTopValue ((UInt32)1 << kNumTopBits)
139 +#define kNumBitModelTotalBits 11
140 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
141 +#define kNumMoveBits 5
143 +typedef struct _CRangeDecoder
150 + ILzmaInCallback *InCallback;
156 +Byte RangeDecoderReadByte(CRangeDecoder *rd)
158 + if (rd->Buffer == rd->BufferLim)
162 + rd->Result = rd->InCallback->Read(rd->InCallback, &rd->Buffer, &size);
163 + rd->BufferLim = rd->Buffer + size;
167 + rd->ExtraBytes = 1;
171 + return (*rd->Buffer++);
174 +/* #define ReadByte (*rd->Buffer++) */
175 +#define ReadByte (RangeDecoderReadByte(rd))
177 +void RangeDecoderInit(CRangeDecoder *rd,
179 + ILzmaInCallback *inCallback
181 + Byte *stream, UInt32 bufferSize
187 + rd->InCallback = inCallback;
188 + rd->Buffer = rd->BufferLim = 0;
190 + rd->Buffer = stream;
191 + rd->BufferLim = stream + bufferSize;
193 + rd->ExtraBytes = 0;
195 + rd->Range = (0xFFFFFFFF);
196 + for(i = 0; i < 5; i++)
197 + rd->Code = (rd->Code << 8) | ReadByte;
200 +#define RC_INIT_VAR UInt32 range = rd->Range; UInt32 code = rd->Code;
201 +#define RC_FLUSH_VAR rd->Range = range; rd->Code = code;
202 +#define RC_NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | ReadByte; }
204 +UInt32 RangeDecoderDecodeDirectBits(CRangeDecoder *rd, int numTotalBits)
209 + for (i = numTotalBits; i > 0; i--)
221 + t = (code - range) >> 31;
223 + code -= range & (t - 1);
224 + result = (result + result) | (1 - t);
232 +int RangeDecoderBitDecode(CProb *prob, CRangeDecoder *rd)
234 + UInt32 bound = (rd->Range >> kNumBitModelTotalBits) * *prob;
235 + if (rd->Code < bound)
238 + *prob += (kBitModelTotal - *prob) >> kNumMoveBits;
239 + if (rd->Range < kTopValue)
241 + rd->Code = (rd->Code << 8) | ReadByte;
248 + rd->Range -= bound;
250 + *prob -= (*prob) >> kNumMoveBits;
251 + if (rd->Range < kTopValue)
253 + rd->Code = (rd->Code << 8) | ReadByte;
260 +#define RC_GET_BIT2(prob, mi, A0, A1) \
261 + UInt32 bound = (range >> kNumBitModelTotalBits) * *prob; \
262 + if (code < bound) \
263 + { A0; range = bound; *prob += (kBitModelTotal - *prob) >> kNumMoveBits; mi <<= 1; } \
265 + { A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
268 +#define RC_GET_BIT(prob, mi) RC_GET_BIT2(prob, mi, ; , ;)
270 +int RangeDecoderBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
274 + #ifdef _LZMA_LOC_OPT
277 + for(i = numLevels; i > 0; i--)
279 + #ifdef _LZMA_LOC_OPT
280 + CProb *prob = probs + mi;
281 + RC_GET_BIT(prob, mi)
283 + mi = (mi + mi) + RangeDecoderBitDecode(probs + mi, rd);
286 + #ifdef _LZMA_LOC_OPT
289 + return mi - (1 << numLevels);
292 +int RangeDecoderReverseBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
297 + #ifdef _LZMA_LOC_OPT
300 + for(i = 0; i < numLevels; i++)
302 + #ifdef _LZMA_LOC_OPT
303 + CProb *prob = probs + mi;
304 + RC_GET_BIT2(prob, mi, ; , symbol |= (1 << i))
306 + int bit = RangeDecoderBitDecode(probs + mi, rd);
307 + mi = mi + mi + bit;
308 + symbol |= (bit << i);
311 + #ifdef _LZMA_LOC_OPT
317 +Byte LzmaLiteralDecode(CProb *probs, CRangeDecoder *rd)
320 + #ifdef _LZMA_LOC_OPT
325 + #ifdef _LZMA_LOC_OPT
326 + CProb *prob = probs + symbol;
327 + RC_GET_BIT(prob, symbol)
329 + symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
332 + while (symbol < 0x100);
333 + #ifdef _LZMA_LOC_OPT
339 +Byte LzmaLiteralDecodeMatch(CProb *probs, CRangeDecoder *rd, Byte matchByte)
342 + #ifdef _LZMA_LOC_OPT
348 + int matchBit = (matchByte >> 7) & 1;
350 + #ifdef _LZMA_LOC_OPT
352 + CProb *prob = probs + ((1 + matchBit) << 8) + symbol;
353 + RC_GET_BIT2(prob, symbol, bit = 0, bit = 1)
356 + bit = RangeDecoderBitDecode(probs + ((1 + matchBit) << 8) + symbol, rd);
357 + symbol = (symbol << 1) | bit;
359 + if (matchBit != bit)
361 + while (symbol < 0x100)
363 + #ifdef _LZMA_LOC_OPT
364 + CProb *prob = probs + symbol;
365 + RC_GET_BIT(prob, symbol)
367 + symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
373 + while (symbol < 0x100);
374 + #ifdef _LZMA_LOC_OPT
380 +#define kNumPosBitsMax 4
381 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
383 +#define kLenNumLowBits 3
384 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
385 +#define kLenNumMidBits 3
386 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
387 +#define kLenNumHighBits 8
388 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
391 +#define LenChoice2 (LenChoice + 1)
392 +#define LenLow (LenChoice2 + 1)
393 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
394 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
395 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
397 +int LzmaLenDecode(CProb *p, CRangeDecoder *rd, int posState)
399 + if(RangeDecoderBitDecode(p + LenChoice, rd) == 0)
400 + return RangeDecoderBitTreeDecode(p + LenLow +
401 + (posState << kLenNumLowBits), kLenNumLowBits, rd);
402 + if(RangeDecoderBitDecode(p + LenChoice2, rd) == 0)
403 + return kLenNumLowSymbols + RangeDecoderBitTreeDecode(p + LenMid +
404 + (posState << kLenNumMidBits), kLenNumMidBits, rd);
405 + return kLenNumLowSymbols + kLenNumMidSymbols +
406 + RangeDecoderBitTreeDecode(p + LenHigh, kLenNumHighBits, rd);
409 +#define kNumStates 12
411 +#define kStartPosModelIndex 4
412 +#define kEndPosModelIndex 14
413 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
415 +#define kNumPosSlotBits 6
416 +#define kNumLenToPosStates 4
418 +#define kNumAlignBits 4
419 +#define kAlignTableSize (1 << kNumAlignBits)
421 +#define kMatchMinLen 2
424 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
425 +#define IsRepG0 (IsRep + kNumStates)
426 +#define IsRepG1 (IsRepG0 + kNumStates)
427 +#define IsRepG2 (IsRepG1 + kNumStates)
428 +#define IsRep0Long (IsRepG2 + kNumStates)
429 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
430 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
431 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
432 +#define LenCoder (Align + kAlignTableSize)
433 +#define RepLenCoder (LenCoder + kNumLenProbs)
434 +#define Literal (RepLenCoder + kNumLenProbs)
436 +#if Literal != LZMA_BASE_SIZE
440 +#ifdef _LZMA_OUT_READ
442 +typedef struct _LzmaVarState
444 + CRangeDecoder RangeDecoder;
446 + UInt32 DictionarySize;
447 + UInt32 DictionaryPos;
454 + int PreviousIsMatch;
458 +int LzmaDecoderInit(
459 + unsigned char *buffer, UInt32 bufferSize,
460 + int lc, int lp, int pb,
461 + unsigned char *dictionary, UInt32 dictionarySize,
463 + ILzmaInCallback *inCallback
465 + unsigned char *inStream, UInt32 inSize
469 + LzmaVarState *vs = (LzmaVarState *)buffer;
470 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
471 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
473 + if (bufferSize < numProbs * sizeof(CProb) + sizeof(LzmaVarState))
474 + return LZMA_RESULT_NOT_ENOUGH_MEM;
475 + vs->Dictionary = dictionary;
476 + vs->DictionarySize = dictionarySize;
477 + vs->DictionaryPos = 0;
479 + vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
484 + vs->PreviousIsMatch = 0;
486 + dictionary[dictionarySize - 1] = 0;
487 + for (i = 0; i < numProbs; i++)
488 + p[i] = kBitModelTotal >> 1;
489 + RangeDecoderInit(&vs->RangeDecoder,
496 + return LZMA_RESULT_OK;
499 +int LzmaDecode(unsigned char *buffer,
500 + unsigned char *outStream, UInt32 outSize,
501 + UInt32 *outSizeProcessed)
503 + LzmaVarState *vs = (LzmaVarState *)buffer;
504 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
505 + CRangeDecoder rd = vs->RangeDecoder;
506 + int state = vs->State;
507 + int previousIsMatch = vs->PreviousIsMatch;
509 + UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
511 + UInt32 posStateMask = (1 << (vs->pb)) - 1;
512 + UInt32 literalPosMask = (1 << (vs->lp)) - 1;
514 + int len = vs->RemainLen;
515 + UInt32 globalPos = vs->GlobalPos;
517 + Byte *dictionary = vs->Dictionary;
518 + UInt32 dictionarySize = vs->DictionarySize;
519 + UInt32 dictionaryPos = vs->DictionaryPos;
523 + *outSizeProcessed = 0;
524 + return LZMA_RESULT_OK;
527 + while(len > 0 && nowPos < outSize)
529 + UInt32 pos = dictionaryPos - rep0;
530 + if (pos >= dictionarySize)
531 + pos += dictionarySize;
532 + outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
533 + if (++dictionaryPos == dictionarySize)
537 + if (dictionaryPos == 0)
538 + previousByte = dictionary[dictionarySize - 1];
540 + previousByte = dictionary[dictionaryPos - 1];
544 + Byte *buffer, UInt32 bufferSize,
545 + int lc, int lp, int pb,
547 + ILzmaInCallback *inCallback,
549 + unsigned char *inStream, UInt32 inSize,
551 + unsigned char *outStream, UInt32 outSize,
552 + UInt32 *outSizeProcessed)
554 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
555 + CProb *p = (CProb *)buffer;
559 + int previousIsMatch = 0;
560 + Byte previousByte = 0;
561 + UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
563 + UInt32 posStateMask = (1 << pb) - 1;
564 + UInt32 literalPosMask = (1 << lp) - 1;
566 + if (bufferSize < numProbs * sizeof(CProb))
567 + return LZMA_RESULT_NOT_ENOUGH_MEM;
568 + for (i = 0; i < numProbs; i++)
569 + p[i] = kBitModelTotal >> 1;
570 + RangeDecoderInit(&rd,
579 + *outSizeProcessed = 0;
580 + while(nowPos < outSize)
582 + int posState = (int)(
584 + #ifdef _LZMA_OUT_READ
590 + if (rd.Result != LZMA_RESULT_OK)
593 + if (rd.ExtraBytes != 0)
594 + return LZMA_RESULT_DATA_ERROR;
595 + if (RangeDecoderBitDecode(p + IsMatch + (state << kNumPosBitsMax) + posState, &rd) == 0)
597 + CProb *probs = p + Literal + (LZMA_LIT_SIZE *
600 + #ifdef _LZMA_OUT_READ
604 + & literalPosMask) << lc) + (previousByte >> (8 - lc))));
606 + if (state < 4) state = 0;
607 + else if (state < 10) state -= 3;
609 + if (previousIsMatch)
612 + #ifdef _LZMA_OUT_READ
613 + UInt32 pos = dictionaryPos - rep0;
614 + if (pos >= dictionarySize)
615 + pos += dictionarySize;
616 + matchByte = dictionary[pos];
618 + matchByte = outStream[nowPos - rep0];
620 + previousByte = LzmaLiteralDecodeMatch(probs, &rd, matchByte);
621 + previousIsMatch = 0;
624 + previousByte = LzmaLiteralDecode(probs, &rd);
625 + outStream[nowPos++] = previousByte;
626 + #ifdef _LZMA_OUT_READ
627 + dictionary[dictionaryPos] = previousByte;
628 + if (++dictionaryPos == dictionarySize)
634 + previousIsMatch = 1;
635 + if (RangeDecoderBitDecode(p + IsRep + state, &rd) == 1)
637 + if (RangeDecoderBitDecode(p + IsRepG0 + state, &rd) == 0)
639 + if (RangeDecoderBitDecode(p + IsRep0Long + (state << kNumPosBitsMax) + posState, &rd) == 0)
641 + #ifdef _LZMA_OUT_READ
646 + #ifdef _LZMA_OUT_READ
651 + return LZMA_RESULT_DATA_ERROR;
652 + state = state < 7 ? 9 : 11;
653 + #ifdef _LZMA_OUT_READ
654 + pos = dictionaryPos - rep0;
655 + if (pos >= dictionarySize)
656 + pos += dictionarySize;
657 + previousByte = dictionary[pos];
658 + dictionary[dictionaryPos] = previousByte;
659 + if (++dictionaryPos == dictionarySize)
662 + previousByte = outStream[nowPos - rep0];
664 + outStream[nowPos++] = previousByte;
671 + if(RangeDecoderBitDecode(p + IsRepG1 + state, &rd) == 0)
675 + if(RangeDecoderBitDecode(p + IsRepG2 + state, &rd) == 0)
687 + len = LzmaLenDecode(p + RepLenCoder, &rd, posState);
688 + state = state < 7 ? 8 : 11;
696 + state = state < 7 ? 7 : 10;
697 + len = LzmaLenDecode(p + LenCoder, &rd, posState);
698 + posSlot = RangeDecoderBitTreeDecode(p + PosSlot +
699 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
700 + kNumPosSlotBits), kNumPosSlotBits, &rd);
701 + if (posSlot >= kStartPosModelIndex)
703 + int numDirectBits = ((posSlot >> 1) - 1);
704 + rep0 = ((2 | ((UInt32)posSlot & 1)) << numDirectBits);
705 + if (posSlot < kEndPosModelIndex)
707 + rep0 += RangeDecoderReverseBitTreeDecode(
708 + p + SpecPos + rep0 - posSlot - 1, numDirectBits, &rd);
712 + rep0 += RangeDecoderDecodeDirectBits(&rd,
713 + numDirectBits - kNumAlignBits) << kNumAlignBits;
714 + rep0 += RangeDecoderReverseBitTreeDecode(p + Align, kNumAlignBits, &rd);
721 + if (rep0 == (UInt32)(0))
723 + /* it's for stream version */
728 + #ifdef _LZMA_OUT_READ
733 + return LZMA_RESULT_DATA_ERROR;
735 + len += kMatchMinLen;
738 + #ifdef _LZMA_OUT_READ
739 + UInt32 pos = dictionaryPos - rep0;
740 + if (pos >= dictionarySize)
741 + pos += dictionarySize;
742 + previousByte = dictionary[pos];
743 + dictionary[dictionaryPos] = previousByte;
744 + if (++dictionaryPos == dictionarySize)
747 + previousByte = outStream[nowPos - rep0];
749 + outStream[nowPos++] = previousByte;
752 + while(len > 0 && nowPos < outSize);
756 + #ifdef _LZMA_OUT_READ
757 + vs->RangeDecoder = rd;
758 + vs->DictionaryPos = dictionaryPos;
759 + vs->GlobalPos = globalPos + nowPos;
760 + vs->Reps[0] = rep0;
761 + vs->Reps[1] = rep1;
762 + vs->Reps[2] = rep2;
763 + vs->Reps[3] = rep3;
765 + vs->PreviousIsMatch = previousIsMatch;
766 + vs->RemainLen = len;
769 + *outSizeProcessed = nowPos;
770 + return LZMA_RESULT_OK;
772 diff -urN linux-2.6.21.1.old/lib/Makefile linux-2.6.21.1.dev/lib/Makefile
773 --- linux-2.6.21.1.old/lib/Makefile 2007-04-27 23:49:26.000000000 +0200
774 +++ linux-2.6.21.1.dev/lib/Makefile 2007-05-26 19:03:45.721680152 +0200
776 lib-y += kobject.o kref.o kobject_uevent.o klist.o
778 obj-y += div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
779 - bust_spinlocks.o hexdump.o
780 + bust_spinlocks.o hexdump.o LzmaDecode.o
782 ifeq ($(CONFIG_DEBUG_KOBJECT),y)
783 CFLAGS_kobject.o += -DDEBUG
785 obj-$(CONFIG_AUDIT_GENERIC) += audit.o
787 obj-$(CONFIG_SWIOTLB) += swiotlb.o
789 obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
791 lib-$(CONFIG_GENERIC_BUG) += bug.o