fixed problem in xmlTextWriterVSprintf caused by misuse of vsnprintf added

* xmlwriter.c: fixed problem in xmlTextWriterVSprintf caused by
  misuse of vsnprintf
* configure.in, config.h.in: added a configuration check for
  va_copy and added a define for VA_COPY for xmlwriter.c fix
* parser.c: fixed problem with CRLF split between chunks (bug
  #319279) (fix provided by Brion Vibber)
diff --git a/ChangeLog b/ChangeLog
index 942f8ef..2c9bbb6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Thu Oct 20 17:05:29 HKT 2005 William Brack <wbrack@mmm.com.hk>
+
+	* xmlwriter.c: fixed problem in xmlTextWriterVSprintf caused by
+	  misuse of vsnprintf
+	* configure.in, config.h.in: added a configuration check for
+	  va_copy and added a define for VA_COPY for xmlwriter.c fix
+	* parser.c: fixed problem with CRLF split between chunks (bug
+	  #319279) (fix provided by Brion Vibber)
+
 Wed Oct 19 18:49:52 CEST 2005 Kasimier Buchcik <libxml2-cvs@cazic.net>
 
 	* xmlschemas.c: Fixed a potential memory leak in
diff --git a/config.h.in b/config.h.in
index 8484193..9b03c22 100644
--- a/config.h.in
+++ b/config.h.in
@@ -237,6 +237,9 @@
 /* Define to 1 if you have the <unistd.h> header file. */
 #undef HAVE_UNISTD_H
 
+/* Whether va_copy() is available */
+#undef HAVE_VA_COPY
+
 /* Define to 1 if you have the `vfprintf' function. */
 #undef HAVE_VFPRINTF
 
@@ -252,6 +255,9 @@
 /* Define to 1 if you have the `_stat' function. */
 #undef HAVE__STAT
 
+/* Whether __va_copy() is available */
+#undef HAVE___VA_COPY
+
 /* Name of package */
 #undef PACKAGE
 
diff --git a/configure.in b/configure.in
index 6119d61..e80e1c6 100644
--- a/configure.in
+++ b/configure.in
@@ -432,6 +432,27 @@
 AC_CHECK_FUNCS(printf sprintf fprintf snprintf vfprintf vsprintf vsnprintf sscanf,,
                NEED_TRIO=1)
 
+dnl Checking for va_copy availability
+AC_MSG_CHECKING([for va_copy])
+AC_TRY_LINK([#include <stdarg.h>
+va_list ap1,ap2;], [va_copy(ap1,ap2);],
+have_va_copy=yes,
+have_va_copy=no)
+AC_MSG_RESULT($have_va_copy)
+if test x"$have_va_copy" = x"yes"; then
+    AC_DEFINE(HAVE_VA_COPY,1,[Whether va_copy() is available])
+else
+    AC_MSG_CHECKING([for __va_copy])
+    AC_TRY_LINK([#include <stdarg.h>
+    va_list ap1,ap2;], [__va_copy(ap1,ap2);],
+    have___va_copy=yes,
+    have___va_copy=no)
+    AC_MSG_RESULT($have___va_copy)
+    if test x"$have___va_copy" = x"yes"; then
+        AC_DEFINE(HAVE___VA_COPY,1,[Whether __va_copy() is available])
+    fi
+fi
+
 dnl Checks for inet libraries:
 AC_CHECK_FUNC(gethostent, , AC_CHECK_LIB(nsl, gethostent))
 AC_CHECK_FUNC(setsockopt, , AC_CHECK_LIB(socket, setsockopt))
diff --git a/parser.c b/parser.c
index aeb8ab3..459c5e9 100644
--- a/parser.c
+++ b/parser.c
@@ -3549,6 +3549,8 @@
 		    ctxt->input->line++; ctxt->input->col = 1;
 		    continue; /* while */
 		}
+		if (!*in)	/* if end of current chunk return */
+		    return;
 		in--;
 	    }
 	    if (*in == '<') {
diff --git a/xmlwriter.c b/xmlwriter.c
index 6227398..25e45fa 100644
--- a/xmlwriter.c
+++ b/xmlwriter.c
@@ -25,6 +25,25 @@
 #define B64CRLF "\r\n"
 
 /*
+ * The following VA_COPY was coded following an example in
+ * the Samba project.  It may not be sufficient for some
+ * esoteric implementations of va_list (i.e. it may need
+ * something involving a memcpy) but (hopefully) will be
+ * sufficient for libxml2.
+ */
+#ifndef VA_COPY
+  #ifdef HAVE_VA_COPY
+    #define VA_COPY(dest, src) va_copy(dest, src)
+  #else
+    #ifdef HAVE___VA_COPY
+      #define VA_COPY(dest,src) __va_copy(dest, src)
+    #else
+      #define VA_COPY(dest,src) (dest) = (src)
+    #endif
+  #endif
+#endif
+
+/*
  * Types are kept private
  */
 typedef enum {
@@ -4373,6 +4392,7 @@
     int size;
     int count;
     xmlChar *buf;
+    va_list locarg;
 
     size = BUFSIZ;
     buf = (xmlChar *) xmlMalloc(size);
@@ -4382,8 +4402,10 @@
         return NULL;
     }
 
-    while (((count = vsnprintf((char *) buf, size, format, argptr)) < 0)
+    VA_COPY(locarg, argptr);
+    while (((count = vsnprintf((char *) buf, size, format, locarg)) < 0)
            || (count == size - 1) || (count == size) || (count > size)) {
+	va_end(locarg);
         xmlFree(buf);
         size += BUFSIZ;
         buf = (xmlChar *) xmlMalloc(size);
@@ -4392,7 +4414,9 @@
                             "xmlTextWriterVSprintf : out of memory!\n");
             return NULL;
         }
+	VA_COPY(locarg, argptr);
     }
+    va_end(locarg);
 
     return buf;
 }