Coverage Report

Created: 2026-07-25 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jasper/src/libjasper/jpc/jpc_t2dec.c
Line
Count
Source
1
/*
2
 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3
 *   British Columbia.
4
 * Copyright (c) 2001-2003 Michael David Adams.
5
 * All rights reserved.
6
 */
7
8
/* __START_OF_JASPER_LICENSE__
9
 * 
10
 * JasPer License Version 2.0
11
 * 
12
 * Copyright (c) 2001-2006 Michael David Adams
13
 * Copyright (c) 1999-2000 Image Power, Inc.
14
 * Copyright (c) 1999-2000 The University of British Columbia
15
 * 
16
 * All rights reserved.
17
 * 
18
 * Permission is hereby granted, free of charge, to any person (the
19
 * "User") obtaining a copy of this software and associated documentation
20
 * files (the "Software"), to deal in the Software without restriction,
21
 * including without limitation the rights to use, copy, modify, merge,
22
 * publish, distribute, and/or sell copies of the Software, and to permit
23
 * persons to whom the Software is furnished to do so, subject to the
24
 * following conditions:
25
 * 
26
 * 1.  The above copyright notices and this permission notice (which
27
 * includes the disclaimer below) shall be included in all copies or
28
 * substantial portions of the Software.
29
 * 
30
 * 2.  The name of a copyright holder shall not be used to endorse or
31
 * promote products derived from the Software without specific prior
32
 * written permission.
33
 * 
34
 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35
 * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36
 * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37
 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39
 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
40
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41
 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
45
 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46
 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47
 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48
 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49
 * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
50
 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51
 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
52
 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53
 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54
 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55
 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56
 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57
 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58
 * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59
 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60
 * 
61
 * __END_OF_JASPER_LICENSE__
62
 */
63
64
/*
65
 * Tier 2 Decoder
66
 *
67
 * $Id$
68
 */
69
70
/******************************************************************************\
71
* Includes.
72
\******************************************************************************/
73
74
#include "jpc_t2dec.h"
75
#include "jpc_bs.h"
76
#include "jpc_dec.h"
77
#include "jpc_cs.h"
78
#include "jpc_t1cod.h"
79
#include "jpc_math.h"
80
81
#include "jasper/jas_types.h"
82
#include "jasper/jas_malloc.h"
83
#include "jasper/jas_math.h"
84
#include "jasper/jas_stream.h"
85
#include "jasper/jas_debug.h"
86
87
#include <stdio.h>
88
89
/******************************************************************************\
90
*
91
\******************************************************************************/
92
93
static long jpc_dec_lookahead(jas_stream_t *in);
94
static int jpc_getcommacode(jpc_bitstream_t *in);
95
static int jpc_getnumnewpasses(jpc_bitstream_t *in);
96
static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int lvlno,
97
  int prcno, unsigned lyrno);
98
99
/******************************************************************************\
100
* Code.
101
\******************************************************************************/
102
103
static int jpc_getcommacode(jpc_bitstream_t *in)
104
348k
{
105
348k
  int n;
106
348k
  int v;
107
108
348k
  n = 0;
109
438k
  for (;;) {
110
438k
    if ((v = jpc_bitstream_getbit(in)) < 0) {
111
0
      return -1;
112
0
    }
113
438k
    if (jpc_bitstream_eof(in)) {
114
260
      return -1;
115
260
    }
116
438k
    if (!v) {
117
348k
      break;
118
348k
    }
119
90.3k
    ++n;
120
90.3k
  }
121
122
348k
  return n;
123
348k
}
124
125
static int jpc_getnumnewpasses(jpc_bitstream_t *in)
126
348k
{
127
348k
  int n;
128
129
348k
  if ((n = jpc_bitstream_getbit(in)) > 0) {
130
109k
    if ((n = jpc_bitstream_getbit(in)) > 0) {
131
78.5k
      if ((n = jpc_bitstream_getbits(in, 2)) == 3) {
132
36.8k
        if ((n = jpc_bitstream_getbits(in, 5)) == 31) {
133
849
          if ((n = jpc_bitstream_getbits(in, 7)) >= 0) {
134
849
            n += 36 + 1;
135
849
          }
136
36.0k
        } else if (n >= 0) {
137
36.0k
          n += 5 + 1;
138
36.0k
        }
139
41.6k
      } else if (n >= 0) {
140
41.6k
        n += 2 + 1;
141
41.6k
      }
142
78.5k
    } else if (!n) {
143
30.8k
      n += 2;
144
30.8k
    }
145
238k
  } else if (!n) {
146
238k
    ++n;
147
238k
  }
148
149
348k
  return n;
150
348k
}
151
152
static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int rlvlno,
153
  int prcno, unsigned lyrno)
154
26.4M
{
155
26.4M
  jpc_bitstream_t *inb;
156
26.4M
  jpc_dec_tcomp_t *tcomp;
157
26.4M
  jpc_dec_rlvl_t *rlvl;
158
26.4M
  jpc_dec_band_t *band;
159
26.4M
  jpc_dec_cblk_t *cblk;
160
26.4M
  int m;
161
26.4M
  jpc_tagtreenode_t *leaf;
162
26.4M
  int included;
163
26.4M
  int ret;
164
26.4M
  int numnewpasses;
165
26.4M
  jpc_dec_seg_t *seg;
166
26.4M
  int len;
167
26.4M
  int present;
168
26.4M
  jpc_ms_t *ms;
169
26.4M
  jpc_dec_tile_t *tile;
170
26.4M
  jpc_dec_ccp_t *ccp;
171
26.4M
  jpc_dec_cp_t *cp;
172
26.4M
  jpc_dec_prc_t *prc;
173
26.4M
  uint_fast32_t bodylen;
174
26.4M
  bool discard;
175
176
  /* Avoid compiler warning about possible use of uninitialized
177
    variable. */
178
26.4M
  bodylen = 0;
179
180
26.4M
  discard = (lyrno >= dec->maxlyrs);
181
182
26.4M
  tile = dec->curtile;
183
26.4M
  cp = tile->cp;
184
26.4M
  ccp = &cp->ccps[compno];
185
186
  /*
187
   * Decode the packet header.
188
   */
189
190
  /* Decode the SOP marker segment if present. */
191
26.4M
  if (cp->csty & JPC_COD_SOP) {
192
1.23M
    if (jpc_dec_lookahead(in) == JPC_MS_SOP) {
193
189k
      if (!(ms = jpc_getms(in, dec->cstate))) {
194
13
        jas_logerrorf("cannot get marker segment\n");
195
13
        return -1;
196
13
      }
197
189k
      if (jpc_ms_gettype(ms) != JPC_MS_SOP) {
198
0
        jpc_ms_destroy(ms);
199
0
        jas_logerrorf("cannot get (SOP) marker segment\n");
200
0
        return -1;
201
0
      }
202
189k
      unsigned int maxNsop = 65536;
203
      /* checking the packet sequence number */
204
189k
      if (tile->pi->pktno % maxNsop != ms->parms.sop.seqno) {
205
74
        jas_logerrorf("incorrect packet sequence number %d was found, but expected %d\n",
206
74
          ms->parms.sop.seqno, tile->pi->pktno % maxNsop);
207
74
        jpc_ms_destroy(ms);
208
74
        return -1;
209
74
      }
210
189k
      jpc_ms_destroy(ms);
211
189k
    }
212
1.23M
  }
213
214
26.4M
  const uint_least64_t hdroffstart = jas_stream_getrwcount(pkthdrstream);
215
216
26.4M
  if (!(inb = jpc_bitstream_sopen(pkthdrstream, "r"))) {
217
0
    jas_logerrorf("cannot open bitstream\n");
218
0
    return -1;
219
0
  }
220
221
26.4M
  if ((present = jpc_bitstream_getbit(inb)) < 0) {
222
0
    jas_logerrorf("cannot get bit\n");
223
0
    jpc_bitstream_close(inb);
224
0
    return 1;
225
0
  }
226
26.4M
  JAS_LOGDEBUGF(10, "\n", present);
227
26.4M
  JAS_LOGDEBUGF(10, "present=%d ", present);
228
229
  /* Is the packet non-empty? */
230
26.4M
  if (present) {
231
    /* The packet is non-empty. */
232
23.0M
    tcomp = &tile->tcomps[compno];
233
23.0M
    rlvl = &tcomp->rlvls[rlvlno];
234
23.0M
    bodylen = 0;
235
23.0M
    unsigned bandno;
236
90.8M
    for (bandno = 0, band = rlvl->bands; bandno < (unsigned)rlvl->numbands;
237
67.7M
      ++bandno, ++band) {
238
67.7M
      if (!band->data) {
239
64.3M
        continue;
240
64.3M
      }
241
3.37M
      prc = &band->prcs[prcno];
242
3.37M
      if (!prc->cblks) {
243
351k
        continue;
244
351k
      }
245
3.02M
      unsigned cblkno;
246
3.02M
      unsigned usedcblkcnt = 0;
247
9.56M
      for (cblkno = 0, cblk = prc->cblks; cblkno < (unsigned)prc->numcblks;
248
6.54M
        ++cblkno, ++cblk) {
249
6.54M
        ++usedcblkcnt;
250
6.54M
        if (!cblk->numpasses) {
251
3.88M
          leaf = jpc_tagtree_getleaf(prc->incltagtree, usedcblkcnt - 1);
252
3.88M
          if ((included = jpc_tagtree_decode(prc->incltagtree, leaf, lyrno + 1, inb)) < 0) {
253
0
            jas_logerrorf("cannot decode tagtree\n");
254
0
            jpc_bitstream_close(inb);
255
0
            return -1;
256
0
          }
257
3.88M
        } else {
258
2.65M
          if ((included = jpc_bitstream_getbit(inb)) < 0) {
259
0
            jas_logerrorf("cannot get bit\n");
260
0
            jpc_bitstream_close(inb);
261
0
            return -1;
262
0
          }
263
2.65M
        }
264
6.54M
        JAS_LOGDEBUGF(10, "\n");
265
6.54M
        JAS_LOGDEBUGF(10, "included=%d ", included);
266
6.54M
        if (!included) {
267
6.19M
          continue;
268
6.19M
        }
269
348k
        if (!cblk->numpasses) {
270
159k
          unsigned i = 1;
271
159k
          leaf = jpc_tagtree_getleaf(prc->numimsbstagtree, usedcblkcnt - 1);
272
55.3M
          for (;;) {
273
55.3M
            if ((ret = jpc_tagtree_decode(prc->numimsbstagtree, leaf, i, inb)) < 0) {
274
0
              jas_logerrorf("cannot decode tagtree\n");
275
0
              jpc_bitstream_close(inb);
276
0
              return -1;
277
0
            }
278
55.3M
            if (ret) {
279
159k
              break;
280
159k
            }
281
55.2M
            ++i;
282
55.2M
          }
283
159k
          cblk->numimsbs = i - 1;
284
159k
          cblk->firstpassno = cblk->numimsbs * 3;
285
159k
        }
286
348k
        if ((numnewpasses = jpc_getnumnewpasses(inb)) < 0) {
287
0
          jas_logerrorf("cannot get number of new passes\n");
288
0
          jpc_bitstream_close(inb);
289
0
          return -1;
290
0
        }
291
348k
        JAS_LOGDEBUGF(10, "numnewpasses=%d ", numnewpasses);
292
348k
        seg = cblk->curseg;
293
348k
        const unsigned savenumnewpasses = numnewpasses;
294
348k
        unsigned mycounter = 0;
295
348k
        if (numnewpasses > 0) {
296
348k
          if (cblk->firstpassno > 10000) {
297
            /* workaround for
298
               CVE-2016-9398: this
299
               large value would
300
               make
301
               JPC_SEGPASSCNT()
302
               return a negative
303
               value, causing an
304
               assertion failure
305
               in
306
               jpc_floorlog2() */
307
46
            jas_logerrorf("first pass number unreasonably large\n");
308
46
            jpc_bitstream_close(inb);
309
46
            return -1;
310
46
          }
311
348k
          if ((m = jpc_getcommacode(inb)) < 0) {
312
260
            jas_logerrorf("cannot get comma code\n");
313
260
            jpc_bitstream_close(inb);
314
260
            return -1;
315
260
          }
316
348k
          cblk->numlenbits += m;
317
348k
          JAS_LOGDEBUGF(10, "increment=%d ", m);
318
815k
          while (numnewpasses > 0) {
319
467k
            const unsigned passno = cblk->firstpassno + cblk->numpasses + mycounter;
320
467k
            if (passno >= 10000) {
321
              /* with this value,
322
                 JPC_SEGPASSCNT()
323
                 would return 0,
324
                 which is an illegal
325
                 value and would
326
                 later crash in
327
                 jpc_floorlog2() */
328
3
              jas_logerrorf("pass number unreasonably large\n");
329
3
              jpc_bitstream_close(inb);
330
3
              return -1;
331
3
            }
332
  /* XXX - the maxpasses is not set precisely but this doesn't matter... */
333
467k
            const unsigned maxpasses = JPC_SEGPASSCNT(passno, cblk->firstpassno, 10000, (ccp->cblkctx & JPC_COX_LAZY) != 0, (ccp->cblkctx & JPC_COX_TERMALL) != 0);
334
467k
            if (!discard && !seg) {
335
297k
              if (!(seg = jpc_seg_alloc())) {
336
0
                jas_logerrorf("cannot allocate segment\n");
337
0
                jpc_bitstream_close(inb);
338
0
                return -1;
339
0
              }
340
297k
              jpc_seglist_insert(&cblk->segs, cblk->segs.tail, seg);
341
297k
              if (!cblk->curseg) {
342
186k
                cblk->curseg = seg;
343
186k
              }
344
297k
              seg->passno = passno;
345
297k
              seg->type = JPC_SEGTYPE(seg->passno, cblk->firstpassno, (ccp->cblkctx & JPC_COX_LAZY) != 0);
346
297k
              seg->maxpasses = maxpasses;
347
297k
            }
348
467k
            const unsigned n = JAS_MIN((unsigned)numnewpasses, maxpasses);
349
467k
            mycounter += n;
350
467k
            numnewpasses -= n;
351
467k
            if ((len = jpc_bitstream_getbits(inb,
352
467k
              cblk->numlenbits + jpc_floorlog2(n))) < 0) {
353
25
              jpc_bitstream_close(inb);
354
25
              jas_logerrorf("cannot get bits\n");
355
25
              return -1;
356
25
            }
357
467k
            JAS_LOGDEBUGF(10, "len=%d ", len);
358
467k
            if (!discard) {
359
458k
              seg->lyrno = lyrno;
360
458k
              seg->numpasses += n;
361
458k
              seg->cnt = len;
362
458k
              seg = seg->next;
363
458k
            }
364
467k
            bodylen += len;
365
467k
          }
366
348k
        }
367
347k
        cblk->numpasses += savenumnewpasses;
368
347k
      }
369
3.02M
    }
370
371
23.0M
    jpc_bitstream_inalign(inb, 0, 0);
372
373
23.0M
  } else {
374
3.35M
    if (jpc_bitstream_inalign(inb, 0x7f, 0)) {
375
303
      jas_logerrorf("alignment failed\n");
376
303
      jpc_bitstream_close(inb);
377
303
      return -1;
378
303
    }
379
3.35M
  }
380
26.4M
  jpc_bitstream_close(inb);
381
382
26.4M
  if (jas_get_debug_level() >= 5) {
383
0
    const uint_least64_t hdroffend = jas_stream_getrwcount(pkthdrstream);
384
0
    const unsigned long hdrlen = hdroffend - hdroffstart;
385
0
    jas_logdebugf(5, "hdrlen=%lu bodylen=%lu \n", (unsigned long) hdrlen,
386
0
      (unsigned long) bodylen);
387
0
  }
388
389
26.4M
  if (cp->csty & JPC_COD_EPH) {
390
190k
    if (!(ms = jpc_getms(pkthdrstream, dec->cstate))) {
391
69
      jas_logerrorf("cannot get (EPH) marker segment\n");
392
69
      return -1;
393
69
    }
394
190k
    if (jpc_ms_gettype(ms) != JPC_MS_EPH) {
395
4
      jpc_ms_destroy(ms);
396
4
      jas_logerrorf("missing EPH marker segment\n");
397
4
      return -1;
398
4
    }
399
190k
    jpc_ms_destroy(ms);
400
190k
  }
401
402
  /* decode the packet body. */
403
404
26.4M
  if (jas_get_debug_level() >= 1) {
405
0
    jas_logdebugf(1, "packet body offset=%06ld\n", (long) jas_stream_getrwcount(in));
406
0
  }
407
408
26.4M
  if (!discard) {
409
13.1M
    tcomp = &tile->tcomps[compno];
410
13.1M
    rlvl = &tcomp->rlvls[rlvlno];
411
13.1M
    unsigned bandno;
412
45.8M
    for (bandno = 0, band = rlvl->bands; bandno < (unsigned)rlvl->numbands;
413
32.7M
      ++bandno, ++band) {
414
32.7M
      if (!band->data) {
415
26.6M
        continue;
416
26.6M
      }
417
6.05M
      prc = &band->prcs[prcno];
418
6.05M
      if (!prc->cblks) {
419
110k
        continue;
420
110k
      }
421
5.94M
      unsigned cblkno;
422
590M
      for (cblkno = 0, cblk = prc->cblks; cblkno < (unsigned)prc->numcblks;
423
584M
        ++cblkno, ++cblk) {
424
584M
        seg = cblk->curseg;
425
586M
        while (seg) {
426
1.93M
          if (!seg->stream) {
427
268k
            if (!(seg->stream = jas_stream_memopen(0, 0))) {
428
0
              jas_logerrorf("cannot open memory stream\n");
429
0
              return -1;
430
0
            }
431
268k
          }
432
#if 0
433
          jas_eprintf("lyrno=%02d, compno=%02d, lvlno=%02d, prcno=%02d, bandno=%02d, cblkno=%02d, passno=%02d numpasses=%02d cnt=%d numbps=%d, numimsbs=%d\n",
434
            lyrno, compno, rlvlno, prcno, band - rlvl->bands,
435
            cblk - prc->cblks, seg->passno, seg->numpasses, seg->cnt,
436
            band->numbps, cblk->numimsbs);
437
#endif
438
1.93M
          if (seg->cnt > 0) {
439
333k
            if (jpc_getdata(in, seg->stream, seg->cnt) < 0) {
440
403
              jas_logerrorf("jas_getdata failed\n");
441
403
              return -1;
442
403
            }
443
332k
            seg->cnt = 0;
444
332k
          }
445
1.93M
          if (seg->numpasses >= seg->maxpasses) {
446
121k
            cblk->curseg = seg->next;
447
121k
          }
448
1.93M
          seg = seg->next;
449
1.93M
        }
450
584M
      }
451
5.94M
    }
452
13.3M
  } else {
453
13.3M
    if (jas_stream_gobble(in, bodylen) != JAS_CAST(int, bodylen)) {
454
143
      jas_logerrorf("jas_stream_gobble failed\n");
455
143
      return -1;
456
143
    }
457
13.3M
  }
458
26.4M
  return 0;
459
26.4M
}
460
461
/********************************************************************************************/
462
/********************************************************************************************/
463
464
int jpc_dec_decodepkts(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in)
465
8.75k
{
466
8.75k
  jpc_dec_tile_t *tile;
467
8.75k
  jpc_pi_t *pi;
468
8.75k
  int ret;
469
470
8.75k
  tile = dec->curtile;
471
8.75k
  pi = tile->pi;
472
26.4M
  for (;;) {
473
26.4M
    if (!tile->pkthdrstream ||
474
26.2M
      jas_stream_peekc(tile->pkthdrstream) == EOF) {
475
26.2M
      switch (jpc_dec_lookahead(in)) {
476
2.97k
      case JPC_MS_EOC:
477
6.38k
      case JPC_MS_SOT:
478
6.38k
        return 0;
479
12.6k
      case JPC_MS_SOP:
480
13.9k
      case JPC_MS_EPH:
481
26.2M
      case 0:
482
26.2M
        break;
483
564
      default:
484
564
        jas_logerrorf("jpc_dec_lookahead failed\n");
485
564
        return -1;
486
26.2M
      }
487
26.2M
    }
488
    // jpc_pi_dump(pi);
489
26.4M
    if ((ret = jpc_pi_next(pi))) {
490
468
      if (ret < 0) {
491
0
        jas_logerrorf("jpc_pi_next failed\n");
492
0
      }
493
      //jas_logerrorf("jpc_pi_next failed (%d)\n", ret);
494
468
      return ret;
495
468
    }
496
26.4M
    if (dec->maxpkts >= 0 && dec->numpkts >= (unsigned)dec->maxpkts) {
497
0
      jas_logwarnf("warning: stopping decode prematurely as requested\n");
498
0
      return 0;
499
0
    }
500
26.4M
    if (jas_get_debug_level() >= 1) {
501
0
      jas_logdebugf(1, "packet offset=%08ld prg=%d cmptno=%02d "
502
0
        "rlvlno=%02d prcno=%03d lyrno=%02d\n", (long)
503
0
        jas_stream_getrwcount(in), jpc_pi_prg(pi), jpc_pi_cmptno(pi),
504
0
        jpc_pi_rlvlno(pi), jpc_pi_prcno(pi), jpc_pi_lyrno(pi));
505
0
    }
506
26.4M
    if (jpc_dec_decodepkt(dec, pkthdrstream, in, jpc_pi_cmptno(pi),
507
26.4M
      jpc_pi_rlvlno(pi), jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) {
508
1.34k
      jas_logerrorf("jpc_dec_decodepkt failed\n");
509
1.34k
      return -1;
510
1.34k
    }
511
26.4M
    ++dec->numpkts;
512
26.4M
  }
513
514
0
  return 0;
515
8.75k
}
516
517
jpc_pi_t *jpc_dec_pi_create(jpc_dec_t *dec, jpc_dec_tile_t *tile)
518
8.30k
{
519
8.30k
  jpc_pi_t *pi;
520
8.30k
  unsigned compno;
521
8.30k
  jpc_picomp_t *picomp;
522
8.30k
  jpc_pirlvl_t *pirlvl;
523
8.30k
  jpc_dec_tcomp_t *tcomp;
524
8.30k
  unsigned rlvlno;
525
8.30k
  jpc_dec_rlvl_t *rlvl;
526
8.30k
  unsigned prcno;
527
8.30k
  unsigned *prclyrno;
528
8.30k
  jpc_dec_cmpt_t *cmpt;
529
530
8.30k
  if (!(pi = jpc_pi_create0())) {
531
0
    return 0;
532
0
  }
533
8.30k
  pi->numcomps = dec->numcomps;
534
8.30k
  if (!(pi->picomps = jas_alloc2(pi->numcomps, sizeof(jpc_picomp_t)))) {
535
0
    jpc_pi_destroy(pi);
536
0
    return 0;
537
0
  }
538
33.2k
  for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno,
539
24.9k
    ++picomp) {
540
24.9k
    picomp->pirlvls = 0;
541
24.9k
  }
542
543
8.30k
  for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps;
544
33.2k
    compno < pi->numcomps; ++compno, ++tcomp, ++picomp) {
545
24.9k
    picomp->numrlvls = tcomp->numrlvls;
546
24.9k
    if (!(picomp->pirlvls = jas_alloc2(picomp->numrlvls,
547
24.9k
      sizeof(jpc_pirlvl_t)))) {
548
0
      jpc_pi_destroy(pi);
549
0
      return 0;
550
0
    }
551
163k
    for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
552
163k
      picomp->numrlvls; ++rlvlno, ++pirlvl) {
553
138k
      pirlvl->prclyrnos = 0;
554
138k
    }
555
24.9k
    for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
556
163k
      rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
557
/* XXX sizeof(long) should be sizeof different type */
558
138k
      pirlvl->numprcs = rlvl->numprcs;
559
138k
      if (!(pirlvl->prclyrnos = jas_alloc2(pirlvl->numprcs,
560
138k
        sizeof(long)))) {
561
6
        jpc_pi_destroy(pi);
562
6
        return 0;
563
6
      }
564
138k
    }
565
24.9k
  }
566
567
8.29k
  pi->maxrlvls = 0;
568
8.29k
  for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps, cmpt =
569
33.2k
    dec->cmpts; compno < pi->numcomps; ++compno, ++tcomp, ++picomp,
570
24.9k
    ++cmpt) {
571
24.9k
    picomp->hsamp = cmpt->hstep;
572
24.9k
    picomp->vsamp = cmpt->vstep;
573
24.9k
    for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
574
163k
      rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
575
138k
      pirlvl->prcwidthexpn = rlvl->prcwidthexpn;
576
138k
      pirlvl->prcheightexpn = rlvl->prcheightexpn;
577
138k
      for (prcno = 0, prclyrno = pirlvl->prclyrnos;
578
8.51M
        prcno < pirlvl->numprcs; ++prcno, ++prclyrno) {
579
8.38M
        *prclyrno = 0;
580
8.38M
      }
581
138k
      pirlvl->numhprcs = rlvl->numhprcs;
582
138k
    }
583
24.9k
    if (pi->maxrlvls < tcomp->numrlvls) {
584
8.33k
      pi->maxrlvls = tcomp->numrlvls;
585
8.33k
    }
586
24.9k
  }
587
588
8.29k
  pi->numlyrs = tile->cp->numlyrs;
589
8.29k
  pi->xstart = tile->xstart;
590
8.29k
  pi->ystart = tile->ystart;
591
8.29k
  pi->xend = tile->xend;
592
8.29k
  pi->yend = tile->yend;
593
594
8.29k
  pi->picomp = 0;
595
8.29k
  pi->pirlvl = 0;
596
8.29k
  pi->x = 0;
597
8.29k
  pi->y = 0;
598
8.29k
  pi->compno = 0;
599
8.29k
  pi->rlvlno = 0;
600
8.29k
  pi->prcno = 0;
601
8.29k
  pi->lyrno = 0;
602
8.29k
  pi->xstep = 0;
603
8.29k
  pi->ystep = 0;
604
605
8.29k
  pi->pchgno = -1;
606
607
8.29k
  pi->defaultpchg.prgord = tile->cp->prgord;
608
8.29k
  pi->defaultpchg.compnostart = 0;
609
8.29k
  pi->defaultpchg.compnoend = pi->numcomps;
610
8.29k
  pi->defaultpchg.rlvlnostart = 0;
611
8.29k
  pi->defaultpchg.rlvlnoend = pi->maxrlvls;
612
8.29k
  pi->defaultpchg.lyrnoend = pi->numlyrs;
613
8.29k
  pi->pchg = 0;
614
615
8.29k
  pi->valid = 0;
616
617
8.29k
  return pi;
618
8.30k
}
619
620
static long jpc_dec_lookahead(jas_stream_t *in)
621
27.5M
{
622
27.5M
  uint_fast16_t x;
623
27.5M
  if (jpc_getuint16(in, &x)) {
624
779
    return -1;
625
779
  }
626
27.5M
  if (jas_stream_ungetc(in, x & 0xff) == EOF ||
627
27.5M
    jas_stream_ungetc(in, x >> 8) == EOF) {
628
0
    return -1;
629
0
  }
630
27.5M
  if (x >= JPC_MS_INMIN && x <= JPC_MS_INMAX) {
631
210k
    return x;
632
210k
  }
633
27.3M
  return 0;
634
27.5M
}