diff --git a/libopenjpeg/int.h b/libopenjpeg/int.h index a15aa95c..147dccfe 100644 --- a/libopenjpeg/int.h +++ b/libopenjpeg/int.h @@ -44,6 +44,8 @@ The functions in INT.H have for goal to realize operations on integers. /*@{*/ /* ----------------------------------------------------------------------- */ +#include + #ifdef HAVE_STDINT_H #include #else @@ -102,8 +104,10 @@ Divide an integer and round upwards @return Returns a divided by b */ static INLINE int int_ceildiv(int a, int b) { - return (a + b - 1) / b; + assert(b); + return (a + b - 1) / b; } + /** Divide an integer by a power of 2 and round upwards @return Returns a divided by 2^b diff --git a/libopenjpeg/j2k.c b/libopenjpeg/j2k.c index a4a17ff2..c8fdc7c2 100644 --- a/libopenjpeg/j2k.c +++ b/libopenjpeg/j2k.c @@ -247,6 +247,12 @@ Add tile header marker information @param len length of marker segment */ static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len); +/** +Validate encoding parameters and image before actual encoding +@param j2k J2K handle +@param image IMAGE handle + */ +static opj_bool j2k_validate_encode( opj_j2k_t *j2k, opj_image_t *image); /*@}*/ @@ -2450,6 +2456,9 @@ opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_code cp = j2k->cp; + /* Parameters validation */ + if (!j2k_validate_encode(j2k, image)) return OPJ_FALSE; + /* INDEX >> */ j2k->cstr_info = cstr_info; if (cstr_info) { @@ -2698,3 +2707,21 @@ static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsi marker->len = len; cstr_info->tile[tileno].marknum++; } + +static opj_bool j2k_validate_encode(opj_j2k_t *j2k, opj_image_t *image) { + + int compno; + opj_bool is_valid = OPJ_TRUE; + + /* preconditions */ + assert(j2k != 00); + assert(image != 00); + + /* PARAMETERS checking */ + for (compno=0; compno < image->numcomps; compno++) { + is_valid &= (image->comps[compno].dx > 1 && j2k->image->comps[compno].dx < 255); + is_valid &= (image->comps[compno].dy > 1 && j2k->image->comps[compno].dy < 255); + } + + return is_valid; +} diff --git a/tests/unit/testempty1.c b/tests/unit/testempty1.c index 8f33f247..6ab2d0e8 100644 --- a/tests/unit/testempty1.c +++ b/tests/unit/testempty1.c @@ -111,6 +111,15 @@ int main(int argc, char *argv[]) cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0); assert( cio ); bSuccess = opj_encode(cinfo, cio, image, NULL); + + if( !bSuccess ) + { + opj_cio_close(cio); + opj_destroy_compress(cinfo); + opj_image_destroy(image); + return 0; + } + assert( bSuccess ); codestream_length = (size_t)cio_tell(cio); diff --git a/tests/unit/testempty2.c b/tests/unit/testempty2.c index f6eac7b9..32d1924d 100644 --- a/tests/unit/testempty2.c +++ b/tests/unit/testempty2.c @@ -115,6 +115,15 @@ int main(int argc, char *argv[]) cio = opj_cio_open((opj_common_ptr)cinfo, NULL, 0); assert( cio ); bSuccess = opj_encode(cinfo, cio, image, NULL); + + if( !bSuccess ) + { + opj_cio_close(cio); + opj_destroy_compress(cinfo); + opj_image_destroy(image); + return 0; + } + assert( bSuccess ); codestream_length = (size_t)cio_tell(cio);