Coverage Report

Created: 2026-10-19 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/JArithmeticDecoder.cc
Line
Count
Source
1
//========================================================================
2
//
3
// JArithmeticDecoder.cc
4
//
5
// Copyright 2002-2004 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include "gmempp.h"
12
#include "Object.h"
13
#include "Stream.h"
14
#include "JArithmeticDecoder.h"
15
16
//------------------------------------------------------------------------
17
// JArithmeticDecoderStates
18
//------------------------------------------------------------------------
19
20
4.57M
JArithmeticDecoderStats::JArithmeticDecoderStats(int contextSizeA) {
21
4.57M
  contextSize = contextSizeA;
22
4.57M
  cxTab = (Guchar *)gmallocn(contextSize, sizeof(Guchar));
23
4.57M
  reset();
24
4.57M
}
25
26
4.57M
JArithmeticDecoderStats::~JArithmeticDecoderStats() {
27
4.57M
  gfree(cxTab);
28
4.57M
}
29
30
2.73k
JArithmeticDecoderStats *JArithmeticDecoderStats::copy() {
31
2.73k
  JArithmeticDecoderStats *stats;
32
33
2.73k
  stats = new JArithmeticDecoderStats(contextSize);
34
2.73k
  memcpy(stats->cxTab, cxTab, contextSize);
35
2.73k
  return stats;
36
2.73k
}
37
38
5.45M
void JArithmeticDecoderStats::reset() {
39
5.45M
  memset(cxTab, 0, contextSize);
40
5.45M
}
41
42
0
void JArithmeticDecoderStats::copyFrom(JArithmeticDecoderStats *stats) {
43
0
  memcpy(cxTab, stats->cxTab, contextSize);
44
0
}
45
46
3.41M
void JArithmeticDecoderStats::setEntry(Guint cx, int i, int mps) {
47
3.41M
  cxTab[cx] = (Guchar)((i << 1) + mps);
48
3.41M
}
49
50
//------------------------------------------------------------------------
51
// JArithmeticDecoder
52
//------------------------------------------------------------------------
53
54
Guint JArithmeticDecoder::qeTab[47] = {
55
  0x56010000, 0x34010000, 0x18010000, 0x0AC10000,
56
  0x05210000, 0x02210000, 0x56010000, 0x54010000,
57
  0x48010000, 0x38010000, 0x30010000, 0x24010000,
58
  0x1C010000, 0x16010000, 0x56010000, 0x54010000,
59
  0x51010000, 0x48010000, 0x38010000, 0x34010000,
60
  0x30010000, 0x28010000, 0x24010000, 0x22010000,
61
  0x1C010000, 0x18010000, 0x16010000, 0x14010000,
62
  0x12010000, 0x11010000, 0x0AC10000, 0x09C10000,
63
  0x08A10000, 0x05210000, 0x04410000, 0x02A10000,
64
  0x02210000, 0x01410000, 0x01110000, 0x00850000,
65
  0x00490000, 0x00250000, 0x00150000, 0x00090000,
66
  0x00050000, 0x00010000, 0x56010000
67
};
68
69
int JArithmeticDecoder::nmpsTab[47] = {
70
   1,  2,  3,  4,  5, 38,  7,  8,  9, 10, 11, 12, 13, 29, 15, 16,
71
  17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
72
  33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 45, 46
73
};
74
75
int JArithmeticDecoder::nlpsTab[47] = {
76
   1,  6,  9, 12, 29, 33,  6, 14, 14, 14, 17, 18, 20, 21, 14, 14,
77
  15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
78
  30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 46
79
};
80
81
int JArithmeticDecoder::switchTab[47] = {
82
  1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,
83
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
85
};
86
87
601k
JArithmeticDecoder::JArithmeticDecoder() {
88
601k
  str = NULL;
89
601k
  dataLen = 0;
90
601k
  limitStream = gFalse;
91
601k
  nBytesRead = 0;
92
601k
  readBufNext = 0;
93
601k
  readBufLength = 0;
94
601k
}
95
96
6.34M
inline Guint JArithmeticDecoder::readByte() {
97
6.34M
  if (limitStream) {
98
5.55M
    if (readBufNext < readBufLength) {
99
1.04M
      return readBuf[readBufNext++];
100
1.04M
    }
101
4.51M
    --dataLen;
102
4.51M
    if (dataLen < 0) {
103
2.30M
      return 0xff;
104
2.30M
    }
105
4.51M
  }
106
2.99M
  ++nBytesRead;
107
2.99M
  return (Guint)str->getChar() & 0xff;
108
6.34M
}
109
110
600k
JArithmeticDecoder::~JArithmeticDecoder() {
111
600k
  cleanup();
112
600k
}
113
114
791k
void JArithmeticDecoder::start() {
115
791k
  buf0 = readByte();
116
791k
  buf1 = readByte();
117
118
  // INITDEC
119
791k
  c = (buf0 ^ 0xff) << 16;
120
791k
  byteIn();
121
791k
  c <<= 7;
122
791k
  ct -= 7;
123
791k
  a = 0x80000000;
124
791k
}
125
126
25.0k
void JArithmeticDecoder::restart(int dataLenA) {
127
25.0k
  Guint cAdd;
128
25.0k
  GBool prevFF;
129
25.0k
  int k, nBits;
130
131
25.0k
  if (dataLen >= 0) {
132
18.4k
    dataLen = dataLenA;
133
18.4k
  } else if (dataLen == -1) {
134
1.85k
    dataLen = dataLenA;
135
1.85k
    buf1 = readByte();
136
4.77k
  } else {
137
4.77k
    k = (-dataLen - 1) * 8 - ct;
138
4.77k
    dataLen = dataLenA;
139
4.77k
    cAdd = 0;
140
4.77k
    prevFF = gFalse;
141
430k
    while (k > 0) {
142
425k
      buf0 = readByte();
143
425k
      if (prevFF) {
144
325k
  cAdd += 0xfe00 - (buf0 << 9);
145
325k
  nBits = 7;
146
325k
      } else {
147
100k
  cAdd += 0xff00 - (buf0 << 8);
148
100k
  nBits = 8;
149
100k
      }
150
425k
      prevFF = buf0 == 0xff;
151
425k
      if (k > nBits) {
152
421k
  cAdd <<= nBits;
153
421k
  k -= nBits;
154
421k
      } else {
155
4.77k
  cAdd <<= k;
156
4.77k
  ct = nBits - k;
157
4.77k
  k = 0;
158
4.77k
      }
159
425k
    }
160
4.77k
    c += cAdd;
161
4.77k
    buf1 = readByte();
162
4.77k
  }
163
25.0k
}
164
165
1.54M
void JArithmeticDecoder::cleanup() {
166
1.54M
  if (limitStream) {
167
    // This saves up to sizeof(readBuf) extra bytes of data from the
168
    // end of packet i, to be used in packet i+1. It's not clear from
169
    // the JPEG 2000 spec exactly how this should work, but this
170
    // kludge does seem to fix decode of some problematic JPEG 2000
171
    // streams. The buffer may need to larger, but I haven't run into
172
    // that case. (And the buffer was originally just one byte, which
173
    // works for almost all cases.)
174
1.28M
    readBufLength = 0;
175
5.53G
    while (dataLen > 0) {
176
5.53G
      if (readBufLength < (int)sizeof(readBuf)) {
177
6.75M
  readBuf[readBufLength++] = (Guchar)str->getChar();
178
6.75M
  ++nBytesRead;
179
6.75M
      }
180
5.53G
      --dataLen;
181
5.53G
    }
182
1.28M
    readBufNext = 0;
183
1.28M
  }
184
1.54M
}
185
186
int JArithmeticDecoder::decodeBit(Guint context,
187
552M
          JArithmeticDecoderStats *stats) {
188
552M
  int bit;
189
552M
  Guint qe;
190
552M
  int iCX, mpsCX;
191
192
552M
  iCX = stats->cxTab[context] >> 1;
193
552M
  mpsCX = stats->cxTab[context] & 1;
194
552M
  qe = qeTab[iCX];
195
552M
  a -= qe;
196
552M
  if (c < a) {
197
530M
    if (a & 0x80000000) {
198
507M
      bit = mpsCX;
199
507M
    } else {
200
      // MPS_EXCHANGE
201
22.7M
      if (a < qe) {
202
3.69M
  bit = 1 - mpsCX;
203
3.69M
  if (switchTab[iCX]) {
204
2.20M
    stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX));
205
2.20M
  } else {
206
1.48M
    stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX);
207
1.48M
  }
208
19.0M
      } else {
209
19.0M
  bit = mpsCX;
210
19.0M
  stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX);
211
19.0M
      }
212
      // RENORMD
213
24.2M
      do {
214
24.2M
  if (ct == 0) {
215
3.06M
    byteIn();
216
3.06M
  }
217
24.2M
  a <<= 1;
218
24.2M
  c <<= 1;
219
24.2M
  --ct;
220
24.2M
      } while (!(a & 0x80000000));
221
22.7M
    }
222
530M
  } else {
223
22.2M
    c -= a;
224
    // LPS_EXCHANGE
225
22.2M
    if (a < qe) {
226
4.35M
      bit = mpsCX;
227
4.35M
      stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX);
228
17.8M
    } else {
229
17.8M
      bit = 1 - mpsCX;
230
17.8M
      if (switchTab[iCX]) {
231
4.85M
  stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX));
232
13.0M
      } else {
233
13.0M
  stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX);
234
13.0M
      }
235
17.8M
    }
236
22.2M
    a = qe;
237
    // RENORMD
238
33.8M
    do {
239
33.8M
      if (ct == 0) {
240
4.30M
  byteIn();
241
4.30M
      }
242
33.8M
      a <<= 1;
243
33.8M
      c <<= 1;
244
33.8M
      --ct;
245
33.8M
    } while (!(a & 0x80000000));
246
22.2M
  }
247
552M
  return bit;
248
552M
}
249
250
int JArithmeticDecoder::decodeByte(Guint context,
251
0
           JArithmeticDecoderStats *stats) {
252
0
  int byte;
253
0
  int i;
254
255
0
  byte = 0;
256
0
  for (i = 0; i < 8; ++i) {
257
0
    byte = (byte << 1) | decodeBit(context, stats);
258
0
  }
259
0
  return byte;
260
0
}
261
262
8.26M
GBool JArithmeticDecoder::decodeInt(int *x, JArithmeticDecoderStats *stats) {
263
8.26M
  int s;
264
8.26M
  Guint v;
265
8.26M
  int i;
266
267
8.26M
  prev = 1;
268
8.26M
  s = decodeIntBit(stats);
269
8.26M
  if (decodeIntBit(stats)) {
270
5.95M
    if (decodeIntBit(stats)) {
271
4.82M
      if (decodeIntBit(stats)) {
272
4.60M
  if (decodeIntBit(stats)) {
273
4.59M
    if (decodeIntBit(stats)) {
274
4.59M
      v = 0;
275
151M
      for (i = 0; i < 32; ++i) {
276
146M
        v = (v << 1) | decodeIntBit(stats);
277
146M
      }
278
4.59M
      v += 4436;
279
4.59M
    } else {
280
3.93k
      v = 0;
281
51.1k
      for (i = 0; i < 12; ++i) {
282
47.1k
        v = (v << 1) | decodeIntBit(stats);
283
47.1k
      }
284
3.93k
      v += 340;
285
3.93k
    }
286
4.59M
  } else {
287
7.73k
    v = 0;
288
69.5k
    for (i = 0; i < 8; ++i) {
289
61.8k
      v = (v << 1) | decodeIntBit(stats);
290
61.8k
    }
291
7.73k
    v += 84;
292
7.73k
  }
293
4.60M
      } else {
294
219k
  v = 0;
295
1.53M
  for (i = 0; i < 6; ++i) {
296
1.31M
    v = (v << 1) | decodeIntBit(stats);
297
1.31M
  }
298
219k
  v += 20;
299
219k
      }
300
4.82M
    } else {
301
1.13M
      v = decodeIntBit(stats);
302
1.13M
      v = (v << 1) | decodeIntBit(stats);
303
1.13M
      v = (v << 1) | decodeIntBit(stats);
304
1.13M
      v = (v << 1) | decodeIntBit(stats);
305
1.13M
      v += 4;
306
1.13M
    }
307
5.95M
  } else {
308
2.30M
    v = decodeIntBit(stats);
309
2.30M
    v = (v << 1) | decodeIntBit(stats);
310
2.30M
  }
311
312
8.26M
  if (s) {
313
5.95M
    if (v == 0) {
314
1.10M
      return gFalse;
315
1.10M
    }
316
4.84M
    *x = -(int)v;
317
4.84M
  } else {
318
2.30M
    *x = (int)v;
319
2.30M
  }
320
7.15M
  return gTrue;
321
8.26M
}
322
323
193M
int JArithmeticDecoder::decodeIntBit(JArithmeticDecoderStats *stats) {
324
193M
  int bit;
325
326
193M
  bit = decodeBit(prev, stats);
327
193M
  if (prev < 0x100) {
328
55.7M
    prev = (prev << 1) | bit;
329
138M
  } else {
330
138M
    prev = (((prev << 1) | bit) & 0x1ff) | 0x100;
331
138M
  }
332
193M
  return bit;
333
193M
}
334
335
Guint JArithmeticDecoder::decodeIAID(Guint codeLen,
336
4.90M
             JArithmeticDecoderStats *stats) {
337
4.90M
  Guint i;
338
4.90M
  int bit;
339
340
4.90M
  prev = 1;
341
8.91M
  for (i = 0; i < codeLen; ++i) {
342
4.01M
    bit = decodeBit(prev, stats);
343
4.01M
    prev = (prev << 1) | bit;
344
4.01M
  }
345
4.90M
  return prev - (1 << codeLen);
346
4.90M
}
347
348
8.16M
void JArithmeticDecoder::byteIn() {
349
8.16M
  if (buf0 == 0xff) {
350
5.67M
    if (buf1 > 0x8f) {
351
5.66M
      if (limitStream) {
352
1.82M
  buf0 = buf1;
353
1.82M
  buf1 = readByte();
354
1.82M
  c = c + 0xff00 - (buf0 << 8);
355
1.82M
      }
356
5.66M
      ct = 8;
357
5.66M
    } else {
358
15.8k
      buf0 = buf1;
359
15.8k
      buf1 = readByte();
360
15.8k
      c = c + 0xfe00 - (buf0 << 9);
361
15.8k
      ct = 7;
362
15.8k
    }
363
5.67M
  } else {
364
2.48M
    buf0 = buf1;
365
2.48M
    buf1 = readByte();
366
2.48M
    c = c + 0xff00 - (buf0 << 8);
367
2.48M
    ct = 8;
368
2.48M
  }
369
8.16M
}