CORBA defines fixes, char encoding atodetection, Daniel
diff --git a/ChangeLog b/ChangeLog
index 90fadfe..2331fd7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,10 +1,14 @@
+Sat May 29 13:34:42 CEST 1999 Daniel Veillard <Daniel.Veillard@w3.org>
+
+	* tree.[ch]: unified the XML_NO_CORBA defines.
+	* parser.c encoding.[ch]: started plugging in char encoding detection
+
 Fri May 28 22:58:42 EDT 1999 Manish Vachharajani <mvachhar@vger.rutgers.edu>
 
 	* tree.c: (xmlSaveFile) - removed double call of xmlContentDump.  
 	  Also freed allocated buffer.
 
-Wed Apr 21 22:07:35 CEST 1999
-
+Wed Apr 21 22:07:35 CEST 1999 Daniel Veillard <Daniel.Veillard@w3.org>
 	* parser.[ch] tree.[ch] entities.[ch] valid.[ch] : removed the main
 	  reentrancy problem at printing. One is left in entities.c, to
 	  remove ASAP
diff --git a/SAX.c b/SAX.c
index 7f9ea18..596f668 100644
--- a/SAX.c
+++ b/SAX.c
@@ -26,8 +26,9 @@
  * Returns a CHAR *
  */
 const CHAR *
-getPublicId(xmlParserCtxtPtr ctxt)
+getPublicId(void *ctx)
 {
+    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
     return(NULL);
 }
 
@@ -41,8 +42,9 @@
  * Returns a CHAR *
  */
 const CHAR *
-getSystemId(xmlParserCtxtPtr ctxt)
+getSystemId(void *ctx)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     return(ctxt->input->filename); 
 }
 
@@ -55,8 +57,9 @@
  * Returns an int
  */
 int
-getLineNumber(xmlParserCtxtPtr ctxt)
+getLineNumber(void *ctx)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     return(ctxt->input->line);
 }
 
@@ -69,8 +72,9 @@
  * Returns an int
  */
 int
-getColumnNumber(xmlParserCtxtPtr ctxt)
+getColumnNumber(void *ctx)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     return(ctxt->input->col);
 }
 
@@ -91,8 +95,9 @@
  * Returns 1 if true
  */
 int
-isStandalone(xmlParserCtxtPtr ctxt)
+isStandalone(void *ctx)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     return(ctxt->myDoc->standalone == 1);
 }
 
@@ -105,8 +110,9 @@
  * Returns 1 if true
  */
 int
-hasInternalSubset(xmlParserCtxtPtr ctxt)
+hasInternalSubset(void *ctx)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     return(ctxt->myDoc->intSubset != NULL);
 }
 
@@ -119,8 +125,9 @@
  * Returns 1 if true
  */
 int
-hasExternalSubset(xmlParserCtxtPtr ctxt)
+hasExternalSubset(void *ctx)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     return(ctxt->myDoc->extSubset != NULL);
 }
 
@@ -131,9 +138,10 @@
  * Does this document has an internal subset
  */
 void
-internalSubset(xmlParserCtxtPtr ctxt, const CHAR *name,
+internalSubset(void *ctx, const CHAR *name,
 	       const CHAR *ExternalID, const CHAR *SystemID)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.internalSubset(%s, %s, %s)\n",
             name, ExternalID, SystemID);
@@ -156,8 +164,9 @@
  * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
  */
 xmlParserInputPtr
-resolveEntity(xmlParserCtxtPtr ctxt, const CHAR *publicId, const CHAR *systemId)
+resolveEntity(void *ctx, const CHAR *publicId, const CHAR *systemId)
 {
+    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
 
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.resolveEntity(%s, %s)\n", publicId, systemId);
@@ -179,8 +188,9 @@
  * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
  */
 xmlEntityPtr
-getEntity(xmlParserCtxtPtr ctxt, const CHAR *name)
+getEntity(void *ctx, const CHAR *name)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlEntityPtr ret;
 
 #ifdef DEBUG_SAX
@@ -204,9 +214,10 @@
  * An entity definition has been parsed
  */
 void
-entityDecl(xmlParserCtxtPtr ctxt, const CHAR *name, int type,
+entityDecl(void *ctx, const CHAR *name, int type,
           const CHAR *publicId, const CHAR *systemId, CHAR *content)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
@@ -227,10 +238,11 @@
  * An attribute definition has been parsed
  */
 void
-attributeDecl(xmlParserCtxtPtr ctxt, const CHAR *elem, const CHAR *name,
+attributeDecl(void *ctx, const CHAR *elem, const CHAR *name,
               int type, int def, const CHAR *defaultValue,
 	      xmlEnumerationPtr tree)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
@@ -252,9 +264,10 @@
  * An element definition has been parsed
  */
 void
-elementDecl(xmlParserCtxtPtr ctxt, const CHAR *name, int type,
+elementDecl(void *ctx, const CHAR *name, int type,
 	    xmlElementContentPtr content)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.elementDecl(%s, %d, ...)\n",
@@ -274,9 +287,10 @@
  * TODO Not handled currently.
  */
 void
-notationDecl(xmlParserCtxtPtr ctxt, const CHAR *name,
+notationDecl(void *ctx, const CHAR *name,
 	     const CHAR *publicId, const CHAR *systemId)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.notationDecl(%s, %s, %s)\n", name, publicId, systemId);
 #endif
@@ -295,10 +309,11 @@
  * TODO Create an Entity node.
  */
 void
-unparsedEntityDecl(xmlParserCtxtPtr ctxt, const CHAR *name,
+unparsedEntityDecl(void *ctx, const CHAR *name,
 		   const CHAR *publicId, const CHAR *systemId,
 		   const CHAR *notationName)
 {
+    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
             name, publicId, systemId, notationName);
@@ -314,8 +329,9 @@
  * Everything is available on the context, so this is useless in our case.
  */
 void
-setDocumentLocator(xmlParserCtxtPtr ctxt, xmlSAXLocatorPtr loc)
+setDocumentLocator(void *ctx, xmlSAXLocatorPtr loc)
 {
+    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.setDocumentLocator()\n");
 #endif
@@ -328,8 +344,9 @@
  * called when the document start being processed.
  */
 void
-startDocument(xmlParserCtxtPtr ctxt)
+startDocument(void *ctx)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlDocPtr doc;
 
 #ifdef DEBUG_SAX
@@ -352,8 +369,9 @@
  * called when the document end has been detected.
  */
 void
-endDocument(xmlParserCtxtPtr ctxt)
+endDocument(void *ctx)
 {
+    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.endDocument()\n");
 #endif
@@ -371,8 +389,9 @@
  * the element.
  */
 void
-attribute(xmlParserCtxtPtr ctxt, const CHAR *fullname, const CHAR *value)
+attribute(void *ctx, const CHAR *fullname, const CHAR *value)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlAttrPtr ret;
     CHAR *name;
     CHAR *ns;
@@ -428,8 +447,9 @@
  * TODO We currently have a small pblm with the arguments ...
  */
 void
-startElement(xmlParserCtxtPtr ctxt, const CHAR *fullname, const CHAR **atts)
+startElement(void *ctx, const CHAR *fullname, const CHAR **atts)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlNodePtr ret;
     xmlNodePtr parent = ctxt->node;
     xmlNsPtr ns;
@@ -515,8 +535,9 @@
  * called when the end of an element has been detected.
  */
 void
-endElement(xmlParserCtxtPtr ctxt, const CHAR *name)
+endElement(void *ctx, const CHAR *name)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlParserNodeInfo node_info;
     xmlNodePtr cur = ctxt->node;
 
@@ -549,8 +570,9 @@
  * called when an entity reference is detected. 
  */
 void
-reference(xmlParserCtxtPtr ctxt, const CHAR *name)
+reference(void *ctx, const CHAR *name)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlNodePtr ret;
 
 #ifdef DEBUG_SAX
@@ -570,8 +592,9 @@
  * Question: how much at a time ???
  */
 void
-characters(xmlParserCtxtPtr ctxt, const CHAR *ch, int len)
+characters(void *ctx, const CHAR *ch, int len)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlNodePtr lastChild;
 
 #ifdef DEBUG_SAX
@@ -606,8 +629,9 @@
  * Question: how much at a time ???
  */
 void
-ignorableWhitespace(xmlParserCtxtPtr ctxt, const CHAR *ch, int len)
+ignorableWhitespace(void *ctx, const CHAR *ch, int len)
 {
+    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.ignorableWhitespace(%.30s, %d)\n", ch, len);
 #endif
@@ -623,9 +647,10 @@
  * A processing instruction has been parsed.
  */
 void
-processingInstruction(xmlParserCtxtPtr ctxt, const CHAR *target,
+processingInstruction(void *ctx, const CHAR *target,
                       const CHAR *data)
 {
+    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.processingInstruction(%s, %s)\n", target, data);
 #endif
@@ -640,8 +665,9 @@
  * An old global namespace has been parsed.
  */
 void
-globalNamespace(xmlParserCtxtPtr ctxt, const CHAR *href, const CHAR *prefix)
+globalNamespace(void *ctx, const CHAR *href, const CHAR *prefix)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.globalNamespace(%s, %s)\n", href, prefix);
 #endif
@@ -656,8 +682,9 @@
  * Set the current element namespace.
  */
 void
-setNamespace(xmlParserCtxtPtr ctxt, const CHAR *name)
+setNamespace(void *ctx, const CHAR *name)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlNsPtr ns;
     xmlNodePtr parent;
 
@@ -682,8 +709,9 @@
  * Get the current element namespace.
  */
 xmlNsPtr
-getNamespace(xmlParserCtxtPtr ctxt)
+getNamespace(void *ctx)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlNsPtr ret;
 
 #ifdef DEBUG_SAX
@@ -702,8 +730,9 @@
  * one read upon parsing.
  */
 int
-checkNamespace(xmlParserCtxtPtr ctxt, CHAR *namespace)
+checkNamespace(void *ctx, CHAR *namespace)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     xmlNodePtr cur = ctxt->node;
 
 #ifdef DEBUG_SAX
@@ -749,8 +778,9 @@
  * A namespace has been parsed.
  */
 void
-namespaceDecl(xmlParserCtxtPtr ctxt, const CHAR *href, const CHAR *prefix)
+namespaceDecl(void *ctx, const CHAR *href, const CHAR *prefix)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 #ifdef DEBUG_SAX
     if (prefix == NULL)
 	fprintf(stderr, "SAX.namespaceDecl(%s, NULL)\n", href);
@@ -768,8 +798,9 @@
  * A comment has been parsed.
  */
 void
-comment(xmlParserCtxtPtr ctxt, const CHAR *value)
+comment(void *ctx, const CHAR *value)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
 #ifdef DEBUG_SAX
     fprintf(stderr, "SAX.comment(%s)\n", value);
 #endif
diff --git a/encoding.c b/encoding.c
index 0db110c..a7e984e 100644
--- a/encoding.c
+++ b/encoding.c
@@ -19,6 +19,7 @@
  * Daniel.Veillard@w3.org
  */
 
+#include <ctype.h>
 #include "encoding.h"
 
 /*
@@ -204,3 +205,109 @@
 }
 
 
+/**
+ * xmlDetectCharEncoding:
+ * @in:  a pointer to the first bytes of the XML entity, must be at least
+ *       4 bytes long.
+ *
+ * Guess the encoding of the entity using the first bytes of the entity content
+ * accordingly of the non-normative appendix F of the XML-1.0 recommendation.
+ * 
+ * Returns one of the XML_CHAR_ENCODING_... values.
+ */
+xmlCharEncoding
+xmlDetectCharEncoding(unsigned char* in)
+{
+    if ((in[0] == 0x00) && (in[1] == 0x00) &&
+        (in[2] == 0x00) && (in[3] == 0x3C))
+	return(XML_CHAR_ENCODING_UCS4BE);
+    if ((in[0] == 0x3C) && (in[1] == 0x00) &&
+        (in[2] == 0x00) && (in[3] == 0x00))
+	return(XML_CHAR_ENCODING_UCS4LE);
+    if ((in[0] == 0x00) && (in[1] == 0x00) &&
+        (in[2] == 0x3C) && (in[3] == 0x00))
+	return(XML_CHAR_ENCODING_UCS4_2143);
+    if ((in[0] == 0x00) && (in[1] == 0x3C) &&
+        (in[2] == 0x00) && (in[3] == 0x00))
+	return(XML_CHAR_ENCODING_UCS4_3412);
+    if ((in[0] == 0xFE) && (in[1] == 0xFF))
+	return(XML_CHAR_ENCODING_UTF16BE);
+    if ((in[0] == 0xFF) && (in[1] == 0xFE))
+	return(XML_CHAR_ENCODING_UTF16LE);
+    if ((in[0] == 0x4C) && (in[1] == 0x6F) &&
+        (in[2] == 0xA7) && (in[3] == 0x94))
+	return(XML_CHAR_ENCODING_EBCDIC);
+    if ((in[0] == 0x3C) && (in[1] == 0x3F) &&
+        (in[2] == 0x78) && (in[3] == 0x6D))
+	return(XML_CHAR_ENCODING_UTF8);
+    return(XML_CHAR_ENCODING_NONE);
+}
+
+/**
+ * xmlParseCharEncoding:
+ * @name:  the encoding name as parsed, in UTF-8 format (ASCCI actually)
+ *
+ * Conpare the string to the known encoding schemes already known. Note
+ * that the comparison is case insensitive accordingly to the section
+ * [XML] 4.3.3 Character Encoding in Entities.
+ * 
+ * Returns one of the XML_CHAR_ENCODING_... values or XML_CHAR_ENCODING_NONE
+ * if not recognized.
+ */
+xmlCharEncoding
+xmlParseCharEncoding(char* name)
+{
+    char upper[500];
+    int i;
+
+    for (i = 0;i < 499;i++) {
+        upper[i] = toupper(name[i]);
+	if (upper[i] == 0) break;
+    }
+    upper[i] = 0;
+
+    if (!strcmp(upper, "")) return(XML_CHAR_ENCODING_NONE);
+    if (!strcmp(upper, "UTF-8")) return(XML_CHAR_ENCODING_UTF8);
+    if (!strcmp(upper, "UTF8")) return(XML_CHAR_ENCODING_UTF8);
+
+    /*
+     * NOTE: if we were able to parse this, the endianness of UTF16 is
+     *       already found and in use
+     */
+    if (!strcmp(upper, "UTF-16")) return(XML_CHAR_ENCODING_UTF16LE);
+    if (!strcmp(upper, "UTF16")) return(XML_CHAR_ENCODING_UTF16LE);
+    
+    if (!strcmp(upper, "ISO-10646-UCS-2")) return(XML_CHAR_ENCODING_UCS2);
+    if (!strcmp(upper, "UCS-2")) return(XML_CHAR_ENCODING_UCS2);
+    if (!strcmp(upper, "UCS2")) return(XML_CHAR_ENCODING_UCS2);
+
+    /*
+     * NOTE: if we were able to parse this, the endianness of UCS4 is
+     *       already found and in use
+     */
+    if (!strcmp(upper, "ISO-10646-UCS-4")) return(XML_CHAR_ENCODING_UCS4LE);
+    if (!strcmp(upper, "UCS-4")) return(XML_CHAR_ENCODING_UCS4LE);
+    if (!strcmp(upper, "UCS4")) return(XML_CHAR_ENCODING_UCS4LE);
+
+    
+    if (!strcmp(upper,  "ISO-8859-1")) return(XML_CHAR_ENCODING_8859_1);
+    if (!strcmp(upper,  "ISO-LATIN-1")) return(XML_CHAR_ENCODING_8859_1);
+    if (!strcmp(upper,  "ISO LATIN 1")) return(XML_CHAR_ENCODING_8859_1);
+
+    if (!strcmp(upper,  "ISO-8859-2")) return(XML_CHAR_ENCODING_8859_2);
+    if (!strcmp(upper,  "ISO-LATIN-2")) return(XML_CHAR_ENCODING_8859_2);
+    if (!strcmp(upper,  "ISO LATIN 2")) return(XML_CHAR_ENCODING_8859_2);
+
+    if (!strcmp(upper,  "ISO-8859-3")) return(XML_CHAR_ENCODING_8859_3);
+    if (!strcmp(upper,  "ISO-8859-4")) return(XML_CHAR_ENCODING_8859_4);
+    if (!strcmp(upper,  "ISO-8859-5")) return(XML_CHAR_ENCODING_8859_5);
+    if (!strcmp(upper,  "ISO-8859-6")) return(XML_CHAR_ENCODING_8859_6);
+    if (!strcmp(upper,  "ISO-8859-7")) return(XML_CHAR_ENCODING_8859_7);
+    if (!strcmp(upper,  "ISO-8859-8")) return(XML_CHAR_ENCODING_8859_8);
+    if (!strcmp(upper,  "ISO-8859-9")) return(XML_CHAR_ENCODING_8859_9);
+
+    if (!strcmp(upper, "ISO-2022-JP")) return(XML_CHAR_ENCODING_2022_JP);
+    if (!strcmp(upper, "Shift_JIS")) return(XML_CHAR_ENCODING_SHIFT_JIS);
+    if (!strcmp(upper, "EUC-JP")) return(XML_CHAR_ENCODING_EUC_JP);
+    return(XML_CHAR_ENCODING_ERROR);
+}
diff --git a/encoding.h b/encoding.h
index b5a11f9..217edbc 100644
--- a/encoding.h
+++ b/encoding.h
@@ -13,22 +13,49 @@
  * [US-ASCII]     Coded Character Set--7-bit American Standard Code for
  *                Information Interchange, ANSI X3.4-1986.
  *
- * Original code from "Martin J. Duerst" <duerst@w3.org>
- *
  * See Copyright for the status of this software.
  *
  * Daniel.Veillard@w3.org
  */
 
-#ifndef __XML_ENCODING_H__
-#define __XML_ENCODING_H__
+#ifndef __XML_CHAR_ENCODING_H__
+#define __XML_CHAR_ENCODING_H__
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+typedef enum {
+    XML_CHAR_ENCODING_ERROR=   -1, /* No char encoding detected */
+    XML_CHAR_ENCODING_NONE=	0, /* No char encoding detected */
+    XML_CHAR_ENCODING_UTF8=	1, /* UTF-8 */
+    XML_CHAR_ENCODING_UTF16LE=	2, /* UTF-16 little endian */
+    XML_CHAR_ENCODING_UTF16BE=	3, /* UTF-16 big endian */
+    XML_CHAR_ENCODING_UCS4LE=	4, /* UCS-4 little endian */
+    XML_CHAR_ENCODING_UCS4BE=	5, /* UCS-4 big endian */
+    XML_CHAR_ENCODING_EBCDIC=	6, /* EBCDIC uh! */
+    XML_CHAR_ENCODING_UCS4_2143=7, /* UCS-4 unusual ordering */
+    XML_CHAR_ENCODING_UCS4_3412=8, /* UCS-4 unusual ordering */
+    XML_CHAR_ENCODING_UCS2=	9, /* UCS-2 */
+    XML_CHAR_ENCODING_8859_1=	10,/* ISO-8859-1 ISO Latin 1 */
+    XML_CHAR_ENCODING_8859_2=	11,/* ISO-8859-2 ISO Latin 2 */
+    XML_CHAR_ENCODING_8859_3=	12,/* ISO-8859-3 */
+    XML_CHAR_ENCODING_8859_4=	13,/* ISO-8859-4 */
+    XML_CHAR_ENCODING_8859_5=	14,/* ISO-8859-5 */
+    XML_CHAR_ENCODING_8859_6=	15,/* ISO-8859-6 */
+    XML_CHAR_ENCODING_8859_7=	16,/* ISO-8859-7 */
+    XML_CHAR_ENCODING_8859_8=	17,/* ISO-8859-8 */
+    XML_CHAR_ENCODING_8859_9=	18,/* ISO-8859-9 */
+    XML_CHAR_ENCODING_2022_JP=  19,/* ISO-2022-JP */
+    XML_CHAR_ENCODING_SHIFT_JIS=20,/* Shift_JIS */
+    XML_CHAR_ENCODING_EUC_JP=   21,/* EUC-JP */
+} xmlCharEncoding;
+
+extern xmlCharEncoding xmlDetectCharEncoding(unsigned char* in);
+extern xmlCharEncoding xmlParseCharEncoding(char* name);
+
 #ifdef __cplusplus
 }
 #endif
 
-#endif /* __XML_ENCODING_H__ */
+#endif /* __XML_CHAR_ENCODING_H__ */
diff --git a/error.c b/error.c
index 3aaaca3..a9f0a0b 100644
--- a/error.c
+++ b/error.c
@@ -20,8 +20,9 @@
  * extra parameters.
  */
 void
-xmlParserError(xmlParserCtxtPtr ctxt, const char *msg, ...)
+xmlParserError(void *ctx, const char *msg, ...)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     const CHAR *cur, *base;
     va_list args;
     int n;
@@ -73,8 +74,9 @@
  * extra parameters.
  */
 void
-xmlParserWarning(xmlParserCtxtPtr ctxt, const char *msg, ...)
+xmlParserWarning(void *ctx, const char *msg, ...)
 {
+    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
     const CHAR *cur, *base;
     va_list args;
     int n;
diff --git a/include/libxml/encoding.h b/include/libxml/encoding.h
index b5a11f9..217edbc 100644
--- a/include/libxml/encoding.h
+++ b/include/libxml/encoding.h
@@ -13,22 +13,49 @@
  * [US-ASCII]     Coded Character Set--7-bit American Standard Code for
  *                Information Interchange, ANSI X3.4-1986.
  *
- * Original code from "Martin J. Duerst" <duerst@w3.org>
- *
  * See Copyright for the status of this software.
  *
  * Daniel.Veillard@w3.org
  */
 
-#ifndef __XML_ENCODING_H__
-#define __XML_ENCODING_H__
+#ifndef __XML_CHAR_ENCODING_H__
+#define __XML_CHAR_ENCODING_H__
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+typedef enum {
+    XML_CHAR_ENCODING_ERROR=   -1, /* No char encoding detected */
+    XML_CHAR_ENCODING_NONE=	0, /* No char encoding detected */
+    XML_CHAR_ENCODING_UTF8=	1, /* UTF-8 */
+    XML_CHAR_ENCODING_UTF16LE=	2, /* UTF-16 little endian */
+    XML_CHAR_ENCODING_UTF16BE=	3, /* UTF-16 big endian */
+    XML_CHAR_ENCODING_UCS4LE=	4, /* UCS-4 little endian */
+    XML_CHAR_ENCODING_UCS4BE=	5, /* UCS-4 big endian */
+    XML_CHAR_ENCODING_EBCDIC=	6, /* EBCDIC uh! */
+    XML_CHAR_ENCODING_UCS4_2143=7, /* UCS-4 unusual ordering */
+    XML_CHAR_ENCODING_UCS4_3412=8, /* UCS-4 unusual ordering */
+    XML_CHAR_ENCODING_UCS2=	9, /* UCS-2 */
+    XML_CHAR_ENCODING_8859_1=	10,/* ISO-8859-1 ISO Latin 1 */
+    XML_CHAR_ENCODING_8859_2=	11,/* ISO-8859-2 ISO Latin 2 */
+    XML_CHAR_ENCODING_8859_3=	12,/* ISO-8859-3 */
+    XML_CHAR_ENCODING_8859_4=	13,/* ISO-8859-4 */
+    XML_CHAR_ENCODING_8859_5=	14,/* ISO-8859-5 */
+    XML_CHAR_ENCODING_8859_6=	15,/* ISO-8859-6 */
+    XML_CHAR_ENCODING_8859_7=	16,/* ISO-8859-7 */
+    XML_CHAR_ENCODING_8859_8=	17,/* ISO-8859-8 */
+    XML_CHAR_ENCODING_8859_9=	18,/* ISO-8859-9 */
+    XML_CHAR_ENCODING_2022_JP=  19,/* ISO-2022-JP */
+    XML_CHAR_ENCODING_SHIFT_JIS=20,/* Shift_JIS */
+    XML_CHAR_ENCODING_EUC_JP=   21,/* EUC-JP */
+} xmlCharEncoding;
+
+extern xmlCharEncoding xmlDetectCharEncoding(unsigned char* in);
+extern xmlCharEncoding xmlParseCharEncoding(char* name);
+
 #ifdef __cplusplus
 }
 #endif
 
-#endif /* __XML_ENCODING_H__ */
+#endif /* __XML_CHAR_ENCODING_H__ */
diff --git a/include/libxml/parser.h b/include/libxml/parser.h
index 3e433a2..2075607 100644
--- a/include/libxml/parser.h
+++ b/include/libxml/parser.h
@@ -81,10 +81,10 @@
  */
 
 typedef struct xmlSAXLocator {
-    const CHAR *(*getPublicId)(xmlParserCtxtPtr ctxt);
-    const CHAR *(*getSystemId)(xmlParserCtxtPtr ctxt);
-    int (*getLineNumber)(xmlParserCtxtPtr ctxt);
-    int (*getColumnNumber)(xmlParserCtxtPtr ctxt);
+    const CHAR *(*getPublicId)(void *ctx);
+    const CHAR *(*getSystemId)(void *ctx);
+    int (*getLineNumber)(void *ctx);
+    int (*getColumnNumber)(void *ctx);
 } _xmlSAXLocator;
 typedef _xmlSAXLocator xmlSAXLocator;
 typedef xmlSAXLocator *xmlSAXLocatorPtr;
@@ -95,48 +95,48 @@
 
 #include "entities.h"
 
-typedef xmlParserInputPtr (*resolveEntitySAXFunc) (xmlParserCtxtPtr ctxt,
+typedef xmlParserInputPtr (*resolveEntitySAXFunc) (void *ctx,
 			    const CHAR *publicId, const CHAR *systemId);
-typedef void (*internalSubsetSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *name,
+typedef void (*internalSubsetSAXFunc) (void *ctx, const CHAR *name,
                             const CHAR *ExternalID, const CHAR *SystemID);
-typedef xmlEntityPtr (*getEntitySAXFunc) (xmlParserCtxtPtr ctxt,
+typedef xmlEntityPtr (*getEntitySAXFunc) (void *ctx,
                             const CHAR *name);
-typedef void (*entityDeclSAXFunc) (xmlParserCtxtPtr ctxt,
+typedef void (*entityDeclSAXFunc) (void *ctx,
                             const CHAR *name, int type, const CHAR *publicId,
 			    const CHAR *systemId, CHAR *content);
-typedef void (*notationDeclSAXFunc)(xmlParserCtxtPtr ctxt, const CHAR *name,
+typedef void (*notationDeclSAXFunc)(void *ctx, const CHAR *name,
 			    const CHAR *publicId, const CHAR *systemId);
-typedef void (*attributeDeclSAXFunc)(xmlParserCtxtPtr ctxt, const CHAR *elem,
+typedef void (*attributeDeclSAXFunc)(void *ctx, const CHAR *elem,
                             const CHAR *name, int type, int def,
 			    const CHAR *defaultValue, xmlEnumerationPtr tree);
-typedef void (*elementDeclSAXFunc)(xmlParserCtxtPtr ctxt, const CHAR *name,
+typedef void (*elementDeclSAXFunc)(void *ctx, const CHAR *name,
 			    int type, xmlElementContentPtr content);
-typedef void (*unparsedEntityDeclSAXFunc)(xmlParserCtxtPtr ctxt,
+typedef void (*unparsedEntityDeclSAXFunc)(void *ctx,
                             const CHAR *name, const CHAR *publicId,
 			    const CHAR *systemId, const CHAR *notationName);
-typedef void (*setDocumentLocatorSAXFunc) (xmlParserCtxtPtr ctxt,
+typedef void (*setDocumentLocatorSAXFunc) (void *ctx,
                             xmlSAXLocatorPtr loc);
-typedef void (*startDocumentSAXFunc) (xmlParserCtxtPtr ctxt);
-typedef void (*endDocumentSAXFunc) (xmlParserCtxtPtr ctxt);
-typedef void (*startElementSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *name,
+typedef void (*startDocumentSAXFunc) (void *ctx);
+typedef void (*endDocumentSAXFunc) (void *ctx);
+typedef void (*startElementSAXFunc) (void *ctx, const CHAR *name,
                             const CHAR **atts);
-typedef void (*endElementSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *name);
-typedef void (*attributeSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *name,
+typedef void (*endElementSAXFunc) (void *ctx, const CHAR *name);
+typedef void (*attributeSAXFunc) (void *ctx, const CHAR *name,
                                   const CHAR *value);
-typedef void (*referenceSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *name);
-typedef void (*charactersSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *ch,
+typedef void (*referenceSAXFunc) (void *ctx, const CHAR *name);
+typedef void (*charactersSAXFunc) (void *ctx, const CHAR *ch,
 		            int len);
-typedef void (*ignorableWhitespaceSAXFunc) (xmlParserCtxtPtr ctxt,
+typedef void (*ignorableWhitespaceSAXFunc) (void *ctx,
 			    const CHAR *ch, int len);
-typedef void (*processingInstructionSAXFunc) (xmlParserCtxtPtr ctxt,
+typedef void (*processingInstructionSAXFunc) (void *ctx,
                             const CHAR *target, const CHAR *data);
-typedef void (*commentSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *value);
-typedef void (*warningSAXFunc) (xmlParserCtxtPtr ctxt, const char *msg, ...);
-typedef void (*errorSAXFunc) (xmlParserCtxtPtr ctxt, const char *msg, ...);
-typedef void (*fatalErrorSAXFunc) (xmlParserCtxtPtr ctxt, const char *msg, ...);
-typedef int (*isStandaloneSAXFunc) (xmlParserCtxtPtr ctxt);
-typedef int (*hasInternalSubsetSAXFunc) (xmlParserCtxtPtr ctxt);
-typedef int (*hasExternalSubsetSAXFunc) (xmlParserCtxtPtr ctxt);
+typedef void (*commentSAXFunc) (void *ctx, const CHAR *value);
+typedef void (*warningSAXFunc) (void *ctx, const char *msg, ...);
+typedef void (*errorSAXFunc) (void *ctx, const char *msg, ...);
+typedef void (*fatalErrorSAXFunc) (void *ctx, const char *msg, ...);
+typedef int (*isStandaloneSAXFunc) (void *ctx);
+typedef int (*hasInternalSubsetSAXFunc) (void *ctx);
+typedef int (*hasExternalSubsetSAXFunc) (void *ctx);
 
 typedef struct xmlSAXHandler {
     internalSubsetSAXFunc internalSubset;
diff --git a/parser.c b/parser.c
index 0e4eafc..d934f82 100644
--- a/parser.c
+++ b/parser.c
@@ -30,6 +30,7 @@
 #include "tree.h"
 #include "parser.h"
 #include "entities.h"
+#include "encoding.h"
 #include "valid.h"
 #include "parserInternals.h"
 
@@ -181,20 +182,20 @@
 
     if (entity == NULL) {
         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	      "internal: xmlNewEntityInputStream entity = NULL\n");
 	return(NULL);
     }
     if (entity->content == NULL) {
         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	      "internal: xmlNewEntityInputStream entity->input = NULL\n");
 	return(NULL);
     }
     input = (xmlParserInputPtr) malloc(sizeof(xmlParserInput));
     if (input == NULL) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "malloc: couldn't allocate a new input stream\n");
+	    ctxt->sax->error(ctxt->userData, "malloc: couldn't allocate a new input stream\n");
 	return(NULL);
     }
     input->filename = entity->SystemID; /* TODO !!! char <- CHAR */
@@ -220,14 +221,14 @@
 
     if (string == NULL) {
         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	      "internal: xmlNewStringInputStream string = NULL\n");
 	return(NULL);
     }
     input = (xmlParserInputPtr) malloc(sizeof(xmlParserInput));
     if (input == NULL) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "malloc: couldn't allocate a new input stream\n");
+	    ctxt->sax->error(ctxt->userData, "malloc: couldn't allocate a new input stream\n");
 	return(NULL);
     }
     input->filename = NULL;
@@ -242,6 +243,139 @@
 
 /************************************************************************
  *									*
+ *		Commodity functions to handle encodings			*
+ *									*
+ ************************************************************************/
+
+/**
+ * xmlSwitchEncoding:
+ * @ctxt:  the parser context
+ * @len:  the len of @cur
+ *
+ * change the input functions when discovering the character encoding
+ * of a given entity.
+ *
+ */
+void
+xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
+{
+    switch (enc) {
+        case XML_CHAR_ENCODING_ERROR:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+	        ctxt->sax->error(ctxt->userData, "encoding unknown\n");
+	    ctxt->wellFormed = 0;
+            break;
+        case XML_CHAR_ENCODING_NONE:
+	    /* let's assume it's UTF-8 without the XML decl */
+            return;
+        case XML_CHAR_ENCODING_UTF8:
+	    /* default encoding, no conversion should be needed */
+            return;
+        case XML_CHAR_ENCODING_UTF16LE:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding UTF16 little endian not supported\n");
+            break;
+        case XML_CHAR_ENCODING_UTF16BE:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding UTF16 big endian not supported\n");
+            break;
+        case XML_CHAR_ENCODING_UCS4LE:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding USC4 little endian not supported\n");
+            break;
+        case XML_CHAR_ENCODING_UCS4BE:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding USC4 big endian not supported\n");
+            break;
+        case XML_CHAR_ENCODING_EBCDIC:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding EBCDIC not supported\n");
+            break;
+        case XML_CHAR_ENCODING_UCS4_2143:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding UCS4 2143 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_UCS4_3412:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding UCS4 3412 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_UCS2:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding UCS2 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_8859_1:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding ISO_8859_1 ISO Latin 1 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_8859_2:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding ISO_8859_2 ISO Latin 2 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_8859_3:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding ISO_8859_3 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_8859_4:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding ISO_8859_4 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_8859_5:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding ISO_8859_5 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_8859_6:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding ISO_8859_6 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_8859_7:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding ISO_8859_7 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_8859_8:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding ISO_8859_8 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_8859_9:
+	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+		  "char encoding ISO_8859_9 not supported\n");
+            break;
+        case XML_CHAR_ENCODING_2022_JP:
+            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+                  "char encoding ISO-2022-JPnot supported\n");
+            break;
+        case XML_CHAR_ENCODING_SHIFT_JIS:
+            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+                  "char encoding Shift_JISnot supported\n");
+            break;
+        case XML_CHAR_ENCODING_EUC_JP:
+            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
+                ctxt->sax->error(ctxt->userData,
+                  "char encoding EUC-JPnot supported\n");
+            break;
+    }
+}
+
+/************************************************************************
+ *									*
  *		Commodity functions to handle CHARs			*
  *									*
  ************************************************************************/
@@ -522,7 +656,7 @@
 
     if (entity->content == NULL) {
         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "xmlHandleEntity %s: content == NULL\n",
+	    ctxt->sax->error(ctxt->userData, "xmlHandleEntity %s: content == NULL\n",
 	               entity->name);
 	ctxt->wellFormed = 0;
         return;
@@ -542,7 +676,7 @@
      * Just handle the content as a set of chars.
      */
     if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
-	ctxt->sax->characters(ctxt, entity->content, len);
+	ctxt->sax->characters(ctxt->userData, entity->content, len);
 
 }
 
@@ -728,7 +862,7 @@
 	while (IS_CHAR(CUR) && (CUR != '"')) NEXT;
 	if (CUR != '"') {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "String not closed \"%.50s\"\n", q);
+	        ctxt->sax->error(ctxt->userData, "String not closed \"%.50s\"\n", q);
 	    ctxt->wellFormed = 0;
         } else {
             ret = xmlStrndup(q, CUR_PTR - q);
@@ -740,7 +874,7 @@
 	while (IS_CHAR(CUR) && (CUR != '\'')) NEXT;
 	if (CUR != '\'') {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "String not closed \"%.50s\"\n", q);
+	        ctxt->sax->error(ctxt->userData, "String not closed \"%.50s\"\n", q);
 	    ctxt->wellFormed = 0;
         } else {
             ret = xmlStrndup(q, CUR_PTR - q);
@@ -833,7 +967,7 @@
 	     */
 	    if (!garbage)
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, "xmlParseNamespace found garbage\n");
+		    ctxt->sax->error(ctxt->userData, "xmlParseNamespace found garbage\n");
 	    ctxt->wellFormed = 0;
             NEXT;
         }
@@ -846,7 +980,7 @@
      * Register the DTD.
     if (href != NULL)
 	if ((ctxt->sax != NULL) && (ctxt->sax->globalNamespace != NULL))
-	    ctxt->sax->globalNamespace(ctxt, href, prefix);
+	    ctxt->sax->globalNamespace(ctxt->userData, href, prefix);
      */
 
     if (prefix != NULL) free(prefix);
@@ -975,7 +1109,7 @@
 	}
 	if (!IS_CHAR(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Unfinished EntityValue\n");
+	        ctxt->sax->error(ctxt->userData, "Unfinished EntityValue\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    ret = xmlStrncat(ret, q, CUR_PTR - q);
@@ -1009,7 +1143,7 @@
 	}
 	if (!IS_CHAR(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Unfinished EntityValue\n");
+	        ctxt->sax->error(ctxt->userData, "Unfinished EntityValue\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    ret = xmlStrncat(ret, q, CUR_PTR - q);
@@ -1017,7 +1151,7 @@
         }
     } else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "xmlParseEntityValue \" or ' expected\n");
+	    ctxt->sax->error(ctxt->userData, "xmlParseEntityValue \" or ' expected\n");
 	ctxt->wellFormed = 0;
     }
     
@@ -1048,7 +1182,7 @@
 	while ((IS_CHAR(CUR)) && (CUR != '"')) {
 	    if (CUR == '<') {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt,
+		    ctxt->sax->error(ctxt->userData,
 		       "Unescaped '<' not allowed in attributes values\n");
 		ctxt->wellFormed = 0;
 	    }
@@ -1083,7 +1217,7 @@
 	}
 	if (!IS_CHAR(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Unfinished AttValue\n");
+	        ctxt->sax->error(ctxt->userData, "Unfinished AttValue\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    ret = xmlStrncat(ret, q, CUR_PTR - q);
@@ -1095,7 +1229,7 @@
 	while ((IS_CHAR(CUR)) && (CUR != '\'')) {
 	    if (CUR == '<') {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt,
+		    ctxt->sax->error(ctxt->userData,
 		       "Unescaped '<' not allowed in attributes values\n");
 		ctxt->wellFormed = 0;
 	    }
@@ -1130,7 +1264,7 @@
 	}
 	if (!IS_CHAR(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Unfinished AttValue\n");
+	        ctxt->sax->error(ctxt->userData, "Unfinished AttValue\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    ret = xmlStrncat(ret, q, CUR_PTR - q);
@@ -1138,7 +1272,7 @@
         }
     } else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "AttValue: \" or ' expected\n");
+	    ctxt->sax->error(ctxt->userData, "AttValue: \" or ' expected\n");
 	ctxt->wellFormed = 0;
     }
     
@@ -1168,7 +1302,7 @@
 	    NEXT;
 	if (!IS_CHAR(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Unfinished SystemLiteral\n");
+	        ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    ret = xmlStrndup(q, CUR_PTR - q);
@@ -1181,7 +1315,7 @@
 	    NEXT;
 	if (!IS_CHAR(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Unfinished SystemLiteral\n");
+	        ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    ret = xmlStrndup(q, CUR_PTR - q);
@@ -1189,7 +1323,7 @@
         }
     } else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "SystemLiteral \" or ' expected\n");
+	    ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
 	ctxt->wellFormed = 0;
     }
     
@@ -1220,7 +1354,7 @@
 	while (IS_PUBIDCHAR(CUR)) NEXT;
 	if (CUR != '"') {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Unfinished PubidLiteral\n");
+	        ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    ret = xmlStrndup(q, CUR_PTR - q);
@@ -1233,7 +1367,7 @@
 	    NEXT;
 	if (!IS_LETTER(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Unfinished PubidLiteral\n");
+	        ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    ret = xmlStrndup(q, CUR_PTR - q);
@@ -1241,7 +1375,7 @@
 	}
     } else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "SystemLiteral \" or ' expected\n");
+	    ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
 	ctxt->wellFormed = 0;
     }
     
@@ -1271,7 +1405,7 @@
 	    if (cdata) break;
 	    else {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt,
+		    ctxt->sax->error(ctxt->userData,
 		       "Sequence ']]>' not allowed in content\n");
 		ctxt->wellFormed = 0;
 	    }
@@ -1286,10 +1420,10 @@
     if (ctxt->sax != NULL) {
 	if (areBlanks(ctxt, q, CUR_PTR - q)) {
 	    if (ctxt->sax->ignorableWhitespace != NULL)
-		ctxt->sax->ignorableWhitespace(ctxt, q, CUR_PTR - q);
+		ctxt->sax->ignorableWhitespace(ctxt->userData, q, CUR_PTR - q);
 	} else {
 	    if (ctxt->sax->characters != NULL)
-		ctxt->sax->characters(ctxt, q, CUR_PTR - q);
+		ctxt->sax->characters(ctxt->userData, q, CUR_PTR - q);
         }
     }
 }
@@ -1326,7 +1460,7 @@
         SKIP(6);
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt,
+		ctxt->sax->error(ctxt->userData,
 		    "Space required after 'SYSTEM'\n");
 	    ctxt->wellFormed = 0;
 	}
@@ -1334,7 +1468,7 @@
 	URI = xmlParseSystemLiteral(ctxt);
 	if (URI == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt,
+	        ctxt->sax->error(ctxt->userData,
 	          "xmlParseExternalID: SYSTEM, no URI\n");
 	    ctxt->wellFormed = 0;
         }
@@ -1344,7 +1478,7 @@
         SKIP(6);
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt,
+		ctxt->sax->error(ctxt->userData,
 		    "Space required after 'PUBLIC'\n");
 	    ctxt->wellFormed = 0;
 	}
@@ -1352,7 +1486,7 @@
 	*publicID = xmlParsePubidLiteral(ctxt);
 	if (*publicID == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, 
+	        ctxt->sax->error(ctxt->userData, 
 	          "xmlParseExternalID: PUBLIC, no Public Identifier\n");
 	    ctxt->wellFormed = 0;
 	}
@@ -1362,7 +1496,7 @@
 	     */
 	    if (!IS_BLANK(CUR)) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt,
+		    ctxt->sax->error(ctxt->userData,
 			"Space required after the Public Identifier\n");
 		ctxt->wellFormed = 0;
 	    }
@@ -1382,7 +1516,7 @@
 	URI = xmlParseSystemLiteral(ctxt);
 	if (URI == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, 
+	        ctxt->sax->error(ctxt->userData, 
 	           "xmlParseExternalID: PUBLIC, no URI\n");
 	    ctxt->wellFormed = 0;
         }
@@ -1424,7 +1558,7 @@
 	    (*r != '-') || (*q != '-'))) {
 	if ((*r == '-') && (*q == '-')) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt,
+	        ctxt->sax->error(ctxt->userData,
 	       "Comment must not contain '--' (double-hyphen)`\n");
 	    ctxt->wellFormed = 0;
 	}
@@ -1432,14 +1566,14 @@
     }
     if (!IS_CHAR(CUR)) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "Comment not terminated \n<!--%.50s\n", start);
+	    ctxt->sax->error(ctxt->userData, "Comment not terminated \n<!--%.50s\n", start);
 	ctxt->wellFormed = 0;
     } else {
         NEXT;
 	if (create) {
 	    val = xmlStrndup(start, q - start);
 	    if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL))
-		ctxt->sax->comment(ctxt, val);
+		ctxt->sax->comment(ctxt->userData, val);
 	    free(val);
 	}
     }
@@ -1466,7 +1600,7 @@
         ((name[1] == 'm') || (name[1] == 'M')) &&
         ((name[2] == 'l') || (name[2] == 'L'))) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "xmlParsePItarget: invalid name prefix 'xml'\n");
+	    ctxt->sax->error(ctxt->userData, "xmlParsePItarget: invalid name prefix 'xml'\n");
 	return(NULL);
     }
     return(name);
@@ -1509,7 +1643,7 @@
 		NEXT;
 	    if (!IS_CHAR(CUR)) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt,
+		    ctxt->sax->error(ctxt->userData,
 		      "xmlParsePI: PI %s never end ...\n", target);
 		ctxt->wellFormed = 0;
 	    } else {
@@ -1523,13 +1657,13 @@
 		 */
 		if ((ctxt->sax) &&
 		    (ctxt->sax->processingInstruction != NULL))
-		    ctxt->sax->processingInstruction(ctxt, target, data);
+		    ctxt->sax->processingInstruction(ctxt->userData, target, data);
 		free(data);
 	    }
 	    free(target);
 	} else {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "xmlParsePI : no target name\n");
+	        ctxt->sax->error(ctxt->userData, "xmlParsePI : no target name\n");
 	    ctxt->wellFormed = 0;
 
 	    /********* Should we try to complete parsing the PI ???
@@ -1575,7 +1709,7 @@
 	SKIP(10);
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, "Space required after '<!NOTATION'\n");
+		ctxt->sax->error(ctxt->userData, "Space required after '<!NOTATION'\n");
 	    ctxt->wellFormed = 0;
 	    return;
 	}
@@ -1584,13 +1718,13 @@
         name = xmlParseName(ctxt);
 	if (name == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "NOTATION: Name expected here\n");
+	        ctxt->sax->error(ctxt->userData, "NOTATION: Name expected here\n");
 	    ctxt->wellFormed = 0;
 	    return;
 	}
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, 
+		ctxt->sax->error(ctxt->userData, 
 		     "Space required after the NOTATION name'\n");
 	    ctxt->wellFormed = 0;
 	    return;
@@ -1606,10 +1740,10 @@
 	if (CUR == '>') {
 	    NEXT;
 	    if ((ctxt->sax != NULL) && (ctxt->sax->notationDecl != NULL))
-		ctxt->sax->notationDecl(ctxt, name, Pubid, Systemid);
+		ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid);
 	} else {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt,
+		ctxt->sax->error(ctxt->userData,
 		       "'>' required to close NOTATION declaration\n");
 	    ctxt->wellFormed = 0;
 	}
@@ -1653,7 +1787,7 @@
 	SKIP(8);
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, "Space required after '<!ENTITY'\n");
+		ctxt->sax->error(ctxt->userData, "Space required after '<!ENTITY'\n");
 	    ctxt->wellFormed = 0;
 	}
 	SKIP_BLANKS;
@@ -1662,7 +1796,7 @@
 	    NEXT;
 	    if (!IS_BLANK(CUR)) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, "Space required after '%'\n");
+		    ctxt->sax->error(ctxt->userData, "Space required after '%'\n");
 		ctxt->wellFormed = 0;
 	    }
 	    SKIP_BLANKS;
@@ -1672,13 +1806,13 @@
         name = xmlParseName(ctxt);
 	if (name == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "xmlParseEntityDecl: no name\n");
+	        ctxt->sax->error(ctxt->userData, "xmlParseEntityDecl: no name\n");
 	    ctxt->wellFormed = 0;
             return;
 	}
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt,
+		ctxt->sax->error(ctxt->userData,
 		     "Space required after the entity name\n");
 	    ctxt->wellFormed = 0;
 	}
@@ -1692,7 +1826,7 @@
 	        value = xmlParseEntityValue(ctxt);
 		if (value) {
 		    if ((ctxt->sax != NULL) && (ctxt->sax->entityDecl != NULL))
-			ctxt->sax->entityDecl(ctxt, name,
+			ctxt->sax->entityDecl(ctxt->userData, name,
 		                    XML_INTERNAL_PARAMETER_ENTITY,
 				    NULL, NULL, value);
 		}
@@ -1700,7 +1834,7 @@
 	        URI = xmlParseExternalID(ctxt, &literal, 1);
 		if (URI) {
 		    if ((ctxt->sax != NULL) && (ctxt->sax->entityDecl != NULL))
-			ctxt->sax->entityDecl(ctxt, name,
+			ctxt->sax->entityDecl(ctxt->userData, name,
 		                    XML_EXTERNAL_PARAMETER_ENTITY,
 				    literal, URI, NULL);
 		}
@@ -1709,14 +1843,14 @@
 	    if ((CUR == '"') || (CUR == '\'')) {
 	        value = xmlParseEntityValue(ctxt);
 		if ((ctxt->sax != NULL) && (ctxt->sax->entityDecl != NULL))
-		    ctxt->sax->entityDecl(ctxt, name,
+		    ctxt->sax->entityDecl(ctxt->userData, name,
 				XML_INTERNAL_GENERAL_ENTITY,
 				NULL, NULL, value);
 	    } else {
 	        URI = xmlParseExternalID(ctxt, &literal, 1);
 		if ((CUR != '>') && (!IS_BLANK(CUR))) {
 		    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-			ctxt->sax->error(ctxt,
+			ctxt->sax->error(ctxt->userData,
 			    "Space required before 'NDATA'\n");
 		    ctxt->wellFormed = 0;
 		}
@@ -1727,19 +1861,19 @@
 		    SKIP(5);
 		    if (!IS_BLANK(CUR)) {
 			if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-			    ctxt->sax->error(ctxt,
+			    ctxt->sax->error(ctxt->userData,
 			        "Space required after 'NDATA'\n");
 			ctxt->wellFormed = 0;
 		    }
 		    SKIP_BLANKS;
 		    ndata = xmlParseName(ctxt);
 		    if ((ctxt->sax != NULL) && (ctxt->sax->entityDecl != NULL))
-			ctxt->sax->entityDecl(ctxt, name,
+			ctxt->sax->entityDecl(ctxt->userData, name,
 				    XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
 				    literal, URI, ndata);
 		} else {
 		    if ((ctxt->sax != NULL) && (ctxt->sax->entityDecl != NULL))
-			ctxt->sax->entityDecl(ctxt, name,
+			ctxt->sax->entityDecl(ctxt->userData, name,
 				    XML_EXTERNAL_GENERAL_PARSED_ENTITY,
 				    literal, URI, NULL);
 		}
@@ -1748,7 +1882,7 @@
 	SKIP_BLANKS;
 	if (CUR != '>') {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, 
+	        ctxt->sax->error(ctxt->userData, 
 	            "xmlParseEntityDecl: entity %s not terminated\n", name);
 	    ctxt->wellFormed = 0;
 	} else
@@ -1803,7 +1937,7 @@
 	val = XML_ATTRIBUTE_FIXED;
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Space required after '#FIXED'\n");
+	        ctxt->sax->error(ctxt->userData, "Space required after '#FIXED'\n");
 	    ctxt->wellFormed = 0;
 	}
 	SKIP_BLANKS;
@@ -1811,7 +1945,7 @@
     ret = xmlParseAttValue(ctxt);
     if (ret == NULL) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	       "Attribute default value declaration error\n");
 	ctxt->wellFormed = 0;
     } else
@@ -1839,7 +1973,7 @@
 
     if (CUR != '(') {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "'(' required to start 'NOTATION'\n");
+	    ctxt->sax->error(ctxt->userData, "'(' required to start 'NOTATION'\n");
 	ctxt->wellFormed = 0;
 	return(NULL);
     }
@@ -1849,7 +1983,7 @@
         name = xmlParseName(ctxt);
 	if (name == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, 
+		ctxt->sax->error(ctxt->userData, 
 		                 "Name expected in NOTATION declaration\n");
 	    ctxt->wellFormed = 0;
 	    return(ret);
@@ -1866,7 +2000,7 @@
     } while (CUR == '|');
     if (CUR != ')') {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	                     "')' required to finish NOTATION declaration\n");
 	ctxt->wellFormed = 0;
 	return(ret);
@@ -1893,7 +2027,7 @@
 
     if (CUR != '(') {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	                     "'(' required to start ATTLIST enumeration\n");
 	ctxt->wellFormed = 0;
 	return(NULL);
@@ -1904,7 +2038,7 @@
         name = xmlParseNmtoken(ctxt);
 	if (name == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, 
+		ctxt->sax->error(ctxt->userData, 
 		                 "NmToken expected in ATTLIST enumeration\n");
 	    ctxt->wellFormed = 0;
 	    return(ret);
@@ -1921,7 +2055,7 @@
     } while (CUR == '|');
     if (CUR != ')') {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	                     "')' required to finish ATTLIST enumeration\n");
 	ctxt->wellFormed = 0;
 	return(ret);
@@ -1954,7 +2088,7 @@
 	SKIP(8);
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Space required after 'NOTATION'\n");
+	        ctxt->sax->error(ctxt->userData, "Space required after 'NOTATION'\n");
 	    ctxt->wellFormed = 0;
 	    return(0);
 	}
@@ -2056,14 +2190,14 @@
 	SKIP(9);
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Space required after '<!ATTLIST'\n");
+	        ctxt->sax->error(ctxt->userData, "Space required after '<!ATTLIST'\n");
 	    ctxt->wellFormed = 0;
 	}
         SKIP_BLANKS;
         elemName = xmlParseName(ctxt);
 	if (elemName == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "ATTLIST: no name for Element\n");
+	        ctxt->sax->error(ctxt->userData, "ATTLIST: no name for Element\n");
 	    ctxt->wellFormed = 0;
 	    return;
 	}
@@ -2077,13 +2211,13 @@
 	    attrName = xmlParseName(ctxt);
 	    if (attrName == NULL) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, "ATTLIST: no name for Attribute\n");
+		    ctxt->sax->error(ctxt->userData, "ATTLIST: no name for Attribute\n");
 		ctxt->wellFormed = 0;
 		break;
 	    }
 	    if (!IS_BLANK(CUR)) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, 
+		    ctxt->sax->error(ctxt->userData, 
 		        "Space required after the attribute name\n");
 		ctxt->wellFormed = 0;
 		break;
@@ -2095,7 +2229,7 @@
 
 	    if (!IS_BLANK(CUR)) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, 
+		    ctxt->sax->error(ctxt->userData, 
 		        "Space required after the attribute type\n");
 		ctxt->wellFormed = 0;
 		break;
@@ -2108,7 +2242,7 @@
             if (CUR != '>') {
 		if (!IS_BLANK(CUR)) {
 		    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-			ctxt->sax->error(ctxt, 
+			ctxt->sax->error(ctxt->userData, 
 			"Space required after the attribute default value\n");
 		    ctxt->wellFormed = 0;
 		    break;
@@ -2117,12 +2251,12 @@
 	    }
 	    if (check == CUR_PTR) {
 	        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, 
+		    ctxt->sax->error(ctxt->userData, 
 		    "xmlParseAttributeListDecl: detected internal error\n");
 		break;
 	    }
 	    if ((ctxt->sax != NULL) && (ctxt->sax->attributeDecl != NULL))
-		ctxt->sax->attributeDecl(ctxt, elemName, attrName,
+		ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName,
 	                        type, def, defaultValue, tree);
 	    if (attrName != NULL)
 		free(attrName);
@@ -2169,7 +2303,7 @@
 	    if (ret == NULL) return(NULL);
 	} /********** else {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, 
+		ctxt->sax->error(ctxt->userData, 
 		    "xmlParseElementMixedContentDecl : '|' or ')' expected\n");
 	    ctxt->wellFormed = 0;
 	    return(NULL);
@@ -2193,7 +2327,7 @@
 	    elem = xmlParseName(ctxt);
 	    if (elem == NULL) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, 
+		    ctxt->sax->error(ctxt->userData, 
 			"xmlParseElementMixedContentDecl : Name expected\n");
 		ctxt->wellFormed = 0;
 		xmlFreeElementContent(cur);
@@ -2212,7 +2346,7 @@
 	} else {
 	    if (elem != NULL) free(elem);
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, 
+		ctxt->sax->error(ctxt->userData, 
 		    "xmlParseElementMixedContentDecl : '|' or ')*' expected\n");
 	    ctxt->wellFormed = 0;
 	    xmlFreeElementContent(ret);
@@ -2221,7 +2355,7 @@
 
     } else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, 
+	    ctxt->sax->error(ctxt->userData, 
 		"xmlParseElementMixedContentDecl : '#PCDATA' expected\n");
 	ctxt->wellFormed = 0;
     }
@@ -2264,7 +2398,7 @@
 	elem = xmlParseName(ctxt);
 	if (elem == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, 
+		ctxt->sax->error(ctxt->userData, 
 		"xmlParseElementChildrenContentDecl : Name or '(' expected\n");
 	    ctxt->wellFormed = 0;
 	    return(NULL);
@@ -2297,7 +2431,7 @@
 	     */
 	    else if (type != CUR) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, 
+		    ctxt->sax->error(ctxt->userData, 
 		    "xmlParseElementChildrenContentDecl : '%c' expected\n",
 		    type);
 		ctxt->wellFormed = 0;
@@ -2328,7 +2462,7 @@
 	     */
 	    else if (type != CUR) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, 
+		    ctxt->sax->error(ctxt->userData, 
 		    "xmlParseElementChildrenContentDecl : '%c' expected\n",
 		    type);
 		ctxt->wellFormed = 0;
@@ -2353,7 +2487,7 @@
 	    }
 	} else {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, 
+		ctxt->sax->error(ctxt->userData, 
 	    "xmlParseElementChildrenContentDecl : ',' '|' or ')' expected\n");
 	    ctxt->wellFormed = 0;
 	    xmlFreeElementContent(ret);
@@ -2370,7 +2504,7 @@
 	    elem = xmlParseName(ctxt);
 	    if (elem == NULL) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, 
+		    ctxt->sax->error(ctxt->userData, 
 		    "xmlParseElementChildrenContentDecl : Name or '(' expected\n");
 		ctxt->wellFormed = 0;
 		return(NULL);
@@ -2436,7 +2570,7 @@
 
     if (CUR != '(') {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, 
+	    ctxt->sax->error(ctxt->userData, 
 		"xmlParseElementContentDecl : '(' expected\n");
 	ctxt->wellFormed = 0;
 	return(-1);
@@ -2457,7 +2591,7 @@
     /****************************
     if (CUR != ')') {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, 
+	    ctxt->sax->error(ctxt->userData, 
 		"xmlParseElementContentDecl : ')' expected\n");
 	ctxt->wellFormed = 0;
 	return(-1);
@@ -2493,7 +2627,7 @@
 	SKIP(9);
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, 
+		ctxt->sax->error(ctxt->userData, 
 		    "Space required after 'ELEMENT'\n");
 	    ctxt->wellFormed = 0;
 	}
@@ -2501,14 +2635,14 @@
         name = xmlParseName(ctxt);
 	if (name == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt,
+	        ctxt->sax->error(ctxt->userData,
 		   "xmlParseElementDecl: no name for Element\n");
 	    ctxt->wellFormed = 0;
 	    return(-1);
 	}
 	if (!IS_BLANK(CUR)) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		ctxt->sax->error(ctxt, 
+		ctxt->sax->error(ctxt->userData, 
 		    "Space required after the element name\n");
 	    ctxt->wellFormed = 0;
 	}
@@ -2532,7 +2666,7 @@
 	    ret = xmlParseElementContentDecl(ctxt, name, &content);
 	} else {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, 
+	        ctxt->sax->error(ctxt->userData, 
 	          "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n");
 	    ctxt->wellFormed = 0;
 	    if (name != NULL) free(name);
@@ -2541,13 +2675,13 @@
 	SKIP_BLANKS;
 	if (CUR != '>') {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, 
+	        ctxt->sax->error(ctxt->userData, 
 	          "xmlParseElementDecl: expected '>' at the end\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    NEXT;
 	    if ((ctxt->sax != NULL) && (ctxt->sax->elementDecl != NULL))
-	        ctxt->sax->elementDecl(ctxt, name, ret, content);
+	        ctxt->sax->elementDecl(ctxt->userData, name, ret, content);
 	}
 	if (name != NULL) {
 	    free(name);
@@ -2605,7 +2739,7 @@
 	        val = val * 16 + (CUR - 'A') + 10;
 	    else {
 	        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, 
+		    ctxt->sax->error(ctxt->userData, 
 		         "xmlParseCharRef: invalid hexadecimal value\n");
 		ctxt->wellFormed = 0;
 		val = 0;
@@ -2622,7 +2756,7 @@
 	        val = val * 10 + (CUR - '0');
 	    else {
 	        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, 
+		    ctxt->sax->error(ctxt->userData, 
 		         "xmlParseCharRef: invalid decimal value\n");
 		ctxt->wellFormed = 0;
 		val = 0;
@@ -2634,7 +2768,7 @@
 	    NEXT;
     } else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "xmlParseCharRef: invalid value\n");
+	    ctxt->sax->error(ctxt->userData, "xmlParseCharRef: invalid value\n");
 	ctxt->wellFormed = 0;
     }
     /*
@@ -2646,7 +2780,7 @@
 	return(xmlStrndup(buf, 1));
     } else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "xmlParseCharRef: invalid CHAR value %d\n",
+	    ctxt->sax->error(ctxt->userData, "xmlParseCharRef: invalid CHAR value %d\n",
 	                     val);
 	ctxt->wellFormed = 0;
     }
@@ -2677,7 +2811,7 @@
         name = xmlParseName(ctxt);
 	if (name == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "xmlParseEntityRef: no name\n");
+	        ctxt->sax->error(ctxt->userData, "xmlParseEntityRef: no name\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    if (CUR == ';') {
@@ -2695,18 +2829,18 @@
 		 */
 		if (ctxt->sax != NULL) {
 		    if (ctxt->sax->getEntity != NULL)
-			ent = ctxt->sax->getEntity(ctxt, name);
+			ent = ctxt->sax->getEntity(ctxt->userData, name);
 
 		    if (((ctxt->sax->isStandalone != NULL) &&
-		         ctxt->sax->isStandalone(ctxt) == 1) ||
+		         ctxt->sax->isStandalone(ctxt->userData) == 1) ||
 			(((ctxt->sax->hasInternalSubset == NULL) ||
-			  ctxt->sax->hasInternalSubset(ctxt) == 0) &&
+			  ctxt->sax->hasInternalSubset(ctxt->userData) == 0) &&
 			 ((ctxt->sax->hasExternalSubset == NULL) ||
-			  ctxt->sax->hasExternalSubset(ctxt) == 0))) {
+			  ctxt->sax->hasExternalSubset(ctxt->userData) == 0))) {
 			if (ent == NULL) {
 			    if ((ctxt->sax != NULL) &&
 			        (ctxt->sax->error != NULL))
-				ctxt->sax->error(ctxt, 
+				ctxt->sax->error(ctxt->userData, 
 				     "Entity '%s' not defined\n", name);
 			    ctxt->wellFormed = 0;
 			}
@@ -2723,14 +2857,14 @@
 			case XML_INTERNAL_PARAMETER_ENTITY:
 			case XML_EXTERNAL_PARAMETER_ENTITY:
 			if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-			    ctxt->sax->error(ctxt, 
+			    ctxt->sax->error(ctxt->userData, 
 		     "Attempt to reference the parameter entity '%s'\n", name);
 			ctxt->wellFormed = 0;
 			break;
                         
 			case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
 			if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-			    ctxt->sax->error(ctxt, 
+			    ctxt->sax->error(ctxt->userData, 
 		     "Attempt to reference unparsed entity '%s'\n", name);
 			ctxt->wellFormed = 0;
 			break;
@@ -2749,7 +2883,7 @@
 		 *   - or keep the reference inline
 		 */
 		if ((ctxt->sax) && (ctxt->sax->resolveEntity != NULL))
-		    input = ctxt->sax->resolveEntity(ctxt, NULL, name);
+		    input = ctxt->sax->resolveEntity(ctxt->userData, NULL, name);
 		if (input != NULL)
 		    xmlPushInput(ctxt, input);
 		else {
@@ -2759,7 +2893,7 @@
 		char cst[2] = { '&', 0 };
 
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt,
+		    ctxt->sax->error(ctxt->userData,
 		                     "xmlParseEntityRef: expecting ';'\n");
 		ctxt->wellFormed = 0;
 		ret = xmlStrndup(cst, 1);
@@ -2821,17 +2955,17 @@
         name = xmlParseName(ctxt);
 	if (name == NULL) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "xmlParsePEReference: no name\n");
+	        ctxt->sax->error(ctxt->userData, "xmlParsePEReference: no name\n");
 	    ctxt->wellFormed = 0;
 	} else {
 	    if (CUR == ';') {
 	        NEXT;
 		if ((ctxt->sax != NULL) && (ctxt->sax->getEntity != NULL))
-			entity = ctxt->sax->getEntity(ctxt, name);
+			entity = ctxt->sax->getEntity(ctxt->userData, name);
 		/* TODO !!!! Must check that it's of the proper type !!! */
 		if (entity == NULL) {
 		    if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
-		        ctxt->sax->warning(ctxt,
+		        ctxt->sax->warning(ctxt->userData,
 		         "xmlParsePEReference: %%%s; not found\n", name);
 		} else {
 		    input = xmlNewEntityInputStream(ctxt, entity);
@@ -2841,7 +2975,7 @@
 		char cst[2] = { '%', 0 };
 
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt,
+		    ctxt->sax->error(ctxt->userData,
 		                     "xmlParsePEReference: expecting ';'\n");
 		ctxt->wellFormed = 0;
 		ret = xmlStrndup(cst, 1);
@@ -2882,7 +3016,7 @@
     name = xmlParseName(ctxt);
     if (name == NULL) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "xmlParseDocTypeDecl : no DOCTYPE name !\n");
+	    ctxt->sax->error(ctxt->userData, "xmlParseDocTypeDecl : no DOCTYPE name !\n");
 	ctxt->wellFormed = 0;
     }
 
@@ -2895,7 +3029,7 @@
     SKIP_BLANKS;
 
     if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL))
-	ctxt->sax->internalSubset(ctxt, name, ExternalID, URI);
+	ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
 
     /*
      * Is there any DTD definition ?
@@ -2916,7 +3050,7 @@
 
 	    if (CUR_PTR == check) {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, 
+		    ctxt->sax->error(ctxt->userData, 
 		 "xmlParseDocTypeDecl: error detected in Markup declaration\n");
 		ctxt->wellFormed = 0;
 		break;
@@ -2930,7 +3064,7 @@
      */
     if (CUR != '>') {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "DOCTYPE unproperly terminated\n");
+	    ctxt->sax->error(ctxt->userData, "DOCTYPE unproperly terminated\n");
 	ctxt->wellFormed = 0;
         /* We shouldn't try to resynchronize ... */
     }
@@ -2974,7 +3108,7 @@
     name = xmlParseName(ctxt);
     if (name == NULL) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "error parsing attribute name\n");
+	    ctxt->sax->error(ctxt->userData, "error parsing attribute name\n");
 	ctxt->wellFormed = 0;
         return(NULL);
     }
@@ -2989,7 +3123,7 @@
 	val = xmlParseAttValue(ctxt);
     } else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	       "Specification mandate value for attribute %s\n", name);
 	ctxt->wellFormed = 0;
 	return(NULL);
@@ -3033,7 +3167,7 @@
     name = xmlParseName(ctxt);
     if (name == NULL) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, 
+	    ctxt->sax->error(ctxt->userData, 
 	     "xmlParseStartTag: invalid element name\n");
 	ctxt->wellFormed = 0;
         return;
@@ -3058,7 +3192,7 @@
 	    for (i = 0; i < nbatts;i += 2) {
 	        if (!xmlStrcmp(atts[i], attname)) {
 		    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-			ctxt->sax->error(ctxt, "Attribute %s redefined\n",
+			ctxt->sax->error(ctxt->userData, "Attribute %s redefined\n",
 			                 name);
 		    ctxt->wellFormed = 0;
 		    free(attname);
@@ -3096,7 +3230,7 @@
 	SKIP_BLANKS;
         if (q == CUR_PTR) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, 
+	        ctxt->sax->error(ctxt->userData, 
 	         "xmlParseStartTag: problem parsing attributes\n");
 	    ctxt->wellFormed = 0;
 	    break;
@@ -3107,7 +3241,7 @@
      * SAX: Start of Element !
      */
     if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
-        ctxt->sax->startElement(ctxt, name, atts);
+        ctxt->sax->startElement(ctxt->userData, name, atts);
 
     free(name);
     if (atts != NULL) {
@@ -3135,7 +3269,7 @@
 
     if ((CUR != '<') || (NXT(1) != '/')) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "xmlParseEndTag: '</' not found\n");
+	    ctxt->sax->error(ctxt->userData, "xmlParseEndTag: '</' not found\n");
 	ctxt->wellFormed = 0;
 	return;
     }
@@ -3149,7 +3283,7 @@
     SKIP_BLANKS;
     if ((!IS_CHAR(CUR)) || (CUR != '>')) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "End tag : expected '>'\n");
+	    ctxt->sax->error(ctxt->userData, "End tag : expected '>'\n");
 	ctxt->wellFormed = 0;
     } else
 	NEXT;
@@ -3158,7 +3292,7 @@
      * SAX: End of Tag
      */
     if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
-        ctxt->sax->endElement(ctxt, name);
+        ctxt->sax->endElement(ctxt->userData, name);
 
     if (name != NULL)
 	free(name);
@@ -3195,14 +3329,14 @@
     base = CUR_PTR;
     if (!IS_CHAR(CUR)) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "CData section not finished\n%.50s\n", base);
+	    ctxt->sax->error(ctxt->userData, "CData section not finished\n%.50s\n", base);
 	ctxt->wellFormed = 0;
         return;
     }
     r = NEXT;
     if (!IS_CHAR(CUR)) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "CData section not finished\n%.50s\n", base);
+	    ctxt->sax->error(ctxt->userData, "CData section not finished\n%.50s\n", base);
 	ctxt->wellFormed = 0;
         return;
     }
@@ -3213,7 +3347,7 @@
     }
     if (!IS_CHAR(CUR)) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "CData section not finished\n%.50s\n", base);
+	    ctxt->sax->error(ctxt->userData, "CData section not finished\n%.50s\n", base);
 	ctxt->wellFormed = 0;
         return;
     }
@@ -3224,11 +3358,11 @@
     if (ctxt->sax != NULL) {
 	if (areBlanks(ctxt, base, CUR_PTR - base)) {
 	    if (ctxt->sax->ignorableWhitespace != NULL)
-		ctxt->sax->ignorableWhitespace(ctxt, base,
+		ctxt->sax->ignorableWhitespace(ctxt->userData, base,
 		                               (CUR_PTR - base) - 2);
 	 } else {
 	    if (ctxt->sax->characters != NULL)
-		ctxt->sax->characters(ctxt, base, (CUR_PTR - base) - 2);
+		ctxt->sax->characters(ctxt->userData, base, (CUR_PTR - base) - 2);
 	 }
     }
 }
@@ -3292,13 +3426,13 @@
 		     * inline predefined entity.
 		     */
                     if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
-			ctxt->sax->characters(ctxt, val, xmlStrlen(val));
+			ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val));
 		} else {
 		    /*
 		     * user defined entity, create a node.
 		     */
                     if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL))
-			ctxt->sax->reference(ctxt, val);
+			ctxt->sax->reference(ctxt->userData, val);
 		}
 		free(val);
 	    }
@@ -3319,7 +3453,7 @@
 
 	if (test == CUR_PTR) {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt,
+	        ctxt->sax->error(ctxt->userData,
 		     "detected an error in element content\n");
 	    ctxt->wellFormed = 0;
             break;
@@ -3355,13 +3489,13 @@
     if ((CUR == '/') && (NXT(1) == '>')) {
         SKIP(2);
 	if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
-	    ctxt->sax->endElement(ctxt, NULL);
+	    ctxt->sax->endElement(ctxt->userData, NULL);
 	return;
     }
     if (CUR == '>') NEXT;
     else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "Couldn't find end of Start Tag\n%.30s\n",
+	    ctxt->sax->error(ctxt->userData, "Couldn't find end of Start Tag\n%.30s\n",
 	                     openTag);
 	ctxt->wellFormed = 0;
 
@@ -3380,7 +3514,7 @@
     xmlParseContent(ctxt);
     if (!IS_CHAR(CUR)) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	         "Premature end of data in tag %.30s\n", openTag);
 	ctxt->wellFormed = 0;
 
@@ -3450,7 +3584,7 @@
 	SKIP_BLANKS;
 	if (CUR != '=') {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "xmlParseVersionInfo : expected '='\n");
+	        ctxt->sax->error(ctxt->userData, "xmlParseVersionInfo : expected '='\n");
 	    ctxt->wellFormed = 0;
 	    return(NULL);
         }
@@ -3462,7 +3596,7 @@
 	    version = xmlParseVersionNum(ctxt);
 	    if (CUR != '"') {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, "String not closed\n%.50s\n", q);
+		    ctxt->sax->error(ctxt->userData, "String not closed\n%.50s\n", q);
 		ctxt->wellFormed = 0;
 	    } else
 	        NEXT;
@@ -3472,13 +3606,13 @@
 	    version = xmlParseVersionNum(ctxt);
 	    if (CUR != '\'') {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, "String not closed\n%.50s\n", q);
+		    ctxt->sax->error(ctxt->userData, "String not closed\n%.50s\n", q);
 		ctxt->wellFormed = 0;
 	    } else
 	        NEXT;
 	} else {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt,
+	        ctxt->sax->error(ctxt->userData,
 		      "xmlParseVersionInfo : expected ' or \"\n");
 		ctxt->wellFormed = 0;
 	}
@@ -3512,7 +3646,7 @@
 	ret = xmlStrndup(q, CUR_PTR - q);
     } else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "Invalid XML encoding name\n");
+	    ctxt->sax->error(ctxt->userData, "Invalid XML encoding name\n");
 	ctxt->wellFormed = 0;
     }
     return(ret);
@@ -3545,7 +3679,7 @@
 	SKIP_BLANKS;
 	if (CUR != '=') {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "xmlParseEncodingDecl : expected '='\n");
+	        ctxt->sax->error(ctxt->userData, "xmlParseEncodingDecl : expected '='\n");
 	    ctxt->wellFormed = 0;
 	    return(NULL);
         }
@@ -3557,7 +3691,7 @@
 	    encoding = xmlParseEncName(ctxt);
 	    if (CUR != '"') {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, "String not closed\n%.50s\n", q);
+		    ctxt->sax->error(ctxt->userData, "String not closed\n%.50s\n", q);
 		ctxt->wellFormed = 0;
 	    } else
 	        NEXT;
@@ -3567,13 +3701,13 @@
 	    encoding = xmlParseEncName(ctxt);
 	    if (CUR != '\'') {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, "String not closed\n%.50s\n", q);
+		    ctxt->sax->error(ctxt->userData, "String not closed\n%.50s\n", q);
 		ctxt->wellFormed = 0;
 	    } else
 	        NEXT;
 	} else if (CUR == '"'){
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt,
+	        ctxt->sax->error(ctxt->userData,
 		     "xmlParseEncodingDecl : expected ' or \"\n");
 	    ctxt->wellFormed = 0;
 	}
@@ -3606,7 +3740,7 @@
 	SKIP(10);
 	if (CUR != '=') {
 	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt,
+	        ctxt->sax->error(ctxt->userData,
 		    "XML standalone declaration : expected '='\n");
 	    ctxt->wellFormed = 0;
 	    return(standalone);
@@ -3624,12 +3758,12 @@
 		SKIP(3);
             } else {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, "standalone accepts only 'yes' or 'no'\n");
+		    ctxt->sax->error(ctxt->userData, "standalone accepts only 'yes' or 'no'\n");
 		ctxt->wellFormed = 0;
 	    }
 	    if (CUR != '\'') {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, "String not closed\n");
+		    ctxt->sax->error(ctxt->userData, "String not closed\n");
 		ctxt->wellFormed = 0;
 	    } else
 	        NEXT;
@@ -3644,19 +3778,19 @@
                 SKIP(3);
             } else {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt,
+		    ctxt->sax->error(ctxt->userData,
 		        "standalone accepts only 'yes' or 'no'\n");
 		ctxt->wellFormed = 0;
 	    }
 	    if (CUR != '"') {
 		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-		    ctxt->sax->error(ctxt, "String not closed\n");
+		    ctxt->sax->error(ctxt->userData, "String not closed\n");
 		ctxt->wellFormed = 0;
 	    } else
 	        NEXT;
 	} else {
             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	        ctxt->sax->error(ctxt, "Standalone value not found\n");
+	        ctxt->sax->error(ctxt->userData, "Standalone value not found\n");
 	    ctxt->wellFormed = 0;
         }
     }
@@ -3683,7 +3817,7 @@
 
     if (!IS_BLANK(CUR)) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "Blank needed after '<?xml'\n");
+	    ctxt->sax->error(ctxt->userData, "Blank needed after '<?xml'\n");
 	ctxt->wellFormed = 0;
     }
     SKIP_BLANKS;
@@ -3706,7 +3840,7 @@
 	    return;
 	}
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "Blank needed here\n");
+	    ctxt->sax->error(ctxt->userData, "Blank needed here\n");
 	ctxt->wellFormed = 0;
     }
     ctxt->encoding = xmlParseEncodingDecl(ctxt);
@@ -3720,7 +3854,7 @@
 	    return;
 	}
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "Blank needed here\n");
+	    ctxt->sax->error(ctxt->userData, "Blank needed here\n");
 	ctxt->wellFormed = 0;
     }
     SKIP_BLANKS;
@@ -3732,12 +3866,12 @@
     } else if (CUR == '>') {
         /* Deprecated old WD ... */
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "XML declaration must end-up with '?>'\n");
+	    ctxt->sax->error(ctxt->userData, "XML declaration must end-up with '?>'\n");
 	ctxt->wellFormed = 0;
 	NEXT;
     } else {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "parsing XML declaration: '?>' expected\n");
+	    ctxt->sax->error(ctxt->userData, "parsing XML declaration: '?>' expected\n");
 	ctxt->wellFormed = 0;
 	MOVETO_ENDTAG(CUR_PTR);
 	NEXT;
@@ -3791,7 +3925,7 @@
      * SAX: beginning of the document processing.
      */
     if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
-        ctxt->sax->setDocumentLocator(ctxt, &xmlDefaultSAXLocator);
+        ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
 
     /*
      * We should check for encoding here and plug-in some
@@ -3803,7 +3937,7 @@
      */
     if (IS_BLANK(CUR)) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	    "Extra spaces at the beginning of the document are not allowed\n");
 	ctxt->wellFormed = 0;
 	SKIP_BLANKS;
@@ -3811,7 +3945,7 @@
 
     if (CUR == 0) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "Document is empty\n");
+	    ctxt->sax->error(ctxt->userData, "Document is empty\n");
 	ctxt->wellFormed = 0;
     }
 
@@ -3838,7 +3972,7 @@
 	ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
     }
     if ((ctxt->sax) && (ctxt->sax->startDocument))
-        ctxt->sax->startDocument(ctxt);
+        ctxt->sax->startDocument(ctxt->userData);
 
     /*
      * The Misc part of the Prolog
@@ -3870,7 +4004,7 @@
 
     if (CUR != 0) {
 	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt,
+	    ctxt->sax->error(ctxt->userData,
 	        "Extra content at the end of the document\n");
 	ctxt->wellFormed = 0;
     }
@@ -3879,7 +4013,7 @@
      * SAX: end of the document processing.
      */
     if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
-        ctxt->sax->endDocument(ctxt);
+        ctxt->sax->endDocument(ctxt->userData);
     if (! ctxt->wellFormed) return(-1);
     return(0);
 }
@@ -3896,6 +4030,7 @@
 xmlCreateDocParserCtxt(CHAR *cur) {
     xmlParserCtxtPtr ctxt;
     xmlParserInputPtr input;
+    xmlCharEncoding enc;
 
     ctxt = (xmlParserCtxtPtr) malloc(sizeof(xmlParserCtxt));
     if (ctxt == NULL) {
@@ -3910,6 +4045,12 @@
 	return(NULL);
     }
 
+    /*
+     * plug some encoding conversion routines here. !!!
+     */
+    enc = xmlDetectCharEncoding(cur);
+    xmlSwitchEncoding(ctxt, enc);
+
     input->filename = NULL;
     input->line = 1;
     input->col = 1;
@@ -3945,7 +4086,10 @@
 
     ctxt = xmlCreateDocParserCtxt(cur);
     if (ctxt == NULL) return(NULL);
-    if (sax != NULL) ctxt->sax = sax;
+    if (sax != NULL) { 
+        ctxt->sax = sax;
+        ctxt->userData = NULL;
+    }
 
     xmlParseDocument(ctxt);
     if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
@@ -4012,6 +4156,7 @@
     struct stat buf;
     char *buffer;
     xmlParserInputPtr inputStream;
+    xmlCharEncoding enc;
 
     res = stat(filename, &buf);
     if (res < 0) return(NULL);
@@ -4095,8 +4240,11 @@
     inputStream->col = 1;
 
     /*
-     * TODO : plug some encoding conversion routines here. !!!
+     * plug some encoding conversion routines here. !!!
      */
+    enc = xmlDetectCharEncoding(buffer);
+    xmlSwitchEncoding(ctxt, enc);
+
     inputStream->base = buffer;
     inputStream->cur = buffer;
     inputStream->free = (xmlParserInputDeallocate) free;
@@ -4127,7 +4275,10 @@
 
     ctxt = xmlCreateFileParserCtxt(filename);
     if (ctxt == NULL) return(NULL);
-    if (sax != NULL) ctxt->sax = sax;
+    if (sax != NULL) {
+        ctxt->sax = sax;
+        ctxt->userData = NULL;
+    }
 
     xmlParseDocument(ctxt);
 
@@ -4184,6 +4335,7 @@
 xmlCreateMemoryParserCtxt(char *buffer, int size) {
     xmlParserCtxtPtr ctxt;
     xmlParserInputPtr input;
+    xmlCharEncoding enc;
 
     buffer[size - 1] = '\0';
 
@@ -4207,8 +4359,11 @@
     input->col = 1;
 
     /*
-     * TODO : plug some encoding conversion routines here. !!!
+     * plug some encoding conversion routines here. !!!
      */
+    enc = xmlDetectCharEncoding(buffer);
+    xmlSwitchEncoding(ctxt, enc);
+
     input->base = buffer;
     input->cur = buffer;
     input->free = NULL;
@@ -4229,8 +4384,6 @@
  * to handle the parsing callback. If sax is NULL, fallback to the default
  * DOM tree building routines.
  * 
- * TODO : plug some encoding conversion routines here. !!!
- *
  * Returns the resulting document tree
  */
 xmlDocPtr
@@ -4240,7 +4393,10 @@
 
     ctxt = xmlCreateMemoryParserCtxt(buffer, size);
     if (ctxt == NULL) return(NULL);
-    if (sax != NULL) ctxt->sax = sax;
+    if (sax != NULL) {
+        ctxt->sax = sax;
+        ctxt->userData = NULL;
+    }
 
     xmlParseDocument(ctxt);
 
@@ -4310,6 +4466,7 @@
     ctxt->node = NULL;
 
     ctxt->sax = &xmlDefaultSAXHandler;
+    ctxt->userData = ctxt;
     ctxt->myDoc = NULL;
     ctxt->wellFormed = 1;
     ctxt->record_info = 0;
@@ -4521,7 +4678,7 @@
 
       if ( tmp_buffer == NULL ) {
         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
-	    ctxt->sax->error(ctxt, "Out of memory\n");
+	    ctxt->sax->error(ctxt->userData, "Out of memory\n");
         return;
       }
       ctxt->node_seq.buffer = tmp_buffer;
diff --git a/parser.h b/parser.h
index 3e433a2..2075607 100644
--- a/parser.h
+++ b/parser.h
@@ -81,10 +81,10 @@
  */
 
 typedef struct xmlSAXLocator {
-    const CHAR *(*getPublicId)(xmlParserCtxtPtr ctxt);
-    const CHAR *(*getSystemId)(xmlParserCtxtPtr ctxt);
-    int (*getLineNumber)(xmlParserCtxtPtr ctxt);
-    int (*getColumnNumber)(xmlParserCtxtPtr ctxt);
+    const CHAR *(*getPublicId)(void *ctx);
+    const CHAR *(*getSystemId)(void *ctx);
+    int (*getLineNumber)(void *ctx);
+    int (*getColumnNumber)(void *ctx);
 } _xmlSAXLocator;
 typedef _xmlSAXLocator xmlSAXLocator;
 typedef xmlSAXLocator *xmlSAXLocatorPtr;
@@ -95,48 +95,48 @@
 
 #include "entities.h"
 
-typedef xmlParserInputPtr (*resolveEntitySAXFunc) (xmlParserCtxtPtr ctxt,
+typedef xmlParserInputPtr (*resolveEntitySAXFunc) (void *ctx,
 			    const CHAR *publicId, const CHAR *systemId);
-typedef void (*internalSubsetSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *name,
+typedef void (*internalSubsetSAXFunc) (void *ctx, const CHAR *name,
                             const CHAR *ExternalID, const CHAR *SystemID);
-typedef xmlEntityPtr (*getEntitySAXFunc) (xmlParserCtxtPtr ctxt,
+typedef xmlEntityPtr (*getEntitySAXFunc) (void *ctx,
                             const CHAR *name);
-typedef void (*entityDeclSAXFunc) (xmlParserCtxtPtr ctxt,
+typedef void (*entityDeclSAXFunc) (void *ctx,
                             const CHAR *name, int type, const CHAR *publicId,
 			    const CHAR *systemId, CHAR *content);
-typedef void (*notationDeclSAXFunc)(xmlParserCtxtPtr ctxt, const CHAR *name,
+typedef void (*notationDeclSAXFunc)(void *ctx, const CHAR *name,
 			    const CHAR *publicId, const CHAR *systemId);
-typedef void (*attributeDeclSAXFunc)(xmlParserCtxtPtr ctxt, const CHAR *elem,
+typedef void (*attributeDeclSAXFunc)(void *ctx, const CHAR *elem,
                             const CHAR *name, int type, int def,
 			    const CHAR *defaultValue, xmlEnumerationPtr tree);
-typedef void (*elementDeclSAXFunc)(xmlParserCtxtPtr ctxt, const CHAR *name,
+typedef void (*elementDeclSAXFunc)(void *ctx, const CHAR *name,
 			    int type, xmlElementContentPtr content);
-typedef void (*unparsedEntityDeclSAXFunc)(xmlParserCtxtPtr ctxt,
+typedef void (*unparsedEntityDeclSAXFunc)(void *ctx,
                             const CHAR *name, const CHAR *publicId,
 			    const CHAR *systemId, const CHAR *notationName);
-typedef void (*setDocumentLocatorSAXFunc) (xmlParserCtxtPtr ctxt,
+typedef void (*setDocumentLocatorSAXFunc) (void *ctx,
                             xmlSAXLocatorPtr loc);
-typedef void (*startDocumentSAXFunc) (xmlParserCtxtPtr ctxt);
-typedef void (*endDocumentSAXFunc) (xmlParserCtxtPtr ctxt);
-typedef void (*startElementSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *name,
+typedef void (*startDocumentSAXFunc) (void *ctx);
+typedef void (*endDocumentSAXFunc) (void *ctx);
+typedef void (*startElementSAXFunc) (void *ctx, const CHAR *name,
                             const CHAR **atts);
-typedef void (*endElementSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *name);
-typedef void (*attributeSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *name,
+typedef void (*endElementSAXFunc) (void *ctx, const CHAR *name);
+typedef void (*attributeSAXFunc) (void *ctx, const CHAR *name,
                                   const CHAR *value);
-typedef void (*referenceSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *name);
-typedef void (*charactersSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *ch,
+typedef void (*referenceSAXFunc) (void *ctx, const CHAR *name);
+typedef void (*charactersSAXFunc) (void *ctx, const CHAR *ch,
 		            int len);
-typedef void (*ignorableWhitespaceSAXFunc) (xmlParserCtxtPtr ctxt,
+typedef void (*ignorableWhitespaceSAXFunc) (void *ctx,
 			    const CHAR *ch, int len);
-typedef void (*processingInstructionSAXFunc) (xmlParserCtxtPtr ctxt,
+typedef void (*processingInstructionSAXFunc) (void *ctx,
                             const CHAR *target, const CHAR *data);
-typedef void (*commentSAXFunc) (xmlParserCtxtPtr ctxt, const CHAR *value);
-typedef void (*warningSAXFunc) (xmlParserCtxtPtr ctxt, const char *msg, ...);
-typedef void (*errorSAXFunc) (xmlParserCtxtPtr ctxt, const char *msg, ...);
-typedef void (*fatalErrorSAXFunc) (xmlParserCtxtPtr ctxt, const char *msg, ...);
-typedef int (*isStandaloneSAXFunc) (xmlParserCtxtPtr ctxt);
-typedef int (*hasInternalSubsetSAXFunc) (xmlParserCtxtPtr ctxt);
-typedef int (*hasExternalSubsetSAXFunc) (xmlParserCtxtPtr ctxt);
+typedef void (*commentSAXFunc) (void *ctx, const CHAR *value);
+typedef void (*warningSAXFunc) (void *ctx, const char *msg, ...);
+typedef void (*errorSAXFunc) (void *ctx, const char *msg, ...);
+typedef void (*fatalErrorSAXFunc) (void *ctx, const char *msg, ...);
+typedef int (*isStandaloneSAXFunc) (void *ctx);
+typedef int (*hasInternalSubsetSAXFunc) (void *ctx);
+typedef int (*hasExternalSubsetSAXFunc) (void *ctx);
 
 typedef struct xmlSAXHandler {
     internalSubsetSAXFunc internalSubset;
diff --git a/testSAX.c b/testSAX.c
index 2ce82cb..f742bce 100644
--- a/testSAX.c
+++ b/testSAX.c
@@ -104,7 +104,7 @@
 int
 isStandaloneDebug(xmlParserCtxtPtr ctxt)
 {
-    fprintf(stderr, "SAX.isStandalone()\n");
+    fprintf(stdout, "SAX.isStandalone()\n");
     return(0);
 }
 
@@ -119,7 +119,7 @@
 int
 hasInternalSubsetDebug(xmlParserCtxtPtr ctxt)
 {
-    fprintf(stderr, "SAX.hasInternalSubset()\n");
+    fprintf(stdout, "SAX.hasInternalSubset()\n");
     return(0);
 }
 
@@ -134,7 +134,7 @@
 int
 hasExternalSubsetDebug(xmlParserCtxtPtr ctxt)
 {
-    fprintf(stderr, "SAX.hasExternalSubset()\n");
+    fprintf(stdout, "SAX.hasExternalSubset()\n");
     return(0);
 }
 
@@ -148,7 +148,7 @@
 internalSubsetDebug(xmlParserCtxtPtr ctxt, const CHAR *name,
 	       const CHAR *ExternalID, const CHAR *SystemID)
 {
-    fprintf(stderr, "SAX.internalSubset(%s, %s, %s)\n",
+    fprintf(stdout, "SAX.internalSubset(%s, %s, %s)\n",
             name, ExternalID, SystemID);
 }
 
@@ -169,7 +169,7 @@
 xmlParserInputPtr
 resolveEntityDebug(xmlParserCtxtPtr ctxt, const CHAR *publicId, const CHAR *systemId)
 {
-    fprintf(stderr, "SAX.resolveEntity(%s, %s)\n",
+    fprintf(stdout, "SAX.resolveEntity(%s, %s)\n",
             (char *)publicId, (char *)systemId);
     return(NULL);
 }
@@ -186,7 +186,7 @@
 xmlEntityPtr
 getEntityDebug(xmlParserCtxtPtr ctxt, const CHAR *name)
 {
-    fprintf(stderr, "SAX.getEntity(%s)\n", name);
+    fprintf(stdout, "SAX.getEntity(%s)\n", name);
     return(NULL);
 }
 
@@ -206,7 +206,7 @@
 entityDeclDebug(xmlParserCtxtPtr ctxt, const CHAR *name, int type,
           const CHAR *publicId, const CHAR *systemId, CHAR *content)
 {
-    fprintf(stderr, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
+    fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
             name, type, publicId, systemId, content);
 }
 
@@ -223,7 +223,7 @@
               int type, int def, const CHAR *defaultValue,
 	      xmlEnumerationPtr tree)
 {
-    fprintf(stderr, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
+    fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
             elem, name, type, def, defaultValue);
 }
 
@@ -240,7 +240,7 @@
 elementDeclDebug(xmlParserCtxtPtr ctxt, const CHAR *name, int type,
 	    xmlElementContentPtr content)
 {
-    fprintf(stderr, "SAX.elementDecl(%s, %d, ...)\n",
+    fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
             name, type);
 }
 
@@ -258,7 +258,7 @@
 notationDeclDebug(xmlParserCtxtPtr ctxt, const CHAR *name,
 	     const CHAR *publicId, const CHAR *systemId)
 {
-    fprintf(stderr, "SAX.notationDecl(%s, %s, %s)\n",
+    fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
             (char *) name, (char *) publicId, (char *) systemId);
 }
 
@@ -278,7 +278,7 @@
 		   const CHAR *publicId, const CHAR *systemId,
 		   const CHAR *notationName)
 {
-    fprintf(stderr, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
+    fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
             (char *) name, (char *) publicId, (char *) systemId,
 	    (char *) notationName);
 }
@@ -294,7 +294,7 @@
 void
 setDocumentLocatorDebug(xmlParserCtxtPtr ctxt, xmlSAXLocatorPtr loc)
 {
-    fprintf(stderr, "SAX.setDocumentLocator()\n");
+    fprintf(stdout, "SAX.setDocumentLocator()\n");
 }
 
 /**
@@ -306,7 +306,7 @@
 void
 startDocumentDebug(xmlParserCtxtPtr ctxt)
 {
-    fprintf(stderr, "SAX.startDocument()\n");
+    fprintf(stdout, "SAX.startDocument()\n");
 }
 
 /**
@@ -318,7 +318,7 @@
 void
 endDocumentDebug(xmlParserCtxtPtr ctxt)
 {
-    fprintf(stderr, "SAX.endDocument()\n");
+    fprintf(stdout, "SAX.endDocument()\n");
 }
 
 /**
@@ -334,14 +334,14 @@
 {
     int i;
 
-    fprintf(stderr, "SAX.startElement(%s", (char *) name);
+    fprintf(stdout, "SAX.startElement(%s", (char *) name);
     if (atts != NULL) {
         for (i = 0;(atts[i] != NULL);i++) {
-	    fprintf(stderr, ", %s='", atts[i++]);
-	    fprintf(stderr, "%s'", atts[i]);
+	    fprintf(stdout, ", %s='", atts[i++]);
+	    fprintf(stdout, "%s'", atts[i]);
 	}
     }
-    fprintf(stderr, ")\n");
+    fprintf(stdout, ")\n");
 }
 
 /**
@@ -354,7 +354,7 @@
 void
 endElementDebug(xmlParserCtxtPtr ctxt, const CHAR *name)
 {
-    fprintf(stderr, "SAX.endElement(%s)\n", (char *) name);
+    fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
 }
 
 /**
@@ -369,7 +369,7 @@
 void
 charactersDebug(xmlParserCtxtPtr ctxt, const CHAR *ch, int len)
 {
-    fprintf(stderr, "SAX.characters(%.30s, %d)\n", (char *) ch, len);
+    fprintf(stdout, "SAX.characters(%.30s, %d)\n", (char *) ch, len);
 }
 
 /**
@@ -382,7 +382,7 @@
 void
 referenceDebug(xmlParserCtxtPtr ctxt, const CHAR *name)
 {
-    fprintf(stderr, "SAX.reference(%s)\n", name);
+    fprintf(stdout, "SAX.reference(%s)\n", name);
 }
 
 /**
@@ -398,7 +398,7 @@
 void
 ignorableWhitespaceDebug(xmlParserCtxtPtr ctxt, const CHAR *ch, int len)
 {
-    fprintf(stderr, "SAX.ignorableWhitespace(%.30s, %d)\n",
+    fprintf(stdout, "SAX.ignorableWhitespace(%.30s, %d)\n",
             (char *) ch, len);
 }
 
@@ -415,7 +415,7 @@
 processingInstructionDebug(xmlParserCtxtPtr ctxt, const CHAR *target,
                       const CHAR *data)
 {
-    fprintf(stderr, "SAX.processingInstruction(%s, %s)\n",
+    fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
             (char *) target, (char *) data);
 }
 
@@ -429,7 +429,7 @@
 void
 commentDebug(xmlParserCtxtPtr ctxt, const CHAR *value)
 {
-    fprintf(stderr, "SAX.comment(%s)\n", value);
+    fprintf(stdout, "SAX.comment(%s)\n", value);
 }
 
 /**
@@ -447,8 +447,8 @@
     va_list args;
 
     va_start(args, msg);
-    fprintf(stderr, "SAX.warning: ");
-    vfprintf(stderr, msg, args);
+    fprintf(stdout, "SAX.warning: ");
+    vfprintf(stdout, msg, args);
     va_end(args);
 }
 
@@ -467,8 +467,8 @@
     va_list args;
 
     va_start(args, msg);
-    fprintf(stderr, "SAX.error: ");
-    vfprintf(stderr, msg, args);
+    fprintf(stdout, "SAX.error: ");
+    vfprintf(stdout, msg, args);
     va_end(args);
 }
 
@@ -487,8 +487,8 @@
     va_list args;
 
     va_start(args, msg);
-    fprintf(stderr, "SAX.fatalError: ");
-    vfprintf(stderr, msg, args);
+    fprintf(stdout, "SAX.fatalError: ");
+    vfprintf(stdout, msg, args);
     va_end(args);
 }
 
@@ -535,7 +535,7 @@
      */
     doc = xmlSAXParseFile(emptySAXHandler, filename, 0);
     if (doc != NULL) {
-        fprintf(stderr, "xmlSAXParseFile returned non-NULL\n");
+        fprintf(stdout, "xmlSAXParseFile returned non-NULL\n");
 	xmlDocDump(stdout, doc);
     }
 
diff --git a/tree.c b/tree.c
index a3b378a..c1e1ebd 100644
--- a/tree.c
+++ b/tree.c
@@ -395,7 +395,7 @@
     cur->encoding = NULL;
     cur->standalone = -1;
     cur->compression = xmlCompressMode;
-#ifndef WITHOUT_CORBA
+#ifndef XML_WITHOUT_CORBA
     cur->_private = NULL;
     cur->vepv = NULL;
 #endif
@@ -736,7 +736,7 @@
 	cur->val = xmlStringGetNodeList(node->doc, value);
     else 
 	cur->val = NULL;
-#ifndef WITHOUT_CORBA
+#ifndef XML_WITHOUT_CORBA
     cur->_private = NULL;
     cur->vepv = NULL;
 #endif
@@ -792,7 +792,7 @@
 	cur->val = xmlStringGetNodeList(doc, value);
     else 
 	cur->val = NULL;
-#ifndef WITHOUT_CORBA
+#ifndef XML_WITHOUT_CORBA
     cur->_private = NULL;
     cur->vepv = NULL;
 #endif
@@ -879,7 +879,7 @@
     cur->ns = ns;
     cur->nsDef = NULL;
     cur->content = NULL;
-#ifndef WITHOUT_CORBA
+#ifndef XML_WITHOUT_CORBA
     cur->_private = NULL;
     cur->vepv = NULL;
 #endif
@@ -1502,7 +1502,7 @@
 	ret->content = xmlStrdup(node->content);
     else
 	ret->content = NULL;
-#ifndef WITHOUT_CORBA
+#ifndef XML_WITHOUT_CORBA
     ret->_private = NULL;
     ret->vepv = NULL;
 #endif
@@ -2776,9 +2776,7 @@
 	if (output == NULL) return(-1);
 #ifdef HAVE_ZLIB_H
     }
-#endif
 
-#ifdef HAVE_ZLIB_H
     if (zoutput != NULL) {
         ret = gzwrite(zoutput, buf->content, sizeof(CHAR) * buf->use);
 	gzclose(zoutput);
diff --git a/xml-error.h b/xml-error.h
index 6d886d2..9308af2 100644
--- a/xml-error.h
+++ b/xml-error.h
@@ -3,6 +3,6 @@
 
 #include "parser.h"
 
-void xmlParserError(xmlParserCtxtPtr ctxt, const char *msg, ...);
-void xmlParserWarning(xmlParserCtxtPtr ctxt, const char *msg, ...);
+void xmlParserError(void *ctx, const char *msg, ...);
+void xmlParserWarning(void *ctx, const char *msg, ...);
 #endif