Coverage Report

Created: 2026-06-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jasper/src/libjasper/jpc/jpc_dec.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
 * $Id$
66
 */
67
68
/******************************************************************************\
69
* Includes.
70
\******************************************************************************/
71
72
#include "jpc_dec.h"
73
#include "jpc_fix.h"
74
#include "jpc_cs.h"
75
#include "jpc_mct.h"
76
#include "jpc_t2dec.h"
77
#include "jpc_t1cod.h"
78
#include "jpc_t1dec.h"
79
#include "jpc_math.h"
80
81
#include "jasper/jas_init.h"
82
#include "jasper/jas_types.h"
83
#include "jasper/jas_math.h"
84
#include "jasper/jas_tvp.h"
85
#include "jasper/jas_malloc.h"
86
#include "jasper/jas_debug.h"
87
88
#include <stdio.h>
89
#include <stdlib.h>
90
#include <assert.h>
91
92
/******************************************************************************\
93
*
94
\******************************************************************************/
95
96
204k
#define JPC_MHSOC 0x0001
97
  /* In the main header, expecting a SOC marker segment. */
98
203k
#define JPC_MHSIZ 0x0002
99
  /* In the main header, expecting a SIZ marker segment. */
100
589k
#define JPC_MH    0x0004
101
  /* In the main header, expecting "other" marker segments. */
102
10.0k
#define JPC_TPHSOT  0x0008
103
  /* In a tile-part header, expecting a SOT marker segment. */
104
24.8k
#define JPC_TPH   0x0010
105
  /* In a tile-part header, expecting "other" marker segments. */
106
3.81k
#define JPC_MT    0x0020
107
  /* In the main trailer. */
108
109
typedef struct {
110
111
  uint_least16_t id;
112
  /* The marker segment type. */
113
114
  uint_least16_t validstates;
115
  /* The states in which this type of marker segment can be
116
    validly encountered. */
117
118
  int (*action)(jpc_dec_t *dec, jpc_ms_t *ms);
119
  /* The action to take upon encountering this type of marker segment. */
120
121
} jpc_dec_mstabent_t;
122
123
/******************************************************************************\
124
*
125
\******************************************************************************/
126
127
/* COD/COC parameters have been specified. */
128
165k
#define JPC_CSET  0x0001
129
/* QCD/QCC parameters have been specified. */
130
2.28M
#define JPC_QSET  0x0002
131
/* COD/COC parameters set from a COC marker segment. */
132
195k
#define JPC_COC 0x0004
133
/* QCD/QCC parameters set from a QCC marker segment. */
134
3.44M
#define JPC_QCC 0x0008
135
136
/******************************************************************************\
137
* Local function prototypes.
138
\******************************************************************************/
139
140
static int jpc_dec_dump(const jpc_dec_t *dec);
141
142
static jpc_ppxstab_t *jpc_ppxstab_create(void);
143
static void jpc_ppxstab_destroy(jpc_ppxstab_t *tab);
144
static int jpc_ppxstab_grow(jpc_ppxstab_t *tab, unsigned maxents);
145
static int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent);
146
static jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab);
147
static int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab);
148
static jpc_ppxstabent_t *jpc_ppxstabent_create(void);
149
static void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent);
150
151
static int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist);
152
static jpc_streamlist_t *jpc_streamlist_create(void);
153
154
static int jpc_streamlist_insert(jpc_streamlist_t *streamlist, unsigned streamno,
155
  jas_stream_t *stream);
156
static jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, unsigned streamno);
157
static void jpc_streamlist_destroy(jpc_streamlist_t *streamlist);
158
159
static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp);
160
static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps);
161
static int jpc_dec_cp_isvalid(const jpc_dec_cp_t *cp);
162
static jpc_dec_cp_t *jpc_dec_cp_copy(const jpc_dec_cp_t *cp);
163
static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, const jpc_cod_t *cod);
164
static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, const jpc_coc_t *coc);
165
static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp,
166
  const jpc_coxcp_t *compparms, unsigned flags);
167
static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, const jpc_qcd_t *qcd);
168
static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, const jpc_qcc_t *qcc);
169
static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp,
170
  const jpc_qcxcp_t *compparms, unsigned flags);
171
static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, const jpc_rgn_t *rgn);
172
static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp);
173
static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp);
174
static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, const jpc_poc_t *poc, int reset);
175
static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, const jpc_poc_t *poc);
176
177
static int jpc_dec_decode(jpc_dec_t *dec);
178
static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts,
179
  jas_stream_t *in);
180
static void jpc_dec_destroy(jpc_dec_t *dec);
181
static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize);
182
static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, unsigned numbps);
183
static jpc_fix_t jpc_calcabsstepsize(unsigned stepsize, unsigned numbits);
184
static int jpc_dec_tiledecode(jpc_dec_t *dec, jpc_dec_tile_t *tile);
185
static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile);
186
static int jpc_dec_tilefini(jpc_dec_t *dec, jpc_dec_tile_t *tile);
187
static int jpc_dec_process_soc(jpc_dec_t *dec, jpc_ms_t *ms);
188
static int jpc_dec_process_sot(jpc_dec_t *dec, jpc_ms_t *ms);
189
static int jpc_dec_process_sod(jpc_dec_t *dec, jpc_ms_t *ms);
190
static int jpc_dec_process_eoc(jpc_dec_t *dec, jpc_ms_t *ms);
191
static int jpc_dec_process_siz(jpc_dec_t *dec, jpc_ms_t *ms);
192
static int jpc_dec_process_cod(jpc_dec_t *dec, jpc_ms_t *ms);
193
static int jpc_dec_process_coc(jpc_dec_t *dec, jpc_ms_t *ms);
194
static int jpc_dec_process_rgn(jpc_dec_t *dec, jpc_ms_t *ms);
195
static int jpc_dec_process_qcd(jpc_dec_t *dec, jpc_ms_t *ms);
196
static int jpc_dec_process_qcc(jpc_dec_t *dec, jpc_ms_t *ms);
197
static int jpc_dec_process_poc(jpc_dec_t *dec, jpc_ms_t *ms);
198
static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms);
199
static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms);
200
static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms);
201
static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms);
202
static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms);
203
static jpc_dec_importopts_t *jpc_dec_opts_create(const char *optstr);
204
static void jpc_dec_opts_destroy(jpc_dec_importopts_t *opts);
205
206
static const jpc_dec_mstabent_t *jpc_dec_mstab_lookup(uint_fast16_t id);
207
208
static void jpc_rlvl_init(jpc_dec_rlvl_t *rlvl);
209
static void jpc_band_init(jpc_dec_band_t *band);
210
static void jpc_prc_init(jpc_dec_prc_t *prc);
211
static void jpc_cblk_init(jpc_dec_cblk_t *cblk);
212
static void jpc_seglist_init(jpc_dec_seglist_t *seglist);
213
214
/******************************************************************************\
215
* Global data.
216
\******************************************************************************/
217
218
static const jpc_dec_mstabent_t jpc_dec_mstab[] = {
219
  {JPC_MS_SOC, JPC_MHSOC, jpc_dec_process_soc},
220
  {JPC_MS_SOT, JPC_MH | JPC_TPHSOT, jpc_dec_process_sot},
221
  {JPC_MS_SOD, JPC_TPH, jpc_dec_process_sod},
222
  {JPC_MS_EOC, JPC_TPHSOT, jpc_dec_process_eoc},
223
  {JPC_MS_SIZ, JPC_MHSIZ, jpc_dec_process_siz},
224
  {JPC_MS_COD, JPC_MH | JPC_TPH, jpc_dec_process_cod},
225
  {JPC_MS_COC, JPC_MH | JPC_TPH, jpc_dec_process_coc},
226
  {JPC_MS_RGN, JPC_MH | JPC_TPH, jpc_dec_process_rgn},
227
  {JPC_MS_QCD, JPC_MH | JPC_TPH, jpc_dec_process_qcd},
228
  {JPC_MS_QCC, JPC_MH | JPC_TPH, jpc_dec_process_qcc},
229
  {JPC_MS_POC, JPC_MH | JPC_TPH, jpc_dec_process_poc},
230
  {JPC_MS_TLM, JPC_MH, 0},
231
  {JPC_MS_PLM, JPC_MH, 0},
232
  {JPC_MS_PLT, JPC_TPH, 0},
233
  {JPC_MS_PPM, JPC_MH, jpc_dec_process_ppm},
234
  {JPC_MS_PPT, JPC_TPH, jpc_dec_process_ppt},
235
  {JPC_MS_SOP, 0, 0},
236
  {JPC_MS_CRG, JPC_MH, jpc_dec_process_crg},
237
  {JPC_MS_COM, JPC_MH | JPC_TPH, jpc_dec_process_com},
238
  {0, JPC_MH | JPC_TPH, jpc_dec_process_unk}
239
};
240
241
/******************************************************************************\
242
* The main entry point for the JPEG-2000 decoder.
243
\******************************************************************************/
244
245
jas_image_t *jpc_decode(jas_stream_t *in, const char *optstr)
246
204k
{
247
204k
  jpc_dec_importopts_t *opts;
248
204k
  jpc_dec_t *dec;
249
204k
  jas_image_t *image;
250
251
204k
  dec = 0;
252
204k
  opts = 0;
253
254
204k
  JAS_LOGDEBUGF(100, "jpc_decode(%p, \"%s\")\n", in, optstr);
255
256
204k
  if (!(opts = jpc_dec_opts_create(optstr))) {
257
0
    goto error;
258
0
  }
259
260
204k
  jpc_init();
261
  //jpc_initluts();
262
263
204k
  if (!(dec = jpc_dec_create(opts, in))) {
264
0
    goto error;
265
0
  }
266
204k
  jpc_dec_opts_destroy(opts);
267
204k
  opts = 0;
268
269
  /* Do most of the work. */
270
204k
  if (jpc_dec_decode(dec)) {
271
200k
    goto error;
272
200k
  }
273
274
3.81k
  if (jas_image_numcmpts(dec->image) >= 3) {
275
816
    jas_image_setclrspc(dec->image, JAS_CLRSPC_SRGB);
276
816
    jas_image_setcmpttype(dec->image, 0,
277
816
      JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
278
816
    jas_image_setcmpttype(dec->image, 1,
279
816
      JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
280
816
    jas_image_setcmpttype(dec->image, 2,
281
816
      JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
282
2.99k
  } else {
283
2.99k
    jas_image_setclrspc(dec->image, JAS_CLRSPC_SGRAY);
284
2.99k
    jas_image_setcmpttype(dec->image, 0,
285
2.99k
      JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
286
2.99k
  }
287
288
  /* Save the return value. */
289
3.81k
  image = dec->image;
290
291
  /* Stop the image from being discarded. */
292
3.81k
  dec->image = 0;
293
294
  /* Destroy decoder. */
295
3.81k
  jpc_dec_destroy(dec);
296
297
3.81k
  return image;
298
299
200k
error:
300
200k
  if (opts) {
301
0
    jpc_dec_opts_destroy(opts);
302
0
  }
303
200k
  if (dec) {
304
200k
    jpc_dec_destroy(dec);
305
200k
  }
306
200k
  return 0;
307
204k
}
308
309
typedef enum {
310
  OPT_MAXLYRS,
311
  OPT_MAXPKTS,
312
  OPT_MAXSAMPLES,
313
  OPT_DEBUG
314
} optid_t;
315
316
static const jas_taginfo_t decopts[] = {
317
  {OPT_MAXLYRS, "maxlyrs"},
318
  {OPT_MAXPKTS, "maxpkts"},
319
  {OPT_MAXSAMPLES, "max_samples"},
320
  {OPT_DEBUG, "debug"},
321
  {-1, 0}
322
};
323
324
static jpc_dec_importopts_t *jpc_dec_opts_create(const char *optstr)
325
204k
{
326
204k
  jpc_dec_importopts_t *opts;
327
204k
  jas_tvparser_t *tvp;
328
329
204k
  opts = 0;
330
331
204k
  if (!(opts = jas_malloc(sizeof(jpc_dec_importopts_t)))) {
332
0
    goto error;
333
0
  }
334
335
204k
  opts->debug = 0;
336
204k
  opts->maxlyrs = JPC_MAXLYRS;
337
204k
  opts->maxpkts = -1;
338
204k
  opts->max_samples = jas_get_dec_default_max_samples();
339
340
204k
  if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) {
341
0
    goto error;
342
0
  }
343
344
408k
  while (!jas_tvparser_next(tvp)) {
345
204k
    switch (jas_taginfo_nonull(jas_taginfos_lookup(decopts,
346
204k
      jas_tvparser_gettag(tvp)))->id) {
347
0
    case OPT_MAXLYRS:
348
0
      opts->maxlyrs = atoi(jas_tvparser_getval(tvp));
349
0
      break;
350
0
    case OPT_DEBUG:
351
0
      opts->debug = atoi(jas_tvparser_getval(tvp));
352
0
      break;
353
0
    case OPT_MAXPKTS:
354
0
      opts->maxpkts = atoi(jas_tvparser_getval(tvp));
355
0
      break;
356
204k
    case OPT_MAXSAMPLES:
357
204k
      opts->max_samples = strtoull(jas_tvparser_getval(tvp), 0, 10);
358
204k
      break;
359
0
    default:
360
0
      jas_logwarnf("warning: ignoring invalid option %s\n",
361
0
        jas_tvparser_gettag(tvp));
362
0
      break;
363
204k
    }
364
204k
  }
365
366
204k
  jas_tvparser_destroy(tvp);
367
368
204k
  return opts;
369
370
0
error:
371
0
  if (opts) {
372
0
    jpc_dec_opts_destroy(opts);
373
0
  }
374
0
  return 0;
375
204k
}
376
377
static void jpc_dec_opts_destroy(jpc_dec_importopts_t *opts)
378
204k
{
379
204k
  jas_free(opts);
380
204k
}
381
382
/******************************************************************************\
383
* Code for table-driven code stream decoder.
384
\******************************************************************************/
385
386
JAS_ATTRIBUTE_CONST
387
static const jpc_dec_mstabent_t *jpc_dec_mstab_lookup(uint_fast16_t id)
388
1.09M
{
389
1.09M
  const jpc_dec_mstabent_t *mstabent;
390
9.23M
  for (mstabent = jpc_dec_mstab; mstabent->id != 0; ++mstabent) {
391
9.20M
    if (mstabent->id == id) {
392
1.06M
      break;
393
1.06M
    }
394
9.20M
  }
395
1.09M
  return mstabent;
396
1.09M
}
397
398
static int jpc_dec_decode(jpc_dec_t *dec)
399
204k
{
400
204k
  jpc_ms_t *ms;
401
204k
  const jpc_dec_mstabent_t *mstabent;
402
204k
  int ret;
403
204k
  jpc_cstate_t *cstate;
404
405
204k
  if (!(cstate = jpc_cstate_create())) {
406
0
    return -1;
407
0
  }
408
204k
  dec->cstate = cstate;
409
410
  /* Initially, we should expect to encounter a SOC marker segment. */
411
204k
  dec->state = JPC_MHSOC;
412
413
1.22M
  for (;;) {
414
415
    /* Get the next marker segment in the code stream. */
416
1.22M
    if (!(ms = jpc_getms(dec->in, cstate))) {
417
126k
      jas_logerrorf("cannot get marker segment\n");
418
126k
      return -1;
419
126k
    }
420
421
1.09M
    mstabent = jpc_dec_mstab_lookup(ms->id);
422
1.09M
    assert(mstabent);
423
424
    /* Ensure that this type of marker segment is permitted
425
      at this point in the code stream. */
426
1.09M
    if (!(dec->state & mstabent->validstates)) {
427
42.6k
      jas_logerrorf("unexpected marker segment type\n");
428
42.6k
      jpc_ms_destroy(ms);
429
42.6k
      return -1;
430
42.6k
    }
431
432
    /* Process the marker segment. */
433
1.05M
    if (mstabent->action) {
434
1.05M
      ret = (*mstabent->action)(dec, ms);
435
1.05M
    } else {
436
      /* No explicit action is required. */
437
904
      ret = 0;
438
904
    }
439
440
    /* Destroy the marker segment. */
441
1.05M
    jpc_ms_destroy(ms);
442
443
1.05M
    if (ret < 0) {
444
30.7k
      return -1;
445
1.02M
    } else if (ret > 0) {
446
3.81k
      break;
447
3.81k
    }
448
449
1.05M
  }
450
451
3.81k
  return 0;
452
204k
}
453
454
static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms)
455
2.11k
{
456
2.11k
  jpc_dec_cmpt_t *cmpt;
457
2.11k
  jpc_crg_t *crg;
458
459
2.11k
  crg = &ms->parms.crg;
460
2.11k
  unsigned cmptno;
461
8.40k
  for (cmptno = 0, cmpt = dec->cmpts; cmptno < dec->numcomps; ++cmptno,
462
6.28k
    ++cmpt) {
463
    /* Ignore the information in the CRG marker segment for now.
464
      This information serves no useful purpose for decoding anyhow.
465
      Some other parts of the code need to be changed if these lines
466
      are uncommented.
467
    cmpt->hsubstep = crg->comps[cmptno].hoff;
468
    cmpt->vsubstep = crg->comps[cmptno].voff;
469
    */
470
    /* suppress -Wunused-but-set-variable */
471
6.28k
    JAS_UNUSED(crg);
472
6.28k
  }
473
2.11k
  return 0;
474
2.11k
}
475
476
static int jpc_dec_process_soc(jpc_dec_t *dec, jpc_ms_t *ms)
477
203k
{
478
203k
  JAS_UNUSED(ms);
479
480
  /* We should expect to encounter a SIZ marker segment next. */
481
203k
  dec->state = JPC_MHSIZ;
482
483
203k
  return 0;
484
203k
}
485
486
static int jpc_dec_process_sot(jpc_dec_t *dec, jpc_ms_t *ms)
487
15.3k
{
488
15.3k
  jpc_dec_tile_t *tile;
489
15.3k
  jpc_sot_t *sot = &ms->parms.sot;
490
15.3k
  jas_image_cmptparm_t *compinfos;
491
15.3k
  jas_image_cmptparm_t *compinfo;
492
15.3k
  jpc_dec_cmpt_t *cmpt;
493
494
15.3k
  if (dec->state == JPC_MH) {
495
496
10.0k
    if (!(compinfos = jas_alloc2(dec->numcomps,
497
10.0k
      sizeof(jas_image_cmptparm_t)))) {
498
0
      return -1;
499
0
    }
500
10.0k
    unsigned cmptno;
501
10.0k
    for (cmptno = 0, cmpt = dec->cmpts, compinfo = compinfos;
502
43.2k
      cmptno < dec->numcomps; ++cmptno, ++cmpt, ++compinfo) {
503
33.1k
      compinfo->tlx = 0;
504
33.1k
      compinfo->tly = 0;
505
33.1k
      compinfo->prec = cmpt->prec;
506
33.1k
      compinfo->sgnd = cmpt->sgnd;
507
33.1k
      compinfo->width = cmpt->width;
508
33.1k
      compinfo->height = cmpt->height;
509
33.1k
      compinfo->hstep = cmpt->hstep;
510
33.1k
      compinfo->vstep = cmpt->vstep;
511
33.1k
    }
512
513
10.0k
    if (!(dec->image = jas_image_create(dec->numcomps, compinfos,
514
10.0k
      JAS_CLRSPC_UNKNOWN))) {
515
0
      jas_free(compinfos);
516
0
      return -1;
517
0
    }
518
10.0k
    jas_free(compinfos);
519
520
    /* Is the packet header information stored in PPM marker segments in
521
      the main header? */
522
10.0k
    if (dec->ppmstab) {
523
      /* Convert the PPM marker segment data into a collection of streams
524
        (one stream per tile-part). */
525
376
      if (!(dec->pkthdrstreams = jpc_ppmstabtostreams(dec->ppmstab))) {
526
212
        return -1;
527
212
      }
528
164
      jpc_ppxstab_destroy(dec->ppmstab);
529
164
      dec->ppmstab = 0;
530
164
    }
531
10.0k
  }
532
533
15.1k
  if (sot->len > 0) {
534
15.1k
    dec->curtileendoff = jas_stream_getrwcount(dec->in) - ms->len -
535
15.1k
      4 + sot->len;
536
15.1k
  } else {
537
0
    dec->curtileendoff = 0;
538
0
  }
539
540
15.1k
  if (sot->tileno >= dec->numtiles) {
541
151
    jas_logerrorf("invalid tile number in SOT marker segment\n");
542
151
    return -1;
543
151
  }
544
  /* Set the current tile. */
545
14.9k
  dec->curtile = &dec->tiles[sot->tileno];
546
14.9k
  tile = dec->curtile;
547
  /* Ensure that this is the expected part number. */
548
14.9k
  if (sot->partno != tile->partno) {
549
85
    return -1;
550
85
  }
551
14.8k
  if (tile->numparts > 0 && sot->partno >= tile->numparts) {
552
9
    return -1;
553
9
  }
554
14.8k
  if (!tile->numparts && sot->numparts > 0) {
555
13.2k
    tile->numparts = sot->numparts;
556
13.2k
  }
557
558
14.8k
  tile->pptstab = 0;
559
560
14.8k
  switch (tile->state) {
561
14.1k
  case JPC_TILE_INIT:
562
    /* This is the first tile-part for this tile. */
563
14.1k
    tile->state = JPC_TILE_ACTIVE;
564
14.1k
    assert(!tile->cp);
565
14.1k
    if (!(tile->cp = jpc_dec_cp_copy(dec->cp))) {
566
0
      return -1;
567
0
    }
568
14.1k
    jpc_dec_cp_resetflags(dec->cp);
569
14.1k
    break;
570
748
  default:
571
748
    if (sot->numparts == sot->partno - 1) {
572
28
      tile->state = JPC_TILE_ACTIVELAST;
573
28
    }
574
748
    break;
575
14.8k
  }
576
577
  /* Note: We do not increment the expected tile-part number until
578
    all processing for this tile-part is complete. */
579
580
  /* We should expect to encounter other tile-part header marker
581
    segments next. */
582
14.8k
  dec->state = JPC_TPH;
583
584
14.8k
  return 0;
585
14.8k
}
586
587
static int jpc_dec_process_sod(jpc_dec_t *dec, jpc_ms_t *ms)
588
14.2k
{
589
14.2k
  jpc_dec_tile_t *tile;
590
14.2k
  int pos;
591
592
14.2k
  JAS_UNUSED(ms);
593
594
14.2k
  if (!(tile = dec->curtile)) {
595
0
    return -1;
596
0
  }
597
598
14.2k
  if (!tile->partno) {
599
13.5k
    if (!jpc_dec_cp_isvalid(tile->cp)) {
600
45
      return -1;
601
45
    }
602
13.5k
    if (jpc_dec_cp_prepare(tile->cp)) {
603
6
      return -1;
604
6
    }
605
13.5k
    if (jpc_dec_tileinit(dec, tile)) {
606
137
      return -1;
607
137
    }
608
13.5k
  }
609
610
  /* Are packet headers stored in the main header or tile-part header? */
611
14.0k
  if (dec->pkthdrstreams) {
612
    /* Get the stream containing the packet header data for this
613
      tile-part. */
614
1.61k
    if (jpc_streamlist_numstreams(dec->pkthdrstreams) != 0 &&
615
1.61k
      !(tile->pkthdrstream = jpc_streamlist_remove(dec->pkthdrstreams,
616
1.61k
      0))) {
617
0
      return -1;
618
0
    }
619
1.61k
  }
620
621
14.0k
  if (tile->pptstab) {
622
515
    if (!tile->pkthdrstream) {
623
509
      if (!(tile->pkthdrstream = jas_stream_memopen(0, 0))) {
624
0
        return -1;
625
0
      }
626
509
    }
627
515
    pos = jas_stream_tell(tile->pkthdrstream);
628
515
    jas_stream_seek(tile->pkthdrstream, 0, SEEK_END);
629
515
    if (jpc_pptstabwrite(tile->pkthdrstream, tile->pptstab)) {
630
0
      return -1;
631
0
    }
632
515
    jas_stream_seek(tile->pkthdrstream, pos, SEEK_SET);
633
515
    jpc_ppxstab_destroy(tile->pptstab);
634
515
    tile->pptstab = 0;
635
515
  }
636
637
14.0k
  if (jas_get_debug_level() >= 10) {
638
0
    jpc_dec_dump(dec);
639
0
  }
640
641
14.0k
  if (jpc_dec_decodepkts(dec, (tile->pkthdrstream) ? tile->pkthdrstream :
642
14.0k
    dec->in, dec->in)) {
643
3.45k
    jas_logerrorf("jpc_dec_decodepkts failed\n");
644
3.45k
    return -1;
645
3.45k
  }
646
647
  /* Gobble any unconsumed tile data. */
648
10.5k
  if (dec->curtileendoff > 0) {
649
10.5k
    long curoff;
650
10.5k
    uint_fast32_t n;
651
10.5k
    curoff = jas_stream_getrwcount(dec->in);
652
10.5k
    if (curoff < dec->curtileendoff) {
653
518
      n = dec->curtileendoff - curoff;
654
518
      jas_logwarnf("warning: ignoring trailing garbage (%lu bytes)\n",
655
518
        (unsigned long) n);
656
657
10.7k
      while (n-- > 0) {
658
10.3k
        if (jas_stream_getc(dec->in) == EOF) {
659
182
          jas_logerrorf("read error\n");
660
182
          return -1;
661
182
        }
662
10.3k
      }
663
10.0k
    } else if (curoff > dec->curtileendoff) {
664
3.15k
      jas_logwarnf("warning: not enough tile data (%lu bytes)\n",
665
3.15k
        (unsigned long) curoff - dec->curtileendoff);
666
3.15k
    }
667
668
10.5k
  }
669
670
10.3k
  if (tile->numparts > 0 && tile->partno == tile->numparts - 1) {
671
5.39k
    if (jpc_dec_tiledecode(dec, tile)) {
672
374
      return -1;
673
374
    }
674
5.02k
    jpc_dec_tilefini(dec, tile);
675
5.02k
  }
676
677
10.0k
  dec->curtile = 0;
678
679
  /* Increment the expected tile-part number. */
680
10.0k
  ++tile->partno;
681
682
  /* We should expect to encounter a SOT marker segment next. */
683
10.0k
  dec->state = JPC_TPHSOT;
684
685
10.0k
  return 0;
686
10.3k
}
687
688
static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile)
689
13.5k
{
690
13.5k
  jpc_dec_tcomp_t *tcomp;
691
13.5k
  unsigned rlvlno;
692
13.5k
  jpc_dec_rlvl_t *rlvl;
693
13.5k
  jpc_dec_band_t *band;
694
13.5k
  jpc_dec_prc_t *prc;
695
13.5k
  jpc_dec_cblk_t *cblk;
696
13.5k
  uint_fast32_t tlprcxstart;
697
13.5k
  uint_fast32_t tlprcystart;
698
13.5k
  uint_fast32_t brprcxend;
699
13.5k
  uint_fast32_t brprcyend;
700
13.5k
  uint_fast32_t tlcbgxstart;
701
13.5k
  uint_fast32_t tlcbgystart;
702
13.5k
  uint_fast32_t brcbgxend;
703
13.5k
  uint_fast32_t cbgxstart;
704
13.5k
  uint_fast32_t cbgystart;
705
13.5k
  uint_fast32_t cbgxend;
706
13.5k
  uint_fast32_t cbgyend;
707
13.5k
  uint_fast32_t tlcblkxstart;
708
13.5k
  uint_fast32_t tlcblkystart;
709
13.5k
  uint_fast32_t brcblkxend;
710
13.5k
  uint_fast32_t brcblkyend;
711
13.5k
  uint_fast32_t cblkxstart;
712
13.5k
  uint_fast32_t cblkystart;
713
13.5k
  uint_fast32_t cblkxend;
714
13.5k
  uint_fast32_t cblkyend;
715
13.5k
  uint_fast32_t tmpxstart;
716
13.5k
  uint_fast32_t tmpystart;
717
13.5k
  uint_fast32_t tmpxend;
718
13.5k
  uint_fast32_t tmpyend;
719
13.5k
  jpc_tsfb_band_t bnds[JPC_MAXBANDS];
720
13.5k
  jpc_pchg_t *pchg;
721
13.5k
  int retval = 0;
722
723
13.5k
  if (jas_get_debug_level() >= 1) {
724
0
    jas_logerrorf("jpc_dec_tileinit called\n");
725
0
  }
726
727
13.5k
  const jpc_dec_cp_t *cp = tile->cp;
728
13.5k
  tile->realmode = 0;
729
13.5k
  if (cp->mctid == JPC_MCT_ICT) {
730
3.55k
    tile->realmode = 1;
731
3.55k
  }
732
733
13.5k
  unsigned compno;
734
13.5k
  const jpc_dec_cmpt_t *cmpt;
735
49.1k
  for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno <
736
49.1k
    dec->numcomps; ++compno, ++tcomp, ++cmpt) {
737
35.8k
    const jpc_dec_ccp_t *ccp = &tile->cp->ccps[compno];
738
35.8k
    if (jas_get_debug_level() >= 10) {
739
0
      jas_logdebugf(10, "[compno %d]\n", compno);
740
0
    }
741
35.8k
    if (ccp->qmfbid == JPC_COX_INS) {
742
22.2k
      tile->realmode = 1;
743
22.2k
    }
744
35.8k
    tcomp->numrlvls = ccp->numrlvls;
745
35.8k
    if (!(tcomp->rlvls = jas_alloc2(tcomp->numrlvls,
746
35.8k
      sizeof(jpc_dec_rlvl_t)))) {
747
0
      retval = -1;
748
0
      goto done;
749
0
    }
750
248k
    for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls;
751
212k
      ++rlvlno, ++rlvl) {
752
#if 0
753
      rlvl->bands = NULL;
754
#else
755
212k
      jpc_rlvl_init(rlvl);
756
212k
#endif
757
212k
    }
758
35.8k
    if (!(tcomp->data = jas_seq2d_create(JPC_CEILDIV(tile->xstart,
759
35.8k
      cmpt->hstep), JPC_CEILDIV(tile->ystart, cmpt->vstep),
760
35.8k
      JPC_CEILDIV(tile->xend, cmpt->hstep), JPC_CEILDIV(tile->yend,
761
35.8k
      cmpt->vstep)))) {
762
10
      retval = -1;
763
10
      goto done;
764
10
    }
765
35.8k
    if (!(tcomp->tsfb = jpc_cod_gettsfb(ccp->qmfbid,
766
35.8k
      tcomp->numrlvls - 1))) {
767
0
      retval = -1;
768
0
      goto done;
769
0
    }
770
35.8k
    {
771
35.8k
      jpc_tsfb_getbands(tcomp->tsfb, jas_seq2d_xstart(tcomp->data),
772
35.8k
        jas_seq2d_ystart(tcomp->data), jas_seq2d_xend(tcomp->data),
773
35.8k
        jas_seq2d_yend(tcomp->data), bnds);
774
35.8k
    }
775
248k
    for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls;
776
212k
      ++rlvlno, ++rlvl) {
777
212k
      rlvl->bands = 0;
778
212k
      rlvl->xstart = JPC_CEILDIVPOW2(tcomp->xstart,
779
212k
        tcomp->numrlvls - 1 - rlvlno);
780
212k
      rlvl->ystart = JPC_CEILDIVPOW2(tcomp->ystart,
781
212k
        tcomp->numrlvls - 1 - rlvlno);
782
212k
      rlvl->xend = JPC_CEILDIVPOW2(tcomp->xend,
783
212k
        tcomp->numrlvls - 1 - rlvlno);
784
212k
      rlvl->yend = JPC_CEILDIVPOW2(tcomp->yend,
785
212k
        tcomp->numrlvls - 1 - rlvlno);
786
212k
      rlvl->prcwidthexpn = ccp->prcwidthexpns[rlvlno];
787
212k
      rlvl->prcheightexpn = ccp->prcheightexpns[rlvlno];
788
212k
      tlprcxstart = JPC_FLOORDIVPOW2(rlvl->xstart,
789
212k
        rlvl->prcwidthexpn) << rlvl->prcwidthexpn;
790
212k
      tlprcystart = JPC_FLOORDIVPOW2(rlvl->ystart,
791
212k
        rlvl->prcheightexpn) << rlvl->prcheightexpn;
792
212k
      brprcxend = JPC_CEILDIVPOW2(rlvl->xend,
793
212k
        rlvl->prcwidthexpn) << rlvl->prcwidthexpn;
794
212k
      brprcyend = JPC_CEILDIVPOW2(rlvl->yend,
795
212k
        rlvl->prcheightexpn) << rlvl->prcheightexpn;
796
212k
      rlvl->numhprcs = (brprcxend - tlprcxstart) >>
797
212k
        rlvl->prcwidthexpn;
798
212k
      rlvl->numvprcs = (brprcyend - tlprcystart) >>
799
212k
        rlvl->prcheightexpn;
800
212k
      rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs;
801
802
212k
      if (jas_get_debug_level() >= 10) {
803
0
        jas_logdebugf(10,
804
0
          "[compno %d rlvlno %d]: xstart %d ystart %d "
805
0
          "xend %d yend %d prcwidthexpn %d prcheightexpn %d "
806
0
          "numhprcs %d numvprcs %d numprcs %d\n",
807
0
          compno, rlvlno, rlvl->xstart, rlvl->ystart, rlvl->xend,
808
0
          rlvl->yend, rlvl->prcwidthexpn, rlvl->prcheightexpn,
809
0
          rlvl->numhprcs, rlvl->numvprcs, rlvl->numprcs);
810
0
      }
811
812
212k
      if (rlvl->numprcs >= 64 * 1024) {
813
        /* avoid out-of-memory due to
814
           malicious file; this limit is
815
           rather arbitrary; "good" files I
816
           have seen have values 1..12 */
817
6
        return -1;
818
6
      }
819
820
212k
      if (rlvl->xstart >= rlvl->xend || rlvl->ystart >= rlvl->yend) {
821
52.1k
        rlvl->bands = 0;
822
52.1k
        rlvl->numprcs = 0;
823
52.1k
        rlvl->numhprcs = 0;
824
52.1k
        rlvl->numvprcs = 0;
825
52.1k
        continue;
826
52.1k
      }  
827
160k
      if (!rlvlno) {
828
22.3k
        tlcbgxstart = tlprcxstart;
829
22.3k
        tlcbgystart = tlprcystart;
830
22.3k
        brcbgxend = brprcxend;
831
22.3k
        rlvl->cbgwidthexpn = rlvl->prcwidthexpn;
832
22.3k
        rlvl->cbgheightexpn = rlvl->prcheightexpn;
833
137k
      } else {
834
137k
        tlcbgxstart = JPC_CEILDIVPOW2(tlprcxstart, 1);
835
137k
        tlcbgystart = JPC_CEILDIVPOW2(tlprcystart, 1);
836
137k
        brcbgxend = JPC_CEILDIVPOW2(brprcxend, 1);
837
137k
        if (rlvl->prcwidthexpn < 1 || rlvl->prcheightexpn < 1) {
838
          /* the control block width/height offset
839
             exponent must not be negative */
840
6
          retval = -1;
841
6
          goto done;
842
6
        }
843
137k
        rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1;
844
137k
        rlvl->cbgheightexpn = rlvl->prcheightexpn - 1;
845
137k
      }
846
160k
      rlvl->cblkwidthexpn = JAS_MIN(ccp->cblkwidthexpn,
847
160k
        rlvl->cbgwidthexpn);
848
160k
      rlvl->cblkheightexpn = JAS_MIN(ccp->cblkheightexpn,
849
160k
        rlvl->cbgheightexpn);
850
851
160k
      rlvl->numbands = (!rlvlno) ? 1 : 3;
852
160k
      if (!(rlvl->bands = jas_alloc2(rlvl->numbands,
853
160k
        sizeof(jpc_dec_band_t)))) {
854
1
        retval = -1;
855
1
        goto done;
856
1
      }
857
160k
      unsigned bandno;
858
160k
#if 1
859
160k
      for (bandno = 0, band = rlvl->bands;
860
596k
        bandno < rlvl->numbands; ++bandno, ++band) {
861
436k
        jpc_band_init(band);
862
436k
      }
863
160k
#endif
864
160k
      for (bandno = 0, band = rlvl->bands;
865
596k
        bandno < rlvl->numbands; ++bandno, ++band) {
866
436k
        unsigned bndno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) +
867
413k
          bandno + 1);
868
436k
        const jpc_tsfb_band_t *bnd = &bnds[bndno];
869
870
436k
        if (jas_get_debug_level() >= 10) {
871
0
          jas_logdebugf(10, "[compno %d rlvlno %d bandno %d]\n", compno, rlvlno, bandno);
872
0
        }
873
874
436k
        band->orient = bnd->orient;
875
436k
        band->stepsize = ccp->stepsizes[bndno];
876
436k
        band->analgain = JPC_NOMINALGAIN(ccp->qmfbid,
877
436k
          tcomp->numrlvls - 1, rlvlno, band->orient);
878
436k
        band->absstepsize = jpc_calcabsstepsize(band->stepsize,
879
436k
          cmpt->prec + band->analgain);
880
436k
        band->numbps = ccp->numguardbits +
881
436k
          JPC_QCX_GETEXPN(band->stepsize) - 1;
882
436k
        band->roishift = (ccp->roishift + band->numbps >= JPC_PREC) ?
883
409k
          (JPC_PREC - 1 - band->numbps) : ccp->roishift;
884
436k
        band->data = 0;
885
436k
        band->prcs = 0;
886
436k
        if (bnd->xstart == bnd->xend || bnd->ystart == bnd->yend) {
887
196k
          continue;
888
196k
        }
889
239k
        if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) {
890
1
          retval = -1;
891
1
          goto done;
892
1
        }
893
239k
        if (jas_seq2d_bindsub(band->data, tcomp->data, bnd->locxstart,
894
239k
                  bnd->locystart, bnd->locxend, bnd->locyend)) {
895
1
          retval = -1;
896
1
          goto done;
897
1
        }
898
239k
        jas_seq2d_setshift(band->data, bnd->xstart, bnd->ystart);
899
900
239k
        assert(rlvl->numprcs);
901
902
239k
        if (!(band->prcs = jas_alloc2(rlvl->numprcs,
903
239k
          sizeof(jpc_dec_prc_t)))) {
904
3
          retval = -1;
905
3
          goto done;
906
3
        }
907
908
/************************************************/
909
239k
        cbgxstart = tlcbgxstart;
910
239k
        cbgystart = tlcbgystart;
911
239k
        unsigned prccnt;
912
239k
#if 1
913
239k
        for (prccnt = rlvl->numprcs, prc = band->prcs;
914
13.3M
          prccnt > 0; --prccnt, ++prc) {
915
13.1M
          jpc_prc_init(prc);
916
13.1M
        }
917
239k
#endif
918
239k
        for (prccnt = rlvl->numprcs, prc = band->prcs;
919
12.9M
          prccnt > 0; --prccnt, ++prc) {
920
12.6M
          if (jas_get_debug_level() >= 10) {
921
0
            jas_logdebugf(10, "[compno %d rlvlno %d bandno %d prccnt %d]\n", compno, rlvlno, bandno, prccnt);
922
0
          }
923
12.6M
          cbgxend = cbgxstart + (1 << rlvl->cbgwidthexpn);
924
12.6M
          cbgyend = cbgystart + (1 << rlvl->cbgheightexpn);
925
12.6M
          prc->xstart = JAS_MAX(cbgxstart, JAS_CAST(uint_fast32_t,
926
12.6M
            jas_seq2d_xstart(band->data)));
927
12.6M
          prc->ystart = JAS_MAX(cbgystart, JAS_CAST(uint_fast32_t,
928
12.6M
            jas_seq2d_ystart(band->data)));
929
12.6M
          prc->xend = JAS_MIN(cbgxend, JAS_CAST(uint_fast32_t,
930
12.6M
            jas_seq2d_xend(band->data)));
931
12.6M
          prc->yend = JAS_MIN(cbgyend, JAS_CAST(uint_fast32_t,
932
12.6M
            jas_seq2d_yend(band->data)));
933
12.6M
          if (prc->xend > prc->xstart && prc->yend > prc->ystart) {
934
12.5M
            tlcblkxstart = JPC_FLOORDIVPOW2(prc->xstart,
935
12.5M
              rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn;
936
12.5M
            tlcblkystart = JPC_FLOORDIVPOW2(prc->ystart,
937
12.5M
              rlvl->cblkheightexpn) << rlvl->cblkheightexpn;
938
12.5M
            brcblkxend = JPC_CEILDIVPOW2(prc->xend,
939
12.5M
              rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn;
940
12.5M
            brcblkyend = JPC_CEILDIVPOW2(prc->yend,
941
12.5M
              rlvl->cblkheightexpn) << rlvl->cblkheightexpn;
942
12.5M
            prc->numhcblks = (brcblkxend - tlcblkxstart) >>
943
12.5M
              rlvl->cblkwidthexpn;
944
12.5M
            prc->numvcblks = (brcblkyend - tlcblkystart) >>
945
12.5M
              rlvl->cblkheightexpn;
946
12.5M
            prc->numcblks = prc->numhcblks * prc->numvcblks;
947
12.5M
            assert(prc->numcblks > 0);
948
949
12.5M
            if (jas_get_debug_level() >= 10) {
950
0
              jas_logdebugf(10, "[compno %d rlvlno %d bandno %d prccnt %d]: "
951
0
                "numhcblks %d numvcblks %d numcblks %d\n",
952
0
                compno, rlvlno, bandno, prccnt, prc->numhcblks, prc->numvcblks,
953
0
                prc->numcblks);
954
0
            }
955
956
12.5M
            if (!(prc->incltagtree = jpc_tagtree_create(
957
12.5M
              prc->numhcblks, prc->numvcblks))) {
958
16
              retval = -1;
959
16
              goto done;
960
16
            }
961
12.5M
            if (!(prc->numimsbstagtree = jpc_tagtree_create(
962
12.5M
              prc->numhcblks, prc->numvcblks))) {
963
26
              retval = -1;
964
26
              goto done;
965
26
            }
966
12.5M
            if (!(prc->cblks = jas_alloc2(prc->numcblks,
967
12.5M
              sizeof(jpc_dec_cblk_t)))) {
968
12
              retval = -1;
969
12
              goto done;
970
12
            }
971
972
12.5M
            cblkxstart = cbgxstart;
973
12.5M
            cblkystart = cbgystart;
974
12.5M
            unsigned cblkcnt;
975
12.5M
#if 1
976
12.5M
            for (cblkcnt = prc->numcblks, cblk = prc->cblks;
977
136M
              cblkcnt > 0; ++cblk, --cblkcnt) {
978
124M
              jpc_cblk_init(cblk);
979
124M
            }
980
12.5M
#endif
981
12.5M
            for (cblkcnt = prc->numcblks, cblk = prc->cblks;
982
2.50G
              cblkcnt > 0;) {
983
2.48G
              if (jas_get_debug_level() >= 10000) {
984
0
                jas_logdebugf(10000, "[compno %d rlvlno %d bandno %d prcno %d cblkcnt %d]\n", compno, rlvlno, bandno, prccnt, cblkcnt);
985
0
              }
986
2.48G
              cblkxend = cblkxstart + (1 << rlvl->cblkwidthexpn);
987
2.48G
              cblkyend = cblkystart + (1 << rlvl->cblkheightexpn);
988
2.48G
              tmpxstart = JAS_MAX(cblkxstart, prc->xstart);
989
2.48G
              tmpystart = JAS_MAX(cblkystart, prc->ystart);
990
2.48G
              tmpxend = JAS_MIN(cblkxend, prc->xend);
991
2.48G
              tmpyend = JAS_MIN(cblkyend, prc->yend);
992
2.48G
              if (tmpxend > tmpxstart && tmpyend > tmpystart) {
993
118M
                cblk->firstpassno = -1;
994
118M
                cblk->numpasses = 0;
995
118M
                cblk->segs.head = 0;
996
118M
                cblk->segs.tail = 0;
997
118M
                cblk->curseg = 0;
998
118M
                cblk->numimsbs = 0;
999
118M
                cblk->numlenbits = 3;
1000
118M
                if (!(cblk->data = jas_seq2d_create(0, 0, 0,
1001
118M
                  0))) {
1002
32
                  retval = -1;
1003
32
                  goto done;
1004
32
                }
1005
118M
                if (jas_seq2d_bindsub(cblk->data, band->data,
1006
118M
                          tmpxstart, tmpystart, tmpxend, tmpyend)) {
1007
17
                  retval = -1;
1008
17
                  goto done;
1009
17
                }
1010
118M
                ++cblk;
1011
118M
                --cblkcnt;
1012
118M
              }
1013
2.48G
              cblkxstart += 1 << rlvl->cblkwidthexpn;
1014
2.48G
              if (cblkxstart >= cbgxend) {
1015
40.5M
                cblkxstart = cbgxstart;
1016
40.5M
                cblkystart += 1 << rlvl->cblkheightexpn;
1017
40.5M
              }
1018
2.48G
            }
1019
1020
12.5M
          } else {
1021
84.6k
            prc->cblks = 0;
1022
84.6k
            prc->incltagtree = 0;
1023
84.6k
            prc->numimsbstagtree = 0;
1024
84.6k
          }
1025
12.6M
          cbgxstart += 1 << rlvl->cbgwidthexpn;
1026
12.6M
          if (cbgxstart >= brcbgxend) {
1027
2.59M
            cbgxstart = tlcbgxstart;
1028
2.59M
            cbgystart += 1 << rlvl->cbgheightexpn;
1029
2.59M
          }
1030
1031
12.6M
        }
1032
/********************************************/
1033
239k
      }
1034
160k
    }
1035
35.8k
  }
1036
1037
13.3k
  if (!(tile->pi = jpc_dec_pi_create(dec, tile))) {
1038
6
    retval = -1;
1039
6
    goto done;
1040
6
  }
1041
1042
51.7k
  for (unsigned pchgno = 0; pchgno < jpc_pchglist_numpchgs(tile->cp->pchglist);
1043
38.3k
    ++pchgno) {
1044
38.3k
    pchg = jpc_pchg_copy(jpc_pchglist_get(tile->cp->pchglist, pchgno));
1045
38.3k
    assert(pchg);
1046
38.3k
    jpc_pi_addpchg(tile->pi, pchg);
1047
38.3k
  }
1048
13.3k
  jpc_pi_init(tile->pi);
1049
1050
13.5k
done:
1051
1052
13.5k
  if (jas_get_debug_level() >= 1) {
1053
0
    jas_logdebugf(1, "jpc_dec_tileinit returning %d\n", retval);
1054
0
  }
1055
1056
13.5k
  return retval;
1057
13.3k
}
1058
1059
static int jpc_dec_tilefini(jpc_dec_t *dec, jpc_dec_tile_t *tile)
1060
7.65M
{
1061
7.65M
  jpc_dec_tcomp_t *tcomp;
1062
7.65M
  unsigned rlvlno;
1063
7.65M
  jpc_dec_seg_t *seg;
1064
7.65M
  jpc_dec_cblk_t *cblk;
1065
1066
7.65M
  if (jas_get_debug_level() >= 1) {
1067
0
    jas_logdebugf(1, "jpc_dec_tilefini called\n");
1068
0
  }
1069
1070
7.65M
  if (tile->tcomps) {
1071
7.65M
    unsigned compno;
1072
36.2M
    for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
1073
28.5M
      ++compno, ++tcomp) {
1074
28.5M
      if (tcomp->rlvls) {
1075
35.8k
        const jpc_dec_rlvl_t *rlvl;
1076
248k
        for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls;
1077
212k
          ++rlvlno, ++rlvl) {
1078
212k
          if (!rlvl->bands) {
1079
52.2k
            continue;
1080
52.2k
          }
1081
160k
          unsigned bandno;
1082
160k
          const jpc_dec_band_t *band;
1083
596k
          for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands;
1084
436k
            ++bandno, ++band) {
1085
436k
            if (band->prcs) {
1086
239k
              unsigned prcno;
1087
239k
              const jpc_dec_prc_t *prc;
1088
13.3M
              for (prcno = 0, prc = band->prcs; prcno <
1089
13.3M
                rlvl->numprcs; ++prcno, ++prc) {
1090
13.1M
                if (prc->cblks) {
1091
12.5M
                  unsigned cblkno;
1092
136M
                  for (cblkno = 0, cblk = prc->cblks; cblkno <
1093
136M
                    prc->numcblks; ++cblkno, ++cblk) {
1094
124M
                    while (cblk->segs.head) {
1095
756k
                      seg = cblk->segs.head;
1096
756k
                      jpc_seglist_remove(&cblk->segs, seg);
1097
756k
                      jpc_seg_destroy(seg);
1098
756k
                    }
1099
124M
                    if (cblk->data) {
1100
118M
                      jas_matrix_destroy(cblk->data);
1101
118M
                    }
1102
124M
                  }
1103
12.5M
                  jas_free(prc->cblks);
1104
12.5M
                }
1105
13.1M
                if (prc->incltagtree) {
1106
12.5M
                  jpc_tagtree_destroy(prc->incltagtree);
1107
12.5M
                }
1108
13.1M
                if (prc->numimsbstagtree) {
1109
12.5M
                  jpc_tagtree_destroy(prc->numimsbstagtree);
1110
12.5M
                }
1111
13.1M
              }
1112
239k
            }
1113
436k
            if (band->data) {
1114
239k
              jas_matrix_destroy(band->data);
1115
239k
            }
1116
436k
            if (band->prcs) {
1117
239k
              jas_free(band->prcs);
1118
239k
            }
1119
436k
          }
1120
160k
          if (rlvl->bands) {
1121
160k
            jas_free(rlvl->bands);
1122
160k
          }
1123
160k
        }
1124
35.8k
      }
1125
28.5M
      if (tcomp->rlvls) {
1126
35.8k
        jas_free(tcomp->rlvls);
1127
35.8k
      }
1128
28.5M
      if (tcomp->data) {
1129
35.8k
        jas_matrix_destroy(tcomp->data);
1130
35.8k
      }
1131
28.5M
      if (tcomp->tsfb) {
1132
35.8k
        jpc_tsfb_destroy(tcomp->tsfb);
1133
35.8k
      }
1134
28.5M
    }
1135
7.65M
  }
1136
1137
7.65M
  if (tile->cp) {
1138
14.1k
    jpc_dec_cp_destroy(tile->cp);
1139
14.1k
    tile->cp = 0;
1140
14.1k
  }
1141
7.65M
  if (tile->tcomps) {
1142
7.65M
    jas_free(tile->tcomps);
1143
7.65M
    tile->tcomps = 0;
1144
7.65M
  }
1145
7.65M
  if (tile->pi) {
1146
13.3k
    jpc_pi_destroy(tile->pi);
1147
13.3k
    tile->pi = 0;
1148
13.3k
  }
1149
7.65M
  if (tile->pkthdrstream) {
1150
2.12k
    jas_stream_close(tile->pkthdrstream);
1151
2.12k
    tile->pkthdrstream = 0;
1152
2.12k
  }
1153
7.65M
  if (tile->pptstab) {
1154
146
    jpc_ppxstab_destroy(tile->pptstab);
1155
146
    tile->pptstab = 0;
1156
146
  }
1157
1158
7.65M
  tile->state = JPC_TILE_DONE;
1159
1160
7.65M
  return 0;
1161
7.65M
}
1162
1163
static int jpc_dec_tiledecode(jpc_dec_t *dec, jpc_dec_tile_t *tile)
1164
9.31k
{
1165
9.31k
  unsigned rlvlno;
1166
9.31k
  int v;
1167
1168
9.31k
  if (jpc_dec_decodecblks(dec, tile)) {
1169
0
    jas_logerrorf("jpc_dec_decodecblks failed\n");
1170
0
    return -1;
1171
0
  }
1172
1173
  /* Perform dequantization. */
1174
9.31k
  unsigned compno;
1175
9.31k
  const jpc_dec_tcomp_t *tcomp;
1176
33.2k
  for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
1177
23.9k
    ++compno, ++tcomp) {
1178
23.9k
    const jpc_dec_ccp_t *ccp = &tile->cp->ccps[compno];
1179
23.9k
    const jpc_dec_rlvl_t *rlvl;
1180
162k
    for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls;
1181
138k
      ++rlvlno, ++rlvl) {
1182
138k
      if (!rlvl->bands) {
1183
33.4k
        continue;
1184
33.4k
      }
1185
105k
      unsigned bandno;
1186
105k
      const jpc_dec_band_t *band;
1187
105k
      for (bandno = 0, band = rlvl->bands;
1188
393k
        bandno < rlvl->numbands; ++bandno, ++band) {
1189
288k
        if (!band->data) {
1190
105k
          continue;
1191
105k
        }
1192
183k
        jpc_undo_roi(band->data, band->roishift, ccp->roishift -
1193
183k
          band->roishift, band->numbps);
1194
183k
        if (ccp->qmfbid == JPC_COX_INS) {
1195
105k
          jas_matrix_asl(band->data, JPC_FIX_FRACBITS);
1196
105k
          jpc_dequantize(band->data, band->absstepsize);
1197
105k
        }
1198
1199
183k
      }
1200
105k
    }
1201
23.9k
  }
1202
1203
  /* Apply an inverse wavelet transform if necessary. */
1204
33.2k
  for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
1205
23.9k
    ++compno, ++tcomp) {
1206
23.9k
    jpc_tsfb_synthesize(tcomp->tsfb, tcomp->data);
1207
23.9k
  }
1208
1209
1210
  /* Apply an inverse intercomponent transform if necessary. */
1211
9.31k
  switch (tile->cp->mctid) {
1212
1.23k
  case JPC_MCT_RCT:
1213
1.23k
    if (dec->numcomps < 3) {
1214
190
      jas_logerrorf("RCT requires at least three components\n");
1215
190
      return -1;
1216
190
    }
1217
1.04k
    if (!jas_image_cmpt_domains_same(dec->image)) {
1218
140
      jas_logerrorf("RCT requires all components have the same domain\n");
1219
140
      return -1;
1220
140
    }
1221
906
    jpc_irct(tile->tcomps[0].data, tile->tcomps[1].data,
1222
906
      tile->tcomps[2].data);
1223
906
    break;
1224
2.44k
  case JPC_MCT_ICT:
1225
2.44k
    if (dec->numcomps < 3) {
1226
180
      jas_logerrorf("ICT requires at least three components\n");
1227
180
      return -1;
1228
180
    }
1229
2.26k
    if (!jas_image_cmpt_domains_same(dec->image)) {
1230
197
      jas_logerrorf("RCT requires all components have the same domain\n");
1231
197
      return -1;
1232
197
    }
1233
2.06k
    jpc_iict(tile->tcomps[0].data, tile->tcomps[1].data,
1234
2.06k
      tile->tcomps[2].data);
1235
2.06k
    break;
1236
9.31k
  }
1237
1238
  /* Perform rounding and convert to integer values. */
1239
31.0k
  for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
1240
22.4k
      ++compno, ++tcomp) {
1241
22.4k
    const jpc_dec_ccp_t *ccp = &tile->cp->ccps[compno];
1242
22.4k
    if (ccp->qmfbid == JPC_COX_INS) {
1243
14.5k
      jas_matrix_t *const data = tcomp->data;
1244
14.5k
      const jas_matind_t height = jas_matrix_numrows(data);
1245
14.5k
      const jas_matind_t numcols = jas_matrix_numcols(data);
1246
8.51M
      for (jas_matind_t i = 0; i < height; ++i) {
1247
8.50M
        jpc_fix_t *p = jas_matrix_getref(data, i, 0);
1248
525M
        for (jas_matind_t j = 0; j < numcols; ++j) {
1249
517M
          v = p[j];
1250
517M
          v = jpc_fix_round(v);
1251
517M
          p[j] = jpc_fixtoint(v);
1252
517M
        }
1253
8.50M
      }
1254
14.5k
    }
1255
22.4k
  }
1256
1257
  /* Perform level shift. */
1258
8.60k
  const jpc_dec_cmpt_t *cmpt;
1259
31.0k
  for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno <
1260
31.0k
    dec->numcomps; ++compno, ++tcomp, ++cmpt) {
1261
22.4k
    if (cmpt->sgnd)
1262
2.65k
      continue;
1263
1264
19.8k
    jas_matrix_t *const data = tcomp->data;
1265
19.8k
    const jas_matind_t width = jas_matrix_numcols(data);
1266
19.8k
    const jas_matind_t height = jas_matrix_numrows(data);
1267
19.8k
    const jas_seqent_t adjust = (jas_seqent_t)1 << (cmpt->prec - 1);
1268
11.8M
    for (jas_matind_t i = 0; i < height; ++i) {
1269
11.7M
      jpc_fix_t *p = jas_matrix_getref(data, i, 0);
1270
769M
      for (jas_matind_t j = 0; j < width; ++j) {
1271
758M
        p[j] += adjust;
1272
758M
      }
1273
11.7M
    }
1274
19.8k
  }
1275
1276
  /* Perform clipping. */
1277
31.0k
  for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno <
1278
31.0k
    dec->numcomps; ++compno, ++tcomp, ++cmpt) {
1279
22.4k
    if (cmpt->prec >= sizeof(jpc_fix_t) * 8 - 2 + cmpt->sgnd)
1280
      /* no need to clip, because the calculated
1281
         minimum/maximum values would overflow our
1282
         integer type anyway */
1283
0
      continue;
1284
1285
22.4k
    const jas_seqent_t mn = cmpt->sgnd
1286
22.4k
      ? (-((jpc_fix_t)1 << (cmpt->prec - 1)))
1287
22.4k
      : (0);
1288
22.4k
    const jas_seqent_t mx = cmpt->sgnd
1289
22.4k
      ? (((jpc_fix_t)1 << (cmpt->prec - 1)) - 1)
1290
22.4k
      : (((jpc_fix_t)1 << cmpt->prec) - 1);
1291
22.4k
    jas_matrix_clip(tcomp->data, mn, mx);
1292
22.4k
  }
1293
1294
  /* XXX need to free tsfb struct */
1295
1296
  /* Write the data for each component of the image. */
1297
28.8k
  for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno <
1298
28.8k
    dec->numcomps; ++compno, ++tcomp, ++cmpt) {
1299
20.3k
    if (jas_image_writecmpt(dec->image, compno, tcomp->xstart -
1300
20.3k
      JPC_CEILDIV(dec->xstart, cmpt->hstep), tcomp->ystart -
1301
20.3k
      JPC_CEILDIV(dec->ystart, cmpt->vstep), jas_matrix_numcols(
1302
20.3k
      tcomp->data), jas_matrix_numrows(tcomp->data), tcomp->data)) {
1303
155
      jas_logerrorf("write component failed\n");
1304
155
      return -1;
1305
155
    }
1306
20.3k
  }
1307
1308
8.44k
  return 0;
1309
8.60k
}
1310
1311
static int jpc_dec_process_eoc(jpc_dec_t *dec, jpc_ms_t *ms)
1312
4.30k
{
1313
4.30k
  jpc_dec_tile_t *tile;
1314
1315
4.30k
  JAS_UNUSED(ms);
1316
1317
4.30k
  unsigned tileno;
1318
1.40M
  for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno,
1319
1.40M
    ++tile) {
1320
1.40M
    if (tile->state == JPC_TILE_ACTIVE ||
1321
1.39M
      tile->state == JPC_TILE_ACTIVELAST) {
1322
3.91k
      if (jpc_dec_tiledecode(dec, tile)) {
1323
488
        return -1;
1324
488
      }
1325
3.91k
    }
1326
    /* If the tile has not yet been finalized, finalize it. */
1327
    // OLD CODE: jpc_dec_tilefini(dec, tile);
1328
1.40M
    if (tile->state != JPC_TILE_DONE) {
1329
1.40M
      jpc_dec_tilefini(dec, tile);
1330
1.40M
    }
1331
1.40M
  }
1332
1333
  /* We are done processing the code stream. */
1334
3.81k
  dec->state = JPC_MT;
1335
1336
3.81k
  return 1;
1337
4.30k
}
1338
1339
static int jpc_dec_process_siz(jpc_dec_t *dec, jpc_ms_t *ms)
1340
37.2k
{
1341
37.2k
  jpc_siz_t *siz = &ms->parms.siz;
1342
37.2k
  jpc_dec_tile_t *tile;
1343
37.2k
  jpc_dec_tcomp_t *tcomp;
1344
37.2k
  jpc_dec_cmpt_t *cmpt;
1345
37.2k
  size_t size;
1346
37.2k
  size_t num_samples;
1347
37.2k
  size_t num_samples_delta;
1348
1349
37.2k
  size_t total_samples;
1350
37.2k
  if (!jas_safe_size_mul(siz->width, siz->height, &total_samples) ||
1351
37.2k
      (dec->max_samples > 0 && total_samples > dec->max_samples)) {
1352
25.0k
    jas_logerrorf("image too large\n");
1353
25.0k
    return -1;
1354
25.0k
  }
1355
1356
12.2k
  size_t tile_samples;
1357
12.2k
  if (!jas_safe_size_mul(siz->tilewidth, siz->tileheight, &tile_samples) ||
1358
12.2k
      (dec->max_samples > 0 && tile_samples > dec->max_samples)) {
1359
247
    jas_logerrorf("tile too large\n");
1360
247
    return -1;
1361
247
  }
1362
1363
11.9k
  dec->xstart = siz->xoff;
1364
11.9k
  dec->ystart = siz->yoff;
1365
11.9k
  dec->xend = siz->width;
1366
11.9k
  dec->yend = siz->height;
1367
11.9k
  dec->tilewidth = siz->tilewidth;
1368
11.9k
  dec->tileheight = siz->tileheight;
1369
11.9k
  dec->tilexoff = siz->tilexoff;
1370
11.9k
  dec->tileyoff = siz->tileyoff;
1371
11.9k
  dec->numcomps = siz->numcomps;
1372
1373
11.9k
  if (!(dec->cp = jpc_dec_cp_create(dec->numcomps))) {
1374
0
    return -1;
1375
0
  }
1376
1377
11.9k
  if (!(dec->cmpts = jas_alloc2(dec->numcomps, sizeof(jpc_dec_cmpt_t)))) {
1378
0
    return -1;
1379
0
  }
1380
1381
11.9k
  num_samples = 0;
1382
11.9k
  unsigned compno;
1383
88.4k
  for (compno = 0, cmpt = dec->cmpts; compno < dec->numcomps; ++compno,
1384
76.4k
    ++cmpt) {
1385
76.4k
    cmpt->prec = siz->comps[compno].prec;
1386
76.4k
    cmpt->sgnd = siz->comps[compno].sgnd;
1387
76.4k
    cmpt->hstep = siz->comps[compno].hsamp;
1388
76.4k
    cmpt->vstep = siz->comps[compno].vsamp;
1389
76.4k
    cmpt->width = JPC_CEILDIV(dec->xend, cmpt->hstep) -
1390
76.4k
      JPC_CEILDIV(dec->xstart, cmpt->hstep);
1391
76.4k
    cmpt->height = JPC_CEILDIV(dec->yend, cmpt->vstep) -
1392
76.4k
      JPC_CEILDIV(dec->ystart, cmpt->vstep);
1393
76.4k
    cmpt->hsubstep = 0;
1394
76.4k
    cmpt->vsubstep = 0;
1395
1396
76.4k
    if (!cmpt->width || !cmpt->height) {
1397
9
      jas_logerrorf("image component has no samples\n");
1398
9
      return -1;
1399
9
    }
1400
76.4k
    if (!jas_safe_size_mul(cmpt->width, cmpt->height, &num_samples_delta)) {
1401
0
      jas_logerrorf("image too large\n");
1402
0
      return -1;
1403
0
    }
1404
76.4k
    if (!jas_safe_size_add(num_samples, num_samples_delta, &num_samples)) {
1405
0
      jas_logerrorf("image too large\n");
1406
0
      return -1;
1407
0
    }
1408
76.4k
  }
1409
1410
11.9k
  if (dec->max_samples > 0 && num_samples > dec->max_samples) {
1411
83
    jas_logerrorf("maximum number of samples exceeded (%zu > %zu)\n",
1412
83
      num_samples, dec->max_samples);
1413
83
    return -1;
1414
83
  }
1415
1416
11.9k
  dec->image = 0;
1417
1418
11.9k
  dec->numhtiles = JPC_CEILDIV(dec->xend - dec->tilexoff, dec->tilewidth);
1419
11.9k
  dec->numvtiles = JPC_CEILDIV(dec->yend - dec->tileyoff, dec->tileheight);
1420
11.9k
  if (!jas_safe_size_mul(dec->numhtiles, dec->numvtiles, &size) ||
1421
11.9k
    size > INT_MAX) {
1422
0
    return -1;
1423
0
  }
1424
11.9k
  if (dec->max_samples > 0 && size > dec->max_samples / 16 / 16) {
1425
    /* avoid Denial of Service by a malicious input file
1426
       with millions of tiny tiles; if max_samples is
1427
       configured, then assume the tiles are at least
1428
       16x16, and don't allow more than this number of
1429
       tiles */
1430
78
    return -1;
1431
78
  }
1432
11.8k
  if (dec->max_samples > 0 && size > dec->max_samples / dec->numcomps / 16) {
1433
    /* another DoS check: since each tile allocates an
1434
       array of components, this check attempts to catch
1435
       excessive tile*component numbers */
1436
15
    return -1;
1437
15
  }
1438
11.8k
  dec->numtiles = size;
1439
11.8k
  JAS_LOGDEBUGF(10, "numtiles = %d; numhtiles = %d; numvtiles = %d;\n",
1440
11.8k
    dec->numtiles, dec->numhtiles, dec->numvtiles);
1441
11.8k
  if (!(dec->tiles = jas_alloc2(dec->numtiles, sizeof(jpc_dec_tile_t)))) {
1442
0
    return -1;
1443
0
  }
1444
1445
11.8k
  unsigned tileno;
1446
7.66M
  for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno,
1447
7.65M
    ++tile) {
1448
    /* initialize all tiles with JPC_TILE_DONE so
1449
       jpc_dec_destroy() knows which ones need a
1450
       jpc_dec_tilefini() call; they are not actually
1451
       "done", of course */
1452
7.65M
    tile->state = JPC_TILE_DONE;
1453
7.65M
  }
1454
1455
7.66M
  for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno,
1456
7.65M
    ++tile) {
1457
7.65M
    const unsigned htileno = tileno % dec->numhtiles;
1458
7.65M
    const unsigned vtileno = tileno / dec->numhtiles;
1459
7.65M
    tile->realmode = 0;
1460
7.65M
    tile->state = JPC_TILE_INIT;
1461
7.65M
    tile->xstart = JAS_MAX(dec->tilexoff + htileno * dec->tilewidth,
1462
7.65M
      dec->xstart);
1463
7.65M
    tile->ystart = JAS_MAX(dec->tileyoff + vtileno * dec->tileheight,
1464
7.65M
      dec->ystart);
1465
7.65M
    tile->xend = JAS_MIN(dec->tilexoff + (htileno + 1) *
1466
7.65M
      dec->tilewidth, dec->xend);
1467
7.65M
    tile->yend = JAS_MIN(dec->tileyoff + (vtileno + 1) *
1468
7.65M
      dec->tileheight, dec->yend);
1469
7.65M
    tile->numparts = 0;
1470
7.65M
    tile->partno = 0;
1471
7.65M
    tile->pkthdrstream = 0;
1472
7.65M
    tile->pptstab = 0;
1473
7.65M
    tile->cp = 0;
1474
7.65M
    tile->pi = 0;
1475
7.65M
    if (!(tile->tcomps = jas_alloc2(dec->numcomps,
1476
7.65M
      sizeof(jpc_dec_tcomp_t)))) {
1477
0
      return -1;
1478
0
    }
1479
7.65M
    for (compno = 0, cmpt = dec->cmpts, tcomp = tile->tcomps;
1480
36.2M
      compno < dec->numcomps; ++compno, ++cmpt, ++tcomp) {
1481
28.5M
      tcomp->rlvls = 0;
1482
28.5M
      tcomp->numrlvls = 0;
1483
28.5M
      tcomp->data = 0;
1484
28.5M
      tcomp->xstart = JPC_CEILDIV(tile->xstart, cmpt->hstep);
1485
28.5M
      tcomp->ystart = JPC_CEILDIV(tile->ystart, cmpt->vstep);
1486
28.5M
      tcomp->xend = JPC_CEILDIV(tile->xend, cmpt->hstep);
1487
28.5M
      tcomp->yend = JPC_CEILDIV(tile->yend, cmpt->vstep);
1488
28.5M
      tcomp->tsfb = 0;
1489
28.5M
    }
1490
7.65M
  }
1491
1492
11.8k
  dec->pkthdrstreams = 0;
1493
1494
  /* We should expect to encounter other main header marker segments
1495
    or an SOT marker segment next. */
1496
11.8k
  dec->state = JPC_MH;
1497
1498
11.8k
  return 0;
1499
11.8k
}
1500
1501
static int jpc_dec_process_cod(jpc_dec_t *dec, jpc_ms_t *ms)
1502
21.2k
{
1503
21.2k
  const jpc_cod_t *cod = &ms->parms.cod;
1504
21.2k
  jpc_dec_tile_t *tile;
1505
1506
21.2k
  switch (dec->state) {
1507
19.3k
  case JPC_MH:
1508
19.3k
    jpc_dec_cp_setfromcod(dec->cp, cod);
1509
19.3k
    break;
1510
1.93k
  case JPC_TPH:
1511
1.93k
    if (!(tile = dec->curtile)) {
1512
0
      return -1;
1513
0
    }
1514
1.93k
    if (tile->partno != 0) {
1515
3
      return -1;
1516
3
    }
1517
1.93k
    jpc_dec_cp_setfromcod(tile->cp, cod);
1518
1.93k
    break;
1519
21.2k
  }
1520
21.2k
  return 0;
1521
21.2k
}
1522
1523
static int jpc_dec_process_coc(jpc_dec_t *dec, jpc_ms_t *ms)
1524
2.96k
{
1525
2.96k
  const jpc_coc_t *coc = &ms->parms.coc;
1526
2.96k
  jpc_dec_tile_t *tile;
1527
1528
2.96k
  if (coc->compno >= dec->numcomps) {
1529
43
    jas_logerrorf("invalid component number in COC marker segment\n");
1530
43
    return -1;
1531
43
  }
1532
2.92k
  switch (dec->state) {
1533
2.09k
  case JPC_MH:
1534
2.09k
    jpc_dec_cp_setfromcoc(dec->cp, coc);
1535
2.09k
    break;
1536
826
  case JPC_TPH:
1537
826
    if (!(tile = dec->curtile)) {
1538
0
      return -1;
1539
0
    }
1540
826
    if (tile->partno > 0) {
1541
3
      return -1;
1542
3
    }
1543
823
    jpc_dec_cp_setfromcoc(tile->cp, coc);
1544
823
    break;
1545
2.92k
  }
1546
2.91k
  return 0;
1547
2.92k
}
1548
1549
static int jpc_dec_process_rgn(jpc_dec_t *dec, jpc_ms_t *ms)
1550
1.74k
{
1551
1.74k
  const jpc_rgn_t *rgn = &ms->parms.rgn;
1552
1.74k
  jpc_dec_tile_t *tile;
1553
1554
1.74k
  if (rgn->compno >= dec->numcomps) {
1555
28
    jas_logerrorf("invalid component number in RGN marker segment\n");
1556
28
    return -1;
1557
28
  }
1558
1.71k
  switch (dec->state) {
1559
1.13k
  case JPC_MH:
1560
1.13k
    jpc_dec_cp_setfromrgn(dec->cp, rgn);
1561
1.13k
    break;
1562
579
  case JPC_TPH:
1563
579
    if (!(tile = dec->curtile)) {
1564
0
      return -1;
1565
0
    }
1566
579
    if (tile->partno > 0) {
1567
3
      return -1;
1568
3
    }
1569
576
    jpc_dec_cp_setfromrgn(tile->cp, rgn);
1570
576
    break;
1571
1.71k
  }
1572
1573
1.71k
  return 0;
1574
1.71k
}
1575
1576
static int jpc_dec_process_qcd(jpc_dec_t *dec, jpc_ms_t *ms)
1577
536k
{
1578
536k
  const jpc_qcd_t *qcd = &ms->parms.qcd;
1579
536k
  jpc_dec_tile_t *tile;
1580
1581
536k
  switch (dec->state) {
1582
533k
  case JPC_MH:
1583
533k
    jpc_dec_cp_setfromqcd(dec->cp, qcd);
1584
533k
    break;
1585
2.60k
  case JPC_TPH:
1586
2.60k
    if (!(tile = dec->curtile)) {
1587
0
      return -1;
1588
0
    }
1589
2.60k
    if (tile->partno > 0) {
1590
3
      return -1;
1591
3
    }
1592
2.59k
    jpc_dec_cp_setfromqcd(tile->cp, qcd);
1593
2.59k
    break;
1594
536k
  }
1595
536k
  return 0;
1596
536k
}
1597
1598
static int jpc_dec_process_qcc(jpc_dec_t *dec, jpc_ms_t *ms)
1599
3.51k
{
1600
3.51k
  const jpc_qcc_t *qcc = &ms->parms.qcc;
1601
3.51k
  jpc_dec_tile_t *tile;
1602
1603
3.51k
  if (qcc->compno >= dec->numcomps) {
1604
58
    jas_logerrorf("invalid component number in QCC marker segment\n");
1605
58
    return -1;
1606
58
  }
1607
3.46k
  switch (dec->state) {
1608
2.08k
  case JPC_MH:
1609
2.08k
    jpc_dec_cp_setfromqcc(dec->cp, qcc);
1610
2.08k
    break;
1611
1.37k
  case JPC_TPH:
1612
1.37k
    if (!(tile = dec->curtile)) {
1613
0
      return -1;
1614
0
    }
1615
1.37k
    if (tile->partno > 0) {
1616
3
      return -1;
1617
3
    }
1618
1.37k
    jpc_dec_cp_setfromqcc(tile->cp, qcc);
1619
1.37k
    break;
1620
3.46k
  }
1621
3.45k
  return 0;
1622
3.46k
}
1623
1624
static int jpc_dec_process_poc(jpc_dec_t *dec, jpc_ms_t *ms)
1625
7.32k
{
1626
7.32k
  const jpc_poc_t *poc = &ms->parms.poc;
1627
7.32k
  jpc_dec_tile_t *tile;
1628
7.32k
  switch (dec->state) {
1629
4.67k
  case JPC_MH:
1630
4.67k
    if (jpc_dec_cp_setfrompoc(dec->cp, poc, 1)) {
1631
0
      return -1;
1632
0
    }
1633
4.67k
    break;
1634
4.67k
  case JPC_TPH:
1635
2.64k
    if (!(tile = dec->curtile)) {
1636
0
      return -1;
1637
0
    }
1638
2.64k
    if (!tile->partno) {
1639
2.13k
      if (jpc_dec_cp_setfrompoc(tile->cp, poc, (!tile->partno))) {
1640
0
        return -1;
1641
0
      }
1642
2.13k
    } else {
1643
510
      jpc_pi_addpchgfrompoc(tile->pi, poc);
1644
510
    }
1645
2.64k
    break;
1646
7.32k
  }
1647
7.32k
  return 0;
1648
7.32k
}
1649
1650
static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms)
1651
130k
{
1652
130k
  jpc_ppm_t *ppm = &ms->parms.ppm;
1653
130k
  jpc_ppxstabent_t *ppmstabent;
1654
1655
130k
  if (!dec->ppmstab) {
1656
659
    if (!(dec->ppmstab = jpc_ppxstab_create())) {
1657
0
      return -1;
1658
0
    }
1659
659
  }
1660
1661
130k
  if (!(ppmstabent = jpc_ppxstabent_create())) {
1662
0
    return -1;
1663
0
  }
1664
130k
  ppmstabent->ind = ppm->ind;
1665
130k
  ppmstabent->data = ppm->data;
1666
130k
  ppm->data = 0;
1667
130k
  ppmstabent->len = ppm->len;
1668
130k
  if (jpc_ppxstab_insert(dec->ppmstab, ppmstabent)) {
1669
0
    jpc_ppxstabent_destroy(ppmstabent);
1670
0
    return -1;
1671
0
  }
1672
130k
  return 0;
1673
130k
}
1674
1675
static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms)
1676
43.2k
{
1677
43.2k
  jpc_ppt_t *ppt = &ms->parms.ppt;
1678
43.2k
  jpc_dec_tile_t *tile;
1679
43.2k
  jpc_ppxstabent_t *pptstabent;
1680
1681
43.2k
  tile = dec->curtile;
1682
43.2k
  if (!tile->pptstab) {
1683
661
    if (!(tile->pptstab = jpc_ppxstab_create())) {
1684
0
      return -1;
1685
0
    }
1686
661
  }
1687
43.2k
  if (!(pptstabent = jpc_ppxstabent_create())) {
1688
0
    return -1;
1689
0
  }
1690
43.2k
  pptstabent->ind = ppt->ind;
1691
43.2k
  pptstabent->data = ppt->data;
1692
43.2k
  ppt->data = 0;
1693
43.2k
  pptstabent->len = ppt->len;
1694
43.2k
  if (jpc_ppxstab_insert(tile->pptstab, pptstabent)) {
1695
0
    jpc_ppxstabent_destroy(pptstabent);
1696
0
    return -1;
1697
0
  }
1698
43.2k
  return 0;
1699
43.2k
}
1700
1701
static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms)
1702
3.63k
{
1703
3.63k
  JAS_UNUSED(dec);
1704
3.63k
  JAS_UNUSED(ms);
1705
1706
3.63k
  return 0;
1707
3.63k
}
1708
1709
static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms)
1710
24.2k
{
1711
24.2k
  JAS_UNUSED(dec);
1712
1713
24.2k
  jas_logwarnf("warning: ignoring unknown marker segment (0x%x)\n",
1714
24.2k
    ms->id);
1715
24.2k
  return 0;
1716
24.2k
}
1717
1718
/******************************************************************************\
1719
*
1720
\******************************************************************************/
1721
1722
static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps)
1723
26.1k
{
1724
26.1k
  jpc_dec_cp_t *cp;
1725
26.1k
  jpc_dec_ccp_t *ccp;
1726
1727
26.1k
  if (!(cp = jas_malloc(sizeof(jpc_dec_cp_t)))) {
1728
0
    return 0;
1729
0
  }
1730
26.1k
  cp->flags = 0;
1731
26.1k
  cp->numcomps = numcomps;
1732
26.1k
  cp->prgord = 0;
1733
26.1k
  cp->numlyrs = 0;
1734
26.1k
  cp->mctid = 0;
1735
26.1k
  cp->csty = 0;
1736
26.1k
  cp->pchglist = 0;
1737
26.1k
  if (!(cp->ccps = jas_alloc2(cp->numcomps, sizeof(jpc_dec_ccp_t)))) {
1738
0
    goto error;
1739
0
  }
1740
26.1k
  if (!(cp->pchglist = jpc_pchglist_create())) {
1741
0
    goto error;
1742
0
  }
1743
26.1k
  unsigned compno;
1744
144k
  for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
1745
118k
    ++compno, ++ccp) {
1746
118k
    ccp->flags = 0;
1747
118k
    ccp->numrlvls = 0;
1748
118k
    ccp->cblkwidthexpn = 0;
1749
118k
    ccp->cblkheightexpn = 0;
1750
118k
    ccp->qmfbid = 0;
1751
118k
    ccp->numstepsizes = 0;
1752
118k
    ccp->numguardbits = 0;
1753
118k
    ccp->roishift = 0;
1754
118k
    ccp->cblkctx = 0;
1755
118k
  }
1756
26.1k
  return cp;
1757
0
error:
1758
0
  if (cp) {
1759
0
    jpc_dec_cp_destroy(cp);
1760
0
  }
1761
0
  return 0;
1762
26.1k
}
1763
1764
static jpc_dec_cp_t *jpc_dec_cp_copy(const jpc_dec_cp_t *cp)
1765
14.1k
{
1766
14.1k
  jpc_dec_cp_t *newcp;
1767
14.1k
  jpc_dec_ccp_t *newccp;
1768
14.1k
  const jpc_dec_ccp_t *ccp;
1769
1770
14.1k
  if (!(newcp = jpc_dec_cp_create(cp->numcomps))) {
1771
0
    return 0;
1772
0
  }
1773
14.1k
  newcp->flags = cp->flags;
1774
14.1k
  newcp->prgord = cp->prgord;
1775
14.1k
  newcp->numlyrs = cp->numlyrs;
1776
14.1k
  newcp->mctid = cp->mctid;
1777
14.1k
  newcp->csty = cp->csty;
1778
14.1k
  jpc_pchglist_destroy(newcp->pchglist);
1779
14.1k
  newcp->pchglist = 0;
1780
14.1k
  if (!(newcp->pchglist = jpc_pchglist_copy(cp->pchglist))) {
1781
0
    jas_free(newcp);
1782
0
    return 0;
1783
0
  }
1784
14.1k
  unsigned compno;
1785
14.1k
  for (compno = 0, newccp = newcp->ccps, ccp = cp->ccps;
1786
56.0k
    compno < cp->numcomps;
1787
41.9k
    ++compno, ++newccp, ++ccp) {
1788
41.9k
    *newccp = *ccp;
1789
41.9k
  }
1790
14.1k
  return newcp;
1791
14.1k
}
1792
1793
static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp)
1794
14.1k
{
1795
14.1k
  jpc_dec_ccp_t *ccp;
1796
14.1k
  cp->flags &= (JPC_CSET | JPC_QSET);
1797
1798
14.1k
  unsigned compno;
1799
56.0k
  for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
1800
41.9k
    ++compno, ++ccp) {
1801
41.9k
    ccp->flags = 0;
1802
41.9k
  }
1803
14.1k
}
1804
1805
static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp)
1806
26.1k
{
1807
26.1k
  if (cp->ccps) {
1808
26.1k
    jas_free(cp->ccps);
1809
26.1k
  }
1810
26.1k
  if (cp->pchglist) {
1811
26.1k
    jpc_pchglist_destroy(cp->pchglist);
1812
26.1k
  }
1813
26.1k
  jas_free(cp);
1814
26.1k
}
1815
1816
static int jpc_dec_cp_isvalid(const jpc_dec_cp_t *cp)
1817
13.5k
{
1818
13.5k
  uint_fast16_t compcnt;
1819
13.5k
  const jpc_dec_ccp_t *ccp;
1820
1821
13.5k
  if (!(cp->flags & JPC_CSET) || !(cp->flags & JPC_QSET)) {
1822
29
    return 0;
1823
29
  }
1824
49.5k
  for (compcnt = cp->numcomps, ccp = cp->ccps; compcnt > 0; --compcnt,
1825
36.0k
    ++ccp) {
1826
    /* Is there enough step sizes for the number of bands? */
1827
36.0k
    if ((ccp->qsty != JPC_QCX_SIQNT && JAS_CAST(int, ccp->numstepsizes) < 3 *
1828
35.9k
      ccp->numrlvls - 2) || (ccp->qsty == JPC_QCX_SIQNT &&
1829
7.20k
      ccp->numstepsizes != 1)) {
1830
16
      return 0;
1831
16
    }
1832
36.0k
  }
1833
13.5k
  return 1;
1834
13.5k
}
1835
1836
static int calcstepsizes(uint_fast16_t refstepsize, unsigned numrlvls,
1837
  uint_fast16_t *stepsizes)
1838
7.20k
{
1839
7.20k
  uint_fast16_t expn;
1840
7.20k
  uint_fast16_t mant;
1841
7.20k
  expn = JPC_QCX_GETEXPN(refstepsize);
1842
7.20k
  mant = JPC_QCX_GETMANT(refstepsize);
1843
7.20k
  const unsigned numbands = 3 * numrlvls - 2;
1844
341k
  for (unsigned bandno = 0; bandno < numbands; ++bandno) {
1845
#if 0
1846
    jas_eprintf("DEBUG %d %d %d %d %d\n", bandno, expn, numrlvls, bandno,
1847
      ((numrlvls - 1) - (numrlvls - 1 - ((bandno > 0) ? ((bandno + 2) / 3)
1848
      : (0)))));
1849
#endif
1850
334k
    const unsigned r = (bandno + 2) / 3;
1851
334k
    const unsigned nb = (r == 0) ? (numrlvls - 1) - r : (numrlvls - 1) - r + 1;
1852
334k
    uint_fast16_t e = expn - (numrlvls - 1) + nb;
1853
334k
    if (e >= 0x20)
1854
6
      return -1;
1855
334k
    stepsizes[bandno] = JPC_QCX_MANT(mant) | JPC_QCX_EXPN(e);
1856
334k
  }
1857
7.19k
  return 0;
1858
7.20k
}
1859
1860
static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp)
1861
13.5k
{
1862
13.5k
  jpc_dec_ccp_t *ccp;
1863
13.5k
  unsigned compno;
1864
49.5k
  for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
1865
35.9k
    ++compno, ++ccp) {
1866
35.9k
    if (!(ccp->csty & JPC_COX_PRT)) {
1867
767k
      for (unsigned i = 0; i < JPC_MAXRLVLS; ++i) {
1868
744k
        ccp->prcwidthexpns[i] = 15;
1869
744k
        ccp->prcheightexpns[i] = 15;
1870
744k
      }
1871
22.5k
    }
1872
35.9k
    if (ccp->qsty == JPC_QCX_SIQNT) {
1873
7.20k
      if (calcstepsizes(ccp->stepsizes[0], ccp->numrlvls, ccp->stepsizes)) {
1874
6
        return -1;
1875
6
      }
1876
7.20k
    }
1877
35.9k
  }
1878
13.5k
  return 0;
1879
13.5k
}
1880
1881
static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, const jpc_cod_t *cod)
1882
21.2k
{
1883
21.2k
  jpc_dec_ccp_t *ccp;
1884
21.2k
  cp->flags |= JPC_CSET;
1885
21.2k
  cp->prgord = cod->prg;
1886
21.2k
  if (cod->mctrans) {
1887
11.5k
    cp->mctid = (cod->compparms.qmfbid == JPC_COX_INS) ? (JPC_MCT_ICT) : (JPC_MCT_RCT);
1888
11.5k
  } else {
1889
9.74k
    cp->mctid = JPC_MCT_NONE;
1890
9.74k
  }
1891
21.2k
  cp->numlyrs = cod->numlyrs;
1892
21.2k
  cp->csty = cod->csty & (JPC_COD_SOP | JPC_COD_EPH);
1893
21.2k
  unsigned compno;
1894
115k
  for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
1895
94.6k
    ++compno, ++ccp) {
1896
94.6k
    jpc_dec_cp_setfromcox(cp, ccp, &cod->compparms, 0);
1897
94.6k
  }
1898
21.2k
  cp->flags |= JPC_CSET;
1899
21.2k
  return 0;
1900
21.2k
}
1901
1902
static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, const jpc_coc_t *coc)
1903
2.91k
{
1904
2.91k
  jpc_dec_cp_setfromcox(cp, &cp->ccps[coc->compno], &coc->compparms, JPC_COC);
1905
2.91k
  return 0;
1906
2.91k
}
1907
1908
static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp,
1909
  const jpc_coxcp_t *compparms, unsigned flags)
1910
97.5k
{
1911
97.5k
  int rlvlno;
1912
1913
97.5k
  JAS_UNUSED(cp);
1914
1915
97.5k
  if ((flags & JPC_COC) || !(ccp->flags & JPC_COC)) {
1916
95.2k
    ccp->numrlvls = compparms->numdlvls + 1;
1917
95.2k
    ccp->cblkwidthexpn = JPC_COX_GETCBLKSIZEEXPN(
1918
95.2k
      compparms->cblkwidthval);
1919
95.2k
    ccp->cblkheightexpn = JPC_COX_GETCBLKSIZEEXPN(
1920
95.2k
      compparms->cblkheightval);
1921
95.2k
    ccp->qmfbid = compparms->qmfbid;
1922
95.2k
    ccp->cblkctx = compparms->cblksty;
1923
95.2k
    ccp->csty = compparms->csty & JPC_COX_PRT;
1924
933k
    for (rlvlno = 0; rlvlno < compparms->numrlvls; ++rlvlno) {
1925
838k
      ccp->prcwidthexpns[rlvlno] =
1926
838k
        compparms->rlvls[rlvlno].parwidthval;
1927
838k
      ccp->prcheightexpns[rlvlno] =
1928
838k
        compparms->rlvls[rlvlno].parheightval;
1929
838k
    }
1930
95.2k
    ccp->flags |= flags | JPC_CSET;
1931
95.2k
  }
1932
97.5k
  return 0;
1933
97.5k
}
1934
1935
static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, const jpc_qcd_t *qcd)
1936
536k
{
1937
536k
  unsigned compno;
1938
536k
  jpc_dec_ccp_t *ccp;
1939
2.25M
  for (compno = 0, ccp = cp->ccps; compno < cp->numcomps;
1940
1.71M
    ++compno, ++ccp) {
1941
1.71M
    jpc_dec_cp_setfromqcx(cp, ccp, &qcd->compparms, 0);
1942
1.71M
  }
1943
536k
  cp->flags |= JPC_QSET;
1944
536k
  return 0;
1945
536k
}
1946
1947
static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, const jpc_qcc_t *qcc)
1948
3.45k
{
1949
3.45k
  return jpc_dec_cp_setfromqcx(cp, &cp->ccps[qcc->compno], &qcc->compparms, JPC_QCC);
1950
3.45k
}
1951
1952
static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp,
1953
  const jpc_qcxcp_t *compparms, unsigned flags)
1954
1.72M
{
1955
1.72M
  int bandno;
1956
1957
1.72M
  JAS_UNUSED(cp);
1958
1959
  /* Sanity check to prevent buffer overflow */
1960
1.72M
  if (compparms->numstepsizes > (3 * JPC_MAXRLVLS + 1)) {
1961
0
    return -1;
1962
0
  }
1963
1964
1.72M
  if ((flags & JPC_QCC) || !(ccp->flags & JPC_QCC)) {
1965
1.71M
    ccp->flags |= flags | JPC_QSET;
1966
2.70M
    for (bandno = 0; bandno < compparms->numstepsizes; ++bandno) {
1967
989k
      ccp->stepsizes[bandno] = compparms->stepsizes[bandno];
1968
989k
    }
1969
1.71M
    ccp->numstepsizes = compparms->numstepsizes;
1970
1.71M
    ccp->numguardbits = compparms->numguard;
1971
1.71M
    ccp->qsty = compparms->qntsty;
1972
1.71M
  }
1973
1.72M
  return 0;
1974
1.72M
}
1975
1976
static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, const jpc_rgn_t *rgn)
1977
1.71k
{
1978
1.71k
  jpc_dec_ccp_t *ccp;
1979
1.71k
  ccp = &cp->ccps[rgn->compno];
1980
1.71k
  ccp->roishift = rgn->roishift;
1981
1.71k
  return 0;
1982
1.71k
}
1983
1984
static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, const jpc_poc_t *poc)
1985
510
{
1986
510
  int pchgno;
1987
510
  jpc_pchg_t *pchg;
1988
11.4k
  for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) {
1989
10.9k
    if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) {
1990
0
      return -1;
1991
0
    }
1992
10.9k
    if (jpc_pchglist_insert(pi->pchglist, -1, pchg)) {
1993
0
      return -1;
1994
0
    }
1995
10.9k
  }
1996
510
  return 0;
1997
510
}
1998
1999
static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, const jpc_poc_t *poc, int reset)
2000
6.81k
{
2001
6.81k
  int pchgno;
2002
6.81k
  jpc_pchg_t *pchg;
2003
6.81k
  if (reset) {
2004
10.7k
    while (jpc_pchglist_numpchgs(cp->pchglist) > 0) {
2005
3.97k
      pchg = jpc_pchglist_remove(cp->pchglist, 0);
2006
3.97k
      jpc_pchg_destroy(pchg);
2007
3.97k
    }
2008
6.81k
  }
2009
79.1k
  for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) {
2010
72.3k
    if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) {
2011
0
      return -1;
2012
0
    }
2013
72.3k
    if (jpc_pchglist_insert(cp->pchglist, -1, pchg)) {
2014
0
      return -1;
2015
0
    }
2016
72.3k
  }
2017
6.81k
  return 0;
2018
6.81k
}
2019
2020
static jpc_fix_t jpc_calcabsstepsize(unsigned stepsize, unsigned numbits)
2021
436k
{
2022
436k
  jpc_fix_t absstepsize;
2023
436k
  int n;
2024
2025
436k
  absstepsize = jpc_inttofix(1);
2026
436k
  n = JPC_FIX_FRACBITS - 11;
2027
436k
  absstepsize |= (n >= 0) ? (JPC_QCX_GETMANT(stepsize) << n) :
2028
436k
    (JPC_QCX_GETMANT(stepsize) >> (-n));
2029
436k
  n = numbits - JPC_QCX_GETEXPN(stepsize);
2030
436k
  absstepsize = (n >= 0) ? (absstepsize << n) : (absstepsize >> (-n));
2031
436k
  return absstepsize;
2032
436k
}
2033
2034
static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize)
2035
105k
{
2036
  // a reconstruction parameter defined in E 1.1.2 of the ISO/IEC 15444-1
2037
105k
  jpc_fix_t recparam = JPC_FIX_HALF;
2038
2039
  // Note:
2040
  // Should anything special be done to handle the error case of
2041
  // absstepsize < 0 (which can arise due to numerical overflow).
2042
  // Using an assert is not appropriate, since this results in program
2043
  // termination when this type of error occurs.
2044
  // Simply allowing a decoded image with very high distortion would appear
2045
  // to be consistent with the policy adopted elsewhere in the codec.
2046
  // assert(absstepsize >= 0);
2047
2048
105k
  if (absstepsize == jpc_inttofix(1)) {
2049
2.89k
    return;
2050
2.89k
  }
2051
2052
102k
  const jas_matind_t height = jas_matrix_numrows(x);
2053
102k
  const size_t width = jas_matrix_numcols(x);
2054
2055
12.5M
  for (jas_matind_t i = 0; i < height; ++i) {
2056
12.4M
    jpc_fix_t *p = jas_matrix_getref(x, i, 0);
2057
572M
    for (size_t j = 0; j < width; ++j) {
2058
559M
      jas_seqent_t t = p[j];
2059
559M
      if (t) {
2060
        // mid-point reconstruction
2061
92.2M
        t = (t > 0) ? jpc_fix_add(t, recparam) : jpc_fix_sub(t, recparam);
2062
92.2M
        t = jpc_fix_mul(t, absstepsize);
2063
92.2M
        p[j] = t;
2064
92.2M
      }
2065
559M
    }
2066
12.4M
  }
2067
2068
102k
}
2069
2070
static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, unsigned numbps)
2071
183k
{
2072
183k
  int thresh;
2073
183k
  jpc_fix_t val;
2074
183k
  jpc_fix_t mag;
2075
183k
  bool warn;
2076
183k
  uint_fast32_t mask;
2077
2078
183k
  if (roishift < 0) {
2079
    /* We could instead return an error here. */
2080
    /* I do not think it matters much. */
2081
6.18k
    jas_logwarnf("warning: forcing negative ROI shift to zero "
2082
6.18k
      "(bitstream is probably corrupt)\n");
2083
6.18k
    roishift = 0;
2084
6.18k
  }
2085
183k
  if (roishift == 0 && bgshift == 0) {
2086
170k
    return;
2087
170k
  }
2088
12.8k
  thresh = 1 << roishift;
2089
2090
12.8k
  warn = false;
2091
2092
12.8k
  const jas_matind_t width = jas_matrix_numcols(x);
2093
12.8k
  const jas_matind_t height = jas_matrix_numrows(x);
2094
4.92M
  for (jas_matind_t i = 0; i < height; ++i) {
2095
4.91M
    jpc_fix_t *p = jas_matrix_getref(x, i, 0);
2096
91.4M
    for (jas_matind_t j = 0; j < width; ++j, ++p) {
2097
86.5M
      val = *p;
2098
86.5M
      mag = JAS_ABS(val);
2099
86.5M
      if (mag >= thresh) {
2100
        /* We are dealing with ROI data. */
2101
6.25M
        mag >>= roishift;
2102
6.25M
        val = (val < 0) ? (-mag) : mag;
2103
6.25M
        *p = val;
2104
80.2M
      } else {
2105
        /* We are dealing with non-ROI (i.e., background) data. */
2106
80.2M
        mag <<= bgshift;
2107
80.2M
        mask = (JAS_CAST(uint_fast32_t, 1) << numbps) - 1;
2108
        /* Perform a basic sanity check on the sample value. */
2109
        /* Some implementations write garbage in the unused
2110
          most-significant bit planes introduced by ROI shifting.
2111
          Here we ensure that any such bits are masked off. */
2112
80.2M
        if (mag & (~mask)) {
2113
217k
          if (!warn) {
2114
288
            jas_logwarnf("warning: possibly corrupt code stream\n");
2115
288
            warn = true;
2116
288
          }
2117
217k
          mag &= mask;
2118
217k
        }
2119
80.2M
        val = (val < 0) ? (-mag) : mag;
2120
80.2M
        *p = val;
2121
80.2M
      }
2122
86.5M
    }
2123
4.91M
  }
2124
12.8k
}
2125
2126
static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts, jas_stream_t *in)
2127
204k
{
2128
204k
  jpc_dec_t *dec;
2129
2130
204k
  if (!(dec = jas_malloc(sizeof(jpc_dec_t)))) {
2131
0
    return 0;
2132
0
  }
2133
2134
204k
  dec->image = 0;
2135
204k
  dec->xstart = 0;
2136
204k
  dec->ystart = 0;
2137
204k
  dec->xend = 0;
2138
204k
  dec->yend = 0;
2139
204k
  dec->tilewidth = 0;
2140
204k
  dec->tileheight = 0;
2141
204k
  dec->tilexoff = 0;
2142
204k
  dec->tileyoff = 0;
2143
204k
  dec->numhtiles = 0;
2144
204k
  dec->numvtiles = 0;
2145
204k
  dec->numtiles = 0;
2146
204k
  dec->tiles = 0;
2147
204k
  dec->curtile = 0;
2148
204k
  dec->numcomps = 0;
2149
204k
  dec->in = in;
2150
204k
  dec->cp = 0;
2151
204k
  dec->maxlyrs = impopts->maxlyrs;
2152
204k
  dec->maxpkts = impopts->maxpkts;
2153
204k
  dec->numpkts = 0;
2154
204k
  dec->ppmseqno = 0;
2155
204k
  dec->state = 0;
2156
204k
  dec->cmpts = 0;
2157
204k
  dec->pkthdrstreams = 0;
2158
204k
  dec->ppmstab = 0;
2159
204k
  dec->curtileendoff = 0;
2160
204k
  dec->max_samples = impopts->max_samples;
2161
2162
204k
  if (jas_get_debug_level() >= 1) {
2163
0
    jas_logdebugf(1, "debug %d\n", jas_get_debug_level());
2164
0
  }
2165
2166
204k
  return dec;
2167
204k
}
2168
2169
static void jpc_dec_destroy(jpc_dec_t *dec)
2170
204k
{
2171
204k
  if (dec->cstate) {
2172
204k
    jpc_cstate_destroy(dec->cstate);
2173
204k
  }
2174
204k
  if (dec->pkthdrstreams) {
2175
164
    jpc_streamlist_destroy(dec->pkthdrstreams);
2176
164
  }
2177
204k
  if (dec->ppmstab) {
2178
495
    jpc_ppxstab_destroy(dec->ppmstab);
2179
495
  }
2180
204k
  if (dec->image) {
2181
6.26k
    jas_image_destroy(dec->image);
2182
6.26k
  }
2183
2184
204k
  if (dec->cp) {
2185
11.9k
    jpc_dec_cp_destroy(dec->cp);
2186
11.9k
  }
2187
2188
204k
  if (dec->cmpts) {
2189
11.9k
    jas_free(dec->cmpts);
2190
11.9k
  }
2191
2192
204k
  if (dec->tiles) {
2193
11.8k
    unsigned tileno;
2194
11.8k
    jpc_dec_tile_t *tile;
2195
2196
7.66M
    for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno,
2197
7.65M
      ++tile) {
2198
7.65M
      if (tile->state != JPC_TILE_DONE) {
2199
6.24M
        jpc_dec_tilefini(dec, tile);
2200
6.24M
      }
2201
7.65M
    }
2202
2203
11.8k
    jas_free(dec->tiles);
2204
11.8k
  }
2205
2206
204k
  jas_free(dec);
2207
204k
}
2208
2209
/******************************************************************************\
2210
*
2211
\******************************************************************************/
2212
2213
void jpc_seglist_insert(jpc_dec_seglist_t *list, jpc_dec_seg_t *ins, jpc_dec_seg_t *node)
2214
943k
{
2215
943k
  jpc_dec_seg_t *prev;
2216
943k
  jpc_dec_seg_t *next;
2217
2218
943k
  prev = ins;
2219
943k
  node->prev = prev;
2220
943k
  next = prev ? (prev->next) : 0;
2221
943k
  node->prev = prev;
2222
943k
  node->next = next;
2223
943k
  if (prev) {
2224
749k
    prev->next = node;
2225
749k
  } else {
2226
193k
    list->head = node;
2227
193k
  }
2228
943k
  if (next) {
2229
0
    next->prev = node;
2230
943k
  } else {
2231
943k
    list->tail = node;
2232
943k
  }
2233
943k
}
2234
2235
void jpc_seglist_remove(jpc_dec_seglist_t *list, jpc_dec_seg_t *seg)
2236
943k
{
2237
943k
  jpc_dec_seg_t *prev;
2238
943k
  jpc_dec_seg_t *next;
2239
2240
943k
  prev = seg->prev;
2241
943k
  next = seg->next;
2242
943k
  if (prev) {
2243
0
    prev->next = next;
2244
943k
  } else {
2245
943k
    list->head = next;
2246
943k
  }
2247
943k
  if (next) {
2248
749k
    next->prev = prev;
2249
749k
  } else {
2250
193k
    list->tail = prev;
2251
193k
  }
2252
943k
  seg->prev = 0;
2253
943k
  seg->next = 0;
2254
943k
}
2255
2256
jpc_dec_seg_t *jpc_seg_alloc()
2257
943k
{
2258
943k
  jpc_dec_seg_t *seg;
2259
2260
943k
  if (!(seg = jas_malloc(sizeof(jpc_dec_seg_t)))) {
2261
0
    return 0;
2262
0
  }
2263
943k
  seg->prev = 0;
2264
943k
  seg->next = 0;
2265
943k
  seg->passno = -1;
2266
943k
  seg->numpasses = 0;
2267
943k
  seg->maxpasses = 0;
2268
943k
  seg->type = JPC_SEG_INVALID;
2269
943k
  seg->stream = 0;
2270
943k
  seg->cnt = 0;
2271
943k
  seg->complete = 0;
2272
943k
  seg->lyrno = -1;
2273
943k
  return seg;
2274
943k
}
2275
2276
void jpc_seg_destroy(jpc_dec_seg_t *seg)
2277
943k
{
2278
943k
  if (seg->stream) {
2279
907k
    jas_stream_close(seg->stream);
2280
907k
  }
2281
943k
  jas_free(seg);
2282
943k
}
2283
2284
static int jpc_dec_dump(const jpc_dec_t *dec)
2285
0
{
2286
0
  assert(!dec->numtiles || dec->tiles);
2287
0
  unsigned tileno;
2288
0
  const jpc_dec_tile_t *tile;
2289
0
  for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles;
2290
0
    ++tileno, ++tile) {
2291
0
    if (!tile->tcomps) {
2292
0
      continue;
2293
0
    }
2294
0
    assert(!dec->numcomps || tile->tcomps);
2295
0
    unsigned compno;
2296
0
    const jpc_dec_tcomp_t *tcomp;
2297
0
    for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps;
2298
0
         ++compno, ++tcomp) {
2299
0
      unsigned rlvlno;
2300
0
      const jpc_dec_rlvl_t *rlvl;
2301
0
      for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno <
2302
0
        tcomp->numrlvls; ++rlvlno, ++rlvl) {
2303
0
        jas_logprintf("RESOLUTION LEVEL %d\n", rlvlno);
2304
0
        jas_logprintf("xs = %"PRIuFAST32", ys = %"PRIuFAST32", xe = %"PRIuFAST32", ye = %"PRIuFAST32", w = %"PRIuFAST32", h = %"PRIuFAST32"\n",
2305
0
          rlvl->xstart, rlvl->ystart, rlvl->xend, rlvl->yend,
2306
0
          rlvl->xend - rlvl->xstart, rlvl->yend - rlvl->ystart);
2307
0
        assert(!rlvl->numbands || rlvl->bands);
2308
0
        unsigned bandno;
2309
0
        const jpc_dec_band_t *band;
2310
0
        for (bandno = 0, band = rlvl->bands;
2311
0
          bandno < rlvl->numbands; ++bandno, ++band) {
2312
0
          jas_logprintf("BAND %d\n", bandno);
2313
0
          if (!band->data) {
2314
0
            jas_logprintf("band has no data (null pointer)\n");
2315
0
            assert(!band->prcs);
2316
0
            continue;
2317
0
          }
2318
0
          jas_logprintf("xs = %"PRIjas_seqent", ys = %"PRIjas_seqent", xe = %"PRIjas_seqent", ye = %"PRIjas_seqent", w = %"PRIjas_seqent", h = %"PRIjas_seqent"\n",
2319
0
            jas_seq2d_xstart(band->data),
2320
0
            jas_seq2d_ystart(band->data),
2321
0
            jas_seq2d_xend(band->data),
2322
0
            jas_seq2d_yend(band->data),
2323
0
            jas_seq2d_xend(band->data) -
2324
0
            jas_seq2d_xstart(band->data),
2325
0
            jas_seq2d_yend(band->data) -
2326
0
            jas_seq2d_ystart(band->data));
2327
0
          assert(!rlvl->numprcs || band->prcs);
2328
0
          unsigned prcno;
2329
0
          const jpc_dec_prc_t *prc;
2330
0
          for (prcno = 0, prc = band->prcs;
2331
0
            prcno < rlvl->numprcs; ++prcno,
2332
0
            ++prc) {
2333
0
            jas_logprintf("CODE BLOCK GROUP %d\n", prcno);
2334
0
            jas_logprintf("xs = %"PRIuFAST32", ys = %"PRIuFAST32", xe = %"PRIuFAST32", ye = %"PRIuFAST32", w = %"PRIuFAST32", h = %"PRIuFAST32"\n",
2335
0
              prc->xstart, prc->ystart, prc->xend, prc->yend,
2336
0
              prc->xend - prc->xstart, prc->yend - prc->ystart);
2337
0
            assert(!prc->numcblks || prc->cblks);
2338
0
            unsigned cblkno;
2339
0
            const jpc_dec_cblk_t *cblk;
2340
0
            for (cblkno = 0, cblk =
2341
0
              prc->cblks; cblkno <
2342
0
              prc->numcblks; ++cblkno,
2343
0
              ++cblk) {
2344
0
              jas_logprintf("CODE BLOCK %d\n", cblkno);
2345
0
              jas_logprintf("xs = %"PRIjas_seqent", ys = %"PRIjas_seqent", xe = %"PRIjas_seqent", ye = %"PRIjas_seqent", w = %"PRIjas_seqent", h = %"PRIjas_seqent"\n",
2346
0
                jas_seq2d_xstart(cblk->data),
2347
0
                jas_seq2d_ystart(cblk->data),
2348
0
                jas_seq2d_xend(cblk->data),
2349
0
                jas_seq2d_yend(cblk->data),
2350
0
                jas_seq2d_xend(cblk->data) -
2351
0
                jas_seq2d_xstart(cblk->data),
2352
0
                jas_seq2d_yend(cblk->data) -
2353
0
                jas_seq2d_ystart(cblk->data));
2354
0
            }
2355
0
          }
2356
0
        }
2357
0
      }
2358
0
    }
2359
0
  }
2360
2361
0
  return 0;
2362
0
}
2363
2364
static jpc_streamlist_t *jpc_streamlist_create()
2365
376
{
2366
376
  jpc_streamlist_t *streamlist;
2367
2368
376
  if (!(streamlist = jas_malloc(sizeof(jpc_streamlist_t)))) {
2369
0
    return 0;
2370
0
  }
2371
376
  streamlist->numstreams = 0;
2372
376
  streamlist->maxstreams = 100;
2373
376
  if (!(streamlist->streams = jas_alloc2(streamlist->maxstreams,
2374
376
    sizeof(jas_stream_t *)))) {
2375
0
    jas_free(streamlist);
2376
0
    return 0;
2377
0
  }
2378
37.9k
  for (unsigned i = 0; i < streamlist->maxstreams; ++i) {
2379
37.6k
    streamlist->streams[i] = 0;
2380
37.6k
  }
2381
376
  return streamlist;
2382
376
}
2383
2384
static int jpc_streamlist_insert(jpc_streamlist_t *streamlist, unsigned streamno,
2385
  jas_stream_t *stream)
2386
17.2k
{
2387
17.2k
  jas_stream_t **newstreams;
2388
  /* Grow the array of streams if necessary. */
2389
17.2k
  if (streamlist->numstreams >= streamlist->maxstreams) {
2390
43
    const unsigned newmaxstreams = streamlist->maxstreams + 1024;
2391
43
    if (!(newstreams = jas_realloc2(streamlist->streams,
2392
43
      (newmaxstreams + 1024), sizeof(jas_stream_t *)))) {
2393
0
      return -1;
2394
0
    }
2395
43
    for (unsigned i = streamlist->numstreams; i < streamlist->maxstreams; ++i) {
2396
0
      streamlist->streams[i] = 0;
2397
0
    }
2398
43
    streamlist->maxstreams = newmaxstreams;
2399
43
    streamlist->streams = newstreams;
2400
43
  }
2401
17.2k
  if (streamno != streamlist->numstreams) {
2402
    /* Can only handle insertion at start of list. */
2403
0
    return -1;
2404
0
  }
2405
17.2k
  streamlist->streams[streamno] = stream;
2406
17.2k
  ++streamlist->numstreams;
2407
17.2k
  return 0;
2408
17.2k
}
2409
2410
static jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, unsigned streamno)
2411
1.61k
{
2412
1.61k
  assert(streamno < streamlist->numstreams);
2413
2414
1.61k
  jas_stream_t *stream;
2415
1.61k
  stream = streamlist->streams[streamno];
2416
149k
  for (unsigned i = streamno + 1; i < streamlist->numstreams; ++i) {
2417
147k
    streamlist->streams[i - 1] = streamlist->streams[i];
2418
147k
  }
2419
1.61k
  --streamlist->numstreams;
2420
1.61k
  return stream;
2421
1.61k
}
2422
2423
static void jpc_streamlist_destroy(jpc_streamlist_t *streamlist)
2424
376
{
2425
376
  if (streamlist->streams) {
2426
16.0k
    for (unsigned streamno = 0; streamno < streamlist->numstreams;
2427
15.6k
      ++streamno) {
2428
15.6k
      jas_stream_close(streamlist->streams[streamno]);
2429
15.6k
    }
2430
376
    jas_free(streamlist->streams);
2431
376
  }
2432
376
  jas_free(streamlist);
2433
376
}
2434
2435
static int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist)
2436
18.8k
{
2437
18.8k
  return streamlist->numstreams;
2438
18.8k
}
2439
2440
static jpc_ppxstab_t *jpc_ppxstab_create()
2441
1.32k
{
2442
1.32k
  jpc_ppxstab_t *tab;
2443
2444
1.32k
  if (!(tab = jas_malloc(sizeof(jpc_ppxstab_t)))) {
2445
0
    return 0;
2446
0
  }
2447
1.32k
  tab->numents = 0;
2448
1.32k
  tab->maxents = 0;
2449
1.32k
  tab->ents = 0;
2450
1.32k
  return tab;
2451
1.32k
}
2452
2453
static void jpc_ppxstab_destroy(jpc_ppxstab_t *tab)
2454
1.32k
{
2455
175k
  for (unsigned i = 0; i < tab->numents; ++i) {
2456
173k
    jpc_ppxstabent_destroy(tab->ents[i]);
2457
173k
  }
2458
1.32k
  if (tab->ents) {
2459
1.32k
    jas_free(tab->ents);
2460
1.32k
  }
2461
1.32k
  jas_free(tab);
2462
1.32k
}
2463
2464
static int jpc_ppxstab_grow(jpc_ppxstab_t *tab, unsigned maxents)
2465
2.55k
{
2466
2.55k
  jpc_ppxstabent_t **newents;
2467
2.55k
  if (tab->maxents < maxents) {
2468
2.55k
    newents = (tab->ents) ? jas_realloc2(tab->ents, maxents,
2469
1.32k
      sizeof(jpc_ppxstabent_t *)) : jas_alloc2(maxents, sizeof(jpc_ppxstabent_t *));
2470
2.55k
    if (!newents) {
2471
0
      return -1;
2472
0
    }
2473
2.55k
    tab->ents = newents;
2474
2.55k
    tab->maxents = maxents;
2475
2.55k
  }
2476
2.55k
  return 0;
2477
2.55k
}
2478
2479
static int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent)
2480
173k
{
2481
173k
  unsigned i;
2482
2483
295M
  for (i = 0; i < tab->numents; ++i) {
2484
295M
    if (tab->ents[i]->ind > ent->ind) {
2485
141k
      break;
2486
141k
    }
2487
295M
  }
2488
173k
  const unsigned inspt = i;
2489
2490
173k
  if (tab->numents >= tab->maxents) {
2491
2.55k
    if (jpc_ppxstab_grow(tab, tab->maxents + 128)) {
2492
0
      return -1;
2493
0
    }
2494
2.55k
  }
2495
2496
52.3M
  for (i = tab->numents; i > inspt; --i) {
2497
52.2M
    tab->ents[i] = tab->ents[i - 1];
2498
52.2M
  }
2499
173k
  tab->ents[i] = ent;
2500
173k
  ++tab->numents;
2501
2502
173k
  return 0;
2503
173k
}
2504
2505
static jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab)
2506
376
{
2507
376
  jpc_streamlist_t *streams;
2508
376
  uint_fast32_t datacnt;
2509
376
  uint_fast32_t tpcnt;
2510
376
  jas_stream_t *stream;
2511
2512
376
  if (!(streams = jpc_streamlist_create())) {
2513
0
    goto error;
2514
0
  }
2515
2516
376
  if (!tab->numents) {
2517
0
    return streams;
2518
0
  }
2519
2520
376
  unsigned entno = 0;
2521
376
  const jpc_ppxstabent_t *ent = tab->ents[entno];
2522
376
  const jas_uchar *dataptr = ent->data;
2523
376
  datacnt = ent->len;
2524
17.2k
  for (;;) {
2525
2526
    /* Get the length of the packet header data for the current
2527
      tile-part. */
2528
17.2k
    if (datacnt < 4) {
2529
22
      goto error;
2530
22
    }
2531
17.2k
    if (!(stream = jas_stream_memopen(0, 0))) {
2532
0
      goto error;
2533
0
    }
2534
17.2k
    if (jpc_streamlist_insert(streams, jpc_streamlist_numstreams(streams),
2535
17.2k
      stream)) {
2536
0
      goto error;
2537
0
    }
2538
17.2k
    tpcnt = ((uint_least32_t)dataptr[0] << 24) | ((uint_least32_t)dataptr[1] << 16) | ((uint_least32_t)dataptr[2] << 8)
2539
17.2k
      | (uint_least32_t)dataptr[3];
2540
17.2k
    datacnt -= 4;
2541
17.2k
    dataptr += 4;
2542
2543
    /* Get the packet header data for the current tile-part. */
2544
25.7k
    while (tpcnt) {
2545
8.69k
      if (!datacnt) {
2546
2.90k
        if (++entno >= tab->numents) {
2547
190
          goto error;
2548
190
        }
2549
2.71k
        ent = tab->ents[entno];
2550
2.71k
        dataptr = ent->data;
2551
2.71k
        datacnt = ent->len;
2552
2.71k
      }
2553
8.50k
      const size_t n = JAS_MIN(tpcnt, datacnt);
2554
8.50k
      if (jas_stream_write(stream, dataptr, n) != n) {
2555
0
        goto error;
2556
0
      }
2557
8.50k
      tpcnt -= n;
2558
8.50k
      dataptr += n;
2559
8.50k
      datacnt -= n;
2560
8.50k
    }
2561
17.0k
    jas_stream_rewind(stream);
2562
17.0k
    if (!datacnt) {
2563
5.37k
      if (++entno >= tab->numents) {
2564
164
        break;
2565
164
      }
2566
5.20k
      ent = tab->ents[entno];
2567
5.20k
      dataptr = ent->data;
2568
5.20k
      datacnt = ent->len;
2569
5.20k
    }
2570
17.0k
  }
2571
2572
164
  return streams;
2573
2574
212
error:
2575
212
  if (streams) {
2576
212
    jpc_streamlist_destroy(streams);
2577
212
  }
2578
212
  return 0;
2579
376
}
2580
2581
static int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab)
2582
515
{
2583
2.26k
  for (unsigned i = 0; i < tab->numents; ++i) {
2584
1.74k
    const jpc_ppxstabent_t *ent = tab->ents[i];
2585
1.74k
    if (jas_stream_write(out, ent->data, ent->len) != ent->len) {
2586
0
      return -1;
2587
0
    }
2588
1.74k
  }
2589
515
  return 0;
2590
515
}
2591
2592
static jpc_ppxstabent_t *jpc_ppxstabent_create()
2593
173k
{
2594
173k
  jpc_ppxstabent_t *ent;
2595
173k
  if (!(ent = jas_malloc(sizeof(jpc_ppxstabent_t)))) {
2596
0
    return 0;
2597
0
  }
2598
173k
  ent->data = 0;
2599
173k
  ent->len = 0;
2600
173k
  ent->ind = 0;
2601
173k
  return ent;
2602
173k
}
2603
2604
static void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent)
2605
173k
{
2606
173k
  if (ent->data) {
2607
11.8k
    jas_free(ent->data);
2608
11.8k
  }
2609
173k
  jas_free(ent);
2610
173k
}
2611
2612
static void jpc_cblk_init(jpc_dec_cblk_t *cblk)
2613
124M
{
2614
124M
#if 1
2615
124M
  memset(cblk, 0, sizeof(jpc_dec_cblk_t));
2616
124M
  cblk->numpasses = 0;
2617
124M
  jpc_seglist_init(&cblk->segs);
2618
124M
  cblk->curseg = 0;
2619
124M
  cblk->numimsbs = 0;
2620
124M
  cblk->numlenbits = 0;
2621
124M
  cblk->firstpassno = 0;
2622
124M
  cblk->data = 0;
2623
124M
#endif
2624
124M
}
2625
2626
static void jpc_prc_init(jpc_dec_prc_t *prc)
2627
13.1M
{
2628
13.1M
#if 1
2629
13.1M
  memset(prc, 0, sizeof(jpc_dec_prc_t));
2630
13.1M
  prc->xstart = 0;
2631
13.1M
  prc->ystart = 0;
2632
13.1M
  prc->xend = 0;
2633
13.1M
  prc->yend = 0;
2634
13.1M
  prc->numhcblks = 0;
2635
13.1M
  prc->numvcblks = 0;
2636
13.1M
  prc->cblks = 0;
2637
13.1M
  prc->incltagtree = 0;
2638
13.1M
  prc->numimsbstagtree = 0;
2639
13.1M
#endif
2640
13.1M
}
2641
2642
static void jpc_band_init(jpc_dec_band_t *band)
2643
436k
{
2644
436k
#if 1
2645
436k
  memset(band, 0, sizeof(jpc_dec_band_t));
2646
436k
  band->prcs = 0;
2647
436k
  band->data = 0;
2648
436k
  band->orient = 0;
2649
436k
  band->stepsize = 0;
2650
436k
  band->absstepsize = 0;
2651
436k
  band->numbps = 0;
2652
436k
  band->analgain = 0;
2653
436k
  band->roishift = 0;
2654
436k
#endif
2655
436k
}
2656
2657
static void jpc_rlvl_init(jpc_dec_rlvl_t *rlvl)
2658
212k
{
2659
212k
#if 1
2660
212k
  memset(rlvl, 0, sizeof(jpc_dec_rlvl_t));
2661
212k
  rlvl->numbands = 0;
2662
212k
  rlvl->bands = 0;
2663
212k
  rlvl->xstart = 0;
2664
212k
  rlvl->ystart = 0;
2665
212k
  rlvl->xend = 0;
2666
212k
  rlvl->yend = 0;
2667
212k
  rlvl->prcwidthexpn = 0;
2668
212k
  rlvl->prcheightexpn = 0;
2669
212k
  rlvl->numhprcs = 0;
2670
212k
  rlvl->numvprcs = 0;
2671
212k
  rlvl->numprcs = 0;
2672
212k
  rlvl->cbgwidthexpn = 0;
2673
212k
  rlvl->cblkheightexpn = 0;
2674
212k
#endif
2675
212k
}
2676
2677
static void jpc_seglist_init(jpc_dec_seglist_t *seglist)
2678
124M
{
2679
124M
  memset(seglist, 0, sizeof(jpc_dec_seglist_t));
2680
124M
  seglist->head = 0;
2681
124M
  seglist->tail = 0;
2682
124M
}