Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SAX2.c : Default SAX2 handler to build a tree. |
3 | | * |
4 | | * See Copyright for the status of this software. |
5 | | * |
6 | | * Author: Daniel Veillard |
7 | | */ |
8 | | |
9 | | |
10 | | #define IN_LIBXML |
11 | | #include "libxml.h" |
12 | | #include <stdlib.h> |
13 | | #include <string.h> |
14 | | #include <limits.h> |
15 | | #include <stddef.h> |
16 | | #include <libxml/SAX2.h> |
17 | | #include <libxml/xmlmemory.h> |
18 | | #include <libxml/tree.h> |
19 | | #include <libxml/parser.h> |
20 | | #include <libxml/parserInternals.h> |
21 | | #include <libxml/valid.h> |
22 | | #include <libxml/entities.h> |
23 | | #include <libxml/xmlerror.h> |
24 | | #include <libxml/xmlIO.h> |
25 | | #include <libxml/uri.h> |
26 | | #include <libxml/valid.h> |
27 | | #include <libxml/HTMLtree.h> |
28 | | |
29 | | #include "private/error.h" |
30 | | #include "private/parser.h" |
31 | | #include "private/tree.h" |
32 | | |
33 | | /* |
34 | | * @param ctxt an XML validation parser context |
35 | | * @param msg a string to accompany the error message |
36 | | */ |
37 | | static void |
38 | 1.50k | xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt) { |
39 | 1.50k | xmlCtxtErrMemory(ctxt); |
40 | 1.50k | } |
41 | | |
42 | | #ifdef LIBXML_VALID_ENABLED |
43 | | /** |
44 | | * Handle a validation error |
45 | | * |
46 | | * @param ctxt an XML validation parser context |
47 | | * @param error the error number |
48 | | * @param msg the error message |
49 | | * @param str1 extra data |
50 | | * @param str2 extra data |
51 | | */ |
52 | | static void LIBXML_ATTR_FORMAT(3,0) |
53 | | xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
54 | | const char *msg, const xmlChar *str1, const xmlChar *str2) |
55 | | { |
56 | | xmlCtxtErr(ctxt, NULL, XML_FROM_DTD, error, XML_ERR_ERROR, |
57 | | str1, str2, NULL, 0, msg, str1, str2); |
58 | | if (ctxt != NULL) |
59 | | ctxt->valid = 0; |
60 | | } |
61 | | #endif /* LIBXML_VALID_ENABLED */ |
62 | | |
63 | | /** |
64 | | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
65 | | * |
66 | | * @param ctxt an XML parser context |
67 | | * @param error the error number |
68 | | * @param msg the error message |
69 | | * @param str1 an error string |
70 | | * @param str2 an error string |
71 | | */ |
72 | | static void LIBXML_ATTR_FORMAT(3,0) |
73 | | xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
74 | | const char *msg, const xmlChar *str1, const xmlChar *str2) |
75 | 113 | { |
76 | 113 | xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
77 | 113 | str1, str2, NULL, 0, msg, str1, str2); |
78 | 113 | } |
79 | | |
80 | | /** |
81 | | * Handle an xml:id error |
82 | | * |
83 | | * @param ctxt an XML validation parser context |
84 | | * @param error the error number |
85 | | * @param msg the error message |
86 | | * @param str1 extra data |
87 | | */ |
88 | | static void LIBXML_ATTR_FORMAT(3,0) |
89 | | xmlErrId(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *msg, |
90 | | const xmlChar *str1) |
91 | 7.80k | { |
92 | 7.80k | xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_ERROR, |
93 | 7.80k | str1, NULL, NULL, 0, msg, str1); |
94 | 7.80k | } |
95 | | |
96 | | /** |
97 | | * Handle a parser warning |
98 | | * |
99 | | * @param ctxt an XML parser context |
100 | | * @param error the error number |
101 | | * @param msg the error message |
102 | | * @param str1 an error string |
103 | | */ |
104 | | static void LIBXML_ATTR_FORMAT(3,0) |
105 | | xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
106 | | const char *msg, const xmlChar *str1) |
107 | 4.81k | { |
108 | 4.81k | xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_WARNING, |
109 | 4.81k | str1, NULL, NULL, 0, msg, str1); |
110 | 4.81k | } |
111 | | |
112 | | /** |
113 | | * Handle a namespace warning |
114 | | * |
115 | | * @param ctxt an XML parser context |
116 | | * @param error the error number |
117 | | * @param msg the error message |
118 | | * @param str1 an error string |
119 | | * @param str2 an error string |
120 | | */ |
121 | | static void LIBXML_ATTR_FORMAT(3,0) |
122 | | xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
123 | | const char *msg, const xmlChar *str1, const xmlChar *str2) |
124 | 0 | { |
125 | 0 | xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_WARNING, |
126 | 0 | str1, str2, NULL, 0, msg, str1, str2); |
127 | 0 | } |
128 | | |
129 | | /** |
130 | | * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN" |
131 | | * |
132 | | * @param ctx the user data (XML parser context) |
133 | | * @returns a xmlChar * |
134 | | */ |
135 | | const xmlChar * |
136 | | xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED) |
137 | 0 | { |
138 | | /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */ |
139 | 0 | return(NULL); |
140 | 0 | } |
141 | | |
142 | | /** |
143 | | * Provides the system ID, basically URL or filename e.g. |
144 | | * http://www.sgmlsource.com/dtds/memo.dtd |
145 | | * |
146 | | * @param ctx the user data (XML parser context) |
147 | | * @returns a xmlChar * |
148 | | */ |
149 | | const xmlChar * |
150 | | xmlSAX2GetSystemId(void *ctx) |
151 | 0 | { |
152 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
153 | 0 | if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL); |
154 | 0 | return((const xmlChar *) ctxt->input->filename); |
155 | 0 | } |
156 | | |
157 | | /** |
158 | | * Provide the line number of the current parsing point. |
159 | | * |
160 | | * @param ctx the user data (XML parser context) |
161 | | * @returns an int |
162 | | */ |
163 | | int |
164 | | xmlSAX2GetLineNumber(void *ctx) |
165 | 0 | { |
166 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
167 | 0 | if ((ctx == NULL) || (ctxt->input == NULL)) return(0); |
168 | 0 | return(ctxt->input->line); |
169 | 0 | } |
170 | | |
171 | | /** |
172 | | * Provide the column number of the current parsing point. |
173 | | * |
174 | | * @param ctx the user data (XML parser context) |
175 | | * @returns an int |
176 | | */ |
177 | | int |
178 | | xmlSAX2GetColumnNumber(void *ctx) |
179 | 0 | { |
180 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
181 | 0 | if ((ctx == NULL) || (ctxt->input == NULL)) return(0); |
182 | 0 | return(ctxt->input->col); |
183 | 0 | } |
184 | | |
185 | | /** |
186 | | * Is this document tagged standalone ? |
187 | | * |
188 | | * @param ctx the user data (XML parser context) |
189 | | * @returns 1 if true |
190 | | */ |
191 | | int |
192 | | xmlSAX2IsStandalone(void *ctx) |
193 | 0 | { |
194 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
195 | 0 | if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0); |
196 | 0 | return(ctxt->myDoc->standalone == 1); |
197 | 0 | } |
198 | | |
199 | | /** |
200 | | * Does this document has an internal subset |
201 | | * |
202 | | * @param ctx the user data (XML parser context) |
203 | | * @returns 1 if true |
204 | | */ |
205 | | int |
206 | | xmlSAX2HasInternalSubset(void *ctx) |
207 | 0 | { |
208 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
209 | 0 | if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0); |
210 | 0 | return(ctxt->myDoc->intSubset != NULL); |
211 | 0 | } |
212 | | |
213 | | /** |
214 | | * Does this document has an external subset |
215 | | * |
216 | | * @param ctx the user data (XML parser context) |
217 | | * @returns 1 if true |
218 | | */ |
219 | | int |
220 | | xmlSAX2HasExternalSubset(void *ctx) |
221 | 0 | { |
222 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
223 | 0 | if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0); |
224 | 0 | return(ctxt->myDoc->extSubset != NULL); |
225 | 0 | } |
226 | | |
227 | | /** |
228 | | * Callback on internal subset declaration. |
229 | | * |
230 | | * @param ctx the user data (XML parser context) |
231 | | * @param name the root element name |
232 | | * @param publicId public identifier of the DTD (optional) |
233 | | * @param systemId system identifier (URL) of the DTD |
234 | | */ |
235 | | void |
236 | | xmlSAX2InternalSubset(void *ctx, const xmlChar *name, |
237 | | const xmlChar *publicId, const xmlChar *systemId) |
238 | 108k | { |
239 | 108k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
240 | 108k | xmlDtdPtr dtd; |
241 | 108k | if (ctx == NULL) return; |
242 | | |
243 | 108k | if (ctxt->myDoc == NULL) |
244 | 0 | return; |
245 | 108k | if ((ctxt->html) && (ctxt->instate != XML_PARSER_MISC)) |
246 | 0 | return; |
247 | 108k | dtd = xmlGetIntSubset(ctxt->myDoc); |
248 | 108k | if (dtd != NULL) { |
249 | 0 | xmlUnlinkNode((xmlNodePtr) dtd); |
250 | 0 | xmlFreeDtd(dtd); |
251 | 0 | ctxt->myDoc->intSubset = NULL; |
252 | 0 | } |
253 | 108k | ctxt->myDoc->intSubset = |
254 | 108k | xmlCreateIntSubset(ctxt->myDoc, name, publicId, systemId); |
255 | 108k | if (ctxt->myDoc->intSubset == NULL) |
256 | 21 | xmlSAX2ErrMemory(ctxt); |
257 | 108k | } |
258 | | |
259 | | /** |
260 | | * Callback on external subset declaration. |
261 | | * |
262 | | * @param ctx the user data (XML parser context) |
263 | | * @param name the root element name |
264 | | * @param publicId public identifier of the DTD (optional) |
265 | | * @param systemId system identifier (URL) of the DTD |
266 | | */ |
267 | | void |
268 | | xmlSAX2ExternalSubset(void *ctx, const xmlChar *name, |
269 | | const xmlChar *publicId, const xmlChar *systemId) |
270 | 37.6k | { |
271 | 37.6k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
272 | 37.6k | if (ctx == NULL) return; |
273 | 37.6k | if ((systemId != NULL) && |
274 | 37.6k | ((ctxt->options & XML_PARSE_NO_XXE) == 0) && |
275 | 37.6k | (((ctxt->validate) || (ctxt->loadsubset & ~XML_SKIP_IDS)) && |
276 | 17.3k | (ctxt->wellFormed && ctxt->myDoc))) { |
277 | | /* |
278 | | * Try to fetch and parse the external subset. |
279 | | */ |
280 | 17.3k | xmlParserInputPtr oldinput; |
281 | 17.3k | int oldinputNr; |
282 | 17.3k | int oldinputMax; |
283 | 17.3k | xmlParserInputPtr *oldinputTab; |
284 | 17.3k | xmlParserInputPtr input = NULL; |
285 | 17.3k | xmlChar *oldencoding; |
286 | 17.3k | unsigned long consumed; |
287 | 17.3k | size_t buffered; |
288 | 17.3k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
289 | 17.3k | int inputMax = 1; |
290 | | #else |
291 | | int inputMax = 5; |
292 | | #endif |
293 | | |
294 | | /* |
295 | | * Ask the Entity resolver to load the damn thing |
296 | | */ |
297 | 17.3k | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
298 | 17.3k | input = ctxt->sax->resolveEntity(ctxt->userData, publicId, |
299 | 17.3k | systemId); |
300 | 17.3k | if (input == NULL) { |
301 | 2.62k | return; |
302 | 2.62k | } |
303 | | |
304 | 14.7k | if (xmlNewDtd(ctxt->myDoc, name, publicId, systemId) == NULL) { |
305 | 6 | xmlSAX2ErrMemory(ctxt); |
306 | 6 | xmlFreeInputStream(input); |
307 | 6 | return; |
308 | 6 | } |
309 | | |
310 | | /* |
311 | | * make sure we won't destroy the main document context |
312 | | */ |
313 | 14.7k | oldinput = ctxt->input; |
314 | 14.7k | oldinputNr = ctxt->inputNr; |
315 | 14.7k | oldinputMax = ctxt->inputMax; |
316 | 14.7k | oldinputTab = ctxt->inputTab; |
317 | 14.7k | oldencoding = ctxt->encoding; |
318 | 14.7k | ctxt->encoding = NULL; |
319 | | |
320 | 14.7k | ctxt->inputTab = xmlMalloc(inputMax * sizeof(xmlParserInputPtr)); |
321 | 14.7k | if (ctxt->inputTab == NULL) { |
322 | 1 | xmlSAX2ErrMemory(ctxt); |
323 | 1 | goto error; |
324 | 1 | } |
325 | 14.7k | ctxt->inputNr = 0; |
326 | 14.7k | ctxt->inputMax = inputMax; |
327 | 14.7k | ctxt->input = NULL; |
328 | 14.7k | if (xmlCtxtPushInput(ctxt, input) < 0) |
329 | 0 | goto error; |
330 | | |
331 | 14.7k | if (input->filename == NULL) |
332 | 14.7k | input->filename = (char *) xmlCanonicPath(systemId); |
333 | 14.7k | input->line = 1; |
334 | 14.7k | input->col = 1; |
335 | 14.7k | input->base = ctxt->input->cur; |
336 | 14.7k | input->cur = ctxt->input->cur; |
337 | 14.7k | input->free = NULL; |
338 | | |
339 | | /* |
340 | | * let's parse that entity knowing it's an external subset. |
341 | | */ |
342 | 14.7k | xmlParseExternalSubset(ctxt, publicId, systemId); |
343 | | |
344 | | /* |
345 | | * Free up the external entities |
346 | | */ |
347 | | |
348 | 16.7k | while (ctxt->inputNr > 1) |
349 | 2.02k | xmlFreeInputStream(xmlCtxtPopInput(ctxt)); |
350 | | |
351 | 14.7k | consumed = ctxt->input->consumed; |
352 | 14.7k | buffered = ctxt->input->cur - ctxt->input->base; |
353 | 14.7k | if (buffered > ULONG_MAX - consumed) |
354 | 0 | consumed = ULONG_MAX; |
355 | 14.7k | else |
356 | 14.7k | consumed += buffered; |
357 | 14.7k | if (consumed > ULONG_MAX - ctxt->sizeentities) |
358 | 0 | ctxt->sizeentities = ULONG_MAX; |
359 | 14.7k | else |
360 | 14.7k | ctxt->sizeentities += consumed; |
361 | | |
362 | 14.7k | error: |
363 | 14.7k | xmlFreeInputStream(input); |
364 | 14.7k | xmlFree(ctxt->inputTab); |
365 | | |
366 | | /* |
367 | | * Restore the parsing context of the main entity |
368 | | */ |
369 | 14.7k | ctxt->input = oldinput; |
370 | 14.7k | ctxt->inputNr = oldinputNr; |
371 | 14.7k | ctxt->inputMax = oldinputMax; |
372 | 14.7k | ctxt->inputTab = oldinputTab; |
373 | 14.7k | if (ctxt->encoding != NULL) |
374 | 5.05k | xmlFree(ctxt->encoding); |
375 | 14.7k | ctxt->encoding = oldencoding; |
376 | | /* ctxt->wellFormed = oldwellFormed; */ |
377 | 14.7k | } |
378 | 37.6k | } |
379 | | |
380 | | /** |
381 | | * This is only used to load DTDs. The preferred way to install |
382 | | * custom resolvers is #xmlCtxtSetResourceLoader. |
383 | | * |
384 | | * @param ctx the user data (XML parser context) |
385 | | * @param publicId The public ID of the entity |
386 | | * @param systemId The system ID (URL) of the entity |
387 | | * @returns a parser input. |
388 | | */ |
389 | | xmlParserInput * |
390 | | xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, |
391 | | const xmlChar *systemId) |
392 | 17.3k | { |
393 | 17.3k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
394 | 17.3k | xmlParserInputPtr ret = NULL; |
395 | 17.3k | xmlChar *URI = NULL; |
396 | | |
397 | 17.3k | if (ctx == NULL) return(NULL); |
398 | | |
399 | 17.3k | if (systemId != NULL) { |
400 | 17.3k | const xmlChar *base = NULL; |
401 | 17.3k | int res; |
402 | | |
403 | 17.3k | if (ctxt->input != NULL) |
404 | 17.3k | base = BAD_CAST ctxt->input->filename; |
405 | | |
406 | | /* |
407 | | * We don't really need the 'directory' struct member, but some |
408 | | * users set it manually to a base URI for memory streams. |
409 | | */ |
410 | 17.3k | if (base == NULL) |
411 | 16.2k | base = BAD_CAST ctxt->directory; |
412 | | |
413 | 17.3k | if ((xmlStrlen(systemId) > XML_MAX_URI_LENGTH) || |
414 | 17.3k | (xmlStrlen(base) > XML_MAX_URI_LENGTH)) { |
415 | 238 | xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long"); |
416 | 238 | return(NULL); |
417 | 238 | } |
418 | 17.1k | res = xmlBuildURISafe(systemId, base, &URI); |
419 | 17.1k | if (URI == NULL) { |
420 | 54 | if (res < 0) |
421 | 3 | xmlSAX2ErrMemory(ctxt); |
422 | 51 | else |
423 | 51 | xmlWarnMsg(ctxt, XML_ERR_INVALID_URI, |
424 | 51 | "Can't resolve URI: %s\n", systemId); |
425 | 54 | return(NULL); |
426 | 54 | } |
427 | 17.1k | if (xmlStrlen(URI) > XML_MAX_URI_LENGTH) { |
428 | 5 | xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long"); |
429 | 5 | xmlFree(URI); |
430 | 5 | return(NULL); |
431 | 5 | } |
432 | 17.1k | } |
433 | | |
434 | 17.0k | ret = xmlLoadResource(ctxt, (const char *) URI, |
435 | 17.0k | (const char *) publicId, XML_RESOURCE_DTD); |
436 | | |
437 | 17.0k | xmlFree(URI); |
438 | 17.0k | return(ret); |
439 | 17.3k | } |
440 | | |
441 | | /** |
442 | | * Get an entity by name |
443 | | * |
444 | | * @param ctx the user data (XML parser context) |
445 | | * @param name The entity name |
446 | | * @returns the xmlEntity if found. |
447 | | */ |
448 | | xmlEntity * |
449 | | xmlSAX2GetEntity(void *ctx, const xmlChar *name) |
450 | 865k | { |
451 | 865k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
452 | 865k | xmlEntityPtr ret = NULL; |
453 | | |
454 | 865k | if (ctx == NULL) return(NULL); |
455 | | |
456 | 865k | if (ctxt->inSubset == 0) { |
457 | 809k | ret = xmlGetPredefinedEntity(name); |
458 | 809k | if (ret != NULL) |
459 | 0 | return(ret); |
460 | 809k | } |
461 | 865k | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) { |
462 | 74 | if (ctxt->inSubset == 2) { |
463 | 0 | ctxt->myDoc->standalone = 0; |
464 | 0 | ret = xmlGetDocEntity(ctxt->myDoc, name); |
465 | 0 | ctxt->myDoc->standalone = 1; |
466 | 74 | } else { |
467 | 74 | ret = xmlGetDocEntity(ctxt->myDoc, name); |
468 | 74 | if (ret == NULL) { |
469 | 74 | ctxt->myDoc->standalone = 0; |
470 | 74 | ret = xmlGetDocEntity(ctxt->myDoc, name); |
471 | 74 | if (ret != NULL) { |
472 | 0 | xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE, |
473 | 0 | "Entity(%s) document marked standalone but requires external subset\n", |
474 | 0 | name, NULL); |
475 | 0 | } |
476 | 74 | ctxt->myDoc->standalone = 1; |
477 | 74 | } |
478 | 74 | } |
479 | 865k | } else { |
480 | 865k | ret = xmlGetDocEntity(ctxt->myDoc, name); |
481 | 865k | } |
482 | 865k | return(ret); |
483 | 865k | } |
484 | | |
485 | | /** |
486 | | * Get a parameter entity by name |
487 | | * |
488 | | * @param ctx the user data (XML parser context) |
489 | | * @param name The entity name |
490 | | * @returns the xmlEntity if found. |
491 | | */ |
492 | | xmlEntity * |
493 | | xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name) |
494 | 140k | { |
495 | 140k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
496 | 140k | xmlEntityPtr ret; |
497 | | |
498 | 140k | if (ctx == NULL) return(NULL); |
499 | | |
500 | 140k | ret = xmlGetParameterEntity(ctxt->myDoc, name); |
501 | 140k | return(ret); |
502 | 140k | } |
503 | | |
504 | | |
505 | | /** |
506 | | * An entity definition has been parsed |
507 | | * |
508 | | * @param ctx the user data (XML parser context) |
509 | | * @param name the entity name |
510 | | * @param type the entity type |
511 | | * @param publicId The public ID of the entity |
512 | | * @param systemId The system ID of the entity |
513 | | * @param content the entity value (without processing). |
514 | | */ |
515 | | void |
516 | | xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type, |
517 | | const xmlChar *publicId, const xmlChar *systemId, xmlChar *content) |
518 | 139k | { |
519 | 139k | xmlEntityPtr ent; |
520 | 139k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
521 | 139k | int extSubset; |
522 | 139k | int res; |
523 | | |
524 | 139k | if ((ctxt == NULL) || (ctxt->myDoc == NULL)) |
525 | 0 | return; |
526 | | |
527 | 139k | extSubset = ctxt->inSubset == 2; |
528 | 139k | res = xmlAddEntity(ctxt->myDoc, extSubset, name, type, publicId, systemId, |
529 | 139k | content, &ent); |
530 | 139k | switch (res) { |
531 | 124k | case XML_ERR_OK: |
532 | 124k | break; |
533 | 34 | case XML_ERR_NO_MEMORY: |
534 | 34 | xmlSAX2ErrMemory(ctxt); |
535 | 34 | return; |
536 | 11.4k | case XML_WAR_ENTITY_REDEFINED: |
537 | 11.4k | if (ctxt->pedantic) { |
538 | 0 | if (extSubset) |
539 | 0 | xmlWarnMsg(ctxt, res, "Entity(%s) already defined in the" |
540 | 0 | " external subset\n", name); |
541 | 0 | else |
542 | 0 | xmlWarnMsg(ctxt, res, "Entity(%s) already defined in the" |
543 | 0 | " internal subset\n", name); |
544 | 0 | } |
545 | 11.4k | return; |
546 | 3.72k | case XML_ERR_REDECL_PREDEF_ENTITY: |
547 | | /* |
548 | | * Technically an error but it's a common mistake to get double |
549 | | * escaping according to "4.6 Predefined Entities" wrong. |
550 | | */ |
551 | 3.72k | xmlWarnMsg(ctxt, res, "Invalid redeclaration of predefined" |
552 | 3.72k | " entity '%s'", name); |
553 | 3.72k | return; |
554 | 0 | default: |
555 | 0 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
556 | 0 | "Unexpected error code from xmlAddEntity\n", |
557 | 0 | NULL, NULL); |
558 | 0 | return; |
559 | 139k | } |
560 | | |
561 | 124k | if ((ent->URI == NULL) && (systemId != NULL)) { |
562 | 46.3k | xmlChar *URI; |
563 | 46.3k | const char *base = NULL; |
564 | 46.3k | int i; |
565 | | |
566 | 90.0k | for (i = ctxt->inputNr - 1; i >= 0; i--) { |
567 | 46.3k | if (ctxt->inputTab[i]->filename != NULL) { |
568 | 2.65k | base = ctxt->inputTab[i]->filename; |
569 | 2.65k | break; |
570 | 2.65k | } |
571 | 46.3k | } |
572 | | |
573 | | /* |
574 | | * We don't really need the 'directory' struct member, but some |
575 | | * users set it manually to a base URI for memory streams. |
576 | | */ |
577 | 46.3k | if (base == NULL) |
578 | 43.7k | base = ctxt->directory; |
579 | | |
580 | 46.3k | res = xmlBuildURISafe(systemId, (const xmlChar *) base, &URI); |
581 | | |
582 | 46.3k | if (URI == NULL) { |
583 | 1.04k | if (res < 0) { |
584 | 2 | xmlSAX2ErrMemory(ctxt); |
585 | 1.04k | } else { |
586 | 1.04k | xmlWarnMsg(ctxt, XML_ERR_INVALID_URI, |
587 | 1.04k | "Can't resolve URI: %s\n", systemId); |
588 | 1.04k | } |
589 | 45.3k | } else if (xmlStrlen(URI) > XML_MAX_URI_LENGTH) { |
590 | 317 | xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long"); |
591 | 317 | xmlFree(URI); |
592 | 44.9k | } else { |
593 | 44.9k | ent->URI = URI; |
594 | 44.9k | } |
595 | 46.3k | } |
596 | 124k | } |
597 | | |
598 | | /** |
599 | | * An attribute definition has been parsed |
600 | | * |
601 | | * @param ctx the user data (XML parser context) |
602 | | * @param elem the name of the element |
603 | | * @param fullname the attribute name |
604 | | * @param type the attribute type |
605 | | * @param def the type of default value |
606 | | * @param defaultValue the attribute default value |
607 | | * @param tree the tree of enumerated value set |
608 | | */ |
609 | | void |
610 | | xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname, |
611 | | int type, int def, const xmlChar *defaultValue, |
612 | | xmlEnumeration *tree) |
613 | 52.3k | { |
614 | 52.3k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
615 | 52.3k | xmlAttributePtr attr; |
616 | 52.3k | const xmlChar *name = NULL; |
617 | 52.3k | xmlChar *prefix = NULL; |
618 | | |
619 | | /* Avoid unused variable warning if features are disabled. */ |
620 | 52.3k | (void) attr; |
621 | | |
622 | 52.3k | if ((ctxt == NULL) || (ctxt->myDoc == NULL)) |
623 | 0 | return; |
624 | | |
625 | 52.3k | if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) && |
626 | 52.3k | (type != XML_ATTRIBUTE_ID)) { |
627 | 87 | xmlErrId(ctxt, XML_DTD_XMLID_TYPE, |
628 | 87 | "xml:id : attribute type should be ID\n", NULL); |
629 | 87 | } |
630 | 52.3k | name = xmlSplitQName4(fullname, &prefix); |
631 | 52.3k | if (name == NULL) |
632 | 4 | xmlSAX2ErrMemory(ctxt); |
633 | 52.3k | ctxt->vctxt.valid = 1; |
634 | 52.3k | if (ctxt->inSubset == 1) |
635 | 50.8k | attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem, |
636 | 50.8k | name, prefix, (xmlAttributeType) type, |
637 | 50.8k | (xmlAttributeDefault) def, defaultValue, tree); |
638 | 1.45k | else if (ctxt->inSubset == 2) |
639 | 1.45k | attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem, |
640 | 1.45k | name, prefix, (xmlAttributeType) type, |
641 | 1.45k | (xmlAttributeDefault) def, defaultValue, tree); |
642 | 0 | else { |
643 | 0 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
644 | 0 | "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n", |
645 | 0 | name, NULL); |
646 | 0 | xmlFree(prefix); |
647 | 0 | xmlFreeEnumeration(tree); |
648 | 0 | return; |
649 | 0 | } |
650 | | #ifdef LIBXML_VALID_ENABLED |
651 | | if (ctxt->vctxt.valid == 0) |
652 | | ctxt->valid = 0; |
653 | | if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) && |
654 | | (ctxt->myDoc->intSubset != NULL)) |
655 | | ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc, |
656 | | attr); |
657 | | #endif /* LIBXML_VALID_ENABLED */ |
658 | 52.3k | if (prefix != NULL) |
659 | 12.9k | xmlFree(prefix); |
660 | 52.3k | } |
661 | | |
662 | | /** |
663 | | * An element definition has been parsed |
664 | | * |
665 | | * @param ctx the user data (XML parser context) |
666 | | * @param name the element name |
667 | | * @param type the element type |
668 | | * @param content the element value tree |
669 | | */ |
670 | | void |
671 | | xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type, |
672 | | xmlElementContent *content) |
673 | 36.6k | { |
674 | 36.6k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
675 | 36.6k | xmlElementPtr elem = NULL; |
676 | | |
677 | | /* Avoid unused variable warning if features are disabled. */ |
678 | 36.6k | (void) elem; |
679 | | |
680 | 36.6k | if ((ctxt == NULL) || (ctxt->myDoc == NULL)) |
681 | 0 | return; |
682 | | |
683 | 36.6k | if (ctxt->inSubset == 1) |
684 | 33.7k | elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, |
685 | 33.7k | name, (xmlElementTypeVal) type, content); |
686 | 2.95k | else if (ctxt->inSubset == 2) |
687 | 2.95k | elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, |
688 | 2.95k | name, (xmlElementTypeVal) type, content); |
689 | 0 | else { |
690 | 0 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
691 | 0 | "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n", |
692 | 0 | name, NULL); |
693 | 0 | return; |
694 | 0 | } |
695 | | #ifdef LIBXML_VALID_ENABLED |
696 | | if (elem == NULL) |
697 | | ctxt->valid = 0; |
698 | | if (ctxt->validate && ctxt->wellFormed && |
699 | | ctxt->myDoc && ctxt->myDoc->intSubset) |
700 | | ctxt->valid &= |
701 | | xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem); |
702 | | #endif /* LIBXML_VALID_ENABLED */ |
703 | 36.6k | } |
704 | | |
705 | | /** |
706 | | * What to do when a notation declaration has been parsed. |
707 | | * |
708 | | * @param ctx the user data (XML parser context) |
709 | | * @param name The name of the notation |
710 | | * @param publicId The public ID of the entity |
711 | | * @param systemId The system ID of the entity |
712 | | */ |
713 | | void |
714 | | xmlSAX2NotationDecl(void *ctx, const xmlChar *name, |
715 | | const xmlChar *publicId, const xmlChar *systemId) |
716 | 6.25k | { |
717 | 6.25k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
718 | 6.25k | xmlNotationPtr nota = NULL; |
719 | | |
720 | | /* Avoid unused variable warning if features are disabled. */ |
721 | 6.25k | (void) nota; |
722 | | |
723 | 6.25k | if ((ctxt == NULL) || (ctxt->myDoc == NULL)) |
724 | 0 | return; |
725 | | |
726 | 6.25k | if ((publicId == NULL) && (systemId == NULL)) { |
727 | 113 | xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING, |
728 | 113 | "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n", |
729 | 113 | name, NULL); |
730 | 113 | return; |
731 | 6.14k | } else if (ctxt->inSubset == 1) |
732 | 6.14k | nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name, |
733 | 6.14k | publicId, systemId); |
734 | 0 | else if (ctxt->inSubset == 2) |
735 | 0 | nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name, |
736 | 0 | publicId, systemId); |
737 | 0 | else { |
738 | 0 | xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING, |
739 | 0 | "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n", |
740 | 0 | name, NULL); |
741 | 0 | return; |
742 | 0 | } |
743 | | #ifdef LIBXML_VALID_ENABLED |
744 | | if (nota == NULL) ctxt->valid = 0; |
745 | | if ((ctxt->validate) && (ctxt->wellFormed) && |
746 | | (ctxt->myDoc->intSubset != NULL)) |
747 | | ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc, |
748 | | nota); |
749 | | #endif /* LIBXML_VALID_ENABLED */ |
750 | 6.25k | } |
751 | | |
752 | | /** |
753 | | * What to do when an unparsed entity declaration is parsed |
754 | | * |
755 | | * @param ctx the user data (XML parser context) |
756 | | * @param name The name of the entity |
757 | | * @param publicId The public ID of the entity |
758 | | * @param systemId The system ID of the entity |
759 | | * @param notationName the name of the notation |
760 | | */ |
761 | | void |
762 | | xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name, |
763 | | const xmlChar *publicId, const xmlChar *systemId, |
764 | | const xmlChar *notationName) |
765 | 223 | { |
766 | 223 | xmlSAX2EntityDecl(ctx, name, XML_EXTERNAL_GENERAL_UNPARSED_ENTITY, |
767 | 223 | publicId, systemId, (xmlChar *) notationName); |
768 | 223 | } |
769 | | |
770 | | /** |
771 | | * Receive the document locator at startup, actually xmlDefaultSAXLocator |
772 | | * Everything is available on the context, so this is useless in our case. |
773 | | * |
774 | | * @param ctx the user data (XML parser context) |
775 | | * @param loc A SAX Locator |
776 | | */ |
777 | | void |
778 | | xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocator *loc ATTRIBUTE_UNUSED) |
779 | 836k | { |
780 | 836k | } |
781 | | |
782 | | /** |
783 | | * called when the document start being processed. |
784 | | * |
785 | | * @param ctx the user data (XML parser context) |
786 | | */ |
787 | | void |
788 | | xmlSAX2StartDocument(void *ctx) |
789 | 777k | { |
790 | 777k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
791 | 777k | xmlDocPtr doc; |
792 | | |
793 | 777k | if (ctx == NULL) return; |
794 | | |
795 | 777k | #ifdef LIBXML_HTML_ENABLED |
796 | 777k | if (ctxt->html) { |
797 | 0 | if (ctxt->myDoc == NULL) |
798 | 0 | ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL); |
799 | 0 | if (ctxt->myDoc == NULL) { |
800 | 0 | xmlSAX2ErrMemory(ctxt); |
801 | 0 | return; |
802 | 0 | } |
803 | 0 | ctxt->myDoc->properties = XML_DOC_HTML; |
804 | 0 | ctxt->myDoc->parseFlags = ctxt->options; |
805 | 0 | } else |
806 | 777k | #endif |
807 | 777k | { |
808 | 777k | doc = ctxt->myDoc = xmlNewDoc(ctxt->version); |
809 | 777k | if (doc != NULL) { |
810 | 777k | doc->properties = 0; |
811 | 777k | if (ctxt->options & XML_PARSE_OLD10) |
812 | 0 | doc->properties |= XML_DOC_OLD10; |
813 | 777k | doc->parseFlags = ctxt->options; |
814 | 777k | doc->standalone = ctxt->standalone; |
815 | 777k | } else { |
816 | 74 | xmlSAX2ErrMemory(ctxt); |
817 | 74 | return; |
818 | 74 | } |
819 | 777k | if ((ctxt->dictNames) && (doc != NULL)) { |
820 | 777k | doc->dict = ctxt->dict; |
821 | 777k | xmlDictReference(doc->dict); |
822 | 777k | } |
823 | 777k | } |
824 | 777k | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) && |
825 | 777k | (ctxt->input != NULL) && (ctxt->input->filename != NULL)) { |
826 | 64.5k | ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename); |
827 | 64.5k | if (ctxt->myDoc->URL == NULL) |
828 | 0 | xmlSAX2ErrMemory(ctxt); |
829 | 64.5k | } |
830 | 777k | } |
831 | | |
832 | | /** |
833 | | * called when the document end has been detected. |
834 | | * |
835 | | * @param ctx the user data (XML parser context) |
836 | | */ |
837 | | void |
838 | | xmlSAX2EndDocument(void *ctx) |
839 | 833k | { |
840 | 833k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
841 | 833k | xmlDocPtr doc; |
842 | | |
843 | 833k | if (ctx == NULL) return; |
844 | | #ifdef LIBXML_VALID_ENABLED |
845 | | if (ctxt->validate && ctxt->wellFormed && |
846 | | ctxt->myDoc && ctxt->myDoc->intSubset) |
847 | | ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc); |
848 | | #endif /* LIBXML_VALID_ENABLED */ |
849 | | |
850 | 833k | doc = ctxt->myDoc; |
851 | 833k | if (doc == NULL) |
852 | 40.4k | return; |
853 | | |
854 | 793k | if (doc->encoding == NULL) { |
855 | 793k | const xmlChar *encoding = xmlGetActualEncoding(ctxt); |
856 | | |
857 | 793k | if (encoding != NULL) { |
858 | 107k | doc->encoding = xmlStrdup(encoding); |
859 | 107k | if (doc->encoding == NULL) |
860 | 321 | xmlSAX2ErrMemory(ctxt); |
861 | 107k | } |
862 | 793k | } |
863 | | |
864 | 793k | #ifdef LIBXML_HTML_ENABLED |
865 | 793k | if (ctxt->html) { |
866 | 0 | if (((ctxt->options & HTML_PARSE_NODEFDTD) == 0) && |
867 | 0 | (doc->intSubset == NULL)) { |
868 | 0 | doc->intSubset = xmlCreateIntSubset(doc, BAD_CAST "html", |
869 | 0 | BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN", |
870 | 0 | BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd"); |
871 | 0 | if (doc->intSubset == NULL) |
872 | 0 | xmlSAX2ErrMemory(ctxt); |
873 | 0 | } |
874 | 0 | } else |
875 | 793k | #endif /* LIBXML_HTML_ENABLED */ |
876 | 793k | { |
877 | 793k | if (ctxt->wellFormed) { |
878 | 601k | doc->properties |= XML_DOC_WELLFORMED; |
879 | 601k | if (ctxt->valid) |
880 | 601k | doc->properties |= XML_DOC_DTDVALID; |
881 | 601k | if (ctxt->nsWellFormed) |
882 | 459k | doc->properties |= XML_DOC_NSVALID; |
883 | 601k | } |
884 | | |
885 | 793k | if (ctxt->options & XML_PARSE_OLD10) |
886 | 0 | doc->properties |= XML_DOC_OLD10; |
887 | 793k | } |
888 | 793k | } |
889 | | |
890 | | static void |
891 | 9.98M | xmlSAX2AppendChild(xmlParserCtxtPtr ctxt, xmlNodePtr node) { |
892 | 9.98M | xmlNodePtr parent; |
893 | 9.98M | xmlNodePtr last; |
894 | | |
895 | 9.98M | if (ctxt->inSubset == 1) { |
896 | 32.0k | parent = (xmlNodePtr) ctxt->myDoc->intSubset; |
897 | 9.95M | } else if (ctxt->inSubset == 2) { |
898 | 6.53k | parent = (xmlNodePtr) ctxt->myDoc->extSubset; |
899 | 9.94M | } else { |
900 | 9.94M | parent = ctxt->node; |
901 | 9.94M | if (parent == NULL) |
902 | 717k | parent = (xmlNodePtr) ctxt->myDoc; |
903 | 9.94M | } |
904 | | |
905 | 9.98M | last = parent->last; |
906 | 9.98M | if (last == NULL) { |
907 | 968k | parent->children = node; |
908 | 9.02M | } else { |
909 | 9.02M | last->next = node; |
910 | 9.02M | node->prev = last; |
911 | 9.02M | } |
912 | | |
913 | 9.98M | parent->last = node; |
914 | 9.98M | node->parent = parent; |
915 | | |
916 | 9.98M | if ((node->type != XML_TEXT_NODE) && |
917 | 9.98M | (ctxt->input != NULL)) { |
918 | 6.75M | if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX) |
919 | 6.73M | node->line = ctxt->input->line; |
920 | 17.4k | else |
921 | 17.4k | node->line = USHRT_MAX; |
922 | 6.75M | } |
923 | 9.98M | } |
924 | | |
925 | | #if defined(LIBXML_SAX1_ENABLED) |
926 | | /** |
927 | | * Handle a namespace error |
928 | | * |
929 | | * @param ctxt an XML parser context |
930 | | * @param error the error number |
931 | | * @param msg the error message |
932 | | * @param str1 an error string |
933 | | * @param str2 an error string |
934 | | */ |
935 | | static void LIBXML_ATTR_FORMAT(3,0) |
936 | | xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
937 | | const char *msg, const xmlChar *str1, const xmlChar *str2) |
938 | | { |
939 | | xmlCtxtErr(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_ERROR, |
940 | | str1, str2, NULL, 0, msg, str1, str2); |
941 | | } |
942 | | |
943 | | /** |
944 | | * Handle an attribute that has been read by the parser. |
945 | | * |
946 | | * Deprecated SAX1 interface. |
947 | | * |
948 | | * @param ctxt the parser context |
949 | | * @param fullname the attribute name, including namespace prefix |
950 | | * @param value the attribute value |
951 | | * @param prefix the namespace prefix |
952 | | */ |
953 | | static void |
954 | | xmlSAX1Attribute(xmlParserCtxtPtr ctxt, const xmlChar *fullname, |
955 | | const xmlChar *value, const xmlChar *prefix) |
956 | | { |
957 | | xmlAttrPtr ret; |
958 | | const xmlChar *name; |
959 | | xmlChar *ns; |
960 | | xmlNsPtr namespace; |
961 | | |
962 | | /* |
963 | | * Split the full name into a namespace prefix and the tag name |
964 | | */ |
965 | | name = xmlSplitQName4(fullname, &ns); |
966 | | if (name == NULL) { |
967 | | xmlSAX2ErrMemory(ctxt); |
968 | | return; |
969 | | } |
970 | | |
971 | | /* |
972 | | * Check whether it's a namespace definition |
973 | | */ |
974 | | if ((ns == NULL) && |
975 | | (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') && |
976 | | (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) { |
977 | | xmlNsPtr nsret; |
978 | | xmlChar *val; |
979 | | |
980 | | /* Avoid unused variable warning if features are disabled. */ |
981 | | (void) nsret; |
982 | | |
983 | | if (!ctxt->replaceEntities) { |
984 | | /* TODO: normalize if needed */ |
985 | | val = xmlExpandEntitiesInAttValue(ctxt, value, /* normalize */ 0); |
986 | | if (val == NULL) { |
987 | | xmlSAX2ErrMemory(ctxt); |
988 | | return; |
989 | | } |
990 | | } else { |
991 | | val = (xmlChar *) value; |
992 | | } |
993 | | |
994 | | if (val[0] != 0) { |
995 | | xmlURIPtr uri; |
996 | | |
997 | | if (xmlParseURISafe((const char *)val, &uri) < 0) |
998 | | xmlSAX2ErrMemory(ctxt); |
999 | | if (uri == NULL) { |
1000 | | xmlNsWarnMsg(ctxt, XML_WAR_NS_URI, |
1001 | | "xmlns:%s: %s not a valid URI\n", name, value); |
1002 | | } else { |
1003 | | if (uri->scheme == NULL) { |
1004 | | xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE, |
1005 | | "xmlns:%s: URI %s is not absolute\n", |
1006 | | name, value); |
1007 | | } |
1008 | | xmlFreeURI(uri); |
1009 | | } |
1010 | | } |
1011 | | |
1012 | | /* a default namespace definition */ |
1013 | | nsret = xmlNewNs(ctxt->node, val, NULL); |
1014 | | if (nsret == NULL) { |
1015 | | xmlSAX2ErrMemory(ctxt); |
1016 | | } |
1017 | | #ifdef LIBXML_VALID_ENABLED |
1018 | | /* |
1019 | | * Validate also for namespace decls, they are attributes from |
1020 | | * an XML-1.0 perspective |
1021 | | */ |
1022 | | else if (ctxt->validate && ctxt->wellFormed && |
1023 | | ctxt->myDoc && ctxt->myDoc->intSubset) { |
1024 | | ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc, |
1025 | | ctxt->node, prefix, nsret, val); |
1026 | | } |
1027 | | #endif /* LIBXML_VALID_ENABLED */ |
1028 | | if (val != value) |
1029 | | xmlFree(val); |
1030 | | return; |
1031 | | } |
1032 | | if ((ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') && |
1033 | | (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) { |
1034 | | xmlNsPtr nsret; |
1035 | | xmlChar *val; |
1036 | | |
1037 | | /* Avoid unused variable warning if features are disabled. */ |
1038 | | (void) nsret; |
1039 | | |
1040 | | if (!ctxt->replaceEntities) { |
1041 | | /* TODO: normalize if needed */ |
1042 | | val = xmlExpandEntitiesInAttValue(ctxt, value, /* normalize */ 0); |
1043 | | if (val == NULL) { |
1044 | | xmlSAX2ErrMemory(ctxt); |
1045 | | xmlFree(ns); |
1046 | | return; |
1047 | | } |
1048 | | } else { |
1049 | | val = (xmlChar *) value; |
1050 | | } |
1051 | | |
1052 | | if (val[0] == 0) { |
1053 | | xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY, |
1054 | | "Empty namespace name for prefix %s\n", name, NULL); |
1055 | | } |
1056 | | if ((ctxt->pedantic != 0) && (val[0] != 0)) { |
1057 | | xmlURIPtr uri; |
1058 | | |
1059 | | if (xmlParseURISafe((const char *)val, &uri) < 0) |
1060 | | xmlSAX2ErrMemory(ctxt); |
1061 | | if (uri == NULL) { |
1062 | | xmlNsWarnMsg(ctxt, XML_WAR_NS_URI, |
1063 | | "xmlns:%s: %s not a valid URI\n", name, value); |
1064 | | } else { |
1065 | | if (uri->scheme == NULL) { |
1066 | | xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE, |
1067 | | "xmlns:%s: URI %s is not absolute\n", name, value); |
1068 | | } |
1069 | | xmlFreeURI(uri); |
1070 | | } |
1071 | | } |
1072 | | |
1073 | | /* a standard namespace definition */ |
1074 | | nsret = xmlNewNs(ctxt->node, val, name); |
1075 | | xmlFree(ns); |
1076 | | |
1077 | | if (nsret == NULL) { |
1078 | | xmlSAX2ErrMemory(ctxt); |
1079 | | } |
1080 | | #ifdef LIBXML_VALID_ENABLED |
1081 | | /* |
1082 | | * Validate also for namespace decls, they are attributes from |
1083 | | * an XML-1.0 perspective |
1084 | | */ |
1085 | | else if (ctxt->validate && ctxt->wellFormed && |
1086 | | ctxt->myDoc && ctxt->myDoc->intSubset) { |
1087 | | ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc, |
1088 | | ctxt->node, prefix, nsret, value); |
1089 | | } |
1090 | | #endif /* LIBXML_VALID_ENABLED */ |
1091 | | if (val != value) |
1092 | | xmlFree(val); |
1093 | | return; |
1094 | | } |
1095 | | |
1096 | | if (ns != NULL) { |
1097 | | int res; |
1098 | | |
1099 | | res = xmlSearchNsSafe(ctxt->node, ns, &namespace); |
1100 | | if (res < 0) |
1101 | | xmlSAX2ErrMemory(ctxt); |
1102 | | |
1103 | | if (namespace == NULL) { |
1104 | | xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
1105 | | "Namespace prefix %s of attribute %s is not defined\n", |
1106 | | ns, name); |
1107 | | } else { |
1108 | | xmlAttrPtr prop; |
1109 | | |
1110 | | prop = ctxt->node->properties; |
1111 | | while (prop != NULL) { |
1112 | | if (prop->ns != NULL) { |
1113 | | if ((xmlStrEqual(name, prop->name)) && |
1114 | | ((namespace == prop->ns) || |
1115 | | (xmlStrEqual(namespace->href, prop->ns->href)))) { |
1116 | | xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, |
1117 | | XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, |
1118 | | name, NULL, NULL, 0, |
1119 | | "Attribute %s in %s redefined\n", |
1120 | | name, namespace->href); |
1121 | | goto error; |
1122 | | } |
1123 | | } |
1124 | | prop = prop->next; |
1125 | | } |
1126 | | } |
1127 | | } else { |
1128 | | namespace = NULL; |
1129 | | } |
1130 | | |
1131 | | /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */ |
1132 | | ret = xmlNewNsProp(ctxt->node, namespace, name, NULL); |
1133 | | if (ret == NULL) { |
1134 | | xmlSAX2ErrMemory(ctxt); |
1135 | | goto error; |
1136 | | } |
1137 | | |
1138 | | if (ctxt->replaceEntities == 0) { |
1139 | | if (xmlNodeParseContent((xmlNodePtr) ret, value, INT_MAX) < 0) |
1140 | | xmlSAX2ErrMemory(ctxt); |
1141 | | } else if (value != NULL) { |
1142 | | ret->children = xmlNewDocText(ctxt->myDoc, value); |
1143 | | if (ret->children == NULL) { |
1144 | | xmlSAX2ErrMemory(ctxt); |
1145 | | } else { |
1146 | | ret->last = ret->children; |
1147 | | ret->children->parent = (xmlNodePtr) ret; |
1148 | | } |
1149 | | } |
1150 | | |
1151 | | #ifdef LIBXML_VALID_ENABLED |
1152 | | if (ctxt->validate && ctxt->wellFormed && |
1153 | | ctxt->myDoc && ctxt->myDoc->intSubset) { |
1154 | | |
1155 | | /* |
1156 | | * If we don't substitute entities, the validation should be |
1157 | | * done on a value with replaced entities anyway. |
1158 | | */ |
1159 | | if (!ctxt->replaceEntities) { |
1160 | | xmlChar *val; |
1161 | | |
1162 | | /* TODO: normalize if needed */ |
1163 | | val = xmlExpandEntitiesInAttValue(ctxt, value, /* normalize */ 0); |
1164 | | |
1165 | | if (val == NULL) |
1166 | | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, |
1167 | | ctxt->myDoc, ctxt->node, ret, value); |
1168 | | else { |
1169 | | xmlChar *nvalnorm; |
1170 | | |
1171 | | /* |
1172 | | * Do the last stage of the attribute normalization |
1173 | | * It need to be done twice ... it's an extra burden related |
1174 | | * to the ability to keep xmlSAX2References in attributes |
1175 | | */ |
1176 | | nvalnorm = xmlValidCtxtNormalizeAttributeValue( |
1177 | | &ctxt->vctxt, ctxt->myDoc, |
1178 | | ctxt->node, fullname, val); |
1179 | | if (nvalnorm != NULL) { |
1180 | | xmlFree(val); |
1181 | | val = nvalnorm; |
1182 | | } |
1183 | | |
1184 | | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, |
1185 | | ctxt->myDoc, ctxt->node, ret, val); |
1186 | | xmlFree(val); |
1187 | | } |
1188 | | } else { |
1189 | | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc, |
1190 | | ctxt->node, ret, value); |
1191 | | } |
1192 | | } else |
1193 | | #endif /* LIBXML_VALID_ENABLED */ |
1194 | | if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) && |
1195 | | (ctxt->input->entity == NULL) && |
1196 | | /* Don't create IDs containing entity references */ |
1197 | | (ret->children != NULL) && |
1198 | | (ret->children->type == XML_TEXT_NODE) && |
1199 | | (ret->children->next == NULL)) { |
1200 | | xmlChar *content = ret->children->content; |
1201 | | /* |
1202 | | * when validating, the ID registration is done at the attribute |
1203 | | * validation level. Otherwise we have to do specific handling here. |
1204 | | */ |
1205 | | if (xmlStrEqual(fullname, BAD_CAST "xml:id")) { |
1206 | | /* |
1207 | | * Add the xml:id value |
1208 | | * |
1209 | | * Open issue: normalization of the value. |
1210 | | */ |
1211 | | if (xmlValidateNCName(content, 1) != 0) { |
1212 | | xmlErrId(ctxt, XML_DTD_XMLID_VALUE, |
1213 | | "xml:id : attribute value %s is not an NCName\n", |
1214 | | content); |
1215 | | } |
1216 | | xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret); |
1217 | | } else { |
1218 | | int res = xmlIsID(ctxt->myDoc, ctxt->node, ret); |
1219 | | |
1220 | | if (res < 0) |
1221 | | xmlCtxtErrMemory(ctxt); |
1222 | | else if (res > 0) |
1223 | | xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret); |
1224 | | else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) |
1225 | | xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret); |
1226 | | } |
1227 | | } |
1228 | | |
1229 | | error: |
1230 | | if (ns != NULL) |
1231 | | xmlFree(ns); |
1232 | | } |
1233 | | |
1234 | | /* |
1235 | | * |
1236 | | * Check defaulted attributes from the DTD |
1237 | | * |
1238 | | * Deprecated SAX1 interface. |
1239 | | */ |
1240 | | static void |
1241 | | xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name, |
1242 | | const xmlChar *prefix, const xmlChar **atts) { |
1243 | | xmlElementPtr elemDecl; |
1244 | | const xmlChar *att; |
1245 | | int internal = 1; |
1246 | | int i; |
1247 | | |
1248 | | elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix); |
1249 | | if (elemDecl == NULL) { |
1250 | | elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix); |
1251 | | internal = 0; |
1252 | | } |
1253 | | |
1254 | | process_external_subset: |
1255 | | |
1256 | | if (elemDecl != NULL) { |
1257 | | xmlAttributePtr attr = elemDecl->attributes; |
1258 | | |
1259 | | #ifdef LIBXML_VALID_ENABLED |
1260 | | /* |
1261 | | * Check against defaulted attributes from the external subset |
1262 | | * if the document is stamped as standalone. |
1263 | | * |
1264 | | * This should be moved to valid.c, but we don't keep track |
1265 | | * whether an attribute was defaulted. |
1266 | | */ |
1267 | | if ((ctxt->myDoc->standalone == 1) && |
1268 | | (ctxt->myDoc->extSubset != NULL) && |
1269 | | (ctxt->validate)) { |
1270 | | while (attr != NULL) { |
1271 | | if ((attr->defaultValue != NULL) && |
1272 | | (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset, |
1273 | | attr->elem, attr->name, |
1274 | | attr->prefix) == attr) && |
1275 | | (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset, |
1276 | | attr->elem, attr->name, |
1277 | | attr->prefix) == NULL)) { |
1278 | | xmlChar *fulln; |
1279 | | |
1280 | | if (attr->prefix != NULL) { |
1281 | | fulln = xmlStrdup(attr->prefix); |
1282 | | if (fulln != NULL) |
1283 | | fulln = xmlStrcat(fulln, BAD_CAST ":"); |
1284 | | if (fulln != NULL) |
1285 | | fulln = xmlStrcat(fulln, attr->name); |
1286 | | } else { |
1287 | | fulln = xmlStrdup(attr->name); |
1288 | | } |
1289 | | if (fulln == NULL) { |
1290 | | xmlSAX2ErrMemory(ctxt); |
1291 | | break; |
1292 | | } |
1293 | | |
1294 | | /* |
1295 | | * Check that the attribute is not declared in the |
1296 | | * serialization |
1297 | | */ |
1298 | | att = NULL; |
1299 | | if (atts != NULL) { |
1300 | | i = 0; |
1301 | | att = atts[i]; |
1302 | | while (att != NULL) { |
1303 | | if (xmlStrEqual(att, fulln)) |
1304 | | break; |
1305 | | i += 2; |
1306 | | att = atts[i]; |
1307 | | } |
1308 | | } |
1309 | | if (att == NULL) { |
1310 | | xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED, |
1311 | | "standalone: attribute %s on %s defaulted from external subset\n", |
1312 | | fulln, |
1313 | | attr->elem); |
1314 | | } |
1315 | | xmlFree(fulln); |
1316 | | } |
1317 | | attr = attr->nexth; |
1318 | | } |
1319 | | } |
1320 | | #endif |
1321 | | |
1322 | | /* |
1323 | | * Actually insert defaulted values when needed |
1324 | | */ |
1325 | | attr = elemDecl->attributes; |
1326 | | while (attr != NULL) { |
1327 | | /* |
1328 | | * Make sure that attributes redefinition occurring in the |
1329 | | * internal subset are not overridden by definitions in the |
1330 | | * external subset. |
1331 | | */ |
1332 | | if (attr->defaultValue != NULL) { |
1333 | | /* |
1334 | | * the element should be instantiated in the tree if: |
1335 | | * - this is a namespace prefix |
1336 | | * - the user required for completion in the tree |
1337 | | * like XSLT |
1338 | | * - there isn't already an attribute definition |
1339 | | * in the internal subset overriding it. |
1340 | | */ |
1341 | | if (((attr->prefix != NULL) && |
1342 | | (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) || |
1343 | | ((attr->prefix == NULL) && |
1344 | | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) || |
1345 | | (ctxt->loadsubset & XML_COMPLETE_ATTRS)) { |
1346 | | xmlAttributePtr tst; |
1347 | | |
1348 | | tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset, |
1349 | | attr->elem, attr->name, |
1350 | | attr->prefix); |
1351 | | if ((tst == attr) || (tst == NULL)) { |
1352 | | xmlChar fn[50]; |
1353 | | xmlChar *fulln; |
1354 | | |
1355 | | fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50); |
1356 | | if (fulln == NULL) { |
1357 | | xmlSAX2ErrMemory(ctxt); |
1358 | | return; |
1359 | | } |
1360 | | |
1361 | | /* |
1362 | | * Check that the attribute is not declared in the |
1363 | | * serialization |
1364 | | */ |
1365 | | att = NULL; |
1366 | | if (atts != NULL) { |
1367 | | i = 0; |
1368 | | att = atts[i]; |
1369 | | while (att != NULL) { |
1370 | | if (xmlStrEqual(att, fulln)) |
1371 | | break; |
1372 | | i += 2; |
1373 | | att = atts[i]; |
1374 | | } |
1375 | | } |
1376 | | if (att == NULL) { |
1377 | | xmlSAX1Attribute(ctxt, fulln, |
1378 | | attr->defaultValue, prefix); |
1379 | | } |
1380 | | if ((fulln != fn) && (fulln != attr->name)) |
1381 | | xmlFree(fulln); |
1382 | | } |
1383 | | } |
1384 | | } |
1385 | | attr = attr->nexth; |
1386 | | } |
1387 | | if (internal == 1) { |
1388 | | elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, |
1389 | | name, prefix); |
1390 | | internal = 0; |
1391 | | goto process_external_subset; |
1392 | | } |
1393 | | } |
1394 | | } |
1395 | | |
1396 | | /** |
1397 | | * called when an opening tag has been processed. |
1398 | | * |
1399 | | * Deprecated SAX1 interface. |
1400 | | * |
1401 | | * @param ctx the user data (XML parser context) |
1402 | | * @param fullname The element name, including namespace prefix |
1403 | | * @param atts An array of name/value attributes pairs, NULL terminated |
1404 | | */ |
1405 | | static void |
1406 | | xmlSAX1StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts) |
1407 | | { |
1408 | | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
1409 | | xmlNodePtr ret; |
1410 | | xmlNodePtr parent; |
1411 | | xmlNsPtr ns; |
1412 | | const xmlChar *name; |
1413 | | xmlChar *prefix; |
1414 | | const xmlChar *att; |
1415 | | const xmlChar *value; |
1416 | | int i, res; |
1417 | | |
1418 | | if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return; |
1419 | | |
1420 | | #ifdef LIBXML_VALID_ENABLED |
1421 | | /* |
1422 | | * First check on validity: |
1423 | | */ |
1424 | | if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) && |
1425 | | ((ctxt->myDoc->intSubset == NULL) || |
1426 | | ((ctxt->myDoc->intSubset->notations == NULL) && |
1427 | | (ctxt->myDoc->intSubset->elements == NULL) && |
1428 | | (ctxt->myDoc->intSubset->attributes == NULL) && |
1429 | | (ctxt->myDoc->intSubset->entities == NULL)))) { |
1430 | | xmlErrValid(ctxt, XML_ERR_NO_DTD, |
1431 | | "Validation failed: no DTD found !", NULL, NULL); |
1432 | | ctxt->validate = 0; |
1433 | | } |
1434 | | #endif |
1435 | | |
1436 | | /* |
1437 | | * Split the full name into a namespace prefix and the tag name |
1438 | | */ |
1439 | | name = xmlSplitQName4(fullname, &prefix); |
1440 | | if (name == NULL) { |
1441 | | xmlSAX2ErrMemory(ctxt); |
1442 | | return; |
1443 | | } |
1444 | | |
1445 | | /* |
1446 | | * Note : the namespace resolution is deferred until the end of the |
1447 | | * attributes parsing, since local namespace can be defined as |
1448 | | * an attribute at this level. |
1449 | | */ |
1450 | | ret = xmlNewDocNode(ctxt->myDoc, NULL, name, NULL); |
1451 | | if (ret == NULL) { |
1452 | | xmlFree(prefix); |
1453 | | xmlSAX2ErrMemory(ctxt); |
1454 | | return; |
1455 | | } |
1456 | | ctxt->nodemem = -1; |
1457 | | |
1458 | | /* Initialize parent before pushing node */ |
1459 | | parent = ctxt->node; |
1460 | | if (parent == NULL) |
1461 | | parent = (xmlNodePtr) ctxt->myDoc; |
1462 | | |
1463 | | /* |
1464 | | * Link the child element |
1465 | | */ |
1466 | | xmlSAX2AppendChild(ctxt, ret); |
1467 | | |
1468 | | /* |
1469 | | * We are parsing a new node. |
1470 | | */ |
1471 | | if (nodePush(ctxt, ret) < 0) { |
1472 | | xmlUnlinkNode(ret); |
1473 | | xmlFreeNode(ret); |
1474 | | if (prefix != NULL) |
1475 | | xmlFree(prefix); |
1476 | | return; |
1477 | | } |
1478 | | |
1479 | | /* |
1480 | | * Insert all the defaulted attributes from the DTD especially |
1481 | | * namespaces |
1482 | | */ |
1483 | | if ((ctxt->myDoc->intSubset != NULL) || |
1484 | | (ctxt->myDoc->extSubset != NULL)) { |
1485 | | xmlCheckDefaultedAttributes(ctxt, name, prefix, atts); |
1486 | | } |
1487 | | |
1488 | | /* |
1489 | | * process all the attributes whose name start with "xmlns" |
1490 | | */ |
1491 | | if (atts != NULL) { |
1492 | | i = 0; |
1493 | | att = atts[i++]; |
1494 | | value = atts[i++]; |
1495 | | while ((att != NULL) && (value != NULL)) { |
1496 | | if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') && |
1497 | | (att[3] == 'n') && (att[4] == 's')) |
1498 | | xmlSAX1Attribute(ctxt, att, value, prefix); |
1499 | | |
1500 | | att = atts[i++]; |
1501 | | value = atts[i++]; |
1502 | | } |
1503 | | } |
1504 | | |
1505 | | /* |
1506 | | * Search the namespace, note that since the attributes have been |
1507 | | * processed, the local namespaces are available. |
1508 | | */ |
1509 | | res = xmlSearchNsSafe(ret, prefix, &ns); |
1510 | | if (res < 0) |
1511 | | xmlSAX2ErrMemory(ctxt); |
1512 | | if ((ns == NULL) && (parent != NULL)) { |
1513 | | res = xmlSearchNsSafe(parent, prefix, &ns); |
1514 | | if (res < 0) |
1515 | | xmlSAX2ErrMemory(ctxt); |
1516 | | } |
1517 | | if ((prefix != NULL) && (ns == NULL)) { |
1518 | | xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
1519 | | "Namespace prefix %s is not defined\n", |
1520 | | prefix, NULL); |
1521 | | ns = xmlNewNs(ret, NULL, prefix); |
1522 | | if (ns == NULL) |
1523 | | xmlSAX2ErrMemory(ctxt); |
1524 | | } |
1525 | | |
1526 | | /* |
1527 | | * set the namespace node, making sure that if the default namespace |
1528 | | * is unbound on a parent we simply keep it NULL |
1529 | | */ |
1530 | | if ((ns != NULL) && (ns->href != NULL) && |
1531 | | ((ns->href[0] != 0) || (ns->prefix != NULL))) |
1532 | | xmlSetNs(ret, ns); |
1533 | | |
1534 | | /* |
1535 | | * process all the other attributes |
1536 | | */ |
1537 | | if (atts != NULL) { |
1538 | | i = 0; |
1539 | | att = atts[i++]; |
1540 | | value = atts[i++]; |
1541 | | while ((att != NULL) && (value != NULL)) { |
1542 | | if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') || |
1543 | | (att[3] != 'n') || (att[4] != 's')) |
1544 | | xmlSAX1Attribute(ctxt, att, value, NULL); |
1545 | | |
1546 | | /* |
1547 | | * Next ones |
1548 | | */ |
1549 | | att = atts[i++]; |
1550 | | value = atts[i++]; |
1551 | | } |
1552 | | } |
1553 | | |
1554 | | #ifdef LIBXML_VALID_ENABLED |
1555 | | /* |
1556 | | * If it's the Document root, finish the DTD validation and |
1557 | | * check the document root element for validity |
1558 | | */ |
1559 | | if ((ctxt->validate) && |
1560 | | ((ctxt->vctxt.flags & XML_VCTXT_DTD_VALIDATED) == 0)) { |
1561 | | int chk; |
1562 | | |
1563 | | chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc); |
1564 | | if (chk <= 0) |
1565 | | ctxt->valid = 0; |
1566 | | if (chk < 0) |
1567 | | ctxt->wellFormed = 0; |
1568 | | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
1569 | | ctxt->vctxt.flags |= XML_VCTXT_DTD_VALIDATED; |
1570 | | } |
1571 | | #endif /* LIBXML_VALID_ENABLED */ |
1572 | | |
1573 | | if (prefix != NULL) |
1574 | | xmlFree(prefix); |
1575 | | |
1576 | | } |
1577 | | #endif /* LIBXML_SAX1_ENABLED */ |
1578 | | |
1579 | | #ifdef LIBXML_HTML_ENABLED |
1580 | | static void |
1581 | | xmlSAX2HtmlAttribute(xmlParserCtxtPtr ctxt, const xmlChar *fullname, |
1582 | 0 | const xmlChar *value) { |
1583 | 0 | xmlAttrPtr ret; |
1584 | 0 | xmlChar *nval = NULL; |
1585 | |
|
1586 | 0 | ret = xmlNewNsProp(ctxt->node, NULL, fullname, NULL); |
1587 | 0 | if (ret == NULL) { |
1588 | 0 | xmlSAX2ErrMemory(ctxt); |
1589 | 0 | return; |
1590 | 0 | } |
1591 | | |
1592 | 0 | if ((value == NULL) && (htmlIsBooleanAttr(fullname))) { |
1593 | 0 | nval = xmlStrdup(fullname); |
1594 | 0 | if (nval == NULL) { |
1595 | 0 | xmlSAX2ErrMemory(ctxt); |
1596 | 0 | return; |
1597 | 0 | } |
1598 | 0 | value = nval; |
1599 | 0 | } |
1600 | | |
1601 | 0 | if (value != NULL) { |
1602 | 0 | ret->children = xmlNewDocText(ctxt->myDoc, value); |
1603 | 0 | if (ret->children == NULL) { |
1604 | 0 | xmlSAX2ErrMemory(ctxt); |
1605 | 0 | } else { |
1606 | 0 | ret->last = ret->children; |
1607 | 0 | ret->children->parent = (xmlNodePtr) ret; |
1608 | 0 | } |
1609 | 0 | } |
1610 | |
|
1611 | 0 | if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) && |
1612 | | /* |
1613 | | * Don't create IDs containing entity references (should |
1614 | | * be always the case with HTML) |
1615 | | */ |
1616 | 0 | (ret->children != NULL) && |
1617 | 0 | (ret->children->type == XML_TEXT_NODE) && |
1618 | 0 | (ret->children->next == NULL)) { |
1619 | 0 | int res = xmlIsID(ctxt->myDoc, ctxt->node, ret); |
1620 | |
|
1621 | 0 | if (res < 0) |
1622 | 0 | xmlCtxtErrMemory(ctxt); |
1623 | 0 | else if (res > 0) |
1624 | 0 | xmlAddID(&ctxt->vctxt, ctxt->myDoc, ret->children->content, ret); |
1625 | 0 | } |
1626 | |
|
1627 | 0 | if (nval != NULL) |
1628 | 0 | xmlFree(nval); |
1629 | 0 | } |
1630 | | |
1631 | | /** |
1632 | | * Called when an opening tag has been processed. |
1633 | | * |
1634 | | * @param ctxt parser context |
1635 | | * @param fullname The element name, including namespace prefix |
1636 | | * @param atts An array of name/value attributes pairs, NULL terminated |
1637 | | */ |
1638 | | static void |
1639 | | xmlSAX2StartHtmlElement(xmlParserCtxtPtr ctxt, const xmlChar *fullname, |
1640 | 0 | const xmlChar **atts) { |
1641 | 0 | xmlNodePtr ret; |
1642 | 0 | xmlNodePtr parent; |
1643 | 0 | const xmlChar *att; |
1644 | 0 | const xmlChar *value; |
1645 | 0 | int i; |
1646 | |
|
1647 | 0 | ret = xmlNewDocNode(ctxt->myDoc, NULL, fullname, NULL); |
1648 | 0 | if (ret == NULL) { |
1649 | 0 | xmlSAX2ErrMemory(ctxt); |
1650 | 0 | return; |
1651 | 0 | } |
1652 | 0 | ctxt->nodemem = -1; |
1653 | | |
1654 | | /* Initialize parent before pushing node */ |
1655 | 0 | parent = ctxt->node; |
1656 | 0 | if (parent == NULL) |
1657 | 0 | parent = (xmlNodePtr) ctxt->myDoc; |
1658 | | |
1659 | | /* |
1660 | | * Link the child element |
1661 | | */ |
1662 | 0 | xmlSAX2AppendChild(ctxt, ret); |
1663 | | |
1664 | | /* |
1665 | | * We are parsing a new node. |
1666 | | */ |
1667 | 0 | if (nodePush(ctxt, ret) < 0) { |
1668 | 0 | xmlUnlinkNode(ret); |
1669 | 0 | xmlFreeNode(ret); |
1670 | 0 | return; |
1671 | 0 | } |
1672 | | |
1673 | 0 | if (atts != NULL) { |
1674 | 0 | i = 0; |
1675 | 0 | att = atts[i++]; |
1676 | 0 | value = atts[i++]; |
1677 | 0 | while (att != NULL) { |
1678 | 0 | xmlSAX2HtmlAttribute(ctxt, att, value); |
1679 | 0 | att = atts[i++]; |
1680 | 0 | value = atts[i++]; |
1681 | 0 | } |
1682 | 0 | } |
1683 | 0 | } |
1684 | | #endif /* LIBXML_HTML_ENABLED */ |
1685 | | |
1686 | | /** |
1687 | | * Called when an opening tag has been processed. |
1688 | | * |
1689 | | * @deprecated Don't call this function directly. |
1690 | | * |
1691 | | * Used for HTML and SAX1. |
1692 | | * |
1693 | | * @param ctx the user data (XML parser context) |
1694 | | * @param fullname The element name, including namespace prefix |
1695 | | * @param atts An array of name/value attributes pairs, NULL terminated |
1696 | | */ |
1697 | | void |
1698 | 0 | xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts) { |
1699 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
1700 | |
|
1701 | 0 | (void) atts; |
1702 | |
|
1703 | 0 | if ((ctxt == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) |
1704 | 0 | return; |
1705 | | |
1706 | | #ifdef LIBXML_SAX1_ENABLED |
1707 | | if (!ctxt->html) { |
1708 | | xmlSAX1StartElement(ctxt, fullname, atts); |
1709 | | return; |
1710 | | } |
1711 | | #endif |
1712 | | |
1713 | 0 | #ifdef LIBXML_HTML_ENABLED |
1714 | 0 | if (ctxt->html) { |
1715 | 0 | xmlSAX2StartHtmlElement(ctxt, fullname, atts); |
1716 | 0 | return; |
1717 | 0 | } |
1718 | 0 | #endif |
1719 | 0 | } |
1720 | | |
1721 | | /** |
1722 | | * called when the end of an element has been detected. |
1723 | | * |
1724 | | * @deprecated Don't call this function directly. |
1725 | | * |
1726 | | * Used for HTML and SAX1. |
1727 | | * |
1728 | | * @param ctx the user data (XML parser context) |
1729 | | * @param name The element name |
1730 | | */ |
1731 | | void |
1732 | | xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED) |
1733 | 0 | { |
1734 | 0 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
1735 | |
|
1736 | 0 | if (ctxt == NULL) |
1737 | 0 | return; |
1738 | | |
1739 | | #if defined(LIBXML_SAX1_ENABLED) && defined(LIBXML_VALID_ENABLED) |
1740 | | if (!ctxt->html && ctxt->validate && ctxt->wellFormed && |
1741 | | ctxt->myDoc && ctxt->myDoc->intSubset) |
1742 | | ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, |
1743 | | ctxt->node); |
1744 | | #endif /* LIBXML_VALID_ENABLED */ |
1745 | | |
1746 | 0 | #if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) |
1747 | 0 | ctxt->nodemem = -1; |
1748 | | |
1749 | | /* |
1750 | | * end of parsing of this node. |
1751 | | */ |
1752 | 0 | nodePop(ctxt); |
1753 | 0 | #endif |
1754 | 0 | } |
1755 | | |
1756 | | /* |
1757 | | * @param ctxt the parser context |
1758 | | * @param doc the document |
1759 | | * @param str the input string |
1760 | | * @param len the string length |
1761 | | * |
1762 | | * Callback for a text node |
1763 | | * |
1764 | | * @returns the newly allocated string or NULL if not needed or error |
1765 | | */ |
1766 | | static xmlNodePtr |
1767 | | xmlSAX2TextNode(xmlParserCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *str, |
1768 | 8.23M | int len) { |
1769 | 8.23M | xmlNodePtr ret; |
1770 | 8.23M | const xmlChar *intern = NULL; |
1771 | | |
1772 | | /* |
1773 | | * Allocate |
1774 | | */ |
1775 | 8.23M | if (ctxt->freeElems != NULL) { |
1776 | 0 | ret = ctxt->freeElems; |
1777 | 0 | ctxt->freeElems = ret->next; |
1778 | 0 | ctxt->freeElemsNr--; |
1779 | 8.23M | } else { |
1780 | 8.23M | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
1781 | 8.23M | } |
1782 | 8.23M | if (ret == NULL) { |
1783 | 225 | xmlCtxtErrMemory(ctxt); |
1784 | 225 | return(NULL); |
1785 | 225 | } |
1786 | 8.23M | memset(ret, 0, sizeof(xmlNode)); |
1787 | | /* |
1788 | | * intern the formatting blanks found between tags, or the |
1789 | | * very short strings |
1790 | | */ |
1791 | 8.23M | if ((!ctxt->html) && (ctxt->dictNames)) { |
1792 | 8.23M | xmlChar cur = str[len]; |
1793 | | |
1794 | 8.23M | if ((len < (int) (2 * sizeof(void *))) && |
1795 | 8.23M | (ctxt->options & XML_PARSE_COMPACT)) { |
1796 | | /* store the string in the node overriding properties and nsDef */ |
1797 | 0 | xmlChar *tmp = (xmlChar *) &(ret->properties); |
1798 | 0 | memcpy(tmp, str, len); |
1799 | 0 | tmp[len] = 0; |
1800 | 0 | intern = tmp; |
1801 | 8.23M | } else if ((len <= 3) && ((cur == '"') || (cur == '\'') || |
1802 | 3.78M | ((cur == '<') && (str[len + 1] != '!')))) { |
1803 | 3.51M | intern = xmlDictLookup(ctxt->dict, str, len); |
1804 | 3.51M | if (intern == NULL) { |
1805 | 1 | xmlSAX2ErrMemory(ctxt); |
1806 | 1 | xmlFree(ret); |
1807 | 1 | return(NULL); |
1808 | 1 | } |
1809 | 4.72M | } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') && |
1810 | 4.72M | (str[len + 1] != '!')) { |
1811 | 552k | int i; |
1812 | | |
1813 | 2.36M | for (i = 1;i < len;i++) { |
1814 | 2.05M | if (!IS_BLANK_CH(str[i])) goto skip; |
1815 | 2.05M | } |
1816 | 307k | intern = xmlDictLookup(ctxt->dict, str, len); |
1817 | 307k | if (intern == NULL) { |
1818 | 1 | xmlSAX2ErrMemory(ctxt); |
1819 | 1 | xmlFree(ret); |
1820 | 1 | return(NULL); |
1821 | 1 | } |
1822 | 307k | } |
1823 | 8.23M | } |
1824 | 8.23M | skip: |
1825 | 8.23M | ret->type = XML_TEXT_NODE; |
1826 | 8.23M | ret->doc = doc; |
1827 | | |
1828 | 8.23M | ret->name = xmlStringText; |
1829 | 8.23M | if (intern == NULL) { |
1830 | 4.41M | ret->content = xmlStrndup(str, len); |
1831 | 4.41M | if (ret->content == NULL) { |
1832 | 151 | xmlSAX2ErrMemory(ctxt); |
1833 | 151 | xmlFree(ret); |
1834 | 151 | return(NULL); |
1835 | 151 | } |
1836 | 4.41M | } else |
1837 | 3.82M | ret->content = (xmlChar *) intern; |
1838 | | |
1839 | 8.23M | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
1840 | 0 | xmlRegisterNodeDefaultValue(ret); |
1841 | 8.23M | return(ret); |
1842 | 8.23M | } |
1843 | | |
1844 | | #ifdef LIBXML_VALID_ENABLED |
1845 | | /* |
1846 | | * @param ctxt the parser context |
1847 | | * @param str the input string |
1848 | | * @param len the string length |
1849 | | * |
1850 | | * Remove the entities from an attribute value |
1851 | | * |
1852 | | * @returns the newly allocated string or NULL if not needed or error |
1853 | | */ |
1854 | | static xmlChar * |
1855 | | xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, |
1856 | | const xmlChar *end) { |
1857 | | const xmlChar *in; |
1858 | | |
1859 | | in = str; |
1860 | | while (in < end) |
1861 | | if (*in++ == '&') |
1862 | | goto decode; |
1863 | | return(NULL); |
1864 | | decode: |
1865 | | /* |
1866 | | * If the value contains '&', we can be sure it was allocated and is |
1867 | | * zero-terminated. |
1868 | | */ |
1869 | | /* TODO: normalize if needed */ |
1870 | | return(xmlExpandEntitiesInAttValue(ctxt, str, /* normalize */ 0)); |
1871 | | } |
1872 | | #endif /* LIBXML_VALID_ENABLED */ |
1873 | | |
1874 | | /** |
1875 | | * Handle an attribute that has been read by the parser. |
1876 | | * The default handling is to convert the attribute into an |
1877 | | * DOM subtree and past it in a new xmlAttr element added to |
1878 | | * the element. |
1879 | | * |
1880 | | * @param ctxt the parser context |
1881 | | * @param localname the local name of the attribute |
1882 | | * @param prefix the attribute namespace prefix if available |
1883 | | * @param value start of the attribute value |
1884 | | * @param valueend end of the attribute value |
1885 | | * @returns the new attribute or NULL in case of error. |
1886 | | */ |
1887 | | static xmlAttrPtr |
1888 | | xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt, |
1889 | | const xmlChar * localname, |
1890 | | const xmlChar * prefix, |
1891 | | const xmlChar * value, |
1892 | | const xmlChar * valueend) |
1893 | 3.86M | { |
1894 | 3.86M | xmlAttrPtr ret; |
1895 | 3.86M | xmlNsPtr namespace = NULL; |
1896 | 3.86M | xmlChar *dup = NULL; |
1897 | | |
1898 | | /* |
1899 | | * Note: if prefix == NULL, the attribute is not in the default namespace |
1900 | | */ |
1901 | 3.86M | if (prefix != NULL) { |
1902 | 116k | namespace = xmlParserNsLookupSax(ctxt, prefix); |
1903 | 116k | if ((namespace == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) { |
1904 | 105k | int res; |
1905 | | |
1906 | 105k | res = xmlSearchNsSafe(ctxt->node, prefix, &namespace); |
1907 | 105k | if (res < 0) |
1908 | 19 | xmlSAX2ErrMemory(ctxt); |
1909 | 105k | } |
1910 | 116k | } |
1911 | | |
1912 | | /* |
1913 | | * allocate the node |
1914 | | */ |
1915 | 3.86M | if (ctxt->freeAttrs != NULL) { |
1916 | 0 | ret = ctxt->freeAttrs; |
1917 | 0 | ctxt->freeAttrs = ret->next; |
1918 | 0 | ctxt->freeAttrsNr--; |
1919 | 3.86M | } else { |
1920 | 3.86M | ret = xmlMalloc(sizeof(*ret)); |
1921 | 3.86M | if (ret == NULL) { |
1922 | 308 | xmlSAX2ErrMemory(ctxt); |
1923 | 308 | return(NULL); |
1924 | 308 | } |
1925 | 3.86M | } |
1926 | | |
1927 | 3.86M | memset(ret, 0, sizeof(xmlAttr)); |
1928 | 3.86M | ret->type = XML_ATTRIBUTE_NODE; |
1929 | | |
1930 | | /* |
1931 | | * xmlParseBalancedChunkMemoryRecover had a bug that could result in |
1932 | | * a mismatch between ctxt->node->doc and ctxt->myDoc. We use |
1933 | | * ctxt->node->doc here, but we should somehow make sure that the |
1934 | | * document pointers match. |
1935 | | */ |
1936 | | |
1937 | | /* assert(ctxt->node->doc == ctxt->myDoc); */ |
1938 | | |
1939 | 3.86M | ret->parent = ctxt->node; |
1940 | 3.86M | ret->doc = ctxt->node->doc; |
1941 | 3.86M | ret->ns = namespace; |
1942 | | |
1943 | 3.86M | if (ctxt->dictNames) { |
1944 | 3.86M | ret->name = localname; |
1945 | 3.86M | } else { |
1946 | 0 | ret->name = xmlStrdup(localname); |
1947 | 0 | if (ret->name == NULL) |
1948 | 0 | xmlSAX2ErrMemory(ctxt); |
1949 | 0 | } |
1950 | | |
1951 | 3.86M | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
1952 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
1953 | | |
1954 | 3.86M | if ((ctxt->replaceEntities == 0) && (!ctxt->html)) { |
1955 | 13.9k | xmlNodePtr tmp; |
1956 | | |
1957 | | /* |
1958 | | * We know that if there is an entity reference, then |
1959 | | * the string has been dup'ed and terminates with 0 |
1960 | | * otherwise with ' or " |
1961 | | */ |
1962 | 13.9k | if (*valueend != 0) { |
1963 | 6.09k | tmp = xmlSAX2TextNode(ctxt, ret->doc, value, valueend - value); |
1964 | 6.09k | ret->children = tmp; |
1965 | 6.09k | ret->last = tmp; |
1966 | 6.09k | if (tmp != NULL) { |
1967 | 6.09k | tmp->parent = (xmlNodePtr) ret; |
1968 | 6.09k | } |
1969 | 7.90k | } else if (valueend > value) { |
1970 | 7.90k | if (xmlNodeParseContent((xmlNodePtr) ret, value, |
1971 | 7.90k | valueend - value) < 0) |
1972 | 0 | xmlSAX2ErrMemory(ctxt); |
1973 | 7.90k | } |
1974 | 3.85M | } else if (value != NULL) { |
1975 | 3.85M | xmlNodePtr tmp; |
1976 | | |
1977 | 3.85M | tmp = xmlSAX2TextNode(ctxt, ret->doc, value, valueend - value); |
1978 | 3.85M | ret->children = tmp; |
1979 | 3.85M | ret->last = tmp; |
1980 | 3.85M | if (tmp != NULL) { |
1981 | 3.85M | tmp->parent = (xmlNodePtr) ret; |
1982 | 3.85M | } |
1983 | 3.85M | } |
1984 | | |
1985 | | #ifdef LIBXML_VALID_ENABLED |
1986 | | if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed && |
1987 | | ctxt->myDoc && ctxt->myDoc->intSubset) { |
1988 | | /* |
1989 | | * If we don't substitute entities, the validation should be |
1990 | | * done on a value with replaced entities anyway. |
1991 | | */ |
1992 | | if (!ctxt->replaceEntities) { |
1993 | | dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend); |
1994 | | if (dup == NULL) { |
1995 | | if (*valueend == 0) { |
1996 | | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, |
1997 | | ctxt->myDoc, ctxt->node, ret, value); |
1998 | | } else { |
1999 | | /* |
2000 | | * That should already be normalized. |
2001 | | * cheaper to finally allocate here than duplicate |
2002 | | * entry points in the full validation code |
2003 | | */ |
2004 | | dup = xmlStrndup(value, valueend - value); |
2005 | | if (dup == NULL) |
2006 | | xmlSAX2ErrMemory(ctxt); |
2007 | | |
2008 | | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, |
2009 | | ctxt->myDoc, ctxt->node, ret, dup); |
2010 | | } |
2011 | | } else { |
2012 | | /* |
2013 | | * dup now contains a string of the flattened attribute |
2014 | | * content with entities substituted. Check if we need to |
2015 | | * apply an extra layer of normalization. |
2016 | | * It need to be done twice ... it's an extra burden related |
2017 | | * to the ability to keep references in attributes |
2018 | | */ |
2019 | | if (ctxt->attsSpecial != NULL) { |
2020 | | xmlChar *nvalnorm; |
2021 | | xmlChar fn[50]; |
2022 | | xmlChar *fullname; |
2023 | | |
2024 | | fullname = xmlBuildQName(localname, prefix, fn, 50); |
2025 | | if (fullname == NULL) { |
2026 | | xmlSAX2ErrMemory(ctxt); |
2027 | | } else { |
2028 | | ctxt->vctxt.valid = 1; |
2029 | | nvalnorm = xmlValidCtxtNormalizeAttributeValue( |
2030 | | &ctxt->vctxt, ctxt->myDoc, |
2031 | | ctxt->node, fullname, dup); |
2032 | | if (ctxt->vctxt.valid != 1) |
2033 | | ctxt->valid = 0; |
2034 | | |
2035 | | if ((fullname != fn) && (fullname != localname)) |
2036 | | xmlFree(fullname); |
2037 | | if (nvalnorm != NULL) { |
2038 | | xmlFree(dup); |
2039 | | dup = nvalnorm; |
2040 | | } |
2041 | | } |
2042 | | } |
2043 | | |
2044 | | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, |
2045 | | ctxt->myDoc, ctxt->node, ret, dup); |
2046 | | } |
2047 | | } else { |
2048 | | /* |
2049 | | * if entities already have been substituted, then |
2050 | | * the attribute as passed is already normalized |
2051 | | */ |
2052 | | dup = xmlStrndup(value, valueend - value); |
2053 | | if (dup == NULL) |
2054 | | xmlSAX2ErrMemory(ctxt); |
2055 | | |
2056 | | ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, |
2057 | | ctxt->myDoc, ctxt->node, ret, dup); |
2058 | | } |
2059 | | } else |
2060 | | #endif /* LIBXML_VALID_ENABLED */ |
2061 | 3.86M | if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) && |
2062 | 3.86M | (ctxt->input->entity == NULL) && |
2063 | | /* Don't create IDs containing entity references */ |
2064 | 3.86M | (ret->children != NULL) && |
2065 | 3.86M | (ret->children->type == XML_TEXT_NODE) && |
2066 | 3.86M | (ret->children->next == NULL)) { |
2067 | 3.85M | xmlChar *content = ret->children->content; |
2068 | | /* |
2069 | | * when validating, the ID registration is done at the attribute |
2070 | | * validation level. Otherwise we have to do specific handling here. |
2071 | | */ |
2072 | 3.85M | if ((prefix == ctxt->str_xml) && |
2073 | 3.85M | (localname[0] == 'i') && (localname[1] == 'd') && |
2074 | 3.85M | (localname[2] == 0)) { |
2075 | | /* |
2076 | | * Add the xml:id value |
2077 | | * |
2078 | | * Open issue: normalization of the value. |
2079 | | */ |
2080 | 9.60k | if (xmlValidateNCName(content, 1) != 0) { |
2081 | 7.72k | xmlErrId(ctxt, XML_DTD_XMLID_VALUE, |
2082 | 7.72k | "xml:id : attribute value %s is not an NCName\n", |
2083 | 7.72k | content); |
2084 | 7.72k | } |
2085 | 9.60k | xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret); |
2086 | 3.84M | } else { |
2087 | 3.84M | int res = xmlIsID(ctxt->myDoc, ctxt->node, ret); |
2088 | | |
2089 | 3.84M | if (res < 0) |
2090 | 2 | xmlCtxtErrMemory(ctxt); |
2091 | 3.84M | else if (res > 0) |
2092 | 12.0k | xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret); |
2093 | 3.82M | else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) |
2094 | 1.28k | xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret); |
2095 | 3.84M | } |
2096 | 3.85M | } |
2097 | 3.86M | if (dup != NULL) |
2098 | 0 | xmlFree(dup); |
2099 | | |
2100 | 3.86M | return(ret); |
2101 | 3.86M | } |
2102 | | |
2103 | | /** |
2104 | | * SAX2 callback when an element start has been detected by the parser. |
2105 | | * It provides the namespace information for the element, as well as |
2106 | | * the new namespace declarations on the element. |
2107 | | * |
2108 | | * @param ctx the user data (XML parser context) |
2109 | | * @param localname the local name of the element |
2110 | | * @param prefix the element namespace prefix if available |
2111 | | * @param URI the element namespace name if available |
2112 | | * @param nb_namespaces number of namespace definitions on that node |
2113 | | * @param namespaces pointer to the array of prefix/URI pairs namespace definitions |
2114 | | * @param nb_attributes the number of attributes on that node |
2115 | | * @param nb_defaulted the number of defaulted attributes. |
2116 | | * @param attributes pointer to the array of (localname/prefix/URI/value/end) |
2117 | | * attribute values. |
2118 | | */ |
2119 | | void |
2120 | | xmlSAX2StartElementNs(void *ctx, |
2121 | | const xmlChar *localname, |
2122 | | const xmlChar *prefix, |
2123 | | const xmlChar *URI, |
2124 | | int nb_namespaces, |
2125 | | const xmlChar **namespaces, |
2126 | | int nb_attributes, |
2127 | | int nb_defaulted, |
2128 | | const xmlChar **attributes) |
2129 | 6.55M | { |
2130 | 6.55M | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
2131 | 6.55M | xmlNodePtr ret; |
2132 | 6.55M | xmlNsPtr last = NULL, ns; |
2133 | 6.55M | const xmlChar *uri, *pref; |
2134 | 6.55M | xmlChar *lname = NULL; |
2135 | 6.55M | int i, j; |
2136 | | |
2137 | 6.55M | if (ctx == NULL) return; |
2138 | | |
2139 | | #ifdef LIBXML_VALID_ENABLED |
2140 | | /* |
2141 | | * First check on validity: |
2142 | | */ |
2143 | | if (ctxt->validate && |
2144 | | ((ctxt->myDoc == NULL) || |
2145 | | ((ctxt->myDoc->extSubset == NULL) && |
2146 | | ((ctxt->myDoc->intSubset == NULL) || |
2147 | | ((ctxt->myDoc->intSubset->notations == NULL) && |
2148 | | (ctxt->myDoc->intSubset->elements == NULL) && |
2149 | | (ctxt->myDoc->intSubset->attributes == NULL) && |
2150 | | (ctxt->myDoc->intSubset->entities == NULL)))))) { |
2151 | | xmlErrValid(ctxt, XML_DTD_NO_DTD, |
2152 | | "Validation failed: no DTD found !", NULL, NULL); |
2153 | | ctxt->validate = 0; |
2154 | | } |
2155 | | #endif /* LIBXML_VALID_ENABLED */ |
2156 | | |
2157 | | /* |
2158 | | * Take care of the rare case of an undefined namespace prefix |
2159 | | */ |
2160 | 6.55M | if ((prefix != NULL) && (URI == NULL)) { |
2161 | 171k | if (ctxt->dictNames) { |
2162 | 171k | const xmlChar *fullname; |
2163 | | |
2164 | 171k | fullname = xmlDictQLookup(ctxt->dict, prefix, localname); |
2165 | 171k | if (fullname == NULL) { |
2166 | 1 | xmlSAX2ErrMemory(ctxt); |
2167 | 1 | return; |
2168 | 1 | } |
2169 | 171k | localname = fullname; |
2170 | 171k | } else { |
2171 | 0 | lname = xmlBuildQName(localname, prefix, NULL, 0); |
2172 | 0 | if (lname == NULL) { |
2173 | 0 | xmlSAX2ErrMemory(ctxt); |
2174 | 0 | return; |
2175 | 0 | } |
2176 | 0 | } |
2177 | 171k | } |
2178 | | /* |
2179 | | * allocate the node |
2180 | | */ |
2181 | 6.55M | if (ctxt->freeElems != NULL) { |
2182 | 0 | ret = ctxt->freeElems; |
2183 | 0 | ctxt->freeElems = ret->next; |
2184 | 0 | ctxt->freeElemsNr--; |
2185 | 0 | memset(ret, 0, sizeof(xmlNode)); |
2186 | 0 | ret->doc = ctxt->myDoc; |
2187 | 0 | ret->type = XML_ELEMENT_NODE; |
2188 | |
|
2189 | 0 | if (ctxt->dictNames) |
2190 | 0 | ret->name = localname; |
2191 | 0 | else { |
2192 | 0 | if (lname == NULL) |
2193 | 0 | ret->name = xmlStrdup(localname); |
2194 | 0 | else |
2195 | 0 | ret->name = lname; |
2196 | 0 | if (ret->name == NULL) { |
2197 | 0 | xmlSAX2ErrMemory(ctxt); |
2198 | 0 | xmlFree(ret); |
2199 | 0 | return; |
2200 | 0 | } |
2201 | 0 | } |
2202 | 0 | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2203 | 0 | xmlRegisterNodeDefaultValue(ret); |
2204 | 6.55M | } else { |
2205 | 6.55M | if (ctxt->dictNames) |
2206 | 6.55M | ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, |
2207 | 6.55M | (xmlChar *) localname, NULL); |
2208 | 0 | else if (lname == NULL) |
2209 | 0 | ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL); |
2210 | 0 | else |
2211 | 0 | ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, lname, NULL); |
2212 | 6.55M | if (ret == NULL) { |
2213 | 130 | xmlSAX2ErrMemory(ctxt); |
2214 | 130 | return; |
2215 | 130 | } |
2216 | 6.55M | } |
2217 | | |
2218 | | /* |
2219 | | * Build the namespace list |
2220 | | */ |
2221 | 7.29M | for (i = 0,j = 0;j < nb_namespaces;j++) { |
2222 | 743k | pref = namespaces[i++]; |
2223 | 743k | uri = namespaces[i++]; |
2224 | 743k | ns = xmlNewNs(NULL, uri, pref); |
2225 | 743k | if (ns != NULL) { |
2226 | 743k | if (last == NULL) { |
2227 | 625k | ret->nsDef = last = ns; |
2228 | 625k | } else { |
2229 | 118k | last->next = ns; |
2230 | 118k | last = ns; |
2231 | 118k | } |
2232 | 743k | if ((URI != NULL) && (prefix == pref)) |
2233 | 612k | ret->ns = ns; |
2234 | 743k | } else { |
2235 | 147 | xmlSAX2ErrMemory(ctxt); |
2236 | 147 | continue; |
2237 | 147 | } |
2238 | | |
2239 | 743k | xmlParserNsUpdateSax(ctxt, pref, ns); |
2240 | | |
2241 | | #ifdef LIBXML_VALID_ENABLED |
2242 | | if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed && |
2243 | | ctxt->myDoc && ctxt->myDoc->intSubset) { |
2244 | | ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc, |
2245 | | ret, prefix, ns, uri); |
2246 | | } |
2247 | | #endif /* LIBXML_VALID_ENABLED */ |
2248 | 743k | } |
2249 | 6.55M | ctxt->nodemem = -1; |
2250 | | |
2251 | | /* |
2252 | | * Link the child element |
2253 | | */ |
2254 | 6.55M | xmlSAX2AppendChild(ctxt, ret); |
2255 | | |
2256 | | /* |
2257 | | * We are parsing a new node. |
2258 | | */ |
2259 | 6.55M | if (nodePush(ctxt, ret) < 0) { |
2260 | 367 | xmlUnlinkNode(ret); |
2261 | 367 | xmlFreeNode(ret); |
2262 | 367 | return; |
2263 | 367 | } |
2264 | | |
2265 | | /* |
2266 | | * Insert the defaulted attributes from the DTD only if requested: |
2267 | | */ |
2268 | 6.55M | if ((nb_defaulted != 0) && |
2269 | 6.55M | ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0)) |
2270 | 272 | nb_attributes -= nb_defaulted; |
2271 | | |
2272 | | /* |
2273 | | * Search the namespace if it wasn't already found |
2274 | | * Note that, if prefix is NULL, this searches for the default Ns |
2275 | | */ |
2276 | 6.55M | if ((URI != NULL) && (ret->ns == NULL)) { |
2277 | 2.76M | ret->ns = xmlParserNsLookupSax(ctxt, prefix); |
2278 | 2.76M | if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) { |
2279 | 17.9k | int res; |
2280 | | |
2281 | 17.9k | res = xmlSearchNsSafe(ret, prefix, &ret->ns); |
2282 | 17.9k | if (res < 0) |
2283 | 1 | xmlSAX2ErrMemory(ctxt); |
2284 | 17.9k | } |
2285 | 2.76M | if (ret->ns == NULL) { |
2286 | 96 | ns = xmlNewNs(ret, NULL, prefix); |
2287 | 96 | if (ns == NULL) { |
2288 | | |
2289 | 96 | xmlSAX2ErrMemory(ctxt); |
2290 | 96 | return; |
2291 | 96 | } |
2292 | 0 | if (prefix != NULL) |
2293 | 0 | xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
2294 | 0 | "Namespace prefix %s was not found\n", |
2295 | 0 | prefix, NULL); |
2296 | 0 | else |
2297 | 0 | xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
2298 | 0 | "Namespace default prefix was not found\n", |
2299 | 0 | NULL, NULL); |
2300 | 0 | } |
2301 | 2.76M | } |
2302 | | |
2303 | | /* |
2304 | | * process all the other attributes |
2305 | | */ |
2306 | 6.55M | if (nb_attributes > 0) { |
2307 | 3.32M | xmlAttrPtr prev = NULL; |
2308 | | |
2309 | 7.19M | for (j = 0,i = 0;i < nb_attributes;i++,j+=5) { |
2310 | 3.86M | xmlAttrPtr attr = NULL; |
2311 | | |
2312 | | /* |
2313 | | * Handle the rare case of an undefined attribute prefix |
2314 | | */ |
2315 | 3.86M | if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) { |
2316 | 39.6k | if (ctxt->dictNames) { |
2317 | 39.6k | const xmlChar *fullname; |
2318 | | |
2319 | 39.6k | fullname = xmlDictQLookup(ctxt->dict, attributes[j+1], |
2320 | 39.6k | attributes[j]); |
2321 | 39.6k | if (fullname == NULL) { |
2322 | 1 | xmlSAX2ErrMemory(ctxt); |
2323 | 1 | return; |
2324 | 1 | } |
2325 | 39.6k | attr = xmlSAX2AttributeNs(ctxt, fullname, NULL, |
2326 | 39.6k | attributes[j+3], |
2327 | 39.6k | attributes[j+4]); |
2328 | 39.6k | goto have_attr; |
2329 | 39.6k | } else { |
2330 | 0 | lname = xmlBuildQName(attributes[j], attributes[j+1], |
2331 | 0 | NULL, 0); |
2332 | 0 | if (lname == NULL) { |
2333 | 0 | xmlSAX2ErrMemory(ctxt); |
2334 | 0 | return; |
2335 | 0 | } |
2336 | 0 | attr = xmlSAX2AttributeNs(ctxt, lname, NULL, |
2337 | 0 | attributes[j+3], |
2338 | 0 | attributes[j+4]); |
2339 | 0 | xmlFree(lname); |
2340 | 0 | goto have_attr; |
2341 | 0 | } |
2342 | 39.6k | } |
2343 | 3.82M | attr = xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1], |
2344 | 3.82M | attributes[j+3], attributes[j+4]); |
2345 | 3.86M | have_attr: |
2346 | 3.86M | if (attr == NULL) |
2347 | 308 | continue; |
2348 | | |
2349 | | /* link at the end to preserve order */ |
2350 | 3.86M | if (prev == NULL) { |
2351 | 3.32M | ctxt->node->properties = attr; |
2352 | 3.32M | } else { |
2353 | 537k | prev->next = attr; |
2354 | 537k | attr->prev = prev; |
2355 | 537k | } |
2356 | | |
2357 | 3.86M | prev = attr; |
2358 | 3.86M | } |
2359 | 3.32M | } |
2360 | | |
2361 | | #ifdef LIBXML_VALID_ENABLED |
2362 | | /* |
2363 | | * If it's the Document root, finish the DTD validation and |
2364 | | * check the document root element for validity |
2365 | | */ |
2366 | | if ((ctxt->validate) && |
2367 | | ((ctxt->vctxt.flags & XML_VCTXT_DTD_VALIDATED) == 0)) { |
2368 | | int chk; |
2369 | | |
2370 | | chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc); |
2371 | | if (chk <= 0) |
2372 | | ctxt->valid = 0; |
2373 | | if (chk < 0) |
2374 | | ctxt->wellFormed = 0; |
2375 | | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
2376 | | ctxt->vctxt.flags |= XML_VCTXT_DTD_VALIDATED; |
2377 | | } |
2378 | | #endif /* LIBXML_VALID_ENABLED */ |
2379 | 6.55M | } |
2380 | | |
2381 | | /** |
2382 | | * SAX2 callback when an element end has been detected by the parser. |
2383 | | * It provides the namespace information for the element. |
2384 | | * |
2385 | | * @param ctx the user data (XML parser context) |
2386 | | * @param localname the local name of the element |
2387 | | * @param prefix the element namespace prefix if available |
2388 | | * @param URI the element namespace name if available |
2389 | | */ |
2390 | | void |
2391 | | xmlSAX2EndElementNs(void *ctx, |
2392 | | const xmlChar * localname ATTRIBUTE_UNUSED, |
2393 | | const xmlChar * prefix ATTRIBUTE_UNUSED, |
2394 | | const xmlChar * URI ATTRIBUTE_UNUSED) |
2395 | 3.84M | { |
2396 | 3.84M | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
2397 | | |
2398 | 3.84M | if (ctx == NULL) return; |
2399 | 3.84M | ctxt->nodemem = -1; |
2400 | | |
2401 | | #ifdef LIBXML_VALID_ENABLED |
2402 | | if (ctxt->validate && ctxt->wellFormed && |
2403 | | ctxt->myDoc && ctxt->myDoc->intSubset) |
2404 | | ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, |
2405 | | ctxt->node); |
2406 | | #endif /* LIBXML_VALID_ENABLED */ |
2407 | | |
2408 | | /* |
2409 | | * end of parsing of this node. |
2410 | | */ |
2411 | 3.84M | nodePop(ctxt); |
2412 | 3.84M | } |
2413 | | |
2414 | | /** |
2415 | | * called when an entity #xmlSAX2Reference is detected. |
2416 | | * |
2417 | | * @param ctx the user data (XML parser context) |
2418 | | * @param name The entity name |
2419 | | */ |
2420 | | void |
2421 | | xmlSAX2Reference(void *ctx, const xmlChar *name) |
2422 | 3.75k | { |
2423 | 3.75k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
2424 | 3.75k | xmlNodePtr ret; |
2425 | | |
2426 | 3.75k | if (ctx == NULL) return; |
2427 | 3.75k | ret = xmlNewReference(ctxt->myDoc, name); |
2428 | 3.75k | if (ret == NULL) { |
2429 | 0 | xmlSAX2ErrMemory(ctxt); |
2430 | 0 | return; |
2431 | 0 | } |
2432 | | |
2433 | 3.75k | xmlSAX2AppendChild(ctxt, ret); |
2434 | 3.75k | } |
2435 | | |
2436 | | /** |
2437 | | * Append characters. |
2438 | | * |
2439 | | * @param ctxt the parser context |
2440 | | * @param ch a xmlChar string |
2441 | | * @param len the number of xmlChar |
2442 | | * @param type text or cdata |
2443 | | */ |
2444 | | static void |
2445 | | xmlSAX2Text(xmlParserCtxtPtr ctxt, const xmlChar *ch, int len, |
2446 | | xmlElementType type) |
2447 | 5.49M | { |
2448 | 5.49M | xmlNodePtr lastChild; |
2449 | 5.49M | xmlNodePtr parent; |
2450 | | |
2451 | 5.49M | if (ctxt == NULL) |
2452 | 0 | return; |
2453 | | |
2454 | 5.49M | parent = ctxt->node; |
2455 | 5.49M | if (parent == NULL) |
2456 | 0 | return; |
2457 | 5.49M | lastChild = parent->last; |
2458 | | |
2459 | | /* |
2460 | | * Try to merge with previous text node using size and capacity |
2461 | | * stored in the parser context to avoid naive concatenation. |
2462 | | * |
2463 | | * Don't merge CDATA sections. In HTML mode, CDATA is used for |
2464 | | * raw text which should be merged. |
2465 | | */ |
2466 | 5.49M | if ((lastChild == NULL) || |
2467 | 5.49M | (lastChild->type != type) || |
2468 | 5.49M | ((!ctxt->html) && (type != XML_TEXT_NODE))) { |
2469 | 4.38M | xmlNode *node; |
2470 | | |
2471 | 4.38M | if (type == XML_TEXT_NODE) |
2472 | 4.37M | node = xmlSAX2TextNode(ctxt, parent->doc, ch, len); |
2473 | 3.11k | else |
2474 | 3.11k | node = xmlNewCDataBlock(parent->doc, ch, len); |
2475 | 4.38M | if (node == NULL) { |
2476 | 151 | xmlSAX2ErrMemory(ctxt); |
2477 | 151 | return; |
2478 | 151 | } |
2479 | | |
2480 | 4.38M | if (lastChild == NULL) { |
2481 | 1.14M | parent->children = node; |
2482 | 1.14M | parent->last = node; |
2483 | 1.14M | node->parent = parent; |
2484 | 3.23M | } else { |
2485 | 3.23M | xmlSAX2AppendChild(ctxt, node); |
2486 | 3.23M | } |
2487 | | |
2488 | 4.38M | ctxt->nodelen = len; |
2489 | 4.38M | ctxt->nodemem = len + 1; |
2490 | 4.38M | lastChild = node; |
2491 | 4.38M | } else { |
2492 | 1.11M | xmlChar *content; |
2493 | 1.11M | int oldSize, newSize, capacity; |
2494 | 1.11M | int maxSize = (ctxt->options & XML_PARSE_HUGE) ? |
2495 | 0 | XML_MAX_HUGE_LENGTH : |
2496 | 1.11M | XML_MAX_TEXT_LENGTH; |
2497 | | |
2498 | 1.11M | content = lastChild->content; |
2499 | 1.11M | oldSize = ctxt->nodelen; |
2500 | 1.11M | capacity = ctxt->nodemem; |
2501 | | |
2502 | | /* Shouldn't happen */ |
2503 | 1.11M | if ((content == NULL) || (capacity <= 0)) { |
2504 | 0 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
2505 | 0 | "xmlSAX2Text: no content"); |
2506 | 0 | return; |
2507 | 0 | } |
2508 | | |
2509 | 1.11M | if ((len > maxSize) || (oldSize > maxSize - len)) { |
2510 | 0 | xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, |
2511 | 0 | "Text node too long, try XML_PARSE_HUGE"); |
2512 | 0 | xmlHaltParser(ctxt); |
2513 | 0 | return; |
2514 | 0 | } |
2515 | | |
2516 | 1.11M | newSize = oldSize + len; |
2517 | | |
2518 | 1.11M | if (newSize >= capacity) { |
2519 | 522k | if (newSize <= 20) |
2520 | 144k | capacity = 40; |
2521 | 377k | else |
2522 | 377k | capacity = newSize > INT_MAX / 2 ? INT_MAX : newSize * 2; |
2523 | | |
2524 | | /* |
2525 | | * If the content was stored in properties or in |
2526 | | * the dictionary, don't realloc. |
2527 | | */ |
2528 | 522k | if ((content == (xmlChar *) &lastChild->properties) || |
2529 | 522k | ((ctxt->nodemem == oldSize + 1) && |
2530 | 522k | (xmlDictOwns(ctxt->dict, content)))) { |
2531 | 1.59k | xmlChar *newContent; |
2532 | | |
2533 | 1.59k | newContent = xmlMalloc(capacity); |
2534 | 1.59k | if (newContent == NULL) { |
2535 | 0 | xmlSAX2ErrMemory(ctxt); |
2536 | 0 | return; |
2537 | 0 | } |
2538 | | |
2539 | 1.59k | memcpy(newContent, content, oldSize); |
2540 | 1.59k | lastChild->properties = NULL; |
2541 | 1.59k | content = newContent; |
2542 | 520k | } else { |
2543 | 520k | content = xmlRealloc(content, capacity); |
2544 | 520k | if (content == NULL) { |
2545 | 22 | xmlSAX2ErrMemory(ctxt); |
2546 | 22 | return; |
2547 | 22 | } |
2548 | 520k | } |
2549 | | |
2550 | 522k | ctxt->nodemem = capacity; |
2551 | 522k | lastChild->content = content; |
2552 | 522k | } |
2553 | | |
2554 | 1.11M | memcpy(&content[oldSize], ch, len); |
2555 | 1.11M | content[newSize] = 0; |
2556 | 1.11M | ctxt->nodelen = newSize; |
2557 | 1.11M | } |
2558 | | |
2559 | 5.49M | if ((lastChild != NULL) && |
2560 | 5.49M | (type == XML_TEXT_NODE) && |
2561 | 5.49M | (ctxt->input != NULL)) { |
2562 | 5.49M | if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX) |
2563 | 5.44M | lastChild->line = ctxt->input->line; |
2564 | 46.4k | else { |
2565 | 46.4k | lastChild->line = USHRT_MAX; |
2566 | 46.4k | if (ctxt->options & XML_PARSE_BIG_LINES) |
2567 | 0 | lastChild->psvi = XML_INT_TO_PTR(ctxt->input->line); |
2568 | 46.4k | } |
2569 | 5.49M | } |
2570 | 5.49M | } |
2571 | | |
2572 | | /** |
2573 | | * receiving some chars from the parser. |
2574 | | * |
2575 | | * @param ctx the user data (XML parser context) |
2576 | | * @param ch a xmlChar string |
2577 | | * @param len the number of xmlChar |
2578 | | */ |
2579 | | void |
2580 | | xmlSAX2Characters(void *ctx, const xmlChar *ch, int len) |
2581 | 5.49M | { |
2582 | 5.49M | xmlSAX2Text((xmlParserCtxtPtr) ctx, ch, len, XML_TEXT_NODE); |
2583 | 5.49M | } |
2584 | | |
2585 | | /** |
2586 | | * receiving some ignorable whitespaces from the parser. |
2587 | | * UNUSED: by default the DOM building will use #xmlSAX2Characters |
2588 | | * |
2589 | | * @param ctx the user data (XML parser context) |
2590 | | * @param ch a xmlChar string |
2591 | | * @param len the number of xmlChar |
2592 | | */ |
2593 | | void |
2594 | | xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED) |
2595 | 0 | { |
2596 | 0 | } |
2597 | | |
2598 | | /** |
2599 | | * A processing instruction has been parsed. |
2600 | | * |
2601 | | * @param ctx the user data (XML parser context) |
2602 | | * @param target the target name |
2603 | | * @param data the PI data's |
2604 | | */ |
2605 | | void |
2606 | | xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target, |
2607 | | const xmlChar *data) |
2608 | 98.3k | { |
2609 | 98.3k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
2610 | 98.3k | xmlNodePtr ret; |
2611 | | |
2612 | 98.3k | if (ctx == NULL) return; |
2613 | | |
2614 | 98.3k | ret = xmlNewDocPI(ctxt->myDoc, target, data); |
2615 | 98.3k | if (ret == NULL) { |
2616 | 7 | xmlSAX2ErrMemory(ctxt); |
2617 | 7 | return; |
2618 | 7 | } |
2619 | | |
2620 | 98.3k | xmlSAX2AppendChild(ctxt, ret); |
2621 | 98.3k | } |
2622 | | |
2623 | | /** |
2624 | | * A #xmlSAX2Comment has been parsed. |
2625 | | * |
2626 | | * @param ctx the user data (XML parser context) |
2627 | | * @param value the #xmlSAX2Comment content |
2628 | | */ |
2629 | | void |
2630 | | xmlSAX2Comment(void *ctx, const xmlChar *value) |
2631 | 99.7k | { |
2632 | 99.7k | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; |
2633 | 99.7k | xmlNodePtr ret; |
2634 | | |
2635 | 99.7k | if (ctx == NULL) return; |
2636 | | |
2637 | 99.7k | ret = xmlNewDocComment(ctxt->myDoc, value); |
2638 | 99.7k | if (ret == NULL) { |
2639 | 6 | xmlSAX2ErrMemory(ctxt); |
2640 | 6 | return; |
2641 | 6 | } |
2642 | | |
2643 | 99.7k | xmlSAX2AppendChild(ctxt, ret); |
2644 | 99.7k | } |
2645 | | |
2646 | | /** |
2647 | | * called when a pcdata block has been parsed |
2648 | | * |
2649 | | * @param ctx the user data (XML parser context) |
2650 | | * @param value The pcdata content |
2651 | | * @param len the block length |
2652 | | */ |
2653 | | void |
2654 | | xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len) |
2655 | 3.11k | { |
2656 | 3.11k | xmlSAX2Text((xmlParserCtxtPtr) ctx, value, len, XML_CDATA_SECTION_NODE); |
2657 | 3.11k | } |
2658 | | |
2659 | | #ifdef LIBXML_SAX1_ENABLED |
2660 | | /** |
2661 | | * Has no effect. |
2662 | | * |
2663 | | * @deprecated Use parser option XML_PARSE_SAX1. |
2664 | | * |
2665 | | * @param version the version, must be 2 |
2666 | | * @returns 2 in case of success and -1 in case of error. |
2667 | | */ |
2668 | | int |
2669 | | xmlSAXDefaultVersion(int version) |
2670 | | { |
2671 | | if (version != 2) |
2672 | | return(-1); |
2673 | | return(2); |
2674 | | } |
2675 | | #endif /* LIBXML_SAX1_ENABLED */ |
2676 | | |
2677 | | /** |
2678 | | * Initialize the default XML SAX handler according to the version |
2679 | | * |
2680 | | * @param hdlr the SAX handler |
2681 | | * @param version the version, 1 or 2 |
2682 | | * @returns 0 in case of success and -1 in case of error. |
2683 | | */ |
2684 | | int |
2685 | | xmlSAXVersion(xmlSAXHandler *hdlr, int version) |
2686 | 1.00M | { |
2687 | 1.00M | if (hdlr == NULL) return(-1); |
2688 | 1.00M | if (version == 2) { |
2689 | 1.00M | hdlr->startElementNs = xmlSAX2StartElementNs; |
2690 | 1.00M | hdlr->endElementNs = xmlSAX2EndElementNs; |
2691 | 1.00M | hdlr->serror = NULL; |
2692 | 1.00M | hdlr->initialized = XML_SAX2_MAGIC; |
2693 | | #ifdef LIBXML_SAX1_ENABLED |
2694 | | } else if (version == 1) { |
2695 | | hdlr->initialized = 1; |
2696 | | #endif /* LIBXML_SAX1_ENABLED */ |
2697 | 1.00M | } else |
2698 | 0 | return(-1); |
2699 | | #ifdef LIBXML_SAX1_ENABLED |
2700 | | hdlr->startElement = xmlSAX2StartElement; |
2701 | | hdlr->endElement = xmlSAX2EndElement; |
2702 | | #else |
2703 | 1.00M | hdlr->startElement = NULL; |
2704 | 1.00M | hdlr->endElement = NULL; |
2705 | 1.00M | #endif /* LIBXML_SAX1_ENABLED */ |
2706 | 1.00M | hdlr->internalSubset = xmlSAX2InternalSubset; |
2707 | 1.00M | hdlr->externalSubset = xmlSAX2ExternalSubset; |
2708 | 1.00M | hdlr->isStandalone = xmlSAX2IsStandalone; |
2709 | 1.00M | hdlr->hasInternalSubset = xmlSAX2HasInternalSubset; |
2710 | 1.00M | hdlr->hasExternalSubset = xmlSAX2HasExternalSubset; |
2711 | 1.00M | hdlr->resolveEntity = xmlSAX2ResolveEntity; |
2712 | 1.00M | hdlr->getEntity = xmlSAX2GetEntity; |
2713 | 1.00M | hdlr->getParameterEntity = xmlSAX2GetParameterEntity; |
2714 | 1.00M | hdlr->entityDecl = xmlSAX2EntityDecl; |
2715 | 1.00M | hdlr->attributeDecl = xmlSAX2AttributeDecl; |
2716 | 1.00M | hdlr->elementDecl = xmlSAX2ElementDecl; |
2717 | 1.00M | hdlr->notationDecl = xmlSAX2NotationDecl; |
2718 | 1.00M | hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl; |
2719 | 1.00M | hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator; |
2720 | 1.00M | hdlr->startDocument = xmlSAX2StartDocument; |
2721 | 1.00M | hdlr->endDocument = xmlSAX2EndDocument; |
2722 | 1.00M | hdlr->reference = xmlSAX2Reference; |
2723 | 1.00M | hdlr->characters = xmlSAX2Characters; |
2724 | 1.00M | hdlr->cdataBlock = xmlSAX2CDataBlock; |
2725 | 1.00M | hdlr->ignorableWhitespace = xmlSAX2Characters; |
2726 | 1.00M | hdlr->processingInstruction = xmlSAX2ProcessingInstruction; |
2727 | 1.00M | hdlr->comment = xmlSAX2Comment; |
2728 | 1.00M | hdlr->warning = xmlParserWarning; |
2729 | 1.00M | hdlr->error = xmlParserError; |
2730 | 1.00M | hdlr->fatalError = xmlParserError; |
2731 | | |
2732 | 1.00M | return(0); |
2733 | 1.00M | } |
2734 | | |
2735 | | /** |
2736 | | * Initialize the default XML SAX2 handler |
2737 | | * |
2738 | | * @param hdlr the SAX handler |
2739 | | * @param warning flag if non-zero sets the handler warning procedure |
2740 | | */ |
2741 | | void |
2742 | | xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning) |
2743 | 0 | { |
2744 | 0 | if ((hdlr == NULL) || (hdlr->initialized != 0)) |
2745 | 0 | return; |
2746 | | |
2747 | 0 | xmlSAXVersion(hdlr, 2); |
2748 | 0 | if (warning == 0) |
2749 | 0 | hdlr->warning = NULL; |
2750 | 0 | } |
2751 | | |
2752 | | /** |
2753 | | * Initialize the default SAX2 handler |
2754 | | * |
2755 | | * @deprecated This function is a no-op. Call #xmlInitParser to |
2756 | | * initialize the library. |
2757 | | * |
2758 | | */ |
2759 | | void |
2760 | | xmlDefaultSAXHandlerInit(void) |
2761 | 0 | { |
2762 | 0 | } |
2763 | | |
2764 | | #ifdef LIBXML_HTML_ENABLED |
2765 | | |
2766 | | /** |
2767 | | * Initialize the default HTML SAX2 handler |
2768 | | * |
2769 | | * @param hdlr the SAX handler |
2770 | | */ |
2771 | | void |
2772 | | xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr) |
2773 | 0 | { |
2774 | 0 | if ((hdlr == NULL) || (hdlr->initialized != 0)) |
2775 | 0 | return; |
2776 | | |
2777 | 0 | hdlr->internalSubset = xmlSAX2InternalSubset; |
2778 | 0 | hdlr->externalSubset = NULL; |
2779 | 0 | hdlr->isStandalone = NULL; |
2780 | 0 | hdlr->hasInternalSubset = NULL; |
2781 | 0 | hdlr->hasExternalSubset = NULL; |
2782 | 0 | hdlr->resolveEntity = NULL; |
2783 | 0 | hdlr->getEntity = xmlSAX2GetEntity; |
2784 | 0 | hdlr->getParameterEntity = NULL; |
2785 | 0 | hdlr->entityDecl = NULL; |
2786 | 0 | hdlr->attributeDecl = NULL; |
2787 | 0 | hdlr->elementDecl = NULL; |
2788 | 0 | hdlr->notationDecl = NULL; |
2789 | 0 | hdlr->unparsedEntityDecl = NULL; |
2790 | 0 | hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator; |
2791 | 0 | hdlr->startDocument = xmlSAX2StartDocument; |
2792 | 0 | hdlr->endDocument = xmlSAX2EndDocument; |
2793 | 0 | hdlr->startElement = xmlSAX2StartElement; |
2794 | 0 | hdlr->endElement = xmlSAX2EndElement; |
2795 | 0 | hdlr->reference = NULL; |
2796 | 0 | hdlr->characters = xmlSAX2Characters; |
2797 | 0 | hdlr->cdataBlock = xmlSAX2CDataBlock; |
2798 | 0 | hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
2799 | 0 | hdlr->processingInstruction = xmlSAX2ProcessingInstruction; |
2800 | 0 | hdlr->comment = xmlSAX2Comment; |
2801 | 0 | hdlr->warning = xmlParserWarning; |
2802 | 0 | hdlr->error = xmlParserError; |
2803 | 0 | hdlr->fatalError = xmlParserError; |
2804 | |
|
2805 | 0 | hdlr->initialized = 1; |
2806 | 0 | } |
2807 | | |
2808 | | /** |
2809 | | * @deprecated This function is a no-op. Call #xmlInitParser to |
2810 | | * initialize the library. |
2811 | | */ |
2812 | | void |
2813 | | htmlDefaultSAXHandlerInit(void) |
2814 | 0 | { |
2815 | 0 | } |
2816 | | |
2817 | | #endif /* LIBXML_HTML_ENABLED */ |