Home | History | Annotate | Download | only in libpng16
      1 
      2 /* pngpread.c - read a png file in push mode
      3  *
      4  * Last changed in libpng 1.6.18 [July 23, 2015]
      5  * Copyright (c) 1998-2002,2004,2006-2015 Glenn Randers-Pehrson
      6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
      7  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
      8  *
      9  * This code is released under the libpng license.
     10  * For conditions of distribution and use, see the disclaimer
     11  * and license in png.h
     12  */
     13 
     14 #include "pngpriv.h"
     15 
     16 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
     17 
     18 /* Push model modes */
     19 #define PNG_READ_SIG_MODE   0
     20 #define PNG_READ_CHUNK_MODE 1
     21 #define PNG_READ_IDAT_MODE  2
     22 #define PNG_READ_tEXt_MODE  4
     23 #define PNG_READ_zTXt_MODE  5
     24 #define PNG_READ_DONE_MODE  6
     25 #define PNG_READ_iTXt_MODE  7
     26 #define PNG_ERROR_MODE      8
     27 
     28 #define PNG_PUSH_SAVE_BUFFER_IF_FULL \
     29 if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
     30    { png_push_save_buffer(png_ptr); return; }
     31 #define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
     32 if (png_ptr->buffer_size < N) \
     33    { png_push_save_buffer(png_ptr); return; }
     34 
     35 void PNGAPI
     36 png_process_data(png_structrp png_ptr, png_inforp info_ptr,
     37     png_bytep buffer, png_size_t buffer_size)
     38 {
     39    if (png_ptr == NULL || info_ptr == NULL)
     40       return;
     41 
     42    png_push_restore_buffer(png_ptr, buffer, buffer_size);
     43 
     44    while (png_ptr->buffer_size)
     45    {
     46       png_process_some_data(png_ptr, info_ptr);
     47    }
     48 }
     49 
     50 png_size_t PNGAPI
     51 png_process_data_pause(png_structrp png_ptr, int save)
     52 {
     53    if (png_ptr != NULL)
     54    {
     55       /* It's easiest for the caller if we do the save; then the caller doesn't
     56        * have to supply the same data again:
     57        */
     58       if (save != 0)
     59          png_push_save_buffer(png_ptr);
     60       else
     61       {
     62          /* This includes any pending saved bytes: */
     63          png_size_t remaining = png_ptr->buffer_size;
     64          png_ptr->buffer_size = 0;
     65 
     66          /* So subtract the saved buffer size, unless all the data
     67           * is actually 'saved', in which case we just return 0
     68           */
     69          if (png_ptr->save_buffer_size < remaining)
     70             return remaining - png_ptr->save_buffer_size;
     71       }
     72    }
     73 
     74    return 0;
     75 }
     76 
     77 png_uint_32 PNGAPI
     78 png_process_data_skip(png_structrp png_ptr)
     79 {
     80   /* TODO: Deprecate and remove this API.
     81    * Somewhere the implementation of this seems to have been lost,
     82    * or abandoned.  It was only to support some internal back-door access
     83    * to png_struct) in libpng-1.4.x.
     84    */
     85    png_app_warning(png_ptr,
     86 "png_process_data_skip is not implemented in any current version of libpng");
     87    return 0;
     88 }
     89 
     90 /* What we do with the incoming data depends on what we were previously
     91  * doing before we ran out of data...
     92  */
     93 void /* PRIVATE */
     94 png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
     95 {
     96    if (png_ptr == NULL)
     97       return;
     98 
     99    switch (png_ptr->process_mode)
    100    {
    101       case PNG_READ_SIG_MODE:
    102       {
    103          png_push_read_sig(png_ptr, info_ptr);
    104          break;
    105       }
    106 
    107       case PNG_READ_CHUNK_MODE:
    108       {
    109          png_push_read_chunk(png_ptr, info_ptr);
    110          break;
    111       }
    112 
    113       case PNG_READ_IDAT_MODE:
    114       {
    115          png_push_read_IDAT(png_ptr);
    116          break;
    117       }
    118 
    119       default:
    120       {
    121          png_ptr->buffer_size = 0;
    122          break;
    123       }
    124    }
    125 }
    126 
    127 /* Read any remaining signature bytes from the stream and compare them with
    128  * the correct PNG signature.  It is possible that this routine is called
    129  * with bytes already read from the signature, either because they have been
    130  * checked by the calling application, or because of multiple calls to this
    131  * routine.
    132  */
    133 void /* PRIVATE */
    134 png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
    135 {
    136    png_size_t num_checked = png_ptr->sig_bytes, /* SAFE, does not exceed 8 */
    137        num_to_check = 8 - num_checked;
    138 
    139    if (png_ptr->buffer_size < num_to_check)
    140    {
    141       num_to_check = png_ptr->buffer_size;
    142    }
    143 
    144    png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
    145        num_to_check);
    146    png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
    147 
    148    if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
    149    {
    150       if (num_checked < 4 &&
    151           png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
    152          png_error(png_ptr, "Not a PNG file");
    153 
    154       else
    155          png_error(png_ptr, "PNG file corrupted by ASCII conversion");
    156    }
    157    else
    158    {
    159       if (png_ptr->sig_bytes >= 8)
    160       {
    161          png_ptr->process_mode = PNG_READ_CHUNK_MODE;
    162       }
    163    }
    164 }
    165 
    166 void /* PRIVATE */
    167 png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
    168 {
    169    png_uint_32 chunk_name;
    170 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
    171    int keep; /* unknown handling method */
    172 #endif
    173 
    174    /* First we make sure we have enough data for the 4-byte chunk name
    175     * and the 4-byte chunk length before proceeding with decoding the
    176     * chunk data.  To fully decode each of these chunks, we also make
    177     * sure we have enough data in the buffer for the 4-byte CRC at the
    178     * end of every chunk (except IDAT, which is handled separately).
    179     */
    180    if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
    181    {
    182       png_byte chunk_length[4];
    183       png_byte chunk_tag[4];
    184 
    185       PNG_PUSH_SAVE_BUFFER_IF_LT(8)
    186       png_push_fill_buffer(png_ptr, chunk_length, 4);
    187       png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
    188       png_reset_crc(png_ptr);
    189       png_crc_read(png_ptr, chunk_tag, 4);
    190       png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
    191       png_check_chunk_name(png_ptr, png_ptr->chunk_name);
    192       png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
    193    }
    194 
    195    chunk_name = png_ptr->chunk_name;
    196 
    197    if (chunk_name == png_IDAT)
    198    {
    199       if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
    200          png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
    201 
    202       /* If we reach an IDAT chunk, this means we have read all of the
    203        * header chunks, and we can start reading the image (or if this
    204        * is called after the image has been read - we have an error).
    205        */
    206       if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
    207          png_error(png_ptr, "Missing IHDR before IDAT");
    208 
    209       else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
    210           (png_ptr->mode & PNG_HAVE_PLTE) == 0)
    211          png_error(png_ptr, "Missing PLTE before IDAT");
    212 
    213       png_ptr->process_mode = PNG_READ_IDAT_MODE;
    214 
    215       if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
    216          if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
    217             if (png_ptr->push_length == 0)
    218                return;
    219 
    220       png_ptr->mode |= PNG_HAVE_IDAT;
    221 
    222       if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
    223          png_benign_error(png_ptr, "Too many IDATs found");
    224    }
    225 
    226    if (chunk_name == png_IHDR)
    227    {
    228       if (png_ptr->push_length != 13)
    229          png_error(png_ptr, "Invalid IHDR length");
    230 
    231       PNG_PUSH_SAVE_BUFFER_IF_FULL
    232       png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
    233    }
    234 
    235    else if (chunk_name == png_IEND)
    236    {
    237       PNG_PUSH_SAVE_BUFFER_IF_FULL
    238       png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
    239 
    240       png_ptr->process_mode = PNG_READ_DONE_MODE;
    241       png_push_have_end(png_ptr, info_ptr);
    242    }
    243 
    244 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
    245    else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
    246    {
    247       PNG_PUSH_SAVE_BUFFER_IF_FULL
    248       png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
    249 
    250       if (chunk_name == png_PLTE)
    251          png_ptr->mode |= PNG_HAVE_PLTE;
    252    }
    253 #endif
    254 
    255    else if (chunk_name == png_PLTE)
    256    {
    257       PNG_PUSH_SAVE_BUFFER_IF_FULL
    258       png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
    259    }
    260 
    261    else if (chunk_name == png_IDAT)
    262    {
    263       png_ptr->idat_size = png_ptr->push_length;
    264       png_ptr->process_mode = PNG_READ_IDAT_MODE;
    265       png_push_have_info(png_ptr, info_ptr);
    266       png_ptr->zstream.avail_out =
    267           (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
    268           png_ptr->iwidth) + 1;
    269       png_ptr->zstream.next_out = png_ptr->row_buf;
    270       return;
    271    }
    272 
    273 #ifdef PNG_READ_gAMA_SUPPORTED
    274    else if (png_ptr->chunk_name == png_gAMA)
    275    {
    276       PNG_PUSH_SAVE_BUFFER_IF_FULL
    277       png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
    278    }
    279 
    280 #endif
    281 #ifdef PNG_READ_sBIT_SUPPORTED
    282    else if (png_ptr->chunk_name == png_sBIT)
    283    {
    284       PNG_PUSH_SAVE_BUFFER_IF_FULL
    285       png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
    286    }
    287 
    288 #endif
    289 #ifdef PNG_READ_cHRM_SUPPORTED
    290    else if (png_ptr->chunk_name == png_cHRM)
    291    {
    292       PNG_PUSH_SAVE_BUFFER_IF_FULL
    293       png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
    294    }
    295 
    296 #endif
    297 #ifdef PNG_READ_sRGB_SUPPORTED
    298    else if (chunk_name == png_sRGB)
    299    {
    300       PNG_PUSH_SAVE_BUFFER_IF_FULL
    301       png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
    302    }
    303 
    304 #endif
    305 #ifdef PNG_READ_iCCP_SUPPORTED
    306    else if (png_ptr->chunk_name == png_iCCP)
    307    {
    308       PNG_PUSH_SAVE_BUFFER_IF_FULL
    309       png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
    310    }
    311 
    312 #endif
    313 #ifdef PNG_READ_sPLT_SUPPORTED
    314    else if (chunk_name == png_sPLT)
    315    {
    316       PNG_PUSH_SAVE_BUFFER_IF_FULL
    317       png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
    318    }
    319 
    320 #endif
    321 #ifdef PNG_READ_tRNS_SUPPORTED
    322    else if (chunk_name == png_tRNS)
    323    {
    324       PNG_PUSH_SAVE_BUFFER_IF_FULL
    325       png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
    326    }
    327 
    328 #endif
    329 #ifdef PNG_READ_bKGD_SUPPORTED
    330    else if (chunk_name == png_bKGD)
    331    {
    332       PNG_PUSH_SAVE_BUFFER_IF_FULL
    333       png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
    334    }
    335 
    336 #endif
    337 #ifdef PNG_READ_hIST_SUPPORTED
    338    else if (chunk_name == png_hIST)
    339    {
    340       PNG_PUSH_SAVE_BUFFER_IF_FULL
    341       png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
    342    }
    343 
    344 #endif
    345 #ifdef PNG_READ_pHYs_SUPPORTED
    346    else if (chunk_name == png_pHYs)
    347    {
    348       PNG_PUSH_SAVE_BUFFER_IF_FULL
    349       png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
    350    }
    351 
    352 #endif
    353 #ifdef PNG_READ_oFFs_SUPPORTED
    354    else if (chunk_name == png_oFFs)
    355    {
    356       PNG_PUSH_SAVE_BUFFER_IF_FULL
    357       png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
    358    }
    359 #endif
    360 
    361 #ifdef PNG_READ_pCAL_SUPPORTED
    362    else if (chunk_name == png_pCAL)
    363    {
    364       PNG_PUSH_SAVE_BUFFER_IF_FULL
    365       png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
    366    }
    367 
    368 #endif
    369 #ifdef PNG_READ_sCAL_SUPPORTED
    370    else if (chunk_name == png_sCAL)
    371    {
    372       PNG_PUSH_SAVE_BUFFER_IF_FULL
    373       png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
    374    }
    375 
    376 #endif
    377 #ifdef PNG_READ_tIME_SUPPORTED
    378    else if (chunk_name == png_tIME)
    379    {
    380       PNG_PUSH_SAVE_BUFFER_IF_FULL
    381       png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
    382    }
    383 
    384 #endif
    385 #ifdef PNG_READ_tEXt_SUPPORTED
    386    else if (chunk_name == png_tEXt)
    387    {
    388       PNG_PUSH_SAVE_BUFFER_IF_FULL
    389       png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
    390    }
    391 
    392 #endif
    393 #ifdef PNG_READ_zTXt_SUPPORTED
    394    else if (chunk_name == png_zTXt)
    395    {
    396       PNG_PUSH_SAVE_BUFFER_IF_FULL
    397       png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
    398    }
    399 
    400 #endif
    401 #ifdef PNG_READ_iTXt_SUPPORTED
    402    else if (chunk_name == png_iTXt)
    403    {
    404       PNG_PUSH_SAVE_BUFFER_IF_FULL
    405       png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
    406    }
    407 #endif
    408 
    409    else
    410    {
    411       PNG_PUSH_SAVE_BUFFER_IF_FULL
    412       png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
    413          PNG_HANDLE_CHUNK_AS_DEFAULT);
    414    }
    415 
    416    png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
    417 }
    418 
    419 void PNGCBAPI
    420 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
    421 {
    422    png_bytep ptr;
    423 
    424    if (png_ptr == NULL)
    425       return;
    426 
    427    ptr = buffer;
    428    if (png_ptr->save_buffer_size != 0)
    429    {
    430       png_size_t save_size;
    431 
    432       if (length < png_ptr->save_buffer_size)
    433          save_size = length;
    434 
    435       else
    436          save_size = png_ptr->save_buffer_size;
    437 
    438       memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
    439       length -= save_size;
    440       ptr += save_size;
    441       png_ptr->buffer_size -= save_size;
    442       png_ptr->save_buffer_size -= save_size;
    443       png_ptr->save_buffer_ptr += save_size;
    444    }
    445    if (length != 0 && png_ptr->current_buffer_size != 0)
    446    {
    447       png_size_t save_size;
    448 
    449       if (length < png_ptr->current_buffer_size)
    450          save_size = length;
    451 
    452       else
    453          save_size = png_ptr->current_buffer_size;
    454 
    455       memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
    456       png_ptr->buffer_size -= save_size;
    457       png_ptr->current_buffer_size -= save_size;
    458       png_ptr->current_buffer_ptr += save_size;
    459    }
    460 }
    461 
    462 void /* PRIVATE */
    463 png_push_save_buffer(png_structrp png_ptr)
    464 {
    465    if (png_ptr->save_buffer_size != 0)
    466    {
    467       if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
    468       {
    469          png_size_t i, istop;
    470          png_bytep sp;
    471          png_bytep dp;
    472 
    473          istop = png_ptr->save_buffer_size;
    474          for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
    475              i < istop; i++, sp++, dp++)
    476          {
    477             *dp = *sp;
    478          }
    479       }
    480    }
    481    if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
    482        png_ptr->save_buffer_max)
    483    {
    484       png_size_t new_max;
    485       png_bytep old_buffer;
    486 
    487       if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
    488           (png_ptr->current_buffer_size + 256))
    489       {
    490          png_error(png_ptr, "Potential overflow of save_buffer");
    491       }
    492 
    493       new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
    494       old_buffer = png_ptr->save_buffer;
    495       png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
    496           (png_size_t)new_max);
    497 
    498       if (png_ptr->save_buffer == NULL)
    499       {
    500          png_free(png_ptr, old_buffer);
    501          png_error(png_ptr, "Insufficient memory for save_buffer");
    502       }
    503 
    504       memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
    505       png_free(png_ptr, old_buffer);
    506       png_ptr->save_buffer_max = new_max;
    507    }
    508    if (png_ptr->current_buffer_size)
    509    {
    510       memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
    511          png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
    512       png_ptr->save_buffer_size += png_ptr->current_buffer_size;
    513       png_ptr->current_buffer_size = 0;
    514    }
    515    png_ptr->save_buffer_ptr = png_ptr->save_buffer;
    516    png_ptr->buffer_size = 0;
    517 }
    518 
    519 void /* PRIVATE */
    520 png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
    521    png_size_t buffer_length)
    522 {
    523    png_ptr->current_buffer = buffer;
    524    png_ptr->current_buffer_size = buffer_length;
    525    png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
    526    png_ptr->current_buffer_ptr = png_ptr->current_buffer;
    527 }
    528 
    529 void /* PRIVATE */
    530 png_push_read_IDAT(png_structrp png_ptr)
    531 {
    532    if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
    533    {
    534       png_byte chunk_length[4];
    535       png_byte chunk_tag[4];
    536 
    537       /* TODO: this code can be commoned up with the same code in push_read */
    538       PNG_PUSH_SAVE_BUFFER_IF_LT(8)
    539       png_push_fill_buffer(png_ptr, chunk_length, 4);
    540       png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
    541       png_reset_crc(png_ptr);
    542       png_crc_read(png_ptr, chunk_tag, 4);
    543       png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
    544       png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
    545 
    546       if (png_ptr->chunk_name != png_IDAT)
    547       {
    548          png_ptr->process_mode = PNG_READ_CHUNK_MODE;
    549 
    550          if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
    551             png_error(png_ptr, "Not enough compressed data");
    552 
    553          return;
    554       }
    555 
    556       png_ptr->idat_size = png_ptr->push_length;
    557    }
    558 
    559    if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
    560    {
    561       png_size_t save_size = png_ptr->save_buffer_size;
    562       png_uint_32 idat_size = png_ptr->idat_size;
    563 
    564       /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
    565        * are of different types and we don't know which variable has the fewest
    566        * bits.  Carefully select the smaller and cast it to the type of the
    567        * larger - this cannot overflow.  Do not cast in the following test - it
    568        * will break on either 16-bit or 64-bit platforms.
    569        */
    570       if (idat_size < save_size)
    571          save_size = (png_size_t)idat_size;
    572 
    573       else
    574          idat_size = (png_uint_32)save_size;
    575 
    576       png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
    577 
    578       png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
    579 
    580       png_ptr->idat_size -= idat_size;
    581       png_ptr->buffer_size -= save_size;
    582       png_ptr->save_buffer_size -= save_size;
    583       png_ptr->save_buffer_ptr += save_size;
    584    }
    585 
    586    if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
    587    {
    588       png_size_t save_size = png_ptr->current_buffer_size;
    589       png_uint_32 idat_size = png_ptr->idat_size;
    590 
    591       /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
    592        * are of different types and we don't know which variable has the fewest
    593        * bits.  Carefully select the smaller and cast it to the type of the
    594        * larger - this cannot overflow.
    595        */
    596       if (idat_size < save_size)
    597          save_size = (png_size_t)idat_size;
    598 
    599       else
    600          idat_size = (png_uint_32)save_size;
    601 
    602       png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
    603 
    604       png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
    605 
    606       png_ptr->idat_size -= idat_size;
    607       png_ptr->buffer_size -= save_size;
    608       png_ptr->current_buffer_size -= save_size;
    609       png_ptr->current_buffer_ptr += save_size;
    610    }
    611 
    612    if (png_ptr->idat_size == 0)
    613    {
    614       PNG_PUSH_SAVE_BUFFER_IF_LT(4)
    615       png_crc_finish(png_ptr, 0);
    616       png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
    617       png_ptr->mode |= PNG_AFTER_IDAT;
    618       png_ptr->zowner = 0;
    619    }
    620 }
    621 
    622 void /* PRIVATE */
    623 png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
    624    png_size_t buffer_length)
    625 {
    626    /* The caller checks for a non-zero buffer length. */
    627    if (!(buffer_length > 0) || buffer == NULL)
    628       png_error(png_ptr, "No IDAT data (internal error)");
    629 
    630    /* This routine must process all the data it has been given
    631     * before returning, calling the row callback as required to
    632     * handle the uncompressed results.
    633     */
    634    png_ptr->zstream.next_in = buffer;
    635    /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
    636    png_ptr->zstream.avail_in = (uInt)buffer_length;
    637 
    638    /* Keep going until the decompressed data is all processed
    639     * or the stream marked as finished.
    640     */
    641    while (png_ptr->zstream.avail_in > 0 &&
    642       (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
    643    {
    644       int ret;
    645 
    646       /* We have data for zlib, but we must check that zlib
    647        * has someplace to put the results.  It doesn't matter
    648        * if we don't expect any results -- it may be the input
    649        * data is just the LZ end code.
    650        */
    651       if (!(png_ptr->zstream.avail_out > 0))
    652       {
    653          /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
    654          png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
    655              png_ptr->iwidth) + 1);
    656 
    657          png_ptr->zstream.next_out = png_ptr->row_buf;
    658       }
    659 
    660       /* Using Z_SYNC_FLUSH here means that an unterminated
    661        * LZ stream (a stream with a missing end code) can still
    662        * be handled, otherwise (Z_NO_FLUSH) a future zlib
    663        * implementation might defer output and therefore
    664        * change the current behavior (see comments in inflate.c
    665        * for why this doesn't happen at present with zlib 1.2.5).
    666        */
    667       ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH);
    668 
    669       /* Check for any failure before proceeding. */
    670       if (ret != Z_OK && ret != Z_STREAM_END)
    671       {
    672          /* Terminate the decompression. */
    673          png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
    674          png_ptr->zowner = 0;
    675 
    676          /* This may be a truncated stream (missing or
    677           * damaged end code).  Treat that as a warning.
    678           */
    679          if (png_ptr->row_number >= png_ptr->num_rows ||
    680              png_ptr->pass > 6)
    681             png_warning(png_ptr, "Truncated compressed data in IDAT");
    682 
    683          else
    684             png_error(png_ptr, "Decompression error in IDAT");
    685 
    686          /* Skip the check on unprocessed input */
    687          return;
    688       }
    689 
    690       /* Did inflate output any data? */
    691       if (png_ptr->zstream.next_out != png_ptr->row_buf)
    692       {
    693          /* Is this unexpected data after the last row?
    694           * If it is, artificially terminate the LZ output
    695           * here.
    696           */
    697          if (png_ptr->row_number >= png_ptr->num_rows ||
    698              png_ptr->pass > 6)
    699          {
    700             /* Extra data. */
    701             png_warning(png_ptr, "Extra compressed data in IDAT");
    702             png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
    703             png_ptr->zowner = 0;
    704 
    705             /* Do no more processing; skip the unprocessed
    706              * input check below.
    707              */
    708             return;
    709          }
    710 
    711          /* Do we have a complete row? */
    712          if (png_ptr->zstream.avail_out == 0)
    713             png_push_process_row(png_ptr);
    714       }
    715 
    716       /* And check for the end of the stream. */
    717       if (ret == Z_STREAM_END)
    718          png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
    719    }
    720 
    721    /* All the data should have been processed, if anything
    722     * is left at this point we have bytes of IDAT data
    723     * after the zlib end code.
    724     */
    725    if (png_ptr->zstream.avail_in > 0)
    726       png_warning(png_ptr, "Extra compression data in IDAT");
    727 }
    728 
    729 void /* PRIVATE */
    730 png_push_process_row(png_structrp png_ptr)
    731 {
    732    /* 1.5.6: row_info moved out of png_struct to a local here. */
    733    png_row_info row_info;
    734 
    735    row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
    736    row_info.color_type = png_ptr->color_type;
    737    row_info.bit_depth = png_ptr->bit_depth;
    738    row_info.channels = png_ptr->channels;
    739    row_info.pixel_depth = png_ptr->pixel_depth;
    740    row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
    741 
    742    if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
    743    {
    744       if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
    745          png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
    746             png_ptr->prev_row + 1, png_ptr->row_buf[0]);
    747       else
    748          png_error(png_ptr, "bad adaptive filter value");
    749    }
    750 
    751    /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
    752     * 1.5.6, while the buffer really is this big in current versions of libpng
    753     * it may not be in the future, so this was changed just to copy the
    754     * interlaced row count:
    755     */
    756    memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
    757 
    758 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
    759    if (png_ptr->transformations != 0)
    760       png_do_read_transformations(png_ptr, &row_info);
    761 #endif
    762 
    763    /* The transformed pixel depth should match the depth now in row_info. */
    764    if (png_ptr->transformed_pixel_depth == 0)
    765    {
    766       png_ptr->transformed_pixel_depth = row_info.pixel_depth;
    767       if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
    768          png_error(png_ptr, "progressive row overflow");
    769    }
    770 
    771    else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
    772       png_error(png_ptr, "internal progressive row size calculation error");
    773 
    774 
    775 #ifdef PNG_READ_INTERLACING_SUPPORTED
    776    /* Expand interlaced rows to full size */
    777    if (png_ptr->interlaced != 0 &&
    778        (png_ptr->transformations & PNG_INTERLACE) != 0)
    779    {
    780       if (png_ptr->pass < 6)
    781          png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
    782             png_ptr->transformations);
    783 
    784       switch (png_ptr->pass)
    785       {
    786          case 0:
    787          {
    788             int i;
    789             for (i = 0; i < 8 && png_ptr->pass == 0; i++)
    790             {
    791                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
    792                png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
    793             }
    794 
    795             if (png_ptr->pass == 2) /* Pass 1 might be empty */
    796             {
    797                for (i = 0; i < 4 && png_ptr->pass == 2; i++)
    798                {
    799                   png_push_have_row(png_ptr, NULL);
    800                   png_read_push_finish_row(png_ptr);
    801                }
    802             }
    803 
    804             if (png_ptr->pass == 4 && png_ptr->height <= 4)
    805             {
    806                for (i = 0; i < 2 && png_ptr->pass == 4; i++)
    807                {
    808                   png_push_have_row(png_ptr, NULL);
    809                   png_read_push_finish_row(png_ptr);
    810                }
    811             }
    812 
    813             if (png_ptr->pass == 6 && png_ptr->height <= 4)
    814             {
    815                 png_push_have_row(png_ptr, NULL);
    816                 png_read_push_finish_row(png_ptr);
    817             }
    818 
    819             break;
    820          }
    821 
    822          case 1:
    823          {
    824             int i;
    825             for (i = 0; i < 8 && png_ptr->pass == 1; i++)
    826             {
    827                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
    828                png_read_push_finish_row(png_ptr);
    829             }
    830 
    831             if (png_ptr->pass == 2) /* Skip top 4 generated rows */
    832             {
    833                for (i = 0; i < 4 && png_ptr->pass == 2; i++)
    834                {
    835                   png_push_have_row(png_ptr, NULL);
    836                   png_read_push_finish_row(png_ptr);
    837                }
    838             }
    839 
    840             break;
    841          }
    842 
    843          case 2:
    844          {
    845             int i;
    846 
    847             for (i = 0; i < 4 && png_ptr->pass == 2; i++)
    848             {
    849                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
    850                png_read_push_finish_row(png_ptr);
    851             }
    852 
    853             for (i = 0; i < 4 && png_ptr->pass == 2; i++)
    854             {
    855                png_push_have_row(png_ptr, NULL);
    856                png_read_push_finish_row(png_ptr);
    857             }
    858 
    859             if (png_ptr->pass == 4) /* Pass 3 might be empty */
    860             {
    861                for (i = 0; i < 2 && png_ptr->pass == 4; i++)
    862                {
    863                   png_push_have_row(png_ptr, NULL);
    864                   png_read_push_finish_row(png_ptr);
    865                }
    866             }
    867 
    868             break;
    869          }
    870 
    871          case 3:
    872          {
    873             int i;
    874 
    875             for (i = 0; i < 4 && png_ptr->pass == 3; i++)
    876             {
    877                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
    878                png_read_push_finish_row(png_ptr);
    879             }
    880 
    881             if (png_ptr->pass == 4) /* Skip top two generated rows */
    882             {
    883                for (i = 0; i < 2 && png_ptr->pass == 4; i++)
    884                {
    885                   png_push_have_row(png_ptr, NULL);
    886                   png_read_push_finish_row(png_ptr);
    887                }
    888             }
    889 
    890             break;
    891          }
    892 
    893          case 4:
    894          {
    895             int i;
    896 
    897             for (i = 0; i < 2 && png_ptr->pass == 4; i++)
    898             {
    899                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
    900                png_read_push_finish_row(png_ptr);
    901             }
    902 
    903             for (i = 0; i < 2 && png_ptr->pass == 4; i++)
    904             {
    905                png_push_have_row(png_ptr, NULL);
    906                png_read_push_finish_row(png_ptr);
    907             }
    908 
    909             if (png_ptr->pass == 6) /* Pass 5 might be empty */
    910             {
    911                png_push_have_row(png_ptr, NULL);
    912                png_read_push_finish_row(png_ptr);
    913             }
    914 
    915             break;
    916          }
    917 
    918          case 5:
    919          {
    920             int i;
    921 
    922             for (i = 0; i < 2 && png_ptr->pass == 5; i++)
    923             {
    924                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
    925                png_read_push_finish_row(png_ptr);
    926             }
    927 
    928             if (png_ptr->pass == 6) /* Skip top generated row */
    929             {
    930                png_push_have_row(png_ptr, NULL);
    931                png_read_push_finish_row(png_ptr);
    932             }
    933 
    934             break;
    935          }
    936 
    937          default:
    938          case 6:
    939          {
    940             png_push_have_row(png_ptr, png_ptr->row_buf + 1);
    941             png_read_push_finish_row(png_ptr);
    942 
    943             if (png_ptr->pass != 6)
    944                break;
    945 
    946             png_push_have_row(png_ptr, NULL);
    947             png_read_push_finish_row(png_ptr);
    948          }
    949       }
    950    }
    951    else
    952 #endif
    953    {
    954       png_push_have_row(png_ptr, png_ptr->row_buf + 1);
    955       png_read_push_finish_row(png_ptr);
    956    }
    957 }
    958 
    959 void /* PRIVATE */
    960 png_read_push_finish_row(png_structrp png_ptr)
    961 {
    962 #ifdef PNG_READ_INTERLACING_SUPPORTED
    963    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
    964 
    965    /* Start of interlace block */
    966    static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
    967 
    968    /* Offset to next interlace block */
    969    static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
    970 
    971    /* Start of interlace block in the y direction */
    972    static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
    973 
    974    /* Offset to next interlace block in the y direction */
    975    static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
    976 
    977    /* Height of interlace block.  This is not currently used - if you need
    978     * it, uncomment it here and in png.h
    979    static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
    980    */
    981 #endif
    982 
    983    png_ptr->row_number++;
    984    if (png_ptr->row_number < png_ptr->num_rows)
    985       return;
    986 
    987 #ifdef PNG_READ_INTERLACING_SUPPORTED
    988    if (png_ptr->interlaced != 0)
    989    {
    990       png_ptr->row_number = 0;
    991       memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
    992 
    993       do
    994       {
    995          png_ptr->pass++;
    996          if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
    997              (png_ptr->pass == 3 && png_ptr->width < 3) ||
    998              (png_ptr->pass == 5 && png_ptr->width < 2))
    999             png_ptr->pass++;
   1000 
   1001          if (png_ptr->pass > 7)
   1002             png_ptr->pass--;
   1003 
   1004          if (png_ptr->pass >= 7)
   1005             break;
   1006 
   1007          png_ptr->iwidth = (png_ptr->width +
   1008              png_pass_inc[png_ptr->pass] - 1 -
   1009              png_pass_start[png_ptr->pass]) /
   1010              png_pass_inc[png_ptr->pass];
   1011 
   1012          if ((png_ptr->transformations & PNG_INTERLACE) != 0)
   1013             break;
   1014 
   1015          png_ptr->num_rows = (png_ptr->height +
   1016              png_pass_yinc[png_ptr->pass] - 1 -
   1017              png_pass_ystart[png_ptr->pass]) /
   1018              png_pass_yinc[png_ptr->pass];
   1019 
   1020       } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
   1021    }
   1022 #endif /* READ_INTERLACING */
   1023 }
   1024 
   1025 void /* PRIVATE */
   1026 png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
   1027 {
   1028    if (png_ptr->info_fn != NULL)
   1029       (*(png_ptr->info_fn))(png_ptr, info_ptr);
   1030 }
   1031 
   1032 void /* PRIVATE */
   1033 png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
   1034 {
   1035    if (png_ptr->end_fn != NULL)
   1036       (*(png_ptr->end_fn))(png_ptr, info_ptr);
   1037 }
   1038 
   1039 void /* PRIVATE */
   1040 png_push_have_row(png_structrp png_ptr, png_bytep row)
   1041 {
   1042    if (png_ptr->row_fn != NULL)
   1043       (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
   1044          (int)png_ptr->pass);
   1045 }
   1046 
   1047 #ifdef PNG_READ_INTERLACING_SUPPORTED
   1048 void PNGAPI
   1049 png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
   1050     png_const_bytep new_row)
   1051 {
   1052    if (png_ptr == NULL)
   1053       return;
   1054 
   1055    /* new_row is a flag here - if it is NULL then the app callback was called
   1056     * from an empty row (see the calls to png_struct::row_fn below), otherwise
   1057     * it must be png_ptr->row_buf+1
   1058     */
   1059    if (new_row != NULL)
   1060       png_combine_row(png_ptr, old_row, 1/*blocky display*/);
   1061 }
   1062 #endif /* READ_INTERLACING */
   1063 
   1064 void PNGAPI
   1065 png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
   1066     png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
   1067     png_progressive_end_ptr end_fn)
   1068 {
   1069    if (png_ptr == NULL)
   1070       return;
   1071 
   1072    png_ptr->info_fn = info_fn;
   1073    png_ptr->row_fn = row_fn;
   1074    png_ptr->end_fn = end_fn;
   1075 
   1076    png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
   1077 }
   1078 
   1079 png_voidp PNGAPI
   1080 png_get_progressive_ptr(png_const_structrp png_ptr)
   1081 {
   1082    if (png_ptr == NULL)
   1083       return (NULL);
   1084 
   1085    return png_ptr->io_ptr;
   1086 }
   1087 #endif /* PROGRESSIVE_READ */
   1088