blob: 99c318f82b469afe11c3fb718b12883f204f5aeb [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* LzmaDec.c -- LZMA Decoder
22009-09-20 : Igor Pavlov : Public domain */
3
4#include "linux/lzma/LzmaDec.h"
5
6//#include <string.h>
7
8#define kNumTopBits 24
9#define kTopValue ((UInt32)1 << kNumTopBits)
10
11#define kNumBitModelTotalBits 11
12#define kBitModelTotal (1 << kNumBitModelTotalBits)
13#define kNumMoveBits 5
14
15#define RC_INIT_SIZE 5
16
17#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
18
19#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
20#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
21#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
22#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
23 { UPDATE_0(p); i = (i + i); A0; } else \
24 { UPDATE_1(p); i = (i + i) + 1; A1; }
25
26#define TREE_GET_BIT(probs, i) { GET_BIT2(probs + i, i, ;, ;); }
27
28#define REV_BIT(p, i, A0, A1) IF_BIT_0(p + i) \
29 { UPDATE_0(p + i); A0; } else \
30 { UPDATE_1(p + i); A1; }
31#define REV_BIT_VAR( p, i, m) REV_BIT(p, i, i += m; m += m, m += m; i += m; )
32#define REV_BIT_CONST(p, i, m) REV_BIT(p, i, i += m; , i += m * 2; )
33#define REV_BIT_LAST( p, i, m) REV_BIT(p, i, i -= m , ; )
34
35#define TREE_DECODE(probs, limit, i) \
36 { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
37
38/* #define _LZMA_SIZE_OPT */
39
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
53
54#define NORMAL_LITER_DEC TREE_GET_BIT(prob, symbol)
55#define MATCHED_LITER_DEC \
56 matchByte += matchByte; \
57 bit = offs; \
58 offs &= matchByte; \
59 probLit = prob + (offs + bit + symbol); \
60 GET_BIT2(probLit, symbol, offs ^= bit; , ;)
61
62
63
64#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
65
66#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
67#define UPDATE_0_CHECK range = bound;
68#define UPDATE_1_CHECK range -= bound; code -= bound;
69#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
70 { UPDATE_0_CHECK; i = (i + i); A0; } else \
71 { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
72#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
73#define TREE_DECODE_CHECK(probs, limit, i) \
74 { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
75
76
77#define REV_BIT_CHECK(p, i, m) IF_BIT_0_CHECK(p + i) \
78 { UPDATE_0_CHECK; i += m; m += m; } else \
79 { UPDATE_1_CHECK; m += m; i += m; }
80
81
82#define kNumPosBitsMax 4
83#define kNumPosStatesMax (1 << kNumPosBitsMax)
84
85#define kLenNumLowBits 3
86#define kLenNumLowSymbols (1 << kLenNumLowBits)
87#define kLenNumHighBits 8
88#define kLenNumHighSymbols (1 << kLenNumHighBits)
89
90#define LenLow 0
91#define LenHigh (LenLow + 2 * (kNumPosStatesMax << kLenNumLowBits))
92#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
93
94#define LenChoice LenLow
95#define LenChoice2 (LenLow + (1 << kLenNumLowBits))
96
97#define kNumStates 12
98#define kNumStates2 16
99#define kNumLitStates 7
100
101#define kStartPosModelIndex 4
102#define kEndPosModelIndex 14
103#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
104
105#define kNumPosSlotBits 6
106#define kNumLenToPosStates 4
107
108#define kNumAlignBits 4
109#define kAlignTableSize (1 << kNumAlignBits)
110
111#define kMatchMinLen 2
112#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols * 2 + kLenNumHighSymbols)
113
114/* External ASM code needs same CLzmaProb array layout. So don't change it. */
115
116/* (probs_1664) is faster and better for code size at some platforms */
117/*
118#ifdef MY_CPU_X86_OR_AMD64
119*/
120#define kStartOffset 1664
121#define GET_PROBS p->probs_1664
122/*
123#define GET_PROBS p->probs + kStartOffset
124#else
125#define kStartOffset 0
126#define GET_PROBS p->probs
127#endif
128*/
129
130#define SpecPos (-kStartOffset)
131#define IsRep0Long (SpecPos + kNumFullDistances)
132#define RepLenCoder (IsRep0Long + (kNumStates2 << kNumPosBitsMax))
133#define LenCoder (RepLenCoder + kNumLenProbs)
134#define IsMatch (LenCoder + kNumLenProbs)
135#define Align (IsMatch + (kNumStates2 << kNumPosBitsMax))
136#define IsRep (Align + kAlignTableSize)
137#define IsRepG0 (IsRep + kNumStates)
138#define IsRepG1 (IsRepG0 + kNumStates)
139#define IsRepG2 (IsRepG1 + kNumStates)
140#define PosSlot (IsRepG2 + kNumStates)
141#define Literal (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
142#define NUM_BASE_PROBS (Literal + kStartOffset)
143
144#if Align != 0 && kStartOffset != 0
145 #error Stop_Compiling_Bad_LZMA_kAlign
146#endif
147
148#if NUM_BASE_PROBS != 1984
149 #error Stop_Compiling_Bad_LZMA_PROBS
150#endif
151
152
153#define LZMA_LIT_SIZE 0x300
154
155#define LzmaProps_GetNumProbs(p) (NUM_BASE_PROBS + ((UInt32)LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
156
157
158#define CALC_POS_STATE(processedPos, pbMask) (((processedPos) & (pbMask)) << 4)
159#define COMBINED_PS_STATE (posState + state)
160#define GET_LEN_STATE (posState)
161
162#define LZMA_DIC_MIN (1 << 12)
163
164/*
165p->remainLen : shows status of LZMA decoder:
166 < kMatchSpecLenStart : normal remain
167 = kMatchSpecLenStart : finished
168 = kMatchSpecLenStart + 1 : need init range coder
169 = kMatchSpecLenStart + 2 : need init range coder and state
170*/
171
172/* ---------- LZMA_DECODE_REAL ---------- */
173/*
174LzmaDec_DecodeReal_3() can be implemented in external ASM file.
1753 - is the code compatibility version of that function for check at link time.
176*/
177
178#define LZMA_DECODE_REAL LzmaDec_DecodeReal_3
179
180/*
181LZMA_DECODE_REAL()
182In:
183 RangeCoder is normalized
184 if (p->dicPos == limit)
185 {
186 LzmaDec_TryDummy() was called before to exclude LITERAL and MATCH-REP cases.
187 So first symbol can be only MATCH-NON-REP. And if that MATCH-NON-REP symbol
188 is not END_OF_PAYALOAD_MARKER, then function returns error code.
189 }
190
191Processing:
192 first LZMA symbol will be decoded in any case
193 All checks for limits are at the end of main loop,
194 It will decode new LZMA-symbols while (p->buf < bufLimit && dicPos < limit),
195 RangeCoder is still without last normalization when (p->buf < bufLimit) is being checked.
196
197Out:
198 RangeCoder is normalized
199 Result:
200 SZ_OK - OK
201 SZ_ERROR_DATA - Error
202 p->remainLen:
203 < kMatchSpecLenStart : normal remain
204 = kMatchSpecLenStart : finished
205*/
206
207
208#ifdef _LZMA_DEC_OPT
209
210int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit);
211
212#else
213
214static
215int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
216{
217 CLzmaProb *probs = GET_PROBS;
218 unsigned state = (unsigned)p->state;
219 UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
220 unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
221 unsigned lc = p->prop.lc;
222 unsigned lpMask = ((unsigned)0x100 << p->prop.lp) - ((unsigned)0x100 >> lc);
223
224 Byte *dic = p->dic;
225 SizeT dicBufSize = p->dicBufSize;
226 SizeT dicPos = p->dicPos;
227
228 UInt32 processedPos = p->processedPos;
229 UInt32 checkDicSize = p->checkDicSize;
230 unsigned len = 0;
231
232 const Byte *buf = p->buf;
233 UInt32 range = p->range;
234 UInt32 code = p->code;
235
236 do
237 {
238 CLzmaProb *prob;
239 UInt32 bound;
240 unsigned ttt;
241 unsigned posState = CALC_POS_STATE(processedPos, pbMask);
242
243 prob = probs + IsMatch + COMBINED_PS_STATE;
244 IF_BIT_0(prob)
245 {
246 unsigned symbol;
247 UPDATE_0(prob);
248 prob = probs + Literal;
249 if (processedPos != 0 || checkDicSize != 0)
250 prob += (UInt32)3 * ((((processedPos << 8) + dic[(dicPos == 0 ? dicBufSize : dicPos) - 1]) & lpMask) << lc);
251 processedPos++;
252
253 if (state < kNumLitStates)
254 {
255 state -= (state < 4) ? state : 3;
256 symbol = 1;
257 #ifdef _LZMA_SIZE_OPT
258 do { NORMAL_LITER_DEC } while (symbol < 0x100);
259 #else
260 NORMAL_LITER_DEC
261 NORMAL_LITER_DEC
262 NORMAL_LITER_DEC
263 NORMAL_LITER_DEC
264 NORMAL_LITER_DEC
265 NORMAL_LITER_DEC
266 NORMAL_LITER_DEC
267 NORMAL_LITER_DEC
268 #endif
269 }
270 else
271 {
272 unsigned matchByte = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
273 unsigned offs = 0x100;
274 state -= (state < 10) ? 3 : 6;
275 symbol = 1;
276 #ifdef _LZMA_SIZE_OPT
277 do
278 {
279 unsigned bit;
280 CLzmaProb *probLit;
281 MATCHED_LITER_DEC
282 }
283 while (symbol < 0x100);
284 #else
285 {
286 unsigned bit;
287 CLzmaProb *probLit;
288 MATCHED_LITER_DEC
289 MATCHED_LITER_DEC
290 MATCHED_LITER_DEC
291 MATCHED_LITER_DEC
292 MATCHED_LITER_DEC
293 MATCHED_LITER_DEC
294 MATCHED_LITER_DEC
295 MATCHED_LITER_DEC
296 }
297 #endif
298 }
299
300 dic[dicPos++] = (Byte)symbol;
301 continue;
302 }
303
304 {
305 UPDATE_1(prob);
306 prob = probs + IsRep + state;
307 IF_BIT_0(prob)
308 {
309 UPDATE_0(prob);
310 state += kNumStates;
311 prob = probs + LenCoder;
312 }
313 else
314 {
315 UPDATE_1(prob);
316 /*
317 // that case was checked before with kBadRepCode
318 if (checkDicSize == 0 && processedPos == 0)
319 return SZ_ERROR_DATA;
320 */
321 prob = probs + IsRepG0 + state;
322 IF_BIT_0(prob)
323 {
324 UPDATE_0(prob);
325 prob = probs + IsRep0Long + COMBINED_PS_STATE;
326 IF_BIT_0(prob)
327 {
328 UPDATE_0(prob);
329 dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
330 dicPos++;
331 processedPos++;
332 state = state < kNumLitStates ? 9 : 11;
333 continue;
334 }
335 UPDATE_1(prob);
336 }
337 else
338 {
339 UInt32 distance;
340 UPDATE_1(prob);
341 prob = probs + IsRepG1 + state;
342 IF_BIT_0(prob)
343 {
344 UPDATE_0(prob);
345 distance = rep1;
346 }
347 else
348 {
349 UPDATE_1(prob);
350 prob = probs + IsRepG2 + state;
351 IF_BIT_0(prob)
352 {
353 UPDATE_0(prob);
354 distance = rep2;
355 }
356 else
357 {
358 UPDATE_1(prob);
359 distance = rep3;
360 rep3 = rep2;
361 }
362 rep2 = rep1;
363 }
364 rep1 = rep0;
365 rep0 = distance;
366 }
367 state = state < kNumLitStates ? 8 : 11;
368 prob = probs + RepLenCoder;
369 }
370
371 #ifdef _LZMA_SIZE_OPT
372 {
373 unsigned lim, offset;
374 CLzmaProb *probLen = prob + LenChoice;
375 IF_BIT_0(probLen)
376 {
377 UPDATE_0(probLen);
378 probLen = prob + LenLow + GET_LEN_STATE;
379 offset = 0;
380 lim = (1 << kLenNumLowBits);
381 }
382 else
383 {
384 UPDATE_1(probLen);
385 probLen = prob + LenChoice2;
386 IF_BIT_0(probLen)
387 {
388 UPDATE_0(probLen);
389 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
390 offset = kLenNumLowSymbols;
391 lim = (1 << kLenNumLowBits);
392 }
393 else
394 {
395 UPDATE_1(probLen);
396 probLen = prob + LenHigh;
397 offset = kLenNumLowSymbols * 2;
398 lim = (1 << kLenNumHighBits);
399 }
400 }
401 TREE_DECODE(probLen, lim, len);
402 len += offset;
403 }
404 #else
405 {
406 CLzmaProb *probLen = prob + LenChoice;
407 IF_BIT_0(probLen)
408 {
409 UPDATE_0(probLen);
410 probLen = prob + LenLow + GET_LEN_STATE;
411 len = 1;
412 TREE_GET_BIT(probLen, len);
413 TREE_GET_BIT(probLen, len);
414 TREE_GET_BIT(probLen, len);
415 len -= 8;
416 }
417 else
418 {
419 UPDATE_1(probLen);
420 probLen = prob + LenChoice2;
421 IF_BIT_0(probLen)
422 {
423 UPDATE_0(probLen);
424 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
425 len = 1;
426 TREE_GET_BIT(probLen, len);
427 TREE_GET_BIT(probLen, len);
428 TREE_GET_BIT(probLen, len);
429 }
430 else
431 {
432 UPDATE_1(probLen);
433 probLen = prob + LenHigh;
434 TREE_DECODE(probLen, (1 << kLenNumHighBits), len);
435 len += kLenNumLowSymbols * 2;
436 }
437 }
438 }
439 #endif
440
441 if (state >= kNumStates)
442 {
443 UInt32 distance;
444 prob = probs + PosSlot +
445 ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
446 TREE_6_DECODE(prob, distance);
447 if (distance >= kStartPosModelIndex)
448 {
449 unsigned posSlot = (unsigned)distance;
450 unsigned numDirectBits = (unsigned)(((distance >> 1) - 1));
451 distance = (2 | (distance & 1));
452 if (posSlot < kEndPosModelIndex)
453 {
454 distance <<= numDirectBits;
455 prob = probs + SpecPos;
456 {
457 UInt32 m = 1;
458 distance++;
459 do
460 {
461 REV_BIT_VAR(prob, distance, m);
462 }
463 while (--numDirectBits);
464 distance -= m;
465 }
466 }
467 else
468 {
469 numDirectBits -= kNumAlignBits;
470 do
471 {
472 NORMALIZE
473 range >>= 1;
474
475 {
476 UInt32 t;
477 code -= range;
478 t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
479 distance = (distance << 1) + (t + 1);
480 code += range & t;
481 }
482 /*
483 distance <<= 1;
484 if (code >= range)
485 {
486 code -= range;
487 distance |= 1;
488 }
489 */
490 }
491 while (--numDirectBits);
492 prob = probs + Align;
493 distance <<= kNumAlignBits;
494 {
495 unsigned i = 1;
496 REV_BIT_CONST(prob, i, 1);
497 REV_BIT_CONST(prob, i, 2);
498 REV_BIT_CONST(prob, i, 4);
499 REV_BIT_LAST (prob, i, 8);
500 distance |= i;
501 }
502 if (distance == (UInt32)0xFFFFFFFF)
503 {
504 len = kMatchSpecLenStart;
505 state -= kNumStates;
506 break;
507 }
508 }
509 }
510
511 rep3 = rep2;
512 rep2 = rep1;
513 rep1 = rep0;
514 rep0 = distance + 1;
515 state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
516 if (distance >= (checkDicSize == 0 ? processedPos: checkDicSize))
517 {
518 p->dicPos = dicPos;
519 return SZ_ERROR_DATA;
520 }
521 }
522
523 len += kMatchMinLen;
524
525 {
526 SizeT rem;
527 unsigned curLen;
528 SizeT pos;
529
530 if ((rem = limit - dicPos) == 0)
531 {
532 p->dicPos = dicPos;
533 return SZ_ERROR_DATA;
534 }
535
536 curLen = ((rem < len) ? (unsigned)rem : len);
537 pos = dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0);
538
539 processedPos += curLen;
540
541 len -= curLen;
542 if (curLen <= dicBufSize - pos)
543 {
544 Byte *dest = dic + dicPos;
545 ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
546 const Byte *lim = dest + curLen;
547 dicPos += curLen;
548 do
549 *(dest) = (Byte)*(dest + src);
550 while (++dest != lim);
551 }
552 else
553 {
554 do
555 {
556 dic[dicPos++] = dic[pos];
557 if (++pos == dicBufSize)
558 pos = 0;
559 }
560 while (--curLen != 0);
561 }
562 }
563 }
564 }
565 while (dicPos < limit && buf < bufLimit);
566
567 NORMALIZE;
568
569 p->buf = buf;
570 p->range = range;
571 p->code = code;
572 p->remainLen = len;
573 p->dicPos = dicPos;
574 p->processedPos = processedPos;
575 p->reps[0] = rep0;
576 p->reps[1] = rep1;
577 p->reps[2] = rep2;
578 p->reps[3] = rep3;
579 p->state = state;
580
581 return SZ_OK;
582}
583#endif
584
585static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
586{
587 if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
588 {
589 Byte *dic = p->dic;
590 SizeT dicPos = p->dicPos;
591 SizeT dicBufSize = p->dicBufSize;
592 unsigned len = (unsigned)p->remainLen;
593 SizeT rep0 = p->reps[0]; /* we use SizeT to avoid the BUG of VC14 for AMD64 */
594 SizeT rem = limit - dicPos;
595 if (rem < len)
596 len = (unsigned)(rem);
597
598 if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
599 p->checkDicSize = p->prop.dicSize;
600
601 p->processedPos += len;
602 p->remainLen -= len;
603 while (len != 0)
604 {
605 len--;
606 dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
607 dicPos++;
608 }
609 p->dicPos = dicPos;
610 }
611}
612
613
614#define kRange0 0xFFFFFFFF
615#define kBound0 ((kRange0 >> kNumBitModelTotalBits) << (kNumBitModelTotalBits - 1))
616#define kBadRepCode (kBound0 + (((kRange0 - kBound0) >> kNumBitModelTotalBits) << (kNumBitModelTotalBits - 1)))
617#if kBadRepCode != (0xC0000000 - 0x400)
618 #error Stop_Compiling_Bad_LZMA_Check
619#endif
620
621static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
622{
623 do
624 {
625 SizeT limit2 = limit;
626 if (p->checkDicSize == 0)
627 {
628 UInt32 rem = p->prop.dicSize - p->processedPos;
629 if (limit - p->dicPos > rem)
630 limit2 = p->dicPos + rem;
631
632 if (p->processedPos == 0)
633 if (p->code >= kBadRepCode)
634 return SZ_ERROR_DATA;
635 }
636
637 RINOK(LZMA_DECODE_REAL(p, limit2, bufLimit));
638
639 if (p->checkDicSize == 0 && p->processedPos >= p->prop.dicSize)
640 p->checkDicSize = p->prop.dicSize;
641
642 LzmaDec_WriteRem(p, limit);
643 }
644 while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
645
646 return 0;
647}
648
649typedef enum
650{
651 DUMMY_ERROR, /* unexpected end of input stream */
652 DUMMY_LIT,
653 DUMMY_MATCH,
654 DUMMY_REP
655} ELzmaDummy;
656
657static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
658{
659 UInt32 range = p->range;
660 UInt32 code = p->code;
661 const Byte *bufLimit = buf + inSize;
662 const CLzmaProb *probs = GET_PROBS;
663 unsigned state = (unsigned)p->state;
664 ELzmaDummy res;
665
666 {
667 const CLzmaProb *prob;
668 UInt32 bound;
669 unsigned ttt;
670 unsigned posState = CALC_POS_STATE(p->processedPos, (1 << p->prop.pb) - 1);
671
672 prob = probs + IsMatch + COMBINED_PS_STATE;
673 IF_BIT_0_CHECK(prob)
674 {
675 UPDATE_0_CHECK
676
677 /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
678
679 prob = probs + Literal;
680 if (p->checkDicSize != 0 || p->processedPos != 0)
681 prob += ((UInt32)LZMA_LIT_SIZE *
682 ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
683 (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
684
685 if (state < kNumLitStates)
686 {
687 unsigned symbol = 1;
688 do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
689 }
690 else
691 {
692 unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
693 (p->dicPos < p->reps[0] ? p->dicBufSize : 0)];
694 unsigned offs = 0x100;
695 unsigned symbol = 1;
696 do
697 {
698 unsigned bit;
699 const CLzmaProb *probLit;
700 matchByte += matchByte;
701 bit = offs;
702 offs &= matchByte;
703 probLit = prob + (offs + bit + symbol);
704 GET_BIT2_CHECK(probLit, symbol, offs ^= bit; , ; )
705 }
706 while (symbol < 0x100);
707 }
708 res = DUMMY_LIT;
709 }
710 else
711 {
712 unsigned len;
713 UPDATE_1_CHECK;
714
715 prob = probs + IsRep + state;
716 IF_BIT_0_CHECK(prob)
717 {
718 UPDATE_0_CHECK;
719 state = 0;
720 prob = probs + LenCoder;
721 res = DUMMY_MATCH;
722 }
723 else
724 {
725 UPDATE_1_CHECK;
726 res = DUMMY_REP;
727 prob = probs + IsRepG0 + state;
728 IF_BIT_0_CHECK(prob)
729 {
730 UPDATE_0_CHECK;
731 prob = probs + IsRep0Long + COMBINED_PS_STATE;
732 IF_BIT_0_CHECK(prob)
733 {
734 UPDATE_0_CHECK;
735 NORMALIZE_CHECK;
736 return DUMMY_REP;
737 }
738 else
739 {
740 UPDATE_1_CHECK;
741 }
742 }
743 else
744 {
745 UPDATE_1_CHECK;
746 prob = probs + IsRepG1 + state;
747 IF_BIT_0_CHECK(prob)
748 {
749 UPDATE_0_CHECK;
750 }
751 else
752 {
753 UPDATE_1_CHECK;
754 prob = probs + IsRepG2 + state;
755 IF_BIT_0_CHECK(prob)
756 {
757 UPDATE_0_CHECK;
758 }
759 else
760 {
761 UPDATE_1_CHECK;
762 }
763 }
764 }
765 state = kNumStates;
766 prob = probs + RepLenCoder;
767 }
768 {
769 unsigned limit, offset;
770 const CLzmaProb *probLen = prob + LenChoice;
771 IF_BIT_0_CHECK(probLen)
772 {
773 UPDATE_0_CHECK;
774 probLen = prob + LenLow + GET_LEN_STATE;
775 offset = 0;
776 limit = 1 << kLenNumLowBits;
777 }
778 else
779 {
780 UPDATE_1_CHECK;
781 probLen = prob + LenChoice2;
782 IF_BIT_0_CHECK(probLen)
783 {
784 UPDATE_0_CHECK;
785 probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
786 offset = kLenNumLowSymbols;
787 limit = 1 << kLenNumLowBits;
788 }
789 else
790 {
791 UPDATE_1_CHECK;
792 probLen = prob + LenHigh;
793 offset = kLenNumLowSymbols * 2;
794 limit = 1 << kLenNumHighBits;
795 }
796 }
797 TREE_DECODE_CHECK(probLen, limit, len);
798 len += offset;
799 }
800
801 if (state < 4)
802 {
803 unsigned posSlot;
804 prob = probs + PosSlot +
805 ((len < kNumLenToPosStates - 1 ? len : kNumLenToPosStates - 1) <<
806 kNumPosSlotBits);
807 TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
808 if (posSlot >= kStartPosModelIndex)
809 {
810 unsigned numDirectBits = ((posSlot >> 1) - 1);
811
812 /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
813
814 if (posSlot < kEndPosModelIndex)
815 {
816 prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits);
817 }
818 else
819 {
820 numDirectBits -= kNumAlignBits;
821 do
822 {
823 NORMALIZE_CHECK
824 range >>= 1;
825 code -= range & (((code - range) >> 31) - 1);
826 /* if (code >= range) code -= range; */
827 }
828 while (--numDirectBits);
829 prob = probs + Align;
830 numDirectBits = kNumAlignBits;
831 }
832 {
833 unsigned i = 1;
834 unsigned m = 1;
835 do
836 {
837 REV_BIT_CHECK(prob, i, m);
838 }
839 while (--numDirectBits);
840 }
841 }
842 }
843 }
844 }
845 NORMALIZE_CHECK;
846 return res;
847}
848
849
850void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
851{
852 p->remainLen = kMatchSpecLenStart + 1;
853 p->tempBufSize = 0;
854
855 if (initDic)
856 {
857 p->processedPos = 0;
858 p->checkDicSize = 0;
859 p->remainLen = kMatchSpecLenStart + 2;
860 }
861 if (initState)
862 p->remainLen = kMatchSpecLenStart + 2;
863}
864
865void LzmaDec_Init(CLzmaDec *p)
866{
867 p->dicPos = 0;
868 LzmaDec_InitDicAndState(p, True, True);
869}
870
871
872SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
873 ELzmaFinishMode finishMode, ELzmaStatus *status)
874{
875 SizeT inSize = *srcLen;
876 (*srcLen) = 0;
877
878 *status = LZMA_STATUS_NOT_SPECIFIED;
879
880 if (p->remainLen > kMatchSpecLenStart)
881 {
882 for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
883 p->tempBuf[p->tempBufSize++] = *src++;
884 if (p->tempBufSize != 0 && p->tempBuf[0] != 0)
885 return SZ_ERROR_DATA;
886 if (p->tempBufSize < RC_INIT_SIZE)
887 {
888 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
889 return SZ_OK;
890 }
891 p->code =
892 ((UInt32)p->tempBuf[1] << 24)
893 | ((UInt32)p->tempBuf[2] << 16)
894 | ((UInt32)p->tempBuf[3] << 8)
895 | ((UInt32)p->tempBuf[4]);
896 p->range = 0xFFFFFFFF;
897 p->tempBufSize = 0;
898
899 if (p->remainLen > kMatchSpecLenStart + 1)
900 {
901 SizeT numProbs = LzmaProps_GetNumProbs(&p->prop);
902 SizeT i;
903 CLzmaProb *probs = p->probs;
904 for (i = 0; i < numProbs; i++)
905 probs[i] = kBitModelTotal >> 1;
906 p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
907 p->state = 0;
908 }
909
910 p->remainLen = 0;
911 }
912
913 LzmaDec_WriteRem(p, dicLimit);
914
915 while (p->remainLen != kMatchSpecLenStart)
916 {
917 int checkEndMarkNow = 0;
918
919 if (p->dicPos >= dicLimit)
920 {
921 if (p->remainLen == 0 && p->code == 0)
922 {
923 *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
924 return SZ_OK;
925 }
926 if (finishMode == LZMA_FINISH_ANY)
927 {
928 *status = LZMA_STATUS_NOT_FINISHED;
929 return SZ_OK;
930 }
931 if (p->remainLen != 0)
932 {
933 *status = LZMA_STATUS_NOT_FINISHED;
934 return SZ_ERROR_DATA;
935 }
936 checkEndMarkNow = 1;
937 }
938
939 if (p->tempBufSize == 0)
940 {
941 SizeT processed;
942 const Byte *bufLimit;
943 if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
944 {
945 int dummyRes = LzmaDec_TryDummy(p, src, inSize);
946 if (dummyRes == DUMMY_ERROR)
947 {
948 memcpy(p->tempBuf, src, inSize);
949 p->tempBufSize = (unsigned)inSize;
950 (*srcLen) += inSize;
951 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
952 return SZ_OK;
953 }
954 if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
955 {
956 *status = LZMA_STATUS_NOT_FINISHED;
957 return SZ_ERROR_DATA;
958 }
959 bufLimit = src;
960 }
961 else
962 bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
963 p->buf = src;
964 if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
965 return SZ_ERROR_DATA;
966 processed = (SizeT)(p->buf - src);
967 (*srcLen) += processed;
968 src += processed;
969 inSize -= processed;
970 }
971 else
972 {
973 unsigned rem = p->tempBufSize, lookAhead = 0;
974 while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
975 p->tempBuf[rem++] = src[lookAhead++];
976 p->tempBufSize = rem;
977 if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
978 {
979 int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
980 if (dummyRes == DUMMY_ERROR)
981 {
982 (*srcLen) += lookAhead;
983 *status = LZMA_STATUS_NEEDS_MORE_INPUT;
984 return SZ_OK;
985 }
986 if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
987 {
988 *status = LZMA_STATUS_NOT_FINISHED;
989 return SZ_ERROR_DATA;
990 }
991 }
992 p->buf = p->tempBuf;
993 if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
994 return SZ_ERROR_DATA;
995
996 {
997 unsigned kkk = (unsigned)(p->buf - p->tempBuf);
998 if (rem < kkk)
999 return SZ_ERROR_FAIL; /* some internal error */
1000 rem -= kkk;
1001 if (lookAhead < rem)
1002 return SZ_ERROR_FAIL; /* some internal error */
1003 lookAhead -= rem;
1004 }
1005 (*srcLen) += lookAhead;
1006 src += lookAhead;
1007 inSize -= lookAhead;
1008 p->tempBufSize = 0;
1009 }
1010 }
1011
1012 if (p->code != 0)
1013 return SZ_ERROR_DATA;
1014 *status = LZMA_STATUS_FINISHED_WITH_MARK;
1015 return SZ_OK;
1016}
1017
1018
1019SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
1020{
1021 SizeT outSize = *destLen;
1022 SizeT inSize = *srcLen;
1023 *srcLen = *destLen = 0;
1024 for (;;)
1025 {
1026 SizeT inSizeCur = inSize, outSizeCur, dicPos;
1027 ELzmaFinishMode curFinishMode;
1028 SRes res;
1029 if (p->dicPos == p->dicBufSize)
1030 p->dicPos = 0;
1031 dicPos = p->dicPos;
1032 if (outSize > p->dicBufSize - dicPos)
1033 {
1034 outSizeCur = p->dicBufSize;
1035 curFinishMode = LZMA_FINISH_ANY;
1036 }
1037 else
1038 {
1039 outSizeCur = dicPos + outSize;
1040 curFinishMode = finishMode;
1041 }
1042
1043 res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
1044 src += inSizeCur;
1045 inSize -= inSizeCur;
1046 *srcLen += inSizeCur;
1047 outSizeCur = p->dicPos - dicPos;
1048 memcpy(dest, p->dic + dicPos, outSizeCur);
1049 dest += outSizeCur;
1050 outSize -= outSizeCur;
1051 *destLen += outSizeCur;
1052 if (res != 0)
1053 return res;
1054 if (outSizeCur == 0 || outSize == 0)
1055 return SZ_OK;
1056 }
1057}
1058
1059void LzmaDec_FreeProbs(CLzmaDec *p, ISzAllocPtr alloc)
1060{
1061 alloc->Free(alloc, p->probs);
1062 p->probs = NULL;
1063}
1064
1065static void LzmaDec_FreeDict(CLzmaDec *p, ISzAllocPtr alloc)
1066{
1067 alloc->Free(alloc, p->dic);
1068 p->dic = NULL;
1069}
1070
1071void LzmaDec_Free(CLzmaDec *p, ISzAllocPtr alloc)
1072{
1073 LzmaDec_FreeProbs(p, alloc);
1074 LzmaDec_FreeDict(p, alloc);
1075}
1076
1077SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
1078{
1079 UInt32 dicSize;
1080 Byte d;
1081
1082 if (size < LZMA_PROPS_SIZE)
1083 return SZ_ERROR_UNSUPPORTED;
1084 else
1085 dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
1086
1087 if (dicSize < LZMA_DIC_MIN)
1088 dicSize = LZMA_DIC_MIN;
1089 p->dicSize = dicSize;
1090
1091 d = data[0];
1092 if (d >= (9 * 5 * 5))
1093 return SZ_ERROR_UNSUPPORTED;
1094
1095 p->lc = (Byte)(d % 9);
1096 d /= 9;
1097 p->pb = (Byte)(d / 5);
1098 p->lp = (Byte)(d % 5);
1099
1100 return SZ_OK;
1101}
1102
1103static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAllocPtr alloc)
1104{
1105 UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
1106 if (!p->probs || numProbs != p->numProbs)
1107 {
1108 LzmaDec_FreeProbs(p, alloc);
1109 p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
1110 if (!p->probs)
1111 return SZ_ERROR_MEM;
1112 p->probs_1664 = p->probs + 1664;
1113 p->numProbs = numProbs;
1114 }
1115 return SZ_OK;
1116}
1117
1118SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc)
1119{
1120 CLzmaProps propNew;
1121 RINOK(LzmaProps_Decode(&propNew, props, propsSize));
1122 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
1123 p->prop = propNew;
1124 return SZ_OK;
1125}
1126
1127SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc)
1128{
1129 CLzmaProps propNew;
1130 SizeT dicBufSize;
1131 RINOK(LzmaProps_Decode(&propNew, props, propsSize));
1132 RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
1133
1134 {
1135 UInt32 dictSize = propNew.dicSize;
1136 SizeT mask = ((UInt32)1 << 12) - 1;
1137 if (dictSize >= ((UInt32)1 << 30)) mask = ((UInt32)1 << 22) - 1;
1138 else if (dictSize >= ((UInt32)1 << 22)) mask = ((UInt32)1 << 20) - 1;;
1139 dicBufSize = ((SizeT)dictSize + mask) & ~mask;
1140 if (dicBufSize < dictSize)
1141 dicBufSize = dictSize;
1142 }
1143
1144 if (!p->dic || dicBufSize != p->dicBufSize)
1145 {
1146 LzmaDec_FreeDict(p, alloc);
1147 p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
1148 if (!p->dic)
1149 {
1150 LzmaDec_FreeProbs(p, alloc);
1151 return SZ_ERROR_MEM;
1152 }
1153 }
1154 p->dicBufSize = dicBufSize;
1155 p->prop = propNew;
1156 return SZ_OK;
1157}
1158
1159SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
1160 const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
1161 ELzmaStatus *status, ISzAllocPtr alloc)
1162{
1163 CLzmaDec p;
1164 SRes res;
1165 SizeT outSize = *destLen, inSize = *srcLen;
1166 *destLen = *srcLen = 0;
1167 *status = LZMA_STATUS_NOT_SPECIFIED;
1168 if (inSize < RC_INIT_SIZE)
1169 return SZ_ERROR_INPUT_EOF;
1170 LzmaDec_Construct(&p);
1171 RINOK(LzmaDec_AllocateProbs(&p, propData, propSize, alloc));
1172 p.dic = dest;
1173 p.dicBufSize = outSize;
1174 LzmaDec_Init(&p);
1175 *srcLen = inSize;
1176 res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
1177 *destLen = p.dicPos;
1178 if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
1179 res = SZ_ERROR_INPUT_EOF;
1180 LzmaDec_FreeProbs(&p, alloc);
1181 return res;
1182}