Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * entities.c : implementation for the XML entities handking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 6 | * Daniel.Veillard@w3.org |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 9 | #ifndef WIN32 |
| 10 | #include "config.h" |
| 11 | #endif |
| 12 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 14 | #include <string.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 15 | #ifdef HAVE_STDLIB_H |
| 16 | #include <stdlib.h> |
| 17 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 18 | #include "xmlmemory.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 19 | #include "entities.h" |
Daniel Veillard | a0555cc | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 20 | #include "parser.h" |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * The XML predefined entities. |
| 24 | */ |
| 25 | |
| 26 | struct xmlPredefinedEntityValue { |
| 27 | const char *name; |
| 28 | const char *value; |
| 29 | }; |
| 30 | struct xmlPredefinedEntityValue xmlPredefinedEntityValues[] = { |
| 31 | { "lt", "<" }, |
| 32 | { "gt", ">" }, |
| 33 | { "apos", "'" }, |
| 34 | { "quot", "\"" }, |
| 35 | { "amp", "&" } |
| 36 | }; |
| 37 | |
| 38 | xmlEntitiesTablePtr xmlPredefinedEntities = NULL; |
| 39 | |
| 40 | /* |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 41 | * xmlFreeEntity : clean-up an entity record. |
| 42 | */ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 43 | void xmlFreeEntity(xmlEntityPtr entity) { |
| 44 | if (entity == NULL) return; |
| 45 | |
| 46 | if (entity->name != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 47 | xmlFree((char *) entity->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 48 | if (entity->ExternalID != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 49 | xmlFree((char *) entity->ExternalID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 50 | if (entity->SystemID != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 51 | xmlFree((char *) entity->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 52 | if (entity->content != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 53 | xmlFree((char *) entity->content); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 54 | if (entity->orig != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 55 | xmlFree((char *) entity->orig); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 56 | memset(entity, -1, sizeof(xmlEntity)); |
| 57 | } |
| 58 | |
| 59 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 60 | * xmlAddEntity : register a new entity for an entities table. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 61 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 62 | static void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 63 | xmlAddEntity(xmlEntitiesTablePtr table, const xmlChar *name, int type, |
| 64 | const xmlChar *ExternalID, const xmlChar *SystemID, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 65 | int i; |
| 66 | xmlEntityPtr cur; |
| 67 | int len; |
| 68 | |
| 69 | for (i = 0;i < table->nb_entities;i++) { |
| 70 | cur = &table->table[i]; |
| 71 | if (!xmlStrcmp(cur->name, name)) { |
| 72 | /* |
| 73 | * The entity is already defined in this Dtd, the spec says to NOT |
| 74 | * override it ... Is it worth a Warning ??? !!! |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 75 | * Not having a cprinting context this seems hard ... |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 76 | */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 77 | if (((type == XML_INTERNAL_PARAMETER_ENTITY) || |
| 78 | (type == XML_EXTERNAL_PARAMETER_ENTITY)) && |
| 79 | ((cur->type == XML_INTERNAL_PARAMETER_ENTITY) || |
| 80 | (cur->type == XML_EXTERNAL_PARAMETER_ENTITY))) |
| 81 | return; |
| 82 | else |
| 83 | if (((type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 84 | (type != XML_EXTERNAL_PARAMETER_ENTITY)) && |
| 85 | ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 86 | (cur->type != XML_EXTERNAL_PARAMETER_ENTITY))) |
| 87 | return; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | if (table->nb_entities >= table->max_entities) { |
| 91 | /* |
| 92 | * need more elements. |
| 93 | */ |
| 94 | table->max_entities *= 2; |
| 95 | table->table = (xmlEntityPtr) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 96 | xmlRealloc(table->table, table->max_entities * sizeof(xmlEntity)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 97 | if (table->table == NULL) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 98 | perror("realloc failed"); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 99 | return; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | cur = &table->table[table->nb_entities]; |
| 103 | cur->name = xmlStrdup(name); |
| 104 | for (len = 0;name[0] != 0;name++)len++; |
| 105 | cur->len = len; |
| 106 | cur->type = type; |
| 107 | if (ExternalID != NULL) |
| 108 | cur->ExternalID = xmlStrdup(ExternalID); |
| 109 | else |
| 110 | cur->ExternalID = NULL; |
| 111 | if (SystemID != NULL) |
| 112 | cur->SystemID = xmlStrdup(SystemID); |
| 113 | else |
| 114 | cur->SystemID = NULL; |
| 115 | if (content != NULL) |
| 116 | cur->content = xmlStrdup(content); |
| 117 | else |
| 118 | cur->content = NULL; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 119 | cur->orig = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 120 | table->nb_entities++; |
| 121 | } |
| 122 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 123 | /** |
| 124 | * xmlInitializePredefinedEntities: |
| 125 | * |
| 126 | * Set up the predefined entities. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 127 | */ |
| 128 | void xmlInitializePredefinedEntities(void) { |
| 129 | int i; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 130 | xmlChar name[50]; |
| 131 | xmlChar value[50]; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 132 | const char *in; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 133 | xmlChar *out; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 134 | |
| 135 | if (xmlPredefinedEntities != NULL) return; |
| 136 | |
| 137 | xmlPredefinedEntities = xmlCreateEntitiesTable(); |
| 138 | for (i = 0;i < sizeof(xmlPredefinedEntityValues) / |
| 139 | sizeof(xmlPredefinedEntityValues[0]);i++) { |
| 140 | in = xmlPredefinedEntityValues[i].name; |
| 141 | out = &name[0]; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 142 | for (;(*out++ = (xmlChar) *in);)in++; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 143 | in = xmlPredefinedEntityValues[i].value; |
| 144 | out = &value[0]; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 145 | for (;(*out++ = (xmlChar) *in);)in++; |
| 146 | xmlAddEntity(xmlPredefinedEntities, (const xmlChar *) &name[0], |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 147 | XML_INTERNAL_PREDEFINED_ENTITY, NULL, NULL, |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 148 | &value[0]); |
| 149 | } |
| 150 | } |
| 151 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 152 | /** |
Daniel Veillard | a594bf4 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 153 | * xmlCleanupPredefinedEntities: |
| 154 | * |
| 155 | * Cleanup up the predefined entities table. |
| 156 | */ |
| 157 | void xmlCleanupPredefinedEntities(void) { |
| 158 | if (xmlPredefinedEntities == NULL) return; |
| 159 | |
| 160 | xmlFreeEntitiesTable(xmlPredefinedEntities); |
| 161 | xmlPredefinedEntities = NULL; |
| 162 | } |
| 163 | |
| 164 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 165 | * xmlGetPredefinedEntity: |
| 166 | * @name: the entity name |
| 167 | * |
| 168 | * Check whether this name is an predefined entity. |
| 169 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 170 | * Returns NULL if not, othervise the entity |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 171 | */ |
| 172 | xmlEntityPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 173 | xmlGetPredefinedEntity(const xmlChar *name) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 174 | int i; |
| 175 | xmlEntityPtr cur; |
| 176 | |
| 177 | if (xmlPredefinedEntities == NULL) |
| 178 | xmlInitializePredefinedEntities(); |
| 179 | for (i = 0;i < xmlPredefinedEntities->nb_entities;i++) { |
| 180 | cur = &xmlPredefinedEntities->table[i]; |
| 181 | if (!xmlStrcmp(cur->name, name)) return(cur); |
| 182 | } |
| 183 | return(NULL); |
| 184 | } |
| 185 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 186 | /** |
| 187 | * xmlAddDtdEntity: |
| 188 | * @doc: the document |
| 189 | * @name: the entity name |
| 190 | * @type: the entity type XML_xxx_yyy_ENTITY |
| 191 | * @ExternalID: the entity external ID if available |
| 192 | * @SystemID: the entity system ID if available |
| 193 | * @content: the entity content |
| 194 | * |
| 195 | * Register a new entity for this document DTD. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 196 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 197 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 198 | xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type, |
| 199 | const xmlChar *ExternalID, const xmlChar *SystemID, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 200 | xmlEntitiesTablePtr table; |
| 201 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 202 | if (doc->extSubset == NULL) { |
| 203 | fprintf(stderr, |
| 204 | "xmlAddDtdEntity: document without external subset !\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 205 | return; |
| 206 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 207 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 208 | if (table == NULL) { |
| 209 | table = xmlCreateEntitiesTable(); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 210 | doc->extSubset->entities = table; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 211 | } |
| 212 | xmlAddEntity(table, name, type, ExternalID, SystemID, content); |
| 213 | } |
| 214 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 215 | /** |
| 216 | * xmlAddDocEntity: |
| 217 | * @doc: the document |
| 218 | * @name: the entity name |
| 219 | * @type: the entity type XML_xxx_yyy_ENTITY |
| 220 | * @ExternalID: the entity external ID if available |
| 221 | * @SystemID: the entity system ID if available |
| 222 | * @content: the entity content |
| 223 | * |
| 224 | * Register a new entity for this document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 225 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 226 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 227 | xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type, |
| 228 | const xmlChar *ExternalID, const xmlChar *SystemID, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 229 | xmlEntitiesTablePtr table; |
| 230 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 231 | if (doc == NULL) { |
| 232 | fprintf(stderr, |
| 233 | "xmlAddDocEntity: document is NULL !\n"); |
| 234 | return; |
| 235 | } |
| 236 | if (doc->intSubset == NULL) { |
| 237 | fprintf(stderr, |
| 238 | "xmlAddDtdEntity: document without internal subset !\n"); |
| 239 | return; |
| 240 | } |
| 241 | table = (xmlEntitiesTablePtr) doc->intSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 242 | if (table == NULL) { |
| 243 | table = xmlCreateEntitiesTable(); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 244 | doc->intSubset->entities = table; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 245 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 246 | xmlAddEntity(table, name, type, ExternalID, SystemID, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 249 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 250 | * xmlGetParameterEntity: |
| 251 | * @doc: the document referencing the entity |
| 252 | * @name: the entity name |
| 253 | * |
| 254 | * Do an entity lookup in the internal and external subsets and |
| 255 | * returns the corresponding parameter entity, if found. |
| 256 | * |
| 257 | * Returns A pointer to the entity structure or NULL if not found. |
| 258 | */ |
| 259 | xmlEntityPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 260 | xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 261 | int i; |
| 262 | xmlEntityPtr cur; |
| 263 | xmlEntitiesTablePtr table; |
| 264 | |
| 265 | if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { |
| 266 | table = (xmlEntitiesTablePtr) doc->intSubset->entities; |
| 267 | for (i = 0;i < table->nb_entities;i++) { |
| 268 | cur = &table->table[i]; |
| 269 | if (((cur->type == XML_INTERNAL_PARAMETER_ENTITY) || |
| 270 | (cur->type == XML_EXTERNAL_PARAMETER_ENTITY)) && |
| 271 | (!xmlStrcmp(cur->name, name))) return(cur); |
| 272 | } |
| 273 | } |
| 274 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 275 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
| 276 | for (i = 0;i < table->nb_entities;i++) { |
| 277 | cur = &table->table[i]; |
| 278 | if (((cur->type == XML_INTERNAL_PARAMETER_ENTITY) || |
| 279 | (cur->type == XML_EXTERNAL_PARAMETER_ENTITY)) && |
| 280 | (!xmlStrcmp(cur->name, name))) return(cur); |
| 281 | } |
| 282 | } |
Daniel Veillard | 1ff7ae3 | 1999-09-01 12:19:13 +0000 | [diff] [blame] | 283 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 284 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
| 285 | for (i = 0;i < table->nb_entities;i++) { |
| 286 | cur = &table->table[i]; |
| 287 | if (((cur->type == XML_INTERNAL_PARAMETER_ENTITY) || |
| 288 | (cur->type == XML_EXTERNAL_PARAMETER_ENTITY)) && |
| 289 | (!xmlStrcmp(cur->name, name))) return(cur); |
| 290 | } |
| 291 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 292 | return(NULL); |
| 293 | } |
| 294 | |
| 295 | /** |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 296 | * xmlGetDtdEntity: |
| 297 | * @doc: the document referencing the entity |
| 298 | * @name: the entity name |
| 299 | * |
| 300 | * Do an entity lookup in the Dtd entity hash table and |
| 301 | * returns the corresponding entity, if found. |
| 302 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 303 | * Returns A pointer to the entity structure or NULL if not found. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 304 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 305 | xmlEntityPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 306 | xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 307 | int i; |
| 308 | xmlEntityPtr cur; |
| 309 | xmlEntitiesTablePtr table; |
| 310 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 311 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 312 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 313 | for (i = 0;i < table->nb_entities;i++) { |
| 314 | cur = &table->table[i]; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 315 | if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 316 | (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 317 | (!xmlStrcmp(cur->name, name))) return(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | return(NULL); |
| 321 | } |
| 322 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 323 | /** |
| 324 | * xmlGetDocEntity: |
| 325 | * @doc: the document referencing the entity |
| 326 | * @name: the entity name |
| 327 | * |
| 328 | * Do an entity lookup in the document entity hash table and |
| 329 | * returns the corrsponding entity, otherwise a lookup is done |
| 330 | * in the predefined entities too. |
| 331 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 332 | * Returns A pointer to the entity structure or NULL if not found. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 333 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 334 | xmlEntityPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 335 | xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 336 | int i; |
| 337 | xmlEntityPtr cur; |
| 338 | xmlEntitiesTablePtr table; |
| 339 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 340 | if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { |
| 341 | table = (xmlEntitiesTablePtr) doc->intSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 342 | for (i = 0;i < table->nb_entities;i++) { |
| 343 | cur = &table->table[i]; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 344 | if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 345 | (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 346 | (!xmlStrcmp(cur->name, name))) return(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
Daniel Veillard | 1ff7ae3 | 1999-09-01 12:19:13 +0000 | [diff] [blame] | 349 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 350 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
| 351 | for (i = 0;i < table->nb_entities;i++) { |
| 352 | cur = &table->table[i]; |
| 353 | if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 354 | (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 355 | (!xmlStrcmp(cur->name, name))) return(cur); |
| 356 | } |
| 357 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 358 | if (xmlPredefinedEntities == NULL) |
| 359 | xmlInitializePredefinedEntities(); |
| 360 | table = xmlPredefinedEntities; |
| 361 | for (i = 0;i < table->nb_entities;i++) { |
| 362 | cur = &table->table[i]; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 363 | if ((cur->type != XML_INTERNAL_PARAMETER_ENTITY) && |
| 364 | (cur->type != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 365 | (!xmlStrcmp(cur->name, name))) return(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | return(NULL); |
| 369 | } |
| 370 | |
| 371 | /* |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 372 | * [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
| 373 | * | [#x10000-#x10FFFF] |
| 374 | * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. |
| 375 | */ |
| 376 | #define IS_CHAR(c) \ |
| 377 | (((c) == 0x09) || ((c) == 0x0a) || ((c) == 0x0d) || \ |
| 378 | (((c) >= 0x20) && ((c) != 0xFFFE) && ((c) != 0xFFFF))) |
| 379 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 380 | /* |
| 381 | * A buffer used for converting entities to their equivalent and back. |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 382 | */ |
| 383 | static int buffer_size = 0; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 384 | static xmlChar *buffer = NULL; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 385 | |
| 386 | void growBuffer(void) { |
| 387 | buffer_size *= 2; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 388 | buffer = (xmlChar *) xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 389 | if (buffer == NULL) { |
| 390 | perror("realloc failed"); |
| 391 | exit(1); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 396 | /** |
| 397 | * xmlEncodeEntities: |
| 398 | * @doc: the document containing the string |
| 399 | * @input: A string to convert to XML. |
| 400 | * |
| 401 | * Do a global encoding of a string, replacing the predefined entities |
| 402 | * and non ASCII values with their entities and CharRef counterparts. |
| 403 | * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 404 | * TODO: remove xmlEncodeEntities, once we are not afraid of breaking binary |
| 405 | * compatibility |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 406 | * |
| 407 | * People must migrate their code to xmlEncodeEntitiesReentrant ! |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 408 | * This routine will issue a warning when encountered. |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 409 | * |
| 410 | * Returns A newly allocated string with the substitution done. |
| 411 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 412 | const xmlChar * |
| 413 | xmlEncodeEntities(xmlDocPtr doc, const xmlChar *input) { |
| 414 | const xmlChar *cur = input; |
| 415 | xmlChar *out = buffer; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 416 | static int warning = 1; |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 417 | int html = 0; |
| 418 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 419 | |
| 420 | if (warning) { |
| 421 | fprintf(stderr, "Deprecated API xmlEncodeEntities() used\n"); |
| 422 | fprintf(stderr, " change code to use xmlEncodeEntitiesReentrant()\n"); |
| 423 | warning = 0; |
| 424 | } |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 425 | |
| 426 | if (input == NULL) return(NULL); |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 427 | if (doc != NULL) |
| 428 | html = (doc->type == XML_HTML_DOCUMENT_NODE); |
| 429 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 430 | if (buffer == NULL) { |
| 431 | buffer_size = 1000; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 432 | buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar)); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 433 | if (buffer == NULL) { |
| 434 | perror("malloc failed"); |
| 435 | exit(1); |
| 436 | } |
| 437 | out = buffer; |
| 438 | } |
| 439 | while (*cur != '\0') { |
| 440 | if (out - buffer > buffer_size - 100) { |
| 441 | int index = out - buffer; |
| 442 | |
| 443 | growBuffer(); |
| 444 | out = &buffer[index]; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * By default one have to encode at least '<', '>', '"' and '&' ! |
| 449 | */ |
| 450 | if (*cur == '<') { |
| 451 | *out++ = '&'; |
| 452 | *out++ = 'l'; |
| 453 | *out++ = 't'; |
| 454 | *out++ = ';'; |
| 455 | } else if (*cur == '>') { |
| 456 | *out++ = '&'; |
| 457 | *out++ = 'g'; |
| 458 | *out++ = 't'; |
| 459 | *out++ = ';'; |
| 460 | } else if (*cur == '&') { |
| 461 | *out++ = '&'; |
| 462 | *out++ = 'a'; |
| 463 | *out++ = 'm'; |
| 464 | *out++ = 'p'; |
| 465 | *out++ = ';'; |
| 466 | } else if (*cur == '"') { |
| 467 | *out++ = '&'; |
| 468 | *out++ = 'q'; |
| 469 | *out++ = 'u'; |
| 470 | *out++ = 'o'; |
| 471 | *out++ = 't'; |
| 472 | *out++ = ';'; |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 473 | } else if ((*cur == '\'') && (!html)) { |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 474 | *out++ = '&'; |
| 475 | *out++ = 'a'; |
| 476 | *out++ = 'p'; |
| 477 | *out++ = 'o'; |
| 478 | *out++ = 's'; |
| 479 | *out++ = ';'; |
| 480 | } else if (((*cur >= 0x20) && (*cur < 0x80)) || |
| 481 | (*cur == '\n') || (*cur == '\r') || (*cur == '\t')) { |
| 482 | /* |
| 483 | * default case, just copy ! |
| 484 | */ |
| 485 | *out++ = *cur; |
| 486 | #ifndef USE_UTF_8 |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 487 | } else if ((sizeof(xmlChar) == 1) && (*cur >= 0x80)) { |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 488 | char buf[10], *ptr; |
| 489 | #ifdef HAVE_SNPRINTF |
| 490 | snprintf(buf, 9, "&#%d;", *cur); |
| 491 | #else |
| 492 | sprintf(buf, "&#%d;", *cur); |
| 493 | #endif |
| 494 | ptr = buf; |
| 495 | while (*ptr != 0) *out++ = *ptr++; |
| 496 | #endif |
| 497 | } else if (IS_CHAR(*cur)) { |
| 498 | char buf[10], *ptr; |
| 499 | |
| 500 | #ifdef HAVE_SNPRINTF |
| 501 | snprintf(buf, 9, "&#%d;", *cur); |
| 502 | #else |
| 503 | sprintf(buf, "&#%d;", *cur); |
| 504 | #endif |
| 505 | ptr = buf; |
| 506 | while (*ptr != 0) *out++ = *ptr++; |
| 507 | } |
| 508 | #if 0 |
| 509 | else { |
| 510 | /* |
| 511 | * default case, this is not a valid char ! |
| 512 | * Skip it... |
| 513 | */ |
| 514 | fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur); |
| 515 | } |
| 516 | #endif |
| 517 | cur++; |
| 518 | } |
| 519 | *out++ = 0; |
| 520 | return(buffer); |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * Macro used to grow the current buffer. |
| 525 | */ |
| 526 | #define growBufferReentrant() { \ |
| 527 | buffer_size *= 2; \ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 528 | buffer = (xmlChar *) xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \ |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 529 | if (buffer == NULL) { \ |
| 530 | perror("realloc failed"); \ |
| 531 | exit(1); \ |
| 532 | } \ |
| 533 | } |
| 534 | |
| 535 | |
| 536 | /** |
| 537 | * xmlEncodeEntitiesReentrant: |
| 538 | * @doc: the document containing the string |
| 539 | * @input: A string to convert to XML. |
| 540 | * |
| 541 | * Do a global encoding of a string, replacing the predefined entities |
| 542 | * and non ASCII values with their entities and CharRef counterparts. |
| 543 | * Contrary to xmlEncodeEntities, this routine is reentrant, and result |
| 544 | * must be deallocated. |
| 545 | * |
| 546 | * TODO !!!! Once moved to UTF-8 internal encoding, the encoding of non-ascii |
| 547 | * get erroneous. |
| 548 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 549 | * Returns A newly allocated string with the substitution done. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 550 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 551 | xmlChar * |
| 552 | xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) { |
| 553 | const xmlChar *cur = input; |
| 554 | xmlChar *buffer = NULL; |
| 555 | xmlChar *out = NULL; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 556 | int buffer_size = 0; |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 557 | int html = 0; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 558 | |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 559 | if (input == NULL) return(NULL); |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 560 | if (doc != NULL) |
| 561 | html = (doc->type == XML_HTML_DOCUMENT_NODE); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 562 | |
| 563 | /* |
| 564 | * allocate an translation buffer. |
| 565 | */ |
| 566 | buffer_size = 1000; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 567 | buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 568 | if (buffer == NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 569 | perror("malloc failed"); |
| 570 | exit(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 571 | } |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 572 | out = buffer; |
| 573 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 574 | while (*cur != '\0') { |
| 575 | if (out - buffer > buffer_size - 100) { |
| 576 | int index = out - buffer; |
| 577 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 578 | growBufferReentrant(); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 579 | out = &buffer[index]; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * By default one have to encode at least '<', '>', '"' and '&' ! |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 584 | */ |
| 585 | if (*cur == '<') { |
| 586 | *out++ = '&'; |
| 587 | *out++ = 'l'; |
| 588 | *out++ = 't'; |
| 589 | *out++ = ';'; |
| 590 | } else if (*cur == '>') { |
| 591 | *out++ = '&'; |
| 592 | *out++ = 'g'; |
| 593 | *out++ = 't'; |
| 594 | *out++ = ';'; |
| 595 | } else if (*cur == '&') { |
| 596 | *out++ = '&'; |
| 597 | *out++ = 'a'; |
| 598 | *out++ = 'm'; |
| 599 | *out++ = 'p'; |
| 600 | *out++ = ';'; |
| 601 | } else if (*cur == '"') { |
| 602 | *out++ = '&'; |
| 603 | *out++ = 'q'; |
| 604 | *out++ = 'u'; |
| 605 | *out++ = 'o'; |
| 606 | *out++ = 't'; |
| 607 | *out++ = ';'; |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 608 | } else if ((*cur == '\'') && (!html)) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 609 | *out++ = '&'; |
| 610 | *out++ = 'a'; |
| 611 | *out++ = 'p'; |
| 612 | *out++ = 'o'; |
| 613 | *out++ = 's'; |
| 614 | *out++ = ';'; |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 615 | } else if (((*cur >= 0x20) && (*cur < 0x80)) || |
| 616 | (*cur == '\n') || (*cur == '\r') || (*cur == '\t')) { |
| 617 | /* |
| 618 | * default case, just copy ! |
| 619 | */ |
| 620 | *out++ = *cur; |
Daniel Veillard | 0ba4d53 | 1998-11-01 19:34:31 +0000 | [diff] [blame] | 621 | #ifndef USE_UTF_8 |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 622 | } else if ((sizeof(xmlChar) == 1) && (*cur >= 0x80)) { |
Daniel Veillard | 0ba4d53 | 1998-11-01 19:34:31 +0000 | [diff] [blame] | 623 | char buf[10], *ptr; |
Daniel Veillard | da4d3c4 | 1998-11-04 20:07:05 +0000 | [diff] [blame] | 624 | #ifdef HAVE_SNPRINTF |
| 625 | snprintf(buf, 9, "&#%d;", *cur); |
| 626 | #else |
| 627 | sprintf(buf, "&#%d;", *cur); |
| 628 | #endif |
Daniel Veillard | 0ba4d53 | 1998-11-01 19:34:31 +0000 | [diff] [blame] | 629 | ptr = buf; |
| 630 | while (*ptr != 0) *out++ = *ptr++; |
| 631 | #endif |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 632 | } else if (IS_CHAR(*cur)) { |
| 633 | char buf[10], *ptr; |
| 634 | |
| 635 | #ifdef HAVE_SNPRINTF |
| 636 | snprintf(buf, 9, "&#%d;", *cur); |
| 637 | #else |
| 638 | sprintf(buf, "&#%d;", *cur); |
| 639 | #endif |
| 640 | ptr = buf; |
| 641 | while (*ptr != 0) *out++ = *ptr++; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 642 | } |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 643 | #if 0 |
| 644 | else { |
| 645 | /* |
| 646 | * default case, this is not a valid char ! |
| 647 | * Skip it... |
| 648 | */ |
| 649 | fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur); |
| 650 | } |
| 651 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 652 | cur++; |
| 653 | } |
| 654 | *out++ = 0; |
| 655 | return(buffer); |
| 656 | } |
| 657 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 658 | /** |
| 659 | * xmlCreateEntitiesTable: |
| 660 | * |
| 661 | * create and initialize an empty entities hash table. |
| 662 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 663 | * Returns the xmlEntitiesTablePtr just created or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 664 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 665 | xmlEntitiesTablePtr |
| 666 | xmlCreateEntitiesTable(void) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 667 | xmlEntitiesTablePtr ret; |
| 668 | |
| 669 | ret = (xmlEntitiesTablePtr) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 670 | xmlMalloc(sizeof(xmlEntitiesTable)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 671 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 672 | fprintf(stderr, "xmlCreateEntitiesTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 673 | (long)sizeof(xmlEntitiesTable)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 674 | return(NULL); |
| 675 | } |
| 676 | ret->max_entities = XML_MIN_ENTITIES_TABLE; |
| 677 | ret->nb_entities = 0; |
| 678 | ret->table = (xmlEntityPtr ) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 679 | xmlMalloc(ret->max_entities * sizeof(xmlEntity)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 680 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 681 | fprintf(stderr, "xmlCreateEntitiesTable : xmlMalloc(%ld) failed\n", |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 682 | ret->max_entities * (long)sizeof(xmlEntity)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 683 | xmlFree(ret); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 684 | return(NULL); |
| 685 | } |
| 686 | return(ret); |
| 687 | } |
| 688 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 689 | /** |
| 690 | * xmlFreeEntitiesTable: |
| 691 | * @table: An entity table |
| 692 | * |
| 693 | * Deallocate the memory used by an entities hash table. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 694 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 695 | void |
| 696 | xmlFreeEntitiesTable(xmlEntitiesTablePtr table) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 697 | int i; |
| 698 | |
| 699 | if (table == NULL) return; |
| 700 | |
| 701 | for (i = 0;i < table->nb_entities;i++) { |
| 702 | xmlFreeEntity(&table->table[i]); |
| 703 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 704 | xmlFree(table->table); |
| 705 | xmlFree(table); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 708 | /** |
| 709 | * xmlCopyEntitiesTable: |
| 710 | * @table: An entity table |
| 711 | * |
| 712 | * Build a copy of an entity table. |
| 713 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 714 | * Returns the new xmlEntitiesTablePtr or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 715 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 716 | xmlEntitiesTablePtr |
| 717 | xmlCopyEntitiesTable(xmlEntitiesTablePtr table) { |
| 718 | xmlEntitiesTablePtr ret; |
| 719 | xmlEntityPtr cur, ent; |
| 720 | int i; |
| 721 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 722 | ret = (xmlEntitiesTablePtr) xmlMalloc(sizeof(xmlEntitiesTable)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 723 | if (ret == NULL) { |
| 724 | fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n"); |
| 725 | return(NULL); |
| 726 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 727 | ret->table = (xmlEntityPtr) xmlMalloc(table->max_entities * |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 728 | sizeof(xmlEntity)); |
| 729 | if (ret->table == NULL) { |
| 730 | fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 731 | xmlFree(ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 732 | return(NULL); |
| 733 | } |
| 734 | ret->max_entities = table->max_entities; |
| 735 | ret->nb_entities = table->nb_entities; |
| 736 | for (i = 0;i < ret->nb_entities;i++) { |
| 737 | cur = &ret->table[i]; |
| 738 | ent = &table->table[i]; |
| 739 | cur->len = ent->len; |
| 740 | cur->type = ent->type; |
| 741 | if (ent->name != NULL) |
| 742 | cur->name = xmlStrdup(ent->name); |
| 743 | else |
| 744 | cur->name = NULL; |
| 745 | if (ent->ExternalID != NULL) |
| 746 | cur->ExternalID = xmlStrdup(ent->ExternalID); |
| 747 | else |
| 748 | cur->ExternalID = NULL; |
| 749 | if (ent->SystemID != NULL) |
| 750 | cur->SystemID = xmlStrdup(ent->SystemID); |
| 751 | else |
| 752 | cur->SystemID = NULL; |
| 753 | if (ent->content != NULL) |
| 754 | cur->content = xmlStrdup(ent->content); |
| 755 | else |
| 756 | cur->content = NULL; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 757 | if (ent->orig != NULL) |
| 758 | cur->orig = xmlStrdup(ent->orig); |
| 759 | else |
| 760 | cur->orig = NULL; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 761 | } |
| 762 | return(ret); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * xmlDumpEntitiesTable: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 767 | * @buf: An XML buffer. |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 768 | * @table: An entity table |
| 769 | * |
| 770 | * This will dump the content of the entity table as an XML DTD definition |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 771 | */ |
| 772 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 773 | xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 774 | int i; |
| 775 | xmlEntityPtr cur; |
| 776 | |
| 777 | if (table == NULL) return; |
| 778 | |
| 779 | for (i = 0;i < table->nb_entities;i++) { |
| 780 | cur = &table->table[i]; |
| 781 | switch (cur->type) { |
| 782 | case XML_INTERNAL_GENERAL_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 783 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 784 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 785 | xmlBufferWriteChar(buf, " "); |
| 786 | if (cur->orig != NULL) |
| 787 | xmlBufferWriteQuotedString(buf, cur->orig); |
| 788 | else |
| 789 | xmlBufferWriteQuotedString(buf, cur->content); |
| 790 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 791 | break; |
| 792 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 793 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 794 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 795 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 796 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 797 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 798 | xmlBufferWriteChar(buf, " "); |
| 799 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 800 | } else { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 801 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 802 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 803 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 804 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 805 | break; |
| 806 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 807 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 808 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 809 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 810 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 811 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 812 | xmlBufferWriteChar(buf, " "); |
| 813 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 814 | } else { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 815 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 816 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 817 | } |
| 818 | if (cur->content != NULL) { /* Should be true ! */ |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 819 | xmlBufferWriteChar(buf, " NDATA "); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 820 | if (cur->orig != NULL) |
| 821 | xmlBufferWriteCHAR(buf, cur->orig); |
| 822 | else |
| 823 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 824 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 825 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 826 | break; |
| 827 | case XML_INTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 828 | xmlBufferWriteChar(buf, "<!ENTITY % "); |
| 829 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 830 | xmlBufferWriteChar(buf, " "); |
| 831 | if (cur->orig == NULL) |
| 832 | xmlBufferWriteQuotedString(buf, cur->content); |
| 833 | else |
| 834 | xmlBufferWriteQuotedString(buf, cur->orig); |
| 835 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 836 | break; |
| 837 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 838 | xmlBufferWriteChar(buf, "<!ENTITY % "); |
| 839 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 840 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 841 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 842 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 843 | xmlBufferWriteChar(buf, " "); |
| 844 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 845 | } else { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 846 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 847 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 848 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 849 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 850 | break; |
| 851 | default: |
| 852 | fprintf(stderr, |
| 853 | "xmlDumpEntitiesTable: internal: unknown type %d\n", |
| 854 | cur->type); |
| 855 | } |
| 856 | } |
| 857 | } |