/* pngpread.c - read a png file in push mode * * Copyright (c) 2018-2024 Cosmin Truta * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson * Copyright (c) 1996-1997 Andreas Dilger * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h
*/
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 * have to supply the same data again:
*/ 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 * is actually 'saved', in which case we just return 0
*/ if (png_ptr->save_buffer_size < remaining) return remaining - png_ptr->save_buffer_size;
}
}
return 0;
}
png_uint_32 PNGAPI
png_process_data_skip(png_structrp png_ptr)
{ /* TODO: Deprecate and remove this API. * Somewhere the implementation of this seems to have been lost, * or abandoned. It was only to support some internal back-door access * to png_struct) in libpng-1.4.x.
*/
png_app_warning(png_ptr, "png_process_data_skip is not implemented in any current version of libpng"); return 0;
}
/* What we do with the incoming data depends on what we were previously * doing before we ran out of data...
*/ 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 * the correct PNG signature. It is possible that this routine is called * with bytes already read from the signature, either because they have been * checked by the calling application, or because of multiple calls to this * 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 * and the 4-byte chunk length before proceeding with decoding the * chunk data. To fully decode each of these chunks, we also make * sure we have enough data in the buffer for the 4-byte CRC at the * end of every chunk (except IDAT, which is handled separately).
*/ if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
{
png_byte chunk_length[4];
png_byte chunk_tag[4];
#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");
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 * header chunks, and we can start reading the image (or if this * is called after the image has been read - we have an error).
*/ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
png_error(png_ptr, "Missing IHDR before IDAT");
/* 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 * are of different types and we don't know which variable has the fewest * bits. Carefully select the smaller and cast it to the type of the * larger - this cannot overflow. Do not cast in the following test - it * will break on either 16-bit or 64-bit platforms.
*/ if (idat_size < save_size)
save_size = (size_t)idat_size;
/* We want the smaller of 'idat_size' and 'current_buffer_size', but they * are of different types and we don't know which variable has the fewest * bits. Carefully select the smaller and cast it to the type of the * larger - this cannot overflow.
*/ 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 * before returning, calling the row callback as required to * handle the uncompressed results.
*/
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 * or the stream marked as finished.
*/ 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 * has someplace to put the results. It doesn't matter * if we don't expect any results -- it may be the input * data is just the LZ end code.
*/ 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 * LZ stream (a stream with a missing end code) can still * be handled, otherwise (Z_NO_FLUSH) a future zlib * implementation might defer output and therefore * change the current behavior (see comments in inflate.c * for why this doesn't happen at present with zlib 1.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 * damaged end code). Treat that as a warning.
*/ 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? * If it is, artificially terminate the LZ output * 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 * input check below.
*/ 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 * is left at this point we have bytes of IDAT data * after the zlib end code.
*/ 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, while the buffer really is this big in current versions of libpng * it may not be in the future, so this was changed just to copy the * interlaced row count:
*/
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");
}
case 1:
{ 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;
}
case 2:
{ 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;
}
case 3:
{ 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;
}
case 4:
{ 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;
}
case 5:
{ 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);
}
break;
}
default: case 6:
{
png_push_have_row(png_ptr, png_ptr->row_buf + 1);
png_read_push_finish_row(png_ptr);
/* new_row is a flag here - if it is NULL then the app callback was called * from an empty row (see the calls to png_struct::row_fn below), otherwise * it must be png_ptr->row_buf+1
*/ if (new_row != NULL)
png_combine_row(png_ptr, old_row, 1/*blocky display*/);
} #endif/* READ_INTERLACING */
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.