Merge pull request #975 from szukw000/changes-for-afl-tests

Catch images broken by AFL
This commit is contained in:
Antonin Descampe 2017-08-08 16:51:54 -07:00 committed by GitHub
commit 0394f8d0f1
12 changed files with 232 additions and 40 deletions

View File

@ -486,6 +486,34 @@ void color_apply_icc_profile(opj_image_t *image)
prec = (int)image->comps[0].prec; prec = (int)image->comps[0].prec;
if (out_space == cmsSigRgbData) { /* enumCS 16 */ if (out_space == cmsSigRgbData) { /* enumCS 16 */
unsigned int i, nr_comp = image->numcomps;
if (nr_comp > 4) {
nr_comp = 4;
}
for (i = 1; i < nr_comp; ++i) { /* AFL test */
if (image->comps[0].dx != image->comps[i].dx) {
break;
}
if (image->comps[0].dy != image->comps[i].dy) {
break;
}
if (image->comps[0].prec != image->comps[i].prec) {
break;
}
if (image->comps[0].sgnd != image->comps[i].sgnd) {
break;
}
}
if (i != nr_comp) {
cmsCloseProfile(in_prof);
return;
}
if (prec <= 8) { if (prec <= 8) {
in_type = TYPE_RGB_8; in_type = TYPE_RGB_8;
out_type = TYPE_RGB_8; out_type = TYPE_RGB_8;

View File

@ -959,10 +959,11 @@ int imagetotga(opj_image_t * image, const char *outfile)
for (i = 0; i < image->numcomps - 1; i++) { for (i = 0; i < image->numcomps - 1; i++) {
if ((image->comps[0].dx != image->comps[i + 1].dx) if ((image->comps[0].dx != image->comps[i + 1].dx)
|| (image->comps[0].dy != image->comps[i + 1].dy) || (image->comps[0].dy != image->comps[i + 1].dy)
|| (image->comps[0].prec != image->comps[i + 1].prec)) { || (image->comps[0].prec != image->comps[i + 1].prec)
|| (image->comps[0].sgnd != image->comps[i + 1].sgnd)) {
fclose(fdest); fclose(fdest);
fprintf(stderr, fprintf(stderr,
"Unable to create a tga file with such J2K image charateristics."); "Unable to create a tga file with such J2K image charateristics.\n");
return 1; return 1;
} }
} }
@ -2343,7 +2344,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile,
{ {
FILE *rawFile = NULL; FILE *rawFile = NULL;
size_t res; size_t res;
unsigned int compno; unsigned int compno, numcomps;
int w, h, fails; int w, h, fails;
int line, row, curr, mask; int line, row, curr, mask;
int *ptr; int *ptr;
@ -2355,6 +2356,33 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile,
return 1; return 1;
} }
numcomps = image->numcomps;
if (numcomps > 4) {
numcomps = 4;
}
for (compno = 1; compno < numcomps; ++compno) {
if (image->comps[0].dx != image->comps[compno].dx) {
break;
}
if (image->comps[0].dy != image->comps[compno].dy) {
break;
}
if (image->comps[0].prec != image->comps[compno].prec) {
break;
}
if (image->comps[0].sgnd != image->comps[compno].sgnd) {
break;
}
}
if (compno != numcomps) {
fprintf(stderr,
"imagetoraw_common: All components shall have the same subsampling, same bit depth, same sign.\n");
fprintf(stderr, "\tAborting\n");
return 1;
}
rawFile = fopen(outfile, "wb"); rawFile = fopen(outfile, "wb");
if (!rawFile) { if (!rawFile) {
fprintf(stderr, "Failed to open %s for writing !!\n", outfile); fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
@ -2466,7 +2494,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile,
} }
} }
} else if (image->comps[compno].prec <= 32) { } else if (image->comps[compno].prec <= 32) {
fprintf(stderr, "More than 16 bits per component no handled yet\n"); fprintf(stderr, "More than 16 bits per component not handled yet\n");
goto fin; goto fin;
} else { } else {
fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec); fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);

View File

@ -832,7 +832,8 @@ int imagetobmp(opj_image_t * image, const char *outfile)
int adjustR, adjustG, adjustB; int adjustR, adjustG, adjustB;
if (image->comps[0].prec < 8) { if (image->comps[0].prec < 8) {
fprintf(stderr, "Unsupported number of components: %d\n", image->comps[0].prec); fprintf(stderr, "imagetobmp: Unsupported precision: %d\n",
image->comps[0].prec);
return 1; return 1;
} }
if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx
@ -840,7 +841,9 @@ int imagetobmp(opj_image_t * image, const char *outfile)
&& image->comps[0].dy == image->comps[1].dy && image->comps[0].dy == image->comps[1].dy
&& image->comps[1].dy == image->comps[2].dy && image->comps[1].dy == image->comps[2].dy
&& image->comps[0].prec == image->comps[1].prec && image->comps[0].prec == image->comps[1].prec
&& image->comps[1].prec == image->comps[2].prec) { && image->comps[1].prec == image->comps[2].prec
&& image->comps[0].sgnd == image->comps[1].sgnd
&& image->comps[1].sgnd == image->comps[2].sgnd) {
/* -->> -->> -->> -->> /* -->> -->> -->> -->>
24 bits color 24 bits color
@ -974,6 +977,10 @@ int imagetobmp(opj_image_t * image, const char *outfile)
fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
return 1; return 1;
} }
if (image->numcomps > 1) {
fprintf(stderr, "imagetobmp: only first component of %d is used.\n",
image->numcomps);
}
w = (int)image->comps[0].w; w = (int)image->comps[0].w;
h = (int)image->comps[0].h; h = (int)image->comps[0].h;

View File

@ -42,6 +42,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <limits.h>
#ifndef OPJ_HAVE_LIBTIFF #ifndef OPJ_HAVE_LIBTIFF
# error OPJ_HAVE_LIBTIFF_NOT_DEFINED # error OPJ_HAVE_LIBTIFF_NOT_DEFINED
@ -50,6 +51,7 @@
#include <tiffio.h> #include <tiffio.h>
#include "openjpeg.h" #include "openjpeg.h"
#include "convert.h" #include "convert.h"
#include "opj_inttypes.h"
/* -->> -->> -->> -->> /* -->> -->> -->> -->>
@ -564,20 +566,18 @@ static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst,
int imagetotif(opj_image_t * image, const char *outfile) int imagetotif(opj_image_t * image, const char *outfile)
{ {
int width, height;
int bps, adjust, sgnd;
int tiPhoto;
TIFF *tif; TIFF *tif;
tdata_t buf; tdata_t buf;
tsize_t strip_size; uint32 width, height, bps, tiPhoto;
int adjust, sgnd;
int64_t strip_size, rowStride, TIFF_MAX;
OPJ_UINT32 i, numcomps; OPJ_UINT32 i, numcomps;
OPJ_SIZE_T rowStride;
OPJ_INT32* buffer32s = NULL; OPJ_INT32* buffer32s = NULL;
OPJ_INT32 const* planes[4]; OPJ_INT32 const* planes[4];
convert_32s_PXCX cvtPxToCx = NULL; convert_32s_PXCX cvtPxToCx = NULL;
convert_32sXXx_C1R cvt32sToTif = NULL; convert_32sXXx_C1R cvt32sToTif = NULL;
bps = (int)image->comps[0].prec; bps = (uint32)image->comps[0].prec;
planes[0] = image->comps[0].data; planes[0] = image->comps[0].data;
numcomps = image->numcomps; numcomps = image->numcomps;
@ -686,33 +686,46 @@ int imagetotif(opj_image_t * image, const char *outfile)
break; break;
} }
sgnd = (int)image->comps[0].sgnd; sgnd = (int)image->comps[0].sgnd;
adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0; adjust = sgnd ? (int)(1 << (image->comps[0].prec - 1)) : 0;
width = (int)image->comps[0].w; width = (uint32)image->comps[0].w;
height = (int)image->comps[0].h; height = (uint32)image->comps[0].h;
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps); TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (uint32)numcomps);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto); TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
if (sizeof(tsize_t) == 4) {
TIFF_MAX = INT_MAX;
} else {
TIFF_MAX = UINT_MAX;
}
strip_size = (int64_t)TIFFStripSize(tif);
strip_size = TIFFStripSize(tif); if ((int64_t)width > (int64_t)(TIFF_MAX / numcomps) ||
rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U; (int64_t)(width * numcomps) > (int64_t)(TIFF_MAX / bps) ||
if (rowStride != (OPJ_SIZE_T)strip_size) { (int64_t)(width * numcomps) > (int64_t)(TIFF_MAX / (int64_t)sizeof(
OPJ_INT32))) {
fprintf(stderr, "Buffer overflow\n");
TIFFClose(tif);
return 1;
}
rowStride = (int64_t)((width * numcomps * bps + 7U) / 8U);
if (rowStride != strip_size) {
fprintf(stderr, "Invalid TIFF strip size\n"); fprintf(stderr, "Invalid TIFF strip size\n");
TIFFClose(tif); TIFFClose(tif);
return 1; return 1;
} }
buf = _TIFFmalloc(strip_size); buf = malloc((OPJ_SIZE_T)strip_size);
if (buf == NULL) { if (buf == NULL) {
TIFFClose(tif); TIFFClose(tif);
return 1; return 1;
} }
buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof( buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(width * numcomps * sizeof(
OPJ_INT32)); OPJ_INT32)));
if (buffer32s == NULL) { if (buffer32s == NULL) {
_TIFFfree(buf); _TIFFfree(buf);
TIFFClose(tif); TIFFClose(tif);
@ -722,7 +735,7 @@ int imagetotif(opj_image_t * image, const char *outfile)
for (i = 0; i < image->comps[0].h; ++i) { for (i = 0; i < image->comps[0].h; ++i) {
cvtPxToCx(planes, buffer32s, (OPJ_SIZE_T)width, adjust); cvtPxToCx(planes, buffer32s, (OPJ_SIZE_T)width, adjust);
cvt32sToTif(buffer32s, (OPJ_BYTE *)buf, (OPJ_SIZE_T)width * numcomps); cvt32sToTif(buffer32s, (OPJ_BYTE *)buf, (OPJ_SIZE_T)width * numcomps);
(void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size); (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, (tsize_t)strip_size);
planes[0] += width; planes[0] += width;
planes[1] += width; planes[1] += width;
planes[2] += width; planes[2] += width;
@ -1235,20 +1248,18 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
TIFF *tif; TIFF *tif;
tdata_t buf; tdata_t buf;
tstrip_t strip; tstrip_t strip;
tsize_t strip_size; int64_t strip_size, rowStride, TIFF_MAX;
int j, currentPlane, numcomps = 0, w, h; int j, currentPlane, numcomps = 0, w, h;
OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN; OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
opj_image_cmptparm_t cmptparm[4]; /* RGBA */ opj_image_cmptparm_t cmptparm[4]; /* RGBA */
opj_image_t *image = NULL; opj_image_t *image = NULL;
int has_alpha = 0; int has_alpha = 0;
unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC; uint32 tiBps, tiPhoto, tiSf, tiSpp, tiPC, tiWidth, tiHeight;
unsigned int tiWidth, tiHeight;
OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz); OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
convert_XXx32s_C1R cvtTifTo32s = NULL; convert_XXx32s_C1R cvtTifTo32s = NULL;
convert_32s_CXPX cvtCxToPx = NULL; convert_32s_CXPX cvtCxToPx = NULL;
OPJ_INT32* buffer32s = NULL; OPJ_INT32* buffer32s = NULL;
OPJ_INT32* planes[4]; OPJ_INT32* planes[4];
OPJ_SIZE_T rowStride;
tif = TIFFOpen(filename, "r"); tif = TIFFOpen(filename, "r");
@ -1269,20 +1280,33 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
w = (int)tiWidth; w = (int)tiWidth;
h = (int)tiHeight; h = (int)tiHeight;
if (tiBps > 16U) { if (tiSpp == 0 || tiSpp > 4) { /* should be 1 ... 4 */
fprintf(stderr, "tiftoimage: Bits=%d, Only 1 to 16 bits implemented\n", tiBps); fprintf(stderr, "tiftoimage: Bad value for samples per pixel == %hu.\n"
fprintf(stderr, "\tAborting\n"); "\tAborting.\n", tiSpp);
TIFFClose(tif);
return NULL;
}
if (tiBps > 16U || tiBps == 0) {
fprintf(stderr, "tiftoimage: Bad values for Bits == %d.\n"
"\tMax. 16 Bits are allowed here.\n\tAborting.\n", tiBps);
TIFFClose(tif); TIFFClose(tif);
return NULL; return NULL;
} }
if (tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) { if (tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) {
fprintf(stderr, fprintf(stderr,
"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n", "tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n\tAborting.\n",
(int) tiPhoto); (int) tiPhoto);
fprintf(stderr, "\tAborting\n");
TIFFClose(tif); TIFFClose(tif);
return NULL; return NULL;
} }
if (tiWidth == 0 || tiHeight == 0) {
fprintf(stderr, "tiftoimage: Bad values for width(%u) "
"and/or height(%u)\n\tAborting.\n", tiWidth, tiHeight);
TIFFClose(tif);
return NULL;
}
w = (int)tiWidth;
h = (int)tiHeight;
switch (tiBps) { switch (tiBps) {
case 1: case 1:
@ -1405,24 +1429,54 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
image->y0 = (OPJ_UINT32)parameters->image_offset_y0; image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 : image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1; image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
if (image->x1 <= image->x0) {
fprintf(stderr, "tiftoimage: Bad value for image->x1(%d) vs. "
"image->x0(%d)\n\tAborting.\n", image->x1, image->x0);
TIFFClose(tif);
opj_image_destroy(image);
return NULL;
}
image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 : image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1; image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
if (image->y1 <= image->y0) {
fprintf(stderr, "tiftoimage: Bad value for image->y1(%d) vs. "
"image->y0(%d)\n\tAborting.\n", image->y1, image->y0);
TIFFClose(tif);
opj_image_destroy(image);
return NULL;
}
for (j = 0; j < numcomps; j++) { for (j = 0; j < numcomps; j++) {
planes[j] = image->comps[j].data; planes[j] = image->comps[j].data;
} }
image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1)); image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
strip_size = TIFFStripSize(tif); strip_size = (int64_t)TIFFStripSize(tif);
buf = _TIFFmalloc(strip_size); buf = malloc((OPJ_SIZE_T)strip_size);
if (buf == NULL) { if (buf == NULL) {
TIFFClose(tif); TIFFClose(tif);
opj_image_destroy(image); opj_image_destroy(image);
return NULL; return NULL;
} }
rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U; if (sizeof(tsize_t) == 4) {
buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32)); TIFF_MAX = INT_MAX;
} else {
TIFF_MAX = UINT_MAX;
}
if ((int64_t)tiWidth > (int64_t)(TIFF_MAX / tiSpp) ||
(int64_t)(tiWidth * tiSpp) > (int64_t)(TIFF_MAX / tiBps) ||
(int64_t)(tiWidth * tiSpp) > (int64_t)(TIFF_MAX / (int64_t)sizeof(OPJ_INT32))) {
fprintf(stderr, "Buffer overflow\n");
_TIFFfree(buf);
TIFFClose(tif);
opj_image_destroy(image);
return NULL;
}
rowStride = (int64_t)((tiWidth * tiSpp * tiBps + 7U) / 8U);
buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(tiWidth * tiSpp * sizeof(
OPJ_INT32)));
if (buffer32s == NULL) { if (buffer32s == NULL) {
_TIFFfree(buf); _TIFFfree(buf);
TIFFClose(tif); TIFFClose(tif);
@ -1438,9 +1492,19 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
/* Read the Image components */ /* Read the Image components */
for (; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++) { for (; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++) {
const OPJ_UINT8 *dat8; const OPJ_UINT8 *dat8;
OPJ_SIZE_T ssize; int64_t ssize;
ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size); ssize = (int64_t)TIFFReadEncodedStrip(tif, strip, buf, (tsize_t)strip_size);
if (ssize < 1 || ssize > strip_size) {
fprintf(stderr, "tiftoimage: Bad value for ssize(%" PRId64 ") "
"vs. strip_size(%" PRId64 ").\n\tAborting.\n", ssize, strip_size);
_TIFFfree(buf);
_TIFFfree(buffer32s);
TIFFClose(tif);
opj_image_destroy(image);
return NULL;
}
dat8 = (const OPJ_UINT8*)buf; dat8 = (const OPJ_UINT8*)buf;
while (ssize >= rowStride) { while (ssize >= rowStride) {

View File

@ -1722,7 +1722,7 @@ fin:
} }
free(dirptr); free(dirptr);
} }
if (numDecompressedImages && !(parameters.quiet)) { if (numDecompressedImages && !failed && !(parameters.quiet)) {
fprintf(stdout, "decode time: %d ms\n", fprintf(stdout, "decode time: %d ms\n",
(int)((tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages)); (int)((tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages));
} }

View File

@ -577,6 +577,8 @@ int main(int argc, char *argv[])
opj_set_warning_handler(l_codec, warning_callback, 00); opj_set_warning_handler(l_codec, warning_callback, 00);
opj_set_error_handler(l_codec, error_callback, 00); opj_set_error_handler(l_codec, error_callback, 00);
parameters.flags |= OPJ_DPARAMETERS_DUMP_FLAG;
/* Setup the decoder decoding parameters using user parameters */ /* Setup the decoder decoding parameters using user parameters */
if (!opj_setup_decoder(l_codec, &parameters)) { if (!opj_setup_decoder(l_codec, &parameters)) {
fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n"); fprintf(stderr, "ERROR -> opj_dump: failed to setup the decoder\n");

View File

@ -2054,6 +2054,7 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
OPJ_UINT32 l_remaining_size; OPJ_UINT32 l_remaining_size;
OPJ_UINT32 l_nb_tiles; OPJ_UINT32 l_nb_tiles;
OPJ_UINT32 l_tmp, l_tx1, l_ty1; OPJ_UINT32 l_tmp, l_tx1, l_ty1;
OPJ_UINT32 l_prec0, l_sgnd0;
opj_image_t *l_image = 00; opj_image_t *l_image = 00;
opj_cp_t *l_cp = 00; opj_cp_t *l_cp = 00;
opj_image_comp_t * l_img_comp = 00; opj_image_comp_t * l_img_comp = 00;
@ -2156,7 +2157,20 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
"Error with SIZ marker: illegal tile offset\n"); "Error with SIZ marker: illegal tile offset\n");
return OPJ_FALSE; return OPJ_FALSE;
} }
if (!p_j2k->dump_state) {
OPJ_UINT32 siz_w, siz_h;
siz_w = l_image->x1 - l_image->x0;
siz_h = l_image->y1 - l_image->y0;
if (p_j2k->ihdr_w > 0 && p_j2k->ihdr_h > 0
&& (p_j2k->ihdr_w != siz_w || p_j2k->ihdr_h != siz_h)) {
opj_event_msg(p_manager, EVT_ERROR,
"Error with SIZ marker: IHDR w(%u) h(%u) vs. SIZ w(%u) h(%u)\n", p_j2k->ihdr_w,
p_j2k->ihdr_h, siz_w, siz_h);
return OPJ_FALSE;
}
}
#ifdef USE_JPWL #ifdef USE_JPWL
if (l_cp->correct) { if (l_cp->correct) {
/* if JPWL is on, we check whether TX errors have damaged /* if JPWL is on, we check whether TX errors have damaged
@ -2211,6 +2225,8 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
l_img_comp = l_image->comps; l_img_comp = l_image->comps;
l_prec0 = 0;
l_sgnd0 = 0;
/* Read the component information */ /* Read the component information */
for (i = 0; i < l_image->numcomps; ++i) { for (i = 0; i < l_image->numcomps; ++i) {
OPJ_UINT32 tmp; OPJ_UINT32 tmp;
@ -2218,6 +2234,20 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
++p_header_data; ++p_header_data;
l_img_comp->prec = (tmp & 0x7f) + 1; l_img_comp->prec = (tmp & 0x7f) + 1;
l_img_comp->sgnd = tmp >> 7; l_img_comp->sgnd = tmp >> 7;
if (p_j2k->dump_state == 0) {
if (i == 0) {
l_prec0 = l_img_comp->prec;
l_sgnd0 = l_img_comp->sgnd;
} else if (l_cp->bpc_is_255 == 0
&& (l_img_comp->prec != l_prec0 || l_img_comp->sgnd != l_sgnd0)) {
opj_event_msg(p_manager, EVT_ERROR,
"Invalid precision and/or sgnd values for comp[%d]:\n"
" [0] prec(%d) sgnd(%d) [%d] prec(%d) sgnd(%d)\n", i, l_prec0, l_sgnd0,
i, l_img_comp->prec, l_img_comp->sgnd);
return OPJ_FALSE;
}
}
opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */ opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */
++p_header_data; ++p_header_data;
l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
@ -2240,7 +2270,6 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
i, l_img_comp->prec); i, l_img_comp->prec);
return OPJ_FALSE; return OPJ_FALSE;
} }
#ifdef USE_JPWL #ifdef USE_JPWL
if (l_cp->correct) { if (l_cp->correct) {
/* if JPWL is on, we check whether TX errors have damaged /* if JPWL is on, we check whether TX errors have damaged
@ -2277,6 +2306,10 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
++l_img_comp; ++l_img_comp;
} }
if (l_cp->tdx == 0 || l_cp->tdy == 0) {
return OPJ_FALSE;
}
/* Compute the number of tiles */ /* Compute the number of tiles */
l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0),
(OPJ_INT32)l_cp->tdx); (OPJ_INT32)l_cp->tdx);
@ -6345,6 +6378,7 @@ void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters)
j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer; j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce; j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
j2k->dump_state = (parameters->flags & OPJ_DPARAMETERS_DUMP_FLAG);
#ifdef USE_JPWL #ifdef USE_JPWL
j2k->m_cp.correct = parameters->jpwl_correct; j2k->m_cp.correct = parameters->jpwl_correct;
j2k->m_cp.exp_comps = parameters->jpwl_exp_comps; j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
@ -8735,6 +8769,7 @@ OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k,
opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n"); opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n");
return OPJ_FALSE; return OPJ_FALSE;
} }
p_j2k->m_tcd->enumcs = p_j2k->enumcs;
/* p_data can be set to NULL when the call will take care of using */ /* p_data can be set to NULL when the call will take care of using */
/* itself the TCD data. This is typically the case for whole single */ /* itself the TCD data. This is typically the case for whole single */

View File

@ -446,6 +446,7 @@ typedef struct opj_cp {
OPJ_BITFIELD ppm : 1; OPJ_BITFIELD ppm : 1;
/** tells if the parameter is a coding or decoding one */ /** tells if the parameter is a coding or decoding one */
OPJ_BITFIELD m_is_decoder : 1; OPJ_BITFIELD m_is_decoder : 1;
OPJ_BITFIELD bpc_is_255 : 1;
/* <<UniPG */ /* <<UniPG */
} opj_cp_t; } opj_cp_t;
@ -574,6 +575,11 @@ typedef struct opj_j2k {
/** Thread pool */ /** Thread pool */
opj_thread_pool_t* m_tp; opj_thread_pool_t* m_tp;
OPJ_UINT32 ihdr_w;
OPJ_UINT32 ihdr_h;
OPJ_UINT32 enumcs;
unsigned int dump_state;
} }
opj_j2k_t; opj_j2k_t;

View File

@ -619,6 +619,11 @@ static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
opj_read_bytes(p_image_header_data, &(jp2->IPR), 1); /* IPR */ opj_read_bytes(p_image_header_data, &(jp2->IPR), 1); /* IPR */
++ p_image_header_data; ++ p_image_header_data;
jp2->j2k->m_cp.bpc_is_255 = (jp2->bpc == 255);
jp2->j2k->ihdr_w = jp2->w;
jp2->j2k->ihdr_h = jp2->h;
jp2->has_ihdr = 1;
return OPJ_TRUE; return OPJ_TRUE;
} }
@ -1573,6 +1578,9 @@ static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
"COLR BOX meth value is not a regular value (%d), " "COLR BOX meth value is not a regular value (%d), "
"so we will ignore the entire Colour Specification box. \n", jp2->meth); "so we will ignore the entire Colour Specification box. \n", jp2->meth);
} }
if (jp2->color.jp2_has_colr) {
jp2->j2k->enumcs = jp2->enumcs;
}
return OPJ_TRUE; return OPJ_TRUE;
} }
@ -2733,6 +2741,7 @@ static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
} }
jp2->jp2_state |= JP2_STATE_HEADER; jp2->jp2_state |= JP2_STATE_HEADER;
jp2->has_jp2h = 1;
return OPJ_TRUE; return OPJ_TRUE;
} }
@ -2838,6 +2847,14 @@ OPJ_BOOL opj_jp2_read_header(opj_stream_private_t *p_stream,
if (! opj_jp2_exec(jp2, jp2->m_procedure_list, p_stream, p_manager)) { if (! opj_jp2_exec(jp2, jp2->m_procedure_list, p_stream, p_manager)) {
return OPJ_FALSE; return OPJ_FALSE;
} }
if (jp2->has_jp2h == 0) {
opj_event_msg(p_manager, EVT_ERROR, "JP2H box missing. Required.\n");
return OPJ_FALSE;
}
if (jp2->has_ihdr == 0) {
opj_event_msg(p_manager, EVT_ERROR, "IHDR box_missing. Required.\n");
return OPJ_FALSE;
}
return opj_j2k_read_header(p_stream, return opj_j2k_read_header(p_stream,
jp2->j2k, jp2->j2k,

View File

@ -187,6 +187,8 @@ typedef struct opj_jp2 {
opj_jp2_color_t color; opj_jp2_color_t color;
OPJ_BOOL ignore_pclr_cmap_cdef; OPJ_BOOL ignore_pclr_cmap_cdef;
OPJ_BYTE has_jp2h;
OPJ_BYTE has_ihdr;
} }
opj_jp2_t; opj_jp2_t;

View File

@ -501,6 +501,7 @@ typedef struct opj_cparameters {
} opj_cparameters_t; } opj_cparameters_t;
#define OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG 0x0001 #define OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG 0x0001
#define OPJ_DPARAMETERS_DUMP_FLAG 0x0002
/** /**
* Decompression parameters * Decompression parameters

View File

@ -248,6 +248,8 @@ typedef struct opj_tcd {
OPJ_BITFIELD m_is_decoder : 1; OPJ_BITFIELD m_is_decoder : 1;
/** Thread pool */ /** Thread pool */
opj_thread_pool_t* thread_pool; opj_thread_pool_t* thread_pool;
/** AFL test */
OPJ_UINT32 enumcs;
} opj_tcd_t; } opj_tcd_t;
/** @name Exported functions */ /** @name Exported functions */