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