xpointer.c 75 KB

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