xpointer.c 75 KB

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