fukasawa e60969
fukasawa e60969
/* pngpread.c - read a png file in push mode
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.18 [July 23, 2015]
fukasawa e60969
 * Copyright (c) 1998-2002,2004,2006-2015 Glenn Randers-Pehrson
fukasawa e60969
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
fukasawa e60969
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
#include "pngpriv.h"
fukasawa e60969
fukasawa e60969
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
fukasawa e60969
fukasawa e60969
/* Push model modes */
fukasawa e60969
#define PNG_READ_SIG_MODE   0
fukasawa e60969
#define PNG_READ_CHUNK_MODE 1
fukasawa e60969
#define PNG_READ_IDAT_MODE  2
fukasawa e60969
#define PNG_READ_tEXt_MODE  4
fukasawa e60969
#define PNG_READ_zTXt_MODE  5
fukasawa e60969
#define PNG_READ_DONE_MODE  6
fukasawa e60969
#define PNG_READ_iTXt_MODE  7
fukasawa e60969
#define PNG_ERROR_MODE      8
fukasawa e60969
fukasawa e60969
#define PNG_PUSH_SAVE_BUFFER_IF_FULL \
fukasawa e60969
if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
fukasawa e60969
   { png_push_save_buffer(png_ptr); return; }
fukasawa e60969
#define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
fukasawa e60969
if (png_ptr->buffer_size < N) \
fukasawa e60969
   { png_push_save_buffer(png_ptr); return; }
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_process_data(png_structrp png_ptr, png_inforp info_ptr,
fukasawa e60969
    png_bytep buffer, png_size_t buffer_size)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL || info_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_push_restore_buffer(png_ptr, buffer, buffer_size);
fukasawa e60969
fukasawa e60969
   while (png_ptr->buffer_size)
fukasawa e60969
   {
fukasawa e60969
      png_process_some_data(png_ptr, info_ptr);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
png_size_t PNGAPI
fukasawa e60969
png_process_data_pause(png_structrp png_ptr, int save)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      /* It's easiest for the caller if we do the save; then the caller doesn't
fukasawa e60969
       * have to supply the same data again:
fukasawa e60969
       */
fukasawa e60969
      if (save != 0)
fukasawa e60969
         png_push_save_buffer(png_ptr);
fukasawa e60969
      else
fukasawa e60969
      {
fukasawa e60969
         /* This includes any pending saved bytes: */
fukasawa e60969
         png_size_t remaining = png_ptr->buffer_size;
fukasawa e60969
         png_ptr->buffer_size = 0;
fukasawa e60969
fukasawa e60969
         /* So subtract the saved buffer size, unless all the data
fukasawa e60969
          * is actually 'saved', in which case we just return 0
fukasawa e60969
          */
fukasawa e60969
         if (png_ptr->save_buffer_size < remaining)
fukasawa e60969
            return remaining - png_ptr->save_buffer_size;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
png_uint_32 PNGAPI
fukasawa e60969
png_process_data_skip(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
  /* TODO: Deprecate and remove this API.
fukasawa e60969
   * Somewhere the implementation of this seems to have been lost,
fukasawa e60969
   * or abandoned.  It was only to support some internal back-door access
fukasawa e60969
   * to png_struct) in libpng-1.4.x.
fukasawa e60969
   */
fukasawa e60969
   png_app_warning(png_ptr,
fukasawa e60969
"png_process_data_skip is not implemented in any current version of libpng");
fukasawa e60969
   return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* What we do with the incoming data depends on what we were previously
fukasawa e60969
 * doing before we ran out of data...
fukasawa e60969
 */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   switch (png_ptr->process_mode)
fukasawa e60969
   {
fukasawa e60969
      case PNG_READ_SIG_MODE:
fukasawa e60969
      {
fukasawa e60969
         png_push_read_sig(png_ptr, info_ptr);
fukasawa e60969
         break;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      case PNG_READ_CHUNK_MODE:
fukasawa e60969
      {
fukasawa e60969
         png_push_read_chunk(png_ptr, info_ptr);
fukasawa e60969
         break;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      case PNG_READ_IDAT_MODE:
fukasawa e60969
      {
fukasawa e60969
         png_push_read_IDAT(png_ptr);
fukasawa e60969
         break;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      default:
fukasawa e60969
      {
fukasawa e60969
         png_ptr->buffer_size = 0;
fukasawa e60969
         break;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* Read any remaining signature bytes from the stream and compare them with
fukasawa e60969
 * the correct PNG signature.  It is possible that this routine is called
fukasawa e60969
 * with bytes already read from the signature, either because they have been
fukasawa e60969
 * checked by the calling application, or because of multiple calls to this
fukasawa e60969
 * routine.
fukasawa e60969
 */
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
fukasawa e60969
{
fukasawa e60969
   png_size_t num_checked = png_ptr->sig_bytes, /* SAFE, does not exceed 8 */
fukasawa e60969
       num_to_check = 8 - num_checked;
fukasawa e60969
fukasawa e60969
   if (png_ptr->buffer_size < num_to_check)
fukasawa e60969
   {
fukasawa e60969
      num_to_check = png_ptr->buffer_size;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
fukasawa e60969
       num_to_check);
fukasawa e60969
   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
fukasawa e60969
fukasawa e60969
   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
fukasawa e60969
   {
fukasawa e60969
      if (num_checked < 4 &&
fukasawa e60969
          png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
fukasawa e60969
         png_error(png_ptr, "Not a PNG file");
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         png_error(png_ptr, "PNG file corrupted by ASCII conversion");
fukasawa e60969
   }
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      if (png_ptr->sig_bytes >= 8)
fukasawa e60969
      {
fukasawa e60969
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 chunk_name;
fukasawa e60969
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
fukasawa e60969
   int keep; /* unknown handling method */
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* First we make sure we have enough data for the 4-byte chunk name
fukasawa e60969
    * and the 4-byte chunk length before proceeding with decoding the
fukasawa e60969
    * chunk data.  To fully decode each of these chunks, we also make
fukasawa e60969
    * sure we have enough data in the buffer for the 4-byte CRC at the
fukasawa e60969
    * end of every chunk (except IDAT, which is handled separately).
fukasawa e60969
    */
fukasawa e60969
   if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
fukasawa e60969
   {
fukasawa e60969
      png_byte chunk_length[4];
fukasawa e60969
      png_byte chunk_tag[4];
fukasawa e60969
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_LT(8)
fukasawa e60969
      png_push_fill_buffer(png_ptr, chunk_length, 4);
fukasawa e60969
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
fukasawa e60969
      png_reset_crc(png_ptr);
fukasawa e60969
      png_crc_read(png_ptr, chunk_tag, 4);
fukasawa e60969
      png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
fukasawa e60969
      png_check_chunk_name(png_ptr, png_ptr->chunk_name);
fukasawa e60969
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   chunk_name = png_ptr->chunk_name;
fukasawa e60969
fukasawa e60969
   if (chunk_name == png_IDAT)
fukasawa e60969
   {
fukasawa e60969
      if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
fukasawa e60969
         png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
fukasawa e60969
fukasawa e60969
      /* If we reach an IDAT chunk, this means we have read all of the
fukasawa e60969
       * header chunks, and we can start reading the image (or if this
fukasawa e60969
       * is called after the image has been read - we have an error).
fukasawa e60969
       */
fukasawa e60969
      if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
fukasawa e60969
         png_error(png_ptr, "Missing IHDR before IDAT");
fukasawa e60969
fukasawa e60969
      else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
fukasawa e60969
          (png_ptr->mode & PNG_HAVE_PLTE) == 0)
fukasawa e60969
         png_error(png_ptr, "Missing PLTE before IDAT");
fukasawa e60969
fukasawa e60969
      png_ptr->mode |= PNG_HAVE_IDAT;
fukasawa e60969
      png_ptr->process_mode = PNG_READ_IDAT_MODE;
fukasawa e60969
fukasawa e60969
      if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
fukasawa e60969
         if (png_ptr->push_length == 0)
fukasawa e60969
            return;
fukasawa e60969
fukasawa e60969
      if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
fukasawa e60969
         png_benign_error(png_ptr, "Too many IDATs found");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (chunk_name == png_IHDR)
fukasawa e60969
   {
fukasawa e60969
      if (png_ptr->push_length != 13)
fukasawa e60969
         png_error(png_ptr, "Invalid IHDR length");
fukasawa e60969
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (chunk_name == png_IEND)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
fukasawa e60969
      png_ptr->process_mode = PNG_READ_DONE_MODE;
fukasawa e60969
      png_push_have_end(png_ptr, info_ptr);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
fukasawa e60969
   else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
fukasawa e60969
fukasawa e60969
      if (chunk_name == png_PLTE)
fukasawa e60969
         png_ptr->mode |= PNG_HAVE_PLTE;
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   else if (chunk_name == png_PLTE)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (chunk_name == png_IDAT)
fukasawa e60969
   {
fukasawa e60969
      png_ptr->idat_size = png_ptr->push_length;
fukasawa e60969
      png_ptr->process_mode = PNG_READ_IDAT_MODE;
fukasawa e60969
      png_push_have_info(png_ptr, info_ptr);
fukasawa e60969
      png_ptr->zstream.avail_out =
fukasawa e60969
          (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
fukasawa e60969
          png_ptr->iwidth) + 1;
fukasawa e60969
      png_ptr->zstream.next_out = png_ptr->row_buf;
fukasawa e60969
      return;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_gAMA_SUPPORTED
fukasawa e60969
   else if (png_ptr->chunk_name == png_gAMA)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_sBIT_SUPPORTED
fukasawa e60969
   else if (png_ptr->chunk_name == png_sBIT)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_cHRM_SUPPORTED
fukasawa e60969
   else if (png_ptr->chunk_name == png_cHRM)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_sRGB_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_sRGB)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_iCCP_SUPPORTED
fukasawa e60969
   else if (png_ptr->chunk_name == png_iCCP)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_sPLT_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_sPLT)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_tRNS_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_tRNS)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_bKGD_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_bKGD)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_hIST_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_hIST)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_pHYs_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_pHYs)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_oFFs_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_oFFs)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_pCAL_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_pCAL)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_sCAL_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_sCAL)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_tIME_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_tIME)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_tEXt_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_tEXt)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_zTXt_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_zTXt)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
#endif
fukasawa e60969
#ifdef PNG_READ_iTXt_SUPPORTED
fukasawa e60969
   else if (chunk_name == png_iTXt)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
fukasawa e60969
   }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_FULL
fukasawa e60969
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
fukasawa e60969
         PNG_HANDLE_CHUNK_AS_DEFAULT);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void PNGCBAPI
fukasawa e60969
png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
fukasawa e60969
{
fukasawa e60969
   png_bytep ptr;
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   ptr = buffer;
fukasawa e60969
   if (png_ptr->save_buffer_size != 0)
fukasawa e60969
   {
fukasawa e60969
      png_size_t save_size;
fukasawa e60969
fukasawa e60969
      if (length < png_ptr->save_buffer_size)
fukasawa e60969
         save_size = length;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         save_size = png_ptr->save_buffer_size;
fukasawa e60969
fukasawa e60969
      memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
fukasawa e60969
      length -= save_size;
fukasawa e60969
      ptr += save_size;
fukasawa e60969
      png_ptr->buffer_size -= save_size;
fukasawa e60969
      png_ptr->save_buffer_size -= save_size;
fukasawa e60969
      png_ptr->save_buffer_ptr += save_size;
fukasawa e60969
   }
fukasawa e60969
   if (length != 0 && png_ptr->current_buffer_size != 0)
fukasawa e60969
   {
fukasawa e60969
      png_size_t save_size;
fukasawa e60969
fukasawa e60969
      if (length < png_ptr->current_buffer_size)
fukasawa e60969
         save_size = length;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         save_size = png_ptr->current_buffer_size;
fukasawa e60969
fukasawa e60969
      memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
fukasawa e60969
      png_ptr->buffer_size -= save_size;
fukasawa e60969
      png_ptr->current_buffer_size -= save_size;
fukasawa e60969
      png_ptr->current_buffer_ptr += save_size;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_push_save_buffer(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr->save_buffer_size != 0)
fukasawa e60969
   {
fukasawa e60969
      if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
fukasawa e60969
      {
fukasawa e60969
         png_size_t i, istop;
fukasawa e60969
         png_bytep sp;
fukasawa e60969
         png_bytep dp;
fukasawa e60969
fukasawa e60969
         istop = png_ptr->save_buffer_size;
fukasawa e60969
         for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
fukasawa e60969
             i < istop; i++, sp++, dp++)
fukasawa e60969
         {
fukasawa e60969
            *dp = *sp;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
fukasawa e60969
       png_ptr->save_buffer_max)
fukasawa e60969
   {
fukasawa e60969
      png_size_t new_max;
fukasawa e60969
      png_bytep old_buffer;
fukasawa e60969
fukasawa e60969
      if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
fukasawa e60969
          (png_ptr->current_buffer_size + 256))
fukasawa e60969
      {
fukasawa e60969
         png_error(png_ptr, "Potential overflow of save_buffer");
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
fukasawa e60969
      old_buffer = png_ptr->save_buffer;
fukasawa e60969
      png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
fukasawa e60969
          (png_size_t)new_max);
fukasawa e60969
fukasawa e60969
      if (png_ptr->save_buffer == NULL)
fukasawa e60969
      {
fukasawa e60969
         png_free(png_ptr, old_buffer);
fukasawa e60969
         png_error(png_ptr, "Insufficient memory for save_buffer");
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
fukasawa e60969
      png_free(png_ptr, old_buffer);
fukasawa e60969
      png_ptr->save_buffer_max = new_max;
fukasawa e60969
   }
fukasawa e60969
   if (png_ptr->current_buffer_size)
fukasawa e60969
   {
fukasawa e60969
      memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
fukasawa e60969
         png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
fukasawa e60969
      png_ptr->save_buffer_size += png_ptr->current_buffer_size;
fukasawa e60969
      png_ptr->current_buffer_size = 0;
fukasawa e60969
   }
fukasawa e60969
   png_ptr->save_buffer_ptr = png_ptr->save_buffer;
fukasawa e60969
   png_ptr->buffer_size = 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
fukasawa e60969
   png_size_t buffer_length)
fukasawa e60969
{
fukasawa e60969
   png_ptr->current_buffer = buffer;
fukasawa e60969
   png_ptr->current_buffer_size = buffer_length;
fukasawa e60969
   png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
fukasawa e60969
   png_ptr->current_buffer_ptr = png_ptr->current_buffer;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_push_read_IDAT(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
fukasawa e60969
   {
fukasawa e60969
      png_byte chunk_length[4];
fukasawa e60969
      png_byte chunk_tag[4];
fukasawa e60969
fukasawa e60969
      /* TODO: this code can be commoned up with the same code in push_read */
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_LT(8)
fukasawa e60969
      png_push_fill_buffer(png_ptr, chunk_length, 4);
fukasawa e60969
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
fukasawa e60969
      png_reset_crc(png_ptr);
fukasawa e60969
      png_crc_read(png_ptr, chunk_tag, 4);
fukasawa e60969
      png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
fukasawa e60969
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
fukasawa e60969
fukasawa e60969
      if (png_ptr->chunk_name != png_IDAT)
fukasawa e60969
      {
fukasawa e60969
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
fukasawa e60969
fukasawa e60969
         if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
fukasawa e60969
            png_error(png_ptr, "Not enough compressed data");
fukasawa e60969
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      png_ptr->idat_size = png_ptr->push_length;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
fukasawa e60969
   {
fukasawa e60969
      png_size_t save_size = png_ptr->save_buffer_size;
fukasawa e60969
      png_uint_32 idat_size = png_ptr->idat_size;
fukasawa e60969
fukasawa e60969
      /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
fukasawa e60969
       * are of different types and we don't know which variable has the fewest
fukasawa e60969
       * bits.  Carefully select the smaller and cast it to the type of the
fukasawa e60969
       * larger - this cannot overflow.  Do not cast in the following test - it
fukasawa e60969
       * will break on either 16-bit or 64-bit platforms.
fukasawa e60969
       */
fukasawa e60969
      if (idat_size < save_size)
fukasawa e60969
         save_size = (png_size_t)idat_size;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         idat_size = (png_uint_32)save_size;
fukasawa e60969
fukasawa e60969
      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
fukasawa e60969
fukasawa e60969
      png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
fukasawa e60969
fukasawa e60969
      png_ptr->idat_size -= idat_size;
fukasawa e60969
      png_ptr->buffer_size -= save_size;
fukasawa e60969
      png_ptr->save_buffer_size -= save_size;
fukasawa e60969
      png_ptr->save_buffer_ptr += save_size;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
fukasawa e60969
   {
fukasawa e60969
      png_size_t save_size = png_ptr->current_buffer_size;
fukasawa e60969
      png_uint_32 idat_size = png_ptr->idat_size;
fukasawa e60969
fukasawa e60969
      /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
fukasawa e60969
       * are of different types and we don't know which variable has the fewest
fukasawa e60969
       * bits.  Carefully select the smaller and cast it to the type of the
fukasawa e60969
       * larger - this cannot overflow.
fukasawa e60969
       */
fukasawa e60969
      if (idat_size < save_size)
fukasawa e60969
         save_size = (png_size_t)idat_size;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         idat_size = (png_uint_32)save_size;
fukasawa e60969
fukasawa e60969
      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
fukasawa e60969
fukasawa e60969
      png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
fukasawa e60969
fukasawa e60969
      png_ptr->idat_size -= idat_size;
fukasawa e60969
      png_ptr->buffer_size -= save_size;
fukasawa e60969
      png_ptr->current_buffer_size -= save_size;
fukasawa e60969
      png_ptr->current_buffer_ptr += save_size;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   if (png_ptr->idat_size == 0)
fukasawa e60969
   {
fukasawa e60969
      PNG_PUSH_SAVE_BUFFER_IF_LT(4)
fukasawa e60969
      png_crc_finish(png_ptr, 0);
fukasawa e60969
      png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
fukasawa e60969
      png_ptr->mode |= PNG_AFTER_IDAT;
fukasawa e60969
      png_ptr->zowner = 0;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
fukasawa e60969
   png_size_t buffer_length)
fukasawa e60969
{
fukasawa e60969
   /* The caller checks for a non-zero buffer length. */
fukasawa e60969
   if (!(buffer_length > 0) || buffer == NULL)
fukasawa e60969
      png_error(png_ptr, "No IDAT data (internal error)");
fukasawa e60969
fukasawa e60969
   /* This routine must process all the data it has been given
fukasawa e60969
    * before returning, calling the row callback as required to
fukasawa e60969
    * handle the uncompressed results.
fukasawa e60969
    */
fukasawa e60969
   png_ptr->zstream.next_in = buffer;
fukasawa e60969
   /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
fukasawa e60969
   png_ptr->zstream.avail_in = (uInt)buffer_length;
fukasawa e60969
fukasawa e60969
   /* Keep going until the decompressed data is all processed
fukasawa e60969
    * or the stream marked as finished.
fukasawa e60969
    */
fukasawa e60969
   while (png_ptr->zstream.avail_in > 0 &&
fukasawa e60969
      (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
fukasawa e60969
   {
fukasawa e60969
      int ret;
fukasawa e60969
fukasawa e60969
      /* We have data for zlib, but we must check that zlib
fukasawa e60969
       * has someplace to put the results.  It doesn't matter
fukasawa e60969
       * if we don't expect any results -- it may be the input
fukasawa e60969
       * data is just the LZ end code.
fukasawa e60969
       */
fukasawa e60969
      if (!(png_ptr->zstream.avail_out > 0))
fukasawa e60969
      {
fukasawa e60969
         /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
fukasawa e60969
         png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
fukasawa e60969
             png_ptr->iwidth) + 1);
fukasawa e60969
fukasawa e60969
         png_ptr->zstream.next_out = png_ptr->row_buf;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Using Z_SYNC_FLUSH here means that an unterminated
fukasawa e60969
       * LZ stream (a stream with a missing end code) can still
fukasawa e60969
       * be handled, otherwise (Z_NO_FLUSH) a future zlib
fukasawa e60969
       * implementation might defer output and therefore
fukasawa e60969
       * change the current behavior (see comments in inflate.c
fukasawa e60969
       * for why this doesn't happen at present with zlib 1.2.5).
fukasawa e60969
       */
fukasawa e60969
      ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
fukasawa e60969
fukasawa e60969
      /* Check for any failure before proceeding. */
fukasawa e60969
      if (ret != Z_OK && ret != Z_STREAM_END)
fukasawa e60969
      {
fukasawa e60969
         /* Terminate the decompression. */
fukasawa e60969
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
fukasawa e60969
         png_ptr->zowner = 0;
fukasawa e60969
fukasawa e60969
         /* This may be a truncated stream (missing or
fukasawa e60969
          * damaged end code).  Treat that as a warning.
fukasawa e60969
          */
fukasawa e60969
         if (png_ptr->row_number >= png_ptr->num_rows ||
fukasawa e60969
             png_ptr->pass > 6)
fukasawa e60969
            png_warning(png_ptr, "Truncated compressed data in IDAT");
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            png_error(png_ptr, "Decompression error in IDAT");
fukasawa e60969
fukasawa e60969
         /* Skip the check on unprocessed input */
fukasawa e60969
         return;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Did inflate output any data? */
fukasawa e60969
      if (png_ptr->zstream.next_out != png_ptr->row_buf)
fukasawa e60969
      {
fukasawa e60969
         /* Is this unexpected data after the last row?
fukasawa e60969
          * If it is, artificially terminate the LZ output
fukasawa e60969
          * here.
fukasawa e60969
          */
fukasawa e60969
         if (png_ptr->row_number >= png_ptr->num_rows ||
fukasawa e60969
             png_ptr->pass > 6)
fukasawa e60969
         {
fukasawa e60969
            /* Extra data. */
fukasawa e60969
            png_warning(png_ptr, "Extra compressed data in IDAT");
fukasawa e60969
            png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
fukasawa e60969
            png_ptr->zowner = 0;
fukasawa e60969
fukasawa e60969
            /* Do no more processing; skip the unprocessed
fukasawa e60969
             * input check below.
fukasawa e60969
             */
fukasawa e60969
            return;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         /* Do we have a complete row? */
fukasawa e60969
         if (png_ptr->zstream.avail_out == 0)
fukasawa e60969
            png_push_process_row(png_ptr);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* And check for the end of the stream. */
fukasawa e60969
      if (ret == Z_STREAM_END)
fukasawa e60969
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* All the data should have been processed, if anything
fukasawa e60969
    * is left at this point we have bytes of IDAT data
fukasawa e60969
    * after the zlib end code.
fukasawa e60969
    */
fukasawa e60969
   if (png_ptr->zstream.avail_in > 0)
fukasawa e60969
      png_warning(png_ptr, "Extra compression data in IDAT");
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_push_process_row(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   /* 1.5.6: row_info moved out of png_struct to a local here. */
fukasawa e60969
   png_row_info row_info;
fukasawa e60969
fukasawa e60969
   row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
fukasawa e60969
   row_info.color_type = png_ptr->color_type;
fukasawa e60969
   row_info.bit_depth = png_ptr->bit_depth;
fukasawa e60969
   row_info.channels = png_ptr->channels;
fukasawa e60969
   row_info.pixel_depth = png_ptr->pixel_depth;
fukasawa e60969
   row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
fukasawa e60969
fukasawa e60969
   if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
fukasawa e60969
   {
fukasawa e60969
      if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
fukasawa e60969
         png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
fukasawa e60969
            png_ptr->prev_row + 1, png_ptr->row_buf[0]);
fukasawa e60969
      else
fukasawa e60969
         png_error(png_ptr, "bad adaptive filter value");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
fukasawa e60969
    * 1.5.6, while the buffer really is this big in current versions of libpng
fukasawa e60969
    * it may not be in the future, so this was changed just to copy the
fukasawa e60969
    * interlaced row count:
fukasawa e60969
    */
fukasawa e60969
   memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
fukasawa e60969
   if (png_ptr->transformations != 0)
fukasawa e60969
      png_do_read_transformations(png_ptr, &row_info);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   /* The transformed pixel depth should match the depth now in row_info. */
fukasawa e60969
   if (png_ptr->transformed_pixel_depth == 0)
fukasawa e60969
   {
fukasawa e60969
      png_ptr->transformed_pixel_depth = row_info.pixel_depth;
fukasawa e60969
      if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
fukasawa e60969
         png_error(png_ptr, "progressive row overflow");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
fukasawa e60969
      png_error(png_ptr, "internal progressive row size calculation error");
fukasawa e60969
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_INTERLACING_SUPPORTED
fukasawa e60969
   /* Expand interlaced rows to full size */
fukasawa e60969
   if (png_ptr->interlaced != 0 &&
fukasawa e60969
       (png_ptr->transformations & PNG_INTERLACE) != 0)
fukasawa e60969
   {
fukasawa e60969
      if (png_ptr->pass < 6)
fukasawa e60969
         png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
fukasawa e60969
            png_ptr->transformations);
fukasawa e60969
fukasawa e60969
      switch (png_ptr->pass)
fukasawa e60969
      {
fukasawa e60969
         case 0:
fukasawa e60969
         {
fukasawa e60969
            int i;
fukasawa e60969
            for (i = 0; i < 8 && png_ptr->pass == 0; i++)
fukasawa e60969
            {
fukasawa e60969
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
fukasawa e60969
               png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            if (png_ptr->pass == 2) /* Pass 1 might be empty */
fukasawa e60969
            {
fukasawa e60969
               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
fukasawa e60969
               {
fukasawa e60969
                  png_push_have_row(png_ptr, NULL);
fukasawa e60969
                  png_read_push_finish_row(png_ptr);
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            if (png_ptr->pass == 4 && png_ptr->height <= 4)
fukasawa e60969
            {
fukasawa e60969
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
fukasawa e60969
               {
fukasawa e60969
                  png_push_have_row(png_ptr, NULL);
fukasawa e60969
                  png_read_push_finish_row(png_ptr);
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            if (png_ptr->pass == 6 && png_ptr->height <= 4)
fukasawa e60969
            {
fukasawa e60969
                png_push_have_row(png_ptr, NULL);
fukasawa e60969
                png_read_push_finish_row(png_ptr);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         case 1:
fukasawa e60969
         {
fukasawa e60969
            int i;
fukasawa e60969
            for (i = 0; i < 8 && png_ptr->pass == 1; i++)
fukasawa e60969
            {
fukasawa e60969
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
fukasawa e60969
               png_read_push_finish_row(png_ptr);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            if (png_ptr->pass == 2) /* Skip top 4 generated rows */
fukasawa e60969
            {
fukasawa e60969
               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
fukasawa e60969
               {
fukasawa e60969
                  png_push_have_row(png_ptr, NULL);
fukasawa e60969
                  png_read_push_finish_row(png_ptr);
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         case 2:
fukasawa e60969
         {
fukasawa e60969
            int i;
fukasawa e60969
fukasawa e60969
            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
fukasawa e60969
            {
fukasawa e60969
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
fukasawa e60969
               png_read_push_finish_row(png_ptr);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
fukasawa e60969
            {
fukasawa e60969
               png_push_have_row(png_ptr, NULL);
fukasawa e60969
               png_read_push_finish_row(png_ptr);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            if (png_ptr->pass == 4) /* Pass 3 might be empty */
fukasawa e60969
            {
fukasawa e60969
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
fukasawa e60969
               {
fukasawa e60969
                  png_push_have_row(png_ptr, NULL);
fukasawa e60969
                  png_read_push_finish_row(png_ptr);
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         case 3:
fukasawa e60969
         {
fukasawa e60969
            int i;
fukasawa e60969
fukasawa e60969
            for (i = 0; i < 4 && png_ptr->pass == 3; i++)
fukasawa e60969
            {
fukasawa e60969
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
fukasawa e60969
               png_read_push_finish_row(png_ptr);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            if (png_ptr->pass == 4) /* Skip top two generated rows */
fukasawa e60969
            {
fukasawa e60969
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
fukasawa e60969
               {
fukasawa e60969
                  png_push_have_row(png_ptr, NULL);
fukasawa e60969
                  png_read_push_finish_row(png_ptr);
fukasawa e60969
               }
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         case 4:
fukasawa e60969
         {
fukasawa e60969
            int i;
fukasawa e60969
fukasawa e60969
            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
fukasawa e60969
            {
fukasawa e60969
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
fukasawa e60969
               png_read_push_finish_row(png_ptr);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
fukasawa e60969
            {
fukasawa e60969
               png_push_have_row(png_ptr, NULL);
fukasawa e60969
               png_read_push_finish_row(png_ptr);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            if (png_ptr->pass == 6) /* Pass 5 might be empty */
fukasawa e60969
            {
fukasawa e60969
               png_push_have_row(png_ptr, NULL);
fukasawa e60969
               png_read_push_finish_row(png_ptr);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         case 5:
fukasawa e60969
         {
fukasawa e60969
            int i;
fukasawa e60969
fukasawa e60969
            for (i = 0; i < 2 && png_ptr->pass == 5; i++)
fukasawa e60969
            {
fukasawa e60969
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
fukasawa e60969
               png_read_push_finish_row(png_ptr);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            if (png_ptr->pass == 6) /* Skip top generated row */
fukasawa e60969
            {
fukasawa e60969
               png_push_have_row(png_ptr, NULL);
fukasawa e60969
               png_read_push_finish_row(png_ptr);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         default:
fukasawa e60969
         case 6:
fukasawa e60969
         {
fukasawa e60969
            png_push_have_row(png_ptr, png_ptr->row_buf + 1);
fukasawa e60969
            png_read_push_finish_row(png_ptr);
fukasawa e60969
fukasawa e60969
            if (png_ptr->pass != 6)
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            png_push_have_row(png_ptr, NULL);
fukasawa e60969
            png_read_push_finish_row(png_ptr);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
   else
fukasawa e60969
#endif
fukasawa e60969
   {
fukasawa e60969
      png_push_have_row(png_ptr, png_ptr->row_buf + 1);
fukasawa e60969
      png_read_push_finish_row(png_ptr);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_read_push_finish_row(png_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
#ifdef PNG_READ_INTERLACING_SUPPORTED
fukasawa e60969
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
fukasawa e60969
fukasawa e60969
   /* Start of interlace block */
fukasawa e60969
   static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
fukasawa e60969
fukasawa e60969
   /* Offset to next interlace block */
fukasawa e60969
   static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
fukasawa e60969
fukasawa e60969
   /* Start of interlace block in the y direction */
fukasawa e60969
   static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
fukasawa e60969
fukasawa e60969
   /* Offset to next interlace block in the y direction */
fukasawa e60969
   static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
fukasawa e60969
fukasawa e60969
   /* Height of interlace block.  This is not currently used - if you need
fukasawa e60969
    * it, uncomment it here and in png.h
fukasawa e60969
   static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
fukasawa e60969
   */
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
   png_ptr->row_number++;
fukasawa e60969
   if (png_ptr->row_number < png_ptr->num_rows)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_INTERLACING_SUPPORTED
fukasawa e60969
   if (png_ptr->interlaced != 0)
fukasawa e60969
   {
fukasawa e60969
      png_ptr->row_number = 0;
fukasawa e60969
      memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
fukasawa e60969
fukasawa e60969
      do
fukasawa e60969
      {
fukasawa e60969
         png_ptr->pass++;
fukasawa e60969
         if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
fukasawa e60969
             (png_ptr->pass == 3 && png_ptr->width < 3) ||
fukasawa e60969
             (png_ptr->pass == 5 && png_ptr->width < 2))
fukasawa e60969
            png_ptr->pass++;
fukasawa e60969
fukasawa e60969
         if (png_ptr->pass > 7)
fukasawa e60969
            png_ptr->pass--;
fukasawa e60969
fukasawa e60969
         if (png_ptr->pass >= 7)
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         png_ptr->iwidth = (png_ptr->width +
fukasawa e60969
             png_pass_inc[png_ptr->pass] - 1 -
fukasawa e60969
             png_pass_start[png_ptr->pass]) /
fukasawa e60969
             png_pass_inc[png_ptr->pass];
fukasawa e60969
fukasawa e60969
         if ((png_ptr->transformations & PNG_INTERLACE) != 0)
fukasawa e60969
            break;
fukasawa e60969
fukasawa e60969
         png_ptr->num_rows = (png_ptr->height +
fukasawa e60969
             png_pass_yinc[png_ptr->pass] - 1 -
fukasawa e60969
             png_pass_ystart[png_ptr->pass]) /
fukasawa e60969
             png_pass_yinc[png_ptr->pass];
fukasawa e60969
fukasawa e60969
      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
fukasawa e60969
   }
fukasawa e60969
#endif /* READ_INTERLACING */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr->info_fn != NULL)
fukasawa e60969
      (*(png_ptr->info_fn))(png_ptr, info_ptr);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr->end_fn != NULL)
fukasawa e60969
      (*(png_ptr->end_fn))(png_ptr, info_ptr);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
void /* PRIVATE */
fukasawa e60969
png_push_have_row(png_structrp png_ptr, png_bytep row)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr->row_fn != NULL)
fukasawa e60969
      (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
fukasawa e60969
         (int)png_ptr->pass);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_INTERLACING_SUPPORTED
fukasawa e60969
void PNGAPI
fukasawa e60969
png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
fukasawa e60969
    png_const_bytep new_row)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   /* new_row is a flag here - if it is NULL then the app callback was called
fukasawa e60969
    * from an empty row (see the calls to png_struct::row_fn below), otherwise
fukasawa e60969
    * it must be png_ptr->row_buf+1
fukasawa e60969
    */
fukasawa e60969
   if (new_row != NULL)
fukasawa e60969
      png_combine_row(png_ptr, old_row, 1/*blocky display*/);
fukasawa e60969
}
fukasawa e60969
#endif /* READ_INTERLACING */
fukasawa e60969
fukasawa e60969
void PNGAPI
fukasawa e60969
png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
fukasawa e60969
    png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
fukasawa e60969
    png_progressive_end_ptr end_fn)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return;
fukasawa e60969
fukasawa e60969
   png_ptr->info_fn = info_fn;
fukasawa e60969
   png_ptr->row_fn = row_fn;
fukasawa e60969
   png_ptr->end_fn = end_fn;
fukasawa e60969
fukasawa e60969
   png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
png_voidp PNGAPI
fukasawa e60969
png_get_progressive_ptr(png_const_structrp png_ptr)
fukasawa e60969
{
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return (NULL);
fukasawa e60969
fukasawa e60969
   return png_ptr->io_ptr;
fukasawa e60969
}
fukasawa e60969
#endif /* PROGRESSIVE_READ */