8990 /opt/onbld/gk is useless
[unleashed.git] / usr / src / common / lzma / LzmaDec.c
blob1e74564c1750014ea14869ff1d4a9af7bb454424
1 /*
2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /* LzmaDec.c -- LZMA Decoder
7 2008-11-06 : Igor Pavlov : Public domain */
9 #include "LzmaDec.h"
11 #ifndef _KERNEL
12 #include <string.h>
13 #endif /* _KERNEL */
15 #define kNumTopBits 24
16 #define kTopValue ((UInt32)1 << kNumTopBits)
18 #define kNumBitModelTotalBits 11
19 #define kBitModelTotal (1 << kNumBitModelTotalBits)
20 #define kNumMoveBits 5
22 #define RC_INIT_SIZE 5
24 #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
26 #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
27 #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
28 #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
29 #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
30 { UPDATE_0(p); i = (i + i); A0; } else \
31 { UPDATE_1(p); i = (i + i) + 1; A1; }
32 #define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
34 #define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
35 #define TREE_DECODE(probs, limit, i) \
36 { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
38 /* #define _LZMA_SIZE_OPT */
40 #ifdef _LZMA_SIZE_OPT
41 #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
42 #else
43 #define TREE_6_DECODE(probs, i) \
44 { i = 1; \
45 TREE_GET_BIT(probs, i); \
46 TREE_GET_BIT(probs, i); \
47 TREE_GET_BIT(probs, i); \
48 TREE_GET_BIT(probs, i); \
49 TREE_GET_BIT(probs, i); \
50 TREE_GET_BIT(probs, i); \
51 i -= 0x40; }
52 #endif
54 #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
56 #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
57 #define UPDATE_0_CHECK range = bound;
58 #define UPDATE_1_CHECK range -= bound; code -= bound;
59 #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
60 { UPDATE_0_CHECK; i = (i + i); A0; } else \
61 { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
62 #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
63 #define TREE_DECODE_CHECK(probs, limit, i) \
64 { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
67 #define kNumPosBitsMax 4
68 #define kNumPosStatesMax (1 << kNumPosBitsMax)
70 #define kLenNumLowBits 3
71 #define kLenNumLowSymbols (1 << kLenNumLowBits)
72 #define kLenNumMidBits 3
73 #define kLenNumMidSymbols (1 << kLenNumMidBits)
74 #define kLenNumHighBits 8
75 #define kLenNumHighSymbols (1 << kLenNumHighBits)
77 #define LenChoice 0
78 #define LenChoice2 (LenChoice + 1)
79 #define LenLow (LenChoice2 + 1)
80 #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
81 #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
82 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
85 #define kNumStates 12
86 #define kNumLitStates 7
88 #define kStartPosModelIndex 4
89 #define kEndPosModelIndex 14
90 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
92 #define kNumPosSlotBits 6
93 #define kNumLenToPosStates 4
95 #define kNumAlignBits 4
96 #define kAlignTableSize (1 << kNumAlignBits)
98 #define kMatchMinLen 2
99 #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
101 #define IsMatch 0
102 #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
103 #define IsRepG0 (IsRep + kNumStates)
104 #define IsRepG1 (IsRepG0 + kNumStates)
105 #define IsRepG2 (IsRepG1 + kNumStates)
106 #define IsRep0Long (IsRepG2 + kNumStates)
107 #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
108 #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
109 #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
110 #define LenCoder (Align + kAlignTableSize)
111 #define RepLenCoder (LenCoder + kNumLenProbs)
112 #define Literal (RepLenCoder + kNumLenProbs)
114 #define LZMA_BASE_SIZE 1846
115 #define LZMA_LIT_SIZE 768
117 #define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
119 #if Literal != LZMA_BASE_SIZE
120 StopCompilingDueBUG
121 #endif
123 static const Byte kLiteralNextStates[kNumStates * 2] =
125 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5,
126 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10
129 #ifdef _KERNEL
130 extern void *memcpy(void *, const void *, size_t);
131 #endif /* _KERNEL */
133 #define LZMA_DIC_MIN (1 << 12)
135 /* First LZMA-symbol is always decoded.
136 And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
137 Out:
138 Result:
139 SZ_OK - OK
140 SZ_ERROR_DATA - Error
141 p->remainLen:
142 < kMatchSpecLenStart : normal remain
143 = kMatchSpecLenStart : finished
144 = kMatchSpecLenStart + 1 : Flush marker
145 = kMatchSpecLenStart + 2 : State Init Marker
148 static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
150 CLzmaProb *probs = p->probs;
152 unsigned state = p->state;
153 UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
154 unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
155 unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
156 unsigned lc = p->prop.lc;
158 Byte *dic = p->dic;
159 SizeT dicBufSize = p->dicBufSize;
160 SizeT dicPos = p->dicPos;
162 UInt32 processedPos = p->processedPos;
163 UInt32 checkDicSize = p->checkDicSize;
164 unsigned len = 0;
166 const Byte *buf = p->buf;
167 UInt32 range = p->range;
168 UInt32 code = p->code;
172 CLzmaProb *prob;
173 UInt32 bound;
174 unsigned ttt;
175 unsigned posState = processedPos & pbMask;
177 prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
178 IF_BIT_0(prob)
180 unsigned symbol;
181 UPDATE_0(prob);
182 prob = probs + Literal;
183 if (checkDicSize != 0 || processedPos != 0)
184 prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
185 (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
187 if (state < kNumLitStates)
189 symbol = 1;
190 do { GET_BIT(prob + symbol, symbol) } while (symbol < 0x100);
192 else
194 unsigned matchByte = p->dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
195 unsigned offs = 0x100;
196 symbol = 1;
199 unsigned bit;
200 CLzmaProb *probLit;
201 matchByte <<= 1;
202 bit = (matchByte & offs);
203 probLit = prob + offs + bit + symbol;
204 GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
206 while (symbol < 0x100);
208 dic[dicPos++] = (Byte)symbol;
209 processedPos++;
211 state = kLiteralNextStates[state];
212 /* if (state < 4) state = 0; else if (state < 10) state -= 3; else state -= 6; */
213 continue;
215 else
217 UPDATE_1(prob);
218 prob = probs + IsRep + state;
219 IF_BIT_0(prob)
221 UPDATE_0(prob);
222 state += kNumStates;
223 prob = probs + LenCoder;
225 else
227 UPDATE_1(prob);
228 if (checkDicSize == 0 && processedPos == 0)
229 return SZ_ERROR_DATA;
230 prob = probs + IsRepG0 + state;
231 IF_BIT_0(prob)
233 UPDATE_0(prob);
234 prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
235 IF_BIT_0(prob)
237 UPDATE_0(prob);
238 dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
239 dicPos++;
240 processedPos++;
241 state = state < kNumLitStates ? 9 : 11;
242 continue;
244 UPDATE_1(prob);
246 else
248 UInt32 distance;
249 UPDATE_1(prob);
250 prob = probs + IsRepG1 + state;
251 IF_BIT_0(prob)
253 UPDATE_0(prob);
254 distance = rep1;
256 else
258 UPDATE_1(prob);
259 prob = probs + IsRepG2 + state;
260 IF_BIT_0(prob)
262 UPDATE_0(prob);
263 distance = rep2;
265 else
267 UPDATE_1(prob);
268 distance = rep3;
269 rep3 = rep2;
271 rep2 = rep1;
273 rep1 = rep0;
274 rep0 = distance;
276 state = state < kNumLitStates ? 8 : 11;
277 prob = probs + RepLenCoder;
280 unsigned limit, offset;
281 CLzmaProb *probLen = prob + LenChoice;
282 IF_BIT_0(probLen)
284 UPDATE_0(probLen);
285 probLen = prob + LenLow + (posState << kLenNumLowBits);
286 offset = 0;
287 limit = (1 << kLenNumLowBits);
289 else
291 UPDATE_1(probLen);
292 probLen = prob + LenChoice2;
293 IF_BIT_0(probLen)
295 UPDATE_0(probLen);
296 probLen = prob + LenMid + (posState << kLenNumMidBits);
297 offset = kLenNumLowSymbols;
298 limit = (1 << kLenNumMidBits);
300 else
302 UPDATE_1(probLen);
303 probLen = prob + LenHigh;
304 offset = kLenNumLowSymbols + kLenNumMidSymbols;
305 limit = (1 << kLenNumHighBits);
308 TREE_DECODE(probLen, limit, len);
309 len += offset;
312 if (state >= kNumStates)
314 UInt32 distance;
315 prob = probs + PosSlot +
316 ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
317 TREE_6_DECODE(prob, distance);
318 if (distance >= kStartPosModelIndex)
320 unsigned posSlot = (unsigned)distance;
321 int numDirectBits = (int)(((distance >> 1) - 1));
322 distance = (2 | (distance & 1));
323 if (posSlot < kEndPosModelIndex)
325 distance <<= numDirectBits;
326 prob = probs + SpecPos + distance - posSlot - 1;
328 UInt32 mask = 1;
329 unsigned i = 1;
332 GET_BIT2(prob + i, i, ; , distance |= mask);
333 mask <<= 1;
335 while (--numDirectBits != 0);
338 else
340 numDirectBits -= kNumAlignBits;
343 NORMALIZE
344 range >>= 1;
347 UInt32 t;
348 code -= range;
349 t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
350 distance = (distance << 1) + (t + 1);
351 code += range & t;
354 distance <<= 1;
355 if (code >= range)
357 code -= range;
358 distance |= 1;
362 while (--numDirectBits != 0);
363 prob = probs + Align;
364 distance <<= kNumAlignBits;
366 unsigned i = 1;
367 GET_BIT2(prob + i, i, ; , distance |= 1);
368 GET_BIT2(prob + i, i, ; , distance |= 2);
369 GET_BIT2(prob + i, i, ; , distance |= 4);
370 GET_BIT2(prob + i, i, ; , distance |= 8);
372 if (distance == (UInt32)0xFFFFFFFF)
374 len += kMatchSpecLenStart;
375 state -= kNumStates;
376 break;
380 rep3 = rep2;
381 rep2 = rep1;
382 rep1 = rep0;
383 rep0 = distance + 1;
384 if (checkDicSize == 0)
386 if (distance >= processedPos)
387 return SZ_ERROR_DATA;
389 else if (distance >= checkDicSize)
390 return SZ_ERROR_DATA;
391 state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
392 /* state = kLiteralNextStates[state]; */
395 len += kMatchMinLen;
397 if (limit == dicPos)
398 return SZ_ERROR_DATA;
400 SizeT rem = limit - dicPos;
401 unsigned curLen = ((rem < len) ? (unsigned)rem : len);
402 SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
404 processedPos += curLen;
406 len -= curLen;
407 if (pos + curLen <= dicBufSize)
409 Byte *dest = dic + dicPos;
410 ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
411 const Byte *lim = dest + curLen;
412 dicPos += curLen;
414 *(dest) = (Byte)*(dest + src);
415 while (++dest != lim);
417 else
421 dic[dicPos++] = dic[pos];
422 if (++pos == dicBufSize)
423 pos = 0;
425 while (--curLen != 0);
430 while (dicPos < limit && buf < bufLimit);
431 NORMALIZE;
432 p->buf = buf;
433 p->range = range;
434 p->code = code;
435 p->remainLen = len;
436 p->dicPos = dicPos;
437 p->processedPos = processedPos;
438 p->reps[0] = rep0;
439 p->reps[1] = rep1;
440 p->reps[2] = rep2;
441 p->reps[3] = rep3;
442 p->state = state;
444 return SZ_OK;
447 static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
449 if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
451 Byte *dic = p->dic;
452 SizeT dicPos = p->dicPos;
453 SizeT dicBufSize = p->dicBufSize;
454 unsigned len = p->remainLen;
455 UInt32 rep0 = p->reps[0];
456 if (limit - dicPos < len)
457 len = (unsigned)(limit - dicPos);
459 if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
460 p->checkDicSize = p->prop.dicSize;
462 p->processedPos += len;
463 p->remainLen -= len;
464 while (len-- != 0)
466 dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
467 dicPos++;
469 p->dicPos = dicPos;
473 static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
477 SizeT limit2 = limit;
478 if (p->checkDicSize == 0)
480 UInt32 rem = p->prop.dicSize - p->processedPos;
481 if (limit - p->dicPos > rem)
482 limit2 = p->dicPos + rem;
484 RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
485 if (p->processedPos >= p->prop.dicSize)
486 p->checkDicSize = p->prop.dicSize;
487 LzmaDec_WriteRem(p, limit);
489 while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
491 if (p->remainLen > kMatchSpecLenStart)
493 p->remainLen = kMatchSpecLenStart;
495 return 0;
498 typedef enum
500 DUMMY_ERROR, /* unexpected end of input stream */
501 DUMMY_LIT,
502 DUMMY_MATCH,
503 DUMMY_REP
504 } ELzmaDummy;
506 static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
508 UInt32 range = p->range;
509 UInt32 code = p->code;
510 const Byte *bufLimit = buf + inSize;
511 CLzmaProb *probs = p->probs;
512 unsigned state = p->state;
513 ELzmaDummy res;
516 CLzmaProb *prob;
517 UInt32 bound;
518 unsigned ttt;
519 unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
521 prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
522 IF_BIT_0_CHECK(prob)
524 UPDATE_0_CHECK
526 /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
528 prob = probs + Literal;
529 if (p->checkDicSize != 0 || p->processedPos != 0)
530 prob += (LZMA_LIT_SIZE *
531 ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
532 (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
534 if (state < kNumLitStates)
536 unsigned symbol = 1;
537 do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
539 else
541 unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
542 ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
543 unsigned offs = 0x100;
544 unsigned symbol = 1;
547 unsigned bit;
548 CLzmaProb *probLit;
549 matchByte <<= 1;
550 bit = (matchByte & offs);
551 probLit = prob + offs + bit + symbol;
552 GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
554 while (symbol < 0x100);
556 res = DUMMY_LIT;
558 else
560 unsigned len;
561 UPDATE_1_CHECK;
563 prob = probs + IsRep + state;
564 IF_BIT_0_CHECK(prob)
566 UPDATE_0_CHECK;
567 state = 0;
568 prob = probs + LenCoder;
569 res = DUMMY_MATCH;
571 else
573 UPDATE_1_CHECK;
574 res = DUMMY_REP;
575 prob = probs + IsRepG0 + state;
576 IF_BIT_0_CHECK(prob)
578 UPDATE_0_CHECK;
579 prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
580 IF_BIT_0_CHECK(prob)
582 UPDATE_0_CHECK;
583 NORMALIZE_CHECK;
584 return DUMMY_REP;
586 else
588 UPDATE_1_CHECK;
591 else
593 UPDATE_1_CHECK;
594 prob = probs + IsRepG1 + state;
595 IF_BIT_0_CHECK(prob)
597 UPDATE_0_CHECK;
599 else
601 UPDATE_1_CHECK;
602 prob = probs + IsRepG2 + state;
603 IF_BIT_0_CHECK(prob)
605 UPDATE_0_CHECK;
607 else
609 UPDATE_1_CHECK;
613 state = kNumStates;
614 prob = probs + RepLenCoder;
617 unsigned limit, offset;
618 CLzmaProb *probLen = prob + LenChoice;
619 IF_BIT_0_CHECK(probLen)
621 UPDATE_0_CHECK;
622 probLen = prob + LenLow + (posState << kLenNumLowBits);
623 offset = 0;
624 limit = 1 << kLenNumLowBits;
626 else
628 UPDATE_1_CHECK;
629 probLen = prob + LenChoice2;
630 IF_BIT_0_CHECK(probLen)
632 UPDATE_0_CHECK;
633 probLen = prob + LenMid + (posState << kLenNumMidBits);
634 offset = kLenNumLowSymbols;
635 limit = 1 << kLenNumMidBits;
637 else
639 UPDATE_1_CHECK;
640 probLen = prob + LenHigh;
641 offset = kLenNumLowSymbols + kLenNumMidSymbols;
642 limit = 1 << kLenNumHighBits;
645 TREE_DECODE_CHECK(probLen, limit, len);
646 len += offset;
649 if (state < 4)
651 unsigned posSlot;
652 prob = probs + PosSlot +
653 ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
654 kNumPosSlotBits);
655 TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
656 if (posSlot >= kStartPosModelIndex)
658 int numDirectBits = ((posSlot >> 1) - 1);
660 /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
662 if (posSlot < kEndPosModelIndex)
664 prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
666 else
668 numDirectBits -= kNumAlignBits;
671 NORMALIZE_CHECK
672 range >>= 1;
673 code -= range & (((code - range) >> 31) - 1);
674 /* if (code >= range) code -= range; */
676 while (--numDirectBits != 0);
677 prob = probs + Align;
678 numDirectBits = kNumAlignBits;
681 unsigned i = 1;
684 GET_BIT_CHECK(prob + i, i);
686 while (--numDirectBits != 0);
692 NORMALIZE_CHECK;
693 return res;
697 static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
699 p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
700 p->range = 0xFFFFFFFF;
701 p->needFlush = 0;
704 void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
706 p->needFlush = 1;
707 p->remainLen = 0;
708 p->tempBufSize = 0;
710 if (initDic)
712 p->processedPos = 0;
713 p->checkDicSize = 0;
714 p->needInitState = 1;
716 if (initState)
717 p->needInitState = 1;
720 void LzmaDec_Init(CLzmaDec *p)
722 p->dicPos = 0;
723 LzmaDec_InitDicAndState(p, True, True);
726 static void LzmaDec_InitStateReal(CLzmaDec *p)
728 UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
729 UInt32 i;
730 CLzmaProb *probs = p->probs;
731 for (i = 0; i < numProbs; i++)
732 probs[i] = kBitModelTotal >> 1;
733 p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
734 p->state = 0;
735 p->needInitState = 0;
738 SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
739 ELzmaFinishMode finishMode, ELzmaStatus *status)
741 SizeT inSize = *srcLen;
742 (*srcLen) = 0;
743 LzmaDec_WriteRem(p, dicLimit);
745 *status = LZMA_STATUS_NOT_SPECIFIED;
747 while (p->remainLen != kMatchSpecLenStart)
749 int checkEndMarkNow;
751 if (p->needFlush != 0)
753 for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
754 p->tempBuf[p->tempBufSize++] = *src++;
755 if (p->tempBufSize < RC_INIT_SIZE)
757 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
758 return SZ_OK;
760 if (p->tempBuf[0] != 0)
761 return SZ_ERROR_DATA;
763 LzmaDec_InitRc(p, p->tempBuf);
764 p->tempBufSize = 0;
767 checkEndMarkNow = 0;
768 if (p->dicPos >= dicLimit)
770 if (p->remainLen == 0 && p->code == 0)
772 *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
773 return SZ_OK;
775 if (finishMode == LZMA_FINISH_ANY)
777 *status = LZMA_STATUS_NOT_FINISHED;
778 return SZ_OK;
780 if (p->remainLen != 0)
782 *status = LZMA_STATUS_NOT_FINISHED;
783 return SZ_ERROR_DATA;
785 checkEndMarkNow = 1;
788 if (p->needInitState)
789 LzmaDec_InitStateReal(p);
791 if (p->tempBufSize == 0)
793 SizeT processed;
794 const Byte *bufLimit;
795 if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
797 int dummyRes = LzmaDec_TryDummy(p, src, inSize);
798 if (dummyRes == DUMMY_ERROR)
800 (void) memcpy(p->tempBuf, src, inSize);
801 p->tempBufSize = (unsigned)inSize;
802 (*srcLen) += inSize;
803 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
804 return SZ_OK;
806 if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
808 *status = LZMA_STATUS_NOT_FINISHED;
809 return SZ_ERROR_DATA;
811 bufLimit = src;
813 else
814 bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
815 p->buf = src;
816 if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
817 return SZ_ERROR_DATA;
818 /*LINTED*/
819 processed = (SizeT)(p->buf - src);
820 (*srcLen) += processed;
821 src += processed;
822 inSize -= processed;
824 else
826 unsigned rem = p->tempBufSize, lookAhead = 0;
827 while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
828 p->tempBuf[rem++] = src[lookAhead++];
829 p->tempBufSize = rem;
830 if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
832 int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
833 if (dummyRes == DUMMY_ERROR)
835 (*srcLen) += lookAhead;
836 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
837 return SZ_OK;
839 if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
841 *status = LZMA_STATUS_NOT_FINISHED;
842 return SZ_ERROR_DATA;
845 p->buf = p->tempBuf;
846 if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
847 return SZ_ERROR_DATA;
848 /*LINTED*/
849 lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
850 (*srcLen) += lookAhead;
851 src += lookAhead;
852 inSize -= lookAhead;
853 p->tempBufSize = 0;
856 if (p->code == 0)
857 *status = LZMA_STATUS_FINISHED_WITH_MARK;
858 return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
861 SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
863 SizeT outSize = *destLen;
864 SizeT inSize = *srcLen;
865 *srcLen = *destLen = 0;
866 for (;;)
868 SizeT inSizeCur = inSize, outSizeCur, dicPos;
869 ELzmaFinishMode curFinishMode;
870 SRes res;
871 if (p->dicPos == p->dicBufSize)
872 p->dicPos = 0;
873 dicPos = p->dicPos;
874 if (outSize > p->dicBufSize - dicPos)
876 outSizeCur = p->dicBufSize;
877 curFinishMode = LZMA_FINISH_ANY;
879 else
881 outSizeCur = dicPos + outSize;
882 curFinishMode = finishMode;
885 res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
886 src += inSizeCur;
887 inSize -= inSizeCur;
888 *srcLen += inSizeCur;
889 outSizeCur = p->dicPos - dicPos;
890 (void) memcpy(dest, p->dic + dicPos, outSizeCur);
891 dest += outSizeCur;
892 outSize -= outSizeCur;
893 *destLen += outSizeCur;
894 if (res != 0)
895 return res;
896 if (outSizeCur == 0 || outSize == 0)
897 return SZ_OK;
901 void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
903 if (p->probs != 0)
904 alloc->Free(alloc, p->probs, (p->numProbs * sizeof (*p->probs)));
905 p->probs = 0;
908 static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
910 if (p->dic != 0)
911 alloc->Free(alloc, p->dic, ((p->prop).dicSize * sizeof (*p->dic)));
912 p->dic = 0;
915 void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
917 LzmaDec_FreeProbs(p, alloc);
918 LzmaDec_FreeDict(p, alloc);
921 SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
923 UInt32 dicSize;
924 Byte d;
926 if (size < LZMA_PROPS_SIZE)
927 return SZ_ERROR_UNSUPPORTED;
928 else
929 dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
931 if (dicSize < LZMA_DIC_MIN)
932 dicSize = LZMA_DIC_MIN;
933 p->dicSize = dicSize;
935 d = data[0];
936 if (d >= (9 * 5 * 5))
937 return SZ_ERROR_UNSUPPORTED;
939 p->lc = d % 9;
940 d /= 9;
941 p->pb = d / 5;
942 p->lp = d % 5;
944 return SZ_OK;
947 static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
949 UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
950 if (p->probs == 0 || numProbs != p->numProbs)
952 LzmaDec_FreeProbs(p, alloc);
953 p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
954 p->numProbs = numProbs;
955 if (p->probs == 0)
956 return SZ_ERROR_MEM;
958 return SZ_OK;
961 SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
963 CLzmaProps propNew;
964 RINOK(LzmaProps_Decode(&propNew, props, propsSize));
965 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
966 p->prop = propNew;
967 return SZ_OK;
970 SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
972 CLzmaProps propNew;
973 SizeT dicBufSize;
974 RINOK(LzmaProps_Decode(&propNew, props, propsSize));
975 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
976 dicBufSize = propNew.dicSize;
977 if (p->dic == 0 || dicBufSize != p->dicBufSize)
979 LzmaDec_FreeDict(p, alloc);
980 p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
981 if (p->dic == 0)
983 LzmaDec_FreeProbs(p, alloc);
984 return SZ_ERROR_MEM;
987 p->dicBufSize = dicBufSize;
988 p->prop = propNew;
989 return SZ_OK;
992 SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
993 const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
994 ELzmaStatus *status, ISzAlloc *alloc)
996 CLzmaDec p;
997 SRes res;
998 SizeT inSize = *srcLen;
999 SizeT outSize = *destLen;
1000 *srcLen = *destLen = 0;
1001 if (inSize < RC_INIT_SIZE)
1002 return SZ_ERROR_INPUT_EOF;
1004 LzmaDec_Construct(&p);
1005 res = LzmaDec_AllocateProbs(&p, propData, propSize, alloc);
1006 if (res != 0)
1007 return res;
1008 p.dic = dest;
1009 p.dicBufSize = outSize;
1011 LzmaDec_Init(&p);
1013 *srcLen = inSize;
1014 res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
1016 if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
1017 res = SZ_ERROR_INPUT_EOF;
1019 (*destLen) = p.dicPos;
1020 LzmaDec_FreeProbs(&p, alloc);
1021 return res;