Coverage Report

Created: 2024-07-27 06:12

/src/lzma-fuzz/sdk/C/Ppmd7Dec.c
Line
Count
Source
1
/* Ppmd7Dec.c -- PPMdH Decoder
2
2018-07-04 : Igor Pavlov : Public domain
3
This code is based on PPMd var.H (2001): Dmitry Shkarin : Public domain */
4
5
#include "Precomp.h"
6
7
#include "Ppmd7.h"
8
9
26.1k
#define kTopValue (1 << 24)
10
11
BoolInt Ppmd7z_RangeDec_Init(CPpmd7z_RangeDec *p)
12
268
{
13
268
  unsigned i;
14
268
  p->Code = 0;
15
268
  p->Range = 0xFFFFFFFF;
16
268
  if (IByteIn_Read(p->Stream) != 0)
17
12
    return False;
18
1.28k
  for (i = 0; i < 4; i++)
19
1.02k
    p->Code = (p->Code << 8) | IByteIn_Read(p->Stream);
20
256
  return (p->Code < 0xFFFFFFFF);
21
268
}
22
23
29.4k
#define GET_Ppmd7z_RangeDec CPpmd7z_RangeDec *p = CONTAINER_FROM_VTBL(pp, CPpmd7z_RangeDec, vt);
24
 
25
static UInt32 Range_GetThreshold(const IPpmd7_RangeDec *pp, UInt32 total)
26
11.6k
{
27
11.6k
  GET_Ppmd7z_RangeDec
28
11.6k
  return p->Code / (p->Range /= total);
29
11.6k
}
30
31
static void Range_Normalize(CPpmd7z_RangeDec *p)
32
17.7k
{
33
17.7k
  if (p->Range < kTopValue)
34
8.31k
  {
35
8.31k
    p->Code = (p->Code << 8) | IByteIn_Read(p->Stream);
36
8.31k
    p->Range <<= 8;
37
8.31k
    if (p->Range < kTopValue)
38
249
    {
39
249
      p->Code = (p->Code << 8) | IByteIn_Read(p->Stream);
40
249
      p->Range <<= 8;
41
249
    }
42
8.31k
  }
43
17.7k
}
44
45
static void Range_Decode(const IPpmd7_RangeDec *pp, UInt32 start, UInt32 size)
46
11.6k
{
47
11.6k
  GET_Ppmd7z_RangeDec
48
11.6k
  p->Code -= start * p->Range;
49
11.6k
  p->Range *= size;
50
11.6k
  Range_Normalize(p);
51
11.6k
}
52
53
static UInt32 Range_DecodeBit(const IPpmd7_RangeDec *pp, UInt32 size0)
54
6.09k
{
55
6.09k
  GET_Ppmd7z_RangeDec
56
6.09k
  UInt32 newBound = (p->Range >> 14) * size0;
57
6.09k
  UInt32 symbol;
58
6.09k
  if (p->Code < newBound)
59
3.98k
  {
60
3.98k
    symbol = 0;
61
3.98k
    p->Range = newBound;
62
3.98k
  }
63
2.11k
  else
64
2.11k
  {
65
2.11k
    symbol = 1;
66
2.11k
    p->Code -= newBound;
67
2.11k
    p->Range -= newBound;
68
2.11k
  }
69
6.09k
  Range_Normalize(p);
70
6.09k
  return symbol;
71
6.09k
}
72
73
void Ppmd7z_RangeDec_CreateVTable(CPpmd7z_RangeDec *p)
74
268
{
75
268
  p->vt.GetThreshold = Range_GetThreshold;
76
268
  p->vt.Decode = Range_Decode;
77
268
  p->vt.DecodeBit = Range_DecodeBit;
78
268
}
79
80
81
715k
#define MASK(sym) ((signed char *)charMask)[sym]
82
83
int Ppmd7_DecodeSymbol(CPpmd7 *p, const IPpmd7_RangeDec *rc)
84
14.2k
{
85
14.2k
  size_t charMask[256 / sizeof(size_t)];
86
14.2k
  if (p->MinContext->NumStats != 1)
87
8.14k
  {
88
8.14k
    CPpmd_State *s = Ppmd7_GetStats(p, p->MinContext);
89
8.14k
    unsigned i;
90
8.14k
    UInt32 count, hiCnt;
91
8.14k
    if ((count = rc->GetThreshold(rc, p->MinContext->SummFreq)) < (hiCnt = s->Freq))
92
1.20k
    {
93
1.20k
      Byte symbol;
94
1.20k
      rc->Decode(rc, 0, s->Freq);
95
1.20k
      p->FoundState = s;
96
1.20k
      symbol = s->Symbol;
97
1.20k
      Ppmd7_Update1_0(p);
98
1.20k
      return symbol;
99
1.20k
    }
100
6.93k
    p->PrevSuccess = 0;
101
6.93k
    i = p->MinContext->NumStats - 1;
102
6.93k
    do
103
658k
    {
104
658k
      if ((hiCnt += (++s)->Freq) > count)
105
5.93k
      {
106
5.93k
        Byte symbol;
107
5.93k
        rc->Decode(rc, hiCnt - s->Freq, s->Freq);
108
5.93k
        p->FoundState = s;
109
5.93k
        symbol = s->Symbol;
110
5.93k
        Ppmd7_Update1(p);
111
5.93k
        return symbol;
112
5.93k
      }
113
658k
    }
114
652k
    while (--i);
115
998
    if (count >= p->MinContext->SummFreq)
116
1
      return -2;
117
997
    p->HiBitsFlag = p->HB2Flag[p->FoundState->Symbol];
118
997
    rc->Decode(rc, hiCnt, p->MinContext->SummFreq - hiCnt);
119
997
    PPMD_SetAllBitsIn256Bytes(charMask);
120
997
    MASK(s->Symbol) = 0;
121
997
    i = p->MinContext->NumStats - 1;
122
13.1k
    do { MASK((--s)->Symbol) = 0; } while (--i);
123
997
  }
124
6.09k
  else
125
6.09k
  {
126
6.09k
    UInt16 *prob = Ppmd7_GetBinSumm(p);
127
6.09k
    if (rc->DecodeBit(rc, *prob) == 0)
128
3.98k
    {
129
3.98k
      Byte symbol;
130
3.98k
      *prob = (UInt16)PPMD_UPDATE_PROB_0(*prob);
131
3.98k
      symbol = (p->FoundState = Ppmd7Context_OneState(p->MinContext))->Symbol;
132
3.98k
      Ppmd7_UpdateBin(p);
133
3.98k
      return symbol;
134
3.98k
    }
135
2.11k
    *prob = (UInt16)PPMD_UPDATE_PROB_1(*prob);
136
2.11k
    p->InitEsc = PPMD7_kExpEscape[*prob >> 10];
137
2.11k
    PPMD_SetAllBitsIn256Bytes(charMask);
138
2.11k
    MASK(Ppmd7Context_OneState(p->MinContext)->Symbol) = 0;
139
2.11k
    p->PrevSuccess = 0;
140
2.11k
  }
141
3.10k
  for (;;)
142
3.60k
  {
143
3.60k
    CPpmd_State *ps[256], *s;
144
3.60k
    UInt32 freqSum, count, hiCnt;
145
3.60k
    CPpmd_See *see;
146
3.60k
    unsigned i, num, numMasked = p->MinContext->NumStats;
147
3.60k
    do
148
5.63k
    {
149
5.63k
      p->OrderFall++;
150
5.63k
      if (!p->MinContext->Suffix)
151
47
        return -1;
152
5.59k
      p->MinContext = Ppmd7_GetContext(p, p->MinContext->Suffix);
153
5.59k
    }
154
5.59k
    while (p->MinContext->NumStats == numMasked);
155
3.55k
    hiCnt = 0;
156
3.55k
    s = Ppmd7_GetStats(p, p->MinContext);
157
3.55k
    i = 0;
158
3.55k
    num = p->MinContext->NumStats - numMasked;
159
3.55k
    do
160
695k
    {
161
695k
      int k = (int)(MASK(s->Symbol));
162
695k
      hiCnt += (s->Freq & k);
163
695k
      ps[i] = s++;
164
695k
      i -= k;
165
695k
    }
166
695k
    while (i != num);
167
    
168
3.55k
    see = Ppmd7_MakeEscFreq(p, numMasked, &freqSum);
169
3.55k
    freqSum += hiCnt;
170
3.55k
    count = rc->GetThreshold(rc, freqSum);
171
    
172
3.55k
    if (count < hiCnt)
173
3.05k
    {
174
3.05k
      Byte symbol;
175
3.05k
      CPpmd_State **pps = ps;
176
327k
      for (hiCnt = 0; (hiCnt += (*pps)->Freq) <= count; pps++);
177
3.05k
      s = *pps;
178
3.05k
      rc->Decode(rc, hiCnt - s->Freq, s->Freq);
179
3.05k
      Ppmd_See_Update(see);
180
3.05k
      p->FoundState = s;
181
3.05k
      symbol = s->Symbol;
182
3.05k
      Ppmd7_Update2(p);
183
3.05k
      return symbol;
184
3.05k
    }
185
499
    if (count >= freqSum)
186
2
      return -2;
187
497
    rc->Decode(rc, hiCnt, freqSum - hiCnt);
188
497
    see->Summ = (UInt16)(see->Summ + freqSum);
189
3.87k
    do { MASK(ps[--i]->Symbol) = 0; } while (i != 0);
190
497
  }
191
3.10k
}