-
Notifications
You must be signed in to change notification settings - Fork 25
/
xpointer.c
2924 lines (2707 loc) · 74.1 KB
/
xpointer.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
/*
* xpointer.c : Code to handle XML Pointer
*
* Base implementation was made accordingly to
* W3C Candidate Recommendation 7 June 2000
* http://www.w3.org/TR/2000/CR-xptr-20000607
*
* Added support for the element() scheme described in:
* W3C Proposed Recommendation 13 November 2002
* http://www.w3.org/TR/2002/PR-xptr-element-20021113/
*
* See Copyright for the status of this software.
*
* 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"
/*
* TODO: better handling of error cases, the full expression should
* be parsed beforehand instead of a progressive evaluation
* TODO: Access into entities references are not supported now ...
* need a start to be able to pop out of entities refs since
* parent is the entity declaration, not the ref.
*/
#include <string.h>
#include <libxml/xpointer.h>
#include <libxml/xmlmemory.h>
#include <libxml/parserInternals.h>
#include <libxml/uri.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/xmlerror.h>
#ifdef LIBXML_XPTR_ENABLED
/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
#define XPTR_XMLNS_SCHEME
#include "private/error.h"
#include "private/xpath.h"
/************************************************************************
* *
* Some factorized error routines *
* *
************************************************************************/
/**
* xmlXPtrErr:
* @ctxt: an XPTR evaluation context
* @extra: extra information
*
* Handle an XPointer error
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlXPtrErr(xmlXPathParserContextPtr ctxt, int code,
const char * msg, const xmlChar *extra)
{
xmlStructuredErrorFunc serror = NULL;
void *data = NULL;
xmlNodePtr node = NULL;
int res;
if (ctxt == NULL)
return;
/* Only report the first error */
if (ctxt->error != 0)
return;
ctxt->error = code;
if (ctxt->context != NULL) {
xmlErrorPtr err = &ctxt->context->lastError;
/* cleanup current last error */
xmlResetError(err);
err->domain = XML_FROM_XPOINTER;
err->code = code;
err->level = XML_ERR_ERROR;
err->str1 = (char *) xmlStrdup(ctxt->base);
if (err->str1 == NULL) {
xmlXPathPErrMemory(ctxt);
return;
}
err->int1 = ctxt->cur - ctxt->base;
err->node = ctxt->context->debugNode;
serror = ctxt->context->error;
data = ctxt->context->userData;
node = ctxt->context->debugNode;
}
res = __xmlRaiseError(serror, NULL, data, NULL, node,
XML_FROM_XPOINTER, code, XML_ERR_ERROR, NULL, 0,
(const char *) extra, (const char *) ctxt->base,
NULL, ctxt->cur - ctxt->base, 0,
msg, extra);
if (res < 0)
xmlXPathPErrMemory(ctxt);
}
/************************************************************************
* *
* A few helper functions for child sequences *
* *
************************************************************************/
#ifdef LIBXML_XPTR_LOCS_ENABLED
/* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level);
/**
* xmlXPtrGetArity:
* @cur: the node
*
* Returns the number of child for an element, -1 in case of error
*/
static int
xmlXPtrGetArity(xmlNodePtr cur) {
int i;
if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
return(-1);
cur = cur->children;
for (i = 0;cur != NULL;cur = cur->next) {
if ((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_DOCUMENT_NODE) ||
(cur->type == XML_HTML_DOCUMENT_NODE)) {
i++;
}
}
return(i);
}
/**
* xmlXPtrGetIndex:
* @cur: the node
*
* Returns the index of the node in its parent children list, -1
* in case of error
*/
static int
xmlXPtrGetIndex(xmlNodePtr cur) {
int i;
if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
return(-1);
for (i = 1;cur != NULL;cur = cur->prev) {
if ((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_DOCUMENT_NODE) ||
(cur->type == XML_HTML_DOCUMENT_NODE)) {
i++;
}
}
return(i);
}
#endif /* LIBXML_XPTR_LOCS_ENABLED */
/**
* xmlXPtrGetNthChild:
* @cur: the node
* @no: the child number
*
* Returns the @no'th element child of @cur or NULL
*/
static xmlNodePtr
xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
int i;
if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
return(cur);
cur = cur->children;
for (i = 0;i <= no;cur = cur->next) {
if (cur == NULL)
return(cur);
if ((cur->type == XML_ELEMENT_NODE) ||
(cur->type == XML_DOCUMENT_NODE) ||
(cur->type == XML_HTML_DOCUMENT_NODE)) {
i++;
if (i == no)
break;
}
}
return(cur);
}
#ifdef LIBXML_XPTR_LOCS_ENABLED
/************************************************************************
* *
* Handling of XPointer specific types *
* *
************************************************************************/
/**
* xmlXPtrErrMemory:
* @extra: extra information
*
* Handle a redefinition of attribute error
*/
static void
xmlXPtrErrMemory(const char *extra ATTRIBUTE_UNUSED)
{
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_XPOINTER, NULL);
}
/**
* xmlXPtrCmpPoints:
* @node1: the first node
* @index1: the first index
* @node2: the second node
* @index2: the second index
*
* Compare two points w.r.t document order
*
* Returns -2 in case of error 1 if first point < second point, 0 if
* that's the same point, -1 otherwise
*/
static int
xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
if ((node1 == NULL) || (node2 == NULL))
return(-2);
/*
* a couple of optimizations which will avoid computations in most cases
*/
if (node1 == node2) {
if (index1 < index2)
return(1);
if (index1 > index2)
return(-1);
return(0);
}
return(xmlXPathCmpNodes(node1, node2));
}
/**
* xmlXPtrNewPoint:
* @node: the xmlNodePtr
* @indx: the indx within the node
*
* Create a new xmlXPathObjectPtr of type point
*
* Returns the newly created object.
*/
static xmlXPathObjectPtr
xmlXPtrNewPoint(xmlNodePtr node, int indx) {
xmlXPathObjectPtr ret;
if (node == NULL)
return(NULL);
if (indx < 0)
return(NULL);
ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
if (ret == NULL) {
xmlXPtrErrMemory("allocating point");
return(NULL);
}
memset(ret, 0 , sizeof(xmlXPathObject));
ret->type = XPATH_POINT;
ret->user = (void *) node;
ret->index = indx;
return(ret);
}
/**
* xmlXPtrRangeCheckOrder:
* @range: an object range
*
* Make sure the points in the range are in the right order
*/
static void
xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
int tmp;
xmlNodePtr tmp2;
if (range == NULL)
return;
if (range->type != XPATH_RANGE)
return;
if (range->user2 == NULL)
return;
tmp = xmlXPtrCmpPoints(range->user, range->index,
range->user2, range->index2);
if (tmp == -1) {
tmp2 = range->user;
range->user = range->user2;
range->user2 = tmp2;
tmp = range->index;
range->index = range->index2;
range->index2 = tmp;
}
}
/**
* xmlXPtrRangesEqual:
* @range1: the first range
* @range2: the second range
*
* Compare two ranges
*
* Returns 1 if equal, 0 otherwise
*/
static int
xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
if (range1 == range2)
return(1);
if ((range1 == NULL) || (range2 == NULL))
return(0);
if (range1->type != range2->type)
return(0);
if (range1->type != XPATH_RANGE)
return(0);
if (range1->user != range2->user)
return(0);
if (range1->index != range2->index)
return(0);
if (range1->user2 != range2->user2)
return(0);
if (range1->index2 != range2->index2)
return(0);
return(1);
}
/**
* xmlXPtrNewRangeInternal:
* @start: the starting node
* @startindex: the start index
* @end: the ending point
* @endindex: the ending index
*
* Internal function to create a new xmlXPathObjectPtr of type range
*
* Returns the newly created object.
*/
static xmlXPathObjectPtr
xmlXPtrNewRangeInternal(xmlNodePtr start, int startindex,
xmlNodePtr end, int endindex) {
xmlXPathObjectPtr ret;
/*
* Namespace nodes must be copied (see xmlXPathNodeSetDupNs).
* Disallow them for now.
*/
if ((start != NULL) && (start->type == XML_NAMESPACE_DECL))
return(NULL);
if ((end != NULL) && (end->type == XML_NAMESPACE_DECL))
return(NULL);
ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
if (ret == NULL) {
xmlXPtrErrMemory("allocating range");
return(NULL);
}
memset(ret, 0, sizeof(xmlXPathObject));
ret->type = XPATH_RANGE;
ret->user = start;
ret->index = startindex;
ret->user2 = end;
ret->index2 = endindex;
return(ret);
}
/**
* xmlXPtrNewRange:
* @start: the starting node
* @startindex: the start index
* @end: the ending point
* @endindex: the ending index
*
* Create a new xmlXPathObjectPtr of type range
*
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPtrNewRange(xmlNodePtr start, int startindex,
xmlNodePtr end, int endindex) {
xmlXPathObjectPtr ret;
if (start == NULL)
return(NULL);
if (end == NULL)
return(NULL);
if (startindex < 0)
return(NULL);
if (endindex < 0)
return(NULL);
ret = xmlXPtrNewRangeInternal(start, startindex, end, endindex);
xmlXPtrRangeCheckOrder(ret);
return(ret);
}
/**
* xmlXPtrNewRangePoints:
* @start: the starting point
* @end: the ending point
*
* Create a new xmlXPathObjectPtr of type range using 2 Points
*
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
xmlXPathObjectPtr ret;
if (start == NULL)
return(NULL);
if (end == NULL)
return(NULL);
if (start->type != XPATH_POINT)
return(NULL);
if (end->type != XPATH_POINT)
return(NULL);
ret = xmlXPtrNewRangeInternal(start->user, start->index, end->user,
end->index);
xmlXPtrRangeCheckOrder(ret);
return(ret);
}
/**
* xmlXPtrNewRangePointNode:
* @start: the starting point
* @end: the ending node
*
* Create a new xmlXPathObjectPtr of type range from a point to a node
*
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
xmlXPathObjectPtr ret;
if (start == NULL)
return(NULL);
if (end == NULL)
return(NULL);
if (start->type != XPATH_POINT)
return(NULL);
ret = xmlXPtrNewRangeInternal(start->user, start->index, end, -1);
xmlXPtrRangeCheckOrder(ret);
return(ret);
}
/**
* xmlXPtrNewRangeNodePoint:
* @start: the starting node
* @end: the ending point
*
* Create a new xmlXPathObjectPtr of type range from a node to a point
*
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
xmlXPathObjectPtr ret;
if (start == NULL)
return(NULL);
if (end == NULL)
return(NULL);
if (end->type != XPATH_POINT)
return(NULL);
ret = xmlXPtrNewRangeInternal(start, -1, end->user, end->index);
xmlXPtrRangeCheckOrder(ret);
return(ret);
}
/**
* xmlXPtrNewRangeNodes:
* @start: the starting node
* @end: the ending node
*
* Create a new xmlXPathObjectPtr of type range using 2 nodes
*
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
xmlXPathObjectPtr ret;
if (start == NULL)
return(NULL);
if (end == NULL)
return(NULL);
ret = xmlXPtrNewRangeInternal(start, -1, end, -1);
xmlXPtrRangeCheckOrder(ret);
return(ret);
}
/**
* xmlXPtrNewCollapsedRange:
* @start: the starting and ending node
*
* Create a new xmlXPathObjectPtr of type range using a single nodes
*
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPtrNewCollapsedRange(xmlNodePtr start) {
xmlXPathObjectPtr ret;
if (start == NULL)
return(NULL);
ret = xmlXPtrNewRangeInternal(start, -1, NULL, -1);
return(ret);
}
/**
* xmlXPtrNewRangeNodeObject:
* @start: the starting node
* @end: the ending object
*
* Create a new xmlXPathObjectPtr of type range from a not to an object
*
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
xmlNodePtr endNode;
int endIndex;
xmlXPathObjectPtr ret;
if (start == NULL)
return(NULL);
if (end == NULL)
return(NULL);
switch (end->type) {
case XPATH_POINT:
endNode = end->user;
endIndex = end->index;
break;
case XPATH_RANGE:
endNode = end->user2;
endIndex = end->index2;
break;
case XPATH_NODESET:
/*
* Empty set ...
*/
if ((end->nodesetval == NULL) || (end->nodesetval->nodeNr <= 0))
return(NULL);
endNode = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
endIndex = -1;
break;
default:
/* TODO */
return(NULL);
}
ret = xmlXPtrNewRangeInternal(start, -1, endNode, endIndex);
xmlXPtrRangeCheckOrder(ret);
return(ret);
}
#define XML_RANGESET_DEFAULT 10
/**
* xmlXPtrLocationSetCreate:
* @val: an initial xmlXPathObjectPtr, or NULL
*
* Create a new xmlLocationSetPtr of type double and of value @val
*
* Returns the newly created object.
*/
xmlLocationSetPtr
xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
xmlLocationSetPtr ret;
ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
if (ret == NULL) {
xmlXPtrErrMemory("allocating locationset");
return(NULL);
}
memset(ret, 0 , sizeof(xmlLocationSet));
if (val != NULL) {
ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
sizeof(xmlXPathObjectPtr));
if (ret->locTab == NULL) {
xmlXPtrErrMemory("allocating locationset");
xmlFree(ret);
return(NULL);
}
memset(ret->locTab, 0 ,
XML_RANGESET_DEFAULT * sizeof(xmlXPathObjectPtr));
ret->locMax = XML_RANGESET_DEFAULT;
ret->locTab[ret->locNr++] = val;
}
return(ret);
}
/**
* xmlXPtrLocationSetAdd:
* @cur: the initial range set
* @val: a new xmlXPathObjectPtr
*
* add a new xmlXPathObjectPtr to an existing LocationSet
* If the location already exist in the set @val is freed.
*/
void
xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
int i;
if ((cur == NULL) || (val == NULL)) return;
/*
* check against doublons
*/
for (i = 0;i < cur->locNr;i++) {
if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
xmlXPathFreeObject(val);
return;
}
}
/*
* grow the locTab if needed
*/
if (cur->locMax == 0) {
cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
sizeof(xmlXPathObjectPtr));
if (cur->locTab == NULL) {
xmlXPtrErrMemory("adding location to set");
return;
}
memset(cur->locTab, 0 ,
XML_RANGESET_DEFAULT * sizeof(xmlXPathObjectPtr));
cur->locMax = XML_RANGESET_DEFAULT;
} else if (cur->locNr == cur->locMax) {
xmlXPathObjectPtr *temp;
cur->locMax *= 2;
temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
sizeof(xmlXPathObjectPtr));
if (temp == NULL) {
xmlXPtrErrMemory("adding location to set");
return;
}
cur->locTab = temp;
}
cur->locTab[cur->locNr++] = val;
}
/**
* xmlXPtrLocationSetMerge:
* @val1: the first LocationSet
* @val2: the second LocationSet
*
* Merges two rangesets, all ranges from @val2 are added to @val1
*
* Returns val1 once extended or NULL in case of error.
*/
xmlLocationSetPtr
xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
int i;
if (val1 == NULL) return(NULL);
if (val2 == NULL) return(val1);
/*
* !!!!! this can be optimized a lot, knowing that both
* val1 and val2 already have unicity of their values.
*/
for (i = 0;i < val2->locNr;i++)
xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
return(val1);
}
/**
* xmlXPtrLocationSetDel:
* @cur: the initial range set
* @val: an xmlXPathObjectPtr
*
* Removes an xmlXPathObjectPtr from an existing LocationSet
*/
void
xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
int i;
if (cur == NULL) return;
if (val == NULL) return;
/*
* check against doublons
*/
for (i = 0;i < cur->locNr;i++)
if (cur->locTab[i] == val) break;
if (i >= cur->locNr) {
return;
}
cur->locNr--;
for (;i < cur->locNr;i++)
cur->locTab[i] = cur->locTab[i + 1];
cur->locTab[cur->locNr] = NULL;
}
/**
* xmlXPtrLocationSetRemove:
* @cur: the initial range set
* @val: the index to remove
*
* Removes an entry from an existing LocationSet list.
*/
void
xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
if (cur == NULL) return;
if (val >= cur->locNr) return;
cur->locNr--;
for (;val < cur->locNr;val++)
cur->locTab[val] = cur->locTab[val + 1];
cur->locTab[cur->locNr] = NULL;
}
/**
* xmlXPtrFreeLocationSet:
* @obj: the xmlLocationSetPtr to free
*
* Free the LocationSet compound (not the actual ranges !).
*/
void
xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
int i;
if (obj == NULL) return;
if (obj->locTab != NULL) {
for (i = 0;i < obj->locNr; i++) {
xmlXPathFreeObject(obj->locTab[i]);
}
xmlFree(obj->locTab);
}
xmlFree(obj);
}
/**
* xmlXPtrNewLocationSetNodes:
* @start: the start NodePtr value
* @end: the end NodePtr value or NULL
*
* Create a new xmlXPathObjectPtr of type LocationSet and initialize
* it with the single range made of the two nodes @start and @end
*
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
xmlXPathObjectPtr ret;
ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
if (ret == NULL) {
xmlXPtrErrMemory("allocating locationset");
return(NULL);
}
memset(ret, 0 , sizeof(xmlXPathObject));
ret->type = XPATH_LOCATIONSET;
if (end == NULL)
ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
else
ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
return(ret);
}
/**
* xmlXPtrNewLocationSetNodeSet:
* @set: a node set
*
* Create a new xmlXPathObjectPtr of type LocationSet and initialize
* it with all the nodes from @set
*
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
xmlXPathObjectPtr ret;
ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
if (ret == NULL) {
xmlXPtrErrMemory("allocating locationset");
return(NULL);
}
memset(ret, 0, sizeof(xmlXPathObject));
ret->type = XPATH_LOCATIONSET;
if (set != NULL) {
int i;
xmlLocationSetPtr newset;
newset = xmlXPtrLocationSetCreate(NULL);
if (newset == NULL)
return(ret);
for (i = 0;i < set->nodeNr;i++)
xmlXPtrLocationSetAdd(newset,
xmlXPtrNewCollapsedRange(set->nodeTab[i]));
ret->user = (void *) newset;
}
return(ret);
}
/**
* xmlXPtrWrapLocationSet:
* @val: the LocationSet value
*
* Wrap the LocationSet @val in a new xmlXPathObjectPtr
*
* Returns the newly created object.
*/
xmlXPathObjectPtr
xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
xmlXPathObjectPtr ret;
ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
if (ret == NULL) {
xmlXPtrErrMemory("allocating locationset");
return(NULL);
}
memset(ret, 0, sizeof(xmlXPathObject));
ret->type = XPATH_LOCATIONSET;
ret->user = (void *) val;
return(ret);
}
#endif /* LIBXML_XPTR_LOCS_ENABLED */
/************************************************************************
* *
* The parser *
* *
************************************************************************/
static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
/*
* Macros for accessing the content. Those should be used only by the parser,
* and not exported.
*
* Dirty macros, i.e. one need to make assumption on the context to use them
*
* CUR returns the current xmlChar value, i.e. a 8 bit value
* in ISO-Latin or UTF-8.
* This should be used internally by the parser
* only to compare to ASCII values otherwise it would break when
* running with UTF-8 encoding.
* NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
* to compare on ASCII based substring.
* SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
* strings within the parser.
* CURRENT Returns the current char value, with the full decoding of
* UTF-8 if we are using this mode. It returns an int.
* NEXT Skip to the next character, this does the proper decoding
* in UTF-8 mode. It also pop-up unfinished entities on the fly.
* It returns the pointer to the current xmlChar.
*/
#define CUR (*ctxt->cur)
#define SKIP(val) ctxt->cur += (val)
#define NXT(val) ctxt->cur[(val)]
#define SKIP_BLANKS \
while (IS_BLANK_CH(*(ctxt->cur))) NEXT
#define CURRENT (*ctxt->cur)
#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
/*
* xmlXPtrGetChildNo:
* @ctxt: the XPointer Parser context
* @index: the child number
*
* Move the current node of the nodeset on the stack to the
* given child if found
*/
static void
xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
xmlNodePtr cur = NULL;
xmlXPathObjectPtr obj;
xmlNodeSetPtr oldset;
CHECK_TYPE(XPATH_NODESET);
obj = valuePop(ctxt);
oldset = obj->nodesetval;
if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
xmlXPathFreeObject(obj);
valuePush(ctxt, xmlXPathNewNodeSet(NULL));
return;
}
cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
if (cur == NULL) {
xmlXPathFreeObject(obj);
valuePush(ctxt, xmlXPathNewNodeSet(NULL));
return;
}
oldset->nodeTab[0] = cur;
valuePush(ctxt, obj);
}
/**
* xmlXPtrEvalXPtrPart:
* @ctxt: the XPointer Parser context
* @name: the preparsed Scheme for the XPtrPart
*
* XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
* | Scheme '(' SchemeSpecificExpr ')'
*
* Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
*
* SchemeSpecificExpr ::= StringWithBalancedParens
*
* StringWithBalancedParens ::=
* [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
* [VC: Parenthesis escaping]
*
* XPtrExpr ::= Expr [VC: Parenthesis escaping]
*
* VC: Parenthesis escaping:
* The end of an XPointer part is signaled by the right parenthesis ")"
* character that is balanced with the left parenthesis "(" character
* that began the part. Any unbalanced parenthesis character inside the
* expression, even within literals, must be escaped with a circumflex (^)
* character preceding it. If the expression contains any literal
* occurrences of the circumflex, each must be escaped with an additional
* circumflex (that is, ^^). If the unescaped parentheses in the expression
* are not balanced, a syntax error results.
*
* Parse and evaluate an XPtrPart. Basically it generates the unescaped
* string and if the scheme is 'xpointer' it will call the XPath interpreter.
*
* TODO: there is no new scheme registration mechanism
*/
static void
xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
xmlChar *buffer, *cur;
int len;
int level;
if (name == NULL)
name = xmlXPathParseName(ctxt);
if (name == NULL)
XP_ERROR(XPATH_EXPR_ERROR);
if (CUR != '(') {
xmlFree(name);
XP_ERROR(XPATH_EXPR_ERROR);
}
NEXT;
level = 1;
len = xmlStrlen(ctxt->cur);
len++;
buffer = (xmlChar *) xmlMallocAtomic(len);
if (buffer == NULL) {
xmlXPathPErrMemory(ctxt);
xmlFree(name);
return;
}
cur = buffer;
while (CUR != 0) {
if (CUR == ')') {
level--;
if (level == 0) {
NEXT;
break;
}
} else if (CUR == '(') {
level++;
} else if (CUR == '^') {
if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
NEXT;
}
}
*cur++ = CUR;
NEXT;
}
*cur = 0;
if ((level != 0) && (CUR == 0)) {
xmlFree(name);
xmlFree(buffer);
XP_ERROR(XPTR_SYNTAX_ERROR);
}
if (xmlStrEqual(name, (xmlChar *) "xpointer") ||
xmlStrEqual(name, (xmlChar *) "xpath1")) {
const xmlChar *oldBase = ctxt->base;
const xmlChar *oldCur = ctxt->cur;
ctxt->cur = ctxt->base = buffer;
/*
* To evaluate an xpointer scheme element (4.3) we need:
* context initialized to the root