Imported Upstream version 2.9.5_rc2
[platform/upstream/libxml2.git] / dict.c
diff --git a/dict.c b/dict.c
index 6de2f03..7b005eb 100644 (file)
--- a/dict.c
+++ b/dict.c
@@ -19,6 +19,7 @@
 #define IN_LIBXML
 #include "libxml.h"
 
+#include <limits.h>
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
@@ -86,14 +87,14 @@ typedef unsigned __int32 uint32_t;
 #endif /* WITH_BIG_KEY */
 
 /*
- * An entry in the dictionnary
+ * An entry in the dictionary
  */
 typedef struct _xmlDictEntry xmlDictEntry;
 typedef xmlDictEntry *xmlDictEntryPtr;
 struct _xmlDictEntry {
     struct _xmlDictEntry *next;
     const xmlChar *name;
-    int len;
+    unsigned int len;
     int valid;
     unsigned long okey;
 };
@@ -104,24 +105,26 @@ struct _xmlDictStrings {
     xmlDictStringsPtr next;
     xmlChar *free;
     xmlChar *end;
-    int size;
-    int nbStrings;
+    size_t size;
+    size_t nbStrings;
     xmlChar array[1];
 };
 /*
- * The entire dictionnary
+ * The entire dictionary
  */
 struct _xmlDict {
     int ref_counter;
 
     struct _xmlDictEntry *dict;
-    int size;
-    int nbElems;
+    size_t size;
+    unsigned int nbElems;
     xmlDictStringsPtr strings;
 
     struct _xmlDict *subdict;
     /* used for randomization */
     int seed;
+    /* used to impose a limit on size */
+    size_t limit;
 };
 
 /*
@@ -140,7 +143,7 @@ static int xmlDictInitialized = 0;
 /*
  * Internal data for random function, protected by xmlDictMutex
  */
-unsigned int rand_seed = 0;
+static unsigned int rand_seed = 0;
 #endif
 #endif
 
@@ -148,13 +151,28 @@ unsigned int rand_seed = 0;
  * xmlInitializeDict:
  *
  * Do the dictionary mutex initialization.
- * this function is not thread safe, initialization should
- * preferably be done once at startup
+ * this function is deprecated
  *
  * Returns 0 if initialization was already done, and 1 if that
  * call led to the initialization
  */
 int xmlInitializeDict(void) {
+    return(0);
+}
+
+/**
+ * __xmlInitializeDict:
+ *
+ * This function is not public
+ * Do the dictionary mutex initialization.
+ * this function is not thread safe, initialization should
+ * normally be done once at setup when called from xmlOnceInit()
+ * we may also land in this code if thread support is not compiled in
+ *
+ * Returns 0 if initialization was already done, and 1 if that
+ * call led to the initialization
+ */
+int __xmlInitializeDict(void) {
     if (xmlDictInitialized)
         return(1);
 
@@ -180,7 +198,7 @@ int __xmlRandom(void) {
     int ret;
 
     if (xmlDictInitialized == 0)
-        xmlInitializeDict();
+        __xmlInitializeDict();
 
     xmlRMutexLock(xmlDictMutex);
 #ifdef HAVE_RAND_R
@@ -211,19 +229,20 @@ xmlDictCleanup(void) {
 
 /*
  * xmlDictAddString:
- * @dict: the dictionnary
+ * @dict: the dictionary
  * @name: the name of the userdata
- * @len: the length of the name, if -1 it is recomputed
+ * @len: the length of the name
  *
  * Add the string to the array[s]
  *
  * Returns the pointer of the local string, or NULL in case of error.
  */
 static const xmlChar *
-xmlDictAddString(xmlDictPtr dict, const xmlChar *name, int namelen) {
+xmlDictAddString(xmlDictPtr dict, const xmlChar *name, unsigned int namelen) {
     xmlDictStringsPtr pool;
     const xmlChar *ret;
-    int size = 0; /* + sizeof(_xmlDictStrings) == 1024 */
+    size_t size = 0; /* + sizeof(_xmlDictStrings) == 1024 */
+    size_t limit = 0;
 
 #ifdef DICT_DEBUG_PATTERNS
     fprintf(stderr, "-");
@@ -233,15 +252,20 @@ xmlDictAddString(xmlDictPtr dict, const xmlChar *name, int namelen) {
        if (pool->end - pool->free > namelen)
            goto found_pool;
        if (pool->size > size) size = pool->size;
+        limit += pool->size;
        pool = pool->next;
     }
     /*
      * Not found, need to allocate
      */
     if (pool == NULL) {
+        if ((dict->limit > 0) && (limit > dict->limit)) {
+            return(NULL);
+        }
+
         if (size == 0) size = 1000;
        else size *= 4; /* exponential growth */
-        if (size < 4 * namelen) 
+        if (size < 4 * namelen)
            size = 4 * namelen; /* just in case ! */
        pool = (xmlDictStringsPtr) xmlMalloc(sizeof(xmlDictStrings) + size);
        if (pool == NULL)
@@ -267,23 +291,24 @@ found_pool:
 
 /*
  * xmlDictAddQString:
- * @dict: the dictionnary
+ * @dict: the dictionary
  * @prefix: the prefix of the userdata
  * @plen: the prefix length
  * @name: the name of the userdata
- * @len: the length of the name, if -1 it is recomputed
+ * @len: the length of the name
  *
  * Add the QName to the array[s]
  *
  * Returns the pointer of the local string, or NULL in case of error.
  */
 static const xmlChar *
-xmlDictAddQString(xmlDictPtr dict, const xmlChar *prefix, int plen,
-                 const xmlChar *name, int namelen)
+xmlDictAddQString(xmlDictPtr dict, const xmlChar *prefix, unsigned int plen,
+                 const xmlChar *name, unsigned int namelen)
 {
     xmlDictStringsPtr pool;
     const xmlChar *ret;
-    int size = 0; /* + sizeof(_xmlDictStrings) == 1024 */
+    size_t size = 0; /* + sizeof(_xmlDictStrings) == 1024 */
+    size_t limit = 0;
 
     if (prefix == NULL) return(xmlDictAddString(dict, name, namelen));
 
@@ -295,12 +320,17 @@ xmlDictAddQString(xmlDictPtr dict, const xmlChar *prefix, int plen,
        if (pool->end - pool->free > namelen + plen + 1)
            goto found_pool;
        if (pool->size > size) size = pool->size;
+        limit += pool->size;
        pool = pool->next;
     }
     /*
      * Not found, need to allocate
      */
     if (pool == NULL) {
+        if ((dict->limit > 0) && (limit > dict->limit)) {
+            return(NULL);
+        }
+
         if (size == 0) size = 1000;
        else size *= 4; /* exponential growth */
         if (size < 4 * (namelen + plen + 1))
@@ -456,7 +486,10 @@ xmlDictComputeFastQKey(const xmlChar *prefix, int plen,
        value += 30 * (*prefix);
 
     if (len > 10) {
-        value += name[len - (plen + 1 + 1)];
+        int offset = len - (plen + 1 + 1);
+       if (offset < 0)
+           offset = len - (10 + 1);
+       value += name[offset];
         len = 10;
        if (plen > 10)
            plen = 10;
@@ -500,14 +533,14 @@ xmlDictComputeFastQKey(const xmlChar *prefix, int plen,
  *
  * Create a new dictionary
  *
- * Returns the newly created dictionnary, or NULL if an error occured.
+ * Returns the newly created dictionary, or NULL if an error occurred.
  */
 xmlDictPtr
 xmlDictCreate(void) {
     xmlDictPtr dict;
 
     if (!xmlDictInitialized)
-        if (!xmlInitializeDict())
+        if (!__xmlInitializeDict())
             return(NULL);
 
 #ifdef DICT_DEBUG_PATTERNS
@@ -517,6 +550,7 @@ xmlDictCreate(void) {
     dict = xmlMalloc(sizeof(xmlDict));
     if (dict) {
         dict->ref_counter = 1;
+        dict->limit = 0;
 
         dict->size = MIN_DICT_SIZE;
        dict->nbElems = 0;
@@ -539,14 +573,14 @@ xmlDictCreate(void) {
 
 /**
  * xmlDictCreateSub:
- * @sub: an existing dictionnary
+ * @sub: an existing dictionary
  *
  * Create a new dictionary, inheriting strings from the read-only
- * dictionnary @sub. On lookup, strings are first searched in the
- * new dictionnary, then in @sub, and if not found are created in the
- * new dictionnary.
+ * dictionary @sub. On lookup, strings are first searched in the
+ * new dictionary, then in @sub, and if not found are created in the
+ * new dictionary.
  *
- * Returns the newly created dictionnary, or NULL if an error occured.
+ * Returns the newly created dictionary, or NULL if an error occurred.
  */
 xmlDictPtr
 xmlDictCreateSub(xmlDictPtr sub) {
@@ -565,7 +599,7 @@ xmlDictCreateSub(xmlDictPtr sub) {
 
 /**
  * xmlDictReference:
- * @dict: the dictionnary
+ * @dict: the dictionary
  *
  * Increment the reference counter of a dictionary
  *
@@ -574,7 +608,7 @@ xmlDictCreateSub(xmlDictPtr sub) {
 int
 xmlDictReference(xmlDictPtr dict) {
     if (!xmlDictInitialized)
-        if (!xmlInitializeDict())
+        if (!__xmlInitializeDict())
             return(-1);
 
     if (dict == NULL) return -1;
@@ -586,17 +620,17 @@ xmlDictReference(xmlDictPtr dict) {
 
 /**
  * xmlDictGrow:
- * @dict: the dictionnary
- * @size: the new size of the dictionnary
+ * @dict: the dictionary
+ * @size: the new size of the dictionary
  *
- * resize the dictionnary
+ * resize the dictionary
  *
  * Returns 0 in case of success, -1 in case of failure
  */
 static int
-xmlDictGrow(xmlDictPtr dict, int size) {
+xmlDictGrow(xmlDictPtr dict, size_t size) {
     unsigned long key, okey;
-    int oldsize, i;
+    size_t oldsize, i;
     xmlDictEntryPtr iter, next;
     struct _xmlDictEntry *olddict;
 #ifdef DEBUG_GROW
@@ -665,7 +699,7 @@ xmlDictGrow(xmlDictPtr dict, int size) {
            } else {
                /*
                 * we don't have much ways to alert from herei
-                * result is loosing an entry and unicity garantee
+                * result is losing an entry and unicity guarantee
                 */
                ret = -1;
            }
@@ -713,7 +747,7 @@ xmlDictGrow(xmlDictPtr dict, int size) {
 
 #ifdef DEBUG_GROW
     xmlGenericError(xmlGenericErrorContext,
-           "xmlDictGrow : from %d to %d, %d elems\n", oldsize, size, nbElem);
+           "xmlDictGrow : from %lu to %lu, %u elems\n", oldsize, size, nbElem);
 #endif
 
     return(ret);
@@ -721,14 +755,14 @@ xmlDictGrow(xmlDictPtr dict, int size) {
 
 /**
  * xmlDictFree:
- * @dict: the dictionnary
+ * @dict: the dictionary
  *
  * Free the hash @dict and its contents. The userdata is
  * deallocated with @f if provided.
  */
 void
 xmlDictFree(xmlDictPtr dict) {
-    int i;
+    size_t i;
     xmlDictEntryPtr iter;
     xmlDictEntryPtr next;
     int inside_dict = 0;
@@ -738,7 +772,7 @@ xmlDictFree(xmlDictPtr dict) {
        return;
 
     if (!xmlDictInitialized)
-        if (!xmlInitializeDict())
+        if (!__xmlInitializeDict())
             return;
 
     /* decrement the counter, it may be shared by a parser and docs */
@@ -783,11 +817,11 @@ xmlDictFree(xmlDictPtr dict) {
 
 /**
  * xmlDictLookup:
- * @dict: the dictionnary
+ * @dict: the dictionary
  * @name: the name of the userdata
  * @len: the length of the name, if -1 it is recomputed
  *
- * Add the @name to the dictionnary @dict if not present.
+ * Add the @name to the dictionary @dict if not present.
  *
  * Returns the internal copy of the name or NULL in case of internal error
  */
@@ -797,17 +831,24 @@ xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len) {
     xmlDictEntryPtr entry;
     xmlDictEntryPtr insert;
     const xmlChar *ret;
+    unsigned int l;
 
     if ((dict == NULL) || (name == NULL))
        return(NULL);
 
     if (len < 0)
-        len = strlen((const char *) name);
+        l = strlen((const char *) name);
+    else
+        l = len;
+
+    if (((dict->limit > 0) && (l >= dict->limit)) ||
+        (l > INT_MAX / 2))
+        return(NULL);
 
     /*
      * Check for duplicate and insertion location.
      */
-    okey = xmlDictComputeKey(dict, name, len);
+    okey = xmlDictComputeKey(dict, name, l);
     key = okey % dict->size;
     if (dict->dict[key].valid == 0) {
        insert = NULL;
@@ -815,25 +856,25 @@ xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len) {
        for (insert = &(dict->dict[key]); insert->next != NULL;
             insert = insert->next) {
 #ifdef __GNUC__
-           if ((insert->okey == okey) && (insert->len == len)) {
-               if (!memcmp(insert->name, name, len))
+           if ((insert->okey == okey) && (insert->len == l)) {
+               if (!memcmp(insert->name, name, l))
                    return(insert->name);
            }
 #else
-           if ((insert->okey == okey) && (insert->len == len) &&
-               (!xmlStrncmp(insert->name, name, len)))
+           if ((insert->okey == okey) && (insert->len == l) &&
+               (!xmlStrncmp(insert->name, name, l)))
                return(insert->name);
 #endif
            nbi++;
        }
 #ifdef __GNUC__
-       if ((insert->okey == okey) && (insert->len == len)) {
-           if (!memcmp(insert->name, name, len))
+       if ((insert->okey == okey) && (insert->len == l)) {
+           if (!memcmp(insert->name, name, l))
                return(insert->name);
        }
 #else
-       if ((insert->okey == okey) && (insert->len == len) &&
-           (!xmlStrncmp(insert->name, name, len)))
+       if ((insert->okey == okey) && (insert->len == l) &&
+           (!xmlStrncmp(insert->name, name, l)))
            return(insert->name);
 #endif
     }
@@ -846,7 +887,7 @@ xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len) {
             (dict->subdict->size != MIN_DICT_SIZE)) ||
             ((dict->size != MIN_DICT_SIZE) &&
             (dict->subdict->size == MIN_DICT_SIZE)))
-           skey = xmlDictComputeKey(dict->subdict, name, len);
+           skey = xmlDictComputeKey(dict->subdict, name, l);
        else
            skey = okey;
 
@@ -857,32 +898,32 @@ xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len) {
            for (tmp = &(dict->subdict->dict[key]); tmp->next != NULL;
                 tmp = tmp->next) {
 #ifdef __GNUC__
-               if ((tmp->okey == skey) && (tmp->len == len)) {
-                   if (!memcmp(tmp->name, name, len))
+               if ((tmp->okey == skey) && (tmp->len == l)) {
+                   if (!memcmp(tmp->name, name, l))
                        return(tmp->name);
                }
 #else
-               if ((tmp->okey == skey) && (tmp->len == len) &&
-                   (!xmlStrncmp(tmp->name, name, len)))
+               if ((tmp->okey == skey) && (tmp->len == l) &&
+                   (!xmlStrncmp(tmp->name, name, l)))
                    return(tmp->name);
 #endif
                nbi++;
            }
 #ifdef __GNUC__
-           if ((tmp->okey == skey) && (tmp->len == len)) {
-               if (!memcmp(tmp->name, name, len))
+           if ((tmp->okey == skey) && (tmp->len == l)) {
+               if (!memcmp(tmp->name, name, l))
                    return(tmp->name);
            }
 #else
-           if ((tmp->okey == skey) && (tmp->len == len) &&
-               (!xmlStrncmp(tmp->name, name, len)))
+           if ((tmp->okey == skey) && (tmp->len == l) &&
+               (!xmlStrncmp(tmp->name, name, l)))
                return(tmp->name);
 #endif
        }
        key = okey % dict->size;
     }
 
-    ret = xmlDictAddString(dict, name, len);
+    ret = xmlDictAddString(dict, name, l);
     if (ret == NULL)
         return(NULL);
     if (insert == NULL) {
@@ -893,13 +934,13 @@ xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len) {
             return(NULL);
     }
     entry->name = ret;
-    entry->len = len;
+    entry->len = l;
     entry->next = NULL;
     entry->valid = 1;
     entry->okey = okey;
 
 
-    if (insert != NULL) 
+    if (insert != NULL)
        insert->next = entry;
 
     dict->nbElems++;
@@ -916,11 +957,11 @@ xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len) {
 
 /**
  * xmlDictExists:
- * @dict: the dictionnary
+ * @dict: the dictionary
  * @name: the name of the userdata
  * @len: the length of the name, if -1 it is recomputed
  *
- * Check if the @name exists in the dictionnary @dict.
+ * Check if the @name exists in the dictionary @dict.
  *
  * Returns the internal copy of the name or NULL if not found.
  */
@@ -928,17 +969,23 @@ const xmlChar *
 xmlDictExists(xmlDictPtr dict, const xmlChar *name, int len) {
     unsigned long key, okey, nbi = 0;
     xmlDictEntryPtr insert;
+    unsigned int l;
 
     if ((dict == NULL) || (name == NULL))
        return(NULL);
 
     if (len < 0)
-        len = strlen((const char *) name);
+        l = strlen((const char *) name);
+    else
+        l = len;
+    if (((dict->limit > 0) && (l >= dict->limit)) ||
+        (l > INT_MAX / 2))
+        return(NULL);
 
     /*
      * Check for duplicate and insertion location.
      */
-    okey = xmlDictComputeKey(dict, name, len);
+    okey = xmlDictComputeKey(dict, name, l);
     key = okey % dict->size;
     if (dict->dict[key].valid == 0) {
        insert = NULL;
@@ -946,25 +993,25 @@ xmlDictExists(xmlDictPtr dict, const xmlChar *name, int len) {
        for (insert = &(dict->dict[key]); insert->next != NULL;
             insert = insert->next) {
 #ifdef __GNUC__
-           if ((insert->okey == okey) && (insert->len == len)) {
-               if (!memcmp(insert->name, name, len))
+           if ((insert->okey == okey) && (insert->len == l)) {
+               if (!memcmp(insert->name, name, l))
                    return(insert->name);
            }
 #else
-           if ((insert->okey == okey) && (insert->len == len) &&
-               (!xmlStrncmp(insert->name, name, len)))
+           if ((insert->okey == okey) && (insert->len == l) &&
+               (!xmlStrncmp(insert->name, name, l)))
                return(insert->name);
 #endif
            nbi++;
        }
 #ifdef __GNUC__
-       if ((insert->okey == okey) && (insert->len == len)) {
-           if (!memcmp(insert->name, name, len))
+       if ((insert->okey == okey) && (insert->len == l)) {
+           if (!memcmp(insert->name, name, l))
                return(insert->name);
        }
 #else
-       if ((insert->okey == okey) && (insert->len == len) &&
-           (!xmlStrncmp(insert->name, name, len)))
+       if ((insert->okey == okey) && (insert->len == l) &&
+           (!xmlStrncmp(insert->name, name, l)))
            return(insert->name);
 #endif
     }
@@ -977,7 +1024,7 @@ xmlDictExists(xmlDictPtr dict, const xmlChar *name, int len) {
             (dict->subdict->size != MIN_DICT_SIZE)) ||
             ((dict->size != MIN_DICT_SIZE) &&
             (dict->subdict->size == MIN_DICT_SIZE)))
-           skey = xmlDictComputeKey(dict->subdict, name, len);
+           skey = xmlDictComputeKey(dict->subdict, name, l);
        else
            skey = okey;
 
@@ -988,25 +1035,25 @@ xmlDictExists(xmlDictPtr dict, const xmlChar *name, int len) {
            for (tmp = &(dict->subdict->dict[key]); tmp->next != NULL;
                 tmp = tmp->next) {
 #ifdef __GNUC__
-               if ((tmp->okey == skey) && (tmp->len == len)) {
-                   if (!memcmp(tmp->name, name, len))
+               if ((tmp->okey == skey) && (tmp->len == l)) {
+                   if (!memcmp(tmp->name, name, l))
                        return(tmp->name);
                }
 #else
-               if ((tmp->okey == skey) && (tmp->len == len) &&
-                   (!xmlStrncmp(tmp->name, name, len)))
+               if ((tmp->okey == skey) && (tmp->len == l) &&
+                   (!xmlStrncmp(tmp->name, name, l)))
                    return(tmp->name);
 #endif
                nbi++;
            }
 #ifdef __GNUC__
-           if ((tmp->okey == skey) && (tmp->len == len)) {
-               if (!memcmp(tmp->name, name, len))
+           if ((tmp->okey == skey) && (tmp->len == l)) {
+               if (!memcmp(tmp->name, name, l))
                    return(tmp->name);
            }
 #else
-           if ((tmp->okey == skey) && (tmp->len == len) &&
-               (!xmlStrncmp(tmp->name, name, len)))
+           if ((tmp->okey == skey) && (tmp->len == l) &&
+               (!xmlStrncmp(tmp->name, name, l)))
                return(tmp->name);
 #endif
        }
@@ -1018,7 +1065,7 @@ xmlDictExists(xmlDictPtr dict, const xmlChar *name, int len) {
 
 /**
  * xmlDictQLookup:
- * @dict: the dictionnary
+ * @dict: the dictionary
  * @prefix: the prefix
  * @name: the name
  *
@@ -1032,7 +1079,7 @@ xmlDictQLookup(xmlDictPtr dict, const xmlChar *prefix, const xmlChar *name) {
     xmlDictEntryPtr entry;
     xmlDictEntryPtr insert;
     const xmlChar *ret;
-    int len, plen, l;
+    unsigned int len, plen, l;
 
     if ((dict == NULL) || (name == NULL))
        return(NULL);
@@ -1108,7 +1155,7 @@ xmlDictQLookup(xmlDictPtr dict, const xmlChar *prefix, const xmlChar *name) {
     entry->valid = 1;
     entry->okey = okey;
 
-    if (insert != NULL) 
+    if (insert != NULL)
        insert->next = entry;
 
     dict->nbElems++;
@@ -1123,7 +1170,7 @@ xmlDictQLookup(xmlDictPtr dict, const xmlChar *prefix, const xmlChar *name) {
 
 /**
  * xmlDictOwns:
- * @dict: the dictionnary
+ * @dict: the dictionary
  * @str: the string
  *
  * check if a string is owned by the disctionary
@@ -1150,11 +1197,11 @@ xmlDictOwns(xmlDictPtr dict, const xmlChar *str) {
 
 /**
  * xmlDictSize:
- * @dict: the dictionnary
+ * @dict: the dictionary
  *
  * Query the number of elements installed in the hash @dict.
  *
- * Returns the number of elements in the dictionnary or
+ * Returns the number of elements in the dictionary or
  * -1 in case of error
  */
 int
@@ -1166,6 +1213,50 @@ xmlDictSize(xmlDictPtr dict) {
     return(dict->nbElems);
 }
 
+/**
+ * xmlDictSetLimit:
+ * @dict: the dictionary
+ * @limit: the limit in bytes
+ *
+ * Set a size limit for the dictionary
+ * Added in 2.9.0
+ *
+ * Returns the previous limit of the dictionary or 0
+ */
+size_t
+xmlDictSetLimit(xmlDictPtr dict, size_t limit) {
+    size_t ret;
+
+    if (dict == NULL)
+       return(0);
+    ret = dict->limit;
+    dict->limit = limit;
+    return(ret);
+}
+
+/**
+ * xmlDictGetUsage:
+ * @dict: the dictionary
+ *
+ * Get how much memory is used by a dictionary for strings
+ * Added in 2.9.0
+ *
+ * Returns the amount of strings allocated
+ */
+size_t
+xmlDictGetUsage(xmlDictPtr dict) {
+    xmlDictStringsPtr pool;
+    size_t limit = 0;
+
+    if (dict == NULL)
+       return(0);
+    pool = dict->strings;
+    while (pool != NULL) {
+        limit += pool->size;
+       pool = pool->next;
+    }
+    return(limit);
+}
 
 #define bottom_dict
 #include "elfgcchack.h"