Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * valid.c : part of the code use to do the DTD handling and the validity |
3 | | * checking |
4 | | * |
5 | | * See Copyright for the status of this software. |
6 | | * |
7 | | * Author: Daniel Veillard |
8 | | */ |
9 | | |
10 | | #define IN_LIBXML |
11 | | #include "libxml.h" |
12 | | |
13 | | #include <string.h> |
14 | | #include <stdlib.h> |
15 | | |
16 | | #include <libxml/xmlmemory.h> |
17 | | #include <libxml/hash.h> |
18 | | #include <libxml/uri.h> |
19 | | #include <libxml/valid.h> |
20 | | #include <libxml/parser.h> |
21 | | #include <libxml/parserInternals.h> |
22 | | #include <libxml/xmlerror.h> |
23 | | #include <libxml/list.h> |
24 | | #include <libxml/xmlsave.h> |
25 | | |
26 | | #include "private/error.h" |
27 | | #include "private/memory.h" |
28 | | #include "private/parser.h" |
29 | | #include "private/regexp.h" |
30 | | #include "private/save.h" |
31 | | #include "private/tree.h" |
32 | | |
33 | | static xmlElementPtr |
34 | | xmlGetDtdElementDesc2(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name); |
35 | | |
36 | | #ifdef LIBXML_VALID_ENABLED |
37 | | static int |
38 | | xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type, |
39 | | const xmlChar *value); |
40 | | #endif |
41 | | /************************************************************************ |
42 | | * * |
43 | | * Error handling routines * |
44 | | * * |
45 | | ************************************************************************/ |
46 | | |
47 | | /** |
48 | | * Handle an out of memory error |
49 | | * |
50 | | * @param ctxt an XML validation parser context |
51 | | */ |
52 | | static void |
53 | | xmlVErrMemory(xmlValidCtxtPtr ctxt) |
54 | 0 | { |
55 | 0 | if (ctxt != NULL) { |
56 | 0 | if (ctxt->flags & XML_VCTXT_USE_PCTXT) { |
57 | 0 | xmlCtxtErrMemory(ctxt->userData); |
58 | 0 | } else { |
59 | 0 | xmlRaiseMemoryError(NULL, ctxt->error, ctxt->userData, |
60 | 0 | XML_FROM_VALID, NULL); |
61 | 0 | } |
62 | 0 | } else { |
63 | 0 | xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_VALID, NULL); |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | | static void |
68 | | xmlDoErrValid(xmlValidCtxtPtr ctxt, xmlNodePtr node, |
69 | | xmlParserErrors code, int level, |
70 | | const xmlChar *str1, const xmlChar *str2, const xmlChar *str3, |
71 | | int int1, |
72 | 7.20k | const char *msg, ...) { |
73 | 7.20k | xmlParserCtxtPtr pctxt = NULL; |
74 | 7.20k | va_list ap; |
75 | | |
76 | 7.20k | if (ctxt == NULL) |
77 | 0 | return; |
78 | 7.20k | if (ctxt->flags & XML_VCTXT_USE_PCTXT) |
79 | 7.20k | pctxt = ctxt->userData; |
80 | | |
81 | 7.20k | va_start(ap, msg); |
82 | 7.20k | if (pctxt != NULL) { |
83 | 7.20k | xmlCtxtVErr(pctxt, node, XML_FROM_VALID, code, level, |
84 | 7.20k | str1, str2, str3, int1, msg, ap); |
85 | 7.20k | } else { |
86 | 0 | xmlGenericErrorFunc channel = NULL; |
87 | 0 | void *data = NULL; |
88 | 0 | int res; |
89 | |
|
90 | 0 | if (ctxt != NULL) { |
91 | 0 | channel = ctxt->error; |
92 | 0 | data = ctxt->userData; |
93 | 0 | } |
94 | 0 | res = xmlVRaiseError(NULL, channel, data, NULL, node, |
95 | 0 | XML_FROM_VALID, code, level, NULL, 0, |
96 | 0 | (const char *) str1, (const char *) str2, |
97 | 0 | (const char *) str2, int1, 0, |
98 | 0 | msg, ap); |
99 | 0 | if (res < 0) |
100 | 0 | xmlVErrMemory(ctxt); |
101 | 0 | } |
102 | 7.20k | va_end(ap); |
103 | 7.20k | } |
104 | | |
105 | | /** |
106 | | * Handle a validation error, provide contextual information |
107 | | * |
108 | | * @param ctxt an XML validation parser context |
109 | | * @param node the node raising the error |
110 | | * @param error the error number |
111 | | * @param msg the error message |
112 | | * @param str1 extra information |
113 | | * @param str2 extra information |
114 | | * @param str3 extra information |
115 | | */ |
116 | | static void LIBXML_ATTR_FORMAT(4,0) |
117 | | xmlErrValidNode(xmlValidCtxtPtr ctxt, |
118 | | xmlNodePtr node, xmlParserErrors error, |
119 | | const char *msg, const xmlChar * str1, |
120 | | const xmlChar * str2, const xmlChar * str3) |
121 | 7.20k | { |
122 | 7.20k | xmlDoErrValid(ctxt, node, error, XML_ERR_ERROR, str1, str2, str3, 0, |
123 | 7.20k | msg, str1, str2, str3); |
124 | 7.20k | } |
125 | | |
126 | | #ifdef LIBXML_VALID_ENABLED |
127 | | /** |
128 | | * Handle a validation error |
129 | | * |
130 | | * @param ctxt an XML validation parser context |
131 | | * @param error the error number |
132 | | * @param msg the error message |
133 | | * @param extra extra information |
134 | | */ |
135 | | static void LIBXML_ATTR_FORMAT(3,0) |
136 | | xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error, |
137 | | const char *msg, const char *extra) |
138 | | { |
139 | | xmlDoErrValid(ctxt, NULL, error, XML_ERR_ERROR, (const xmlChar *) extra, |
140 | | NULL, NULL, 0, msg, extra); |
141 | | } |
142 | | |
143 | | /** |
144 | | * Handle a validation error, provide contextual information |
145 | | * |
146 | | * @param ctxt an XML validation parser context |
147 | | * @param node the node raising the error |
148 | | * @param error the error number |
149 | | * @param msg the error message |
150 | | * @param str1 extra information |
151 | | * @param int2 extra information |
152 | | * @param str3 extra information |
153 | | */ |
154 | | static void LIBXML_ATTR_FORMAT(4,0) |
155 | | xmlErrValidNodeNr(xmlValidCtxtPtr ctxt, |
156 | | xmlNodePtr node, xmlParserErrors error, |
157 | | const char *msg, const xmlChar * str1, |
158 | | int int2, const xmlChar * str3) |
159 | | { |
160 | | xmlDoErrValid(ctxt, node, error, XML_ERR_ERROR, str1, str3, NULL, int2, |
161 | | msg, str1, int2, str3); |
162 | | } |
163 | | |
164 | | /** |
165 | | * Handle a validation error, provide contextual information |
166 | | * |
167 | | * @param ctxt an XML validation parser context |
168 | | * @param node the node raising the error |
169 | | * @param error the error number |
170 | | * @param msg the error message |
171 | | * @param str1 extra information |
172 | | * @param str2 extra information |
173 | | * @param str3 extra information |
174 | | */ |
175 | | static void LIBXML_ATTR_FORMAT(4,0) |
176 | | xmlErrValidWarning(xmlValidCtxtPtr ctxt, |
177 | | xmlNodePtr node, xmlParserErrors error, |
178 | | const char *msg, const xmlChar * str1, |
179 | | const xmlChar * str2, const xmlChar * str3) |
180 | | { |
181 | | xmlDoErrValid(ctxt, node, error, XML_ERR_WARNING, str1, str2, str3, 0, |
182 | | msg, str1, str2, str3); |
183 | | } |
184 | | |
185 | | |
186 | | |
187 | | #ifdef LIBXML_REGEXP_ENABLED |
188 | | /* |
189 | | * If regexp are enabled we can do continuous validation without the |
190 | | * need of a tree to validate the content model. this is done in each |
191 | | * callbacks. |
192 | | * Each xmlValidState represent the validation state associated to the |
193 | | * set of nodes currently open from the document root to the current element. |
194 | | */ |
195 | | |
196 | | |
197 | | typedef struct _xmlValidState { |
198 | | xmlElementPtr elemDecl; /* pointer to the content model */ |
199 | | xmlNodePtr node; /* pointer to the current node */ |
200 | | xmlRegExecCtxtPtr exec; /* regexp runtime */ |
201 | | } _xmlValidState; |
202 | | |
203 | | |
204 | | static int |
205 | | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) { |
206 | | if (ctxt->vstateNr >= ctxt->vstateMax) { |
207 | | xmlValidState *tmp; |
208 | | int newSize; |
209 | | |
210 | | newSize = xmlGrowCapacity(ctxt->vstateMax, sizeof(tmp[0]), |
211 | | 10, XML_MAX_ITEMS); |
212 | | if (newSize < 0) { |
213 | | xmlVErrMemory(ctxt); |
214 | | return(-1); |
215 | | } |
216 | | tmp = xmlRealloc(ctxt->vstateTab, newSize * sizeof(tmp[0])); |
217 | | if (tmp == NULL) { |
218 | | xmlVErrMemory(ctxt); |
219 | | return(-1); |
220 | | } |
221 | | ctxt->vstateTab = tmp; |
222 | | ctxt->vstateMax = newSize; |
223 | | } |
224 | | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr]; |
225 | | ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl; |
226 | | ctxt->vstateTab[ctxt->vstateNr].node = node; |
227 | | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
228 | | if (elemDecl->contModel == NULL) |
229 | | xmlValidBuildContentModel(ctxt, elemDecl); |
230 | | if (elemDecl->contModel != NULL) { |
231 | | ctxt->vstateTab[ctxt->vstateNr].exec = |
232 | | xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
233 | | if (ctxt->vstateTab[ctxt->vstateNr].exec == NULL) { |
234 | | xmlVErrMemory(ctxt); |
235 | | return(-1); |
236 | | } |
237 | | } else { |
238 | | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
239 | | xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl, |
240 | | XML_ERR_INTERNAL_ERROR, |
241 | | "Failed to build content model regexp for %s\n", |
242 | | node->name, NULL, NULL); |
243 | | } |
244 | | } |
245 | | return(ctxt->vstateNr++); |
246 | | } |
247 | | |
248 | | static int |
249 | | vstateVPop(xmlValidCtxtPtr ctxt) { |
250 | | xmlElementPtr elemDecl; |
251 | | |
252 | | if (ctxt->vstateNr < 1) return(-1); |
253 | | ctxt->vstateNr--; |
254 | | elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl; |
255 | | ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL; |
256 | | ctxt->vstateTab[ctxt->vstateNr].node = NULL; |
257 | | if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) { |
258 | | xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec); |
259 | | } |
260 | | ctxt->vstateTab[ctxt->vstateNr].exec = NULL; |
261 | | if (ctxt->vstateNr >= 1) |
262 | | ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1]; |
263 | | else |
264 | | ctxt->vstate = NULL; |
265 | | return(ctxt->vstateNr); |
266 | | } |
267 | | |
268 | | #else /* not LIBXML_REGEXP_ENABLED */ |
269 | | /* |
270 | | * If regexp are not enabled, it uses a home made algorithm less |
271 | | * complex and easier to |
272 | | * debug/maintain than a generic NFA -> DFA state based algo. The |
273 | | * only restriction is on the deepness of the tree limited by the |
274 | | * size of the occurs bitfield |
275 | | * |
276 | | * this is the content of a saved state for rollbacks |
277 | | */ |
278 | | |
279 | | #define ROLLBACK_OR 0 |
280 | | #define ROLLBACK_PARENT 1 |
281 | | |
282 | | typedef struct _xmlValidState { |
283 | | xmlElementContentPtr cont; /* pointer to the content model subtree */ |
284 | | xmlNodePtr node; /* pointer to the current node in the list */ |
285 | | long occurs;/* bitfield for multiple occurrences */ |
286 | | unsigned char depth; /* current depth in the overall tree */ |
287 | | unsigned char state; /* ROLLBACK_XXX */ |
288 | | } _xmlValidState; |
289 | | |
290 | | #define MAX_RECURSE 25000 |
291 | | #define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8) |
292 | | #define CONT ctxt->vstate->cont |
293 | | #define NODE ctxt->vstate->node |
294 | | #define DEPTH ctxt->vstate->depth |
295 | | #define OCCURS ctxt->vstate->occurs |
296 | | #define STATE ctxt->vstate->state |
297 | | |
298 | | #define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH)) |
299 | | #define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1)) |
300 | | |
301 | | #define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH) |
302 | | #define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1) |
303 | | |
304 | | static int |
305 | | vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont, |
306 | | xmlNodePtr node, unsigned char depth, long occurs, |
307 | | unsigned char state) { |
308 | | int i = ctxt->vstateNr - 1; |
309 | | |
310 | | if (ctxt->vstateNr >= ctxt->vstateMax) { |
311 | | xmlValidState *tmp; |
312 | | int newSize; |
313 | | |
314 | | newSize = xmlGrowCapacity(ctxt->vstateMax, sizeof(tmp[0]), |
315 | | 8, MAX_RECURSE); |
316 | | if (newSize < 0) { |
317 | | xmlVErrMemory(ctxt); |
318 | | return(-1); |
319 | | } |
320 | | tmp = xmlRealloc(ctxt->vstateTab, newSize * sizeof(tmp[0])); |
321 | | if (tmp == NULL) { |
322 | | xmlVErrMemory(ctxt); |
323 | | return(-1); |
324 | | } |
325 | | ctxt->vstateTab = tmp; |
326 | | ctxt->vstateMax = newSize; |
327 | | ctxt->vstate = &ctxt->vstateTab[0]; |
328 | | } |
329 | | /* |
330 | | * Don't push on the stack a state already here |
331 | | */ |
332 | | if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) && |
333 | | (ctxt->vstateTab[i].node == node) && |
334 | | (ctxt->vstateTab[i].depth == depth) && |
335 | | (ctxt->vstateTab[i].occurs == occurs) && |
336 | | (ctxt->vstateTab[i].state == state)) |
337 | | return(ctxt->vstateNr); |
338 | | ctxt->vstateTab[ctxt->vstateNr].cont = cont; |
339 | | ctxt->vstateTab[ctxt->vstateNr].node = node; |
340 | | ctxt->vstateTab[ctxt->vstateNr].depth = depth; |
341 | | ctxt->vstateTab[ctxt->vstateNr].occurs = occurs; |
342 | | ctxt->vstateTab[ctxt->vstateNr].state = state; |
343 | | return(ctxt->vstateNr++); |
344 | | } |
345 | | |
346 | | static int |
347 | | vstateVPop(xmlValidCtxtPtr ctxt) { |
348 | | if (ctxt->vstateNr <= 1) return(-1); |
349 | | ctxt->vstateNr--; |
350 | | ctxt->vstate = &ctxt->vstateTab[0]; |
351 | | ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont; |
352 | | ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node; |
353 | | ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth; |
354 | | ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs; |
355 | | ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state; |
356 | | return(ctxt->vstateNr); |
357 | | } |
358 | | |
359 | | #endif /* LIBXML_REGEXP_ENABLED */ |
360 | | |
361 | | static int |
362 | | nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value) |
363 | | { |
364 | | if (ctxt->nodeNr >= ctxt->nodeMax) { |
365 | | xmlNodePtr *tmp; |
366 | | int newSize; |
367 | | |
368 | | newSize = xmlGrowCapacity(ctxt->nodeMax, sizeof(tmp[0]), |
369 | | 4, XML_MAX_ITEMS); |
370 | | if (newSize < 0) { |
371 | | xmlVErrMemory(ctxt); |
372 | | return (-1); |
373 | | } |
374 | | tmp = xmlRealloc(ctxt->nodeTab, newSize * sizeof(tmp[0])); |
375 | | if (tmp == NULL) { |
376 | | xmlVErrMemory(ctxt); |
377 | | return (-1); |
378 | | } |
379 | | ctxt->nodeTab = tmp; |
380 | | ctxt->nodeMax = newSize; |
381 | | } |
382 | | ctxt->nodeTab[ctxt->nodeNr] = value; |
383 | | ctxt->node = value; |
384 | | return (ctxt->nodeNr++); |
385 | | } |
386 | | static xmlNodePtr |
387 | | nodeVPop(xmlValidCtxtPtr ctxt) |
388 | | { |
389 | | xmlNodePtr ret; |
390 | | |
391 | | if (ctxt->nodeNr <= 0) |
392 | | return (NULL); |
393 | | ctxt->nodeNr--; |
394 | | if (ctxt->nodeNr > 0) |
395 | | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
396 | | else |
397 | | ctxt->node = NULL; |
398 | | ret = ctxt->nodeTab[ctxt->nodeNr]; |
399 | | ctxt->nodeTab[ctxt->nodeNr] = NULL; |
400 | | return (ret); |
401 | | } |
402 | | |
403 | | /* TODO: use hash table for accesses to elem and attribute definitions */ |
404 | | |
405 | | |
406 | | #define CHECK_DTD \ |
407 | | if (doc == NULL) return(0); \ |
408 | | else if ((doc->intSubset == NULL) && \ |
409 | | (doc->extSubset == NULL)) return(0) |
410 | | |
411 | | #ifdef LIBXML_REGEXP_ENABLED |
412 | | |
413 | | /************************************************************************ |
414 | | * * |
415 | | * Content model validation based on the regexps * |
416 | | * * |
417 | | ************************************************************************/ |
418 | | |
419 | | /** |
420 | | * Generate the automata sequence needed for that type |
421 | | * |
422 | | * @param content the content model |
423 | | * @param ctxt the schema parser context |
424 | | * @param name the element name whose content is being built |
425 | | * @returns 1 if successful or 0 in case of error. |
426 | | */ |
427 | | static int |
428 | | xmlValidBuildAContentModel(xmlElementContentPtr content, |
429 | | xmlValidCtxtPtr ctxt, |
430 | | const xmlChar *name) { |
431 | | if (content == NULL) { |
432 | | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
433 | | "Found NULL content in content model of %s\n", |
434 | | name, NULL, NULL); |
435 | | return(0); |
436 | | } |
437 | | switch (content->type) { |
438 | | case XML_ELEMENT_CONTENT_PCDATA: |
439 | | xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR, |
440 | | "Found PCDATA in content model of %s\n", |
441 | | name, NULL, NULL); |
442 | | return(0); |
443 | | break; |
444 | | case XML_ELEMENT_CONTENT_ELEMENT: { |
445 | | xmlAutomataStatePtr oldstate = ctxt->state; |
446 | | xmlChar fn[50]; |
447 | | xmlChar *fullname; |
448 | | |
449 | | fullname = xmlBuildQName(content->name, content->prefix, fn, 50); |
450 | | if (fullname == NULL) { |
451 | | xmlVErrMemory(ctxt); |
452 | | return(0); |
453 | | } |
454 | | |
455 | | switch (content->ocur) { |
456 | | case XML_ELEMENT_CONTENT_ONCE: |
457 | | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
458 | | ctxt->state, NULL, fullname, NULL); |
459 | | break; |
460 | | case XML_ELEMENT_CONTENT_OPT: |
461 | | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
462 | | ctxt->state, NULL, fullname, NULL); |
463 | | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
464 | | break; |
465 | | case XML_ELEMENT_CONTENT_PLUS: |
466 | | ctxt->state = xmlAutomataNewTransition(ctxt->am, |
467 | | ctxt->state, NULL, fullname, NULL); |
468 | | xmlAutomataNewTransition(ctxt->am, ctxt->state, |
469 | | ctxt->state, fullname, NULL); |
470 | | break; |
471 | | case XML_ELEMENT_CONTENT_MULT: |
472 | | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
473 | | ctxt->state, NULL); |
474 | | xmlAutomataNewTransition(ctxt->am, |
475 | | ctxt->state, ctxt->state, fullname, NULL); |
476 | | break; |
477 | | } |
478 | | if ((fullname != fn) && (fullname != content->name)) |
479 | | xmlFree(fullname); |
480 | | break; |
481 | | } |
482 | | case XML_ELEMENT_CONTENT_SEQ: { |
483 | | xmlAutomataStatePtr oldstate, oldend; |
484 | | xmlElementContentOccur ocur; |
485 | | |
486 | | /* |
487 | | * Simply iterate over the content |
488 | | */ |
489 | | oldstate = ctxt->state; |
490 | | ocur = content->ocur; |
491 | | if (ocur != XML_ELEMENT_CONTENT_ONCE) { |
492 | | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
493 | | oldstate = ctxt->state; |
494 | | } |
495 | | do { |
496 | | if (xmlValidBuildAContentModel(content->c1, ctxt, name) == 0) |
497 | | return(0); |
498 | | content = content->c2; |
499 | | } while ((content->type == XML_ELEMENT_CONTENT_SEQ) && |
500 | | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
501 | | if (xmlValidBuildAContentModel(content, ctxt, name) == 0) |
502 | | return(0); |
503 | | oldend = ctxt->state; |
504 | | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
505 | | switch (ocur) { |
506 | | case XML_ELEMENT_CONTENT_ONCE: |
507 | | break; |
508 | | case XML_ELEMENT_CONTENT_OPT: |
509 | | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
510 | | break; |
511 | | case XML_ELEMENT_CONTENT_MULT: |
512 | | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
513 | | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
514 | | break; |
515 | | case XML_ELEMENT_CONTENT_PLUS: |
516 | | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
517 | | break; |
518 | | } |
519 | | break; |
520 | | } |
521 | | case XML_ELEMENT_CONTENT_OR: { |
522 | | xmlAutomataStatePtr oldstate, oldend; |
523 | | xmlElementContentOccur ocur; |
524 | | |
525 | | ocur = content->ocur; |
526 | | if ((ocur == XML_ELEMENT_CONTENT_PLUS) || |
527 | | (ocur == XML_ELEMENT_CONTENT_MULT)) { |
528 | | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, |
529 | | ctxt->state, NULL); |
530 | | } |
531 | | oldstate = ctxt->state; |
532 | | oldend = xmlAutomataNewState(ctxt->am); |
533 | | |
534 | | /* |
535 | | * iterate over the subtypes and remerge the end with an |
536 | | * epsilon transition |
537 | | */ |
538 | | do { |
539 | | ctxt->state = oldstate; |
540 | | if (xmlValidBuildAContentModel(content->c1, ctxt, name) == 0) |
541 | | return(0); |
542 | | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
543 | | content = content->c2; |
544 | | } while ((content->type == XML_ELEMENT_CONTENT_OR) && |
545 | | (content->ocur == XML_ELEMENT_CONTENT_ONCE)); |
546 | | ctxt->state = oldstate; |
547 | | if (xmlValidBuildAContentModel(content, ctxt, name) == 0) |
548 | | return(0); |
549 | | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend); |
550 | | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL); |
551 | | switch (ocur) { |
552 | | case XML_ELEMENT_CONTENT_ONCE: |
553 | | break; |
554 | | case XML_ELEMENT_CONTENT_OPT: |
555 | | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
556 | | break; |
557 | | case XML_ELEMENT_CONTENT_MULT: |
558 | | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
559 | | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
560 | | break; |
561 | | case XML_ELEMENT_CONTENT_PLUS: |
562 | | xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate); |
563 | | break; |
564 | | } |
565 | | break; |
566 | | } |
567 | | default: |
568 | | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
569 | | "ContentModel broken for element %s\n", |
570 | | (const char *) name); |
571 | | return(0); |
572 | | } |
573 | | return(1); |
574 | | } |
575 | | /** |
576 | | * (Re)Build the automata associated to the content model of this |
577 | | * element |
578 | | * |
579 | | * @deprecated Internal function, don't use. |
580 | | * |
581 | | * @param ctxt a validation context |
582 | | * @param elem an element declaration node |
583 | | * @returns 1 in case of success, 0 in case of error |
584 | | */ |
585 | | int |
586 | | xmlValidBuildContentModel(xmlValidCtxt *ctxt, xmlElement *elem) { |
587 | | int ret = 0; |
588 | | |
589 | | if ((ctxt == NULL) || (elem == NULL)) |
590 | | return(0); |
591 | | if (elem->type != XML_ELEMENT_DECL) |
592 | | return(0); |
593 | | if (elem->etype != XML_ELEMENT_TYPE_ELEMENT) |
594 | | return(1); |
595 | | /* TODO: should we rebuild in this case ? */ |
596 | | if (elem->contModel != NULL) { |
597 | | if (!xmlRegexpIsDeterminist(elem->contModel)) { |
598 | | ctxt->valid = 0; |
599 | | return(0); |
600 | | } |
601 | | return(1); |
602 | | } |
603 | | |
604 | | ctxt->am = xmlNewAutomata(); |
605 | | if (ctxt->am == NULL) { |
606 | | xmlVErrMemory(ctxt); |
607 | | return(0); |
608 | | } |
609 | | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
610 | | if (xmlValidBuildAContentModel(elem->content, ctxt, elem->name) == 0) |
611 | | goto done; |
612 | | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
613 | | elem->contModel = xmlAutomataCompile(ctxt->am); |
614 | | if (elem->contModel == NULL) { |
615 | | xmlVErrMemory(ctxt); |
616 | | goto done; |
617 | | } |
618 | | if (xmlRegexpIsDeterminist(elem->contModel) != 1) { |
619 | | char expr[5000]; |
620 | | expr[0] = 0; |
621 | | xmlSnprintfElementContent(expr, 5000, elem->content, 1); |
622 | | xmlErrValidNode(ctxt, (xmlNodePtr) elem, |
623 | | XML_DTD_CONTENT_NOT_DETERMINIST, |
624 | | "Content model of %s is not deterministic: %s\n", |
625 | | elem->name, BAD_CAST expr, NULL); |
626 | | ctxt->valid = 0; |
627 | | goto done; |
628 | | } |
629 | | |
630 | | ret = 1; |
631 | | |
632 | | done: |
633 | | ctxt->state = NULL; |
634 | | xmlFreeAutomata(ctxt->am); |
635 | | ctxt->am = NULL; |
636 | | return(ret); |
637 | | } |
638 | | |
639 | | #endif /* LIBXML_REGEXP_ENABLED */ |
640 | | |
641 | | /**************************************************************** |
642 | | * * |
643 | | * Util functions for data allocation/deallocation * |
644 | | * * |
645 | | ****************************************************************/ |
646 | | |
647 | | /** |
648 | | * Allocate a validation context structure. |
649 | | * |
650 | | * @returns the new validation context or NULL if a memory |
651 | | * allocation failed. |
652 | | */ |
653 | | xmlValidCtxt *xmlNewValidCtxt(void) { |
654 | | xmlValidCtxtPtr ret; |
655 | | |
656 | | ret = xmlMalloc(sizeof (xmlValidCtxt)); |
657 | | if (ret == NULL) |
658 | | return (NULL); |
659 | | |
660 | | (void) memset(ret, 0, sizeof (xmlValidCtxt)); |
661 | | ret->flags |= XML_VCTXT_VALIDATE; |
662 | | |
663 | | return (ret); |
664 | | } |
665 | | |
666 | | /** |
667 | | * Free a validation context structure. |
668 | | * |
669 | | * @param cur the validation context to free |
670 | | */ |
671 | | void |
672 | | xmlFreeValidCtxt(xmlValidCtxt *cur) { |
673 | | if (cur == NULL) |
674 | | return; |
675 | | if (cur->vstateTab != NULL) |
676 | | xmlFree(cur->vstateTab); |
677 | | if (cur->nodeTab != NULL) |
678 | | xmlFree(cur->nodeTab); |
679 | | xmlFree(cur); |
680 | | } |
681 | | |
682 | | #endif /* LIBXML_VALID_ENABLED */ |
683 | | |
684 | | /** |
685 | | * Allocate an element content structure for the document. |
686 | | * |
687 | | * @deprecated Internal function, don't use. |
688 | | * |
689 | | * @param doc the document |
690 | | * @param name the subelement name or NULL |
691 | | * @param type the type of element content decl |
692 | | * @returns the new element content structure or NULL on |
693 | | * error. |
694 | | */ |
695 | | xmlElementContent * |
696 | | xmlNewDocElementContent(xmlDoc *doc, const xmlChar *name, |
697 | 4 | xmlElementContentType type) { |
698 | 4 | xmlElementContentPtr ret; |
699 | 4 | xmlDictPtr dict = NULL; |
700 | | |
701 | 4 | if (doc != NULL) |
702 | 4 | dict = doc->dict; |
703 | | |
704 | 4 | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
705 | 4 | if (ret == NULL) |
706 | 0 | return(NULL); |
707 | 4 | memset(ret, 0, sizeof(xmlElementContent)); |
708 | 4 | ret->type = type; |
709 | 4 | ret->ocur = XML_ELEMENT_CONTENT_ONCE; |
710 | 4 | if (name != NULL) { |
711 | 3 | int l; |
712 | 3 | const xmlChar *tmp; |
713 | | |
714 | 3 | tmp = xmlSplitQName3(name, &l); |
715 | 3 | if (tmp == NULL) { |
716 | 0 | if (dict == NULL) |
717 | 0 | ret->name = xmlStrdup(name); |
718 | 0 | else |
719 | 0 | ret->name = xmlDictLookup(dict, name, -1); |
720 | 3 | } else { |
721 | 3 | if (dict == NULL) { |
722 | 0 | ret->prefix = xmlStrndup(name, l); |
723 | 0 | ret->name = xmlStrdup(tmp); |
724 | 3 | } else { |
725 | 3 | ret->prefix = xmlDictLookup(dict, name, l); |
726 | 3 | ret->name = xmlDictLookup(dict, tmp, -1); |
727 | 3 | } |
728 | 3 | if (ret->prefix == NULL) |
729 | 0 | goto error; |
730 | 3 | } |
731 | 3 | if (ret->name == NULL) |
732 | 0 | goto error; |
733 | 3 | } |
734 | 4 | return(ret); |
735 | | |
736 | 0 | error: |
737 | 0 | xmlFreeDocElementContent(doc, ret); |
738 | 0 | return(NULL); |
739 | 4 | } |
740 | | |
741 | | /** |
742 | | * Allocate an element content structure. |
743 | | * Deprecated in favor of #xmlNewDocElementContent |
744 | | * |
745 | | * @deprecated Internal function, don't use. |
746 | | * |
747 | | * @param name the subelement name or NULL |
748 | | * @param type the type of element content decl |
749 | | * @returns the new element content structure or NULL on |
750 | | * error. |
751 | | */ |
752 | | xmlElementContent * |
753 | 0 | xmlNewElementContent(const xmlChar *name, xmlElementContentType type) { |
754 | 0 | return(xmlNewDocElementContent(NULL, name, type)); |
755 | 0 | } |
756 | | |
757 | | /** |
758 | | * Build a copy of an element content description. |
759 | | * |
760 | | * @deprecated Internal function, don't use. |
761 | | * |
762 | | * @param doc the document owning the element declaration |
763 | | * @param cur An element content pointer. |
764 | | * @returns the new xmlElementContent or NULL in case of error. |
765 | | */ |
766 | | xmlElementContent * |
767 | 0 | xmlCopyDocElementContent(xmlDoc *doc, xmlElementContent *cur) { |
768 | 0 | xmlElementContentPtr ret = NULL, prev = NULL, tmp; |
769 | 0 | xmlDictPtr dict = NULL; |
770 | |
|
771 | 0 | if (cur == NULL) return(NULL); |
772 | | |
773 | 0 | if (doc != NULL) |
774 | 0 | dict = doc->dict; |
775 | |
|
776 | 0 | ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
777 | 0 | if (ret == NULL) |
778 | 0 | return(NULL); |
779 | 0 | memset(ret, 0, sizeof(xmlElementContent)); |
780 | 0 | ret->type = cur->type; |
781 | 0 | ret->ocur = cur->ocur; |
782 | 0 | if (cur->name != NULL) { |
783 | 0 | if (dict) |
784 | 0 | ret->name = xmlDictLookup(dict, cur->name, -1); |
785 | 0 | else |
786 | 0 | ret->name = xmlStrdup(cur->name); |
787 | 0 | if (ret->name == NULL) |
788 | 0 | goto error; |
789 | 0 | } |
790 | | |
791 | 0 | if (cur->prefix != NULL) { |
792 | 0 | if (dict) |
793 | 0 | ret->prefix = xmlDictLookup(dict, cur->prefix, -1); |
794 | 0 | else |
795 | 0 | ret->prefix = xmlStrdup(cur->prefix); |
796 | 0 | if (ret->prefix == NULL) |
797 | 0 | goto error; |
798 | 0 | } |
799 | 0 | if (cur->c1 != NULL) { |
800 | 0 | ret->c1 = xmlCopyDocElementContent(doc, cur->c1); |
801 | 0 | if (ret->c1 == NULL) |
802 | 0 | goto error; |
803 | 0 | ret->c1->parent = ret; |
804 | 0 | } |
805 | 0 | if (cur->c2 != NULL) { |
806 | 0 | prev = ret; |
807 | 0 | cur = cur->c2; |
808 | 0 | while (cur != NULL) { |
809 | 0 | tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent)); |
810 | 0 | if (tmp == NULL) |
811 | 0 | goto error; |
812 | 0 | memset(tmp, 0, sizeof(xmlElementContent)); |
813 | 0 | tmp->type = cur->type; |
814 | 0 | tmp->ocur = cur->ocur; |
815 | 0 | prev->c2 = tmp; |
816 | 0 | tmp->parent = prev; |
817 | 0 | if (cur->name != NULL) { |
818 | 0 | if (dict) |
819 | 0 | tmp->name = xmlDictLookup(dict, cur->name, -1); |
820 | 0 | else |
821 | 0 | tmp->name = xmlStrdup(cur->name); |
822 | 0 | if (tmp->name == NULL) |
823 | 0 | goto error; |
824 | 0 | } |
825 | | |
826 | 0 | if (cur->prefix != NULL) { |
827 | 0 | if (dict) |
828 | 0 | tmp->prefix = xmlDictLookup(dict, cur->prefix, -1); |
829 | 0 | else |
830 | 0 | tmp->prefix = xmlStrdup(cur->prefix); |
831 | 0 | if (tmp->prefix == NULL) |
832 | 0 | goto error; |
833 | 0 | } |
834 | 0 | if (cur->c1 != NULL) { |
835 | 0 | tmp->c1 = xmlCopyDocElementContent(doc,cur->c1); |
836 | 0 | if (tmp->c1 == NULL) |
837 | 0 | goto error; |
838 | 0 | tmp->c1->parent = tmp; |
839 | 0 | } |
840 | 0 | prev = tmp; |
841 | 0 | cur = cur->c2; |
842 | 0 | } |
843 | 0 | } |
844 | 0 | return(ret); |
845 | | |
846 | 0 | error: |
847 | 0 | xmlFreeElementContent(ret); |
848 | 0 | return(NULL); |
849 | 0 | } |
850 | | |
851 | | /** |
852 | | * Build a copy of an element content description. |
853 | | * Deprecated, use #xmlCopyDocElementContent instead |
854 | | * |
855 | | * @deprecated Internal function, don't use. |
856 | | * |
857 | | * @param cur An element content pointer. |
858 | | * @returns the new xmlElementContent or NULL in case of error. |
859 | | */ |
860 | | xmlElementContent * |
861 | 0 | xmlCopyElementContent(xmlElementContent *cur) { |
862 | 0 | return(xmlCopyDocElementContent(NULL, cur)); |
863 | 0 | } |
864 | | |
865 | | /** |
866 | | * Free an element content structure. The whole subtree is removed. |
867 | | * |
868 | | * @deprecated Internal function, don't use. |
869 | | * |
870 | | * @param doc the document owning the element declaration |
871 | | * @param cur the element content tree to free |
872 | | */ |
873 | | void |
874 | 160 | xmlFreeDocElementContent(xmlDoc *doc, xmlElementContent *cur) { |
875 | 160 | xmlDictPtr dict = NULL; |
876 | 160 | size_t depth = 0; |
877 | | |
878 | 160 | if (cur == NULL) |
879 | 158 | return; |
880 | 2 | if (doc != NULL) |
881 | 2 | dict = doc->dict; |
882 | | |
883 | 4 | while (1) { |
884 | 4 | xmlElementContentPtr parent; |
885 | | |
886 | 5 | while ((cur->c1 != NULL) || (cur->c2 != NULL)) { |
887 | 1 | cur = (cur->c1 != NULL) ? cur->c1 : cur->c2; |
888 | 1 | depth += 1; |
889 | 1 | } |
890 | | |
891 | 4 | if (dict) { |
892 | 4 | if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name))) |
893 | 0 | xmlFree((xmlChar *) cur->name); |
894 | 4 | if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix))) |
895 | 0 | xmlFree((xmlChar *) cur->prefix); |
896 | 4 | } else { |
897 | 0 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
898 | 0 | if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix); |
899 | 0 | } |
900 | 4 | parent = cur->parent; |
901 | 4 | if ((depth == 0) || (parent == NULL)) { |
902 | 2 | xmlFree(cur); |
903 | 2 | break; |
904 | 2 | } |
905 | 2 | if (cur == parent->c1) |
906 | 1 | parent->c1 = NULL; |
907 | 1 | else |
908 | 1 | parent->c2 = NULL; |
909 | 2 | xmlFree(cur); |
910 | | |
911 | 2 | if (parent->c2 != NULL) { |
912 | 1 | cur = parent->c2; |
913 | 1 | } else { |
914 | 1 | depth -= 1; |
915 | 1 | cur = parent; |
916 | 1 | } |
917 | 2 | } |
918 | 2 | } |
919 | | |
920 | | /** |
921 | | * Free an element content structure. The whole subtree is removed. |
922 | | * Deprecated, use #xmlFreeDocElementContent instead |
923 | | * |
924 | | * @deprecated Internal function, don't use. |
925 | | * |
926 | | * @param cur the element content tree to free |
927 | | */ |
928 | | void |
929 | 0 | xmlFreeElementContent(xmlElementContent *cur) { |
930 | 0 | xmlFreeDocElementContent(NULL, cur); |
931 | 0 | } |
932 | | |
933 | | #ifdef LIBXML_OUTPUT_ENABLED |
934 | | /** |
935 | | * Deprecated, unsafe, use #xmlSnprintfElementContent |
936 | | * |
937 | | * @deprecated Internal function, don't use. |
938 | | * |
939 | | * @param buf an output buffer |
940 | | * @param content An element table |
941 | | * @param englob 1 if one must print the englobing parenthesis, 0 otherwise |
942 | | */ |
943 | | void |
944 | | xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED, |
945 | | xmlElementContent *content ATTRIBUTE_UNUSED, |
946 | 0 | int englob ATTRIBUTE_UNUSED) { |
947 | 0 | } |
948 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
949 | | |
950 | | /** |
951 | | * This will dump the content of the element content definition |
952 | | * Intended just for the debug routine |
953 | | * |
954 | | * @deprecated Internal function, don't use. |
955 | | * |
956 | | * @param buf an output buffer |
957 | | * @param size the buffer size |
958 | | * @param content An element table |
959 | | * @param englob 1 if one must print the englobing parenthesis, 0 otherwise |
960 | | */ |
961 | | void |
962 | 0 | xmlSnprintfElementContent(char *buf, int size, xmlElementContent *content, int englob) { |
963 | 0 | int len; |
964 | |
|
965 | 0 | if (content == NULL) return; |
966 | 0 | len = strlen(buf); |
967 | 0 | if (size - len < 50) { |
968 | 0 | if ((size - len > 4) && (buf[len - 1] != '.')) |
969 | 0 | strcat(buf, " ..."); |
970 | 0 | return; |
971 | 0 | } |
972 | 0 | if (englob) strcat(buf, "("); |
973 | 0 | switch (content->type) { |
974 | 0 | case XML_ELEMENT_CONTENT_PCDATA: |
975 | 0 | strcat(buf, "#PCDATA"); |
976 | 0 | break; |
977 | 0 | case XML_ELEMENT_CONTENT_ELEMENT: { |
978 | 0 | int qnameLen = xmlStrlen(content->name); |
979 | |
|
980 | 0 | if (content->prefix != NULL) |
981 | 0 | qnameLen += xmlStrlen(content->prefix) + 1; |
982 | 0 | if (size - len < qnameLen + 10) { |
983 | 0 | strcat(buf, " ..."); |
984 | 0 | return; |
985 | 0 | } |
986 | 0 | if (content->prefix != NULL) { |
987 | 0 | strcat(buf, (char *) content->prefix); |
988 | 0 | strcat(buf, ":"); |
989 | 0 | } |
990 | 0 | if (content->name != NULL) |
991 | 0 | strcat(buf, (char *) content->name); |
992 | 0 | break; |
993 | 0 | } |
994 | 0 | case XML_ELEMENT_CONTENT_SEQ: |
995 | 0 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
996 | 0 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
997 | 0 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
998 | 0 | else |
999 | 0 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
1000 | 0 | len = strlen(buf); |
1001 | 0 | if (size - len < 50) { |
1002 | 0 | if ((size - len > 4) && (buf[len - 1] != '.')) |
1003 | 0 | strcat(buf, " ..."); |
1004 | 0 | return; |
1005 | 0 | } |
1006 | 0 | strcat(buf, " , "); |
1007 | 0 | if (((content->c2->type == XML_ELEMENT_CONTENT_OR) || |
1008 | 0 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
1009 | 0 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
1010 | 0 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
1011 | 0 | else |
1012 | 0 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
1013 | 0 | break; |
1014 | 0 | case XML_ELEMENT_CONTENT_OR: |
1015 | 0 | if ((content->c1->type == XML_ELEMENT_CONTENT_OR) || |
1016 | 0 | (content->c1->type == XML_ELEMENT_CONTENT_SEQ)) |
1017 | 0 | xmlSnprintfElementContent(buf, size, content->c1, 1); |
1018 | 0 | else |
1019 | 0 | xmlSnprintfElementContent(buf, size, content->c1, 0); |
1020 | 0 | len = strlen(buf); |
1021 | 0 | if (size - len < 50) { |
1022 | 0 | if ((size - len > 4) && (buf[len - 1] != '.')) |
1023 | 0 | strcat(buf, " ..."); |
1024 | 0 | return; |
1025 | 0 | } |
1026 | 0 | strcat(buf, " | "); |
1027 | 0 | if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) || |
1028 | 0 | (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) && |
1029 | 0 | (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT)) |
1030 | 0 | xmlSnprintfElementContent(buf, size, content->c2, 1); |
1031 | 0 | else |
1032 | 0 | xmlSnprintfElementContent(buf, size, content->c2, 0); |
1033 | 0 | break; |
1034 | 0 | } |
1035 | 0 | if (size - strlen(buf) <= 2) return; |
1036 | 0 | if (englob) |
1037 | 0 | strcat(buf, ")"); |
1038 | 0 | switch (content->ocur) { |
1039 | 0 | case XML_ELEMENT_CONTENT_ONCE: |
1040 | 0 | break; |
1041 | 0 | case XML_ELEMENT_CONTENT_OPT: |
1042 | 0 | strcat(buf, "?"); |
1043 | 0 | break; |
1044 | 0 | case XML_ELEMENT_CONTENT_MULT: |
1045 | 0 | strcat(buf, "*"); |
1046 | 0 | break; |
1047 | 0 | case XML_ELEMENT_CONTENT_PLUS: |
1048 | 0 | strcat(buf, "+"); |
1049 | 0 | break; |
1050 | 0 | } |
1051 | 0 | } |
1052 | | |
1053 | | /**************************************************************** |
1054 | | * * |
1055 | | * Registration of DTD declarations * |
1056 | | * * |
1057 | | ****************************************************************/ |
1058 | | |
1059 | | /** |
1060 | | * Deallocate the memory used by an element definition |
1061 | | * |
1062 | | * @param elem an element |
1063 | | */ |
1064 | | static void |
1065 | 160 | xmlFreeElement(xmlElementPtr elem) { |
1066 | 160 | if (elem == NULL) return; |
1067 | 160 | xmlUnlinkNode((xmlNodePtr) elem); |
1068 | 160 | xmlFreeDocElementContent(elem->doc, elem->content); |
1069 | 160 | if (elem->name != NULL) |
1070 | 160 | xmlFree((xmlChar *) elem->name); |
1071 | 160 | if (elem->prefix != NULL) |
1072 | 2 | xmlFree((xmlChar *) elem->prefix); |
1073 | | #ifdef LIBXML_REGEXP_ENABLED |
1074 | | if (elem->contModel != NULL) |
1075 | | xmlRegFreeRegexp(elem->contModel); |
1076 | | #endif |
1077 | 160 | xmlFree(elem); |
1078 | 160 | } |
1079 | | |
1080 | | |
1081 | | /** |
1082 | | * Register a new element declaration. |
1083 | | * |
1084 | | * @deprecated Internal function, don't use. |
1085 | | * |
1086 | | * @param ctxt the validation context |
1087 | | * @param dtd pointer to the DTD |
1088 | | * @param name the entity name |
1089 | | * @param type the element type |
1090 | | * @param content the element content tree or NULL |
1091 | | * @returns the entity or NULL on error. |
1092 | | */ |
1093 | | xmlElement * |
1094 | | xmlAddElementDecl(xmlValidCtxt *ctxt, |
1095 | | xmlDtd *dtd, const xmlChar *name, |
1096 | | xmlElementTypeVal type, |
1097 | 2 | xmlElementContent *content) { |
1098 | 2 | xmlElementPtr ret; |
1099 | 2 | xmlElementTablePtr table; |
1100 | 2 | xmlAttributePtr oldAttributes = NULL; |
1101 | 2 | const xmlChar *localName; |
1102 | 2 | xmlChar *prefix = NULL; |
1103 | | |
1104 | 2 | if ((dtd == NULL) || (name == NULL)) |
1105 | 0 | return(NULL); |
1106 | | |
1107 | 2 | switch (type) { |
1108 | 0 | case XML_ELEMENT_TYPE_EMPTY: |
1109 | 0 | case XML_ELEMENT_TYPE_ANY: |
1110 | 0 | if (content != NULL) |
1111 | 0 | return(NULL); |
1112 | 0 | break; |
1113 | 0 | case XML_ELEMENT_TYPE_MIXED: |
1114 | 2 | case XML_ELEMENT_TYPE_ELEMENT: |
1115 | 2 | if (content == NULL) |
1116 | 0 | return(NULL); |
1117 | 2 | break; |
1118 | 2 | default: |
1119 | 0 | return(NULL); |
1120 | 2 | } |
1121 | | |
1122 | | /* |
1123 | | * check if name is a QName |
1124 | | */ |
1125 | 2 | localName = xmlSplitQName4(name, &prefix); |
1126 | 2 | if (localName == NULL) |
1127 | 0 | goto mem_error; |
1128 | | |
1129 | | /* |
1130 | | * Create the Element table if needed. |
1131 | | */ |
1132 | 2 | table = (xmlElementTablePtr) dtd->elements; |
1133 | 2 | if (table == NULL) { |
1134 | 1 | xmlDictPtr dict = NULL; |
1135 | | |
1136 | 1 | if (dtd->doc != NULL) |
1137 | 1 | dict = dtd->doc->dict; |
1138 | 1 | table = xmlHashCreateDict(0, dict); |
1139 | 1 | if (table == NULL) |
1140 | 0 | goto mem_error; |
1141 | 1 | dtd->elements = (void *) table; |
1142 | 1 | } |
1143 | | |
1144 | | /* |
1145 | | * lookup old attributes inserted on an undefined element in the |
1146 | | * internal subset. |
1147 | | */ |
1148 | 2 | if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) { |
1149 | 2 | ret = xmlHashLookup2(dtd->doc->intSubset->elements, localName, prefix); |
1150 | 2 | if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) { |
1151 | 0 | oldAttributes = ret->attributes; |
1152 | 0 | ret->attributes = NULL; |
1153 | 0 | xmlHashRemoveEntry2(dtd->doc->intSubset->elements, localName, prefix, |
1154 | 0 | NULL); |
1155 | 0 | xmlFreeElement(ret); |
1156 | 0 | } |
1157 | 2 | } |
1158 | | |
1159 | | /* |
1160 | | * The element may already be present if one of its attribute |
1161 | | * was registered first |
1162 | | */ |
1163 | 2 | ret = xmlHashLookup2(table, localName, prefix); |
1164 | 2 | if (ret != NULL) { |
1165 | 0 | if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) { |
1166 | | #ifdef LIBXML_VALID_ENABLED |
1167 | | /* |
1168 | | * The element is already defined in this DTD. |
1169 | | */ |
1170 | | if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_VALIDATE)) |
1171 | | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED, |
1172 | | "Redefinition of element %s\n", |
1173 | | name, NULL, NULL); |
1174 | | #endif /* LIBXML_VALID_ENABLED */ |
1175 | 0 | if (prefix != NULL) |
1176 | 0 | xmlFree(prefix); |
1177 | 0 | return(NULL); |
1178 | 0 | } |
1179 | 0 | if (prefix != NULL) { |
1180 | 0 | xmlFree(prefix); |
1181 | 0 | prefix = NULL; |
1182 | 0 | } |
1183 | 2 | } else { |
1184 | 2 | int res; |
1185 | | |
1186 | 2 | ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
1187 | 2 | if (ret == NULL) |
1188 | 0 | goto mem_error; |
1189 | 2 | memset(ret, 0, sizeof(xmlElement)); |
1190 | 2 | ret->type = XML_ELEMENT_DECL; |
1191 | | |
1192 | | /* |
1193 | | * fill the structure. |
1194 | | */ |
1195 | 2 | ret->name = xmlStrdup(localName); |
1196 | 2 | if (ret->name == NULL) { |
1197 | 0 | xmlFree(ret); |
1198 | 0 | goto mem_error; |
1199 | 0 | } |
1200 | 2 | ret->prefix = prefix; |
1201 | 2 | prefix = NULL; |
1202 | | |
1203 | | /* |
1204 | | * Validity Check: |
1205 | | * Insertion must not fail |
1206 | | */ |
1207 | 2 | res = xmlHashAdd2(table, localName, ret->prefix, ret); |
1208 | 2 | if (res <= 0) { |
1209 | 0 | xmlFreeElement(ret); |
1210 | 0 | goto mem_error; |
1211 | 0 | } |
1212 | | /* |
1213 | | * For new element, may have attributes from earlier |
1214 | | * definition in internal subset |
1215 | | */ |
1216 | 2 | ret->attributes = oldAttributes; |
1217 | 2 | } |
1218 | | |
1219 | | /* |
1220 | | * Finish to fill the structure. |
1221 | | */ |
1222 | 2 | ret->etype = type; |
1223 | | /* |
1224 | | * Avoid a stupid copy when called by the parser |
1225 | | * and flag it by setting a special parent value |
1226 | | * so the parser doesn't unallocate it. |
1227 | | */ |
1228 | 2 | if (content != NULL) { |
1229 | 2 | if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_USE_PCTXT)) { |
1230 | 2 | ret->content = content; |
1231 | 2 | content->parent = (xmlElementContentPtr) 1; |
1232 | 2 | } else if (content != NULL){ |
1233 | 0 | ret->content = xmlCopyDocElementContent(dtd->doc, content); |
1234 | 0 | if (ret->content == NULL) |
1235 | 0 | goto mem_error; |
1236 | 0 | } |
1237 | 2 | } |
1238 | | |
1239 | | /* |
1240 | | * Link it to the DTD |
1241 | | */ |
1242 | 2 | ret->parent = dtd; |
1243 | 2 | ret->doc = dtd->doc; |
1244 | 2 | if (dtd->last == NULL) { |
1245 | 1 | dtd->children = dtd->last = (xmlNodePtr) ret; |
1246 | 1 | } else { |
1247 | 1 | dtd->last->next = (xmlNodePtr) ret; |
1248 | 1 | ret->prev = dtd->last; |
1249 | 1 | dtd->last = (xmlNodePtr) ret; |
1250 | 1 | } |
1251 | 2 | if (prefix != NULL) |
1252 | 0 | xmlFree(prefix); |
1253 | 2 | return(ret); |
1254 | | |
1255 | 0 | mem_error: |
1256 | 0 | xmlVErrMemory(ctxt); |
1257 | 0 | if (prefix != NULL) |
1258 | 0 | xmlFree(prefix); |
1259 | 0 | return(NULL); |
1260 | 2 | } |
1261 | | |
1262 | | static void |
1263 | 160 | xmlFreeElementTableEntry(void *elem, const xmlChar *name ATTRIBUTE_UNUSED) { |
1264 | 160 | xmlFreeElement((xmlElementPtr) elem); |
1265 | 160 | } |
1266 | | |
1267 | | /** |
1268 | | * Deallocate the memory used by an element hash table. |
1269 | | * |
1270 | | * @deprecated Internal function, don't use. |
1271 | | * |
1272 | | * @param table An element table |
1273 | | */ |
1274 | | void |
1275 | 154 | xmlFreeElementTable(xmlElementTable *table) { |
1276 | 154 | xmlHashFree(table, xmlFreeElementTableEntry); |
1277 | 154 | } |
1278 | | |
1279 | | /** |
1280 | | * Build a copy of an element. |
1281 | | * |
1282 | | * @param payload an element |
1283 | | * @param name unused |
1284 | | * @returns the new xmlElement or NULL in case of error. |
1285 | | */ |
1286 | | static void * |
1287 | 0 | xmlCopyElement(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { |
1288 | 0 | xmlElementPtr elem = (xmlElementPtr) payload; |
1289 | 0 | xmlElementPtr cur; |
1290 | |
|
1291 | 0 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
1292 | 0 | if (cur == NULL) |
1293 | 0 | return(NULL); |
1294 | 0 | memset(cur, 0, sizeof(xmlElement)); |
1295 | 0 | cur->type = XML_ELEMENT_DECL; |
1296 | 0 | cur->etype = elem->etype; |
1297 | 0 | if (elem->name != NULL) { |
1298 | 0 | cur->name = xmlStrdup(elem->name); |
1299 | 0 | if (cur->name == NULL) |
1300 | 0 | goto error; |
1301 | 0 | } |
1302 | 0 | if (elem->prefix != NULL) { |
1303 | 0 | cur->prefix = xmlStrdup(elem->prefix); |
1304 | 0 | if (cur->prefix == NULL) |
1305 | 0 | goto error; |
1306 | 0 | } |
1307 | 0 | if (elem->content != NULL) { |
1308 | 0 | cur->content = xmlCopyElementContent(elem->content); |
1309 | 0 | if (cur->content == NULL) |
1310 | 0 | goto error; |
1311 | 0 | } |
1312 | | /* TODO : rebuild the attribute list on the copy */ |
1313 | 0 | cur->attributes = NULL; |
1314 | 0 | return(cur); |
1315 | | |
1316 | 0 | error: |
1317 | 0 | xmlFreeElement(cur); |
1318 | 0 | return(NULL); |
1319 | 0 | } |
1320 | | |
1321 | | /** |
1322 | | * Build a copy of an element table. |
1323 | | * |
1324 | | * @deprecated Internal function, don't use. |
1325 | | * |
1326 | | * @param table An element table |
1327 | | * @returns the new xmlElementTable or NULL in case of error. |
1328 | | */ |
1329 | | xmlElementTable * |
1330 | 0 | xmlCopyElementTable(xmlElementTable *table) { |
1331 | 0 | return(xmlHashCopySafe(table, xmlCopyElement, xmlFreeElementTableEntry)); |
1332 | 0 | } |
1333 | | |
1334 | | #ifdef LIBXML_OUTPUT_ENABLED |
1335 | | /** |
1336 | | * This will dump the content of the element declaration as an XML |
1337 | | * DTD definition. |
1338 | | * |
1339 | | * @deprecated Use #xmlSaveTree. |
1340 | | * |
1341 | | * @param buf the XML buffer output |
1342 | | * @param elem An element table |
1343 | | */ |
1344 | | void |
1345 | 0 | xmlDumpElementDecl(xmlBuffer *buf, xmlElement *elem) { |
1346 | 0 | xmlSaveCtxtPtr save; |
1347 | |
|
1348 | 0 | if ((buf == NULL) || (elem == NULL)) |
1349 | 0 | return; |
1350 | | |
1351 | 0 | save = xmlSaveToBuffer(buf, NULL, 0); |
1352 | 0 | xmlSaveTree(save, (xmlNodePtr) elem); |
1353 | 0 | if (xmlSaveFinish(save) != XML_ERR_OK) |
1354 | 0 | xmlFree(xmlBufferDetach(buf)); |
1355 | 0 | } |
1356 | | |
1357 | | /** |
1358 | | * This routine is used by the hash scan function. It just reverses |
1359 | | * the arguments. |
1360 | | * |
1361 | | * @param elem an element declaration |
1362 | | * @param save a save context |
1363 | | * @param name unused |
1364 | | */ |
1365 | | static void |
1366 | | xmlDumpElementDeclScan(void *elem, void *save, |
1367 | 0 | const xmlChar *name ATTRIBUTE_UNUSED) { |
1368 | 0 | xmlSaveTree(save, elem); |
1369 | 0 | } |
1370 | | |
1371 | | /** |
1372 | | * This will dump the content of the element table as an XML DTD |
1373 | | * definition. |
1374 | | * |
1375 | | * @deprecated Don't use. |
1376 | | * |
1377 | | * @param buf the XML buffer output |
1378 | | * @param table An element table |
1379 | | */ |
1380 | | void |
1381 | 0 | xmlDumpElementTable(xmlBuffer *buf, xmlElementTable *table) { |
1382 | 0 | xmlSaveCtxtPtr save; |
1383 | |
|
1384 | 0 | if ((buf == NULL) || (table == NULL)) |
1385 | 0 | return; |
1386 | | |
1387 | 0 | save = xmlSaveToBuffer(buf, NULL, 0); |
1388 | 0 | xmlHashScan(table, xmlDumpElementDeclScan, save); |
1389 | 0 | if (xmlSaveFinish(save) != XML_ERR_OK) |
1390 | 0 | xmlFree(xmlBufferDetach(buf)); |
1391 | 0 | } |
1392 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
1393 | | |
1394 | | /** |
1395 | | * Create and initialize an enumeration attribute node. |
1396 | | * |
1397 | | * @deprecated Internal function, don't use. |
1398 | | * |
1399 | | * @param name the enumeration name or NULL |
1400 | | * @returns the xmlEnumeration just created or NULL in case |
1401 | | * of error. |
1402 | | */ |
1403 | | xmlEnumeration * |
1404 | 0 | xmlCreateEnumeration(const xmlChar *name) { |
1405 | 0 | xmlEnumerationPtr ret; |
1406 | |
|
1407 | 0 | ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration)); |
1408 | 0 | if (ret == NULL) |
1409 | 0 | return(NULL); |
1410 | 0 | memset(ret, 0, sizeof(xmlEnumeration)); |
1411 | |
|
1412 | 0 | if (name != NULL) { |
1413 | 0 | ret->name = xmlStrdup(name); |
1414 | 0 | if (ret->name == NULL) { |
1415 | 0 | xmlFree(ret); |
1416 | 0 | return(NULL); |
1417 | 0 | } |
1418 | 0 | } |
1419 | | |
1420 | 0 | return(ret); |
1421 | 0 | } |
1422 | | |
1423 | | /** |
1424 | | * Free an enumeration attribute node (recursive). |
1425 | | * |
1426 | | * @param cur the tree to free. |
1427 | | */ |
1428 | | void |
1429 | 0 | xmlFreeEnumeration(xmlEnumeration *cur) { |
1430 | 0 | while (cur != NULL) { |
1431 | 0 | xmlEnumerationPtr next = cur->next; |
1432 | |
|
1433 | 0 | xmlFree((xmlChar *) cur->name); |
1434 | 0 | xmlFree(cur); |
1435 | |
|
1436 | 0 | cur = next; |
1437 | 0 | } |
1438 | 0 | } |
1439 | | |
1440 | | /** |
1441 | | * Copy an enumeration attribute node (recursive). |
1442 | | * |
1443 | | * @deprecated Internal function, don't use. |
1444 | | * |
1445 | | * @param cur the tree to copy. |
1446 | | * @returns the xmlEnumeration just created or NULL in case |
1447 | | * of error. |
1448 | | */ |
1449 | | xmlEnumeration * |
1450 | 0 | xmlCopyEnumeration(xmlEnumeration *cur) { |
1451 | 0 | xmlEnumerationPtr ret = NULL; |
1452 | 0 | xmlEnumerationPtr last = NULL; |
1453 | |
|
1454 | 0 | while (cur != NULL) { |
1455 | 0 | xmlEnumerationPtr copy = xmlCreateEnumeration(cur->name); |
1456 | |
|
1457 | 0 | if (copy == NULL) { |
1458 | 0 | xmlFreeEnumeration(ret); |
1459 | 0 | return(NULL); |
1460 | 0 | } |
1461 | | |
1462 | 0 | if (ret == NULL) { |
1463 | 0 | ret = last = copy; |
1464 | 0 | } else { |
1465 | 0 | last->next = copy; |
1466 | 0 | last = copy; |
1467 | 0 | } |
1468 | |
|
1469 | 0 | cur = cur->next; |
1470 | 0 | } |
1471 | | |
1472 | 0 | return(ret); |
1473 | 0 | } |
1474 | | |
1475 | | #ifdef LIBXML_VALID_ENABLED |
1476 | | /** |
1477 | | * Verify that the element doesn't have too many ID attributes |
1478 | | * declared. |
1479 | | * |
1480 | | * @param ctxt the validation context |
1481 | | * @param elem the element name |
1482 | | * @param err whether to raise errors here |
1483 | | * @returns the number of ID attributes found. |
1484 | | */ |
1485 | | static int |
1486 | | xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) { |
1487 | | xmlAttributePtr cur; |
1488 | | int ret = 0; |
1489 | | |
1490 | | if (elem == NULL) return(0); |
1491 | | cur = elem->attributes; |
1492 | | while (cur != NULL) { |
1493 | | if (cur->atype == XML_ATTRIBUTE_ID) { |
1494 | | ret ++; |
1495 | | if ((ret > 1) && (err)) |
1496 | | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID, |
1497 | | "Element %s has too many ID attributes defined : %s\n", |
1498 | | elem->name, cur->name, NULL); |
1499 | | } |
1500 | | cur = cur->nexth; |
1501 | | } |
1502 | | return(ret); |
1503 | | } |
1504 | | #endif /* LIBXML_VALID_ENABLED */ |
1505 | | |
1506 | | /** |
1507 | | * Deallocate the memory used by an attribute definition. |
1508 | | * |
1509 | | * @param attr an attribute |
1510 | | */ |
1511 | | static void |
1512 | 3.03k | xmlFreeAttribute(xmlAttributePtr attr) { |
1513 | 3.03k | xmlDictPtr dict; |
1514 | | |
1515 | 3.03k | if (attr == NULL) return; |
1516 | 3.03k | if (attr->doc != NULL) |
1517 | 3.03k | dict = attr->doc->dict; |
1518 | 0 | else |
1519 | 0 | dict = NULL; |
1520 | 3.03k | xmlUnlinkNode((xmlNodePtr) attr); |
1521 | 3.03k | if (attr->tree != NULL) |
1522 | 0 | xmlFreeEnumeration(attr->tree); |
1523 | 3.03k | if (dict) { |
1524 | 3.03k | if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem))) |
1525 | 0 | xmlFree((xmlChar *) attr->elem); |
1526 | 3.03k | if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name))) |
1527 | 0 | xmlFree((xmlChar *) attr->name); |
1528 | 3.03k | if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix))) |
1529 | 0 | xmlFree((xmlChar *) attr->prefix); |
1530 | 3.03k | if ((attr->defaultValue != NULL) && |
1531 | 3.03k | (!xmlDictOwns(dict, attr->defaultValue))) |
1532 | 0 | xmlFree((xmlChar *) attr->defaultValue); |
1533 | 3.03k | } else { |
1534 | 0 | if (attr->elem != NULL) |
1535 | 0 | xmlFree((xmlChar *) attr->elem); |
1536 | 0 | if (attr->name != NULL) |
1537 | 0 | xmlFree((xmlChar *) attr->name); |
1538 | 0 | if (attr->defaultValue != NULL) |
1539 | 0 | xmlFree((xmlChar *) attr->defaultValue); |
1540 | 0 | if (attr->prefix != NULL) |
1541 | 0 | xmlFree((xmlChar *) attr->prefix); |
1542 | 0 | } |
1543 | 3.03k | xmlFree(attr); |
1544 | 3.03k | } |
1545 | | |
1546 | | |
1547 | | /** |
1548 | | * Register a new attribute declaration. |
1549 | | * |
1550 | | * @deprecated Internal function, don't use. |
1551 | | * |
1552 | | * @param ctxt the validation context |
1553 | | * @param dtd pointer to the DTD |
1554 | | * @param elem the element name |
1555 | | * @param name the attribute name |
1556 | | * @param ns the attribute namespace prefix |
1557 | | * @param type the attribute type |
1558 | | * @param def the attribute default type |
1559 | | * @param defaultValue the attribute default value |
1560 | | * @param tree if it's an enumeration, the associated list |
1561 | | * @returns the attribute decl or NULL on error. |
1562 | | */ |
1563 | | xmlAttribute * |
1564 | | xmlAddAttributeDecl(xmlValidCtxt *ctxt, |
1565 | | xmlDtd *dtd, const xmlChar *elem, |
1566 | | const xmlChar *name, const xmlChar *ns, |
1567 | | xmlAttributeType type, xmlAttributeDefault def, |
1568 | 3.03k | const xmlChar *defaultValue, xmlEnumeration *tree) { |
1569 | 3.03k | xmlAttributePtr ret = NULL; |
1570 | 3.03k | xmlAttributeTablePtr table; |
1571 | 3.03k | xmlElementPtr elemDef; |
1572 | 3.03k | xmlDictPtr dict = NULL; |
1573 | 3.03k | int res; |
1574 | | |
1575 | 3.03k | if (dtd == NULL) { |
1576 | 0 | xmlFreeEnumeration(tree); |
1577 | 0 | return(NULL); |
1578 | 0 | } |
1579 | 3.03k | if (name == NULL) { |
1580 | 0 | xmlFreeEnumeration(tree); |
1581 | 0 | return(NULL); |
1582 | 0 | } |
1583 | 3.03k | if (elem == NULL) { |
1584 | 0 | xmlFreeEnumeration(tree); |
1585 | 0 | return(NULL); |
1586 | 0 | } |
1587 | 3.03k | if (dtd->doc != NULL) |
1588 | 3.03k | dict = dtd->doc->dict; |
1589 | | |
1590 | | #ifdef LIBXML_VALID_ENABLED |
1591 | | if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_VALIDATE) && |
1592 | | (defaultValue != NULL) && |
1593 | | (!xmlValidateAttributeValueInternal(dtd->doc, type, defaultValue))) { |
1594 | | xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT, |
1595 | | "Attribute %s of %s: invalid default value\n", |
1596 | | elem, name, defaultValue); |
1597 | | defaultValue = NULL; |
1598 | | if (ctxt != NULL) |
1599 | | ctxt->valid = 0; |
1600 | | } |
1601 | | #endif /* LIBXML_VALID_ENABLED */ |
1602 | | |
1603 | | /* |
1604 | | * Check first that an attribute defined in the external subset wasn't |
1605 | | * already defined in the internal subset |
1606 | | */ |
1607 | 3.03k | if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) && |
1608 | 3.03k | (dtd->doc->intSubset != NULL) && |
1609 | 3.03k | (dtd->doc->intSubset->attributes != NULL)) { |
1610 | 0 | ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem); |
1611 | 0 | if (ret != NULL) { |
1612 | 0 | xmlFreeEnumeration(tree); |
1613 | 0 | return(NULL); |
1614 | 0 | } |
1615 | 0 | } |
1616 | | |
1617 | | /* |
1618 | | * Create the Attribute table if needed. |
1619 | | */ |
1620 | 3.03k | table = (xmlAttributeTablePtr) dtd->attributes; |
1621 | 3.03k | if (table == NULL) { |
1622 | 154 | table = xmlHashCreateDict(0, dict); |
1623 | 154 | dtd->attributes = (void *) table; |
1624 | 154 | } |
1625 | 3.03k | if (table == NULL) |
1626 | 0 | goto mem_error; |
1627 | | |
1628 | 3.03k | ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
1629 | 3.03k | if (ret == NULL) |
1630 | 0 | goto mem_error; |
1631 | 3.03k | memset(ret, 0, sizeof(xmlAttribute)); |
1632 | 3.03k | ret->type = XML_ATTRIBUTE_DECL; |
1633 | | |
1634 | | /* |
1635 | | * fill the structure. |
1636 | | */ |
1637 | 3.03k | ret->atype = type; |
1638 | | /* |
1639 | | * doc must be set before possible error causes call |
1640 | | * to xmlFreeAttribute (because it's used to check on |
1641 | | * dict use) |
1642 | | */ |
1643 | 3.03k | ret->doc = dtd->doc; |
1644 | 3.03k | if (dict) { |
1645 | 3.03k | ret->name = xmlDictLookup(dict, name, -1); |
1646 | 3.03k | ret->elem = xmlDictLookup(dict, elem, -1); |
1647 | 3.03k | } else { |
1648 | 0 | ret->name = xmlStrdup(name); |
1649 | 0 | ret->elem = xmlStrdup(elem); |
1650 | 0 | } |
1651 | 3.03k | if ((ret->name == NULL) || (ret->elem == NULL)) |
1652 | 0 | goto mem_error; |
1653 | 3.03k | if (ns != NULL) { |
1654 | 1.01k | if (dict) |
1655 | 1.01k | ret->prefix = xmlDictLookup(dict, ns, -1); |
1656 | 0 | else |
1657 | 0 | ret->prefix = xmlStrdup(ns); |
1658 | 1.01k | if (ret->prefix == NULL) |
1659 | 0 | goto mem_error; |
1660 | 1.01k | } |
1661 | 3.03k | ret->def = def; |
1662 | 3.03k | ret->tree = tree; |
1663 | 3.03k | tree = NULL; |
1664 | 3.03k | if (defaultValue != NULL) { |
1665 | 1.58k | if (dict) |
1666 | 1.58k | ret->defaultValue = xmlDictLookup(dict, defaultValue, -1); |
1667 | 0 | else |
1668 | 0 | ret->defaultValue = xmlStrdup(defaultValue); |
1669 | 1.58k | if (ret->defaultValue == NULL) |
1670 | 0 | goto mem_error; |
1671 | 1.58k | } |
1672 | | |
1673 | 3.03k | elemDef = xmlGetDtdElementDesc2(ctxt, dtd, elem); |
1674 | 3.03k | if (elemDef == NULL) |
1675 | 0 | goto mem_error; |
1676 | | |
1677 | | /* |
1678 | | * Validity Check: |
1679 | | * Search the DTD for previous declarations of the ATTLIST |
1680 | | */ |
1681 | 3.03k | res = xmlHashAdd3(table, ret->name, ret->prefix, ret->elem, ret); |
1682 | 3.03k | if (res <= 0) { |
1683 | 2.68k | if (res < 0) |
1684 | 0 | goto mem_error; |
1685 | | #ifdef LIBXML_VALID_ENABLED |
1686 | | /* |
1687 | | * The attribute is already defined in this DTD. |
1688 | | */ |
1689 | | if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_VALIDATE)) |
1690 | | xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, |
1691 | | XML_DTD_ATTRIBUTE_REDEFINED, |
1692 | | "Attribute %s of element %s: already defined\n", |
1693 | | name, elem, NULL); |
1694 | | #endif /* LIBXML_VALID_ENABLED */ |
1695 | 2.68k | xmlFreeAttribute(ret); |
1696 | 2.68k | return(NULL); |
1697 | 2.68k | } |
1698 | | |
1699 | | /* |
1700 | | * Insert namespace default def first they need to be |
1701 | | * processed first. |
1702 | | */ |
1703 | 349 | if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) || |
1704 | 349 | ((ret->prefix != NULL && |
1705 | 348 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) { |
1706 | 2 | ret->nexth = elemDef->attributes; |
1707 | 2 | elemDef->attributes = ret; |
1708 | 347 | } else { |
1709 | 347 | xmlAttributePtr tmp = elemDef->attributes; |
1710 | | |
1711 | 350 | while ((tmp != NULL) && |
1712 | 350 | ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) || |
1713 | 193 | ((ret->prefix != NULL && |
1714 | 189 | (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) { |
1715 | 4 | if (tmp->nexth == NULL) |
1716 | 1 | break; |
1717 | 3 | tmp = tmp->nexth; |
1718 | 3 | } |
1719 | 347 | if (tmp != NULL) { |
1720 | 190 | ret->nexth = tmp->nexth; |
1721 | 190 | tmp->nexth = ret; |
1722 | 190 | } else { |
1723 | 157 | ret->nexth = elemDef->attributes; |
1724 | 157 | elemDef->attributes = ret; |
1725 | 157 | } |
1726 | 347 | } |
1727 | | |
1728 | | /* |
1729 | | * Link it to the DTD |
1730 | | */ |
1731 | 349 | ret->parent = dtd; |
1732 | 349 | if (dtd->last == NULL) { |
1733 | 151 | dtd->children = dtd->last = (xmlNodePtr) ret; |
1734 | 198 | } else { |
1735 | 198 | dtd->last->next = (xmlNodePtr) ret; |
1736 | 198 | ret->prev = dtd->last; |
1737 | 198 | dtd->last = (xmlNodePtr) ret; |
1738 | 198 | } |
1739 | 349 | return(ret); |
1740 | | |
1741 | 0 | mem_error: |
1742 | 0 | xmlVErrMemory(ctxt); |
1743 | 0 | xmlFreeEnumeration(tree); |
1744 | 0 | xmlFreeAttribute(ret); |
1745 | 0 | return(NULL); |
1746 | 3.03k | } |
1747 | | |
1748 | | static void |
1749 | 349 | xmlFreeAttributeTableEntry(void *attr, const xmlChar *name ATTRIBUTE_UNUSED) { |
1750 | 349 | xmlFreeAttribute((xmlAttributePtr) attr); |
1751 | 349 | } |
1752 | | |
1753 | | /** |
1754 | | * Deallocate the memory used by an entities hash table. |
1755 | | * |
1756 | | * @deprecated Internal function, don't use. |
1757 | | * |
1758 | | * @param table An attribute table |
1759 | | */ |
1760 | | void |
1761 | 154 | xmlFreeAttributeTable(xmlAttributeTable *table) { |
1762 | 154 | xmlHashFree(table, xmlFreeAttributeTableEntry); |
1763 | 154 | } |
1764 | | |
1765 | | /** |
1766 | | * Build a copy of an attribute declaration. |
1767 | | * |
1768 | | * @param payload an attribute declaration |
1769 | | * @param name unused |
1770 | | * @returns the new xmlAttribute or NULL in case of error. |
1771 | | */ |
1772 | | static void * |
1773 | 0 | xmlCopyAttribute(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { |
1774 | 0 | xmlAttributePtr attr = (xmlAttributePtr) payload; |
1775 | 0 | xmlAttributePtr cur; |
1776 | |
|
1777 | 0 | cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute)); |
1778 | 0 | if (cur == NULL) |
1779 | 0 | return(NULL); |
1780 | 0 | memset(cur, 0, sizeof(xmlAttribute)); |
1781 | 0 | cur->type = XML_ATTRIBUTE_DECL; |
1782 | 0 | cur->atype = attr->atype; |
1783 | 0 | cur->def = attr->def; |
1784 | 0 | if (attr->tree != NULL) { |
1785 | 0 | cur->tree = xmlCopyEnumeration(attr->tree); |
1786 | 0 | if (cur->tree == NULL) |
1787 | 0 | goto error; |
1788 | 0 | } |
1789 | 0 | if (attr->elem != NULL) { |
1790 | 0 | cur->elem = xmlStrdup(attr->elem); |
1791 | 0 | if (cur->elem == NULL) |
1792 | 0 | goto error; |
1793 | 0 | } |
1794 | 0 | if (attr->name != NULL) { |
1795 | 0 | cur->name = xmlStrdup(attr->name); |
1796 | 0 | if (cur->name == NULL) |
1797 | 0 | goto error; |
1798 | 0 | } |
1799 | 0 | if (attr->prefix != NULL) { |
1800 | 0 | cur->prefix = xmlStrdup(attr->prefix); |
1801 | 0 | if (cur->prefix == NULL) |
1802 | 0 | goto error; |
1803 | 0 | } |
1804 | 0 | if (attr->defaultValue != NULL) { |
1805 | 0 | cur->defaultValue = xmlStrdup(attr->defaultValue); |
1806 | 0 | if (cur->defaultValue == NULL) |
1807 | 0 | goto error; |
1808 | 0 | } |
1809 | 0 | return(cur); |
1810 | | |
1811 | 0 | error: |
1812 | 0 | xmlFreeAttribute(cur); |
1813 | 0 | return(NULL); |
1814 | 0 | } |
1815 | | |
1816 | | /** |
1817 | | * Build a copy of an attribute table. |
1818 | | * |
1819 | | * @deprecated Internal function, don't use. |
1820 | | * |
1821 | | * @param table An attribute table |
1822 | | * @returns the new xmlAttributeTable or NULL in case of error. |
1823 | | */ |
1824 | | xmlAttributeTable * |
1825 | 0 | xmlCopyAttributeTable(xmlAttributeTable *table) { |
1826 | 0 | return(xmlHashCopySafe(table, xmlCopyAttribute, |
1827 | 0 | xmlFreeAttributeTableEntry)); |
1828 | 0 | } |
1829 | | |
1830 | | #ifdef LIBXML_OUTPUT_ENABLED |
1831 | | /** |
1832 | | * This will dump the content of the attribute declaration as an XML |
1833 | | * DTD definition. |
1834 | | * |
1835 | | * @deprecated Use #xmlSaveTree. |
1836 | | * |
1837 | | * @param buf the XML buffer output |
1838 | | * @param attr An attribute declaration |
1839 | | */ |
1840 | | void |
1841 | 0 | xmlDumpAttributeDecl(xmlBuffer *buf, xmlAttribute *attr) { |
1842 | 0 | xmlSaveCtxtPtr save; |
1843 | |
|
1844 | 0 | if ((buf == NULL) || (attr == NULL)) |
1845 | 0 | return; |
1846 | | |
1847 | 0 | save = xmlSaveToBuffer(buf, NULL, 0); |
1848 | 0 | xmlSaveTree(save, (xmlNodePtr) attr); |
1849 | 0 | if (xmlSaveFinish(save) != XML_ERR_OK) |
1850 | 0 | xmlFree(xmlBufferDetach(buf)); |
1851 | 0 | } |
1852 | | |
1853 | | /** |
1854 | | * This is used with the hash scan function - just reverses |
1855 | | * arguments. |
1856 | | * |
1857 | | * @param attr an attribute declaration |
1858 | | * @param save a save context |
1859 | | * @param name unused |
1860 | | */ |
1861 | | static void |
1862 | | xmlDumpAttributeDeclScan(void *attr, void *save, |
1863 | 0 | const xmlChar *name ATTRIBUTE_UNUSED) { |
1864 | 0 | xmlSaveTree(save, attr); |
1865 | 0 | } |
1866 | | |
1867 | | /** |
1868 | | * This will dump the content of the attribute table as an XML |
1869 | | * DTD definition. |
1870 | | * |
1871 | | * @deprecated Don't use. |
1872 | | * |
1873 | | * @param buf the XML buffer output |
1874 | | * @param table an attribute table |
1875 | | */ |
1876 | | void |
1877 | 0 | xmlDumpAttributeTable(xmlBuffer *buf, xmlAttributeTable *table) { |
1878 | 0 | xmlSaveCtxtPtr save; |
1879 | |
|
1880 | 0 | if ((buf == NULL) || (table == NULL)) |
1881 | 0 | return; |
1882 | | |
1883 | 0 | save = xmlSaveToBuffer(buf, NULL, 0); |
1884 | 0 | xmlHashScan(table, xmlDumpAttributeDeclScan, save); |
1885 | 0 | if (xmlSaveFinish(save) != XML_ERR_OK) |
1886 | 0 | xmlFree(xmlBufferDetach(buf)); |
1887 | 0 | } |
1888 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
1889 | | |
1890 | | /************************************************************************ |
1891 | | * * |
1892 | | * NOTATIONs * |
1893 | | * * |
1894 | | ************************************************************************/ |
1895 | | /** |
1896 | | * Deallocate the memory used by an notation definition. |
1897 | | * |
1898 | | * @param nota a notation |
1899 | | */ |
1900 | | static void |
1901 | 1.08k | xmlFreeNotation(xmlNotationPtr nota) { |
1902 | 1.08k | if (nota == NULL) return; |
1903 | 1.08k | if (nota->name != NULL) |
1904 | 1.08k | xmlFree((xmlChar *) nota->name); |
1905 | 1.08k | if (nota->PublicID != NULL) |
1906 | 409 | xmlFree((xmlChar *) nota->PublicID); |
1907 | 1.08k | if (nota->SystemID != NULL) |
1908 | 736 | xmlFree((xmlChar *) nota->SystemID); |
1909 | 1.08k | xmlFree(nota); |
1910 | 1.08k | } |
1911 | | |
1912 | | |
1913 | | /** |
1914 | | * Register a new notation declaration. |
1915 | | * |
1916 | | * @deprecated Internal function, don't use. |
1917 | | * |
1918 | | * @param dtd pointer to the DTD |
1919 | | * @param ctxt the validation context |
1920 | | * @param name the entity name |
1921 | | * @param publicId the public identifier or NULL |
1922 | | * @param systemId the system identifier or NULL |
1923 | | * @returns the notation or NULL on error. |
1924 | | */ |
1925 | | xmlNotation * |
1926 | | xmlAddNotationDecl(xmlValidCtxt *ctxt, xmlDtd *dtd, const xmlChar *name, |
1927 | 1.08k | const xmlChar *publicId, const xmlChar *systemId) { |
1928 | 1.08k | xmlNotationPtr ret = NULL; |
1929 | 1.08k | xmlNotationTablePtr table; |
1930 | 1.08k | int res; |
1931 | | |
1932 | 1.08k | if (dtd == NULL) { |
1933 | 0 | return(NULL); |
1934 | 0 | } |
1935 | 1.08k | if (name == NULL) { |
1936 | 0 | return(NULL); |
1937 | 0 | } |
1938 | 1.08k | if ((publicId == NULL) && (systemId == NULL)) { |
1939 | 0 | return(NULL); |
1940 | 0 | } |
1941 | | |
1942 | | /* |
1943 | | * Create the Notation table if needed. |
1944 | | */ |
1945 | 1.08k | table = (xmlNotationTablePtr) dtd->notations; |
1946 | 1.08k | if (table == NULL) { |
1947 | 1 | xmlDictPtr dict = NULL; |
1948 | 1 | if (dtd->doc != NULL) |
1949 | 1 | dict = dtd->doc->dict; |
1950 | | |
1951 | 1 | dtd->notations = table = xmlHashCreateDict(0, dict); |
1952 | 1 | if (table == NULL) |
1953 | 0 | goto mem_error; |
1954 | 1 | } |
1955 | | |
1956 | 1.08k | ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
1957 | 1.08k | if (ret == NULL) |
1958 | 0 | goto mem_error; |
1959 | 1.08k | memset(ret, 0, sizeof(xmlNotation)); |
1960 | | |
1961 | | /* |
1962 | | * fill the structure. |
1963 | | */ |
1964 | 1.08k | ret->name = xmlStrdup(name); |
1965 | 1.08k | if (ret->name == NULL) |
1966 | 0 | goto mem_error; |
1967 | 1.08k | if (systemId != NULL) { |
1968 | 736 | ret->SystemID = xmlStrdup(systemId); |
1969 | 736 | if (ret->SystemID == NULL) |
1970 | 0 | goto mem_error; |
1971 | 736 | } |
1972 | 1.08k | if (publicId != NULL) { |
1973 | 409 | ret->PublicID = xmlStrdup(publicId); |
1974 | 409 | if (ret->PublicID == NULL) |
1975 | 0 | goto mem_error; |
1976 | 409 | } |
1977 | | |
1978 | | /* |
1979 | | * Validity Check: |
1980 | | * Check the DTD for previous declarations of the ATTLIST |
1981 | | */ |
1982 | 1.08k | res = xmlHashAdd(table, name, ret); |
1983 | 1.08k | if (res <= 0) { |
1984 | 1.06k | if (res < 0) |
1985 | 0 | goto mem_error; |
1986 | | #ifdef LIBXML_VALID_ENABLED |
1987 | | if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_VALIDATE)) |
1988 | | xmlErrValid(ctxt, XML_DTD_NOTATION_REDEFINED, |
1989 | | "xmlAddNotationDecl: %s already defined\n", |
1990 | | (const char *) name); |
1991 | | #endif /* LIBXML_VALID_ENABLED */ |
1992 | 1.06k | xmlFreeNotation(ret); |
1993 | 1.06k | return(NULL); |
1994 | 1.06k | } |
1995 | 24 | return(ret); |
1996 | | |
1997 | 0 | mem_error: |
1998 | 0 | xmlVErrMemory(ctxt); |
1999 | 0 | xmlFreeNotation(ret); |
2000 | 0 | return(NULL); |
2001 | 1.08k | } |
2002 | | |
2003 | | static void |
2004 | 24 | xmlFreeNotationTableEntry(void *nota, const xmlChar *name ATTRIBUTE_UNUSED) { |
2005 | 24 | xmlFreeNotation((xmlNotationPtr) nota); |
2006 | 24 | } |
2007 | | |
2008 | | /** |
2009 | | * Deallocate the memory used by an entities hash table. |
2010 | | * |
2011 | | * @deprecated Internal function, don't use. |
2012 | | * |
2013 | | * @param table An notation table |
2014 | | */ |
2015 | | void |
2016 | 1 | xmlFreeNotationTable(xmlNotationTable *table) { |
2017 | 1 | xmlHashFree(table, xmlFreeNotationTableEntry); |
2018 | 1 | } |
2019 | | |
2020 | | /** |
2021 | | * Build a copy of a notation. |
2022 | | * |
2023 | | * @param payload a notation |
2024 | | * @param name unused |
2025 | | * @returns the new xmlNotation or NULL in case of error. |
2026 | | */ |
2027 | | static void * |
2028 | 0 | xmlCopyNotation(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { |
2029 | 0 | xmlNotationPtr nota = (xmlNotationPtr) payload; |
2030 | 0 | xmlNotationPtr cur; |
2031 | |
|
2032 | 0 | cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation)); |
2033 | 0 | if (cur == NULL) |
2034 | 0 | return(NULL); |
2035 | 0 | memset(cur, 0, sizeof(*cur)); |
2036 | 0 | if (nota->name != NULL) { |
2037 | 0 | cur->name = xmlStrdup(nota->name); |
2038 | 0 | if (cur->name == NULL) |
2039 | 0 | goto error; |
2040 | 0 | } |
2041 | 0 | if (nota->PublicID != NULL) { |
2042 | 0 | cur->PublicID = xmlStrdup(nota->PublicID); |
2043 | 0 | if (cur->PublicID == NULL) |
2044 | 0 | goto error; |
2045 | 0 | } |
2046 | 0 | if (nota->SystemID != NULL) { |
2047 | 0 | cur->SystemID = xmlStrdup(nota->SystemID); |
2048 | 0 | if (cur->SystemID == NULL) |
2049 | 0 | goto error; |
2050 | 0 | } |
2051 | 0 | return(cur); |
2052 | | |
2053 | 0 | error: |
2054 | 0 | xmlFreeNotation(cur); |
2055 | 0 | return(NULL); |
2056 | 0 | } |
2057 | | |
2058 | | /** |
2059 | | * Build a copy of a notation table. |
2060 | | * |
2061 | | * @deprecated Internal function, don't use. |
2062 | | * |
2063 | | * @param table A notation table |
2064 | | * @returns the new xmlNotationTable or NULL in case of error. |
2065 | | */ |
2066 | | xmlNotationTable * |
2067 | 0 | xmlCopyNotationTable(xmlNotationTable *table) { |
2068 | 0 | return(xmlHashCopySafe(table, xmlCopyNotation, xmlFreeNotationTableEntry)); |
2069 | 0 | } |
2070 | | |
2071 | | #ifdef LIBXML_OUTPUT_ENABLED |
2072 | | /** |
2073 | | * This will dump the content the notation declaration as an XML |
2074 | | * DTD definition. |
2075 | | * |
2076 | | * @deprecated Don't use. |
2077 | | * |
2078 | | * @param buf the XML buffer output |
2079 | | * @param nota A notation declaration |
2080 | | */ |
2081 | | void |
2082 | 0 | xmlDumpNotationDecl(xmlBuffer *buf, xmlNotation *nota) { |
2083 | 0 | xmlSaveCtxtPtr save; |
2084 | |
|
2085 | 0 | if ((buf == NULL) || (nota == NULL)) |
2086 | 0 | return; |
2087 | | |
2088 | 0 | save = xmlSaveToBuffer(buf, NULL, 0); |
2089 | 0 | xmlSaveNotationDecl(save, nota); |
2090 | 0 | if (xmlSaveFinish(save) != XML_ERR_OK) |
2091 | 0 | xmlFree(xmlBufferDetach(buf)); |
2092 | 0 | } |
2093 | | |
2094 | | /** |
2095 | | * This will dump the content of the notation table as an XML |
2096 | | * DTD definition. |
2097 | | * |
2098 | | * @deprecated Don't use. |
2099 | | * |
2100 | | * @param buf the XML buffer output |
2101 | | * @param table A notation table |
2102 | | */ |
2103 | | void |
2104 | 0 | xmlDumpNotationTable(xmlBuffer *buf, xmlNotationTable *table) { |
2105 | 0 | xmlSaveCtxtPtr save; |
2106 | |
|
2107 | 0 | if ((buf == NULL) || (table == NULL)) |
2108 | 0 | return; |
2109 | | |
2110 | 0 | save = xmlSaveToBuffer(buf, NULL, 0); |
2111 | 0 | xmlSaveNotationTable(save, table); |
2112 | 0 | if (xmlSaveFinish(save) != XML_ERR_OK) |
2113 | 0 | xmlFree(xmlBufferDetach(buf)); |
2114 | 0 | } |
2115 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
2116 | | |
2117 | | /************************************************************************ |
2118 | | * * |
2119 | | * IDs * |
2120 | | * * |
2121 | | ************************************************************************/ |
2122 | | |
2123 | | /** |
2124 | | * Free a string if it is not owned by the dictionary in the |
2125 | | * current scope |
2126 | | * |
2127 | | * @param str a string |
2128 | | */ |
2129 | | #define DICT_FREE(str) \ |
2130 | 296 | if ((str) && ((!dict) || \ |
2131 | 296 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
2132 | 296 | xmlFree((char *)(str)); |
2133 | | |
2134 | | static int |
2135 | 0 | xmlIsStreaming(xmlValidCtxtPtr ctxt) { |
2136 | 0 | xmlParserCtxtPtr pctxt; |
2137 | |
|
2138 | 0 | if (ctxt == NULL) |
2139 | 0 | return(0); |
2140 | 0 | if ((ctxt->flags & XML_VCTXT_USE_PCTXT) == 0) |
2141 | 0 | return(0); |
2142 | 0 | pctxt = ctxt->userData; |
2143 | 0 | return(pctxt->parseMode == XML_PARSE_READER); |
2144 | 0 | } |
2145 | | |
2146 | | /** |
2147 | | * Deallocate the memory used by an id definition. |
2148 | | * |
2149 | | * @param id an id |
2150 | | */ |
2151 | | static void |
2152 | 296 | xmlFreeID(xmlIDPtr id) { |
2153 | 296 | xmlDictPtr dict = NULL; |
2154 | | |
2155 | 296 | if (id == NULL) return; |
2156 | | |
2157 | 296 | if (id->doc != NULL) |
2158 | 296 | dict = id->doc->dict; |
2159 | | |
2160 | 296 | if (id->value != NULL) |
2161 | 296 | DICT_FREE(id->value) |
2162 | 296 | if (id->name != NULL) |
2163 | 0 | DICT_FREE(id->name) |
2164 | 296 | if (id->attr != NULL) { |
2165 | 296 | id->attr->id = NULL; |
2166 | 296 | id->attr->atype = 0; |
2167 | 296 | } |
2168 | | |
2169 | 296 | xmlFree(id); |
2170 | 296 | } |
2171 | | |
2172 | | |
2173 | | /** |
2174 | | * Add an attribute to the docment's ID table. |
2175 | | * |
2176 | | * @param attr the attribute holding the ID |
2177 | | * @param value the attribute (ID) value |
2178 | | * @param idPtr pointer to resulting ID |
2179 | | * @returns 1 on success, 0 if the ID already exists, -1 if a memory |
2180 | | * allocation fails. |
2181 | | */ |
2182 | | static int |
2183 | 7.50k | xmlAddIDInternal(xmlAttrPtr attr, const xmlChar *value, xmlIDPtr *idPtr) { |
2184 | 7.50k | xmlDocPtr doc; |
2185 | 7.50k | xmlIDPtr id; |
2186 | 7.50k | xmlIDTablePtr table; |
2187 | 7.50k | int ret; |
2188 | | |
2189 | 7.50k | if (idPtr != NULL) |
2190 | 7.50k | *idPtr = NULL; |
2191 | 7.50k | if ((value == NULL) || (value[0] == 0)) |
2192 | 0 | return(0); |
2193 | 7.50k | if (attr == NULL) |
2194 | 0 | return(0); |
2195 | | |
2196 | 7.50k | doc = attr->doc; |
2197 | 7.50k | if (doc == NULL) |
2198 | 0 | return(0); |
2199 | | |
2200 | | /* |
2201 | | * Create the ID table if needed. |
2202 | | */ |
2203 | 7.50k | table = (xmlIDTablePtr) doc->ids; |
2204 | 7.50k | if (table == NULL) { |
2205 | 47 | doc->ids = table = xmlHashCreateDict(0, doc->dict); |
2206 | 47 | if (table == NULL) |
2207 | 0 | return(-1); |
2208 | 7.45k | } else { |
2209 | 7.45k | id = xmlHashLookup(table, value); |
2210 | 7.45k | if (id != NULL) |
2211 | 7.20k | return(0); |
2212 | 7.45k | } |
2213 | | |
2214 | 296 | id = (xmlIDPtr) xmlMalloc(sizeof(xmlID)); |
2215 | 296 | if (id == NULL) |
2216 | 0 | return(-1); |
2217 | 296 | memset(id, 0, sizeof(*id)); |
2218 | | |
2219 | | /* |
2220 | | * fill the structure. |
2221 | | */ |
2222 | 296 | id->doc = doc; |
2223 | 296 | id->value = xmlStrdup(value); |
2224 | 296 | if (id->value == NULL) { |
2225 | 0 | xmlFreeID(id); |
2226 | 0 | return(-1); |
2227 | 0 | } |
2228 | | |
2229 | 296 | if (attr->id != NULL) |
2230 | 0 | xmlRemoveID(doc, attr); |
2231 | | |
2232 | 296 | if (xmlHashAddEntry(table, value, id) < 0) { |
2233 | 0 | xmlFreeID(id); |
2234 | 0 | return(-1); |
2235 | 0 | } |
2236 | | |
2237 | 296 | ret = 1; |
2238 | 296 | if (idPtr != NULL) |
2239 | 296 | *idPtr = id; |
2240 | | |
2241 | 296 | id->attr = attr; |
2242 | 296 | id->lineno = xmlGetLineNo(attr->parent); |
2243 | 296 | attr->atype = XML_ATTRIBUTE_ID; |
2244 | 296 | attr->id = id; |
2245 | | |
2246 | 296 | return(ret); |
2247 | 296 | } |
2248 | | |
2249 | | /** |
2250 | | * Add an attribute to the docment's ID table. |
2251 | | * |
2252 | | * @since 2.13.0 |
2253 | | * |
2254 | | * @param attr the attribute holding the ID |
2255 | | * @param value the attribute (ID) value |
2256 | | * @returns 1 on success, 0 if the ID already exists, -1 if a memory |
2257 | | * allocation fails. |
2258 | | */ |
2259 | | int |
2260 | 0 | xmlAddIDSafe(xmlAttr *attr, const xmlChar *value) { |
2261 | 0 | return(xmlAddIDInternal(attr, value, NULL)); |
2262 | 0 | } |
2263 | | |
2264 | | /** |
2265 | | * Add an attribute to the docment's ID table. |
2266 | | * |
2267 | | * @param ctxt the validation context (optional) |
2268 | | * @param doc pointer to the document, must equal `attr->doc` |
2269 | | * @param value the attribute (ID) value |
2270 | | * @param attr the attribute holding the ID |
2271 | | * @returns the new xmlID or NULL on error. |
2272 | | */ |
2273 | | xmlID * |
2274 | | xmlAddID(xmlValidCtxt *ctxt, xmlDoc *doc, const xmlChar *value, |
2275 | 7.50k | xmlAttr *attr) { |
2276 | 7.50k | xmlIDPtr id; |
2277 | 7.50k | int res; |
2278 | | |
2279 | 7.50k | if ((attr == NULL) || (doc != attr->doc)) |
2280 | 0 | return(NULL); |
2281 | | |
2282 | 7.50k | res = xmlAddIDInternal(attr, value, &id); |
2283 | 7.50k | if (res < 0) { |
2284 | 0 | xmlVErrMemory(ctxt); |
2285 | 0 | } |
2286 | 7.50k | else if (res == 0) { |
2287 | 7.20k | if (ctxt != NULL) { |
2288 | | /* |
2289 | | * The id is already defined in this DTD. |
2290 | | */ |
2291 | 7.20k | xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED, |
2292 | 7.20k | "ID %s already defined\n", value, NULL, NULL); |
2293 | 7.20k | } |
2294 | 7.20k | } |
2295 | | |
2296 | 7.50k | return(id); |
2297 | 7.50k | } |
2298 | | |
2299 | | static void |
2300 | 296 | xmlFreeIDTableEntry(void *id, const xmlChar *name ATTRIBUTE_UNUSED) { |
2301 | 296 | xmlFreeID((xmlIDPtr) id); |
2302 | 296 | } |
2303 | | |
2304 | | /** |
2305 | | * Deallocate the memory used by an ID hash table. |
2306 | | * |
2307 | | * @param table An id table |
2308 | | */ |
2309 | | void |
2310 | 47 | xmlFreeIDTable(xmlIDTable *table) { |
2311 | 47 | xmlHashFree(table, xmlFreeIDTableEntry); |
2312 | 47 | } |
2313 | | |
2314 | | /** |
2315 | | * Determine whether an attribute is of type ID. In case we have DTD(s) |
2316 | | * then this is done if DTD loading has been requested. In case |
2317 | | * of HTML documents parsed with the HTML parser, ID detection is |
2318 | | * done systematically. |
2319 | | * |
2320 | | * @param doc the document |
2321 | | * @param elem the element carrying the attribute |
2322 | | * @param attr the attribute |
2323 | | * @returns 0 or 1 depending on the lookup result or -1 if a memory |
2324 | | * allocation failed. |
2325 | | */ |
2326 | | int |
2327 | 5.18k | xmlIsID(xmlDoc *doc, xmlNode *elem, xmlAttr *attr) { |
2328 | 5.18k | if ((attr == NULL) || (attr->name == NULL)) |
2329 | 0 | return(0); |
2330 | | |
2331 | 5.18k | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
2332 | 0 | if (xmlStrEqual(BAD_CAST "id", attr->name)) |
2333 | 0 | return(1); |
2334 | | |
2335 | 0 | if ((elem == NULL) || (elem->type != XML_ELEMENT_NODE)) |
2336 | 0 | return(0); |
2337 | | |
2338 | 0 | if ((xmlStrEqual(BAD_CAST "name", attr->name)) && |
2339 | 0 | (xmlStrEqual(elem->name, BAD_CAST "a"))) |
2340 | 0 | return(1); |
2341 | 5.18k | } else { |
2342 | 5.18k | xmlAttributePtr attrDecl = NULL; |
2343 | 5.18k | xmlChar felem[50]; |
2344 | 5.18k | xmlChar *fullelemname; |
2345 | 5.18k | const xmlChar *aprefix; |
2346 | | |
2347 | 5.18k | if ((attr->ns != NULL) && (attr->ns->prefix != NULL) && |
2348 | 5.18k | (!strcmp((char *) attr->name, "id")) && |
2349 | 5.18k | (!strcmp((char *) attr->ns->prefix, "xml"))) |
2350 | 0 | return(1); |
2351 | | |
2352 | 5.18k | if ((doc == NULL) || |
2353 | 5.18k | ((doc->intSubset == NULL) && (doc->extSubset == NULL))) |
2354 | 3.96k | return(0); |
2355 | | |
2356 | 1.21k | if ((elem == NULL) || |
2357 | 1.21k | (elem->type != XML_ELEMENT_NODE) || |
2358 | 1.21k | (elem->name == NULL)) |
2359 | 0 | return(0); |
2360 | | |
2361 | 1.21k | fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ? |
2362 | 36 | xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) : |
2363 | 1.21k | (xmlChar *)elem->name; |
2364 | 1.21k | if (fullelemname == NULL) |
2365 | 0 | return(-1); |
2366 | | |
2367 | 1.21k | aprefix = (attr->ns != NULL) ? attr->ns->prefix : NULL; |
2368 | | |
2369 | 1.21k | if (fullelemname != NULL) { |
2370 | 1.21k | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullelemname, |
2371 | 1.21k | attr->name, aprefix); |
2372 | 1.21k | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
2373 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullelemname, |
2374 | 0 | attr->name, aprefix); |
2375 | 1.21k | } |
2376 | | |
2377 | 1.21k | if ((fullelemname != felem) && (fullelemname != elem->name)) |
2378 | 36 | xmlFree(fullelemname); |
2379 | | |
2380 | 1.21k | if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID)) |
2381 | 0 | return(1); |
2382 | 1.21k | } |
2383 | | |
2384 | 1.21k | return(0); |
2385 | 5.18k | } |
2386 | | |
2387 | | /** |
2388 | | * Remove the given attribute from the document's ID table. |
2389 | | * |
2390 | | * @param doc the document |
2391 | | * @param attr the attribute |
2392 | | * @returns -1 if the lookup failed and 0 otherwise. |
2393 | | */ |
2394 | | int |
2395 | 0 | xmlRemoveID(xmlDoc *doc, xmlAttr *attr) { |
2396 | 0 | xmlIDTablePtr table; |
2397 | |
|
2398 | 0 | if (doc == NULL) return(-1); |
2399 | 0 | if ((attr == NULL) || (attr->id == NULL)) return(-1); |
2400 | | |
2401 | 0 | table = (xmlIDTablePtr) doc->ids; |
2402 | 0 | if (table == NULL) |
2403 | 0 | return(-1); |
2404 | | |
2405 | 0 | if (xmlHashRemoveEntry(table, attr->id->value, xmlFreeIDTableEntry) < 0) |
2406 | 0 | return(-1); |
2407 | | |
2408 | 0 | return(0); |
2409 | 0 | } |
2410 | | |
2411 | | /** |
2412 | | * Search the document's ID table for the attribute with the |
2413 | | * given ID. |
2414 | | * |
2415 | | * @param doc pointer to the document |
2416 | | * @param ID the ID value |
2417 | | * @returns the attribute or NULL if not found. |
2418 | | */ |
2419 | | xmlAttr * |
2420 | 0 | xmlGetID(xmlDoc *doc, const xmlChar *ID) { |
2421 | 0 | xmlIDTablePtr table; |
2422 | 0 | xmlIDPtr id; |
2423 | |
|
2424 | 0 | if (doc == NULL) { |
2425 | 0 | return(NULL); |
2426 | 0 | } |
2427 | | |
2428 | 0 | if (ID == NULL) { |
2429 | 0 | return(NULL); |
2430 | 0 | } |
2431 | | |
2432 | 0 | table = (xmlIDTablePtr) doc->ids; |
2433 | 0 | if (table == NULL) |
2434 | 0 | return(NULL); |
2435 | | |
2436 | 0 | id = xmlHashLookup(table, ID); |
2437 | 0 | if (id == NULL) |
2438 | 0 | return(NULL); |
2439 | 0 | if (id->attr == NULL) { |
2440 | | /* |
2441 | | * We are operating on a stream, return a well known reference |
2442 | | * since the attribute node doesn't exist anymore |
2443 | | */ |
2444 | 0 | return((xmlAttrPtr) doc); |
2445 | 0 | } |
2446 | 0 | return(id->attr); |
2447 | 0 | } |
2448 | | |
2449 | | /************************************************************************ |
2450 | | * * |
2451 | | * Refs * |
2452 | | * * |
2453 | | ************************************************************************/ |
2454 | | typedef struct xmlRemoveMemo_t |
2455 | | { |
2456 | | xmlListPtr l; |
2457 | | xmlAttrPtr ap; |
2458 | | } xmlRemoveMemo; |
2459 | | |
2460 | | typedef xmlRemoveMemo *xmlRemoveMemoPtr; |
2461 | | |
2462 | | typedef struct xmlValidateMemo_t |
2463 | | { |
2464 | | xmlValidCtxtPtr ctxt; |
2465 | | const xmlChar *name; |
2466 | | } xmlValidateMemo; |
2467 | | |
2468 | | typedef xmlValidateMemo *xmlValidateMemoPtr; |
2469 | | |
2470 | | /** |
2471 | | * Deallocate the memory used by a ref definition. |
2472 | | * |
2473 | | * @param lk A list link |
2474 | | */ |
2475 | | static void |
2476 | 0 | xmlFreeRef(xmlLinkPtr lk) { |
2477 | 0 | xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk); |
2478 | 0 | if (ref == NULL) return; |
2479 | 0 | if (ref->value != NULL) |
2480 | 0 | xmlFree((xmlChar *)ref->value); |
2481 | 0 | if (ref->name != NULL) |
2482 | 0 | xmlFree((xmlChar *)ref->name); |
2483 | 0 | xmlFree(ref); |
2484 | 0 | } |
2485 | | |
2486 | | /** |
2487 | | * Deallocate the memory used by a list of references. |
2488 | | * |
2489 | | * @param payload A list of references. |
2490 | | * @param name unused |
2491 | | */ |
2492 | | static void |
2493 | 0 | xmlFreeRefTableEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) { |
2494 | 0 | xmlListPtr list_ref = (xmlListPtr) payload; |
2495 | 0 | if (list_ref == NULL) return; |
2496 | 0 | xmlListDelete(list_ref); |
2497 | 0 | } |
2498 | | |
2499 | | /** |
2500 | | * @param data Contents of current link |
2501 | | * @param user Value supplied by the user |
2502 | | * @returns 0 to abort the walk or 1 to continue. |
2503 | | */ |
2504 | | static int |
2505 | | xmlWalkRemoveRef(const void *data, void *user) |
2506 | 0 | { |
2507 | 0 | xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr; |
2508 | 0 | xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap; |
2509 | 0 | xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l; |
2510 | |
|
2511 | 0 | if (attr0 == attr1) { /* Matched: remove and terminate walk */ |
2512 | 0 | xmlListRemoveFirst(ref_list, (void *)data); |
2513 | 0 | return 0; |
2514 | 0 | } |
2515 | 0 | return 1; |
2516 | 0 | } |
2517 | | |
2518 | | /** |
2519 | | * Do nothing. Used to create unordered lists. |
2520 | | * |
2521 | | * @param data0 Value supplied by the user |
2522 | | * @param data1 Value supplied by the user |
2523 | | * @returns 0 |
2524 | | */ |
2525 | | static int |
2526 | | xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED, |
2527 | | const void *data1 ATTRIBUTE_UNUSED) |
2528 | 0 | { |
2529 | 0 | return (0); |
2530 | 0 | } |
2531 | | |
2532 | | /** |
2533 | | * Register a new ref declaration. |
2534 | | * |
2535 | | * @deprecated Don't use. This function will be removed from the |
2536 | | * public API. |
2537 | | * |
2538 | | * @param ctxt the validation context |
2539 | | * @param doc pointer to the document |
2540 | | * @param value the value name |
2541 | | * @param attr the attribute holding the Ref |
2542 | | * @returns the new xmlRef or NULL o error. |
2543 | | */ |
2544 | | xmlRef * |
2545 | | xmlAddRef(xmlValidCtxt *ctxt, xmlDoc *doc, const xmlChar *value, |
2546 | 0 | xmlAttr *attr) { |
2547 | 0 | xmlRefPtr ret = NULL; |
2548 | 0 | xmlRefTablePtr table; |
2549 | 0 | xmlListPtr ref_list; |
2550 | |
|
2551 | 0 | if (doc == NULL) { |
2552 | 0 | return(NULL); |
2553 | 0 | } |
2554 | 0 | if (value == NULL) { |
2555 | 0 | return(NULL); |
2556 | 0 | } |
2557 | 0 | if (attr == NULL) { |
2558 | 0 | return(NULL); |
2559 | 0 | } |
2560 | | |
2561 | | /* |
2562 | | * Create the Ref table if needed. |
2563 | | */ |
2564 | 0 | table = (xmlRefTablePtr) doc->refs; |
2565 | 0 | if (table == NULL) { |
2566 | 0 | doc->refs = table = xmlHashCreateDict(0, doc->dict); |
2567 | 0 | if (table == NULL) |
2568 | 0 | goto failed; |
2569 | 0 | } |
2570 | | |
2571 | 0 | ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef)); |
2572 | 0 | if (ret == NULL) |
2573 | 0 | goto failed; |
2574 | 0 | memset(ret, 0, sizeof(*ret)); |
2575 | | |
2576 | | /* |
2577 | | * fill the structure. |
2578 | | */ |
2579 | 0 | ret->value = xmlStrdup(value); |
2580 | 0 | if (ret->value == NULL) |
2581 | 0 | goto failed; |
2582 | 0 | if (xmlIsStreaming(ctxt)) { |
2583 | | /* |
2584 | | * Operating in streaming mode, attr is gonna disappear |
2585 | | */ |
2586 | 0 | ret->name = xmlStrdup(attr->name); |
2587 | 0 | if (ret->name == NULL) |
2588 | 0 | goto failed; |
2589 | 0 | ret->attr = NULL; |
2590 | 0 | } else { |
2591 | 0 | ret->name = NULL; |
2592 | 0 | ret->attr = attr; |
2593 | 0 | } |
2594 | 0 | ret->lineno = xmlGetLineNo(attr->parent); |
2595 | | |
2596 | | /* To add a reference :- |
2597 | | * References are maintained as a list of references, |
2598 | | * Lookup the entry, if no entry create new nodelist |
2599 | | * Add the owning node to the NodeList |
2600 | | * Return the ref |
2601 | | */ |
2602 | |
|
2603 | 0 | ref_list = xmlHashLookup(table, value); |
2604 | 0 | if (ref_list == NULL) { |
2605 | 0 | int res; |
2606 | |
|
2607 | 0 | ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare); |
2608 | 0 | if (ref_list == NULL) |
2609 | 0 | goto failed; |
2610 | 0 | res = xmlHashAdd(table, value, ref_list); |
2611 | 0 | if (res <= 0) { |
2612 | 0 | xmlListDelete(ref_list); |
2613 | 0 | goto failed; |
2614 | 0 | } |
2615 | 0 | } |
2616 | 0 | if (xmlListAppend(ref_list, ret) != 0) |
2617 | 0 | goto failed; |
2618 | 0 | return(ret); |
2619 | | |
2620 | 0 | failed: |
2621 | 0 | xmlVErrMemory(ctxt); |
2622 | 0 | if (ret != NULL) { |
2623 | 0 | if (ret->value != NULL) |
2624 | 0 | xmlFree((char *)ret->value); |
2625 | 0 | if (ret->name != NULL) |
2626 | 0 | xmlFree((char *)ret->name); |
2627 | 0 | xmlFree(ret); |
2628 | 0 | } |
2629 | 0 | return(NULL); |
2630 | 0 | } |
2631 | | |
2632 | | /** |
2633 | | * Deallocate the memory used by an Ref hash table. |
2634 | | * |
2635 | | * @deprecated Don't use. This function will be removed from the |
2636 | | * public API. |
2637 | | * |
2638 | | * @param table a ref table |
2639 | | */ |
2640 | | void |
2641 | 0 | xmlFreeRefTable(xmlRefTable *table) { |
2642 | 0 | xmlHashFree(table, xmlFreeRefTableEntry); |
2643 | 0 | } |
2644 | | |
2645 | | /** |
2646 | | * Determine whether an attribute is of type Ref. In case we have DTD(s) |
2647 | | * then this is simple, otherwise we use an heuristic: name Ref (upper |
2648 | | * or lowercase). |
2649 | | * |
2650 | | * @deprecated Don't use. This function will be removed from the |
2651 | | * public API. |
2652 | | * |
2653 | | * @param doc the document |
2654 | | * @param elem the element carrying the attribute |
2655 | | * @param attr the attribute |
2656 | | * @returns 0 or 1 depending on the lookup result. |
2657 | | */ |
2658 | | int |
2659 | 5.18k | xmlIsRef(xmlDoc *doc, xmlNode *elem, xmlAttr *attr) { |
2660 | 5.18k | if (attr == NULL) |
2661 | 0 | return(0); |
2662 | 5.18k | if (doc == NULL) { |
2663 | 0 | doc = attr->doc; |
2664 | 0 | if (doc == NULL) return(0); |
2665 | 0 | } |
2666 | | |
2667 | 5.18k | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
2668 | 3.96k | return(0); |
2669 | 3.96k | } else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
2670 | | /* TODO @@@ */ |
2671 | 0 | return(0); |
2672 | 1.21k | } else { |
2673 | 1.21k | xmlAttributePtr attrDecl; |
2674 | 1.21k | const xmlChar *aprefix; |
2675 | | |
2676 | 1.21k | if (elem == NULL) return(0); |
2677 | 1.21k | aprefix = (attr->ns != NULL) ? attr->ns->prefix : NULL; |
2678 | 1.21k | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, attr->name, |
2679 | 1.21k | aprefix); |
2680 | 1.21k | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
2681 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, attr->name, |
2682 | 0 | aprefix); |
2683 | | |
2684 | 1.21k | if ((attrDecl != NULL) && |
2685 | 1.21k | (attrDecl->atype == XML_ATTRIBUTE_IDREF || |
2686 | 0 | attrDecl->atype == XML_ATTRIBUTE_IDREFS)) |
2687 | 0 | return(1); |
2688 | 1.21k | } |
2689 | 1.21k | return(0); |
2690 | 5.18k | } |
2691 | | |
2692 | | /** |
2693 | | * Remove the given attribute from the Ref table maintained internally. |
2694 | | * |
2695 | | * @deprecated Don't use. This function will be removed from the |
2696 | | * public API. |
2697 | | * |
2698 | | * @param doc the document |
2699 | | * @param attr the attribute |
2700 | | * @returns -1 if the lookup failed and 0 otherwise. |
2701 | | */ |
2702 | | int |
2703 | 0 | xmlRemoveRef(xmlDoc *doc, xmlAttr *attr) { |
2704 | 0 | xmlListPtr ref_list; |
2705 | 0 | xmlRefTablePtr table; |
2706 | 0 | xmlChar *ID; |
2707 | 0 | xmlRemoveMemo target; |
2708 | |
|
2709 | 0 | if (doc == NULL) return(-1); |
2710 | 0 | if (attr == NULL) return(-1); |
2711 | | |
2712 | 0 | table = (xmlRefTablePtr) doc->refs; |
2713 | 0 | if (table == NULL) |
2714 | 0 | return(-1); |
2715 | | |
2716 | 0 | ID = xmlNodeListGetString(doc, attr->children, 1); |
2717 | 0 | if (ID == NULL) |
2718 | 0 | return(-1); |
2719 | | |
2720 | 0 | ref_list = xmlHashLookup(table, ID); |
2721 | 0 | if(ref_list == NULL) { |
2722 | 0 | xmlFree(ID); |
2723 | 0 | return (-1); |
2724 | 0 | } |
2725 | | |
2726 | | /* At this point, ref_list refers to a list of references which |
2727 | | * have the same key as the supplied attr. Our list of references |
2728 | | * is ordered by reference address and we don't have that information |
2729 | | * here to use when removing. We'll have to walk the list and |
2730 | | * check for a matching attribute, when we find one stop the walk |
2731 | | * and remove the entry. |
2732 | | * The list is ordered by reference, so that means we don't have the |
2733 | | * key. Passing the list and the reference to the walker means we |
2734 | | * will have enough data to be able to remove the entry. |
2735 | | */ |
2736 | 0 | target.l = ref_list; |
2737 | 0 | target.ap = attr; |
2738 | | |
2739 | | /* Remove the supplied attr from our list */ |
2740 | 0 | xmlListWalk(ref_list, xmlWalkRemoveRef, &target); |
2741 | | |
2742 | | /*If the list is empty then remove the list entry in the hash */ |
2743 | 0 | if (xmlListEmpty(ref_list)) |
2744 | 0 | xmlHashRemoveEntry(table, ID, xmlFreeRefTableEntry); |
2745 | 0 | xmlFree(ID); |
2746 | 0 | return(0); |
2747 | 0 | } |
2748 | | |
2749 | | /** |
2750 | | * Find the set of references for the supplied ID. |
2751 | | * |
2752 | | * @deprecated Don't use. This function will be removed from the |
2753 | | * public API. |
2754 | | * |
2755 | | * @param doc pointer to the document |
2756 | | * @param ID the ID value |
2757 | | * @returns the list of nodes matching the ID or NULL on error. |
2758 | | */ |
2759 | | xmlList * |
2760 | 0 | xmlGetRefs(xmlDoc *doc, const xmlChar *ID) { |
2761 | 0 | xmlRefTablePtr table; |
2762 | |
|
2763 | 0 | if (doc == NULL) { |
2764 | 0 | return(NULL); |
2765 | 0 | } |
2766 | | |
2767 | 0 | if (ID == NULL) { |
2768 | 0 | return(NULL); |
2769 | 0 | } |
2770 | | |
2771 | 0 | table = (xmlRefTablePtr) doc->refs; |
2772 | 0 | if (table == NULL) |
2773 | 0 | return(NULL); |
2774 | | |
2775 | 0 | return (xmlHashLookup(table, ID)); |
2776 | 0 | } |
2777 | | |
2778 | | /************************************************************************ |
2779 | | * * |
2780 | | * Routines for validity checking * |
2781 | | * * |
2782 | | ************************************************************************/ |
2783 | | |
2784 | | /** |
2785 | | * Search the DTD for the description of this element. |
2786 | | * |
2787 | | * NOTE: A NULL return value can also mean that a memory allocation failed. |
2788 | | * |
2789 | | * @param dtd a pointer to the DtD to search |
2790 | | * @param name the element name |
2791 | | * @returns the xmlElement or NULL if not found. |
2792 | | */ |
2793 | | |
2794 | | xmlElement * |
2795 | 0 | xmlGetDtdElementDesc(xmlDtd *dtd, const xmlChar *name) { |
2796 | 0 | xmlElementTablePtr table; |
2797 | 0 | xmlElementPtr cur; |
2798 | 0 | const xmlChar *localname; |
2799 | 0 | xmlChar *prefix; |
2800 | |
|
2801 | 0 | if ((dtd == NULL) || (dtd->elements == NULL) || |
2802 | 0 | (name == NULL)) |
2803 | 0 | return(NULL); |
2804 | | |
2805 | 0 | table = (xmlElementTablePtr) dtd->elements; |
2806 | 0 | if (table == NULL) |
2807 | 0 | return(NULL); |
2808 | | |
2809 | 0 | localname = xmlSplitQName4(name, &prefix); |
2810 | 0 | if (localname == NULL) |
2811 | 0 | return(NULL); |
2812 | 0 | cur = xmlHashLookup2(table, localname, prefix); |
2813 | 0 | if (prefix != NULL) |
2814 | 0 | xmlFree(prefix); |
2815 | 0 | return(cur); |
2816 | 0 | } |
2817 | | |
2818 | | /** |
2819 | | * Search the DTD for the description of this element. |
2820 | | * |
2821 | | * @param ctxt a validation context |
2822 | | * @param dtd a pointer to the DtD to search |
2823 | | * @param name the element name |
2824 | | * @returns the xmlElement or NULL if not found. |
2825 | | */ |
2826 | | |
2827 | | static xmlElementPtr |
2828 | 3.03k | xmlGetDtdElementDesc2(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name) { |
2829 | 3.03k | xmlElementTablePtr table; |
2830 | 3.03k | xmlElementPtr cur = NULL; |
2831 | 3.03k | const xmlChar *localName; |
2832 | 3.03k | xmlChar *prefix = NULL; |
2833 | | |
2834 | 3.03k | if (dtd == NULL) return(NULL); |
2835 | | |
2836 | | /* |
2837 | | * Create the Element table if needed. |
2838 | | */ |
2839 | 3.03k | if (dtd->elements == NULL) { |
2840 | 153 | xmlDictPtr dict = NULL; |
2841 | | |
2842 | 153 | if (dtd->doc != NULL) |
2843 | 153 | dict = dtd->doc->dict; |
2844 | | |
2845 | 153 | dtd->elements = xmlHashCreateDict(0, dict); |
2846 | 153 | if (dtd->elements == NULL) |
2847 | 0 | goto mem_error; |
2848 | 153 | } |
2849 | 3.03k | table = (xmlElementTablePtr) dtd->elements; |
2850 | | |
2851 | 3.03k | localName = xmlSplitQName4(name, &prefix); |
2852 | 3.03k | if (localName == NULL) |
2853 | 0 | goto mem_error; |
2854 | 3.03k | cur = xmlHashLookup2(table, localName, prefix); |
2855 | 3.03k | if (cur == NULL) { |
2856 | 158 | cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement)); |
2857 | 158 | if (cur == NULL) |
2858 | 0 | goto mem_error; |
2859 | 158 | memset(cur, 0, sizeof(xmlElement)); |
2860 | 158 | cur->type = XML_ELEMENT_DECL; |
2861 | 158 | cur->doc = dtd->doc; |
2862 | | |
2863 | | /* |
2864 | | * fill the structure. |
2865 | | */ |
2866 | 158 | cur->name = xmlStrdup(localName); |
2867 | 158 | if (cur->name == NULL) |
2868 | 0 | goto mem_error; |
2869 | 158 | cur->prefix = prefix; |
2870 | 158 | prefix = NULL; |
2871 | 158 | cur->etype = XML_ELEMENT_TYPE_UNDEFINED; |
2872 | | |
2873 | 158 | if (xmlHashAdd2(table, localName, cur->prefix, cur) <= 0) |
2874 | 0 | goto mem_error; |
2875 | 158 | } |
2876 | | |
2877 | 3.03k | if (prefix != NULL) |
2878 | 1 | xmlFree(prefix); |
2879 | 3.03k | return(cur); |
2880 | | |
2881 | 0 | mem_error: |
2882 | 0 | xmlVErrMemory(ctxt); |
2883 | 0 | xmlFree(prefix); |
2884 | 0 | xmlFreeElement(cur); |
2885 | 0 | return(NULL); |
2886 | 3.03k | } |
2887 | | |
2888 | | /** |
2889 | | * Search the DTD for the description of this element. |
2890 | | * |
2891 | | * @param dtd a pointer to the DtD to search |
2892 | | * @param name the element name |
2893 | | * @param prefix the element namespace prefix |
2894 | | * @returns the xmlElement or NULL if not found. |
2895 | | */ |
2896 | | |
2897 | | xmlElement * |
2898 | | xmlGetDtdQElementDesc(xmlDtd *dtd, const xmlChar *name, |
2899 | 0 | const xmlChar *prefix) { |
2900 | 0 | xmlElementTablePtr table; |
2901 | |
|
2902 | 0 | if (dtd == NULL) return(NULL); |
2903 | 0 | if (dtd->elements == NULL) return(NULL); |
2904 | 0 | table = (xmlElementTablePtr) dtd->elements; |
2905 | |
|
2906 | 0 | return(xmlHashLookup2(table, name, prefix)); |
2907 | 0 | } |
2908 | | |
2909 | | /** |
2910 | | * Search the DTD for the description of this attribute on |
2911 | | * this element. |
2912 | | * |
2913 | | * @param dtd a pointer to the DtD to search |
2914 | | * @param elem the element name |
2915 | | * @param name the attribute name |
2916 | | * @returns the xmlAttribute or NULL if not found. |
2917 | | */ |
2918 | | |
2919 | | xmlAttribute * |
2920 | 0 | xmlGetDtdAttrDesc(xmlDtd *dtd, const xmlChar *elem, const xmlChar *name) { |
2921 | 0 | xmlAttributeTablePtr table; |
2922 | 0 | xmlAttributePtr cur; |
2923 | 0 | const xmlChar *localname; |
2924 | 0 | xmlChar *prefix = NULL; |
2925 | |
|
2926 | 0 | if ((dtd == NULL) || (dtd->attributes == NULL) || |
2927 | 0 | (elem == NULL) || (name == NULL)) |
2928 | 0 | return(NULL); |
2929 | | |
2930 | 0 | table = (xmlAttributeTablePtr) dtd->attributes; |
2931 | 0 | if (table == NULL) |
2932 | 0 | return(NULL); |
2933 | | |
2934 | 0 | localname = xmlSplitQName4(name, &prefix); |
2935 | 0 | if (localname == NULL) |
2936 | 0 | return(NULL); |
2937 | 0 | cur = xmlHashLookup3(table, localname, prefix, elem); |
2938 | 0 | if (prefix != NULL) |
2939 | 0 | xmlFree(prefix); |
2940 | 0 | return(cur); |
2941 | 0 | } |
2942 | | |
2943 | | /** |
2944 | | * Search the DTD for the description of this qualified attribute on |
2945 | | * this element. |
2946 | | * |
2947 | | * @param dtd a pointer to the DtD to search |
2948 | | * @param elem the element name |
2949 | | * @param name the attribute name |
2950 | | * @param prefix the attribute namespace prefix |
2951 | | * @returns the xmlAttribute or NULL if not found. |
2952 | | */ |
2953 | | |
2954 | | xmlAttribute * |
2955 | | xmlGetDtdQAttrDesc(xmlDtd *dtd, const xmlChar *elem, const xmlChar *name, |
2956 | 2.43k | const xmlChar *prefix) { |
2957 | 2.43k | xmlAttributeTablePtr table; |
2958 | | |
2959 | 2.43k | if (dtd == NULL) return(NULL); |
2960 | 2.43k | if (dtd->attributes == NULL) return(NULL); |
2961 | 876 | table = (xmlAttributeTablePtr) dtd->attributes; |
2962 | | |
2963 | 876 | return(xmlHashLookup3(table, name, prefix, elem)); |
2964 | 2.43k | } |
2965 | | |
2966 | | /** |
2967 | | * Search the DTD for the description of this notation. |
2968 | | * |
2969 | | * @param dtd a pointer to the DtD to search |
2970 | | * @param name the notation name |
2971 | | * @returns the xmlNotation or NULL if not found. |
2972 | | */ |
2973 | | |
2974 | | xmlNotation * |
2975 | 0 | xmlGetDtdNotationDesc(xmlDtd *dtd, const xmlChar *name) { |
2976 | 0 | xmlNotationTablePtr table; |
2977 | |
|
2978 | 0 | if (dtd == NULL) return(NULL); |
2979 | 0 | if (dtd->notations == NULL) return(NULL); |
2980 | 0 | table = (xmlNotationTablePtr) dtd->notations; |
2981 | |
|
2982 | 0 | return(xmlHashLookup(table, name)); |
2983 | 0 | } |
2984 | | |
2985 | | #ifdef LIBXML_VALID_ENABLED |
2986 | | /** |
2987 | | * Validate that the given name match a notation declaration. |
2988 | | * [ VC: Notation Declared ] |
2989 | | * |
2990 | | * @deprecated Internal function, don't use. |
2991 | | * |
2992 | | * @param ctxt the validation context |
2993 | | * @param doc the document |
2994 | | * @param notationName the notation name to check |
2995 | | * @returns 1 if valid or 0 otherwise. |
2996 | | */ |
2997 | | |
2998 | | int |
2999 | | xmlValidateNotationUse(xmlValidCtxt *ctxt, xmlDoc *doc, |
3000 | | const xmlChar *notationName) { |
3001 | | xmlNotationPtr notaDecl; |
3002 | | if ((doc == NULL) || (doc->intSubset == NULL) || |
3003 | | (notationName == NULL)) return(-1); |
3004 | | |
3005 | | notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName); |
3006 | | if ((notaDecl == NULL) && (doc->extSubset != NULL)) |
3007 | | notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName); |
3008 | | |
3009 | | if (notaDecl == NULL) { |
3010 | | xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION, |
3011 | | "NOTATION %s is not declared\n", |
3012 | | notationName, NULL, NULL); |
3013 | | return(0); |
3014 | | } |
3015 | | return(1); |
3016 | | } |
3017 | | #endif /* LIBXML_VALID_ENABLED */ |
3018 | | |
3019 | | /** |
3020 | | * Search in the DTDs whether an element accepts mixed content |
3021 | | * or ANY (basically if it is supposed to accept text children). |
3022 | | * |
3023 | | * @deprecated Internal function, don't use. |
3024 | | * |
3025 | | * @param doc the document |
3026 | | * @param name the element name |
3027 | | * @returns 0 if no, 1 if yes, and -1 if no element description |
3028 | | * is available. |
3029 | | */ |
3030 | | |
3031 | | int |
3032 | 0 | xmlIsMixedElement(xmlDoc *doc, const xmlChar *name) { |
3033 | 0 | xmlElementPtr elemDecl; |
3034 | |
|
3035 | 0 | if ((doc == NULL) || (doc->intSubset == NULL)) return(-1); |
3036 | | |
3037 | 0 | elemDecl = xmlGetDtdElementDesc(doc->intSubset, name); |
3038 | 0 | if ((elemDecl == NULL) && (doc->extSubset != NULL)) |
3039 | 0 | elemDecl = xmlGetDtdElementDesc(doc->extSubset, name); |
3040 | 0 | if (elemDecl == NULL) return(-1); |
3041 | 0 | switch (elemDecl->etype) { |
3042 | 0 | case XML_ELEMENT_TYPE_UNDEFINED: |
3043 | 0 | return(-1); |
3044 | 0 | case XML_ELEMENT_TYPE_ELEMENT: |
3045 | 0 | return(0); |
3046 | 0 | case XML_ELEMENT_TYPE_EMPTY: |
3047 | | /* |
3048 | | * return 1 for EMPTY since we want VC error to pop up |
3049 | | * on <empty> </empty> for example |
3050 | | */ |
3051 | 0 | case XML_ELEMENT_TYPE_ANY: |
3052 | 0 | case XML_ELEMENT_TYPE_MIXED: |
3053 | 0 | return(1); |
3054 | 0 | } |
3055 | 0 | return(1); |
3056 | 0 | } |
3057 | | |
3058 | | #ifdef LIBXML_VALID_ENABLED |
3059 | | |
3060 | | /** |
3061 | | * Normalize a string in-place. |
3062 | | * |
3063 | | * @param str a string |
3064 | | */ |
3065 | | static void |
3066 | | xmlValidNormalizeString(xmlChar *str) { |
3067 | | xmlChar *dst; |
3068 | | const xmlChar *src; |
3069 | | |
3070 | | if (str == NULL) |
3071 | | return; |
3072 | | src = str; |
3073 | | dst = str; |
3074 | | |
3075 | | while (*src == 0x20) src++; |
3076 | | while (*src != 0) { |
3077 | | if (*src == 0x20) { |
3078 | | while (*src == 0x20) src++; |
3079 | | if (*src != 0) |
3080 | | *dst++ = 0x20; |
3081 | | } else { |
3082 | | *dst++ = *src++; |
3083 | | } |
3084 | | } |
3085 | | *dst = 0; |
3086 | | } |
3087 | | |
3088 | | /** |
3089 | | * Validate that the given value matches the Name production. |
3090 | | * |
3091 | | * @param value an Name value |
3092 | | * @param flags scan flags |
3093 | | * @returns 1 if valid or 0 otherwise. |
3094 | | */ |
3095 | | |
3096 | | static int |
3097 | | xmlValidateNameValueInternal(const xmlChar *value, int flags) { |
3098 | | if ((value == NULL) || (value[0] == 0)) |
3099 | | return(0); |
3100 | | |
3101 | | value = xmlScanName(value, SIZE_MAX, flags); |
3102 | | return((value != NULL) && (*value == 0)); |
3103 | | } |
3104 | | |
3105 | | /** |
3106 | | * Validate that the given value matches the Name production. |
3107 | | * |
3108 | | * @param value an Name value |
3109 | | * @returns 1 if valid or 0 otherwise. |
3110 | | */ |
3111 | | |
3112 | | int |
3113 | | xmlValidateNameValue(const xmlChar *value) { |
3114 | | return(xmlValidateNameValueInternal(value, 0)); |
3115 | | } |
3116 | | |
3117 | | /** |
3118 | | * Validate that the given value matches the Names production. |
3119 | | * |
3120 | | * @param value an Names value |
3121 | | * @param flags scan flags |
3122 | | * @returns 1 if valid or 0 otherwise. |
3123 | | */ |
3124 | | |
3125 | | static int |
3126 | | xmlValidateNamesValueInternal(const xmlChar *value, int flags) { |
3127 | | const xmlChar *cur; |
3128 | | |
3129 | | if (value == NULL) |
3130 | | return(0); |
3131 | | |
3132 | | cur = xmlScanName(value, SIZE_MAX, flags); |
3133 | | if ((cur == NULL) || (cur == value)) |
3134 | | return(0); |
3135 | | |
3136 | | /* Should not test IS_BLANK(val) here -- see erratum E20*/ |
3137 | | while (*cur == 0x20) { |
3138 | | while (*cur == 0x20) |
3139 | | cur += 1; |
3140 | | |
3141 | | value = cur; |
3142 | | cur = xmlScanName(value, SIZE_MAX, flags); |
3143 | | if ((cur == NULL) || (cur == value)) |
3144 | | return(0); |
3145 | | } |
3146 | | |
3147 | | return(*cur == 0); |
3148 | | } |
3149 | | |
3150 | | /** |
3151 | | * Validate that the given value matches the Names production. |
3152 | | * |
3153 | | * @param value an Names value |
3154 | | * @returns 1 if valid or 0 otherwise. |
3155 | | */ |
3156 | | |
3157 | | int |
3158 | | xmlValidateNamesValue(const xmlChar *value) { |
3159 | | return(xmlValidateNamesValueInternal(value, 0)); |
3160 | | } |
3161 | | |
3162 | | /** |
3163 | | * Validate that the given value matches the Nmtoken production. |
3164 | | * |
3165 | | * [ VC: Name Token ] |
3166 | | * |
3167 | | * @param value an Nmtoken value |
3168 | | * @param flags scan flags |
3169 | | * @returns 1 if valid or 0 otherwise. |
3170 | | */ |
3171 | | |
3172 | | static int |
3173 | | xmlValidateNmtokenValueInternal(const xmlChar *value, int flags) { |
3174 | | if ((value == NULL) || (value[0] == 0)) |
3175 | | return(0); |
3176 | | |
3177 | | value = xmlScanName(value, SIZE_MAX, flags | XML_SCAN_NMTOKEN); |
3178 | | return((value != NULL) && (*value == 0)); |
3179 | | } |
3180 | | |
3181 | | /** |
3182 | | * Validate that the given value matches the Nmtoken production. |
3183 | | * |
3184 | | * [ VC: Name Token ] |
3185 | | * |
3186 | | * @param value an Nmtoken value |
3187 | | * @returns 1 if valid or 0 otherwise. |
3188 | | */ |
3189 | | |
3190 | | int |
3191 | | xmlValidateNmtokenValue(const xmlChar *value) { |
3192 | | return(xmlValidateNmtokenValueInternal(value, 0)); |
3193 | | } |
3194 | | |
3195 | | /** |
3196 | | * Validate that the given value matches the Nmtokens production. |
3197 | | * |
3198 | | * [ VC: Name Token ] |
3199 | | * |
3200 | | * @param value an Nmtokens value |
3201 | | * @param flags scan flags |
3202 | | * @returns 1 if valid or 0 otherwise. |
3203 | | */ |
3204 | | |
3205 | | static int |
3206 | | xmlValidateNmtokensValueInternal(const xmlChar *value, int flags) { |
3207 | | const xmlChar *cur; |
3208 | | |
3209 | | if (value == NULL) |
3210 | | return(0); |
3211 | | |
3212 | | cur = value; |
3213 | | while (IS_BLANK_CH(*cur)) |
3214 | | cur += 1; |
3215 | | |
3216 | | value = cur; |
3217 | | cur = xmlScanName(value, SIZE_MAX, flags | XML_SCAN_NMTOKEN); |
3218 | | if ((cur == NULL) || (cur == value)) |
3219 | | return(0); |
3220 | | |
3221 | | /* Should not test IS_BLANK(val) here -- see erratum E20*/ |
3222 | | while (*cur == 0x20) { |
3223 | | while (*cur == 0x20) |
3224 | | cur += 1; |
3225 | | if (*cur == 0) |
3226 | | return(1); |
3227 | | |
3228 | | value = cur; |
3229 | | cur = xmlScanName(value, SIZE_MAX, flags | XML_SCAN_NMTOKEN); |
3230 | | if ((cur == NULL) || (cur == value)) |
3231 | | return(0); |
3232 | | } |
3233 | | |
3234 | | return(*cur == 0); |
3235 | | } |
3236 | | |
3237 | | /** |
3238 | | * Validate that the given value matches the Nmtokens production. |
3239 | | * |
3240 | | * [ VC: Name Token ] |
3241 | | * |
3242 | | * @param value an Nmtokens value |
3243 | | * @returns 1 if valid or 0 otherwise. |
3244 | | */ |
3245 | | |
3246 | | int |
3247 | | xmlValidateNmtokensValue(const xmlChar *value) { |
3248 | | return(xmlValidateNmtokensValueInternal(value, 0)); |
3249 | | } |
3250 | | |
3251 | | /** |
3252 | | * Try to validate a single notation definition. |
3253 | | * |
3254 | | * @deprecated Internal function, don't use. |
3255 | | * |
3256 | | * It seems that no validity constraint exists on notation declarations. |
3257 | | * But this function gets called anyway ... |
3258 | | * |
3259 | | * @param ctxt the validation context |
3260 | | * @param doc a document instance |
3261 | | * @param nota a notation definition |
3262 | | * @returns 1 if valid or 0 otherwise. |
3263 | | */ |
3264 | | |
3265 | | int |
3266 | | xmlValidateNotationDecl(xmlValidCtxt *ctxt ATTRIBUTE_UNUSED, xmlDoc *doc ATTRIBUTE_UNUSED, |
3267 | | xmlNotation *nota ATTRIBUTE_UNUSED) { |
3268 | | int ret = 1; |
3269 | | |
3270 | | return(ret); |
3271 | | } |
3272 | | |
3273 | | /** |
3274 | | * Validate that the given attribute value matches the proper production. |
3275 | | * |
3276 | | * @param doc the document |
3277 | | * @param type an attribute type |
3278 | | * @param value an attribute value |
3279 | | * @returns 1 if valid or 0 otherwise. |
3280 | | */ |
3281 | | |
3282 | | static int |
3283 | | xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type, |
3284 | | const xmlChar *value) { |
3285 | | int flags = 0; |
3286 | | |
3287 | | if ((doc != NULL) && (doc->properties & XML_DOC_OLD10)) |
3288 | | flags |= XML_SCAN_OLD10; |
3289 | | |
3290 | | switch (type) { |
3291 | | case XML_ATTRIBUTE_ENTITIES: |
3292 | | case XML_ATTRIBUTE_IDREFS: |
3293 | | return(xmlValidateNamesValueInternal(value, flags)); |
3294 | | case XML_ATTRIBUTE_ENTITY: |
3295 | | case XML_ATTRIBUTE_IDREF: |
3296 | | case XML_ATTRIBUTE_ID: |
3297 | | case XML_ATTRIBUTE_NOTATION: |
3298 | | return(xmlValidateNameValueInternal(value, flags)); |
3299 | | case XML_ATTRIBUTE_NMTOKENS: |
3300 | | case XML_ATTRIBUTE_ENUMERATION: |
3301 | | return(xmlValidateNmtokensValueInternal(value, flags)); |
3302 | | case XML_ATTRIBUTE_NMTOKEN: |
3303 | | return(xmlValidateNmtokenValueInternal(value, flags)); |
3304 | | case XML_ATTRIBUTE_CDATA: |
3305 | | break; |
3306 | | } |
3307 | | return(1); |
3308 | | } |
3309 | | |
3310 | | /** |
3311 | | * Validate that the given attribute value matches the proper production. |
3312 | | * |
3313 | | * @deprecated Internal function, don't use. |
3314 | | * |
3315 | | * [ VC: ID ] |
3316 | | * Values of type ID must match the Name production.... |
3317 | | * |
3318 | | * [ VC: IDREF ] |
3319 | | * Values of type IDREF must match the Name production, and values |
3320 | | * of type IDREFS must match Names ... |
3321 | | * |
3322 | | * [ VC: Entity Name ] |
3323 | | * Values of type ENTITY must match the Name production, values |
3324 | | * of type ENTITIES must match Names ... |
3325 | | * |
3326 | | * [ VC: Name Token ] |
3327 | | * Values of type NMTOKEN must match the Nmtoken production; values |
3328 | | * of type NMTOKENS must match Nmtokens. |
3329 | | * |
3330 | | * @param type an attribute type |
3331 | | * @param value an attribute value |
3332 | | * @returns 1 if valid or 0 otherwise. |
3333 | | */ |
3334 | | int |
3335 | | xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) { |
3336 | | return(xmlValidateAttributeValueInternal(NULL, type, value)); |
3337 | | } |
3338 | | |
3339 | | /** |
3340 | | * Validate that the given attribute value matches a given type. |
3341 | | * This typically cannot be done before having finished parsing |
3342 | | * the subsets. |
3343 | | * |
3344 | | * [ VC: IDREF ] |
3345 | | * Values of type IDREF must match one of the declared IDs. |
3346 | | * Values of type IDREFS must match a sequence of the declared IDs |
3347 | | * each Name must match the value of an ID attribute on some element |
3348 | | * in the XML document; i.e. IDREF values must match the value of |
3349 | | * some ID attribute. |
3350 | | * |
3351 | | * [ VC: Entity Name ] |
3352 | | * Values of type ENTITY must match one declared entity. |
3353 | | * Values of type ENTITIES must match a sequence of declared entities. |
3354 | | * |
3355 | | * [ VC: Notation Attributes ] |
3356 | | * all notation names in the declaration must be declared. |
3357 | | * |
3358 | | * @param ctxt the validation context |
3359 | | * @param doc the document |
3360 | | * @param name the attribute name (used for error reporting only) |
3361 | | * @param type the attribute type |
3362 | | * @param value the attribute value |
3363 | | * @returns 1 if valid or 0 otherwise. |
3364 | | */ |
3365 | | |
3366 | | static int |
3367 | | xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
3368 | | const xmlChar *name, xmlAttributeType type, const xmlChar *value) { |
3369 | | int ret = 1; |
3370 | | switch (type) { |
3371 | | case XML_ATTRIBUTE_IDREFS: |
3372 | | case XML_ATTRIBUTE_IDREF: |
3373 | | case XML_ATTRIBUTE_ID: |
3374 | | case XML_ATTRIBUTE_NMTOKENS: |
3375 | | case XML_ATTRIBUTE_ENUMERATION: |
3376 | | case XML_ATTRIBUTE_NMTOKEN: |
3377 | | case XML_ATTRIBUTE_CDATA: |
3378 | | break; |
3379 | | case XML_ATTRIBUTE_ENTITY: { |
3380 | | xmlEntityPtr ent; |
3381 | | |
3382 | | ent = xmlGetDocEntity(doc, value); |
3383 | | /* yeah it's a bit messy... */ |
3384 | | if ((ent == NULL) && (doc->standalone == 1)) { |
3385 | | doc->standalone = 0; |
3386 | | ent = xmlGetDocEntity(doc, value); |
3387 | | } |
3388 | | if (ent == NULL) { |
3389 | | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
3390 | | XML_DTD_UNKNOWN_ENTITY, |
3391 | | "ENTITY attribute %s reference an unknown entity \"%s\"\n", |
3392 | | name, value, NULL); |
3393 | | ret = 0; |
3394 | | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
3395 | | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
3396 | | XML_DTD_ENTITY_TYPE, |
3397 | | "ENTITY attribute %s reference an entity \"%s\" of wrong type\n", |
3398 | | name, value, NULL); |
3399 | | ret = 0; |
3400 | | } |
3401 | | break; |
3402 | | } |
3403 | | case XML_ATTRIBUTE_ENTITIES: { |
3404 | | xmlChar *dup, *nam = NULL, *cur, save; |
3405 | | xmlEntityPtr ent; |
3406 | | |
3407 | | dup = xmlStrdup(value); |
3408 | | if (dup == NULL) { |
3409 | | xmlVErrMemory(ctxt); |
3410 | | return(0); |
3411 | | } |
3412 | | cur = dup; |
3413 | | while (*cur != 0) { |
3414 | | nam = cur; |
3415 | | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
3416 | | save = *cur; |
3417 | | *cur = 0; |
3418 | | ent = xmlGetDocEntity(doc, nam); |
3419 | | if (ent == NULL) { |
3420 | | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
3421 | | XML_DTD_UNKNOWN_ENTITY, |
3422 | | "ENTITIES attribute %s reference an unknown entity \"%s\"\n", |
3423 | | name, nam, NULL); |
3424 | | ret = 0; |
3425 | | } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
3426 | | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
3427 | | XML_DTD_ENTITY_TYPE, |
3428 | | "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n", |
3429 | | name, nam, NULL); |
3430 | | ret = 0; |
3431 | | } |
3432 | | if (save == 0) |
3433 | | break; |
3434 | | *cur = save; |
3435 | | while (IS_BLANK_CH(*cur)) cur++; |
3436 | | } |
3437 | | xmlFree(dup); |
3438 | | break; |
3439 | | } |
3440 | | case XML_ATTRIBUTE_NOTATION: { |
3441 | | xmlNotationPtr nota; |
3442 | | |
3443 | | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
3444 | | if ((nota == NULL) && (doc->extSubset != NULL)) |
3445 | | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
3446 | | |
3447 | | if (nota == NULL) { |
3448 | | xmlErrValidNode(ctxt, (xmlNodePtr) doc, |
3449 | | XML_DTD_UNKNOWN_NOTATION, |
3450 | | "NOTATION attribute %s reference an unknown notation \"%s\"\n", |
3451 | | name, value, NULL); |
3452 | | ret = 0; |
3453 | | } |
3454 | | break; |
3455 | | } |
3456 | | } |
3457 | | return(ret); |
3458 | | } |
3459 | | |
3460 | | /** |
3461 | | * Performs the validation-related extra step of the normalization |
3462 | | * of attribute values: |
3463 | | * |
3464 | | * @deprecated Internal function, don't use. |
3465 | | * |
3466 | | * If the declared value is not CDATA, then the XML processor must further |
3467 | | * process the normalized attribute value by discarding any leading and |
3468 | | * trailing space (\#x20) characters, and by replacing sequences of space |
3469 | | * (\#x20) characters by single space (\#x20) character. |
3470 | | * |
3471 | | * Also check VC: Standalone Document Declaration in P32, and update |
3472 | | * `ctxt->valid` accordingly |
3473 | | * |
3474 | | * @param ctxt the validation context |
3475 | | * @param doc the document |
3476 | | * @param elem the parent |
3477 | | * @param name the attribute name |
3478 | | * @param value the attribute value |
3479 | | * @returns a new normalized string if normalization is needed, NULL |
3480 | | * otherwise. The caller must free the returned value. |
3481 | | */ |
3482 | | |
3483 | | xmlChar * |
3484 | | xmlValidCtxtNormalizeAttributeValue(xmlValidCtxt *ctxt, xmlDoc *doc, |
3485 | | xmlNode *elem, const xmlChar *name, const xmlChar *value) { |
3486 | | xmlChar *ret; |
3487 | | xmlAttributePtr attrDecl = NULL; |
3488 | | const xmlChar *localName; |
3489 | | xmlChar *prefix = NULL; |
3490 | | int extsubset = 0; |
3491 | | |
3492 | | if (doc == NULL) return(NULL); |
3493 | | if (elem == NULL) return(NULL); |
3494 | | if (name == NULL) return(NULL); |
3495 | | if (value == NULL) return(NULL); |
3496 | | |
3497 | | localName = xmlSplitQName4(name, &prefix); |
3498 | | if (localName == NULL) |
3499 | | goto mem_error; |
3500 | | |
3501 | | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
3502 | | xmlChar buf[50]; |
3503 | | xmlChar *elemname; |
3504 | | |
3505 | | elemname = xmlBuildQName(elem->name, elem->ns->prefix, buf, 50); |
3506 | | if (elemname == NULL) |
3507 | | goto mem_error; |
3508 | | if (doc->intSubset != NULL) |
3509 | | attrDecl = xmlHashLookup3(doc->intSubset->attributes, localName, |
3510 | | prefix, elemname); |
3511 | | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
3512 | | attrDecl = xmlHashLookup3(doc->extSubset->attributes, localName, |
3513 | | prefix, elemname); |
3514 | | if (attrDecl != NULL) |
3515 | | extsubset = 1; |
3516 | | } |
3517 | | if ((elemname != buf) && (elemname != elem->name)) |
3518 | | xmlFree(elemname); |
3519 | | } |
3520 | | if ((attrDecl == NULL) && (doc->intSubset != NULL)) |
3521 | | attrDecl = xmlHashLookup3(doc->intSubset->attributes, localName, |
3522 | | prefix, elem->name); |
3523 | | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
3524 | | attrDecl = xmlHashLookup3(doc->extSubset->attributes, localName, |
3525 | | prefix, elem->name); |
3526 | | if (attrDecl != NULL) |
3527 | | extsubset = 1; |
3528 | | } |
3529 | | |
3530 | | if (attrDecl == NULL) |
3531 | | goto done; |
3532 | | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
3533 | | goto done; |
3534 | | |
3535 | | ret = xmlStrdup(value); |
3536 | | if (ret == NULL) |
3537 | | goto mem_error; |
3538 | | xmlValidNormalizeString(ret); |
3539 | | if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) { |
3540 | | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE, |
3541 | | "standalone: %s on %s value had to be normalized based on external subset declaration\n", |
3542 | | name, elem->name, NULL); |
3543 | | ctxt->valid = 0; |
3544 | | } |
3545 | | |
3546 | | xmlFree(prefix); |
3547 | | return(ret); |
3548 | | |
3549 | | mem_error: |
3550 | | xmlVErrMemory(ctxt); |
3551 | | |
3552 | | done: |
3553 | | xmlFree(prefix); |
3554 | | return(NULL); |
3555 | | } |
3556 | | |
3557 | | /** |
3558 | | * Performs the validation-related extra step of the normalization |
3559 | | * of attribute values: |
3560 | | * |
3561 | | * @deprecated Internal function, don't use. |
3562 | | * |
3563 | | * If the declared value is not CDATA, then the XML processor must further |
3564 | | * process the normalized attribute value by discarding any leading and |
3565 | | * trailing space (\#x20) characters, and by replacing sequences of space |
3566 | | * (\#x20) characters by single space (\#x20) character. |
3567 | | * |
3568 | | * @param doc the document |
3569 | | * @param elem the parent |
3570 | | * @param name the attribute name |
3571 | | * @param value the attribute value |
3572 | | * @returns a new normalized string if normalization is needed, NULL |
3573 | | * otherwise. The caller must free the returned value. |
3574 | | */ |
3575 | | |
3576 | | xmlChar * |
3577 | | xmlValidNormalizeAttributeValue(xmlDoc *doc, xmlNode *elem, |
3578 | | const xmlChar *name, const xmlChar *value) { |
3579 | | xmlChar *ret; |
3580 | | xmlAttributePtr attrDecl = NULL; |
3581 | | |
3582 | | if (doc == NULL) return(NULL); |
3583 | | if (elem == NULL) return(NULL); |
3584 | | if (name == NULL) return(NULL); |
3585 | | if (value == NULL) return(NULL); |
3586 | | |
3587 | | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
3588 | | xmlChar fn[50]; |
3589 | | xmlChar *fullname; |
3590 | | |
3591 | | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
3592 | | if (fullname == NULL) |
3593 | | return(NULL); |
3594 | | if ((fullname != fn) && (fullname != elem->name)) |
3595 | | xmlFree(fullname); |
3596 | | } |
3597 | | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name); |
3598 | | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
3599 | | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name); |
3600 | | |
3601 | | if (attrDecl == NULL) |
3602 | | return(NULL); |
3603 | | if (attrDecl->atype == XML_ATTRIBUTE_CDATA) |
3604 | | return(NULL); |
3605 | | |
3606 | | ret = xmlStrdup(value); |
3607 | | if (ret == NULL) |
3608 | | return(NULL); |
3609 | | xmlValidNormalizeString(ret); |
3610 | | return(ret); |
3611 | | } |
3612 | | |
3613 | | static void |
3614 | | xmlValidateAttributeIdCallback(void *payload, void *data, |
3615 | | const xmlChar *name ATTRIBUTE_UNUSED) { |
3616 | | xmlAttributePtr attr = (xmlAttributePtr) payload; |
3617 | | int *count = (int *) data; |
3618 | | if (attr->atype == XML_ATTRIBUTE_ID) (*count)++; |
3619 | | } |
3620 | | |
3621 | | /** |
3622 | | * Try to validate a single attribute definition. |
3623 | | * Performs the following checks as described by the |
3624 | | * XML-1.0 recommendation: |
3625 | | * |
3626 | | * @deprecated Internal function, don't use. |
3627 | | * |
3628 | | * - [ VC: Attribute Default Legal ] |
3629 | | * - [ VC: Enumeration ] |
3630 | | * - [ VC: ID Attribute Default ] |
3631 | | * |
3632 | | * The ID/IDREF uniqueness and matching are done separately. |
3633 | | * |
3634 | | * @param ctxt the validation context |
3635 | | * @param doc a document instance |
3636 | | * @param attr an attribute definition |
3637 | | * @returns 1 if valid or 0 otherwise. |
3638 | | */ |
3639 | | |
3640 | | int |
3641 | | xmlValidateAttributeDecl(xmlValidCtxt *ctxt, xmlDoc *doc, |
3642 | | xmlAttribute *attr) { |
3643 | | int ret = 1; |
3644 | | int val; |
3645 | | CHECK_DTD; |
3646 | | if(attr == NULL) return(1); |
3647 | | |
3648 | | /* Attribute Default Legal */ |
3649 | | /* Enumeration */ |
3650 | | if (attr->defaultValue != NULL) { |
3651 | | val = xmlValidateAttributeValueInternal(doc, attr->atype, |
3652 | | attr->defaultValue); |
3653 | | if (val == 0) { |
3654 | | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT, |
3655 | | "Syntax of default value for attribute %s of %s is not valid\n", |
3656 | | attr->name, attr->elem, NULL); |
3657 | | } |
3658 | | ret &= val; |
3659 | | } |
3660 | | |
3661 | | /* ID Attribute Default */ |
3662 | | if ((attr->atype == XML_ATTRIBUTE_ID)&& |
3663 | | (attr->def != XML_ATTRIBUTE_IMPLIED) && |
3664 | | (attr->def != XML_ATTRIBUTE_REQUIRED)) { |
3665 | | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED, |
3666 | | "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n", |
3667 | | attr->name, attr->elem, NULL); |
3668 | | ret = 0; |
3669 | | } |
3670 | | |
3671 | | /* One ID per Element Type */ |
3672 | | if (attr->atype == XML_ATTRIBUTE_ID) { |
3673 | | xmlElementPtr elem = NULL; |
3674 | | const xmlChar *elemLocalName; |
3675 | | xmlChar *elemPrefix; |
3676 | | int nbId; |
3677 | | |
3678 | | elemLocalName = xmlSplitQName4(attr->elem, &elemPrefix); |
3679 | | if (elemLocalName == NULL) { |
3680 | | xmlVErrMemory(ctxt); |
3681 | | return(0); |
3682 | | } |
3683 | | |
3684 | | /* the trick is that we parse DtD as their own internal subset */ |
3685 | | if (doc->intSubset != NULL) |
3686 | | elem = xmlHashLookup2(doc->intSubset->elements, |
3687 | | elemLocalName, elemPrefix); |
3688 | | if (elem != NULL) { |
3689 | | nbId = xmlScanIDAttributeDecl(ctxt, elem, 0); |
3690 | | } else { |
3691 | | xmlAttributeTablePtr table; |
3692 | | |
3693 | | /* |
3694 | | * The attribute may be declared in the internal subset and the |
3695 | | * element in the external subset. |
3696 | | */ |
3697 | | nbId = 0; |
3698 | | if (doc->intSubset != NULL) { |
3699 | | table = (xmlAttributeTablePtr) doc->intSubset->attributes; |
3700 | | xmlHashScan3(table, NULL, NULL, attr->elem, |
3701 | | xmlValidateAttributeIdCallback, &nbId); |
3702 | | } |
3703 | | } |
3704 | | if (nbId > 1) { |
3705 | | |
3706 | | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
3707 | | "Element %s has %d ID attribute defined in the internal subset : %s\n", |
3708 | | attr->elem, nbId, attr->name); |
3709 | | ret = 0; |
3710 | | } else if (doc->extSubset != NULL) { |
3711 | | int extId = 0; |
3712 | | elem = xmlHashLookup2(doc->extSubset->elements, |
3713 | | elemLocalName, elemPrefix); |
3714 | | if (elem != NULL) { |
3715 | | extId = xmlScanIDAttributeDecl(ctxt, elem, 0); |
3716 | | } |
3717 | | if (extId > 1) { |
3718 | | xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
3719 | | "Element %s has %d ID attribute defined in the external subset : %s\n", |
3720 | | attr->elem, extId, attr->name); |
3721 | | ret = 0; |
3722 | | } else if (extId + nbId > 1) { |
3723 | | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET, |
3724 | | "Element %s has ID attributes defined in the internal and external subset : %s\n", |
3725 | | attr->elem, attr->name, NULL); |
3726 | | ret = 0; |
3727 | | } |
3728 | | } |
3729 | | |
3730 | | xmlFree(elemPrefix); |
3731 | | } |
3732 | | |
3733 | | /* Validity Constraint: Enumeration */ |
3734 | | if ((attr->defaultValue != NULL) && (attr->tree != NULL)) { |
3735 | | xmlEnumerationPtr tree = attr->tree; |
3736 | | while (tree != NULL) { |
3737 | | if (xmlStrEqual(tree->name, attr->defaultValue)) break; |
3738 | | tree = tree->next; |
3739 | | } |
3740 | | if (tree == NULL) { |
3741 | | xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE, |
3742 | | "Default value \"%s\" for attribute %s of %s is not among the enumerated set\n", |
3743 | | attr->defaultValue, attr->name, attr->elem); |
3744 | | ret = 0; |
3745 | | } |
3746 | | } |
3747 | | |
3748 | | return(ret); |
3749 | | } |
3750 | | |
3751 | | /** |
3752 | | * Try to validate a single element definition. |
3753 | | * Performs the following checks as described by the |
3754 | | * XML-1.0 recommendation: |
3755 | | * |
3756 | | * @deprecated Internal function, don't use. |
3757 | | * |
3758 | | * - [ VC: One ID per Element Type ] |
3759 | | * - [ VC: No Duplicate Types ] |
3760 | | * - [ VC: Unique Element Type Declaration ] |
3761 | | * |
3762 | | * @param ctxt the validation context |
3763 | | * @param doc a document instance |
3764 | | * @param elem an element definition |
3765 | | * @returns 1 if valid or 0 otherwise. |
3766 | | */ |
3767 | | |
3768 | | int |
3769 | | xmlValidateElementDecl(xmlValidCtxt *ctxt, xmlDoc *doc, |
3770 | | xmlElement *elem) { |
3771 | | int ret = 1; |
3772 | | xmlElementPtr tst; |
3773 | | const xmlChar *localName; |
3774 | | xmlChar *prefix; |
3775 | | |
3776 | | CHECK_DTD; |
3777 | | |
3778 | | if (elem == NULL) return(1); |
3779 | | |
3780 | | /* No Duplicate Types */ |
3781 | | if (elem->etype == XML_ELEMENT_TYPE_MIXED) { |
3782 | | xmlElementContentPtr cur, next; |
3783 | | const xmlChar *name; |
3784 | | |
3785 | | cur = elem->content; |
3786 | | while (cur != NULL) { |
3787 | | if (cur->type != XML_ELEMENT_CONTENT_OR) break; |
3788 | | if (cur->c1 == NULL) break; |
3789 | | if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
3790 | | name = cur->c1->name; |
3791 | | next = cur->c2; |
3792 | | while (next != NULL) { |
3793 | | if (next->type == XML_ELEMENT_CONTENT_ELEMENT) { |
3794 | | if ((xmlStrEqual(next->name, name)) && |
3795 | | (xmlStrEqual(next->prefix, cur->c1->prefix))) { |
3796 | | if (cur->c1->prefix == NULL) { |
3797 | | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
3798 | | "Definition of %s has duplicate references of %s\n", |
3799 | | elem->name, name, NULL); |
3800 | | } else { |
3801 | | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
3802 | | "Definition of %s has duplicate references of %s:%s\n", |
3803 | | elem->name, cur->c1->prefix, name); |
3804 | | } |
3805 | | ret = 0; |
3806 | | } |
3807 | | break; |
3808 | | } |
3809 | | if (next->c1 == NULL) break; |
3810 | | if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break; |
3811 | | if ((xmlStrEqual(next->c1->name, name)) && |
3812 | | (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) { |
3813 | | if (cur->c1->prefix == NULL) { |
3814 | | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
3815 | | "Definition of %s has duplicate references to %s\n", |
3816 | | elem->name, name, NULL); |
3817 | | } else { |
3818 | | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR, |
3819 | | "Definition of %s has duplicate references to %s:%s\n", |
3820 | | elem->name, cur->c1->prefix, name); |
3821 | | } |
3822 | | ret = 0; |
3823 | | } |
3824 | | next = next->c2; |
3825 | | } |
3826 | | } |
3827 | | cur = cur->c2; |
3828 | | } |
3829 | | } |
3830 | | |
3831 | | localName = xmlSplitQName4(elem->name, &prefix); |
3832 | | if (localName == NULL) { |
3833 | | xmlVErrMemory(ctxt); |
3834 | | return(0); |
3835 | | } |
3836 | | |
3837 | | /* VC: Unique Element Type Declaration */ |
3838 | | if (doc->intSubset != NULL) { |
3839 | | tst = xmlHashLookup2(doc->intSubset->elements, localName, prefix); |
3840 | | |
3841 | | if ((tst != NULL ) && (tst != elem) && |
3842 | | ((tst->prefix == elem->prefix) || |
3843 | | (xmlStrEqual(tst->prefix, elem->prefix))) && |
3844 | | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
3845 | | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
3846 | | "Redefinition of element %s\n", |
3847 | | elem->name, NULL, NULL); |
3848 | | ret = 0; |
3849 | | } |
3850 | | } |
3851 | | if (doc->extSubset != NULL) { |
3852 | | tst = xmlHashLookup2(doc->extSubset->elements, localName, prefix); |
3853 | | |
3854 | | if ((tst != NULL ) && (tst != elem) && |
3855 | | ((tst->prefix == elem->prefix) || |
3856 | | (xmlStrEqual(tst->prefix, elem->prefix))) && |
3857 | | (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) { |
3858 | | xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED, |
3859 | | "Redefinition of element %s\n", |
3860 | | elem->name, NULL, NULL); |
3861 | | ret = 0; |
3862 | | } |
3863 | | } |
3864 | | |
3865 | | xmlFree(prefix); |
3866 | | return(ret); |
3867 | | } |
3868 | | |
3869 | | /** |
3870 | | * Try to validate a single attribute for an element. |
3871 | | * Performs the following checks as described by the |
3872 | | * XML-1.0 recommendation: |
3873 | | * |
3874 | | * @deprecated Internal function, don't use. |
3875 | | * |
3876 | | * - [ VC: Attribute Value Type ] |
3877 | | * - [ VC: Fixed Attribute Default ] |
3878 | | * - [ VC: Entity Name ] |
3879 | | * - [ VC: Name Token ] |
3880 | | * - [ VC: ID ] |
3881 | | * - [ VC: IDREF ] |
3882 | | * - [ VC: Entity Name ] |
3883 | | * - [ VC: Notation Attributes ] |
3884 | | * |
3885 | | * ID/IDREF uniqueness and matching are handled separately. |
3886 | | * |
3887 | | * @param ctxt the validation context |
3888 | | * @param doc a document instance |
3889 | | * @param elem an element instance |
3890 | | * @param attr an attribute instance |
3891 | | * @param value the attribute value (without entities processing) |
3892 | | * @returns 1 if valid or 0 otherwise. |
3893 | | */ |
3894 | | |
3895 | | int |
3896 | | xmlValidateOneAttribute(xmlValidCtxt *ctxt, xmlDoc *doc, |
3897 | | xmlNode *elem, xmlAttr *attr, const xmlChar *value) |
3898 | | { |
3899 | | xmlAttributePtr attrDecl = NULL; |
3900 | | const xmlChar *aprefix; |
3901 | | int val; |
3902 | | int ret = 1; |
3903 | | |
3904 | | CHECK_DTD; |
3905 | | if ((elem == NULL) || (elem->name == NULL)) return(0); |
3906 | | if ((attr == NULL) || (attr->name == NULL)) return(0); |
3907 | | |
3908 | | aprefix = (attr->ns != NULL) ? attr->ns->prefix : NULL; |
3909 | | |
3910 | | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) { |
3911 | | xmlChar fn[50]; |
3912 | | xmlChar *fullname; |
3913 | | |
3914 | | fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50); |
3915 | | if (fullname == NULL) { |
3916 | | xmlVErrMemory(ctxt); |
3917 | | return(0); |
3918 | | } |
3919 | | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
3920 | | attr->name, aprefix); |
3921 | | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
3922 | | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
3923 | | attr->name, aprefix); |
3924 | | if ((fullname != fn) && (fullname != elem->name)) |
3925 | | xmlFree(fullname); |
3926 | | } |
3927 | | if (attrDecl == NULL) { |
3928 | | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
3929 | | attr->name, aprefix); |
3930 | | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
3931 | | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
3932 | | attr->name, aprefix); |
3933 | | } |
3934 | | |
3935 | | |
3936 | | /* Validity Constraint: Attribute Value Type */ |
3937 | | if (attrDecl == NULL) { |
3938 | | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
3939 | | "No declaration for attribute %s of element %s\n", |
3940 | | attr->name, elem->name, NULL); |
3941 | | return(0); |
3942 | | } |
3943 | | if (attr->atype == XML_ATTRIBUTE_ID) |
3944 | | xmlRemoveID(doc, attr); |
3945 | | attr->atype = attrDecl->atype; |
3946 | | |
3947 | | val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value); |
3948 | | if (val == 0) { |
3949 | | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
3950 | | "Syntax of value for attribute %s of %s is not valid\n", |
3951 | | attr->name, elem->name, NULL); |
3952 | | ret = 0; |
3953 | | } |
3954 | | |
3955 | | /* Validity constraint: Fixed Attribute Default */ |
3956 | | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
3957 | | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
3958 | | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
3959 | | "Value for attribute %s of %s is different from default \"%s\"\n", |
3960 | | attr->name, elem->name, attrDecl->defaultValue); |
3961 | | ret = 0; |
3962 | | } |
3963 | | } |
3964 | | |
3965 | | /* Validity Constraint: ID uniqueness */ |
3966 | | if (attrDecl->atype == XML_ATTRIBUTE_ID) { |
3967 | | if (xmlAddID(ctxt, doc, value, attr) == NULL) |
3968 | | ret = 0; |
3969 | | } |
3970 | | |
3971 | | if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) || |
3972 | | (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) { |
3973 | | if (xmlAddRef(ctxt, doc, value, attr) == NULL) |
3974 | | ret = 0; |
3975 | | } |
3976 | | |
3977 | | /* Validity Constraint: Notation Attributes */ |
3978 | | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
3979 | | xmlEnumerationPtr tree = attrDecl->tree; |
3980 | | xmlNotationPtr nota; |
3981 | | |
3982 | | /* First check that the given NOTATION was declared */ |
3983 | | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
3984 | | if (nota == NULL) |
3985 | | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
3986 | | |
3987 | | if (nota == NULL) { |
3988 | | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
3989 | | "Value \"%s\" for attribute %s of %s is not a declared Notation\n", |
3990 | | value, attr->name, elem->name); |
3991 | | ret = 0; |
3992 | | } |
3993 | | |
3994 | | /* Second, verify that it's among the list */ |
3995 | | while (tree != NULL) { |
3996 | | if (xmlStrEqual(tree->name, value)) break; |
3997 | | tree = tree->next; |
3998 | | } |
3999 | | if (tree == NULL) { |
4000 | | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
4001 | | "Value \"%s\" for attribute %s of %s is not among the enumerated notations\n", |
4002 | | value, attr->name, elem->name); |
4003 | | ret = 0; |
4004 | | } |
4005 | | } |
4006 | | |
4007 | | /* Validity Constraint: Enumeration */ |
4008 | | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
4009 | | xmlEnumerationPtr tree = attrDecl->tree; |
4010 | | while (tree != NULL) { |
4011 | | if (xmlStrEqual(tree->name, value)) break; |
4012 | | tree = tree->next; |
4013 | | } |
4014 | | if (tree == NULL) { |
4015 | | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
4016 | | "Value \"%s\" for attribute %s of %s is not among the enumerated set\n", |
4017 | | value, attr->name, elem->name); |
4018 | | ret = 0; |
4019 | | } |
4020 | | } |
4021 | | |
4022 | | /* Fixed Attribute Default */ |
4023 | | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
4024 | | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
4025 | | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
4026 | | "Value for attribute %s of %s must be \"%s\"\n", |
4027 | | attr->name, elem->name, attrDecl->defaultValue); |
4028 | | ret = 0; |
4029 | | } |
4030 | | |
4031 | | /* Extra check for the attribute value */ |
4032 | | ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name, |
4033 | | attrDecl->atype, value); |
4034 | | |
4035 | | return(ret); |
4036 | | } |
4037 | | |
4038 | | /** |
4039 | | * Try to validate a single namespace declaration for an element. |
4040 | | * Performs the following checks as described by the |
4041 | | * XML-1.0 recommendation: |
4042 | | * |
4043 | | * @deprecated Internal function, don't use. |
4044 | | * |
4045 | | * - [ VC: Attribute Value Type ] |
4046 | | * - [ VC: Fixed Attribute Default ] |
4047 | | * - [ VC: Entity Name ] |
4048 | | * - [ VC: Name Token ] |
4049 | | * - [ VC: ID ] |
4050 | | * - [ VC: IDREF ] |
4051 | | * - [ VC: Entity Name ] |
4052 | | * - [ VC: Notation Attributes ] |
4053 | | * |
4054 | | * ID/IDREF uniqueness and matching are handled separately. |
4055 | | * |
4056 | | * @param ctxt the validation context |
4057 | | * @param doc a document instance |
4058 | | * @param elem an element instance |
4059 | | * @param prefix the namespace prefix |
4060 | | * @param ns an namespace declaration instance |
4061 | | * @param value the attribute value (without entities processing) |
4062 | | * @returns 1 if valid or 0 otherwise. |
4063 | | */ |
4064 | | |
4065 | | int |
4066 | | xmlValidateOneNamespace(xmlValidCtxt *ctxt, xmlDoc *doc, |
4067 | | xmlNode *elem, const xmlChar *prefix, xmlNs *ns, const xmlChar *value) { |
4068 | | /* xmlElementPtr elemDecl; */ |
4069 | | xmlAttributePtr attrDecl = NULL; |
4070 | | int val; |
4071 | | int ret = 1; |
4072 | | |
4073 | | CHECK_DTD; |
4074 | | if ((elem == NULL) || (elem->name == NULL)) return(0); |
4075 | | if ((ns == NULL) || (ns->href == NULL)) return(0); |
4076 | | |
4077 | | if (prefix != NULL) { |
4078 | | xmlChar fn[50]; |
4079 | | xmlChar *fullname; |
4080 | | |
4081 | | fullname = xmlBuildQName(elem->name, prefix, fn, 50); |
4082 | | if (fullname == NULL) { |
4083 | | xmlVErrMemory(ctxt); |
4084 | | return(0); |
4085 | | } |
4086 | | if (ns->prefix != NULL) { |
4087 | | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
4088 | | ns->prefix, BAD_CAST "xmlns"); |
4089 | | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4090 | | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
4091 | | ns->prefix, BAD_CAST "xmlns"); |
4092 | | } else { |
4093 | | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname, |
4094 | | BAD_CAST "xmlns", NULL); |
4095 | | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4096 | | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname, |
4097 | | BAD_CAST "xmlns", NULL); |
4098 | | } |
4099 | | if ((fullname != fn) && (fullname != elem->name)) |
4100 | | xmlFree(fullname); |
4101 | | } |
4102 | | if (attrDecl == NULL) { |
4103 | | if (ns->prefix != NULL) { |
4104 | | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
4105 | | ns->prefix, BAD_CAST "xmlns"); |
4106 | | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4107 | | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
4108 | | ns->prefix, BAD_CAST "xmlns"); |
4109 | | } else { |
4110 | | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, |
4111 | | BAD_CAST "xmlns", NULL); |
4112 | | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
4113 | | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, |
4114 | | BAD_CAST "xmlns", NULL); |
4115 | | } |
4116 | | } |
4117 | | |
4118 | | |
4119 | | /* Validity Constraint: Attribute Value Type */ |
4120 | | if (attrDecl == NULL) { |
4121 | | if (ns->prefix != NULL) { |
4122 | | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
4123 | | "No declaration for attribute xmlns:%s of element %s\n", |
4124 | | ns->prefix, elem->name, NULL); |
4125 | | } else { |
4126 | | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE, |
4127 | | "No declaration for attribute xmlns of element %s\n", |
4128 | | elem->name, NULL, NULL); |
4129 | | } |
4130 | | return(0); |
4131 | | } |
4132 | | |
4133 | | val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value); |
4134 | | if (val == 0) { |
4135 | | if (ns->prefix != NULL) { |
4136 | | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
4137 | | "Syntax of value for attribute xmlns:%s of %s is not valid\n", |
4138 | | ns->prefix, elem->name, NULL); |
4139 | | } else { |
4140 | | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT, |
4141 | | "Syntax of value for attribute xmlns of %s is not valid\n", |
4142 | | elem->name, NULL, NULL); |
4143 | | } |
4144 | | ret = 0; |
4145 | | } |
4146 | | |
4147 | | /* Validity constraint: Fixed Attribute Default */ |
4148 | | if (attrDecl->def == XML_ATTRIBUTE_FIXED) { |
4149 | | if (!xmlStrEqual(value, attrDecl->defaultValue)) { |
4150 | | if (ns->prefix != NULL) { |
4151 | | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
4152 | | "Value for attribute xmlns:%s of %s is different from default \"%s\"\n", |
4153 | | ns->prefix, elem->name, attrDecl->defaultValue); |
4154 | | } else { |
4155 | | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT, |
4156 | | "Value for attribute xmlns of %s is different from default \"%s\"\n", |
4157 | | elem->name, attrDecl->defaultValue, NULL); |
4158 | | } |
4159 | | ret = 0; |
4160 | | } |
4161 | | } |
4162 | | |
4163 | | /* Validity Constraint: Notation Attributes */ |
4164 | | if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) { |
4165 | | xmlEnumerationPtr tree = attrDecl->tree; |
4166 | | xmlNotationPtr nota; |
4167 | | |
4168 | | /* First check that the given NOTATION was declared */ |
4169 | | nota = xmlGetDtdNotationDesc(doc->intSubset, value); |
4170 | | if (nota == NULL) |
4171 | | nota = xmlGetDtdNotationDesc(doc->extSubset, value); |
4172 | | |
4173 | | if (nota == NULL) { |
4174 | | if (ns->prefix != NULL) { |
4175 | | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
4176 | | "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n", |
4177 | | value, ns->prefix, elem->name); |
4178 | | } else { |
4179 | | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION, |
4180 | | "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n", |
4181 | | value, elem->name, NULL); |
4182 | | } |
4183 | | ret = 0; |
4184 | | } |
4185 | | |
4186 | | /* Second, verify that it's among the list */ |
4187 | | while (tree != NULL) { |
4188 | | if (xmlStrEqual(tree->name, value)) break; |
4189 | | tree = tree->next; |
4190 | | } |
4191 | | if (tree == NULL) { |
4192 | | if (ns->prefix != NULL) { |
4193 | | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
4194 | | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n", |
4195 | | value, ns->prefix, elem->name); |
4196 | | } else { |
4197 | | xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE, |
4198 | | "Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n", |
4199 | | value, elem->name, NULL); |
4200 | | } |
4201 | | ret = 0; |
4202 | | } |
4203 | | } |
4204 | | |
4205 | | /* Validity Constraint: Enumeration */ |
4206 | | if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) { |
4207 | | xmlEnumerationPtr tree = attrDecl->tree; |
4208 | | while (tree != NULL) { |
4209 | | if (xmlStrEqual(tree->name, value)) break; |
4210 | | tree = tree->next; |
4211 | | } |
4212 | | if (tree == NULL) { |
4213 | | if (ns->prefix != NULL) { |
4214 | | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
4215 | | "Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n", |
4216 | | value, ns->prefix, elem->name); |
4217 | | } else { |
4218 | | xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE, |
4219 | | "Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n", |
4220 | | value, elem->name, NULL); |
4221 | | } |
4222 | | ret = 0; |
4223 | | } |
4224 | | } |
4225 | | |
4226 | | /* Fixed Attribute Default */ |
4227 | | if ((attrDecl->def == XML_ATTRIBUTE_FIXED) && |
4228 | | (!xmlStrEqual(attrDecl->defaultValue, value))) { |
4229 | | if (ns->prefix != NULL) { |
4230 | | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
4231 | | "Value for attribute xmlns:%s of %s must be \"%s\"\n", |
4232 | | ns->prefix, elem->name, attrDecl->defaultValue); |
4233 | | } else { |
4234 | | xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE, |
4235 | | "Value for attribute xmlns of %s must be \"%s\"\n", |
4236 | | elem->name, attrDecl->defaultValue, NULL); |
4237 | | } |
4238 | | ret = 0; |
4239 | | } |
4240 | | |
4241 | | /* Extra check for the attribute value */ |
4242 | | if (ns->prefix != NULL) { |
4243 | | ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix, |
4244 | | attrDecl->atype, value); |
4245 | | } else { |
4246 | | ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns", |
4247 | | attrDecl->atype, value); |
4248 | | } |
4249 | | |
4250 | | return(ret); |
4251 | | } |
4252 | | |
4253 | | #ifndef LIBXML_REGEXP_ENABLED |
4254 | | /** |
4255 | | * Skip ignorable elements w.r.t. the validation process |
4256 | | * |
4257 | | * @param ctxt the validation context |
4258 | | * @param child the child list |
4259 | | * @returns the first element to consider for validation of the content model |
4260 | | */ |
4261 | | |
4262 | | static xmlNodePtr |
4263 | | xmlValidateSkipIgnorable(xmlNodePtr child) { |
4264 | | while (child != NULL) { |
4265 | | switch (child->type) { |
4266 | | /* These things are ignored (skipped) during validation. */ |
4267 | | case XML_PI_NODE: |
4268 | | case XML_COMMENT_NODE: |
4269 | | case XML_XINCLUDE_START: |
4270 | | case XML_XINCLUDE_END: |
4271 | | child = child->next; |
4272 | | break; |
4273 | | case XML_TEXT_NODE: |
4274 | | if (xmlIsBlankNode(child)) |
4275 | | child = child->next; |
4276 | | else |
4277 | | return(child); |
4278 | | break; |
4279 | | /* keep current node */ |
4280 | | default: |
4281 | | return(child); |
4282 | | } |
4283 | | } |
4284 | | return(child); |
4285 | | } |
4286 | | |
4287 | | /** |
4288 | | * Try to validate the content model of an element internal function |
4289 | | * |
4290 | | * @param ctxt the validation context |
4291 | | * @returns 1 if valid or 0 ,-1 in case of error, -2 if an entity |
4292 | | * reference is found and -3 if the validation succeeded but |
4293 | | * the content model is not determinist. |
4294 | | */ |
4295 | | |
4296 | | static int |
4297 | | xmlValidateElementType(xmlValidCtxtPtr ctxt) { |
4298 | | int ret = -1; |
4299 | | int determinist = 1; |
4300 | | |
4301 | | NODE = xmlValidateSkipIgnorable(NODE); |
4302 | | if ((NODE == NULL) && (CONT == NULL)) |
4303 | | return(1); |
4304 | | if ((NODE == NULL) && |
4305 | | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
4306 | | (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) { |
4307 | | return(1); |
4308 | | } |
4309 | | if (CONT == NULL) return(-1); |
4310 | | if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE)) |
4311 | | return(-2); |
4312 | | |
4313 | | /* |
4314 | | * We arrive here when more states need to be examined |
4315 | | */ |
4316 | | cont: |
4317 | | |
4318 | | /* |
4319 | | * We just recovered from a rollback generated by a possible |
4320 | | * epsilon transition, go directly to the analysis phase |
4321 | | */ |
4322 | | if (STATE == ROLLBACK_PARENT) { |
4323 | | ret = 1; |
4324 | | goto analyze; |
4325 | | } |
4326 | | |
4327 | | /* |
4328 | | * we may have to save a backup state here. This is the equivalent |
4329 | | * of handling epsilon transition in NFAs. |
4330 | | */ |
4331 | | if ((CONT != NULL) && |
4332 | | ((CONT->parent == NULL) || |
4333 | | (CONT->parent == (xmlElementContentPtr) 1) || |
4334 | | (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) && |
4335 | | ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) || |
4336 | | (CONT->ocur == XML_ELEMENT_CONTENT_OPT) || |
4337 | | ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) { |
4338 | | if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0) |
4339 | | return(0); |
4340 | | } |
4341 | | |
4342 | | |
4343 | | /* |
4344 | | * Check first if the content matches |
4345 | | */ |
4346 | | switch (CONT->type) { |
4347 | | case XML_ELEMENT_CONTENT_PCDATA: |
4348 | | if (NODE == NULL) { |
4349 | | ret = 0; |
4350 | | break; |
4351 | | } |
4352 | | if (NODE->type == XML_TEXT_NODE) { |
4353 | | /* |
4354 | | * go to next element in the content model |
4355 | | * skipping ignorable elems |
4356 | | */ |
4357 | | do { |
4358 | | NODE = NODE->next; |
4359 | | NODE = xmlValidateSkipIgnorable(NODE); |
4360 | | if ((NODE != NULL) && |
4361 | | (NODE->type == XML_ENTITY_REF_NODE)) |
4362 | | return(-2); |
4363 | | } while ((NODE != NULL) && |
4364 | | ((NODE->type != XML_ELEMENT_NODE) && |
4365 | | (NODE->type != XML_TEXT_NODE) && |
4366 | | (NODE->type != XML_CDATA_SECTION_NODE))); |
4367 | | ret = 1; |
4368 | | break; |
4369 | | } else { |
4370 | | ret = 0; |
4371 | | break; |
4372 | | } |
4373 | | break; |
4374 | | case XML_ELEMENT_CONTENT_ELEMENT: |
4375 | | if (NODE == NULL) { |
4376 | | ret = 0; |
4377 | | break; |
4378 | | } |
4379 | | ret = ((NODE->type == XML_ELEMENT_NODE) && |
4380 | | (xmlStrEqual(NODE->name, CONT->name))); |
4381 | | if (ret == 1) { |
4382 | | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
4383 | | ret = (CONT->prefix == NULL); |
4384 | | } else if (CONT->prefix == NULL) { |
4385 | | ret = 0; |
4386 | | } else { |
4387 | | ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix); |
4388 | | } |
4389 | | } |
4390 | | if (ret == 1) { |
4391 | | /* |
4392 | | * go to next element in the content model |
4393 | | * skipping ignorable elems |
4394 | | */ |
4395 | | do { |
4396 | | NODE = NODE->next; |
4397 | | NODE = xmlValidateSkipIgnorable(NODE); |
4398 | | if ((NODE != NULL) && |
4399 | | (NODE->type == XML_ENTITY_REF_NODE)) |
4400 | | return(-2); |
4401 | | } while ((NODE != NULL) && |
4402 | | ((NODE->type != XML_ELEMENT_NODE) && |
4403 | | (NODE->type != XML_TEXT_NODE) && |
4404 | | (NODE->type != XML_CDATA_SECTION_NODE))); |
4405 | | } else { |
4406 | | ret = 0; |
4407 | | break; |
4408 | | } |
4409 | | break; |
4410 | | case XML_ELEMENT_CONTENT_OR: |
4411 | | /* |
4412 | | * Small optimization. |
4413 | | */ |
4414 | | if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) { |
4415 | | if ((NODE == NULL) || |
4416 | | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
4417 | | DEPTH++; |
4418 | | CONT = CONT->c2; |
4419 | | goto cont; |
4420 | | } |
4421 | | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
4422 | | ret = (CONT->c1->prefix == NULL); |
4423 | | } else if (CONT->c1->prefix == NULL) { |
4424 | | ret = 0; |
4425 | | } else { |
4426 | | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
4427 | | } |
4428 | | if (ret == 0) { |
4429 | | DEPTH++; |
4430 | | CONT = CONT->c2; |
4431 | | goto cont; |
4432 | | } |
4433 | | } |
4434 | | |
4435 | | /* |
4436 | | * save the second branch 'or' branch |
4437 | | */ |
4438 | | if (vstateVPush(ctxt, CONT->c2, NODE, DEPTH + 1, |
4439 | | OCCURS, ROLLBACK_OR) < 0) |
4440 | | return(-1); |
4441 | | DEPTH++; |
4442 | | CONT = CONT->c1; |
4443 | | goto cont; |
4444 | | case XML_ELEMENT_CONTENT_SEQ: |
4445 | | /* |
4446 | | * Small optimization. |
4447 | | */ |
4448 | | if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) && |
4449 | | ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
4450 | | (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
4451 | | if ((NODE == NULL) || |
4452 | | (!xmlStrEqual(NODE->name, CONT->c1->name))) { |
4453 | | DEPTH++; |
4454 | | CONT = CONT->c2; |
4455 | | goto cont; |
4456 | | } |
4457 | | if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) { |
4458 | | ret = (CONT->c1->prefix == NULL); |
4459 | | } else if (CONT->c1->prefix == NULL) { |
4460 | | ret = 0; |
4461 | | } else { |
4462 | | ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix); |
4463 | | } |
4464 | | if (ret == 0) { |
4465 | | DEPTH++; |
4466 | | CONT = CONT->c2; |
4467 | | goto cont; |
4468 | | } |
4469 | | } |
4470 | | DEPTH++; |
4471 | | CONT = CONT->c1; |
4472 | | goto cont; |
4473 | | } |
4474 | | |
4475 | | /* |
4476 | | * At this point handle going up in the tree |
4477 | | */ |
4478 | | if (ret == -1) { |
4479 | | return(ret); |
4480 | | } |
4481 | | analyze: |
4482 | | while (CONT != NULL) { |
4483 | | /* |
4484 | | * First do the analysis depending on the occurrence model at |
4485 | | * this level. |
4486 | | */ |
4487 | | if (ret == 0) { |
4488 | | switch (CONT->ocur) { |
4489 | | xmlNodePtr cur; |
4490 | | |
4491 | | case XML_ELEMENT_CONTENT_ONCE: |
4492 | | cur = ctxt->vstate->node; |
4493 | | if (vstateVPop(ctxt) < 0 ) { |
4494 | | return(0); |
4495 | | } |
4496 | | if (cur != ctxt->vstate->node) |
4497 | | determinist = -3; |
4498 | | goto cont; |
4499 | | case XML_ELEMENT_CONTENT_PLUS: |
4500 | | if (OCCURRENCE == 0) { |
4501 | | cur = ctxt->vstate->node; |
4502 | | if (vstateVPop(ctxt) < 0 ) { |
4503 | | return(0); |
4504 | | } |
4505 | | if (cur != ctxt->vstate->node) |
4506 | | determinist = -3; |
4507 | | goto cont; |
4508 | | } |
4509 | | ret = 1; |
4510 | | break; |
4511 | | case XML_ELEMENT_CONTENT_MULT: |
4512 | | ret = 1; |
4513 | | break; |
4514 | | case XML_ELEMENT_CONTENT_OPT: |
4515 | | ret = 1; |
4516 | | break; |
4517 | | } |
4518 | | } else { |
4519 | | switch (CONT->ocur) { |
4520 | | case XML_ELEMENT_CONTENT_OPT: |
4521 | | ret = 1; |
4522 | | break; |
4523 | | case XML_ELEMENT_CONTENT_ONCE: |
4524 | | ret = 1; |
4525 | | break; |
4526 | | case XML_ELEMENT_CONTENT_PLUS: |
4527 | | if (STATE == ROLLBACK_PARENT) { |
4528 | | ret = 1; |
4529 | | break; |
4530 | | } |
4531 | | if (NODE == NULL) { |
4532 | | ret = 1; |
4533 | | break; |
4534 | | } |
4535 | | SET_OCCURRENCE; |
4536 | | goto cont; |
4537 | | case XML_ELEMENT_CONTENT_MULT: |
4538 | | if (STATE == ROLLBACK_PARENT) { |
4539 | | ret = 1; |
4540 | | break; |
4541 | | } |
4542 | | if (NODE == NULL) { |
4543 | | ret = 1; |
4544 | | break; |
4545 | | } |
4546 | | /* SET_OCCURRENCE; */ |
4547 | | goto cont; |
4548 | | } |
4549 | | } |
4550 | | STATE = 0; |
4551 | | |
4552 | | /* |
4553 | | * Then act accordingly at the parent level |
4554 | | */ |
4555 | | RESET_OCCURRENCE; |
4556 | | if ((CONT->parent == NULL) || |
4557 | | (CONT->parent == (xmlElementContentPtr) 1)) |
4558 | | break; |
4559 | | |
4560 | | switch (CONT->parent->type) { |
4561 | | case XML_ELEMENT_CONTENT_PCDATA: |
4562 | | return(-1); |
4563 | | case XML_ELEMENT_CONTENT_ELEMENT: |
4564 | | return(-1); |
4565 | | case XML_ELEMENT_CONTENT_OR: |
4566 | | if (ret == 1) { |
4567 | | CONT = CONT->parent; |
4568 | | DEPTH--; |
4569 | | } else { |
4570 | | CONT = CONT->parent; |
4571 | | DEPTH--; |
4572 | | } |
4573 | | break; |
4574 | | case XML_ELEMENT_CONTENT_SEQ: |
4575 | | if (ret == 0) { |
4576 | | CONT = CONT->parent; |
4577 | | DEPTH--; |
4578 | | } else if (CONT == CONT->parent->c1) { |
4579 | | CONT = CONT->parent->c2; |
4580 | | goto cont; |
4581 | | } else { |
4582 | | CONT = CONT->parent; |
4583 | | DEPTH--; |
4584 | | } |
4585 | | } |
4586 | | } |
4587 | | if (NODE != NULL) { |
4588 | | xmlNodePtr cur; |
4589 | | |
4590 | | cur = ctxt->vstate->node; |
4591 | | if (vstateVPop(ctxt) < 0 ) { |
4592 | | return(0); |
4593 | | } |
4594 | | if (cur != ctxt->vstate->node) |
4595 | | determinist = -3; |
4596 | | goto cont; |
4597 | | } |
4598 | | if (ret == 0) { |
4599 | | xmlNodePtr cur; |
4600 | | |
4601 | | cur = ctxt->vstate->node; |
4602 | | if (vstateVPop(ctxt) < 0 ) { |
4603 | | return(0); |
4604 | | } |
4605 | | if (cur != ctxt->vstate->node) |
4606 | | determinist = -3; |
4607 | | goto cont; |
4608 | | } |
4609 | | return(determinist); |
4610 | | } |
4611 | | #endif |
4612 | | |
4613 | | /** |
4614 | | * This will dump the list of elements to the buffer |
4615 | | * Intended just for the debug routine |
4616 | | * |
4617 | | * @param buf an output buffer |
4618 | | * @param size the size of the buffer |
4619 | | * @param node an element |
4620 | | * @param glob 1 if one must print the englobing parenthesis, 0 otherwise |
4621 | | */ |
4622 | | static void |
4623 | | xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) { |
4624 | | xmlNodePtr cur; |
4625 | | int len; |
4626 | | |
4627 | | if (node == NULL) return; |
4628 | | if (glob) strcat(buf, "("); |
4629 | | cur = node; |
4630 | | while (cur != NULL) { |
4631 | | len = strlen(buf); |
4632 | | if (size - len < 50) { |
4633 | | if ((size - len > 4) && (buf[len - 1] != '.')) |
4634 | | strcat(buf, " ..."); |
4635 | | return; |
4636 | | } |
4637 | | switch (cur->type) { |
4638 | | case XML_ELEMENT_NODE: { |
4639 | | int qnameLen = xmlStrlen(cur->name); |
4640 | | |
4641 | | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) |
4642 | | qnameLen += xmlStrlen(cur->ns->prefix) + 1; |
4643 | | if (size - len < qnameLen + 10) { |
4644 | | if ((size - len > 4) && (buf[len - 1] != '.')) |
4645 | | strcat(buf, " ..."); |
4646 | | return; |
4647 | | } |
4648 | | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
4649 | | strcat(buf, (char *) cur->ns->prefix); |
4650 | | strcat(buf, ":"); |
4651 | | } |
4652 | | if (cur->name != NULL) |
4653 | | strcat(buf, (char *) cur->name); |
4654 | | if (cur->next != NULL) |
4655 | | strcat(buf, " "); |
4656 | | break; |
4657 | | } |
4658 | | case XML_TEXT_NODE: |
4659 | | if (xmlIsBlankNode(cur)) |
4660 | | break; |
4661 | | /* Falls through. */ |
4662 | | case XML_CDATA_SECTION_NODE: |
4663 | | case XML_ENTITY_REF_NODE: |
4664 | | strcat(buf, "CDATA"); |
4665 | | if (cur->next != NULL) |
4666 | | strcat(buf, " "); |
4667 | | break; |
4668 | | case XML_ATTRIBUTE_NODE: |
4669 | | case XML_DOCUMENT_NODE: |
4670 | | case XML_HTML_DOCUMENT_NODE: |
4671 | | case XML_DOCUMENT_TYPE_NODE: |
4672 | | case XML_DOCUMENT_FRAG_NODE: |
4673 | | case XML_NOTATION_NODE: |
4674 | | case XML_NAMESPACE_DECL: |
4675 | | strcat(buf, "???"); |
4676 | | if (cur->next != NULL) |
4677 | | strcat(buf, " "); |
4678 | | break; |
4679 | | case XML_ENTITY_NODE: |
4680 | | case XML_PI_NODE: |
4681 | | case XML_DTD_NODE: |
4682 | | case XML_COMMENT_NODE: |
4683 | | case XML_ELEMENT_DECL: |
4684 | | case XML_ATTRIBUTE_DECL: |
4685 | | case XML_ENTITY_DECL: |
4686 | | case XML_XINCLUDE_START: |
4687 | | case XML_XINCLUDE_END: |
4688 | | break; |
4689 | | } |
4690 | | cur = cur->next; |
4691 | | } |
4692 | | if (glob) strcat(buf, ")"); |
4693 | | } |
4694 | | |
4695 | | /** |
4696 | | * Try to validate the content model of an element |
4697 | | * |
4698 | | * @param ctxt the validation context |
4699 | | * @param child the child list |
4700 | | * @param elemDecl pointer to the element declaration |
4701 | | * @param warn emit the error message |
4702 | | * @param parent the parent element (for error reporting) |
4703 | | * @returns 1 if valid or 0 if not and -1 in case of error |
4704 | | */ |
4705 | | |
4706 | | static int |
4707 | | xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child, |
4708 | | xmlElementPtr elemDecl, int warn, xmlNodePtr parent) { |
4709 | | int ret = 1; |
4710 | | #ifndef LIBXML_REGEXP_ENABLED |
4711 | | xmlNodePtr repl = NULL, last = NULL, tmp; |
4712 | | #endif |
4713 | | xmlNodePtr cur; |
4714 | | xmlElementContentPtr cont; |
4715 | | const xmlChar *name; |
4716 | | |
4717 | | if ((elemDecl == NULL) || (parent == NULL) || (ctxt == NULL)) |
4718 | | return(-1); |
4719 | | cont = elemDecl->content; |
4720 | | name = elemDecl->name; |
4721 | | |
4722 | | #ifdef LIBXML_REGEXP_ENABLED |
4723 | | /* Build the regexp associated to the content model */ |
4724 | | if (elemDecl->contModel == NULL) |
4725 | | ret = xmlValidBuildContentModel(ctxt, elemDecl); |
4726 | | if (elemDecl->contModel == NULL) { |
4727 | | return(-1); |
4728 | | } else { |
4729 | | xmlRegExecCtxtPtr exec; |
4730 | | |
4731 | | if (!xmlRegexpIsDeterminist(elemDecl->contModel)) { |
4732 | | return(-1); |
4733 | | } |
4734 | | ctxt->nodeMax = 0; |
4735 | | ctxt->nodeNr = 0; |
4736 | | ctxt->nodeTab = NULL; |
4737 | | exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL); |
4738 | | if (exec == NULL) { |
4739 | | xmlVErrMemory(ctxt); |
4740 | | return(-1); |
4741 | | } |
4742 | | cur = child; |
4743 | | while (cur != NULL) { |
4744 | | switch (cur->type) { |
4745 | | case XML_ENTITY_REF_NODE: |
4746 | | /* |
4747 | | * Push the current node to be able to roll back |
4748 | | * and process within the entity |
4749 | | */ |
4750 | | if ((cur->children != NULL) && |
4751 | | (cur->children->children != NULL)) { |
4752 | | if (nodeVPush(ctxt, cur) < 0) { |
4753 | | ret = -1; |
4754 | | goto fail; |
4755 | | } |
4756 | | cur = cur->children->children; |
4757 | | continue; |
4758 | | } |
4759 | | break; |
4760 | | case XML_TEXT_NODE: |
4761 | | if (xmlIsBlankNode(cur)) |
4762 | | break; |
4763 | | ret = 0; |
4764 | | goto fail; |
4765 | | case XML_CDATA_SECTION_NODE: |
4766 | | /* TODO */ |
4767 | | ret = 0; |
4768 | | goto fail; |
4769 | | case XML_ELEMENT_NODE: |
4770 | | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
4771 | | xmlChar fn[50]; |
4772 | | xmlChar *fullname; |
4773 | | |
4774 | | fullname = xmlBuildQName(cur->name, |
4775 | | cur->ns->prefix, fn, 50); |
4776 | | if (fullname == NULL) { |
4777 | | xmlVErrMemory(ctxt); |
4778 | | ret = -1; |
4779 | | goto fail; |
4780 | | } |
4781 | | ret = xmlRegExecPushString(exec, fullname, NULL); |
4782 | | if ((fullname != fn) && (fullname != cur->name)) |
4783 | | xmlFree(fullname); |
4784 | | } else { |
4785 | | ret = xmlRegExecPushString(exec, cur->name, NULL); |
4786 | | } |
4787 | | break; |
4788 | | default: |
4789 | | break; |
4790 | | } |
4791 | | if (ret == XML_REGEXP_OUT_OF_MEMORY) |
4792 | | xmlVErrMemory(ctxt); |
4793 | | /* |
4794 | | * Switch to next element |
4795 | | */ |
4796 | | cur = cur->next; |
4797 | | while (cur == NULL) { |
4798 | | cur = nodeVPop(ctxt); |
4799 | | if (cur == NULL) |
4800 | | break; |
4801 | | cur = cur->next; |
4802 | | } |
4803 | | } |
4804 | | ret = xmlRegExecPushString(exec, NULL, NULL); |
4805 | | if (ret == XML_REGEXP_OUT_OF_MEMORY) |
4806 | | xmlVErrMemory(ctxt); |
4807 | | fail: |
4808 | | xmlRegFreeExecCtxt(exec); |
4809 | | } |
4810 | | #else /* LIBXML_REGEXP_ENABLED */ |
4811 | | /* |
4812 | | * Allocate the stack |
4813 | | */ |
4814 | | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
4815 | | ctxt->vstateMax = 8; |
4816 | | #else |
4817 | | ctxt->vstateMax = 1; |
4818 | | #endif |
4819 | | ctxt->vstateTab = xmlMalloc(ctxt->vstateMax * sizeof(ctxt->vstateTab[0])); |
4820 | | if (ctxt->vstateTab == NULL) { |
4821 | | xmlVErrMemory(ctxt); |
4822 | | return(-1); |
4823 | | } |
4824 | | /* |
4825 | | * The first entry in the stack is reserved to the current state |
4826 | | */ |
4827 | | ctxt->nodeMax = 0; |
4828 | | ctxt->nodeNr = 0; |
4829 | | ctxt->nodeTab = NULL; |
4830 | | ctxt->vstate = &ctxt->vstateTab[0]; |
4831 | | ctxt->vstateNr = 1; |
4832 | | CONT = cont; |
4833 | | NODE = child; |
4834 | | DEPTH = 0; |
4835 | | OCCURS = 0; |
4836 | | STATE = 0; |
4837 | | ret = xmlValidateElementType(ctxt); |
4838 | | if ((ret == -3) && (warn)) { |
4839 | | char expr[5000]; |
4840 | | expr[0] = 0; |
4841 | | xmlSnprintfElementContent(expr, 5000, elemDecl->content, 1); |
4842 | | xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl, |
4843 | | XML_DTD_CONTENT_NOT_DETERMINIST, |
4844 | | "Content model of %s is not deterministic: %s\n", |
4845 | | name, BAD_CAST expr, NULL); |
4846 | | } else if (ret == -2) { |
4847 | | /* |
4848 | | * An entities reference appeared at this level. |
4849 | | * Build a minimal representation of this node content |
4850 | | * sufficient to run the validation process on it |
4851 | | */ |
4852 | | cur = child; |
4853 | | while (cur != NULL) { |
4854 | | switch (cur->type) { |
4855 | | case XML_ENTITY_REF_NODE: |
4856 | | /* |
4857 | | * Push the current node to be able to roll back |
4858 | | * and process within the entity |
4859 | | */ |
4860 | | if ((cur->children != NULL) && |
4861 | | (cur->children->children != NULL)) { |
4862 | | if (nodeVPush(ctxt, cur) < 0) { |
4863 | | xmlFreeNodeList(repl); |
4864 | | ret = -1; |
4865 | | goto done; |
4866 | | } |
4867 | | cur = cur->children->children; |
4868 | | continue; |
4869 | | } |
4870 | | break; |
4871 | | case XML_TEXT_NODE: |
4872 | | if (xmlIsBlankNode(cur)) |
4873 | | break; |
4874 | | /* falls through */ |
4875 | | case XML_CDATA_SECTION_NODE: |
4876 | | case XML_ELEMENT_NODE: |
4877 | | /* |
4878 | | * Allocate a new node and minimally fills in |
4879 | | * what's required |
4880 | | */ |
4881 | | tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
4882 | | if (tmp == NULL) { |
4883 | | xmlVErrMemory(ctxt); |
4884 | | xmlFreeNodeList(repl); |
4885 | | ret = -1; |
4886 | | goto done; |
4887 | | } |
4888 | | tmp->type = cur->type; |
4889 | | tmp->name = cur->name; |
4890 | | tmp->ns = cur->ns; |
4891 | | tmp->next = NULL; |
4892 | | tmp->content = NULL; |
4893 | | if (repl == NULL) |
4894 | | repl = last = tmp; |
4895 | | else { |
4896 | | last->next = tmp; |
4897 | | last = tmp; |
4898 | | } |
4899 | | if (cur->type == XML_CDATA_SECTION_NODE) { |
4900 | | /* |
4901 | | * E59 spaces in CDATA does not match the |
4902 | | * nonterminal S |
4903 | | */ |
4904 | | tmp->content = xmlStrdup(BAD_CAST "CDATA"); |
4905 | | } |
4906 | | break; |
4907 | | default: |
4908 | | break; |
4909 | | } |
4910 | | /* |
4911 | | * Switch to next element |
4912 | | */ |
4913 | | cur = cur->next; |
4914 | | while (cur == NULL) { |
4915 | | cur = nodeVPop(ctxt); |
4916 | | if (cur == NULL) |
4917 | | break; |
4918 | | cur = cur->next; |
4919 | | } |
4920 | | } |
4921 | | |
4922 | | /* |
4923 | | * Relaunch the validation |
4924 | | */ |
4925 | | ctxt->vstate = &ctxt->vstateTab[0]; |
4926 | | ctxt->vstateNr = 1; |
4927 | | CONT = cont; |
4928 | | NODE = repl; |
4929 | | DEPTH = 0; |
4930 | | OCCURS = 0; |
4931 | | STATE = 0; |
4932 | | ret = xmlValidateElementType(ctxt); |
4933 | | } |
4934 | | #endif /* LIBXML_REGEXP_ENABLED */ |
4935 | | if ((warn) && ((ret != 1) && (ret != -3))) { |
4936 | | if (ctxt != NULL) { |
4937 | | char expr[5000]; |
4938 | | char list[5000]; |
4939 | | |
4940 | | expr[0] = 0; |
4941 | | xmlSnprintfElementContent(&expr[0], 5000, cont, 1); |
4942 | | list[0] = 0; |
4943 | | #ifndef LIBXML_REGEXP_ENABLED |
4944 | | if (repl != NULL) |
4945 | | xmlSnprintfElements(&list[0], 5000, repl, 1); |
4946 | | else |
4947 | | #endif /* LIBXML_REGEXP_ENABLED */ |
4948 | | xmlSnprintfElements(&list[0], 5000, child, 1); |
4949 | | |
4950 | | if (name != NULL) { |
4951 | | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
4952 | | "Element %s content does not follow the DTD, expecting %s, got %s\n", |
4953 | | name, BAD_CAST expr, BAD_CAST list); |
4954 | | } else { |
4955 | | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
4956 | | "Element content does not follow the DTD, expecting %s, got %s\n", |
4957 | | BAD_CAST expr, BAD_CAST list, NULL); |
4958 | | } |
4959 | | } else { |
4960 | | if (name != NULL) { |
4961 | | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
4962 | | "Element %s content does not follow the DTD\n", |
4963 | | name, NULL, NULL); |
4964 | | } else { |
4965 | | xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL, |
4966 | | "Element content does not follow the DTD\n", |
4967 | | NULL, NULL, NULL); |
4968 | | } |
4969 | | } |
4970 | | ret = 0; |
4971 | | } |
4972 | | if (ret == -3) |
4973 | | ret = 1; |
4974 | | |
4975 | | #ifndef LIBXML_REGEXP_ENABLED |
4976 | | done: |
4977 | | /* |
4978 | | * Deallocate the copy if done, and free up the validation stack |
4979 | | */ |
4980 | | while (repl != NULL) { |
4981 | | tmp = repl->next; |
4982 | | xmlFree(repl); |
4983 | | repl = tmp; |
4984 | | } |
4985 | | ctxt->vstateMax = 0; |
4986 | | if (ctxt->vstateTab != NULL) { |
4987 | | xmlFree(ctxt->vstateTab); |
4988 | | ctxt->vstateTab = NULL; |
4989 | | } |
4990 | | #endif |
4991 | | ctxt->nodeMax = 0; |
4992 | | ctxt->nodeNr = 0; |
4993 | | if (ctxt->nodeTab != NULL) { |
4994 | | xmlFree(ctxt->nodeTab); |
4995 | | ctxt->nodeTab = NULL; |
4996 | | } |
4997 | | return(ret); |
4998 | | |
4999 | | } |
5000 | | |
5001 | | /** |
5002 | | * Check that an element follows \#CDATA. |
5003 | | * |
5004 | | * @param ctxt the validation context |
5005 | | * @param doc a document instance |
5006 | | * @param elem an element instance |
5007 | | * @returns 1 if valid or 0 otherwise. |
5008 | | */ |
5009 | | static int |
5010 | | xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
5011 | | xmlNodePtr elem) { |
5012 | | int ret = 1; |
5013 | | xmlNodePtr cur, child; |
5014 | | |
5015 | | if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) || |
5016 | | (elem->type != XML_ELEMENT_NODE)) |
5017 | | return(0); |
5018 | | |
5019 | | child = elem->children; |
5020 | | |
5021 | | cur = child; |
5022 | | while (cur != NULL) { |
5023 | | switch (cur->type) { |
5024 | | case XML_ENTITY_REF_NODE: |
5025 | | /* |
5026 | | * Push the current node to be able to roll back |
5027 | | * and process within the entity |
5028 | | */ |
5029 | | if ((cur->children != NULL) && |
5030 | | (cur->children->children != NULL)) { |
5031 | | if (nodeVPush(ctxt, cur) < 0) { |
5032 | | ret = 0; |
5033 | | goto done; |
5034 | | } |
5035 | | cur = cur->children->children; |
5036 | | continue; |
5037 | | } |
5038 | | break; |
5039 | | case XML_COMMENT_NODE: |
5040 | | case XML_PI_NODE: |
5041 | | case XML_TEXT_NODE: |
5042 | | case XML_CDATA_SECTION_NODE: |
5043 | | break; |
5044 | | default: |
5045 | | ret = 0; |
5046 | | goto done; |
5047 | | } |
5048 | | /* |
5049 | | * Switch to next element |
5050 | | */ |
5051 | | cur = cur->next; |
5052 | | while (cur == NULL) { |
5053 | | cur = nodeVPop(ctxt); |
5054 | | if (cur == NULL) |
5055 | | break; |
5056 | | cur = cur->next; |
5057 | | } |
5058 | | } |
5059 | | done: |
5060 | | ctxt->nodeMax = 0; |
5061 | | ctxt->nodeNr = 0; |
5062 | | if (ctxt->nodeTab != NULL) { |
5063 | | xmlFree(ctxt->nodeTab); |
5064 | | ctxt->nodeTab = NULL; |
5065 | | } |
5066 | | return(ret); |
5067 | | } |
5068 | | |
5069 | | #ifdef LIBXML_REGEXP_ENABLED |
5070 | | /** |
5071 | | * Check if the given node is part of the content model. |
5072 | | * |
5073 | | * @param ctxt the validation context |
5074 | | * @param cont the mixed content model |
5075 | | * @param qname the qualified name as appearing in the serialization |
5076 | | * @returns 1 if yes, 0 if no, -1 in case of error |
5077 | | */ |
5078 | | static int |
5079 | | xmlValidateCheckMixed(xmlValidCtxtPtr ctxt, |
5080 | | xmlElementContentPtr cont, const xmlChar *qname) { |
5081 | | const xmlChar *name; |
5082 | | int plen; |
5083 | | name = xmlSplitQName3(qname, &plen); |
5084 | | |
5085 | | if (name == NULL) { |
5086 | | while (cont != NULL) { |
5087 | | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
5088 | | if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname))) |
5089 | | return(1); |
5090 | | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
5091 | | (cont->c1 != NULL) && |
5092 | | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
5093 | | if ((cont->c1->prefix == NULL) && |
5094 | | (xmlStrEqual(cont->c1->name, qname))) |
5095 | | return(1); |
5096 | | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
5097 | | (cont->c1 == NULL) || |
5098 | | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
5099 | | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
5100 | | "Internal: MIXED struct corrupted\n", |
5101 | | NULL); |
5102 | | break; |
5103 | | } |
5104 | | cont = cont->c2; |
5105 | | } |
5106 | | } else { |
5107 | | while (cont != NULL) { |
5108 | | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
5109 | | if ((cont->prefix != NULL) && |
5110 | | (xmlStrncmp(cont->prefix, qname, plen) == 0) && |
5111 | | (xmlStrEqual(cont->name, name))) |
5112 | | return(1); |
5113 | | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
5114 | | (cont->c1 != NULL) && |
5115 | | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
5116 | | if ((cont->c1->prefix != NULL) && |
5117 | | (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) && |
5118 | | (xmlStrEqual(cont->c1->name, name))) |
5119 | | return(1); |
5120 | | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
5121 | | (cont->c1 == NULL) || |
5122 | | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
5123 | | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
5124 | | "Internal: MIXED struct corrupted\n", |
5125 | | NULL); |
5126 | | break; |
5127 | | } |
5128 | | cont = cont->c2; |
5129 | | } |
5130 | | } |
5131 | | return(0); |
5132 | | } |
5133 | | #endif /* LIBXML_REGEXP_ENABLED */ |
5134 | | |
5135 | | /** |
5136 | | * Finds a declaration associated to an element in the document. |
5137 | | * |
5138 | | * @param ctxt the validation context |
5139 | | * @param doc a document instance |
5140 | | * @param elem an element instance |
5141 | | * @param extsubset pointer, (out) indicate if the declaration was found |
5142 | | * in the external subset. |
5143 | | * @returns the pointer to the declaration or NULL if not found. |
5144 | | */ |
5145 | | static xmlElementPtr |
5146 | | xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc, |
5147 | | xmlNodePtr elem, int *extsubset) { |
5148 | | xmlElementPtr elemDecl = NULL; |
5149 | | const xmlChar *prefix = NULL; |
5150 | | |
5151 | | if ((ctxt == NULL) || (doc == NULL) || |
5152 | | (elem == NULL) || (elem->name == NULL)) |
5153 | | return(NULL); |
5154 | | if (extsubset != NULL) |
5155 | | *extsubset = 0; |
5156 | | |
5157 | | /* |
5158 | | * Fetch the declaration for the qualified name |
5159 | | */ |
5160 | | if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) |
5161 | | prefix = elem->ns->prefix; |
5162 | | |
5163 | | if (prefix != NULL) { |
5164 | | elemDecl = xmlGetDtdQElementDesc(doc->intSubset, |
5165 | | elem->name, prefix); |
5166 | | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
5167 | | elemDecl = xmlGetDtdQElementDesc(doc->extSubset, |
5168 | | elem->name, prefix); |
5169 | | if ((elemDecl != NULL) && (extsubset != NULL)) |
5170 | | *extsubset = 1; |
5171 | | } |
5172 | | } |
5173 | | |
5174 | | /* |
5175 | | * Fetch the declaration for the non qualified name |
5176 | | * This is "non-strict" validation should be done on the |
5177 | | * full QName but in that case being flexible makes sense. |
5178 | | */ |
5179 | | if (elemDecl == NULL) { |
5180 | | elemDecl = xmlGetDtdQElementDesc(doc->intSubset, elem->name, NULL); |
5181 | | if ((elemDecl == NULL) && (doc->extSubset != NULL)) { |
5182 | | elemDecl = xmlGetDtdQElementDesc(doc->extSubset, elem->name, NULL); |
5183 | | if ((elemDecl != NULL) && (extsubset != NULL)) |
5184 | | *extsubset = 1; |
5185 | | } |
5186 | | } |
5187 | | if (elemDecl == NULL) { |
5188 | | xmlErrValidNode(ctxt, elem, |
5189 | | XML_DTD_UNKNOWN_ELEM, |
5190 | | "No declaration for element %s\n", |
5191 | | elem->name, NULL, NULL); |
5192 | | } |
5193 | | return(elemDecl); |
5194 | | } |
5195 | | |
5196 | | #ifdef LIBXML_REGEXP_ENABLED |
5197 | | /** |
5198 | | * Push a new element start on the validation stack. |
5199 | | * |
5200 | | * @deprecated Internal function, don't use. |
5201 | | * |
5202 | | * @param ctxt the validation context |
5203 | | * @param doc a document instance |
5204 | | * @param elem an element instance |
5205 | | * @param qname the qualified name as appearing in the serialization |
5206 | | * @returns 1 if no validation problem was found or 0 otherwise. |
5207 | | */ |
5208 | | int |
5209 | | xmlValidatePushElement(xmlValidCtxt *ctxt, xmlDoc *doc, |
5210 | | xmlNode *elem, const xmlChar *qname) { |
5211 | | int ret = 1; |
5212 | | xmlElementPtr eDecl; |
5213 | | int extsubset = 0; |
5214 | | |
5215 | | if (ctxt == NULL) |
5216 | | return(0); |
5217 | | |
5218 | | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
5219 | | xmlValidStatePtr state = ctxt->vstate; |
5220 | | xmlElementPtr elemDecl; |
5221 | | |
5222 | | /* |
5223 | | * Check the new element against the content model of the new elem. |
5224 | | */ |
5225 | | if (state->elemDecl != NULL) { |
5226 | | elemDecl = state->elemDecl; |
5227 | | |
5228 | | switch(elemDecl->etype) { |
5229 | | case XML_ELEMENT_TYPE_UNDEFINED: |
5230 | | ret = 0; |
5231 | | break; |
5232 | | case XML_ELEMENT_TYPE_EMPTY: |
5233 | | xmlErrValidNode(ctxt, state->node, |
5234 | | XML_DTD_NOT_EMPTY, |
5235 | | "Element %s was declared EMPTY this one has content\n", |
5236 | | state->node->name, NULL, NULL); |
5237 | | ret = 0; |
5238 | | break; |
5239 | | case XML_ELEMENT_TYPE_ANY: |
5240 | | /* I don't think anything is required then */ |
5241 | | break; |
5242 | | case XML_ELEMENT_TYPE_MIXED: |
5243 | | /* simple case of declared as #PCDATA */ |
5244 | | if ((elemDecl->content != NULL) && |
5245 | | (elemDecl->content->type == |
5246 | | XML_ELEMENT_CONTENT_PCDATA)) { |
5247 | | xmlErrValidNode(ctxt, state->node, |
5248 | | XML_DTD_NOT_PCDATA, |
5249 | | "Element %s was declared #PCDATA but contains non text nodes\n", |
5250 | | state->node->name, NULL, NULL); |
5251 | | ret = 0; |
5252 | | } else { |
5253 | | ret = xmlValidateCheckMixed(ctxt, elemDecl->content, |
5254 | | qname); |
5255 | | if (ret != 1) { |
5256 | | xmlErrValidNode(ctxt, state->node, |
5257 | | XML_DTD_INVALID_CHILD, |
5258 | | "Element %s is not declared in %s list of possible children\n", |
5259 | | qname, state->node->name, NULL); |
5260 | | } |
5261 | | } |
5262 | | break; |
5263 | | case XML_ELEMENT_TYPE_ELEMENT: |
5264 | | /* |
5265 | | * TODO: |
5266 | | * VC: Standalone Document Declaration |
5267 | | * - element types with element content, if white space |
5268 | | * occurs directly within any instance of those types. |
5269 | | */ |
5270 | | if (state->exec != NULL) { |
5271 | | ret = xmlRegExecPushString(state->exec, qname, NULL); |
5272 | | if (ret == XML_REGEXP_OUT_OF_MEMORY) { |
5273 | | xmlVErrMemory(ctxt); |
5274 | | return(0); |
5275 | | } |
5276 | | if (ret < 0) { |
5277 | | xmlErrValidNode(ctxt, state->node, |
5278 | | XML_DTD_CONTENT_MODEL, |
5279 | | "Element %s content does not follow the DTD, Misplaced %s\n", |
5280 | | state->node->name, qname, NULL); |
5281 | | ret = 0; |
5282 | | } else { |
5283 | | ret = 1; |
5284 | | } |
5285 | | } |
5286 | | break; |
5287 | | } |
5288 | | } |
5289 | | } |
5290 | | eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
5291 | | vstateVPush(ctxt, eDecl, elem); |
5292 | | return(ret); |
5293 | | } |
5294 | | |
5295 | | /** |
5296 | | * Check the CData parsed for validation in the current stack. |
5297 | | * |
5298 | | * @deprecated Internal function, don't use. |
5299 | | * |
5300 | | * @param ctxt the validation context |
5301 | | * @param data some character data read |
5302 | | * @param len the length of the data |
5303 | | * @returns 1 if no validation problem was found or 0 otherwise. |
5304 | | */ |
5305 | | int |
5306 | | xmlValidatePushCData(xmlValidCtxt *ctxt, const xmlChar *data, int len) { |
5307 | | int ret = 1; |
5308 | | |
5309 | | if (ctxt == NULL) |
5310 | | return(0); |
5311 | | if (len <= 0) |
5312 | | return(ret); |
5313 | | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
5314 | | xmlValidStatePtr state = ctxt->vstate; |
5315 | | xmlElementPtr elemDecl; |
5316 | | |
5317 | | /* |
5318 | | * Check the new element against the content model of the new elem. |
5319 | | */ |
5320 | | if (state->elemDecl != NULL) { |
5321 | | elemDecl = state->elemDecl; |
5322 | | |
5323 | | switch(elemDecl->etype) { |
5324 | | case XML_ELEMENT_TYPE_UNDEFINED: |
5325 | | ret = 0; |
5326 | | break; |
5327 | | case XML_ELEMENT_TYPE_EMPTY: |
5328 | | xmlErrValidNode(ctxt, state->node, |
5329 | | XML_DTD_NOT_EMPTY, |
5330 | | "Element %s was declared EMPTY this one has content\n", |
5331 | | state->node->name, NULL, NULL); |
5332 | | ret = 0; |
5333 | | break; |
5334 | | case XML_ELEMENT_TYPE_ANY: |
5335 | | break; |
5336 | | case XML_ELEMENT_TYPE_MIXED: |
5337 | | break; |
5338 | | case XML_ELEMENT_TYPE_ELEMENT: { |
5339 | | int i; |
5340 | | |
5341 | | for (i = 0;i < len;i++) { |
5342 | | if (!IS_BLANK_CH(data[i])) { |
5343 | | xmlErrValidNode(ctxt, state->node, |
5344 | | XML_DTD_CONTENT_MODEL, |
5345 | | "Element %s content does not follow the DTD, Text not allowed\n", |
5346 | | state->node->name, NULL, NULL); |
5347 | | ret = 0; |
5348 | | goto done; |
5349 | | } |
5350 | | } |
5351 | | /* |
5352 | | * TODO: |
5353 | | * VC: Standalone Document Declaration |
5354 | | * element types with element content, if white space |
5355 | | * occurs directly within any instance of those types. |
5356 | | */ |
5357 | | break; |
5358 | | } |
5359 | | } |
5360 | | } |
5361 | | } |
5362 | | done: |
5363 | | return(ret); |
5364 | | } |
5365 | | |
5366 | | /** |
5367 | | * Pop the element end from the validation stack. |
5368 | | * |
5369 | | * @deprecated Internal function, don't use. |
5370 | | * |
5371 | | * @param ctxt the validation context |
5372 | | * @param doc a document instance |
5373 | | * @param elem an element instance |
5374 | | * @param qname the qualified name as appearing in the serialization |
5375 | | * @returns 1 if no validation problem was found or 0 otherwise. |
5376 | | */ |
5377 | | int |
5378 | | xmlValidatePopElement(xmlValidCtxt *ctxt, xmlDoc *doc ATTRIBUTE_UNUSED, |
5379 | | xmlNode *elem ATTRIBUTE_UNUSED, |
5380 | | const xmlChar *qname ATTRIBUTE_UNUSED) { |
5381 | | int ret = 1; |
5382 | | |
5383 | | if (ctxt == NULL) |
5384 | | return(0); |
5385 | | |
5386 | | if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) { |
5387 | | xmlValidStatePtr state = ctxt->vstate; |
5388 | | xmlElementPtr elemDecl; |
5389 | | |
5390 | | /* |
5391 | | * Check the new element against the content model of the new elem. |
5392 | | */ |
5393 | | if (state->elemDecl != NULL) { |
5394 | | elemDecl = state->elemDecl; |
5395 | | |
5396 | | if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) { |
5397 | | if (state->exec != NULL) { |
5398 | | ret = xmlRegExecPushString(state->exec, NULL, NULL); |
5399 | | if (ret <= 0) { |
5400 | | if (ret == XML_REGEXP_OUT_OF_MEMORY) |
5401 | | xmlVErrMemory(ctxt); |
5402 | | else |
5403 | | xmlErrValidNode(ctxt, state->node, |
5404 | | XML_DTD_CONTENT_MODEL, |
5405 | | "Element %s content does not follow the DTD, Expecting more children\n", |
5406 | | state->node->name, NULL,NULL); |
5407 | | ret = 0; |
5408 | | } else { |
5409 | | /* |
5410 | | * previous validation errors should not generate |
5411 | | * a new one here |
5412 | | */ |
5413 | | ret = 1; |
5414 | | } |
5415 | | } |
5416 | | } |
5417 | | } |
5418 | | vstateVPop(ctxt); |
5419 | | } |
5420 | | return(ret); |
5421 | | } |
5422 | | #endif /* LIBXML_REGEXP_ENABLED */ |
5423 | | |
5424 | | /** |
5425 | | * Try to validate a single element and its attributes. |
5426 | | * Performs the following checks as described by the |
5427 | | * XML-1.0 recommendation: |
5428 | | * |
5429 | | * @deprecated Internal function, don't use. |
5430 | | * |
5431 | | * - [ VC: Element Valid ] |
5432 | | * - [ VC: Required Attribute ] |
5433 | | * |
5434 | | * Then calls #xmlValidateOneAttribute for each attribute present. |
5435 | | * |
5436 | | * ID/IDREF checks are handled separately. |
5437 | | * |
5438 | | * @param ctxt the validation context |
5439 | | * @param doc a document instance |
5440 | | * @param elem an element instance |
5441 | | * @returns 1 if valid or 0 otherwise. |
5442 | | */ |
5443 | | |
5444 | | int |
5445 | | xmlValidateOneElement(xmlValidCtxt *ctxt, xmlDoc *doc, |
5446 | | xmlNode *elem) { |
5447 | | xmlElementPtr elemDecl = NULL; |
5448 | | xmlElementContentPtr cont; |
5449 | | xmlAttributePtr attr; |
5450 | | xmlNodePtr child; |
5451 | | int ret = 1, tmp; |
5452 | | const xmlChar *name; |
5453 | | int extsubset = 0; |
5454 | | |
5455 | | CHECK_DTD; |
5456 | | |
5457 | | if (elem == NULL) return(0); |
5458 | | switch (elem->type) { |
5459 | | case XML_TEXT_NODE: |
5460 | | case XML_CDATA_SECTION_NODE: |
5461 | | case XML_ENTITY_REF_NODE: |
5462 | | case XML_PI_NODE: |
5463 | | case XML_COMMENT_NODE: |
5464 | | case XML_XINCLUDE_START: |
5465 | | case XML_XINCLUDE_END: |
5466 | | return(1); |
5467 | | case XML_ELEMENT_NODE: |
5468 | | break; |
5469 | | default: |
5470 | | xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR, |
5471 | | "unexpected element type\n", NULL, NULL ,NULL); |
5472 | | return(0); |
5473 | | } |
5474 | | |
5475 | | /* |
5476 | | * Fetch the declaration |
5477 | | */ |
5478 | | elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset); |
5479 | | if (elemDecl == NULL) |
5480 | | return(0); |
5481 | | |
5482 | | /* |
5483 | | * If vstateNr is not zero that means continuous validation is |
5484 | | * activated, do not try to check the content model at that level. |
5485 | | */ |
5486 | | if (ctxt->vstateNr == 0) { |
5487 | | /* Check that the element content matches the definition */ |
5488 | | switch (elemDecl->etype) { |
5489 | | case XML_ELEMENT_TYPE_UNDEFINED: |
5490 | | xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM, |
5491 | | "No declaration for element %s\n", |
5492 | | elem->name, NULL, NULL); |
5493 | | return(0); |
5494 | | case XML_ELEMENT_TYPE_EMPTY: |
5495 | | if (elem->children != NULL) { |
5496 | | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY, |
5497 | | "Element %s was declared EMPTY this one has content\n", |
5498 | | elem->name, NULL, NULL); |
5499 | | ret = 0; |
5500 | | } |
5501 | | break; |
5502 | | case XML_ELEMENT_TYPE_ANY: |
5503 | | /* I don't think anything is required then */ |
5504 | | break; |
5505 | | case XML_ELEMENT_TYPE_MIXED: |
5506 | | |
5507 | | /* simple case of declared as #PCDATA */ |
5508 | | if ((elemDecl->content != NULL) && |
5509 | | (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) { |
5510 | | ret = xmlValidateOneCdataElement(ctxt, doc, elem); |
5511 | | if (!ret) { |
5512 | | xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA, |
5513 | | "Element %s was declared #PCDATA but contains non text nodes\n", |
5514 | | elem->name, NULL, NULL); |
5515 | | } |
5516 | | break; |
5517 | | } |
5518 | | child = elem->children; |
5519 | | /* Hum, this start to get messy */ |
5520 | | while (child != NULL) { |
5521 | | if (child->type == XML_ELEMENT_NODE) { |
5522 | | name = child->name; |
5523 | | if ((child->ns != NULL) && (child->ns->prefix != NULL)) { |
5524 | | xmlChar fn[50]; |
5525 | | xmlChar *fullname; |
5526 | | |
5527 | | fullname = xmlBuildQName(child->name, child->ns->prefix, |
5528 | | fn, 50); |
5529 | | if (fullname == NULL) { |
5530 | | xmlVErrMemory(ctxt); |
5531 | | return(0); |
5532 | | } |
5533 | | cont = elemDecl->content; |
5534 | | while (cont != NULL) { |
5535 | | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
5536 | | if (xmlStrEqual(cont->name, fullname)) |
5537 | | break; |
5538 | | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
5539 | | (cont->c1 != NULL) && |
5540 | | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){ |
5541 | | if (xmlStrEqual(cont->c1->name, fullname)) |
5542 | | break; |
5543 | | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
5544 | | (cont->c1 == NULL) || |
5545 | | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){ |
5546 | | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
5547 | | "Internal: MIXED struct corrupted\n", |
5548 | | NULL); |
5549 | | break; |
5550 | | } |
5551 | | cont = cont->c2; |
5552 | | } |
5553 | | if ((fullname != fn) && (fullname != child->name)) |
5554 | | xmlFree(fullname); |
5555 | | if (cont != NULL) |
5556 | | goto child_ok; |
5557 | | } |
5558 | | cont = elemDecl->content; |
5559 | | while (cont != NULL) { |
5560 | | if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) { |
5561 | | if (xmlStrEqual(cont->name, name)) break; |
5562 | | } else if ((cont->type == XML_ELEMENT_CONTENT_OR) && |
5563 | | (cont->c1 != NULL) && |
5564 | | (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) { |
5565 | | if (xmlStrEqual(cont->c1->name, name)) break; |
5566 | | } else if ((cont->type != XML_ELEMENT_CONTENT_OR) || |
5567 | | (cont->c1 == NULL) || |
5568 | | (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) { |
5569 | | xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT, |
5570 | | "Internal: MIXED struct corrupted\n", |
5571 | | NULL); |
5572 | | break; |
5573 | | } |
5574 | | cont = cont->c2; |
5575 | | } |
5576 | | if (cont == NULL) { |
5577 | | xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD, |
5578 | | "Element %s is not declared in %s list of possible children\n", |
5579 | | name, elem->name, NULL); |
5580 | | ret = 0; |
5581 | | } |
5582 | | } |
5583 | | child_ok: |
5584 | | child = child->next; |
5585 | | } |
5586 | | break; |
5587 | | case XML_ELEMENT_TYPE_ELEMENT: |
5588 | | if ((doc->standalone == 1) && (extsubset == 1)) { |
5589 | | /* |
5590 | | * VC: Standalone Document Declaration |
5591 | | * - element types with element content, if white space |
5592 | | * occurs directly within any instance of those types. |
5593 | | */ |
5594 | | child = elem->children; |
5595 | | while (child != NULL) { |
5596 | | if ((child->type == XML_TEXT_NODE) && |
5597 | | (child->content != NULL)) { |
5598 | | const xmlChar *content = child->content; |
5599 | | |
5600 | | while (IS_BLANK_CH(*content)) |
5601 | | content++; |
5602 | | if (*content == 0) { |
5603 | | xmlErrValidNode(ctxt, elem, |
5604 | | XML_DTD_STANDALONE_WHITE_SPACE, |
5605 | | "standalone: %s declared in the external subset contains white spaces nodes\n", |
5606 | | elem->name, NULL, NULL); |
5607 | | ret = 0; |
5608 | | break; |
5609 | | } |
5610 | | } |
5611 | | child =child->next; |
5612 | | } |
5613 | | } |
5614 | | child = elem->children; |
5615 | | cont = elemDecl->content; |
5616 | | tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem); |
5617 | | if (tmp <= 0) |
5618 | | ret = 0; |
5619 | | break; |
5620 | | } |
5621 | | } /* not continuous */ |
5622 | | |
5623 | | /* [ VC: Required Attribute ] */ |
5624 | | attr = elemDecl->attributes; |
5625 | | while (attr != NULL) { |
5626 | | if (attr->def == XML_ATTRIBUTE_REQUIRED) { |
5627 | | int qualified = -1; |
5628 | | |
5629 | | if ((attr->prefix == NULL) && |
5630 | | (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) { |
5631 | | xmlNsPtr ns; |
5632 | | |
5633 | | ns = elem->nsDef; |
5634 | | while (ns != NULL) { |
5635 | | if (ns->prefix == NULL) |
5636 | | goto found; |
5637 | | ns = ns->next; |
5638 | | } |
5639 | | } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) { |
5640 | | xmlNsPtr ns; |
5641 | | |
5642 | | ns = elem->nsDef; |
5643 | | while (ns != NULL) { |
5644 | | if (xmlStrEqual(attr->name, ns->prefix)) |
5645 | | goto found; |
5646 | | ns = ns->next; |
5647 | | } |
5648 | | } else { |
5649 | | xmlAttrPtr attrib; |
5650 | | |
5651 | | attrib = elem->properties; |
5652 | | while (attrib != NULL) { |
5653 | | if (xmlStrEqual(attrib->name, attr->name)) { |
5654 | | if (attr->prefix != NULL) { |
5655 | | xmlNsPtr nameSpace = attrib->ns; |
5656 | | |
5657 | | /* |
5658 | | * qualified names handling is problematic, having a |
5659 | | * different prefix should be possible but DTDs don't |
5660 | | * allow to define the URI instead of the prefix :-( |
5661 | | */ |
5662 | | if (nameSpace == NULL) { |
5663 | | if (qualified < 0) |
5664 | | qualified = 0; |
5665 | | } else if (!xmlStrEqual(nameSpace->prefix, |
5666 | | attr->prefix)) { |
5667 | | if (qualified < 1) |
5668 | | qualified = 1; |
5669 | | } else |
5670 | | goto found; |
5671 | | } else { |
5672 | | /* |
5673 | | * We should allow applications to define namespaces |
5674 | | * for their application even if the DTD doesn't |
5675 | | * carry one, otherwise, basically we would always |
5676 | | * break. |
5677 | | */ |
5678 | | goto found; |
5679 | | } |
5680 | | } |
5681 | | attrib = attrib->next; |
5682 | | } |
5683 | | } |
5684 | | if (qualified == -1) { |
5685 | | if (attr->prefix == NULL) { |
5686 | | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
5687 | | "Element %s does not carry attribute %s\n", |
5688 | | elem->name, attr->name, NULL); |
5689 | | ret = 0; |
5690 | | } else { |
5691 | | xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE, |
5692 | | "Element %s does not carry attribute %s:%s\n", |
5693 | | elem->name, attr->prefix,attr->name); |
5694 | | ret = 0; |
5695 | | } |
5696 | | } else if (qualified == 0) { |
5697 | | xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX, |
5698 | | "Element %s required attribute %s:%s has no prefix\n", |
5699 | | elem->name, attr->prefix, attr->name); |
5700 | | } else if (qualified == 1) { |
5701 | | xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX, |
5702 | | "Element %s required attribute %s:%s has different prefix\n", |
5703 | | elem->name, attr->prefix, attr->name); |
5704 | | } |
5705 | | } |
5706 | | found: |
5707 | | attr = attr->nexth; |
5708 | | } |
5709 | | return(ret); |
5710 | | } |
5711 | | |
5712 | | /** |
5713 | | * Try to validate the root element. |
5714 | | * Performs the following check as described by the |
5715 | | * XML-1.0 recommendation: |
5716 | | * |
5717 | | * @deprecated Internal function, don't use. |
5718 | | * |
5719 | | * - [ VC: Root Element Type ] |
5720 | | * |
5721 | | * It doesn't try to recurse or apply other checks to the element. |
5722 | | * |
5723 | | * @param ctxt the validation context |
5724 | | * @param doc a document instance |
5725 | | * @returns 1 if valid or 0 otherwise. |
5726 | | */ |
5727 | | |
5728 | | int |
5729 | | xmlValidateRoot(xmlValidCtxt *ctxt, xmlDoc *doc) { |
5730 | | xmlNodePtr root; |
5731 | | int ret; |
5732 | | |
5733 | | if (doc == NULL) return(0); |
5734 | | |
5735 | | root = xmlDocGetRootElement(doc); |
5736 | | if ((root == NULL) || (root->name == NULL)) { |
5737 | | xmlErrValid(ctxt, XML_DTD_NO_ROOT, |
5738 | | "no root element\n", NULL); |
5739 | | return(0); |
5740 | | } |
5741 | | |
5742 | | /* |
5743 | | * When doing post validation against a separate DTD, those may |
5744 | | * no internal subset has been generated |
5745 | | */ |
5746 | | if ((doc->intSubset != NULL) && |
5747 | | (doc->intSubset->name != NULL)) { |
5748 | | /* |
5749 | | * Check first the document root against the NQName |
5750 | | */ |
5751 | | if (!xmlStrEqual(doc->intSubset->name, root->name)) { |
5752 | | if ((root->ns != NULL) && (root->ns->prefix != NULL)) { |
5753 | | xmlChar fn[50]; |
5754 | | xmlChar *fullname; |
5755 | | |
5756 | | fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50); |
5757 | | if (fullname == NULL) { |
5758 | | xmlVErrMemory(ctxt); |
5759 | | return(0); |
5760 | | } |
5761 | | ret = xmlStrEqual(doc->intSubset->name, fullname); |
5762 | | if ((fullname != fn) && (fullname != root->name)) |
5763 | | xmlFree(fullname); |
5764 | | if (ret == 1) |
5765 | | goto name_ok; |
5766 | | } |
5767 | | if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) && |
5768 | | (xmlStrEqual(root->name, BAD_CAST "html"))) |
5769 | | goto name_ok; |
5770 | | xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME, |
5771 | | "root and DTD name do not match '%s' and '%s'\n", |
5772 | | root->name, doc->intSubset->name, NULL); |
5773 | | return(0); |
5774 | | } |
5775 | | } |
5776 | | name_ok: |
5777 | | return(1); |
5778 | | } |
5779 | | |
5780 | | |
5781 | | /** |
5782 | | * Try to validate the subtree under an element. |
5783 | | * |
5784 | | * @param ctxt the validation context |
5785 | | * @param doc a document instance |
5786 | | * @param root an element instance |
5787 | | * @returns 1 if valid or 0 otherwise. |
5788 | | */ |
5789 | | |
5790 | | int |
5791 | | xmlValidateElement(xmlValidCtxt *ctxt, xmlDoc *doc, xmlNode *root) { |
5792 | | xmlNodePtr elem; |
5793 | | xmlAttrPtr attr; |
5794 | | xmlNsPtr ns; |
5795 | | const xmlChar *value; |
5796 | | int ret = 1; |
5797 | | |
5798 | | if (root == NULL) return(0); |
5799 | | |
5800 | | CHECK_DTD; |
5801 | | |
5802 | | elem = root; |
5803 | | while (1) { |
5804 | | ret &= xmlValidateOneElement(ctxt, doc, elem); |
5805 | | |
5806 | | if (elem->type == XML_ELEMENT_NODE) { |
5807 | | attr = elem->properties; |
5808 | | while (attr != NULL) { |
5809 | | if (attr->children == NULL) |
5810 | | value = xmlStrdup(BAD_CAST ""); |
5811 | | else |
5812 | | value = xmlNodeListGetString(doc, attr->children, 0); |
5813 | | if (value == NULL) { |
5814 | | xmlVErrMemory(ctxt); |
5815 | | ret = 0; |
5816 | | } else { |
5817 | | ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value); |
5818 | | xmlFree((char *)value); |
5819 | | } |
5820 | | attr= attr->next; |
5821 | | } |
5822 | | |
5823 | | ns = elem->nsDef; |
5824 | | while (ns != NULL) { |
5825 | | if (elem->ns == NULL) |
5826 | | ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL, |
5827 | | ns, ns->href); |
5828 | | else |
5829 | | ret &= xmlValidateOneNamespace(ctxt, doc, elem, |
5830 | | elem->ns->prefix, ns, |
5831 | | ns->href); |
5832 | | ns = ns->next; |
5833 | | } |
5834 | | |
5835 | | if (elem->children != NULL) { |
5836 | | elem = elem->children; |
5837 | | continue; |
5838 | | } |
5839 | | } |
5840 | | |
5841 | | while (1) { |
5842 | | if (elem == root) |
5843 | | goto done; |
5844 | | if (elem->next != NULL) |
5845 | | break; |
5846 | | elem = elem->parent; |
5847 | | } |
5848 | | elem = elem->next; |
5849 | | } |
5850 | | |
5851 | | done: |
5852 | | return(ret); |
5853 | | } |
5854 | | |
5855 | | /** |
5856 | | * @param ref A reference to be validated |
5857 | | * @param ctxt Validation context |
5858 | | * @param name Name of ID we are searching for |
5859 | | */ |
5860 | | static void |
5861 | | xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt, |
5862 | | const xmlChar *name) { |
5863 | | xmlAttrPtr id; |
5864 | | xmlAttrPtr attr; |
5865 | | |
5866 | | if (ref == NULL) |
5867 | | return; |
5868 | | if ((ref->attr == NULL) && (ref->name == NULL)) |
5869 | | return; |
5870 | | attr = ref->attr; |
5871 | | if (attr == NULL) { |
5872 | | xmlChar *dup, *str = NULL, *cur, save; |
5873 | | |
5874 | | dup = xmlStrdup(name); |
5875 | | if (dup == NULL) { |
5876 | | xmlVErrMemory(ctxt); |
5877 | | return; |
5878 | | } |
5879 | | cur = dup; |
5880 | | while (*cur != 0) { |
5881 | | str = cur; |
5882 | | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
5883 | | save = *cur; |
5884 | | *cur = 0; |
5885 | | id = xmlGetID(ctxt->doc, str); |
5886 | | if (id == NULL) { |
5887 | | xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID, |
5888 | | "attribute %s line %d references an unknown ID \"%s\"\n", |
5889 | | ref->name, ref->lineno, str); |
5890 | | ctxt->valid = 0; |
5891 | | } |
5892 | | if (save == 0) |
5893 | | break; |
5894 | | *cur = save; |
5895 | | while (IS_BLANK_CH(*cur)) cur++; |
5896 | | } |
5897 | | xmlFree(dup); |
5898 | | } else if (attr->atype == XML_ATTRIBUTE_IDREF) { |
5899 | | id = xmlGetID(ctxt->doc, name); |
5900 | | if (id == NULL) { |
5901 | | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
5902 | | "IDREF attribute %s references an unknown ID \"%s\"\n", |
5903 | | attr->name, name, NULL); |
5904 | | ctxt->valid = 0; |
5905 | | } |
5906 | | } else if (attr->atype == XML_ATTRIBUTE_IDREFS) { |
5907 | | xmlChar *dup, *str = NULL, *cur, save; |
5908 | | |
5909 | | dup = xmlStrdup(name); |
5910 | | if (dup == NULL) { |
5911 | | xmlVErrMemory(ctxt); |
5912 | | ctxt->valid = 0; |
5913 | | return; |
5914 | | } |
5915 | | cur = dup; |
5916 | | while (*cur != 0) { |
5917 | | str = cur; |
5918 | | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
5919 | | save = *cur; |
5920 | | *cur = 0; |
5921 | | id = xmlGetID(ctxt->doc, str); |
5922 | | if (id == NULL) { |
5923 | | xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID, |
5924 | | "IDREFS attribute %s references an unknown ID \"%s\"\n", |
5925 | | attr->name, str, NULL); |
5926 | | ctxt->valid = 0; |
5927 | | } |
5928 | | if (save == 0) |
5929 | | break; |
5930 | | *cur = save; |
5931 | | while (IS_BLANK_CH(*cur)) cur++; |
5932 | | } |
5933 | | xmlFree(dup); |
5934 | | } |
5935 | | } |
5936 | | |
5937 | | /** |
5938 | | * @param data Contents of current link |
5939 | | * @param user Value supplied by the user |
5940 | | * @returns 0 to abort the walk or 1 to continue. |
5941 | | */ |
5942 | | static int |
5943 | | xmlWalkValidateList(const void *data, void *user) |
5944 | | { |
5945 | | xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user; |
5946 | | xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name); |
5947 | | return 1; |
5948 | | } |
5949 | | |
5950 | | /** |
5951 | | * @param payload list of references |
5952 | | * @param data validation context |
5953 | | * @param name name of ID we are searching for |
5954 | | */ |
5955 | | static void |
5956 | | xmlValidateCheckRefCallback(void *payload, void *data, const xmlChar *name) { |
5957 | | xmlListPtr ref_list = (xmlListPtr) payload; |
5958 | | xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data; |
5959 | | xmlValidateMemo memo; |
5960 | | |
5961 | | if (ref_list == NULL) |
5962 | | return; |
5963 | | memo.ctxt = ctxt; |
5964 | | memo.name = name; |
5965 | | |
5966 | | xmlListWalk(ref_list, xmlWalkValidateList, &memo); |
5967 | | |
5968 | | } |
5969 | | |
5970 | | /** |
5971 | | * Performs the final step of document validation once all the |
5972 | | * incremental validation steps have been completed. |
5973 | | * |
5974 | | * @deprecated Internal function, don't use. |
5975 | | * |
5976 | | * Performs the following checks described by the XML Rec: |
5977 | | * |
5978 | | * - Check all the IDREF/IDREFS attributes definition for validity. |
5979 | | * |
5980 | | * @param ctxt the validation context |
5981 | | * @param doc a document instance |
5982 | | * @returns 1 if valid or 0 otherwise. |
5983 | | */ |
5984 | | |
5985 | | int |
5986 | | xmlValidateDocumentFinal(xmlValidCtxt *ctxt, xmlDoc *doc) { |
5987 | | xmlRefTablePtr table; |
5988 | | xmlParserCtxtPtr pctxt = NULL; |
5989 | | xmlParserInputPtr oldInput = NULL; |
5990 | | |
5991 | | if (ctxt == NULL) |
5992 | | return(0); |
5993 | | if (doc == NULL) { |
5994 | | xmlErrValid(ctxt, XML_DTD_NO_DOC, |
5995 | | "xmlValidateDocumentFinal: doc == NULL\n", NULL); |
5996 | | return(0); |
5997 | | } |
5998 | | |
5999 | | /* |
6000 | | * Check all the NOTATION/NOTATIONS attributes |
6001 | | */ |
6002 | | /* |
6003 | | * Check all the ENTITY/ENTITIES attributes definition for validity |
6004 | | */ |
6005 | | /* |
6006 | | * Check all the IDREF/IDREFS attributes definition for validity |
6007 | | */ |
6008 | | |
6009 | | /* |
6010 | | * Don't print line numbers. |
6011 | | */ |
6012 | | if (ctxt->flags & XML_VCTXT_USE_PCTXT) { |
6013 | | pctxt = ctxt->userData; |
6014 | | oldInput = pctxt->input; |
6015 | | pctxt->input = NULL; |
6016 | | } |
6017 | | |
6018 | | table = (xmlRefTablePtr) doc->refs; |
6019 | | ctxt->doc = doc; |
6020 | | ctxt->valid = 1; |
6021 | | xmlHashScan(table, xmlValidateCheckRefCallback, ctxt); |
6022 | | |
6023 | | if (ctxt->flags & XML_VCTXT_USE_PCTXT) |
6024 | | pctxt->input = oldInput; |
6025 | | |
6026 | | return(ctxt->valid); |
6027 | | } |
6028 | | |
6029 | | /** |
6030 | | * Try to validate the document against the DTD instance. |
6031 | | * |
6032 | | * Note that the internal subset (if present) is de-coupled |
6033 | | * (i.e. not used), which could cause problems if ID or IDREF |
6034 | | * attributes are present. |
6035 | | * |
6036 | | * @param ctxt the validation context |
6037 | | * @param doc a document instance |
6038 | | * @param dtd a DTD instance |
6039 | | * @returns 1 if valid or 0 otherwise. |
6040 | | */ |
6041 | | |
6042 | | int |
6043 | | xmlValidateDtd(xmlValidCtxt *ctxt, xmlDoc *doc, xmlDtd *dtd) { |
6044 | | int ret; |
6045 | | xmlDtdPtr oldExt, oldInt; |
6046 | | xmlNodePtr root; |
6047 | | |
6048 | | if (dtd == NULL) |
6049 | | return(0); |
6050 | | if (doc == NULL) |
6051 | | return(0); |
6052 | | |
6053 | | oldExt = doc->extSubset; |
6054 | | oldInt = doc->intSubset; |
6055 | | doc->extSubset = dtd; |
6056 | | doc->intSubset = NULL; |
6057 | | if (doc->ids != NULL) { |
6058 | | xmlFreeIDTable(doc->ids); |
6059 | | doc->ids = NULL; |
6060 | | } |
6061 | | if (doc->refs != NULL) { |
6062 | | xmlFreeRefTable(doc->refs); |
6063 | | doc->refs = NULL; |
6064 | | } |
6065 | | |
6066 | | ret = xmlValidateRoot(ctxt, doc); |
6067 | | if (ret != 0) { |
6068 | | root = xmlDocGetRootElement(doc); |
6069 | | ret = xmlValidateElement(ctxt, doc, root); |
6070 | | ret &= xmlValidateDocumentFinal(ctxt, doc); |
6071 | | } |
6072 | | |
6073 | | doc->extSubset = oldExt; |
6074 | | doc->intSubset = oldInt; |
6075 | | if (doc->ids != NULL) { |
6076 | | xmlFreeIDTable(doc->ids); |
6077 | | doc->ids = NULL; |
6078 | | } |
6079 | | if (doc->refs != NULL) { |
6080 | | xmlFreeRefTable(doc->refs); |
6081 | | doc->refs = NULL; |
6082 | | } |
6083 | | |
6084 | | return(ret); |
6085 | | } |
6086 | | |
6087 | | /** |
6088 | | * Validate a document against a DTD. |
6089 | | * |
6090 | | * Like #xmlValidateDtd but uses the parser context's error handler. |
6091 | | * |
6092 | | * @since 2.14.0 |
6093 | | * |
6094 | | * @param ctxt a parser context |
6095 | | * @param doc a document instance |
6096 | | * @param dtd a dtd instance |
6097 | | * @returns 1 if valid or 0 otherwise. |
6098 | | */ |
6099 | | int |
6100 | | xmlCtxtValidateDtd(xmlParserCtxt *ctxt, xmlDoc *doc, xmlDtd *dtd) { |
6101 | | if ((ctxt == NULL) || (ctxt->html)) |
6102 | | return(0); |
6103 | | |
6104 | | xmlCtxtReset(ctxt); |
6105 | | |
6106 | | return(xmlValidateDtd(&ctxt->vctxt, doc, dtd)); |
6107 | | } |
6108 | | |
6109 | | static void |
6110 | | xmlValidateNotationCallback(void *payload, void *data, |
6111 | | const xmlChar *name ATTRIBUTE_UNUSED) { |
6112 | | xmlEntityPtr cur = (xmlEntityPtr) payload; |
6113 | | xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data; |
6114 | | if (cur == NULL) |
6115 | | return; |
6116 | | if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
6117 | | xmlChar *notation = cur->content; |
6118 | | |
6119 | | if (notation != NULL) { |
6120 | | int ret; |
6121 | | |
6122 | | ret = xmlValidateNotationUse(ctxt, cur->doc, notation); |
6123 | | if (ret != 1) { |
6124 | | ctxt->valid = 0; |
6125 | | } |
6126 | | } |
6127 | | } |
6128 | | } |
6129 | | |
6130 | | static void |
6131 | | xmlValidateAttributeCallback(void *payload, void *data, |
6132 | | const xmlChar *name ATTRIBUTE_UNUSED) { |
6133 | | xmlAttributePtr cur = (xmlAttributePtr) payload; |
6134 | | xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data; |
6135 | | xmlDocPtr doc; |
6136 | | xmlElementPtr elem = NULL; |
6137 | | |
6138 | | if (cur == NULL) |
6139 | | return; |
6140 | | if (cur->atype == XML_ATTRIBUTE_NOTATION) { |
6141 | | const xmlChar *elemLocalName; |
6142 | | xmlChar *elemPrefix; |
6143 | | |
6144 | | doc = cur->doc; |
6145 | | if (cur->elem == NULL) { |
6146 | | xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR, |
6147 | | "xmlValidateAttributeCallback(%s): internal error\n", |
6148 | | (const char *) cur->name); |
6149 | | return; |
6150 | | } |
6151 | | |
6152 | | elemLocalName = xmlSplitQName4(cur->elem, &elemPrefix); |
6153 | | if (elemLocalName == NULL) { |
6154 | | xmlVErrMemory(ctxt); |
6155 | | return; |
6156 | | } |
6157 | | |
6158 | | if ((doc != NULL) && (doc->intSubset != NULL)) |
6159 | | elem = xmlHashLookup2(doc->intSubset->elements, |
6160 | | elemLocalName, elemPrefix); |
6161 | | if ((elem == NULL) && (doc != NULL) && (doc->extSubset != NULL)) |
6162 | | elem = xmlHashLookup2(doc->extSubset->elements, |
6163 | | elemLocalName, elemPrefix); |
6164 | | if ((elem == NULL) && (cur->parent != NULL) && |
6165 | | (cur->parent->type == XML_DTD_NODE)) |
6166 | | elem = xmlHashLookup2(((xmlDtdPtr) cur->parent)->elements, |
6167 | | elemLocalName, elemPrefix); |
6168 | | |
6169 | | xmlFree(elemPrefix); |
6170 | | |
6171 | | if (elem == NULL) { |
6172 | | xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM, |
6173 | | "attribute %s: could not find decl for element %s\n", |
6174 | | cur->name, cur->elem, NULL); |
6175 | | return; |
6176 | | } |
6177 | | if (elem->etype == XML_ELEMENT_TYPE_EMPTY) { |
6178 | | xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION, |
6179 | | "NOTATION attribute %s declared for EMPTY element %s\n", |
6180 | | cur->name, cur->elem, NULL); |
6181 | | ctxt->valid = 0; |
6182 | | } |
6183 | | } |
6184 | | } |
6185 | | |
6186 | | /** |
6187 | | * Performs the final validation steps of DTD content once all the |
6188 | | * subsets have been parsed. |
6189 | | * |
6190 | | * @deprecated Internal function, don't use. |
6191 | | * |
6192 | | * Performs the following checks described by the XML Rec: |
6193 | | * |
6194 | | * - check that ENTITY and ENTITIES type attributes default or |
6195 | | * possible values matches one of the defined entities. |
6196 | | * - check that NOTATION type attributes default or |
6197 | | * possible values matches one of the defined notations. |
6198 | | * |
6199 | | * @param ctxt the validation context |
6200 | | * @param doc a document instance |
6201 | | * @returns 1 if valid or 0 if invalid and -1 if not well-formed. |
6202 | | */ |
6203 | | |
6204 | | int |
6205 | | xmlValidateDtdFinal(xmlValidCtxt *ctxt, xmlDoc *doc) { |
6206 | | xmlDtdPtr dtd; |
6207 | | xmlAttributeTablePtr table; |
6208 | | xmlEntitiesTablePtr entities; |
6209 | | |
6210 | | if ((doc == NULL) || (ctxt == NULL)) return(0); |
6211 | | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) |
6212 | | return(0); |
6213 | | ctxt->doc = doc; |
6214 | | ctxt->valid = 1; |
6215 | | dtd = doc->intSubset; |
6216 | | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
6217 | | table = (xmlAttributeTablePtr) dtd->attributes; |
6218 | | xmlHashScan(table, xmlValidateAttributeCallback, ctxt); |
6219 | | } |
6220 | | if ((dtd != NULL) && (dtd->entities != NULL)) { |
6221 | | entities = (xmlEntitiesTablePtr) dtd->entities; |
6222 | | xmlHashScan(entities, xmlValidateNotationCallback, ctxt); |
6223 | | } |
6224 | | dtd = doc->extSubset; |
6225 | | if ((dtd != NULL) && (dtd->attributes != NULL)) { |
6226 | | table = (xmlAttributeTablePtr) dtd->attributes; |
6227 | | xmlHashScan(table, xmlValidateAttributeCallback, ctxt); |
6228 | | } |
6229 | | if ((dtd != NULL) && (dtd->entities != NULL)) { |
6230 | | entities = (xmlEntitiesTablePtr) dtd->entities; |
6231 | | xmlHashScan(entities, xmlValidateNotationCallback, ctxt); |
6232 | | } |
6233 | | return(ctxt->valid); |
6234 | | } |
6235 | | |
6236 | | /** |
6237 | | * Validate a document. |
6238 | | * |
6239 | | * @param ctxt parser context (optional) |
6240 | | * @param vctxt validation context (optional) |
6241 | | * @param doc document |
6242 | | * @returns 1 if valid or 0 otherwise. |
6243 | | */ |
6244 | | static int |
6245 | | xmlValidateDocumentInternal(xmlParserCtxtPtr ctxt, xmlValidCtxtPtr vctxt, |
6246 | | xmlDocPtr doc) { |
6247 | | int ret; |
6248 | | xmlNodePtr root; |
6249 | | |
6250 | | if (doc == NULL) |
6251 | | return(0); |
6252 | | if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) { |
6253 | | xmlErrValid(vctxt, XML_DTD_NO_DTD, |
6254 | | "no DTD found!\n", NULL); |
6255 | | return(0); |
6256 | | } |
6257 | | |
6258 | | if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) || |
6259 | | (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) { |
6260 | | xmlChar *sysID = NULL; |
6261 | | |
6262 | | if (doc->intSubset->SystemID != NULL) { |
6263 | | int res; |
6264 | | |
6265 | | res = xmlBuildURISafe(doc->intSubset->SystemID, doc->URL, &sysID); |
6266 | | if (res < 0) { |
6267 | | xmlVErrMemory(vctxt); |
6268 | | return 0; |
6269 | | } else if (res != 0) { |
6270 | | xmlErrValid(vctxt, XML_DTD_LOAD_ERROR, |
6271 | | "Could not build URI for external subset \"%s\"\n", |
6272 | | (const char *) doc->intSubset->SystemID); |
6273 | | return 0; |
6274 | | } |
6275 | | } |
6276 | | |
6277 | | if (ctxt != NULL) { |
6278 | | xmlParserInputPtr input; |
6279 | | |
6280 | | input = xmlLoadResource(ctxt, (const char *) sysID, |
6281 | | (const char *) doc->intSubset->ExternalID, |
6282 | | XML_RESOURCE_DTD); |
6283 | | if (input == NULL) { |
6284 | | xmlFree(sysID); |
6285 | | return 0; |
6286 | | } |
6287 | | |
6288 | | doc->extSubset = xmlCtxtParseDtd(ctxt, input, |
6289 | | doc->intSubset->ExternalID, |
6290 | | sysID); |
6291 | | } else { |
6292 | | doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID, sysID); |
6293 | | } |
6294 | | |
6295 | | if (sysID != NULL) |
6296 | | xmlFree(sysID); |
6297 | | if (doc->extSubset == NULL) { |
6298 | | if (doc->intSubset->SystemID != NULL) { |
6299 | | xmlErrValid(vctxt, XML_DTD_LOAD_ERROR, |
6300 | | "Could not load the external subset \"%s\"\n", |
6301 | | (const char *) doc->intSubset->SystemID); |
6302 | | } else { |
6303 | | xmlErrValid(vctxt, XML_DTD_LOAD_ERROR, |
6304 | | "Could not load the external subset \"%s\"\n", |
6305 | | (const char *) doc->intSubset->ExternalID); |
6306 | | } |
6307 | | return(0); |
6308 | | } |
6309 | | } |
6310 | | |
6311 | | if (doc->ids != NULL) { |
6312 | | xmlFreeIDTable(doc->ids); |
6313 | | doc->ids = NULL; |
6314 | | } |
6315 | | if (doc->refs != NULL) { |
6316 | | xmlFreeRefTable(doc->refs); |
6317 | | doc->refs = NULL; |
6318 | | } |
6319 | | ret = xmlValidateDtdFinal(vctxt, doc); |
6320 | | if (!xmlValidateRoot(vctxt, doc)) return(0); |
6321 | | |
6322 | | root = xmlDocGetRootElement(doc); |
6323 | | ret &= xmlValidateElement(vctxt, doc, root); |
6324 | | ret &= xmlValidateDocumentFinal(vctxt, doc); |
6325 | | return(ret); |
6326 | | } |
6327 | | |
6328 | | /** |
6329 | | * Try to validate the document instance. |
6330 | | * |
6331 | | * @deprecated This function can't report malloc or other failures. |
6332 | | * Use #xmlCtxtValidateDocument. |
6333 | | * |
6334 | | * Performs the all the checks described by the XML Rec, |
6335 | | * i.e. validates the internal and external subset (if present) |
6336 | | * and validates the document tree. |
6337 | | * |
6338 | | * @param vctxt the validation context |
6339 | | * @param doc a document instance |
6340 | | * @returns 1 if valid or 0 otherwise. |
6341 | | */ |
6342 | | int |
6343 | | xmlValidateDocument(xmlValidCtxt *vctxt, xmlDoc *doc) { |
6344 | | return(xmlValidateDocumentInternal(NULL, vctxt, doc)); |
6345 | | } |
6346 | | |
6347 | | /** |
6348 | | * Validate a document. |
6349 | | * |
6350 | | * Like #xmlValidateDocument but uses the parser context's error handler. |
6351 | | * |
6352 | | * Option XML_PARSE_DTDLOAD should be enabled in the parser context |
6353 | | * to make external entities work. |
6354 | | * |
6355 | | * @since 2.14.0 |
6356 | | * |
6357 | | * @param ctxt a parser context |
6358 | | * @param doc a document instance |
6359 | | * @returns 1 if valid or 0 otherwise. |
6360 | | */ |
6361 | | int |
6362 | | xmlCtxtValidateDocument(xmlParserCtxt *ctxt, xmlDoc *doc) { |
6363 | | if ((ctxt == NULL) || (ctxt->html)) |
6364 | | return(0); |
6365 | | |
6366 | | xmlCtxtReset(ctxt); |
6367 | | |
6368 | | return(xmlValidateDocumentInternal(ctxt, &ctxt->vctxt, doc)); |
6369 | | } |
6370 | | |
6371 | | /************************************************************************ |
6372 | | * * |
6373 | | * Routines for dynamic validation editing * |
6374 | | * * |
6375 | | ************************************************************************/ |
6376 | | |
6377 | | /** |
6378 | | * Build/extend a list of potential children allowed by the content tree |
6379 | | * |
6380 | | * @deprecated Internal function, don't use. |
6381 | | * |
6382 | | * @param ctree an element content tree |
6383 | | * @param names an array to store the list of child names |
6384 | | * @param len a pointer to the number of element in the list |
6385 | | * @param max the size of the array |
6386 | | * @returns the number of element in the list, or -1 in case of error. |
6387 | | */ |
6388 | | |
6389 | | int |
6390 | | xmlValidGetPotentialChildren(xmlElementContent *ctree, |
6391 | | const xmlChar **names, |
6392 | | int *len, int max) { |
6393 | | int i; |
6394 | | |
6395 | | if ((ctree == NULL) || (names == NULL) || (len == NULL)) |
6396 | | return(-1); |
6397 | | if (*len >= max) return(*len); |
6398 | | |
6399 | | switch (ctree->type) { |
6400 | | case XML_ELEMENT_CONTENT_PCDATA: |
6401 | | for (i = 0; i < *len;i++) |
6402 | | if (xmlStrEqual(BAD_CAST "#PCDATA", names[i])) return(*len); |
6403 | | names[(*len)++] = BAD_CAST "#PCDATA"; |
6404 | | break; |
6405 | | case XML_ELEMENT_CONTENT_ELEMENT: |
6406 | | for (i = 0; i < *len;i++) |
6407 | | if (xmlStrEqual(ctree->name, names[i])) return(*len); |
6408 | | names[(*len)++] = ctree->name; |
6409 | | break; |
6410 | | case XML_ELEMENT_CONTENT_SEQ: |
6411 | | xmlValidGetPotentialChildren(ctree->c1, names, len, max); |
6412 | | xmlValidGetPotentialChildren(ctree->c2, names, len, max); |
6413 | | break; |
6414 | | case XML_ELEMENT_CONTENT_OR: |
6415 | | xmlValidGetPotentialChildren(ctree->c1, names, len, max); |
6416 | | xmlValidGetPotentialChildren(ctree->c2, names, len, max); |
6417 | | break; |
6418 | | } |
6419 | | |
6420 | | return(*len); |
6421 | | } |
6422 | | |
6423 | | /* |
6424 | | * Dummy function to suppress messages while we try out valid elements |
6425 | | */ |
6426 | | static void xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED, |
6427 | | const char *msg ATTRIBUTE_UNUSED, ...) { |
6428 | | } |
6429 | | |
6430 | | /** |
6431 | | * This function returns the list of authorized children to insert |
6432 | | * within an existing tree while respecting the validity constraints |
6433 | | * forced by the Dtd. The insertion point is defined using `prev` and |
6434 | | * `next` in the following ways: |
6435 | | * to insert before 'node': xmlValidGetValidElements(node->prev, node, ... |
6436 | | * to insert next 'node': xmlValidGetValidElements(node, node->next, ... |
6437 | | * to replace 'node': xmlValidGetValidElements(node->prev, node->next, ... |
6438 | | * to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs, |
6439 | | * to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ... |
6440 | | * |
6441 | | * pointers to the element names are inserted at the beginning of the array |
6442 | | * and do not need to be freed. |
6443 | | * |
6444 | | * @deprecated This feature will be removed. |
6445 | | * |
6446 | | * @param prev an element to insert after |
6447 | | * @param next an element to insert next |
6448 | | * @param names an array to store the list of child names |
6449 | | * @param max the size of the array |
6450 | | * @returns the number of element in the list, or -1 in case of error. If |
6451 | | * the function returns the value `max` the caller is invited to grow the |
6452 | | * receiving array and retry. |
6453 | | */ |
6454 | | |
6455 | | int |
6456 | | xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names, |
6457 | | int max) { |
6458 | | xmlValidCtxt vctxt; |
6459 | | int nb_valid_elements = 0; |
6460 | | const xmlChar *elements[256]={0}; |
6461 | | int nb_elements = 0, i; |
6462 | | const xmlChar *name; |
6463 | | |
6464 | | xmlNode *ref_node; |
6465 | | xmlNode *parent; |
6466 | | xmlNode *test_node; |
6467 | | |
6468 | | xmlNode *prev_next; |
6469 | | xmlNode *next_prev; |
6470 | | xmlNode *parent_childs; |
6471 | | xmlNode *parent_last; |
6472 | | |
6473 | | xmlElement *element_desc; |
6474 | | |
6475 | | if (prev == NULL && next == NULL) |
6476 | | return(-1); |
6477 | | |
6478 | | if (names == NULL) return(-1); |
6479 | | if (max <= 0) return(-1); |
6480 | | |
6481 | | memset(&vctxt, 0, sizeof (xmlValidCtxt)); |
6482 | | vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */ |
6483 | | |
6484 | | nb_valid_elements = 0; |
6485 | | ref_node = prev ? prev : next; |
6486 | | parent = ref_node->parent; |
6487 | | |
6488 | | /* |
6489 | | * Retrieves the parent element declaration |
6490 | | */ |
6491 | | element_desc = xmlGetDtdElementDesc(parent->doc->intSubset, |
6492 | | parent->name); |
6493 | | if ((element_desc == NULL) && (parent->doc->extSubset != NULL)) |
6494 | | element_desc = xmlGetDtdElementDesc(parent->doc->extSubset, |
6495 | | parent->name); |
6496 | | if (element_desc == NULL) return(-1); |
6497 | | |
6498 | | /* |
6499 | | * Do a backup of the current tree structure |
6500 | | */ |
6501 | | prev_next = prev ? prev->next : NULL; |
6502 | | next_prev = next ? next->prev : NULL; |
6503 | | parent_childs = parent->children; |
6504 | | parent_last = parent->last; |
6505 | | |
6506 | | /* |
6507 | | * Creates a dummy node and insert it into the tree |
6508 | | */ |
6509 | | test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL); |
6510 | | if (test_node == NULL) |
6511 | | return(-1); |
6512 | | |
6513 | | test_node->parent = parent; |
6514 | | test_node->prev = prev; |
6515 | | test_node->next = next; |
6516 | | name = test_node->name; |
6517 | | |
6518 | | if (prev) prev->next = test_node; |
6519 | | else parent->children = test_node; |
6520 | | |
6521 | | if (next) next->prev = test_node; |
6522 | | else parent->last = test_node; |
6523 | | |
6524 | | /* |
6525 | | * Insert each potential child node and check if the parent is |
6526 | | * still valid |
6527 | | */ |
6528 | | nb_elements = xmlValidGetPotentialChildren(element_desc->content, |
6529 | | elements, &nb_elements, 256); |
6530 | | |
6531 | | for (i = 0;i < nb_elements;i++) { |
6532 | | test_node->name = elements[i]; |
6533 | | if (xmlValidateOneElement(&vctxt, parent->doc, parent)) { |
6534 | | int j; |
6535 | | |
6536 | | for (j = 0; j < nb_valid_elements;j++) |
6537 | | if (xmlStrEqual(elements[i], names[j])) break; |
6538 | | names[nb_valid_elements++] = elements[i]; |
6539 | | if (nb_valid_elements >= max) break; |
6540 | | } |
6541 | | } |
6542 | | |
6543 | | /* |
6544 | | * Restore the tree structure |
6545 | | */ |
6546 | | if (prev) prev->next = prev_next; |
6547 | | if (next) next->prev = next_prev; |
6548 | | parent->children = parent_childs; |
6549 | | parent->last = parent_last; |
6550 | | |
6551 | | /* |
6552 | | * Free up the dummy node |
6553 | | */ |
6554 | | test_node->name = name; |
6555 | | xmlFreeNode(test_node); |
6556 | | |
6557 | | return(nb_valid_elements); |
6558 | | } |
6559 | | #endif /* LIBXML_VALID_ENABLED */ |