[libpng16] Added symbol prefixing that allows all the libpng external symbols

to be prefixed (suggested by Reuben Hawkins).  This is work in progress that
breaks some non-configure builds.
diff --git a/ANNOUNCE b/ANNOUNCE
index f7bc6b3..1aad932 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -157,6 +157,8 @@
   Cleaned up pngpriv.h to consistently declare all functions and data.
     Also eliminated PNG_CONST_DATA, which is apparently not needed but we
     can't be sure until it is gone.
+  Added symbol prefixing that allows all the libpng external symbols
+    to be prefixed (suggested by Reuben Hawkins).
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/CHANGES b/CHANGES
index d7d8672..ee0a51a 100644
--- a/CHANGES
+++ b/CHANGES
@@ -3908,6 +3908,8 @@
   Cleaned up pngpriv.h to consistently declare all functions and data.
     Also eliminated PNG_CONST_DATA, which is apparently not needed but we
     can't be sure until it is gone.
+  Added symbol prefixing that allows all the libpng external symbols
+    to be prefixed (suggested by Reuben Hawkins).
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/Makefile.am b/Makefile.am
index a8adba3..cdbf7fc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -83,7 +83,7 @@
 
 CLEANFILES= *.tf? pngout.png libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@.pc \
 	libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@-config libpng.vers libpng.sym \
-	check.new pnglibconf.* symbols.new pngtest-log.txt \
+	check.new pnglibconf.* pngprefix.h symbols.new pngtest-log.txt \
 	$(SCRIPT_CLEANFILES)
 
 MAINTAINERCLEANFILES = Makefile.in aclocal.m4 config.guess config.h.in \
@@ -111,6 +111,7 @@
 	cp libpng-config $@
 
 scripts/sym.out scripts/vers.out: png.h pngconf.h pnglibconf.h
+scripts/prefix.out: png.h pngconf.h pnglibconf.out
 scripts/symbols.out: png.h pngconf.h $(srcdir)/scripts/pnglibconf.h.prebuilt
 
 libpng.sym: scripts/sym.out
@@ -119,9 +120,34 @@
 libpng.vers: scripts/vers.out
 	rm -f $@
 	cp $? $@
+
+if prefix
+# Rename functions in scripts/prefix.out with a PNG_PREFIX prefix.
+# Rename macros in scripts/macro.lst from PNG_PREFIXpng_ to PNG_ (the actual
+# implementation of the macro).
+pnglibconf.h: pnglibconf.out scripts/prefix.out scripts/macro.lst
+	rm -f $@
+	$(AWK) 's==0 && NR>1{print prev}\
+	   s==0{prev=$$0}\
+	   s==1{print "#define", $$1, "@PNG_PREFIX@" $$1}\
+	   s==2{print "#define @PNG_PREFIX@png_" $$1, "PNG_" $$1}\
+	   END{print prev}' s=0 pnglibconf.out s=1 scripts/prefix.out\
+	   s=2 ${srcdir}/scripts/macro.lst >pnglibconf.tf8
+	mv pnglibconf.tf8 $@
+
+pngprefix.h: scripts/intprefix.out
+	rm -f pngprefix.tf1
+	$(AWK) '{print "#define", $$1, "@PNG_PREFIX@" $$1}' $? >pngprefix.tf1
+	mv pngprefix.tf1 $@
+else
 pnglibconf.h: pnglibconf.out
 	rm -f $@
 	cp $? $@
+
+pngprefix.h: # is empty
+	:>$@
+endif
+
 $(srcdir)/scripts/pnglibconf.h.prebuilt:
 	@echo "Attempting to build $@" >&2
 	@echo "This is a machine generated file, but if you want to make" >&2
@@ -141,6 +167,10 @@
 		-DSYMBOL_PREFIX='$(SYMBOL_PREFIX)'\
 		-DPNG_NO_USE_READ_MACROS -DPNG_BUILDING_SYMBOL_TABLE
 
+if prefix
+SYMBOL_CFLAGS += -DPNG_PREFIX='@PNG_PREFIX@'
+endif
+
 .dfn.out:
 	rm -f $@ $*.c $*.tf[123]
 	test -d scripts || mkdir scripts
@@ -187,7 +217,7 @@
 	mv pnglibconf.tf7 $@
 
 $(libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@_la_OBJECTS): png.h pngconf.h \
-	pnglibconf.h pngpriv.h pngdebug.h pnginfo.h pngstruct.h
+	pnglibconf.h pngpriv.h pngdebug.h pnginfo.h pngstruct.h pngprefix.h
 
 test: check-am
 
diff --git a/configure.ac b/configure.ac
index fd756bf..1acc964 100644
--- a/configure.ac
+++ b/configure.ac
@@ -221,6 +221,16 @@
    [binconfigs='${binconfigs}'])
 AC_SUBST([binconfigs])
 
+# Support for prefixes to the API function names; this will generate defines
+# at the start of the build to rename exported library functions
+AC_ARG_WITH(libpng-prefix,
+   AS_HELP_STRING([[[--with-libpng-prefix]]],
+      [prefix libpng exported function (API) names with the given value]),
+   [if test "${withval:-no}" != "no"; then
+      AC_SUBST([PNG_PREFIX], [${withval}])
+    fi])
+AM_CONDITIONAL([prefix], [test "${with_libpng_prefix:-no}" != "no"])
+
 # Because GCC by default assembles code with an executable stack, even though it
 # compiles C code with a non-executable stack, it is necessary to do a fixup
 # here (this may by GCC specific)
diff --git a/png.h b/png.h
index e7b5f47..c5c02bf 100644
--- a/png.h
+++ b/png.h
@@ -1,7 +1,7 @@
 
 /* png.h - header file for PNG reference library
  *
- * libpng version 1.6.0beta08 - January 29, 2012
+ * libpng version 1.6.0beta08 - January 31, 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.0beta08 - January 29, 2012: Glenn
+ *   libpng versions 0.97, January 1998, through 1.6.0beta08 - January 31, 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.0beta08, January 29, 2012, are
+ * libpng versions 1.2.6, August 15, 2004, through 1.6.0beta08, January 31, 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:
  * =========================
  *
- *    January 29, 2012
+ *    January 31, 2012
  *
  *    Since the PNG Development group is an ad-hoc body, we can't make
  *    an official declaration.
@@ -376,7 +376,7 @@
 /* Version information for png.h - this should match the version in png.c */
 #define PNG_LIBPNG_VER_STRING "1.6.0beta08"
 #define PNG_HEADER_VERSION_STRING \
-     " libpng version 1.6.0beta08 - January 29, 2012\n"
+     " libpng version 1.6.0beta08 - January 31, 2012\n"
 
 #define PNG_LIBPNG_VER_SONUM   16
 #define PNG_LIBPNG_VER_DLLNUM  16
@@ -1949,12 +1949,10 @@
 #ifdef PNG_BENIGN_ERRORS_SUPPORTED
 /* Benign error in libpng.  Can continue, but may have a problem.
  * User can choose whether to handle as a fatal error or as a warning. */
-#  undef png_benign_error
 PNG_EXPORT(107, void, png_benign_error, (png_const_structrp png_ptr,
     png_const_charp warning_message));
 
 /* Same, chunk name is prepended to message. */
-#  undef png_chunk_benign_error
 PNG_EXPORT(108, void, png_chunk_benign_error, (png_const_structrp png_ptr,
     png_const_charp warning_message));
 
@@ -2599,7 +2597,7 @@
  * The png_get_int_32() routine assumes we are using two's complement
  * format for negative values, which is almost certainly true.
  */
-#  define png_get_uint_32(buf) \
+#  define PNG_get_uint_32(buf) \
      (((png_uint_32)(*(buf)) << 24) + \
       ((png_uint_32)(*((buf) + 1)) << 16) + \
       ((png_uint_32)(*((buf) + 2)) << 8) + \
@@ -2608,15 +2606,31 @@
    /* From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the
     * function) incorrectly returned a value of type png_uint_32.
     */
-#  define png_get_uint_16(buf) \
+#  define PNG_get_uint_16(buf) \
      ((png_uint_16) \
       (((unsigned int)(*(buf)) << 8) + \
        ((unsigned int)(*((buf) + 1)))))
 
-#  define png_get_int_32(buf) \
+#  define PNG_get_int_32(buf) \
      ((png_int_32)((*(buf) & 0x80) \
       ? -((png_int_32)((png_get_uint_32(buf) ^ 0xffffffffL) + 1)) \
       : (png_int_32)png_get_uint_32(buf)))
+
+   /* If PNG_PREFIX is defined the same thing as below happens in pnglibconf.h,
+    * but defining a macro name prefixed with PNG_PREFIX.
+    */
+#  ifndef PNG_PREFIX
+#     define png_get_uint_32(buf) PNG_get_uint_32(buf)
+#     define png_get_uint_16(buf) PNG_get_uint_16(buf)
+#     define png_get_int_32(buf)  PNG_get_int_32(buf)
+#  endif
+#else
+#  ifdef PNG_PREFIX
+      /* No macros; revert to the (redefined) function */
+#     define PNG_get_uint_32 (png_get_uint_32)
+#     define PNG_get_uint_16 (png_get_uint_16)
+#     define PNG_get_int_32  (png_get_int_32)
+#  endif
 #endif
 
 /*******************************************************************************
diff --git a/pngpriv.h b/pngpriv.h
index 2ae5122..731b4ef 100644
--- a/pngpriv.h
+++ b/pngpriv.h
@@ -139,6 +139,7 @@
 #include "png.h"
 #include "pnginfo.h"
 #include "pngstruct.h"
+#include "pngprefix.h"
 
 /* pngconf.h does not set PNG_DLL_EXPORT unless it is required, so: */
 #ifndef PNG_DLL_EXPORT
@@ -360,9 +361,14 @@
  * To enable listing global, but internal, symbols the following macros should
  * always be used to declare an extern data or function object in this file.
  */
-#define PNG_INTERNAL_DATA(type, name, array) extern type name array
-#define PNG_INTERNAL_FUNCTION(type, name, args, attributes)\
-   extern PNG_FUNCTION(type, name, args, PNG_EMPTY attributes)
+#ifndef PNG_INTERNAL_DATA
+#  define PNG_INTERNAL_DATA(type, name, array) extern type name array
+#endif
+
+#ifndef PNG_INTERNAL_FUNCTION
+#  define PNG_INTERNAL_FUNCTION(type, name, args, attributes)\
+      extern PNG_FUNCTION(type, name, args, PNG_EMPTY attributes)
+#endif
 
 /* CONSTANTS and UTILITY MACROS
  * These are used internally by libpng and not exposed in the API
diff --git a/scripts/intprefix.dfn b/scripts/intprefix.dfn
new file mode 100644
index 0000000..346a6fd
--- /dev/null
+++ b/scripts/intprefix.dfn
@@ -0,0 +1,18 @@
+
+/* intprefix.dfn - generate an unprefixed internal symbol list
+ *
+ * Last changed in libpng version 1.6.0 [January 30, 2012]
+ * Copyright (c) 2012 Glenn Randers-Pehrson
+ *
+ * This code is released under the libpng license.
+ * For conditions of distribution and use, see the disclaimer
+ * and license in png.h
+ */
+
+#define PNG_INTERNAL_DATA(type, name, array)\
+	PNG_DEFN_MAGIC-name-PNG_DEFN_END
+
+#define PNG_INTERNAL_FUNCTION(type, name, args, attributes)\
+	PNG_DEFN_MAGIC-name-PNG_DEFN_END
+
+#include "../pngpriv.h"
diff --git a/scripts/macro.lst b/scripts/macro.lst
new file mode 100644
index 0000000..57124b7
--- /dev/null
+++ b/scripts/macro.lst
@@ -0,0 +1,3 @@
+get_uint_32(buf)
+get_uint_16(buf)
+get_int_32(buf)
diff --git a/scripts/pnglibconf.dfa b/scripts/pnglibconf.dfa
index d3d199d..3150451 100644
--- a/scripts/pnglibconf.dfa
+++ b/scripts/pnglibconf.dfa
@@ -161,6 +161,12 @@
 
 setting API_RULE default 0
 
+# This allows a prefix to be added to the front of every API functon name (and
+# therefore every symbol) by redefining all the function names with the prefix
+# in a new file "pngprefix.h"; this file must be installed in png.h if this
+# facility is used.
+setting PREFIX
+
 # Default to using the read macros
 
 setting DEFAULT_READ_MACROS default 1
diff --git a/scripts/prefix.dfn b/scripts/prefix.dfn
new file mode 100644
index 0000000..1e8561f
--- /dev/null
+++ b/scripts/prefix.dfn
@@ -0,0 +1,20 @@
+
+/* prefix.dfn - generate an unprefixed symbol list
+ *
+ * Last changed in libpng version 1.6.0 [January 30, 2012]
+ * Copyright (c) 2012 Glenn Randers-Pehrson
+ *
+ * This code is released under the libpng license.
+ * For conditions of distribution and use, see the disclaimer
+ * and license in png.h
+ */
+
+#define PNG_EXPORTA(ordinal, type, name, args, attributes)\
+	PNG_DEFN_MAGIC-name-PNG_DEFN_END
+
+/* The configuration information *before* the additional of symbol renames,
+ * the list is the C name list; no symbol prefix.
+ */
+#include "pnglibconf.out"
+
+#include "../png.h"