Coverage Report

Created: 2025-10-10 08:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.05/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
2.94M
JArithmeticDecoderStats::JArithmeticDecoderStats(int contextSizeA) {
21
2.94M
  contextSize = contextSizeA;
22
2.94M
  cxTab = (Guchar *)gmallocn(contextSize, sizeof(Guchar));
23
2.94M
  reset();
24
2.94M
}
25
26
2.93M
JArithmeticDecoderStats::~JArithmeticDecoderStats() {
27
2.93M
  gfree(cxTab);
28
2.93M
}
29
30
1.48k
JArithmeticDecoderStats *JArithmeticDecoderStats::copy() {
31
1.48k
  JArithmeticDecoderStats *stats;
32
33
1.48k
  stats = new JArithmeticDecoderStats(contextSize);
34
1.48k
  memcpy(stats->cxTab, cxTab, contextSize);
35
1.48k
  return stats;
36
1.48k
}
37
38
3.51M
void JArithmeticDecoderStats::reset() {
39
3.51M
  memset(cxTab, 0, contextSize);
40
3.51M
}
41
42
0
void JArithmeticDecoderStats::copyFrom(JArithmeticDecoderStats *stats) {
43
0
  memcpy(cxTab, stats->cxTab, contextSize);
44
0
}
45
46
2.38M
void JArithmeticDecoderStats::setEntry(Guint cx, int i, int mps) {
47
2.38M
  cxTab[cx] = (Guchar)((i << 1) + mps);
48
2.38M
}
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
441k
JArithmeticDecoder::JArithmeticDecoder() {
88
441k
  str = NULL;
89
441k
  dataLen = 0;
90
441k
  limitStream = gFalse;
91
441k
  nBytesRead = 0;
92
441k
  readBuf = -1;
93
441k
}
94
95
171M
inline Guint JArithmeticDecoder::readByte() {
96
171M
  Guint x;
97
98
171M
  if (limitStream) {
99
170M
    if (readBuf >= 0) {
100
214k
      x = (Guint)readBuf;
101
214k
      readBuf = -1;
102
214k
      return x;
103
214k
    }
104
170M
    --dataLen;
105
170M
    if (dataLen < 0) {
106
3.13M
      return 0xff;
107
3.13M
    }
108
170M
  }
109
167M
  ++nBytesRead;
110
167M
  return (Guint)str->getChar() & 0xff;
111
171M
}
112
113
441k
JArithmeticDecoder::~JArithmeticDecoder() {
114
441k
  cleanup();
115
441k
}
116
117
613k
void JArithmeticDecoder::start() {
118
613k
  buf0 = readByte();
119
613k
  buf1 = readByte();
120
121
  // INITDEC
122
613k
  c = (buf0 ^ 0xff) << 16;
123
613k
  byteIn();
124
613k
  c <<= 7;
125
613k
  ct -= 7;
126
613k
  a = 0x80000000;
127
613k
}
128
129
10.6k
void JArithmeticDecoder::restart(int dataLenA) {
130
10.6k
  Guint cAdd;
131
10.6k
  GBool prevFF;
132
10.6k
  int k, nBits;
133
134
10.6k
  if (dataLen >= 0) {
135
6.88k
    dataLen = dataLenA;
136
6.88k
  } else if (dataLen == -1) {
137
733
    dataLen = dataLenA;
138
733
    buf1 = readByte();
139
3.00k
  } else {
140
3.00k
    k = (-dataLen - 1) * 8 - ct;
141
3.00k
    dataLen = dataLenA;
142
3.00k
    cAdd = 0;
143
3.00k
    prevFF = gFalse;
144
462k
    while (k > 0) {
145
459k
      buf0 = readByte();
146
459k
      if (prevFF) {
147
404k
  cAdd += 0xfe00 - (buf0 << 9);
148
404k
  nBits = 7;
149
404k
      } else {
150
54.8k
  cAdd += 0xff00 - (buf0 << 8);
151
54.8k
  nBits = 8;
152
54.8k
      }
153
459k
      prevFF = buf0 == 0xff;
154
459k
      if (k > nBits) {
155
456k
  cAdd <<= nBits;
156
456k
  k -= nBits;
157
456k
      } else {
158
3.00k
  cAdd <<= k;
159
3.00k
  ct = nBits - k;
160
3.00k
  k = 0;
161
3.00k
      }
162
459k
    }
163
3.00k
    c += cAdd;
164
3.00k
    buf1 = readByte();
165
3.00k
  }
166
10.6k
}
167
168
1.25M
void JArithmeticDecoder::cleanup() {
169
1.25M
  if (limitStream) {
170
    // This saves one extra byte of data from the end of packet i, to
171
    // be used in packet i+1.  It's not clear from the JPEG 2000 spec
172
    // exactly how this should work, but this kludge does seem to fix
173
    // decode of some problematic JPEG 2000 streams.  It may actually
174
    // be necessary to buffer an arbitrary number of bytes (not just
175
    // one byte), but I haven't run into that case yet.
176
166M
    while (dataLen > 0) {
177
165M
      readBuf = -1;
178
165M
      readBuf = readByte();
179
165M
    }
180
1.09M
  }
181
1.25M
}
182
183
int JArithmeticDecoder::decodeBit(Guint context,
184
113M
          JArithmeticDecoderStats *stats) {
185
113M
  int bit;
186
113M
  Guint qe;
187
113M
  int iCX, mpsCX;
188
189
113M
  iCX = stats->cxTab[context] >> 1;
190
113M
  mpsCX = stats->cxTab[context] & 1;
191
113M
  qe = qeTab[iCX];
192
113M
  a -= qe;
193
113M
  if (c < a) {
194
97.5M
    if (a & 0x80000000) {
195
79.4M
      bit = mpsCX;
196
79.4M
    } else {
197
      // MPS_EXCHANGE
198
18.1M
      if (a < qe) {
199
3.08M
  bit = 1 - mpsCX;
200
3.08M
  if (switchTab[iCX]) {
201
1.61M
    stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX));
202
1.61M
  } else {
203
1.47M
    stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX);
204
1.47M
  }
205
15.0M
      } else {
206
15.0M
  bit = mpsCX;
207
15.0M
  stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX);
208
15.0M
      }
209
      // RENORMD
210
19.4M
      do {
211
19.4M
  if (ct == 0) {
212
2.45M
    byteIn();
213
2.45M
  }
214
19.4M
  a <<= 1;
215
19.4M
  c <<= 1;
216
19.4M
  --ct;
217
19.4M
      } while (!(a & 0x80000000));
218
18.1M
    }
219
97.5M
  } else {
220
15.9M
    c -= a;
221
    // LPS_EXCHANGE
222
15.9M
    if (a < qe) {
223
3.37M
      bit = mpsCX;
224
3.37M
      stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX);
225
12.5M
    } else {
226
12.5M
      bit = 1 - mpsCX;
227
12.5M
      if (switchTab[iCX]) {
228
2.24M
  stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX));
229
10.3M
      } else {
230
10.3M
  stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX);
231
10.3M
      }
232
12.5M
    }
233
15.9M
    a = qe;
234
    // RENORMD
235
25.0M
    do {
236
25.0M
      if (ct == 0) {
237
3.16M
  byteIn();
238
3.16M
      }
239
25.0M
      a <<= 1;
240
25.0M
      c <<= 1;
241
25.0M
      --ct;
242
25.0M
    } while (!(a & 0x80000000));
243
15.9M
  }
244
113M
  return bit;
245
113M
}
246
247
int JArithmeticDecoder::decodeByte(Guint context,
248
0
           JArithmeticDecoderStats *stats) {
249
0
  int byte;
250
0
  int i;
251
252
0
  byte = 0;
253
0
  for (i = 0; i < 8; ++i) {
254
0
    byte = (byte << 1) | decodeBit(context, stats);
255
0
  }
256
0
  return byte;
257
0
}
258
259
167k
GBool JArithmeticDecoder::decodeInt(int *x, JArithmeticDecoderStats *stats) {
260
167k
  int s;
261
167k
  Guint v;
262
167k
  int i;
263
264
167k
  prev = 1;
265
167k
  s = decodeIntBit(stats);
266
167k
  if (decodeIntBit(stats)) {
267
66.7k
    if (decodeIntBit(stats)) {
268
24.1k
      if (decodeIntBit(stats)) {
269
13.5k
  if (decodeIntBit(stats)) {
270
5.62k
    if (decodeIntBit(stats)) {
271
2.06k
      v = 0;
272
68.0k
      for (i = 0; i < 32; ++i) {
273
66.0k
        v = (v << 1) | decodeIntBit(stats);
274
66.0k
      }
275
2.06k
      v += 4436;
276
3.56k
    } else {
277
3.56k
      v = 0;
278
46.3k
      for (i = 0; i < 12; ++i) {
279
42.7k
        v = (v << 1) | decodeIntBit(stats);
280
42.7k
      }
281
3.56k
      v += 340;
282
3.56k
    }
283
7.91k
  } else {
284
7.91k
    v = 0;
285
71.1k
    for (i = 0; i < 8; ++i) {
286
63.2k
      v = (v << 1) | decodeIntBit(stats);
287
63.2k
    }
288
7.91k
    v += 84;
289
7.91k
  }
290
13.5k
      } else {
291
10.5k
  v = 0;
292
74.1k
  for (i = 0; i < 6; ++i) {
293
63.5k
    v = (v << 1) | decodeIntBit(stats);
294
63.5k
  }
295
10.5k
  v += 20;
296
10.5k
      }
297
42.5k
    } else {
298
42.5k
      v = decodeIntBit(stats);
299
42.5k
      v = (v << 1) | decodeIntBit(stats);
300
42.5k
      v = (v << 1) | decodeIntBit(stats);
301
42.5k
      v = (v << 1) | decodeIntBit(stats);
302
42.5k
      v += 4;
303
42.5k
    }
304
100k
  } else {
305
100k
    v = decodeIntBit(stats);
306
100k
    v = (v << 1) | decodeIntBit(stats);
307
100k
  }
308
309
167k
  if (s) {
310
65.7k
    if (v == 0) {
311
12.8k
      return gFalse;
312
12.8k
    }
313
52.8k
    *x = -(int)v;
314
101k
  } else {
315
101k
    *x = (int)v;
316
101k
  }
317
154k
  return gTrue;
318
167k
}
319
320
1.05M
int JArithmeticDecoder::decodeIntBit(JArithmeticDecoderStats *stats) {
321
1.05M
  int bit;
322
323
1.05M
  bit = decodeBit(prev, stats);
324
1.05M
  if (prev < 0x100) {
325
895k
    prev = (prev << 1) | bit;
326
895k
  } else {
327
158k
    prev = (((prev << 1) | bit) & 0x1ff) | 0x100;
328
158k
  }
329
1.05M
  return bit;
330
1.05M
}
331
332
Guint JArithmeticDecoder::decodeIAID(Guint codeLen,
333
94.9k
             JArithmeticDecoderStats *stats) {
334
94.9k
  Guint i;
335
94.9k
  int bit;
336
337
94.9k
  prev = 1;
338
508k
  for (i = 0; i < codeLen; ++i) {
339
413k
    bit = decodeBit(prev, stats);
340
413k
    prev = (prev << 1) | bit;
341
413k
  }
342
94.9k
  return prev - (1 << codeLen);
343
94.9k
}
344
345
6.22M
void JArithmeticDecoder::byteIn() {
346
6.22M
  if (buf0 == 0xff) {
347
4.54M
    if (buf1 > 0x8f) {
348
4.53M
      if (limitStream) {
349
2.57M
  buf0 = buf1;
350
2.57M
  buf1 = readByte();
351
2.57M
  c = c + 0xff00 - (buf0 << 8);
352
2.57M
      }
353
4.53M
      ct = 8;
354
4.53M
    } else {
355
9.68k
      buf0 = buf1;
356
9.68k
      buf1 = readByte();
357
9.68k
      c = c + 0xfe00 - (buf0 << 9);
358
9.68k
      ct = 7;
359
9.68k
    }
360
4.54M
  } else {
361
1.68M
    buf0 = buf1;
362
1.68M
    buf1 = readByte();
363
1.68M
    c = c + 0xff00 - (buf0 << 8);
364
1.68M
    ct = 8;
365
1.68M
  }
366
6.22M
}