Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * entities.c : implementation for the XML entities handling  | 
3  |  |  *  | 
4  |  |  * See Copyright for the status of this software.  | 
5  |  |  *  | 
6  |  |  * daniel@veillard.com  | 
7  |  |  */  | 
8  |  |  | 
9  |  | /* To avoid EBCDIC trouble when parsing on zOS */  | 
10  |  | #if defined(__MVS__)  | 
11  |  | #pragma convert("ISO8859-1") | 
12  |  | #endif  | 
13  |  |  | 
14  |  | #define IN_LIBXML  | 
15  |  | #include "libxml.h"  | 
16  |  |  | 
17  |  | #include <string.h>  | 
18  |  | #include <stdlib.h>  | 
19  |  |  | 
20  |  | #include <libxml/xmlmemory.h>  | 
21  |  | #include <libxml/hash.h>  | 
22  |  | #include <libxml/entities.h>  | 
23  |  | #include <libxml/parser.h>  | 
24  |  | #include <libxml/parserInternals.h>  | 
25  |  | #include <libxml/xmlerror.h>  | 
26  |  | #include <libxml/globals.h>  | 
27  |  | #include <libxml/dict.h>  | 
28  |  |  | 
29  |  | #include "private/entities.h"  | 
30  |  | #include "private/error.h"  | 
31  |  |  | 
32  |  | /*  | 
33  |  |  * The XML predefined entities.  | 
34  |  |  */  | 
35  |  |  | 
36  |  | static xmlEntity xmlEntityLt = { | 
37  |  |     NULL, XML_ENTITY_DECL, BAD_CAST "lt",  | 
38  |  |     NULL, NULL, NULL, NULL, NULL, NULL,  | 
39  |  |     BAD_CAST "<", BAD_CAST "<", 1,  | 
40  |  |     XML_INTERNAL_PREDEFINED_ENTITY,  | 
41  |  |     NULL, NULL, NULL, NULL, 0, 0, 0  | 
42  |  | };  | 
43  |  | static xmlEntity xmlEntityGt = { | 
44  |  |     NULL, XML_ENTITY_DECL, BAD_CAST "gt",  | 
45  |  |     NULL, NULL, NULL, NULL, NULL, NULL,  | 
46  |  |     BAD_CAST ">", BAD_CAST ">", 1,  | 
47  |  |     XML_INTERNAL_PREDEFINED_ENTITY,  | 
48  |  |     NULL, NULL, NULL, NULL, 0, 0, 0  | 
49  |  | };  | 
50  |  | static xmlEntity xmlEntityAmp = { | 
51  |  |     NULL, XML_ENTITY_DECL, BAD_CAST "amp",  | 
52  |  |     NULL, NULL, NULL, NULL, NULL, NULL,  | 
53  |  |     BAD_CAST "&", BAD_CAST "&", 1,  | 
54  |  |     XML_INTERNAL_PREDEFINED_ENTITY,  | 
55  |  |     NULL, NULL, NULL, NULL, 0, 0, 0  | 
56  |  | };  | 
57  |  | static xmlEntity xmlEntityQuot = { | 
58  |  |     NULL, XML_ENTITY_DECL, BAD_CAST "quot",  | 
59  |  |     NULL, NULL, NULL, NULL, NULL, NULL,  | 
60  |  |     BAD_CAST "\"", BAD_CAST "\"", 1,  | 
61  |  |     XML_INTERNAL_PREDEFINED_ENTITY,  | 
62  |  |     NULL, NULL, NULL, NULL, 0, 0, 0  | 
63  |  | };  | 
64  |  | static xmlEntity xmlEntityApos = { | 
65  |  |     NULL, XML_ENTITY_DECL, BAD_CAST "apos",  | 
66  |  |     NULL, NULL, NULL, NULL, NULL, NULL,  | 
67  |  |     BAD_CAST "'", BAD_CAST "'", 1,  | 
68  |  |     XML_INTERNAL_PREDEFINED_ENTITY,  | 
69  |  |     NULL, NULL, NULL, NULL, 0, 0, 0  | 
70  |  | };  | 
71  |  |  | 
72  |  | /**  | 
73  |  |  * xmlEntitiesErrMemory:  | 
74  |  |  * @extra:  extra information  | 
75  |  |  *  | 
76  |  |  * Handle an out of memory condition  | 
77  |  |  */  | 
78  |  | static void  | 
79  |  | xmlEntitiesErrMemory(const char *extra)  | 
80  | 0  | { | 
81  | 0  |     __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);  | 
82  | 0  | }  | 
83  |  |  | 
84  |  | /**  | 
85  |  |  * xmlEntitiesErr:  | 
86  |  |  * @code:  the error code  | 
87  |  |  * @msg:  the message  | 
88  |  |  *  | 
89  |  |  * Raise an error.  | 
90  |  |  */  | 
91  |  | static void LIBXML_ATTR_FORMAT(2,0)  | 
92  |  | xmlEntitiesErr(xmlParserErrors code, const char *msg)  | 
93  | 0  | { | 
94  | 0  |     __xmlSimpleError(XML_FROM_TREE, code, NULL, msg, NULL);  | 
95  | 0  | }  | 
96  |  |  | 
97  |  | /**  | 
98  |  |  * xmlEntitiesWarn:  | 
99  |  |  * @code:  the error code  | 
100  |  |  * @msg:  the message  | 
101  |  |  *  | 
102  |  |  * Raise a warning.  | 
103  |  |  */  | 
104  |  | static void LIBXML_ATTR_FORMAT(2,0)  | 
105  |  | xmlEntitiesWarn(xmlParserErrors code, const char *msg, const xmlChar *str1)  | 
106  | 4.46k  | { | 
107  | 4.46k  |     __xmlRaiseError(NULL, NULL, NULL,  | 
108  | 4.46k  |                 NULL, NULL, XML_FROM_TREE, code,  | 
109  | 4.46k  |                 XML_ERR_WARNING, NULL, 0,  | 
110  | 4.46k  |                 (const char *)str1, NULL, NULL, 0, 0,  | 
111  | 4.46k  |                 msg, (const char *)str1, NULL);  | 
112  | 4.46k  | }  | 
113  |  |  | 
114  |  | /*  | 
115  |  |  * xmlFreeEntity : clean-up an entity record.  | 
116  |  |  */  | 
117  |  | static void  | 
118  |  | xmlFreeEntity(xmlEntityPtr entity)  | 
119  | 4.67M  | { | 
120  | 4.67M  |     xmlDictPtr dict = NULL;  | 
121  |  |  | 
122  | 4.67M  |     if (entity == NULL)  | 
123  | 0  |         return;  | 
124  |  |  | 
125  | 4.67M  |     if (entity->doc != NULL)  | 
126  | 4.67M  |         dict = entity->doc->dict;  | 
127  |  |  | 
128  |  |  | 
129  | 4.67M  |     if ((entity->children) && (entity->owner == 1) &&  | 
130  | 4.67M  |         (entity == (xmlEntityPtr) entity->children->parent))  | 
131  | 179k  |         xmlFreeNodeList(entity->children);  | 
132  | 4.67M  |     if ((entity->name != NULL) &&  | 
133  | 4.67M  |         ((dict == NULL) || (!xmlDictOwns(dict, entity->name))))  | 
134  | 1.71M  |         xmlFree((char *) entity->name);  | 
135  | 4.67M  |     if (entity->ExternalID != NULL)  | 
136  | 327k  |         xmlFree((char *) entity->ExternalID);  | 
137  | 4.67M  |     if (entity->SystemID != NULL)  | 
138  | 677k  |         xmlFree((char *) entity->SystemID);  | 
139  | 4.67M  |     if (entity->URI != NULL)  | 
140  | 209k  |         xmlFree((char *) entity->URI);  | 
141  | 4.67M  |     if (entity->content != NULL)  | 
142  | 4.29M  |         xmlFree((char *) entity->content);  | 
143  | 4.67M  |     if (entity->orig != NULL)  | 
144  | 3.75M  |         xmlFree((char *) entity->orig);  | 
145  | 4.67M  |     xmlFree(entity);  | 
146  | 4.67M  | }  | 
147  |  |  | 
148  |  | /*  | 
149  |  |  * xmlCreateEntity:  | 
150  |  |  *  | 
151  |  |  * internal routine doing the entity node structures allocations  | 
152  |  |  */  | 
153  |  | static xmlEntityPtr  | 
154  |  | xmlCreateEntity(xmlDictPtr dict, const xmlChar *name, int type,  | 
155  |  |           const xmlChar *ExternalID, const xmlChar *SystemID,  | 
156  | 4.67M  |           const xmlChar *content) { | 
157  | 4.67M  |     xmlEntityPtr ret;  | 
158  |  |  | 
159  | 4.67M  |     ret = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));  | 
160  | 4.67M  |     if (ret == NULL) { | 
161  | 0  |         xmlEntitiesErrMemory("xmlCreateEntity: malloc failed"); | 
162  | 0  |   return(NULL);  | 
163  | 0  |     }  | 
164  | 4.67M  |     memset(ret, 0, sizeof(xmlEntity));  | 
165  | 4.67M  |     ret->type = XML_ENTITY_DECL;  | 
166  |  |  | 
167  |  |     /*  | 
168  |  |      * fill the structure.  | 
169  |  |      */  | 
170  | 4.67M  |     ret->etype = (xmlEntityType) type;  | 
171  | 4.67M  |     if (dict == NULL) { | 
172  | 1.71M  |   ret->name = xmlStrdup(name);  | 
173  | 1.71M  |   if (ExternalID != NULL)  | 
174  | 211k  |       ret->ExternalID = xmlStrdup(ExternalID);  | 
175  | 1.71M  |   if (SystemID != NULL)  | 
176  | 302k  |       ret->SystemID = xmlStrdup(SystemID);  | 
177  | 2.96M  |     } else { | 
178  | 2.96M  |         ret->name = xmlDictLookup(dict, name, -1);  | 
179  | 2.96M  |   ret->ExternalID = xmlStrdup(ExternalID);  | 
180  | 2.96M  |   ret->SystemID = xmlStrdup(SystemID);  | 
181  | 2.96M  |     }  | 
182  | 4.67M  |     if (content != NULL) { | 
183  | 4.29M  |         ret->length = xmlStrlen(content);  | 
184  | 4.29M  |   ret->content = xmlStrndup(content, ret->length);  | 
185  | 4.29M  |      } else { | 
186  | 388k  |         ret->length = 0;  | 
187  | 388k  |         ret->content = NULL;  | 
188  | 388k  |     }  | 
189  | 4.67M  |     ret->URI = NULL; /* to be computed by the layer knowing  | 
190  |  |       the defining entity */  | 
191  | 4.67M  |     ret->orig = NULL;  | 
192  | 4.67M  |     ret->owner = 0;  | 
193  |  |  | 
194  | 4.67M  |     return(ret);  | 
195  | 4.67M  | }  | 
196  |  |  | 
197  |  | /*  | 
198  |  |  * xmlAddEntity : register a new entity for an entities table.  | 
199  |  |  */  | 
200  |  | static xmlEntityPtr  | 
201  |  | xmlAddEntity(xmlDtdPtr dtd, const xmlChar *name, int type,  | 
202  |  |     const xmlChar *ExternalID, const xmlChar *SystemID,  | 
203  | 4.68M  |     const xmlChar *content) { | 
204  | 4.68M  |     xmlDictPtr dict = NULL;  | 
205  | 4.68M  |     xmlEntitiesTablePtr table = NULL;  | 
206  | 4.68M  |     xmlEntityPtr ret, predef;  | 
207  |  |  | 
208  | 4.68M  |     if (name == NULL)  | 
209  | 0  |   return(NULL);  | 
210  | 4.68M  |     if (dtd == NULL)  | 
211  | 0  |   return(NULL);  | 
212  | 4.68M  |     if (dtd->doc != NULL)  | 
213  | 4.68M  |         dict = dtd->doc->dict;  | 
214  |  |  | 
215  | 4.68M  |     switch (type) { | 
216  | 1.46M  |         case XML_INTERNAL_GENERAL_ENTITY:  | 
217  | 1.79M  |         case XML_EXTERNAL_GENERAL_PARSED_ENTITY:  | 
218  | 2.11M  |         case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:  | 
219  | 2.11M  |             predef = xmlGetPredefinedEntity(name);  | 
220  | 2.11M  |             if (predef != NULL) { | 
221  | 254k  |                 int valid = 0;  | 
222  |  |  | 
223  |  |                 /* 4.6 Predefined Entities */  | 
224  | 254k  |                 if ((type == XML_INTERNAL_GENERAL_ENTITY) &&  | 
225  | 254k  |                     (content != NULL)) { | 
226  | 253k  |                     int c = predef->content[0];  | 
227  |  |  | 
228  | 253k  |                     if (((content[0] == c) && (content[1] == 0)) &&  | 
229  | 253k  |                         ((c == '>') || (c == '\'') || (c == '"'))) { | 
230  | 143k  |                         valid = 1;  | 
231  | 143k  |                     } else if ((content[0] == '&') && (content[1] == '#')) { | 
232  | 107k  |                         if (content[2] == 'x') { | 
233  | 2.95k  |                             xmlChar *hex = BAD_CAST "0123456789ABCDEF";  | 
234  | 2.95k  |                             xmlChar ref[] = "00;";  | 
235  |  |  | 
236  | 2.95k  |                             ref[0] = hex[c / 16 % 16];  | 
237  | 2.95k  |                             ref[1] = hex[c % 16];  | 
238  | 2.95k  |                             if (xmlStrcasecmp(&content[3], ref) == 0)  | 
239  | 2.14k  |                                 valid = 1;  | 
240  | 104k  |                         } else { | 
241  | 104k  |                             xmlChar ref[] = "00;";  | 
242  |  |  | 
243  | 104k  |                             ref[0] = '0' + c / 10 % 10;  | 
244  | 104k  |                             ref[1] = '0' + c % 10;  | 
245  | 104k  |                             if (xmlStrEqual(&content[2], ref))  | 
246  | 104k  |                                 valid = 1;  | 
247  | 104k  |                         }  | 
248  | 107k  |                     }  | 
249  | 253k  |                 }  | 
250  | 254k  |                 if (!valid) { | 
251  | 4.46k  |                     xmlEntitiesWarn(XML_ERR_ENTITY_PROCESSING,  | 
252  | 4.46k  |                             "xmlAddEntity: invalid redeclaration of predefined"  | 
253  | 4.46k  |                             " entity '%s'", name);  | 
254  | 4.46k  |                     return(NULL);  | 
255  | 4.46k  |                 }  | 
256  | 254k  |             }  | 
257  | 2.11M  |       if (dtd->entities == NULL)  | 
258  | 248k  |     dtd->entities = xmlHashCreateDict(0, dict);  | 
259  | 2.11M  |       table = dtd->entities;  | 
260  | 2.11M  |       break;  | 
261  | 2.53M  |         case XML_INTERNAL_PARAMETER_ENTITY:  | 
262  | 2.56M  |         case XML_EXTERNAL_PARAMETER_ENTITY:  | 
263  | 2.56M  |       if (dtd->pentities == NULL)  | 
264  | 112k  |     dtd->pentities = xmlHashCreateDict(0, dict);  | 
265  | 2.56M  |       table = dtd->pentities;  | 
266  | 2.56M  |       break;  | 
267  | 0  |         case XML_INTERNAL_PREDEFINED_ENTITY:  | 
268  | 0  |       return(NULL);  | 
269  | 4.68M  |     }  | 
270  | 4.67M  |     if (table == NULL)  | 
271  | 0  |   return(NULL);  | 
272  | 4.67M  |     ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);  | 
273  | 4.67M  |     if (ret == NULL)  | 
274  | 0  |         return(NULL);  | 
275  | 4.67M  |     ret->doc = dtd->doc;  | 
276  |  |  | 
277  | 4.67M  |     if (xmlHashAddEntry(table, name, ret)) { | 
278  |  |   /*  | 
279  |  |    * entity was already defined at another level.  | 
280  |  |    */  | 
281  | 629k  |         xmlFreeEntity(ret);  | 
282  | 629k  |   return(NULL);  | 
283  | 629k  |     }  | 
284  | 4.04M  |     return(ret);  | 
285  | 4.67M  | }  | 
286  |  |  | 
287  |  | /**  | 
288  |  |  * xmlGetPredefinedEntity:  | 
289  |  |  * @name:  the entity name  | 
290  |  |  *  | 
291  |  |  * Check whether this name is an predefined entity.  | 
292  |  |  *  | 
293  |  |  * Returns NULL if not, otherwise the entity  | 
294  |  |  */  | 
295  |  | xmlEntityPtr  | 
296  | 1.19G  | xmlGetPredefinedEntity(const xmlChar *name) { | 
297  | 1.19G  |     if (name == NULL) return(NULL);  | 
298  | 1.19G  |     switch (name[0]) { | 
299  | 1.03G  |         case 'l':  | 
300  | 1.03G  |       if (xmlStrEqual(name, BAD_CAST "lt"))  | 
301  | 1.33M  |           return(&xmlEntityLt);  | 
302  | 1.03G  |       break;  | 
303  | 1.03G  |         case 'g':  | 
304  | 5.40M  |       if (xmlStrEqual(name, BAD_CAST "gt"))  | 
305  | 951k  |           return(&xmlEntityGt);  | 
306  | 4.45M  |       break;  | 
307  | 4.45M  |         case 'a':  | 
308  | 3.52M  |       if (xmlStrEqual(name, BAD_CAST "amp"))  | 
309  | 652k  |           return(&xmlEntityAmp);  | 
310  | 2.87M  |       if (xmlStrEqual(name, BAD_CAST "apos"))  | 
311  | 337k  |           return(&xmlEntityApos);  | 
312  | 2.53M  |       break;  | 
313  | 2.53M  |         case 'q':  | 
314  | 849k  |       if (xmlStrEqual(name, BAD_CAST "quot"))  | 
315  | 297k  |           return(&xmlEntityQuot);  | 
316  | 552k  |       break;  | 
317  | 146M  |   default:  | 
318  | 146M  |       break;  | 
319  | 1.19G  |     }  | 
320  | 1.19G  |     return(NULL);  | 
321  | 1.19G  | }  | 
322  |  |  | 
323  |  | /**  | 
324  |  |  * xmlAddDtdEntity:  | 
325  |  |  * @doc:  the document  | 
326  |  |  * @name:  the entity name  | 
327  |  |  * @type:  the entity type XML_xxx_yyy_ENTITY  | 
328  |  |  * @ExternalID:  the entity external ID if available  | 
329  |  |  * @SystemID:  the entity system ID if available  | 
330  |  |  * @content:  the entity content  | 
331  |  |  *  | 
332  |  |  * Register a new entity for this document DTD external subset.  | 
333  |  |  *  | 
334  |  |  * Returns a pointer to the entity or NULL in case of error  | 
335  |  |  */  | 
336  |  | xmlEntityPtr  | 
337  |  | xmlAddDtdEntity(xmlDocPtr doc, const xmlChar *name, int type,  | 
338  |  |           const xmlChar *ExternalID, const xmlChar *SystemID,  | 
339  | 2.57M  |     const xmlChar *content) { | 
340  | 2.57M  |     xmlEntityPtr ret;  | 
341  | 2.57M  |     xmlDtdPtr dtd;  | 
342  |  |  | 
343  | 2.57M  |     if (doc == NULL) { | 
344  | 0  |   xmlEntitiesErr(XML_DTD_NO_DOC,  | 
345  | 0  |           "xmlAddDtdEntity: document is NULL");  | 
346  | 0  |   return(NULL);  | 
347  | 0  |     }  | 
348  | 2.57M  |     if (doc->extSubset == NULL) { | 
349  | 0  |   xmlEntitiesErr(XML_DTD_NO_DTD,  | 
350  | 0  |           "xmlAddDtdEntity: document without external subset");  | 
351  | 0  |   return(NULL);  | 
352  | 0  |     }  | 
353  | 2.57M  |     dtd = doc->extSubset;  | 
354  | 2.57M  |     ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);  | 
355  | 2.57M  |     if (ret == NULL) return(NULL);  | 
356  |  |  | 
357  |  |     /*  | 
358  |  |      * Link it to the DTD  | 
359  |  |      */  | 
360  | 2.43M  |     ret->parent = dtd;  | 
361  | 2.43M  |     ret->doc = dtd->doc;  | 
362  | 2.43M  |     if (dtd->last == NULL) { | 
363  | 4.86k  |   dtd->children = dtd->last = (xmlNodePtr) ret;  | 
364  | 2.42M  |     } else { | 
365  | 2.42M  |         dtd->last->next = (xmlNodePtr) ret;  | 
366  | 2.42M  |   ret->prev = dtd->last;  | 
367  | 2.42M  |   dtd->last = (xmlNodePtr) ret;  | 
368  | 2.42M  |     }  | 
369  | 2.43M  |     return(ret);  | 
370  | 2.57M  | }  | 
371  |  |  | 
372  |  | /**  | 
373  |  |  * xmlAddDocEntity:  | 
374  |  |  * @doc:  the document  | 
375  |  |  * @name:  the entity name  | 
376  |  |  * @type:  the entity type XML_xxx_yyy_ENTITY  | 
377  |  |  * @ExternalID:  the entity external ID if available  | 
378  |  |  * @SystemID:  the entity system ID if available  | 
379  |  |  * @content:  the entity content  | 
380  |  |  *  | 
381  |  |  * Register a new entity for this document.  | 
382  |  |  *  | 
383  |  |  * Returns a pointer to the entity or NULL in case of error  | 
384  |  |  */  | 
385  |  | xmlEntityPtr  | 
386  |  | xmlAddDocEntity(xmlDocPtr doc, const xmlChar *name, int type,  | 
387  |  |           const xmlChar *ExternalID, const xmlChar *SystemID,  | 
388  | 2.10M  |           const xmlChar *content) { | 
389  | 2.10M  |     xmlEntityPtr ret;  | 
390  | 2.10M  |     xmlDtdPtr dtd;  | 
391  |  |  | 
392  | 2.10M  |     if (doc == NULL) { | 
393  | 0  |   xmlEntitiesErr(XML_DTD_NO_DOC,  | 
394  | 0  |           "xmlAddDocEntity: document is NULL");  | 
395  | 0  |   return(NULL);  | 
396  | 0  |     }  | 
397  | 2.10M  |     if (doc->intSubset == NULL) { | 
398  | 0  |   xmlEntitiesErr(XML_DTD_NO_DTD,  | 
399  | 0  |           "xmlAddDocEntity: document without internal subset");  | 
400  | 0  |   return(NULL);  | 
401  | 0  |     }  | 
402  | 2.10M  |     dtd = doc->intSubset;  | 
403  | 2.10M  |     ret = xmlAddEntity(dtd, name, type, ExternalID, SystemID, content);  | 
404  | 2.10M  |     if (ret == NULL) return(NULL);  | 
405  |  |  | 
406  |  |     /*  | 
407  |  |      * Link it to the DTD  | 
408  |  |      */  | 
409  | 1.61M  |     ret->parent = dtd;  | 
410  | 1.61M  |     ret->doc = dtd->doc;  | 
411  | 1.61M  |     if (dtd->last == NULL) { | 
412  | 133k  |   dtd->children = dtd->last = (xmlNodePtr) ret;  | 
413  | 1.48M  |     } else { | 
414  | 1.48M  |   dtd->last->next = (xmlNodePtr) ret;  | 
415  | 1.48M  |   ret->prev = dtd->last;  | 
416  | 1.48M  |   dtd->last = (xmlNodePtr) ret;  | 
417  | 1.48M  |     }  | 
418  | 1.61M  |     return(ret);  | 
419  | 2.10M  | }  | 
420  |  |  | 
421  |  | /**  | 
422  |  |  * xmlNewEntity:  | 
423  |  |  * @doc:  the document  | 
424  |  |  * @name:  the entity name  | 
425  |  |  * @type:  the entity type XML_xxx_yyy_ENTITY  | 
426  |  |  * @ExternalID:  the entity external ID if available  | 
427  |  |  * @SystemID:  the entity system ID if available  | 
428  |  |  * @content:  the entity content  | 
429  |  |  *  | 
430  |  |  * Create a new entity, this differs from xmlAddDocEntity() that if  | 
431  |  |  * the document is NULL or has no internal subset defined, then an  | 
432  |  |  * unlinked entity structure will be returned, it is then the responsibility  | 
433  |  |  * of the caller to link it to the document later or free it when not needed  | 
434  |  |  * anymore.  | 
435  |  |  *  | 
436  |  |  * Returns a pointer to the entity or NULL in case of error  | 
437  |  |  */  | 
438  |  | xmlEntityPtr  | 
439  |  | xmlNewEntity(xmlDocPtr doc, const xmlChar *name, int type,  | 
440  |  |        const xmlChar *ExternalID, const xmlChar *SystemID,  | 
441  | 0  |        const xmlChar *content) { | 
442  | 0  |     xmlEntityPtr ret;  | 
443  | 0  |     xmlDictPtr dict;  | 
444  |  | 
  | 
445  | 0  |     if ((doc != NULL) && (doc->intSubset != NULL)) { | 
446  | 0  |   return(xmlAddDocEntity(doc, name, type, ExternalID, SystemID, content));  | 
447  | 0  |     }  | 
448  | 0  |     if (doc != NULL)  | 
449  | 0  |         dict = doc->dict;  | 
450  | 0  |     else  | 
451  | 0  |         dict = NULL;  | 
452  | 0  |     ret = xmlCreateEntity(dict, name, type, ExternalID, SystemID, content);  | 
453  | 0  |     if (ret == NULL)  | 
454  | 0  |         return(NULL);  | 
455  | 0  |     ret->doc = doc;  | 
456  | 0  |     return(ret);  | 
457  | 0  | }  | 
458  |  |  | 
459  |  | /**  | 
460  |  |  * xmlGetEntityFromTable:  | 
461  |  |  * @table:  an entity table  | 
462  |  |  * @name:  the entity name  | 
463  |  |  * @parameter:  look for parameter entities  | 
464  |  |  *  | 
465  |  |  * Do an entity lookup in the table.  | 
466  |  |  * returns the corresponding parameter entity, if found.  | 
467  |  |  *  | 
468  |  |  * Returns A pointer to the entity structure or NULL if not found.  | 
469  |  |  */  | 
470  |  | static xmlEntityPtr  | 
471  | 1.02G  | xmlGetEntityFromTable(xmlEntitiesTablePtr table, const xmlChar *name) { | 
472  | 1.02G  |     return((xmlEntityPtr) xmlHashLookup(table, name));  | 
473  | 1.02G  | }  | 
474  |  |  | 
475  |  | /**  | 
476  |  |  * xmlGetParameterEntity:  | 
477  |  |  * @doc:  the document referencing the entity  | 
478  |  |  * @name:  the entity name  | 
479  |  |  *  | 
480  |  |  * Do an entity lookup in the internal and external subsets and  | 
481  |  |  * returns the corresponding parameter entity, if found.  | 
482  |  |  *  | 
483  |  |  * Returns A pointer to the entity structure or NULL if not found.  | 
484  |  |  */  | 
485  |  | xmlEntityPtr  | 
486  | 390M  | xmlGetParameterEntity(xmlDocPtr doc, const xmlChar *name) { | 
487  | 390M  |     xmlEntitiesTablePtr table;  | 
488  | 390M  |     xmlEntityPtr ret;  | 
489  |  |  | 
490  | 390M  |     if (doc == NULL)  | 
491  | 1.82k  |   return(NULL);  | 
492  | 390M  |     if ((doc->intSubset != NULL) && (doc->intSubset->pentities != NULL)) { | 
493  | 381M  |   table = (xmlEntitiesTablePtr) doc->intSubset->pentities;  | 
494  | 381M  |   ret = xmlGetEntityFromTable(table, name);  | 
495  | 381M  |   if (ret != NULL)  | 
496  | 308M  |       return(ret);  | 
497  | 381M  |     }  | 
498  | 82.1M  |     if ((doc->extSubset != NULL) && (doc->extSubset->pentities != NULL)) { | 
499  | 9.48M  |   table = (xmlEntitiesTablePtr) doc->extSubset->pentities;  | 
500  | 9.48M  |   return(xmlGetEntityFromTable(table, name));  | 
501  | 9.48M  |     }  | 
502  | 72.6M  |     return(NULL);  | 
503  | 82.1M  | }  | 
504  |  |  | 
505  |  | /**  | 
506  |  |  * xmlGetDtdEntity:  | 
507  |  |  * @doc:  the document referencing the entity  | 
508  |  |  * @name:  the entity name  | 
509  |  |  *  | 
510  |  |  * Do an entity lookup in the DTD entity hash table and  | 
511  |  |  * returns the corresponding entity, if found.  | 
512  |  |  * Note: the first argument is the document node, not the DTD node.  | 
513  |  |  *  | 
514  |  |  * Returns A pointer to the entity structure or NULL if not found.  | 
515  |  |  */  | 
516  |  | xmlEntityPtr  | 
517  | 0  | xmlGetDtdEntity(xmlDocPtr doc, const xmlChar *name) { | 
518  | 0  |     xmlEntitiesTablePtr table;  | 
519  |  | 
  | 
520  | 0  |     if (doc == NULL)  | 
521  | 0  |   return(NULL);  | 
522  | 0  |     if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) { | 
523  | 0  |   table = (xmlEntitiesTablePtr) doc->extSubset->entities;  | 
524  | 0  |   return(xmlGetEntityFromTable(table, name));  | 
525  | 0  |     }  | 
526  | 0  |     return(NULL);  | 
527  | 0  | }  | 
528  |  |  | 
529  |  | /**  | 
530  |  |  * xmlGetDocEntity:  | 
531  |  |  * @doc:  the document referencing the entity  | 
532  |  |  * @name:  the entity name  | 
533  |  |  *  | 
534  |  |  * Do an entity lookup in the document entity hash table and  | 
535  |  |  * returns the corresponding entity, otherwise a lookup is done  | 
536  |  |  * in the predefined entities too.  | 
537  |  |  *  | 
538  |  |  * Returns A pointer to the entity structure or NULL if not found.  | 
539  |  |  */  | 
540  |  | xmlEntityPtr  | 
541  | 635M  | xmlGetDocEntity(const xmlDoc *doc, const xmlChar *name) { | 
542  | 635M  |     xmlEntityPtr cur;  | 
543  | 635M  |     xmlEntitiesTablePtr table;  | 
544  |  |  | 
545  | 635M  |     if (doc != NULL) { | 
546  | 635M  |   if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) { | 
547  | 634M  |       table = (xmlEntitiesTablePtr) doc->intSubset->entities;  | 
548  | 634M  |       cur = xmlGetEntityFromTable(table, name);  | 
549  | 634M  |       if (cur != NULL)  | 
550  | 452M  |     return(cur);  | 
551  | 634M  |   }  | 
552  | 183M  |   if (doc->standalone != 1) { | 
553  | 183M  |       if ((doc->extSubset != NULL) &&  | 
554  | 183M  |     (doc->extSubset->entities != NULL)) { | 
555  | 4.20M  |     table = (xmlEntitiesTablePtr) doc->extSubset->entities;  | 
556  | 4.20M  |     cur = xmlGetEntityFromTable(table, name);  | 
557  | 4.20M  |     if (cur != NULL)  | 
558  | 267k  |         return(cur);  | 
559  | 4.20M  |       }  | 
560  | 183M  |   }  | 
561  | 183M  |     }  | 
562  | 182M  |     return(xmlGetPredefinedEntity(name));  | 
563  | 635M  | }  | 
564  |  |  | 
565  |  | /*  | 
566  |  |  * Macro used to grow the current buffer.  | 
567  |  |  */  | 
568  | 1.36k  | #define growBufferReentrant() {           \ | 
569  | 1.36k  |     xmlChar *tmp;                                                       \  | 
570  | 1.36k  |     size_t new_size = buffer_size * 2;                                  \  | 
571  | 1.36k  |     if (new_size < buffer_size) goto mem_error;                         \  | 
572  | 1.36k  |     tmp = (xmlChar *) xmlRealloc(buffer, new_size);                 \  | 
573  | 1.36k  |     if (tmp == NULL) goto mem_error;                                    \  | 
574  | 1.36k  |     buffer = tmp;             \  | 
575  | 1.36k  |     buffer_size = new_size;           \  | 
576  | 1.36k  | }  | 
577  |  |  | 
578  |  | /**  | 
579  |  |  * xmlEncodeEntitiesInternal:  | 
580  |  |  * @doc:  the document containing the string  | 
581  |  |  * @input:  A string to convert to XML.  | 
582  |  |  * @attr: are we handling an attribute value  | 
583  |  |  *  | 
584  |  |  * Do a global encoding of a string, replacing the predefined entities  | 
585  |  |  * and non ASCII values with their entities and CharRef counterparts.  | 
586  |  |  * Contrary to xmlEncodeEntities, this routine is reentrant, and result  | 
587  |  |  * must be deallocated.  | 
588  |  |  *  | 
589  |  |  * Returns A newly allocated string with the substitution done.  | 
590  |  |  */  | 
591  |  | static xmlChar *  | 
592  | 5.44k  | xmlEncodeEntitiesInternal(xmlDocPtr doc, const xmlChar *input, int attr) { | 
593  | 5.44k  |     const xmlChar *cur = input;  | 
594  | 5.44k  |     xmlChar *buffer = NULL;  | 
595  | 5.44k  |     xmlChar *out = NULL;  | 
596  | 5.44k  |     size_t buffer_size = 0;  | 
597  | 5.44k  |     int html = 0;  | 
598  |  |  | 
599  | 5.44k  |     if (input == NULL) return(NULL);  | 
600  | 5.44k  |     if (doc != NULL)  | 
601  | 5.44k  |         html = (doc->type == XML_HTML_DOCUMENT_NODE);  | 
602  |  |  | 
603  |  |     /*  | 
604  |  |      * allocate an translation buffer.  | 
605  |  |      */  | 
606  | 5.44k  |     buffer_size = 1000;  | 
607  | 5.44k  |     buffer = (xmlChar *) xmlMalloc(buffer_size);  | 
608  | 5.44k  |     if (buffer == NULL) { | 
609  | 0  |         xmlEntitiesErrMemory("xmlEncodeEntities: malloc failed"); | 
610  | 0  |   return(NULL);  | 
611  | 0  |     }  | 
612  | 5.44k  |     out = buffer;  | 
613  |  |  | 
614  | 2.98M  |     while (*cur != '\0') { | 
615  | 2.98M  |         size_t indx = out - buffer;  | 
616  | 2.98M  |         if (indx + 100 > buffer_size) { | 
617  |  |  | 
618  | 2.73k  |       growBufferReentrant();  | 
619  | 2.73k  |       out = &buffer[indx];  | 
620  | 2.73k  |   }  | 
621  |  |  | 
622  |  |   /*  | 
623  |  |    * By default one have to encode at least '<', '>', '"' and '&' !  | 
624  |  |    */  | 
625  | 2.98M  |   if (*cur == '<') { | 
626  | 735  |       const xmlChar *end;  | 
627  |  |  | 
628  |  |       /*  | 
629  |  |        * Special handling of server side include in HTML attributes  | 
630  |  |        */  | 
631  | 735  |       if (html && attr &&  | 
632  | 735  |           (cur[1] == '!') && (cur[2] == '-') && (cur[3] == '-') &&  | 
633  | 735  |           ((end = xmlStrstr(cur, BAD_CAST "-->")) != NULL)) { | 
634  | 0  |           while (cur != end) { | 
635  | 0  |         *out++ = *cur++;  | 
636  | 0  |         indx = out - buffer;  | 
637  | 0  |         if (indx + 100 > buffer_size) { | 
638  | 0  |       growBufferReentrant();  | 
639  | 0  |       out = &buffer[indx];  | 
640  | 0  |         }  | 
641  | 0  |     }  | 
642  | 0  |     *out++ = *cur++;  | 
643  | 0  |     *out++ = *cur++;  | 
644  | 0  |     *out++ = *cur++;  | 
645  | 0  |     continue;  | 
646  | 0  |       }  | 
647  | 735  |       *out++ = '&';  | 
648  | 735  |       *out++ = 'l';  | 
649  | 735  |       *out++ = 't';  | 
650  | 735  |       *out++ = ';';  | 
651  | 2.98M  |   } else if (*cur == '>') { | 
652  | 289k  |       *out++ = '&';  | 
653  | 289k  |       *out++ = 'g';  | 
654  | 289k  |       *out++ = 't';  | 
655  | 289k  |       *out++ = ';';  | 
656  | 2.69M  |   } else if (*cur == '&') { | 
657  |  |       /*  | 
658  |  |        * Special handling of &{...} construct from HTML 4, see | 
659  |  |        * http://www.w3.org/TR/html401/appendix/notes.html#h-B.7.1  | 
660  |  |        */  | 
661  | 926  |       if (html && attr && (cur[1] == '{') && | 
662  | 926  |           (strchr((const char *) cur, '}'))) { | 
663  | 0  |           while (*cur != '}') { | 
664  | 0  |         *out++ = *cur++;  | 
665  | 0  |         indx = out - buffer;  | 
666  | 0  |         if (indx + 100 > buffer_size) { | 
667  | 0  |       growBufferReentrant();  | 
668  | 0  |       out = &buffer[indx];  | 
669  | 0  |         }  | 
670  | 0  |     }  | 
671  | 0  |     *out++ = *cur++;  | 
672  | 0  |     continue;  | 
673  | 0  |       }  | 
674  | 926  |       *out++ = '&';  | 
675  | 926  |       *out++ = 'a';  | 
676  | 926  |       *out++ = 'm';  | 
677  | 926  |       *out++ = 'p';  | 
678  | 926  |       *out++ = ';';  | 
679  | 2.69M  |   } else if (((*cur >= 0x20) && (*cur < 0x80)) ||  | 
680  | 2.69M  |       (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) { | 
681  |  |       /*  | 
682  |  |        * default case, just copy !  | 
683  |  |        */  | 
684  | 2.23M  |       *out++ = *cur;  | 
685  | 2.23M  |   } else if (*cur >= 0x80) { | 
686  | 451k  |       if (((doc != NULL) && (doc->encoding != NULL)) || (html)) { | 
687  |  |     /*  | 
688  |  |      * Bjørn Reese <br@sseusa.com> provided the patch  | 
689  |  |           xmlChar xc;  | 
690  |  |           xc = (*cur & 0x3F) << 6;  | 
691  |  |           if (cur[1] != 0) { | 
692  |  |         xc += *(++cur) & 0x3F;  | 
693  |  |         *out++ = xc;  | 
694  |  |           } else  | 
695  |  |      */  | 
696  | 356k  |     *out++ = *cur;  | 
697  | 356k  |       } else { | 
698  |  |     /*  | 
699  |  |      * We assume we have UTF-8 input.  | 
700  |  |      * It must match either:  | 
701  |  |      *   110xxxxx 10xxxxxx  | 
702  |  |      *   1110xxxx 10xxxxxx 10xxxxxx  | 
703  |  |      *   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx  | 
704  |  |      * That is:  | 
705  |  |      *   cur[0] is 11xxxxxx  | 
706  |  |      *   cur[1] is 10xxxxxx  | 
707  |  |      *   cur[2] is 10xxxxxx if cur[0] is 111xxxxx  | 
708  |  |      *   cur[3] is 10xxxxxx if cur[0] is 1111xxxx  | 
709  |  |      *   cur[0] is not 11111xxx  | 
710  |  |      */  | 
711  | 94.9k  |     char buf[11], *ptr;  | 
712  | 94.9k  |     int val = 0, l = 1;  | 
713  |  |  | 
714  | 94.9k  |     if (((cur[0] & 0xC0) != 0xC0) ||  | 
715  | 94.9k  |         ((cur[1] & 0xC0) != 0x80) ||  | 
716  | 94.9k  |         (((cur[0] & 0xE0) == 0xE0) && ((cur[2] & 0xC0) != 0x80)) ||  | 
717  | 94.9k  |         (((cur[0] & 0xF0) == 0xF0) && ((cur[3] & 0xC0) != 0x80)) ||  | 
718  | 94.9k  |         (((cur[0] & 0xF8) == 0xF8))) { | 
719  | 0  |         xmlEntitiesErr(XML_CHECK_NOT_UTF8,  | 
720  | 0  |           "xmlEncodeEntities: input not UTF-8");  | 
721  | 0  |         if (doc != NULL)  | 
722  | 0  |       doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");  | 
723  | 0  |         snprintf(buf, sizeof(buf), "&#%d;", *cur);  | 
724  | 0  |         buf[sizeof(buf) - 1] = 0;  | 
725  | 0  |         ptr = buf;  | 
726  | 0  |         while (*ptr != 0) *out++ = *ptr++;  | 
727  | 0  |         cur++;  | 
728  | 0  |         continue;  | 
729  | 94.9k  |     } else if (*cur < 0xE0) { | 
730  | 91.8k  |                     val = (cur[0]) & 0x1F;  | 
731  | 91.8k  |         val <<= 6;  | 
732  | 91.8k  |         val |= (cur[1]) & 0x3F;  | 
733  | 91.8k  |         l = 2;  | 
734  | 91.8k  |     } else if (*cur < 0xF0) { | 
735  | 2.77k  |                     val = (cur[0]) & 0x0F;  | 
736  | 2.77k  |         val <<= 6;  | 
737  | 2.77k  |         val |= (cur[1]) & 0x3F;  | 
738  | 2.77k  |         val <<= 6;  | 
739  | 2.77k  |         val |= (cur[2]) & 0x3F;  | 
740  | 2.77k  |         l = 3;  | 
741  | 2.77k  |     } else if (*cur < 0xF8) { | 
742  | 309  |                     val = (cur[0]) & 0x07;  | 
743  | 309  |         val <<= 6;  | 
744  | 309  |         val |= (cur[1]) & 0x3F;  | 
745  | 309  |         val <<= 6;  | 
746  | 309  |         val |= (cur[2]) & 0x3F;  | 
747  | 309  |         val <<= 6;  | 
748  | 309  |         val |= (cur[3]) & 0x3F;  | 
749  | 309  |         l = 4;  | 
750  | 309  |     }  | 
751  | 94.9k  |     if ((l == 1) || (!IS_CHAR(val))) { | 
752  | 0  |         xmlEntitiesErr(XML_ERR_INVALID_CHAR,  | 
753  | 0  |       "xmlEncodeEntities: char out of range\n");  | 
754  | 0  |         if (doc != NULL)  | 
755  | 0  |       doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");  | 
756  | 0  |         snprintf(buf, sizeof(buf), "&#%d;", *cur);  | 
757  | 0  |         buf[sizeof(buf) - 1] = 0;  | 
758  | 0  |         ptr = buf;  | 
759  | 0  |         while (*ptr != 0) *out++ = *ptr++;  | 
760  | 0  |         cur++;  | 
761  | 0  |         continue;  | 
762  | 0  |     }  | 
763  |  |     /*  | 
764  |  |      * We could do multiple things here. Just save as a char ref  | 
765  |  |      */  | 
766  | 94.9k  |     snprintf(buf, sizeof(buf), "&#x%X;", val);  | 
767  | 94.9k  |     buf[sizeof(buf) - 1] = 0;  | 
768  | 94.9k  |     ptr = buf;  | 
769  | 673k  |     while (*ptr != 0) *out++ = *ptr++;  | 
770  | 94.9k  |     cur += l;  | 
771  | 94.9k  |     continue;  | 
772  | 94.9k  |       }  | 
773  | 451k  |   } else if (IS_BYTE_CHAR(*cur)) { | 
774  | 39  |       char buf[11], *ptr;  | 
775  |  |  | 
776  | 39  |       snprintf(buf, sizeof(buf), "&#%d;", *cur);  | 
777  | 39  |       buf[sizeof(buf) - 1] = 0;  | 
778  | 39  |             ptr = buf;  | 
779  | 234  |       while (*ptr != 0) *out++ = *ptr++;  | 
780  | 39  |   }  | 
781  | 2.88M  |   cur++;  | 
782  | 2.88M  |     }  | 
783  | 5.44k  |     *out = 0;  | 
784  | 5.44k  |     return(buffer);  | 
785  |  |  | 
786  | 0  | mem_error:  | 
787  | 0  |     xmlEntitiesErrMemory("xmlEncodeEntities: realloc failed"); | 
788  | 0  |     xmlFree(buffer);  | 
789  | 0  |     return(NULL);  | 
790  | 5.44k  | }  | 
791  |  |  | 
792  |  | /**  | 
793  |  |  * xmlEncodeAttributeEntities:  | 
794  |  |  * @doc:  the document containing the string  | 
795  |  |  * @input:  A string to convert to XML.  | 
796  |  |  *  | 
797  |  |  * Do a global encoding of a string, replacing the predefined entities  | 
798  |  |  * and non ASCII values with their entities and CharRef counterparts for  | 
799  |  |  * attribute values.  | 
800  |  |  *  | 
801  |  |  * Returns A newly allocated string with the substitution done.  | 
802  |  |  */  | 
803  |  | xmlChar *  | 
804  | 5.44k  | xmlEncodeAttributeEntities(xmlDocPtr doc, const xmlChar *input) { | 
805  | 5.44k  |     return xmlEncodeEntitiesInternal(doc, input, 1);  | 
806  | 5.44k  | }  | 
807  |  |  | 
808  |  | /**  | 
809  |  |  * xmlEncodeEntitiesReentrant:  | 
810  |  |  * @doc:  the document containing the string  | 
811  |  |  * @input:  A string to convert to XML.  | 
812  |  |  *  | 
813  |  |  * Do a global encoding of a string, replacing the predefined entities  | 
814  |  |  * and non ASCII values with their entities and CharRef counterparts.  | 
815  |  |  * Contrary to xmlEncodeEntities, this routine is reentrant, and result  | 
816  |  |  * must be deallocated.  | 
817  |  |  *  | 
818  |  |  * Returns A newly allocated string with the substitution done.  | 
819  |  |  */  | 
820  |  | xmlChar *  | 
821  | 0  | xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) { | 
822  | 0  |     return xmlEncodeEntitiesInternal(doc, input, 0);  | 
823  | 0  | }  | 
824  |  |  | 
825  |  | /**  | 
826  |  |  * xmlEncodeSpecialChars:  | 
827  |  |  * @doc:  the document containing the string  | 
828  |  |  * @input:  A string to convert to XML.  | 
829  |  |  *  | 
830  |  |  * Do a global encoding of a string, replacing the predefined entities  | 
831  |  |  * this routine is reentrant, and result must be deallocated.  | 
832  |  |  *  | 
833  |  |  * Returns A newly allocated string with the substitution done.  | 
834  |  |  */  | 
835  |  | xmlChar *  | 
836  | 0  | xmlEncodeSpecialChars(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlChar *input) { | 
837  | 0  |     const xmlChar *cur = input;  | 
838  | 0  |     xmlChar *buffer = NULL;  | 
839  | 0  |     xmlChar *out = NULL;  | 
840  | 0  |     size_t buffer_size = 0;  | 
841  | 0  |     if (input == NULL) return(NULL);  | 
842  |  |  | 
843  |  |     /*  | 
844  |  |      * allocate an translation buffer.  | 
845  |  |      */  | 
846  | 0  |     buffer_size = 1000;  | 
847  | 0  |     buffer = (xmlChar *) xmlMalloc(buffer_size);  | 
848  | 0  |     if (buffer == NULL) { | 
849  | 0  |         xmlEntitiesErrMemory("xmlEncodeSpecialChars: malloc failed"); | 
850  | 0  |   return(NULL);  | 
851  | 0  |     }  | 
852  | 0  |     out = buffer;  | 
853  |  | 
  | 
854  | 0  |     while (*cur != '\0') { | 
855  | 0  |         size_t indx = out - buffer;  | 
856  | 0  |         if (indx + 10 > buffer_size) { | 
857  |  | 
  | 
858  | 0  |       growBufferReentrant();  | 
859  | 0  |       out = &buffer[indx];  | 
860  | 0  |   }  | 
861  |  |  | 
862  |  |   /*  | 
863  |  |    * By default one have to encode at least '<', '>', '"' and '&' !  | 
864  |  |    */  | 
865  | 0  |   if (*cur == '<') { | 
866  | 0  |       *out++ = '&';  | 
867  | 0  |       *out++ = 'l';  | 
868  | 0  |       *out++ = 't';  | 
869  | 0  |       *out++ = ';';  | 
870  | 0  |   } else if (*cur == '>') { | 
871  | 0  |       *out++ = '&';  | 
872  | 0  |       *out++ = 'g';  | 
873  | 0  |       *out++ = 't';  | 
874  | 0  |       *out++ = ';';  | 
875  | 0  |   } else if (*cur == '&') { | 
876  | 0  |       *out++ = '&';  | 
877  | 0  |       *out++ = 'a';  | 
878  | 0  |       *out++ = 'm';  | 
879  | 0  |       *out++ = 'p';  | 
880  | 0  |       *out++ = ';';  | 
881  | 0  |   } else if (*cur == '"') { | 
882  | 0  |       *out++ = '&';  | 
883  | 0  |       *out++ = 'q';  | 
884  | 0  |       *out++ = 'u';  | 
885  | 0  |       *out++ = 'o';  | 
886  | 0  |       *out++ = 't';  | 
887  | 0  |       *out++ = ';';  | 
888  | 0  |   } else if (*cur == '\r') { | 
889  | 0  |       *out++ = '&';  | 
890  | 0  |       *out++ = '#';  | 
891  | 0  |       *out++ = '1';  | 
892  | 0  |       *out++ = '3';  | 
893  | 0  |       *out++ = ';';  | 
894  | 0  |   } else { | 
895  |  |       /*  | 
896  |  |        * Works because on UTF-8, all extended sequences cannot  | 
897  |  |        * result in bytes in the ASCII range.  | 
898  |  |        */  | 
899  | 0  |       *out++ = *cur;  | 
900  | 0  |   }  | 
901  | 0  |   cur++;  | 
902  | 0  |     }  | 
903  | 0  |     *out = 0;  | 
904  | 0  |     return(buffer);  | 
905  |  |  | 
906  | 0  | mem_error:  | 
907  | 0  |     xmlEntitiesErrMemory("xmlEncodeSpecialChars: realloc failed"); | 
908  | 0  |     xmlFree(buffer);  | 
909  | 0  |     return(NULL);  | 
910  | 0  | }  | 
911  |  |  | 
912  |  | /**  | 
913  |  |  * xmlCreateEntitiesTable:  | 
914  |  |  *  | 
915  |  |  * create and initialize an empty entities hash table.  | 
916  |  |  * This really doesn't make sense and should be deprecated  | 
917  |  |  *  | 
918  |  |  * Returns the xmlEntitiesTablePtr just created or NULL in case of error.  | 
919  |  |  */  | 
920  |  | xmlEntitiesTablePtr  | 
921  | 0  | xmlCreateEntitiesTable(void) { | 
922  | 0  |     return((xmlEntitiesTablePtr) xmlHashCreate(0));  | 
923  | 0  | }  | 
924  |  |  | 
925  |  | /**  | 
926  |  |  * xmlFreeEntityWrapper:  | 
927  |  |  * @entity:  An entity  | 
928  |  |  * @name:  its name  | 
929  |  |  *  | 
930  |  |  * Deallocate the memory used by an entities in the hash table.  | 
931  |  |  */  | 
932  |  | static void  | 
933  | 4.04M  | xmlFreeEntityWrapper(void *entity, const xmlChar *name ATTRIBUTE_UNUSED) { | 
934  | 4.04M  |     if (entity != NULL)  | 
935  | 4.04M  |   xmlFreeEntity((xmlEntityPtr) entity);  | 
936  | 4.04M  | }  | 
937  |  |  | 
938  |  | /**  | 
939  |  |  * xmlFreeEntitiesTable:  | 
940  |  |  * @table:  An entity table  | 
941  |  |  *  | 
942  |  |  * Deallocate the memory used by an entities hash table.  | 
943  |  |  */  | 
944  |  | void  | 
945  | 360k  | xmlFreeEntitiesTable(xmlEntitiesTablePtr table) { | 
946  | 360k  |     xmlHashFree(table, xmlFreeEntityWrapper);  | 
947  | 360k  | }  | 
948  |  |  | 
949  |  | #ifdef LIBXML_TREE_ENABLED  | 
950  |  | /**  | 
951  |  |  * xmlCopyEntity:  | 
952  |  |  * @ent:  An entity  | 
953  |  |  *  | 
954  |  |  * Build a copy of an entity  | 
955  |  |  *  | 
956  |  |  * Returns the new xmlEntitiesPtr or NULL in case of error.  | 
957  |  |  */  | 
958  |  | static void *  | 
959  | 0  | xmlCopyEntity(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { | 
960  | 0  |     xmlEntityPtr ent = (xmlEntityPtr) payload;  | 
961  | 0  |     xmlEntityPtr cur;  | 
962  |  | 
  | 
963  | 0  |     cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));  | 
964  | 0  |     if (cur == NULL) { | 
965  | 0  |         xmlEntitiesErrMemory("xmlCopyEntity:: malloc failed"); | 
966  | 0  |   return(NULL);  | 
967  | 0  |     }  | 
968  | 0  |     memset(cur, 0, sizeof(xmlEntity));  | 
969  | 0  |     cur->type = XML_ENTITY_DECL;  | 
970  |  | 
  | 
971  | 0  |     cur->etype = ent->etype;  | 
972  | 0  |     if (ent->name != NULL)  | 
973  | 0  |   cur->name = xmlStrdup(ent->name);  | 
974  | 0  |     if (ent->ExternalID != NULL)  | 
975  | 0  |   cur->ExternalID = xmlStrdup(ent->ExternalID);  | 
976  | 0  |     if (ent->SystemID != NULL)  | 
977  | 0  |   cur->SystemID = xmlStrdup(ent->SystemID);  | 
978  | 0  |     if (ent->content != NULL)  | 
979  | 0  |   cur->content = xmlStrdup(ent->content);  | 
980  | 0  |     if (ent->orig != NULL)  | 
981  | 0  |   cur->orig = xmlStrdup(ent->orig);  | 
982  | 0  |     if (ent->URI != NULL)  | 
983  | 0  |   cur->URI = xmlStrdup(ent->URI);  | 
984  | 0  |     return(cur);  | 
985  | 0  | }  | 
986  |  |  | 
987  |  | /**  | 
988  |  |  * xmlCopyEntitiesTable:  | 
989  |  |  * @table:  An entity table  | 
990  |  |  *  | 
991  |  |  * Build a copy of an entity table.  | 
992  |  |  *  | 
993  |  |  * Returns the new xmlEntitiesTablePtr or NULL in case of error.  | 
994  |  |  */  | 
995  |  | xmlEntitiesTablePtr  | 
996  | 0  | xmlCopyEntitiesTable(xmlEntitiesTablePtr table) { | 
997  | 0  |     return(xmlHashCopy(table, xmlCopyEntity));  | 
998  | 0  | }  | 
999  |  | #endif /* LIBXML_TREE_ENABLED */  | 
1000  |  |  | 
1001  |  | #ifdef LIBXML_OUTPUT_ENABLED  | 
1002  |  |  | 
1003  |  | /**  | 
1004  |  |  * xmlDumpEntityContent:  | 
1005  |  |  * @buf:  An XML buffer.  | 
1006  |  |  * @content:  The entity content.  | 
1007  |  |  *  | 
1008  |  |  * This will dump the quoted string value, taking care of the special  | 
1009  |  |  * treatment required by %  | 
1010  |  |  */  | 
1011  |  | static void  | 
1012  | 4.65k  | xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) { | 
1013  | 4.65k  |     if (xmlStrchr(content, '%')) { | 
1014  | 0  |         const xmlChar * base, *cur;  | 
1015  |  | 
  | 
1016  | 0  |   xmlBufferCCat(buf, "\"");  | 
1017  | 0  |   base = cur = content;  | 
1018  | 0  |   while (*cur != 0) { | 
1019  | 0  |       if (*cur == '"') { | 
1020  | 0  |     if (base != cur)  | 
1021  | 0  |         xmlBufferAdd(buf, base, cur - base);  | 
1022  | 0  |     xmlBufferAdd(buf, BAD_CAST """, 6);  | 
1023  | 0  |     cur++;  | 
1024  | 0  |     base = cur;  | 
1025  | 0  |       } else if (*cur == '%') { | 
1026  | 0  |     if (base != cur)  | 
1027  | 0  |         xmlBufferAdd(buf, base, cur - base);  | 
1028  | 0  |     xmlBufferAdd(buf, BAD_CAST "%", 6);  | 
1029  | 0  |     cur++;  | 
1030  | 0  |     base = cur;  | 
1031  | 0  |       } else { | 
1032  | 0  |     cur++;  | 
1033  | 0  |       }  | 
1034  | 0  |   }  | 
1035  | 0  |   if (base != cur)  | 
1036  | 0  |       xmlBufferAdd(buf, base, cur - base);  | 
1037  | 0  |   xmlBufferCCat(buf, "\"");  | 
1038  | 4.65k  |     } else { | 
1039  | 4.65k  |         xmlBufferWriteQuotedString(buf, content);  | 
1040  | 4.65k  |     }  | 
1041  | 4.65k  | }  | 
1042  |  |  | 
1043  |  | /**  | 
1044  |  |  * xmlDumpEntityDecl:  | 
1045  |  |  * @buf:  An XML buffer.  | 
1046  |  |  * @ent:  An entity table  | 
1047  |  |  *  | 
1048  |  |  * This will dump the content of the entity table as an XML DTD definition  | 
1049  |  |  */  | 
1050  |  | void  | 
1051  | 367k  | xmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) { | 
1052  | 367k  |     if ((buf == NULL) || (ent == NULL)) return;  | 
1053  | 367k  |     switch (ent->etype) { | 
1054  | 233k  |   case XML_INTERNAL_GENERAL_ENTITY:  | 
1055  | 233k  |       xmlBufferWriteChar(buf, "<!ENTITY ");  | 
1056  | 233k  |       xmlBufferWriteCHAR(buf, ent->name);  | 
1057  | 233k  |       xmlBufferWriteChar(buf, " ");  | 
1058  | 233k  |       if (ent->orig != NULL)  | 
1059  | 228k  |     xmlBufferWriteQuotedString(buf, ent->orig);  | 
1060  | 4.65k  |       else  | 
1061  | 4.65k  |     xmlDumpEntityContent(buf, ent->content);  | 
1062  | 233k  |       xmlBufferWriteChar(buf, ">\n");  | 
1063  | 233k  |       break;  | 
1064  | 33.4k  |   case XML_EXTERNAL_GENERAL_PARSED_ENTITY:  | 
1065  | 33.4k  |       xmlBufferWriteChar(buf, "<!ENTITY ");  | 
1066  | 33.4k  |       xmlBufferWriteCHAR(buf, ent->name);  | 
1067  | 33.4k  |       if (ent->ExternalID != NULL) { | 
1068  | 3.09k  |      xmlBufferWriteChar(buf, " PUBLIC ");  | 
1069  | 3.09k  |      xmlBufferWriteQuotedString(buf, ent->ExternalID);  | 
1070  | 3.09k  |      xmlBufferWriteChar(buf, " ");  | 
1071  | 3.09k  |      xmlBufferWriteQuotedString(buf, ent->SystemID);  | 
1072  | 30.3k  |       } else { | 
1073  | 30.3k  |      xmlBufferWriteChar(buf, " SYSTEM ");  | 
1074  | 30.3k  |      xmlBufferWriteQuotedString(buf, ent->SystemID);  | 
1075  | 30.3k  |       }  | 
1076  | 33.4k  |       xmlBufferWriteChar(buf, ">\n");  | 
1077  | 33.4k  |       break;  | 
1078  | 1.86k  |   case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:  | 
1079  | 1.86k  |       xmlBufferWriteChar(buf, "<!ENTITY ");  | 
1080  | 1.86k  |       xmlBufferWriteCHAR(buf, ent->name);  | 
1081  | 1.86k  |       if (ent->ExternalID != NULL) { | 
1082  | 633  |      xmlBufferWriteChar(buf, " PUBLIC ");  | 
1083  | 633  |      xmlBufferWriteQuotedString(buf, ent->ExternalID);  | 
1084  | 633  |      xmlBufferWriteChar(buf, " ");  | 
1085  | 633  |      xmlBufferWriteQuotedString(buf, ent->SystemID);  | 
1086  | 1.23k  |       } else { | 
1087  | 1.23k  |      xmlBufferWriteChar(buf, " SYSTEM ");  | 
1088  | 1.23k  |      xmlBufferWriteQuotedString(buf, ent->SystemID);  | 
1089  | 1.23k  |       }  | 
1090  | 1.86k  |       if (ent->content != NULL) { /* Should be true ! */ | 
1091  | 1.77k  |     xmlBufferWriteChar(buf, " NDATA ");  | 
1092  | 1.77k  |     if (ent->orig != NULL)  | 
1093  | 102  |         xmlBufferWriteCHAR(buf, ent->orig);  | 
1094  | 1.67k  |     else  | 
1095  | 1.67k  |         xmlBufferWriteCHAR(buf, ent->content);  | 
1096  | 1.77k  |       }  | 
1097  | 1.86k  |       xmlBufferWriteChar(buf, ">\n");  | 
1098  | 1.86k  |       break;  | 
1099  | 93.1k  |   case XML_INTERNAL_PARAMETER_ENTITY:  | 
1100  | 93.1k  |       xmlBufferWriteChar(buf, "<!ENTITY % ");  | 
1101  | 93.1k  |       xmlBufferWriteCHAR(buf, ent->name);  | 
1102  | 93.1k  |       xmlBufferWriteChar(buf, " ");  | 
1103  | 93.1k  |       if (ent->orig == NULL)  | 
1104  | 0  |     xmlDumpEntityContent(buf, ent->content);  | 
1105  | 93.1k  |       else  | 
1106  | 93.1k  |     xmlBufferWriteQuotedString(buf, ent->orig);  | 
1107  | 93.1k  |       xmlBufferWriteChar(buf, ">\n");  | 
1108  | 93.1k  |       break;  | 
1109  | 5.57k  |   case XML_EXTERNAL_PARAMETER_ENTITY:  | 
1110  | 5.57k  |       xmlBufferWriteChar(buf, "<!ENTITY % ");  | 
1111  | 5.57k  |       xmlBufferWriteCHAR(buf, ent->name);  | 
1112  | 5.57k  |       if (ent->ExternalID != NULL) { | 
1113  | 133  |      xmlBufferWriteChar(buf, " PUBLIC ");  | 
1114  | 133  |      xmlBufferWriteQuotedString(buf, ent->ExternalID);  | 
1115  | 133  |      xmlBufferWriteChar(buf, " ");  | 
1116  | 133  |      xmlBufferWriteQuotedString(buf, ent->SystemID);  | 
1117  | 5.44k  |       } else { | 
1118  | 5.44k  |      xmlBufferWriteChar(buf, " SYSTEM ");  | 
1119  | 5.44k  |      xmlBufferWriteQuotedString(buf, ent->SystemID);  | 
1120  | 5.44k  |       }  | 
1121  | 5.57k  |       xmlBufferWriteChar(buf, ">\n");  | 
1122  | 5.57k  |       break;  | 
1123  | 0  |   default:  | 
1124  | 0  |       xmlEntitiesErr(XML_DTD_UNKNOWN_ENTITY,  | 
1125  | 0  |     "xmlDumpEntitiesDecl: internal: unknown type entity type");  | 
1126  | 367k  |     }  | 
1127  | 367k  | }  | 
1128  |  |  | 
1129  |  | /**  | 
1130  |  |  * xmlDumpEntityDeclScan:  | 
1131  |  |  * @ent:  An entity table  | 
1132  |  |  * @buf:  An XML buffer.  | 
1133  |  |  *  | 
1134  |  |  * When using the hash table scan function, arguments need to be reversed  | 
1135  |  |  */  | 
1136  |  | static void  | 
1137  |  | xmlDumpEntityDeclScan(void *ent, void *buf,  | 
1138  | 0  |                       const xmlChar *name ATTRIBUTE_UNUSED) { | 
1139  | 0  |     xmlDumpEntityDecl((xmlBufferPtr) buf, (xmlEntityPtr) ent);  | 
1140  | 0  | }  | 
1141  |  |  | 
1142  |  | /**  | 
1143  |  |  * xmlDumpEntitiesTable:  | 
1144  |  |  * @buf:  An XML buffer.  | 
1145  |  |  * @table:  An entity table  | 
1146  |  |  *  | 
1147  |  |  * This will dump the content of the entity table as an XML DTD definition  | 
1148  |  |  */  | 
1149  |  | void  | 
1150  | 0  | xmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) { | 
1151  | 0  |     xmlHashScan(table, xmlDumpEntityDeclScan, buf);  | 
1152  | 0  | }  | 
1153  |  | #endif /* LIBXML_OUTPUT_ENABLED */  |