Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2 | * entities.c : implementation for the XML entities handling |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 6 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 9 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 10 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #ifdef HAVE_STDLIB_H |
| 14 | #include <stdlib.h> |
| 15 | #endif |
| 16 | #include <libxml/xmlmemory.h> |
| 17 | #include <libxml/hash.h> |
| 18 | #include <libxml/entities.h> |
| 19 | #include <libxml/parser.h> |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 20 | #include <libxml/parserInternals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 21 | #include <libxml/xmlerror.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 22 | #include <libxml/globals.h> |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 23 | #include <libxml/dict.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 24 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 25 | /* |
| 26 | * The XML predefined entities. |
| 27 | */ |
| 28 | |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 29 | static xmlEntity xmlEntityLt = { |
| 30 | NULL, XML_ENTITY_DECL, BAD_CAST "lt", |
| 31 | NULL, NULL, NULL, NULL, NULL, NULL, |
| 32 | BAD_CAST "<", BAD_CAST "<", 1, |
| 33 | XML_INTERNAL_PREDEFINED_ENTITY, |
| 34 | NULL, NULL, NULL, NULL, 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 35 | }; |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 36 | static xmlEntity xmlEntityGt = { |
| 37 | NULL, XML_ENTITY_DECL, BAD_CAST "gt", |
| 38 | NULL, NULL, NULL, NULL, NULL, NULL, |
| 39 | BAD_CAST ">", BAD_CAST ">", 1, |
| 40 | XML_INTERNAL_PREDEFINED_ENTITY, |
| 41 | NULL, NULL, NULL, NULL, 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 42 | }; |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 43 | static xmlEntity xmlEntityAmp = { |
| 44 | NULL, XML_ENTITY_DECL, BAD_CAST "amp", |
| 45 | NULL, NULL, NULL, NULL, NULL, NULL, |
| 46 | BAD_CAST "&", BAD_CAST "&", 1, |
| 47 | XML_INTERNAL_PREDEFINED_ENTITY, |
| 48 | NULL, NULL, NULL, NULL, 0 |
| 49 | }; |
| 50 | static xmlEntity xmlEntityQuot = { |
| 51 | NULL, XML_ENTITY_DECL, BAD_CAST "quot", |
| 52 | NULL, NULL, NULL, NULL, NULL, NULL, |
| 53 | BAD_CAST "\"", BAD_CAST "\"", 1, |
| 54 | XML_INTERNAL_PREDEFINED_ENTITY, |
| 55 | NULL, NULL, NULL, NULL, 0 |
| 56 | }; |
| 57 | static xmlEntity xmlEntityApos = { |
| 58 | NULL, XML_ENTITY_DECL, BAD_CAST "apos", |
| 59 | NULL, NULL, NULL, NULL, NULL, NULL, |
| 60 | BAD_CAST "'", BAD_CAST "'", 1, |
| 61 | XML_INTERNAL_PREDEFINED_ENTITY, |
| 62 | NULL, NULL, NULL, NULL, 0 |
| 63 | }; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 64 | |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 65 | /** |
| 66 | * xmlEntitiesErrMemory: |
| 67 | * @extra: extra informations |
| 68 | * |
| 69 | * Handle an out of memory condition |
| 70 | */ |
| 71 | static void |
| 72 | xmlEntitiesErrMemory(const char *extra) |
| 73 | { |
| 74 | __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * xmlEntitiesErr: |
| 79 | * @code: the error code |
| 80 | * @msg: the message |
| 81 | * |
| 82 | * Handle an out of memory condition |
| 83 | */ |
| 84 | static void |
| 85 | xmlEntitiesErr(xmlParserErrors code, const char *msg) |
| 86 | { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 87 | __xmlSimpleError(XML_FROM_TREE, code, NULL, msg, NULL); |
| 88 | } |
| 89 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 90 | /* |
| 91 | * xmlFreeEntity : clean-up an entity record. |
| 92 | */ |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 93 | static void |
| 94 | xmlFreeEntity(xmlEntityPtr entity) |
| 95 | { |
| 96 | xmlDictPtr dict = NULL; |
| 97 | |
| 98 | if (entity == NULL) |
| 99 | return; |
| 100 | |
| 101 | if (entity->doc != NULL) |
| 102 | dict = entity->doc->dict; |
| 103 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 104 | |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 105 | if ((entity->children) && (entity->owner == 1) && |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 106 | (entity == (xmlEntityPtr) entity->children->parent)) |
| 107 | xmlFreeNodeList(entity->children); |
| 108 | if (dict != NULL) { |
| 109 | if ((entity->name != NULL) && (!xmlDictOwns(dict, entity->name))) |
| 110 | xmlFree((char *) entity->name); |
| 111 | if ((entity->ExternalID != NULL) && |
| 112 | (!xmlDictOwns(dict, entity->ExternalID))) |
| 113 | xmlFree((char *) entity->ExternalID); |
| 114 | if ((entity->SystemID != NULL) && |
| 115 | (!xmlDictOwns(dict, entity->SystemID))) |
| 116 | xmlFree((char *) entity->SystemID); |
| 117 | if ((entity->URI != NULL) && (!xmlDictOwns(dict, entity->URI))) |
| 118 | xmlFree((char *) entity->URI); |
| 119 | if ((entity->content != NULL) |
| 120 | && (!xmlDictOwns(dict, entity->content))) |
| 121 | xmlFree((char *) entity->content); |
| 122 | if ((entity->orig != NULL) && (!xmlDictOwns(dict, entity->orig))) |
| 123 | xmlFree((char *) entity->orig); |
| 124 | } else { |
| 125 | if (entity->name != NULL) |
| 126 | xmlFree((char *) entity->name); |
| 127 | if (entity->ExternalID != NULL) |
| 128 | xmlFree((char *) entity->ExternalID); |
| 129 | if (entity->SystemID != NULL) |
| 130 | xmlFree((char *) entity->SystemID); |
| 131 | if (entity->URI != NULL) |
| 132 | xmlFree((char *) entity->URI); |
| 133 | if (entity->content != NULL) |
| 134 | xmlFree((char *) entity->content); |
| 135 | if (entity->orig != NULL) |
| 136 | xmlFree((char *) entity->orig); |
| 137 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 138 | xmlFree(entity); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * xmlAddEntity : register a new entity for an entities table. |
| 143 | */ |
| 144 | static xmlEntityPtr |
| 145 | xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type, |
| 146 | const xmlChar *ExternalID, const xmlChar *SystemID, |
| 147 | const xmlChar *content) { |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 148 | xmlDictPtr dict = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 149 | xmlEntitiesTablePtr table = NULL; |
| 150 | xmlEntityPtr ret; |
| 151 | |
| 152 | if (name == NULL) |
| 153 | return(NULL); |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 154 | if (dtd == NULL) |
| 155 | return(NULL); |
| 156 | if (dtd->doc != NULL) |
| 157 | dict = dtd->doc->dict; |
| 158 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 159 | switch (type) { |
| 160 | case XML_INTERNAL_GENERAL_ENTITY: |
| 161 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 162 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 163 | if (dtd->entities == NULL) |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 164 | dtd->entities = xmlHashCreateDict(0, dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 165 | table = dtd->entities; |
| 166 | break; |
| 167 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 168 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 169 | if (dtd->pentities == NULL) |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 170 | dtd->pentities = xmlHashCreateDict(0, dict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 171 | table = dtd->pentities; |
| 172 | break; |
| 173 | case XML_INTERNAL_PREDEFINED_ENTITY: |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 174 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 175 | } |
| 176 | if (table == NULL) |
| 177 | return(NULL); |
| 178 | ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity)); |
| 179 | if (ret == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 180 | xmlEntitiesErrMemory("xmlAddEntity:: malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 181 | return(NULL); |
| 182 | } |
| 183 | memset(ret, 0, sizeof(xmlEntity)); |
| 184 | ret->type = XML_ENTITY_DECL; |
| 185 | |
| 186 | /* |
| 187 | * fill the structure. |
| 188 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 189 | ret->etype = (xmlEntityType) type; |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 190 | if (dict == NULL) { |
| 191 | ret->name = xmlStrdup(name); |
| 192 | if (ExternalID != NULL) |
| 193 | ret->ExternalID = xmlStrdup(ExternalID); |
| 194 | if (SystemID != NULL) |
| 195 | ret->SystemID = xmlStrdup(SystemID); |
| 196 | } else { |
| 197 | ret->name = xmlDictLookup(dict, name, -1); |
| 198 | if (ExternalID != NULL) |
| 199 | ret->ExternalID = xmlDictLookup(dict, ExternalID, -1); |
| 200 | if (SystemID != NULL) |
| 201 | ret->SystemID = xmlDictLookup(dict, SystemID, -1); |
| 202 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 203 | if (content != NULL) { |
| 204 | ret->length = xmlStrlen(content); |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 205 | if ((dict != NULL) && (ret->length < 5)) |
| 206 | ret->content = (xmlChar *) |
| 207 | xmlDictLookup(dict, content, ret->length); |
| 208 | else |
| 209 | ret->content = xmlStrndup(content, ret->length); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 210 | } else { |
| 211 | ret->length = 0; |
| 212 | ret->content = NULL; |
| 213 | } |
| 214 | ret->URI = NULL; /* to be computed by the layer knowing |
| 215 | the defining entity */ |
| 216 | ret->orig = NULL; |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 217 | ret->owner = 0; |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 218 | ret->doc = dtd->doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 219 | |
| 220 | if (xmlHashAddEntry(table, name, ret)) { |
| 221 | /* |
| 222 | * entity was already defined at another level. |
| 223 | */ |
| 224 | xmlFreeEntity(ret); |
| 225 | return(NULL); |
| 226 | } |
| 227 | return(ret); |
| 228 | } |
| 229 | |
| 230 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 231 | * xmlGetPredefinedEntity: |
| 232 | * @name: the entity name |
| 233 | * |
| 234 | * Check whether this name is an predefined entity. |
| 235 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 236 | * Returns NULL if not, otherwise the entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 237 | */ |
| 238 | xmlEntityPtr |
| 239 | xmlGetPredefinedEntity(const xmlChar *name) { |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 240 | if (name == NULL) return(NULL); |
| 241 | switch (name[0]) { |
| 242 | case 'l': |
| 243 | if (xmlStrEqual(name, BAD_CAST "lt")) |
| 244 | return(&xmlEntityLt); |
| 245 | break; |
| 246 | case 'g': |
| 247 | if (xmlStrEqual(name, BAD_CAST "gt")) |
| 248 | return(&xmlEntityGt); |
| 249 | break; |
| 250 | case 'a': |
| 251 | if (xmlStrEqual(name, BAD_CAST "amp")) |
| 252 | return(&xmlEntityAmp); |
| 253 | if (xmlStrEqual(name, BAD_CAST "apos")) |
| 254 | return(&xmlEntityApos); |
| 255 | break; |
| 256 | case 'q': |
| 257 | if (xmlStrEqual(name, BAD_CAST "quot")) |
| 258 | return(&xmlEntityQuot); |
| 259 | break; |
| 260 | default: |
| 261 | break; |
| 262 | } |
| 263 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
| 267 | * xmlAddDtdEntity: |
| 268 | * @doc: the document |
| 269 | * @name: the entity name |
| 270 | * @type: the entity type XML_xxx_yyy_ENTITY |
| 271 | * @ExternalID: the entity external ID if available |
| 272 | * @SystemID: the entity system ID if available |
| 273 | * @content: the entity content |
| 274 | * |
| 275 | * Register a new entity for this document DTD external subset. |
| 276 | * |
| 277 | * Returns a pointer to the entity or NULL in case of error |
| 278 | */ |
| 279 | xmlEntityPtr |
| 280 | xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type, |
| 281 | const xmlChar *ExternalID, const xmlChar *SystemID, |
| 282 | const xmlChar *content) { |
| 283 | xmlEntityPtr ret; |
| 284 | xmlDtdPtr dtd; |
| 285 | |
| 286 | if (doc == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 287 | xmlEntitiesErr(XML_DTD_NO_DOC, |
| 288 | "xmlAddDtdEntity: document is NULL"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 289 | return(NULL); |
| 290 | } |
| 291 | if (doc->extSubset == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 292 | xmlEntitiesErr(XML_DTD_NO_DTD, |
| 293 | "xmlAddDtdEntity: document without external subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 294 | return(NULL); |
| 295 | } |
| 296 | dtd = doc->extSubset; |
| 297 | ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content); |
| 298 | if (ret == NULL) return(NULL); |
| 299 | |
| 300 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 301 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 302 | */ |
| 303 | ret->parent = dtd; |
| 304 | ret->doc = dtd->doc; |
| 305 | if (dtd->last == NULL) { |
| 306 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 307 | } else { |
| 308 | dtd->last->next = (xmlNodePtr) ret; |
| 309 | ret->prev = dtd->last; |
| 310 | dtd->last = (xmlNodePtr) ret; |
| 311 | } |
| 312 | return(ret); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * xmlAddDocEntity: |
| 317 | * @doc: the document |
| 318 | * @name: the entity name |
| 319 | * @type: the entity type XML_xxx_yyy_ENTITY |
| 320 | * @ExternalID: the entity external ID if available |
| 321 | * @SystemID: the entity system ID if available |
| 322 | * @content: the entity content |
| 323 | * |
| 324 | * Register a new entity for this document. |
| 325 | * |
| 326 | * Returns a pointer to the entity or NULL in case of error |
| 327 | */ |
| 328 | xmlEntityPtr |
| 329 | xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type, |
| 330 | const xmlChar *ExternalID, const xmlChar *SystemID, |
| 331 | const xmlChar *content) { |
| 332 | xmlEntityPtr ret; |
| 333 | xmlDtdPtr dtd; |
| 334 | |
| 335 | if (doc == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 336 | xmlEntitiesErr(XML_DTD_NO_DOC, |
| 337 | "xmlAddDocEntity: document is NULL"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 338 | return(NULL); |
| 339 | } |
| 340 | if (doc->intSubset == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 341 | xmlEntitiesErr(XML_DTD_NO_DTD, |
| 342 | "xmlAddDocEntity: document without internal subset"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 343 | return(NULL); |
| 344 | } |
| 345 | dtd = doc->intSubset; |
| 346 | ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content); |
| 347 | if (ret == NULL) return(NULL); |
| 348 | |
| 349 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 350 | * Link it to the DTD |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 351 | */ |
| 352 | ret->parent = dtd; |
| 353 | ret->doc = dtd->doc; |
| 354 | if (dtd->last == NULL) { |
| 355 | dtd->children = dtd->last = (xmlNodePtr) ret; |
| 356 | } else { |
| 357 | dtd->last->next = (xmlNodePtr) ret; |
| 358 | ret->prev = dtd->last; |
| 359 | dtd->last = (xmlNodePtr) ret; |
| 360 | } |
| 361 | return(ret); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * xmlGetEntityFromTable: |
| 366 | * @table: an entity table |
| 367 | * @name: the entity name |
| 368 | * @parameter: look for parameter entities |
| 369 | * |
| 370 | * Do an entity lookup in the table. |
| 371 | * returns the corresponding parameter entity, if found. |
| 372 | * |
| 373 | * Returns A pointer to the entity structure or NULL if not found. |
| 374 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 375 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 376 | xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) { |
| 377 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * xmlGetParameterEntity: |
| 382 | * @doc: the document referencing the entity |
| 383 | * @name: the entity name |
| 384 | * |
| 385 | * Do an entity lookup in the internal and external subsets and |
| 386 | * returns the corresponding parameter entity, if found. |
| 387 | * |
| 388 | * Returns A pointer to the entity structure or NULL if not found. |
| 389 | */ |
| 390 | xmlEntityPtr |
| 391 | xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) { |
| 392 | xmlEntitiesTablePtr table; |
| 393 | xmlEntityPtr ret; |
| 394 | |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 395 | if (doc == NULL) |
| 396 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 397 | if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) { |
| 398 | table = (xmlEntitiesTablePtr) doc->intSubset->pentities; |
| 399 | ret = xmlGetEntityFromTable(table, name); |
| 400 | if (ret != NULL) |
| 401 | return(ret); |
| 402 | } |
| 403 | if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) { |
| 404 | table = (xmlEntitiesTablePtr) doc->extSubset->pentities; |
| 405 | return(xmlGetEntityFromTable(table, name)); |
| 406 | } |
| 407 | return(NULL); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * xmlGetDtdEntity: |
| 412 | * @doc: the document referencing the entity |
| 413 | * @name: the entity name |
| 414 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 415 | * Do an entity lookup in the DTD entity hash table and |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 416 | * returns the corresponding entity, if found. |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 417 | * Note: the first argument is the document node, not the DTD node. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 418 | * |
| 419 | * Returns A pointer to the entity structure or NULL if not found. |
| 420 | */ |
| 421 | xmlEntityPtr |
| 422 | xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) { |
| 423 | xmlEntitiesTablePtr table; |
| 424 | |
Daniel Veillard | 3606581 | 2002-01-24 15:02:46 +0000 | [diff] [blame] | 425 | if (doc == NULL) |
| 426 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 427 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 428 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
| 429 | return(xmlGetEntityFromTable(table, name)); |
| 430 | } |
| 431 | return(NULL); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * xmlGetDocEntity: |
| 436 | * @doc: the document referencing the entity |
| 437 | * @name: the entity name |
| 438 | * |
| 439 | * Do an entity lookup in the document entity hash table and |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 440 | * returns the corresponding entity, otherwise a lookup is done |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 441 | * in the predefined entities too. |
| 442 | * |
| 443 | * Returns A pointer to the entity structure or NULL if not found. |
| 444 | */ |
| 445 | xmlEntityPtr |
| 446 | xmlGetDocEntity(xmlDocPtr doc, const xmlChar *name) { |
| 447 | xmlEntityPtr cur; |
| 448 | xmlEntitiesTablePtr table; |
| 449 | |
| 450 | if (doc != NULL) { |
| 451 | if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { |
| 452 | table = (xmlEntitiesTablePtr) doc->intSubset->entities; |
| 453 | cur = xmlGetEntityFromTable(table, name); |
| 454 | if (cur != NULL) |
| 455 | return(cur); |
| 456 | } |
Daniel Veillard | 2875770 | 2002-02-18 11:19:30 +0000 | [diff] [blame] | 457 | if (doc->standalone != 1) { |
| 458 | if ((doc->extSubset != NULL) && |
| 459 | (doc->extSubset->entities != NULL)) { |
| 460 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
| 461 | cur = xmlGetEntityFromTable(table, name); |
| 462 | if (cur != NULL) |
| 463 | return(cur); |
| 464 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
Daniel Veillard | d3a2e4c | 2003-09-30 13:38:04 +0000 | [diff] [blame] | 467 | return(xmlGetPredefinedEntity(name)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 471 | * Macro used to grow the current buffer. |
| 472 | */ |
| 473 | #define growBufferReentrant() { \ |
| 474 | buffer_size *= 2; \ |
| 475 | buffer = (xmlChar *) \ |
| 476 | xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \ |
| 477 | if (buffer == NULL) { \ |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 478 | xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: realloc failed");\ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 479 | return(NULL); \ |
| 480 | } \ |
| 481 | } |
| 482 | |
| 483 | |
| 484 | /** |
| 485 | * xmlEncodeEntitiesReentrant: |
| 486 | * @doc: the document containing the string |
| 487 | * @input: A string to convert to XML. |
| 488 | * |
| 489 | * Do a global encoding of a string, replacing the predefined entities |
| 490 | * and non ASCII values with their entities and CharRef counterparts. |
| 491 | * Contrary to xmlEncodeEntities, this routine is reentrant, and result |
| 492 | * must be deallocated. |
| 493 | * |
| 494 | * Returns A newly allocated string with the substitution done. |
| 495 | */ |
| 496 | xmlChar * |
| 497 | xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) { |
| 498 | const xmlChar *cur = input; |
| 499 | xmlChar *buffer = NULL; |
| 500 | xmlChar *out = NULL; |
| 501 | int buffer_size = 0; |
| 502 | int html = 0; |
| 503 | |
| 504 | if (input == NULL) return(NULL); |
| 505 | if (doc != NULL) |
| 506 | html = (doc->type == XML_HTML_DOCUMENT_NODE); |
| 507 | |
| 508 | /* |
| 509 | * allocate an translation buffer. |
| 510 | */ |
| 511 | buffer_size = 1000; |
| 512 | buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar)); |
| 513 | if (buffer == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 514 | xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 515 | return(NULL); |
| 516 | } |
| 517 | out = buffer; |
| 518 | |
| 519 | while (*cur != '\0') { |
| 520 | if (out - buffer > buffer_size - 100) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 521 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 522 | |
| 523 | growBufferReentrant(); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 524 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* |
| 528 | * By default one have to encode at least '<', '>', '"' and '&' ! |
| 529 | */ |
| 530 | if (*cur == '<') { |
| 531 | *out++ = '&'; |
| 532 | *out++ = 'l'; |
| 533 | *out++ = 't'; |
| 534 | *out++ = ';'; |
| 535 | } else if (*cur == '>') { |
| 536 | *out++ = '&'; |
| 537 | *out++ = 'g'; |
| 538 | *out++ = 't'; |
| 539 | *out++ = ';'; |
| 540 | } else if (*cur == '&') { |
| 541 | *out++ = '&'; |
| 542 | *out++ = 'a'; |
| 543 | *out++ = 'm'; |
| 544 | *out++ = 'p'; |
| 545 | *out++ = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 546 | } else if (((*cur >= 0x20) && (*cur < 0x80)) || |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 547 | (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 548 | /* |
| 549 | * default case, just copy ! |
| 550 | */ |
| 551 | *out++ = *cur; |
| 552 | } else if (*cur >= 0x80) { |
Daniel Veillard | 122376b | 2001-04-24 12:12:30 +0000 | [diff] [blame] | 553 | if (((doc != NULL) && (doc->encoding != NULL)) || (html)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 554 | /* |
| 555 | * Bjørn Reese <br@sseusa.com> provided the patch |
| 556 | xmlChar xc; |
| 557 | xc = (*cur & 0x3F) << 6; |
| 558 | if (cur[1] != 0) { |
| 559 | xc += *(++cur) & 0x3F; |
| 560 | *out++ = xc; |
| 561 | } else |
| 562 | */ |
Daniel Veillard | 2728f84 | 2006-03-09 16:49:24 +0000 | [diff] [blame] | 563 | *out++ = *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 564 | } else { |
| 565 | /* |
| 566 | * We assume we have UTF-8 input. |
| 567 | */ |
Daniel Veillard | b2517d8 | 2003-10-01 19:13:56 +0000 | [diff] [blame] | 568 | char buf[11], *ptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 569 | int val = 0, l = 1; |
| 570 | |
| 571 | if (*cur < 0xC0) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 572 | xmlEntitiesErr(XML_CHECK_NOT_UTF8, |
| 573 | "xmlEncodeEntitiesReentrant : input not UTF-8"); |
Daniel Veillard | 122376b | 2001-04-24 12:12:30 +0000 | [diff] [blame] | 574 | if (doc != NULL) |
| 575 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 576 | snprintf(buf, sizeof(buf), "&#%d;", *cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 577 | buf[sizeof(buf) - 1] = 0; |
| 578 | ptr = buf; |
| 579 | while (*ptr != 0) *out++ = *ptr++; |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 580 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 581 | continue; |
| 582 | } else if (*cur < 0xE0) { |
| 583 | val = (cur[0]) & 0x1F; |
| 584 | val <<= 6; |
| 585 | val |= (cur[1]) & 0x3F; |
| 586 | l = 2; |
| 587 | } else if (*cur < 0xF0) { |
| 588 | val = (cur[0]) & 0x0F; |
| 589 | val <<= 6; |
| 590 | val |= (cur[1]) & 0x3F; |
| 591 | val <<= 6; |
| 592 | val |= (cur[2]) & 0x3F; |
| 593 | l = 3; |
| 594 | } else if (*cur < 0xF8) { |
| 595 | val = (cur[0]) & 0x07; |
| 596 | val <<= 6; |
| 597 | val |= (cur[1]) & 0x3F; |
| 598 | val <<= 6; |
| 599 | val |= (cur[2]) & 0x3F; |
| 600 | val <<= 6; |
| 601 | val |= (cur[3]) & 0x3F; |
| 602 | l = 4; |
| 603 | } |
| 604 | if ((l == 1) || (!IS_CHAR(val))) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 605 | xmlEntitiesErr(XML_ERR_INVALID_CHAR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 606 | "xmlEncodeEntitiesReentrant : char out of range\n"); |
Daniel Veillard | 122376b | 2001-04-24 12:12:30 +0000 | [diff] [blame] | 607 | if (doc != NULL) |
| 608 | doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 609 | snprintf(buf, sizeof(buf), "&#%d;", *cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 610 | buf[sizeof(buf) - 1] = 0; |
| 611 | ptr = buf; |
| 612 | while (*ptr != 0) *out++ = *ptr++; |
| 613 | cur++; |
| 614 | continue; |
| 615 | } |
| 616 | /* |
| 617 | * We could do multiple things here. Just save as a char ref |
| 618 | */ |
Daniel Veillard | 2728f84 | 2006-03-09 16:49:24 +0000 | [diff] [blame] | 619 | snprintf(buf, sizeof(buf), "&#x%X;", val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 620 | buf[sizeof(buf) - 1] = 0; |
| 621 | ptr = buf; |
| 622 | while (*ptr != 0) *out++ = *ptr++; |
| 623 | cur += l; |
| 624 | continue; |
| 625 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 626 | } else if (IS_BYTE_CHAR(*cur)) { |
Daniel Veillard | b2517d8 | 2003-10-01 19:13:56 +0000 | [diff] [blame] | 627 | char buf[11], *ptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 628 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 629 | snprintf(buf, sizeof(buf), "&#%d;", *cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 630 | buf[sizeof(buf) - 1] = 0; |
| 631 | ptr = buf; |
| 632 | while (*ptr != 0) *out++ = *ptr++; |
| 633 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 634 | cur++; |
| 635 | } |
| 636 | *out++ = 0; |
| 637 | return(buffer); |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * xmlEncodeSpecialChars: |
| 642 | * @doc: the document containing the string |
| 643 | * @input: A string to convert to XML. |
| 644 | * |
| 645 | * Do a global encoding of a string, replacing the predefined entities |
| 646 | * this routine is reentrant, and result must be deallocated. |
| 647 | * |
| 648 | * Returns A newly allocated string with the substitution done. |
| 649 | */ |
| 650 | xmlChar * |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 651 | xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED, const xmlChar *input) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 652 | const xmlChar *cur = input; |
| 653 | xmlChar *buffer = NULL; |
| 654 | xmlChar *out = NULL; |
| 655 | int buffer_size = 0; |
William M. Brack | 899e64a | 2003-09-26 18:03:42 +0000 | [diff] [blame] | 656 | if (input == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 657 | |
| 658 | /* |
| 659 | * allocate an translation buffer. |
| 660 | */ |
| 661 | buffer_size = 1000; |
| 662 | buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar)); |
| 663 | if (buffer == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 664 | xmlEntitiesErrMemory("xmlEncodeSpecialChars: malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 665 | return(NULL); |
| 666 | } |
| 667 | out = buffer; |
| 668 | |
| 669 | while (*cur != '\0') { |
| 670 | if (out - buffer > buffer_size - 10) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 671 | int indx = out - buffer; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 672 | |
| 673 | growBufferReentrant(); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 674 | out = &buffer[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* |
| 678 | * By default one have to encode at least '<', '>', '"' and '&' ! |
| 679 | */ |
| 680 | if (*cur == '<') { |
| 681 | *out++ = '&'; |
| 682 | *out++ = 'l'; |
| 683 | *out++ = 't'; |
| 684 | *out++ = ';'; |
| 685 | } else if (*cur == '>') { |
| 686 | *out++ = '&'; |
| 687 | *out++ = 'g'; |
| 688 | *out++ = 't'; |
| 689 | *out++ = ';'; |
| 690 | } else if (*cur == '&') { |
| 691 | *out++ = '&'; |
| 692 | *out++ = 'a'; |
| 693 | *out++ = 'm'; |
| 694 | *out++ = 'p'; |
| 695 | *out++ = ';'; |
| 696 | } else if (*cur == '"') { |
| 697 | *out++ = '&'; |
| 698 | *out++ = 'q'; |
| 699 | *out++ = 'u'; |
| 700 | *out++ = 'o'; |
| 701 | *out++ = 't'; |
| 702 | *out++ = ';'; |
Daniel Veillard | 19ab45b | 2003-02-26 15:49:03 +0000 | [diff] [blame] | 703 | } else if (*cur == '\r') { |
| 704 | *out++ = '&'; |
| 705 | *out++ = '#'; |
| 706 | *out++ = '1'; |
| 707 | *out++ = '3'; |
| 708 | *out++ = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 709 | } else { |
| 710 | /* |
| 711 | * Works because on UTF-8, all extended sequences cannot |
| 712 | * result in bytes in the ASCII range. |
| 713 | */ |
| 714 | *out++ = *cur; |
| 715 | } |
| 716 | cur++; |
| 717 | } |
| 718 | *out++ = 0; |
| 719 | return(buffer); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * xmlCreateEntitiesTable: |
| 724 | * |
| 725 | * create and initialize an empty entities hash table. |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 726 | * This really doesn't make sense and should be deprecated |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 727 | * |
| 728 | * Returns the xmlEntitiesTablePtr just created or NULL in case of error. |
| 729 | */ |
| 730 | xmlEntitiesTablePtr |
| 731 | xmlCreateEntitiesTable(void) { |
| 732 | return((xmlEntitiesTablePtr) xmlHashCreate(0)); |
| 733 | } |
| 734 | |
| 735 | /** |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 736 | * xmlFreeEntityWrapper: |
| 737 | * @entity: An entity |
| 738 | * @name: its name |
| 739 | * |
| 740 | * Deallocate the memory used by an entities in the hash table. |
| 741 | */ |
| 742 | static void |
| 743 | xmlFreeEntityWrapper(xmlEntityPtr entity, |
| 744 | const xmlChar *name ATTRIBUTE_UNUSED) { |
| 745 | if (entity != NULL) |
| 746 | xmlFreeEntity(entity); |
| 747 | } |
| 748 | |
| 749 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 750 | * xmlFreeEntitiesTable: |
| 751 | * @table: An entity table |
| 752 | * |
| 753 | * Deallocate the memory used by an entities hash table. |
| 754 | */ |
| 755 | void |
| 756 | xmlFreeEntitiesTable(xmlEntitiesTablePtr table) { |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 757 | xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 760 | #ifdef LIBXML_TREE_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 761 | /** |
| 762 | * xmlCopyEntity: |
| 763 | * @ent: An entity |
| 764 | * |
| 765 | * Build a copy of an entity |
| 766 | * |
| 767 | * Returns the new xmlEntitiesPtr or NULL in case of error. |
| 768 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 769 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 770 | xmlCopyEntity(xmlEntityPtr ent) { |
| 771 | xmlEntityPtr cur; |
| 772 | |
| 773 | cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity)); |
| 774 | if (cur == NULL) { |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 775 | xmlEntitiesErrMemory("xmlCopyEntity:: malloc failed"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 776 | return(NULL); |
| 777 | } |
| 778 | memset(cur, 0, sizeof(xmlEntity)); |
Daniel Veillard | 845cce4 | 2002-01-09 11:51:37 +0000 | [diff] [blame] | 779 | cur->type = XML_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 780 | |
| 781 | cur->etype = ent->etype; |
| 782 | if (ent->name != NULL) |
| 783 | cur->name = xmlStrdup(ent->name); |
| 784 | if (ent->ExternalID != NULL) |
| 785 | cur->ExternalID = xmlStrdup(ent->ExternalID); |
| 786 | if (ent->SystemID != NULL) |
| 787 | cur->SystemID = xmlStrdup(ent->SystemID); |
| 788 | if (ent->content != NULL) |
| 789 | cur->content = xmlStrdup(ent->content); |
| 790 | if (ent->orig != NULL) |
| 791 | cur->orig = xmlStrdup(ent->orig); |
Daniel Veillard | 8ee9c8f | 2002-01-26 21:42:58 +0000 | [diff] [blame] | 792 | if (ent->URI != NULL) |
| 793 | cur->URI = xmlStrdup(ent->URI); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 794 | return(cur); |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * xmlCopyEntitiesTable: |
| 799 | * @table: An entity table |
| 800 | * |
| 801 | * Build a copy of an entity table. |
| 802 | * |
| 803 | * Returns the new xmlEntitiesTablePtr or NULL in case of error. |
| 804 | */ |
| 805 | xmlEntitiesTablePtr |
| 806 | xmlCopyEntitiesTable(xmlEntitiesTablePtr table) { |
| 807 | return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity)); |
| 808 | } |
Daniel Veillard | 652327a | 2003-09-29 18:02:38 +0000 | [diff] [blame] | 809 | #endif /* LIBXML_TREE_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 810 | |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 811 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | 18ab872 | 2003-12-09 22:51:37 +0000 | [diff] [blame] | 812 | |
| 813 | /** |
| 814 | * xmlDumpEntityContent: |
| 815 | * @buf: An XML buffer. |
| 816 | * @content: The entity content. |
| 817 | * |
| 818 | * This will dump the quoted string value, taking care of the special |
| 819 | * treatment required by % |
| 820 | */ |
| 821 | static void |
| 822 | xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) { |
| 823 | if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; |
| 824 | if (xmlStrchr(content, '%')) { |
| 825 | const xmlChar * base, *cur; |
| 826 | |
| 827 | xmlBufferCCat(buf, "\""); |
| 828 | base = cur = content; |
| 829 | while (*cur != 0) { |
| 830 | if (*cur == '"') { |
| 831 | if (base != cur) |
| 832 | xmlBufferAdd(buf, base, cur - base); |
| 833 | xmlBufferAdd(buf, BAD_CAST """, 6); |
| 834 | cur++; |
| 835 | base = cur; |
| 836 | } else if (*cur == '%') { |
| 837 | if (base != cur) |
| 838 | xmlBufferAdd(buf, base, cur - base); |
| 839 | xmlBufferAdd(buf, BAD_CAST "%", 6); |
| 840 | cur++; |
| 841 | base = cur; |
| 842 | } else { |
| 843 | cur++; |
| 844 | } |
| 845 | } |
| 846 | if (base != cur) |
| 847 | xmlBufferAdd(buf, base, cur - base); |
| 848 | xmlBufferCCat(buf, "\""); |
| 849 | } else { |
| 850 | xmlBufferWriteQuotedString(buf, content); |
| 851 | } |
| 852 | } |
| 853 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 854 | /** |
| 855 | * xmlDumpEntityDecl: |
| 856 | * @buf: An XML buffer. |
| 857 | * @ent: An entity table |
| 858 | * |
| 859 | * This will dump the content of the entity table as an XML DTD definition |
| 860 | */ |
| 861 | void |
| 862 | xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 863 | if ((buf == NULL) || (ent == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 864 | switch (ent->etype) { |
| 865 | case XML_INTERNAL_GENERAL_ENTITY: |
| 866 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 867 | xmlBufferWriteCHAR(buf, ent->name); |
| 868 | xmlBufferWriteChar(buf, " "); |
| 869 | if (ent->orig != NULL) |
| 870 | xmlBufferWriteQuotedString(buf, ent->orig); |
| 871 | else |
Daniel Veillard | 18ab872 | 2003-12-09 22:51:37 +0000 | [diff] [blame] | 872 | xmlDumpEntityContent(buf, ent->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 873 | xmlBufferWriteChar(buf, ">\n"); |
| 874 | break; |
| 875 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 876 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 877 | xmlBufferWriteCHAR(buf, ent->name); |
| 878 | if (ent->ExternalID != NULL) { |
| 879 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 880 | xmlBufferWriteQuotedString(buf, ent->ExternalID); |
| 881 | xmlBufferWriteChar(buf, " "); |
| 882 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 883 | } else { |
| 884 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 885 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 886 | } |
| 887 | xmlBufferWriteChar(buf, ">\n"); |
| 888 | break; |
| 889 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
| 890 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 891 | xmlBufferWriteCHAR(buf, ent->name); |
| 892 | if (ent->ExternalID != NULL) { |
| 893 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 894 | xmlBufferWriteQuotedString(buf, ent->ExternalID); |
| 895 | xmlBufferWriteChar(buf, " "); |
| 896 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 897 | } else { |
| 898 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 899 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 900 | } |
| 901 | if (ent->content != NULL) { /* Should be true ! */ |
| 902 | xmlBufferWriteChar(buf, " NDATA "); |
| 903 | if (ent->orig != NULL) |
| 904 | xmlBufferWriteCHAR(buf, ent->orig); |
| 905 | else |
| 906 | xmlBufferWriteCHAR(buf, ent->content); |
| 907 | } |
| 908 | xmlBufferWriteChar(buf, ">\n"); |
| 909 | break; |
| 910 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 911 | xmlBufferWriteChar(buf, "<!ENTITY % "); |
| 912 | xmlBufferWriteCHAR(buf, ent->name); |
| 913 | xmlBufferWriteChar(buf, " "); |
| 914 | if (ent->orig == NULL) |
Daniel Veillard | 18ab872 | 2003-12-09 22:51:37 +0000 | [diff] [blame] | 915 | xmlDumpEntityContent(buf, ent->content); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 916 | else |
| 917 | xmlBufferWriteQuotedString(buf, ent->orig); |
| 918 | xmlBufferWriteChar(buf, ">\n"); |
| 919 | break; |
| 920 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 921 | xmlBufferWriteChar(buf, "<!ENTITY % "); |
| 922 | xmlBufferWriteCHAR(buf, ent->name); |
| 923 | if (ent->ExternalID != NULL) { |
| 924 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 925 | xmlBufferWriteQuotedString(buf, ent->ExternalID); |
| 926 | xmlBufferWriteChar(buf, " "); |
| 927 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 928 | } else { |
| 929 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 930 | xmlBufferWriteQuotedString(buf, ent->SystemID); |
| 931 | } |
| 932 | xmlBufferWriteChar(buf, ">\n"); |
| 933 | break; |
| 934 | default: |
Daniel Veillard | ce244ad | 2004-11-05 10:03:46 +0000 | [diff] [blame] | 935 | xmlEntitiesErr(XML_DTD_UNKNOWN_ENTITY, |
| 936 | "xmlDumpEntitiesDecl: internal: unknown type entity type"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | |
| 940 | /** |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 941 | * xmlDumpEntityDeclScan: |
| 942 | * @ent: An entity table |
| 943 | * @buf: An XML buffer. |
| 944 | * |
| 945 | * When using the hash table scan function, arguments need to be reversed |
| 946 | */ |
| 947 | static void |
| 948 | xmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) { |
| 949 | xmlDumpEntityDecl(buf, ent); |
| 950 | } |
| 951 | |
| 952 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 953 | * xmlDumpEntitiesTable: |
| 954 | * @buf: An XML buffer. |
| 955 | * @table: An entity table |
| 956 | * |
| 957 | * This will dump the content of the entity table as an XML DTD definition |
| 958 | */ |
| 959 | void |
| 960 | xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) { |
William M. Brack | 9e66059 | 2003-10-20 14:56:06 +0000 | [diff] [blame] | 961 | xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 962 | } |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 963 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 964 | #define bottom_entities |
| 965 | #include "elfgcchack.h" |