Coverage Report

Created: 2026-10-13 06:47

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
92.9k
JArithmeticDecoderStats::JArithmeticDecoderStats(int contextSizeA) {
21
92.9k
  contextSize = contextSizeA;
22
92.9k
  cxTab = (Guchar *)gmallocn(contextSize, sizeof(Guchar));
23
92.9k
  reset();
24
92.9k
}
25
26
92.8k
JArithmeticDecoderStats::~JArithmeticDecoderStats() {
27
92.8k
  gfree(cxTab);
28
92.8k
}
29
30
0
JArithmeticDecoderStats *JArithmeticDecoderStats::copy() {
31
0
  JArithmeticDecoderStats *stats;
32
33
0
  stats = new JArithmeticDecoderStats(contextSize);
34
0
  memcpy(stats->cxTab, cxTab, contextSize);
35
0
  return stats;
36
0
}
37
38
144k
void JArithmeticDecoderStats::reset() {
39
144k
  memset(cxTab, 0, contextSize);
40
144k
}
41
42
0
void JArithmeticDecoderStats::copyFrom(JArithmeticDecoderStats *stats) {
43
0
  memcpy(cxTab, stats->cxTab, contextSize);
44
0
}
45
46
194k
void JArithmeticDecoderStats::setEntry(Guint cx, int i, int mps) {
47
194k
  cxTab[cx] = (Guchar)((i << 1) + mps);
48
194k
}
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
21.6k
JArithmeticDecoder::JArithmeticDecoder() {
88
21.6k
  str = NULL;
89
21.6k
  dataLen = 0;
90
21.6k
  limitStream = gFalse;
91
21.6k
  nBytesRead = 0;
92
21.6k
  readBufNext = 0;
93
21.6k
  readBufLength = 0;
94
21.6k
}
95
96
880k
inline Guint JArithmeticDecoder::readByte() {
97
880k
  if (limitStream) {
98
721k
    if (readBufNext < readBufLength) {
99
107k
      return readBuf[readBufNext++];
100
107k
    }
101
613k
    --dataLen;
102
613k
    if (dataLen < 0) {
103
321k
      return 0xff;
104
321k
    }
105
613k
  }
106
450k
  ++nBytesRead;
107
450k
  return (Guint)str->getChar() & 0xff;
108
880k
}
109
110
21.6k
JArithmeticDecoder::~JArithmeticDecoder() {
111
21.6k
  cleanup();
112
21.6k
}
113
114
61.9k
void JArithmeticDecoder::start() {
115
61.9k
  buf0 = readByte();
116
61.9k
  buf1 = readByte();
117
118
  // INITDEC
119
61.9k
  c = (buf0 ^ 0xff) << 16;
120
61.9k
  byteIn();
121
61.9k
  c <<= 7;
122
61.9k
  ct -= 7;
123
61.9k
  a = 0x80000000;
124
61.9k
}
125
126
3.48k
void JArithmeticDecoder::restart(int dataLenA) {
127
3.48k
  Guint cAdd;
128
3.48k
  GBool prevFF;
129
3.48k
  int k, nBits;
130
131
3.48k
  if (dataLen >= 0) {
132
1.62k
    dataLen = dataLenA;
133
1.86k
  } else if (dataLen == -1) {
134
185
    dataLen = dataLenA;
135
185
    buf1 = readByte();
136
1.68k
  } else {
137
1.68k
    k = (-dataLen - 1) * 8 - ct;
138
1.68k
    dataLen = dataLenA;
139
1.68k
    cAdd = 0;
140
1.68k
    prevFF = gFalse;
141
88.9k
    while (k > 0) {
142
87.2k
      buf0 = readByte();
143
87.2k
      if (prevFF) {
144
65.2k
  cAdd += 0xfe00 - (buf0 << 9);
145
65.2k
  nBits = 7;
146
65.2k
      } else {
147
22.0k
  cAdd += 0xff00 - (buf0 << 8);
148
22.0k
  nBits = 8;
149
22.0k
      }
150
87.2k
      prevFF = buf0 == 0xff;
151
87.2k
      if (k > nBits) {
152
85.6k
  cAdd <<= nBits;
153
85.6k
  k -= nBits;
154
85.6k
      } else {
155
1.68k
  cAdd <<= k;
156
1.68k
  ct = nBits - k;
157
1.68k
  k = 0;
158
1.68k
      }
159
87.2k
    }
160
1.68k
    c += cAdd;
161
1.68k
    buf1 = readByte();
162
1.68k
  }
163
3.48k
}
164
165
100k
void JArithmeticDecoder::cleanup() {
166
100k
  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
96.3k
    readBufLength = 0;
175
330M
    while (dataLen > 0) {
176
330M
      if (readBufLength < (int)sizeof(readBuf)) {
177
747k
  readBuf[readBufLength++] = (Guchar)str->getChar();
178
747k
  ++nBytesRead;
179
747k
      }
180
330M
      --dataLen;
181
330M
    }
182
96.3k
    readBufNext = 0;
183
96.3k
  }
184
100k
}
185
186
int JArithmeticDecoder::decodeBit(Guint context,
187
20.2M
          JArithmeticDecoderStats *stats) {
188
20.2M
  int bit;
189
20.2M
  Guint qe;
190
20.2M
  int iCX, mpsCX;
191
192
20.2M
  iCX = stats->cxTab[context] >> 1;
193
20.2M
  mpsCX = stats->cxTab[context] & 1;
194
20.2M
  qe = qeTab[iCX];
195
20.2M
  a -= qe;
196
20.2M
  if (c < a) {
197
16.4M
    if (a & 0x80000000) {
198
12.5M
      bit = mpsCX;
199
12.5M
    } else {
200
      // MPS_EXCHANGE
201
3.91M
      if (a < qe) {
202
650k
  bit = 1 - mpsCX;
203
650k
  if (switchTab[iCX]) {
204
284k
    stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX));
205
366k
  } else {
206
366k
    stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX);
207
366k
  }
208
3.26M
      } else {
209
3.26M
  bit = mpsCX;
210
3.26M
  stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX);
211
3.26M
      }
212
      // RENORMD
213
4.20M
      do {
214
4.20M
  if (ct == 0) {
215
535k
    byteIn();
216
535k
  }
217
4.20M
  a <<= 1;
218
4.20M
  c <<= 1;
219
4.20M
  --ct;
220
4.20M
      } while (!(a & 0x80000000));
221
3.91M
    }
222
16.4M
  } else {
223
3.77M
    c -= a;
224
    // LPS_EXCHANGE
225
3.77M
    if (a < qe) {
226
823k
      bit = mpsCX;
227
823k
      stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX);
228
2.94M
    } else {
229
2.94M
      bit = 1 - mpsCX;
230
2.94M
      if (switchTab[iCX]) {
231
368k
  stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX));
232
2.58M
      } else {
233
2.58M
  stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX);
234
2.58M
      }
235
2.94M
    }
236
3.77M
    a = qe;
237
    // RENORMD
238
6.01M
    do {
239
6.01M
      if (ct == 0) {
240
754k
  byteIn();
241
754k
      }
242
6.01M
      a <<= 1;
243
6.01M
      c <<= 1;
244
6.01M
      --ct;
245
6.01M
    } while (!(a & 0x80000000));
246
3.77M
  }
247
20.2M
  return bit;
248
20.2M
}
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
20.0k
GBool JArithmeticDecoder::decodeInt(int *x, JArithmeticDecoderStats *stats) {
263
20.0k
  int s;
264
20.0k
  Guint v;
265
20.0k
  int i;
266
267
20.0k
  prev = 1;
268
20.0k
  s = decodeIntBit(stats);
269
20.0k
  if (decodeIntBit(stats)) {
270
9.97k
    if (decodeIntBit(stats)) {
271
4.43k
      if (decodeIntBit(stats)) {
272
3.17k
  if (decodeIntBit(stats)) {
273
1.26k
    if (decodeIntBit(stats)) {
274
696
      v = 0;
275
22.9k
      for (i = 0; i < 32; ++i) {
276
22.2k
        v = (v << 1) | decodeIntBit(stats);
277
22.2k
      }
278
696
      v += 4436;
279
696
    } else {
280
568
      v = 0;
281
7.38k
      for (i = 0; i < 12; ++i) {
282
6.81k
        v = (v << 1) | decodeIntBit(stats);
283
6.81k
      }
284
568
      v += 340;
285
568
    }
286
1.91k
  } else {
287
1.91k
    v = 0;
288
17.2k
    for (i = 0; i < 8; ++i) {
289
15.3k
      v = (v << 1) | decodeIntBit(stats);
290
15.3k
    }
291
1.91k
    v += 84;
292
1.91k
  }
293
3.17k
      } else {
294
1.25k
  v = 0;
295
8.76k
  for (i = 0; i < 6; ++i) {
296
7.51k
    v = (v << 1) | decodeIntBit(stats);
297
7.51k
  }
298
1.25k
  v += 20;
299
1.25k
      }
300
5.54k
    } else {
301
5.54k
      v = decodeIntBit(stats);
302
5.54k
      v = (v << 1) | decodeIntBit(stats);
303
5.54k
      v = (v << 1) | decodeIntBit(stats);
304
5.54k
      v = (v << 1) | decodeIntBit(stats);
305
5.54k
      v += 4;
306
5.54k
    }
307
10.1k
  } else {
308
10.1k
    v = decodeIntBit(stats);
309
10.1k
    v = (v << 1) | decodeIntBit(stats);
310
10.1k
  }
311
312
20.0k
  if (s) {
313
7.88k
    if (v == 0) {
314
846
      return gFalse;
315
846
    }
316
7.03k
    *x = -(int)v;
317
12.2k
  } else {
318
12.2k
    *x = (int)v;
319
12.2k
  }
320
19.2k
  return gTrue;
321
20.0k
}
322
323
153k
int JArithmeticDecoder::decodeIntBit(JArithmeticDecoderStats *stats) {
324
153k
  int bit;
325
326
153k
  bit = decodeBit(prev, stats);
327
153k
  if (prev < 0x100) {
328
114k
    prev = (prev << 1) | bit;
329
114k
  } else {
330
38.6k
    prev = (((prev << 1) | bit) & 0x1ff) | 0x100;
331
38.6k
  }
332
153k
  return bit;
333
153k
}
334
335
Guint JArithmeticDecoder::decodeIAID(Guint codeLen,
336
8.56k
             JArithmeticDecoderStats *stats) {
337
8.56k
  Guint i;
338
8.56k
  int bit;
339
340
8.56k
  prev = 1;
341
51.4k
  for (i = 0; i < codeLen; ++i) {
342
42.8k
    bit = decodeBit(prev, stats);
343
42.8k
    prev = (prev << 1) | bit;
344
42.8k
  }
345
8.56k
  return prev - (1 << codeLen);
346
8.56k
}
347
348
1.35M
void JArithmeticDecoder::byteIn() {
349
1.35M
  if (buf0 == 0xff) {
350
925k
    if (buf1 > 0x8f) {
351
923k
      if (limitStream) {
352
238k
  buf0 = buf1;
353
238k
  buf1 = readByte();
354
238k
  c = c + 0xff00 - (buf0 << 8);
355
238k
      }
356
923k
      ct = 8;
357
923k
    } else {
358
1.71k
      buf0 = buf1;
359
1.71k
      buf1 = readByte();
360
1.71k
      c = c + 0xfe00 - (buf0 << 9);
361
1.71k
      ct = 7;
362
1.71k
    }
363
925k
  } else {
364
426k
    buf0 = buf1;
365
426k
    buf1 = readByte();
366
426k
    c = c + 0xff00 - (buf0 << 8);
367
426k
    ct = 8;
368
426k
  }
369
1.35M
}