Blame entities.c

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