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