Coverage Report

Created: 2026-09-01 07:01

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
1.75M
JArithmeticDecoderStats::JArithmeticDecoderStats(int contextSizeA) {
21
1.75M
  contextSize = contextSizeA;
22
1.75M
  cxTab = (Guchar *)gmallocn(contextSize, sizeof(Guchar));
23
1.75M
  reset();
24
1.75M
}
25
26
1.75M
JArithmeticDecoderStats::~JArithmeticDecoderStats() {
27
1.75M
  gfree(cxTab);
28
1.75M
}
29
30
2.08k
JArithmeticDecoderStats *JArithmeticDecoderStats::copy() {
31
2.08k
  JArithmeticDecoderStats *stats;
32
33
2.08k
  stats = new JArithmeticDecoderStats(contextSize);
34
2.08k
  memcpy(stats->cxTab, cxTab, contextSize);
35
2.08k
  return stats;
36
2.08k
}
37
38
2.18M
void JArithmeticDecoderStats::reset() {
39
2.18M
  memset(cxTab, 0, contextSize);
40
2.18M
}
41
42
0
void JArithmeticDecoderStats::copyFrom(JArithmeticDecoderStats *stats) {
43
0
  memcpy(cxTab, stats->cxTab, contextSize);
44
0
}
45
46
1.68M
void JArithmeticDecoderStats::setEntry(Guint cx, int i, int mps) {
47
1.68M
  cxTab[cx] = (Guchar)((i << 1) + mps);
48
1.68M
}
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
275k
JArithmeticDecoder::JArithmeticDecoder() {
88
275k
  str = NULL;
89
275k
  dataLen = 0;
90
275k
  limitStream = gFalse;
91
275k
  nBytesRead = 0;
92
275k
  readBufNext = 0;
93
275k
  readBufLength = 0;
94
275k
}
95
96
2.66M
inline Guint JArithmeticDecoder::readByte() {
97
2.66M
  if (limitStream) {
98
2.34M
    if (readBufNext < readBufLength) {
99
748k
      return readBuf[readBufNext++];
100
748k
    }
101
1.59M
    --dataLen;
102
1.59M
    if (dataLen < 0) {
103
618k
      return 0xff;
104
618k
    }
105
1.59M
  }
106
1.29M
  ++nBytesRead;
107
1.29M
  return (Guint)str->getChar() & 0xff;
108
2.66M
}
109
110
275k
JArithmeticDecoder::~JArithmeticDecoder() {
111
275k
  cleanup();
112
275k
}
113
114
482k
void JArithmeticDecoder::start() {
115
482k
  buf0 = readByte();
116
482k
  buf1 = readByte();
117
118
  // INITDEC
119
482k
  c = (buf0 ^ 0xff) << 16;
120
482k
  byteIn();
121
482k
  c <<= 7;
122
482k
  ct -= 7;
123
482k
  a = 0x80000000;
124
482k
}
125
126
7.57k
void JArithmeticDecoder::restart(int dataLenA) {
127
7.57k
  Guint cAdd;
128
7.57k
  GBool prevFF;
129
7.57k
  int k, nBits;
130
131
7.57k
  if (dataLen >= 0) {
132
4.17k
    dataLen = dataLenA;
133
4.17k
  } else if (dataLen == -1) {
134
1.15k
    dataLen = dataLenA;
135
1.15k
    buf1 = readByte();
136
2.24k
  } else {
137
2.24k
    k = (-dataLen - 1) * 8 - ct;
138
2.24k
    dataLen = dataLenA;
139
2.24k
    cAdd = 0;
140
2.24k
    prevFF = gFalse;
141
175k
    while (k > 0) {
142
173k
      buf0 = readByte();
143
173k
      if (prevFF) {
144
167k
  cAdd += 0xfe00 - (buf0 << 9);
145
167k
  nBits = 7;
146
167k
      } else {
147
5.54k
  cAdd += 0xff00 - (buf0 << 8);
148
5.54k
  nBits = 8;
149
5.54k
      }
150
173k
      prevFF = buf0 == 0xff;
151
173k
      if (k > nBits) {
152
170k
  cAdd <<= nBits;
153
170k
  k -= nBits;
154
170k
      } else {
155
2.24k
  cAdd <<= k;
156
2.24k
  ct = nBits - k;
157
2.24k
  k = 0;
158
2.24k
      }
159
173k
    }
160
2.24k
    c += cAdd;
161
2.24k
    buf1 = readByte();
162
2.24k
  }
163
7.57k
}
164
165
871k
void JArithmeticDecoder::cleanup() {
166
871k
  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
773k
    readBufLength = 0;
175
1.06G
    while (dataLen > 0) {
176
1.06G
      if (readBufLength < (int)sizeof(readBuf)) {
177
4.87M
  readBuf[readBufLength++] = (Guchar)str->getChar();
178
4.87M
  ++nBytesRead;
179
4.87M
      }
180
1.06G
      --dataLen;
181
1.06G
    }
182
773k
    readBufNext = 0;
183
773k
  }
184
871k
}
185
186
int JArithmeticDecoder::decodeBit(Guint context,
187
42.0M
          JArithmeticDecoderStats *stats) {
188
42.0M
  int bit;
189
42.0M
  Guint qe;
190
42.0M
  int iCX, mpsCX;
191
192
42.0M
  iCX = stats->cxTab[context] >> 1;
193
42.0M
  mpsCX = stats->cxTab[context] & 1;
194
42.0M
  qe = qeTab[iCX];
195
42.0M
  a -= qe;
196
42.0M
  if (c < a) {
197
33.1M
    if (a & 0x80000000) {
198
24.9M
      bit = mpsCX;
199
24.9M
    } else {
200
      // MPS_EXCHANGE
201
8.20M
      if (a < qe) {
202
1.26M
  bit = 1 - mpsCX;
203
1.26M
  if (switchTab[iCX]) {
204
775k
    stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX));
205
775k
  } else {
206
485k
    stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX);
207
485k
  }
208
6.94M
      } else {
209
6.94M
  bit = mpsCX;
210
6.94M
  stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX);
211
6.94M
      }
212
      // RENORMD
213
8.69M
      do {
214
8.69M
  if (ct == 0) {
215
1.12M
    byteIn();
216
1.12M
  }
217
8.69M
  a <<= 1;
218
8.69M
  c <<= 1;
219
8.69M
  --ct;
220
8.69M
      } while (!(a & 0x80000000));
221
8.20M
    }
222
33.1M
  } else {
223
8.92M
    c -= a;
224
    // LPS_EXCHANGE
225
8.92M
    if (a < qe) {
226
1.61M
      bit = mpsCX;
227
1.61M
      stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX);
228
7.30M
    } else {
229
7.30M
      bit = 1 - mpsCX;
230
7.30M
      if (switchTab[iCX]) {
231
2.23M
  stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX));
232
5.07M
      } else {
233
5.07M
  stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX);
234
5.07M
      }
235
7.30M
    }
236
8.92M
    a = qe;
237
    // RENORMD
238
13.5M
    do {
239
13.5M
      if (ct == 0) {
240
1.72M
  byteIn();
241
1.72M
      }
242
13.5M
      a <<= 1;
243
13.5M
      c <<= 1;
244
13.5M
      --ct;
245
13.5M
    } while (!(a & 0x80000000));
246
8.92M
  }
247
42.0M
  return bit;
248
42.0M
}
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
50.5k
GBool JArithmeticDecoder::decodeInt(int *x, JArithmeticDecoderStats *stats) {
263
50.5k
  int s;
264
50.5k
  Guint v;
265
50.5k
  int i;
266
267
50.5k
  prev = 1;
268
50.5k
  s = decodeIntBit(stats);
269
50.5k
  if (decodeIntBit(stats)) {
270
25.7k
    if (decodeIntBit(stats)) {
271
11.7k
      if (decodeIntBit(stats)) {
272
7.61k
  if (decodeIntBit(stats)) {
273
2.96k
    if (decodeIntBit(stats)) {
274
2.25k
      v = 0;
275
74.2k
      for (i = 0; i < 32; ++i) {
276
72.0k
        v = (v << 1) | decodeIntBit(stats);
277
72.0k
      }
278
2.25k
      v += 4436;
279
2.25k
    } else {
280
716
      v = 0;
281
9.30k
      for (i = 0; i < 12; ++i) {
282
8.59k
        v = (v << 1) | decodeIntBit(stats);
283
8.59k
      }
284
716
      v += 340;
285
716
    }
286
4.64k
  } else {
287
4.64k
    v = 0;
288
41.8k
    for (i = 0; i < 8; ++i) {
289
37.1k
      v = (v << 1) | decodeIntBit(stats);
290
37.1k
    }
291
4.64k
    v += 84;
292
4.64k
  }
293
7.61k
      } else {
294
4.18k
  v = 0;
295
29.3k
  for (i = 0; i < 6; ++i) {
296
25.1k
    v = (v << 1) | decodeIntBit(stats);
297
25.1k
  }
298
4.18k
  v += 20;
299
4.18k
      }
300
13.9k
    } else {
301
13.9k
      v = decodeIntBit(stats);
302
13.9k
      v = (v << 1) | decodeIntBit(stats);
303
13.9k
      v = (v << 1) | decodeIntBit(stats);
304
13.9k
      v = (v << 1) | decodeIntBit(stats);
305
13.9k
      v += 4;
306
13.9k
    }
307
25.7k
  } else {
308
24.7k
    v = decodeIntBit(stats);
309
24.7k
    v = (v << 1) | decodeIntBit(stats);
310
24.7k
  }
311
312
50.5k
  if (s) {
313
14.8k
    if (v == 0) {
314
2.23k
      return gFalse;
315
2.23k
    }
316
12.6k
    *x = -(int)v;
317
35.6k
  } else {
318
35.6k
    *x = (int)v;
319
35.6k
  }
320
48.3k
  return gTrue;
321
50.5k
}
322
323
397k
int JArithmeticDecoder::decodeIntBit(JArithmeticDecoderStats *stats) {
324
397k
  int bit;
325
326
397k
  bit = decodeBit(prev, stats);
327
397k
  if (prev < 0x100) {
328
291k
    prev = (prev << 1) | bit;
329
291k
  } else {
330
106k
    prev = (((prev << 1) | bit) & 0x1ff) | 0x100;
331
106k
  }
332
397k
  return bit;
333
397k
}
334
335
Guint JArithmeticDecoder::decodeIAID(Guint codeLen,
336
32.4k
             JArithmeticDecoderStats *stats) {
337
32.4k
  Guint i;
338
32.4k
  int bit;
339
340
32.4k
  prev = 1;
341
129k
  for (i = 0; i < codeLen; ++i) {
342
97.3k
    bit = decodeBit(prev, stats);
343
97.3k
    prev = (prev << 1) | bit;
344
97.3k
  }
345
32.4k
  return prev - (1 << codeLen);
346
32.4k
}
347
348
3.33M
void JArithmeticDecoder::byteIn() {
349
3.33M
  if (buf0 == 0xff) {
350
2.18M
    if (buf1 > 0x8f) {
351
2.17M
      if (limitStream) {
352
365k
  buf0 = buf1;
353
365k
  buf1 = readByte();
354
365k
  c = c + 0xff00 - (buf0 << 8);
355
365k
      }
356
2.17M
      ct = 8;
357
2.17M
    } else {
358
7.28k
      buf0 = buf1;
359
7.28k
      buf1 = readByte();
360
7.28k
      c = c + 0xfe00 - (buf0 << 9);
361
7.28k
      ct = 7;
362
7.28k
    }
363
2.18M
  } else {
364
1.14M
    buf0 = buf1;
365
1.14M
    buf1 = readByte();
366
1.14M
    c = c + 0xff00 - (buf0 << 8);
367
1.14M
    ct = 8;
368
1.14M
  }
369
3.33M
}