diff --git a/src/bin/common/color.c b/src/bin/common/color.c index 234c7bdf..5018a090 100644 --- a/src/bin/common/color.c +++ b/src/bin/common/color.c @@ -395,13 +395,36 @@ void color_apply_icc_profile(opj_image_t *image) out_space = cmsGetColorSpace(in_prof); intent = cmsGetHeaderRenderingIntent(in_prof); - max_w = image->comps[0].w; max_h = image->comps[0].h; prec = (int)image->comps[0].prec; 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) { + 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 ) { in_type = TYPE_RGB_8; diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index deee4f6e..c55c65a8 100644 --- a/src/bin/jp2/convert.c +++ b/src/bin/jp2/convert.c @@ -906,9 +906,10 @@ int imagetotga(opj_image_t * image, const char *outfile) { for (i = 0; i < image->numcomps-1; i++) { if ((image->comps[0].dx != image->comps[i+1].dx) ||(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); - fprintf(stderr, "Unable to create a tga file with such J2K image charateristics."); + fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.\n"); return 1; } } @@ -1743,7 +1744,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) int *red, *green, *blue, *alpha; int wr, hr, max; int i; - unsigned int compno, ncomp; + unsigned int compno, ncomp, ui; int adjustR, adjustG, adjustB, adjustA; int fails, two, want_gray, has_alpha, triple; int prec, v; @@ -1777,6 +1778,8 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) && image->comps[1].dy == image->comps[2].dy && image->comps[0].prec == image->comps[1].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 ))) { fdest = fopen(outfile, "wb"); @@ -1904,6 +1907,8 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split) fprintf(stderr, "imagetopnm: memory out\n"); return 1; } + fprintf(stderr, " imagetopnm: creating %d .pgm files.\n",ncomp); + for (compno = 0; compno < ncomp; compno++) { if (ncomp > 1) @@ -2126,7 +2131,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL { FILE *rawFile = NULL; size_t res; - unsigned int compno; + unsigned int compno, numcomps; int w, h, fails; int line, row, curr, mask; int *ptr; @@ -2139,6 +2144,32 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL 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"); if (!rawFile) { fprintf(stderr, "Failed to open %s for writing !!\n", outfile); @@ -2146,9 +2177,9 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL } fails = 1; - fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps); + fprintf(stdout,"Raw image characteristics: %d components\n", numcomps); - for(compno = 0; compno < image->numcomps; compno++) + for(compno = 0; compno < numcomps; compno++) { fprintf(stdout,"Component %u characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w, image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned"); @@ -2238,7 +2269,7 @@ static int imagetoraw_common(opj_image_t * image, const char *outfile, OPJ_BOOL } 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; } else diff --git a/src/bin/jp2/convertbmp.c b/src/bin/jp2/convertbmp.c index ae83077c..42fba6cd 100644 --- a/src/bin/jp2/convertbmp.c +++ b/src/bin/jp2/convertbmp.c @@ -807,7 +807,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) { int adjustR, adjustG, adjustB; 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; } if (image->numcomps >= 3 && image->comps[0].dx == image->comps[1].dx @@ -815,7 +815,9 @@ int imagetobmp(opj_image_t * image, const char *outfile) { && image->comps[0].dy == image->comps[1].dy && image->comps[1].dy == image->comps[2].dy && 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 @@ -925,6 +927,9 @@ int imagetobmp(opj_image_t * image, const char *outfile) { fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile); 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; h = (int)image->comps[0].h; diff --git a/src/bin/jp2/converttif.c b/src/bin/jp2/converttif.c index 143d3be6..cf75abfd 100644 --- a/src/bin/jp2/converttif.c +++ b/src/bin/jp2/converttif.c @@ -553,20 +553,18 @@ static void tif_32sto16u(const OPJ_INT32* pSrc, OPJ_UINT16* pDst, OPJ_SIZE_T len int imagetotif(opj_image_t * image, const char *outfile) { - int width, height; - int bps,adjust, sgnd; - int tiPhoto; + uint32 width, height, bps, tiPhoto; + int adjust, sgnd; TIFF *tif; tdata_t buf; - tsize_t strip_size; + tmsize_t strip_size, rowStride; OPJ_UINT32 i, numcomps; - OPJ_SIZE_T rowStride; OPJ_INT32* buffer32s = NULL; OPJ_INT32 const* planes[4]; convert_32s_PXCX cvtPxToCx = NULL; convert_32sXXx_C1R cvt32sToTif = NULL; - bps = (int)image->comps[0].prec; + bps = (uint32)image->comps[0].prec; planes[0] = image->comps[0].data; numcomps = image->numcomps; @@ -590,6 +588,7 @@ int imagetotif(opj_image_t * image, const char *outfile) } else { tiPhoto = PHOTOMETRIC_MINISBLACK; } + for (i = 1U; i < numcomps; ++i) { if (image->comps[0].dx != image->comps[i].dx) { break; @@ -610,7 +609,7 @@ int imagetotif(opj_image_t * image, const char *outfile) fprintf(stderr,"\tAborting\n"); return 1; } - + if(bps > 16) bps = 0; if(bps == 0) { @@ -674,13 +673,13 @@ int imagetotif(opj_image_t * image, const char *outfile) break; } sgnd = (int)image->comps[0].sgnd; - adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0; - width = (int)image->comps[0].w; - height = (int)image->comps[0].h; + adjust = sgnd ? (int)(1 << (image->comps[0].prec - 1)) : 0; + width = (uint32)image->comps[0].w; + height = (uint32)image->comps[0].h; TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height); - TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps); + TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (uint32)numcomps); TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps); TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); @@ -688,8 +687,8 @@ int imagetotif(opj_image_t * image, const char *outfile) TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1); strip_size = TIFFStripSize(tif); - rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U; - if (rowStride != (OPJ_SIZE_T)strip_size) { + rowStride = (width * numcomps * bps + 7U) / 8U; + if (rowStride != strip_size) { fprintf(stderr, "Invalid TIFF strip size\n"); TIFFClose(tif); return 1; @@ -699,7 +698,7 @@ int imagetotif(opj_image_t * image, const char *outfile) TIFFClose(tif); return 1; } - buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(OPJ_INT32)); + buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(width * numcomps * sizeof(OPJ_INT32))); if (buffer32s == NULL) { _TIFFfree(buf); TIFFClose(tif); @@ -1211,20 +1210,19 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) TIFF *tif; tdata_t buf; tstrip_t strip; - tsize_t strip_size; + tmsize_t strip_size; int j, currentPlane, numcomps = 0, w, h; OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN; opj_image_cmptparm_t cmptparm[4]; /* RGBA */ opj_image_t *image = NULL; int has_alpha = 0; - unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC; - unsigned int tiWidth, tiHeight; + uint32 tiBps, tiPhoto, tiSf, tiSpp, tiPC, tiWidth, tiHeight; OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz); convert_XXx32s_C1R cvtTifTo32s = NULL; convert_32s_CXPX cvtCxToPx = NULL; OPJ_INT32* buffer32s = NULL; OPJ_INT32* planes[4]; - OPJ_SIZE_T rowStride; + tmsize_t rowStride; tif = TIFFOpen(filename, "r"); @@ -1243,22 +1241,35 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &tiSpp); TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &tiPhoto); TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &tiPC); - w= (int)tiWidth; - h= (int)tiHeight; - - if(tiBps > 16U) { - fprintf(stderr,"tiftoimage: Bits=%d, Only 1 to 16 bits implemented\n",tiBps); - fprintf(stderr,"\tAborting\n"); + + if(tiSpp == 0 || tiSpp > 4) { /* should be 1 ... 4 */ + fprintf(stderr,"tiftoimage: Bad value for samples per pixel == %hu.\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); return NULL; } if(tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) { - fprintf(stderr,"tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto); + fprintf(stderr,"tiftoimage: Bad color format %d.\n" + "\tOnly RGB(A) and GRAY(A) has been implemented\n",(int) tiPhoto); fprintf(stderr,"\tAborting\n"); TIFFClose(tif); 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) { case 1: case 2: @@ -1312,7 +1323,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo); - + if(extrasamples >= 1) { switch(sampleinfo[0]) @@ -1333,7 +1344,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) else /* extrasamples == 0 */ if(tiSpp == 4 || tiSpp == 2) has_alpha = 1; } - + /* initialize image components */ memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t)); @@ -1346,7 +1357,7 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) } else { is_cinema = 0U; } - + if(tiPhoto == PHOTOMETRIC_RGB) /* RGB(A) */ { numcomps = 3 + has_alpha; @@ -1384,10 +1395,24 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) image->x0 = (OPJ_UINT32)parameters->image_offset_x0; image->y0 = (OPJ_UINT32)parameters->image_offset_y0; 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->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++) { planes[j] = image->comps[j].data; @@ -1395,15 +1420,15 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1)); strip_size = TIFFStripSize(tif); - + buf = _TIFFmalloc(strip_size); if (buf == NULL) { TIFFClose(tif); opj_image_destroy(image); return NULL; } - rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U; - buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32)); + rowStride = (w * tiSpp * tiBps + 7U) / 8U; + buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(w * tiSpp * sizeof(OPJ_INT32))); if (buffer32s == NULL) { _TIFFfree(buf); TIFFClose(tif); @@ -1421,11 +1446,20 @@ opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters) for(; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++) { const OPJ_UINT8 *dat8; - OPJ_SIZE_T ssize; + tmsize_t ssize; - ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size); + ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size); + if(ssize < 1 || ssize > strip_size) { + fprintf(stderr,"tiftoimage: Bad value for ssize(%ld) " + "vs. strip_size(%ld).\n\tAborting.\n",ssize,strip_size); + _TIFFfree(buf); + _TIFFfree(buffer32s); + TIFFClose(tif); + opj_image_destroy(image); + return NULL; + } dat8 = (const OPJ_UINT8*)buf; - + while (ssize >= rowStride) { cvtTifTo32s(dat8, buffer32s, (OPJ_SIZE_T)w * tiSpp); cvtCxToPx(buffer32s, planes, (OPJ_SIZE_T)w); diff --git a/src/bin/jp2/opj_decompress.c b/src/bin/jp2/opj_decompress.c index 83160c3d..572e537d 100644 --- a/src/bin/jp2/opj_decompress.c +++ b/src/bin/jp2/opj_decompress.c @@ -1503,7 +1503,7 @@ int main(int argc, char **argv) failed = 1; } else { - fprintf(stdout,"[INFO] Generated Outfile %s\n",parameters.outfile); + fprintf(stdout,"[INFO] Generated Outfile %s (or .pgm files)\n",parameters.outfile); } break; @@ -1607,7 +1607,7 @@ fin: if(dirptr->filename_buf) free(dirptr->filename_buf); free(dirptr); } - if (numDecompressedImages) { + if (numDecompressedImages && !failed) { fprintf(stdout, "decode time: %d ms\n", (int)( (tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages)); } return failed ? EXIT_FAILURE : EXIT_SUCCESS; diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c index 66802bb9..b98a7362 100644 --- a/src/lib/openjp2/j2k.c +++ b/src/lib/openjp2/j2k.c @@ -2000,6 +2000,7 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, OPJ_UINT32 l_remaining_size; OPJ_UINT32 l_nb_tiles; OPJ_UINT32 l_tmp, l_tx1, l_ty1; + OPJ_UINT32 l_prec0, l_sgnd0; opj_image_t *l_image = 00; opj_cp_t *l_cp = 00; opj_image_comp_t * l_img_comp = 00; @@ -2085,7 +2086,17 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: illegal tile offset\n"); return OPJ_FALSE; } - + { + OPJ_UINT32 w, h, tw, th; + + w = l_image->x1 - l_image->x0; h = l_image->y1 - l_image->y0; + tw = l_cp->tdx - l_cp->tx0; th = l_cp->tdy - l_cp->ty0; + + if(tw > w || th > h) { + opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: IMAGE w(%u) h(%u) vs. TILE w(%u) h(%u)\n", w, h, tw, th); + return OPJ_FALSE; + } + } #ifdef USE_JPWL if (l_cp->correct) { /* if JPWL is on, we check whether TX errors have damaged @@ -2138,6 +2149,7 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, l_img_comp = l_image->comps; + l_prec0 = 0; l_sgnd0 = 0; /* Read the component information */ for (i = 0; i < l_image->numcomps; ++i){ OPJ_UINT32 tmp; @@ -2145,6 +2157,19 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, ++p_header_data; l_img_comp->prec = (tmp & 0x7f) + 1; l_img_comp->sgnd = tmp >> 7; + + 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 */ ++p_header_data; l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */ @@ -2158,7 +2183,7 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, i, l_img_comp->dx, l_img_comp->dy); return OPJ_FALSE; } - if( l_img_comp->prec > 38) { /* TODO openjpeg won't handle more than ? */ + if( l_img_comp->prec < 1 || l_img_comp->prec > 38) { /* TODO openjpeg won't handle more than ? */ opj_event_msg(p_manager, EVT_ERROR, "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm)\n", i, l_img_comp->prec); @@ -2196,7 +2221,6 @@ static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k, l_img_comp->factor = l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */ ++l_img_comp; } - /* Compute the number of tiles */ l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx); l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy); @@ -4855,7 +4879,7 @@ static OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k, if (l_tcp->m_data) { if (! opj_tcd_init_decode_tile(l_tcd, i)) { opj_tcd_destroy(l_tcd); - opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile %d\n",i); return OPJ_FALSE; } @@ -8130,7 +8154,7 @@ OPJ_BOOL opj_j2k_read_tile_header( opj_j2k_t * p_j2k, } /*FIXME ???*/ if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number, p_manager)) { - opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n"); + opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile %d\n",p_j2k->m_current_tile_number); return OPJ_FALSE; } @@ -9779,10 +9803,10 @@ static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, opj_event_mgr_t * p_manager) { OPJ_BOOL l_go_on = OPJ_TRUE; - OPJ_UINT32 l_current_tile_no; + OPJ_UINT32 l_current_tile_no = 0; OPJ_UINT32 l_data_size,l_max_data_size; OPJ_INT32 l_tile_x0,l_tile_y0,l_tile_x1,l_tile_y1; - OPJ_UINT32 l_nb_comps; + OPJ_UINT32 l_nb_comps = 0; OPJ_BYTE * l_current_data; OPJ_UINT32 nr_tiles = 0; @@ -9791,9 +9815,10 @@ static OPJ_BOOL opj_j2k_decode_tiles ( opj_j2k_t *p_j2k, opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tiles\n"); return OPJ_FALSE; } - l_max_data_size = 1000; + l_max_data_size = 1000; l_data_size = 0; for (;;) { + l_tile_x0 = l_tile_y0 = l_tile_x1 = l_tile_y1 = 0; if (! opj_j2k_read_tile_header( p_j2k, &l_current_tile_no, &l_data_size, @@ -10030,6 +10055,9 @@ OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k, for (compno = 0; compno < p_image->numcomps; compno++) { p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded; p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; + if(p_image->comps[compno].data == NULL){ + return OPJ_FALSE; + } #if 0 char fn[256]; sprintf( fn, "/tmp/%d.raw", compno ); @@ -10131,6 +10159,9 @@ OPJ_BOOL opj_j2k_get_tile( opj_j2k_t *p_j2k, p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data; + if(p_image->comps[compno].data == NULL){ + return OPJ_FALSE; + } p_j2k->m_output_image->comps[compno].data = NULL; } diff --git a/src/lib/openjp2/jp2.c b/src/lib/openjp2/jp2.c index ea81d0f5..0a43b17e 100644 --- a/src/lib/openjp2/jp2.c +++ b/src/lib/openjp2/jp2.c @@ -597,6 +597,8 @@ static OPJ_BOOL opj_jp2_read_ihdr( opj_jp2_t *jp2, opj_read_bytes(p_image_header_data,&(jp2->IPR),1); /* IPR */ ++ p_image_header_data; + jp2->j2k->m_cp.bpc_is_255 = (jp2->bpc == 255); + return OPJ_TRUE; } diff --git a/src/lib/openjp2/pi.c b/src/lib/openjp2/pi.c index 41a2f046..d0db3eee 100644 --- a/src/lib/openjp2/pi.c +++ b/src/lib/openjp2/pi.c @@ -324,10 +324,14 @@ static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi) { for (compno = 0; compno < pi->numcomps; compno++) { comp = &pi->comps[compno]; for (resno = 0; resno < comp->numresolutions; resno++) { + int64_t dx64, dy64; OPJ_UINT32 dx, dy; res = &comp->resolutions[resno]; - dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno)); - dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno)); + dx64 = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno)); + dy64 = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno)); + if(dx64 <= 0 || dx64 > UINT_MAX || dy64 <= 0 || dy64 > UINT_MAX) return OPJ_FALSE; + + dx = (OPJ_UINT32)dx64; dy = (OPJ_UINT32)dy64; pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx); pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy); } @@ -408,10 +412,15 @@ static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) { for (compno = 0; compno < pi->numcomps; compno++) { comp = &pi->comps[compno]; for (resno = 0; resno < comp->numresolutions; resno++) { + int64_t dx64, dy64; OPJ_UINT32 dx, dy; res = &comp->resolutions[resno]; - dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno)); - dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno)); + dx64 = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno)); + dy64 = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno)); + if(dx64 <= 0 || dx64 > UINT_MAX || dy64 <= 0 || dy64 > UINT_MAX) return OPJ_FALSE; + + dx = (OPJ_UINT32)dx64; dy = (OPJ_UINT32)dy64; + pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx); pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy); } @@ -491,10 +500,15 @@ static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) { pi->dx = 0; pi->dy = 0; for (resno = 0; resno < comp->numresolutions; resno++) { + int64_t dx64, dy64; OPJ_UINT32 dx, dy; res = &comp->resolutions[resno]; - dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno)); - dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno)); + dx64 = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno)); + dy64 = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno)); + if(dx64 <= 0 || dx64 > UINT_MAX || dy64 <= 0 || dy64 > UINT_MAX) return OPJ_FALSE; + + dx = (OPJ_UINT32)dx64; dy = (OPJ_UINT32)dy64; + pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx); pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy); } diff --git a/src/lib/openjp2/tcd.c b/src/lib/openjp2/tcd.c index 36f40843..f96ec4c2 100644 --- a/src/lib/openjp2/tcd.c +++ b/src/lib/openjp2/tcd.c @@ -681,7 +681,8 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, OPJ_UINT32 l_nb_code_blocks_size; /* size of data for a tile */ OPJ_UINT32 l_data_size; - + OPJ_INT32 l_max_w, l_max_h, l_tile_w, l_tile_h; + l_cp = p_tcd->cp; l_tcp = &(l_cp->tcps[p_tile_no]); l_tile = p_tcd->tcd_image->tiles; @@ -720,6 +721,7 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, } /*fprintf(stderr, "Tile border = %d,%d,%d,%d\n", l_tile->x0, l_tile->y0,l_tile->x1,l_tile->y1);*/ + l_max_w = l_image->comps[0].w; l_max_h = l_image->comps[0].h; /*tile->numcomps = image->numcomps; */ for (compno = 0; compno < l_tile->numcomps; ++compno) { /*fprintf(stderr, "compno = %d/%d\n", compno, l_tile->numcomps);*/ @@ -730,7 +732,14 @@ static INLINE OPJ_BOOL opj_tcd_init_tile(opj_tcd_t *p_tcd, OPJ_UINT32 p_tile_no, l_tilec->x1 = opj_int_ceildiv(l_tile->x1, (OPJ_INT32)l_image_comp->dx); l_tilec->y1 = opj_int_ceildiv(l_tile->y1, (OPJ_INT32)l_image_comp->dy); /*fprintf(stderr, "\tTile compo border = %d,%d,%d,%d\n", l_tilec->x0, l_tilec->y0,l_tilec->x1,l_tilec->y1);*/ - + + l_tile_w = l_tilec->x1 - l_tilec->x0; l_tile_h = l_tilec->y1 - l_tilec->y0; + + if(l_tile_w > l_max_w || l_tile_h > l_max_h) { + opj_event_msg(manager, EVT_ERROR, "component[%d] tile w(%d) h(%d) vs. max_w(%d) max_h(%d)\n", + compno, l_tile_w,l_tile_h,l_max_w,l_max_h); + return OPJ_FALSE; + } /* compute l_data_size with overflow check */ l_data_size = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0); /* issue 733, l_data_size == 0U, probably something wrong should be checked before getting here */