Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * hash.c: chained hash tables |
| 3 | * |
| 4 | * Reference: Your favorite introductory book on algorithms |
| 5 | * |
Daniel Veillard | 8973d58 | 2012-02-04 19:07:44 +0800 | [diff] [blame] | 6 | * Copyright (C) 2000,2012 Bjorn Reese and Daniel Veillard. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7 | * |
| 8 | * Permission to use, copy, modify, and distribute this software for any |
| 9 | * purpose with or without fee is hereby granted, provided that the above |
| 10 | * copyright notice and this permission notice appear in all copies. |
| 11 | * |
| 12 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
| 13 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
Haibo Huang | cfd91dc | 2020-07-30 23:01:33 -0700 | [diff] [blame] | 14 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15 | * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER. |
| 16 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 17 | * Author: breese@users.sourceforge.net |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 20 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 21 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 22 | |
| 23 | #include <string.h> |
Daniel Veillard | 8973d58 | 2012-02-04 19:07:44 +0800 | [diff] [blame] | 24 | #ifdef HAVE_STDLIB_H |
| 25 | #include <stdlib.h> |
| 26 | #endif |
| 27 | #ifdef HAVE_TIME_H |
| 28 | #include <time.h> |
| 29 | #endif |
| 30 | |
| 31 | /* |
| 32 | * Following http://www.ocert.org/advisories/ocert-2011-003.html |
| 33 | * it seems that having hash randomization might be a good idea |
| 34 | * when using XML with untrusted data |
| 35 | */ |
Haibo Huang | cfd91dc | 2020-07-30 23:01:33 -0700 | [diff] [blame] | 36 | #if defined(HAVE_RAND) && defined(HAVE_SRAND) && defined(HAVE_TIME) && \ |
| 37 | !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) |
Daniel Veillard | 8973d58 | 2012-02-04 19:07:44 +0800 | [diff] [blame] | 38 | #define HASH_RANDOMIZATION |
| 39 | #endif |
| 40 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 41 | #include <libxml/parser.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 42 | #include <libxml/hash.h> |
| 43 | #include <libxml/xmlmemory.h> |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 44 | #include <libxml/xmlerror.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 45 | #include <libxml/globals.h> |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 46 | |
| 47 | #define MAX_HASH_LEN 8 |
| 48 | |
| 49 | /* #define DEBUG_GROW */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * A single entry in the hash table |
| 53 | */ |
| 54 | typedef struct _xmlHashEntry xmlHashEntry; |
| 55 | typedef xmlHashEntry *xmlHashEntryPtr; |
| 56 | struct _xmlHashEntry { |
| 57 | struct _xmlHashEntry *next; |
| 58 | xmlChar *name; |
| 59 | xmlChar *name2; |
| 60 | xmlChar *name3; |
| 61 | void *payload; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 62 | int valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * The entire hash table |
| 67 | */ |
| 68 | struct _xmlHashTable { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 69 | struct _xmlHashEntry *table; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 70 | int size; |
| 71 | int nbElems; |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 72 | xmlDictPtr dict; |
Daniel Veillard | 8973d58 | 2012-02-04 19:07:44 +0800 | [diff] [blame] | 73 | #ifdef HASH_RANDOMIZATION |
| 74 | int random_seed; |
| 75 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* |
| 79 | * xmlHashComputeKey: |
| 80 | * Calculate the hash key |
| 81 | */ |
Haibo Huang | cfd91dc | 2020-07-30 23:01:33 -0700 | [diff] [blame] | 82 | #ifdef __clang__ |
| 83 | ATTRIBUTE_NO_SANITIZE("unsigned-integer-overflow") |
| 84 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 85 | static unsigned long |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 86 | xmlHashComputeKey(xmlHashTablePtr table, const xmlChar *name, |
| 87 | const xmlChar *name2, const xmlChar *name3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 88 | unsigned long value = 0L; |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 89 | unsigned long ch; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 90 | |
Daniel Veillard | 8973d58 | 2012-02-04 19:07:44 +0800 | [diff] [blame] | 91 | #ifdef HASH_RANDOMIZATION |
| 92 | value = table->random_seed; |
| 93 | #endif |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 94 | if (name != NULL) { |
| 95 | value += 30 * (*name); |
| 96 | while ((ch = *name++) != 0) { |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 97 | value = value ^ ((value << 5) + (value >> 3) + ch); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
Daniel Franke | b123711 | 2013-04-12 18:53:53 +0800 | [diff] [blame] | 100 | value = value ^ ((value << 5) + (value >> 3)); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 101 | if (name2 != NULL) { |
| 102 | while ((ch = *name2++) != 0) { |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 103 | value = value ^ ((value << 5) + (value >> 3) + ch); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
Daniel Franke | b123711 | 2013-04-12 18:53:53 +0800 | [diff] [blame] | 106 | value = value ^ ((value << 5) + (value >> 3)); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 107 | if (name3 != NULL) { |
| 108 | while ((ch = *name3++) != 0) { |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 109 | value = value ^ ((value << 5) + (value >> 3) + ch); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 110 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 111 | } |
| 112 | return (value % table->size); |
| 113 | } |
| 114 | |
Haibo Huang | cfd91dc | 2020-07-30 23:01:33 -0700 | [diff] [blame] | 115 | #ifdef __clang__ |
| 116 | ATTRIBUTE_NO_SANITIZE("unsigned-integer-overflow") |
| 117 | #endif |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 118 | static unsigned long |
| 119 | xmlHashComputeQKey(xmlHashTablePtr table, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 120 | const xmlChar *prefix, const xmlChar *name, |
| 121 | const xmlChar *prefix2, const xmlChar *name2, |
| 122 | const xmlChar *prefix3, const xmlChar *name3) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 123 | unsigned long value = 0L; |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 124 | unsigned long ch; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 125 | |
Daniel Veillard | 8973d58 | 2012-02-04 19:07:44 +0800 | [diff] [blame] | 126 | #ifdef HASH_RANDOMIZATION |
| 127 | value = table->random_seed; |
| 128 | #endif |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 129 | if (prefix != NULL) |
| 130 | value += 30 * (*prefix); |
| 131 | else |
| 132 | value += 30 * (*name); |
| 133 | |
| 134 | if (prefix != NULL) { |
| 135 | while ((ch = *prefix++) != 0) { |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 136 | value = value ^ ((value << 5) + (value >> 3) + ch); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 137 | } |
| 138 | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)':'); |
| 139 | } |
| 140 | if (name != NULL) { |
| 141 | while ((ch = *name++) != 0) { |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 142 | value = value ^ ((value << 5) + (value >> 3) + ch); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
Daniel Franke | b123711 | 2013-04-12 18:53:53 +0800 | [diff] [blame] | 145 | value = value ^ ((value << 5) + (value >> 3)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 146 | if (prefix2 != NULL) { |
| 147 | while ((ch = *prefix2++) != 0) { |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 148 | value = value ^ ((value << 5) + (value >> 3) + ch); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 149 | } |
| 150 | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)':'); |
| 151 | } |
| 152 | if (name2 != NULL) { |
| 153 | while ((ch = *name2++) != 0) { |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 154 | value = value ^ ((value << 5) + (value >> 3) + ch); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
Daniel Franke | b123711 | 2013-04-12 18:53:53 +0800 | [diff] [blame] | 157 | value = value ^ ((value << 5) + (value >> 3)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 158 | if (prefix3 != NULL) { |
| 159 | while ((ch = *prefix3++) != 0) { |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 160 | value = value ^ ((value << 5) + (value >> 3) + ch); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 161 | } |
| 162 | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)':'); |
| 163 | } |
| 164 | if (name3 != NULL) { |
| 165 | while ((ch = *name3++) != 0) { |
Elliott Hughes | ecdab2a | 2022-02-23 14:33:50 -0800 | [diff] [blame] | 166 | value = value ^ ((value << 5) + (value >> 3) + ch); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | return (value % table->size); |
| 170 | } |
| 171 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 172 | /** |
| 173 | * xmlHashCreate: |
| 174 | * @size: the size of the hash table |
| 175 | * |
| 176 | * Create a new xmlHashTablePtr. |
| 177 | * |
Nick Wellnhofer | 8bbe450 | 2017-06-17 16:15:09 +0200 | [diff] [blame] | 178 | * Returns the newly created object, or NULL if an error occurred. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 179 | */ |
| 180 | xmlHashTablePtr |
| 181 | xmlHashCreate(int size) { |
| 182 | xmlHashTablePtr table; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 183 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 184 | if (size <= 0) |
| 185 | size = 256; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 186 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 187 | table = xmlMalloc(sizeof(xmlHashTable)); |
| 188 | if (table) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 189 | table->dict = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 190 | table->size = size; |
| 191 | table->nbElems = 0; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 192 | table->table = xmlMalloc(size * sizeof(xmlHashEntry)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 193 | if (table->table) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 194 | memset(table->table, 0, size * sizeof(xmlHashEntry)); |
Daniel Veillard | 8973d58 | 2012-02-04 19:07:44 +0800 | [diff] [blame] | 195 | #ifdef HASH_RANDOMIZATION |
Daniel Veillard | 379ebc1 | 2012-05-18 15:41:31 +0800 | [diff] [blame] | 196 | table->random_seed = __xmlRandom(); |
Daniel Veillard | 8973d58 | 2012-02-04 19:07:44 +0800 | [diff] [blame] | 197 | #endif |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 198 | return(table); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 199 | } |
| 200 | xmlFree(table); |
| 201 | } |
| 202 | return(NULL); |
| 203 | } |
| 204 | |
| 205 | /** |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 206 | * xmlHashCreateDict: |
| 207 | * @size: the size of the hash table |
| 208 | * @dict: a dictionary to use for the hash |
| 209 | * |
| 210 | * Create a new xmlHashTablePtr which will use @dict as the internal dictionary |
| 211 | * |
Nick Wellnhofer | 8bbe450 | 2017-06-17 16:15:09 +0200 | [diff] [blame] | 212 | * Returns the newly created object, or NULL if an error occurred. |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 213 | */ |
| 214 | xmlHashTablePtr |
| 215 | xmlHashCreateDict(int size, xmlDictPtr dict) { |
| 216 | xmlHashTablePtr table; |
| 217 | |
| 218 | table = xmlHashCreate(size); |
| 219 | if (table != NULL) { |
| 220 | table->dict = dict; |
| 221 | xmlDictReference(dict); |
| 222 | } |
| 223 | return(table); |
| 224 | } |
| 225 | |
| 226 | /** |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 227 | * xmlHashGrow: |
| 228 | * @table: the hash table |
| 229 | * @size: the new size of the hash table |
| 230 | * |
| 231 | * resize the hash table |
| 232 | * |
| 233 | * Returns 0 in case of success, -1 in case of failure |
| 234 | */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 235 | static int |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 236 | xmlHashGrow(xmlHashTablePtr table, int size) { |
| 237 | unsigned long key; |
| 238 | int oldsize, i; |
| 239 | xmlHashEntryPtr iter, next; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 240 | struct _xmlHashEntry *oldtable; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 241 | #ifdef DEBUG_GROW |
| 242 | unsigned long nbElem = 0; |
| 243 | #endif |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 244 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 245 | if (table == NULL) |
| 246 | return(-1); |
| 247 | if (size < 8) |
| 248 | return(-1); |
| 249 | if (size > 8 * 2048) |
| 250 | return(-1); |
| 251 | |
| 252 | oldsize = table->size; |
| 253 | oldtable = table->table; |
| 254 | if (oldtable == NULL) |
| 255 | return(-1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 256 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 257 | table->table = xmlMalloc(size * sizeof(xmlHashEntry)); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 258 | if (table->table == NULL) { |
| 259 | table->table = oldtable; |
| 260 | return(-1); |
| 261 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 262 | memset(table->table, 0, size * sizeof(xmlHashEntry)); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 263 | table->size = size; |
| 264 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 265 | /* If the two loops are merged, there would be situations where |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 266 | a new entry needs to allocated and data copied into it from |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 267 | the main table. So instead, we run through the array twice, first |
| 268 | copying all the elements in the main array (where we can't get |
| 269 | conflicts) and then the rest, so we only free (and don't allocate) |
| 270 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 271 | for (i = 0; i < oldsize; i++) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 272 | if (oldtable[i].valid == 0) |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 273 | continue; |
| 274 | key = xmlHashComputeKey(table, oldtable[i].name, oldtable[i].name2, |
| 275 | oldtable[i].name3); |
| 276 | memcpy(&(table->table[key]), &(oldtable[i]), sizeof(xmlHashEntry)); |
| 277 | table->table[key].next = NULL; |
| 278 | } |
| 279 | |
| 280 | for (i = 0; i < oldsize; i++) { |
| 281 | iter = oldtable[i].next; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 282 | while (iter) { |
| 283 | next = iter->next; |
| 284 | |
| 285 | /* |
| 286 | * put back the entry in the new table |
| 287 | */ |
| 288 | |
| 289 | key = xmlHashComputeKey(table, iter->name, iter->name2, |
| 290 | iter->name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 291 | if (table->table[key].valid == 0) { |
| 292 | memcpy(&(table->table[key]), iter, sizeof(xmlHashEntry)); |
| 293 | table->table[key].next = NULL; |
| 294 | xmlFree(iter); |
| 295 | } else { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 296 | iter->next = table->table[key].next; |
| 297 | table->table[key].next = iter; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 298 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 299 | |
| 300 | #ifdef DEBUG_GROW |
| 301 | nbElem++; |
| 302 | #endif |
| 303 | |
| 304 | iter = next; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | xmlFree(oldtable); |
| 309 | |
| 310 | #ifdef DEBUG_GROW |
| 311 | xmlGenericError(xmlGenericErrorContext, |
| 312 | "xmlHashGrow : from %d to %d, %d elems\n", oldsize, size, nbElem); |
| 313 | #endif |
| 314 | |
| 315 | return(0); |
| 316 | } |
| 317 | |
| 318 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 319 | * xmlHashFree: |
| 320 | * @table: the hash table |
| 321 | * @f: the deallocator function for items in the hash |
| 322 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 323 | * Free the hash @table and its contents. The userdata is |
| 324 | * deallocated with @f if provided. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 325 | */ |
| 326 | void |
| 327 | xmlHashFree(xmlHashTablePtr table, xmlHashDeallocator f) { |
| 328 | int i; |
| 329 | xmlHashEntryPtr iter; |
| 330 | xmlHashEntryPtr next; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 331 | int inside_table = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 332 | int nbElems; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 333 | |
| 334 | if (table == NULL) |
| 335 | return; |
| 336 | if (table->table) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 337 | nbElems = table->nbElems; |
| 338 | for(i = 0; (i < table->size) && (nbElems > 0); i++) { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 339 | iter = &(table->table[i]); |
| 340 | if (iter->valid == 0) |
| 341 | continue; |
| 342 | inside_table = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 343 | while (iter) { |
| 344 | next = iter->next; |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 345 | if ((f != NULL) && (iter->payload != NULL)) |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 346 | f(iter->payload, iter->name); |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 347 | if (table->dict == NULL) { |
| 348 | if (iter->name) |
| 349 | xmlFree(iter->name); |
| 350 | if (iter->name2) |
| 351 | xmlFree(iter->name2); |
| 352 | if (iter->name3) |
| 353 | xmlFree(iter->name3); |
| 354 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 355 | iter->payload = NULL; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 356 | if (!inside_table) |
| 357 | xmlFree(iter); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 358 | nbElems--; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 359 | inside_table = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 360 | iter = next; |
| 361 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 362 | } |
| 363 | xmlFree(table->table); |
| 364 | } |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 365 | if (table->dict) |
| 366 | xmlDictFree(table->dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 367 | xmlFree(table); |
| 368 | } |
| 369 | |
| 370 | /** |
Nick Wellnhofer | e03f0a1 | 2017-11-09 16:42:47 +0100 | [diff] [blame] | 371 | * xmlHashDefaultDeallocator: |
| 372 | * @entry: the hash table entry |
| 373 | * @name: the entry's name |
| 374 | * |
| 375 | * Free a hash table entry with xmlFree. |
| 376 | */ |
| 377 | void |
| 378 | xmlHashDefaultDeallocator(void *entry, const xmlChar *name ATTRIBUTE_UNUSED) { |
| 379 | xmlFree(entry); |
| 380 | } |
| 381 | |
| 382 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 383 | * xmlHashAddEntry: |
| 384 | * @table: the hash table |
| 385 | * @name: the name of the userdata |
| 386 | * @userdata: a pointer to the userdata |
| 387 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 388 | * Add the @userdata to the hash @table. This can later be retrieved |
| 389 | * by using the @name. Duplicate names generate errors. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 390 | * |
| 391 | * Returns 0 the addition succeeded and -1 in case of error. |
| 392 | */ |
| 393 | int |
| 394 | xmlHashAddEntry(xmlHashTablePtr table, const xmlChar *name, void *userdata) { |
| 395 | return(xmlHashAddEntry3(table, name, NULL, NULL, userdata)); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * xmlHashAddEntry2: |
| 400 | * @table: the hash table |
| 401 | * @name: the name of the userdata |
| 402 | * @name2: a second name of the userdata |
| 403 | * @userdata: a pointer to the userdata |
| 404 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 405 | * Add the @userdata to the hash @table. This can later be retrieved |
| 406 | * by using the (@name, @name2) tuple. Duplicate tuples generate errors. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 407 | * |
| 408 | * Returns 0 the addition succeeded and -1 in case of error. |
| 409 | */ |
| 410 | int |
| 411 | xmlHashAddEntry2(xmlHashTablePtr table, const xmlChar *name, |
| 412 | const xmlChar *name2, void *userdata) { |
| 413 | return(xmlHashAddEntry3(table, name, name2, NULL, userdata)); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * xmlHashUpdateEntry: |
| 418 | * @table: the hash table |
| 419 | * @name: the name of the userdata |
| 420 | * @userdata: a pointer to the userdata |
| 421 | * @f: the deallocator function for replaced item (if any) |
| 422 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 423 | * Add the @userdata to the hash @table. This can later be retrieved |
| 424 | * by using the @name. Existing entry for this @name will be removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 425 | * and freed with @f if found. |
| 426 | * |
| 427 | * Returns 0 the addition succeeded and -1 in case of error. |
| 428 | */ |
| 429 | int |
| 430 | xmlHashUpdateEntry(xmlHashTablePtr table, const xmlChar *name, |
| 431 | void *userdata, xmlHashDeallocator f) { |
| 432 | return(xmlHashUpdateEntry3(table, name, NULL, NULL, userdata, f)); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * xmlHashUpdateEntry2: |
| 437 | * @table: the hash table |
| 438 | * @name: the name of the userdata |
| 439 | * @name2: a second name of the userdata |
| 440 | * @userdata: a pointer to the userdata |
| 441 | * @f: the deallocator function for replaced item (if any) |
| 442 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 443 | * Add the @userdata to the hash @table. This can later be retrieved |
| 444 | * by using the (@name, @name2) tuple. Existing entry for this tuple will |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 445 | * be removed and freed with @f if found. |
| 446 | * |
| 447 | * Returns 0 the addition succeeded and -1 in case of error. |
| 448 | */ |
| 449 | int |
| 450 | xmlHashUpdateEntry2(xmlHashTablePtr table, const xmlChar *name, |
| 451 | const xmlChar *name2, void *userdata, |
| 452 | xmlHashDeallocator f) { |
| 453 | return(xmlHashUpdateEntry3(table, name, name2, NULL, userdata, f)); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * xmlHashLookup: |
| 458 | * @table: the hash table |
| 459 | * @name: the name of the userdata |
| 460 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 461 | * Find the userdata specified by the @name. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 462 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 463 | * Returns the pointer to the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 464 | */ |
| 465 | void * |
| 466 | xmlHashLookup(xmlHashTablePtr table, const xmlChar *name) { |
| 467 | return(xmlHashLookup3(table, name, NULL, NULL)); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * xmlHashLookup2: |
| 472 | * @table: the hash table |
| 473 | * @name: the name of the userdata |
| 474 | * @name2: a second name of the userdata |
| 475 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 476 | * Find the userdata specified by the (@name, @name2) tuple. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 477 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 478 | * Returns the pointer to the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 479 | */ |
| 480 | void * |
| 481 | xmlHashLookup2(xmlHashTablePtr table, const xmlChar *name, |
| 482 | const xmlChar *name2) { |
| 483 | return(xmlHashLookup3(table, name, name2, NULL)); |
| 484 | } |
| 485 | |
| 486 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 487 | * xmlHashQLookup: |
| 488 | * @table: the hash table |
| 489 | * @prefix: the prefix of the userdata |
| 490 | * @name: the name of the userdata |
| 491 | * |
| 492 | * Find the userdata specified by the QName @prefix:@name/@name. |
| 493 | * |
| 494 | * Returns the pointer to the userdata |
| 495 | */ |
| 496 | void * |
| 497 | xmlHashQLookup(xmlHashTablePtr table, const xmlChar *prefix, |
| 498 | const xmlChar *name) { |
| 499 | return(xmlHashQLookup3(table, prefix, name, NULL, NULL, NULL, NULL)); |
| 500 | } |
| 501 | |
| 502 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 503 | * xmlHashQLookup2: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 504 | * @table: the hash table |
| 505 | * @prefix: the prefix of the userdata |
| 506 | * @name: the name of the userdata |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 507 | * @prefix2: the second prefix of the userdata |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 508 | * @name2: a second name of the userdata |
| 509 | * |
| 510 | * Find the userdata specified by the QNames tuple |
| 511 | * |
| 512 | * Returns the pointer to the userdata |
| 513 | */ |
| 514 | void * |
| 515 | xmlHashQLookup2(xmlHashTablePtr table, const xmlChar *prefix, |
| 516 | const xmlChar *name, const xmlChar *prefix2, |
| 517 | const xmlChar *name2) { |
| 518 | return(xmlHashQLookup3(table, prefix, name, prefix2, name2, NULL, NULL)); |
| 519 | } |
| 520 | |
| 521 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 522 | * xmlHashAddEntry3: |
| 523 | * @table: the hash table |
| 524 | * @name: the name of the userdata |
| 525 | * @name2: a second name of the userdata |
| 526 | * @name3: a third name of the userdata |
| 527 | * @userdata: a pointer to the userdata |
| 528 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 529 | * Add the @userdata to the hash @table. This can later be retrieved |
| 530 | * by using the tuple (@name, @name2, @name3). Duplicate entries generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 531 | * errors. |
| 532 | * |
| 533 | * Returns 0 the addition succeeded and -1 in case of error. |
| 534 | */ |
| 535 | int |
| 536 | xmlHashAddEntry3(xmlHashTablePtr table, const xmlChar *name, |
| 537 | const xmlChar *name2, const xmlChar *name3, |
| 538 | void *userdata) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 539 | unsigned long key, len = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 540 | xmlHashEntryPtr entry; |
| 541 | xmlHashEntryPtr insert; |
| 542 | |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 543 | if ((table == NULL) || (name == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 544 | return(-1); |
| 545 | |
| 546 | /* |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 547 | * If using a dict internalize if needed |
| 548 | */ |
| 549 | if (table->dict) { |
| 550 | if (!xmlDictOwns(table->dict, name)) { |
| 551 | name = xmlDictLookup(table->dict, name, -1); |
| 552 | if (name == NULL) |
| 553 | return(-1); |
| 554 | } |
| 555 | if ((name2 != NULL) && (!xmlDictOwns(table->dict, name2))) { |
| 556 | name2 = xmlDictLookup(table->dict, name2, -1); |
| 557 | if (name2 == NULL) |
| 558 | return(-1); |
| 559 | } |
| 560 | if ((name3 != NULL) && (!xmlDictOwns(table->dict, name3))) { |
| 561 | name3 = xmlDictLookup(table->dict, name3, -1); |
| 562 | if (name3 == NULL) |
| 563 | return(-1); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 568 | * Check for duplicate and insertion location. |
| 569 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 570 | key = xmlHashComputeKey(table, name, name2, name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 571 | if (table->table[key].valid == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 572 | insert = NULL; |
| 573 | } else { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 574 | if (table->dict) { |
| 575 | for (insert = &(table->table[key]); insert->next != NULL; |
| 576 | insert = insert->next) { |
| 577 | if ((insert->name == name) && |
| 578 | (insert->name2 == name2) && |
| 579 | (insert->name3 == name3)) |
| 580 | return(-1); |
| 581 | len++; |
| 582 | } |
| 583 | if ((insert->name == name) && |
| 584 | (insert->name2 == name2) && |
| 585 | (insert->name3 == name3)) |
| 586 | return(-1); |
| 587 | } else { |
| 588 | for (insert = &(table->table[key]); insert->next != NULL; |
| 589 | insert = insert->next) { |
| 590 | if ((xmlStrEqual(insert->name, name)) && |
| 591 | (xmlStrEqual(insert->name2, name2)) && |
| 592 | (xmlStrEqual(insert->name3, name3))) |
| 593 | return(-1); |
| 594 | len++; |
| 595 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 596 | if ((xmlStrEqual(insert->name, name)) && |
| 597 | (xmlStrEqual(insert->name2, name2)) && |
| 598 | (xmlStrEqual(insert->name3, name3))) |
| 599 | return(-1); |
| 600 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 603 | if (insert == NULL) { |
| 604 | entry = &(table->table[key]); |
| 605 | } else { |
| 606 | entry = xmlMalloc(sizeof(xmlHashEntry)); |
| 607 | if (entry == NULL) |
| 608 | return(-1); |
| 609 | } |
| 610 | |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 611 | if (table->dict != NULL) { |
| 612 | entry->name = (xmlChar *) name; |
| 613 | entry->name2 = (xmlChar *) name2; |
| 614 | entry->name3 = (xmlChar *) name3; |
| 615 | } else { |
| 616 | entry->name = xmlStrdup(name); |
| 617 | entry->name2 = xmlStrdup(name2); |
| 618 | entry->name3 = xmlStrdup(name3); |
| 619 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 620 | entry->payload = userdata; |
| 621 | entry->next = NULL; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 622 | entry->valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 623 | |
| 624 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 625 | if (insert != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 626 | insert->next = entry; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 627 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 628 | table->nbElems++; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 629 | |
| 630 | if (len > MAX_HASH_LEN) |
| 631 | xmlHashGrow(table, MAX_HASH_LEN * table->size); |
| 632 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 633 | return(0); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * xmlHashUpdateEntry3: |
| 638 | * @table: the hash table |
| 639 | * @name: the name of the userdata |
| 640 | * @name2: a second name of the userdata |
| 641 | * @name3: a third name of the userdata |
| 642 | * @userdata: a pointer to the userdata |
| 643 | * @f: the deallocator function for replaced item (if any) |
| 644 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 645 | * Add the @userdata to the hash @table. This can later be retrieved |
| 646 | * by using the tuple (@name, @name2, @name3). Existing entry for this tuple |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 647 | * will be removed and freed with @f if found. |
| 648 | * |
| 649 | * Returns 0 the addition succeeded and -1 in case of error. |
| 650 | */ |
| 651 | int |
| 652 | xmlHashUpdateEntry3(xmlHashTablePtr table, const xmlChar *name, |
| 653 | const xmlChar *name2, const xmlChar *name3, |
| 654 | void *userdata, xmlHashDeallocator f) { |
| 655 | unsigned long key; |
| 656 | xmlHashEntryPtr entry; |
| 657 | xmlHashEntryPtr insert; |
| 658 | |
| 659 | if ((table == NULL) || name == NULL) |
| 660 | return(-1); |
| 661 | |
| 662 | /* |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 663 | * If using a dict internalize if needed |
| 664 | */ |
| 665 | if (table->dict) { |
| 666 | if (!xmlDictOwns(table->dict, name)) { |
| 667 | name = xmlDictLookup(table->dict, name, -1); |
| 668 | if (name == NULL) |
| 669 | return(-1); |
| 670 | } |
| 671 | if ((name2 != NULL) && (!xmlDictOwns(table->dict, name2))) { |
| 672 | name2 = xmlDictLookup(table->dict, name2, -1); |
| 673 | if (name2 == NULL) |
| 674 | return(-1); |
| 675 | } |
| 676 | if ((name3 != NULL) && (!xmlDictOwns(table->dict, name3))) { |
| 677 | name3 = xmlDictLookup(table->dict, name3, -1); |
| 678 | if (name3 == NULL) |
| 679 | return(-1); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 684 | * Check for duplicate and insertion location. |
| 685 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 686 | key = xmlHashComputeKey(table, name, name2, name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 687 | if (table->table[key].valid == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 688 | insert = NULL; |
| 689 | } else { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 690 | if (table ->dict) { |
| 691 | for (insert = &(table->table[key]); insert->next != NULL; |
| 692 | insert = insert->next) { |
| 693 | if ((insert->name == name) && |
| 694 | (insert->name2 == name2) && |
| 695 | (insert->name3 == name3)) { |
| 696 | if (f) |
| 697 | f(insert->payload, insert->name); |
| 698 | insert->payload = userdata; |
| 699 | return(0); |
| 700 | } |
| 701 | } |
| 702 | if ((insert->name == name) && |
| 703 | (insert->name2 == name2) && |
| 704 | (insert->name3 == name3)) { |
| 705 | if (f) |
| 706 | f(insert->payload, insert->name); |
| 707 | insert->payload = userdata; |
| 708 | return(0); |
| 709 | } |
| 710 | } else { |
| 711 | for (insert = &(table->table[key]); insert->next != NULL; |
| 712 | insert = insert->next) { |
| 713 | if ((xmlStrEqual(insert->name, name)) && |
| 714 | (xmlStrEqual(insert->name2, name2)) && |
| 715 | (xmlStrEqual(insert->name3, name3))) { |
| 716 | if (f) |
| 717 | f(insert->payload, insert->name); |
| 718 | insert->payload = userdata; |
| 719 | return(0); |
| 720 | } |
| 721 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 722 | if ((xmlStrEqual(insert->name, name)) && |
| 723 | (xmlStrEqual(insert->name2, name2)) && |
| 724 | (xmlStrEqual(insert->name3, name3))) { |
| 725 | if (f) |
| 726 | f(insert->payload, insert->name); |
| 727 | insert->payload = userdata; |
| 728 | return(0); |
| 729 | } |
| 730 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 733 | if (insert == NULL) { |
| 734 | entry = &(table->table[key]); |
| 735 | } else { |
| 736 | entry = xmlMalloc(sizeof(xmlHashEntry)); |
| 737 | if (entry == NULL) |
| 738 | return(-1); |
| 739 | } |
| 740 | |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 741 | if (table->dict != NULL) { |
| 742 | entry->name = (xmlChar *) name; |
| 743 | entry->name2 = (xmlChar *) name2; |
| 744 | entry->name3 = (xmlChar *) name3; |
| 745 | } else { |
| 746 | entry->name = xmlStrdup(name); |
| 747 | entry->name2 = xmlStrdup(name2); |
| 748 | entry->name3 = xmlStrdup(name3); |
| 749 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 750 | entry->payload = userdata; |
| 751 | entry->next = NULL; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 752 | entry->valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 753 | table->nbElems++; |
| 754 | |
| 755 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 756 | if (insert != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 757 | insert->next = entry; |
| 758 | } |
| 759 | return(0); |
| 760 | } |
| 761 | |
| 762 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 763 | * xmlHashLookup3: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 764 | * @table: the hash table |
| 765 | * @name: the name of the userdata |
| 766 | * @name2: a second name of the userdata |
| 767 | * @name3: a third name of the userdata |
| 768 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 769 | * Find the userdata specified by the (@name, @name2, @name3) tuple. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 770 | * |
| 771 | * Returns the a pointer to the userdata |
| 772 | */ |
| 773 | void * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 774 | xmlHashLookup3(xmlHashTablePtr table, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 775 | const xmlChar *name2, const xmlChar *name3) { |
| 776 | unsigned long key; |
| 777 | xmlHashEntryPtr entry; |
| 778 | |
| 779 | if (table == NULL) |
| 780 | return(NULL); |
| 781 | if (name == NULL) |
| 782 | return(NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 783 | key = xmlHashComputeKey(table, name, name2, name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 784 | if (table->table[key].valid == 0) |
| 785 | return(NULL); |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 786 | if (table->dict) { |
| 787 | for (entry = &(table->table[key]); entry != NULL; entry = entry->next) { |
| 788 | if ((entry->name == name) && |
| 789 | (entry->name2 == name2) && |
| 790 | (entry->name3 == name3)) |
| 791 | return(entry->payload); |
| 792 | } |
| 793 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 794 | for (entry = &(table->table[key]); entry != NULL; entry = entry->next) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 795 | if ((xmlStrEqual(entry->name, name)) && |
| 796 | (xmlStrEqual(entry->name2, name2)) && |
| 797 | (xmlStrEqual(entry->name3, name3))) |
| 798 | return(entry->payload); |
| 799 | } |
| 800 | return(NULL); |
| 801 | } |
| 802 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 803 | /** |
| 804 | * xmlHashQLookup3: |
| 805 | * @table: the hash table |
| 806 | * @prefix: the prefix of the userdata |
| 807 | * @name: the name of the userdata |
| 808 | * @prefix2: the second prefix of the userdata |
| 809 | * @name2: a second name of the userdata |
| 810 | * @prefix3: the third prefix of the userdata |
| 811 | * @name3: a third name of the userdata |
| 812 | * |
| 813 | * Find the userdata specified by the (@name, @name2, @name3) tuple. |
| 814 | * |
| 815 | * Returns the a pointer to the userdata |
| 816 | */ |
| 817 | void * |
| 818 | xmlHashQLookup3(xmlHashTablePtr table, |
| 819 | const xmlChar *prefix, const xmlChar *name, |
| 820 | const xmlChar *prefix2, const xmlChar *name2, |
| 821 | const xmlChar *prefix3, const xmlChar *name3) { |
| 822 | unsigned long key; |
| 823 | xmlHashEntryPtr entry; |
| 824 | |
| 825 | if (table == NULL) |
| 826 | return(NULL); |
| 827 | if (name == NULL) |
| 828 | return(NULL); |
| 829 | key = xmlHashComputeQKey(table, prefix, name, prefix2, |
| 830 | name2, prefix3, name3); |
| 831 | if (table->table[key].valid == 0) |
| 832 | return(NULL); |
| 833 | for (entry = &(table->table[key]); entry != NULL; entry = entry->next) { |
| 834 | if ((xmlStrQEqual(prefix, name, entry->name)) && |
| 835 | (xmlStrQEqual(prefix2, name2, entry->name2)) && |
| 836 | (xmlStrQEqual(prefix3, name3, entry->name3))) |
| 837 | return(entry->payload); |
| 838 | } |
| 839 | return(NULL); |
| 840 | } |
| 841 | |
Daniel Veillard | 153120c | 2002-06-18 07:58:35 +0000 | [diff] [blame] | 842 | typedef struct { |
| 843 | xmlHashScanner hashscanner; |
| 844 | void *data; |
| 845 | } stubData; |
| 846 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 847 | static void |
| 848 | stubHashScannerFull (void *payload, void *data, const xmlChar *name, |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 849 | const xmlChar *name2 ATTRIBUTE_UNUSED, |
| 850 | const xmlChar *name3 ATTRIBUTE_UNUSED) { |
Daniel Veillard | 153120c | 2002-06-18 07:58:35 +0000 | [diff] [blame] | 851 | stubData *stubdata = (stubData *) data; |
| 852 | stubdata->hashscanner (payload, stubdata->data, (xmlChar *) name); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 853 | } |
| 854 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 855 | /** |
| 856 | * xmlHashScan: |
| 857 | * @table: the hash table |
| 858 | * @f: the scanner function for items in the hash |
| 859 | * @data: extra data passed to f |
| 860 | * |
| 861 | * Scan the hash @table and applied @f to each value. |
| 862 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 863 | void |
| 864 | xmlHashScan(xmlHashTablePtr table, xmlHashScanner f, void *data) { |
Daniel Veillard | 153120c | 2002-06-18 07:58:35 +0000 | [diff] [blame] | 865 | stubData stubdata; |
| 866 | stubdata.data = data; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 867 | stubdata.hashscanner = f; |
Daniel Veillard | 153120c | 2002-06-18 07:58:35 +0000 | [diff] [blame] | 868 | xmlHashScanFull (table, stubHashScannerFull, &stubdata); |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /** |
| 872 | * xmlHashScanFull: |
| 873 | * @table: the hash table |
| 874 | * @f: the scanner function for items in the hash |
| 875 | * @data: extra data passed to f |
| 876 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 877 | * Scan the hash @table and applied @f to each value. |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 878 | */ |
| 879 | void |
| 880 | xmlHashScanFull(xmlHashTablePtr table, xmlHashScannerFull f, void *data) { |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 881 | int i, nb; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 882 | xmlHashEntryPtr iter; |
| 883 | xmlHashEntryPtr next; |
| 884 | |
| 885 | if (table == NULL) |
| 886 | return; |
| 887 | if (f == NULL) |
| 888 | return; |
| 889 | |
| 890 | if (table->table) { |
| 891 | for(i = 0; i < table->size; i++) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 892 | if (table->table[i].valid == 0) |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 893 | continue; |
| 894 | iter = &(table->table[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 895 | while (iter) { |
| 896 | next = iter->next; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 897 | nb = table->nbElems; |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 898 | if ((f != NULL) && (iter->payload != NULL)) |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 899 | f(iter->payload, data, iter->name, |
| 900 | iter->name2, iter->name3); |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 901 | if (nb != table->nbElems) { |
| 902 | /* table was modified by the callback, be careful */ |
| 903 | if (iter == &(table->table[i])) { |
| 904 | if (table->table[i].valid == 0) |
| 905 | iter = NULL; |
| 906 | if (table->table[i].next != next) |
| 907 | iter = &(table->table[i]); |
| 908 | } else |
| 909 | iter = next; |
| 910 | } else |
| 911 | iter = next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * xmlHashScan3: |
| 919 | * @table: the hash table |
| 920 | * @name: the name of the userdata or NULL |
| 921 | * @name2: a second name of the userdata or NULL |
| 922 | * @name3: a third name of the userdata or NULL |
| 923 | * @f: the scanner function for items in the hash |
| 924 | * @data: extra data passed to f |
| 925 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 926 | * Scan the hash @table and applied @f to each value matching |
| 927 | * (@name, @name2, @name3) tuple. If one of the names is null, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 928 | * the comparison is considered to match. |
| 929 | */ |
| 930 | void |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 931 | xmlHashScan3(xmlHashTablePtr table, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 932 | const xmlChar *name2, const xmlChar *name3, |
| 933 | xmlHashScanner f, void *data) { |
Nick Wellnhofer | e03f0a1 | 2017-11-09 16:42:47 +0100 | [diff] [blame] | 934 | stubData stubdata; |
| 935 | stubdata.data = data; |
| 936 | stubdata.hashscanner = f; |
| 937 | xmlHashScanFull3(table, name, name2, name3, stubHashScannerFull, |
| 938 | &stubdata); |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /** |
| 942 | * xmlHashScanFull3: |
| 943 | * @table: the hash table |
| 944 | * @name: the name of the userdata or NULL |
| 945 | * @name2: a second name of the userdata or NULL |
| 946 | * @name3: a third name of the userdata or NULL |
| 947 | * @f: the scanner function for items in the hash |
| 948 | * @data: extra data passed to f |
| 949 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 950 | * Scan the hash @table and applied @f to each value matching |
| 951 | * (@name, @name2, @name3) tuple. If one of the names is null, |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 952 | * the comparison is considered to match. |
| 953 | */ |
| 954 | void |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 955 | xmlHashScanFull3(xmlHashTablePtr table, const xmlChar *name, |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 956 | const xmlChar *name2, const xmlChar *name3, |
| 957 | xmlHashScannerFull f, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 958 | int i; |
| 959 | xmlHashEntryPtr iter; |
| 960 | xmlHashEntryPtr next; |
| 961 | |
| 962 | if (table == NULL) |
| 963 | return; |
| 964 | if (f == NULL) |
| 965 | return; |
| 966 | |
| 967 | if (table->table) { |
| 968 | for(i = 0; i < table->size; i++) { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 969 | if (table->table[i].valid == 0) |
| 970 | continue; |
| 971 | iter = &(table->table[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 972 | while (iter) { |
| 973 | next = iter->next; |
| 974 | if (((name == NULL) || (xmlStrEqual(name, iter->name))) && |
| 975 | ((name2 == NULL) || (xmlStrEqual(name2, iter->name2))) && |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 976 | ((name3 == NULL) || (xmlStrEqual(name3, iter->name3))) && |
| 977 | (iter->payload != NULL)) { |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 978 | f(iter->payload, data, iter->name, |
| 979 | iter->name2, iter->name3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 980 | } |
| 981 | iter = next; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * xmlHashCopy: |
| 989 | * @table: the hash table |
| 990 | * @f: the copier function for items in the hash |
| 991 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 992 | * Scan the hash @table and applied @f to each value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 993 | * |
| 994 | * Returns the new table or NULL in case of error. |
| 995 | */ |
| 996 | xmlHashTablePtr |
| 997 | xmlHashCopy(xmlHashTablePtr table, xmlHashCopier f) { |
| 998 | int i; |
| 999 | xmlHashEntryPtr iter; |
| 1000 | xmlHashEntryPtr next; |
| 1001 | xmlHashTablePtr ret; |
| 1002 | |
| 1003 | if (table == NULL) |
| 1004 | return(NULL); |
| 1005 | if (f == NULL) |
| 1006 | return(NULL); |
| 1007 | |
| 1008 | ret = xmlHashCreate(table->size); |
Gaurav Gupta | 1811add | 2014-07-14 17:50:27 +0800 | [diff] [blame] | 1009 | if (ret == NULL) |
| 1010 | return(NULL); |
| 1011 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1012 | if (table->table) { |
| 1013 | for(i = 0; i < table->size; i++) { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1014 | if (table->table[i].valid == 0) |
| 1015 | continue; |
| 1016 | iter = &(table->table[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1017 | while (iter) { |
| 1018 | next = iter->next; |
| 1019 | xmlHashAddEntry3(ret, iter->name, iter->name2, |
| 1020 | iter->name3, f(iter->payload, iter->name)); |
| 1021 | iter = next; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | ret->nbElems = table->nbElems; |
| 1026 | return(ret); |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * xmlHashSize: |
| 1031 | * @table: the hash table |
| 1032 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1033 | * Query the number of elements installed in the hash @table. |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1034 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1035 | * Returns the number of elements in the hash table or |
| 1036 | * -1 in case of error |
| 1037 | */ |
| 1038 | int |
| 1039 | xmlHashSize(xmlHashTablePtr table) { |
| 1040 | if (table == NULL) |
| 1041 | return(-1); |
| 1042 | return(table->nbElems); |
| 1043 | } |
| 1044 | |
| 1045 | /** |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 1046 | * xmlHashRemoveEntry: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1047 | * @table: the hash table |
| 1048 | * @name: the name of the userdata |
| 1049 | * @f: the deallocator function for removed item (if any) |
| 1050 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1051 | * Find the userdata specified by the @name and remove |
| 1052 | * it from the hash @table. Existing userdata for this tuple will be removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1053 | * and freed with @f. |
| 1054 | * |
| 1055 | * Returns 0 if the removal succeeded and -1 in case of error or not found. |
| 1056 | */ |
| 1057 | int xmlHashRemoveEntry(xmlHashTablePtr table, const xmlChar *name, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1058 | xmlHashDeallocator f) { |
| 1059 | return(xmlHashRemoveEntry3(table, name, NULL, NULL, f)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /** |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 1063 | * xmlHashRemoveEntry2: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1064 | * @table: the hash table |
| 1065 | * @name: the name of the userdata |
| 1066 | * @name2: a second name of the userdata |
| 1067 | * @f: the deallocator function for removed item (if any) |
| 1068 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1069 | * Find the userdata specified by the (@name, @name2) tuple and remove |
| 1070 | * it from the hash @table. Existing userdata for this tuple will be removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1071 | * and freed with @f. |
| 1072 | * |
| 1073 | * Returns 0 if the removal succeeded and -1 in case of error or not found. |
| 1074 | */ |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1075 | int |
| 1076 | xmlHashRemoveEntry2(xmlHashTablePtr table, const xmlChar *name, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1077 | const xmlChar *name2, xmlHashDeallocator f) { |
| 1078 | return(xmlHashRemoveEntry3(table, name, name2, NULL, f)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1082 | * xmlHashRemoveEntry3: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1083 | * @table: the hash table |
| 1084 | * @name: the name of the userdata |
| 1085 | * @name2: a second name of the userdata |
| 1086 | * @name3: a third name of the userdata |
| 1087 | * @f: the deallocator function for removed item (if any) |
| 1088 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1089 | * Find the userdata specified by the (@name, @name2, @name3) tuple and remove |
| 1090 | * it from the hash @table. Existing userdata for this tuple will be removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1091 | * and freed with @f. |
| 1092 | * |
| 1093 | * Returns 0 if the removal succeeded and -1 in case of error or not found. |
| 1094 | */ |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1095 | int |
| 1096 | xmlHashRemoveEntry3(xmlHashTablePtr table, const xmlChar *name, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1097 | const xmlChar *name2, const xmlChar *name3, xmlHashDeallocator f) { |
| 1098 | unsigned long key; |
| 1099 | xmlHashEntryPtr entry; |
| 1100 | xmlHashEntryPtr prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1101 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1102 | if (table == NULL || name == NULL) |
| 1103 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1104 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1105 | key = xmlHashComputeKey(table, name, name2, name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1106 | if (table->table[key].valid == 0) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1107 | return(-1); |
| 1108 | } else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1109 | for (entry = &(table->table[key]); entry != NULL; entry = entry->next) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1110 | if (xmlStrEqual(entry->name, name) && |
| 1111 | xmlStrEqual(entry->name2, name2) && |
| 1112 | xmlStrEqual(entry->name3, name3)) { |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 1113 | if ((f != NULL) && (entry->payload != NULL)) |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1114 | f(entry->payload, entry->name); |
| 1115 | entry->payload = NULL; |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1116 | if (table->dict == NULL) { |
| 1117 | if(entry->name) |
| 1118 | xmlFree(entry->name); |
| 1119 | if(entry->name2) |
| 1120 | xmlFree(entry->name2); |
| 1121 | if(entry->name3) |
| 1122 | xmlFree(entry->name3); |
| 1123 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1124 | if(prev) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1125 | prev->next = entry->next; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1126 | xmlFree(entry); |
| 1127 | } else { |
| 1128 | if (entry->next == NULL) { |
| 1129 | entry->valid = 0; |
| 1130 | } else { |
| 1131 | entry = entry->next; |
| 1132 | memcpy(&(table->table[key]), entry, sizeof(xmlHashEntry)); |
| 1133 | xmlFree(entry); |
| 1134 | } |
| 1135 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 1136 | table->nbElems--; |
| 1137 | return(0); |
| 1138 | } |
| 1139 | prev = entry; |
| 1140 | } |
| 1141 | return(-1); |
| 1142 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |