This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
valid.c
6905 lines (6289 loc) · 182 KB
/
valid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* valid.c : part of the code use to do the DTD handling and the validity
* checking
*
* See Copyright for the status of this software.
*
* daniel@veillard.com
*/
#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/hash.h>
#include <libxml/uri.h>
#include <libxml/valid.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/xmlerror.h>
#include <libxml/list.h>
#include <libxml/xmlsave.h>
#include "private/error.h"
#include "private/parser.h"
#include "private/regexp.h"
#include "private/save.h"
#include "private/tree.h"
static xmlElementPtr
xmlGetDtdElementDesc2(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name);
#ifdef LIBXML_VALID_ENABLED
static int
xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
const xmlChar *value);
#endif
/************************************************************************
* *
* Error handling routines *
* *
************************************************************************/
/**
* xmlVErrMemory:
* @ctxt: an XML validation parser context
* @extra: extra information
*
* Handle an out of memory error
*/
static void
xmlVErrMemory(xmlValidCtxtPtr ctxt)
{
if (ctxt != NULL) {
if (ctxt->flags & XML_VCTXT_USE_PCTXT) {
xmlCtxtErrMemory(ctxt->userData);
} else {
xmlRaiseMemoryError(NULL, ctxt->error, ctxt->userData,
XML_FROM_VALID, NULL);
}
} else {
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_VALID, NULL);
}
}
static void
xmlDoErrValid(xmlValidCtxtPtr ctxt, xmlNodePtr node,
xmlParserErrors code, int level,
const xmlChar *str1, const xmlChar *str2, const xmlChar *str3,
int int1,
const char *msg, ...) {
xmlParserCtxtPtr pctxt = NULL;
va_list ap;
if (ctxt == NULL)
return;
if (ctxt->flags & XML_VCTXT_USE_PCTXT)
pctxt = ctxt->userData;
va_start(ap, msg);
if (pctxt != NULL) {
xmlCtxtVErr(pctxt, node, XML_FROM_VALID, code, level,
str1, str2, str3, int1, msg, ap);
} else {
xmlGenericErrorFunc channel = NULL;
void *data = NULL;
int res;
if (ctxt != NULL) {
channel = ctxt->error;
data = ctxt->userData;
}
res = xmlVRaiseError(NULL, channel, data, NULL, node,
XML_FROM_VALID, code, level, NULL, 0,
(const char *) str1, (const char *) str2,
(const char *) str2, int1, 0,
msg, ap);
if (res < 0)
xmlVErrMemory(ctxt);
}
va_end(ap);
}
/**
* xmlErrValid:
* @ctxt: an XML validation parser context
* @error: the error number
* @extra: extra information
*
* Handle a validation error
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const char *extra)
{
xmlDoErrValid(ctxt, NULL, error, XML_ERR_ERROR, (const xmlChar *) extra,
NULL, NULL, 0, msg, extra);
}
#ifdef LIBXML_VALID_ENABLED
/**
* xmlErrValidNode:
* @ctxt: an XML validation parser context
* @node: the node raising the error
* @error: the error number
* @str1: extra information
* @str2: extra information
* @str3: extra information
*
* Handle a validation error, provide contextual information
*/
static void LIBXML_ATTR_FORMAT(4,0)
xmlErrValidNode(xmlValidCtxtPtr ctxt,
xmlNodePtr node, xmlParserErrors error,
const char *msg, const xmlChar * str1,
const xmlChar * str2, const xmlChar * str3)
{
xmlDoErrValid(ctxt, node, error, XML_ERR_ERROR, str1, str2, str3, 0,
msg, str1, str2, str3);
}
/**
* xmlErrValidNodeNr:
* @ctxt: an XML validation parser context
* @node: the node raising the error
* @error: the error number
* @str1: extra information
* @int2: extra information
* @str3: extra information
*
* Handle a validation error, provide contextual information
*/
static void LIBXML_ATTR_FORMAT(4,0)
xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
xmlNodePtr node, xmlParserErrors error,
const char *msg, const xmlChar * str1,
int int2, const xmlChar * str3)
{
xmlDoErrValid(ctxt, node, error, XML_ERR_ERROR, str1, str3, NULL, int2,
msg, str1, int2, str3);
}
/**
* xmlErrValidWarning:
* @ctxt: an XML validation parser context
* @node: the node raising the error
* @error: the error number
* @str1: extra information
* @str2: extra information
* @str3: extra information
*
* Handle a validation error, provide contextual information
*/
static void LIBXML_ATTR_FORMAT(4,0)
xmlErrValidWarning(xmlValidCtxtPtr ctxt,
xmlNodePtr node, xmlParserErrors error,
const char *msg, const xmlChar * str1,
const xmlChar * str2, const xmlChar * str3)
{
xmlDoErrValid(ctxt, node, error, XML_ERR_WARNING, str1, str2, str3, 0,
msg, str1, str2, str3);
}
#ifdef LIBXML_REGEXP_ENABLED
/*
* If regexp are enabled we can do continuous validation without the
* need of a tree to validate the content model. this is done in each
* callbacks.
* Each xmlValidState represent the validation state associated to the
* set of nodes currently open from the document root to the current element.
*/
typedef struct _xmlValidState {
xmlElementPtr elemDecl; /* pointer to the content model */
xmlNodePtr node; /* pointer to the current node */
xmlRegExecCtxtPtr exec; /* regexp runtime */
} _xmlValidState;
static int
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
ctxt->vstateMax = 10;
ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
sizeof(ctxt->vstateTab[0]));
if (ctxt->vstateTab == NULL) {
xmlVErrMemory(ctxt);
return(-1);
}
}
if (ctxt->vstateNr >= ctxt->vstateMax) {
xmlValidState *tmp;
tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
if (tmp == NULL) {
xmlVErrMemory(ctxt);
return(-1);
}
ctxt->vstateMax *= 2;
ctxt->vstateTab = tmp;
}
ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
ctxt->vstateTab[ctxt->vstateNr].node = node;
if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
if (elemDecl->contModel == NULL)
xmlValidBuildContentModel(ctxt, elemDecl);
if (elemDecl->contModel != NULL) {
ctxt->vstateTab[ctxt->vstateNr].exec =
xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
if (ctxt->vstateTab[ctxt->vstateNr].exec == NULL) {
xmlVErrMemory(ctxt);
return(-1);
}
} else {
ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
XML_ERR_INTERNAL_ERROR,
"Failed to build content model regexp for %s\n",
node->name, NULL, NULL);
}
}
return(ctxt->vstateNr++);
}
static int
vstateVPop(xmlValidCtxtPtr ctxt) {
xmlElementPtr elemDecl;
if (ctxt->vstateNr < 1) return(-1);
ctxt->vstateNr--;
elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
ctxt->vstateTab[ctxt->vstateNr].node = NULL;
if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
}
ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
if (ctxt->vstateNr >= 1)
ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
else
ctxt->vstate = NULL;
return(ctxt->vstateNr);
}
#else /* not LIBXML_REGEXP_ENABLED */
/*
* If regexp are not enabled, it uses a home made algorithm less
* complex and easier to
* debug/maintain than a generic NFA -> DFA state based algo. The
* only restriction is on the deepness of the tree limited by the
* size of the occurs bitfield
*
* this is the content of a saved state for rollbacks
*/
#define ROLLBACK_OR 0
#define ROLLBACK_PARENT 1
typedef struct _xmlValidState {
xmlElementContentPtr cont; /* pointer to the content model subtree */
xmlNodePtr node; /* pointer to the current node in the list */
long occurs;/* bitfield for multiple occurrences */
unsigned char depth; /* current depth in the overall tree */
unsigned char state; /* ROLLBACK_XXX */
} _xmlValidState;
#define MAX_RECURSE 25000
#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
#define CONT ctxt->vstate->cont
#define NODE ctxt->vstate->node
#define DEPTH ctxt->vstate->depth
#define OCCURS ctxt->vstate->occurs
#define STATE ctxt->vstate->state
#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
static int
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
xmlNodePtr node, unsigned char depth, long occurs,
unsigned char state) {
int i = ctxt->vstateNr - 1;
if (ctxt->vstateNr > MAX_RECURSE) {
return(-1);
}
if (ctxt->vstateTab == NULL) {
ctxt->vstateMax = 8;
ctxt->vstateTab = (xmlValidState *) xmlMalloc(
ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
if (ctxt->vstateTab == NULL) {
xmlVErrMemory(ctxt);
return(-1);
}
}
if (ctxt->vstateNr >= ctxt->vstateMax) {
xmlValidState *tmp;
tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
if (tmp == NULL) {
xmlVErrMemory(ctxt);
return(-1);
}
ctxt->vstateMax *= 2;
ctxt->vstateTab = tmp;
ctxt->vstate = &ctxt->vstateTab[0];
}
/*
* Don't push on the stack a state already here
*/
if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
(ctxt->vstateTab[i].node == node) &&
(ctxt->vstateTab[i].depth == depth) &&
(ctxt->vstateTab[i].occurs == occurs) &&
(ctxt->vstateTab[i].state == state))
return(ctxt->vstateNr);
ctxt->vstateTab[ctxt->vstateNr].cont = cont;
ctxt->vstateTab[ctxt->vstateNr].node = node;
ctxt->vstateTab[ctxt->vstateNr].depth = depth;
ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
ctxt->vstateTab[ctxt->vstateNr].state = state;
return(ctxt->vstateNr++);
}
static int
vstateVPop(xmlValidCtxtPtr ctxt) {
if (ctxt->vstateNr <= 1) return(-1);
ctxt->vstateNr--;
ctxt->vstate = &ctxt->vstateTab[0];
ctxt->vstate->cont = ctxt->vstateTab[ctxt->vstateNr].cont;
ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
return(ctxt->vstateNr);
}
#endif /* LIBXML_REGEXP_ENABLED */
static int
nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
{
if (ctxt->nodeMax <= 0) {
ctxt->nodeMax = 4;
ctxt->nodeTab =
(xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
sizeof(ctxt->nodeTab[0]));
if (ctxt->nodeTab == NULL) {
xmlVErrMemory(ctxt);
ctxt->nodeMax = 0;
return (0);
}
}
if (ctxt->nodeNr >= ctxt->nodeMax) {
xmlNodePtr *tmp;
tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
if (tmp == NULL) {
xmlVErrMemory(ctxt);
return (0);
}
ctxt->nodeMax *= 2;
ctxt->nodeTab = tmp;
}
ctxt->nodeTab[ctxt->nodeNr] = value;
ctxt->node = value;
return (ctxt->nodeNr++);
}
static xmlNodePtr
nodeVPop(xmlValidCtxtPtr ctxt)
{
xmlNodePtr ret;
if (ctxt->nodeNr <= 0)
return (NULL);
ctxt->nodeNr--;
if (ctxt->nodeNr > 0)
ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
else
ctxt->node = NULL;
ret = ctxt->nodeTab[ctxt->nodeNr];
ctxt->nodeTab[ctxt->nodeNr] = NULL;
return (ret);
}
/* TODO: use hash table for accesses to elem and attribute definitions */
#define CHECK_DTD \
if (doc == NULL) return(0); \
else if ((doc->intSubset == NULL) && \
(doc->extSubset == NULL)) return(0)
#ifdef LIBXML_REGEXP_ENABLED
/************************************************************************
* *
* Content model validation based on the regexps *
* *
************************************************************************/
/**
* xmlValidBuildAContentModel:
* @content: the content model
* @ctxt: the schema parser context
* @name: the element name whose content is being built
*
* Generate the automata sequence needed for that type
*
* Returns 1 if successful or 0 in case of error.
*/
static int
xmlValidBuildAContentModel(xmlElementContentPtr content,
xmlValidCtxtPtr ctxt,
const xmlChar *name) {
if (content == NULL) {
xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
"Found NULL content in content model of %s\n",
name, NULL, NULL);
return(0);
}
switch (content->type) {
case XML_ELEMENT_CONTENT_PCDATA:
xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
"Found PCDATA in content model of %s\n",
name, NULL, NULL);
return(0);
break;
case XML_ELEMENT_CONTENT_ELEMENT: {
xmlAutomataStatePtr oldstate = ctxt->state;
xmlChar fn[50];
xmlChar *fullname;
fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
if (fullname == NULL) {
xmlVErrMemory(ctxt);
return(0);
}
switch (content->ocur) {
case XML_ELEMENT_CONTENT_ONCE:
ctxt->state = xmlAutomataNewTransition(ctxt->am,
ctxt->state, NULL, fullname, NULL);
break;
case XML_ELEMENT_CONTENT_OPT:
ctxt->state = xmlAutomataNewTransition(ctxt->am,
ctxt->state, NULL, fullname, NULL);
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
break;
case XML_ELEMENT_CONTENT_PLUS:
ctxt->state = xmlAutomataNewTransition(ctxt->am,
ctxt->state, NULL, fullname, NULL);
xmlAutomataNewTransition(ctxt->am, ctxt->state,
ctxt->state, fullname, NULL);
break;
case XML_ELEMENT_CONTENT_MULT:
ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
ctxt->state, NULL);
xmlAutomataNewTransition(ctxt->am,
ctxt->state, ctxt->state, fullname, NULL);
break;
}
if ((fullname != fn) && (fullname != content->name))
xmlFree(fullname);
break;
}
case XML_ELEMENT_CONTENT_SEQ: {
xmlAutomataStatePtr oldstate, oldend;
xmlElementContentOccur ocur;
/*
* Simply iterate over the content
*/
oldstate = ctxt->state;
ocur = content->ocur;
if (ocur != XML_ELEMENT_CONTENT_ONCE) {
ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
oldstate = ctxt->state;
}
do {
if (xmlValidBuildAContentModel(content->c1, ctxt, name) == 0)
return(0);
content = content->c2;
} while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
(content->ocur == XML_ELEMENT_CONTENT_ONCE));
if (xmlValidBuildAContentModel(content, ctxt, name) == 0)
return(0);
oldend = ctxt->state;
ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
switch (ocur) {
case XML_ELEMENT_CONTENT_ONCE:
break;
case XML_ELEMENT_CONTENT_OPT:
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
break;
case XML_ELEMENT_CONTENT_MULT:
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
break;
case XML_ELEMENT_CONTENT_PLUS:
xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
break;
}
break;
}
case XML_ELEMENT_CONTENT_OR: {
xmlAutomataStatePtr oldstate, oldend;
xmlElementContentOccur ocur;
ocur = content->ocur;
if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
(ocur == XML_ELEMENT_CONTENT_MULT)) {
ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
ctxt->state, NULL);
}
oldstate = ctxt->state;
oldend = xmlAutomataNewState(ctxt->am);
/*
* iterate over the subtypes and remerge the end with an
* epsilon transition
*/
do {
ctxt->state = oldstate;
if (xmlValidBuildAContentModel(content->c1, ctxt, name) == 0)
return(0);
xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
content = content->c2;
} while ((content->type == XML_ELEMENT_CONTENT_OR) &&
(content->ocur == XML_ELEMENT_CONTENT_ONCE));
ctxt->state = oldstate;
if (xmlValidBuildAContentModel(content, ctxt, name) == 0)
return(0);
xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
switch (ocur) {
case XML_ELEMENT_CONTENT_ONCE:
break;
case XML_ELEMENT_CONTENT_OPT:
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
break;
case XML_ELEMENT_CONTENT_MULT:
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
break;
case XML_ELEMENT_CONTENT_PLUS:
xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
break;
}
break;
}
default:
xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
"ContentModel broken for element %s\n",
(const char *) name);
return(0);
}
return(1);
}
/**
* xmlValidBuildContentModel:
* @ctxt: a validation context
* @elem: an element declaration node
*
* DEPRECATED: Internal function, don't use.
*
* (Re)Build the automata associated to the content model of this
* element
*
* Returns 1 in case of success, 0 in case of error
*/
int
xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
int ret = 0;
if ((ctxt == NULL) || (elem == NULL))
return(0);
if (elem->type != XML_ELEMENT_DECL)
return(0);
if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
return(1);
/* TODO: should we rebuild in this case ? */
if (elem->contModel != NULL) {
if (!xmlRegexpIsDeterminist(elem->contModel)) {
ctxt->valid = 0;
return(0);
}
return(1);
}
ctxt->am = xmlNewAutomata();
if (ctxt->am == NULL) {
xmlVErrMemory(ctxt);
return(0);
}
ctxt->state = xmlAutomataGetInitState(ctxt->am);
if (xmlValidBuildAContentModel(elem->content, ctxt, elem->name) == 0)
goto done;
xmlAutomataSetFinalState(ctxt->am, ctxt->state);
elem->contModel = xmlAutomataCompile(ctxt->am);
if (elem->contModel == NULL) {
xmlVErrMemory(ctxt);
goto done;
}
if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
char expr[5000];
expr[0] = 0;
xmlSnprintfElementContent(expr, 5000, elem->content, 1);
xmlErrValidNode(ctxt, (xmlNodePtr) elem,
XML_DTD_CONTENT_NOT_DETERMINIST,
"Content model of %s is not deterministic: %s\n",
elem->name, BAD_CAST expr, NULL);
ctxt->valid = 0;
goto done;
}
ret = 1;
done:
ctxt->state = NULL;
xmlFreeAutomata(ctxt->am);
ctxt->am = NULL;
return(ret);
}
#endif /* LIBXML_REGEXP_ENABLED */
/****************************************************************
* *
* Util functions for data allocation/deallocation *
* *
****************************************************************/
/**
* xmlNewValidCtxt:
*
* Allocate a validation context structure.
*
* Returns NULL if not, otherwise the new validation context structure
*/
xmlValidCtxtPtr xmlNewValidCtxt(void) {
xmlValidCtxtPtr ret;
ret = xmlMalloc(sizeof (xmlValidCtxt));
if (ret == NULL)
return (NULL);
(void) memset(ret, 0, sizeof (xmlValidCtxt));
return (ret);
}
/**
* xmlFreeValidCtxt:
* @cur: the validation context to free
*
* Free a validation context structure.
*/
void
xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
if (cur == NULL)
return;
if (cur->vstateTab != NULL)
xmlFree(cur->vstateTab);
if (cur->nodeTab != NULL)
xmlFree(cur->nodeTab);
xmlFree(cur);
}
#endif /* LIBXML_VALID_ENABLED */
/**
* xmlNewDocElementContent:
* @doc: the document
* @name: the subelement name or NULL
* @type: the type of element content decl
*
* Allocate an element content structure for the document.
*
* Returns NULL if not, otherwise the new element content structure
*/
xmlElementContentPtr
xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name,
xmlElementContentType type) {
xmlElementContentPtr ret;
xmlDictPtr dict = NULL;
if (doc != NULL)
dict = doc->dict;
switch(type) {
case XML_ELEMENT_CONTENT_ELEMENT:
if (name == NULL) {
xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewElementContent : name == NULL !\n",
NULL);
}
break;
case XML_ELEMENT_CONTENT_PCDATA:
case XML_ELEMENT_CONTENT_SEQ:
case XML_ELEMENT_CONTENT_OR:
if (name != NULL) {
xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
"xmlNewElementContent : name != NULL !\n",
NULL);
}
break;
default:
xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
"Internal: ELEMENT content corrupted invalid type\n",
NULL);
return(NULL);
}
ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
if (ret == NULL)
return(NULL);
memset(ret, 0, sizeof(xmlElementContent));
ret->type = type;
ret->ocur = XML_ELEMENT_CONTENT_ONCE;
if (name != NULL) {
int l;
const xmlChar *tmp;
tmp = xmlSplitQName3(name, &l);
if (tmp == NULL) {
if (dict == NULL)
ret->name = xmlStrdup(name);
else
ret->name = xmlDictLookup(dict, name, -1);
} else {
if (dict == NULL) {
ret->prefix = xmlStrndup(name, l);
ret->name = xmlStrdup(tmp);
} else {
ret->prefix = xmlDictLookup(dict, name, l);
ret->name = xmlDictLookup(dict, tmp, -1);
}
if (ret->prefix == NULL)
goto error;
}
if (ret->name == NULL)
goto error;
}
return(ret);
error:
xmlFreeDocElementContent(doc, ret);
return(NULL);
}
/**
* xmlNewElementContent:
* @name: the subelement name or NULL
* @type: the type of element content decl
*
* Allocate an element content structure.
* Deprecated in favor of xmlNewDocElementContent
*
* Returns NULL if not, otherwise the new element content structure
*/
xmlElementContentPtr
xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
return(xmlNewDocElementContent(NULL, name, type));
}
/**
* xmlCopyDocElementContent:
* @doc: the document owning the element declaration
* @cur: An element content pointer.
*
* Build a copy of an element content description.
*
* Returns the new xmlElementContentPtr or NULL in case of error.
*/
xmlElementContentPtr
xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
xmlElementContentPtr ret = NULL, prev = NULL, tmp;
xmlDictPtr dict = NULL;
if (cur == NULL) return(NULL);
if (doc != NULL)
dict = doc->dict;
ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
if (ret == NULL)
return(NULL);
memset(ret, 0, sizeof(xmlElementContent));
ret->type = cur->type;
ret->ocur = cur->ocur;
if (cur->name != NULL) {
if (dict)
ret->name = xmlDictLookup(dict, cur->name, -1);
else
ret->name = xmlStrdup(cur->name);
if (ret->name == NULL)
goto error;
}
if (cur->prefix != NULL) {
if (dict)
ret->prefix = xmlDictLookup(dict, cur->prefix, -1);
else
ret->prefix = xmlStrdup(cur->prefix);
if (ret->prefix == NULL)
goto error;
}
if (cur->c1 != NULL) {
ret->c1 = xmlCopyDocElementContent(doc, cur->c1);
if (ret->c1 == NULL)
goto error;
ret->c1->parent = ret;
}
if (cur->c2 != NULL) {
prev = ret;
cur = cur->c2;
while (cur != NULL) {
tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
if (tmp == NULL)
goto error;
memset(tmp, 0, sizeof(xmlElementContent));
tmp->type = cur->type;
tmp->ocur = cur->ocur;
prev->c2 = tmp;
tmp->parent = prev;
if (cur->name != NULL) {
if (dict)
tmp->name = xmlDictLookup(dict, cur->name, -1);
else
tmp->name = xmlStrdup(cur->name);
if (tmp->name == NULL)
goto error;
}
if (cur->prefix != NULL) {
if (dict)
tmp->prefix = xmlDictLookup(dict, cur->prefix, -1);
else
tmp->prefix = xmlStrdup(cur->prefix);
if (tmp->prefix == NULL)
goto error;
}
if (cur->c1 != NULL) {
tmp->c1 = xmlCopyDocElementContent(doc,cur->c1);
if (tmp->c1 == NULL)
goto error;
tmp->c1->parent = tmp;
}
prev = tmp;
cur = cur->c2;
}
}
return(ret);
error:
xmlFreeElementContent(ret);
return(NULL);
}
/**
* xmlCopyElementContent:
* @cur: An element content pointer.
*
* Build a copy of an element content description.
* Deprecated, use xmlCopyDocElementContent instead
*
* Returns the new xmlElementContentPtr or NULL in case of error.
*/
xmlElementContentPtr
xmlCopyElementContent(xmlElementContentPtr cur) {
return(xmlCopyDocElementContent(NULL, cur));
}
/**
* xmlFreeDocElementContent:
* @doc: the document owning the element declaration
* @cur: the element content tree to free
*
* Free an element content structure. The whole subtree is removed.
*/
void
xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
xmlDictPtr dict = NULL;
size_t depth = 0;
if (cur == NULL)
return;
if (doc != NULL)
dict = doc->dict;
while (1) {
xmlElementContentPtr parent;
while ((cur->c1 != NULL) || (cur->c2 != NULL)) {
cur = (cur->c1 != NULL) ? cur->c1 : cur->c2;
depth += 1;
}
switch (cur->type) {
case XML_ELEMENT_CONTENT_PCDATA:
case XML_ELEMENT_CONTENT_ELEMENT:
case XML_ELEMENT_CONTENT_SEQ:
case XML_ELEMENT_CONTENT_OR:
break;
default:
xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
"Internal: ELEMENT content corrupted invalid type\n",
NULL);
return;
}
if (dict) {
if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
xmlFree((xmlChar *) cur->name);
if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix)))
xmlFree((xmlChar *) cur->prefix);
} else {
if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
}
parent = cur->parent;
if ((depth == 0) || (parent == NULL)) {
xmlFree(cur);
break;
}
if (cur == parent->c1)
parent->c1 = NULL;
else
parent->c2 = NULL;
xmlFree(cur);
if (parent->c2 != NULL) {
cur = parent->c2;
} else {
depth -= 1;
cur = parent;
}
}
}
/**
* xmlFreeElementContent:
* @cur: the element content tree to free
*
* Free an element content structure. The whole subtree is removed.
* Deprecated, use xmlFreeDocElementContent instead
*/
void
xmlFreeElementContent(xmlElementContentPtr cur) {
xmlFreeDocElementContent(NULL, cur);
}
#ifdef LIBXML_OUTPUT_ENABLED
/**
* xmlSprintfElementContent:
* @buf: an output buffer
* @content: An element table
* @englob: 1 if one must print the englobing parenthesis, 0 otherwise
*
* Deprecated, unsafe, use xmlSnprintfElementContent
*/
void
xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
xmlElementContentPtr content ATTRIBUTE_UNUSED,
int englob ATTRIBUTE_UNUSED) {
}
#endif /* LIBXML_OUTPUT_ENABLED */
/**