Coverage Report

Created: 2024-07-27 06:12

/src/lzma-fuzz/sdk/C/Lzma2Dec.c
Line
Count
Source (jump to first uncovered line)
1
/* Lzma2Dec.c -- LZMA2 Decoder
2
2019-02-02 : Igor Pavlov : Public domain */
3
4
/* #define SHOW_DEBUG_INFO */
5
6
#include "Precomp.h"
7
8
#ifdef SHOW_DEBUG_INFO
9
#include <stdio.h>
10
#endif
11
12
#include <string.h>
13
14
#include "Lzma2Dec.h"
15
16
/*
17
00000000  -  End of data
18
00000001 U U  -  Uncompressed, reset dic, need reset state and set new prop
19
00000010 U U  -  Uncompressed, no reset
20
100uuuuu U U P P  -  LZMA, no reset
21
101uuuuu U U P P  -  LZMA, reset state
22
110uuuuu U U P P S  -  LZMA, reset state + set new prop
23
111uuuuu U U P P S  -  LZMA, reset state + set new prop, reset dic
24
25
  u, U - Unpack Size
26
  P - Pack Size
27
  S - Props
28
*/
29
30
990
#define LZMA2_CONTROL_COPY_RESET_DIC 1
31
32
3.70k
#define LZMA2_IS_UNCOMPRESSED_STATE(p) (((p)->control & (1 << 7)) == 0)
33
34
989
#define LZMA2_LCLP_MAX 4
35
984
#define LZMA2_DIC_SIZE_FROM_PROP(p) (((UInt32)2 | ((p) & 1)) << ((p) / 2 + 11))
36
37
#ifdef SHOW_DEBUG_INFO
38
#define PRF(x) x
39
#else
40
#define PRF(x)
41
#endif
42
43
typedef enum
44
{
45
  LZMA2_STATE_CONTROL,
46
  LZMA2_STATE_UNPACK0,
47
  LZMA2_STATE_UNPACK1,
48
  LZMA2_STATE_PACK0,
49
  LZMA2_STATE_PACK1,
50
  LZMA2_STATE_PROP,
51
  LZMA2_STATE_DATA,
52
  LZMA2_STATE_DATA_CONT,
53
  LZMA2_STATE_FINISHED,
54
  LZMA2_STATE_ERROR
55
} ELzma2State;
56
57
static SRes Lzma2Dec_GetOldProps(Byte prop, Byte *props)
58
492
{
59
492
  UInt32 dicSize;
60
492
  if (prop > 40)
61
0
    return SZ_ERROR_UNSUPPORTED;
62
492
  dicSize = (prop == 40) ? 0xFFFFFFFF : LZMA2_DIC_SIZE_FROM_PROP(prop);
63
492
  props[0] = (Byte)LZMA2_LCLP_MAX;
64
492
  props[1] = (Byte)(dicSize);
65
492
  props[2] = (Byte)(dicSize >> 8);
66
492
  props[3] = (Byte)(dicSize >> 16);
67
492
  props[4] = (Byte)(dicSize >> 24);
68
492
  return SZ_OK;
69
492
}
70
71
SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc)
72
492
{
73
492
  Byte props[LZMA_PROPS_SIZE];
74
492
  RINOK(Lzma2Dec_GetOldProps(prop, props));
75
492
  return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
76
492
}
77
78
SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc)
79
0
{
80
0
  Byte props[LZMA_PROPS_SIZE];
81
0
  RINOK(Lzma2Dec_GetOldProps(prop, props));
82
0
  return LzmaDec_Allocate(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
83
0
}
84
85
void Lzma2Dec_Init(CLzma2Dec *p)
86
492
{
87
492
  p->state = LZMA2_STATE_CONTROL;
88
492
  p->needInitLevel = 0xE0;
89
492
  p->isExtraMode = False;
90
492
  p->unpackSize = 0;
91
  
92
  // p->decoder.dicPos = 0; // we can use it instead of full init
93
492
  LzmaDec_Init(&p->decoder);
94
492
}
95
96
static ELzma2State Lzma2Dec_UpdateState(CLzma2Dec *p, Byte b)
97
4.88k
{
98
4.88k
  switch (p->state)
99
4.88k
  {
100
1.21k
    case LZMA2_STATE_CONTROL:
101
1.21k
      p->isExtraMode = False;
102
1.21k
      p->control = b;
103
1.21k
      PRF(printf("\n %8X", (unsigned)p->decoder.dicPos));
104
1.21k
      PRF(printf(" %02X", (unsigned)b));
105
1.21k
      if (b == 0)
106
157
        return LZMA2_STATE_FINISHED;
107
1.06k
      if (LZMA2_IS_UNCOMPRESSED_STATE(p))
108
502
      {
109
502
        if (b == LZMA2_CONTROL_COPY_RESET_DIC)
110
260
          p->needInitLevel = 0xC0;
111
242
        else if (b > 2 || p->needInitLevel == 0xE0)
112
11
          return LZMA2_STATE_ERROR;
113
502
      }
114
560
      else
115
560
      {
116
560
        if (b < p->needInitLevel)
117
8
          return LZMA2_STATE_ERROR;
118
552
        p->needInitLevel = 0;
119
552
        p->unpackSize = (UInt32)(b & 0x1F) << 16;
120
552
      }
121
1.04k
      return LZMA2_STATE_UNPACK0;
122
    
123
1.03k
    case LZMA2_STATE_UNPACK0:
124
1.03k
      p->unpackSize |= (UInt32)b << 8;
125
1.03k
      return LZMA2_STATE_UNPACK1;
126
    
127
1.03k
    case LZMA2_STATE_UNPACK1:
128
1.03k
      p->unpackSize |= (UInt32)b;
129
1.03k
      p->unpackSize++;
130
1.03k
      PRF(printf(" %7u", (unsigned)p->unpackSize));
131
1.03k
      return LZMA2_IS_UNCOMPRESSED_STATE(p) ? LZMA2_STATE_DATA : LZMA2_STATE_PACK0;
132
    
133
547
    case LZMA2_STATE_PACK0:
134
547
      p->packSize = (UInt32)b << 8;
135
547
      return LZMA2_STATE_PACK1;
136
137
547
    case LZMA2_STATE_PACK1:
138
547
      p->packSize |= (UInt32)b;
139
547
      p->packSize++;
140
      // if (p->packSize < 5) return LZMA2_STATE_ERROR;
141
547
      PRF(printf(" %5u", (unsigned)p->packSize));
142
547
      return (p->control & 0x40) ? LZMA2_STATE_PROP : LZMA2_STATE_DATA;
143
144
500
    case LZMA2_STATE_PROP:
145
500
    {
146
500
      unsigned lc, lp;
147
500
      if (b >= (9 * 5 * 5))
148
3
        return LZMA2_STATE_ERROR;
149
497
      lc = b % 9;
150
497
      b /= 9;
151
497
      p->decoder.prop.pb = (Byte)(b / 5);
152
497
      lp = b % 5;
153
497
      if (lc + lp > LZMA2_LCLP_MAX)
154
6
        return LZMA2_STATE_ERROR;
155
491
      p->decoder.prop.lc = (Byte)lc;
156
491
      p->decoder.prop.lp = (Byte)lp;
157
491
      return LZMA2_STATE_DATA;
158
497
    }
159
4.88k
  }
160
0
  return LZMA2_STATE_ERROR;
161
4.88k
}
162
163
static void LzmaDec_UpdateWithUncompressed(CLzmaDec *p, const Byte *src, SizeT size)
164
488
{
165
488
  memcpy(p->dic + p->dicPos, src, size);
166
488
  p->dicPos += size;
167
488
  if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= size)
168
0
    p->checkDicSize = p->prop.dicSize;
169
488
  p->processedPos += (UInt32)size;
170
488
}
171
172
void LzmaDec_InitDicAndState(CLzmaDec *p, BoolInt initDic, BoolInt initState);
173
174
175
SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit,
176
    const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
177
651
{
178
651
  SizeT inSize = *srcLen;
179
651
  *srcLen = 0;
180
651
  *status = LZMA_STATUS_NOT_SPECIFIED;
181
182
6.69k
  while (p->state != LZMA2_STATE_ERROR)
183
6.67k
  {
184
6.67k
    SizeT dicPos;
185
186
6.67k
    if (p->state == LZMA2_STATE_FINISHED)
187
157
    {
188
157
      *status = LZMA_STATUS_FINISHED_WITH_MARK;
189
157
      return SZ_OK;
190
157
    }
191
    
192
6.51k
    dicPos = p->decoder.dicPos;
193
    
194
6.51k
    if (dicPos == dicLimit && finishMode == LZMA_FINISH_ANY)
195
0
    {
196
0
      *status = LZMA_STATUS_NOT_FINISHED;
197
0
      return SZ_OK;
198
0
    }
199
200
6.51k
    if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT)
201
4.90k
    {
202
4.90k
      if (*srcLen == inSize)
203
16
      {
204
16
        *status = LZMA_STATUS_NEEDS_MORE_INPUT;
205
16
        return SZ_OK;
206
16
      }
207
4.88k
      (*srcLen)++;
208
4.88k
      p->state = Lzma2Dec_UpdateState(p, *src++);
209
4.88k
      if (dicPos == dicLimit && p->state != LZMA2_STATE_FINISHED)
210
4
        break;
211
4.88k
      continue;
212
4.88k
    }
213
    
214
1.60k
    {
215
1.60k
      SizeT inCur = inSize - *srcLen;
216
1.60k
      SizeT outCur = dicLimit - dicPos;
217
1.60k
      ELzmaFinishMode curFinishMode = LZMA_FINISH_ANY;
218
      
219
1.60k
      if (outCur >= p->unpackSize)
220
1.14k
      {
221
1.14k
        outCur = (SizeT)p->unpackSize;
222
1.14k
        curFinishMode = LZMA_FINISH_END;
223
1.14k
      }
224
225
1.60k
      if (LZMA2_IS_UNCOMPRESSED_STATE(p))
226
601
      {
227
601
        if (inCur == 0)
228
110
        {
229
110
          *status = LZMA_STATUS_NEEDS_MORE_INPUT;
230
110
          return SZ_OK;
231
110
        }
232
233
491
        if (p->state == LZMA2_STATE_DATA)
234
488
        {
235
488
          BoolInt initDic = (p->control == LZMA2_CONTROL_COPY_RESET_DIC);
236
488
          LzmaDec_InitDicAndState(&p->decoder, initDic, False);
237
488
        }
238
239
491
        if (inCur > outCur)
240
436
          inCur = outCur;
241
491
        if (inCur == 0)
242
3
          break;
243
244
488
        LzmaDec_UpdateWithUncompressed(&p->decoder, src, inCur);
245
246
488
        src += inCur;
247
488
        *srcLen += inCur;
248
488
        p->unpackSize -= (UInt32)inCur;
249
488
        p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT;
250
488
      }
251
1.00k
      else
252
1.00k
      {
253
1.00k
        SRes res;
254
255
1.00k
        if (p->state == LZMA2_STATE_DATA)
256
538
        {
257
538
          BoolInt initDic = (p->control >= 0xE0);
258
538
          BoolInt initState = (p->control >= 0xA0);
259
538
          LzmaDec_InitDicAndState(&p->decoder, initDic, initState);
260
538
          p->state = LZMA2_STATE_DATA_CONT;
261
538
        }
262
  
263
1.00k
        if (inCur > p->packSize)
264
667
          inCur = (SizeT)p->packSize;
265
        
266
1.00k
        res = LzmaDec_DecodeToDic(&p->decoder, dicPos + outCur, src, &inCur, curFinishMode, status);
267
268
1.00k
        src += inCur;
269
1.00k
        *srcLen += inCur;
270
1.00k
        p->packSize -= (UInt32)inCur;
271
1.00k
        outCur = p->decoder.dicPos - dicPos;
272
1.00k
        p->unpackSize -= (UInt32)outCur;
273
274
1.00k
        if (res != 0)
275
33
          break;
276
        
277
975
        if (*status == LZMA_STATUS_NEEDS_MORE_INPUT)
278
227
        {
279
227
          if (p->packSize == 0)
280
35
            break;
281
192
          return SZ_OK;
282
227
        }
283
284
748
        if (inCur == 0 && outCur == 0)
285
374
        {
286
374
          if (*status != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
287
374
              || p->unpackSize != 0
288
374
              || p->packSize != 0)
289
76
            break;
290
298
          p->state = LZMA2_STATE_CONTROL;
291
298
        }
292
        
293
672
        *status = LZMA_STATUS_NOT_SPECIFIED;
294
672
      }
295
1.60k
    }
296
1.60k
  }
297
  
298
176
  *status = LZMA_STATUS_NOT_SPECIFIED;
299
176
  p->state = LZMA2_STATE_ERROR;
300
176
  return SZ_ERROR_DATA;
301
651
}
302
303
304
305
306
ELzma2ParseStatus Lzma2Dec_Parse(CLzma2Dec *p,
307
    SizeT outSize,
308
    const Byte *src, SizeT *srcLen,
309
    int checkFinishBlock)
310
0
{
311
0
  SizeT inSize = *srcLen;
312
0
  *srcLen = 0;
313
314
0
  while (p->state != LZMA2_STATE_ERROR)
315
0
  {
316
0
    if (p->state == LZMA2_STATE_FINISHED)
317
0
      return (ELzma2ParseStatus)LZMA_STATUS_FINISHED_WITH_MARK;
318
319
0
    if (outSize == 0 && !checkFinishBlock)
320
0
      return (ELzma2ParseStatus)LZMA_STATUS_NOT_FINISHED;
321
    
322
0
    if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT)
323
0
    {
324
0
      if (*srcLen == inSize)
325
0
        return (ELzma2ParseStatus)LZMA_STATUS_NEEDS_MORE_INPUT;
326
0
      (*srcLen)++;
327
328
0
      p->state = Lzma2Dec_UpdateState(p, *src++);
329
330
0
      if (p->state == LZMA2_STATE_UNPACK0)
331
0
      {
332
        // if (p->decoder.dicPos != 0)
333
0
        if (p->control == LZMA2_CONTROL_COPY_RESET_DIC || p->control >= 0xE0)
334
0
          return LZMA2_PARSE_STATUS_NEW_BLOCK;
335
        // if (outSize == 0) return LZMA_STATUS_NOT_FINISHED;
336
0
      }
337
338
      // The following code can be commented.
339
      // It's not big problem, if we read additional input bytes.
340
      // It will be stopped later in LZMA2_STATE_DATA / LZMA2_STATE_DATA_CONT state.
341
342
0
      if (outSize == 0 && p->state != LZMA2_STATE_FINISHED)
343
0
      {
344
        // checkFinishBlock is true. So we expect that block must be finished,
345
        // We can return LZMA_STATUS_NOT_SPECIFIED or LZMA_STATUS_NOT_FINISHED here
346
        // break;
347
0
        return (ELzma2ParseStatus)LZMA_STATUS_NOT_FINISHED;
348
0
      }
349
350
0
      if (p->state == LZMA2_STATE_DATA)
351
0
        return LZMA2_PARSE_STATUS_NEW_CHUNK;
352
353
0
      continue;
354
0
    }
355
356
0
    if (outSize == 0)
357
0
      return (ELzma2ParseStatus)LZMA_STATUS_NOT_FINISHED;
358
359
0
    {
360
0
      SizeT inCur = inSize - *srcLen;
361
362
0
      if (LZMA2_IS_UNCOMPRESSED_STATE(p))
363
0
      {
364
0
        if (inCur == 0)
365
0
          return (ELzma2ParseStatus)LZMA_STATUS_NEEDS_MORE_INPUT;
366
0
        if (inCur > p->unpackSize)
367
0
          inCur = p->unpackSize;
368
0
        if (inCur > outSize)
369
0
          inCur = outSize;
370
0
        p->decoder.dicPos += inCur;
371
0
        src += inCur;
372
0
        *srcLen += inCur;
373
0
        outSize -= inCur;
374
0
        p->unpackSize -= (UInt32)inCur;
375
0
        p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT;
376
0
      }
377
0
      else
378
0
      {
379
0
        p->isExtraMode = True;
380
381
0
        if (inCur == 0)
382
0
        {
383
0
          if (p->packSize != 0)
384
0
            return (ELzma2ParseStatus)LZMA_STATUS_NEEDS_MORE_INPUT;
385
0
        }
386
0
        else if (p->state == LZMA2_STATE_DATA)
387
0
        {
388
0
          p->state = LZMA2_STATE_DATA_CONT;
389
0
          if (*src != 0)
390
0
          {
391
            // first byte of lzma chunk must be Zero
392
0
            *srcLen += 1;
393
0
            p->packSize--;
394
0
            break;
395
0
          }
396
0
        }
397
  
398
0
        if (inCur > p->packSize)
399
0
          inCur = (SizeT)p->packSize;
400
401
0
        src += inCur;
402
0
        *srcLen += inCur;
403
0
        p->packSize -= (UInt32)inCur;
404
405
0
        if (p->packSize == 0)
406
0
        {
407
0
          SizeT rem = outSize;
408
0
          if (rem > p->unpackSize)
409
0
            rem = p->unpackSize;
410
0
          p->decoder.dicPos += rem;
411
0
          p->unpackSize -= (UInt32)rem;
412
0
          outSize -= rem;
413
0
          if (p->unpackSize == 0)
414
0
            p->state = LZMA2_STATE_CONTROL;
415
0
        }
416
0
      }
417
0
    }
418
0
  }
419
  
420
0
  p->state = LZMA2_STATE_ERROR;
421
0
  return (ELzma2ParseStatus)LZMA_STATUS_NOT_SPECIFIED;
422
0
}
423
424
425
426
427
SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
428
0
{
429
0
  SizeT outSize = *destLen, inSize = *srcLen;
430
0
  *srcLen = *destLen = 0;
431
  
432
0
  for (;;)
433
0
  {
434
0
    SizeT inCur = inSize, outCur, dicPos;
435
0
    ELzmaFinishMode curFinishMode;
436
0
    SRes res;
437
    
438
0
    if (p->decoder.dicPos == p->decoder.dicBufSize)
439
0
      p->decoder.dicPos = 0;
440
0
    dicPos = p->decoder.dicPos;
441
0
    curFinishMode = LZMA_FINISH_ANY;
442
0
    outCur = p->decoder.dicBufSize - dicPos;
443
    
444
0
    if (outCur >= outSize)
445
0
    {
446
0
      outCur = outSize;
447
0
      curFinishMode = finishMode;
448
0
    }
449
450
0
    res = Lzma2Dec_DecodeToDic(p, dicPos + outCur, src, &inCur, curFinishMode, status);
451
    
452
0
    src += inCur;
453
0
    inSize -= inCur;
454
0
    *srcLen += inCur;
455
0
    outCur = p->decoder.dicPos - dicPos;
456
0
    memcpy(dest, p->decoder.dic + dicPos, outCur);
457
0
    dest += outCur;
458
0
    outSize -= outCur;
459
0
    *destLen += outCur;
460
0
    if (res != 0)
461
0
      return res;
462
0
    if (outCur == 0 || outSize == 0)
463
0
      return SZ_OK;
464
0
  }
465
0
}
466
467
468
SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
469
    Byte prop, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAllocPtr alloc)
470
0
{
471
0
  CLzma2Dec p;
472
0
  SRes res;
473
0
  SizeT outSize = *destLen, inSize = *srcLen;
474
0
  *destLen = *srcLen = 0;
475
0
  *status = LZMA_STATUS_NOT_SPECIFIED;
476
0
  Lzma2Dec_Construct(&p);
477
0
  RINOK(Lzma2Dec_AllocateProbs(&p, prop, alloc));
478
0
  p.decoder.dic = dest;
479
0
  p.decoder.dicBufSize = outSize;
480
0
  Lzma2Dec_Init(&p);
481
0
  *srcLen = inSize;
482
0
  res = Lzma2Dec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
483
0
  *destLen = p.decoder.dicPos;
484
0
  if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
485
0
    res = SZ_ERROR_INPUT_EOF;
486
0
  Lzma2Dec_FreeProbs(&p, alloc);
487
0
  return res;
488
0
}