[libpng16] Added pngvalid.c compile time checks for const APIs.
diff --git a/contrib/libtests/pngvalid.c b/contrib/libtests/pngvalid.c
index fc780a0..03a40de 100644
--- a/contrib/libtests/pngvalid.c
+++ b/contrib/libtests/pngvalid.c
@@ -78,6 +78,13 @@
    ((png_fixed_point)(PNG_GAMMA_THRESHOLD * PNG_FP_1))
 #endif
 
+#if PNG_LIBPNG_VER < 10600
+   /* 1.6.0 constifies many APIs, the following exists to allow pngvalid to be
+    * compiled against earlier versions.
+    */
+#  define png_const_strutp png_structp
+#endif
+
 #include "zlib.h"   /* For crc32 */
 
 #include <float.h>  /* For floating point constants */
@@ -782,7 +789,7 @@
 
 /* Generate an error message (in the given buffer) */
 static size_t
-store_message(png_store *ps, png_structp pp, char *buffer, size_t bufsize,
+store_message(png_store *ps, png_const_structp pp, char *buffer, size_t bufsize,
    size_t pos, PNG_CONST char *msg)
 {
    if (pp != NULL && pp == ps->pread)
@@ -822,7 +829,7 @@
 
 /* Verbose output to the error stream: */
 static void
-store_verbose(png_store *ps, png_structp pp, png_const_charp prefix,
+store_verbose(png_store *ps, png_const_structp pp, png_const_charp prefix,
    png_const_charp message)
 {
    char buffer[512];
@@ -837,7 +844,8 @@
 
 /* Log an error or warning - the relevant count is always incremented. */
 static void
-store_log(png_store* ps, png_structp pp, png_const_charp message, int is_error)
+store_log(png_store* ps, png_const_structp pp, png_const_charp message,
+   int is_error)
 {
    /* The warning is copied to the error buffer if there are no errors and it is
     * the first warning.  The error is copied to the error buffer if it is the
@@ -866,8 +874,9 @@
 
 /* Functions to use as PNG callbacks. */
 static void
-store_error(png_structp pp, png_const_charp message) /* PNG_NORETURN */
+store_error(png_structp ppIn, png_const_charp message) /* PNG_NORETURN */
 {
+   png_const_structp pp = ppIn;
    png_store *ps = voidcast(png_store*, png_get_error_ptr(pp));
 
    if (!ps->expect_error)
@@ -881,8 +890,9 @@
 }
 
 static void
-store_warning(png_structp pp, png_const_charp message)
+store_warning(png_structp ppIn, png_const_charp message)
 {
+   png_const_structp pp = ppIn;
    png_store *ps = voidcast(png_store*, png_get_error_ptr(pp));
 
    if (!ps->expect_warning)
@@ -896,7 +906,7 @@
  */
 /* Return a single row from the correct image. */
 static png_bytep
-store_image_row(PNG_CONST png_store* ps, png_structp pp, int nImage,
+store_image_row(PNG_CONST png_store* ps, png_const_structp pp, int nImage,
    png_uint_32 y)
 {
    png_size_t coffset = (nImage * ps->image_h + y) * (ps->cb_row + 5) + 2;
@@ -911,7 +921,7 @@
 }
 
 static void
-store_image_free(png_store *ps, png_structp pp)
+store_image_free(png_store *ps, png_const_structp pp)
 {
    if (ps->image != NULL)
    {
@@ -933,8 +943,8 @@
 }
 
 static void
-store_ensure_image(png_store *ps, png_structp pp, int nImages, png_size_t cbRow,
-   png_uint_32 cRows)
+store_ensure_image(png_store *ps, png_const_structp pp, int nImages,
+   png_size_t cbRow, png_uint_32 cRows)
 {
    png_size_t cb = nImages * cRows * (cbRow + 5);
 
@@ -999,7 +1009,7 @@
 }
 
 static void
-store_image_check(PNG_CONST png_store* ps, png_structp pp, int iImage)
+store_image_check(PNG_CONST png_store* ps, png_const_structp pp, int iImage)
 {
    png_const_bytep image = ps->image;
 
@@ -1029,8 +1039,9 @@
 }
 
 static void
-store_write(png_structp pp, png_bytep pb, png_size_t st)
+store_write(png_structp ppIn, png_bytep pb, png_size_t st)
 {
+   png_const_structp pp = ppIn;
    png_store *ps = voidcast(png_store*, png_get_io_ptr(pp));
 
    if (ps->pwrite != pp)
@@ -1056,9 +1067,9 @@
 }
 
 static void
-store_flush(png_structp pp)
+store_flush(png_structp ppIn)
 {
-   UNUSED(pp) /*DOES NOTHING*/
+   UNUSED(ppIn) /*DOES NOTHING*/
 }
 
 static size_t
@@ -1149,8 +1160,9 @@
 }
 
 static void
-store_read(png_structp pp, png_bytep pb, png_size_t st)
+store_read(png_structp ppIn, png_bytep pb, png_size_t st)
 {
+   png_const_structp pp = ppIn;
    png_store *ps = voidcast(png_store*, png_get_io_ptr(pp));
 
    if (ps == NULL || ps->pread != pp)
@@ -1238,7 +1250,7 @@
  * all the memory.
  */
 static void
-store_pool_error(png_store *ps, png_structp pp, PNG_CONST char *msg)
+store_pool_error(png_store *ps, png_const_structp pp, PNG_CONST char *msg)
 {
    if (pp != NULL)
       png_error(pp, msg);
@@ -1251,7 +1263,7 @@
 }
 
 static void
-store_memory_free(png_structp pp, store_pool *pool, store_memory *memory)
+store_memory_free(png_const_structp pp, store_pool *pool, store_memory *memory)
 {
    /* Note that pp may be NULL (see store_pool_delete below), the caller has
     * found 'memory' in pool->list *and* unlinked this entry, so this is a valid
@@ -1337,8 +1349,9 @@
 
 /* The memory callbacks: */
 static png_voidp
-store_malloc(png_structp pp, png_alloc_size_t cb)
+store_malloc(png_structp ppIn, png_alloc_size_t cb)
 {
+   png_const_structp pp = ppIn;
    store_pool *pool = voidcast(store_pool*, png_get_mem_ptr(pp));
    store_memory *new = voidcast(store_memory*, malloc(cb + (sizeof *new) +
       (sizeof pool->mark)));
@@ -1385,8 +1398,9 @@
 }
 
 static void
-store_free(png_structp pp, png_voidp memory)
+store_free(png_structp ppIn, png_voidp memory)
 {
+   png_const_structp pp = ppIn;
    store_pool *pool = voidcast(store_pool*, png_get_mem_ptr(pp));
    store_memory *this = voidcast(store_memory*, memory), **test;
 
@@ -2540,8 +2554,9 @@
 
 /* The callback: */
 static void
-modifier_read(png_structp pp, png_bytep pb, png_size_t st)
+modifier_read(png_structp ppIn, png_bytep pb, png_size_t st)
 {
+   png_const_structp pp = ppIn;
    png_modifier *pm = voidcast(png_modifier*, png_get_io_ptr(pp));
 
    if (pm == NULL || pm->this.pread != pp)
@@ -2974,6 +2989,7 @@
 /* Initialize a standard palette on a write stream.  The 'do_tRNS' argument
  * indicates whether or not to also set the tRNS chunk.
  */
+/* TODO: the png_structp here can probably be 'const' in the future */
 static void
 init_standard_palette(png_store *ps, png_structp pp, png_infop pi, int npalette,
    int do_tRNS)
@@ -3022,7 +3038,7 @@
  * API to determine this prior to 1.5, so we need an inquiry function:
  */
 static int
-npasses_from_interlace_type(png_structp pp, int interlace_type)
+npasses_from_interlace_type(png_const_structp pp, int interlace_type)
 {
    switch (interlace_type)
    {
@@ -3038,7 +3054,7 @@
 }
 
 static unsigned int
-bit_size(png_structp pp, png_byte colour_type, png_byte bit_depth)
+bit_size(png_const_structp pp, png_byte colour_type, png_byte bit_depth)
 {
    switch (colour_type)
    {
@@ -3063,7 +3079,8 @@
 #define SIZE_HEIGHTMAX 16 /* Maximum range of size images */
 
 static size_t
-transform_rowsize(png_structp pp, png_byte colour_type, png_byte bit_depth)
+transform_rowsize(png_const_structp pp, png_byte colour_type,
+   png_byte bit_depth)
 {
    return (TRANSFORM_WIDTH * bit_size(pp, colour_type, bit_depth)) / 8;
 }
@@ -3074,7 +3091,7 @@
 #define transform_width(pp, colour_type, bit_depth) TRANSFORM_WIDTH
 
 static png_uint_32
-transform_height(png_structp pp, png_byte colour_type, png_byte bit_depth)
+transform_height(png_const_structp pp, png_byte colour_type, png_byte bit_depth)
 {
    switch (bit_size(pp, colour_type, bit_depth))
    {
@@ -3107,7 +3124,7 @@
  * of the transform image sizes.
  */
 static png_uint_32
-standard_width(png_structp pp, png_uint_32 id)
+standard_width(png_const_structp pp, png_uint_32 id)
 {
    png_uint_32 width = WIDTH_FROM_ID(id);
    UNUSED(pp)
@@ -3119,7 +3136,7 @@
 }
 
 static png_uint_32
-standard_height(png_structp pp, png_uint_32 id)
+standard_height(png_const_structp pp, png_uint_32 id)
 {
    png_uint_32 height = HEIGHT_FROM_ID(id);
 
@@ -3130,7 +3147,7 @@
 }
 
 static png_uint_32
-standard_rowsize(png_structp pp, png_uint_32 id)
+standard_rowsize(png_const_structp pp, png_uint_32 id)
 {
    png_uint_32 width = standard_width(pp, id);
 
@@ -3140,7 +3157,7 @@
 }
 
 static void
-transform_row(png_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
+transform_row(png_const_structp pp, png_byte buffer[TRANSFORM_ROWMAX],
    png_byte colour_type, png_byte bit_depth, png_uint_32 y)
 {
    png_uint_32 v = y << 7;
@@ -3656,8 +3673,8 @@
 
 /* Return a row based on image id and 'y' for checking: */
 static void
-standard_row(png_structp pp, png_byte std[STANDARD_ROWMAX], png_uint_32 id,
-   png_uint_32 y)
+standard_row(png_const_structp pp, png_byte std[STANDARD_ROWMAX],
+   png_uint_32 id, png_uint_32 y)
 {
    if (WIDTH_FROM_ID(id) == 0)
       transform_row(pp, std, COL_FROM_ID(id), DEPTH_FROM_ID(id), y);
@@ -3671,6 +3688,9 @@
  * to ensure that they get detected - it should not be possible to write an
  * invalid image with libpng!
  */
+/* TODO: the 'set' functions can probably all be made to take a
+ * png_const_structp rather than a modifiable one.
+ */
 #ifdef PNG_WARNINGS_SUPPORTED
 static void
 sBIT0_error_fn(png_structp pp, png_infop pi)
@@ -4074,7 +4094,8 @@
  * palette (it does not check for a transparent colour in the non-palette case.)
  */
 static int
-read_palette(store_palette palette, int *npalette, png_structp pp, png_infop pi)
+read_palette(store_palette palette, int *npalette, png_const_structp pp,
+   png_infop pi)
 {
    png_colorp pal;
    png_bytep trans_alpha;
@@ -4165,7 +4186,8 @@
  * non-transform case).
  */
 static void
-standard_palette_validate(standard_display *dp, png_structp pp, png_infop pi)
+standard_palette_validate(standard_display *dp, png_const_structp pp,
+   png_infop pi)
 {
    int npalette;
    store_palette palette;
@@ -4359,8 +4381,8 @@
  * image.
  */
 static void
-standard_info_part2(standard_display *dp, png_structp pp, png_infop pi,
-    int nImages)
+standard_info_part2(standard_display *dp, png_const_structp pp,
+    png_const_infop pi, int nImages)
 {
    /* Record cbRow now that it can be found. */
    dp->pixel_size = bit_size(pp, png_get_color_type(pp, pi),
@@ -4418,8 +4440,9 @@
 }
 
 static void
-progressive_row(png_structp pp, png_bytep new_row, png_uint_32 y, int pass)
+progressive_row(png_structp ppIn, png_bytep new_row, png_uint_32 y, int pass)
 {
+   png_const_structp pp = ppIn;
    PNG_CONST standard_display *dp = voidcast(standard_display*,
       png_get_progressive_ptr(pp));
 
@@ -4538,7 +4561,7 @@
 }
 
 static void
-standard_row_validate(standard_display *dp, png_structp pp,
+standard_row_validate(standard_display *dp, png_const_structp pp,
    int iImage, int iDisplay, png_uint_32 y)
 {
    int where;
@@ -4594,7 +4617,7 @@
 }
 
 static void
-standard_image_validate(standard_display *dp, png_structp pp, int iImage,
+standard_image_validate(standard_display *dp, png_const_structp pp, int iImage,
     int iDisplay)
 {
    png_uint_32 y;
@@ -4613,8 +4636,9 @@
 }
 
 static void
-standard_end(png_structp pp, png_infop pi)
+standard_end(png_structp ppIn, png_infop pi)
 {
+   png_const_structp pp = ppIn;
    standard_display *dp = voidcast(standard_display*,
       png_get_progressive_ptr(pp));
 
@@ -5121,7 +5145,7 @@
     * reporting via png_error and png_warning.
     */
    void (*mod)(PNG_CONST struct image_transform *this, image_pixel *that,
-      png_structp pp, PNG_CONST struct transform_display *display);
+      png_const_structp pp, PNG_CONST struct transform_display *display);
 
    /* Add this transform to the list and return true if the transform is
     * meaningful for this colour type and bit depth - if false then the
@@ -5216,7 +5240,7 @@
 
 static void
 image_transform_mod_end(PNG_CONST image_transform *this, image_pixel *that,
-    png_structp pp, PNG_CONST transform_display *display)
+    png_const_structp pp, PNG_CONST transform_display *display)
 {
    PNG_CONST unsigned int scale = (1U<<that->sample_depth)-1;
 
@@ -5445,7 +5469,7 @@
 }
 
 static void
-transform_range_check(png_structp pp, unsigned int r, unsigned int g,
+transform_range_check(png_const_structp pp, unsigned int r, unsigned int g,
    unsigned int b, unsigned int a, unsigned int in_digitized, double in,
    unsigned int out, png_byte sample_depth, double err, double limit,
    PNG_CONST char *name, double digitization_error)
@@ -5488,7 +5512,8 @@
 }
 
 static void
-transform_image_validate(transform_display *dp, png_structp pp, png_infop pi)
+transform_image_validate(transform_display *dp, png_const_structp pp,
+   png_infop pi)
 {
    /* Constants for the loop below: */
    PNG_CONST png_store* PNG_CONST ps = dp->this.ps;
@@ -5646,8 +5671,9 @@
 }
 
 static void
-transform_end(png_structp pp, png_infop pi)
+transform_end(png_structp ppIn, png_infop pi)
 {
+   png_const_structp pp = ppIn;
    transform_display *dp = voidcast(transform_display*,
       png_get_progressive_ptr(pp));
 
@@ -5793,7 +5819,8 @@
 
 static void
 image_transform_png_set_palette_to_rgb_mod(PNG_CONST image_transform *this,
-    image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+    image_pixel *that, png_const_structp pp,
+    PNG_CONST transform_display *display)
 {
    if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
       image_pixel_convert_PLTE(that);
@@ -5830,7 +5857,8 @@
 
 static void
 image_transform_png_set_tRNS_to_alpha_mod(PNG_CONST image_transform *this,
-   image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+   image_pixel *that, png_const_structp pp,
+   PNG_CONST transform_display *display)
 {
    /* LIBPNG BUG: this always forces palette images to RGB. */
    if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
@@ -5887,7 +5915,8 @@
 
 static void
 image_transform_png_set_gray_to_rgb_mod(PNG_CONST image_transform *this,
-    image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+    image_pixel *that, png_const_structp pp,
+    PNG_CONST transform_display *display)
 {
    /* NOTE: we can actually pend the tRNS processing at this point because we
     * can correctly recognize the original pixel value even though we have
@@ -5945,7 +5974,8 @@
 
 static void
 image_transform_png_set_expand_mod(PNG_CONST image_transform *this,
-    image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+    image_pixel *that, png_const_structp pp,
+    PNG_CONST transform_display *display)
 {
    /* The general expand case depends on what the colour type is: */
    if (that->colour_type == PNG_COLOR_TYPE_PALETTE)
@@ -5994,7 +6024,7 @@
 
 static void
 image_transform_png_set_expand_gray_1_2_4_to_8_mod(
-    PNG_CONST image_transform *this, image_pixel *that, png_structp pp,
+    PNG_CONST image_transform *this, image_pixel *that, png_const_structp pp,
     PNG_CONST transform_display *display)
 {
    image_transform_png_set_expand_mod(this, that, pp, display);
@@ -6025,7 +6055,8 @@
 
 static void
 image_transform_png_set_expand_16_mod(PNG_CONST image_transform *this,
-    image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+    image_pixel *that, png_const_structp pp,
+    PNG_CONST transform_display *display)
 {
    /* Expect expand_16 to expand everything to 16 bits as a result of also
     * causing 'expand' to happen.
@@ -6072,7 +6103,8 @@
 
 static void
 image_transform_png_set_scale_16_mod(PNG_CONST image_transform *this,
-    image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+    image_pixel *that, png_const_structp pp,
+    PNG_CONST transform_display *display)
 {
    if (that->bit_depth == 16)
    {
@@ -6115,7 +6147,8 @@
 
 static void
 image_transform_png_set_strip_16_mod(PNG_CONST image_transform *this,
-    image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+    image_pixel *that, png_const_structp pp,
+    PNG_CONST transform_display *display)
 {
    if (that->bit_depth == 16)
    {
@@ -6181,7 +6214,8 @@
 
 static void
 image_transform_png_set_strip_alpha_mod(PNG_CONST image_transform *this,
-    image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+    image_pixel *that, png_const_structp pp,
+    PNG_CONST transform_display *display)
 {
    if (that->colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
       that->colour_type = PNG_COLOR_TYPE_GRAY;
@@ -6513,7 +6547,8 @@
 
 static void
 image_transform_png_set_rgb_to_gray_mod(PNG_CONST image_transform *this,
-    image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+    image_pixel *that, png_const_structp pp,
+    PNG_CONST transform_display *display)
 {
    if ((that->colour_type & PNG_COLOR_MASK_COLOR) != 0)
    {
@@ -6764,7 +6799,8 @@
 
 static void
 image_transform_png_set_background_mod(PNG_CONST image_transform *this,
-    image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+    image_pixel *that, png_const_structp pp,
+    PNG_CONST transform_display *display)
 {
    /* Check for tRNS first: */
    if (that->have_tRNS && that->colour_type != PNG_COLOR_TYPE_PALETTE)
@@ -7014,7 +7050,8 @@
 
 static void
 image_transform_png_set_@_mod(PNG_CONST image_transform *this,
-    image_pixel *that, png_structp pp, PNG_CONST transform_display *display)
+    image_pixel *that, png_const_structp pp,
+    PNG_CONST transform_display *display)
 {
    this->next->mod(this->next, that, pp, display);
 }
@@ -7317,7 +7354,7 @@
  */
 typedef struct validate_info
 {
-   png_structp  pp;
+   png_const_structp  pp;
    gamma_display *dp;
    png_byte sbit;
    int use_input_precision;
@@ -7347,7 +7384,7 @@
 validate_info;
 
 static void
-init_validate_info(validate_info *vi, gamma_display *dp, png_struct *pp,
+init_validate_info(validate_info *vi, gamma_display *dp, png_const_structp pp,
     int in_depth, int out_depth)
 {
    PNG_CONST unsigned int outmax = (1U<<out_depth)-1;
@@ -8060,7 +8097,8 @@
 }
 
 static void
-gamma_image_validate(gamma_display *dp, png_structp pp, png_infop pi)
+gamma_image_validate(gamma_display *dp, png_const_structp pp,
+   png_infop pi)
 {
    /* Get some constants derived from the input and output file formats: */
    PNG_CONST png_store* PNG_CONST ps = dp->this.ps;
@@ -8249,8 +8287,9 @@
 }
 
 static void
-gamma_end(png_structp pp, png_infop pi)
+gamma_end(png_structp ppIn, png_infop pi)
 {
+   png_const_structp pp = ppIn;
    gamma_display *dp = voidcast(gamma_display*, png_get_progressive_ptr(pp));
 
    if (!dp->this.speed)