Diff for /embedaddon/libxml2/entities.c between versions 1.1.1.1 and 1.1.1.2

version 1.1.1.1, 2012/02/21 23:37:57 version 1.1.1.2, 2014/06/15 19:53:28
Line 22 Line 22
 #include <libxml/globals.h>  #include <libxml/globals.h>
 #include <libxml/dict.h>  #include <libxml/dict.h>
   
   #include "save.h"
   
 /*  /*
  * The XML predefined entities.   * The XML predefined entities.
  */   */
   
 static xmlEntity xmlEntityLt = {  static xmlEntity xmlEntityLt = {
     NULL, XML_ENTITY_DECL, BAD_CAST "lt",      NULL, XML_ENTITY_DECL, BAD_CAST "lt",
    NULL, NULL, NULL, NULL, NULL, NULL,     NULL, NULL, NULL, NULL, NULL, NULL,
     BAD_CAST "<", BAD_CAST "<", 1,      BAD_CAST "<", BAD_CAST "<", 1,
     XML_INTERNAL_PREDEFINED_ENTITY,      XML_INTERNAL_PREDEFINED_ENTITY,
     NULL, NULL, NULL, NULL, 0, 1      NULL, NULL, NULL, NULL, 0, 1
 };  };
 static xmlEntity xmlEntityGt = {  static xmlEntity xmlEntityGt = {
     NULL, XML_ENTITY_DECL, BAD_CAST "gt",      NULL, XML_ENTITY_DECL, BAD_CAST "gt",
    NULL, NULL, NULL, NULL, NULL, NULL,     NULL, NULL, NULL, NULL, NULL, NULL,
     BAD_CAST ">", BAD_CAST ">", 1,      BAD_CAST ">", BAD_CAST ">", 1,
     XML_INTERNAL_PREDEFINED_ENTITY,      XML_INTERNAL_PREDEFINED_ENTITY,
     NULL, NULL, NULL, NULL, 0, 1      NULL, NULL, NULL, NULL, 0, 1
 };  };
 static xmlEntity xmlEntityAmp = {  static xmlEntity xmlEntityAmp = {
     NULL, XML_ENTITY_DECL, BAD_CAST "amp",      NULL, XML_ENTITY_DECL, BAD_CAST "amp",
    NULL, NULL, NULL, NULL, NULL, NULL,     NULL, NULL, NULL, NULL, NULL, NULL,
     BAD_CAST "&", BAD_CAST "&", 1,      BAD_CAST "&", BAD_CAST "&", 1,
     XML_INTERNAL_PREDEFINED_ENTITY,      XML_INTERNAL_PREDEFINED_ENTITY,
     NULL, NULL, NULL, NULL, 0, 1      NULL, NULL, NULL, NULL, 0, 1
 };  };
 static xmlEntity xmlEntityQuot = {  static xmlEntity xmlEntityQuot = {
     NULL, XML_ENTITY_DECL, BAD_CAST "quot",      NULL, XML_ENTITY_DECL, BAD_CAST "quot",
    NULL, NULL, NULL, NULL, NULL, NULL,     NULL, NULL, NULL, NULL, NULL, NULL,
     BAD_CAST "\"", BAD_CAST "\"", 1,      BAD_CAST "\"", BAD_CAST "\"", 1,
     XML_INTERNAL_PREDEFINED_ENTITY,      XML_INTERNAL_PREDEFINED_ENTITY,
     NULL, NULL, NULL, NULL, 0, 1      NULL, NULL, NULL, NULL, 0, 1
 };  };
 static xmlEntity xmlEntityApos = {  static xmlEntity xmlEntityApos = {
     NULL, XML_ENTITY_DECL, BAD_CAST "apos",      NULL, XML_ENTITY_DECL, BAD_CAST "apos",
    NULL, NULL, NULL, NULL, NULL, NULL,     NULL, NULL, NULL, NULL, NULL, NULL,
     BAD_CAST "'", BAD_CAST "'", 1,      BAD_CAST "'", BAD_CAST "'", 1,
     XML_INTERNAL_PREDEFINED_ENTITY,      XML_INTERNAL_PREDEFINED_ENTITY,
     NULL, NULL, NULL, NULL, 0, 1      NULL, NULL, NULL, NULL, 0, 1
Line 426  xmlNewEntity(xmlDocPtr doc, const xmlChar *name, int t Line 428  xmlNewEntity(xmlDocPtr doc, const xmlChar *name, int t
  *   *
  * Do an entity lookup in the table.   * Do an entity lookup in the table.
  * returns the corresponding parameter entity, if found.   * returns the corresponding parameter entity, if found.
 *  *
  * Returns A pointer to the entity structure or NULL if not found.   * Returns A pointer to the entity structure or NULL if not found.
  */   */
 static xmlEntityPtr  static xmlEntityPtr
Line 441  xmlGetEntityFromTable(xmlEntitiesTablePtr table, const Line 443  xmlGetEntityFromTable(xmlEntitiesTablePtr table, const
  *   *
  * Do an entity lookup in the internal and external subsets and   * Do an entity lookup in the internal and external subsets and
  * returns the corresponding parameter entity, if found.   * returns the corresponding parameter entity, if found.
 *  *
  * Returns A pointer to the entity structure or NULL if not found.   * Returns A pointer to the entity structure or NULL if not found.
  */   */
 xmlEntityPtr  xmlEntityPtr
Line 472  xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *na Line 474  xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *na
  * Do an entity lookup in the DTD entity hash table and   * Do an entity lookup in the DTD entity hash table and
  * returns the corresponding entity, if found.   * returns the corresponding entity, if found.
  * Note: the first argument is the document node, not the DTD node.   * Note: the first argument is the document node, not the DTD node.
 *  *
  * Returns A pointer to the entity structure or NULL if not found.   * Returns A pointer to the entity structure or NULL if not found.
  */   */
 xmlEntityPtr  xmlEntityPtr
Line 496  xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) { Line 498  xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) {
  * Do an entity lookup in the document entity hash table and   * Do an entity lookup in the document entity hash table and
  * returns the corresponding entity, otherwise a lookup is done   * returns the corresponding entity, otherwise a lookup is done
  * in the predefined entities too.   * in the predefined entities too.
 *  *
  * Returns A pointer to the entity structure or NULL if not found.   * Returns A pointer to the entity structure or NULL if not found.
  */   */
 xmlEntityPtr  xmlEntityPtr
Line 528  xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) { Line 530  xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) {
  * Macro used to grow the current buffer.   * Macro used to grow the current buffer.
  */   */
 #define growBufferReentrant() {                                         \  #define growBufferReentrant() {                                         \
    buffer_size *= 2;                                                    \    xmlChar *tmp;                                                       \
    buffer = (xmlChar *)                                           \    size_t new_size = buffer_size * 2;                                  \
                xmlRealloc(buffer, buffer_size * sizeof(xmlChar));   \    if (new_size < buffer_size) goto mem_error;                         \
    if (buffer == NULL) {                                               \    tmp = (xmlChar *) xmlRealloc(buffer, new_size);                      \
        xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: realloc failed");\    if (tmp == NULL) goto mem_error;                                    \
        return(NULL);                                                       \    buffer = tmp;                                                       \
    }                                                                   \    buffer_size = new_size;                                               \
 }  }
   
   
 /**  /**
 * xmlEncodeEntitiesReentrant: * xmlEncodeEntitiesInternal:
  * @doc:  the document containing the string   * @doc:  the document containing the string
  * @input:  A string to convert to XML.   * @input:  A string to convert to XML.
    * @attr: are we handling an atrbute value
  *   *
  * Do a global encoding of a string, replacing the predefined entities   * Do a global encoding of a string, replacing the predefined entities
  * and non ASCII values with their entities and CharRef counterparts.   * and non ASCII values with their entities and CharRef counterparts.
Line 550  xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) { Line 552  xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) {
  *   *
  * Returns A newly allocated string with the substitution done.   * Returns A newly allocated string with the substitution done.
  */   */
xmlChar *static xmlChar *
xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {xmlEncodeEntitiesInternal(xmlDocPtr doc, const xmlChar *input, int attr) {
     const xmlChar *cur = input;      const xmlChar *cur = input;
     xmlChar *buffer = NULL;      xmlChar *buffer = NULL;
     xmlChar *out = NULL;      xmlChar *out = NULL;
    int buffer_size = 0;    size_t buffer_size = 0;
     int html = 0;      int html = 0;
   
     if (input == NULL) return(NULL);      if (input == NULL) return(NULL);
Line 568  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha Line 570  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha
     buffer_size = 1000;      buffer_size = 1000;
     buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));      buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
     if (buffer == NULL) {      if (buffer == NULL) {
        xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: malloc failed");        xmlEntitiesErrMemory("xmlEncodeEntities: malloc failed");
         return(NULL);          return(NULL);
     }      }
     out = buffer;      out = buffer;
   
     while (*cur != '\0') {      while (*cur != '\0') {
        if (out - buffer > buffer_size - 100) {        size_t indx = out - buffer;
            int indx = out - buffer;        if (indx + 100 > buffer_size) {
   
             growBufferReentrant();              growBufferReentrant();
             out = &buffer[indx];              out = &buffer[indx];
Line 585  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha Line 587  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha
          * By default one have to encode at least '<', '>', '"' and '&' !           * By default one have to encode at least '<', '>', '"' and '&' !
          */           */
         if (*cur == '<') {          if (*cur == '<') {
               const xmlChar *end;
   
               /*
                * Special handling of server side include in HTML attributes
                */
               if (html && attr &&
                   (cur[1] == '!') && (cur[2] == '-') && (cur[3] == '-') &&
                   ((end = xmlStrstr(cur, BAD_CAST "-->")) != NULL)) {
                   while (cur != end) {
                       *out++ = *cur++;
                       indx = out - buffer;
                       if (indx + 100 > buffer_size) {
                           growBufferReentrant();
                           out = &buffer[indx];
                       }
                   }
                   *out++ = *cur++;
                   *out++ = *cur++;
                   *out++ = *cur++;
                   continue;
               }
             *out++ = '&';              *out++ = '&';
             *out++ = 'l';              *out++ = 'l';
             *out++ = 't';              *out++ = 't';
Line 595  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha Line 618  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha
             *out++ = 't';              *out++ = 't';
             *out++ = ';';              *out++ = ';';
         } else if (*cur == '&') {          } else if (*cur == '&') {
               /*
                * Special handling of &{...} construct from HTML 4, see
                * http://www.w3.org/TR/html401/appendix/notes.html#h-B.7.1
                */
               if (html && attr && (cur[1] == '{') &&
                   (strchr((const char *) cur, '}'))) {
                   while (*cur != '}') {
                       *out++ = *cur++;
                       indx = out - buffer;
                       if (indx + 100 > buffer_size) {
                           growBufferReentrant();
                           out = &buffer[indx];
                       }
                   }
                   *out++ = *cur++;
                   continue;
               }
             *out++ = '&';              *out++ = '&';
             *out++ = 'a';              *out++ = 'a';
             *out++ = 'm';              *out++ = 'm';
Line 609  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha Line 649  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha
         } else if (*cur >= 0x80) {          } else if (*cur >= 0x80) {
             if (((doc != NULL) && (doc->encoding != NULL)) || (html)) {              if (((doc != NULL) && (doc->encoding != NULL)) || (html)) {
                 /*                  /*
                 * Bjørn Reese <br@sseusa.com> provided the patch                 * Bjørn Reese <br@sseusa.com> provided the patch
                 xmlChar xc;                  xmlChar xc;
                 xc = (*cur & 0x3F) << 6;                  xc = (*cur & 0x3F) << 6;
                 if (cur[1] != 0) {                  if (cur[1] != 0) {
Line 627  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha Line 667  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha
   
                 if (*cur < 0xC0) {                  if (*cur < 0xC0) {
                     xmlEntitiesErr(XML_CHECK_NOT_UTF8,                      xmlEntitiesErr(XML_CHECK_NOT_UTF8,
                            "xmlEncodeEntitiesReentrant : input not UTF-8");                            "xmlEncodeEntities: input not UTF-8");
                     if (doc != NULL)                      if (doc != NULL)
                         doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");                          doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
                     snprintf(buf, sizeof(buf), "&#%d;", *cur);                      snprintf(buf, sizeof(buf), "&#%d;", *cur);
Line 660  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha Line 700  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha
                 }                  }
                 if ((l == 1) || (!IS_CHAR(val))) {                  if ((l == 1) || (!IS_CHAR(val))) {
                     xmlEntitiesErr(XML_ERR_INVALID_CHAR,                      xmlEntitiesErr(XML_ERR_INVALID_CHAR,
                        "xmlEncodeEntitiesReentrant : char out of range\n");                        "xmlEncodeEntities: char out of range\n");
                     if (doc != NULL)                      if (doc != NULL)
                         doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");                          doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
                     snprintf(buf, sizeof(buf), "&#%d;", *cur);                      snprintf(buf, sizeof(buf), "&#%d;", *cur);
Line 692  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha Line 732  xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlCha
     }      }
     *out = 0;      *out = 0;
     return(buffer);      return(buffer);
   
   mem_error:
       xmlEntitiesErrMemory("xmlEncodeEntities: realloc failed");
       xmlFree(buffer);
       return(NULL);
 }  }
   
 /**  /**
    * xmlEncodeAttributeEntities:
    * @doc:  the document containing the string
    * @input:  A string to convert to XML.
    *
    * Do a global encoding of a string, replacing the predefined entities
    * and non ASCII values with their entities and CharRef counterparts for
    * attribute values.
    *
    * Returns A newly allocated string with the substitution done.
    */
   xmlChar *
   xmlEncodeAttributeEntities(xmlDocPtr doc, const xmlChar *input) {
       return xmlEncodeEntitiesInternal(doc, input, 1);
   }
   
   /**
    * xmlEncodeEntitiesReentrant:
    * @doc:  the document containing the string
    * @input:  A string to convert to XML.
    *
    * Do a global encoding of a string, replacing the predefined entities
    * and non ASCII values with their entities and CharRef counterparts.
    * Contrary to xmlEncodeEntities, this routine is reentrant, and result
    * must be deallocated.
    *
    * Returns A newly allocated string with the substitution done.
    */
   xmlChar *
   xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {
       return xmlEncodeEntitiesInternal(doc, input, 0);
   }
   
   /**
  * xmlEncodeSpecialChars:   * xmlEncodeSpecialChars:
  * @doc:  the document containing the string   * @doc:  the document containing the string
  * @input:  A string to convert to XML.   * @input:  A string to convert to XML.
Line 709  xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED,  Line 787  xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED, 
     const xmlChar *cur = input;      const xmlChar *cur = input;
     xmlChar *buffer = NULL;      xmlChar *buffer = NULL;
     xmlChar *out = NULL;      xmlChar *out = NULL;
    int buffer_size = 0;    size_t buffer_size = 0;
     if (input == NULL) return(NULL);      if (input == NULL) return(NULL);
   
     /*      /*
Line 724  xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED,  Line 802  xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED, 
     out = buffer;      out = buffer;
   
     while (*cur != '\0') {      while (*cur != '\0') {
        if (out - buffer > buffer_size - 10) {        size_t indx = out - buffer;
            int indx = out - buffer;        if (indx + 10 > buffer_size) {
   
             growBufferReentrant();              growBufferReentrant();
             out = &buffer[indx];              out = &buffer[indx];
Line 774  xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED,  Line 852  xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED, 
     }      }
     *out = 0;      *out = 0;
     return(buffer);      return(buffer);
   
   mem_error:
       xmlEntitiesErrMemory("xmlEncodeSpecialChars: realloc failed");
       xmlFree(buffer);
       return(NULL);
 }  }
   
 /**  /**
Line 820  xmlFreeEntitiesTable(xmlEntitiesTablePtr table) { Line 903  xmlFreeEntitiesTable(xmlEntitiesTablePtr table) {
  * @ent:  An entity   * @ent:  An entity
  *   *
  * Build a copy of an entity   * Build a copy of an entity
 *  *
  * Returns the new xmlEntitiesPtr or NULL in case of error.   * Returns the new xmlEntitiesPtr or NULL in case of error.
  */   */
 static xmlEntityPtr  static xmlEntityPtr
Line 856  xmlCopyEntity(xmlEntityPtr ent) { Line 939  xmlCopyEntity(xmlEntityPtr ent) {
  * @table:  An entity table   * @table:  An entity table
  *   *
  * Build a copy of an entity table.   * Build a copy of an entity table.
 *  *
  * Returns the new xmlEntitiesTablePtr or NULL in case of error.   * Returns the new xmlEntitiesTablePtr or NULL in case of error.
  */   */
 xmlEntitiesTablePtr  xmlEntitiesTablePtr
Line 1005  static void Line 1088  static void
 xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) {  xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) {
     xmlDumpEntityDecl(buf, ent);      xmlDumpEntityDecl(buf, ent);
 }  }
      
 /**  /**
  * xmlDumpEntitiesTable:   * xmlDumpEntitiesTable:
  * @buf:  An XML buffer.   * @buf:  An XML buffer.

Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.2


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>