Back to index

plt-scheme  4.2.1
pngpread.c
Go to the documentation of this file.
00001 
00002 /* pngpread.c - read a png file in push mode
00003  *
00004  * libpng 1.2.5 - October 3, 2002
00005  * For conditions of distribution and use, see copyright notice in png.h
00006  * Copyright (c) 1998-2002 Glenn Randers-Pehrson
00007  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
00008  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
00009  */
00010 
00011 #define PNG_INTERNAL
00012 #include "png.h"
00013 
00014 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
00015 
00016 /* push model modes */
00017 #define PNG_READ_SIG_MODE   0
00018 #define PNG_READ_CHUNK_MODE 1
00019 #define PNG_READ_IDAT_MODE  2
00020 #define PNG_SKIP_MODE       3
00021 #define PNG_READ_tEXt_MODE  4
00022 #define PNG_READ_zTXt_MODE  5
00023 #define PNG_READ_DONE_MODE  6
00024 #define PNG_READ_iTXt_MODE  7
00025 #define PNG_ERROR_MODE      8
00026 
00027 void PNGAPI
00028 png_process_data(png_structp png_ptr, png_infop info_ptr,
00029    png_bytep buffer, png_size_t buffer_size)
00030 {
00031    png_push_restore_buffer(png_ptr, buffer, buffer_size);
00032 
00033    while (png_ptr->buffer_size)
00034    {
00035       png_process_some_data(png_ptr, info_ptr);
00036    }
00037 }
00038 
00039 /* What we do with the incoming data depends on what we were previously
00040  * doing before we ran out of data...
00041  */
00042 void /* PRIVATE */
00043 png_process_some_data(png_structp png_ptr, png_infop info_ptr)
00044 {
00045    switch (png_ptr->process_mode)
00046    {
00047       case PNG_READ_SIG_MODE:
00048       {
00049          png_push_read_sig(png_ptr, info_ptr);
00050          break;
00051       }
00052       case PNG_READ_CHUNK_MODE:
00053       {
00054          png_push_read_chunk(png_ptr, info_ptr);
00055          break;
00056       }
00057       case PNG_READ_IDAT_MODE:
00058       {
00059          png_push_read_IDAT(png_ptr);
00060          break;
00061       }
00062 #if defined(PNG_READ_tEXt_SUPPORTED)
00063       case PNG_READ_tEXt_MODE:
00064       {
00065          png_push_read_tEXt(png_ptr, info_ptr);
00066          break;
00067       }
00068 #endif
00069 #if defined(PNG_READ_zTXt_SUPPORTED)
00070       case PNG_READ_zTXt_MODE:
00071       {
00072          png_push_read_zTXt(png_ptr, info_ptr);
00073          break;
00074       }
00075 #endif
00076 #if defined(PNG_READ_iTXt_SUPPORTED)
00077       case PNG_READ_iTXt_MODE:
00078       {
00079          png_push_read_iTXt(png_ptr, info_ptr);
00080          break;
00081       }
00082 #endif
00083       case PNG_SKIP_MODE:
00084       {
00085          png_push_crc_finish(png_ptr);
00086          break;
00087       }
00088       default:
00089       {
00090          png_ptr->buffer_size = 0;
00091          break;
00092       }
00093    }
00094 }
00095 
00096 /* Read any remaining signature bytes from the stream and compare them with
00097  * the correct PNG signature.  It is possible that this routine is called
00098  * with bytes already read from the signature, either because they have been
00099  * checked by the calling application, or because of multiple calls to this
00100  * routine.
00101  */
00102 void /* PRIVATE */
00103 png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
00104 {
00105    png_size_t num_checked = png_ptr->sig_bytes,
00106              num_to_check = 8 - num_checked;
00107 
00108    if (png_ptr->buffer_size < num_to_check)
00109    {
00110       num_to_check = png_ptr->buffer_size;
00111    }
00112 
00113    png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
00114       num_to_check);
00115    png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes+num_to_check);
00116 
00117    if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
00118    {
00119       if (num_checked < 4 &&
00120           png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
00121          png_error(png_ptr, "Not a PNG file");
00122       else
00123          png_error(png_ptr, "PNG file corrupted by ASCII conversion");
00124    }
00125    else
00126    {
00127       if (png_ptr->sig_bytes >= 8)
00128       {
00129          png_ptr->process_mode = PNG_READ_CHUNK_MODE;
00130       }
00131    }
00132 }
00133 
00134 void /* PRIVATE */
00135 png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
00136 {
00137 #ifdef PNG_USE_LOCAL_ARRAYS
00138       PNG_IHDR;
00139       PNG_IDAT;
00140       PNG_IEND;
00141       PNG_PLTE;
00142 #if defined(PNG_READ_bKGD_SUPPORTED)
00143       PNG_bKGD;
00144 #endif
00145 #if defined(PNG_READ_cHRM_SUPPORTED)
00146       PNG_cHRM;
00147 #endif
00148 #if defined(PNG_READ_gAMA_SUPPORTED)
00149       PNG_gAMA;
00150 #endif
00151 #if defined(PNG_READ_hIST_SUPPORTED)
00152       PNG_hIST;
00153 #endif
00154 #if defined(PNG_READ_iCCP_SUPPORTED)
00155       PNG_iCCP;
00156 #endif
00157 #if defined(PNG_READ_iTXt_SUPPORTED)
00158       PNG_iTXt;
00159 #endif
00160 #if defined(PNG_READ_oFFs_SUPPORTED)
00161       PNG_oFFs;
00162 #endif
00163 #if defined(PNG_READ_pCAL_SUPPORTED)
00164       PNG_pCAL;
00165 #endif
00166 #if defined(PNG_READ_pHYs_SUPPORTED)
00167       PNG_pHYs;
00168 #endif
00169 #if defined(PNG_READ_sBIT_SUPPORTED)
00170       PNG_sBIT;
00171 #endif
00172 #if defined(PNG_READ_sCAL_SUPPORTED)
00173       PNG_sCAL;
00174 #endif
00175 #if defined(PNG_READ_sRGB_SUPPORTED)
00176       PNG_sRGB;
00177 #endif
00178 #if defined(PNG_READ_sPLT_SUPPORTED)
00179       PNG_sPLT;
00180 #endif
00181 #if defined(PNG_READ_tEXt_SUPPORTED)
00182       PNG_tEXt;
00183 #endif
00184 #if defined(PNG_READ_tIME_SUPPORTED)
00185       PNG_tIME;
00186 #endif
00187 #if defined(PNG_READ_tRNS_SUPPORTED)
00188       PNG_tRNS;
00189 #endif
00190 #if defined(PNG_READ_zTXt_SUPPORTED)
00191       PNG_zTXt;
00192 #endif
00193 #endif /* PNG_USE_LOCAL_ARRAYS */
00194    /* First we make sure we have enough data for the 4 byte chunk name
00195     * and the 4 byte chunk length before proceeding with decoding the
00196     * chunk data.  To fully decode each of these chunks, we also make
00197     * sure we have enough data in the buffer for the 4 byte CRC at the
00198     * end of every chunk (except IDAT, which is handled separately).
00199     */
00200    if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
00201    {
00202       png_byte chunk_length[4];
00203 
00204       if (png_ptr->buffer_size < 8)
00205       {
00206          png_push_save_buffer(png_ptr);
00207          return;
00208       }
00209 
00210       png_push_fill_buffer(png_ptr, chunk_length, 4);
00211       png_ptr->push_length = png_get_uint_32(chunk_length);
00212       png_reset_crc(png_ptr);
00213       png_crc_read(png_ptr, png_ptr->chunk_name, 4);
00214       png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
00215    }
00216 
00217    if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
00218    {
00219       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00220       {
00221          png_push_save_buffer(png_ptr);
00222          return;
00223       }
00224       png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
00225    }
00226    else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
00227    {
00228       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00229       {
00230          png_push_save_buffer(png_ptr);
00231          return;
00232       }
00233       png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
00234    }
00235    else if (!png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
00236    {
00237       /* If we reach an IDAT chunk, this means we have read all of the
00238        * header chunks, and we can start reading the image (or if this
00239        * is called after the image has been read - we have an error).
00240        */
00241      if (!(png_ptr->mode & PNG_HAVE_IHDR))
00242        png_error(png_ptr, "Missing IHDR before IDAT");
00243      else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
00244          !(png_ptr->mode & PNG_HAVE_PLTE))
00245        png_error(png_ptr, "Missing PLTE before IDAT");
00246 
00247       if (png_ptr->mode & PNG_HAVE_IDAT)
00248       {
00249          if (png_ptr->push_length == 0)
00250             return;
00251 
00252          if (png_ptr->mode & PNG_AFTER_IDAT)
00253             png_error(png_ptr, "Too many IDAT's found");
00254       }
00255 
00256       png_ptr->idat_size = png_ptr->push_length;
00257       png_ptr->mode |= PNG_HAVE_IDAT;
00258       png_ptr->process_mode = PNG_READ_IDAT_MODE;
00259       png_push_have_info(png_ptr, info_ptr);
00260       png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
00261       png_ptr->zstream.next_out = png_ptr->row_buf;
00262       return;
00263    }
00264    else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
00265    {
00266       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00267       {
00268          png_push_save_buffer(png_ptr);
00269          return;
00270       }
00271       png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
00272 
00273       png_ptr->process_mode = PNG_READ_DONE_MODE;
00274       png_push_have_end(png_ptr, info_ptr);
00275    }
00276 #if defined(PNG_READ_gAMA_SUPPORTED)
00277    else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
00278    {
00279       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00280       {
00281          png_push_save_buffer(png_ptr);
00282          return;
00283       }
00284       png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
00285    }
00286 #endif
00287 #if defined(PNG_READ_sBIT_SUPPORTED)
00288    else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
00289    {
00290       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00291       {
00292          png_push_save_buffer(png_ptr);
00293          return;
00294       }
00295       png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
00296    }
00297 #endif
00298 #if defined(PNG_READ_cHRM_SUPPORTED)
00299    else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
00300    {
00301       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00302       {
00303          png_push_save_buffer(png_ptr);
00304          return;
00305       }
00306       png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
00307    }
00308 #endif
00309 #if defined(PNG_READ_sRGB_SUPPORTED)
00310    else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
00311    {
00312       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00313       {
00314          png_push_save_buffer(png_ptr);
00315          return;
00316       }
00317       png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
00318    }
00319 #endif
00320 #if defined(PNG_READ_iCCP_SUPPORTED)
00321    else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
00322    {
00323       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00324       {
00325          png_push_save_buffer(png_ptr);
00326          return;
00327       }
00328       png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
00329    }
00330 #endif
00331 #if defined(PNG_READ_sPLT_SUPPORTED)
00332    else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
00333    {
00334       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00335       {
00336          png_push_save_buffer(png_ptr);
00337          return;
00338       }
00339       png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
00340    }
00341 #endif
00342 #if defined(PNG_READ_tRNS_SUPPORTED)
00343    else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
00344    {
00345       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00346       {
00347          png_push_save_buffer(png_ptr);
00348          return;
00349       }
00350       png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
00351    }
00352 #endif
00353 #if defined(PNG_READ_bKGD_SUPPORTED)
00354    else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
00355    {
00356       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00357       {
00358          png_push_save_buffer(png_ptr);
00359          return;
00360       }
00361       png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
00362    }
00363 #endif
00364 #if defined(PNG_READ_hIST_SUPPORTED)
00365    else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
00366    {
00367       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00368       {
00369          png_push_save_buffer(png_ptr);
00370          return;
00371       }
00372       png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
00373    }
00374 #endif
00375 #if defined(PNG_READ_pHYs_SUPPORTED)
00376    else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
00377    {
00378       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00379       {
00380          png_push_save_buffer(png_ptr);
00381          return;
00382       }
00383       png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
00384    }
00385 #endif
00386 #if defined(PNG_READ_oFFs_SUPPORTED)
00387    else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
00388    {
00389       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00390       {
00391          png_push_save_buffer(png_ptr);
00392          return;
00393       }
00394       png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
00395    }
00396 #endif
00397 #if defined(PNG_READ_pCAL_SUPPORTED)
00398    else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
00399    {
00400       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00401       {
00402          png_push_save_buffer(png_ptr);
00403          return;
00404       }
00405       png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
00406    }
00407 #endif
00408 #if defined(PNG_READ_sCAL_SUPPORTED)
00409    else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
00410    {
00411       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00412       {
00413          png_push_save_buffer(png_ptr);
00414          return;
00415       }
00416       png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
00417    }
00418 #endif
00419 #if defined(PNG_READ_tIME_SUPPORTED)
00420    else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
00421    {
00422       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00423       {
00424          png_push_save_buffer(png_ptr);
00425          return;
00426       }
00427       png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
00428    }
00429 #endif
00430 #if defined(PNG_READ_tEXt_SUPPORTED)
00431    else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
00432    {
00433       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00434       {
00435          png_push_save_buffer(png_ptr);
00436          return;
00437       }
00438       png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
00439    }
00440 #endif
00441 #if defined(PNG_READ_zTXt_SUPPORTED)
00442    else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
00443    {
00444       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00445       {
00446          png_push_save_buffer(png_ptr);
00447          return;
00448       }
00449       png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
00450    }
00451 #endif
00452 #if defined(PNG_READ_iTXt_SUPPORTED)
00453    else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
00454    {
00455       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00456       {
00457          png_push_save_buffer(png_ptr);
00458          return;
00459       }
00460       png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
00461    }
00462 #endif
00463    else
00464    {
00465       if (png_ptr->push_length + 4 > png_ptr->buffer_size)
00466       {
00467          png_push_save_buffer(png_ptr);
00468          return;
00469       }
00470       png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
00471    }
00472 
00473    png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
00474 }
00475 
00476 void /* PRIVATE */
00477 png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
00478 {
00479    png_ptr->process_mode = PNG_SKIP_MODE;
00480    png_ptr->skip_length = skip;
00481 }
00482 
00483 void /* PRIVATE */
00484 png_push_crc_finish(png_structp png_ptr)
00485 {
00486    if (png_ptr->skip_length && png_ptr->save_buffer_size)
00487    {
00488       png_size_t save_size;
00489 
00490       if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
00491          save_size = (png_size_t)png_ptr->skip_length;
00492       else
00493          save_size = png_ptr->save_buffer_size;
00494 
00495       png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
00496 
00497       png_ptr->skip_length -= save_size;
00498       png_ptr->buffer_size -= save_size;
00499       png_ptr->save_buffer_size -= save_size;
00500       png_ptr->save_buffer_ptr += save_size;
00501    }
00502    if (png_ptr->skip_length && png_ptr->current_buffer_size)
00503    {
00504       png_size_t save_size;
00505 
00506       if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
00507          save_size = (png_size_t)png_ptr->skip_length;
00508       else
00509          save_size = png_ptr->current_buffer_size;
00510 
00511       png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
00512 
00513       png_ptr->skip_length -= save_size;
00514       png_ptr->buffer_size -= save_size;
00515       png_ptr->current_buffer_size -= save_size;
00516       png_ptr->current_buffer_ptr += save_size;
00517    }
00518    if (!png_ptr->skip_length)
00519    {
00520       if (png_ptr->buffer_size < 4)
00521       {
00522          png_push_save_buffer(png_ptr);
00523          return;
00524       }
00525 
00526       png_crc_finish(png_ptr, 0);
00527       png_ptr->process_mode = PNG_READ_CHUNK_MODE;
00528    }
00529 }
00530 
00531 void PNGAPI
00532 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
00533 {
00534    png_bytep ptr;
00535 
00536    ptr = buffer;
00537    if (png_ptr->save_buffer_size)
00538    {
00539       png_size_t save_size;
00540 
00541       if (length < png_ptr->save_buffer_size)
00542          save_size = length;
00543       else
00544          save_size = png_ptr->save_buffer_size;
00545 
00546       png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
00547       length -= save_size;
00548       ptr += save_size;
00549       png_ptr->buffer_size -= save_size;
00550       png_ptr->save_buffer_size -= save_size;
00551       png_ptr->save_buffer_ptr += save_size;
00552    }
00553    if (length && png_ptr->current_buffer_size)
00554    {
00555       png_size_t save_size;
00556 
00557       if (length < png_ptr->current_buffer_size)
00558          save_size = length;
00559       else
00560          save_size = png_ptr->current_buffer_size;
00561 
00562       png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
00563       png_ptr->buffer_size -= save_size;
00564       png_ptr->current_buffer_size -= save_size;
00565       png_ptr->current_buffer_ptr += save_size;
00566    }
00567 }
00568 
00569 void /* PRIVATE */
00570 png_push_save_buffer(png_structp png_ptr)
00571 {
00572    if (png_ptr->save_buffer_size)
00573    {
00574       if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
00575       {
00576          png_size_t i,istop;
00577          png_bytep sp;
00578          png_bytep dp;
00579 
00580          istop = png_ptr->save_buffer_size;
00581          for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
00582             i < istop; i++, sp++, dp++)
00583          {
00584             *dp = *sp;
00585          }
00586       }
00587    }
00588    if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
00589       png_ptr->save_buffer_max)
00590    {
00591       png_size_t new_max;
00592       png_bytep old_buffer;
00593 
00594       new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
00595       old_buffer = png_ptr->save_buffer;
00596       png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
00597          (png_uint_32)new_max);
00598       png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
00599       png_free(png_ptr, old_buffer);
00600       png_ptr->save_buffer_max = new_max;
00601    }
00602    if (png_ptr->current_buffer_size)
00603    {
00604       png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
00605          png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
00606       png_ptr->save_buffer_size += png_ptr->current_buffer_size;
00607       png_ptr->current_buffer_size = 0;
00608    }
00609    png_ptr->save_buffer_ptr = png_ptr->save_buffer;
00610    png_ptr->buffer_size = 0;
00611 }
00612 
00613 void /* PRIVATE */
00614 png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
00615    png_size_t buffer_length)
00616 {
00617    png_ptr->current_buffer = buffer;
00618    png_ptr->current_buffer_size = buffer_length;
00619    png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
00620    png_ptr->current_buffer_ptr = png_ptr->current_buffer;
00621 }
00622 
00623 void /* PRIVATE */
00624 png_push_read_IDAT(png_structp png_ptr)
00625 {
00626 #ifdef PNG_USE_LOCAL_ARRAYS
00627    PNG_IDAT;
00628 #endif
00629    if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
00630    {
00631       png_byte chunk_length[4];
00632 
00633       if (png_ptr->buffer_size < 8)
00634       {
00635          png_push_save_buffer(png_ptr);
00636          return;
00637       }
00638 
00639       png_push_fill_buffer(png_ptr, chunk_length, 4);
00640       png_ptr->push_length = png_get_uint_32(chunk_length);
00641 
00642       png_reset_crc(png_ptr);
00643       png_crc_read(png_ptr, png_ptr->chunk_name, 4);
00644       png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
00645 
00646       if (png_memcmp(png_ptr->chunk_name, (png_bytep)png_IDAT, 4))
00647       {
00648          png_ptr->process_mode = PNG_READ_CHUNK_MODE;
00649          if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
00650             png_error(png_ptr, "Not enough compressed data");
00651          return;
00652       }
00653 
00654       png_ptr->idat_size = png_ptr->push_length;
00655    }
00656    if (png_ptr->idat_size && png_ptr->save_buffer_size)
00657    {
00658       png_size_t save_size;
00659 
00660       if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
00661       {
00662          save_size = (png_size_t)png_ptr->idat_size;
00663          /* check for overflow */
00664          if((png_uint_32)save_size != png_ptr->idat_size)
00665             png_error(png_ptr, "save_size overflowed in pngpread");
00666       }
00667       else
00668          save_size = png_ptr->save_buffer_size;
00669 
00670       png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
00671       if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
00672          png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
00673       png_ptr->idat_size -= save_size;
00674       png_ptr->buffer_size -= save_size;
00675       png_ptr->save_buffer_size -= save_size;
00676       png_ptr->save_buffer_ptr += save_size;
00677    }
00678    if (png_ptr->idat_size && png_ptr->current_buffer_size)
00679    {
00680       png_size_t save_size;
00681 
00682       if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
00683       {
00684          save_size = (png_size_t)png_ptr->idat_size;
00685          /* check for overflow */
00686          if((png_uint_32)save_size != png_ptr->idat_size)
00687             png_error(png_ptr, "save_size overflowed in pngpread");
00688       }
00689       else
00690          save_size = png_ptr->current_buffer_size;
00691 
00692       png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
00693       if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
00694         png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
00695 
00696       png_ptr->idat_size -= save_size;
00697       png_ptr->buffer_size -= save_size;
00698       png_ptr->current_buffer_size -= save_size;
00699       png_ptr->current_buffer_ptr += save_size;
00700    }
00701    if (!png_ptr->idat_size)
00702    {
00703       if (png_ptr->buffer_size < 4)
00704       {
00705          png_push_save_buffer(png_ptr);
00706          return;
00707       }
00708 
00709       png_crc_finish(png_ptr, 0);
00710       png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
00711       png_ptr->mode |= PNG_AFTER_IDAT;
00712    }
00713 }
00714 
00715 void /* PRIVATE */
00716 png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
00717    png_size_t buffer_length)
00718 {
00719    int ret;
00720 
00721    if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
00722       png_error(png_ptr, "Extra compression data");
00723 
00724    png_ptr->zstream.next_in = buffer;
00725    png_ptr->zstream.avail_in = (uInt)buffer_length;
00726    for(;;)
00727    {
00728       ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
00729       if (ret != Z_OK)
00730       {
00731          if (ret == Z_STREAM_END)
00732          {
00733             if (png_ptr->zstream.avail_in)
00734                png_error(png_ptr, "Extra compressed data");
00735             if (!(png_ptr->zstream.avail_out))
00736             {
00737                png_push_process_row(png_ptr);
00738             }
00739 
00740             png_ptr->mode |= PNG_AFTER_IDAT;
00741             png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
00742             break;
00743          }
00744          else if (ret == Z_BUF_ERROR)
00745             break;
00746          else
00747             png_error(png_ptr, "Decompression Error");
00748       }
00749       if (!(png_ptr->zstream.avail_out))
00750       {
00751          if ((
00752 #if defined(PNG_READ_INTERLACING_SUPPORTED)
00753              png_ptr->interlaced && png_ptr->pass > 6) ||
00754              (!png_ptr->interlaced &&
00755 #endif
00756              png_ptr->row_number == png_ptr->num_rows-1))
00757          {
00758            if (png_ptr->zstream.avail_in)
00759              png_warning(png_ptr, "Too much data in IDAT chunks");
00760            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
00761            break;
00762          }
00763          png_push_process_row(png_ptr);
00764          png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
00765          png_ptr->zstream.next_out = png_ptr->row_buf;
00766       }
00767       else
00768          break;
00769    }
00770 }
00771 
00772 void /* PRIVATE */
00773 png_push_process_row(png_structp png_ptr)
00774 {
00775    png_ptr->row_info.color_type = png_ptr->color_type;
00776    png_ptr->row_info.width = png_ptr->iwidth;
00777    png_ptr->row_info.channels = png_ptr->channels;
00778    png_ptr->row_info.bit_depth = png_ptr->bit_depth;
00779    png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
00780 
00781    png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
00782       (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
00783 
00784    png_read_filter_row(png_ptr, &(png_ptr->row_info),
00785       png_ptr->row_buf + 1, png_ptr->prev_row + 1,
00786       (int)(png_ptr->row_buf[0]));
00787 
00788    png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
00789       png_ptr->rowbytes + 1);
00790 
00791    if (png_ptr->transformations)
00792       png_do_read_transformations(png_ptr);
00793 
00794 #if defined(PNG_READ_INTERLACING_SUPPORTED)
00795    /* blow up interlaced rows to full size */
00796    if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
00797    {
00798       if (png_ptr->pass < 6)
00799 /*       old interface (pre-1.0.9):
00800          png_do_read_interlace(&(png_ptr->row_info),
00801             png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
00802  */
00803          png_do_read_interlace(png_ptr);
00804 
00805     switch (png_ptr->pass)
00806     {
00807          case 0:
00808          {
00809             int i;
00810             for (i = 0; i < 8 && png_ptr->pass == 0; i++)
00811             {
00812                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
00813                png_read_push_finish_row(png_ptr); /* updates png_ptr->pass */
00814             }
00815             if (png_ptr->pass == 2) /* pass 1 might be empty */
00816             {
00817                for (i = 0; i < 4 && png_ptr->pass == 2; i++)
00818                {
00819                   png_push_have_row(png_ptr, png_bytep_NULL);
00820                   png_read_push_finish_row(png_ptr);
00821                }
00822             }
00823             if (png_ptr->pass == 4 && png_ptr->height <= 4)
00824             {
00825                for (i = 0; i < 2 && png_ptr->pass == 4; i++)
00826                {
00827                   png_push_have_row(png_ptr, png_bytep_NULL);
00828                   png_read_push_finish_row(png_ptr);
00829                }
00830             }
00831             if (png_ptr->pass == 6 && png_ptr->height <= 4)
00832             {
00833                 png_push_have_row(png_ptr, png_bytep_NULL);
00834                 png_read_push_finish_row(png_ptr);
00835             }
00836             break;
00837          }
00838          case 1:
00839          {
00840             int i;
00841             for (i = 0; i < 8 && png_ptr->pass == 1; i++)
00842             {
00843                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
00844                png_read_push_finish_row(png_ptr);
00845             }
00846             if (png_ptr->pass == 2) /* skip top 4 generated rows */
00847             {
00848                for (i = 0; i < 4 && png_ptr->pass == 2; i++)
00849                {
00850                   png_push_have_row(png_ptr, png_bytep_NULL);
00851                   png_read_push_finish_row(png_ptr);
00852                }
00853             }
00854             break;
00855          }
00856          case 2:
00857          {
00858             int i;
00859             for (i = 0; i < 4 && png_ptr->pass == 2; i++)
00860             {
00861                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
00862                png_read_push_finish_row(png_ptr);
00863             }
00864             for (i = 0; i < 4 && png_ptr->pass == 2; i++)
00865             {
00866                png_push_have_row(png_ptr, png_bytep_NULL);
00867                png_read_push_finish_row(png_ptr);
00868             }
00869             if (png_ptr->pass == 4) /* pass 3 might be empty */
00870             {
00871                for (i = 0; i < 2 && png_ptr->pass == 4; i++)
00872                {
00873                   png_push_have_row(png_ptr, png_bytep_NULL);
00874                   png_read_push_finish_row(png_ptr);
00875                }
00876             }
00877             break;
00878          }
00879          case 3:
00880          {
00881             int i;
00882             for (i = 0; i < 4 && png_ptr->pass == 3; i++)
00883             {
00884                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
00885                png_read_push_finish_row(png_ptr);
00886             }
00887             if (png_ptr->pass == 4) /* skip top two generated rows */
00888             {
00889                for (i = 0; i < 2 && png_ptr->pass == 4; i++)
00890                {
00891                   png_push_have_row(png_ptr, png_bytep_NULL);
00892                   png_read_push_finish_row(png_ptr);
00893                }
00894             }
00895             break;
00896          }
00897          case 4:
00898          {
00899             int i;
00900             for (i = 0; i < 2 && png_ptr->pass == 4; i++)
00901             {
00902                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
00903                png_read_push_finish_row(png_ptr);
00904             }
00905             for (i = 0; i < 2 && png_ptr->pass == 4; i++)
00906             {
00907                png_push_have_row(png_ptr, png_bytep_NULL);
00908                png_read_push_finish_row(png_ptr);
00909             }
00910             if (png_ptr->pass == 6) /* pass 5 might be empty */
00911             {
00912                png_push_have_row(png_ptr, png_bytep_NULL);
00913                png_read_push_finish_row(png_ptr);
00914             }
00915             break;
00916          }
00917          case 5:
00918          {
00919             int i;
00920             for (i = 0; i < 2 && png_ptr->pass == 5; i++)
00921             {
00922                png_push_have_row(png_ptr, png_ptr->row_buf + 1);
00923                png_read_push_finish_row(png_ptr);
00924             }
00925             if (png_ptr->pass == 6) /* skip top generated row */
00926             {
00927                png_push_have_row(png_ptr, png_bytep_NULL);
00928                png_read_push_finish_row(png_ptr);
00929             }
00930             break;
00931          }
00932          case 6:
00933          {
00934             png_push_have_row(png_ptr, png_ptr->row_buf + 1);
00935             png_read_push_finish_row(png_ptr);
00936             if (png_ptr->pass != 6)
00937                break;
00938             png_push_have_row(png_ptr, png_bytep_NULL);
00939             png_read_push_finish_row(png_ptr);
00940          }
00941       }
00942    }
00943    else
00944 #endif
00945    {
00946       png_push_have_row(png_ptr, png_ptr->row_buf + 1);
00947       png_read_push_finish_row(png_ptr);
00948    }
00949 }
00950 
00951 void /* PRIVATE */
00952 png_read_push_finish_row(png_structp png_ptr)
00953 {
00954 #ifdef PNG_USE_LOCAL_ARRAYS
00955    /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
00956 
00957    /* start of interlace block */
00958    const int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
00959 
00960    /* offset to next interlace block */
00961    const int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
00962 
00963    /* start of interlace block in the y direction */
00964    const int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
00965 
00966    /* offset to next interlace block in the y direction */
00967    const int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
00968 
00969    /* Width of interlace block.  This is not currently used - if you need
00970     * it, uncomment it here and in png.h
00971    const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
00972    */
00973 
00974    /* Height of interlace block.  This is not currently used - if you need
00975     * it, uncomment it here and in png.h
00976    const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
00977    */
00978 #endif
00979 
00980    png_ptr->row_number++;
00981    if (png_ptr->row_number < png_ptr->num_rows)
00982       return;
00983 
00984    if (png_ptr->interlaced)
00985    {
00986       png_ptr->row_number = 0;
00987       png_memset_check(png_ptr, png_ptr->prev_row, 0,
00988          png_ptr->rowbytes + 1);
00989       do
00990       {
00991          png_ptr->pass++;
00992          if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
00993              (png_ptr->pass == 3 && png_ptr->width < 3) ||
00994              (png_ptr->pass == 5 && png_ptr->width < 2))
00995            png_ptr->pass++;
00996 
00997          if (png_ptr->pass > 7)
00998             png_ptr->pass--;
00999          if (png_ptr->pass >= 7)
01000             break;
01001 
01002          png_ptr->iwidth = (png_ptr->width +
01003             png_pass_inc[png_ptr->pass] - 1 -
01004             png_pass_start[png_ptr->pass]) /
01005             png_pass_inc[png_ptr->pass];
01006 
01007          png_ptr->irowbytes = ((png_ptr->iwidth *
01008             png_ptr->pixel_depth + 7) >> 3) + 1;
01009 
01010          if (png_ptr->transformations & PNG_INTERLACE)
01011             break;
01012 
01013          png_ptr->num_rows = (png_ptr->height +
01014             png_pass_yinc[png_ptr->pass] - 1 -
01015             png_pass_ystart[png_ptr->pass]) /
01016             png_pass_yinc[png_ptr->pass];
01017 
01018       } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
01019    }
01020 }
01021 
01022 #if defined(PNG_READ_tEXt_SUPPORTED)
01023 void /* PRIVATE */
01024 png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
01025    length)
01026 {
01027    if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
01028       {
01029          png_error(png_ptr, "Out of place tEXt");
01030          /* to quiet some compiler warnings */
01031          if(info_ptr == NULL) return;
01032       }
01033 
01034 #ifdef PNG_MAX_MALLOC_64K
01035    png_ptr->skip_length = 0;  /* This may not be necessary */
01036 
01037    if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
01038    {
01039       png_warning(png_ptr, "tEXt chunk too large to fit in memory");
01040       png_ptr->skip_length = length - (png_uint_32)65535L;
01041       length = (png_uint_32)65535L;
01042    }
01043 #endif
01044 
01045    png_ptr->current_text = (png_charp)png_malloc(png_ptr,
01046          (png_uint_32)(length+1));
01047    png_ptr->current_text[length] = '\0';
01048    png_ptr->current_text_ptr = png_ptr->current_text;
01049    png_ptr->current_text_size = (png_size_t)length;
01050    png_ptr->current_text_left = (png_size_t)length;
01051    png_ptr->process_mode = PNG_READ_tEXt_MODE;
01052 }
01053 
01054 void /* PRIVATE */
01055 png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
01056 {
01057    if (png_ptr->buffer_size && png_ptr->current_text_left)
01058    {
01059       png_size_t text_size;
01060 
01061       if (png_ptr->buffer_size < png_ptr->current_text_left)
01062          text_size = png_ptr->buffer_size;
01063       else
01064          text_size = png_ptr->current_text_left;
01065       png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
01066       png_ptr->current_text_left -= text_size;
01067       png_ptr->current_text_ptr += text_size;
01068    }
01069    if (!(png_ptr->current_text_left))
01070    {
01071       png_textp text_ptr;
01072       png_charp text;
01073       png_charp key;
01074       int ret;
01075 
01076       if (png_ptr->buffer_size < 4)
01077       {
01078          png_push_save_buffer(png_ptr);
01079          return;
01080       }
01081 
01082       png_push_crc_finish(png_ptr);
01083 
01084 #if defined(PNG_MAX_MALLOC_64K)
01085       if (png_ptr->skip_length)
01086          return;
01087 #endif
01088 
01089       key = png_ptr->current_text;
01090 
01091       for (text = key; *text; text++)
01092          /* empty loop */ ;
01093 
01094       if (text != key + png_ptr->current_text_size)
01095          text++;
01096 
01097       text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
01098       text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
01099       text_ptr->key = key;
01100 #ifdef PNG_iTXt_SUPPORTED
01101       text_ptr->lang = NULL;
01102       text_ptr->lang_key = NULL;
01103 #endif
01104       text_ptr->text = text;
01105 
01106       ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
01107 
01108       png_free(png_ptr, key);
01109       png_free(png_ptr, text_ptr);
01110       png_ptr->current_text = NULL;
01111 
01112       if (ret)
01113         png_warning(png_ptr, "Insufficient memory to store text chunk.");
01114    }
01115 }
01116 #endif
01117 
01118 #if defined(PNG_READ_zTXt_SUPPORTED)
01119 void /* PRIVATE */
01120 png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
01121    length)
01122 {
01123    if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
01124       {
01125          png_error(png_ptr, "Out of place zTXt");
01126          /* to quiet some compiler warnings */
01127          if(info_ptr == NULL) return;
01128       }
01129 
01130 #ifdef PNG_MAX_MALLOC_64K
01131    /* We can't handle zTXt chunks > 64K, since we don't have enough space
01132     * to be able to store the uncompressed data.  Actually, the threshold
01133     * is probably around 32K, but it isn't as definite as 64K is.
01134     */
01135    if (length > (png_uint_32)65535L)
01136    {
01137       png_warning(png_ptr, "zTXt chunk too large to fit in memory");
01138       png_push_crc_skip(png_ptr, length);
01139       return;
01140    }
01141 #endif
01142 
01143    png_ptr->current_text = (png_charp)png_malloc(png_ptr,
01144        (png_uint_32)(length+1));
01145    png_ptr->current_text[length] = '\0';
01146    png_ptr->current_text_ptr = png_ptr->current_text;
01147    png_ptr->current_text_size = (png_size_t)length;
01148    png_ptr->current_text_left = (png_size_t)length;
01149    png_ptr->process_mode = PNG_READ_zTXt_MODE;
01150 }
01151 
01152 void /* PRIVATE */
01153 png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
01154 {
01155    if (png_ptr->buffer_size && png_ptr->current_text_left)
01156    {
01157       png_size_t text_size;
01158 
01159       if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
01160          text_size = png_ptr->buffer_size;
01161       else
01162          text_size = png_ptr->current_text_left;
01163       png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
01164       png_ptr->current_text_left -= text_size;
01165       png_ptr->current_text_ptr += text_size;
01166    }
01167    if (!(png_ptr->current_text_left))
01168    {
01169       png_textp text_ptr;
01170       png_charp text;
01171       png_charp key;
01172       int ret;
01173       png_size_t text_size, key_size;
01174 
01175       if (png_ptr->buffer_size < 4)
01176       {
01177          png_push_save_buffer(png_ptr);
01178          return;
01179       }
01180 
01181       png_push_crc_finish(png_ptr);
01182 
01183       key = png_ptr->current_text;
01184 
01185       for (text = key; *text; text++)
01186          /* empty loop */ ;
01187 
01188       /* zTXt can't have zero text */
01189       if (text == key + png_ptr->current_text_size)
01190       {
01191          png_ptr->current_text = NULL;
01192          png_free(png_ptr, key);
01193          return;
01194       }
01195 
01196       text++;
01197 
01198       if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
01199       {
01200          png_ptr->current_text = NULL;
01201          png_free(png_ptr, key);
01202          return;
01203       }
01204 
01205       text++;
01206 
01207       png_ptr->zstream.next_in = (png_bytep )text;
01208       png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
01209          (text - key));
01210       png_ptr->zstream.next_out = png_ptr->zbuf;
01211       png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
01212 
01213       key_size = text - key;
01214       text_size = 0;
01215       text = NULL;
01216       ret = Z_STREAM_END;
01217 
01218       while (png_ptr->zstream.avail_in)
01219       {
01220          ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
01221          if (ret != Z_OK && ret != Z_STREAM_END)
01222          {
01223             inflateReset(&png_ptr->zstream);
01224             png_ptr->zstream.avail_in = 0;
01225             png_ptr->current_text = NULL;
01226             png_free(png_ptr, key);
01227             png_free(png_ptr, text);
01228             return;
01229          }
01230          if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
01231          {
01232             if (text == NULL)
01233             {
01234                text = (png_charp)png_malloc(png_ptr,
01235                   (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
01236                      + key_size + 1));
01237                png_memcpy(text + key_size, png_ptr->zbuf,
01238                   png_ptr->zbuf_size - png_ptr->zstream.avail_out);
01239                png_memcpy(text, key, key_size);
01240                text_size = key_size + png_ptr->zbuf_size -
01241                   png_ptr->zstream.avail_out;
01242                *(text + text_size) = '\0';
01243             }
01244             else
01245             {
01246                png_charp tmp;
01247 
01248                tmp = text;
01249                text = (png_charp)png_malloc(png_ptr, text_size +
01250                   (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out
01251                    + 1));
01252                png_memcpy(text, tmp, text_size);
01253                png_free(png_ptr, tmp);
01254                png_memcpy(text + text_size, png_ptr->zbuf,
01255                   png_ptr->zbuf_size - png_ptr->zstream.avail_out);
01256                text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
01257                *(text + text_size) = '\0';
01258             }
01259             if (ret != Z_STREAM_END)
01260             {
01261                png_ptr->zstream.next_out = png_ptr->zbuf;
01262                png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
01263             }
01264          }
01265          else
01266          {
01267             break;
01268          }
01269 
01270          if (ret == Z_STREAM_END)
01271             break;
01272       }
01273 
01274       inflateReset(&png_ptr->zstream);
01275       png_ptr->zstream.avail_in = 0;
01276 
01277       if (ret != Z_STREAM_END)
01278       {
01279          png_ptr->current_text = NULL;
01280          png_free(png_ptr, key);
01281          png_free(png_ptr, text);
01282          return;
01283       }
01284 
01285       png_ptr->current_text = NULL;
01286       png_free(png_ptr, key);
01287       key = text;
01288       text += key_size;
01289 
01290       text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
01291       text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
01292       text_ptr->key = key;
01293 #ifdef PNG_iTXt_SUPPORTED
01294       text_ptr->lang = NULL;
01295       text_ptr->lang_key = NULL;
01296 #endif
01297       text_ptr->text = text;
01298 
01299       ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
01300 
01301       png_free(png_ptr, key);
01302       png_free(png_ptr, text_ptr);
01303 
01304       if (ret)
01305         png_warning(png_ptr, "Insufficient memory to store text chunk.");
01306    }
01307 }
01308 #endif
01309 
01310 #if defined(PNG_READ_iTXt_SUPPORTED)
01311 void /* PRIVATE */
01312 png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
01313    length)
01314 {
01315    if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
01316       {
01317          png_error(png_ptr, "Out of place iTXt");
01318          /* to quiet some compiler warnings */
01319          if(info_ptr == NULL) return;
01320       }
01321 
01322 #ifdef PNG_MAX_MALLOC_64K
01323    png_ptr->skip_length = 0;  /* This may not be necessary */
01324 
01325    if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
01326    {
01327       png_warning(png_ptr, "iTXt chunk too large to fit in memory");
01328       png_ptr->skip_length = length - (png_uint_32)65535L;
01329       length = (png_uint_32)65535L;
01330    }
01331 #endif
01332 
01333    png_ptr->current_text = (png_charp)png_malloc(png_ptr,
01334          (png_uint_32)(length+1));
01335    png_ptr->current_text[length] = '\0';
01336    png_ptr->current_text_ptr = png_ptr->current_text;
01337    png_ptr->current_text_size = (png_size_t)length;
01338    png_ptr->current_text_left = (png_size_t)length;
01339    png_ptr->process_mode = PNG_READ_iTXt_MODE;
01340 }
01341 
01342 void /* PRIVATE */
01343 png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
01344 {
01345 
01346    if (png_ptr->buffer_size && png_ptr->current_text_left)
01347    {
01348       png_size_t text_size;
01349 
01350       if (png_ptr->buffer_size < png_ptr->current_text_left)
01351          text_size = png_ptr->buffer_size;
01352       else
01353          text_size = png_ptr->current_text_left;
01354       png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
01355       png_ptr->current_text_left -= text_size;
01356       png_ptr->current_text_ptr += text_size;
01357    }
01358    if (!(png_ptr->current_text_left))
01359    {
01360       png_textp text_ptr;
01361       png_charp key;
01362       int comp_flag;
01363       png_charp lang;
01364       png_charp lang_key;
01365       png_charp text;
01366       int ret;
01367 
01368       if (png_ptr->buffer_size < 4)
01369       {
01370          png_push_save_buffer(png_ptr);
01371          return;
01372       }
01373 
01374       png_push_crc_finish(png_ptr);
01375 
01376 #if defined(PNG_MAX_MALLOC_64K)
01377       if (png_ptr->skip_length)
01378          return;
01379 #endif
01380 
01381       key = png_ptr->current_text;
01382 
01383       for (lang = key; *lang; lang++)
01384          /* empty loop */ ;
01385 
01386       if (lang != key + png_ptr->current_text_size)
01387          lang++;
01388 
01389       comp_flag = *lang++;
01390       lang++;     /* skip comp_type, always zero */
01391 
01392       for (lang_key = lang; *lang_key; lang_key++)
01393          /* empty loop */ ;
01394       lang_key++;        /* skip NUL separator */
01395 
01396       for (text = lang_key; *text; text++)
01397          /* empty loop */ ;
01398 
01399       if (text != key + png_ptr->current_text_size)
01400          text++;
01401 
01402       text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text));
01403       text_ptr->compression = comp_flag + 2;
01404       text_ptr->key = key;
01405       text_ptr->lang = lang;
01406       text_ptr->lang_key = lang_key;
01407       text_ptr->text = text;
01408       text_ptr->text_length = 0;
01409       text_ptr->itxt_length = png_strlen(text);
01410 
01411       ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
01412 
01413       png_ptr->current_text = NULL;
01414 
01415       png_free(png_ptr, text_ptr);
01416       if (ret)
01417         png_warning(png_ptr, "Insufficient memory to store iTXt chunk.");
01418    }
01419 }
01420 #endif
01421 
01422 /* This function is called when we haven't found a handler for this
01423  * chunk.  If there isn't a problem with the chunk itself (ie a bad chunk
01424  * name or a critical chunk), the chunk is (currently) silently ignored.
01425  */
01426 void /* PRIVATE */
01427 png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
01428    length)
01429 {
01430    png_uint_32 skip=0;
01431    png_check_chunk_name(png_ptr, png_ptr->chunk_name);
01432 
01433    if (!(png_ptr->chunk_name[0] & 0x20))
01434    {
01435 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
01436       if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
01437            HANDLE_CHUNK_ALWAYS
01438 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
01439            && png_ptr->read_user_chunk_fn == NULL
01440 #endif
01441          )
01442 #endif
01443          png_chunk_error(png_ptr, "unknown critical chunk");
01444 
01445       /* to quiet compiler warnings about unused info_ptr */
01446       if (info_ptr == NULL)
01447          return;
01448    }
01449 
01450 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
01451    if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
01452    {
01453        png_unknown_chunk chunk;
01454 
01455 #ifdef PNG_MAX_MALLOC_64K
01456        if (length > (png_uint_32)65535L)
01457        {
01458            png_warning(png_ptr, "unknown chunk too large to fit in memory");
01459            skip = length - (png_uint_32)65535L;
01460            length = (png_uint_32)65535L;
01461        }
01462 #endif
01463 
01464        png_strcpy((png_charp)chunk.name, (png_charp)png_ptr->chunk_name);
01465        chunk.data = (png_bytep)png_malloc(png_ptr, length);
01466        png_crc_read(png_ptr, chunk.data, length);
01467        chunk.size = length;
01468 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
01469        if(png_ptr->read_user_chunk_fn != NULL)
01470        {
01471           /* callback to user unknown chunk handler */
01472           if ((*(png_ptr->read_user_chunk_fn)) (png_ptr, &chunk) <= 0)
01473           {
01474              if (!(png_ptr->chunk_name[0] & 0x20))
01475                 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
01476                      HANDLE_CHUNK_ALWAYS)
01477                    png_chunk_error(png_ptr, "unknown critical chunk");
01478           }
01479              png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
01480        }
01481        else
01482 #endif
01483           png_set_unknown_chunks(png_ptr, info_ptr, &chunk, 1);
01484        png_free(png_ptr, chunk.data);
01485    }
01486    else
01487 #endif
01488       skip=length;
01489    png_push_crc_skip(png_ptr, skip);
01490 }
01491 
01492 void /* PRIVATE */
01493 png_push_have_info(png_structp png_ptr, png_infop info_ptr)
01494 {
01495    if (png_ptr->info_fn != NULL)
01496       (*(png_ptr->info_fn))(png_ptr, info_ptr);
01497 }
01498 
01499 void /* PRIVATE */
01500 png_push_have_end(png_structp png_ptr, png_infop info_ptr)
01501 {
01502    if (png_ptr->end_fn != NULL)
01503       (*(png_ptr->end_fn))(png_ptr, info_ptr);
01504 }
01505 
01506 void /* PRIVATE */
01507 png_push_have_row(png_structp png_ptr, png_bytep row)
01508 {
01509    if (png_ptr->row_fn != NULL)
01510       (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
01511          (int)png_ptr->pass);
01512 }
01513 
01514 void PNGAPI
01515 png_progressive_combine_row (png_structp png_ptr,
01516    png_bytep old_row, png_bytep new_row)
01517 {
01518 #ifdef PNG_USE_LOCAL_ARRAYS
01519    const int FARDATA png_pass_dsp_mask[7] =
01520       {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
01521 #endif
01522    if (new_row != NULL)    /* new_row must == png_ptr->row_buf here. */
01523       png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
01524 }
01525 
01526 void PNGAPI
01527 png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
01528    png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
01529    png_progressive_end_ptr end_fn)
01530 {
01531    png_ptr->info_fn = info_fn;
01532    png_ptr->row_fn = row_fn;
01533    png_ptr->end_fn = end_fn;
01534 
01535    png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
01536 }
01537 
01538 png_voidp PNGAPI
01539 png_get_progressive_ptr(png_structp png_ptr)
01540 {
01541    return png_ptr->io_ptr;
01542 }
01543 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */