while (png_ptr->buffer_size)
{
png_process_some_data(png_ptr, info_ptr);
}
}
size_t PNGAPI
png_process_data_pause(png_structrp png_ptr, int save)
{ if (png_ptr != NULL)
{ /* It's easiest for the caller if we do the save; then the caller doesn't *havetosupplythesamedataagain:
*/ if (save != 0)
png_push_save_buffer(png_ptr); else
{ /* This includes any pending saved bytes: */
size_t remaining = png_ptr->buffer_size;
png_ptr->buffer_size = 0;
/* So subtract the saved buffer size, unless all the data *isactually'saved',inwhichcasewejustreturn0
*/ if (png_ptr->save_buffer_size < remaining) return remaining - png_ptr->save_buffer_size;
}
}
return0;
}
png_uint_32 PNGAPI
png_process_data_skip(png_structrp png_ptr)
{ /* TODO: Deprecate and remove this API. *Somewheretheimplementationofthisseemstohavebeenlost, *orabandoned.Itwasonlytosupportsomeinternalback-dooraccess *topng_struct)inlibpng-1.4.x.
*/
png_app_warning(png_ptr, "png_process_data_skip is not implemented in any current version of libpng"); return0;
}
/* What we do with the incoming data depends on what we were previously *doingbeforeweranoutofdata...
*/ void/* PRIVATE */
png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
{ if (png_ptr == NULL) return;
switch (png_ptr->process_mode)
{ case PNG_READ_SIG_MODE:
{
png_push_read_sig(png_ptr, info_ptr); break;
}
case PNG_READ_CHUNK_MODE:
{
png_push_read_chunk(png_ptr, info_ptr); break;
}
case PNG_READ_IDAT_MODE:
{
png_push_read_IDAT(png_ptr); break;
}
default:
{
png_ptr->buffer_size = 0; break;
}
}
}
/* Read any remaining signature bytes from the stream and compare them with *thecorrectPNGsignature.Itispossiblethatthisroutineiscalled *withbytesalreadyreadfromthesignature,eitherbecausetheyhavebeen *checkedbythecallingapplication,orbecauseofmultiplecallstothis *routine.
*/ void/* PRIVATE */
png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
{
size_t num_checked = png_ptr->sig_bytes; /* SAFE, does not exceed 8 */
size_t num_to_check = 8 - num_checked;
if (png_ptr->buffer_size < num_to_check)
{
num_to_check = png_ptr->buffer_size;
}
/* First we make sure we have enough data for the 4-byte chunk name *andthe4-bytechunklengthbeforeproceedingwithdecodingthe *chunkdata.Tofullydecodeeachofthesechunks,wealsomake *surewehaveenoughdatainthebufferforthe4-byteCRCatthe *endofeverychunk(exceptIDAT,whichishandledseparately).
*/ if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
{
PNG_PUSH_SAVE_BUFFER_IF_LT(8)
png_ptr->push_length = png_read_chunk_header(png_ptr);
png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
}
chunk_name = png_ptr->chunk_name;
#ifdef PNG_READ_APNG_SUPPORTED if (png_ptr->num_frames_read > 0 &&
png_ptr->num_frames_read < info_ptr->num_frames)
{ if (chunk_name == png_IDAT)
{ /* Discard trailing IDATs for the first frame */ if ((png_ptr->mode & PNG_HAVE_fcTL) != 0 ||
png_ptr->num_frames_read > 1)
png_error(png_ptr, "out of place IDAT");
elseif (chunk_name == png_fdAT)
{ if ((png_ptr->mode & PNG_HAVE_fcTL) == 0)
{ /* Discard trailing fdATs for frames other than the first */ if (png_ptr->num_frames_read < 2)
png_error(png_ptr, "out of place fdAT");
if (chunk_name == png_IDAT)
{ if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
/* If we reach an IDAT chunk, this means we have read all of the *headerchunks,andwecanstartreadingtheimage(orifthis *iscalledaftertheimagehasbeenread-wehaveanerror).
*/ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
png_error(png_ptr, "Missing IHDR before IDAT");
if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0) if (png_ptr->push_length == 0) return;
png_ptr->mode |= PNG_HAVE_IDAT;
if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
png_benign_error(png_ptr, "Too many IDATs found");
}
elseif ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
{ /* These flags must be set consistently for all non-IDAT chunks, *includingtheunknownchunks.
*/
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT | PNG_AFTER_IDAT;
}
if (chunk_name == png_IHDR)
{ if (png_ptr->push_length != 13)
png_error(png_ptr, "Invalid IHDR length");
/* TODO: this code can be commoned up with the same code in push_read */ #ifdef PNG_READ_APNG_SUPPORTED
PNG_PUSH_SAVE_BUFFER_IF_LT(12) #else
PNG_PUSH_SAVE_BUFFER_IF_LT(8) #endif
png_push_fill_buffer(png_ptr, chunk_length, 4);
png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
png_reset_crc(png_ptr);
png_crc_read(png_ptr, chunk_tag, 4);
png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
/* We want the smaller of 'idat_size' and 'current_buffer_size', but they *areofdifferenttypesandwedon'tknowwhichvariablehasthefewest *bits.Carefullyselectthesmallerandcastittothetypeofthe *larger-thiscannotoverflow.Donotcastinthefollowingtest-it *willbreakoneither16-bitor64-bitplatforms.
*/ if (idat_size < save_size)
save_size = (size_t)idat_size;
/* We want the smaller of 'idat_size' and 'current_buffer_size', but they *areofdifferenttypesandwedon'tknowwhichvariablehasthefewest *bits.Carefullyselectthesmallerandcastittothetypeofthe *larger-thiscannotoverflow.
*/ if (idat_size < save_size)
save_size = (size_t)idat_size;
void/* PRIVATE */
png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
size_t buffer_length)
{ /* The caller checks for a non-zero buffer length. */ if (!(buffer_length > 0) || buffer == NULL)
png_error(png_ptr, "No IDAT data (internal error)");
#ifdef PNG_READ_APNG_SUPPORTED /* If the app is not APNG-aware, decode only the first frame */ if ((png_ptr->apng_flags & PNG_APNG_APP) == 0 &&
png_ptr->num_frames_read > 0)
{
png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; return;
} #endif
/* This routine must process all the data it has been given *beforereturning,callingtherowcallbackasrequiredto *handletheuncompressedresults.
*/
png_ptr->zstream.next_in = buffer; /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
png_ptr->zstream.avail_in = (uInt)buffer_length;
/* Keep going until the decompressed data is all processed *orthestreammarkedasfinished.
*/ while (png_ptr->zstream.avail_in > 0 &&
(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
{ int ret;
/* We have data for zlib, but we must check that zlib *hassomeplacetoputtheresults.Itdoesn'tmatter *ifwedon'texpectanyresults--itmaybetheinput *dataisjusttheLZendcode.
*/ if (!(png_ptr->zstream.avail_out > 0))
{ /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
png_ptr->iwidth) + 1);
png_ptr->zstream.next_out = png_ptr->row_buf;
}
/* Using Z_SYNC_FLUSH here means that an unterminated *LZstream(astreamwithamissingendcode)canstill *behandled,otherwise(Z_NO_FLUSH)afuturezlib *implementationmightdeferoutputandtherefore *changethecurrentbehavior(seecommentsininflate.c *forwhythisdoesn'thappenatpresentwithzlib1.2.5).
*/
ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
/* Check for any failure before proceeding. */ if (ret != Z_OK && ret != Z_STREAM_END)
{ /* Terminate the decompression. */
png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
png_ptr->zowner = 0;
/* This may be a truncated stream (missing or *damagedendcode).Treatthatasawarning.
*/ if (png_ptr->row_number >= png_ptr->num_rows ||
png_ptr->pass > 6)
png_warning(png_ptr, "Truncated compressed data in IDAT");
else
{ if (ret == Z_DATA_ERROR)
png_benign_error(png_ptr, "IDAT: ADLER32 checksum mismatch"); else
png_error(png_ptr, "Decompression error in IDAT");
}
/* Skip the check on unprocessed input */ return;
}
/* Did inflate output any data? */ if (png_ptr->zstream.next_out != png_ptr->row_buf)
{ /* Is this unexpected data after the last row? *Ifitis,artificiallyterminatetheLZoutput *here.
*/ if (png_ptr->row_number >= png_ptr->num_rows ||
png_ptr->pass > 6)
{ /* Extra data. */
png_warning(png_ptr, "Extra compressed data in IDAT");
png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
png_ptr->zowner = 0;
/* Do no more processing; skip the unprocessed *inputcheckbelow.
*/ return;
}
/* Do we have a complete row? */ if (png_ptr->zstream.avail_out == 0)
png_push_process_row(png_ptr);
}
/* And check for the end of the stream. */ if (ret == Z_STREAM_END)
png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
}
/* All the data should have been processed, if anything *isleftatthispointwehavebytesofIDATdata *afterthezlibendcode.
*/ if (png_ptr->zstream.avail_in > 0)
png_warning(png_ptr, "Extra compression data in IDAT");
}
void/* PRIVATE */
png_push_process_row(png_structrp png_ptr)
{ /* 1.5.6: row_info moved out of png_struct to a local here. */
png_row_info row_info;
/* libpng 1.5.6: the following line was copying png_ptr->rowbytes before *1.5.6,whilethebufferreallyisthisbigincurrentversionsoflibpng *itmaynotbeinthefuture,sothiswaschangedjusttocopythe *interlacedrowcount:
*/
memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
#ifdef PNG_READ_TRANSFORMS_SUPPORTED if (png_ptr->transformations != 0)
png_do_read_transformations(png_ptr, &row_info); #endif
/* The transformed pixel depth should match the depth now in row_info. */ if (png_ptr->transformed_pixel_depth == 0)
{
png_ptr->transformed_pixel_depth = row_info.pixel_depth; if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
png_error(png_ptr, "progressive row overflow");
}
case1:
{ int i; for (i = 0; i < 8 && png_ptr->pass == 1; i++)
{
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
png_read_push_finish_row(png_ptr);
}
if (png_ptr->pass == 2) /* Skip top 4 generated rows */
{ for (i = 0; i < 4 && png_ptr->pass == 2; i++)
{
png_push_have_row(png_ptr, NULL);
png_read_push_finish_row(png_ptr);
}
}
break;
}
case2:
{ int i;
for (i = 0; i < 4 && png_ptr->pass == 2; i++)
{
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
png_read_push_finish_row(png_ptr);
}
for (i = 0; i < 4 && png_ptr->pass == 2; i++)
{
png_push_have_row(png_ptr, NULL);
png_read_push_finish_row(png_ptr);
}
if (png_ptr->pass == 4) /* Pass 3 might be empty */
{ for (i = 0; i < 2 && png_ptr->pass == 4; i++)
{
png_push_have_row(png_ptr, NULL);
png_read_push_finish_row(png_ptr);
}
}
break;
}
case3:
{ int i;
for (i = 0; i < 4 && png_ptr->pass == 3; i++)
{
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
png_read_push_finish_row(png_ptr);
}
if (png_ptr->pass == 4) /* Skip top two generated rows */
{ for (i = 0; i < 2 && png_ptr->pass == 4; i++)
{
png_push_have_row(png_ptr, NULL);
png_read_push_finish_row(png_ptr);
}
}
break;
}
case4:
{ int i;
for (i = 0; i < 2 && png_ptr->pass == 4; i++)
{
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
png_read_push_finish_row(png_ptr);
}
for (i = 0; i < 2 && png_ptr->pass == 4; i++)
{
png_push_have_row(png_ptr, NULL);
png_read_push_finish_row(png_ptr);
}
if (png_ptr->pass == 6) /* Pass 5 might be empty */
{
png_push_have_row(png_ptr, NULL);
png_read_push_finish_row(png_ptr);
}
break;
}
case5:
{ int i;
for (i = 0; i < 2 && png_ptr->pass == 5; i++)
{
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
png_read_push_finish_row(png_ptr);
}
if (png_ptr->pass == 6) /* Skip top generated row */
{
png_push_have_row(png_ptr, NULL);
png_read_push_finish_row(png_ptr);
}
/* new_row is a flag here - if it is NULL then the app callback was called *fromanemptyrow(seethecallstopng_struct::row_fnbelow),otherwise *itmustbepng_ptr->row_buf+1
*/ if (new_row != NULL)
png_combine_row(png_ptr, old_row, 1/*blocky display*/);
} #endif/* READ_INTERLACING */