Fixed a really nasty problem raised by a DocBook XSLT transform provided

* entities.c parser.c tree.c include/libxml/entities.h: Fixed
  a really nasty problem raised by a DocBook XSLT transform
  provided by Sebastian Bergmann
Daniel
diff --git a/ChangeLog b/ChangeLog
index 5d19501..e9c187e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Mon Dec 30 00:59:07 CET 2002 Daniel Veillard <daniel@veillard.com>
+
+	* entities.c parser.c tree.c include/libxml/entities.h: Fixed
+	  a really nasty problem raised by a DocBook XSLT transform
+	  provided by Sebastian Bergmann
+
 Sun Dec 29 12:13:18 CET 2002 Daniel Veillard <daniel@veillard.com>
 
 	* xmlreader.c python/tests/reader.py: fixed a bug pointed out
diff --git a/entities.c b/entities.c
index dbdf9eb..8f3dfd8 100644
--- a/entities.c
+++ b/entities.c
@@ -48,7 +48,7 @@
 static void xmlFreeEntity(xmlEntityPtr entity) {
     if (entity == NULL) return;
 
-    if ((entity->children) &&
+    if ((entity->children) && (entity->owner == 1) &&
 	(entity == (xmlEntityPtr) entity->children->parent))
 	xmlFreeNodeList(entity->children);
     if (entity->name != NULL)
@@ -127,6 +127,7 @@
     ret->URI = NULL; /* to be computed by the layer knowing
 			the defining entity */
     ret->orig = NULL;
+    ret->owner = 0;
 
     if (xmlHashAddEntry(table, name, ret)) {
 	/*
@@ -847,6 +848,20 @@
 }
 
 /**
+ * xmlFreeEntityWrapper:
+ * @entity:  An entity
+ * @name:  its name
+ *
+ * Deallocate the memory used by an entities in the hash table.
+ */
+static void
+xmlFreeEntityWrapper(xmlEntityPtr entity,
+	               const xmlChar *name ATTRIBUTE_UNUSED) {
+    if (entity != NULL)
+	xmlFreeEntity(entity);
+}
+
+/**
  * xmlFreeEntitiesTable:
  * @table:  An entity table
  *
@@ -854,7 +869,7 @@
  */
 void
 xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
-    xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntity);
+    xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper);
 }
 
 /**
diff --git a/include/libxml/entities.h b/include/libxml/entities.h
index 480009c..ea7f202 100644
--- a/include/libxml/entities.h
+++ b/include/libxml/entities.h
@@ -52,6 +52,7 @@
 
     struct _xmlEntity     *nexte;	/* unused */
     const xmlChar           *URI;	/* the full URI as computed */
+    int                    owner;	/* does the entity own the childrens */
 };
 
 /*
diff --git a/parser.c b/parser.c
index 578a73c..79b46c0 100644
--- a/parser.c
+++ b/parser.c
@@ -5474,6 +5474,7 @@
 			    (ent->children == NULL)) {
 			    ent->children = list;
 			    ent->last = list;
+			    ent->owner = 1;
 			    list->parent = (xmlNodePtr) ent;
 			} else {
 			    xmlFreeNodeList(list);
@@ -5539,7 +5540,9 @@
 				    (list->next == NULL)) {
 				    list->parent = (xmlNodePtr) ent;
 				    list = NULL;
+				    ent->owner = 1;
 				} else {
+				    ent->owner = 0;
 				    while (list != NULL) {
 					list->parent = (xmlNodePtr) ctxt->node;
 					list->doc = ctxt->myDoc;
@@ -5552,6 +5555,7 @@
 				      xmlAddEntityReference(ent, list, NULL);
 				}
 			    } else {
+				ent->owner = 1;
 				while (list != NULL) {
 				    list->parent = (xmlNodePtr) ent;
 				    if (list->next == NULL)
diff --git a/tree.c b/tree.c
index 807189e..67b7d16 100644
--- a/tree.c
+++ b/tree.c
@@ -837,6 +837,7 @@
 
 			    ent->children = xmlStringGetNodeList(doc,
 				    (const xmlChar*)node->content);
+			    ent->owner = 1;
 			    temp = ent->children;
 			    while (temp) {
 				temp->parent = (xmlNodePtr)ent;