1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /* pngtest.c - a simple test program to test libpng
  26  *
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file and, per its terms, should not be removed:
  31  *
  32  * Last changed in libpng 1.5.25 [December 3, 2015]
  33  * Copyright (c) 1998-2015 Glenn Randers-Pehrson
  34  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  35  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  36  *
  37  * This code is released under the libpng license.
  38  * For conditions of distribution and use, see the disclaimer
  39  * and license in png.h
  40  *
  41  * This program reads in a PNG image, writes it out again, and then
  42  * compares the two files.  If the files are identical, this shows that
  43  * the basic chunk handling, filtering, and (de)compression code is working
  44  * properly.  It does not currently test all of the transforms, although
  45  * it probably should.
  46  *
  47  * The program will report "FAIL" in certain legitimate cases:
  48  * 1) when the compression level or filter selection method is changed.
  49  * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
  50  * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
  51  *    exist in the input file.
  52  * 4) others not listed here...
  53  * In these cases, it is best to check with another tool such as "pngcheck"
  54  * to see what the differences between the two files are.
  55  *
  56  * If a filename is given on the command-line, then this file is used
  57  * for the input, rather than the default "pngtest.png".  This allows
  58  * testing a wide variety of files easily.  You can also test a number
  59  * of files at once by typing "pngtest -m file1.png file2.png ..."
  60  */
  61 
  62 #define _POSIX_SOURCE 1
  63 
  64 #include <stdio.h>
  65 #include <stdlib.h>
  66 #include <string.h>
  67 
  68 /* Defined so I can write to a file on gui/windowing platforms */
  69 /*  #define STDERR stderr  */
  70 #define STDERR stdout   /* For DOS */
  71 
  72 #include "png.h"
  73 
  74 /* Known chunks that exist in pngtest.png must be supported or pngtest will fail
  75  * simply as a result of re-ordering them.  This may be fixed in 1.7
  76  *
  77  * pngtest allocates a single row buffer for each row and overwrites it,
  78  * therefore if the write side doesn't support the writing of interlaced images
  79  * nothing can be done for an interlaced image (and the code below will fail
  80  * horribly trying to write extra data after writing garbage).
  81  */
  82 #if defined PNG_READ_SUPPORTED && /* else nothing can be done */\
  83    defined PNG_READ_bKGD_SUPPORTED &&\
  84    defined PNG_READ_cHRM_SUPPORTED &&\
  85    defined PNG_READ_gAMA_SUPPORTED &&\
  86    defined PNG_READ_oFFs_SUPPORTED &&\
  87    defined PNG_READ_pCAL_SUPPORTED &&\
  88    defined PNG_READ_pHYs_SUPPORTED &&\
  89    defined PNG_READ_sBIT_SUPPORTED &&\
  90    defined PNG_READ_sCAL_SUPPORTED &&\
  91    defined PNG_READ_sRGB_SUPPORTED &&\
  92    defined PNG_READ_sPLT_SUPPORTED &&\
  93    defined PNG_READ_tEXt_SUPPORTED &&\
  94    defined PNG_READ_tIME_SUPPORTED &&\
  95    defined PNG_READ_zTXt_SUPPORTED &&\
  96    (defined PNG_WRITE_INTERLACING_SUPPORTED || PNG_LIBPNG_VER >= 10700)
  97 
  98 #ifdef PNG_ZLIB_HEADER
  99 #  include PNG_ZLIB_HEADER /* defined by pnglibconf.h from 1.7 */
 100 #else
 101 #  include "zlib.h"
 102 #endif
 103 
 104 /* Copied from pngpriv.h but only used in error messages below. */
 105 #ifndef PNG_ZBUF_SIZE
 106 #  define PNG_ZBUF_SIZE 8192
 107 #endif
 108 #define FCLOSE(file) fclose(file)
 109 
 110 #ifndef PNG_STDIO_SUPPORTED
 111 typedef FILE                * png_FILE_p;
 112 #endif
 113 
 114 /* Makes pngtest verbose so we can find problems. */
 115 #ifndef PNG_DEBUG
 116 #  define PNG_DEBUG 0
 117 #endif
 118 
 119 #if PNG_DEBUG > 1
 120 #  define pngtest_debug(m)        ((void)fprintf(stderr, m "\n"))
 121 #  define pngtest_debug1(m,p1)    ((void)fprintf(stderr, m "\n", p1))
 122 #  define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
 123 #else
 124 #  define pngtest_debug(m)        ((void)0)
 125 #  define pngtest_debug1(m,p1)    ((void)0)
 126 #  define pngtest_debug2(m,p1,p2) ((void)0)
 127 #endif
 128 
 129 #if !PNG_DEBUG
 130 #  define SINGLE_ROWBUF_ALLOC  /* Makes buffer overruns easier to nail */
 131 #endif
 132 
 133 #ifndef PNG_UNUSED
 134 #  define PNG_UNUSED(param) (void)param;
 135 #endif
 136 
 137 /* Turn on CPU timing
 138 #define PNGTEST_TIMING
 139 */
 140 
 141 #ifndef PNG_FLOATING_POINT_SUPPORTED
 142 #undef PNGTEST_TIMING
 143 #endif
 144 
 145 #ifdef PNGTEST_TIMING
 146 static float t_start, t_stop, t_decode, t_encode, t_misc;
 147 #include <time.h>
 148 #endif
 149 
 150 #ifdef PNG_TIME_RFC1123_SUPPORTED
 151 #define PNG_tIME_STRING_LENGTH 29
 152 static int tIME_chunk_present = 0;
 153 static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
 154 
 155 #if PNG_LIBPNG_VER < 10619
 156 #define png_convert_to_rfc1123_buffer(ts, t) tIME_to_str(read_ptr, ts, t)
 157 
 158 static int
 159 tIME_to_str(png_structp png_ptr, png_charp ts, png_const_timep t)
 160 {
 161     png_const_charp str = png_convert_to_rfc1123(png_ptr, t);
 162 
 163     if (str == NULL)
 164         return 0;
 165 
 166     strcpy(ts, str);
 167     return 1;
 168 }
 169 #endif /* older libpng */
 170 #endif
 171 
 172 static int verbose = 0;
 173 static int strict = 0;
 174 static int relaxed = 0;
 175 static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */
 176 static int error_count = 0; /* count calls to png_error */
 177 static int warning_count = 0; /* count calls to png_warning */
 178 
 179 /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
 180 #ifndef png_jmpbuf
 181 #  define png_jmpbuf(png_ptr) png_ptr->jmpbuf
 182 #endif
 183 
 184 /* Defines for unknown chunk handling if required. */
 185 #ifndef PNG_HANDLE_CHUNK_ALWAYS
 186 #  define PNG_HANDLE_CHUNK_ALWAYS       3
 187 #endif
 188 #ifndef PNG_HANDLE_CHUNK_IF_SAFE
 189 #  define PNG_HANDLE_CHUNK_IF_SAFE      2
 190 #endif
 191 
 192 /* Utility to save typing/errors, the argument must be a name */
 193 #define MEMZERO(var) ((void)memset(&var, 0, sizeof var))
 194 
 195 /* Example of using row callbacks to make a simple progress meter */
 196 static int status_pass = 1;
 197 static int status_dots_requested = 0;
 198 static int status_dots = 1;
 199 
 200 static void PNGCBAPI
 201 read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
 202 {
 203    if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
 204       return;
 205 
 206    if (status_pass != pass)
 207    {
 208       fprintf(stdout, "\n Pass %d: ", pass);
 209       status_pass = pass;
 210       status_dots = 31;
 211    }
 212 
 213    status_dots--;
 214 
 215    if (status_dots == 0)
 216    {
 217       fprintf(stdout, "\n         ");
 218       status_dots=30;
 219    }
 220 
 221    fprintf(stdout, "r");
 222 }
 223 
 224 #ifdef PNG_WRITE_SUPPORTED
 225 static void PNGCBAPI
 226 write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
 227 {
 228    if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
 229       return;
 230 
 231    fprintf(stdout, "w");
 232 }
 233 #endif
 234 
 235 
 236 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
 237 /* Example of using a user transform callback (doesn't do anything at present).
 238  */
 239 static void PNGCBAPI
 240 read_user_callback(png_structp png_ptr, png_row_infop row_info, png_bytep data)
 241 {
 242    PNG_UNUSED(png_ptr)
 243    PNG_UNUSED(row_info)
 244    PNG_UNUSED(data)
 245 }
 246 #endif
 247 
 248 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
 249 /* Example of using user transform callback (we don't transform anything,
 250  * but merely count the zero samples)
 251  */
 252 
 253 static png_uint_32 zero_samples;
 254 
 255 static void PNGCBAPI
 256 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
 257 {
 258    png_bytep dp = data;
 259    if (png_ptr == NULL)
 260       return;
 261 
 262    /* Contents of row_info:
 263     *  png_uint_32 width      width of row
 264     *  png_uint_32 rowbytes   number of bytes in row
 265     *  png_byte color_type    color type of pixels
 266     *  png_byte bit_depth     bit depth of samples
 267     *  png_byte channels      number of channels (1-4)
 268     *  png_byte pixel_depth   bits per pixel (depth*channels)
 269     */
 270 
 271     /* Counts the number of zero samples (or zero pixels if color_type is 3 */
 272 
 273     if (row_info->color_type == 0 || row_info->color_type == 3)
 274     {
 275        int pos = 0;
 276        png_uint_32 n, nstop;
 277 
 278        for (n = 0, nstop=row_info->width; n<nstop; n++)
 279        {
 280           if (row_info->bit_depth == 1)
 281           {
 282              if (((*dp << pos++ ) & 0x80) == 0)
 283                 zero_samples++;
 284 
 285              if (pos == 8)
 286              {
 287                 pos = 0;
 288                 dp++;
 289              }
 290           }
 291 
 292           if (row_info->bit_depth == 2)
 293           {
 294              if (((*dp << (pos+=2)) & 0xc0) == 0)
 295                 zero_samples++;
 296 
 297              if (pos == 8)
 298              {
 299                 pos = 0;
 300                 dp++;
 301              }
 302           }
 303 
 304           if (row_info->bit_depth == 4)
 305           {
 306              if (((*dp << (pos+=4)) & 0xf0) == 0)
 307                 zero_samples++;
 308 
 309              if (pos == 8)
 310              {
 311                 pos = 0;
 312                 dp++;
 313              }
 314           }
 315 
 316           if (row_info->bit_depth == 8)
 317              if (*dp++ == 0)
 318                 zero_samples++;
 319 
 320           if (row_info->bit_depth == 16)
 321           {
 322              if ((*dp | *(dp+1)) == 0)
 323                 zero_samples++;
 324              dp+=2;
 325           }
 326        }
 327     }
 328     else /* Other color types */
 329     {
 330        png_uint_32 n, nstop;
 331        int channel;
 332        int color_channels = row_info->channels;
 333        if (row_info->color_type > 3)
 334           color_channels--;
 335 
 336        for (n = 0, nstop=row_info->width; n<nstop; n++)
 337        {
 338           for (channel = 0; channel < color_channels; channel++)
 339           {
 340              if (row_info->bit_depth == 8)
 341                 if (*dp++ == 0)
 342                    zero_samples++;
 343 
 344              if (row_info->bit_depth == 16)
 345              {
 346                 if ((*dp | *(dp+1)) == 0)
 347                    zero_samples++;
 348 
 349                 dp+=2;
 350              }
 351           }
 352           if (row_info->color_type > 3)
 353           {
 354              dp++;
 355              if (row_info->bit_depth == 16)
 356                 dp++;
 357           }
 358        }
 359     }
 360 }
 361 #endif /* WRITE_USER_TRANSFORM */
 362 
 363 #ifndef PNG_STDIO_SUPPORTED
 364 /* START of code to validate stdio-free compilation */
 365 /* These copies of the default read/write functions come from pngrio.c and
 366  * pngwio.c.  They allow "don't include stdio" testing of the library.
 367  * This is the function that does the actual reading of data.  If you are
 368  * not reading from a standard C stream, you should create a replacement
 369  * read_data function and use it at run time with png_set_read_fn(), rather
 370  * than changing the library.
 371  */
 372 
 373 #ifdef PNG_IO_STATE_SUPPORTED
 374 void
 375 pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
 376    png_uint_32 io_op);
 377 void
 378 pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
 379    png_uint_32 io_op)
 380 {
 381    png_uint_32 io_state = png_get_io_state(png_ptr);
 382    int err = 0;
 383 
 384    /* Check if the current operation (reading / writing) is as expected. */
 385    if ((io_state & PNG_IO_MASK_OP) != io_op)
 386       png_error(png_ptr, "Incorrect operation in I/O state");
 387 
 388    /* Check if the buffer size specific to the current location
 389     * (file signature / header / data / crc) is as expected.
 390     */
 391    switch (io_state & PNG_IO_MASK_LOC)
 392    {
 393    case PNG_IO_SIGNATURE:
 394       if (data_length > 8)
 395          err = 1;
 396       break;
 397    case PNG_IO_CHUNK_HDR:
 398       if (data_length != 8)
 399          err = 1;
 400       break;
 401    case PNG_IO_CHUNK_DATA:
 402       break;  /* no restrictions here */
 403    case PNG_IO_CHUNK_CRC:
 404       if (data_length != 4)
 405          err = 1;
 406       break;
 407    default:
 408       err = 1;  /* uninitialized */
 409    }
 410    if (err != 0)
 411       png_error(png_ptr, "Bad I/O state or buffer size");
 412 }
 413 #endif
 414 
 415 static void PNGCBAPI
 416 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
 417 {
 418    png_size_t check = 0;
 419    png_voidp io_ptr;
 420 
 421    /* fread() returns 0 on error, so it is OK to store this in a png_size_t
 422     * instead of an int, which is what fread() actually returns.
 423     */
 424    io_ptr = png_get_io_ptr(png_ptr);
 425    if (io_ptr != NULL)
 426    {
 427       check = fread(data, 1, length, (png_FILE_p)io_ptr);
 428    }
 429 
 430    if (check != length)
 431    {
 432       png_error(png_ptr, "Read Error");
 433    }
 434 
 435 #ifdef PNG_IO_STATE_SUPPORTED
 436    pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
 437 #endif
 438 }
 439 
 440 #ifdef PNG_WRITE_FLUSH_SUPPORTED
 441 static void PNGCBAPI
 442 pngtest_flush(png_structp png_ptr)
 443 {
 444    /* Do nothing; fflush() is said to be just a waste of energy. */
 445    PNG_UNUSED(png_ptr)   /* Stifle compiler warning */
 446 }
 447 #endif
 448 
 449 /* This is the function that does the actual writing of data.  If you are
 450  * not writing to a standard C stream, you should create a replacement
 451  * write_data function and use it at run time with png_set_write_fn(), rather
 452  * than changing the library.
 453  */
 454 static void PNGCBAPI
 455 pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
 456 {
 457    png_size_t check;
 458 
 459    check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
 460 
 461    if (check != length)
 462    {
 463       png_error(png_ptr, "Write Error");
 464    }
 465 
 466 #ifdef PNG_IO_STATE_SUPPORTED
 467    pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
 468 #endif
 469 }
 470 #endif /* !STDIO */
 471 
 472 /* This function is called when there is a warning, but the library thinks
 473  * it can continue anyway.  Replacement functions don't have to do anything
 474  * here if you don't want to.  In the default configuration, png_ptr is
 475  * not used, but it is passed in case it may be useful.
 476  */
 477 typedef struct
 478 {
 479    PNG_CONST char *file_name;
 480 }  pngtest_error_parameters;
 481 
 482 static void PNGCBAPI
 483 pngtest_warning(png_structp png_ptr, png_const_charp message)
 484 {
 485    PNG_CONST char *name = "UNKNOWN (ERROR!)";
 486    pngtest_error_parameters *test =
 487       (pngtest_error_parameters*)png_get_error_ptr(png_ptr);
 488 
 489    ++warning_count;
 490 
 491    if (test != NULL && test->file_name != NULL)
 492       name = test->file_name;
 493 
 494    fprintf(STDERR, "%s: libpng warning: %s\n", name, message);
 495 }
 496 
 497 /* This is the default error handling function.  Note that replacements for
 498  * this function MUST NOT RETURN, or the program will likely crash.  This
 499  * function is used by default, or if the program supplies NULL for the
 500  * error function pointer in png_set_error_fn().
 501  */
 502 static void PNGCBAPI
 503 pngtest_error(png_structp png_ptr, png_const_charp message)
 504 {
 505    ++error_count;
 506 
 507    pngtest_warning(png_ptr, message);
 508    /* We can return because png_error calls the default handler, which is
 509     * actually OK in this case.
 510     */
 511 }
 512 
 513 /* END of code to validate stdio-free compilation */
 514 
 515 /* START of code to validate memory allocation and deallocation */
 516 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
 517 
 518 /* Allocate memory.  For reasonable files, size should never exceed
 519  * 64K.  However, zlib may allocate more than 64K if you don't tell
 520  * it not to.  See zconf.h and png.h for more information.  zlib does
 521  * need to allocate exactly 64K, so whatever you call here must
 522  * have the ability to do that.
 523  *
 524  * This piece of code can be compiled to validate max 64K allocations
 525  * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
 526  */
 527 typedef struct memory_information
 528 {
 529    png_alloc_size_t          size;
 530    png_voidp                 pointer;
 531    struct memory_information *next;
 532 } memory_information;
 533 typedef memory_information *memory_infop;
 534 
 535 static memory_infop pinformation = NULL;
 536 static int current_allocation = 0;
 537 static int maximum_allocation = 0;
 538 static int total_allocation = 0;
 539 static int num_allocations = 0;
 540 
 541 png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
 542     png_alloc_size_t size));
 543 void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
 544 
 545 png_voidp
 546 PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
 547 {
 548 
 549    /* png_malloc has already tested for NULL; png_create_struct calls
 550     * png_debug_malloc directly, with png_ptr == NULL which is OK
 551     */
 552 
 553    if (size == 0)
 554       return (NULL);
 555 
 556    /* This calls the library allocator twice, once to get the requested
 557       buffer and once to get a new free list entry. */
 558    {
 559       /* Disable malloc_fn and free_fn */
 560       memory_infop pinfo;
 561       png_set_mem_fn(png_ptr, NULL, NULL, NULL);
 562       pinfo = (memory_infop)png_malloc(png_ptr,
 563          (sizeof *pinfo));
 564       pinfo->size = size;
 565       current_allocation += size;
 566       total_allocation += size;
 567       num_allocations ++;
 568 
 569       if (current_allocation > maximum_allocation)
 570          maximum_allocation = current_allocation;
 571 
 572       pinfo->pointer = png_malloc(png_ptr, size);
 573       /* Restore malloc_fn and free_fn */
 574 
 575       png_set_mem_fn(png_ptr,
 576           NULL, png_debug_malloc, png_debug_free);
 577 
 578       if (size != 0 && pinfo->pointer == NULL)
 579       {
 580          current_allocation -= size;
 581          total_allocation -= size;
 582          png_error(png_ptr,
 583            "out of memory in pngtest->png_debug_malloc");
 584       }
 585 
 586       pinfo->next = pinformation;
 587       pinformation = pinfo;
 588       /* Make sure the caller isn't assuming zeroed memory. */
 589       memset(pinfo->pointer, 0xdd, pinfo->size);
 590 
 591       if (verbose != 0)
 592          printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
 593             pinfo->pointer);
 594 
 595       return (png_voidp)(pinfo->pointer);
 596    }
 597 }
 598 
 599 /* Free a pointer.  It is removed from the list at the same time. */
 600 void PNGCBAPI
 601 png_debug_free(png_structp png_ptr, png_voidp ptr)
 602 {
 603    if (png_ptr == NULL)
 604       fprintf(STDERR, "NULL pointer to png_debug_free.\n");
 605 
 606    if (ptr == 0)
 607    {
 608 #if 0 /* This happens all the time. */
 609       fprintf(STDERR, "WARNING: freeing NULL pointer\n");
 610 #endif
 611       return;
 612    }
 613 
 614    /* Unlink the element from the list. */
 615    if (pinformation != NULL)
 616    {
 617       memory_infop *ppinfo = &pinformation;
 618 
 619       for (;;)
 620       {
 621          memory_infop pinfo = *ppinfo;
 622 
 623          if (pinfo->pointer == ptr)
 624          {
 625             *ppinfo = pinfo->next;
 626             current_allocation -= pinfo->size;
 627             if (current_allocation < 0)
 628                fprintf(STDERR, "Duplicate free of memory\n");
 629             /* We must free the list element too, but first kill
 630                the memory that is to be freed. */
 631             memset(ptr, 0x55, pinfo->size);
 632             free(pinfo);
 633             pinfo = NULL;
 634             break;
 635          }
 636 
 637          if (pinfo->next == NULL)
 638          {
 639             fprintf(STDERR, "Pointer %p not found\n", ptr);
 640             break;
 641          }
 642 
 643          ppinfo = &pinfo->next;
 644       }
 645    }
 646 
 647    /* Finally free the data. */
 648    if (verbose != 0)
 649       printf("Freeing %p\n", ptr);
 650 
 651    if (ptr != NULL)
 652       free(ptr);
 653    ptr = NULL;
 654 }
 655 #endif /* USER_MEM && DEBUG */
 656 /* END of code to test memory allocation/deallocation */
 657 
 658 
 659 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
 660 /* Demonstration of user chunk support of the sTER and vpAg chunks */
 661 
 662 /* (sTER is a public chunk not yet known by libpng.  vpAg is a private
 663 chunk used in ImageMagick to store "virtual page" size).  */
 664 
 665 static struct user_chunk_data
 666 {
 667    png_const_infop info_ptr;
 668    png_uint_32     vpAg_width, vpAg_height;
 669    png_byte        vpAg_units;
 670    png_byte        sTER_mode;
 671    int             location[2];
 672 }
 673 user_chunk_data;
 674 
 675 /* Used for location and order; zero means nothing. */
 676 #define have_sTER   0x01
 677 #define have_vpAg   0x02
 678 #define before_PLTE 0x10
 679 #define before_IDAT 0x20
 680 #define after_IDAT  0x40
 681 
 682 static void
 683 init_callback_info(png_const_infop info_ptr)
 684 {
 685    MEMZERO(user_chunk_data);
 686    user_chunk_data.info_ptr = info_ptr;
 687 }
 688 
 689 static int
 690 set_location(png_structp png_ptr, struct user_chunk_data *data, int what)
 691 {
 692    int location;
 693 
 694    if ((data->location[0] & what) != 0 || (data->location[1] & what) != 0)
 695       return 0; /* already have one of these */
 696 
 697    /* Find where we are (the code below zeroes info_ptr to indicate that the
 698     * chunks before the first IDAT have been read.)
 699     */
 700    if (data->info_ptr == NULL) /* after IDAT */
 701       location = what | after_IDAT;
 702 
 703    else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE) != 0)
 704       location = what | before_IDAT;
 705 
 706    else
 707       location = what | before_PLTE;
 708 
 709    if (data->location[0] == 0)
 710       data->location[0] = location;
 711 
 712    else
 713       data->location[1] = location;
 714 
 715    return 1; /* handled */
 716 }
 717 
 718 static int PNGCBAPI
 719 read_user_chunk_callback(png_struct *png_ptr, png_unknown_chunkp chunk)
 720 {
 721    struct user_chunk_data *my_user_chunk_data =
 722       (struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr);
 723 
 724    if (my_user_chunk_data == NULL)
 725       png_error(png_ptr, "lost user chunk pointer");
 726 
 727    /* Return one of the following:
 728     *    return (-n);  chunk had an error
 729     *    return (0);  did not recognize
 730     *    return (n);  success
 731     *
 732     * The unknown chunk structure contains the chunk data:
 733     * png_byte name[5];
 734     * png_byte *data;
 735     * png_size_t size;
 736     *
 737     * Note that libpng has already taken care of the CRC handling.
 738     */
 739 
 740    if (chunk->name[0] == 115 && chunk->name[1] ==  84 &&     /* s  T */
 741        chunk->name[2] ==  69 && chunk->name[3] ==  82)       /* E  R */
 742       {
 743          /* Found sTER chunk */
 744          if (chunk->size != 1)
 745             return (-1); /* Error return */
 746 
 747          if (chunk->data[0] != 0 && chunk->data[0] != 1)
 748             return (-1);  /* Invalid mode */
 749 
 750          if (set_location(png_ptr, my_user_chunk_data, have_sTER) != 0)
 751          {
 752             my_user_chunk_data->sTER_mode=chunk->data[0];
 753             return (1);
 754          }
 755 
 756          else
 757             return (0); /* duplicate sTER - give it to libpng */
 758       }
 759 
 760    if (chunk->name[0] != 118 || chunk->name[1] != 112 ||    /* v  p */
 761        chunk->name[2] !=  65 || chunk->name[3] != 103)      /* A  g */
 762       return (0); /* Did not recognize */
 763 
 764    /* Found ImageMagick vpAg chunk */
 765 
 766    if (chunk->size != 9)
 767       return (-1); /* Error return */
 768 
 769    if (set_location(png_ptr, my_user_chunk_data, have_vpAg) == 0)
 770       return (0);  /* duplicate vpAg */
 771 
 772    my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data);
 773    my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4);
 774    my_user_chunk_data->vpAg_units = chunk->data[8];
 775 
 776    return (1);
 777 }
 778 
 779 #ifdef PNG_WRITE_SUPPORTED
 780 static void
 781 write_sTER_chunk(png_structp write_ptr)
 782 {
 783    png_byte sTER[5] = {115,  84,  69,  82, '\0'};
 784 
 785    if (verbose != 0)
 786       fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
 787 
 788    png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);
 789 }
 790 
 791 static void
 792 write_vpAg_chunk(png_structp write_ptr)
 793 {
 794    png_byte vpAg[5] = {118, 112,  65, 103, '\0'};
 795 
 796    png_byte vpag_chunk_data[9];
 797 
 798    if (verbose != 0)
 799       fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",
 800         (unsigned long)user_chunk_data.vpAg_width,
 801         (unsigned long)user_chunk_data.vpAg_height,
 802         user_chunk_data.vpAg_units);
 803 
 804    png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);
 805    png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);
 806    vpag_chunk_data[8] = user_chunk_data.vpAg_units;
 807    png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);
 808 }
 809 
 810 static void
 811 write_chunks(png_structp write_ptr, int location)
 812 {
 813    int i;
 814 
 815    /* Notice that this preserves the original chunk order, however chunks
 816     * intercepted by the callback will be written *after* chunks passed to
 817     * libpng.  This will actually reverse a pair of sTER chunks or a pair of
 818     * vpAg chunks, resulting in an error later.  This is not worth worrying
 819     * about - the chunks should not be duplicated!
 820     */
 821    for (i=0; i<2; ++i)
 822    {
 823       if (user_chunk_data.location[i] == (location | have_sTER))
 824          write_sTER_chunk(write_ptr);
 825 
 826       else if (user_chunk_data.location[i] == (location | have_vpAg))
 827          write_vpAg_chunk(write_ptr);
 828    }
 829 }
 830 #endif /* WRITE */
 831 #else /* !READ_USER_CHUNKS */
 832 #  define write_chunks(pp,loc) ((void)0)
 833 #endif
 834 /* END of code to demonstrate user chunk support */
 835 
 836 /* START of code to check that libpng has the required text support; this only
 837  * checks for the write support because if read support is missing the chunk
 838  * will simply not be reported back to pngtest.
 839  */
 840 #ifdef PNG_TEXT_SUPPORTED
 841 static void
 842 pngtest_check_text_support(png_structp png_ptr, png_textp text_ptr,
 843    int num_text)
 844 {
 845    while (num_text > 0)
 846    {
 847       switch (text_ptr[--num_text].compression)
 848       {
 849          case PNG_TEXT_COMPRESSION_NONE:
 850             break;
 851 
 852          case PNG_TEXT_COMPRESSION_zTXt:
 853 #           ifndef PNG_WRITE_zTXt_SUPPORTED
 854                ++unsupported_chunks;
 855                /* In libpng 1.7 this now does an app-error, so stop it: */
 856                text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
 857 #           endif
 858             break;
 859 
 860          case PNG_ITXT_COMPRESSION_NONE:
 861          case PNG_ITXT_COMPRESSION_zTXt:
 862 #           ifndef PNG_WRITE_iTXt_SUPPORTED
 863                ++unsupported_chunks;
 864                text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
 865 #           endif
 866             break;
 867 
 868          default:
 869             /* This is an error */
 870             png_error(png_ptr, "invalid text chunk compression field");
 871             break;
 872       }
 873    }
 874 }
 875 #endif
 876 /* END of code to check that libpng has the required text support */
 877 
 878 /* Test one file */
 879 static int
 880 test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)
 881 {
 882    static png_FILE_p fpin;
 883    static png_FILE_p fpout;  /* "static" prevents setjmp corruption */
 884    pngtest_error_parameters error_parameters;
 885    png_structp read_ptr;
 886    png_infop read_info_ptr, end_info_ptr;
 887 #ifdef PNG_WRITE_SUPPORTED
 888    png_structp write_ptr;
 889    png_infop write_info_ptr;
 890    png_infop write_end_info_ptr;
 891 #ifdef PNG_WRITE_FILTER_SUPPORTED
 892    int interlace_preserved = 1;
 893 #endif /* WRITE_FILTER */
 894 #else /* !WRITE */
 895    png_structp write_ptr = NULL;
 896    png_infop write_info_ptr = NULL;
 897    png_infop write_end_info_ptr = NULL;
 898 #endif /* !WRITE */
 899    png_bytep row_buf;
 900    png_uint_32 y;
 901    png_uint_32 width, height;
 902    volatile int num_passes;
 903    int pass;
 904    int bit_depth, color_type;
 905 
 906    row_buf = NULL;
 907    error_parameters.file_name = inname;
 908 
 909    if ((fpin = fopen(inname, "rb")) == NULL)
 910    {
 911       fprintf(STDERR, "Could not find input file %s\n", inname);
 912       return (1);
 913    }
 914 
 915    if ((fpout = fopen(outname, "wb")) == NULL)
 916    {
 917       fprintf(STDERR, "Could not open output file %s\n", outname);
 918       FCLOSE(fpin);
 919       return (1);
 920    }
 921 
 922    pngtest_debug("Allocating read and write structures");
 923 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
 924    read_ptr =
 925       png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
 926       NULL, NULL, NULL, png_debug_malloc, png_debug_free);
 927 #else
 928    read_ptr =
 929       png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
 930 #endif
 931    png_set_error_fn(read_ptr, &error_parameters, pngtest_error,
 932       pngtest_warning);
 933 
 934 #ifdef PNG_WRITE_SUPPORTED
 935 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
 936    write_ptr =
 937       png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
 938       NULL, NULL, NULL, png_debug_malloc, png_debug_free);
 939 #else
 940    write_ptr =
 941       png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
 942 #endif
 943    png_set_error_fn(write_ptr, &error_parameters, pngtest_error,
 944       pngtest_warning);
 945 #endif
 946    pngtest_debug("Allocating read_info, write_info and end_info structures");
 947    read_info_ptr = png_create_info_struct(read_ptr);
 948    end_info_ptr = png_create_info_struct(read_ptr);
 949 #ifdef PNG_WRITE_SUPPORTED
 950    write_info_ptr = png_create_info_struct(write_ptr);
 951    write_end_info_ptr = png_create_info_struct(write_ptr);
 952 #endif
 953 
 954 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
 955    init_callback_info(read_info_ptr);
 956    png_set_read_user_chunk_fn(read_ptr, &user_chunk_data,
 957      read_user_chunk_callback);
 958 #endif
 959 
 960 #ifdef PNG_SETJMP_SUPPORTED
 961    pngtest_debug("Setting jmpbuf for read struct");
 962    if (setjmp(png_jmpbuf(read_ptr)))
 963    {
 964       fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
 965       png_free(read_ptr, row_buf);
 966       row_buf = NULL;
 967       png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
 968 #ifdef PNG_WRITE_SUPPORTED
 969       png_destroy_info_struct(write_ptr, &write_end_info_ptr);
 970       png_destroy_write_struct(&write_ptr, &write_info_ptr);
 971 #endif
 972       FCLOSE(fpin);
 973       FCLOSE(fpout);
 974       return (1);
 975    }
 976 
 977 #ifdef PNG_WRITE_SUPPORTED
 978    pngtest_debug("Setting jmpbuf for write struct");
 979 
 980    if (setjmp(png_jmpbuf(write_ptr)))
 981    {
 982       fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
 983       png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
 984       png_destroy_info_struct(write_ptr, &write_end_info_ptr);
 985 #ifdef PNG_WRITE_SUPPORTED
 986       png_destroy_write_struct(&write_ptr, &write_info_ptr);
 987 #endif
 988       FCLOSE(fpin);
 989       FCLOSE(fpout);
 990       return (1);
 991    }
 992 #endif
 993 #endif
 994 
 995    if (strict != 0)
 996    {
 997       /* Treat png_benign_error() as errors on read */
 998       png_set_benign_errors(read_ptr, 0);
 999 
1000 #ifdef PNG_WRITE_SUPPORTED
1001       /* Treat them as errors on write */
1002       png_set_benign_errors(write_ptr, 0);
1003 #endif
1004 
1005       /* if strict is not set, then app warnings and errors are treated as
1006        * warnings in release builds, but not in unstable builds; this can be
1007        * changed with '--relaxed'.
1008        */
1009    }
1010 
1011    else if (relaxed != 0)
1012    {
1013       /* Allow application (pngtest) errors and warnings to pass */
1014       png_set_benign_errors(read_ptr, 1);
1015 
1016 #ifdef PNG_WRITE_SUPPORTED
1017       png_set_benign_errors(write_ptr, 1);
1018 #endif
1019    }
1020 
1021    pngtest_debug("Initializing input and output streams");
1022 #ifdef PNG_STDIO_SUPPORTED
1023    png_init_io(read_ptr, fpin);
1024 #  ifdef PNG_WRITE_SUPPORTED
1025    png_init_io(write_ptr, fpout);
1026 #  endif
1027 #else
1028    png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
1029 #  ifdef PNG_WRITE_SUPPORTED
1030    png_set_write_fn(write_ptr, (png_voidp)fpout,  pngtest_write_data,
1031 #    ifdef PNG_WRITE_FLUSH_SUPPORTED
1032       pngtest_flush);
1033 #    else
1034       NULL);
1035 #    endif
1036 #  endif
1037 #endif
1038 
1039    if (status_dots_requested == 1)
1040    {
1041 #ifdef PNG_WRITE_SUPPORTED
1042       png_set_write_status_fn(write_ptr, write_row_callback);
1043 #endif
1044       png_set_read_status_fn(read_ptr, read_row_callback);
1045    }
1046 
1047    else
1048    {
1049 #ifdef PNG_WRITE_SUPPORTED
1050       png_set_write_status_fn(write_ptr, NULL);
1051 #endif
1052       png_set_read_status_fn(read_ptr, NULL);
1053    }
1054 
1055 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1056    png_set_read_user_transform_fn(read_ptr, read_user_callback);
1057 #endif
1058 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1059    zero_samples = 0;
1060    png_set_write_user_transform_fn(write_ptr, count_zero_samples);
1061 #endif
1062 
1063 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1064    /* Preserve all the unknown chunks, if possible.  If this is disabled then,
1065     * even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use
1066     * libpng to *save* the unknown chunks on read (because we can't switch the
1067     * save option on!)
1068     *
1069     * Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all
1070     * unknown chunks and write will write them all.
1071     */
1072 #ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
1073    png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1074       NULL, 0);
1075 #endif
1076 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1077    png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
1078       NULL, 0);
1079 #endif
1080 #endif
1081 
1082    pngtest_debug("Reading info struct");
1083    png_read_info(read_ptr, read_info_ptr);
1084 
1085 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1086    /* This is a bit of a hack; there is no obvious way in the callback function
1087     * to determine that the chunks before the first IDAT have been read, so
1088     * remove the info_ptr (which is only used to determine position relative to
1089     * PLTE) here to indicate that we are after the IDAT.
1090     */
1091    user_chunk_data.info_ptr = NULL;
1092 #endif
1093 
1094    pngtest_debug("Transferring info struct");
1095    {
1096       int interlace_type, compression_type, filter_type;
1097 
1098       if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
1099           &color_type, &interlace_type, &compression_type, &filter_type) != 0)
1100       {
1101          png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
1102             color_type, interlace_type, compression_type, filter_type);
1103          /* num_passes may not be available below if interlace support is not
1104           * provided by libpng for both read and write.
1105           */
1106          switch (interlace_type)
1107          {
1108             case PNG_INTERLACE_NONE:
1109                num_passes = 1;
1110                break;
1111 
1112             case PNG_INTERLACE_ADAM7:
1113                num_passes = 7;
1114                break;
1115 
1116             default:
1117                png_error(read_ptr, "invalid interlace type");
1118                /*NOT REACHED*/
1119          }
1120       }
1121 
1122       else
1123          png_error(read_ptr, "png_get_IHDR failed");
1124    }
1125 #ifdef PNG_FIXED_POINT_SUPPORTED
1126 #ifdef PNG_cHRM_SUPPORTED
1127    {
1128       png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1129          blue_y;
1130 
1131       if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
1132          &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
1133       {
1134          png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
1135             red_y, green_x, green_y, blue_x, blue_y);
1136       }
1137    }
1138 #endif
1139 #ifdef PNG_gAMA_SUPPORTED
1140    {
1141       png_fixed_point gamma;
1142 
1143       if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma) != 0)
1144          png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
1145    }
1146 #endif
1147 #else /* Use floating point versions */
1148 #ifdef PNG_FLOATING_POINT_SUPPORTED
1149 #ifdef PNG_cHRM_SUPPORTED
1150    {
1151       double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
1152          blue_y;
1153 
1154       if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
1155          &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
1156       {
1157          png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
1158             red_y, green_x, green_y, blue_x, blue_y);
1159       }
1160    }
1161 #endif
1162 #ifdef PNG_gAMA_SUPPORTED
1163    {
1164       double gamma;
1165 
1166       if (png_get_gAMA(read_ptr, read_info_ptr, &gamma) != 0)
1167          png_set_gAMA(write_ptr, write_info_ptr, gamma);
1168    }
1169 #endif
1170 #endif /* Floating point */
1171 #endif /* Fixed point */
1172 #ifdef PNG_iCCP_SUPPORTED
1173    {
1174       png_charp name;
1175       png_bytep profile;
1176       png_uint_32 proflen;
1177       int compression_type;
1178 
1179       if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
1180                       &profile, &proflen) != 0)
1181       {
1182          png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
1183                       profile, proflen);
1184       }
1185    }
1186 #endif
1187 #ifdef PNG_sRGB_SUPPORTED
1188    {
1189       int intent;
1190 
1191       if (png_get_sRGB(read_ptr, read_info_ptr, &intent) != 0)
1192          png_set_sRGB(write_ptr, write_info_ptr, intent);
1193    }
1194 #endif
1195    {
1196       png_colorp palette;
1197       int num_palette;
1198 
1199       if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette) != 0)
1200          png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
1201    }
1202 #ifdef PNG_bKGD_SUPPORTED
1203    {
1204       png_color_16p background;
1205 
1206       if (png_get_bKGD(read_ptr, read_info_ptr, &background) != 0)
1207       {
1208          png_set_bKGD(write_ptr, write_info_ptr, background);
1209       }
1210    }
1211 #endif
1212 #ifdef PNG_hIST_SUPPORTED
1213    {
1214       png_uint_16p hist;
1215 
1216       if (png_get_hIST(read_ptr, read_info_ptr, &hist) != 0)
1217          png_set_hIST(write_ptr, write_info_ptr, hist);
1218    }
1219 #endif
1220 #ifdef PNG_oFFs_SUPPORTED
1221    {
1222       png_int_32 offset_x, offset_y;
1223       int unit_type;
1224 
1225       if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
1226           &unit_type) != 0)
1227       {
1228          png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
1229       }
1230    }
1231 #endif
1232 #ifdef PNG_pCAL_SUPPORTED
1233    {
1234       png_charp purpose, units;
1235       png_charpp params;
1236       png_int_32 X0, X1;
1237       int type, nparams;
1238 
1239       if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
1240          &nparams, &units, &params) != 0)
1241       {
1242          png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
1243             nparams, units, params);
1244       }
1245    }
1246 #endif
1247 #ifdef PNG_pHYs_SUPPORTED
1248    {
1249       png_uint_32 res_x, res_y;
1250       int unit_type;
1251 
1252       if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y,
1253           &unit_type) != 0)
1254          png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
1255    }
1256 #endif
1257 #ifdef PNG_sBIT_SUPPORTED
1258    {
1259       png_color_8p sig_bit;
1260 
1261       if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit) != 0)
1262          png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
1263    }
1264 #endif
1265 #ifdef PNG_sCAL_SUPPORTED
1266 #if defined(PNG_FLOATING_POINT_SUPPORTED) && \
1267    defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
1268    {
1269       int unit;
1270       double scal_width, scal_height;
1271 
1272       if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
1273          &scal_height) != 0)
1274       {
1275          png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
1276       }
1277    }
1278 #else
1279 #ifdef PNG_FIXED_POINT_SUPPORTED
1280    {
1281       int unit;
1282       png_charp scal_width, scal_height;
1283 
1284       if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
1285           &scal_height) != 0)
1286       {
1287          png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1288              scal_height);
1289       }
1290    }
1291 #endif
1292 #endif
1293 #endif
1294 
1295 #ifdef PNG_sPLT_SUPPORTED
1296    {
1297        png_sPLT_tp entries;
1298 
1299        int num_entries = (int) png_get_sPLT(read_ptr, read_info_ptr, &entries);
1300        if (num_entries)
1301        {
1302            png_set_sPLT(write_ptr, write_info_ptr, entries, num_entries);
1303        }
1304    }
1305 #endif
1306 
1307 #ifdef PNG_TEXT_SUPPORTED
1308    {
1309       png_textp text_ptr;
1310       int num_text;
1311 
1312       if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1313       {
1314          pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1315 
1316          pngtest_check_text_support(read_ptr, text_ptr, num_text);
1317 
1318          if (verbose != 0)
1319          {
1320             int i;
1321 
1322             printf("\n");
1323             for (i=0; i<num_text; i++)
1324             {
1325                printf("   Text compression[%d]=%d\n",
1326                      i, text_ptr[i].compression);
1327             }
1328          }
1329 
1330          png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1331       }
1332    }
1333 #endif
1334 #ifdef PNG_tIME_SUPPORTED
1335    {
1336       png_timep mod_time;
1337 
1338       if (png_get_tIME(read_ptr, read_info_ptr, &mod_time) != 0)
1339       {
1340          png_set_tIME(write_ptr, write_info_ptr, mod_time);
1341 #ifdef PNG_TIME_RFC1123_SUPPORTED
1342          if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
1343             tIME_string[(sizeof tIME_string) - 1] = '\0';
1344 
1345          else
1346          {
1347             strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
1348             tIME_string[(sizeof tIME_string) - 1] = '\0';
1349          }
1350 
1351          tIME_chunk_present++;
1352 #endif /* TIME_RFC1123 */
1353       }
1354    }
1355 #endif
1356 #ifdef PNG_tRNS_SUPPORTED
1357    {
1358       png_bytep trans_alpha;
1359       int num_trans;
1360       png_color_16p trans_color;
1361 
1362       if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
1363          &trans_color) != 0)
1364       {
1365          int sample_max = (1 << bit_depth);
1366          /* libpng doesn't reject a tRNS chunk with out-of-range samples */
1367          if (!((color_type == PNG_COLOR_TYPE_GRAY &&
1368              (int)trans_color->gray > sample_max) ||
1369              (color_type == PNG_COLOR_TYPE_RGB &&
1370              ((int)trans_color->red > sample_max ||
1371              (int)trans_color->green > sample_max ||
1372              (int)trans_color->blue > sample_max))))
1373             png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
1374                trans_color);
1375       }
1376    }
1377 #endif
1378 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1379    {
1380       png_unknown_chunkp unknowns;
1381       int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
1382          &unknowns);
1383 
1384       if (num_unknowns != 0)
1385       {
1386          png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
1387            num_unknowns);
1388 #if PNG_LIBPNG_VER < 10600
1389          /* Copy the locations from the read_info_ptr.  The automatically
1390           * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1391           * because they are reset from the write pointer (removed in 1.6.0).
1392           */
1393          {
1394             int i;
1395             for (i = 0; i < num_unknowns; i++)
1396               png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
1397                 unknowns[i].location);
1398          }
1399 #endif
1400       }
1401    }
1402 #endif
1403 
1404 #ifdef PNG_WRITE_SUPPORTED
1405    pngtest_debug("Writing info struct");
1406 
1407    /* Write the info in two steps so that if we write the 'unknown' chunks here
1408     * they go to the correct place.
1409     */
1410    png_write_info_before_PLTE(write_ptr, write_info_ptr);
1411 
1412    write_chunks(write_ptr, before_PLTE); /* before PLTE */
1413 
1414    png_write_info(write_ptr, write_info_ptr);
1415 
1416    write_chunks(write_ptr, before_IDAT); /* after PLTE */
1417 #endif
1418 
1419 #ifdef SINGLE_ROWBUF_ALLOC
1420    pngtest_debug("Allocating row buffer...");
1421    row_buf = (png_bytep)png_malloc(read_ptr,
1422       png_get_rowbytes(read_ptr, read_info_ptr));
1423 
1424    pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);
1425 #endif /* SINGLE_ROWBUF_ALLOC */
1426    pngtest_debug("Writing row data");
1427 
1428 #if defined(PNG_READ_INTERLACING_SUPPORTED) &&\
1429    defined(PNG_WRITE_INTERLACING_SUPPORTED)
1430    /* Both must be defined for libpng to be able to handle the interlace,
1431     * otherwise it gets handled below by simply reading and writing the passes
1432     * directly.
1433     */
1434    if (png_set_interlace_handling(read_ptr) != num_passes)
1435       png_error(write_ptr,
1436             "png_set_interlace_handling(read): wrong pass count ");
1437    if (png_set_interlace_handling(write_ptr) != num_passes)
1438       png_error(write_ptr,
1439             "png_set_interlace_handling(write): wrong pass count ");
1440 #else /* png_set_interlace_handling not called on either read or write */
1441 #  define calc_pass_height
1442 #endif /* not using libpng interlace handling */
1443 
1444 #ifdef PNGTEST_TIMING
1445    t_stop = (float)clock();
1446    t_misc += (t_stop - t_start);
1447    t_start = t_stop;
1448 #endif
1449    for (pass = 0; pass < num_passes; pass++)
1450    {
1451 #     ifdef calc_pass_height
1452          png_uint_32 pass_height;
1453 
1454          if (num_passes == 7) /* interlaced */
1455          {
1456             if (PNG_PASS_COLS(width, pass) > 0)
1457                pass_height = PNG_PASS_ROWS(height, pass);
1458 
1459             else
1460                pass_height = 0;
1461          }
1462 
1463          else /* not interlaced */
1464             pass_height = height;
1465 #     else
1466 #        define pass_height height
1467 #     endif
1468 
1469       pngtest_debug1("Writing row data for pass %d", pass);
1470       for (y = 0; y < pass_height; y++)
1471       {
1472 #ifndef SINGLE_ROWBUF_ALLOC
1473          pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
1474 
1475          row_buf = (png_bytep)png_malloc(read_ptr,
1476             png_get_rowbytes(read_ptr, read_info_ptr));
1477 
1478          pngtest_debug2("\t0x%08lx (%lu bytes)", (unsigned long)row_buf,
1479             (unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));
1480 
1481 #endif /* !SINGLE_ROWBUF_ALLOC */
1482          png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
1483 
1484 #ifdef PNG_WRITE_SUPPORTED
1485 #ifdef PNGTEST_TIMING
1486          t_stop = (float)clock();
1487          t_decode += (t_stop - t_start);
1488          t_start = t_stop;
1489 #endif
1490          png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1491 #ifdef PNGTEST_TIMING
1492          t_stop = (float)clock();
1493          t_encode += (t_stop - t_start);
1494          t_start = t_stop;
1495 #endif
1496 #endif /* WRITE */
1497 
1498 #ifndef SINGLE_ROWBUF_ALLOC
1499          pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
1500          png_free(read_ptr, row_buf);
1501          row_buf = NULL;
1502 #endif /* !SINGLE_ROWBUF_ALLOC */
1503       }
1504    }
1505 
1506 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1507 #  ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1508       png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
1509 #  endif
1510 #  ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1511       png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
1512 #  endif
1513 #endif
1514 
1515    pngtest_debug("Reading and writing end_info data");
1516 
1517    png_read_end(read_ptr, end_info_ptr);
1518 #ifdef PNG_TEXT_SUPPORTED
1519    {
1520       png_textp text_ptr;
1521       int num_text;
1522 
1523       if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1524       {
1525          pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1526 
1527          pngtest_check_text_support(read_ptr, text_ptr, num_text);
1528 
1529          if (verbose != 0)
1530          {
1531             int i;
1532 
1533             printf("\n");
1534             for (i=0; i<num_text; i++)
1535             {
1536                printf("   Text compression[%d]=%d\n",
1537                      i, text_ptr[i].compression);
1538             }
1539          }
1540 
1541          png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1542       }
1543    }
1544 #endif
1545 #ifdef PNG_tIME_SUPPORTED
1546    {
1547       png_timep mod_time;
1548 
1549       if (png_get_tIME(read_ptr, end_info_ptr, &mod_time) != 0)
1550       {
1551          png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
1552 #ifdef PNG_TIME_RFC1123_SUPPORTED
1553          if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
1554             tIME_string[(sizeof tIME_string) - 1] = '\0';
1555 
1556          else
1557          {
1558             strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
1559             tIME_string[(sizeof tIME_string)-1] = '\0';
1560          }
1561 
1562          tIME_chunk_present++;
1563 #endif /* TIME_RFC1123 */
1564       }
1565    }
1566 #endif
1567 #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1568    {
1569       png_unknown_chunkp unknowns;
1570       int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
1571          &unknowns);
1572 
1573       if (num_unknowns != 0)
1574       {
1575          png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
1576            num_unknowns);
1577 #if PNG_LIBPNG_VER < 10600
1578          /* Copy the locations from the read_info_ptr.  The automatically
1579           * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1580           * because they are reset from the write pointer (removed in 1.6.0).
1581           */
1582          {
1583             int i;
1584             for (i = 0; i < num_unknowns; i++)
1585               png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
1586                 unknowns[i].location);
1587          }
1588 #endif
1589       }
1590    }
1591 #endif
1592 
1593 #ifdef PNG_WRITE_SUPPORTED
1594 #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1595    /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
1596     * This is here just to make pngtest replicate the results from libpng
1597     * versions prior to 1.5.4, and to test this new API.
1598     */
1599    png_set_text_compression_strategy(write_ptr, Z_FILTERED);
1600 #endif
1601 
1602    /* When the unknown vpAg/sTER chunks are written by pngtest the only way to
1603     * do it is to write them *before* calling png_write_end.  When unknown
1604     * chunks are written by libpng, however, they are written just before IEND.
1605     * There seems to be no way round this, however vpAg/sTER are not expected
1606     * after IDAT.
1607     */
1608    write_chunks(write_ptr, after_IDAT);
1609 
1610    png_write_end(write_ptr, write_end_info_ptr);
1611 #endif
1612 
1613 #ifdef PNG_EASY_ACCESS_SUPPORTED
1614    if (verbose != 0)
1615    {
1616       png_uint_32 iwidth, iheight;
1617       iwidth = png_get_image_width(write_ptr, write_info_ptr);
1618       iheight = png_get_image_height(write_ptr, write_info_ptr);
1619       fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
1620          (unsigned long)iwidth, (unsigned long)iheight);
1621    }
1622 #endif
1623 
1624    pngtest_debug("Destroying data structs");
1625 #ifdef SINGLE_ROWBUF_ALLOC
1626    pngtest_debug("destroying row_buf for read_ptr");
1627    png_free(read_ptr, row_buf);
1628    row_buf = NULL;
1629 #endif /* SINGLE_ROWBUF_ALLOC */
1630    pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
1631    png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
1632 #ifdef PNG_WRITE_SUPPORTED
1633    pngtest_debug("destroying write_end_info_ptr");
1634    png_destroy_info_struct(write_ptr, &write_end_info_ptr);
1635    pngtest_debug("destroying write_ptr, write_info_ptr");
1636    png_destroy_write_struct(&write_ptr, &write_info_ptr);
1637 #endif
1638    pngtest_debug("Destruction complete.");
1639 
1640    FCLOSE(fpin);
1641    FCLOSE(fpout);
1642 
1643    /* Summarize any warnings or errors and in 'strict' mode fail the test.
1644     * Unsupported chunks can result in warnings, in that case ignore the strict
1645     * setting, otherwise fail the test on warnings as well as errors.
1646     */
1647    if (error_count > 0)
1648    {
1649       /* We don't really expect to get here because of the setjmp handling
1650        * above, but this is safe.
1651        */
1652       fprintf(STDERR, "\n  %s: %d libpng errors found (%d warnings)",
1653          inname, error_count, warning_count);
1654 
1655       if (strict != 0)
1656          return (1);
1657    }
1658 
1659 #  ifdef PNG_WRITE_SUPPORTED
1660       /* If there is no write support nothing was written! */
1661       else if (unsupported_chunks > 0)
1662       {
1663          fprintf(STDERR, "\n  %s: unsupported chunks (%d)%s",
1664             inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
1665       }
1666 #  endif
1667 
1668    else if (warning_count > 0)
1669    {
1670       fprintf(STDERR, "\n  %s: %d libpng warnings found",
1671          inname, warning_count);
1672 
1673       if (strict != 0)
1674          return (1);
1675    }
1676 
1677    pngtest_debug("Opening files for comparison");
1678    if ((fpin = fopen(inname, "rb")) == NULL)
1679    {
1680       fprintf(STDERR, "Could not find file %s\n", inname);
1681       return (1);
1682    }
1683 
1684    if ((fpout = fopen(outname, "rb")) == NULL)
1685    {
1686       fprintf(STDERR, "Could not find file %s\n", outname);
1687       FCLOSE(fpin);
1688       return (1);
1689    }
1690 
1691 #if defined (PNG_WRITE_SUPPORTED) /* else nothing was written */ &&\
1692     defined (PNG_WRITE_FILTER_SUPPORTED)
1693    if (interlace_preserved != 0) /* else the files will be changed */
1694    {
1695       for (;;)
1696       {
1697          static int wrote_question = 0;
1698          png_size_t num_in, num_out;
1699          char inbuf[256], outbuf[256];
1700 
1701          num_in = fread(inbuf, 1, sizeof inbuf, fpin);
1702          num_out = fread(outbuf, 1, sizeof outbuf, fpout);
1703 
1704          if (num_in != num_out)
1705          {
1706             fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
1707                     inname, outname);
1708 
1709             if (wrote_question == 0 && unsupported_chunks == 0)
1710             {
1711                fprintf(STDERR,
1712          "   Was %s written with the same maximum IDAT chunk size (%d bytes),",
1713                  inname, PNG_ZBUF_SIZE);
1714                fprintf(STDERR,
1715                  "\n   filtering heuristic (libpng default), compression");
1716                fprintf(STDERR,
1717                  " level (zlib default),\n   and zlib version (%s)?\n\n",
1718                  ZLIB_VERSION);
1719                wrote_question = 1;
1720             }
1721 
1722             FCLOSE(fpin);
1723             FCLOSE(fpout);
1724 
1725             if (strict != 0 && unsupported_chunks == 0)
1726               return (1);
1727 
1728             else
1729               return (0);
1730          }
1731 
1732          if (num_in == 0)
1733             break;
1734 
1735          if (memcmp(inbuf, outbuf, num_in))
1736          {
1737             fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
1738                outname);
1739 
1740             if (wrote_question == 0 && unsupported_chunks == 0)
1741             {
1742                fprintf(STDERR,
1743          "   Was %s written with the same maximum IDAT chunk size (%d bytes),",
1744                     inname, PNG_ZBUF_SIZE);
1745                fprintf(STDERR,
1746                  "\n   filtering heuristic (libpng default), compression");
1747                fprintf(STDERR,
1748                  " level (zlib default),\n   and zlib version (%s)?\n\n",
1749                  ZLIB_VERSION);
1750                wrote_question = 1;
1751             }
1752 
1753             FCLOSE(fpin);
1754             FCLOSE(fpout);
1755 
1756             /* NOTE: the unsupported_chunks escape is permitted here because
1757              * unsupported text chunk compression will result in the compression
1758              * mode being changed (to NONE) yet, in the test case, the result
1759              * can be exactly the same size!
1760              */
1761             if (strict != 0 && unsupported_chunks == 0)
1762               return (1);
1763 
1764             else
1765               return (0);
1766          }
1767       }
1768    }
1769 #endif /* WRITE && WRITE_FILTER */
1770 
1771    FCLOSE(fpin);
1772    FCLOSE(fpout);
1773 
1774    return (0);
1775 }
1776 
1777 /* Input and output filenames */
1778 #ifdef RISCOS
1779 static PNG_CONST char *inname = "pngtest/png";
1780 static PNG_CONST char *outname = "pngout/png";
1781 #else
1782 static PNG_CONST char *inname = "pngtest.png";
1783 static PNG_CONST char *outname = "pngout.png";
1784 #endif
1785 
1786 int
1787 main(int argc, char *argv[])
1788 {
1789    int multiple = 0;
1790    int ierror = 0;
1791 
1792    png_structp dummy_ptr;
1793 
1794    fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
1795    fprintf(STDERR, "   with zlib   version %s\n", ZLIB_VERSION);
1796    fprintf(STDERR, "%s", png_get_copyright(NULL));
1797    /* Show the version of libpng used in building the library */
1798    fprintf(STDERR, " library (%lu):%s",
1799       (unsigned long)png_access_version_number(),
1800       png_get_header_version(NULL));
1801 
1802    /* Show the version of libpng used in building the application */
1803    fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
1804       PNG_HEADER_VERSION_STRING);
1805 
1806    /* Do some consistency checking on the memory allocation settings, I'm
1807     * not sure this matters, but it is nice to know, the first of these
1808     * tests should be impossible because of the way the macros are set
1809     * in pngconf.h
1810     */
1811 #if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1812       fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
1813 #endif
1814    /* I think the following can happen. */
1815 #if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1816       fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
1817 #endif
1818 
1819    if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1820    {
1821       fprintf(STDERR,
1822          "Warning: versions are different between png.h and png.c\n");
1823       fprintf(STDERR, "  png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1824       fprintf(STDERR, "  png.c version: %s\n\n", png_libpng_ver);
1825       ++ierror;
1826    }
1827 
1828    if (argc > 1)
1829    {
1830       if (strcmp(argv[1], "-m") == 0)
1831       {
1832          multiple = 1;
1833          status_dots_requested = 0;
1834       }
1835 
1836       else if (strcmp(argv[1], "-mv") == 0 ||
1837                strcmp(argv[1], "-vm") == 0 )
1838       {
1839          multiple = 1;
1840          verbose = 1;
1841          status_dots_requested = 1;
1842       }
1843 
1844       else if (strcmp(argv[1], "-v") == 0)
1845       {
1846          verbose = 1;
1847          status_dots_requested = 1;
1848          inname = argv[2];
1849       }
1850 
1851       else if (strcmp(argv[1], "--strict") == 0)
1852       {
1853          status_dots_requested = 0;
1854          verbose = 1;
1855          inname = argv[2];
1856          strict++;
1857          relaxed = 0;
1858       }
1859 
1860       else if (strcmp(argv[1], "--relaxed") == 0)
1861       {
1862          status_dots_requested = 0;
1863          verbose = 1;
1864          inname = argv[2];
1865          strict = 0;
1866          relaxed++;
1867       }
1868 
1869       else
1870       {
1871          inname = argv[1];
1872          status_dots_requested = 0;
1873       }
1874    }
1875 
1876    if (multiple == 0 && argc == 3 + verbose)
1877      outname = argv[2 + verbose];
1878 
1879    if ((multiple == 0 && argc > 3 + verbose) ||
1880        (multiple != 0 && argc < 2))
1881    {
1882      fprintf(STDERR,
1883        "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1884         argv[0], argv[0]);
1885      fprintf(STDERR,
1886        "  reads/writes one PNG file (without -m) or multiple files (-m)\n");
1887      fprintf(STDERR,
1888        "  with -m %s is used as a temporary file\n", outname);
1889      exit(1);
1890    }
1891 
1892    if (multiple != 0)
1893    {
1894       int i;
1895 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1896       int allocation_now = current_allocation;
1897 #endif
1898       for (i=2; i<argc; ++i)
1899       {
1900          int kerror;
1901          fprintf(STDERR, "\n Testing %s:", argv[i]);
1902 #if PNG_DEBUG > 0
1903          fprintf(STDERR, "\n");
1904 #endif
1905          kerror = test_one_file(argv[i], outname);
1906          if (kerror == 0)
1907          {
1908 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1909             fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1910                (unsigned long)zero_samples);
1911 #else
1912             fprintf(STDERR, " PASS\n");
1913 #endif
1914 #ifdef PNG_TIME_RFC1123_SUPPORTED
1915             if (tIME_chunk_present != 0)
1916                fprintf(STDERR, " tIME = %s\n", tIME_string);
1917 
1918             tIME_chunk_present = 0;
1919 #endif /* TIME_RFC1123 */
1920          }
1921 
1922          else
1923          {
1924             fprintf(STDERR, " FAIL\n");
1925             ierror += kerror;
1926          }
1927 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1928          if (allocation_now != current_allocation)
1929             fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1930                current_allocation - allocation_now);
1931 
1932          if (current_allocation != 0)
1933          {
1934             memory_infop pinfo = pinformation;
1935 
1936             fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1937                current_allocation);
1938 
1939             while (pinfo != NULL)
1940             {
1941                fprintf(STDERR, " %lu bytes at %p\n",
1942                  (unsigned long)pinfo->size,
1943                  pinfo->pointer);
1944                pinfo = pinfo->next;
1945             }
1946          }
1947 #endif
1948       }
1949 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1950          fprintf(STDERR, " Current memory allocation: %10d bytes\n",
1951             current_allocation);
1952          fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
1953             maximum_allocation);
1954          fprintf(STDERR, " Total   memory allocation: %10d bytes\n",
1955             total_allocation);
1956          fprintf(STDERR, "     Number of allocations: %10d\n",
1957             num_allocations);
1958 #endif
1959    }
1960 
1961    else
1962    {
1963       int i;
1964       for (i = 0; i<3; ++i)
1965       {
1966          int kerror;
1967 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1968          int allocation_now = current_allocation;
1969 #endif
1970          if (i == 1)
1971             status_dots_requested = 1;
1972 
1973          else if (verbose == 0)
1974             status_dots_requested = 0;
1975 
1976          if (i == 0 || verbose == 1 || ierror != 0)
1977          {
1978             fprintf(STDERR, "\n Testing %s:", inname);
1979 #if PNG_DEBUG > 0
1980             fprintf(STDERR, "\n");
1981 #endif
1982          }
1983 
1984          kerror = test_one_file(inname, outname);
1985 
1986          if (kerror == 0)
1987          {
1988             if (verbose == 1 || i == 2)
1989             {
1990 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
1991                 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
1992                    (unsigned long)zero_samples);
1993 #else
1994                 fprintf(STDERR, " PASS\n");
1995 #endif
1996 #ifdef PNG_TIME_RFC1123_SUPPORTED
1997              if (tIME_chunk_present != 0)
1998                 fprintf(STDERR, " tIME = %s\n", tIME_string);
1999 #endif /* TIME_RFC1123 */
2000             }
2001          }
2002 
2003          else
2004          {
2005             if (verbose == 0 && i != 2)
2006             {
2007                fprintf(STDERR, "\n Testing %s:", inname);
2008 #if PNG_DEBUG > 0
2009                fprintf(STDERR, "\n");
2010 #endif
2011             }
2012 
2013             fprintf(STDERR, " FAIL\n");
2014             ierror += kerror;
2015          }
2016 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
2017          if (allocation_now != current_allocation)
2018              fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
2019                current_allocation - allocation_now);
2020 
2021          if (current_allocation != 0)
2022          {
2023              memory_infop pinfo = pinformation;
2024 
2025              fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
2026                 current_allocation);
2027 
2028              while (pinfo != NULL)
2029              {
2030                 fprintf(STDERR, " %lu bytes at %p\n",
2031                    (unsigned long)pinfo->size, pinfo->pointer);
2032                 pinfo = pinfo->next;
2033              }
2034           }
2035 #endif
2036        }
2037 #if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
2038        fprintf(STDERR, " Current memory allocation: %10d bytes\n",
2039           current_allocation);
2040        fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
2041           maximum_allocation);
2042        fprintf(STDERR, " Total   memory allocation: %10d bytes\n",
2043           total_allocation);
2044        fprintf(STDERR, "     Number of allocations: %10d\n",
2045             num_allocations);
2046 #endif
2047    }
2048 
2049 #ifdef PNGTEST_TIMING
2050    t_stop = (float)clock();
2051    t_misc += (t_stop - t_start);
2052    t_start = t_stop;
2053    fprintf(STDERR, " CPU time used = %.3f seconds",
2054       (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
2055    fprintf(STDERR, " (decoding %.3f,\n",
2056       t_decode/(float)CLOCKS_PER_SEC);
2057    fprintf(STDERR, "        encoding %.3f ,",
2058       t_encode/(float)CLOCKS_PER_SEC);
2059    fprintf(STDERR, " other %.3f seconds)\n\n",
2060       t_misc/(float)CLOCKS_PER_SEC);
2061 #endif
2062 
2063    if (ierror == 0)
2064       fprintf(STDERR, " libpng passes test\n");
2065 
2066    else
2067       fprintf(STDERR, " libpng FAILS test\n");
2068 
2069    dummy_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2070    fprintf(STDERR, " Default limits:\n");
2071    fprintf(STDERR, "  width_max  = %lu\n",
2072       (unsigned long) png_get_user_width_max(dummy_ptr));
2073    fprintf(STDERR, "  height_max = %lu\n",
2074       (unsigned long) png_get_user_height_max(dummy_ptr));
2075    if (png_get_chunk_cache_max(dummy_ptr) == 0)
2076       fprintf(STDERR, "  cache_max  = unlimited\n");
2077    else
2078       fprintf(STDERR, "  cache_max  = %lu\n",
2079          (unsigned long) png_get_chunk_cache_max(dummy_ptr));
2080    if (png_get_chunk_malloc_max(dummy_ptr) == 0)
2081       fprintf(STDERR, "  malloc_max = unlimited\n");
2082    else
2083       fprintf(STDERR, "  malloc_max = %lu\n",
2084          (unsigned long) png_get_chunk_malloc_max(dummy_ptr));
2085    png_destroy_read_struct(&dummy_ptr, NULL, NULL);
2086 
2087    return (int)(ierror != 0);
2088 }
2089 #else
2090 int
2091 main(void)
2092 {
2093    fprintf(STDERR,
2094       " test ignored because libpng was not built with read support\n");
2095    /* And skip this test */
2096    return PNG_LIBPNG_VER < 10600 ? 0 : 77;
2097 }
2098 #endif
2099 
2100 /* Generate a compiler error if there is an old png.h in the search path. */
2101 typedef png_libpng_version_1_6_20 Your_png_h_is_not_version_1_6_20;