2 +++ b/include/linux/LzmaDecode.h
6 + LZMA Decoder interface
8 + LZMA SDK 4.05 Copyright (c) 1999-2004 Igor Pavlov (2004-08-25)
9 + http://www.7-zip.org/
11 + LZMA SDK is licensed under two licenses:
12 + 1) GNU Lesser General Public License (GNU LGPL)
13 + 2) Common Public License (CPL)
14 + It means that you can select one of these two licenses and
15 + follow rules of that license.
18 + Igor Pavlov, as the author of this code, expressly permits you to
19 + statically or dynamically link your code (or bind by name) to the
20 + interfaces of this file without subjecting your linked code to the
21 + terms of the CPL or GNU LGPL. Any modifications or additions
22 + to this file, however, are subject to the LGPL or CPL terms.
25 +#ifndef __LZMADECODE_H
26 +#define __LZMADECODE_H
28 +/* #define _LZMA_IN_CB */
29 +/* Use callback for input data */
31 +/* #define _LZMA_OUT_READ */
32 +/* Use read function for output data */
34 +/* #define _LZMA_PROB32 */
35 +/* It can increase speed on some 32-bit CPUs,
36 + but memory usage will be doubled in that case */
38 +/* #define _LZMA_LOC_OPT */
39 +/* Enable local speed optimizations inside code */
42 +#ifdef _LZMA_UINT32_IS_ULONG
43 +#define UInt32 unsigned long
45 +#define UInt32 unsigned int
52 +#define CProb unsigned short
55 +#define LZMA_RESULT_OK 0
56 +#define LZMA_RESULT_DATA_ERROR 1
57 +#define LZMA_RESULT_NOT_ENOUGH_MEM 2
60 +typedef struct _ILzmaInCallback
62 + int (*Read)(void *object, unsigned char **buffer, UInt32 *bufferSize);
66 +#define LZMA_BASE_SIZE 1846
67 +#define LZMA_LIT_SIZE 768
70 +bufferSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb)
71 +bufferSize += 100 in case of _LZMA_OUT_READ
72 +by default CProb is unsigned short,
73 +but if specify _LZMA_PROB_32, CProb will be UInt32(unsigned int)
76 +#ifdef _LZMA_OUT_READ
78 + unsigned char *buffer, UInt32 bufferSize,
79 + int lc, int lp, int pb,
80 + unsigned char *dictionary, UInt32 dictionarySize,
82 + ILzmaInCallback *inCallback
84 + unsigned char *inStream, UInt32 inSize
90 + unsigned char *buffer,
91 + #ifndef _LZMA_OUT_READ
93 + int lc, int lp, int pb,
95 + ILzmaInCallback *inCallback,
97 + unsigned char *inStream, UInt32 inSize,
100 + unsigned char *outStream, UInt32 outSize,
101 + UInt32 *outSizeProcessed);
105 +++ b/lib/LzmaDecode.c
111 + LZMA SDK 4.05 Copyright (c) 1999-2004 Igor Pavlov (2004-08-25)
112 + http://www.7-zip.org/
114 + LZMA SDK is licensed under two licenses:
115 + 1) GNU Lesser General Public License (GNU LGPL)
116 + 2) Common Public License (CPL)
117 + It means that you can select one of these two licenses and
118 + follow rules of that license.
121 + Igor Pavlov, as the author of this code, expressly permits you to
122 + statically or dynamically link your code (or bind by name) to the
123 + interfaces of this file without subjecting your linked code to the
124 + terms of the CPL or GNU LGPL. Any modifications or additions
125 + to this file, however, are subject to the LGPL or CPL terms.
128 +#include <linux/LzmaDecode.h>
131 +#define Byte unsigned char
134 +#define kNumTopBits 24
135 +#define kTopValue ((UInt32)1 << kNumTopBits)
137 +#define kNumBitModelTotalBits 11
138 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
139 +#define kNumMoveBits 5
141 +typedef struct _CRangeDecoder
148 + ILzmaInCallback *InCallback;
154 +Byte RangeDecoderReadByte(CRangeDecoder *rd)
156 + if (rd->Buffer == rd->BufferLim)
160 + rd->Result = rd->InCallback->Read(rd->InCallback, &rd->Buffer, &size);
161 + rd->BufferLim = rd->Buffer + size;
165 + rd->ExtraBytes = 1;
169 + return (*rd->Buffer++);
172 +/* #define ReadByte (*rd->Buffer++) */
173 +#define ReadByte (RangeDecoderReadByte(rd))
175 +void RangeDecoderInit(CRangeDecoder *rd,
177 + ILzmaInCallback *inCallback
179 + Byte *stream, UInt32 bufferSize
185 + rd->InCallback = inCallback;
186 + rd->Buffer = rd->BufferLim = 0;
188 + rd->Buffer = stream;
189 + rd->BufferLim = stream + bufferSize;
191 + rd->ExtraBytes = 0;
193 + rd->Range = (0xFFFFFFFF);
194 + for(i = 0; i < 5; i++)
195 + rd->Code = (rd->Code << 8) | ReadByte;
198 +#define RC_INIT_VAR UInt32 range = rd->Range; UInt32 code = rd->Code;
199 +#define RC_FLUSH_VAR rd->Range = range; rd->Code = code;
200 +#define RC_NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | ReadByte; }
202 +UInt32 RangeDecoderDecodeDirectBits(CRangeDecoder *rd, int numTotalBits)
207 + for (i = numTotalBits; i > 0; i--)
219 + t = (code - range) >> 31;
221 + code -= range & (t - 1);
222 + result = (result + result) | (1 - t);
230 +int RangeDecoderBitDecode(CProb *prob, CRangeDecoder *rd)
232 + UInt32 bound = (rd->Range >> kNumBitModelTotalBits) * *prob;
233 + if (rd->Code < bound)
236 + *prob += (kBitModelTotal - *prob) >> kNumMoveBits;
237 + if (rd->Range < kTopValue)
239 + rd->Code = (rd->Code << 8) | ReadByte;
246 + rd->Range -= bound;
248 + *prob -= (*prob) >> kNumMoveBits;
249 + if (rd->Range < kTopValue)
251 + rd->Code = (rd->Code << 8) | ReadByte;
258 +#define RC_GET_BIT2(prob, mi, A0, A1) \
259 + UInt32 bound = (range >> kNumBitModelTotalBits) * *prob; \
260 + if (code < bound) \
261 + { A0; range = bound; *prob += (kBitModelTotal - *prob) >> kNumMoveBits; mi <<= 1; } \
263 + { A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
266 +#define RC_GET_BIT(prob, mi) RC_GET_BIT2(prob, mi, ; , ;)
268 +int RangeDecoderBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
272 + #ifdef _LZMA_LOC_OPT
275 + for(i = numLevels; i > 0; i--)
277 + #ifdef _LZMA_LOC_OPT
278 + CProb *prob = probs + mi;
279 + RC_GET_BIT(prob, mi)
281 + mi = (mi + mi) + RangeDecoderBitDecode(probs + mi, rd);
284 + #ifdef _LZMA_LOC_OPT
287 + return mi - (1 << numLevels);
290 +int RangeDecoderReverseBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
295 + #ifdef _LZMA_LOC_OPT
298 + for(i = 0; i < numLevels; i++)
300 + #ifdef _LZMA_LOC_OPT
301 + CProb *prob = probs + mi;
302 + RC_GET_BIT2(prob, mi, ; , symbol |= (1 << i))
304 + int bit = RangeDecoderBitDecode(probs + mi, rd);
305 + mi = mi + mi + bit;
306 + symbol |= (bit << i);
309 + #ifdef _LZMA_LOC_OPT
315 +Byte LzmaLiteralDecode(CProb *probs, CRangeDecoder *rd)
318 + #ifdef _LZMA_LOC_OPT
323 + #ifdef _LZMA_LOC_OPT
324 + CProb *prob = probs + symbol;
325 + RC_GET_BIT(prob, symbol)
327 + symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
330 + while (symbol < 0x100);
331 + #ifdef _LZMA_LOC_OPT
337 +Byte LzmaLiteralDecodeMatch(CProb *probs, CRangeDecoder *rd, Byte matchByte)
340 + #ifdef _LZMA_LOC_OPT
346 + int matchBit = (matchByte >> 7) & 1;
348 + #ifdef _LZMA_LOC_OPT
350 + CProb *prob = probs + ((1 + matchBit) << 8) + symbol;
351 + RC_GET_BIT2(prob, symbol, bit = 0, bit = 1)
354 + bit = RangeDecoderBitDecode(probs + ((1 + matchBit) << 8) + symbol, rd);
355 + symbol = (symbol << 1) | bit;
357 + if (matchBit != bit)
359 + while (symbol < 0x100)
361 + #ifdef _LZMA_LOC_OPT
362 + CProb *prob = probs + symbol;
363 + RC_GET_BIT(prob, symbol)
365 + symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
371 + while (symbol < 0x100);
372 + #ifdef _LZMA_LOC_OPT
378 +#define kNumPosBitsMax 4
379 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
381 +#define kLenNumLowBits 3
382 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
383 +#define kLenNumMidBits 3
384 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
385 +#define kLenNumHighBits 8
386 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
389 +#define LenChoice2 (LenChoice + 1)
390 +#define LenLow (LenChoice2 + 1)
391 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
392 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
393 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
395 +int LzmaLenDecode(CProb *p, CRangeDecoder *rd, int posState)
397 + if(RangeDecoderBitDecode(p + LenChoice, rd) == 0)
398 + return RangeDecoderBitTreeDecode(p + LenLow +
399 + (posState << kLenNumLowBits), kLenNumLowBits, rd);
400 + if(RangeDecoderBitDecode(p + LenChoice2, rd) == 0)
401 + return kLenNumLowSymbols + RangeDecoderBitTreeDecode(p + LenMid +
402 + (posState << kLenNumMidBits), kLenNumMidBits, rd);
403 + return kLenNumLowSymbols + kLenNumMidSymbols +
404 + RangeDecoderBitTreeDecode(p + LenHigh, kLenNumHighBits, rd);
407 +#define kNumStates 12
409 +#define kStartPosModelIndex 4
410 +#define kEndPosModelIndex 14
411 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
413 +#define kNumPosSlotBits 6
414 +#define kNumLenToPosStates 4
416 +#define kNumAlignBits 4
417 +#define kAlignTableSize (1 << kNumAlignBits)
419 +#define kMatchMinLen 2
422 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
423 +#define IsRepG0 (IsRep + kNumStates)
424 +#define IsRepG1 (IsRepG0 + kNumStates)
425 +#define IsRepG2 (IsRepG1 + kNumStates)
426 +#define IsRep0Long (IsRepG2 + kNumStates)
427 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
428 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
429 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
430 +#define LenCoder (Align + kAlignTableSize)
431 +#define RepLenCoder (LenCoder + kNumLenProbs)
432 +#define Literal (RepLenCoder + kNumLenProbs)
434 +#if Literal != LZMA_BASE_SIZE
438 +#ifdef _LZMA_OUT_READ
440 +typedef struct _LzmaVarState
442 + CRangeDecoder RangeDecoder;
444 + UInt32 DictionarySize;
445 + UInt32 DictionaryPos;
452 + int PreviousIsMatch;
456 +int LzmaDecoderInit(
457 + unsigned char *buffer, UInt32 bufferSize,
458 + int lc, int lp, int pb,
459 + unsigned char *dictionary, UInt32 dictionarySize,
461 + ILzmaInCallback *inCallback
463 + unsigned char *inStream, UInt32 inSize
467 + LzmaVarState *vs = (LzmaVarState *)buffer;
468 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
469 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
471 + if (bufferSize < numProbs * sizeof(CProb) + sizeof(LzmaVarState))
472 + return LZMA_RESULT_NOT_ENOUGH_MEM;
473 + vs->Dictionary = dictionary;
474 + vs->DictionarySize = dictionarySize;
475 + vs->DictionaryPos = 0;
477 + vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
482 + vs->PreviousIsMatch = 0;
484 + dictionary[dictionarySize - 1] = 0;
485 + for (i = 0; i < numProbs; i++)
486 + p[i] = kBitModelTotal >> 1;
487 + RangeDecoderInit(&vs->RangeDecoder,
494 + return LZMA_RESULT_OK;
497 +int LzmaDecode(unsigned char *buffer,
498 + unsigned char *outStream, UInt32 outSize,
499 + UInt32 *outSizeProcessed)
501 + LzmaVarState *vs = (LzmaVarState *)buffer;
502 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
503 + CRangeDecoder rd = vs->RangeDecoder;
504 + int state = vs->State;
505 + int previousIsMatch = vs->PreviousIsMatch;
507 + UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
509 + UInt32 posStateMask = (1 << (vs->pb)) - 1;
510 + UInt32 literalPosMask = (1 << (vs->lp)) - 1;
512 + int len = vs->RemainLen;
513 + UInt32 globalPos = vs->GlobalPos;
515 + Byte *dictionary = vs->Dictionary;
516 + UInt32 dictionarySize = vs->DictionarySize;
517 + UInt32 dictionaryPos = vs->DictionaryPos;
521 + *outSizeProcessed = 0;
522 + return LZMA_RESULT_OK;
525 + while(len > 0 && nowPos < outSize)
527 + UInt32 pos = dictionaryPos - rep0;
528 + if (pos >= dictionarySize)
529 + pos += dictionarySize;
530 + outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
531 + if (++dictionaryPos == dictionarySize)
535 + if (dictionaryPos == 0)
536 + previousByte = dictionary[dictionarySize - 1];
538 + previousByte = dictionary[dictionaryPos - 1];
542 + Byte *buffer, UInt32 bufferSize,
543 + int lc, int lp, int pb,
545 + ILzmaInCallback *inCallback,
547 + unsigned char *inStream, UInt32 inSize,
549 + unsigned char *outStream, UInt32 outSize,
550 + UInt32 *outSizeProcessed)
552 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
553 + CProb *p = (CProb *)buffer;
557 + int previousIsMatch = 0;
558 + Byte previousByte = 0;
559 + UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
561 + UInt32 posStateMask = (1 << pb) - 1;
562 + UInt32 literalPosMask = (1 << lp) - 1;
564 + if (bufferSize < numProbs * sizeof(CProb))
565 + return LZMA_RESULT_NOT_ENOUGH_MEM;
566 + for (i = 0; i < numProbs; i++)
567 + p[i] = kBitModelTotal >> 1;
568 + RangeDecoderInit(&rd,
577 + *outSizeProcessed = 0;
578 + while(nowPos < outSize)
580 + int posState = (int)(
582 + #ifdef _LZMA_OUT_READ
588 + if (rd.Result != LZMA_RESULT_OK)
591 + if (rd.ExtraBytes != 0)
592 + return LZMA_RESULT_DATA_ERROR;
593 + if (RangeDecoderBitDecode(p + IsMatch + (state << kNumPosBitsMax) + posState, &rd) == 0)
595 + CProb *probs = p + Literal + (LZMA_LIT_SIZE *
598 + #ifdef _LZMA_OUT_READ
602 + & literalPosMask) << lc) + (previousByte >> (8 - lc))));
604 + if (state < 4) state = 0;
605 + else if (state < 10) state -= 3;
607 + if (previousIsMatch)
610 + #ifdef _LZMA_OUT_READ
611 + UInt32 pos = dictionaryPos - rep0;
612 + if (pos >= dictionarySize)
613 + pos += dictionarySize;
614 + matchByte = dictionary[pos];
616 + matchByte = outStream[nowPos - rep0];
618 + previousByte = LzmaLiteralDecodeMatch(probs, &rd, matchByte);
619 + previousIsMatch = 0;
622 + previousByte = LzmaLiteralDecode(probs, &rd);
623 + outStream[nowPos++] = previousByte;
624 + #ifdef _LZMA_OUT_READ
625 + dictionary[dictionaryPos] = previousByte;
626 + if (++dictionaryPos == dictionarySize)
632 + previousIsMatch = 1;
633 + if (RangeDecoderBitDecode(p + IsRep + state, &rd) == 1)
635 + if (RangeDecoderBitDecode(p + IsRepG0 + state, &rd) == 0)
637 + if (RangeDecoderBitDecode(p + IsRep0Long + (state << kNumPosBitsMax) + posState, &rd) == 0)
639 + #ifdef _LZMA_OUT_READ
644 + #ifdef _LZMA_OUT_READ
649 + return LZMA_RESULT_DATA_ERROR;
650 + state = state < 7 ? 9 : 11;
651 + #ifdef _LZMA_OUT_READ
652 + pos = dictionaryPos - rep0;
653 + if (pos >= dictionarySize)
654 + pos += dictionarySize;
655 + previousByte = dictionary[pos];
656 + dictionary[dictionaryPos] = previousByte;
657 + if (++dictionaryPos == dictionarySize)
660 + previousByte = outStream[nowPos - rep0];
662 + outStream[nowPos++] = previousByte;
669 + if(RangeDecoderBitDecode(p + IsRepG1 + state, &rd) == 0)
673 + if(RangeDecoderBitDecode(p + IsRepG2 + state, &rd) == 0)
685 + len = LzmaLenDecode(p + RepLenCoder, &rd, posState);
686 + state = state < 7 ? 8 : 11;
694 + state = state < 7 ? 7 : 10;
695 + len = LzmaLenDecode(p + LenCoder, &rd, posState);
696 + posSlot = RangeDecoderBitTreeDecode(p + PosSlot +
697 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
698 + kNumPosSlotBits), kNumPosSlotBits, &rd);
699 + if (posSlot >= kStartPosModelIndex)
701 + int numDirectBits = ((posSlot >> 1) - 1);
702 + rep0 = ((2 | ((UInt32)posSlot & 1)) << numDirectBits);
703 + if (posSlot < kEndPosModelIndex)
705 + rep0 += RangeDecoderReverseBitTreeDecode(
706 + p + SpecPos + rep0 - posSlot - 1, numDirectBits, &rd);
710 + rep0 += RangeDecoderDecodeDirectBits(&rd,
711 + numDirectBits - kNumAlignBits) << kNumAlignBits;
712 + rep0 += RangeDecoderReverseBitTreeDecode(p + Align, kNumAlignBits, &rd);
719 + if (rep0 == (UInt32)(0))
721 + /* it's for stream version */
726 + #ifdef _LZMA_OUT_READ
731 + return LZMA_RESULT_DATA_ERROR;
733 + len += kMatchMinLen;
736 + #ifdef _LZMA_OUT_READ
737 + UInt32 pos = dictionaryPos - rep0;
738 + if (pos >= dictionarySize)
739 + pos += dictionarySize;
740 + previousByte = dictionary[pos];
741 + dictionary[dictionaryPos] = previousByte;
742 + if (++dictionaryPos == dictionarySize)
745 + previousByte = outStream[nowPos - rep0];
747 + outStream[nowPos++] = previousByte;
750 + while(len > 0 && nowPos < outSize);
754 + #ifdef _LZMA_OUT_READ
755 + vs->RangeDecoder = rd;
756 + vs->DictionaryPos = dictionaryPos;
757 + vs->GlobalPos = globalPos + nowPos;
758 + vs->Reps[0] = rep0;
759 + vs->Reps[1] = rep1;
760 + vs->Reps[2] = rep2;
761 + vs->Reps[3] = rep3;
763 + vs->PreviousIsMatch = previousIsMatch;
764 + vs->RemainLen = len;
767 + *outSizeProcessed = nowPos;
768 + return LZMA_RESULT_OK;
772 @@ -14,7 +14,7 @@ lib-$(CONFIG_SMP) += cpumask.o
773 lib-y += kobject.o kref.o klist.o
775 obj-y += div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
776 - bust_spinlocks.o hexdump.o kasprintf.o bitmap.o
777 + bust_spinlocks.o hexdump.o kasprintf.o bitmap.o LzmaDecode.o
779 ifeq ($(CONFIG_DEBUG_KOBJECT),y)
780 CFLAGS_kobject.o += -DDEBUG
781 @@ -64,6 +64,7 @@ obj-$(CONFIG_SMP) += percpu_counter.o
782 obj-$(CONFIG_AUDIT_GENERIC) += audit.o
784 obj-$(CONFIG_SWIOTLB) += swiotlb.o
786 obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
788 lib-$(CONFIG_GENERIC_BUG) += bug.o