Coverage Report

Created: 2024-07-27 06:13

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