contrib: Apply various fixes to libtests

pngimage.c:
Initialize sig_bits on a NOTREACHED path to avoid warnings about using
uninitialized variables.

pngstest.c:
Enlarge buffers and fix signedness to avoid legitimate warnings about
potential buffer overflows.

pngunknown.c:
pngvalid.c:
Use NULL instead of 0 for pointers and apply other style fixes.

makepng.c:
tarith.c:
Apply various style fixes.

Also remove the "last changed" version info from source comments.
The version control system maintains this information automatically.
diff --git a/contrib/libtests/gentests.sh b/contrib/libtests/gentests.sh
index f0f8d23..eba7ce5 100755
--- a/contrib/libtests/gentests.sh
+++ b/contrib/libtests/gentests.sh
@@ -2,8 +2,6 @@
 #
 # Copyright (c) 2013 John Cunningham Bowler
 #
-# Last changed in libpng 1.6.0 [February 14, 2013]
-#
 # This code is released under the libpng license.
 # For conditions of distribution and use, see the disclaimer
 # and license in png.h
diff --git a/contrib/libtests/makepng.c b/contrib/libtests/makepng.c
index 312062b..088df53 100644
--- a/contrib/libtests/makepng.c
+++ b/contrib/libtests/makepng.c
@@ -3,8 +3,6 @@
 /* Copyright: */
 #define COPYRIGHT "\251 2013,2015 John Cunningham Bowler"
 /*
- * Last changed in libpng 1.6.20 [November 24, 2015]
- *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
  * and license in png.h
@@ -299,25 +297,28 @@
             unsigned int x, y;
             volatile unsigned int ip = 0;
 
-            for (x=0; x<size; ++x) for (y=0; y<size; ++y)
+            for (x=0; x<size; ++x)
             {
-               ip = x + (size * y);
+               for (y=0; y<size; ++y)
+               {
+                  ip = x + (size * y);
 
-               /* size is at most 16, so the scaled value below fits in 16 bits
-                */
-#              define interp(pos, c1, c2) ((pos * c1) + ((size-pos) * c2))
-#              define xyinterp(x, y, c1, c2, c3, c4) (((size * size / 2) +\
-                  (interp(x, c1, c2) * y + (size-y) * interp(x, c3, c4))) /\
-                  (size*size))
+                  /* size is at most 16, so the scaled value below fits in 16 bits
+                  */
+#                 define interp(pos, c1, c2) ((pos * c1) + ((size-pos) * c2))
+#                 define xyinterp(x, y, c1, c2, c3, c4) (((size * size / 2) +\
+                     (interp(x, c1, c2) * y + (size-y) * interp(x, c3, c4))) /\
+                     (size*size))
 
-               set_color(palette+ip, trans+ip,
-                  /* color:    green, red,blue,white */
-                  xyinterp(x, y,   0, 255,   0, 255),
-                  xyinterp(x, y, 255,   0,   0, 255),
-                  xyinterp(x, y,   0,   0, 255, 255),
-                  /* alpha:        0, 102, 204, 255) */
-                  xyinterp(x, y,   0, 102, 204, 255),
-                  gamma_table);
+                  set_color(palette+ip, trans+ip,
+                     /* color:    green, red,blue,white */
+                     xyinterp(x, y,   0, 255,   0, 255),
+                     xyinterp(x, y, 255,   0,   0, 255),
+                     xyinterp(x, y,   0,   0, 255, 255),
+                     /* alpha:        0, 102, 204, 255) */
+                     xyinterp(x, y,   0, 102, 204, 255),
+                     gamma_table);
+               }
             }
 
             return ip+1;
@@ -396,7 +397,7 @@
       image_size_of_type(color_type, bit_depth, colors, small)-1;
    png_uint_32 depth_max = (1U << bit_depth)-1; /* up to 65536 */
 
-   if (colors[0] == 0) if (small)
+   if (colors[0] == 0 && small)
    {
       unsigned int pixel_depth = pixel_depth_of_type(color_type, bit_depth);
 
@@ -858,10 +859,13 @@
       {
          unsigned int i;
 
-         if (real_gamma == 45455) for (i=0; i<256; ++i)
+         if (real_gamma == 45455)
          {
-            gamma_table[i] = (png_byte)i;
-            conv = 1.;
+            for (i=0; i<256; ++i)
+            {
+               gamma_table[i] = (png_byte)i;
+               conv = 1.;
+            }
          }
 
          else
@@ -1430,10 +1434,13 @@
    for (i=0; *param && i<nparams; ++i)
    {
       list[i] = param;
-      while (*++param) if (*param == '\n' || *param == ':')
+      while (*++param)
       {
-         *param++ = 0; /* Terminate last parameter */
-         break;        /* And start a new one. */
+         if (*param == '\n' || *param == ':')
+         {
+            *param++ = 0; /* Terminate last parameter */
+            break;        /* And start a new one. */
+         }
       }
    }
 
diff --git a/contrib/libtests/pngimage.c b/contrib/libtests/pngimage.c
index f130c04..1fe499a 100644
--- a/contrib/libtests/pngimage.c
+++ b/contrib/libtests/pngimage.c
@@ -1,9 +1,9 @@
+
 /* pngimage.c
  *
+ * Copyright (c) 2021 Cosmin Truta
  * Copyright (c) 2015,2016 John Cunningham Bowler
  *
- * Last changed in libpng 1.6.24 [August 4, 2016]
- *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
  * and license in png.h
@@ -12,6 +12,7 @@
  * using png_read_png and then write with png_write_png.  Test all possible
  * transforms.
  */
+
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
@@ -317,11 +318,10 @@
 
    t &= -t; /* first set bit */
 
-   for (i=0; i<TTABLE_SIZE; ++i) if (transform_info[i].name != NULL)
-   {
-      if ((transform_info[i].transform & t) != 0)
-         return transform_info[i].name;
-   }
+   for (i=0; i<TTABLE_SIZE; ++i)
+      if (transform_info[i].name != NULL)
+         if ((transform_info[i].transform & t) != 0)
+            return transform_info[i].name;
 
    return "invalid transform";
 }
@@ -338,13 +338,16 @@
 {
    unsigned int i;
 
-   for (i=0; i<TTABLE_SIZE; ++i) if (transform_info[i].name != NULL)
+   for (i=0; i<TTABLE_SIZE; ++i)
    {
-      if (transform_info[i].when & TRANSFORM_R)
-         read_transforms |= transform_info[i].transform;
+      if (transform_info[i].name != NULL)
+      {
+         if (transform_info[i].when & TRANSFORM_R)
+            read_transforms |= transform_info[i].transform;
 
-      if (transform_info[i].when & TRANSFORM_W)
-         write_transforms |= transform_info[i].transform;
+         if (transform_info[i].when & TRANSFORM_W)
+            write_transforms |= transform_info[i].transform;
+      }
    }
 
    /* Reversible transforms are those which are supported on both read and
@@ -962,21 +965,24 @@
       int bd = dp->bit_depth;
       unsigned int i;
 
-      for (i=0; i<TTABLE_SIZE; ++i) if (transform_info[i].name != NULL)
+      for (i=0; i<TTABLE_SIZE; ++i)
       {
-         int transform = transform_info[i].transform;
+         if (transform_info[i].name != NULL)
+         {
+            int transform = transform_info[i].transform;
 
-         if ((transform_info[i].valid_chunks == 0 ||
-               (transform_info[i].valid_chunks & chunks) != 0) &&
-            (transform_info[i].color_mask_required & ct) ==
-               transform_info[i].color_mask_required &&
-            (transform_info[i].color_mask_absent & ct) == 0 &&
-            (transform_info[i].bit_depths & bd) != 0 &&
-            (transform_info[i].when & TRANSFORM_R) != 0)
-            active |= transform;
+            if ((transform_info[i].valid_chunks == 0 ||
+                  (transform_info[i].valid_chunks & chunks) != 0) &&
+               (transform_info[i].color_mask_required & ct) ==
+                  transform_info[i].color_mask_required &&
+               (transform_info[i].color_mask_absent & ct) == 0 &&
+               (transform_info[i].bit_depths & bd) != 0 &&
+               (transform_info[i].when & TRANSFORM_R) != 0)
+               active |= transform;
 
-         else if ((transform_info[i].when & TRANSFORM_R) != 0)
-            inactive |= transform;
+            else if ((transform_info[i].when & TRANSFORM_R) != 0)
+               inactive |= transform;
+         }
       }
 
       /* Some transforms appear multiple times in the table; the 'active' status
@@ -1081,8 +1087,9 @@
                size_t x;
 
                /* Find the first error */
-               for (x=0; x<rowbytes-1; ++x) if (row[x] != orig[x])
-                  break;
+               for (x=0; x<rowbytes-1; ++x)
+                  if (row[x] != orig[x])
+                     break;
 
                display_log(dp, APP_FAIL,
                   "byte(%lu,%lu) changed 0x%.2x -> 0x%.2x",
@@ -1137,6 +1144,7 @@
                display_log(dp, LIBPNG_ERROR, "invalid colour type %d",
                   color_type);
                /*NOTREACHED*/
+               memset(sig_bits, 0, sizeof(sig_bits));
                bpp = 0;
                break;
          }
@@ -1198,7 +1206,7 @@
                sig_bits[0] = (png_byte)b;
                break;
 
-            case 4: /* Relicate twice */
+            case 4: /* Replicate twice */
                /* Value is 1, 2, 3 or 4 */
                b = 0xf & ((0xf << 4) >> sig_bits[0]);
                b |= b << 4;
@@ -1686,8 +1694,9 @@
 
                printf("%s: pngimage ", pass ? "PASS" : "FAIL");
 
-               for (j=1; j<option_end; ++j) if (j != ilog)
-                  printf("%s ", argv[j]);
+               for (j=1; j<option_end; ++j)
+                  if (j != ilog)
+                     printf("%s ", argv[j]);
 
                printf("%s\n", d.filename);
             }
diff --git a/contrib/libtests/pngstest.c b/contrib/libtests/pngstest.c
index a368bf0..cd4f3cd 100644
--- a/contrib/libtests/pngstest.c
+++ b/contrib/libtests/pngstest.c
@@ -1,7 +1,7 @@
-/*-
- * pngstest.c
+
+/* pngstest.c
  *
- * Last changed in libpng 1.6.31 [July 27, 2017]
+ * Copyright (c) 2021 Cosmin Truta
  * Copyright (c) 2013-2017 John Cunningham Bowler
  *
  * This code is released under the libpng license.
@@ -10,8 +10,9 @@
  *
  * Test for the PNG 'simplified' APIs.
  */
+
 #define _ISOC90_SOURCE 1
-#define MALLOC_CHECK_ 2/*glibc facility: turn on debugging*/
+#define MALLOC_CHECK_ 2 /*glibc facility: turn on debugging*/
 
 #include <stddef.h>
 #include <stdlib.h>
@@ -2701,7 +2702,7 @@
             {
                if ((a->opts & ACCUMULATE) == 0)
                {
-                  char pindex[9];
+                  char pindex[16];
                   sprintf(pindex, "%lu[%lu]", (unsigned long)y,
                      (unsigned long)a->image.colormap_entries);
                   logerror(a, a->file_name, ": bad pixel index: ", pindex);
@@ -2712,12 +2713,12 @@
             else if (y >= b->image.colormap_entries)
             {
                if ((b->opts & ACCUMULATE) == 0)
-                  {
-                  char pindex[9];
+               {
+                  char pindex[16];
                   sprintf(pindex, "%lu[%lu]", (unsigned long)y,
                      (unsigned long)b->image.colormap_entries);
                   logerror(b, b->file_name, ": bad pixel index: ", pindex);
-                  }
+               }
                result = 0;
             }
 
@@ -2820,8 +2821,11 @@
          bchannels = component_loc(bloc, formatb);
 
          /* Hence the btoa array. */
-         for (i=0; i<4; ++i) if (bloc[i] < 4)
-            btoa[bloc[i]] = aloc[i]; /* may be '4' for alpha */
+         for (i=0; i<4; ++i)
+         {
+            if (bloc[i] < 4)
+               btoa[bloc[i]] = aloc[i]; /* may be '4' for alpha */
+         }
 
          if (alpha_added)
             alpha_added = bloc[0]; /* location of alpha channel in image b */
@@ -3209,10 +3213,10 @@
    else if (image->opts & USE_FILE)
    {
 #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
-      static int counter = 0;
+      static unsigned int counter = 0;
       char name[32];
 
-      sprintf(name, "%s%d.png", tmpf, ++counter);
+      sprintf(name, "%s%u.png", tmpf, ++counter);
 
       if (png_image_write_to_file(&image->image, name, convert_to_8bit,
          image->buffer+16, (png_int_32)image->stride, image->colormap))
diff --git a/contrib/libtests/pngunknown.c b/contrib/libtests/pngunknown.c
index 05bdd83..dfa9d10 100644
--- a/contrib/libtests/pngunknown.c
+++ b/contrib/libtests/pngunknown.c
@@ -1,7 +1,7 @@
 
 /* pngunknown.c - test the read side unknown chunk handling
  *
- * Last changed in libpng 1.6.32 [August 24, 2017]
+ * Copyright (c) 2021 Cosmin Truta
  * Copyright (c) 2015,2017 Glenn Randers-Pehrson
  * Written by John Cunningham Bowler
  *
@@ -370,7 +370,9 @@
 {
    int i = NINFO;
 
-   while (--i >= 0) if (chunk_info[i].flag == flag) return i;
+   while (--i >= 0)
+      if (chunk_info[i].flag == flag)
+         return i;
 
    fprintf(stderr, "pngunknown: internal error\n");
    exit(4);
@@ -547,27 +549,30 @@
 
       case PNG_HANDLE_CHUNK_AS_DEFAULT:
       case PNG_HANDLE_CHUNK_NEVER:
-         discard = 1/*handled; discard*/;
+         discard = 1; /*handled; discard*/
          break;
 
       case PNG_HANDLE_CHUNK_IF_SAFE:
       case PNG_HANDLE_CHUNK_ALWAYS:
-         discard = 0/*not handled; keep*/;
+         discard = 0; /*not handled; keep*/
          break;
    }
 
    /* Also store information about this chunk in the display, the relevant flag
     * is set if the chunk is to be kept ('not handled'.)
     */
-   if (chunk >= 0) if (!discard) /* stupidity to stop a GCC warning */
+   if (chunk >= 0)
    {
-      png_uint_32 flag = chunk_info[chunk].flag;
+      if (!discard) /* stupidity to stop a GCC warning */
+      {
+         png_uint_32 flag = chunk_info[chunk].flag;
 
-      if (pc->location & PNG_AFTER_IDAT)
-         d->after_IDAT |= flag;
+         if (pc->location & PNG_AFTER_IDAT)
+            d->after_IDAT |= flag;
 
-      else
-         d->before_IDAT |= flag;
+         else
+            d->before_IDAT |= flag;
+      }
    }
 
    /* However if there is no support to store unknown chunks don't ask libpng to
@@ -841,8 +846,9 @@
             {
                png_uint_32 y;
 
-               for (y=0; y<height; ++y) if (PNG_ROW_IN_INTERLACE_PASS(y, ipass))
-                  png_read_row(d->png_ptr, NULL, NULL);
+               for (y=0; y<height; ++y)
+                  if (PNG_ROW_IN_INTERLACE_PASS(y, ipass))
+                     png_read_row(d->png_ptr, NULL, NULL);
             }
          }
       } /* interlaced */
@@ -1091,15 +1097,15 @@
 
 static const char *standard_tests[] =
 {
- "discard", "default=discard", 0,
- "save", "default=save", 0,
- "if-safe", "default=if-safe", 0,
- "vpAg", "vpAg=if-safe", 0,
- "sTER", "sTER=if-safe", 0,
- "IDAT", "default=discard", "IDAT=save", 0,
- "sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
-   "sBIT=save", "sRGB=save", "eXIf=save", 0,
- 0/*end*/
+   "discard", "default=discard", NULL,
+   "save", "default=save", NULL,
+   "if-safe", "default=if-safe", NULL,
+   "vpAg", "vpAg=if-safe", NULL,
+   "sTER", "sTER=if-safe", NULL,
+   "IDAT", "default=discard", "IDAT=save", NULL,
+   "sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
+      "sBIT=save", "sRGB=save", "eXIf=save", NULL,
+   NULL /*end*/
 };
 
 static PNG_NORETURN void
@@ -1115,7 +1121,7 @@
 main(int argc, const char **argv)
 {
    FILE *fp;
-   png_uint_32 default_flags[4/*valid,unknown{before,after}*/];
+   png_uint_32 default_flags[4]; /*valid,unknown{before,after}*/
    int strict = 0, default_tests = 0;
    const char *count_argv = "default=save";
    const char *touch_file = NULL;
@@ -1153,8 +1159,9 @@
    /* GCC BUG: if (default_tests && argc != 1) triggers some weird GCC argc
     * optimization which causes warnings with -Wstrict-overflow!
     */
-   else if (default_tests) if (argc != 1)
-      usage(d.program, "extra arguments");
+   else if (default_tests)
+      if (argc != 1)
+         usage(d.program, "extra arguments");
 
    /* The name of the test file is the last argument; remove it. */
    d.file = argv[--argc];
@@ -1216,7 +1223,11 @@
          const char *result;
          int arg_count = 0;
 
-         while (*next) ++next, ++arg_count;
+         while (*next != NULL)
+         {
+            ++next;
+            ++arg_count;
+         }
 
          perform_one_test_safe(fp, arg_count, test, default_flags, &d,
             this_test);
diff --git a/contrib/libtests/pngvalid.c b/contrib/libtests/pngvalid.c
index 9a2da6a..86e2f75 100644
--- a/contrib/libtests/pngvalid.c
+++ b/contrib/libtests/pngvalid.c
@@ -1,7 +1,7 @@
 
 /* pngvalid.c - validate libpng by constructing then reading png files.
  *
- * Last changed in libpng 1.6.31 [July 27, 2017]
+ * Copyright (c) 2021 Cosmin Truta
  * Copyright (c) 2014-2017 John Cunningham Bowler
  *
  * This code is released under the libpng license.
@@ -5823,8 +5823,9 @@
       for (interlace_type = PNG_INTERLACE_NONE;
            interlace_type < INTERLACE_LAST; ++interlace_type)
       {
-         standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
-            interlace_type, 0, 0, 0), do_read_interlace, pm->use_update_info);
+         standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
+            0/*palette*/, interlace_type, 0, 0, 0), do_read_interlace,
+            pm->use_update_info);
 
          if (fail(pm))
             return 0;
@@ -5877,47 +5878,50 @@
    {
       png_uint_32 h, w;
 
-      for (h=1; h<=16; h+=hinc[bdlo]) for (w=1; w<=16; w+=winc[bdlo])
+      for (h=1; h<=16; h+=hinc[bdlo])
       {
-         /* First test all the 'size' images against the sequential
-          * reader using libpng to deinterlace (where required.)  This
-          * validates the write side of libpng.  There are four possibilities
-          * to validate.
-          */
-         standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
-            PNG_INTERLACE_NONE, w, h, 0), 0/*do_interlace*/,
-            pm->use_update_info);
+         for (w=1; w<=16; w+=winc[bdlo])
+         {
+            /* First test all the 'size' images against the sequential
+            * reader using libpng to deinterlace (where required.)  This
+            * validates the write side of libpng.  There are four possibilities
+            * to validate.
+            */
+            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
+               0/*palette*/, PNG_INTERLACE_NONE, w, h, 0), 0/*do_interlace*/,
+               pm->use_update_info);
 
-         if (fail(pm))
-            return 0;
+            if (fail(pm))
+               return 0;
 
-         standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
-            PNG_INTERLACE_NONE, w, h, 1), 0/*do_interlace*/,
-            pm->use_update_info);
+            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
+               0/*palette*/, PNG_INTERLACE_NONE, w, h, 1), 0/*do_interlace*/,
+               pm->use_update_info);
 
-         if (fail(pm))
-            return 0;
+            if (fail(pm))
+               return 0;
 
-         /* Now validate the interlaced read side - do_interlace true,
-          * in the progressive case this does actually make a difference
-          * to the code used in the non-interlaced case too.
-          */
-         standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
-            PNG_INTERLACE_NONE, w, h, 0), 1/*do_interlace*/,
-            pm->use_update_info);
+            /* Now validate the interlaced read side - do_interlace true,
+            * in the progressive case this does actually make a difference
+            * to the code used in the non-interlaced case too.
+            */
+            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
+               0/*palette*/, PNG_INTERLACE_NONE, w, h, 0), 1/*do_interlace*/,
+               pm->use_update_info);
 
-         if (fail(pm))
-            return 0;
+            if (fail(pm))
+               return 0;
 
-#     if CAN_WRITE_INTERLACE
-         /* Validate the pngvalid code itself: */
-         standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
-            PNG_INTERLACE_ADAM7, w, h, 1), 1/*do_interlace*/,
-            pm->use_update_info);
+#        if CAN_WRITE_INTERLACE
+            /* Validate the pngvalid code itself: */
+            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
+               0/*palette*/, PNG_INTERLACE_ADAM7, w, h, 1), 1/*do_interlace*/,
+               pm->use_update_info);
 
-         if (fail(pm))
-            return 0;
-#     endif
+            if (fail(pm))
+               return 0;
+#        endif
+         }
       }
    }
 
@@ -5928,45 +5932,48 @@
    {
       png_uint_32 h, w;
 
-      for (h=1; h<=16; h+=hinc[bdlo]) for (w=1; w<=16; w+=winc[bdlo])
+      for (h=1; h<=16; h+=hinc[bdlo])
       {
-#     ifdef PNG_READ_INTERLACING_SUPPORTED
-         /* Test with pngvalid generated interlaced images first; we have
-          * already verify these are ok (unless pngvalid has self-consistent
-          * read/write errors, which is unlikely), so this detects errors in the
-          * read side first:
-          */
-#     if CAN_WRITE_INTERLACE
-         standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
-            PNG_INTERLACE_ADAM7, w, h, 1), 0/*do_interlace*/,
-            pm->use_update_info);
+         for (w=1; w<=16; w+=winc[bdlo])
+         {
+#        ifdef PNG_READ_INTERLACING_SUPPORTED
+            /* Test with pngvalid generated interlaced images first; we have
+            * already verify these are ok (unless pngvalid has self-consistent
+            * read/write errors, which is unlikely), so this detects errors in
+            * the read side first:
+            */
+#        if CAN_WRITE_INTERLACE
+            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
+               0/*palette*/, PNG_INTERLACE_ADAM7, w, h, 1), 0/*do_interlace*/,
+               pm->use_update_info);
 
-         if (fail(pm))
-            return 0;
-#     endif
-#     endif /* READ_INTERLACING */
+            if (fail(pm))
+               return 0;
+#        endif
+#        endif /* READ_INTERLACING */
 
-#     ifdef PNG_WRITE_INTERLACING_SUPPORTED
-         /* Test the libpng write side against the pngvalid read side: */
-         standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
-            PNG_INTERLACE_ADAM7, w, h, 0), 1/*do_interlace*/,
-            pm->use_update_info);
+#        ifdef PNG_WRITE_INTERLACING_SUPPORTED
+            /* Test the libpng write side against the pngvalid read side: */
+            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
+               0/*palette*/, PNG_INTERLACE_ADAM7, w, h, 0), 1/*do_interlace*/,
+               pm->use_update_info);
 
-         if (fail(pm))
-            return 0;
-#     endif
+            if (fail(pm))
+               return 0;
+#        endif
 
-#     ifdef PNG_READ_INTERLACING_SUPPORTED
-#     ifdef PNG_WRITE_INTERLACING_SUPPORTED
-         /* Test both together: */
-         standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
-            PNG_INTERLACE_ADAM7, w, h, 0), 0/*do_interlace*/,
-            pm->use_update_info);
+#        ifdef PNG_READ_INTERLACING_SUPPORTED
+#        ifdef PNG_WRITE_INTERLACING_SUPPORTED
+            /* Test both together: */
+            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
+               0/*palette*/, PNG_INTERLACE_ADAM7, w, h, 0), 0/*do_interlace*/,
+               pm->use_update_info);
 
-         if (fail(pm))
-            return 0;
-#     endif
-#     endif /* READ_INTERLACING */
+            if (fail(pm))
+               return 0;
+#        endif
+#        endif /* READ_INTERLACING */
+         }
       }
    }
 
@@ -10650,16 +10657,21 @@
    {
       unsigned int i, j;
 
-      for (i=0; i<pm->ngamma_tests; ++i) for (j=0; j<pm->ngamma_tests; ++j)
-         if (i != j)
+      for (i=0; i<pm->ngamma_tests; ++i)
+      {
+         for (j=0; j<pm->ngamma_tests; ++j)
          {
-            gamma_transform_test(pm, colour_type, bit_depth, palette_number,
-               pm->interlace_type, 1/pm->gammas[i], pm->gammas[j], 0/*sBIT*/,
-               pm->use_input_precision, 0 /*do not scale16*/);
+            if (i != j)
+            {
+               gamma_transform_test(pm, colour_type, bit_depth, palette_number,
+                  pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
+                  0/*sBIT*/, pm->use_input_precision, 0/*do not scale16*/);
 
-            if (fail(pm))
-               return;
+               if (fail(pm))
+                  return;
+            }
          }
+      }
    }
 }
 
@@ -10688,14 +10700,17 @@
          {
             unsigned int j;
 
-            for (j=0; j<pm->ngamma_tests; ++j) if (i != j)
+            for (j=0; j<pm->ngamma_tests; ++j)
             {
-               gamma_transform_test(pm, colour_type, bit_depth, npalette,
-                  pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
-                  sbit, pm->use_input_precision_sbit, 0 /*scale16*/);
+               if (i != j)
+               {
+                  gamma_transform_test(pm, colour_type, bit_depth, npalette,
+                     pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
+                     sbit, pm->use_input_precision_sbit, 0 /*scale16*/);
 
-               if (fail(pm))
-                  return;
+                  if (fail(pm))
+                     return;
+               }
             }
          }
       }
@@ -10947,14 +10962,17 @@
       unsigned int i, j;
 
       /* Don't skip the i==j case here - it's relevant. */
-      for (i=0; i<pm->ngamma_tests; ++i) for (j=0; j<pm->ngamma_tests; ++j)
+      for (i=0; i<pm->ngamma_tests; ++i)
       {
-         gamma_composition_test(pm, colour_type, bit_depth, palette_number,
-            pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
-            pm->use_input_precision, do_background, expand_16);
+         for (j=0; j<pm->ngamma_tests; ++j)
+         {
+            gamma_composition_test(pm, colour_type, bit_depth, palette_number,
+               pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
+               pm->use_input_precision, do_background, expand_16);
 
-         if (fail(pm))
-            return;
+            if (fail(pm))
+               return;
+         }
       }
    }
 }
@@ -11057,7 +11075,7 @@
          pm->calculations_use_input_precision = 0;
 
       if (summary)
-         summarize_gamma_errors(pm, 0/*who*/, 1/*low bit depth*/, 1/*indexed*/);
+         summarize_gamma_errors(pm, NULL/*who*/, 1/*low bit depth*/, 1/*indexed*/);
 
       if (fail(pm))
          return;
@@ -11183,8 +11201,10 @@
 {
    int x, y;
    ++pass;
-   for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
-      return y;
+   for (y=0; y<8; ++y)
+      for (x=0; x<8; ++x)
+         if (adam7[y][x] == pass)
+            return y;
    return 0xf;
 }
 
@@ -11193,8 +11213,10 @@
 {
    int x, y;
    ++pass;
-   for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
-      return x;
+   for (x=0; x<8; ++x)
+      for (y=0; y<8; ++y)
+         if (adam7[y][x] == pass)
+            return x;
    return 0xf;
 }
 
@@ -11203,18 +11225,24 @@
 {
    int x, y, base=(-1), inc=8;
    ++pass;
-   for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
+   for (y=0; y<8; ++y)
    {
-      if (base == (-1))
-         base = y;
-      else if (base == y)
-         {}
-      else if (inc == y-base)
-         base=y;
-      else if (inc == 8)
-         inc = y-base, base=y;
-      else if (inc != y-base)
-         return 0xff; /* error - more than one 'inc' value! */
+      for (x=0; x<8; ++x)
+      {
+         if (adam7[y][x] == pass)
+         {
+            if (base == (-1))
+               base = y;
+            else if (base == y)
+               {}
+            else if (inc == y-base)
+               base=y;
+            else if (inc == 8)
+               inc = y-base, base=y;
+            else if (inc != y-base)
+               return 0xff; /* error - more than one 'inc' value! */
+         }
+      }
    }
 
    if (base == (-1)) return 0xfe; /* error - no row in pass! */
@@ -11237,18 +11265,24 @@
 {
    int x, y, base=(-1), inc=8;
    ++pass;
-   for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
+   for (x=0; x<8; ++x)
    {
-      if (base == (-1))
-         base = x;
-      else if (base == x)
-         {}
-      else if (inc == x-base)
-         base=x;
-      else if (inc == 8)
-         inc = x-base, base=x;
-      else if (inc != x-base)
-         return 0xff; /* error - more than one 'inc' value! */
+      for (y=0; y<8; ++y)
+      {
+         if (adam7[y][x] == pass)
+         {
+            if (base == (-1))
+               base = x;
+            else if (base == x)
+               {}
+            else if (inc == x-base)
+               base=x;
+            else if (inc == 8)
+               inc = x-base, base=x;
+            else if (inc != x-base)
+               return 0xff; /* error - more than one 'inc' value! */
+         }
+      }
    }
 
    if (base == (-1)) return 0xfe; /* error - no row in pass! */
@@ -11312,8 +11346,9 @@
    int x;
    y &= 7;
    ++pass;
-   for (x=0; x<8; ++x) if (adam7[y][x] == pass)
-      return 1;
+   for (x=0; x<8; ++x)
+      if (adam7[y][x] == pass)
+         return 1;
 
    return 0;
 }
@@ -11325,8 +11360,9 @@
    int y;
    x &= 7;
    ++pass;
-   for (y=0; y<8; ++y) if (adam7[y][x] == pass)
-      return 1;
+   for (y=0; y<8; ++y)
+      if (adam7[y][x] == pass)
+         return 1;
 
    return 0;
 }
@@ -11340,11 +11376,17 @@
 
    height &= 7;
    ++pass;
-   for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
+   for (y=0; y<8; ++y)
    {
-      rows += tiles;
-      if (y < height) ++rows;
-      break; /* i.e. break the 'x', column, loop. */
+      for (x=0; x<8; ++x)
+      {
+         if (adam7[y][x] == pass)
+         {
+            rows += tiles;
+            if (y < height) ++rows;
+            break; /* i.e. break the 'x', column, loop. */
+         }
+      }
    }
 
    return rows;
@@ -11359,11 +11401,17 @@
 
    width &= 7;
    ++pass;
-   for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
+   for (x=0; x<8; ++x)
    {
-      cols += tiles;
-      if (x < width) ++cols;
-      break; /* i.e. break the 'y', row, loop. */
+      for (y=0; y<8; ++y)
+      {
+         if (adam7[y][x] == pass)
+         {
+            cols += tiles;
+            if (x < width) ++cols;
+            break; /* i.e. break the 'y', row, loop. */
+         }
+      }
    }
 
    return cols;
@@ -11811,21 +11859,21 @@
 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
       else if (strncmp(*argv, "--transform-disable=",
          sizeof "--transform-disable") == 0)
-         {
+      {
          pm.test_transform = 1;
          transform_disable(*argv + sizeof "--transform-disable");
-         }
+      }
 
       else if (strncmp(*argv, "--transform-enable=",
          sizeof "--transform-enable") == 0)
-         {
+      {
          pm.test_transform = 1;
          transform_enable(*argv + sizeof "--transform-enable");
-         }
+      }
 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
 
       else if (strcmp(*argv, "--gamma") == 0)
-         {
+      {
          /* Just do two gamma tests here (2.2 and linear) for speed: */
          pm.ngamma_tests = 2U;
          pm.test_gamma_threshold = 1;
@@ -11834,7 +11882,7 @@
          pm.test_gamma_scale16 = 1;
          pm.test_gamma_background = 1; /* composition */
          pm.test_gamma_alpha_mode = 1;
-         }
+      }
 
       else if (strcmp(*argv, "--nogamma") == 0)
          pm.ngamma_tests = 0;
diff --git a/contrib/libtests/readpng.c b/contrib/libtests/readpng.c
index 60a2d46..7528e90 100644
--- a/contrib/libtests/readpng.c
+++ b/contrib/libtests/readpng.c
@@ -1,9 +1,8 @@
+
 /* readpng.c
  *
  * Copyright (c) 2013 John Cunningham Bowler
  *
- * Last changed in libpng 1.6.1 [March 28, 2013]
- *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
  * and license in png.h
diff --git a/contrib/libtests/tarith.c b/contrib/libtests/tarith.c
index c03f05a..ead77e9 100644
--- a/contrib/libtests/tarith.c
+++ b/contrib/libtests/tarith.c
@@ -1,10 +1,9 @@
 
 /* tarith.c
  *
+ * Copyright (c) 2021 Cosmin Truta
  * Copyright (c) 2011-2013 John Cunningham Bowler
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
- *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
  * and license in png.h
@@ -88,6 +87,7 @@
    int    minorarith = 0;
 
    while (--argc > 0)
+   {
       if (strcmp(*++argv, "-a") == 0)
          showall = 1;
       else if (strcmp(*argv, "-e") == 0 && argc > 0)
@@ -105,6 +105,7 @@
          fprintf(stderr, "unknown argument %s\n", *argv);
          return 1;
       }
+   }
 
    do
    {
@@ -130,8 +131,8 @@
        */
       if (buffer[precision+7] != 71)
       {
-         fprintf(stderr, "%g[%d] -> '%s'[%lu] buffer overflow\n", test,
-            precision, buffer, (unsigned long)strlen(buffer));
+         fprintf(stderr, "%g[%d] -> '%s'[%lu] buffer overflow\n",
+            test, precision, buffer, (unsigned long)strlen(buffer));
          failed = 1;
       }
 
@@ -146,16 +147,16 @@
          if (test >= 0 && strcmp(buffer, "inf") ||
              test <  0 && strcmp(buffer, "-inf"))
          {
-            fprintf(stderr, "%g[%d] -> '%s' but expected 'inf'\n", test,
-               precision, buffer);
+            fprintf(stderr, "%g[%d] -> '%s' but expected 'inf'\n",
+               test, precision, buffer);
             failed = 1;
          }
       }
       else if (!png_check_fp_number(buffer, precision+10, &state, &index) ||
           buffer[index] != 0)
       {
-         fprintf(stderr, "%g[%d] -> '%s' but has bad format ('%c')\n", test,
-         precision, buffer, buffer[index]);
+         fprintf(stderr, "%g[%d] -> '%s' but has bad format ('%c')\n",
+            test, precision, buffer, buffer[index]);
          failed = 1;
       }
       else if (PNG_FP_IS_NEGATIVE(state) && !(test < 0))
@@ -187,7 +188,7 @@
          /* Check the result against the original. */
          double out = atof(buffer);
          double change = fabs((out - test)/test);
-         double allow = .5/pow(10,
+         double allow = .5 / pow(10,
             (precision >= DBL_DIG) ? DBL_DIG-1 : precision-1);
 
          /* NOTE: if you hit this error case are you compiling with gcc
@@ -257,8 +258,9 @@
    }
    while (--count);
 
-   printf("Tested %d finite values, %d non-finite, %d OK (%d failed) %d minor "
-      "arithmetic errors\n", finite, nonfinite, ok, failcount, minorarith);
+   printf("Tested %d finite values, %d non-finite, %d OK (%d failed) "
+      "%d minor arithmetic errors\n",
+      finite, nonfinite, ok, failcount, minorarith);
    printf(" Error with >=%d digit precision %.2f%%\n", DBL_DIG, max_abs);
    printf(" Error with < %d digit precision %.2f%%\n", DBL_DIG, max);
 
@@ -371,8 +373,8 @@
    /* This should never fail (it's a serious bug if it does): */
    if (index != 0 && index != 1)
    {
-      fprintf(stderr, "%s: read beyond end of string (%lu)\n", co->number,
-         (unsigned long)index);
+      fprintf(stderr, "%s: read beyond end of string (%lu)\n",
+         co->number, (unsigned long)index);
       return 0;
    }
 
@@ -503,8 +505,8 @@
       if (number_is_valid != c.number_was_valid)
       {
          fprintf(stderr,
-            "%s: character '%c' [0x%.2x] changed number validity\n", co->number,
-            ch, ch);
+            "%s: character '%c' [0x%.2x] changed number validity\n",
+            co->number, ch, ch);
          return 0;
       }
 
@@ -521,10 +523,13 @@
 {
    int ch;
 
-   if (c.cnumber+4 < sizeof co->number) for (ch=0; ch<256; ++ch)
+   if (c.cnumber+4 < sizeof co->number)
    {
-      if (!check_one_character(co, c, ch))
-         return 0;
+      for (ch=0; ch<256; ++ch)
+      {
+         if (!check_one_character(co, c, ch))
+            return 0;
+      }
    }
 
    return 1;
@@ -539,10 +544,13 @@
 
    if (c.cnumber+4 < sizeof co->number && c.limit >= 0)
    {
-      if (c.limit > 0) for (i=0; tests[i]; ++i)
+      if (c.limit > 0)
       {
-         if (!check_one_character(co, c, tests[i]))
-               return 0;
+         for (i=0; tests[i]; ++i)
+         {
+            if (!check_one_character(co, c, tests[i]))
+                  return 0;
+         }
       }
 
       /* At the end check all the characters. */
@@ -616,10 +624,10 @@
    png_int_32 times, div;
 
    while (--argc > 0)
-      {
-         fprintf(stderr, "unknown argument %s\n", *++argv);
-         return 1;
-      }
+   {
+      fprintf(stderr, "unknown argument %s\n", *++argv);
+      return 1;
+   }
 
    /* Find out about the random number generator. */
    randbuffer = RAND_MAX;
@@ -687,25 +695,25 @@
         ok = 0, ++overflow, fpround = fp/*misleading*/;
 
       if (verbose)
-         fprintf(stderr, "TEST %d * %d / %d -> %lld (%s)\n", a, times, div,
-            fp, ok ? "ok" : "overflow");
+         fprintf(stderr, "TEST %d * %d / %d -> %lld (%s)\n",
+            a, times, div, fp, ok ? "ok" : "overflow");
 
       ++tested;
       if (png_muldiv(&result, a, times, div) != ok)
       {
          ++error;
          if (ok)
-             fprintf(stderr, "%d * %d / %d -> overflow (expected %lld)\n", a,
-                times, div, fp);
+             fprintf(stderr, "%d * %d / %d -> overflow (expected %lld)\n",
+                a, times, div, fp);
          else
-             fprintf(stderr, "%d * %d / %d -> %d (expected overflow %lld)\n", a,
-                times, div, result, fp);
+             fprintf(stderr, "%d * %d / %d -> %d (expected overflow %lld)\n",
+                a, times, div, result, fp);
       }
       else if (ok && result != fpround)
       {
          ++error;
-         fprintf(stderr, "%d * %d / %d -> %d not %lld\n", a, times, div, result,
-            fp);
+         fprintf(stderr, "%d * %d / %d -> %d not %lld\n",
+            a, times, div, result, fp);
       }
       else
          ++passed;
@@ -721,8 +729,8 @@
    }
    while (--count > 0);
 
-   printf("%d tests including %d overflows, %d passed, %d failed (%d 64-bit "
-      "errors)\n", tested, overflow, passed, error, error64);
+   printf("%d tests including %d overflows, %d passed, %d failed "
+      "(%d 64-bit errors)\n", tested, overflow, passed, error, error64);
    return 0;
 }
 
@@ -821,8 +829,9 @@
          {
             if (error > .68) /* By experiment error is less than .68 */
             {
-               fprintf(stderr, "16-bit log error: %d: got %u, expected %f"
-                  " error: %f\n", i, png_log16bit(i), correct, error);
+               fprintf(stderr,
+                  "16-bit log error: %d: got %u, expected %f error: %f\n",
+                  i, png_log16bit(i), correct, error);
             }
          }
       }
@@ -841,8 +850,9 @@
             maxerr = fabs(error);
          if (fabs(error) > 1883) /* By experiment. */
          {
-            fprintf(stderr, "32-bit exp error: %d: got %u, expected %f"
-                  " error: %f\n", i, png_exp(i), correct, error);
+            fprintf(stderr,
+               "32-bit exp error: %d: got %u, expected %f error: %f\n",
+               i, png_exp(i), correct, error);
          }
       }
 
@@ -859,8 +869,9 @@
             maxerr = fabs(error);
          if (fabs(error) > .50002) /* By experiment */
          {
-            fprintf(stderr, "8-bit exp error: %d: got %u, expected %f"
-                  " error: %f\n", i, png_exp8bit(i), correct, error);
+            fprintf(stderr,
+               "8-bit exp error: %d: got %u, expected %f error: %f\n",
+               i, png_exp8bit(i), correct, error);
          }
       }
 
@@ -877,8 +888,9 @@
             maxerr = fabs(error);
          if (fabs(error) > .524) /* By experiment */
          {
-            fprintf(stderr, "16-bit exp error: %d: got %u, expected %f"
-                  " error: %f\n", i, png_exp16bit(i), correct, error);
+            fprintf(stderr,
+               "16-bit exp error: %d: got %u, expected %f error: %f\n",
+               i, png_exp16bit(i), correct, error);
          }
       }
 
diff --git a/contrib/libtests/timepng.c b/contrib/libtests/timepng.c
index 23d8b17..a592c8c 100644
--- a/contrib/libtests/timepng.c
+++ b/contrib/libtests/timepng.c
@@ -1,9 +1,8 @@
+
 /* timepng.c
  *
  * Copyright (c) 2013,2016 John Cunningham Bowler
  *
- * Last changed in libpng 1.6.22 [May 26, 2016]
- *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
  * and license in png.h