@@ -502,7 +502,6 @@ OPJ_BOOL opj_t2_decode_packets(opj_tcd_t* tcd,
502502 l_current_pi->precno, l_current_pi->layno, skip_packet ? "skipped" : "kept");
503503 */
504504 }
505-
506505 if (!skip_packet ) {
507506 l_nb_bytes_read = 0 ;
508507
@@ -1348,6 +1347,7 @@ static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
13481347 opj_tcd_cblk_dec_t * l_cblk = 00 ;
13491348 opj_tcd_resolution_t * l_res =
13501349 & p_tile -> comps [p_pi -> compno ].resolutions [p_pi -> resno ];
1350+ OPJ_UINT32 partial_buffer = 0 ;
13511351
13521352 OPJ_ARG_NOT_USED (p_t2 );
13531353 OPJ_ARG_NOT_USED (pack_info );
@@ -1367,6 +1367,12 @@ static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
13671367 for (cblkno = 0 ; cblkno < l_nb_code_blocks ; ++ cblkno ) {
13681368 opj_tcd_seg_t * l_seg = 00 ;
13691369
1370+ // if we have a partial data stream, set numchunks to zero
1371+ // since we have no data to actually decode.
1372+ if (partial_buffer ) {
1373+ l_cblk -> numchunks = 0 ;
1374+ }
1375+
13701376 if (!l_cblk -> numnewpasses ) {
13711377 /* nothing to do */
13721378 ++ l_cblk ;
@@ -1389,12 +1395,29 @@ static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
13891395 /* Check possible overflow (on l_current_data only, assumes input args already checked) then size */
13901396 if ((((OPJ_SIZE_T )l_current_data + (OPJ_SIZE_T )l_seg -> newlen ) <
13911397 (OPJ_SIZE_T )l_current_data ) ||
1392- (l_current_data + l_seg -> newlen > p_src_data + p_max_length )) {
1398+ (l_current_data + l_seg -> newlen > p_src_data + p_max_length ) ||
1399+ (partial_buffer )) {
13931400 opj_event_msg (p_manager , EVT_ERROR ,
13941401 "read: segment too long (%d) with max (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n" ,
13951402 l_seg -> newlen , p_max_length , cblkno , p_pi -> precno , bandno , p_pi -> resno ,
13961403 p_pi -> compno );
1397- return OPJ_FALSE ;
1404+ // NOTE - originall we would return OPJ_FALSE here when we encountered partial bitstream
1405+ //return OPJ_FALSE;
1406+
1407+ // skip this codeblock since it is a partial read
1408+ partial_buffer = 1 ;
1409+ l_cblk -> numchunks = 0 ;
1410+
1411+ l_seg -> numpasses += l_seg -> numnewpasses ;
1412+ l_cblk -> numnewpasses -= l_seg -> numnewpasses ;
1413+ if (l_cblk -> numnewpasses > 0 ) {
1414+ ++ l_seg ;
1415+ ++ l_cblk -> numsegs ;
1416+ }
1417+ if (l_cblk -> numnewpasses > 0 ) {
1418+ break ;
1419+ }
1420+ continue ;
13981421 }
13991422
14001423#ifdef USE_JPWL
@@ -1456,8 +1479,12 @@ static OPJ_BOOL opj_t2_read_packet_data(opj_t2_t* p_t2,
14561479 ++ l_band ;
14571480 }
14581481
1459- * (p_data_read ) = (OPJ_UINT32 )(l_current_data - p_src_data );
1460-
1482+ // return the number of bytes read
1483+ if (partial_buffer ) {
1484+ * (p_data_read ) = p_max_length ;
1485+ } else {
1486+ * (p_data_read ) = (OPJ_UINT32 )(l_current_data - p_src_data );
1487+ }
14611488
14621489 return OPJ_TRUE ;
14631490}
0 commit comments