Skip to content

Commit

Permalink
Merge pull request #534 from mayeut/issue-235
Browse files Browse the repository at this point in the history
Add checks for odd looking cmap & for cmap outside jp2h box
  • Loading branch information
mayeut committed Jul 20, 2015
2 parents fd1f7cf + 96ae1b8 commit 2c7dab7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/lib/openjp2/jp2.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,23 @@ static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
is_sane = OPJ_FALSE;
}
}
/* Issue 235/447 weird cmap */
if (1 && is_sane && (image->numcomps==1U)) {
for (i = 0; i < nr_channels; i++) {
if (!pcol_usage[i]) {
is_sane = 0U;
opj_event_msg(p_manager, EVT_WARNING, "Component mapping seems wrong. Trying to correct.\n", i);
break;
}
}
if (!is_sane) {
is_sane = OPJ_TRUE;
for (i = 0; i < nr_channels; i++) {
cmap[i].mtyp = 1U;
cmap[i].pcol = (OPJ_BYTE) i;
}
}
}
opj_free(pcol_usage);
if (!is_sane) {
return OPJ_FALSE;
Expand Down Expand Up @@ -1990,6 +2007,7 @@ OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
opj_jp2_box_t box;
OPJ_UINT32 l_nb_bytes_read;
const opj_jp2_header_handler_t * l_current_handler;
const opj_jp2_header_handler_t * l_current_handler_misplaced;
OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE;
OPJ_UINT32 l_current_data_size;
OPJ_BYTE * l_current_data = 00;
Expand Down Expand Up @@ -2033,9 +2051,26 @@ OPJ_BOOL opj_jp2_read_header_procedure( opj_jp2_t *jp2,
}

l_current_handler = opj_jp2_find_handler(box.type);
l_current_handler_misplaced = opj_jp2_img_find_handler(box.type);
l_current_data_size = box.length - l_nb_bytes_read;

if (l_current_handler != 00) {
if ((l_current_handler != 00) || (l_current_handler_misplaced != 00)) {
if (l_current_handler == 00) {
opj_event_msg(p_manager, EVT_WARNING, "Found a misplaced '%c%c%c%c' box outside jp2h box\n", (OPJ_BYTE)(box.type>>24), (OPJ_BYTE)(box.type>>16), (OPJ_BYTE)(box.type>>8), (OPJ_BYTE)(box.type>>0));
if (jp2->jp2_state & JP2_STATE_HEADER) {
/* read anyway, we already have jp2h */
l_current_handler = l_current_handler_misplaced;
} else {
opj_event_msg(p_manager, EVT_WARNING, "JPEG2000 Header box not read yet, '%c%c%c%c' box will be ignored\n", (OPJ_BYTE)(box.type>>24), (OPJ_BYTE)(box.type>>16), (OPJ_BYTE)(box.type>>8), (OPJ_BYTE)(box.type>>0));
jp2->jp2_state |= JP2_STATE_UNKNOWN;
if (opj_stream_skip(stream,l_current_data_size,p_manager) != l_current_data_size) {
opj_event_msg(p_manager, EVT_ERROR, "Problem with skipping JPEG2000 box, stream error\n");
opj_free(l_current_data);
return OPJ_FALSE;
}
continue;
}
}
if ((OPJ_OFF_T)l_current_data_size > opj_stream_get_number_byte_left(stream)) {
/* do not even try to malloc if we can't read */
opj_event_msg(p_manager, EVT_ERROR, "Invalid box size %d for box '%c%c%c%c'. Need %d bytes, %d bytes remaining \n", box.length, (OPJ_BYTE)(box.type>>24), (OPJ_BYTE)(box.type>>16), (OPJ_BYTE)(box.type>>8), (OPJ_BYTE)(box.type>>0), l_current_data_size, (OPJ_UINT32)opj_stream_get_number_byte_left(stream));
Expand Down
3 changes: 3 additions & 0 deletions tests/nonregression/md5refs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,6 @@ cfe04d15cf9d615fc36357dcb3b3956b p0_14_tif-1.tif
b6f71c941e3a5b8d2547792ccec58d54 p0_14_tif-4.tif
81fcdd90917efb95aed94c6522d1c188 p0_14_tif-6.tif
6808377b760b4ef3559ba8b14ed9b91a p0_14_tif-8.tif
dd15b3d487d36a3682be0679300a4319 issue235.jp2_0.pgx
b9cd6dc76b141fb1fec15f50c1f70e01 issue235.jp2_1.pgx
3edef2ae197ef30b08deda1b28825399 issue235.jp2_2.pgx
4 changes: 4 additions & 0 deletions tests/nonregression/test_suite.ctest.in
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,7 @@ opj_decompress -i @INPUT_NR_PATH@/basn6a08.jp2 -o @TEMP_PATH@/basn6a08_tif-10.ti
opj_decompress -i @INPUT_NR_PATH@/basn6a08.jp2 -o @TEMP_PATH@/basn6a08_tif-12.tif -p 12S
opj_decompress -i @INPUT_NR_PATH@/basn6a08.jp2 -o @TEMP_PATH@/basn6a08_tif-14.tif -p 14S
opj_decompress -i @INPUT_NR_PATH@/basn6a08.jp2 -o @TEMP_PATH@/basn6a08_tif-16.tif -p 16S

#issue 235 CMAP outside jp2h box. CMAP is buggy
opj_decompress -i @INPUT_NR_PATH@/issue235.jp2 -o @TEMP_PATH@/issue235.jp2.pgx

0 comments on commit 2c7dab7

Please sign in to comment.