Fix
[ryzomcore.git] / nel / 3rdparty / seven_zip / LzmaDec.c
blobba3e1dd50ef52940fb49d45a4d4992c1113410de
1 /* LzmaDec.c -- LZMA Decoder
2 2018-07-04 : Igor Pavlov : Public domain */
4 #include "Precomp.h"
6 #include <string.h>
8 /* #include "CpuArch.h" */
9 #include "LzmaDec.h"
11 #define kNumTopBits 24
12 #define kTopValue ((UInt32)1 << kNumTopBits)
14 #define kNumBitModelTotalBits 11
15 #define kBitModelTotal (1 << kNumBitModelTotalBits)
16 #define kNumMoveBits 5
18 #define RC_INIT_SIZE 5
20 #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
22 #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * (UInt32)ttt; if (code < bound)
23 #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
24 #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
25 #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
26 { UPDATE_0(p); i = (i + i); A0; } else \
27 { UPDATE_1(p); i = (i + i) + 1; A1; }
29 #define TREE_GET_BIT(probs, i) { GET_BIT2(probs + i, i, ;, ;); }
31 #define REV_BIT(p, i, A0, A1) IF_BIT_0(p + i) \
32 { UPDATE_0(p + i); A0; } else \
33 { UPDATE_1(p + i); A1; }
34 #define REV_BIT_VAR( p, i, m) REV_BIT(p, i, i += m; m += m, m += m; i += m; )
35 #define REV_BIT_CONST(p, i, m) REV_BIT(p, i, i += m; , i += m * 2; )
36 #define REV_BIT_LAST( p, i, m) REV_BIT(p, i, i -= m , ; )
38 #define TREE_DECODE(probs, limit, i) \
39 { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
41 /* #define _LZMA_SIZE_OPT */
43 #ifdef _LZMA_SIZE_OPT
44 #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
45 #else
46 #define TREE_6_DECODE(probs, i) \
47 { i = 1; \
48 TREE_GET_BIT(probs, i); \
49 TREE_GET_BIT(probs, i); \
50 TREE_GET_BIT(probs, i); \
51 TREE_GET_BIT(probs, i); \
52 TREE_GET_BIT(probs, i); \
53 TREE_GET_BIT(probs, i); \
54 i -= 0x40; }
55 #endif
57 #define NORMAL_LITER_DEC TREE_GET_BIT(prob, symbol)
58 #define MATCHED_LITER_DEC \
59 matchByte += matchByte; \
60 bit = offs; \
61 offs &= matchByte; \
62 probLit = prob + (offs + bit + symbol); \
63 GET_BIT2(probLit, symbol, offs ^= bit; , ;)
67 #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
69 #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * (UInt32)ttt; if (code < bound)
70 #define UPDATE_0_CHECK range = bound;
71 #define UPDATE_1_CHECK range -= bound; code -= bound;
72 #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
73 { UPDATE_0_CHECK; i = (i + i); A0; } else \
74 { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
75 #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
76 #define TREE_DECODE_CHECK(probs, limit, i) \
77 { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
80 #define REV_BIT_CHECK(p, i, m) IF_BIT_0_CHECK(p + i) \
81 { UPDATE_0_CHECK; i += m; m += m; } else \
82 { UPDATE_1_CHECK; m += m; i += m; }
85 #define kNumPosBitsMax 4
86 #define kNumPosStatesMax (1 << kNumPosBitsMax)
88 #define kLenNumLowBits 3
89 #define kLenNumLowSymbols (1 << kLenNumLowBits)
90 #define kLenNumHighBits 8
91 #define kLenNumHighSymbols (1 << kLenNumHighBits)
93 #define LenLow 0
94 #define LenHigh (LenLow + 2 * (kNumPosStatesMax << kLenNumLowBits))
95 #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
97 #define LenChoice LenLow
98 #define LenChoice2 (LenLow + (1 << kLenNumLowBits))
100 #define kNumStates 12
101 #define kNumStates2 16
102 #define kNumLitStates 7
104 #define kStartPosModelIndex 4
105 #define kEndPosModelIndex 14
106 #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
108 #define kNumPosSlotBits 6
109 #define kNumLenToPosStates 4
111 #define kNumAlignBits 4
112 #define kAlignTableSize (1 << kNumAlignBits)
114 #define kMatchMinLen 2
115 #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols * 2 + kLenNumHighSymbols)
117 /* External ASM code needs same CLzmaProb array layout. So don't change it. */
119 /* (probs_1664) is faster and better for code size at some platforms */
121 #ifdef MY_CPU_X86_OR_AMD64
123 #define kStartOffset 1664
124 #define GET_PROBS p->probs_1664
126 #define GET_PROBS p->probs + kStartOffset
127 #else
128 #define kStartOffset 0
129 #define GET_PROBS p->probs
130 #endif
133 #define SpecPos (-kStartOffset)
134 #define IsRep0Long (SpecPos + kNumFullDistances)
135 #define RepLenCoder (IsRep0Long + (kNumStates2 << kNumPosBitsMax))
136 #define LenCoder (RepLenCoder + kNumLenProbs)
137 #define IsMatch (LenCoder + kNumLenProbs)
138 #define Align (IsMatch + (kNumStates2 << kNumPosBitsMax))
139 #define IsRep (Align + kAlignTableSize)
140 #define IsRepG0 (IsRep + kNumStates)
141 #define IsRepG1 (IsRepG0 + kNumStates)
142 #define IsRepG2 (IsRepG1 + kNumStates)
143 #define PosSlot (IsRepG2 + kNumStates)
144 #define Literal (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
145 #define NUM_BASE_PROBS (Literal + kStartOffset)
147 #if Align != 0 && kStartOffset != 0
148 #error Stop_Compiling_Bad_LZMA_kAlign
149 #endif
151 #if NUM_BASE_PROBS != 1984
152 #error Stop_Compiling_Bad_LZMA_PROBS
153 #endif
156 #define LZMA_LIT_SIZE 0x300
158 #define LzmaProps_GetNumProbs(p) (NUM_BASE_PROBS + ((UInt32)LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
161 #define CALC_POS_STATE(processedPos, pbMask) (((processedPos) & (pbMask)) << 4)
162 #define COMBINED_PS_STATE (posState + state)
163 #define GET_LEN_STATE (posState)
165 #define LZMA_DIC_MIN (1 << 12)
168 p->remainLen : shows status of LZMA decoder:
169 < kMatchSpecLenStart : normal remain
170 = kMatchSpecLenStart : finished
171 = kMatchSpecLenStart + 1 : need init range coder
172 = kMatchSpecLenStart + 2 : need init range coder and state
175 /* ---------- LZMA_DECODE_REAL ---------- */
177 LzmaDec_DecodeReal_3() can be implemented in external ASM file.
178 3 - is the code compatibility version of that function for check at link time.
181 #define LZMA_DECODE_REAL LzmaDec_DecodeReal_3
184 LZMA_DECODE_REAL()
186 RangeCoder is normalized
187 if (p->dicPos == limit)
189 LzmaDec_TryDummy() was called before to exclude LITERAL and MATCH-REP cases.
190 So first symbol can be only MATCH-NON-REP. And if that MATCH-NON-REP symbol
191 is not END_OF_PAYALOAD_MARKER, then function returns error code.
194 Processing:
195 first LZMA symbol will be decoded in any case
196 All checks for limits are at the end of main loop,
197 It will decode new LZMA-symbols while (p->buf < bufLimit && dicPos < limit),
198 RangeCoder is still without last normalization when (p->buf < bufLimit) is being checked.
200 Out:
201 RangeCoder is normalized
202 Result:
203 SZ_OK - OK
204 SZ_ERROR_DATA - Error
205 p->remainLen:
206 < kMatchSpecLenStart : normal remain
207 = kMatchSpecLenStart : finished
211 #ifdef _LZMA_DEC_OPT
213 int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit);
215 #else
217 static
218 int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
220 CLzmaProb *probs = GET_PROBS;
221 unsigned state = (unsigned)p->state;
222 UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
223 unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
224 unsigned lc = p->prop.lc;
225 unsigned lpMask = ((unsigned)0x100 << p->prop.lp) - ((unsigned)0x100 >> lc);
227 Byte *dic = p->dic;
228 SizeT dicBufSize = p->dicBufSize;
229 SizeT dicPos = p->dicPos;
231 UInt32 processedPos = p->processedPos;
232 UInt32 checkDicSize = p->checkDicSize;
233 unsigned len = 0;
235 const Byte *buf = p->buf;
236 UInt32 range = p->range;
237 UInt32 code = p->code;
241 CLzmaProb *prob;
242 UInt32 bound;
243 unsigned ttt;
244 unsigned posState = CALC_POS_STATE(processedPos, pbMask);
246 prob = probs + IsMatch + COMBINED_PS_STATE;
247 IF_BIT_0(prob)
249 unsigned symbol;
250 UPDATE_0(prob);
251 prob = probs + Literal;
252 if (processedPos != 0 || checkDicSize != 0)
253 prob += (UInt32)3 * ((((processedPos << 8) + dic[(dicPos == 0 ? dicBufSize : dicPos) - 1]) & lpMask) << lc);
254 processedPos++;
256 if (state < kNumLitStates)
258 state -= (state < 4) ? state : 3;
259 symbol = 1;
260 #ifdef _LZMA_SIZE_OPT
261 do { NORMAL_LITER_DEC } while (symbol < 0x100);
262 #else
263 NORMAL_LITER_DEC
264 NORMAL_LITER_DEC
265 NORMAL_LITER_DEC
266 NORMAL_LITER_DEC
267 NORMAL_LITER_DEC
268 NORMAL_LITER_DEC
269 NORMAL_LITER_DEC
270 NORMAL_LITER_DEC
271 #endif
273 else
275 unsigned matchByte = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
276 unsigned offs = 0x100;
277 state -= (state < 10) ? 3 : 6;
278 symbol = 1;
279 #ifdef _LZMA_SIZE_OPT
282 unsigned bit;
283 CLzmaProb *probLit;
284 MATCHED_LITER_DEC
286 while (symbol < 0x100);
287 #else
289 unsigned bit;
290 CLzmaProb *probLit;
291 MATCHED_LITER_DEC
292 MATCHED_LITER_DEC
293 MATCHED_LITER_DEC
294 MATCHED_LITER_DEC
295 MATCHED_LITER_DEC
296 MATCHED_LITER_DEC
297 MATCHED_LITER_DEC
298 MATCHED_LITER_DEC
300 #endif
303 dic[dicPos++] = (Byte)symbol;
304 continue;
308 UPDATE_1(prob);
309 prob = probs + IsRep + state;
310 IF_BIT_0(prob)
312 UPDATE_0(prob);
313 state += kNumStates;
314 prob = probs + LenCoder;
316 else
318 UPDATE_1(prob);
320 // that case was checked before with kBadRepCode
321 if (checkDicSize == 0 && processedPos == 0)
322 return SZ_ERROR_DATA;
324 prob = probs + IsRepG0 + state;
325 IF_BIT_0(prob)
327 UPDATE_0(prob);
328 prob = probs + IsRep0Long + COMBINED_PS_STATE;
329 IF_BIT_0(prob)
331 UPDATE_0(prob);
332 dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
333 dicPos++;
334 processedPos++;
335 state = state < kNumLitStates ? 9 : 11;
336 continue;
338 UPDATE_1(prob);
340 else
342 UInt32 distance;
343 UPDATE_1(prob);
344 prob = probs + IsRepG1 + state;
345 IF_BIT_0(prob)
347 UPDATE_0(prob);
348 distance = rep1;
350 else
352 UPDATE_1(prob);
353 prob = probs + IsRepG2 + state;
354 IF_BIT_0(prob)
356 UPDATE_0(prob);
357 distance = rep2;
359 else
361 UPDATE_1(prob);
362 distance = rep3;
363 rep3 = rep2;
365 rep2 = rep1;
367 rep1 = rep0;
368 rep0 = distance;
370 state = state < kNumLitStates ? 8 : 11;
371 prob = probs + RepLenCoder;
374 #ifdef _LZMA_SIZE_OPT
376 unsigned lim, offset;
377 CLzmaProb *probLen = prob + LenChoice;
378 IF_BIT_0(probLen)
380 UPDATE_0(probLen);
381 probLen = prob + LenLow + GET_LEN_STATE;
382 offset = 0;
383 lim = (1 << kLenNumLowBits);
385 else
387 UPDATE_1(probLen);
388 probLen = prob + LenChoice2;
389 IF_BIT_0(probLen)
391 UPDATE_0(probLen);
392 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
393 offset = kLenNumLowSymbols;
394 lim = (1 << kLenNumLowBits);
396 else
398 UPDATE_1(probLen);
399 probLen = prob + LenHigh;
400 offset = kLenNumLowSymbols * 2;
401 lim = (1 << kLenNumHighBits);
404 TREE_DECODE(probLen, lim, len);
405 len += offset;
407 #else
409 CLzmaProb *probLen = prob + LenChoice;
410 IF_BIT_0(probLen)
412 UPDATE_0(probLen);
413 probLen = prob + LenLow + GET_LEN_STATE;
414 len = 1;
415 TREE_GET_BIT(probLen, len);
416 TREE_GET_BIT(probLen, len);
417 TREE_GET_BIT(probLen, len);
418 len -= 8;
420 else
422 UPDATE_1(probLen);
423 probLen = prob + LenChoice2;
424 IF_BIT_0(probLen)
426 UPDATE_0(probLen);
427 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
428 len = 1;
429 TREE_GET_BIT(probLen, len);
430 TREE_GET_BIT(probLen, len);
431 TREE_GET_BIT(probLen, len);
433 else
435 UPDATE_1(probLen);
436 probLen = prob + LenHigh;
437 TREE_DECODE(probLen, (1 << kLenNumHighBits), len);
438 len += kLenNumLowSymbols * 2;
442 #endif
444 if (state >= kNumStates)
446 UInt32 distance;
447 prob = probs + PosSlot +
448 ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
449 TREE_6_DECODE(prob, distance);
450 if (distance >= kStartPosModelIndex)
452 unsigned posSlot = (unsigned)distance;
453 unsigned numDirectBits = (unsigned)(((distance >> 1) - 1));
454 distance = (2 | (distance & 1));
455 if (posSlot < kEndPosModelIndex)
457 distance <<= numDirectBits;
458 prob = probs + SpecPos;
460 UInt32 m = 1;
461 distance++;
464 REV_BIT_VAR(prob, distance, m);
466 while (--numDirectBits);
467 distance -= m;
470 else
472 numDirectBits -= kNumAlignBits;
475 NORMALIZE
476 range >>= 1;
479 UInt32 t;
480 code -= range;
481 t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
482 distance = (distance << 1) + (t + 1);
483 code += range & t;
486 distance <<= 1;
487 if (code >= range)
489 code -= range;
490 distance |= 1;
494 while (--numDirectBits);
495 prob = probs + Align;
496 distance <<= kNumAlignBits;
498 unsigned i = 1;
499 REV_BIT_CONST(prob, i, 1);
500 REV_BIT_CONST(prob, i, 2);
501 REV_BIT_CONST(prob, i, 4);
502 REV_BIT_LAST (prob, i, 8);
503 distance |= i;
505 if (distance == (UInt32)0xFFFFFFFF)
507 len = kMatchSpecLenStart;
508 state -= kNumStates;
509 break;
514 rep3 = rep2;
515 rep2 = rep1;
516 rep1 = rep0;
517 rep0 = distance + 1;
518 state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
519 if (distance >= (checkDicSize == 0 ? processedPos: checkDicSize))
521 p->dicPos = dicPos;
522 return SZ_ERROR_DATA;
526 len += kMatchMinLen;
529 SizeT rem;
530 unsigned curLen;
531 SizeT pos;
533 if ((rem = limit - dicPos) == 0)
535 p->dicPos = dicPos;
536 return SZ_ERROR_DATA;
539 curLen = ((rem < len) ? (unsigned)rem : len);
540 pos = dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0);
542 processedPos += (UInt32)curLen;
544 len -= curLen;
545 if (curLen <= dicBufSize - pos)
547 Byte *dest = dic + dicPos;
548 ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
549 const Byte *lim = dest + curLen;
550 dicPos += (SizeT)curLen;
552 *(dest) = (Byte)*(dest + src);
553 while (++dest != lim);
555 else
559 dic[dicPos++] = dic[pos];
560 if (++pos == dicBufSize)
561 pos = 0;
563 while (--curLen != 0);
568 while (dicPos < limit && buf < bufLimit);
570 NORMALIZE;
572 p->buf = buf;
573 p->range = range;
574 p->code = code;
575 p->remainLen = (UInt32)len;
576 p->dicPos = dicPos;
577 p->processedPos = processedPos;
578 p->reps[0] = rep0;
579 p->reps[1] = rep1;
580 p->reps[2] = rep2;
581 p->reps[3] = rep3;
582 p->state = (UInt32)state;
584 return SZ_OK;
586 #endif
588 static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
590 if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
592 Byte *dic = p->dic;
593 SizeT dicPos = p->dicPos;
594 SizeT dicBufSize = p->dicBufSize;
595 unsigned len = (unsigned)p->remainLen;
596 SizeT rep0 = p->reps[0]; /* we use SizeT to avoid the BUG of VC14 for AMD64 */
597 SizeT rem = limit - dicPos;
598 if (rem < len)
599 len = (unsigned)(rem);
601 if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
602 p->checkDicSize = p->prop.dicSize;
604 p->processedPos += (UInt32)len;
605 p->remainLen -= (UInt32)len;
606 while (len != 0)
608 len--;
609 dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
610 dicPos++;
612 p->dicPos = dicPos;
617 #define kRange0 0xFFFFFFFF
618 #define kBound0 ((kRange0 >> kNumBitModelTotalBits) << (kNumBitModelTotalBits - 1))
619 #define kBadRepCode (kBound0 + (((kRange0 - kBound0) >> kNumBitModelTotalBits) << (kNumBitModelTotalBits - 1)))
620 #if kBadRepCode != (0xC0000000 - 0x400)
621 #error Stop_Compiling_Bad_LZMA_Check
622 #endif
624 static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
628 SizeT limit2 = limit;
629 if (p->checkDicSize == 0)
631 UInt32 rem = p->prop.dicSize - p->processedPos;
632 if (limit - p->dicPos > rem)
633 limit2 = p->dicPos + rem;
635 if (p->processedPos == 0)
636 if (p->code >= kBadRepCode)
637 return SZ_ERROR_DATA;
640 RINOK(LZMA_DECODE_REAL(p, limit2, bufLimit));
642 if (p->checkDicSize == 0 && p->processedPos >= p->prop.dicSize)
643 p->checkDicSize = p->prop.dicSize;
645 LzmaDec_WriteRem(p, limit);
647 while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
649 return 0;
652 typedef enum
654 DUMMY_ERROR, /* unexpected end of input stream */
655 DUMMY_LIT,
656 DUMMY_MATCH,
657 DUMMY_REP
658 } ELzmaDummy;
660 static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
662 UInt32 range = p->range;
663 UInt32 code = p->code;
664 const Byte *bufLimit = buf + inSize;
665 const CLzmaProb *probs = GET_PROBS;
666 unsigned state = (unsigned)p->state;
667 ELzmaDummy res;
670 const CLzmaProb *prob;
671 UInt32 bound;
672 unsigned ttt;
673 unsigned posState = CALC_POS_STATE(p->processedPos, (1 << p->prop.pb) - 1);
675 prob = probs + IsMatch + COMBINED_PS_STATE;
676 IF_BIT_0_CHECK(prob)
678 UPDATE_0_CHECK
680 /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
682 prob = probs + Literal;
683 if (p->checkDicSize != 0 || p->processedPos != 0)
684 prob += ((UInt32)LZMA_LIT_SIZE *
685 ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
686 (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
688 if (state < kNumLitStates)
690 unsigned symbol = 1;
691 do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
693 else
695 unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
696 (p->dicPos < p->reps[0] ? p->dicBufSize : 0)];
697 unsigned offs = 0x100;
698 unsigned symbol = 1;
701 unsigned bit;
702 const CLzmaProb *probLit;
703 matchByte += matchByte;
704 bit = offs;
705 offs &= matchByte;
706 probLit = prob + (offs + bit + symbol);
707 GET_BIT2_CHECK(probLit, symbol, offs ^= bit; , ; )
709 while (symbol < 0x100);
711 res = DUMMY_LIT;
713 else
715 unsigned len;
716 UPDATE_1_CHECK;
718 prob = probs + IsRep + state;
719 IF_BIT_0_CHECK(prob)
721 UPDATE_0_CHECK;
722 state = 0;
723 prob = probs + LenCoder;
724 res = DUMMY_MATCH;
726 else
728 UPDATE_1_CHECK;
729 res = DUMMY_REP;
730 prob = probs + IsRepG0 + state;
731 IF_BIT_0_CHECK(prob)
733 UPDATE_0_CHECK;
734 prob = probs + IsRep0Long + COMBINED_PS_STATE;
735 IF_BIT_0_CHECK(prob)
737 UPDATE_0_CHECK;
738 NORMALIZE_CHECK;
739 return DUMMY_REP;
741 else
743 UPDATE_1_CHECK;
746 else
748 UPDATE_1_CHECK;
749 prob = probs + IsRepG1 + state;
750 IF_BIT_0_CHECK(prob)
752 UPDATE_0_CHECK;
754 else
756 UPDATE_1_CHECK;
757 prob = probs + IsRepG2 + state;
758 IF_BIT_0_CHECK(prob)
760 UPDATE_0_CHECK;
762 else
764 UPDATE_1_CHECK;
768 state = kNumStates;
769 prob = probs + RepLenCoder;
772 unsigned limit, offset;
773 const CLzmaProb *probLen = prob + LenChoice;
774 IF_BIT_0_CHECK(probLen)
776 UPDATE_0_CHECK;
777 probLen = prob + LenLow + GET_LEN_STATE;
778 offset = 0;
779 limit = 1 << kLenNumLowBits;
781 else
783 UPDATE_1_CHECK;
784 probLen = prob + LenChoice2;
785 IF_BIT_0_CHECK(probLen)
787 UPDATE_0_CHECK;
788 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
789 offset = kLenNumLowSymbols;
790 limit = 1 << kLenNumLowBits;
792 else
794 UPDATE_1_CHECK;
795 probLen = prob + LenHigh;
796 offset = kLenNumLowSymbols * 2;
797 limit = 1 << kLenNumHighBits;
800 TREE_DECODE_CHECK(probLen, limit, len);
801 len += offset;
804 if (state < 4)
806 unsigned posSlot;
807 prob = probs + PosSlot +
808 ((len < kNumLenToPosStates - 1 ? len : kNumLenToPosStates - 1) <<
809 kNumPosSlotBits);
810 TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
811 if (posSlot >= kStartPosModelIndex)
813 unsigned numDirectBits = ((posSlot >> 1) - 1);
815 /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
817 if (posSlot < kEndPosModelIndex)
819 prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits);
821 else
823 numDirectBits -= kNumAlignBits;
826 NORMALIZE_CHECK
827 range >>= 1;
828 code -= range & (((code - range) >> 31) - 1);
829 /* if (code >= range) code -= range; */
831 while (--numDirectBits);
832 prob = probs + Align;
833 numDirectBits = kNumAlignBits;
836 unsigned i = 1;
837 unsigned m = 1;
840 REV_BIT_CHECK(prob, i, m);
842 while (--numDirectBits);
848 NORMALIZE_CHECK;
849 return res;
853 void LzmaDec_InitDicAndState(CLzmaDec *p, BoolInt initDic, BoolInt initState)
855 p->remainLen = kMatchSpecLenStart + 1;
856 p->tempBufSize = 0;
858 if (initDic)
860 p->processedPos = 0;
861 p->checkDicSize = 0;
862 p->remainLen = kMatchSpecLenStart + 2;
864 if (initState)
865 p->remainLen = kMatchSpecLenStart + 2;
868 void LzmaDec_Init(CLzmaDec *p)
870 p->dicPos = 0;
871 LzmaDec_InitDicAndState(p, True, True);
875 SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
876 ELzmaFinishMode finishMode, ELzmaStatus *status)
878 SizeT inSize = *srcLen;
879 (*srcLen) = 0;
881 *status = LZMA_STATUS_NOT_SPECIFIED;
883 if (p->remainLen > kMatchSpecLenStart)
885 for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
886 p->tempBuf[p->tempBufSize++] = *src++;
887 if (p->tempBufSize != 0 && p->tempBuf[0] != 0)
888 return SZ_ERROR_DATA;
889 if (p->tempBufSize < RC_INIT_SIZE)
891 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
892 return SZ_OK;
894 p->code =
895 ((UInt32)p->tempBuf[1] << 24)
896 | ((UInt32)p->tempBuf[2] << 16)
897 | ((UInt32)p->tempBuf[3] << 8)
898 | ((UInt32)p->tempBuf[4]);
899 p->range = 0xFFFFFFFF;
900 p->tempBufSize = 0;
902 if (p->remainLen > kMatchSpecLenStart + 1)
904 SizeT numProbs = LzmaProps_GetNumProbs(&p->prop);
905 SizeT i;
906 CLzmaProb *probs = p->probs;
907 for (i = 0; i < numProbs; i++)
908 probs[i] = kBitModelTotal >> 1;
909 p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
910 p->state = 0;
913 p->remainLen = 0;
916 LzmaDec_WriteRem(p, dicLimit);
918 while (p->remainLen != kMatchSpecLenStart)
920 int checkEndMarkNow = 0;
922 if (p->dicPos >= dicLimit)
924 if (p->remainLen == 0 && p->code == 0)
926 *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
927 return SZ_OK;
929 if (finishMode == LZMA_FINISH_ANY)
931 *status = LZMA_STATUS_NOT_FINISHED;
932 return SZ_OK;
934 if (p->remainLen != 0)
936 *status = LZMA_STATUS_NOT_FINISHED;
937 return SZ_ERROR_DATA;
939 checkEndMarkNow = 1;
942 if (p->tempBufSize == 0)
944 SizeT processed;
945 const Byte *bufLimit;
946 if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
948 int dummyRes = LzmaDec_TryDummy(p, src, inSize);
949 if (dummyRes == DUMMY_ERROR)
951 memcpy(p->tempBuf, src, inSize);
952 p->tempBufSize = (unsigned)inSize;
953 (*srcLen) += inSize;
954 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
955 return SZ_OK;
957 if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
959 *status = LZMA_STATUS_NOT_FINISHED;
960 return SZ_ERROR_DATA;
962 bufLimit = src;
964 else
965 bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
966 p->buf = src;
967 if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
968 return SZ_ERROR_DATA;
969 processed = (SizeT)(p->buf - src);
970 (*srcLen) += processed;
971 src += processed;
972 inSize -= processed;
974 else
976 unsigned rem = p->tempBufSize, lookAhead = 0;
977 while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
978 p->tempBuf[rem++] = src[lookAhead++];
979 p->tempBufSize = rem;
980 if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
982 int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, (SizeT)rem);
983 if (dummyRes == DUMMY_ERROR)
985 (*srcLen) += (SizeT)lookAhead;
986 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
987 return SZ_OK;
989 if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
991 *status = LZMA_STATUS_NOT_FINISHED;
992 return SZ_ERROR_DATA;
995 p->buf = p->tempBuf;
996 if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
997 return SZ_ERROR_DATA;
1000 unsigned kkk = (unsigned)(p->buf - p->tempBuf);
1001 if (rem < kkk)
1002 return SZ_ERROR_FAIL; /* some internal error */
1003 rem -= kkk;
1004 if (lookAhead < rem)
1005 return SZ_ERROR_FAIL; /* some internal error */
1006 lookAhead -= rem;
1008 (*srcLen) += (SizeT)lookAhead;
1009 src += lookAhead;
1010 inSize -= (SizeT)lookAhead;
1011 p->tempBufSize = 0;
1015 if (p->code != 0)
1016 return SZ_ERROR_DATA;
1017 *status = LZMA_STATUS_FINISHED_WITH_MARK;
1018 return SZ_OK;
1022 SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
1024 SizeT outSize = *destLen;
1025 SizeT inSize = *srcLen;
1026 *srcLen = *destLen = 0;
1027 for (;;)
1029 SizeT inSizeCur = inSize, outSizeCur, dicPos;
1030 ELzmaFinishMode curFinishMode;
1031 SRes res;
1032 if (p->dicPos == p->dicBufSize)
1033 p->dicPos = 0;
1034 dicPos = p->dicPos;
1035 if (outSize > p->dicBufSize - dicPos)
1037 outSizeCur = p->dicBufSize;
1038 curFinishMode = LZMA_FINISH_ANY;
1040 else
1042 outSizeCur = dicPos + outSize;
1043 curFinishMode = finishMode;
1046 res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
1047 src += inSizeCur;
1048 inSize -= inSizeCur;
1049 *srcLen += inSizeCur;
1050 outSizeCur = p->dicPos - dicPos;
1051 memcpy(dest, p->dic + dicPos, outSizeCur);
1052 dest += outSizeCur;
1053 outSize -= outSizeCur;
1054 *destLen += outSizeCur;
1055 if (res != 0)
1056 return res;
1057 if (outSizeCur == 0 || outSize == 0)
1058 return SZ_OK;
1062 void LzmaDec_FreeProbs(CLzmaDec *p, ISzAllocPtr alloc)
1064 ISzAlloc_Free(alloc, p->probs);
1065 p->probs = NULL;
1068 static void LzmaDec_FreeDict(CLzmaDec *p, ISzAllocPtr alloc)
1070 ISzAlloc_Free(alloc, p->dic);
1071 p->dic = NULL;
1074 void LzmaDec_Free(CLzmaDec *p, ISzAllocPtr alloc)
1076 LzmaDec_FreeProbs(p, alloc);
1077 LzmaDec_FreeDict(p, alloc);
1080 SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
1082 UInt32 dicSize;
1083 Byte d;
1085 if (size < LZMA_PROPS_SIZE)
1086 return SZ_ERROR_UNSUPPORTED;
1087 else
1088 dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
1090 if (dicSize < LZMA_DIC_MIN)
1091 dicSize = LZMA_DIC_MIN;
1092 p->dicSize = dicSize;
1094 d = data[0];
1095 if (d >= (9 * 5 * 5))
1096 return SZ_ERROR_UNSUPPORTED;
1098 p->lc = (Byte)(d % 9);
1099 d /= 9;
1100 p->pb = (Byte)(d / 5);
1101 p->lp = (Byte)(d % 5);
1103 return SZ_OK;
1106 static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAllocPtr alloc)
1108 UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
1109 if (!p->probs || numProbs != p->numProbs)
1111 LzmaDec_FreeProbs(p, alloc);
1112 p->probs = (CLzmaProb *)ISzAlloc_Alloc(alloc, numProbs * sizeof(CLzmaProb));
1113 if (!p->probs)
1114 return SZ_ERROR_MEM;
1115 p->probs_1664 = p->probs + 1664;
1116 p->numProbs = numProbs;
1118 return SZ_OK;
1121 SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc)
1123 CLzmaProps propNew;
1124 RINOK(LzmaProps_Decode(&propNew, props, propsSize));
1125 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
1126 p->prop = propNew;
1127 return SZ_OK;
1130 SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc)
1132 CLzmaProps propNew;
1133 SizeT dicBufSize;
1134 RINOK(LzmaProps_Decode(&propNew, props, propsSize));
1135 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
1138 UInt32 dictSize = propNew.dicSize;
1139 SizeT mask = ((UInt32)1 << 12) - 1;
1140 if (dictSize >= ((UInt32)1 << 30)) mask = ((UInt32)1 << 22) - 1;
1141 else if (dictSize >= ((UInt32)1 << 22)) mask = ((UInt32)1 << 20) - 1;;
1142 dicBufSize = ((SizeT)dictSize + mask) & ~mask;
1143 if (dicBufSize < dictSize)
1144 dicBufSize = dictSize;
1147 if (!p->dic || dicBufSize != p->dicBufSize)
1149 LzmaDec_FreeDict(p, alloc);
1150 p->dic = (Byte *)ISzAlloc_Alloc(alloc, dicBufSize);
1151 if (!p->dic)
1153 LzmaDec_FreeProbs(p, alloc);
1154 return SZ_ERROR_MEM;
1157 p->dicBufSize = dicBufSize;
1158 p->prop = propNew;
1159 return SZ_OK;
1162 SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
1163 const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
1164 ELzmaStatus *status, ISzAllocPtr alloc)
1166 CLzmaDec p;
1167 SRes res;
1168 SizeT outSize = *destLen, inSize = *srcLen;
1169 *destLen = *srcLen = 0;
1170 *status = LZMA_STATUS_NOT_SPECIFIED;
1171 if (inSize < RC_INIT_SIZE)
1172 return SZ_ERROR_INPUT_EOF;
1173 LzmaDec_Construct(&p);
1174 RINOK(LzmaDec_AllocateProbs(&p, propData, propSize, alloc));
1175 p.dic = dest;
1176 p.dicBufSize = outSize;
1177 LzmaDec_Init(&p);
1178 *srcLen = inSize;
1179 res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
1180 *destLen = p.dicPos;
1181 if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
1182 res = SZ_ERROR_INPUT_EOF;
1183 LzmaDec_FreeProbs(&p, alloc);
1184 return res;