MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / squashfs / LzmaDecode.c
blob292b3a7469e5c9c85c1368c0c7d1f16829742580
1 /*
2 LzmaDecode.c
3 LZMA Decoder
4 LZMA SDK 4.01 Copyright (c) 1999-2004 Igor Pavlov (2004-02-15)
5 */
7 #include "LzmaDecode.h"
9 #ifndef Byte
10 #define Byte unsigned char
11 #endif
13 #define kNumTopBits 24
14 #define kTopValue ((UInt32)1 << kNumTopBits)
16 #define kNumBitModelTotalBits 11
17 #define kBitModelTotal (1 << kNumBitModelTotalBits)
18 #define kNumMoveBits 5
20 typedef struct _CRangeDecoder
22 Byte *Buffer;
23 Byte *BufferLim;
24 UInt32 Range;
25 UInt32 Code;
26 #ifdef _LZMA_IN_CB
27 ILzmaInCallback *InCallback;
28 int Result;
29 #endif
30 int ExtraBytes;
31 } CRangeDecoder;
33 Byte RangeDecoderReadByte(CRangeDecoder *rd)
35 if (rd->Buffer == rd->BufferLim)
37 #ifdef _LZMA_IN_CB
38 UInt32 size;
39 rd->Result = rd->InCallback->Read(rd->InCallback, &rd->Buffer, &size);
40 rd->BufferLim = rd->Buffer + size;
41 if (size == 0)
42 #endif
44 rd->ExtraBytes = 1;
45 return 0xFF;
48 return (*rd->Buffer++);
51 /* #define ReadByte (*rd->Buffer++) */
52 #define ReadByte (RangeDecoderReadByte(rd))
54 void RangeDecoderInit(CRangeDecoder *rd,
55 #ifdef _LZMA_IN_CB
56 ILzmaInCallback *inCallback
57 #else
58 Byte *stream, UInt32 bufferSize
59 #endif
62 int i;
63 #ifdef _LZMA_IN_CB
64 rd->InCallback = inCallback;
65 rd->Buffer = rd->BufferLim = 0;
66 #else
67 rd->Buffer = stream;
68 rd->BufferLim = stream + bufferSize;
69 #endif
70 rd->ExtraBytes = 0;
71 rd->Code = 0;
72 rd->Range = (0xFFFFFFFF);
73 for(i = 0; i < 5; i++)
74 rd->Code = (rd->Code << 8) | ReadByte;
77 #define RC_INIT_VAR UInt32 range = rd->Range; UInt32 code = rd->Code;
78 #define RC_FLUSH_VAR rd->Range = range; rd->Code = code;
79 #define RC_NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | ReadByte; }
81 UInt32 RangeDecoderDecodeDirectBits(CRangeDecoder *rd, int numTotalBits)
83 RC_INIT_VAR
84 UInt32 result = 0;
85 int i;
86 for (i = numTotalBits; i > 0; i--)
88 /* UInt32 t; */
89 range >>= 1;
91 result <<= 1;
92 if (code >= range)
94 code -= range;
95 result |= 1;
98 t = (code - range) >> 31;
99 t &= 1;
100 code -= range & (t - 1);
101 result = (result + result) | (1 - t);
103 RC_NORMALIZE
105 RC_FLUSH_VAR
106 return result;
109 int RangeDecoderBitDecode(CProb *prob, CRangeDecoder *rd)
111 UInt32 bound = (rd->Range >> kNumBitModelTotalBits) * *prob;
112 if (rd->Code < bound)
114 rd->Range = bound;
115 *prob += (kBitModelTotal - *prob) >> kNumMoveBits;
116 if (rd->Range < kTopValue)
118 rd->Code = (rd->Code << 8) | ReadByte;
119 rd->Range <<= 8;
121 return 0;
123 else
125 rd->Range -= bound;
126 rd->Code -= bound;
127 *prob -= (*prob) >> kNumMoveBits;
128 if (rd->Range < kTopValue)
130 rd->Code = (rd->Code << 8) | ReadByte;
131 rd->Range <<= 8;
133 return 1;
137 #define RC_GET_BIT2(prob, mi, A0, A1) \
138 UInt32 bound = (range >> kNumBitModelTotalBits) * *prob; \
139 if (code < bound) \
140 { A0; range = bound; *prob += (kBitModelTotal - *prob) >> kNumMoveBits; mi <<= 1; } \
141 else \
142 { A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
143 RC_NORMALIZE
145 #define RC_GET_BIT(prob, mi) RC_GET_BIT2(prob, mi, ; , ;)
147 int RangeDecoderBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
149 int mi = 1;
150 int i;
151 #ifdef _LZMA_LOC_OPT
152 RC_INIT_VAR
153 #endif
154 for(i = numLevels; i > 0; i--)
156 #ifdef _LZMA_LOC_OPT
157 CProb *prob = probs + mi;
158 RC_GET_BIT(prob, mi)
159 #else
160 mi = (mi + mi) + RangeDecoderBitDecode(probs + mi, rd);
161 #endif
163 #ifdef _LZMA_LOC_OPT
164 RC_FLUSH_VAR
165 #endif
166 return mi - (1 << numLevels);
169 int RangeDecoderReverseBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
171 int mi = 1;
172 int i;
173 int symbol = 0;
174 #ifdef _LZMA_LOC_OPT
175 RC_INIT_VAR
176 #endif
177 for(i = 0; i < numLevels; i++)
179 #ifdef _LZMA_LOC_OPT
180 CProb *prob = probs + mi;
181 RC_GET_BIT2(prob, mi, ; , symbol |= (1 << i))
182 #else
183 int bit = RangeDecoderBitDecode(probs + mi, rd);
184 mi = mi + mi + bit;
185 symbol |= (bit << i);
186 #endif
188 #ifdef _LZMA_LOC_OPT
189 RC_FLUSH_VAR
190 #endif
191 return symbol;
194 Byte LzmaLiteralDecode(CProb *probs, CRangeDecoder *rd)
196 int symbol = 1;
197 #ifdef _LZMA_LOC_OPT
198 RC_INIT_VAR
199 #endif
202 #ifdef _LZMA_LOC_OPT
203 CProb *prob = probs + symbol;
204 RC_GET_BIT(prob, symbol)
205 #else
206 symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
207 #endif
209 while (symbol < 0x100);
210 #ifdef _LZMA_LOC_OPT
211 RC_FLUSH_VAR
212 #endif
213 return symbol;
216 Byte LzmaLiteralDecodeMatch(CProb *probs, CRangeDecoder *rd, Byte matchByte)
218 int symbol = 1;
219 #ifdef _LZMA_LOC_OPT
220 RC_INIT_VAR
221 #endif
224 int bit;
225 int matchBit = (matchByte >> 7) & 1;
226 matchByte <<= 1;
227 #ifdef _LZMA_LOC_OPT
229 CProb *prob = probs + ((1 + matchBit) << 8) + symbol;
230 RC_GET_BIT2(prob, symbol, bit = 0, bit = 1)
232 #else
233 bit = RangeDecoderBitDecode(probs + ((1 + matchBit) << 8) + symbol, rd);
234 symbol = (symbol << 1) | bit;
235 #endif
236 if (matchBit != bit)
238 while (symbol < 0x100)
240 #ifdef _LZMA_LOC_OPT
241 CProb *prob = probs + symbol;
242 RC_GET_BIT(prob, symbol)
243 #else
244 symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
245 #endif
247 break;
250 while (symbol < 0x100);
251 #ifdef _LZMA_LOC_OPT
252 RC_FLUSH_VAR
253 #endif
254 return symbol;
257 #define kNumPosBitsMax 4
258 #define kNumPosStatesMax (1 << kNumPosBitsMax)
260 #define kLenNumLowBits 3
261 #define kLenNumLowSymbols (1 << kLenNumLowBits)
262 #define kLenNumMidBits 3
263 #define kLenNumMidSymbols (1 << kLenNumMidBits)
264 #define kLenNumHighBits 8
265 #define kLenNumHighSymbols (1 << kLenNumHighBits)
267 #define LenChoice 0
268 #define LenChoice2 (LenChoice + 1)
269 #define LenLow (LenChoice2 + 1)
270 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
271 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
272 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
274 int LzmaLenDecode(CProb *p, CRangeDecoder *rd, int posState)
276 if(RangeDecoderBitDecode(p + LenChoice, rd) == 0)
277 return RangeDecoderBitTreeDecode(p + LenLow +
278 (posState << kLenNumLowBits), kLenNumLowBits, rd);
279 if(RangeDecoderBitDecode(p + LenChoice2, rd) == 0)
280 return kLenNumLowSymbols + RangeDecoderBitTreeDecode(p + LenMid +
281 (posState << kLenNumMidBits), kLenNumMidBits, rd);
282 return kLenNumLowSymbols + kLenNumMidSymbols +
283 RangeDecoderBitTreeDecode(p + LenHigh, kLenNumHighBits, rd);
286 #define kNumStates 12
288 #define kStartPosModelIndex 4
289 #define kEndPosModelIndex 14
290 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
292 #define kNumPosSlotBits 6
293 #define kNumLenToPosStates 4
295 #define kNumAlignBits 4
296 #define kAlignTableSize (1 << kNumAlignBits)
298 #define kMatchMinLen 2
300 #define IsMatch 0
301 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
302 #define IsRepG0 (IsRep + kNumStates)
303 #define IsRepG1 (IsRepG0 + kNumStates)
304 #define IsRepG2 (IsRepG1 + kNumStates)
305 #define IsRep0Long (IsRepG2 + kNumStates)
306 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
307 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
308 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
309 #define LenCoder (Align + kAlignTableSize)
310 #define RepLenCoder (LenCoder + kNumLenProbs)
311 #define Literal (RepLenCoder + kNumLenProbs)
313 #if Literal != LZMA_BASE_SIZE
314 StopCompilingDueBUG
315 #endif
317 #ifdef _LZMA_OUT_READ
319 typedef struct _LzmaVarState
321 CRangeDecoder RangeDecoder;
322 Byte *Dictionary;
323 UInt32 DictionarySize;
324 UInt32 DictionaryPos;
325 UInt32 GlobalPos;
326 UInt32 Reps[4];
327 int lc;
328 int lp;
329 int pb;
330 int State;
331 int PreviousIsMatch;
332 int RemainLen;
333 } LzmaVarState;
335 int LzmaDecoderInit(
336 unsigned char *buffer, UInt32 bufferSize,
337 int lc, int lp, int pb,
338 unsigned char *dictionary, UInt32 dictionarySize,
339 #ifdef _LZMA_IN_CB
340 ILzmaInCallback *inCallback
341 #else
342 unsigned char *inStream, UInt32 inSize
343 #endif
346 LzmaVarState *vs = (LzmaVarState *)buffer;
347 CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
348 UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
349 UInt32 i;
350 if (bufferSize < numProbs * sizeof(CProb) + sizeof(LzmaVarState))
351 return LZMA_RESULT_NOT_ENOUGH_MEM;
352 vs->Dictionary = dictionary;
353 vs->DictionarySize = dictionarySize;
354 vs->DictionaryPos = 0;
355 vs->GlobalPos = 0;
356 vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
357 vs->lc = lc;
358 vs->lp = lp;
359 vs->pb = pb;
360 vs->State = 0;
361 vs->PreviousIsMatch = 0;
362 vs->RemainLen = 0;
363 dictionary[dictionarySize - 1] = 0;
364 for (i = 0; i < numProbs; i++)
365 p[i] = kBitModelTotal >> 1;
366 RangeDecoderInit(&vs->RangeDecoder,
367 #ifdef _LZMA_IN_CB
368 inCallback
369 #else
370 inStream, inSize
371 #endif
373 return LZMA_RESULT_OK;
376 int LzmaDecode(unsigned char *buffer,
377 unsigned char *outStream, UInt32 outSize,
378 UInt32 *outSizeProcessed)
380 LzmaVarState *vs = (LzmaVarState *)buffer;
381 CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
382 CRangeDecoder rd = vs->RangeDecoder;
383 int state = vs->State;
384 int previousIsMatch = vs->PreviousIsMatch;
385 Byte previousByte;
386 UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
387 UInt32 nowPos = 0;
388 UInt32 posStateMask = (1 << (vs->pb)) - 1;
389 UInt32 literalPosMask = (1 << (vs->lp)) - 1;
390 int lc = vs->lc;
391 int len = vs->RemainLen;
392 UInt32 globalPos = vs->GlobalPos;
394 Byte *dictionary = vs->Dictionary;
395 UInt32 dictionarySize = vs->DictionarySize;
396 UInt32 dictionaryPos = vs->DictionaryPos;
398 if (len == -1)
400 *outSizeProcessed = 0;
401 return LZMA_RESULT_OK;
404 while(len > 0 && nowPos < outSize)
406 UInt32 pos = dictionaryPos - rep0;
407 if (pos >= dictionarySize)
408 pos += dictionarySize;
409 outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
410 if (++dictionaryPos == dictionarySize)
411 dictionaryPos = 0;
412 len--;
414 if (dictionaryPos == 0)
415 previousByte = dictionary[dictionarySize - 1];
416 else
417 previousByte = dictionary[dictionaryPos - 1];
418 #else
420 int LzmaDecode(
421 Byte *buffer, UInt32 bufferSize,
422 int lc, int lp, int pb,
423 #ifdef _LZMA_IN_CB
424 ILzmaInCallback *inCallback,
425 #else
426 unsigned char *inStream, UInt32 inSize,
427 #endif
428 unsigned char *outStream, UInt32 outSize,
429 UInt32 *outSizeProcessed)
431 UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
432 CProb *p = (CProb *)buffer;
433 CRangeDecoder rd;
434 UInt32 i;
435 int state = 0;
436 int previousIsMatch = 0;
437 Byte previousByte = 0;
438 UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
439 UInt32 nowPos = 0;
440 UInt32 posStateMask = (1 << pb) - 1;
441 UInt32 literalPosMask = (1 << lp) - 1;
442 int len = 0;
443 if (bufferSize < numProbs * sizeof(CProb))
444 return LZMA_RESULT_NOT_ENOUGH_MEM;
445 for (i = 0; i < numProbs; i++)
446 p[i] = kBitModelTotal >> 1;
447 RangeDecoderInit(&rd,
448 #ifdef _LZMA_IN_CB
449 inCallback
450 #else
451 inStream, inSize
452 #endif
454 #endif
456 *outSizeProcessed = 0;
457 while(nowPos < outSize)
459 int posState = (int)(
460 (nowPos
461 #ifdef _LZMA_OUT_READ
462 + globalPos
463 #endif
465 & posStateMask);
466 #ifdef _LZMA_IN_CB
467 if (rd.Result != LZMA_RESULT_OK)
468 return rd.Result;
469 #endif
470 if (rd.ExtraBytes != 0)
471 return LZMA_RESULT_DATA_ERROR;
472 if (RangeDecoderBitDecode(p + IsMatch + (state << kNumPosBitsMax) + posState, &rd) == 0)
474 CProb *probs = p + Literal + (LZMA_LIT_SIZE *
476 (nowPos
477 #ifdef _LZMA_OUT_READ
478 + globalPos
479 #endif
481 & literalPosMask) << lc) + (previousByte >> (8 - lc))));
483 if (state < 4) state = 0;
484 else if (state < 10) state -= 3;
485 else state -= 6;
486 if (previousIsMatch)
488 Byte matchByte;
489 #ifdef _LZMA_OUT_READ
490 UInt32 pos = dictionaryPos - rep0;
491 if (pos >= dictionarySize)
492 pos += dictionarySize;
493 matchByte = dictionary[pos];
494 #else
495 matchByte = outStream[nowPos - rep0];
496 #endif
497 previousByte = LzmaLiteralDecodeMatch(probs, &rd, matchByte);
498 previousIsMatch = 0;
500 else
501 previousByte = LzmaLiteralDecode(probs, &rd);
502 outStream[nowPos++] = previousByte;
503 #ifdef _LZMA_OUT_READ
504 dictionary[dictionaryPos] = previousByte;
505 if (++dictionaryPos == dictionarySize)
506 dictionaryPos = 0;
507 #endif
509 else
511 previousIsMatch = 1;
512 if (RangeDecoderBitDecode(p + IsRep + state, &rd) == 1)
514 if (RangeDecoderBitDecode(p + IsRepG0 + state, &rd) == 0)
516 if (RangeDecoderBitDecode(p + IsRep0Long + (state << kNumPosBitsMax) + posState, &rd) == 0)
518 #ifdef _LZMA_OUT_READ
519 UInt32 pos;
520 #endif
521 if (
522 (nowPos
523 #ifdef _LZMA_OUT_READ
524 + globalPos
525 #endif
527 == 0)
528 return LZMA_RESULT_DATA_ERROR;
529 state = state < 7 ? 9 : 11;
530 #ifdef _LZMA_OUT_READ
531 pos = dictionaryPos - rep0;
532 if (pos >= dictionarySize)
533 pos += dictionarySize;
534 previousByte = dictionary[pos];
535 dictionary[dictionaryPos] = previousByte;
536 if (++dictionaryPos == dictionarySize)
537 dictionaryPos = 0;
538 #else
539 previousByte = outStream[nowPos - rep0];
540 #endif
541 outStream[nowPos++] = previousByte;
542 continue;
545 else
547 UInt32 distance;
548 if(RangeDecoderBitDecode(p + IsRepG1 + state, &rd) == 0)
549 distance = rep1;
550 else
552 if(RangeDecoderBitDecode(p + IsRepG2 + state, &rd) == 0)
553 distance = rep2;
554 else
556 distance = rep3;
557 rep3 = rep2;
559 rep2 = rep1;
561 rep1 = rep0;
562 rep0 = distance;
564 len = LzmaLenDecode(p + RepLenCoder, &rd, posState);
565 state = state < 7 ? 8 : 11;
567 else
569 int posSlot;
570 rep3 = rep2;
571 rep2 = rep1;
572 rep1 = rep0;
573 state = state < 7 ? 7 : 10;
574 len = LzmaLenDecode(p + LenCoder, &rd, posState);
575 posSlot = RangeDecoderBitTreeDecode(p + PosSlot +
576 ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
577 kNumPosSlotBits), kNumPosSlotBits, &rd);
578 if (posSlot >= kStartPosModelIndex)
580 int numDirectBits = ((posSlot >> 1) - 1);
581 rep0 = ((2 | ((UInt32)posSlot & 1)) << numDirectBits);
582 if (posSlot < kEndPosModelIndex)
584 rep0 += RangeDecoderReverseBitTreeDecode(
585 p + SpecPos + rep0 - posSlot - 1, numDirectBits, &rd);
587 else
589 rep0 += RangeDecoderDecodeDirectBits(&rd,
590 numDirectBits - kNumAlignBits) << kNumAlignBits;
591 rep0 += RangeDecoderReverseBitTreeDecode(p + Align, kNumAlignBits, &rd);
594 else
595 rep0 = posSlot;
596 rep0++;
598 if (rep0 == (UInt32)(0))
600 /* it's for stream version */
601 len = -1;
602 break;
604 if (rep0 > nowPos
605 #ifdef _LZMA_OUT_READ
606 + globalPos
607 #endif
610 return LZMA_RESULT_DATA_ERROR;
612 len += kMatchMinLen;
615 #ifdef _LZMA_OUT_READ
616 UInt32 pos = dictionaryPos - rep0;
617 if (pos >= dictionarySize)
618 pos += dictionarySize;
619 previousByte = dictionary[pos];
620 dictionary[dictionaryPos] = previousByte;
621 if (++dictionaryPos == dictionarySize)
622 dictionaryPos = 0;
623 #else
624 previousByte = outStream[nowPos - rep0];
625 #endif
626 outStream[nowPos++] = previousByte;
627 len--;
629 while(len > 0 && nowPos < outSize);
633 #ifdef _LZMA_OUT_READ
634 vs->RangeDecoder = rd;
635 vs->DictionaryPos = dictionaryPos;
636 vs->GlobalPos = globalPos + nowPos;
637 vs->Reps[0] = rep0;
638 vs->Reps[1] = rep1;
639 vs->Reps[2] = rep2;
640 vs->Reps[3] = rep3;
641 vs->State = state;
642 vs->PreviousIsMatch = previousIsMatch;
643 vs->RemainLen = len;
644 #endif
646 *outSizeProcessed = nowPos;
647 return LZMA_RESULT_OK;