[libpng16] Revised png_set_keep_unknown_chunks() so num_chunks < 0 means

ignore all unknown chunks and all known chunks except for IHDR, PLTE, tRNS,
IDAT, and IEND.  Previously it only meant ignore all unknown chunks, the
same as num_chunks == 0. Revised png_image_skip_unused_chunks() to
provide a list of chunks to be processed instead of a list of chunks to
ignore.  Revised contrib/gregbook/readpng2.c accordingly.
diff --git a/ANNOUNCE b/ANNOUNCE
index 10a572b..2748641 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -1,5 +1,5 @@
 
-Libpng 1.6.0beta25 - June 12, 2012
+Libpng 1.6.0beta25 - June 16, 2012
 
 This is not intended to be a public release.  It will be replaced
 within a few weeks by a public version or by another test version.
@@ -385,7 +385,13 @@
 Version 1.6.0beta24 [June 7, 2012]
   Don't check palette indexes if num_palette is 0 (as it can be in MNG files).
 
-Version 1.6.0beta25 [June 12, 2012]
+Version 1.6.0beta25 [June 16, 2012]
+  Revised png_set_keep_unknown_chunks() so num_chunks < 0 means ignore all
+    unknown chunks and all known chunks except for IHDR, PLTE, tRNS, IDAT,
+    and IEND.  Previously it only meant ignore all unknown chunks, the
+    same as num_chunks == 0. Revised png_image_skip_unused_chunks() to
+    provide a list of chunks to be processed instead of a list of chunks to
+    ignore.  Revised contrib/gregbook/readpng2.c accordingly.
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/CHANGES b/CHANGES
index 52a60a0..a08218d 100644
--- a/CHANGES
+++ b/CHANGES
@@ -4136,7 +4136,13 @@
 Version 1.6.0beta24 [June 7, 2012]
   Don't check palette indexes if num_palette is 0 (as it can be in MNG files).
 
-Version 1.6.0beta25 [June 12, 2012]
+Version 1.6.0beta25 [June 16, 2012]
+  Revised png_set_keep_unknown_chunks() so num_chunks < 0 means ignore all
+    unknown chunks and all known chunks except for IHDR, PLTE, tRNS, IDAT,
+    and IEND.  Previously it only meant ignore all unknown chunks, the
+    same as num_chunks == 0. Revised png_image_skip_unused_chunks() to
+    provide a list of chunks to be processed instead of a list of chunks to
+    ignore.  Revised contrib/gregbook/readpng2.c accordingly.
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/contrib/gregbook/readpng2.c b/contrib/gregbook/readpng2.c
index b9746b7..e179db7 100644
--- a/contrib/gregbook/readpng2.c
+++ b/contrib/gregbook/readpng2.c
@@ -136,29 +136,23 @@
      * used, i.e., all chunks recognized by libpng except for IHDR, PLTE, IDAT,
      * IEND, tRNS, bKGD, gAMA, and sRGB (small performance improvement) */
     {
-        /* These byte strings were copied from png.h.  If a future libpng
-         * version recognizes more chunks, add them to this list.  If a
-         * future version of readpng2.c recognizes more chunks, delete them
-         * from this list. */
-        static /* const */ png_byte chunks_to_ignore[] = {
-             99,  72,  82,  77, '\0',  /* cHRM */
-            104,  73,  83,  84, '\0',  /* hIST */
-            105,  67,  67,  80, '\0',  /* iCCP */
-            105,  84,  88, 116, '\0',  /* iTXt */
-            111,  70,  70, 115, '\0',  /* oFFs */
-            112,  67,  65,  76, '\0',  /* pCAL */
-            112,  72,  89, 115, '\0',  /* pHYs */
-            115,  66,  73,  84, '\0',  /* sBIT */
-            115,  67,  65,  76, '\0',  /* sCAL */
-            115,  80,  76,  84, '\0',  /* sPLT */
-            115,  84,  69,  82, '\0',  /* sTER */
-            116,  69,  88, 116, '\0',  /* tEXt */
-            116,  73,  77,  69, '\0',  /* tIME */
-            122,  84,  88, 116, '\0'   /* zTXt */
-        };
+        /* These byte strings were copied from png.h.  If a future version
+         * of readpng2.c recognizes more chunks, add them to this list.
+         */
+        static PNG_CONST png_byte chunks_to_process[] = {
+            98,  75,  71,  68, '\0',  /* bKGD */
+           103,  65,  77,  65, '\0',  /* gAMA */
+           115,  82,  71,  66, '\0',  /* sRGB */
+           };
 
-        png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
-          chunks_to_ignore, sizeof(chunks_to_ignore)/5);
+       /* Ignore all chunks except for IHDR, PLTE, tRNS, IDAT, and IEND */
+       png_set_keep_unknown_chunks(png_ptr, -1 /* PNG_HANDLE_CHUNK_NEVER */,
+          NULL, -1);
+
+       /* But do not ignore chunks in the "chunks_to_process" list */
+       png_set_keep_unknown_chunks(png_ptr,
+          0 /* PNG_HANDLE_CHUNK_AS_DEFAULT */, chunks_to_process,
+          sizeof(chunks_to_process)/5);
     }
 #endif /* PNG_HANDLE_AS_UNKNOWN_SUPPORTED */
 
diff --git a/libpng-manual.txt b/libpng-manual.txt
index 2f7bca3..dda62ad 100644
--- a/libpng-manual.txt
+++ b/libpng-manual.txt
@@ -1,6 +1,6 @@
 libpng-manual.txt - A description on how to use and modify libpng
 
- libpng version 1.6.0beta25 - June 12, 2012
+ libpng version 1.6.0beta25 - June 16, 2012
  Updated and distributed by Glenn Randers-Pehrson
  <glennrp at users.sourceforge.net>
  Copyright (c) 1998-2011 Glenn Randers-Pehrson
@@ -11,7 +11,7 @@
 
  Based on:
 
- libpng versions 0.97, January 1998, through 1.6.0beta25 - June 12, 2012
+ libpng versions 0.97, January 1998, through 1.6.0beta25 - June 16, 2012
  Updated and distributed by Glenn Randers-Pehrson
  Copyright (c) 1998-2011 Glenn Randers-Pehrson
 
@@ -572,6 +572,7 @@
 
     png_set_keep_unknown_chunks(png_ptr, keep,
         chunk_list, num_chunks);
+
     keep       - 0: default unknown chunk handling
                  1: ignore; do not keep
                  2: keep only if safe-to-copy
@@ -585,11 +586,16 @@
 
     chunk_list - list of chunks affected (a byte string,
                  five bytes per chunk, NULL or '\0' if
-                 num_chunks is 0)
+                 num_chunks is positive; ignored if
+                 numchunks <= 0).
 
     num_chunks - number of chunks affected; if 0, all
-                 unknown chunks are affected.  If nonzero,
-                 only the chunks in the list are affected
+                 unknown chunks are affected.  If positive,
+                 only the chunks in the list are affected,
+                 and if negative all unknown chunks and
+                 all known chunks except for the IHDR,
+                 PLTE, tRNS, IDAT, and IEND chunks are
+                 affected.
 
 Unknown chunks declared in this way will be saved as raw data onto a
 list of png_unknown_chunk structures.  If a chunk that is normally
@@ -5013,7 +5019,7 @@
 
 XVI. Y2K Compliance in libpng
 
-June 12, 2012
+June 16, 2012
 
 Since the PNG Development group is an ad-hoc body, we can't make
 an official declaration.
@@ -5022,16 +5028,16 @@
 upward through 1.6.0beta25 are Y2K compliant.  It is my belief that earlier
 versions were also Y2K compliant.
 
-Libpng only has three year fields.  One is a 2-byte unsigned integer that
-will hold years up to 65535.  The other two hold the date in text
-format, and will hold years up to 9999.
+Libpng only has two year fields.  One is a 2-byte unsigned integer
+that will hold years up to 65535.  The other, which is deprecated,
+holds the date in text format, and will hold years up to 9999.
 
 The integer is
     "png_uint_16 year" in png_time_struct.
 
-The strings are
-    "png_charp time_buffer" in png_struct and
-    "near_time_buffer", which is a local character string in png.c.
+The string is
+    "char time_buffer[29]" in png_struct.  This is no longer used
+in libpng-1.6.x and will be removed from libpng-1.7.0.
 
 There are seven time-related functions:
 
diff --git a/libpng.3 b/libpng.3
index 3218d30..2916889 100644
--- a/libpng.3
+++ b/libpng.3
@@ -1,4 +1,4 @@
-.TH LIBPNG 3 "June 12, 2012"
+.TH LIBPNG 3 "June 16, 2012"
 .SH NAME
 libpng \- Portable Network Graphics (PNG) Reference Library 1.6.0beta25
 .SH SYNOPSIS
@@ -1007,7 +1007,7 @@
 .SH LIBPNG.TXT
 libpng-manual.txt - A description on how to use and modify libpng
 
- libpng version 1.6.0beta25 - June 12, 2012
+ libpng version 1.6.0beta25 - June 16, 2012
  Updated and distributed by Glenn Randers-Pehrson
  <glennrp at users.sourceforge.net>
  Copyright (c) 1998-2011 Glenn Randers-Pehrson
@@ -1018,7 +1018,7 @@
 
  Based on:
 
- libpng versions 0.97, January 1998, through 1.6.0beta25 - June 12, 2012
+ libpng versions 0.97, January 1998, through 1.6.0beta25 - June 16, 2012
  Updated and distributed by Glenn Randers-Pehrson
  Copyright (c) 1998-2011 Glenn Randers-Pehrson
 
@@ -1579,6 +1579,7 @@
 
     png_set_keep_unknown_chunks(png_ptr, keep,
         chunk_list, num_chunks);
+
     keep       - 0: default unknown chunk handling
                  1: ignore; do not keep
                  2: keep only if safe-to-copy
@@ -1592,11 +1593,16 @@
 
     chunk_list - list of chunks affected (a byte string,
                  five bytes per chunk, NULL or '\0' if
-                 num_chunks is 0)
+                 num_chunks is positive; ignored if
+                 numchunks <= 0).
 
     num_chunks - number of chunks affected; if 0, all
-                 unknown chunks are affected.  If nonzero,
-                 only the chunks in the list are affected
+                 unknown chunks are affected.  If positive,
+                 only the chunks in the list are affected,
+                 and if negative all unknown chunks and
+                 all known chunks except for the IHDR,
+                 PLTE, tRNS, IDAT, and IEND chunks are
+                 affected.
 
 Unknown chunks declared in this way will be saved as raw data onto a
 list of png_unknown_chunk structures.  If a chunk that is normally
@@ -6021,7 +6027,7 @@
 
 .SH XVI. Y2K Compliance in libpng
 
-June 12, 2012
+June 16, 2012
 
 Since the PNG Development group is an ad-hoc body, we can't make
 an official declaration.
@@ -6030,16 +6036,16 @@
 upward through 1.6.0beta25 are Y2K compliant.  It is my belief that earlier
 versions were also Y2K compliant.
 
-Libpng only has three year fields.  One is a 2-byte unsigned integer that
-will hold years up to 65535.  The other two hold the date in text
-format, and will hold years up to 9999.
+Libpng only has two year fields.  One is a 2-byte unsigned integer
+that will hold years up to 65535.  The other, which is deprecated,
+holds the date in text format, and will hold years up to 9999.
 
 The integer is
     "png_uint_16 year" in png_time_struct.
 
-The strings are
-    "png_charp time_buffer" in png_struct and
-    "near_time_buffer", which is a local character string in png.c.
+The string is
+    "char time_buffer[29]" in png_struct.  This is no longer used
+in libpng-1.6.x and will be removed from libpng-1.7.0.
 
 There are seven time-related functions:
 
@@ -6289,7 +6295,7 @@
 
 Thanks to Frank J. T. Wojcik for helping with the documentation.
 
-Libpng version 1.6.0beta25 - June 12, 2012:
+Libpng version 1.6.0beta25 - June 16, 2012:
 Initially created in 1995 by Guy Eric Schalnat, then of Group 42, Inc.
 Currently maintained by Glenn Randers-Pehrson (glennrp at users.sourceforge.net).
 
@@ -6312,7 +6318,7 @@
 
 This code is released under the libpng license.
 
-libpng versions 1.2.6, August 15, 2004, through 1.6.0beta25, June 12, 2012, are
+libpng versions 1.2.6, August 15, 2004, through 1.6.0beta25, June 16, 2012, are
 Copyright (c) 2004,2006-2007 Glenn Randers-Pehrson, and are
 distributed according to the same disclaimer and license as libpng-1.2.5
 with the following individual added to the list of Contributing Authors
@@ -6411,7 +6417,7 @@
 
 Glenn Randers-Pehrson
 glennrp at users.sourceforge.net
-June 12, 2012
+June 16, 2012
 
 .\" end of man page
 
diff --git a/png.h b/png.h
index b177fd5..8151eab 100644
--- a/png.h
+++ b/png.h
@@ -1,7 +1,7 @@
 
 /* png.h - header file for PNG reference library
  *
- * libpng version 1.6.0beta25 - June 12, 2012
+ * libpng version 1.6.0beta25 - June 16, 2012
  * Copyright (c) 1998-2012 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -11,7 +11,7 @@
  * Authors and maintainers:
  *   libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat
  *   libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger
- *   libpng versions 0.97, January 1998, through 1.6.0beta25 - June 12, 2012: Glenn
+ *   libpng versions 0.97, January 1998, through 1.6.0beta25 - June 16, 2012: Glenn
  *   See also "Contributing Authors", below.
  *
  * Note about libpng version numbers:
@@ -198,7 +198,7 @@
  *
  * This code is released under the libpng license.
  *
- * libpng versions 1.2.6, August 15, 2004, through 1.6.0beta25, June 12, 2012, are
+ * libpng versions 1.2.6, August 15, 2004, through 1.6.0beta25, June 16, 2012, are
  * Copyright (c) 2004, 2006-2012 Glenn Randers-Pehrson, and are
  * distributed according to the same disclaimer and license as libpng-1.2.5
  * with the following individual added to the list of Contributing Authors:
@@ -310,7 +310,7 @@
  * Y2K compliance in libpng:
  * =========================
  *
- *    June 12, 2012
+ *    June 16, 2012
  *
  *    Since the PNG Development group is an ad-hoc body, we can't make
  *    an official declaration.
@@ -320,14 +320,15 @@
  *    earlier versions were also Y2K compliant.
  *
  *    Libpng only has two year fields.  One is a 2-byte unsigned integer
- *    that will hold years up to 65535.  The other holds the date in text
- *    format, and will hold years up to 9999.
+ *    that will hold years up to 65535.  The other, which is deprecated,
+ *    holds the date in text format, and will hold years up to 9999.
  *
  *    The integer is
  *        "png_uint_16 year" in png_time_struct.
  *
  *    The string is
- *        "png_char time_buffer" in png_struct
+ *        "char time_buffer[29]" in png_struct.  This is no longer used
+ *    in libpng-1.6.x and will be removed from libpng-1.7.0.
  *
  *    There are seven time-related functions:
  *        png.c: png_convert_to_rfc_1123_buffer() in png.c
@@ -377,7 +378,7 @@
 /* Version information for png.h - this should match the version in png.c */
 #define PNG_LIBPNG_VER_STRING "1.6.0beta25"
 #define PNG_HEADER_VERSION_STRING \
-     " libpng version 1.6.0beta25 - June 12, 2012\n"
+     " libpng version 1.6.0beta25 - June 16, 2012\n"
 
 #define PNG_LIBPNG_VER_SONUM   16
 #define PNG_LIBPNG_VER_DLLNUM  16
@@ -2318,15 +2319,22 @@
 
 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
 /* Provide a list of chunks and how they are to be handled, if the built-in
-   handling or default unknown chunk handling is not desired.  Any chunks not
-   listed will be handled in the default manner.  The IHDR and IEND chunks
-   must not be listed.  Because this turns off the default handling for chunks
-   that would otherwise be recognized the behavior of libpng transformations may
-   well become incorrect!
-      keep = 0: PNG_HANDLE_CHUNK_AS_DEFAULT: follow default behavior
-           = 1: PNG_HANDLE_CHUNK_NEVER:      do not keep
-           = 2: PNG_HANDLE_CHUNK_IF_SAFE:    keep only if safe-to-copy
-           = 3: PNG_HANDLE_CHUNK_ALWAYS:     keep even if unsafe-to-copy
+ * handling or default unknown chunk handling is not desired.  Any chunks not
+ * listed will be handled in the default manner.  The IHDR and IEND chunks
+ * must not be listed.  Because this turns off the default handling for chunks
+ * that would otherwise be recognized the behavior of libpng transformations may
+ * well become incorrect!
+ *    keep = 0: PNG_HANDLE_CHUNK_AS_DEFAULT: follow default behavior
+ *         = 1: PNG_HANDLE_CHUNK_NEVER:      do not keep
+ *         = 2: PNG_HANDLE_CHUNK_IF_SAFE:    keep only if safe-to-copy
+ *         = 3: PNG_HANDLE_CHUNK_ALWAYS:     keep even if unsafe-to-copy
+ * If num_chunks is 0, then the "keep" parameter specifies the default
+ * manner for handling unknown chunks.  If num_chunks is positive, then
+ * the "keep" parameter specifies the manner for handling only those chunks
+ * appearing in the chunk_list array.  If it is negative, then the "keep"
+ * parameter specifies the manner for handling all unknown chunks plus
+ * all chunks recognized by libpng except for the IHDR, PLTE, tRNS, IDAT,
+ * and IEND chunks.
 */
 PNG_EXPORT(172, void, png_set_keep_unknown_chunks, (png_structrp png_ptr,
     int keep, png_const_bytep chunk_list, int num_chunks));
diff --git a/pngread.c b/pngread.c
index 32b01dd..928525f 100644
--- a/pngread.c
+++ b/pngread.c
@@ -1462,31 +1462,26 @@
     *
     * This provides a small performance improvement and eliminates any
     * potential vulnerability to security problems in the unused chunks.
-    *
-    * TODO: make it so that this is an explicit list to process, not a list
-    * to ignore?
     */
    {
-       static PNG_CONST png_byte chunks_to_ignore[] = {
-           104,  73,  83,  84, '\0',  /* hIST */
-           105,  84,  88, 116, '\0',  /* iTXt */
-           111,  70,  70, 115, '\0',  /* oFFs */
-           112,  67,  65,  76, '\0',  /* pCAL */
-           112,  72,  89, 115, '\0',  /* pHYs */
-           115,  67,  65,  76, '\0',  /* sCAL */
-           115,  80,  76,  84, '\0',  /* sPLT */
-           116,  69,  88, 116, '\0',  /* tEXt */
-           116,  73,  77,  69, '\0',  /* tIME */
-           122,  84,  88, 116, '\0'   /* zTXt */
-       };
+         static PNG_CONST png_byte chunks_to_process[] = {
+            98,  75,  71,  68, '\0',  /* bKGD */
+            99,  72,  82,  77, '\0',  /* cHRM */
+           103,  65,  77,  65, '\0',  /* gAMA */
+           105,  67,  67,  80, '\0',  /* iCCP */
+           115,  66,  73,  84, '\0',  /* sBIT */
+           115,  82,  71,  66, '\0',  /* sRGB */
+           };
 
-       /* Ignore unknown chunks */
+       /* Ignore unknown chunks and all other chunks except for the
+        * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
+        */
        png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
-         NULL, 0);
+         NULL, -1);
 
-       /* Ignore known but unused chunks */
-       png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */,
-         chunks_to_ignore, (sizeof chunks_to_ignore)/5);
+       /* But do not ignore image data handling chunks */
+       png_set_keep_unknown_chunks(png_ptr, 0 /* PNG_HANDLE_CHUNK_AS_DEFAULT */,
+         chunks_to_process, (sizeof chunks_to_process)/5);
     }
 }
 
diff --git a/pngset.c b/pngset.c
index f197c84..65cb695 100644
--- a/pngset.c
+++ b/pngset.c
@@ -1183,14 +1183,44 @@
       else
          png_ptr->flags &= ~PNG_FLAG_KEEP_UNSAFE_CHUNKS;
 
-      return;
+      if (num_chunksIn == 0)
+        return;
    }
 
+   if (num_chunksIn < 0)
+      {
+         /* Ignore all unknown chunks and all chunks recognized by
+          * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
+          */
+         static PNG_CONST png_byte chunks_to_ignore[] = {
+            98,  75,  71,  68, '\0',  /* bKGD */
+            99,  72,  82,  77, '\0',  /* cHRM */
+           103,  65,  77,  65, '\0',  /* gAMA */
+           104,  73,  83,  84, '\0',  /* hIST */
+           105,  67,  67,  80, '\0',  /* iCCP */
+           105,  84,  88, 116, '\0',  /* iTXt */
+           111,  70,  70, 115, '\0',  /* oFFs */
+           112,  67,  65,  76, '\0',  /* pCAL */
+           112,  72,  89, 115, '\0',  /* pHYs */
+           115,  66,  73,  84, '\0',  /* sBIT */
+           115,  67,  65,  76, '\0',  /* sCAL */
+           115,  80,  76,  84, '\0',  /* sPLT */
+           115,  84,  69,  82, '\0',  /* sTER */
+           115,  82,  71,  66, '\0',  /* sRGB */
+           116,  69,  88, 116, '\0',  /* tEXt */
+           116,  73,  77,  69, '\0',  /* tIME */
+           122,  84,  88, 116, '\0'   /* zTXt */
+           };
+
+         chunk_list = chunks_to_ignore;
+         num_chunks = (unsigned int) sizeof(chunks_to_ignore)/5;
+       }
+
    if (chunk_list == NULL)
       return;
 
-   /* The argument is >0 */
-   num_chunks = (unsigned int)num_chunksIn;
+   if (num_chunksIn > 0)
+     num_chunks = (unsigned int)num_chunksIn;
 
    old_num_chunks = png_ptr->num_chunk_list;
    new_list = png_voidcast(png_bytep, png_malloc(png_ptr,