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