xpointer.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957
  1. /*
  2. * xpointer.c : Code to handle XML Pointer
  3. *
  4. * Base implementation was made accordingly to
  5. * W3C Candidate Recommendation 7 June 2000
  6. * http://www.w3.org/TR/2000/CR-xptr-20000607
  7. *
  8. * Added support for the element() scheme described in:
  9. * W3C Proposed Recommendation 13 November 2002
  10. * http://www.w3.org/TR/2002/PR-xptr-element-20021113/
  11. *
  12. * See Copyright for the status of this software.
  13. *
  14. * daniel@veillard.com
  15. */
  16. /* To avoid EBCDIC trouble when parsing on zOS */
  17. #if defined(__MVS__)
  18. #pragma convert("ISO8859-1")
  19. #endif
  20. #define IN_LIBXML
  21. #include "libxml.h"
  22. /*
  23. * TODO: better handling of error cases, the full expression should
  24. * be parsed beforehand instead of a progressive evaluation
  25. * TODO: Access into entities references are not supported now ...
  26. * need a start to be able to pop out of entities refs since
  27. * parent is the entity declaration, not the ref.
  28. */
  29. #include <string.h>
  30. #include <libxml/xpointer.h>
  31. #include <libxml/xmlmemory.h>
  32. #include <libxml/parserInternals.h>
  33. #include <libxml/uri.h>
  34. #include <libxml/xpath.h>
  35. #include <libxml/xpathInternals.h>
  36. #include <libxml/xmlerror.h>
  37. #include <libxml/globals.h>
  38. #ifdef LIBXML_XPTR_ENABLED
  39. /* Add support of the xmlns() xpointer scheme to initialize the namespaces */
  40. #define XPTR_XMLNS_SCHEME
  41. /* #define DEBUG_RANGES */
  42. #ifdef DEBUG_RANGES
  43. #ifdef LIBXML_DEBUG_ENABLED
  44. #include <libxml/debugXML.h>
  45. #endif
  46. #endif
  47. #define TODO \
  48. xmlGenericError(xmlGenericErrorContext, \
  49. "Unimplemented block at %s:%d\n", \
  50. __FILE__, __LINE__);
  51. #define STRANGE \
  52. xmlGenericError(xmlGenericErrorContext, \
  53. "Internal error at %s:%d\n", \
  54. __FILE__, __LINE__);
  55. /************************************************************************
  56. * *
  57. * Some factorized error routines *
  58. * *
  59. ************************************************************************/
  60. /**
  61. * xmlXPtrErrMemory:
  62. * @extra: extra information
  63. *
  64. * Handle a redefinition of attribute error
  65. */
  66. static void
  67. xmlXPtrErrMemory(const char *extra)
  68. {
  69. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_XPOINTER,
  70. XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0, extra,
  71. NULL, NULL, 0, 0,
  72. "Memory allocation failed : %s\n", extra);
  73. }
  74. /**
  75. * xmlXPtrErr:
  76. * @ctxt: an XPTR evaluation context
  77. * @extra: extra information
  78. *
  79. * Handle a redefinition of attribute error
  80. */
  81. static void LIBXML_ATTR_FORMAT(3,0)
  82. xmlXPtrErr(xmlXPathParserContextPtr ctxt, int error,
  83. const char * msg, const xmlChar *extra)
  84. {
  85. if (ctxt != NULL)
  86. ctxt->error = error;
  87. if ((ctxt == NULL) || (ctxt->context == NULL)) {
  88. __xmlRaiseError(NULL, NULL, NULL,
  89. NULL, NULL, XML_FROM_XPOINTER, error,
  90. XML_ERR_ERROR, NULL, 0,
  91. (const char *) extra, NULL, NULL, 0, 0,
  92. msg, extra);
  93. return;
  94. }
  95. /* cleanup current last error */
  96. xmlResetError(&ctxt->context->lastError);
  97. ctxt->context->lastError.domain = XML_FROM_XPOINTER;
  98. ctxt->context->lastError.code = error;
  99. ctxt->context->lastError.level = XML_ERR_ERROR;
  100. ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
  101. ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
  102. ctxt->context->lastError.node = ctxt->context->debugNode;
  103. if (ctxt->context->error != NULL) {
  104. ctxt->context->error(ctxt->context->userData,
  105. &ctxt->context->lastError);
  106. } else {
  107. __xmlRaiseError(NULL, NULL, NULL,
  108. NULL, ctxt->context->debugNode, XML_FROM_XPOINTER,
  109. error, XML_ERR_ERROR, NULL, 0,
  110. (const char *) extra, (const char *) ctxt->base, NULL,
  111. ctxt->cur - ctxt->base, 0,
  112. msg, extra);
  113. }
  114. }
  115. /************************************************************************
  116. * *
  117. * A few helper functions for child sequences *
  118. * *
  119. ************************************************************************/
  120. /* xmlXPtrAdvanceNode is a private function, but used by xinclude.c */
  121. xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level);
  122. /**
  123. * xmlXPtrGetArity:
  124. * @cur: the node
  125. *
  126. * Returns the number of child for an element, -1 in case of error
  127. */
  128. static int
  129. xmlXPtrGetArity(xmlNodePtr cur) {
  130. int i;
  131. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
  132. return(-1);
  133. cur = cur->children;
  134. for (i = 0;cur != NULL;cur = cur->next) {
  135. if ((cur->type == XML_ELEMENT_NODE) ||
  136. (cur->type == XML_DOCUMENT_NODE) ||
  137. (cur->type == XML_HTML_DOCUMENT_NODE)) {
  138. i++;
  139. }
  140. }
  141. return(i);
  142. }
  143. /**
  144. * xmlXPtrGetIndex:
  145. * @cur: the node
  146. *
  147. * Returns the index of the node in its parent children list, -1
  148. * in case of error
  149. */
  150. static int
  151. xmlXPtrGetIndex(xmlNodePtr cur) {
  152. int i;
  153. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
  154. return(-1);
  155. for (i = 1;cur != NULL;cur = cur->prev) {
  156. if ((cur->type == XML_ELEMENT_NODE) ||
  157. (cur->type == XML_DOCUMENT_NODE) ||
  158. (cur->type == XML_HTML_DOCUMENT_NODE)) {
  159. i++;
  160. }
  161. }
  162. return(i);
  163. }
  164. /**
  165. * xmlXPtrGetNthChild:
  166. * @cur: the node
  167. * @no: the child number
  168. *
  169. * Returns the @no'th element child of @cur or NULL
  170. */
  171. static xmlNodePtr
  172. xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
  173. int i;
  174. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
  175. return(cur);
  176. cur = cur->children;
  177. for (i = 0;i <= no;cur = cur->next) {
  178. if (cur == NULL)
  179. return(cur);
  180. if ((cur->type == XML_ELEMENT_NODE) ||
  181. (cur->type == XML_DOCUMENT_NODE) ||
  182. (cur->type == XML_HTML_DOCUMENT_NODE)) {
  183. i++;
  184. if (i == no)
  185. break;
  186. }
  187. }
  188. return(cur);
  189. }
  190. /************************************************************************
  191. * *
  192. * Handling of XPointer specific types *
  193. * *
  194. ************************************************************************/
  195. /**
  196. * xmlXPtrCmpPoints:
  197. * @node1: the first node
  198. * @index1: the first index
  199. * @node2: the second node
  200. * @index2: the second index
  201. *
  202. * Compare two points w.r.t document order
  203. *
  204. * Returns -2 in case of error 1 if first point < second point, 0 if
  205. * that's the same point, -1 otherwise
  206. */
  207. static int
  208. xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
  209. if ((node1 == NULL) || (node2 == NULL))
  210. return(-2);
  211. /*
  212. * a couple of optimizations which will avoid computations in most cases
  213. */
  214. if (node1 == node2) {
  215. if (index1 < index2)
  216. return(1);
  217. if (index1 > index2)
  218. return(-1);
  219. return(0);
  220. }
  221. return(xmlXPathCmpNodes(node1, node2));
  222. }
  223. /**
  224. * xmlXPtrNewPoint:
  225. * @node: the xmlNodePtr
  226. * @indx: the indx within the node
  227. *
  228. * Create a new xmlXPathObjectPtr of type point
  229. *
  230. * Returns the newly created object.
  231. */
  232. static xmlXPathObjectPtr
  233. xmlXPtrNewPoint(xmlNodePtr node, int indx) {
  234. xmlXPathObjectPtr ret;
  235. if (node == NULL)
  236. return(NULL);
  237. if (indx < 0)
  238. return(NULL);
  239. ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
  240. if (ret == NULL) {
  241. xmlXPtrErrMemory("allocating point");
  242. return(NULL);
  243. }
  244. memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
  245. ret->type = XPATH_POINT;
  246. ret->user = (void *) node;
  247. ret->index = indx;
  248. return(ret);
  249. }
  250. /**
  251. * xmlXPtrRangeCheckOrder:
  252. * @range: an object range
  253. *
  254. * Make sure the points in the range are in the right order
  255. */
  256. static void
  257. xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
  258. int tmp;
  259. xmlNodePtr tmp2;
  260. if (range == NULL)
  261. return;
  262. if (range->type != XPATH_RANGE)
  263. return;
  264. if (range->user2 == NULL)
  265. return;
  266. tmp = xmlXPtrCmpPoints(range->user, range->index,
  267. range->user2, range->index2);
  268. if (tmp == -1) {
  269. tmp2 = range->user;
  270. range->user = range->user2;
  271. range->user2 = tmp2;
  272. tmp = range->index;
  273. range->index = range->index2;
  274. range->index2 = tmp;
  275. }
  276. }
  277. /**
  278. * xmlXPtrRangesEqual:
  279. * @range1: the first range
  280. * @range2: the second range
  281. *
  282. * Compare two ranges
  283. *
  284. * Returns 1 if equal, 0 otherwise
  285. */
  286. static int
  287. xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
  288. if (range1 == range2)
  289. return(1);
  290. if ((range1 == NULL) || (range2 == NULL))
  291. return(0);
  292. if (range1->type != range2->type)
  293. return(0);
  294. if (range1->type != XPATH_RANGE)
  295. return(0);
  296. if (range1->user != range2->user)
  297. return(0);
  298. if (range1->index != range2->index)
  299. return(0);
  300. if (range1->user2 != range2->user2)
  301. return(0);
  302. if (range1->index2 != range2->index2)
  303. return(0);
  304. return(1);
  305. }
  306. /**
  307. * xmlXPtrNewRangeInternal:
  308. * @start: the starting node
  309. * @startindex: the start index
  310. * @end: the ending point
  311. * @endindex: the ending index
  312. *
  313. * Internal function to create a new xmlXPathObjectPtr of type range
  314. *
  315. * Returns the newly created object.
  316. */
  317. static xmlXPathObjectPtr
  318. xmlXPtrNewRangeInternal(xmlNodePtr start, int startindex,
  319. xmlNodePtr end, int endindex) {
  320. xmlXPathObjectPtr ret;
  321. /*
  322. * Namespace nodes must be copied (see xmlXPathNodeSetDupNs).
  323. * Disallow them for now.
  324. */
  325. if ((start != NULL) && (start->type == XML_NAMESPACE_DECL))
  326. return(NULL);
  327. if ((end != NULL) && (end->type == XML_NAMESPACE_DECL))
  328. return(NULL);
  329. ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
  330. if (ret == NULL) {
  331. xmlXPtrErrMemory("allocating range");
  332. return(NULL);
  333. }
  334. memset(ret, 0, sizeof(xmlXPathObject));
  335. ret->type = XPATH_RANGE;
  336. ret->user = start;
  337. ret->index = startindex;
  338. ret->user2 = end;
  339. ret->index2 = endindex;
  340. return(ret);
  341. }
  342. /**
  343. * xmlXPtrNewRange:
  344. * @start: the starting node
  345. * @startindex: the start index
  346. * @end: the ending point
  347. * @endindex: the ending index
  348. *
  349. * Create a new xmlXPathObjectPtr of type range
  350. *
  351. * Returns the newly created object.
  352. */
  353. xmlXPathObjectPtr
  354. xmlXPtrNewRange(xmlNodePtr start, int startindex,
  355. xmlNodePtr end, int endindex) {
  356. xmlXPathObjectPtr ret;
  357. if (start == NULL)
  358. return(NULL);
  359. if (end == NULL)
  360. return(NULL);
  361. if (startindex < 0)
  362. return(NULL);
  363. if (endindex < 0)
  364. return(NULL);
  365. ret = xmlXPtrNewRangeInternal(start, startindex, end, endindex);
  366. xmlXPtrRangeCheckOrder(ret);
  367. return(ret);
  368. }
  369. /**
  370. * xmlXPtrNewRangePoints:
  371. * @start: the starting point
  372. * @end: the ending point
  373. *
  374. * Create a new xmlXPathObjectPtr of type range using 2 Points
  375. *
  376. * Returns the newly created object.
  377. */
  378. xmlXPathObjectPtr
  379. xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
  380. xmlXPathObjectPtr ret;
  381. if (start == NULL)
  382. return(NULL);
  383. if (end == NULL)
  384. return(NULL);
  385. if (start->type != XPATH_POINT)
  386. return(NULL);
  387. if (end->type != XPATH_POINT)
  388. return(NULL);
  389. ret = xmlXPtrNewRangeInternal(start->user, start->index, end->user,
  390. end->index);
  391. xmlXPtrRangeCheckOrder(ret);
  392. return(ret);
  393. }
  394. /**
  395. * xmlXPtrNewRangePointNode:
  396. * @start: the starting point
  397. * @end: the ending node
  398. *
  399. * Create a new xmlXPathObjectPtr of type range from a point to a node
  400. *
  401. * Returns the newly created object.
  402. */
  403. xmlXPathObjectPtr
  404. xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
  405. xmlXPathObjectPtr ret;
  406. if (start == NULL)
  407. return(NULL);
  408. if (end == NULL)
  409. return(NULL);
  410. if (start->type != XPATH_POINT)
  411. return(NULL);
  412. ret = xmlXPtrNewRangeInternal(start->user, start->index, end, -1);
  413. xmlXPtrRangeCheckOrder(ret);
  414. return(ret);
  415. }
  416. /**
  417. * xmlXPtrNewRangeNodePoint:
  418. * @start: the starting node
  419. * @end: the ending point
  420. *
  421. * Create a new xmlXPathObjectPtr of type range from a node to a point
  422. *
  423. * Returns the newly created object.
  424. */
  425. xmlXPathObjectPtr
  426. xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
  427. xmlXPathObjectPtr ret;
  428. if (start == NULL)
  429. return(NULL);
  430. if (end == NULL)
  431. return(NULL);
  432. if (end->type != XPATH_POINT)
  433. return(NULL);
  434. ret = xmlXPtrNewRangeInternal(start, -1, end->user, end->index);
  435. xmlXPtrRangeCheckOrder(ret);
  436. return(ret);
  437. }
  438. /**
  439. * xmlXPtrNewRangeNodes:
  440. * @start: the starting node
  441. * @end: the ending node
  442. *
  443. * Create a new xmlXPathObjectPtr of type range using 2 nodes
  444. *
  445. * Returns the newly created object.
  446. */
  447. xmlXPathObjectPtr
  448. xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
  449. xmlXPathObjectPtr ret;
  450. if (start == NULL)
  451. return(NULL);
  452. if (end == NULL)
  453. return(NULL);
  454. ret = xmlXPtrNewRangeInternal(start, -1, end, -1);
  455. xmlXPtrRangeCheckOrder(ret);
  456. return(ret);
  457. }
  458. /**
  459. * xmlXPtrNewCollapsedRange:
  460. * @start: the starting and ending node
  461. *
  462. * Create a new xmlXPathObjectPtr of type range using a single nodes
  463. *
  464. * Returns the newly created object.
  465. */
  466. xmlXPathObjectPtr
  467. xmlXPtrNewCollapsedRange(xmlNodePtr start) {
  468. xmlXPathObjectPtr ret;
  469. if (start == NULL)
  470. return(NULL);
  471. ret = xmlXPtrNewRangeInternal(start, -1, NULL, -1);
  472. return(ret);
  473. }
  474. /**
  475. * xmlXPtrNewRangeNodeObject:
  476. * @start: the starting node
  477. * @end: the ending object
  478. *
  479. * Create a new xmlXPathObjectPtr of type range from a not to an object
  480. *
  481. * Returns the newly created object.
  482. */
  483. xmlXPathObjectPtr
  484. xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
  485. xmlNodePtr endNode;
  486. int endIndex;
  487. xmlXPathObjectPtr ret;
  488. if (start == NULL)
  489. return(NULL);
  490. if (end == NULL)
  491. return(NULL);
  492. switch (end->type) {
  493. case XPATH_POINT:
  494. endNode = end->user;
  495. endIndex = end->index;
  496. break;
  497. case XPATH_RANGE:
  498. endNode = end->user2;
  499. endIndex = end->index2;
  500. break;
  501. case XPATH_NODESET:
  502. /*
  503. * Empty set ...
  504. */
  505. if ((end->nodesetval == NULL) || (end->nodesetval->nodeNr <= 0))
  506. return(NULL);
  507. endNode = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
  508. endIndex = -1;
  509. break;
  510. default:
  511. /* TODO */
  512. return(NULL);
  513. }
  514. ret = xmlXPtrNewRangeInternal(start, -1, endNode, endIndex);
  515. xmlXPtrRangeCheckOrder(ret);
  516. return(ret);
  517. }
  518. #define XML_RANGESET_DEFAULT 10
  519. /**
  520. * xmlXPtrLocationSetCreate:
  521. * @val: an initial xmlXPathObjectPtr, or NULL
  522. *
  523. * Create a new xmlLocationSetPtr of type double and of value @val
  524. *
  525. * Returns the newly created object.
  526. */
  527. xmlLocationSetPtr
  528. xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
  529. xmlLocationSetPtr ret;
  530. ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
  531. if (ret == NULL) {
  532. xmlXPtrErrMemory("allocating locationset");
  533. return(NULL);
  534. }
  535. memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
  536. if (val != NULL) {
  537. ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
  538. sizeof(xmlXPathObjectPtr));
  539. if (ret->locTab == NULL) {
  540. xmlXPtrErrMemory("allocating locationset");
  541. xmlFree(ret);
  542. return(NULL);
  543. }
  544. memset(ret->locTab, 0 ,
  545. XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
  546. ret->locMax = XML_RANGESET_DEFAULT;
  547. ret->locTab[ret->locNr++] = val;
  548. }
  549. return(ret);
  550. }
  551. /**
  552. * xmlXPtrLocationSetAdd:
  553. * @cur: the initial range set
  554. * @val: a new xmlXPathObjectPtr
  555. *
  556. * add a new xmlXPathObjectPtr to an existing LocationSet
  557. * If the location already exist in the set @val is freed.
  558. */
  559. void
  560. xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
  561. int i;
  562. if ((cur == NULL) || (val == NULL)) return;
  563. /*
  564. * check against doublons
  565. */
  566. for (i = 0;i < cur->locNr;i++) {
  567. if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
  568. xmlXPathFreeObject(val);
  569. return;
  570. }
  571. }
  572. /*
  573. * grow the locTab if needed
  574. */
  575. if (cur->locMax == 0) {
  576. cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
  577. sizeof(xmlXPathObjectPtr));
  578. if (cur->locTab == NULL) {
  579. xmlXPtrErrMemory("adding location to set");
  580. return;
  581. }
  582. memset(cur->locTab, 0 ,
  583. XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
  584. cur->locMax = XML_RANGESET_DEFAULT;
  585. } else if (cur->locNr == cur->locMax) {
  586. xmlXPathObjectPtr *temp;
  587. cur->locMax *= 2;
  588. temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
  589. sizeof(xmlXPathObjectPtr));
  590. if (temp == NULL) {
  591. xmlXPtrErrMemory("adding location to set");
  592. return;
  593. }
  594. cur->locTab = temp;
  595. }
  596. cur->locTab[cur->locNr++] = val;
  597. }
  598. /**
  599. * xmlXPtrLocationSetMerge:
  600. * @val1: the first LocationSet
  601. * @val2: the second LocationSet
  602. *
  603. * Merges two rangesets, all ranges from @val2 are added to @val1
  604. *
  605. * Returns val1 once extended or NULL in case of error.
  606. */
  607. xmlLocationSetPtr
  608. xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
  609. int i;
  610. if (val1 == NULL) return(NULL);
  611. if (val2 == NULL) return(val1);
  612. /*
  613. * !!!!! this can be optimized a lot, knowing that both
  614. * val1 and val2 already have unicity of their values.
  615. */
  616. for (i = 0;i < val2->locNr;i++)
  617. xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
  618. return(val1);
  619. }
  620. /**
  621. * xmlXPtrLocationSetDel:
  622. * @cur: the initial range set
  623. * @val: an xmlXPathObjectPtr
  624. *
  625. * Removes an xmlXPathObjectPtr from an existing LocationSet
  626. */
  627. void
  628. xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
  629. int i;
  630. if (cur == NULL) return;
  631. if (val == NULL) return;
  632. /*
  633. * check against doublons
  634. */
  635. for (i = 0;i < cur->locNr;i++)
  636. if (cur->locTab[i] == val) break;
  637. if (i >= cur->locNr) {
  638. #ifdef DEBUG
  639. xmlGenericError(xmlGenericErrorContext,
  640. "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
  641. #endif
  642. return;
  643. }
  644. cur->locNr--;
  645. for (;i < cur->locNr;i++)
  646. cur->locTab[i] = cur->locTab[i + 1];
  647. cur->locTab[cur->locNr] = NULL;
  648. }
  649. /**
  650. * xmlXPtrLocationSetRemove:
  651. * @cur: the initial range set
  652. * @val: the index to remove
  653. *
  654. * Removes an entry from an existing LocationSet list.
  655. */
  656. void
  657. xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
  658. if (cur == NULL) return;
  659. if (val >= cur->locNr) return;
  660. cur->locNr--;
  661. for (;val < cur->locNr;val++)
  662. cur->locTab[val] = cur->locTab[val + 1];
  663. cur->locTab[cur->locNr] = NULL;
  664. }
  665. /**
  666. * xmlXPtrFreeLocationSet:
  667. * @obj: the xmlLocationSetPtr to free
  668. *
  669. * Free the LocationSet compound (not the actual ranges !).
  670. */
  671. void
  672. xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
  673. int i;
  674. if (obj == NULL) return;
  675. if (obj->locTab != NULL) {
  676. for (i = 0;i < obj->locNr; i++) {
  677. xmlXPathFreeObject(obj->locTab[i]);
  678. }
  679. xmlFree(obj->locTab);
  680. }
  681. xmlFree(obj);
  682. }
  683. /**
  684. * xmlXPtrNewLocationSetNodes:
  685. * @start: the start NodePtr value
  686. * @end: the end NodePtr value or NULL
  687. *
  688. * Create a new xmlXPathObjectPtr of type LocationSet and initialize
  689. * it with the single range made of the two nodes @start and @end
  690. *
  691. * Returns the newly created object.
  692. */
  693. xmlXPathObjectPtr
  694. xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
  695. xmlXPathObjectPtr ret;
  696. ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
  697. if (ret == NULL) {
  698. xmlXPtrErrMemory("allocating locationset");
  699. return(NULL);
  700. }
  701. memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
  702. ret->type = XPATH_LOCATIONSET;
  703. if (end == NULL)
  704. ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
  705. else
  706. ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
  707. return(ret);
  708. }
  709. /**
  710. * xmlXPtrNewLocationSetNodeSet:
  711. * @set: a node set
  712. *
  713. * Create a new xmlXPathObjectPtr of type LocationSet and initialize
  714. * it with all the nodes from @set
  715. *
  716. * Returns the newly created object.
  717. */
  718. xmlXPathObjectPtr
  719. xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
  720. xmlXPathObjectPtr ret;
  721. ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
  722. if (ret == NULL) {
  723. xmlXPtrErrMemory("allocating locationset");
  724. return(NULL);
  725. }
  726. memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
  727. ret->type = XPATH_LOCATIONSET;
  728. if (set != NULL) {
  729. int i;
  730. xmlLocationSetPtr newset;
  731. newset = xmlXPtrLocationSetCreate(NULL);
  732. if (newset == NULL)
  733. return(ret);
  734. for (i = 0;i < set->nodeNr;i++)
  735. xmlXPtrLocationSetAdd(newset,
  736. xmlXPtrNewCollapsedRange(set->nodeTab[i]));
  737. ret->user = (void *) newset;
  738. }
  739. return(ret);
  740. }
  741. /**
  742. * xmlXPtrWrapLocationSet:
  743. * @val: the LocationSet value
  744. *
  745. * Wrap the LocationSet @val in a new xmlXPathObjectPtr
  746. *
  747. * Returns the newly created object.
  748. */
  749. xmlXPathObjectPtr
  750. xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
  751. xmlXPathObjectPtr ret;
  752. ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
  753. if (ret == NULL) {
  754. xmlXPtrErrMemory("allocating locationset");
  755. return(NULL);
  756. }
  757. memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
  758. ret->type = XPATH_LOCATIONSET;
  759. ret->user = (void *) val;
  760. return(ret);
  761. }
  762. /************************************************************************
  763. * *
  764. * The parser *
  765. * *
  766. ************************************************************************/
  767. static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
  768. /*
  769. * Macros for accessing the content. Those should be used only by the parser,
  770. * and not exported.
  771. *
  772. * Dirty macros, i.e. one need to make assumption on the context to use them
  773. *
  774. * CUR returns the current xmlChar value, i.e. a 8 bit value
  775. * in ISO-Latin or UTF-8.
  776. * This should be used internally by the parser
  777. * only to compare to ASCII values otherwise it would break when
  778. * running with UTF-8 encoding.
  779. * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
  780. * to compare on ASCII based substring.
  781. * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
  782. * strings within the parser.
  783. * CURRENT Returns the current char value, with the full decoding of
  784. * UTF-8 if we are using this mode. It returns an int.
  785. * NEXT Skip to the next character, this does the proper decoding
  786. * in UTF-8 mode. It also pop-up unfinished entities on the fly.
  787. * It returns the pointer to the current xmlChar.
  788. */
  789. #define CUR (*ctxt->cur)
  790. #define SKIP(val) ctxt->cur += (val)
  791. #define NXT(val) ctxt->cur[(val)]
  792. #define SKIP_BLANKS \
  793. while (IS_BLANK_CH(*(ctxt->cur))) NEXT
  794. #define CURRENT (*ctxt->cur)
  795. #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
  796. /*
  797. * xmlXPtrGetChildNo:
  798. * @ctxt: the XPointer Parser context
  799. * @index: the child number
  800. *
  801. * Move the current node of the nodeset on the stack to the
  802. * given child if found
  803. */
  804. static void
  805. xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
  806. xmlNodePtr cur = NULL;
  807. xmlXPathObjectPtr obj;
  808. xmlNodeSetPtr oldset;
  809. CHECK_TYPE(XPATH_NODESET);
  810. obj = valuePop(ctxt);
  811. oldset = obj->nodesetval;
  812. if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
  813. xmlXPathFreeObject(obj);
  814. valuePush(ctxt, xmlXPathNewNodeSet(NULL));
  815. return;
  816. }
  817. cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
  818. if (cur == NULL) {
  819. xmlXPathFreeObject(obj);
  820. valuePush(ctxt, xmlXPathNewNodeSet(NULL));
  821. return;
  822. }
  823. oldset->nodeTab[0] = cur;
  824. valuePush(ctxt, obj);
  825. }
  826. /**
  827. * xmlXPtrEvalXPtrPart:
  828. * @ctxt: the XPointer Parser context
  829. * @name: the preparsed Scheme for the XPtrPart
  830. *
  831. * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
  832. * | Scheme '(' SchemeSpecificExpr ')'
  833. *
  834. * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
  835. *
  836. * SchemeSpecificExpr ::= StringWithBalancedParens
  837. *
  838. * StringWithBalancedParens ::=
  839. * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
  840. * [VC: Parenthesis escaping]
  841. *
  842. * XPtrExpr ::= Expr [VC: Parenthesis escaping]
  843. *
  844. * VC: Parenthesis escaping:
  845. * The end of an XPointer part is signaled by the right parenthesis ")"
  846. * character that is balanced with the left parenthesis "(" character
  847. * that began the part. Any unbalanced parenthesis character inside the
  848. * expression, even within literals, must be escaped with a circumflex (^)
  849. * character preceding it. If the expression contains any literal
  850. * occurrences of the circumflex, each must be escaped with an additional
  851. * circumflex (that is, ^^). If the unescaped parentheses in the expression
  852. * are not balanced, a syntax error results.
  853. *
  854. * Parse and evaluate an XPtrPart. Basically it generates the unescaped
  855. * string and if the scheme is 'xpointer' it will call the XPath interpreter.
  856. *
  857. * TODO: there is no new scheme registration mechanism
  858. */
  859. static void
  860. xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
  861. xmlChar *buffer, *cur;
  862. int len;
  863. int level;
  864. if (name == NULL)
  865. name = xmlXPathParseName(ctxt);
  866. if (name == NULL)
  867. XP_ERROR(XPATH_EXPR_ERROR);
  868. if (CUR != '(') {
  869. xmlFree(name);
  870. XP_ERROR(XPATH_EXPR_ERROR);
  871. }
  872. NEXT;
  873. level = 1;
  874. len = xmlStrlen(ctxt->cur);
  875. len++;
  876. buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
  877. if (buffer == NULL) {
  878. xmlXPtrErrMemory("allocating buffer");
  879. xmlFree(name);
  880. return;
  881. }
  882. cur = buffer;
  883. while (CUR != 0) {
  884. if (CUR == ')') {
  885. level--;
  886. if (level == 0) {
  887. NEXT;
  888. break;
  889. }
  890. } else if (CUR == '(') {
  891. level++;
  892. } else if (CUR == '^') {
  893. if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
  894. NEXT;
  895. }
  896. }
  897. *cur++ = CUR;
  898. NEXT;
  899. }
  900. *cur = 0;
  901. if ((level != 0) && (CUR == 0)) {
  902. xmlFree(name);
  903. xmlFree(buffer);
  904. XP_ERROR(XPTR_SYNTAX_ERROR);
  905. }
  906. if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
  907. const xmlChar *oldBase = ctxt->base;
  908. const xmlChar *oldCur = ctxt->cur;
  909. ctxt->cur = ctxt->base = buffer;
  910. /*
  911. * To evaluate an xpointer scheme element (4.3) we need:
  912. * context initialized to the root
  913. * context position initialized to 1
  914. * context size initialized to 1
  915. */
  916. ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
  917. ctxt->context->proximityPosition = 1;
  918. ctxt->context->contextSize = 1;
  919. xmlXPathEvalExpr(ctxt);
  920. ctxt->base = oldBase;
  921. ctxt->cur = oldCur;
  922. } else if (xmlStrEqual(name, (xmlChar *) "element")) {
  923. const xmlChar *oldBase = ctxt->base;
  924. const xmlChar *oldCur = ctxt->cur;
  925. xmlChar *name2;
  926. ctxt->cur = ctxt->base = buffer;
  927. if (buffer[0] == '/') {
  928. xmlXPathRoot(ctxt);
  929. xmlXPtrEvalChildSeq(ctxt, NULL);
  930. } else {
  931. name2 = xmlXPathParseName(ctxt);
  932. if (name2 == NULL) {
  933. ctxt->base = oldBase;
  934. ctxt->cur = oldCur;
  935. xmlFree(buffer);
  936. xmlFree(name);
  937. XP_ERROR(XPATH_EXPR_ERROR);
  938. }
  939. xmlXPtrEvalChildSeq(ctxt, name2);
  940. }
  941. ctxt->base = oldBase;
  942. ctxt->cur = oldCur;
  943. #ifdef XPTR_XMLNS_SCHEME
  944. } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
  945. const xmlChar *oldBase = ctxt->base;
  946. const xmlChar *oldCur = ctxt->cur;
  947. xmlChar *prefix;
  948. ctxt->cur = ctxt->base = buffer;
  949. prefix = xmlXPathParseNCName(ctxt);
  950. if (prefix == NULL) {
  951. ctxt->base = oldBase;
  952. ctxt->cur = oldCur;
  953. xmlFree(buffer);
  954. xmlFree(name);
  955. XP_ERROR(XPTR_SYNTAX_ERROR);
  956. }
  957. SKIP_BLANKS;
  958. if (CUR != '=') {
  959. ctxt->base = oldBase;
  960. ctxt->cur = oldCur;
  961. xmlFree(prefix);
  962. xmlFree(buffer);
  963. xmlFree(name);
  964. XP_ERROR(XPTR_SYNTAX_ERROR);
  965. }
  966. NEXT;
  967. SKIP_BLANKS;
  968. xmlXPathRegisterNs(ctxt->context, prefix, ctxt->cur);
  969. ctxt->base = oldBase;
  970. ctxt->cur = oldCur;
  971. xmlFree(prefix);
  972. #endif /* XPTR_XMLNS_SCHEME */
  973. } else {
  974. xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
  975. "unsupported scheme '%s'\n", name);
  976. }
  977. xmlFree(buffer);
  978. xmlFree(name);
  979. }
  980. /**
  981. * xmlXPtrEvalFullXPtr:
  982. * @ctxt: the XPointer Parser context
  983. * @name: the preparsed Scheme for the first XPtrPart
  984. *
  985. * FullXPtr ::= XPtrPart (S? XPtrPart)*
  986. *
  987. * As the specs says:
  988. * -----------
  989. * When multiple XPtrParts are provided, they must be evaluated in
  990. * left-to-right order. If evaluation of one part fails, the nexti
  991. * is evaluated. The following conditions cause XPointer part failure:
  992. *
  993. * - An unknown scheme
  994. * - A scheme that does not locate any sub-resource present in the resource
  995. * - A scheme that is not applicable to the media type of the resource
  996. *
  997. * The XPointer application must consume a failed XPointer part and
  998. * attempt to evaluate the next one, if any. The result of the first
  999. * XPointer part whose evaluation succeeds is taken to be the fragment
  1000. * located by the XPointer as a whole. If all the parts fail, the result
  1001. * for the XPointer as a whole is a sub-resource error.
  1002. * -----------
  1003. *
  1004. * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
  1005. * expressions or other schemes.
  1006. */
  1007. static void
  1008. xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
  1009. if (name == NULL)
  1010. name = xmlXPathParseName(ctxt);
  1011. if (name == NULL)
  1012. XP_ERROR(XPATH_EXPR_ERROR);
  1013. while (name != NULL) {
  1014. ctxt->error = XPATH_EXPRESSION_OK;
  1015. xmlXPtrEvalXPtrPart(ctxt, name);
  1016. /* in case of syntax error, break here */
  1017. if ((ctxt->error != XPATH_EXPRESSION_OK) &&
  1018. (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
  1019. return;
  1020. /*
  1021. * If the returned value is a non-empty nodeset
  1022. * or location set, return here.
  1023. */
  1024. if (ctxt->value != NULL) {
  1025. xmlXPathObjectPtr obj = ctxt->value;
  1026. switch (obj->type) {
  1027. case XPATH_LOCATIONSET: {
  1028. xmlLocationSetPtr loc = ctxt->value->user;
  1029. if ((loc != NULL) && (loc->locNr > 0))
  1030. return;
  1031. break;
  1032. }
  1033. case XPATH_NODESET: {
  1034. xmlNodeSetPtr loc = ctxt->value->nodesetval;
  1035. if ((loc != NULL) && (loc->nodeNr > 0))
  1036. return;
  1037. break;
  1038. }
  1039. default:
  1040. break;
  1041. }
  1042. /*
  1043. * Evaluating to improper values is equivalent to
  1044. * a sub-resource error, clean-up the stack
  1045. */
  1046. do {
  1047. obj = valuePop(ctxt);
  1048. if (obj != NULL) {
  1049. xmlXPathFreeObject(obj);
  1050. }
  1051. } while (obj != NULL);
  1052. }
  1053. /*
  1054. * Is there another XPointer part.
  1055. */
  1056. SKIP_BLANKS;
  1057. name = xmlXPathParseName(ctxt);
  1058. }
  1059. }
  1060. /**
  1061. * xmlXPtrEvalChildSeq:
  1062. * @ctxt: the XPointer Parser context
  1063. * @name: a possible ID name of the child sequence
  1064. *
  1065. * ChildSeq ::= '/1' ('/' [0-9]*)*
  1066. * | Name ('/' [0-9]*)+
  1067. *
  1068. * Parse and evaluate a Child Sequence. This routine also handle the
  1069. * case of a Bare Name used to get a document ID.
  1070. */
  1071. static void
  1072. xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
  1073. /*
  1074. * XPointer don't allow by syntax to address in multirooted trees
  1075. * this might prove useful in some cases, warn about it.
  1076. */
  1077. if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
  1078. xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
  1079. "warning: ChildSeq not starting by /1\n", NULL);
  1080. }
  1081. if (name != NULL) {
  1082. valuePush(ctxt, xmlXPathNewString(name));
  1083. xmlFree(name);
  1084. xmlXPathIdFunction(ctxt, 1);
  1085. CHECK_ERROR;
  1086. }
  1087. while (CUR == '/') {
  1088. int child = 0, overflow = 0;
  1089. NEXT;
  1090. while ((CUR >= '0') && (CUR <= '9')) {
  1091. int d = CUR - '0';
  1092. if (child > INT_MAX / 10)
  1093. overflow = 1;
  1094. else
  1095. child *= 10;
  1096. if (child > INT_MAX - d)
  1097. overflow = 1;
  1098. else
  1099. child += d;
  1100. NEXT;
  1101. }
  1102. if (overflow)
  1103. child = 0;
  1104. xmlXPtrGetChildNo(ctxt, child);
  1105. }
  1106. }
  1107. /**
  1108. * xmlXPtrEvalXPointer:
  1109. * @ctxt: the XPointer Parser context
  1110. *
  1111. * XPointer ::= Name
  1112. * | ChildSeq
  1113. * | FullXPtr
  1114. *
  1115. * Parse and evaluate an XPointer
  1116. */
  1117. static void
  1118. xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
  1119. if (ctxt->valueTab == NULL) {
  1120. /* Allocate the value stack */
  1121. ctxt->valueTab = (xmlXPathObjectPtr *)
  1122. xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
  1123. if (ctxt->valueTab == NULL) {
  1124. xmlXPtrErrMemory("allocating evaluation context");
  1125. return;
  1126. }
  1127. ctxt->valueNr = 0;
  1128. ctxt->valueMax = 10;
  1129. ctxt->value = NULL;
  1130. ctxt->valueFrame = 0;
  1131. }
  1132. SKIP_BLANKS;
  1133. if (CUR == '/') {
  1134. xmlXPathRoot(ctxt);
  1135. xmlXPtrEvalChildSeq(ctxt, NULL);
  1136. } else {
  1137. xmlChar *name;
  1138. name = xmlXPathParseName(ctxt);
  1139. if (name == NULL)
  1140. XP_ERROR(XPATH_EXPR_ERROR);
  1141. if (CUR == '(') {
  1142. xmlXPtrEvalFullXPtr(ctxt, name);
  1143. /* Short evaluation */
  1144. return;
  1145. } else {
  1146. /* this handle both Bare Names and Child Sequences */
  1147. xmlXPtrEvalChildSeq(ctxt, name);
  1148. }
  1149. }
  1150. SKIP_BLANKS;
  1151. if (CUR != 0)
  1152. XP_ERROR(XPATH_EXPR_ERROR);
  1153. }
  1154. /************************************************************************
  1155. * *
  1156. * General routines *
  1157. * *
  1158. ************************************************************************/
  1159. static
  1160. void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
  1161. static
  1162. void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
  1163. static
  1164. void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
  1165. static
  1166. void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
  1167. static
  1168. void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
  1169. static
  1170. void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
  1171. static
  1172. void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
  1173. /**
  1174. * xmlXPtrNewContext:
  1175. * @doc: the XML document
  1176. * @here: the node that directly contains the XPointer being evaluated or NULL
  1177. * @origin: the element from which a user or program initiated traversal of
  1178. * the link, or NULL.
  1179. *
  1180. * Create a new XPointer context
  1181. *
  1182. * Returns the xmlXPathContext just allocated.
  1183. */
  1184. xmlXPathContextPtr
  1185. xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
  1186. xmlXPathContextPtr ret;
  1187. ret = xmlXPathNewContext(doc);
  1188. if (ret == NULL)
  1189. return(ret);
  1190. ret->xptr = 1;
  1191. ret->here = here;
  1192. ret->origin = origin;
  1193. xmlXPathRegisterFunc(ret, (xmlChar *)"range",
  1194. xmlXPtrRangeFunction);
  1195. xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
  1196. xmlXPtrRangeInsideFunction);
  1197. xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
  1198. xmlXPtrStringRangeFunction);
  1199. xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
  1200. xmlXPtrStartPointFunction);
  1201. xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
  1202. xmlXPtrEndPointFunction);
  1203. xmlXPathRegisterFunc(ret, (xmlChar *)"here",
  1204. xmlXPtrHereFunction);
  1205. xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
  1206. xmlXPtrOriginFunction);
  1207. return(ret);
  1208. }
  1209. /**
  1210. * xmlXPtrEval:
  1211. * @str: the XPointer expression
  1212. * @ctx: the XPointer context
  1213. *
  1214. * Evaluate the XPath Location Path in the given context.
  1215. *
  1216. * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
  1217. * the caller has to free the object.
  1218. */
  1219. xmlXPathObjectPtr
  1220. xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
  1221. xmlXPathParserContextPtr ctxt;
  1222. xmlXPathObjectPtr res = NULL, tmp;
  1223. xmlXPathObjectPtr init = NULL;
  1224. int stack = 0;
  1225. xmlInitParser();
  1226. if ((ctx == NULL) || (str == NULL))
  1227. return(NULL);
  1228. ctxt = xmlXPathNewParserContext(str, ctx);
  1229. if (ctxt == NULL)
  1230. return(NULL);
  1231. ctxt->xptr = 1;
  1232. xmlXPtrEvalXPointer(ctxt);
  1233. if ((ctxt->value != NULL) &&
  1234. (ctxt->value->type != XPATH_NODESET) &&
  1235. (ctxt->value->type != XPATH_LOCATIONSET)) {
  1236. xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
  1237. "xmlXPtrEval: evaluation failed to return a node set\n",
  1238. NULL);
  1239. } else {
  1240. res = valuePop(ctxt);
  1241. }
  1242. do {
  1243. tmp = valuePop(ctxt);
  1244. if (tmp != NULL) {
  1245. if (tmp != init) {
  1246. if (tmp->type == XPATH_NODESET) {
  1247. /*
  1248. * Evaluation may push a root nodeset which is unused
  1249. */
  1250. xmlNodeSetPtr set;
  1251. set = tmp->nodesetval;
  1252. if ((set == NULL) || (set->nodeNr != 1) ||
  1253. (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
  1254. stack++;
  1255. } else
  1256. stack++;
  1257. }
  1258. xmlXPathFreeObject(tmp);
  1259. }
  1260. } while (tmp != NULL);
  1261. if (stack != 0) {
  1262. xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
  1263. "xmlXPtrEval: object(s) left on the eval stack\n",
  1264. NULL);
  1265. }
  1266. if (ctxt->error != XPATH_EXPRESSION_OK) {
  1267. xmlXPathFreeObject(res);
  1268. res = NULL;
  1269. }
  1270. xmlXPathFreeParserContext(ctxt);
  1271. return(res);
  1272. }
  1273. /**
  1274. * xmlXPtrBuildRangeNodeList:
  1275. * @range: a range object
  1276. *
  1277. * Build a node list tree copy of the range
  1278. *
  1279. * Returns an xmlNodePtr list or NULL.
  1280. * the caller has to free the node tree.
  1281. */
  1282. static xmlNodePtr
  1283. xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
  1284. /* pointers to generated nodes */
  1285. xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
  1286. /* pointers to traversal nodes */
  1287. xmlNodePtr start, cur, end;
  1288. int index1, index2;
  1289. if (range == NULL)
  1290. return(NULL);
  1291. if (range->type != XPATH_RANGE)
  1292. return(NULL);
  1293. start = (xmlNodePtr) range->user;
  1294. if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
  1295. return(NULL);
  1296. end = range->user2;
  1297. if (end == NULL)
  1298. return(xmlCopyNode(start, 1));
  1299. if (end->type == XML_NAMESPACE_DECL)
  1300. return(NULL);
  1301. cur = start;
  1302. index1 = range->index;
  1303. index2 = range->index2;
  1304. while (cur != NULL) {
  1305. if (cur == end) {
  1306. if (cur->type == XML_TEXT_NODE) {
  1307. const xmlChar *content = cur->content;
  1308. int len;
  1309. if (content == NULL) {
  1310. tmp = xmlNewTextLen(NULL, 0);
  1311. } else {
  1312. len = index2;
  1313. if ((cur == start) && (index1 > 1)) {
  1314. content += (index1 - 1);
  1315. len -= (index1 - 1);
  1316. index1 = 0;
  1317. } else {
  1318. len = index2;
  1319. }
  1320. tmp = xmlNewTextLen(content, len);
  1321. }
  1322. /* single sub text node selection */
  1323. if (list == NULL)
  1324. return(tmp);
  1325. /* prune and return full set */
  1326. if (last != NULL)
  1327. xmlAddNextSibling(last, tmp);
  1328. else
  1329. xmlAddChild(parent, tmp);
  1330. return(list);
  1331. } else {
  1332. tmp = xmlCopyNode(cur, 0);
  1333. if (list == NULL) {
  1334. list = tmp;
  1335. parent = tmp;
  1336. } else {
  1337. if (last != NULL)
  1338. parent = xmlAddNextSibling(last, tmp);
  1339. else
  1340. parent = xmlAddChild(parent, tmp);
  1341. }
  1342. last = NULL;
  1343. if (index2 > 1) {
  1344. end = xmlXPtrGetNthChild(cur, index2 - 1);
  1345. index2 = 0;
  1346. }
  1347. if ((cur == start) && (index1 > 1)) {
  1348. cur = xmlXPtrGetNthChild(cur, index1 - 1);
  1349. index1 = 0;
  1350. } else {
  1351. cur = cur->children;
  1352. }
  1353. /*
  1354. * Now gather the remaining nodes from cur to end
  1355. */
  1356. continue; /* while */
  1357. }
  1358. } else if ((cur == start) &&
  1359. (list == NULL) /* looks superfluous but ... */ ) {
  1360. if ((cur->type == XML_TEXT_NODE) ||
  1361. (cur->type == XML_CDATA_SECTION_NODE)) {
  1362. const xmlChar *content = cur->content;
  1363. if (content == NULL) {
  1364. tmp = xmlNewTextLen(NULL, 0);
  1365. } else {
  1366. if (index1 > 1) {
  1367. content += (index1 - 1);
  1368. }
  1369. tmp = xmlNewText(content);
  1370. }
  1371. last = list = tmp;
  1372. } else {
  1373. if ((cur == start) && (index1 > 1)) {
  1374. tmp = xmlCopyNode(cur, 0);
  1375. list = tmp;
  1376. parent = tmp;
  1377. last = NULL;
  1378. cur = xmlXPtrGetNthChild(cur, index1 - 1);
  1379. index1 = 0;
  1380. /*
  1381. * Now gather the remaining nodes from cur to end
  1382. */
  1383. continue; /* while */
  1384. }
  1385. tmp = xmlCopyNode(cur, 1);
  1386. list = tmp;
  1387. parent = NULL;
  1388. last = tmp;
  1389. }
  1390. } else {
  1391. tmp = NULL;
  1392. switch (cur->type) {
  1393. case XML_DTD_NODE:
  1394. case XML_ELEMENT_DECL:
  1395. case XML_ATTRIBUTE_DECL:
  1396. case XML_ENTITY_NODE:
  1397. /* Do not copy DTD information */
  1398. break;
  1399. case XML_ENTITY_DECL:
  1400. TODO /* handle crossing entities -> stack needed */
  1401. break;
  1402. case XML_XINCLUDE_START:
  1403. case XML_XINCLUDE_END:
  1404. /* don't consider it part of the tree content */
  1405. break;
  1406. case XML_ATTRIBUTE_NODE:
  1407. /* Humm, should not happen ! */
  1408. STRANGE
  1409. break;
  1410. default:
  1411. tmp = xmlCopyNode(cur, 1);
  1412. break;
  1413. }
  1414. if (tmp != NULL) {
  1415. if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
  1416. STRANGE
  1417. return(NULL);
  1418. }
  1419. if (last != NULL)
  1420. xmlAddNextSibling(last, tmp);
  1421. else {
  1422. last = xmlAddChild(parent, tmp);
  1423. }
  1424. }
  1425. }
  1426. /*
  1427. * Skip to next node in document order
  1428. */
  1429. if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
  1430. STRANGE
  1431. return(NULL);
  1432. }
  1433. cur = xmlXPtrAdvanceNode(cur, NULL);
  1434. }
  1435. return(list);
  1436. }
  1437. /**
  1438. * xmlXPtrBuildNodeList:
  1439. * @obj: the XPointer result from the evaluation.
  1440. *
  1441. * Build a node list tree copy of the XPointer result.
  1442. * This will drop Attributes and Namespace declarations.
  1443. *
  1444. * Returns an xmlNodePtr list or NULL.
  1445. * the caller has to free the node tree.
  1446. */
  1447. xmlNodePtr
  1448. xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
  1449. xmlNodePtr list = NULL, last = NULL;
  1450. int i;
  1451. if (obj == NULL)
  1452. return(NULL);
  1453. switch (obj->type) {
  1454. case XPATH_NODESET: {
  1455. xmlNodeSetPtr set = obj->nodesetval;
  1456. if (set == NULL)
  1457. return(NULL);
  1458. for (i = 0;i < set->nodeNr;i++) {
  1459. if (set->nodeTab[i] == NULL)
  1460. continue;
  1461. switch (set->nodeTab[i]->type) {
  1462. case XML_TEXT_NODE:
  1463. case XML_CDATA_SECTION_NODE:
  1464. case XML_ELEMENT_NODE:
  1465. case XML_ENTITY_REF_NODE:
  1466. case XML_ENTITY_NODE:
  1467. case XML_PI_NODE:
  1468. case XML_COMMENT_NODE:
  1469. case XML_DOCUMENT_NODE:
  1470. case XML_HTML_DOCUMENT_NODE:
  1471. #ifdef LIBXML_DOCB_ENABLED
  1472. case XML_DOCB_DOCUMENT_NODE:
  1473. #endif
  1474. case XML_XINCLUDE_START:
  1475. case XML_XINCLUDE_END:
  1476. break;
  1477. case XML_ATTRIBUTE_NODE:
  1478. case XML_NAMESPACE_DECL:
  1479. case XML_DOCUMENT_TYPE_NODE:
  1480. case XML_DOCUMENT_FRAG_NODE:
  1481. case XML_NOTATION_NODE:
  1482. case XML_DTD_NODE:
  1483. case XML_ELEMENT_DECL:
  1484. case XML_ATTRIBUTE_DECL:
  1485. case XML_ENTITY_DECL:
  1486. continue; /* for */
  1487. }
  1488. if (last == NULL)
  1489. list = last = xmlCopyNode(set->nodeTab[i], 1);
  1490. else {
  1491. xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
  1492. if (last->next != NULL)
  1493. last = last->next;
  1494. }
  1495. }
  1496. break;
  1497. }
  1498. case XPATH_LOCATIONSET: {
  1499. xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
  1500. if (set == NULL)
  1501. return(NULL);
  1502. for (i = 0;i < set->locNr;i++) {
  1503. if (last == NULL)
  1504. list = last = xmlXPtrBuildNodeList(set->locTab[i]);
  1505. else
  1506. xmlAddNextSibling(last,
  1507. xmlXPtrBuildNodeList(set->locTab[i]));
  1508. if (last != NULL) {
  1509. while (last->next != NULL)
  1510. last = last->next;
  1511. }
  1512. }
  1513. break;
  1514. }
  1515. case XPATH_RANGE:
  1516. return(xmlXPtrBuildRangeNodeList(obj));
  1517. case XPATH_POINT:
  1518. return(xmlCopyNode(obj->user, 0));
  1519. default:
  1520. break;
  1521. }
  1522. return(list);
  1523. }
  1524. /************************************************************************
  1525. * *
  1526. * XPointer functions *
  1527. * *
  1528. ************************************************************************/
  1529. /**
  1530. * xmlXPtrNbLocChildren:
  1531. * @node: an xmlNodePtr
  1532. *
  1533. * Count the number of location children of @node or the length of the
  1534. * string value in case of text/PI/Comments nodes
  1535. *
  1536. * Returns the number of location children
  1537. */
  1538. static int
  1539. xmlXPtrNbLocChildren(xmlNodePtr node) {
  1540. int ret = 0;
  1541. if (node == NULL)
  1542. return(-1);
  1543. switch (node->type) {
  1544. case XML_HTML_DOCUMENT_NODE:
  1545. case XML_DOCUMENT_NODE:
  1546. case XML_ELEMENT_NODE:
  1547. node = node->children;
  1548. while (node != NULL) {
  1549. if (node->type == XML_ELEMENT_NODE)
  1550. ret++;
  1551. node = node->next;
  1552. }
  1553. break;
  1554. case XML_ATTRIBUTE_NODE:
  1555. return(-1);
  1556. case XML_PI_NODE:
  1557. case XML_COMMENT_NODE:
  1558. case XML_TEXT_NODE:
  1559. case XML_CDATA_SECTION_NODE:
  1560. case XML_ENTITY_REF_NODE:
  1561. ret = xmlStrlen(node->content);
  1562. break;
  1563. default:
  1564. return(-1);
  1565. }
  1566. return(ret);
  1567. }
  1568. /**
  1569. * xmlXPtrHereFunction:
  1570. * @ctxt: the XPointer Parser context
  1571. * @nargs: the number of args
  1572. *
  1573. * Function implementing here() operation
  1574. * as described in 5.4.3
  1575. */
  1576. static void
  1577. xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
  1578. CHECK_ARITY(0);
  1579. if (ctxt->context->here == NULL)
  1580. XP_ERROR(XPTR_SYNTAX_ERROR);
  1581. valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
  1582. }
  1583. /**
  1584. * xmlXPtrOriginFunction:
  1585. * @ctxt: the XPointer Parser context
  1586. * @nargs: the number of args
  1587. *
  1588. * Function implementing origin() operation
  1589. * as described in 5.4.3
  1590. */
  1591. static void
  1592. xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
  1593. CHECK_ARITY(0);
  1594. if (ctxt->context->origin == NULL)
  1595. XP_ERROR(XPTR_SYNTAX_ERROR);
  1596. valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
  1597. }
  1598. /**
  1599. * xmlXPtrStartPointFunction:
  1600. * @ctxt: the XPointer Parser context
  1601. * @nargs: the number of args
  1602. *
  1603. * Function implementing start-point() operation
  1604. * as described in 5.4.3
  1605. * ----------------
  1606. * location-set start-point(location-set)
  1607. *
  1608. * For each location x in the argument location-set, start-point adds a
  1609. * location of type point to the result location-set. That point represents
  1610. * the start point of location x and is determined by the following rules:
  1611. *
  1612. * - If x is of type point, the start point is x.
  1613. * - If x is of type range, the start point is the start point of x.
  1614. * - If x is of type root, element, text, comment, or processing instruction,
  1615. * - the container node of the start point is x and the index is 0.
  1616. * - If x is of type attribute or namespace, the function must signal a
  1617. * syntax error.
  1618. * ----------------
  1619. *
  1620. */
  1621. static void
  1622. xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
  1623. xmlXPathObjectPtr tmp, obj, point;
  1624. xmlLocationSetPtr newset = NULL;
  1625. xmlLocationSetPtr oldset = NULL;
  1626. CHECK_ARITY(1);
  1627. if ((ctxt->value == NULL) ||
  1628. ((ctxt->value->type != XPATH_LOCATIONSET) &&
  1629. (ctxt->value->type != XPATH_NODESET)))
  1630. XP_ERROR(XPATH_INVALID_TYPE)
  1631. obj = valuePop(ctxt);
  1632. if (obj->type == XPATH_NODESET) {
  1633. /*
  1634. * First convert to a location set
  1635. */
  1636. tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
  1637. xmlXPathFreeObject(obj);
  1638. if (tmp == NULL)
  1639. XP_ERROR(XPATH_MEMORY_ERROR)
  1640. obj = tmp;
  1641. }
  1642. newset = xmlXPtrLocationSetCreate(NULL);
  1643. if (newset == NULL) {
  1644. xmlXPathFreeObject(obj);
  1645. XP_ERROR(XPATH_MEMORY_ERROR);
  1646. }
  1647. oldset = (xmlLocationSetPtr) obj->user;
  1648. if (oldset != NULL) {
  1649. int i;
  1650. for (i = 0; i < oldset->locNr; i++) {
  1651. tmp = oldset->locTab[i];
  1652. if (tmp == NULL)
  1653. continue;
  1654. point = NULL;
  1655. switch (tmp->type) {
  1656. case XPATH_POINT:
  1657. point = xmlXPtrNewPoint(tmp->user, tmp->index);
  1658. break;
  1659. case XPATH_RANGE: {
  1660. xmlNodePtr node = tmp->user;
  1661. if (node != NULL) {
  1662. if ((node->type == XML_ATTRIBUTE_NODE) ||
  1663. (node->type == XML_NAMESPACE_DECL)) {
  1664. xmlXPathFreeObject(obj);
  1665. xmlXPtrFreeLocationSet(newset);
  1666. XP_ERROR(XPTR_SYNTAX_ERROR);
  1667. }
  1668. point = xmlXPtrNewPoint(node, tmp->index);
  1669. }
  1670. break;
  1671. }
  1672. default:
  1673. /*** Should we raise an error ?
  1674. xmlXPathFreeObject(obj);
  1675. xmlXPathFreeObject(newset);
  1676. XP_ERROR(XPATH_INVALID_TYPE)
  1677. ***/
  1678. break;
  1679. }
  1680. if (point != NULL)
  1681. xmlXPtrLocationSetAdd(newset, point);
  1682. }
  1683. }
  1684. xmlXPathFreeObject(obj);
  1685. valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
  1686. }
  1687. /**
  1688. * xmlXPtrEndPointFunction:
  1689. * @ctxt: the XPointer Parser context
  1690. * @nargs: the number of args
  1691. *
  1692. * Function implementing end-point() operation
  1693. * as described in 5.4.3
  1694. * ----------------------------
  1695. * location-set end-point(location-set)
  1696. *
  1697. * For each location x in the argument location-set, end-point adds a
  1698. * location of type point to the result location-set. That point represents
  1699. * the end point of location x and is determined by the following rules:
  1700. *
  1701. * - If x is of type point, the resulting point is x.
  1702. * - If x is of type range, the resulting point is the end point of x.
  1703. * - If x is of type root or element, the container node of the resulting
  1704. * point is x and the index is the number of location children of x.
  1705. * - If x is of type text, comment, or processing instruction, the container
  1706. * node of the resulting point is x and the index is the length of the
  1707. * string-value of x.
  1708. * - If x is of type attribute or namespace, the function must signal a
  1709. * syntax error.
  1710. * ----------------------------
  1711. */
  1712. static void
  1713. xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
  1714. xmlXPathObjectPtr tmp, obj, point;
  1715. xmlLocationSetPtr newset = NULL;
  1716. xmlLocationSetPtr oldset = NULL;
  1717. CHECK_ARITY(1);
  1718. if ((ctxt->value == NULL) ||
  1719. ((ctxt->value->type != XPATH_LOCATIONSET) &&
  1720. (ctxt->value->type != XPATH_NODESET)))
  1721. XP_ERROR(XPATH_INVALID_TYPE)
  1722. obj = valuePop(ctxt);
  1723. if (obj->type == XPATH_NODESET) {
  1724. /*
  1725. * First convert to a location set
  1726. */
  1727. tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
  1728. xmlXPathFreeObject(obj);
  1729. if (tmp == NULL)
  1730. XP_ERROR(XPATH_MEMORY_ERROR)
  1731. obj = tmp;
  1732. }
  1733. newset = xmlXPtrLocationSetCreate(NULL);
  1734. if (newset == NULL) {
  1735. xmlXPathFreeObject(obj);
  1736. XP_ERROR(XPATH_MEMORY_ERROR);
  1737. }
  1738. oldset = (xmlLocationSetPtr) obj->user;
  1739. if (oldset != NULL) {
  1740. int i;
  1741. for (i = 0; i < oldset->locNr; i++) {
  1742. tmp = oldset->locTab[i];
  1743. if (tmp == NULL)
  1744. continue;
  1745. point = NULL;
  1746. switch (tmp->type) {
  1747. case XPATH_POINT:
  1748. point = xmlXPtrNewPoint(tmp->user, tmp->index);
  1749. break;
  1750. case XPATH_RANGE: {
  1751. xmlNodePtr node = tmp->user2;
  1752. if (node != NULL) {
  1753. if ((node->type == XML_ATTRIBUTE_NODE) ||
  1754. (node->type == XML_NAMESPACE_DECL)) {
  1755. xmlXPathFreeObject(obj);
  1756. xmlXPtrFreeLocationSet(newset);
  1757. XP_ERROR(XPTR_SYNTAX_ERROR);
  1758. }
  1759. point = xmlXPtrNewPoint(node, tmp->index2);
  1760. } else if (tmp->user == NULL) {
  1761. point = xmlXPtrNewPoint(node,
  1762. xmlXPtrNbLocChildren(node));
  1763. }
  1764. break;
  1765. }
  1766. default:
  1767. /*** Should we raise an error ?
  1768. xmlXPathFreeObject(obj);
  1769. xmlXPathFreeObject(newset);
  1770. XP_ERROR(XPATH_INVALID_TYPE)
  1771. ***/
  1772. break;
  1773. }
  1774. if (point != NULL)
  1775. xmlXPtrLocationSetAdd(newset, point);
  1776. }
  1777. }
  1778. xmlXPathFreeObject(obj);
  1779. valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
  1780. }
  1781. /**
  1782. * xmlXPtrCoveringRange:
  1783. * @ctxt: the XPointer Parser context
  1784. * @loc: the location for which the covering range must be computed
  1785. *
  1786. * A covering range is a range that wholly encompasses a location
  1787. * Section 5.3.3. Covering Ranges for All Location Types
  1788. * http://www.w3.org/TR/xptr#N2267
  1789. *
  1790. * Returns a new location or NULL in case of error
  1791. */
  1792. static xmlXPathObjectPtr
  1793. xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
  1794. if (loc == NULL)
  1795. return(NULL);
  1796. if ((ctxt == NULL) || (ctxt->context == NULL) ||
  1797. (ctxt->context->doc == NULL))
  1798. return(NULL);
  1799. switch (loc->type) {
  1800. case XPATH_POINT:
  1801. return(xmlXPtrNewRange(loc->user, loc->index,
  1802. loc->user, loc->index));
  1803. case XPATH_RANGE:
  1804. if (loc->user2 != NULL) {
  1805. return(xmlXPtrNewRange(loc->user, loc->index,
  1806. loc->user2, loc->index2));
  1807. } else {
  1808. xmlNodePtr node = (xmlNodePtr) loc->user;
  1809. if (node == (xmlNodePtr) ctxt->context->doc) {
  1810. return(xmlXPtrNewRange(node, 0, node,
  1811. xmlXPtrGetArity(node)));
  1812. } else {
  1813. switch (node->type) {
  1814. case XML_ATTRIBUTE_NODE:
  1815. /* !!! our model is slightly different than XPath */
  1816. return(xmlXPtrNewRange(node, 0, node,
  1817. xmlXPtrGetArity(node)));
  1818. case XML_ELEMENT_NODE:
  1819. case XML_TEXT_NODE:
  1820. case XML_CDATA_SECTION_NODE:
  1821. case XML_ENTITY_REF_NODE:
  1822. case XML_PI_NODE:
  1823. case XML_COMMENT_NODE:
  1824. case XML_DOCUMENT_NODE:
  1825. case XML_NOTATION_NODE:
  1826. case XML_HTML_DOCUMENT_NODE: {
  1827. int indx = xmlXPtrGetIndex(node);
  1828. node = node->parent;
  1829. return(xmlXPtrNewRange(node, indx - 1,
  1830. node, indx + 1));
  1831. }
  1832. default:
  1833. return(NULL);
  1834. }
  1835. }
  1836. }
  1837. default:
  1838. TODO /* missed one case ??? */
  1839. }
  1840. return(NULL);
  1841. }
  1842. /**
  1843. * xmlXPtrRangeFunction:
  1844. * @ctxt: the XPointer Parser context
  1845. * @nargs: the number of args
  1846. *
  1847. * Function implementing the range() function 5.4.3
  1848. * location-set range(location-set )
  1849. *
  1850. * The range function returns ranges covering the locations in
  1851. * the argument location-set. For each location x in the argument
  1852. * location-set, a range location representing the covering range of
  1853. * x is added to the result location-set.
  1854. */
  1855. static void
  1856. xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
  1857. int i;
  1858. xmlXPathObjectPtr set;
  1859. xmlLocationSetPtr oldset;
  1860. xmlLocationSetPtr newset;
  1861. CHECK_ARITY(1);
  1862. if ((ctxt->value == NULL) ||
  1863. ((ctxt->value->type != XPATH_LOCATIONSET) &&
  1864. (ctxt->value->type != XPATH_NODESET)))
  1865. XP_ERROR(XPATH_INVALID_TYPE)
  1866. set = valuePop(ctxt);
  1867. if (set->type == XPATH_NODESET) {
  1868. xmlXPathObjectPtr tmp;
  1869. /*
  1870. * First convert to a location set
  1871. */
  1872. tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
  1873. xmlXPathFreeObject(set);
  1874. if (tmp == NULL)
  1875. XP_ERROR(XPATH_MEMORY_ERROR)
  1876. set = tmp;
  1877. }
  1878. oldset = (xmlLocationSetPtr) set->user;
  1879. /*
  1880. * The loop is to compute the covering range for each item and add it
  1881. */
  1882. newset = xmlXPtrLocationSetCreate(NULL);
  1883. if (newset == NULL) {
  1884. xmlXPathFreeObject(set);
  1885. XP_ERROR(XPATH_MEMORY_ERROR);
  1886. }
  1887. if (oldset != NULL) {
  1888. for (i = 0;i < oldset->locNr;i++) {
  1889. xmlXPtrLocationSetAdd(newset,
  1890. xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
  1891. }
  1892. }
  1893. /*
  1894. * Save the new value and cleanup
  1895. */
  1896. valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
  1897. xmlXPathFreeObject(set);
  1898. }
  1899. /**
  1900. * xmlXPtrInsideRange:
  1901. * @ctxt: the XPointer Parser context
  1902. * @loc: the location for which the inside range must be computed
  1903. *
  1904. * A inside range is a range described in the range-inside() description
  1905. *
  1906. * Returns a new location or NULL in case of error
  1907. */
  1908. static xmlXPathObjectPtr
  1909. xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
  1910. if (loc == NULL)
  1911. return(NULL);
  1912. if ((ctxt == NULL) || (ctxt->context == NULL) ||
  1913. (ctxt->context->doc == NULL))
  1914. return(NULL);
  1915. switch (loc->type) {
  1916. case XPATH_POINT: {
  1917. xmlNodePtr node = (xmlNodePtr) loc->user;
  1918. switch (node->type) {
  1919. case XML_PI_NODE:
  1920. case XML_COMMENT_NODE:
  1921. case XML_TEXT_NODE:
  1922. case XML_CDATA_SECTION_NODE: {
  1923. if (node->content == NULL) {
  1924. return(xmlXPtrNewRange(node, 0, node, 0));
  1925. } else {
  1926. return(xmlXPtrNewRange(node, 0, node,
  1927. xmlStrlen(node->content)));
  1928. }
  1929. }
  1930. case XML_ATTRIBUTE_NODE:
  1931. case XML_ELEMENT_NODE:
  1932. case XML_ENTITY_REF_NODE:
  1933. case XML_DOCUMENT_NODE:
  1934. case XML_NOTATION_NODE:
  1935. case XML_HTML_DOCUMENT_NODE: {
  1936. return(xmlXPtrNewRange(node, 0, node,
  1937. xmlXPtrGetArity(node)));
  1938. }
  1939. default:
  1940. break;
  1941. }
  1942. return(NULL);
  1943. }
  1944. case XPATH_RANGE: {
  1945. xmlNodePtr node = (xmlNodePtr) loc->user;
  1946. if (loc->user2 != NULL) {
  1947. return(xmlXPtrNewRange(node, loc->index,
  1948. loc->user2, loc->index2));
  1949. } else {
  1950. switch (node->type) {
  1951. case XML_PI_NODE:
  1952. case XML_COMMENT_NODE:
  1953. case XML_TEXT_NODE:
  1954. case XML_CDATA_SECTION_NODE: {
  1955. if (node->content == NULL) {
  1956. return(xmlXPtrNewRange(node, 0, node, 0));
  1957. } else {
  1958. return(xmlXPtrNewRange(node, 0, node,
  1959. xmlStrlen(node->content)));
  1960. }
  1961. }
  1962. case XML_ATTRIBUTE_NODE:
  1963. case XML_ELEMENT_NODE:
  1964. case XML_ENTITY_REF_NODE:
  1965. case XML_DOCUMENT_NODE:
  1966. case XML_NOTATION_NODE:
  1967. case XML_HTML_DOCUMENT_NODE: {
  1968. return(xmlXPtrNewRange(node, 0, node,
  1969. xmlXPtrGetArity(node)));
  1970. }
  1971. default:
  1972. break;
  1973. }
  1974. return(NULL);
  1975. }
  1976. }
  1977. default:
  1978. TODO /* missed one case ??? */
  1979. }
  1980. return(NULL);
  1981. }
  1982. /**
  1983. * xmlXPtrRangeInsideFunction:
  1984. * @ctxt: the XPointer Parser context
  1985. * @nargs: the number of args
  1986. *
  1987. * Function implementing the range-inside() function 5.4.3
  1988. * location-set range-inside(location-set )
  1989. *
  1990. * The range-inside function returns ranges covering the contents of
  1991. * the locations in the argument location-set. For each location x in
  1992. * the argument location-set, a range location is added to the result
  1993. * location-set. If x is a range location, then x is added to the
  1994. * result location-set. If x is not a range location, then x is used
  1995. * as the container location of the start and end points of the range
  1996. * location to be added; the index of the start point of the range is
  1997. * zero; if the end point is a character point then its index is the
  1998. * length of the string-value of x, and otherwise is the number of
  1999. * location children of x.
  2000. *
  2001. */
  2002. static void
  2003. xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
  2004. int i;
  2005. xmlXPathObjectPtr set;
  2006. xmlLocationSetPtr oldset;
  2007. xmlLocationSetPtr newset;
  2008. CHECK_ARITY(1);
  2009. if ((ctxt->value == NULL) ||
  2010. ((ctxt->value->type != XPATH_LOCATIONSET) &&
  2011. (ctxt->value->type != XPATH_NODESET)))
  2012. XP_ERROR(XPATH_INVALID_TYPE)
  2013. set = valuePop(ctxt);
  2014. if (set->type == XPATH_NODESET) {
  2015. xmlXPathObjectPtr tmp;
  2016. /*
  2017. * First convert to a location set
  2018. */
  2019. tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
  2020. xmlXPathFreeObject(set);
  2021. if (tmp == NULL)
  2022. XP_ERROR(XPATH_MEMORY_ERROR)
  2023. set = tmp;
  2024. }
  2025. /*
  2026. * The loop is to compute the covering range for each item and add it
  2027. */
  2028. newset = xmlXPtrLocationSetCreate(NULL);
  2029. if (newset == NULL) {
  2030. xmlXPathFreeObject(set);
  2031. XP_ERROR(XPATH_MEMORY_ERROR);
  2032. }
  2033. oldset = (xmlLocationSetPtr) set->user;
  2034. if (oldset != NULL) {
  2035. for (i = 0;i < oldset->locNr;i++) {
  2036. xmlXPtrLocationSetAdd(newset,
  2037. xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
  2038. }
  2039. }
  2040. /*
  2041. * Save the new value and cleanup
  2042. */
  2043. valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
  2044. xmlXPathFreeObject(set);
  2045. }
  2046. /**
  2047. * xmlXPtrRangeToFunction:
  2048. * @ctxt: the XPointer Parser context
  2049. * @nargs: the number of args
  2050. *
  2051. * Implement the range-to() XPointer function
  2052. *
  2053. * Obsolete. range-to is not a real function but a special type of location
  2054. * step which is handled in xpath.c.
  2055. */
  2056. void
  2057. xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt,
  2058. int nargs ATTRIBUTE_UNUSED) {
  2059. XP_ERROR(XPATH_EXPR_ERROR);
  2060. }
  2061. /**
  2062. * xmlXPtrAdvanceNode:
  2063. * @cur: the node
  2064. * @level: incremented/decremented to show level in tree
  2065. *
  2066. * Advance to the next element or text node in document order
  2067. * TODO: add a stack for entering/exiting entities
  2068. *
  2069. * Returns -1 in case of failure, 0 otherwise
  2070. */
  2071. xmlNodePtr
  2072. xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) {
  2073. next:
  2074. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
  2075. return(NULL);
  2076. if (cur->children != NULL) {
  2077. cur = cur->children ;
  2078. if (level != NULL)
  2079. (*level)++;
  2080. goto found;
  2081. }
  2082. skip: /* This label should only be needed if something is wrong! */
  2083. if (cur->next != NULL) {
  2084. cur = cur->next;
  2085. goto found;
  2086. }
  2087. do {
  2088. cur = cur->parent;
  2089. if (level != NULL)
  2090. (*level)--;
  2091. if (cur == NULL) return(NULL);
  2092. if (cur->next != NULL) {
  2093. cur = cur->next;
  2094. goto found;
  2095. }
  2096. } while (cur != NULL);
  2097. found:
  2098. if ((cur->type != XML_ELEMENT_NODE) &&
  2099. (cur->type != XML_TEXT_NODE) &&
  2100. (cur->type != XML_DOCUMENT_NODE) &&
  2101. (cur->type != XML_HTML_DOCUMENT_NODE) &&
  2102. (cur->type != XML_CDATA_SECTION_NODE)) {
  2103. if (cur->type == XML_ENTITY_REF_NODE) { /* Shouldn't happen */
  2104. TODO
  2105. goto skip;
  2106. }
  2107. goto next;
  2108. }
  2109. return(cur);
  2110. }
  2111. /**
  2112. * xmlXPtrAdvanceChar:
  2113. * @node: the node
  2114. * @indx: the indx
  2115. * @bytes: the number of bytes
  2116. *
  2117. * Advance a point of the associated number of bytes (not UTF8 chars)
  2118. *
  2119. * Returns -1 in case of failure, 0 otherwise
  2120. */
  2121. static int
  2122. xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
  2123. xmlNodePtr cur;
  2124. int pos;
  2125. int len;
  2126. if ((node == NULL) || (indx == NULL))
  2127. return(-1);
  2128. cur = *node;
  2129. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
  2130. return(-1);
  2131. pos = *indx;
  2132. while (bytes >= 0) {
  2133. /*
  2134. * First position to the beginning of the first text node
  2135. * corresponding to this point
  2136. */
  2137. while ((cur != NULL) &&
  2138. ((cur->type == XML_ELEMENT_NODE) ||
  2139. (cur->type == XML_DOCUMENT_NODE) ||
  2140. (cur->type == XML_HTML_DOCUMENT_NODE))) {
  2141. if (pos > 0) {
  2142. cur = xmlXPtrGetNthChild(cur, pos);
  2143. pos = 0;
  2144. } else {
  2145. cur = xmlXPtrAdvanceNode(cur, NULL);
  2146. pos = 0;
  2147. }
  2148. }
  2149. if (cur == NULL) {
  2150. *node = NULL;
  2151. *indx = 0;
  2152. return(-1);
  2153. }
  2154. /*
  2155. * if there is no move needed return the current value.
  2156. */
  2157. if (pos == 0) pos = 1;
  2158. if (bytes == 0) {
  2159. *node = cur;
  2160. *indx = pos;
  2161. return(0);
  2162. }
  2163. /*
  2164. * We should have a text (or cdata) node ...
  2165. */
  2166. len = 0;
  2167. if ((cur->type != XML_ELEMENT_NODE) &&
  2168. (cur->content != NULL)) {
  2169. len = xmlStrlen(cur->content);
  2170. }
  2171. if (pos > len) {
  2172. /* Strange, the indx in the text node is greater than it's len */
  2173. STRANGE
  2174. pos = len;
  2175. }
  2176. if (pos + bytes >= len) {
  2177. bytes -= (len - pos);
  2178. cur = xmlXPtrAdvanceNode(cur, NULL);
  2179. pos = 0;
  2180. } else if (pos + bytes < len) {
  2181. pos += bytes;
  2182. *node = cur;
  2183. *indx = pos;
  2184. return(0);
  2185. }
  2186. }
  2187. return(-1);
  2188. }
  2189. /**
  2190. * xmlXPtrMatchString:
  2191. * @string: the string to search
  2192. * @start: the start textnode
  2193. * @startindex: the start index
  2194. * @end: the end textnode IN/OUT
  2195. * @endindex: the end index IN/OUT
  2196. *
  2197. * Check whether the document contains @string at the position
  2198. * (@start, @startindex) and limited by the (@end, @endindex) point
  2199. *
  2200. * Returns -1 in case of failure, 0 if not found, 1 if found in which case
  2201. * (@start, @startindex) will indicate the position of the beginning
  2202. * of the range and (@end, @endindex) will indicate the end
  2203. * of the range
  2204. */
  2205. static int
  2206. xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
  2207. xmlNodePtr *end, int *endindex) {
  2208. xmlNodePtr cur;
  2209. int pos; /* 0 based */
  2210. int len; /* in bytes */
  2211. int stringlen; /* in bytes */
  2212. int match;
  2213. if (string == NULL)
  2214. return(-1);
  2215. if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
  2216. return(-1);
  2217. if ((end == NULL) || (*end == NULL) ||
  2218. ((*end)->type == XML_NAMESPACE_DECL) || (endindex == NULL))
  2219. return(-1);
  2220. cur = start;
  2221. pos = startindex - 1;
  2222. stringlen = xmlStrlen(string);
  2223. while (stringlen > 0) {
  2224. if ((cur == *end) && (pos + stringlen > *endindex))
  2225. return(0);
  2226. if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
  2227. len = xmlStrlen(cur->content);
  2228. if (len >= pos + stringlen) {
  2229. match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
  2230. if (match) {
  2231. #ifdef DEBUG_RANGES
  2232. xmlGenericError(xmlGenericErrorContext,
  2233. "found range %d bytes at index %d of ->",
  2234. stringlen, pos + 1);
  2235. xmlDebugDumpString(stdout, cur->content);
  2236. xmlGenericError(xmlGenericErrorContext, "\n");
  2237. #endif
  2238. *end = cur;
  2239. *endindex = pos + stringlen;
  2240. return(1);
  2241. } else {
  2242. return(0);
  2243. }
  2244. } else {
  2245. int sub = len - pos;
  2246. match = (!xmlStrncmp(&cur->content[pos], string, sub));
  2247. if (match) {
  2248. #ifdef DEBUG_RANGES
  2249. xmlGenericError(xmlGenericErrorContext,
  2250. "found subrange %d bytes at index %d of ->",
  2251. sub, pos + 1);
  2252. xmlDebugDumpString(stdout, cur->content);
  2253. xmlGenericError(xmlGenericErrorContext, "\n");
  2254. #endif
  2255. string = &string[sub];
  2256. stringlen -= sub;
  2257. } else {
  2258. return(0);
  2259. }
  2260. }
  2261. }
  2262. cur = xmlXPtrAdvanceNode(cur, NULL);
  2263. if (cur == NULL)
  2264. return(0);
  2265. pos = 0;
  2266. }
  2267. return(1);
  2268. }
  2269. /**
  2270. * xmlXPtrSearchString:
  2271. * @string: the string to search
  2272. * @start: the start textnode IN/OUT
  2273. * @startindex: the start index IN/OUT
  2274. * @end: the end textnode
  2275. * @endindex: the end index
  2276. *
  2277. * Search the next occurrence of @string within the document content
  2278. * until the (@end, @endindex) point is reached
  2279. *
  2280. * Returns -1 in case of failure, 0 if not found, 1 if found in which case
  2281. * (@start, @startindex) will indicate the position of the beginning
  2282. * of the range and (@end, @endindex) will indicate the end
  2283. * of the range
  2284. */
  2285. static int
  2286. xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
  2287. xmlNodePtr *end, int *endindex) {
  2288. xmlNodePtr cur;
  2289. const xmlChar *str;
  2290. int pos; /* 0 based */
  2291. int len; /* in bytes */
  2292. xmlChar first;
  2293. if (string == NULL)
  2294. return(-1);
  2295. if ((start == NULL) || (*start == NULL) ||
  2296. ((*start)->type == XML_NAMESPACE_DECL) || (startindex == NULL))
  2297. return(-1);
  2298. if ((end == NULL) || (endindex == NULL))
  2299. return(-1);
  2300. cur = *start;
  2301. pos = *startindex - 1;
  2302. first = string[0];
  2303. while (cur != NULL) {
  2304. if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
  2305. len = xmlStrlen(cur->content);
  2306. while (pos <= len) {
  2307. if (first != 0) {
  2308. str = xmlStrchr(&cur->content[pos], first);
  2309. if (str != NULL) {
  2310. pos = (str - (xmlChar *)(cur->content));
  2311. #ifdef DEBUG_RANGES
  2312. xmlGenericError(xmlGenericErrorContext,
  2313. "found '%c' at index %d of ->",
  2314. first, pos + 1);
  2315. xmlDebugDumpString(stdout, cur->content);
  2316. xmlGenericError(xmlGenericErrorContext, "\n");
  2317. #endif
  2318. if (xmlXPtrMatchString(string, cur, pos + 1,
  2319. end, endindex)) {
  2320. *start = cur;
  2321. *startindex = pos + 1;
  2322. return(1);
  2323. }
  2324. pos++;
  2325. } else {
  2326. pos = len + 1;
  2327. }
  2328. } else {
  2329. /*
  2330. * An empty string is considered to match before each
  2331. * character of the string-value and after the final
  2332. * character.
  2333. */
  2334. #ifdef DEBUG_RANGES
  2335. xmlGenericError(xmlGenericErrorContext,
  2336. "found '' at index %d of ->",
  2337. pos + 1);
  2338. xmlDebugDumpString(stdout, cur->content);
  2339. xmlGenericError(xmlGenericErrorContext, "\n");
  2340. #endif
  2341. *start = cur;
  2342. *startindex = pos + 1;
  2343. *end = cur;
  2344. *endindex = pos + 1;
  2345. return(1);
  2346. }
  2347. }
  2348. }
  2349. if ((cur == *end) && (pos >= *endindex))
  2350. return(0);
  2351. cur = xmlXPtrAdvanceNode(cur, NULL);
  2352. if (cur == NULL)
  2353. return(0);
  2354. pos = 1;
  2355. }
  2356. return(0);
  2357. }
  2358. /**
  2359. * xmlXPtrGetLastChar:
  2360. * @node: the node
  2361. * @index: the index
  2362. *
  2363. * Computes the point coordinates of the last char of this point
  2364. *
  2365. * Returns -1 in case of failure, 0 otherwise
  2366. */
  2367. static int
  2368. xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
  2369. xmlNodePtr cur;
  2370. int pos, len = 0;
  2371. if ((node == NULL) || (*node == NULL) ||
  2372. ((*node)->type == XML_NAMESPACE_DECL) || (indx == NULL))
  2373. return(-1);
  2374. cur = *node;
  2375. pos = *indx;
  2376. if ((cur->type == XML_ELEMENT_NODE) ||
  2377. (cur->type == XML_DOCUMENT_NODE) ||
  2378. (cur->type == XML_HTML_DOCUMENT_NODE)) {
  2379. if (pos > 0) {
  2380. cur = xmlXPtrGetNthChild(cur, pos);
  2381. }
  2382. }
  2383. while (cur != NULL) {
  2384. if (cur->last != NULL)
  2385. cur = cur->last;
  2386. else if ((cur->type != XML_ELEMENT_NODE) &&
  2387. (cur->content != NULL)) {
  2388. len = xmlStrlen(cur->content);
  2389. break;
  2390. } else {
  2391. return(-1);
  2392. }
  2393. }
  2394. if (cur == NULL)
  2395. return(-1);
  2396. *node = cur;
  2397. *indx = len;
  2398. return(0);
  2399. }
  2400. /**
  2401. * xmlXPtrGetStartPoint:
  2402. * @obj: an range
  2403. * @node: the resulting node
  2404. * @indx: the resulting index
  2405. *
  2406. * read the object and return the start point coordinates.
  2407. *
  2408. * Returns -1 in case of failure, 0 otherwise
  2409. */
  2410. static int
  2411. xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
  2412. if ((obj == NULL) || (node == NULL) || (indx == NULL))
  2413. return(-1);
  2414. switch (obj->type) {
  2415. case XPATH_POINT:
  2416. *node = obj->user;
  2417. if (obj->index <= 0)
  2418. *indx = 0;
  2419. else
  2420. *indx = obj->index;
  2421. return(0);
  2422. case XPATH_RANGE:
  2423. *node = obj->user;
  2424. if (obj->index <= 0)
  2425. *indx = 0;
  2426. else
  2427. *indx = obj->index;
  2428. return(0);
  2429. default:
  2430. break;
  2431. }
  2432. return(-1);
  2433. }
  2434. /**
  2435. * xmlXPtrGetEndPoint:
  2436. * @obj: an range
  2437. * @node: the resulting node
  2438. * @indx: the resulting indx
  2439. *
  2440. * read the object and return the end point coordinates.
  2441. *
  2442. * Returns -1 in case of failure, 0 otherwise
  2443. */
  2444. static int
  2445. xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
  2446. if ((obj == NULL) || (node == NULL) || (indx == NULL))
  2447. return(-1);
  2448. switch (obj->type) {
  2449. case XPATH_POINT:
  2450. *node = obj->user;
  2451. if (obj->index <= 0)
  2452. *indx = 0;
  2453. else
  2454. *indx = obj->index;
  2455. return(0);
  2456. case XPATH_RANGE:
  2457. *node = obj->user;
  2458. if (obj->index <= 0)
  2459. *indx = 0;
  2460. else
  2461. *indx = obj->index;
  2462. return(0);
  2463. default:
  2464. break;
  2465. }
  2466. return(-1);
  2467. }
  2468. /**
  2469. * xmlXPtrStringRangeFunction:
  2470. * @ctxt: the XPointer Parser context
  2471. * @nargs: the number of args
  2472. *
  2473. * Function implementing the string-range() function
  2474. * range as described in 5.4.2
  2475. *
  2476. * ------------------------------
  2477. * [Definition: For each location in the location-set argument,
  2478. * string-range returns a set of string ranges, a set of substrings in a
  2479. * string. Specifically, the string-value of the location is searched for
  2480. * substrings that match the string argument, and the resulting location-set
  2481. * will contain a range location for each non-overlapping match.]
  2482. * An empty string is considered to match before each character of the
  2483. * string-value and after the final character. Whitespace in a string
  2484. * is matched literally, with no normalization except that provided by
  2485. * XML for line ends. The third argument gives the position of the first
  2486. * character to be in the resulting range, relative to the start of the
  2487. * match. The default value is 1, which makes the range start immediately
  2488. * before the first character of the matched string. The fourth argument
  2489. * gives the number of characters in the range; the default is that the
  2490. * range extends to the end of the matched string.
  2491. *
  2492. * Element boundaries, as well as entire embedded nodes such as processing
  2493. * instructions and comments, are ignored as defined in [XPath].
  2494. *
  2495. * If the string in the second argument is not found in the string-value
  2496. * of the location, or if a value in the third or fourth argument indicates
  2497. * a string that is beyond the beginning or end of the document, the
  2498. * expression fails.
  2499. *
  2500. * The points of the range-locations in the returned location-set will
  2501. * all be character points.
  2502. * ------------------------------
  2503. */
  2504. static void
  2505. xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
  2506. int i, startindex, endindex = 0, fendindex;
  2507. xmlNodePtr start, end = 0, fend;
  2508. xmlXPathObjectPtr set = NULL;
  2509. xmlLocationSetPtr oldset;
  2510. xmlLocationSetPtr newset = NULL;
  2511. xmlXPathObjectPtr string = NULL;
  2512. xmlXPathObjectPtr position = NULL;
  2513. xmlXPathObjectPtr number = NULL;
  2514. int found, pos = 0, num = 0;
  2515. /*
  2516. * Grab the arguments
  2517. */
  2518. if ((nargs < 2) || (nargs > 4))
  2519. XP_ERROR(XPATH_INVALID_ARITY);
  2520. if (nargs >= 4) {
  2521. if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_NUMBER)) {
  2522. xmlXPathErr(ctxt, XPATH_INVALID_TYPE);
  2523. goto error;
  2524. }
  2525. number = valuePop(ctxt);
  2526. if (number != NULL)
  2527. num = (int) number->floatval;
  2528. }
  2529. if (nargs >= 3) {
  2530. if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_NUMBER)) {
  2531. xmlXPathErr(ctxt, XPATH_INVALID_TYPE);
  2532. goto error;
  2533. }
  2534. position = valuePop(ctxt);
  2535. if (position != NULL)
  2536. pos = (int) position->floatval;
  2537. }
  2538. if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_STRING)) {
  2539. xmlXPathErr(ctxt, XPATH_INVALID_TYPE);
  2540. goto error;
  2541. }
  2542. string = valuePop(ctxt);
  2543. if ((ctxt->value == NULL) ||
  2544. ((ctxt->value->type != XPATH_LOCATIONSET) &&
  2545. (ctxt->value->type != XPATH_NODESET))) {
  2546. xmlXPathErr(ctxt, XPATH_INVALID_TYPE);
  2547. goto error;
  2548. }
  2549. set = valuePop(ctxt);
  2550. newset = xmlXPtrLocationSetCreate(NULL);
  2551. if (newset == NULL) {
  2552. xmlXPathErr(ctxt, XPATH_MEMORY_ERROR);
  2553. goto error;
  2554. }
  2555. if (set->nodesetval == NULL) {
  2556. goto error;
  2557. }
  2558. if (set->type == XPATH_NODESET) {
  2559. xmlXPathObjectPtr tmp;
  2560. /*
  2561. * First convert to a location set
  2562. */
  2563. tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
  2564. xmlXPathFreeObject(set);
  2565. if (tmp == NULL) {
  2566. xmlXPathErr(ctxt, XPATH_MEMORY_ERROR);
  2567. goto error;
  2568. }
  2569. set = tmp;
  2570. }
  2571. oldset = (xmlLocationSetPtr) set->user;
  2572. /*
  2573. * The loop is to search for each element in the location set
  2574. * the list of location set corresponding to that search
  2575. */
  2576. for (i = 0;i < oldset->locNr;i++) {
  2577. #ifdef DEBUG_RANGES
  2578. xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
  2579. #endif
  2580. xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
  2581. xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
  2582. xmlXPtrAdvanceChar(&start, &startindex, 0);
  2583. xmlXPtrGetLastChar(&end, &endindex);
  2584. #ifdef DEBUG_RANGES
  2585. xmlGenericError(xmlGenericErrorContext,
  2586. "from index %d of ->", startindex);
  2587. xmlDebugDumpString(stdout, start->content);
  2588. xmlGenericError(xmlGenericErrorContext, "\n");
  2589. xmlGenericError(xmlGenericErrorContext,
  2590. "to index %d of ->", endindex);
  2591. xmlDebugDumpString(stdout, end->content);
  2592. xmlGenericError(xmlGenericErrorContext, "\n");
  2593. #endif
  2594. do {
  2595. fend = end;
  2596. fendindex = endindex;
  2597. found = xmlXPtrSearchString(string->stringval, &start, &startindex,
  2598. &fend, &fendindex);
  2599. if (found == 1) {
  2600. if (position == NULL) {
  2601. xmlXPtrLocationSetAdd(newset,
  2602. xmlXPtrNewRange(start, startindex, fend, fendindex));
  2603. } else if (xmlXPtrAdvanceChar(&start, &startindex,
  2604. pos - 1) == 0) {
  2605. if ((number != NULL) && (num > 0)) {
  2606. int rindx;
  2607. xmlNodePtr rend;
  2608. rend = start;
  2609. rindx = startindex - 1;
  2610. if (xmlXPtrAdvanceChar(&rend, &rindx,
  2611. num) == 0) {
  2612. xmlXPtrLocationSetAdd(newset,
  2613. xmlXPtrNewRange(start, startindex,
  2614. rend, rindx));
  2615. }
  2616. } else if ((number != NULL) && (num <= 0)) {
  2617. xmlXPtrLocationSetAdd(newset,
  2618. xmlXPtrNewRange(start, startindex,
  2619. start, startindex));
  2620. } else {
  2621. xmlXPtrLocationSetAdd(newset,
  2622. xmlXPtrNewRange(start, startindex,
  2623. fend, fendindex));
  2624. }
  2625. }
  2626. start = fend;
  2627. startindex = fendindex;
  2628. if (string->stringval[0] == 0)
  2629. startindex++;
  2630. }
  2631. } while (found == 1);
  2632. }
  2633. /*
  2634. * Save the new value and cleanup
  2635. */
  2636. error:
  2637. if (newset != NULL)
  2638. valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
  2639. xmlXPathFreeObject(set);
  2640. xmlXPathFreeObject(string);
  2641. if (position) xmlXPathFreeObject(position);
  2642. if (number) xmlXPathFreeObject(number);
  2643. }
  2644. /**
  2645. * xmlXPtrEvalRangePredicate:
  2646. * @ctxt: the XPointer Parser context
  2647. *
  2648. * [8] Predicate ::= '[' PredicateExpr ']'
  2649. * [9] PredicateExpr ::= Expr
  2650. *
  2651. * Evaluate a predicate as in xmlXPathEvalPredicate() but for
  2652. * a Location Set instead of a node set
  2653. */
  2654. void
  2655. xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
  2656. const xmlChar *cur;
  2657. xmlXPathObjectPtr res;
  2658. xmlXPathObjectPtr obj, tmp;
  2659. xmlLocationSetPtr newset = NULL;
  2660. xmlLocationSetPtr oldset;
  2661. int i;
  2662. if (ctxt == NULL) return;
  2663. SKIP_BLANKS;
  2664. if (CUR != '[') {
  2665. XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
  2666. }
  2667. NEXT;
  2668. SKIP_BLANKS;
  2669. /*
  2670. * Extract the old set, and then evaluate the result of the
  2671. * expression for all the element in the set. use it to grow
  2672. * up a new set.
  2673. */
  2674. CHECK_TYPE(XPATH_LOCATIONSET);
  2675. obj = valuePop(ctxt);
  2676. oldset = obj->user;
  2677. ctxt->context->node = NULL;
  2678. if ((oldset == NULL) || (oldset->locNr == 0)) {
  2679. ctxt->context->contextSize = 0;
  2680. ctxt->context->proximityPosition = 0;
  2681. xmlXPathEvalExpr(ctxt);
  2682. res = valuePop(ctxt);
  2683. if (res != NULL)
  2684. xmlXPathFreeObject(res);
  2685. valuePush(ctxt, obj);
  2686. CHECK_ERROR;
  2687. } else {
  2688. /*
  2689. * Save the expression pointer since we will have to evaluate
  2690. * it multiple times. Initialize the new set.
  2691. */
  2692. cur = ctxt->cur;
  2693. newset = xmlXPtrLocationSetCreate(NULL);
  2694. for (i = 0; i < oldset->locNr; i++) {
  2695. ctxt->cur = cur;
  2696. /*
  2697. * Run the evaluation with a node list made of a single item
  2698. * in the nodeset.
  2699. */
  2700. ctxt->context->node = oldset->locTab[i]->user;
  2701. tmp = xmlXPathNewNodeSet(ctxt->context->node);
  2702. valuePush(ctxt, tmp);
  2703. ctxt->context->contextSize = oldset->locNr;
  2704. ctxt->context->proximityPosition = i + 1;
  2705. xmlXPathEvalExpr(ctxt);
  2706. CHECK_ERROR;
  2707. /*
  2708. * The result of the evaluation need to be tested to
  2709. * decided whether the filter succeeded or not
  2710. */
  2711. res = valuePop(ctxt);
  2712. if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
  2713. xmlXPtrLocationSetAdd(newset,
  2714. xmlXPathObjectCopy(oldset->locTab[i]));
  2715. }
  2716. /*
  2717. * Cleanup
  2718. */
  2719. if (res != NULL)
  2720. xmlXPathFreeObject(res);
  2721. if (ctxt->value == tmp) {
  2722. res = valuePop(ctxt);
  2723. xmlXPathFreeObject(res);
  2724. }
  2725. ctxt->context->node = NULL;
  2726. }
  2727. /*
  2728. * The result is used as the new evaluation set.
  2729. */
  2730. xmlXPathFreeObject(obj);
  2731. ctxt->context->node = NULL;
  2732. ctxt->context->contextSize = -1;
  2733. ctxt->context->proximityPosition = -1;
  2734. valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
  2735. }
  2736. if (CUR != ']') {
  2737. XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
  2738. }
  2739. NEXT;
  2740. SKIP_BLANKS;
  2741. }
  2742. #define bottom_xpointer
  2743. #include "elfgcchack.h"
  2744. #endif