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