-
Notifications
You must be signed in to change notification settings - Fork 25
/
xpath.c
13566 lines (12543 loc) · 366 KB
/
xpath.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
/*
* xpath.c: XML Path Language implementation
* XPath is a language for addressing parts of an XML document,
* designed to be used by both XSLT and XPointer
*
* Reference: W3C Recommendation 16 November 1999
* http://www.w3.org/TR/1999/REC-xpath-19991116
* Public reference:
* http://www.w3.org/TR/xpath
*
* See Copyright for the status of this software
*
* Author: daniel@veillard.com
*
*/
/* To avoid EBCDIC trouble when parsing on zOS */
#if defined(__MVS__)
#pragma convert("ISO8859-1")
#endif
#define IN_LIBXML
#include "libxml.h"
#include <limits.h>
#include <string.h>
#include <stddef.h>
#include <math.h>
#include <float.h>
#include <ctype.h>
#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/parserInternals.h>
#include <libxml/hash.h>
#ifdef LIBXML_XPTR_LOCS_ENABLED
#include <libxml/xpointer.h>
#endif
#ifdef LIBXML_DEBUG_ENABLED
#include <libxml/debugXML.h>
#endif
#include <libxml/xmlerror.h>
#include <libxml/threads.h>
#ifdef LIBXML_PATTERN_ENABLED
#include <libxml/pattern.h>
#endif
#include "private/buf.h"
#include "private/error.h"
#include "private/xpath.h"
/* Disabled for now */
#if 0
#ifdef LIBXML_PATTERN_ENABLED
#define XPATH_STREAMING
#endif
#endif
/**
* WITH_TIM_SORT:
*
* Use the Timsort algorithm provided in timsort.h to sort
* nodeset as this is a great improvement over the old Shell sort
* used in xmlXPathNodeSetSort()
*/
#define WITH_TIM_SORT
/*
* XP_OPTIMIZED_NON_ELEM_COMPARISON:
* If defined, this will use xmlXPathCmpNodesExt() instead of
* xmlXPathCmpNodes(). The new function is optimized comparison of
* non-element nodes; actually it will speed up comparison only if
* xmlXPathOrderDocElems() was called in order to index the elements of
* a tree in document order; Libxslt does such an indexing, thus it will
* benefit from this optimization.
*/
#define XP_OPTIMIZED_NON_ELEM_COMPARISON
/*
* XP_OPTIMIZED_FILTER_FIRST:
* If defined, this will optimize expressions like "key('foo', 'val')[b][1]"
* in a way, that it stop evaluation at the first node.
*/
#define XP_OPTIMIZED_FILTER_FIRST
/*
* XPATH_MAX_STEPS:
* when compiling an XPath expression we arbitrary limit the maximum
* number of step operation in the compiled expression. 1000000 is
* an insanely large value which should never be reached under normal
* circumstances
*/
#define XPATH_MAX_STEPS 1000000
/*
* XPATH_MAX_STACK_DEPTH:
* when evaluating an XPath expression we arbitrary limit the maximum
* number of object allowed to be pushed on the stack. 1000000 is
* an insanely large value which should never be reached under normal
* circumstances
*/
#define XPATH_MAX_STACK_DEPTH 1000000
/*
* XPATH_MAX_NODESET_LENGTH:
* when evaluating an XPath expression nodesets are created and we
* arbitrary limit the maximum length of those node set. 10000000 is
* an insanely large value which should never be reached under normal
* circumstances, one would first need to construct an in memory tree
* with more than 10 millions nodes.
*/
#define XPATH_MAX_NODESET_LENGTH 10000000
/*
* XPATH_MAX_RECRUSION_DEPTH:
* Maximum amount of nested functions calls when parsing or evaluating
* expressions
*/
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
#define XPATH_MAX_RECURSION_DEPTH 500
#elif defined(_WIN32)
/* Windows typically limits stack size to 1MB. */
#define XPATH_MAX_RECURSION_DEPTH 1000
#else
#define XPATH_MAX_RECURSION_DEPTH 5000
#endif
/*
* TODO:
* There are a few spots where some tests are done which depend upon ascii
* data. These should be enhanced for full UTF8 support (see particularly
* any use of the macros IS_ASCII_CHARACTER and IS_ASCII_DIGIT)
*/
#if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
/************************************************************************
* *
* Floating point stuff *
* *
************************************************************************/
double xmlXPathNAN = 0.0;
double xmlXPathPINF = 0.0;
double xmlXPathNINF = 0.0;
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4723) // potential divide by 0
#endif
/**
* xmlXPathInit:
*
* DEPRECATED: Alias for xmlInitParser.
*/
void
xmlXPathInit(void) {
xmlInitParser();
}
/**
* xmlInitXPathInternal:
*
* Initialize the XPath environment
*/
ATTRIBUTE_NO_SANITIZE("float-divide-by-zero")
void
xmlInitXPathInternal(void) {
#if defined(NAN) && defined(INFINITY)
xmlXPathNAN = NAN;
xmlXPathPINF = INFINITY;
xmlXPathNINF = -INFINITY;
#else
/* MSVC doesn't allow division by zero in constant expressions. */
double zero = 0.0;
xmlXPathNAN = 0.0 / zero;
xmlXPathPINF = 1.0 / zero;
xmlXPathNINF = -xmlXPathPINF;
#endif
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
/**
* xmlXPathIsNaN:
* @val: a double value
*
* Checks whether a double is a NaN.
*
* Returns 1 if the value is a NaN, 0 otherwise
*/
int
xmlXPathIsNaN(double val) {
#ifdef isnan
return isnan(val);
#else
return !(val == val);
#endif
}
/**
* xmlXPathIsInf:
* @val: a double value
*
* Checks whether a double is an infinity.
*
* Returns 1 if the value is +Infinite, -1 if -Infinite, 0 otherwise
*/
int
xmlXPathIsInf(double val) {
#ifdef isinf
return isinf(val) ? (val > 0 ? 1 : -1) : 0;
#else
if (val >= xmlXPathPINF)
return 1;
if (val <= -xmlXPathPINF)
return -1;
return 0;
#endif
}
#endif /* SCHEMAS or XPATH */
#ifdef LIBXML_XPATH_ENABLED
/*
* TODO: when compatibility allows remove all "fake node libxslt" strings
* the test should just be name[0] = ' '
*/
static const xmlNs xmlXPathXMLNamespaceStruct = {
NULL,
XML_NAMESPACE_DECL,
XML_XML_NAMESPACE,
BAD_CAST "xml",
NULL,
NULL
};
static const xmlNs *const xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct;
#ifndef LIBXML_THREAD_ENABLED
/*
* Optimizer is disabled only when threaded apps are detected while
* the library ain't compiled for thread safety.
*/
static int xmlXPathDisableOptimizer = 0;
#endif
static void
xmlXPathNodeSetClear(xmlNodeSetPtr set, int hasNsNodes);
#ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON
/**
* xmlXPathCmpNodesExt:
* @node1: the first node
* @node2: the second node
*
* Compare two nodes w.r.t document order.
* This one is optimized for handling of non-element nodes.
*
* Returns -2 in case of error 1 if first point < second point, 0 if
* it's the same node, -1 otherwise
*/
static int
xmlXPathCmpNodesExt(xmlNodePtr node1, xmlNodePtr node2) {
int depth1, depth2;
int misc = 0, precedence1 = 0, precedence2 = 0;
xmlNodePtr miscNode1 = NULL, miscNode2 = NULL;
xmlNodePtr cur, root;
ptrdiff_t l1, l2;
if ((node1 == NULL) || (node2 == NULL))
return(-2);
if (node1 == node2)
return(0);
/*
* a couple of optimizations which will avoid computations in most cases
*/
switch (node1->type) {
case XML_ELEMENT_NODE:
if (node2->type == XML_ELEMENT_NODE) {
if ((0 > (ptrdiff_t) node1->content) &&
(0 > (ptrdiff_t) node2->content) &&
(node1->doc == node2->doc))
{
l1 = -((ptrdiff_t) node1->content);
l2 = -((ptrdiff_t) node2->content);
if (l1 < l2)
return(1);
if (l1 > l2)
return(-1);
} else
goto turtle_comparison;
}
break;
case XML_ATTRIBUTE_NODE:
precedence1 = 1; /* element is owner */
miscNode1 = node1;
node1 = node1->parent;
misc = 1;
break;
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_COMMENT_NODE:
case XML_PI_NODE: {
miscNode1 = node1;
/*
* Find nearest element node.
*/
if (node1->prev != NULL) {
do {
node1 = node1->prev;
if (node1->type == XML_ELEMENT_NODE) {
precedence1 = 3; /* element in prev-sibl axis */
break;
}
if (node1->prev == NULL) {
precedence1 = 2; /* element is parent */
/*
* URGENT TODO: Are there any cases, where the
* parent of such a node is not an element node?
*/
node1 = node1->parent;
break;
}
} while (1);
} else {
precedence1 = 2; /* element is parent */
node1 = node1->parent;
}
if ((node1 == NULL) || (node1->type != XML_ELEMENT_NODE) ||
(0 <= (ptrdiff_t) node1->content)) {
/*
* Fallback for whatever case.
*/
node1 = miscNode1;
precedence1 = 0;
} else
misc = 1;
}
break;
case XML_NAMESPACE_DECL:
/*
* TODO: why do we return 1 for namespace nodes?
*/
return(1);
default:
break;
}
switch (node2->type) {
case XML_ELEMENT_NODE:
break;
case XML_ATTRIBUTE_NODE:
precedence2 = 1; /* element is owner */
miscNode2 = node2;
node2 = node2->parent;
misc = 1;
break;
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
case XML_COMMENT_NODE:
case XML_PI_NODE: {
miscNode2 = node2;
if (node2->prev != NULL) {
do {
node2 = node2->prev;
if (node2->type == XML_ELEMENT_NODE) {
precedence2 = 3; /* element in prev-sibl axis */
break;
}
if (node2->prev == NULL) {
precedence2 = 2; /* element is parent */
node2 = node2->parent;
break;
}
} while (1);
} else {
precedence2 = 2; /* element is parent */
node2 = node2->parent;
}
if ((node2 == NULL) || (node2->type != XML_ELEMENT_NODE) ||
(0 <= (ptrdiff_t) node2->content))
{
node2 = miscNode2;
precedence2 = 0;
} else
misc = 1;
}
break;
case XML_NAMESPACE_DECL:
return(1);
default:
break;
}
if (misc) {
if (node1 == node2) {
if (precedence1 == precedence2) {
/*
* The ugly case; but normally there aren't many
* adjacent non-element nodes around.
*/
cur = miscNode2->prev;
while (cur != NULL) {
if (cur == miscNode1)
return(1);
if (cur->type == XML_ELEMENT_NODE)
return(-1);
cur = cur->prev;
}
return (-1);
} else {
/*
* Evaluate based on higher precedence wrt to the element.
* TODO: This assumes attributes are sorted before content.
* Is this 100% correct?
*/
if (precedence1 < precedence2)
return(1);
else
return(-1);
}
}
/*
* Special case: One of the helper-elements is contained by the other.
* <foo>
* <node2>
* <node1>Text-1(precedence1 == 2)</node1>
* </node2>
* Text-6(precedence2 == 3)
* </foo>
*/
if ((precedence2 == 3) && (precedence1 > 1)) {
cur = node1->parent;
while (cur) {
if (cur == node2)
return(1);
cur = cur->parent;
}
}
if ((precedence1 == 3) && (precedence2 > 1)) {
cur = node2->parent;
while (cur) {
if (cur == node1)
return(-1);
cur = cur->parent;
}
}
}
/*
* Speedup using document order if available.
*/
if ((node1->type == XML_ELEMENT_NODE) &&
(node2->type == XML_ELEMENT_NODE) &&
(0 > (ptrdiff_t) node1->content) &&
(0 > (ptrdiff_t) node2->content) &&
(node1->doc == node2->doc)) {
l1 = -((ptrdiff_t) node1->content);
l2 = -((ptrdiff_t) node2->content);
if (l1 < l2)
return(1);
if (l1 > l2)
return(-1);
}
turtle_comparison:
if (node1 == node2->prev)
return(1);
if (node1 == node2->next)
return(-1);
/*
* compute depth to root
*/
for (depth2 = 0, cur = node2; cur->parent != NULL; cur = cur->parent) {
if (cur->parent == node1)
return(1);
depth2++;
}
root = cur;
for (depth1 = 0, cur = node1; cur->parent != NULL; cur = cur->parent) {
if (cur->parent == node2)
return(-1);
depth1++;
}
/*
* Distinct document (or distinct entities :-( ) case.
*/
if (root != cur) {
return(-2);
}
/*
* get the nearest common ancestor.
*/
while (depth1 > depth2) {
depth1--;
node1 = node1->parent;
}
while (depth2 > depth1) {
depth2--;
node2 = node2->parent;
}
while (node1->parent != node2->parent) {
node1 = node1->parent;
node2 = node2->parent;
/* should not happen but just in case ... */
if ((node1 == NULL) || (node2 == NULL))
return(-2);
}
/*
* Find who's first.
*/
if (node1 == node2->prev)
return(1);
if (node1 == node2->next)
return(-1);
/*
* Speedup using document order if available.
*/
if ((node1->type == XML_ELEMENT_NODE) &&
(node2->type == XML_ELEMENT_NODE) &&
(0 > (ptrdiff_t) node1->content) &&
(0 > (ptrdiff_t) node2->content) &&
(node1->doc == node2->doc)) {
l1 = -((ptrdiff_t) node1->content);
l2 = -((ptrdiff_t) node2->content);
if (l1 < l2)
return(1);
if (l1 > l2)
return(-1);
}
for (cur = node1->next;cur != NULL;cur = cur->next)
if (cur == node2)
return(1);
return(-1); /* assume there is no sibling list corruption */
}
#endif /* XP_OPTIMIZED_NON_ELEM_COMPARISON */
/*
* Wrapper for the Timsort algorithm from timsort.h
*/
#ifdef WITH_TIM_SORT
#define SORT_NAME libxml_domnode
#define SORT_TYPE xmlNodePtr
/**
* wrap_cmp:
* @x: a node
* @y: another node
*
* Comparison function for the Timsort implementation
*
* Returns -2 in case of error -1 if first point < second point, 0 if
* it's the same node, +1 otherwise
*/
static
int wrap_cmp( xmlNodePtr x, xmlNodePtr y );
#ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON
static int wrap_cmp( xmlNodePtr x, xmlNodePtr y )
{
int res = xmlXPathCmpNodesExt(x, y);
return res == -2 ? res : -res;
}
#else
static int wrap_cmp( xmlNodePtr x, xmlNodePtr y )
{
int res = xmlXPathCmpNodes(x, y);
return res == -2 ? res : -res;
}
#endif
#define SORT_CMP(x, y) (wrap_cmp(x, y))
#include "timsort.h"
#endif /* WITH_TIM_SORT */
/************************************************************************
* *
* Error handling routines *
* *
************************************************************************/
/**
* XP_ERRORNULL:
* @X: the error code
*
* Macro to raise an XPath error and return NULL.
*/
#define XP_ERRORNULL(X) \
{ xmlXPathErr(ctxt, X); return(NULL); }
/*
* The array xmlXPathErrorMessages corresponds to the enum xmlXPathError
*/
static const char* const xmlXPathErrorMessages[] = {
"Ok\n",
"Number encoding\n",
"Unfinished literal\n",
"Start of literal\n",
"Expected $ for variable reference\n",
"Undefined variable\n",
"Invalid predicate\n",
"Invalid expression\n",
"Missing closing curly brace\n",
"Unregistered function\n",
"Invalid operand\n",
"Invalid type\n",
"Invalid number of arguments\n",
"Invalid context size\n",
"Invalid context position\n",
"Memory allocation error\n",
"Syntax error\n",
"Resource error\n",
"Sub resource error\n",
"Undefined namespace prefix\n",
"Encoding error\n",
"Char out of XML range\n",
"Invalid or incomplete context\n",
"Stack usage error\n",
"Forbidden variable\n",
"Operation limit exceeded\n",
"Recursion limit exceeded\n",
"?? Unknown error ??\n" /* Must be last in the list! */
};
#define MAXERRNO ((int)(sizeof(xmlXPathErrorMessages) / \
sizeof(xmlXPathErrorMessages[0])) - 1)
/**
* xmlXPathErrMemory:
* @ctxt: an XPath context
*
* Handle a memory allocation failure.
*/
void
xmlXPathErrMemory(xmlXPathContextPtr ctxt)
{
if (ctxt == NULL)
return;
xmlRaiseMemoryError(ctxt->error, NULL, ctxt->userData, XML_FROM_XPATH,
&ctxt->lastError);
}
/**
* xmlXPathPErrMemory:
* @ctxt: an XPath parser context
*
* Handle a memory allocation failure.
*/
void
xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt)
{
if (ctxt == NULL)
return;
ctxt->error = XPATH_MEMORY_ERROR;
xmlXPathErrMemory(ctxt->context);
}
/**
* xmlXPathErr:
* @ctxt: a XPath parser context
* @code: the error code
*
* Handle an XPath error
*/
void
xmlXPathErr(xmlXPathParserContextPtr ctxt, int code)
{
xmlStructuredErrorFunc schannel = NULL;
xmlGenericErrorFunc channel = NULL;
void *data = NULL;
xmlNodePtr node = NULL;
int res;
if (ctxt == NULL)
return;
if ((code < 0) || (code > MAXERRNO))
code = MAXERRNO;
/* Only report the first error */
if (ctxt->error != 0)
return;
ctxt->error = code;
if (ctxt->context != NULL) {
xmlErrorPtr err = &ctxt->context->lastError;
/* Don't overwrite memory error. */
if (err->code == XML_ERR_NO_MEMORY)
return;
/* cleanup current last error */
xmlResetError(err);
err->domain = XML_FROM_XPATH;
err->code = code + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK;
err->level = XML_ERR_ERROR;
if (ctxt->base != NULL) {
err->str1 = (char *) xmlStrdup(ctxt->base);
if (err->str1 == NULL) {
xmlXPathPErrMemory(ctxt);
return;
}
}
err->int1 = ctxt->cur - ctxt->base;
err->node = ctxt->context->debugNode;
schannel = ctxt->context->error;
data = ctxt->context->userData;
node = ctxt->context->debugNode;
}
if (schannel == NULL) {
channel = xmlGenericError;
data = xmlGenericErrorContext;
}
res = __xmlRaiseError(schannel, channel, data, NULL, node, XML_FROM_XPATH,
code + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
XML_ERR_ERROR, NULL, 0,
(const char *) ctxt->base, NULL, NULL,
ctxt->cur - ctxt->base, 0,
"%s", xmlXPathErrorMessages[code]);
if (res < 0)
xmlXPathPErrMemory(ctxt);
}
/**
* xmlXPatherror:
* @ctxt: the XPath Parser context
* @file: the file name
* @line: the line number
* @no: the error number
*
* Formats an error message.
*/
void
xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED,
int line ATTRIBUTE_UNUSED, int no) {
xmlXPathErr(ctxt, no);
}
/**
* xmlXPathCheckOpLimit:
* @ctxt: the XPath Parser context
* @opCount: the number of operations to be added
*
* Adds opCount to the running total of operations and returns -1 if the
* operation limit is exceeded. Returns 0 otherwise.
*/
static int
xmlXPathCheckOpLimit(xmlXPathParserContextPtr ctxt, unsigned long opCount) {
xmlXPathContextPtr xpctxt = ctxt->context;
if ((opCount > xpctxt->opLimit) ||
(xpctxt->opCount > xpctxt->opLimit - opCount)) {
xpctxt->opCount = xpctxt->opLimit;
xmlXPathErr(ctxt, XPATH_OP_LIMIT_EXCEEDED);
return(-1);
}
xpctxt->opCount += opCount;
return(0);
}
#define OP_LIMIT_EXCEEDED(ctxt, n) \
((ctxt->context->opLimit != 0) && (xmlXPathCheckOpLimit(ctxt, n) < 0))
/************************************************************************
* *
* Parser Types *
* *
************************************************************************/
/*
* Types are private:
*/
typedef enum {
XPATH_OP_END=0,
XPATH_OP_AND,
XPATH_OP_OR,
XPATH_OP_EQUAL,
XPATH_OP_CMP,
XPATH_OP_PLUS,
XPATH_OP_MULT,
XPATH_OP_UNION,
XPATH_OP_ROOT,
XPATH_OP_NODE,
XPATH_OP_COLLECT,
XPATH_OP_VALUE, /* 11 */
XPATH_OP_VARIABLE,
XPATH_OP_FUNCTION,
XPATH_OP_ARG,
XPATH_OP_PREDICATE,
XPATH_OP_FILTER, /* 16 */
XPATH_OP_SORT /* 17 */
#ifdef LIBXML_XPTR_LOCS_ENABLED
,XPATH_OP_RANGETO
#endif
} xmlXPathOp;
typedef enum {
AXIS_ANCESTOR = 1,
AXIS_ANCESTOR_OR_SELF,
AXIS_ATTRIBUTE,
AXIS_CHILD,
AXIS_DESCENDANT,
AXIS_DESCENDANT_OR_SELF,
AXIS_FOLLOWING,
AXIS_FOLLOWING_SIBLING,
AXIS_NAMESPACE,
AXIS_PARENT,
AXIS_PRECEDING,
AXIS_PRECEDING_SIBLING,
AXIS_SELF
} xmlXPathAxisVal;
typedef enum {
NODE_TEST_NONE = 0,
NODE_TEST_TYPE = 1,
NODE_TEST_PI = 2,
NODE_TEST_ALL = 3,
NODE_TEST_NS = 4,
NODE_TEST_NAME = 5
} xmlXPathTestVal;
typedef enum {
NODE_TYPE_NODE = 0,
NODE_TYPE_COMMENT = XML_COMMENT_NODE,
NODE_TYPE_TEXT = XML_TEXT_NODE,
NODE_TYPE_PI = XML_PI_NODE
} xmlXPathTypeVal;
typedef struct _xmlXPathStepOp xmlXPathStepOp;
typedef xmlXPathStepOp *xmlXPathStepOpPtr;
struct _xmlXPathStepOp {
xmlXPathOp op; /* The identifier of the operation */
int ch1; /* First child */
int ch2; /* Second child */
int value;
int value2;
int value3;
void *value4;
void *value5;
xmlXPathFunction cache;
void *cacheURI;
};
struct _xmlXPathCompExpr {
int nbStep; /* Number of steps in this expression */
int maxStep; /* Maximum number of steps allocated */
xmlXPathStepOp *steps; /* ops for computation of this expression */
int last; /* index of last step in expression */
xmlChar *expr; /* the expression being computed */
xmlDictPtr dict; /* the dictionary to use if any */
#ifdef XPATH_STREAMING
xmlPatternPtr stream;
#endif
};
/************************************************************************
* *
* Forward declarations *
* *
************************************************************************/
static void
xmlXPathReleaseObject(xmlXPathContextPtr ctxt, xmlXPathObjectPtr obj);
static int
xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt,
xmlXPathStepOpPtr op, xmlNodePtr *first);
static int
xmlXPathCompOpEvalToBoolean(xmlXPathParserContextPtr ctxt,
xmlXPathStepOpPtr op,
int isPredicate);
static void
xmlXPathFreeObjectEntry(void *obj, const xmlChar *name);
/************************************************************************
* *
* Parser Type functions *
* *
************************************************************************/
/**
* xmlXPathNewCompExpr:
*
* Create a new Xpath component
*
* Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error
*/
static xmlXPathCompExprPtr
xmlXPathNewCompExpr(void) {
xmlXPathCompExprPtr cur;
cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr));
if (cur == NULL)
return(NULL);
memset(cur, 0, sizeof(xmlXPathCompExpr));
cur->maxStep = 10;
cur->nbStep = 0;
cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep *
sizeof(xmlXPathStepOp));
if (cur->steps == NULL) {
xmlFree(cur);
return(NULL);
}
memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp));
cur->last = -1;
return(cur);
}
/**
* xmlXPathFreeCompExpr:
* @comp: an XPATH comp
*
* Free up the memory allocated by @comp
*/
void
xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp)
{
xmlXPathStepOpPtr op;
int i;
if (comp == NULL)
return;
if (comp->dict == NULL) {
for (i = 0; i < comp->nbStep; i++) {
op = &comp->steps[i];
if (op->value4 != NULL) {
if (op->op == XPATH_OP_VALUE)
xmlXPathFreeObject(op->value4);
else
xmlFree(op->value4);
}
if (op->value5 != NULL)
xmlFree(op->value5);
}
} else {
for (i = 0; i < comp->nbStep; i++) {
op = &comp->steps[i];
if (op->value4 != NULL) {
if (op->op == XPATH_OP_VALUE)
xmlXPathFreeObject(op->value4);
}
}
xmlDictFree(comp->dict);
}
if (comp->steps != NULL) {
xmlFree(comp->steps);
}
#ifdef XPATH_STREAMING
if (comp->stream != NULL) {
xmlFreePatternList(comp->stream);
}
#endif
if (comp->expr != NULL) {
xmlFree(comp->expr);
}
xmlFree(comp);
}
/**
* xmlXPathCompExprAdd:
* @comp: the compiled expression
* @ch1: first child index
* @ch2: second child index
* @op: an op
* @value: the first int value
* @value2: the second int value
* @value3: the third int value
* @value4: the first string value
* @value5: the second string value
*
* Add a step to an XPath Compiled Expression
*
* Returns -1 in case of failure, the index otherwise
*/
static int
xmlXPathCompExprAdd(xmlXPathParserContextPtr ctxt, int ch1, int ch2,
xmlXPathOp op, int value,
int value2, int value3, void *value4, void *value5) {
xmlXPathCompExprPtr comp = ctxt->comp;
if (comp->nbStep >= comp->maxStep) {
xmlXPathStepOp *real;
if (comp->maxStep >= XPATH_MAX_STEPS) {
xmlXPathPErrMemory(ctxt);
return(-1);
}
comp->maxStep *= 2;
real = (xmlXPathStepOp *) xmlRealloc(comp->steps,
comp->maxStep * sizeof(xmlXPathStepOp));
if (real == NULL) {