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 | |
| 9 | #include <stdio.h> |
Seth Alves | e7f12e6 | 1998-10-01 20:51:15 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 11 | #include <string.h> |
| 12 | #include "entities.h" |
| 13 | |
| 14 | /* |
| 15 | * The XML predefined entities. |
| 16 | */ |
| 17 | |
| 18 | struct xmlPredefinedEntityValue { |
| 19 | const char *name; |
| 20 | const char *value; |
| 21 | }; |
| 22 | struct xmlPredefinedEntityValue xmlPredefinedEntityValues[] = { |
| 23 | { "lt", "<" }, |
| 24 | { "gt", ">" }, |
| 25 | { "apos", "'" }, |
| 26 | { "quot", "\"" }, |
| 27 | { "amp", "&" } |
| 28 | }; |
| 29 | |
| 30 | xmlEntitiesTablePtr xmlPredefinedEntities = NULL; |
| 31 | |
| 32 | /* |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 33 | * Macro used to grow the current buffer. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 34 | */ |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 35 | #define growBuffer() { \ |
| 36 | buffer_size *= 2; \ |
| 37 | buffer = (CHAR *) realloc(buffer, buffer_size * sizeof(CHAR)); \ |
| 38 | if (buffer == NULL) { \ |
| 39 | perror("realloc failed"); \ |
| 40 | exit(1); \ |
| 41 | } \ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 44 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 45 | /* |
| 46 | * xmlFreeEntity : clean-up an entity record. |
| 47 | */ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 48 | void xmlFreeEntity(xmlEntityPtr entity) { |
| 49 | if (entity == NULL) return; |
| 50 | |
| 51 | if (entity->name != NULL) |
| 52 | free((char *) entity->name); |
| 53 | if (entity->ExternalID != NULL) |
| 54 | free((char *) entity->ExternalID); |
| 55 | if (entity->SystemID != NULL) |
| 56 | free((char *) entity->SystemID); |
| 57 | if (entity->content != NULL) |
| 58 | free((char *) entity->content); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 59 | if (entity->orig != NULL) |
| 60 | free((char *) entity->orig); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 61 | memset(entity, -1, sizeof(xmlEntity)); |
| 62 | } |
| 63 | |
| 64 | /* |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 65 | * xmlAddEntity : register a new entity for an entities table. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 66 | * |
| 67 | * TODO !!! We should check here that the combination of type |
| 68 | * ExternalID and SystemID is valid. |
| 69 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 70 | static void |
| 71 | xmlAddEntity(xmlEntitiesTablePtr table, const CHAR *name, int type, |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 72 | const CHAR *ExternalID, const CHAR *SystemID, CHAR *content) { |
| 73 | int i; |
| 74 | xmlEntityPtr cur; |
| 75 | int len; |
| 76 | |
| 77 | for (i = 0;i < table->nb_entities;i++) { |
| 78 | cur = &table->table[i]; |
| 79 | if (!xmlStrcmp(cur->name, name)) { |
| 80 | /* |
| 81 | * The entity is already defined in this Dtd, the spec says to NOT |
| 82 | * override it ... Is it worth a Warning ??? !!! |
| 83 | */ |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | if (table->nb_entities >= table->max_entities) { |
| 88 | /* |
| 89 | * need more elements. |
| 90 | */ |
| 91 | table->max_entities *= 2; |
| 92 | table->table = (xmlEntityPtr) |
| 93 | realloc(table->table, table->max_entities * sizeof(xmlEntity)); |
| 94 | if (table->table) { |
| 95 | perror("realloc failed"); |
| 96 | exit(1); |
| 97 | } |
| 98 | } |
| 99 | cur = &table->table[table->nb_entities]; |
| 100 | cur->name = xmlStrdup(name); |
| 101 | for (len = 0;name[0] != 0;name++)len++; |
| 102 | cur->len = len; |
| 103 | cur->type = type; |
| 104 | if (ExternalID != NULL) |
| 105 | cur->ExternalID = xmlStrdup(ExternalID); |
| 106 | else |
| 107 | cur->ExternalID = NULL; |
| 108 | if (SystemID != NULL) |
| 109 | cur->SystemID = xmlStrdup(SystemID); |
| 110 | else |
| 111 | cur->SystemID = NULL; |
| 112 | if (content != NULL) |
| 113 | cur->content = xmlStrdup(content); |
| 114 | else |
| 115 | cur->content = NULL; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 116 | cur->orig = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 117 | table->nb_entities++; |
| 118 | } |
| 119 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 120 | /** |
| 121 | * xmlInitializePredefinedEntities: |
| 122 | * |
| 123 | * Set up the predefined entities. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 124 | */ |
| 125 | void xmlInitializePredefinedEntities(void) { |
| 126 | int i; |
| 127 | CHAR name[50]; |
| 128 | CHAR value[50]; |
| 129 | const char *in; |
| 130 | CHAR *out; |
| 131 | |
| 132 | if (xmlPredefinedEntities != NULL) return; |
| 133 | |
| 134 | xmlPredefinedEntities = xmlCreateEntitiesTable(); |
| 135 | for (i = 0;i < sizeof(xmlPredefinedEntityValues) / |
| 136 | sizeof(xmlPredefinedEntityValues[0]);i++) { |
| 137 | in = xmlPredefinedEntityValues[i].name; |
| 138 | out = &name[0]; |
| 139 | for (;(*out++ = (CHAR) *in);)in++; |
| 140 | in = xmlPredefinedEntityValues[i].value; |
| 141 | out = &value[0]; |
| 142 | for (;(*out++ = (CHAR) *in);)in++; |
| 143 | xmlAddEntity(xmlPredefinedEntities, (const CHAR *) &name[0], |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 144 | XML_INTERNAL_PREDEFINED_ENTITY, NULL, NULL, |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 145 | &value[0]); |
| 146 | } |
| 147 | } |
| 148 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 149 | /** |
| 150 | * xmlGetPredefinedEntity: |
| 151 | * @name: the entity name |
| 152 | * |
| 153 | * Check whether this name is an predefined entity. |
| 154 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 155 | * Returns NULL if not, othervise the entity |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 156 | */ |
| 157 | xmlEntityPtr |
| 158 | xmlGetPredefinedEntity(const CHAR *name) { |
| 159 | int i; |
| 160 | xmlEntityPtr cur; |
| 161 | |
| 162 | if (xmlPredefinedEntities == NULL) |
| 163 | xmlInitializePredefinedEntities(); |
| 164 | for (i = 0;i < xmlPredefinedEntities->nb_entities;i++) { |
| 165 | cur = &xmlPredefinedEntities->table[i]; |
| 166 | if (!xmlStrcmp(cur->name, name)) return(cur); |
| 167 | } |
| 168 | return(NULL); |
| 169 | } |
| 170 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 171 | /** |
| 172 | * xmlAddDtdEntity: |
| 173 | * @doc: the document |
| 174 | * @name: the entity name |
| 175 | * @type: the entity type XML_xxx_yyy_ENTITY |
| 176 | * @ExternalID: the entity external ID if available |
| 177 | * @SystemID: the entity system ID if available |
| 178 | * @content: the entity content |
| 179 | * |
| 180 | * Register a new entity for this document DTD. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 181 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 182 | void |
| 183 | xmlAddDtdEntity(xmlDocPtr doc, const CHAR *name, int type, |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 184 | const CHAR *ExternalID, const CHAR *SystemID, CHAR *content) { |
| 185 | xmlEntitiesTablePtr table; |
| 186 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 187 | if (doc->extSubset == NULL) { |
| 188 | fprintf(stderr, |
| 189 | "xmlAddDtdEntity: document without external subset !\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 190 | return; |
| 191 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 192 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 193 | if (table == NULL) { |
| 194 | table = xmlCreateEntitiesTable(); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 195 | doc->extSubset->entities = table; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 196 | } |
| 197 | xmlAddEntity(table, name, type, ExternalID, SystemID, content); |
| 198 | } |
| 199 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 200 | /** |
| 201 | * xmlAddDocEntity: |
| 202 | * @doc: the document |
| 203 | * @name: the entity name |
| 204 | * @type: the entity type XML_xxx_yyy_ENTITY |
| 205 | * @ExternalID: the entity external ID if available |
| 206 | * @SystemID: the entity system ID if available |
| 207 | * @content: the entity content |
| 208 | * |
| 209 | * Register a new entity for this document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 210 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 211 | void |
| 212 | xmlAddDocEntity(xmlDocPtr doc, const CHAR *name, int type, |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 213 | const CHAR *ExternalID, const CHAR *SystemID, CHAR *content) { |
| 214 | xmlEntitiesTablePtr table; |
| 215 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 216 | if (doc == NULL) { |
| 217 | fprintf(stderr, |
| 218 | "xmlAddDocEntity: document is NULL !\n"); |
| 219 | return; |
| 220 | } |
| 221 | if (doc->intSubset == NULL) { |
| 222 | fprintf(stderr, |
| 223 | "xmlAddDtdEntity: document without internal subset !\n"); |
| 224 | return; |
| 225 | } |
| 226 | table = (xmlEntitiesTablePtr) doc->intSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 227 | if (table == NULL) { |
| 228 | table = xmlCreateEntitiesTable(); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 229 | doc->intSubset->entities = table; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 230 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 231 | xmlAddEntity(table, name, type, ExternalID, SystemID, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 234 | /** |
| 235 | * xmlGetDtdEntity: |
| 236 | * @doc: the document referencing the entity |
| 237 | * @name: the entity name |
| 238 | * |
| 239 | * Do an entity lookup in the Dtd entity hash table and |
| 240 | * returns the corresponding entity, if found. |
| 241 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 242 | * Returns A pointer to the entity structure or NULL if not found. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 243 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 244 | xmlEntityPtr |
| 245 | xmlGetDtdEntity(xmlDocPtr doc, const CHAR *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 246 | int i; |
| 247 | xmlEntityPtr cur; |
| 248 | xmlEntitiesTablePtr table; |
| 249 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 250 | if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { |
| 251 | table = (xmlEntitiesTablePtr) doc->extSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 252 | for (i = 0;i < table->nb_entities;i++) { |
| 253 | cur = &table->table[i]; |
| 254 | if (!xmlStrcmp(cur->name, name)) return(cur); |
| 255 | } |
| 256 | } |
| 257 | return(NULL); |
| 258 | } |
| 259 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 260 | /** |
| 261 | * xmlGetDocEntity: |
| 262 | * @doc: the document referencing the entity |
| 263 | * @name: the entity name |
| 264 | * |
| 265 | * Do an entity lookup in the document entity hash table and |
| 266 | * returns the corrsponding entity, otherwise a lookup is done |
| 267 | * in the predefined entities too. |
| 268 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 269 | * Returns A pointer to the entity structure or NULL if not found. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 270 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 271 | xmlEntityPtr |
| 272 | xmlGetDocEntity(xmlDocPtr doc, const CHAR *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 273 | int i; |
| 274 | xmlEntityPtr cur; |
| 275 | xmlEntitiesTablePtr table; |
| 276 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 277 | if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { |
| 278 | table = (xmlEntitiesTablePtr) doc->intSubset->entities; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 279 | for (i = 0;i < table->nb_entities;i++) { |
| 280 | cur = &table->table[i]; |
| 281 | if (!xmlStrcmp(cur->name, name)) return(cur); |
| 282 | } |
| 283 | } |
| 284 | if (xmlPredefinedEntities == NULL) |
| 285 | xmlInitializePredefinedEntities(); |
| 286 | table = xmlPredefinedEntities; |
| 287 | for (i = 0;i < table->nb_entities;i++) { |
| 288 | cur = &table->table[i]; |
| 289 | if (!xmlStrcmp(cur->name, name)) return(cur); |
| 290 | } |
| 291 | |
| 292 | return(NULL); |
| 293 | } |
| 294 | |
| 295 | /* |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 296 | * [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
| 297 | * | [#x10000-#x10FFFF] |
| 298 | * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. |
| 299 | */ |
| 300 | #define IS_CHAR(c) \ |
| 301 | (((c) == 0x09) || ((c) == 0x0a) || ((c) == 0x0d) || \ |
| 302 | (((c) >= 0x20) && ((c) != 0xFFFE) && ((c) != 0xFFFF))) |
| 303 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 304 | /** |
| 305 | * xmlEncodeEntities: |
| 306 | * @doc: the document containing the string |
| 307 | * @input: A string to convert to XML. |
| 308 | * |
| 309 | * Do a global encoding of a string, replacing the predefined entities |
| 310 | * and non ASCII values with their entities and CharRef counterparts. |
| 311 | * |
Daniel Veillard | 0ba4d53 | 1998-11-01 19:34:31 +0000 | [diff] [blame] | 312 | * TODO !!!! Once moved to UTF-8 internal encoding, the encoding of non-ascii |
| 313 | * get erroneous. |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 314 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 315 | * Returns A newly allocated string with the substitution done. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 316 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 317 | CHAR * |
| 318 | xmlEncodeEntities(xmlDocPtr doc, const CHAR *input) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 319 | const CHAR *cur = input; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 320 | CHAR *buffer = NULL; |
| 321 | CHAR *out = NULL; |
| 322 | int buffer_size = 0; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 323 | |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 324 | if (input == NULL) return(NULL); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 325 | |
| 326 | /* |
| 327 | * allocate an translation buffer. |
| 328 | */ |
| 329 | buffer_size = 1000; |
| 330 | buffer = (CHAR *) malloc(buffer_size * sizeof(CHAR)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 331 | if (buffer == NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 332 | perror("malloc failed"); |
| 333 | exit(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 334 | } |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 335 | out = buffer; |
| 336 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 337 | while (*cur != '\0') { |
| 338 | if (out - buffer > buffer_size - 100) { |
| 339 | int index = out - buffer; |
| 340 | |
| 341 | growBuffer(); |
| 342 | out = &buffer[index]; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * By default one have to encode at least '<', '>', '"' and '&' ! |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 347 | */ |
| 348 | if (*cur == '<') { |
| 349 | *out++ = '&'; |
| 350 | *out++ = 'l'; |
| 351 | *out++ = 't'; |
| 352 | *out++ = ';'; |
| 353 | } else if (*cur == '>') { |
| 354 | *out++ = '&'; |
| 355 | *out++ = 'g'; |
| 356 | *out++ = 't'; |
| 357 | *out++ = ';'; |
| 358 | } else if (*cur == '&') { |
| 359 | *out++ = '&'; |
| 360 | *out++ = 'a'; |
| 361 | *out++ = 'm'; |
| 362 | *out++ = 'p'; |
| 363 | *out++ = ';'; |
| 364 | } else if (*cur == '"') { |
| 365 | *out++ = '&'; |
| 366 | *out++ = 'q'; |
| 367 | *out++ = 'u'; |
| 368 | *out++ = 'o'; |
| 369 | *out++ = 't'; |
| 370 | *out++ = ';'; |
| 371 | } else if (*cur == '\'') { |
| 372 | *out++ = '&'; |
| 373 | *out++ = 'a'; |
| 374 | *out++ = 'p'; |
| 375 | *out++ = 'o'; |
| 376 | *out++ = 's'; |
| 377 | *out++ = ';'; |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 378 | } else if (((*cur >= 0x20) && (*cur < 0x80)) || |
| 379 | (*cur == '\n') || (*cur == '\r') || (*cur == '\t')) { |
| 380 | /* |
| 381 | * default case, just copy ! |
| 382 | */ |
| 383 | *out++ = *cur; |
Daniel Veillard | 0ba4d53 | 1998-11-01 19:34:31 +0000 | [diff] [blame] | 384 | #ifndef USE_UTF_8 |
| 385 | } else if ((sizeof(CHAR) == 1) && (*cur >= 0x80)) { |
| 386 | char buf[10], *ptr; |
Daniel Veillard | da4d3c4 | 1998-11-04 20:07:05 +0000 | [diff] [blame] | 387 | #ifdef HAVE_SNPRINTF |
| 388 | snprintf(buf, 9, "&#%d;", *cur); |
| 389 | #else |
| 390 | sprintf(buf, "&#%d;", *cur); |
| 391 | #endif |
Daniel Veillard | 0ba4d53 | 1998-11-01 19:34:31 +0000 | [diff] [blame] | 392 | ptr = buf; |
| 393 | while (*ptr != 0) *out++ = *ptr++; |
| 394 | #endif |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 395 | } else if (IS_CHAR(*cur)) { |
| 396 | char buf[10], *ptr; |
| 397 | |
| 398 | #ifdef HAVE_SNPRINTF |
| 399 | snprintf(buf, 9, "&#%d;", *cur); |
| 400 | #else |
| 401 | sprintf(buf, "&#%d;", *cur); |
| 402 | #endif |
| 403 | ptr = buf; |
| 404 | while (*ptr != 0) *out++ = *ptr++; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 405 | } |
Daniel Veillard | 8cc0d1f | 1998-11-16 01:04:26 +0000 | [diff] [blame] | 406 | #if 0 |
| 407 | else { |
| 408 | /* |
| 409 | * default case, this is not a valid char ! |
| 410 | * Skip it... |
| 411 | */ |
| 412 | fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur); |
| 413 | } |
| 414 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 415 | cur++; |
| 416 | } |
| 417 | *out++ = 0; |
| 418 | return(buffer); |
| 419 | } |
| 420 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 421 | /** |
| 422 | * xmlCreateEntitiesTable: |
| 423 | * |
| 424 | * create and initialize an empty entities hash table. |
| 425 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 426 | * Returns the xmlEntitiesTablePtr just created or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 427 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 428 | xmlEntitiesTablePtr |
| 429 | xmlCreateEntitiesTable(void) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 430 | xmlEntitiesTablePtr ret; |
| 431 | |
| 432 | ret = (xmlEntitiesTablePtr) |
| 433 | malloc(sizeof(xmlEntitiesTable)); |
| 434 | if (ret == NULL) { |
| 435 | fprintf(stderr, "xmlCreateEntitiesTable : malloc(%d) failed\n", |
| 436 | sizeof(xmlEntitiesTable)); |
| 437 | return(NULL); |
| 438 | } |
| 439 | ret->max_entities = XML_MIN_ENTITIES_TABLE; |
| 440 | ret->nb_entities = 0; |
| 441 | ret->table = (xmlEntityPtr ) |
| 442 | malloc(ret->max_entities * sizeof(xmlEntity)); |
| 443 | if (ret == NULL) { |
| 444 | fprintf(stderr, "xmlCreateEntitiesTable : malloc(%d) failed\n", |
| 445 | ret->max_entities * sizeof(xmlEntity)); |
| 446 | free(ret); |
| 447 | return(NULL); |
| 448 | } |
| 449 | return(ret); |
| 450 | } |
| 451 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 452 | /** |
| 453 | * xmlFreeEntitiesTable: |
| 454 | * @table: An entity table |
| 455 | * |
| 456 | * Deallocate the memory used by an entities hash table. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 457 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 458 | void |
| 459 | xmlFreeEntitiesTable(xmlEntitiesTablePtr table) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 460 | int i; |
| 461 | |
| 462 | if (table == NULL) return; |
| 463 | |
| 464 | for (i = 0;i < table->nb_entities;i++) { |
| 465 | xmlFreeEntity(&table->table[i]); |
| 466 | } |
| 467 | free(table->table); |
| 468 | free(table); |
| 469 | } |
| 470 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 471 | /** |
| 472 | * xmlCopyEntitiesTable: |
| 473 | * @table: An entity table |
| 474 | * |
| 475 | * Build a copy of an entity table. |
| 476 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 477 | * Returns the new xmlEntitiesTablePtr or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 478 | */ |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 479 | xmlEntitiesTablePtr |
| 480 | xmlCopyEntitiesTable(xmlEntitiesTablePtr table) { |
| 481 | xmlEntitiesTablePtr ret; |
| 482 | xmlEntityPtr cur, ent; |
| 483 | int i; |
| 484 | |
| 485 | ret = (xmlEntitiesTablePtr) malloc(sizeof(xmlEntitiesTable)); |
| 486 | if (ret == NULL) { |
| 487 | fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n"); |
| 488 | return(NULL); |
| 489 | } |
| 490 | ret->table = (xmlEntityPtr) malloc(table->max_entities * |
| 491 | sizeof(xmlEntity)); |
| 492 | if (ret->table == NULL) { |
| 493 | fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n"); |
| 494 | free(ret); |
| 495 | return(NULL); |
| 496 | } |
| 497 | ret->max_entities = table->max_entities; |
| 498 | ret->nb_entities = table->nb_entities; |
| 499 | for (i = 0;i < ret->nb_entities;i++) { |
| 500 | cur = &ret->table[i]; |
| 501 | ent = &table->table[i]; |
| 502 | cur->len = ent->len; |
| 503 | cur->type = ent->type; |
| 504 | if (ent->name != NULL) |
| 505 | cur->name = xmlStrdup(ent->name); |
| 506 | else |
| 507 | cur->name = NULL; |
| 508 | if (ent->ExternalID != NULL) |
| 509 | cur->ExternalID = xmlStrdup(ent->ExternalID); |
| 510 | else |
| 511 | cur->ExternalID = NULL; |
| 512 | if (ent->SystemID != NULL) |
| 513 | cur->SystemID = xmlStrdup(ent->SystemID); |
| 514 | else |
| 515 | cur->SystemID = NULL; |
| 516 | if (ent->content != NULL) |
| 517 | cur->content = xmlStrdup(ent->content); |
| 518 | else |
| 519 | cur->content = NULL; |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 520 | if (ent->orig != NULL) |
| 521 | cur->orig = xmlStrdup(ent->orig); |
| 522 | else |
| 523 | cur->orig = NULL; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 524 | } |
| 525 | return(ret); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * xmlDumpEntitiesTable: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 530 | * @buf: An XML buffer. |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 531 | * @table: An entity table |
| 532 | * |
| 533 | * 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] | 534 | */ |
| 535 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 536 | xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 537 | int i; |
| 538 | xmlEntityPtr cur; |
| 539 | |
| 540 | if (table == NULL) return; |
| 541 | |
| 542 | for (i = 0;i < table->nb_entities;i++) { |
| 543 | cur = &table->table[i]; |
| 544 | switch (cur->type) { |
| 545 | case XML_INTERNAL_GENERAL_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 546 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 547 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 548 | xmlBufferWriteChar(buf, " "); |
| 549 | if (cur->orig != NULL) |
| 550 | xmlBufferWriteQuotedString(buf, cur->orig); |
| 551 | else |
| 552 | xmlBufferWriteQuotedString(buf, cur->content); |
| 553 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 554 | break; |
| 555 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 556 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 557 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 558 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 559 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 560 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 561 | xmlBufferWriteChar(buf, " "); |
| 562 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 563 | } else { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 564 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 565 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 566 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 567 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 568 | break; |
| 569 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 570 | xmlBufferWriteChar(buf, "<!ENTITY "); |
| 571 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 572 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 573 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 574 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 575 | xmlBufferWriteChar(buf, " "); |
| 576 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 577 | } else { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 578 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 579 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 580 | } |
| 581 | if (cur->content != NULL) { /* Should be true ! */ |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 582 | xmlBufferWriteChar(buf, " NDATA "); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 583 | if (cur->orig != NULL) |
| 584 | xmlBufferWriteCHAR(buf, cur->orig); |
| 585 | else |
| 586 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 587 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 588 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 589 | break; |
| 590 | case XML_INTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 591 | xmlBufferWriteChar(buf, "<!ENTITY % "); |
| 592 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 593 | xmlBufferWriteChar(buf, " "); |
| 594 | if (cur->orig == NULL) |
| 595 | xmlBufferWriteQuotedString(buf, cur->content); |
| 596 | else |
| 597 | xmlBufferWriteQuotedString(buf, cur->orig); |
| 598 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 599 | break; |
| 600 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 601 | xmlBufferWriteChar(buf, "<!ENTITY % "); |
| 602 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 603 | if (cur->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 604 | xmlBufferWriteChar(buf, " PUBLIC "); |
| 605 | xmlBufferWriteQuotedString(buf, cur->ExternalID); |
| 606 | xmlBufferWriteChar(buf, " "); |
| 607 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 608 | } else { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame^] | 609 | xmlBufferWriteChar(buf, " SYSTEM "); |
| 610 | xmlBufferWriteQuotedString(buf, cur->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 611 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 612 | xmlBufferWriteChar(buf, ">\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 613 | break; |
| 614 | default: |
| 615 | fprintf(stderr, |
| 616 | "xmlDumpEntitiesTable: internal: unknown type %d\n", |
| 617 | cur->type); |
| 618 | } |
| 619 | } |
| 620 | } |