Blame entities.c

Packit 423ecb
/*
Packit 423ecb
 * entities.c : implementation for the XML entities handling
Packit 423ecb
 *
Packit 423ecb
 * See Copyright for the status of this software.
Packit 423ecb
 *
Packit 423ecb
 * daniel@veillard.com
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
/* To avoid EBCDIC trouble when parsing on zOS */
Packit 423ecb
#if defined(__MVS__)
Packit 423ecb
#pragma convert("ISO8859-1")
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#define IN_LIBXML
Packit 423ecb
#include "libxml.h"
Packit 423ecb
Packit 423ecb
#include <string.h>
Packit 423ecb
#ifdef HAVE_STDLIB_H
Packit 423ecb
#include <stdlib.h>
Packit 423ecb
#endif
Packit 423ecb
#include <libxml/xmlmemory.h>
Packit 423ecb
#include <libxml/hash.h>
Packit 423ecb
#include <libxml/entities.h>
Packit 423ecb
#include <libxml/parser.h>
Packit 423ecb
#include <libxml/parserInternals.h>
Packit 423ecb
#include <libxml/xmlerror.h>
Packit 423ecb
#include <libxml/globals.h>
Packit 423ecb
#include <libxml/dict.h>
Packit 423ecb
Packit 423ecb
#include "save.h"
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * The XML predefined entities.
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
static xmlEntity xmlEntityLt = {
Packit 423ecb
    NULL, XML_ENTITY_DECL, BAD_CAST "lt",
Packit 423ecb
    NULL, NULL, NULL, NULL, NULL, NULL,
Packit 423ecb
    BAD_CAST "<", BAD_CAST "<", 1,
Packit 423ecb
    XML_INTERNAL_PREDEFINED_ENTITY,
Packit 423ecb
    NULL, NULL, NULL, NULL, 0, 1
Packit 423ecb
};
Packit 423ecb
static xmlEntity xmlEntityGt = {
Packit 423ecb
    NULL, XML_ENTITY_DECL, BAD_CAST "gt",
Packit 423ecb
    NULL, NULL, NULL, NULL, NULL, NULL,
Packit 423ecb
    BAD_CAST ">", BAD_CAST ">", 1,
Packit 423ecb
    XML_INTERNAL_PREDEFINED_ENTITY,
Packit 423ecb
    NULL, NULL, NULL, NULL, 0, 1
Packit 423ecb
};
Packit 423ecb
static xmlEntity xmlEntityAmp = {
Packit 423ecb
    NULL, XML_ENTITY_DECL, BAD_CAST "amp",
Packit 423ecb
    NULL, NULL, NULL, NULL, NULL, NULL,
Packit 423ecb
    BAD_CAST "&", BAD_CAST "&", 1,
Packit 423ecb
    XML_INTERNAL_PREDEFINED_ENTITY,
Packit 423ecb
    NULL, NULL, NULL, NULL, 0, 1
Packit 423ecb
};
Packit 423ecb
static xmlEntity xmlEntityQuot = {
Packit 423ecb
    NULL, XML_ENTITY_DECL, BAD_CAST "quot",
Packit 423ecb
    NULL, NULL, NULL, NULL, NULL, NULL,
Packit 423ecb
    BAD_CAST "\"", BAD_CAST "\"", 1,
Packit 423ecb
    XML_INTERNAL_PREDEFINED_ENTITY,
Packit 423ecb
    NULL, NULL, NULL, NULL, 0, 1
Packit 423ecb
};
Packit 423ecb
static xmlEntity xmlEntityApos = {
Packit 423ecb
    NULL, XML_ENTITY_DECL, BAD_CAST "apos",
Packit 423ecb
    NULL, NULL, NULL, NULL, NULL, NULL,
Packit 423ecb
    BAD_CAST "'", BAD_CAST "'", 1,
Packit 423ecb
    XML_INTERNAL_PREDEFINED_ENTITY,
Packit 423ecb
    NULL, NULL, NULL, NULL, 0, 1
Packit 423ecb
};
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlEntitiesErrMemory:
Packit 423ecb
 * @extra:  extra informations
Packit 423ecb
 *
Packit 423ecb
 * Handle an out of memory condition
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlEntitiesErrMemory(const char *extra)
Packit 423ecb
{
Packit 423ecb
    __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlEntitiesErr:
Packit 423ecb
 * @code:  the error code
Packit 423ecb
 * @msg:  the message
Packit 423ecb
 *
Packit 423ecb
 * Handle an out of memory condition
Packit 423ecb
 */
Packit 423ecb
static void LIBXML_ATTR_FORMAT(2,0)
Packit 423ecb
xmlEntitiesErr(xmlParserErrors code, const char *msg)
Packit 423ecb
{
Packit 423ecb
    __xmlSimpleError(XML_FROM_TREE, code, NULL, msg, NULL);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlFreeEntity : clean-up an entity record.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlFreeEntity(xmlEntityPtr entity)
Packit 423ecb
{
Packit 423ecb
    xmlDictPtr dict = NULL;
Packit 423ecb
Packit 423ecb
    if (entity == NULL)
Packit 423ecb
        return;
Packit 423ecb
Packit 423ecb
    if (entity->doc != NULL)
Packit 423ecb
        dict = entity->doc->dict;
Packit 423ecb
Packit 423ecb
Packit 423ecb
    if ((entity->children) && (entity->owner == 1) &&
Packit 423ecb
        (entity == (xmlEntityPtr) entity->children->parent))
Packit 423ecb
        xmlFreeNodeList(entity->children);
Packit 423ecb
    if (dict != NULL) {
Packit 423ecb
        if ((entity->name != NULL) && (!xmlDictOwns(dict, entity->name)))
Packit 423ecb
            xmlFree((char *) entity->name);
Packit 423ecb
        if ((entity->ExternalID != NULL) &&
Packit 423ecb
	    (!xmlDictOwns(dict, entity->ExternalID)))
Packit 423ecb
            xmlFree((char *) entity->ExternalID);
Packit 423ecb
        if ((entity->SystemID != NULL) &&
Packit 423ecb
	    (!xmlDictOwns(dict, entity->SystemID)))
Packit 423ecb
            xmlFree((char *) entity->SystemID);
Packit 423ecb
        if ((entity->URI != NULL) && (!xmlDictOwns(dict, entity->URI)))
Packit 423ecb
            xmlFree((char *) entity->URI);
Packit 423ecb
        if ((entity->content != NULL)
Packit 423ecb
            && (!xmlDictOwns(dict, entity->content)))
Packit 423ecb
            xmlFree((char *) entity->content);
Packit 423ecb
        if ((entity->orig != NULL) && (!xmlDictOwns(dict, entity->orig)))
Packit 423ecb
            xmlFree((char *) entity->orig);
Packit 423ecb
    } else {
Packit 423ecb
        if (entity->name != NULL)
Packit 423ecb
            xmlFree((char *) entity->name);
Packit 423ecb
        if (entity->ExternalID != NULL)
Packit 423ecb
            xmlFree((char *) entity->ExternalID);
Packit 423ecb
        if (entity->SystemID != NULL)
Packit 423ecb
            xmlFree((char *) entity->SystemID);
Packit 423ecb
        if (entity->URI != NULL)
Packit 423ecb
            xmlFree((char *) entity->URI);
Packit 423ecb
        if (entity->content != NULL)
Packit 423ecb
            xmlFree((char *) entity->content);
Packit 423ecb
        if (entity->orig != NULL)
Packit 423ecb
            xmlFree((char *) entity->orig);
Packit 423ecb
    }
Packit 423ecb
    xmlFree(entity);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlCreateEntity:
Packit 423ecb
 *
Packit 423ecb
 * internal routine doing the entity node strutures allocations
Packit 423ecb
 */
Packit 423ecb
static xmlEntityPtr
Packit 423ecb
xmlCreateEntity(xmlDictPtr dict, const xmlChar *name, int type,
Packit 423ecb
	        const xmlChar *ExternalID, const xmlChar *SystemID,
Packit 423ecb
	        const xmlChar *content) {
Packit 423ecb
    xmlEntityPtr ret;
Packit 423ecb
Packit 423ecb
    ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
Packit 423ecb
    if (ret == NULL) {
Packit 423ecb
        xmlEntitiesErrMemory("xmlCreateEntity: malloc failed");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    memset(ret, 0, sizeof(xmlEntity));
Packit 423ecb
    ret->type = XML_ENTITY_DECL;
Packit 423ecb
    ret->checked = 0;
Packit 5696c8
    ret->guard = XML_ENTITY_NOT_BEING_CHECKED;
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * fill the structure.
Packit 423ecb
     */
Packit 423ecb
    ret->etype = (xmlEntityType) type;
Packit 423ecb
    if (dict == NULL) {
Packit 423ecb
	ret->name = xmlStrdup(name);
Packit 423ecb
	if (ExternalID != NULL)
Packit 423ecb
	    ret->ExternalID = xmlStrdup(ExternalID);
Packit 423ecb
	if (SystemID != NULL)
Packit 423ecb
	    ret->SystemID = xmlStrdup(SystemID);
Packit 423ecb
    } else {
Packit 423ecb
        ret->name = xmlDictLookup(dict, name, -1);
Packit 423ecb
	if (ExternalID != NULL)
Packit 423ecb
	    ret->ExternalID = xmlDictLookup(dict, ExternalID, -1);
Packit 423ecb
	if (SystemID != NULL)
Packit 423ecb
	    ret->SystemID = xmlDictLookup(dict, SystemID, -1);
Packit 423ecb
    }
Packit 423ecb
    if (content != NULL) {
Packit 423ecb
        ret->length = xmlStrlen(content);
Packit 423ecb
	if ((dict != NULL) && (ret->length < 5))
Packit 423ecb
	    ret->content = (xmlChar *)
Packit 423ecb
	                   xmlDictLookup(dict, content, ret->length);
Packit 423ecb
	else
Packit 423ecb
	    ret->content = xmlStrndup(content, ret->length);
Packit 423ecb
     } else {
Packit 423ecb
        ret->length = 0;
Packit 423ecb
        ret->content = NULL;
Packit 423ecb
    }
Packit 423ecb
    ret->URI = NULL; /* to be computed by the layer knowing
Packit 423ecb
			the defining entity */
Packit 423ecb
    ret->orig = NULL;
Packit 423ecb
    ret->owner = 0;
Packit 423ecb
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * xmlAddEntity : register a new entity for an entities table.
Packit 423ecb
 */
Packit 423ecb
static xmlEntityPtr
Packit 423ecb
xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type,
Packit 423ecb
	  const xmlChar *ExternalID, const xmlChar *SystemID,
Packit 423ecb
	  const xmlChar *content) {
Packit 423ecb
    xmlDictPtr dict = NULL;
Packit 423ecb
    xmlEntitiesTablePtr table = NULL;
Packit 423ecb
    xmlEntityPtr ret;
Packit 423ecb
Packit 423ecb
    if (name == NULL)
Packit 423ecb
	return(NULL);
Packit 423ecb
    if (dtd == NULL)
Packit 423ecb
	return(NULL);
Packit 423ecb
    if (dtd->doc != NULL)
Packit 423ecb
        dict = dtd->doc->dict;
Packit 423ecb
Packit 423ecb
    switch (type) {
Packit 423ecb
        case XML_INTERNAL_GENERAL_ENTITY:
Packit 423ecb
        case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
Packit 423ecb
        case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Packit 423ecb
	    if (dtd->entities == NULL)
Packit 423ecb
		dtd->entities = xmlHashCreateDict(0, dict);
Packit 423ecb
	    table = dtd->entities;
Packit 423ecb
	    break;
Packit 423ecb
        case XML_INTERNAL_PARAMETER_ENTITY:
Packit 423ecb
        case XML_EXTERNAL_PARAMETER_ENTITY:
Packit 423ecb
	    if (dtd->pentities == NULL)
Packit 423ecb
		dtd->pentities = xmlHashCreateDict(0, dict);
Packit 423ecb
	    table = dtd->pentities;
Packit 423ecb
	    break;
Packit 423ecb
        case XML_INTERNAL_PREDEFINED_ENTITY:
Packit 423ecb
	    return(NULL);
Packit 423ecb
    }
Packit 423ecb
    if (table == NULL)
Packit 423ecb
	return(NULL);
Packit 423ecb
    ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);
Packit 423ecb
    if (ret == NULL)
Packit 423ecb
        return(NULL);
Packit 423ecb
    ret->doc = dtd->doc;
Packit 423ecb
Packit 423ecb
    if (xmlHashAddEntry(table, name, ret)) {
Packit 423ecb
	/*
Packit 423ecb
	 * entity was already defined at another level.
Packit 423ecb
	 */
Packit 423ecb
        xmlFreeEntity(ret);
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlGetPredefinedEntity:
Packit 423ecb
 * @name:  the entity name
Packit 423ecb
 *
Packit 423ecb
 * Check whether this name is an predefined entity.
Packit 423ecb
 *
Packit 423ecb
 * Returns NULL if not, otherwise the entity
Packit 423ecb
 */
Packit 423ecb
xmlEntityPtr
Packit 423ecb
xmlGetPredefinedEntity(const xmlChar *name) {
Packit 423ecb
    if (name == NULL) return(NULL);
Packit 423ecb
    switch (name[0]) {
Packit 423ecb
        case 'l':
Packit 423ecb
	    if (xmlStrEqual(name, BAD_CAST "lt"))
Packit 423ecb
	        return(&xmlEntityLt);
Packit 423ecb
	    break;
Packit 423ecb
        case 'g':
Packit 423ecb
	    if (xmlStrEqual(name, BAD_CAST "gt"))
Packit 423ecb
	        return(&xmlEntityGt);
Packit 423ecb
	    break;
Packit 423ecb
        case 'a':
Packit 423ecb
	    if (xmlStrEqual(name, BAD_CAST "amp"))
Packit 423ecb
	        return(&xmlEntityAmp);
Packit 423ecb
	    if (xmlStrEqual(name, BAD_CAST "apos"))
Packit 423ecb
	        return(&xmlEntityApos);
Packit 423ecb
	    break;
Packit 423ecb
        case 'q':
Packit 423ecb
	    if (xmlStrEqual(name, BAD_CAST "quot"))
Packit 423ecb
	        return(&xmlEntityQuot);
Packit 423ecb
	    break;
Packit 423ecb
	default:
Packit 423ecb
	    break;
Packit 423ecb
    }
Packit 423ecb
    return(NULL);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlAddDtdEntity:
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @name:  the entity name
Packit 423ecb
 * @type:  the entity type XML_xxx_yyy_ENTITY
Packit 423ecb
 * @ExternalID:  the entity external ID if available
Packit 423ecb
 * @SystemID:  the entity system ID if available
Packit 423ecb
 * @content:  the entity content
Packit 423ecb
 *
Packit 423ecb
 * Register a new entity for this document DTD external subset.
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to the entity or NULL in case of error
Packit 423ecb
 */
Packit 423ecb
xmlEntityPtr
Packit 423ecb
xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,
Packit 423ecb
	        const xmlChar *ExternalID, const xmlChar *SystemID,
Packit 423ecb
		const xmlChar *content) {
Packit 423ecb
    xmlEntityPtr ret;
Packit 423ecb
    xmlDtdPtr dtd;
Packit 423ecb
Packit 423ecb
    if (doc == NULL) {
Packit 423ecb
	xmlEntitiesErr(XML_DTD_NO_DOC,
Packit 423ecb
	        "xmlAddDtdEntity: document is NULL");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    if (doc->extSubset == NULL) {
Packit 423ecb
	xmlEntitiesErr(XML_DTD_NO_DTD,
Packit 423ecb
	        "xmlAddDtdEntity: document without external subset");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    dtd = doc->extSubset;
Packit 423ecb
    ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
Packit 423ecb
    if (ret == NULL) return(NULL);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Link it to the DTD
Packit 423ecb
     */
Packit 423ecb
    ret->parent = dtd;
Packit 423ecb
    ret->doc = dtd->doc;
Packit 423ecb
    if (dtd->last == NULL) {
Packit 423ecb
	dtd->children = dtd->last = (xmlNodePtr) ret;
Packit 423ecb
    } else {
Packit 423ecb
        dtd->last->next = (xmlNodePtr) ret;
Packit 423ecb
	ret->prev = dtd->last;
Packit 423ecb
	dtd->last = (xmlNodePtr) ret;
Packit 423ecb
    }
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlAddDocEntity:
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @name:  the entity name
Packit 423ecb
 * @type:  the entity type XML_xxx_yyy_ENTITY
Packit 423ecb
 * @ExternalID:  the entity external ID if available
Packit 423ecb
 * @SystemID:  the entity system ID if available
Packit 423ecb
 * @content:  the entity content
Packit 423ecb
 *
Packit 423ecb
 * Register a new entity for this document.
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to the entity or NULL in case of error
Packit 423ecb
 */
Packit 423ecb
xmlEntityPtr
Packit 423ecb
xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,
Packit 423ecb
	        const xmlChar *ExternalID, const xmlChar *SystemID,
Packit 423ecb
	        const xmlChar *content) {
Packit 423ecb
    xmlEntityPtr ret;
Packit 423ecb
    xmlDtdPtr dtd;
Packit 423ecb
Packit 423ecb
    if (doc == NULL) {
Packit 423ecb
	xmlEntitiesErr(XML_DTD_NO_DOC,
Packit 423ecb
	        "xmlAddDocEntity: document is NULL");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    if (doc->intSubset == NULL) {
Packit 423ecb
	xmlEntitiesErr(XML_DTD_NO_DTD,
Packit 423ecb
	        "xmlAddDocEntity: document without internal subset");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    dtd = doc->intSubset;
Packit 423ecb
    ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);
Packit 423ecb
    if (ret == NULL) return(NULL);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Link it to the DTD
Packit 423ecb
     */
Packit 423ecb
    ret->parent = dtd;
Packit 423ecb
    ret->doc = dtd->doc;
Packit 423ecb
    if (dtd->last == NULL) {
Packit 423ecb
	dtd->children = dtd->last = (xmlNodePtr) ret;
Packit 423ecb
    } else {
Packit 423ecb
	dtd->last->next = (xmlNodePtr) ret;
Packit 423ecb
	ret->prev = dtd->last;
Packit 423ecb
	dtd->last = (xmlNodePtr) ret;
Packit 423ecb
    }
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNewEntity:
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 * @name:  the entity name
Packit 423ecb
 * @type:  the entity type XML_xxx_yyy_ENTITY
Packit 423ecb
 * @ExternalID:  the entity external ID if available
Packit 423ecb
 * @SystemID:  the entity system ID if available
Packit 423ecb
 * @content:  the entity content
Packit 423ecb
 *
Packit 423ecb
 * Create a new entity, this differs from xmlAddDocEntity() that if
Packit 423ecb
 * the document is NULL or has no internal subset defined, then an
Packit 423ecb
 * unlinked entity structure will be returned, it is then the responsability
Packit 423ecb
 * of the caller to link it to the document later or free it when not needed
Packit 423ecb
 * anymore.
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to the entity or NULL in case of error
Packit 423ecb
 */
Packit 423ecb
xmlEntityPtr
Packit 423ecb
xmlNewEntity(xmlDocPtr doc, const xmlChar *name, int type,
Packit 423ecb
	     const xmlChar *ExternalID, const xmlChar *SystemID,
Packit 423ecb
	     const xmlChar *content) {
Packit 423ecb
    xmlEntityPtr ret;
Packit 423ecb
    xmlDictPtr dict;
Packit 423ecb
Packit 423ecb
    if ((doc != NULL) && (doc->intSubset != NULL)) {
Packit 423ecb
	return(xmlAddDocEntity(doc, name, type, ExternalID, SystemID, content));
Packit 423ecb
    }
Packit 423ecb
    if (doc != NULL)
Packit 423ecb
        dict = doc->dict;
Packit 423ecb
    else
Packit 423ecb
        dict = NULL;
Packit 423ecb
    ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);
Packit 423ecb
    if (ret == NULL)
Packit 423ecb
        return(NULL);
Packit 423ecb
    ret->doc = doc;
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlGetEntityFromTable:
Packit 423ecb
 * @table:  an entity table
Packit 423ecb
 * @name:  the entity name
Packit 423ecb
 * @parameter:  look for parameter entities
Packit 423ecb
 *
Packit 423ecb
 * Do an entity lookup in the table.
Packit 423ecb
 * returns the corresponding parameter entity, if found.
Packit 423ecb
 *
Packit 423ecb
 * Returns A pointer to the entity structure or NULL if not found.
Packit 423ecb
 */
Packit 423ecb
static xmlEntityPtr
Packit 423ecb
xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) {
Packit 423ecb
    return((xmlEntityPtr) xmlHashLookup(table, name));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlGetParameterEntity:
Packit 423ecb
 * @doc:  the document referencing the entity
Packit 423ecb
 * @name:  the entity name
Packit 423ecb
 *
Packit 423ecb
 * Do an entity lookup in the internal and external subsets and
Packit 423ecb
 * returns the corresponding parameter entity, if found.
Packit 423ecb
 *
Packit 423ecb
 * Returns A pointer to the entity structure or NULL if not found.
Packit 423ecb
 */
Packit 423ecb
xmlEntityPtr
Packit 423ecb
xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) {
Packit 423ecb
    xmlEntitiesTablePtr table;
Packit 423ecb
    xmlEntityPtr ret;
Packit 423ecb
Packit 423ecb
    if (doc == NULL)
Packit 423ecb
	return(NULL);
Packit 423ecb
    if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) {
Packit 423ecb
	table = (xmlEntitiesTablePtr) doc->intSubset->pentities;
Packit 423ecb
	ret = xmlGetEntityFromTable(table, name);
Packit 423ecb
	if (ret != NULL)
Packit 423ecb
	    return(ret);
Packit 423ecb
    }
Packit 423ecb
    if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) {
Packit 423ecb
	table = (xmlEntitiesTablePtr) doc->extSubset->pentities;
Packit 423ecb
	return(xmlGetEntityFromTable(table, name));
Packit 423ecb
    }
Packit 423ecb
    return(NULL);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlGetDtdEntity:
Packit 423ecb
 * @doc:  the document referencing the entity
Packit 423ecb
 * @name:  the entity name
Packit 423ecb
 *
Packit 423ecb
 * Do an entity lookup in the DTD entity hash table and
Packit 423ecb
 * returns the corresponding entity, if found.
Packit 423ecb
 * Note: the first argument is the document node, not the DTD node.
Packit 423ecb
 *
Packit 423ecb
 * Returns A pointer to the entity structure or NULL if not found.
Packit 423ecb
 */
Packit 423ecb
xmlEntityPtr
Packit 423ecb
xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
Packit 423ecb
    xmlEntitiesTablePtr table;
Packit 423ecb
Packit 423ecb
    if (doc == NULL)
Packit 423ecb
	return(NULL);
Packit 423ecb
    if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
Packit 423ecb
	table = (xmlEntitiesTablePtr) doc->extSubset->entities;
Packit 423ecb
	return(xmlGetEntityFromTable(table, name));
Packit 423ecb
    }
Packit 423ecb
    return(NULL);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlGetDocEntity:
Packit 423ecb
 * @doc:  the document referencing the entity
Packit 423ecb
 * @name:  the entity name
Packit 423ecb
 *
Packit 423ecb
 * Do an entity lookup in the document entity hash table and
Packit 423ecb
 * returns the corresponding entity, otherwise a lookup is done
Packit 423ecb
 * in the predefined entities too.
Packit 423ecb
 *
Packit 423ecb
 * Returns A pointer to the entity structure or NULL if not found.
Packit 423ecb
 */
Packit 423ecb
xmlEntityPtr
Packit 423ecb
xmlGetDocEntity(const xmlDoc *doc, const xmlChar *name) {
Packit 423ecb
    xmlEntityPtr cur;
Packit 423ecb
    xmlEntitiesTablePtr table;
Packit 423ecb
Packit 423ecb
    if (doc != NULL) {
Packit 423ecb
	if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
Packit 423ecb
	    table = (xmlEntitiesTablePtr) doc->intSubset->entities;
Packit 423ecb
	    cur = xmlGetEntityFromTable(table, name);
Packit 423ecb
	    if (cur != NULL)
Packit 423ecb
		return(cur);
Packit 423ecb
	}
Packit 423ecb
	if (doc->standalone != 1) {
Packit 423ecb
	    if ((doc->extSubset != NULL) &&
Packit 423ecb
		(doc->extSubset->entities != NULL)) {
Packit 423ecb
		table = (xmlEntitiesTablePtr) doc->extSubset->entities;
Packit 423ecb
		cur = xmlGetEntityFromTable(table, name);
Packit 423ecb
		if (cur != NULL)
Packit 423ecb
		    return(cur);
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    return(xmlGetPredefinedEntity(name));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * Macro used to grow the current buffer.
Packit 423ecb
 */
Packit 423ecb
#define growBufferReentrant() {						\
Packit 423ecb
    xmlChar *tmp;                                                       \
Packit 423ecb
    size_t new_size = buffer_size * 2;                                  \
Packit 423ecb
    if (new_size < buffer_size) goto mem_error;                         \
Packit 423ecb
    tmp = (xmlChar *) xmlRealloc(buffer, new_size);	                \
Packit 423ecb
    if (tmp == NULL) goto mem_error;                                    \
Packit 423ecb
    buffer = tmp;							\
Packit 423ecb
    buffer_size = new_size;						\
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlEncodeEntitiesInternal:
Packit 423ecb
 * @doc:  the document containing the string
Packit 423ecb
 * @input:  A string to convert to XML.
Packit 423ecb
 * @attr: are we handling an atrbute value
Packit 423ecb
 *
Packit 423ecb
 * Do a global encoding of a string, replacing the predefined entities
Packit 423ecb
 * and non ASCII values with their entities and CharRef counterparts.
Packit 423ecb
 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
Packit 423ecb
 * must be deallocated.
Packit 423ecb
 *
Packit 423ecb
 * Returns A newly allocated string with the substitution done.
Packit 423ecb
 */
Packit 423ecb
static xmlChar *
Packit 423ecb
xmlEncodeEntitiesInternal(xmlDocPtr doc, const xmlChar *input, int attr) {
Packit 423ecb
    const xmlChar *cur = input;
Packit 423ecb
    xmlChar *buffer = NULL;
Packit 423ecb
    xmlChar *out = NULL;
Packit 423ecb
    size_t buffer_size = 0;
Packit 423ecb
    int html = 0;
Packit 423ecb
Packit 423ecb
    if (input == NULL) return(NULL);
Packit 423ecb
    if (doc != NULL)
Packit 423ecb
        html = (doc->type == XML_HTML_DOCUMENT_NODE);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * allocate an translation buffer.
Packit 423ecb
     */
Packit 423ecb
    buffer_size = 1000;
Packit 423ecb
    buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
Packit 423ecb
    if (buffer == NULL) {
Packit 423ecb
        xmlEntitiesErrMemory("xmlEncodeEntities: malloc failed");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    out = buffer;
Packit 423ecb
Packit 423ecb
    while (*cur != '\0') {
Packit 423ecb
        size_t indx = out - buffer;
Packit 423ecb
        if (indx + 100 > buffer_size) {
Packit 423ecb
Packit 423ecb
	    growBufferReentrant();
Packit 423ecb
	    out = &buffer[indx];
Packit 423ecb
	}
Packit 423ecb
Packit 423ecb
	/*
Packit 423ecb
	 * By default one have to encode at least '<', '>', '"' and '&' !
Packit 423ecb
	 */
Packit 423ecb
	if (*cur == '<') {
Packit 423ecb
	    const xmlChar *end;
Packit 423ecb
Packit 423ecb
	    /*
Packit 423ecb
	     * Special handling of server side include in HTML attributes
Packit 423ecb
	     */
Packit 423ecb
	    if (html && attr &&
Packit 423ecb
	        (cur[1] == '!') && (cur[2] == '-') && (cur[3] == '-') &&
Packit 423ecb
	        ((end = xmlStrstr(cur, BAD_CAST "-->")) != NULL)) {
Packit 423ecb
	        while (cur != end) {
Packit 423ecb
		    *out++ = *cur++;
Packit 423ecb
		    indx = out - buffer;
Packit 423ecb
		    if (indx + 100 > buffer_size) {
Packit 423ecb
			growBufferReentrant();
Packit 423ecb
			out = &buffer[indx];
Packit 423ecb
		    }
Packit 423ecb
		}
Packit 423ecb
		*out++ = *cur++;
Packit 423ecb
		*out++ = *cur++;
Packit 423ecb
		*out++ = *cur++;
Packit 423ecb
		continue;
Packit 423ecb
	    }
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = 'l';
Packit 423ecb
	    *out++ = 't';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	} else if (*cur == '>') {
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = 'g';
Packit 423ecb
	    *out++ = 't';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	} else if (*cur == '&') {
Packit 423ecb
	    /*
Packit 423ecb
	     * Special handling of &{...} construct from HTML 4, see
Packit 423ecb
	     * http://www.w3.org/TR/html401/appendix/notes.html#h-B.7.1
Packit 423ecb
	     */
Packit 423ecb
	    if (html && attr && (cur[1] == '{') &&
Packit 423ecb
	        (strchr((const char *) cur, '}'))) {
Packit 423ecb
	        while (*cur != '}') {
Packit 423ecb
		    *out++ = *cur++;
Packit 423ecb
		    indx = out - buffer;
Packit 423ecb
		    if (indx + 100 > buffer_size) {
Packit 423ecb
			growBufferReentrant();
Packit 423ecb
			out = &buffer[indx];
Packit 423ecb
		    }
Packit 423ecb
		}
Packit 423ecb
		*out++ = *cur++;
Packit 423ecb
		continue;
Packit 423ecb
	    }
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = 'a';
Packit 423ecb
	    *out++ = 'm';
Packit 423ecb
	    *out++ = 'p';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	} else if (((*cur >= 0x20) && (*cur < 0x80)) ||
Packit 423ecb
	    (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) {
Packit 423ecb
	    /*
Packit 423ecb
	     * default case, just copy !
Packit 423ecb
	     */
Packit 423ecb
	    *out++ = *cur;
Packit 423ecb
	} else if (*cur >= 0x80) {
Packit 423ecb
	    if (((doc != NULL) && (doc->encoding != NULL)) || (html)) {
Packit 423ecb
		/*
Packit 423ecb
		 * Bjørn Reese <br@sseusa.com> provided the patch
Packit 423ecb
	        xmlChar xc;
Packit 423ecb
	        xc = (*cur & 0x3F) << 6;
Packit 423ecb
	        if (cur[1] != 0) {
Packit 423ecb
		    xc += *(++cur) & 0x3F;
Packit 423ecb
		    *out++ = xc;
Packit 423ecb
	        } else
Packit 423ecb
		 */
Packit 423ecb
		*out++ = *cur;
Packit 423ecb
	    } else {
Packit 423ecb
		/*
Packit 423ecb
		 * We assume we have UTF-8 input.
Packit 423ecb
		 */
Packit 423ecb
		char buf[11], *ptr;
Packit 423ecb
		int val = 0, l = 1;
Packit 423ecb
Packit 423ecb
		if (*cur < 0xC0) {
Packit 423ecb
		    xmlEntitiesErr(XML_CHECK_NOT_UTF8,
Packit 423ecb
			    "xmlEncodeEntities: input not UTF-8");
Packit 423ecb
		    if (doc != NULL)
Packit 423ecb
			doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Packit 423ecb
		    snprintf(buf, sizeof(buf), "&#%d;", *cur);
Packit 423ecb
		    buf[sizeof(buf) - 1] = 0;
Packit 423ecb
		    ptr = buf;
Packit 423ecb
		    while (*ptr != 0) *out++ = *ptr++;
Packit 423ecb
		    cur++;
Packit 423ecb
		    continue;
Packit 423ecb
		} else if (*cur < 0xE0) {
Packit 423ecb
                    val = (cur[0]) & 0x1F;
Packit 423ecb
		    val <<= 6;
Packit 423ecb
		    val |= (cur[1]) & 0x3F;
Packit 423ecb
		    l = 2;
Packit 423ecb
		} else if (*cur < 0xF0) {
Packit 423ecb
                    val = (cur[0]) & 0x0F;
Packit 423ecb
		    val <<= 6;
Packit 423ecb
		    val |= (cur[1]) & 0x3F;
Packit 423ecb
		    val <<= 6;
Packit 423ecb
		    val |= (cur[2]) & 0x3F;
Packit 423ecb
		    l = 3;
Packit 423ecb
		} else if (*cur < 0xF8) {
Packit 423ecb
                    val = (cur[0]) & 0x07;
Packit 423ecb
		    val <<= 6;
Packit 423ecb
		    val |= (cur[1]) & 0x3F;
Packit 423ecb
		    val <<= 6;
Packit 423ecb
		    val |= (cur[2]) & 0x3F;
Packit 423ecb
		    val <<= 6;
Packit 423ecb
		    val |= (cur[3]) & 0x3F;
Packit 423ecb
		    l = 4;
Packit 423ecb
		}
Packit 423ecb
		if ((l == 1) || (!IS_CHAR(val))) {
Packit 423ecb
		    xmlEntitiesErr(XML_ERR_INVALID_CHAR,
Packit 423ecb
			"xmlEncodeEntities: char out of range\n");
Packit 423ecb
		    if (doc != NULL)
Packit 423ecb
			doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
Packit 423ecb
		    snprintf(buf, sizeof(buf), "&#%d;", *cur);
Packit 423ecb
		    buf[sizeof(buf) - 1] = 0;
Packit 423ecb
		    ptr = buf;
Packit 423ecb
		    while (*ptr != 0) *out++ = *ptr++;
Packit 423ecb
		    cur++;
Packit 423ecb
		    continue;
Packit 423ecb
		}
Packit 423ecb
		/*
Packit 423ecb
		 * We could do multiple things here. Just save as a char ref
Packit 423ecb
		 */
Packit 423ecb
		snprintf(buf, sizeof(buf), "&#x%X;", val);
Packit 423ecb
		buf[sizeof(buf) - 1] = 0;
Packit 423ecb
		ptr = buf;
Packit 423ecb
		while (*ptr != 0) *out++ = *ptr++;
Packit 423ecb
		cur += l;
Packit 423ecb
		continue;
Packit 423ecb
	    }
Packit 423ecb
	} else if (IS_BYTE_CHAR(*cur)) {
Packit 423ecb
	    char buf[11], *ptr;
Packit 423ecb
Packit 423ecb
	    snprintf(buf, sizeof(buf), "&#%d;", *cur);
Packit 423ecb
	    buf[sizeof(buf) - 1] = 0;
Packit 423ecb
            ptr = buf;
Packit 423ecb
	    while (*ptr != 0) *out++ = *ptr++;
Packit 423ecb
	}
Packit 423ecb
	cur++;
Packit 423ecb
    }
Packit 423ecb
    *out = 0;
Packit 423ecb
    return(buffer);
Packit 423ecb
Packit 423ecb
mem_error:
Packit 423ecb
    xmlEntitiesErrMemory("xmlEncodeEntities: realloc failed");
Packit 423ecb
    xmlFree(buffer);
Packit 423ecb
    return(NULL);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlEncodeAttributeEntities:
Packit 423ecb
 * @doc:  the document containing the string
Packit 423ecb
 * @input:  A string to convert to XML.
Packit 423ecb
 *
Packit 423ecb
 * Do a global encoding of a string, replacing the predefined entities
Packit 423ecb
 * and non ASCII values with their entities and CharRef counterparts for
Packit 423ecb
 * attribute values.
Packit 423ecb
 *
Packit 423ecb
 * Returns A newly allocated string with the substitution done.
Packit 423ecb
 */
Packit 423ecb
xmlChar *
Packit 423ecb
xmlEncodeAttributeEntities(xmlDocPtr doc, const xmlChar *input) {
Packit 423ecb
    return xmlEncodeEntitiesInternal(doc, input, 1);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlEncodeEntitiesReentrant:
Packit 423ecb
 * @doc:  the document containing the string
Packit 423ecb
 * @input:  A string to convert to XML.
Packit 423ecb
 *
Packit 423ecb
 * Do a global encoding of a string, replacing the predefined entities
Packit 423ecb
 * and non ASCII values with their entities and CharRef counterparts.
Packit 423ecb
 * Contrary to xmlEncodeEntities, this routine is reentrant, and result
Packit 423ecb
 * must be deallocated.
Packit 423ecb
 *
Packit 423ecb
 * Returns A newly allocated string with the substitution done.
Packit 423ecb
 */
Packit 423ecb
xmlChar *
Packit 423ecb
xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
Packit 423ecb
    return xmlEncodeEntitiesInternal(doc, input, 0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlEncodeSpecialChars:
Packit 423ecb
 * @doc:  the document containing the string
Packit 423ecb
 * @input:  A string to convert to XML.
Packit 423ecb
 *
Packit 423ecb
 * Do a global encoding of a string, replacing the predefined entities
Packit 423ecb
 * this routine is reentrant, and result must be deallocated.
Packit 423ecb
 *
Packit 423ecb
 * Returns A newly allocated string with the substitution done.
Packit 423ecb
 */
Packit 423ecb
xmlChar *
Packit 423ecb
xmlEncodeSpecialChars(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlChar *input) {
Packit 423ecb
    const xmlChar *cur = input;
Packit 423ecb
    xmlChar *buffer = NULL;
Packit 423ecb
    xmlChar *out = NULL;
Packit 423ecb
    size_t buffer_size = 0;
Packit 423ecb
    if (input == NULL) return(NULL);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * allocate an translation buffer.
Packit 423ecb
     */
Packit 423ecb
    buffer_size = 1000;
Packit 423ecb
    buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
Packit 423ecb
    if (buffer == NULL) {
Packit 423ecb
        xmlEntitiesErrMemory("xmlEncodeSpecialChars: malloc failed");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    out = buffer;
Packit 423ecb
Packit 423ecb
    while (*cur != '\0') {
Packit 423ecb
        size_t indx = out - buffer;
Packit 423ecb
        if (indx + 10 > buffer_size) {
Packit 423ecb
Packit 423ecb
	    growBufferReentrant();
Packit 423ecb
	    out = &buffer[indx];
Packit 423ecb
	}
Packit 423ecb
Packit 423ecb
	/*
Packit 423ecb
	 * By default one have to encode at least '<', '>', '"' and '&' !
Packit 423ecb
	 */
Packit 423ecb
	if (*cur == '<') {
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = 'l';
Packit 423ecb
	    *out++ = 't';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	} else if (*cur == '>') {
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = 'g';
Packit 423ecb
	    *out++ = 't';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	} else if (*cur == '&') {
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = 'a';
Packit 423ecb
	    *out++ = 'm';
Packit 423ecb
	    *out++ = 'p';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	} else if (*cur == '"') {
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = 'q';
Packit 423ecb
	    *out++ = 'u';
Packit 423ecb
	    *out++ = 'o';
Packit 423ecb
	    *out++ = 't';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	} else if (*cur == '\r') {
Packit 423ecb
	    *out++ = '&';
Packit 423ecb
	    *out++ = '#';
Packit 423ecb
	    *out++ = '1';
Packit 423ecb
	    *out++ = '3';
Packit 423ecb
	    *out++ = ';';
Packit 423ecb
	} else {
Packit 423ecb
	    /*
Packit 423ecb
	     * Works because on UTF-8, all extended sequences cannot
Packit 423ecb
	     * result in bytes in the ASCII range.
Packit 423ecb
	     */
Packit 423ecb
	    *out++ = *cur;
Packit 423ecb
	}
Packit 423ecb
	cur++;
Packit 423ecb
    }
Packit 423ecb
    *out = 0;
Packit 423ecb
    return(buffer);
Packit 423ecb
Packit 423ecb
mem_error:
Packit 423ecb
    xmlEntitiesErrMemory("xmlEncodeSpecialChars: realloc failed");
Packit 423ecb
    xmlFree(buffer);
Packit 423ecb
    return(NULL);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCreateEntitiesTable:
Packit 423ecb
 *
Packit 423ecb
 * create and initialize an empty entities hash table.
Packit 423ecb
 * This really doesn't make sense and should be deprecated
Packit 423ecb
 *
Packit 423ecb
 * Returns the xmlEntitiesTablePtr just created or NULL in case of error.
Packit 423ecb
 */
Packit 423ecb
xmlEntitiesTablePtr
Packit 423ecb
xmlCreateEntitiesTable(void) {
Packit 423ecb
    return((xmlEntitiesTablePtr) xmlHashCreate(0));
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlFreeEntityWrapper:
Packit 423ecb
 * @entity:  An entity
Packit 423ecb
 * @name:  its name
Packit 423ecb
 *
Packit 423ecb
 * Deallocate the memory used by an entities in the hash table.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlFreeEntityWrapper(xmlEntityPtr entity,
Packit 423ecb
	               const xmlChar *name ATTRIBUTE_UNUSED) {
Packit 423ecb
    if (entity != NULL)
Packit 423ecb
	xmlFreeEntity(entity);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlFreeEntitiesTable:
Packit 423ecb
 * @table:  An entity table
Packit 423ecb
 *
Packit 423ecb
 * Deallocate the memory used by an entities hash table.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
Packit 423ecb
    xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_TREE_ENABLED
Packit 423ecb
/**
Packit 423ecb
 * xmlCopyEntity:
Packit 423ecb
 * @ent:  An entity
Packit 423ecb
 *
Packit 423ecb
 * Build a copy of an entity
Packit 423ecb
 *
Packit 423ecb
 * Returns the new xmlEntitiesPtr or NULL in case of error.
Packit 423ecb
 */
Packit 423ecb
static xmlEntityPtr
Packit 423ecb
xmlCopyEntity(xmlEntityPtr ent) {
Packit 423ecb
    xmlEntityPtr cur;
Packit 423ecb
Packit 423ecb
    cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));
Packit 423ecb
    if (cur == NULL) {
Packit 423ecb
        xmlEntitiesErrMemory("xmlCopyEntity:: malloc failed");
Packit 423ecb
	return(NULL);
Packit 423ecb
    }
Packit 423ecb
    memset(cur, 0, sizeof(xmlEntity));
Packit 423ecb
    cur->type = XML_ENTITY_DECL;
Packit 423ecb
Packit 423ecb
    cur->etype = ent->etype;
Packit 423ecb
    if (ent->name != NULL)
Packit 423ecb
	cur->name = xmlStrdup(ent->name);
Packit 423ecb
    if (ent->ExternalID != NULL)
Packit 423ecb
	cur->ExternalID = xmlStrdup(ent->ExternalID);
Packit 423ecb
    if (ent->SystemID != NULL)
Packit 423ecb
	cur->SystemID = xmlStrdup(ent->SystemID);
Packit 423ecb
    if (ent->content != NULL)
Packit 423ecb
	cur->content = xmlStrdup(ent->content);
Packit 423ecb
    if (ent->orig != NULL)
Packit 423ecb
	cur->orig = xmlStrdup(ent->orig);
Packit 423ecb
    if (ent->URI != NULL)
Packit 423ecb
	cur->URI = xmlStrdup(ent->URI);
Packit 5696c8
    cur->guard = 0;
Packit 423ecb
    return(cur);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCopyEntitiesTable:
Packit 423ecb
 * @table:  An entity table
Packit 423ecb
 *
Packit 423ecb
 * Build a copy of an entity table.
Packit 423ecb
 *
Packit 423ecb
 * Returns the new xmlEntitiesTablePtr or NULL in case of error.
Packit 423ecb
 */
Packit 423ecb
xmlEntitiesTablePtr
Packit 423ecb
xmlCopyEntitiesTable(xmlEntitiesTablePtr table) {
Packit 423ecb
    return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity));
Packit 423ecb
}
Packit 423ecb
#endif /* LIBXML_TREE_ENABLED */
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_OUTPUT_ENABLED
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDumpEntityContent:
Packit 423ecb
 * @buf:  An XML buffer.
Packit 423ecb
 * @content:  The entity content.
Packit 423ecb
 *
Packit 423ecb
 * This will dump the quoted string value, taking care of the special
Packit 423ecb
 * treatment required by %
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) {
Packit 423ecb
    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Packit 423ecb
    if (xmlStrchr(content, '%')) {
Packit 423ecb
        const xmlChar * base, *cur;
Packit 423ecb
Packit 423ecb
	xmlBufferCCat(buf, "\"");
Packit 423ecb
	base = cur = content;
Packit 423ecb
	while (*cur != 0) {
Packit 423ecb
	    if (*cur == '"') {
Packit 423ecb
		if (base != cur)
Packit 423ecb
		    xmlBufferAdd(buf, base, cur - base);
Packit 423ecb
		xmlBufferAdd(buf, BAD_CAST """, 6);
Packit 423ecb
		cur++;
Packit 423ecb
		base = cur;
Packit 423ecb
	    } else if (*cur == '%') {
Packit 423ecb
		if (base != cur)
Packit 423ecb
		    xmlBufferAdd(buf, base, cur - base);
Packit 423ecb
		xmlBufferAdd(buf, BAD_CAST "%", 6);
Packit 423ecb
		cur++;
Packit 423ecb
		base = cur;
Packit 423ecb
	    } else {
Packit 423ecb
		cur++;
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
	if (base != cur)
Packit 423ecb
	    xmlBufferAdd(buf, base, cur - base);
Packit 423ecb
	xmlBufferCCat(buf, "\"");
Packit 423ecb
    } else {
Packit 423ecb
        xmlBufferWriteQuotedString(buf, content);
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDumpEntityDecl:
Packit 423ecb
 * @buf:  An XML buffer.
Packit 423ecb
 * @ent:  An entity table
Packit 423ecb
 *
Packit 423ecb
 * This will dump the content of the entity table as an XML DTD definition
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {
Packit 423ecb
    if ((buf == NULL) || (ent == NULL)) return;
Packit 423ecb
    switch (ent->etype) {
Packit 423ecb
	case XML_INTERNAL_GENERAL_ENTITY:
Packit 423ecb
	    xmlBufferWriteChar(buf, "
Packit 423ecb
	    xmlBufferWriteCHAR(buf, ent->name);
Packit 423ecb
	    xmlBufferWriteChar(buf, " ");
Packit 423ecb
	    if (ent->orig != NULL)
Packit 423ecb
		xmlBufferWriteQuotedString(buf, ent->orig);
Packit 423ecb
	    else
Packit 423ecb
		xmlDumpEntityContent(buf, ent->content);
Packit 423ecb
	    xmlBufferWriteChar(buf, ">\n");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
Packit 423ecb
	    xmlBufferWriteChar(buf, "
Packit 423ecb
	    xmlBufferWriteCHAR(buf, ent->name);
Packit 423ecb
	    if (ent->ExternalID != NULL) {
Packit 423ecb
		 xmlBufferWriteChar(buf, " PUBLIC ");
Packit 423ecb
		 xmlBufferWriteQuotedString(buf, ent->ExternalID);
Packit 423ecb
		 xmlBufferWriteChar(buf, " ");
Packit 423ecb
		 xmlBufferWriteQuotedString(buf, ent->SystemID);
Packit 423ecb
	    } else {
Packit 423ecb
		 xmlBufferWriteChar(buf, " SYSTEM ");
Packit 423ecb
		 xmlBufferWriteQuotedString(buf, ent->SystemID);
Packit 423ecb
	    }
Packit 423ecb
	    xmlBufferWriteChar(buf, ">\n");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Packit 423ecb
	    xmlBufferWriteChar(buf, "
Packit 423ecb
	    xmlBufferWriteCHAR(buf, ent->name);
Packit 423ecb
	    if (ent->ExternalID != NULL) {
Packit 423ecb
		 xmlBufferWriteChar(buf, " PUBLIC ");
Packit 423ecb
		 xmlBufferWriteQuotedString(buf, ent->ExternalID);
Packit 423ecb
		 xmlBufferWriteChar(buf, " ");
Packit 423ecb
		 xmlBufferWriteQuotedString(buf, ent->SystemID);
Packit 423ecb
	    } else {
Packit 423ecb
		 xmlBufferWriteChar(buf, " SYSTEM ");
Packit 423ecb
		 xmlBufferWriteQuotedString(buf, ent->SystemID);
Packit 423ecb
	    }
Packit 423ecb
	    if (ent->content != NULL) { /* Should be true ! */
Packit 423ecb
		xmlBufferWriteChar(buf, " NDATA ");
Packit 423ecb
		if (ent->orig != NULL)
Packit 423ecb
		    xmlBufferWriteCHAR(buf, ent->orig);
Packit 423ecb
		else
Packit 423ecb
		    xmlBufferWriteCHAR(buf, ent->content);
Packit 423ecb
	    }
Packit 423ecb
	    xmlBufferWriteChar(buf, ">\n");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_INTERNAL_PARAMETER_ENTITY:
Packit 423ecb
	    xmlBufferWriteChar(buf, "
Packit 423ecb
	    xmlBufferWriteCHAR(buf, ent->name);
Packit 423ecb
	    xmlBufferWriteChar(buf, " ");
Packit 423ecb
	    if (ent->orig == NULL)
Packit 423ecb
		xmlDumpEntityContent(buf, ent->content);
Packit 423ecb
	    else
Packit 423ecb
		xmlBufferWriteQuotedString(buf, ent->orig);
Packit 423ecb
	    xmlBufferWriteChar(buf, ">\n");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_EXTERNAL_PARAMETER_ENTITY:
Packit 423ecb
	    xmlBufferWriteChar(buf, "
Packit 423ecb
	    xmlBufferWriteCHAR(buf, ent->name);
Packit 423ecb
	    if (ent->ExternalID != NULL) {
Packit 423ecb
		 xmlBufferWriteChar(buf, " PUBLIC ");
Packit 423ecb
		 xmlBufferWriteQuotedString(buf, ent->ExternalID);
Packit 423ecb
		 xmlBufferWriteChar(buf, " ");
Packit 423ecb
		 xmlBufferWriteQuotedString(buf, ent->SystemID);
Packit 423ecb
	    } else {
Packit 423ecb
		 xmlBufferWriteChar(buf, " SYSTEM ");
Packit 423ecb
		 xmlBufferWriteQuotedString(buf, ent->SystemID);
Packit 423ecb
	    }
Packit 423ecb
	    xmlBufferWriteChar(buf, ">\n");
Packit 423ecb
	    break;
Packit 423ecb
	default:
Packit 423ecb
	    xmlEntitiesErr(XML_DTD_UNKNOWN_ENTITY,
Packit 423ecb
		"xmlDumpEntitiesDecl: internal: unknown type entity type");
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDumpEntityDeclScan:
Packit 423ecb
 * @ent:  An entity table
Packit 423ecb
 * @buf:  An XML buffer.
Packit 423ecb
 *
Packit 423ecb
 * When using the hash table scan function, arguments need to be reversed
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) {
Packit 423ecb
    xmlDumpEntityDecl(buf, ent);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDumpEntitiesTable:
Packit 423ecb
 * @buf:  An XML buffer.
Packit 423ecb
 * @table:  An entity table
Packit 423ecb
 *
Packit 423ecb
 * This will dump the content of the entity table as an XML DTD definition
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {
Packit 423ecb
    xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf);
Packit 423ecb
}
Packit 423ecb
#endif /* LIBXML_OUTPUT_ENABLED */
Packit 423ecb
#define bottom_entities
Packit 423ecb
#include "elfgcchack.h"