pattern.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621
  1. /*
  2. * pattern.c: Implementation of selectors for nodes
  3. *
  4. * Reference:
  5. * http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/
  6. * to some extent
  7. * http://www.w3.org/TR/1999/REC-xml-19991116
  8. *
  9. * See Copyright for the status of this software.
  10. *
  11. * daniel@veillard.com
  12. */
  13. /*
  14. * TODO:
  15. * - compilation flags to check for specific syntaxes
  16. * using flags of xmlPatterncompile()
  17. * - making clear how pattern starting with / or . need to be handled,
  18. * currently push(NULL, NULL) means a reset of the streaming context
  19. * and indicating we are on / (the document node), probably need
  20. * something similar for .
  21. * - get rid of the "compile" starting with lowercase
  22. * - DONE (2006-05-16): get rid of the Strdup/Strndup in case of dictionary
  23. */
  24. #define IN_LIBXML
  25. #include "libxml.h"
  26. #include <string.h>
  27. #include <libxml/xmlmemory.h>
  28. #include <libxml/tree.h>
  29. #include <libxml/hash.h>
  30. #include <libxml/dict.h>
  31. #include <libxml/xmlerror.h>
  32. #include <libxml/parserInternals.h>
  33. #include <libxml/pattern.h>
  34. #ifdef LIBXML_PATTERN_ENABLED
  35. /* #define DEBUG_STREAMING */
  36. #ifdef ERROR
  37. #undef ERROR
  38. #endif
  39. #define ERROR(a, b, c, d)
  40. #define ERROR5(a, b, c, d, e)
  41. #define XML_STREAM_STEP_DESC 1
  42. #define XML_STREAM_STEP_FINAL 2
  43. #define XML_STREAM_STEP_ROOT 4
  44. #define XML_STREAM_STEP_ATTR 8
  45. #define XML_STREAM_STEP_NODE 16
  46. #define XML_STREAM_STEP_IN_SET 32
  47. /*
  48. * NOTE: Those private flags (XML_STREAM_xxx) are used
  49. * in _xmlStreamCtxt->flag. They extend the public
  50. * xmlPatternFlags, so be careful not to interfere with the
  51. * reserved values for xmlPatternFlags.
  52. */
  53. #define XML_STREAM_FINAL_IS_ANY_NODE 1<<14
  54. #define XML_STREAM_FROM_ROOT 1<<15
  55. #define XML_STREAM_DESC 1<<16
  56. /*
  57. * XML_STREAM_ANY_NODE is used for comparison against
  58. * xmlElementType enums, to indicate a node of any type.
  59. */
  60. #define XML_STREAM_ANY_NODE 100
  61. #define XML_PATTERN_NOTPATTERN (XML_PATTERN_XPATH | \
  62. XML_PATTERN_XSSEL | \
  63. XML_PATTERN_XSFIELD)
  64. #define XML_STREAM_XS_IDC(c) ((c)->flags & \
  65. (XML_PATTERN_XSSEL | XML_PATTERN_XSFIELD))
  66. #define XML_STREAM_XS_IDC_SEL(c) ((c)->flags & XML_PATTERN_XSSEL)
  67. #define XML_STREAM_XS_IDC_FIELD(c) ((c)->flags & XML_PATTERN_XSFIELD)
  68. #define XML_PAT_COPY_NSNAME(c, r, nsname) \
  69. if ((c)->comp->dict) \
  70. r = (xmlChar *) xmlDictLookup((c)->comp->dict, BAD_CAST nsname, -1); \
  71. else r = xmlStrdup(BAD_CAST nsname);
  72. #define XML_PAT_FREE_STRING(c, r) if ((c)->comp->dict == NULL) xmlFree(r);
  73. typedef struct _xmlStreamStep xmlStreamStep;
  74. typedef xmlStreamStep *xmlStreamStepPtr;
  75. struct _xmlStreamStep {
  76. int flags; /* properties of that step */
  77. const xmlChar *name; /* first string value if NULL accept all */
  78. const xmlChar *ns; /* second string value */
  79. int nodeType; /* type of node */
  80. };
  81. typedef struct _xmlStreamComp xmlStreamComp;
  82. typedef xmlStreamComp *xmlStreamCompPtr;
  83. struct _xmlStreamComp {
  84. xmlDict *dict; /* the dictionary if any */
  85. int nbStep; /* number of steps in the automata */
  86. int maxStep; /* allocated number of steps */
  87. xmlStreamStepPtr steps; /* the array of steps */
  88. int flags;
  89. };
  90. struct _xmlStreamCtxt {
  91. struct _xmlStreamCtxt *next;/* link to next sub pattern if | */
  92. xmlStreamCompPtr comp; /* the compiled stream */
  93. int nbState; /* number of states in the automata */
  94. int maxState; /* allocated number of states */
  95. int level; /* how deep are we ? */
  96. int *states; /* the array of step indexes */
  97. int flags; /* validation options */
  98. int blockLevel;
  99. };
  100. static void xmlFreeStreamComp(xmlStreamCompPtr comp);
  101. /*
  102. * Types are private:
  103. */
  104. typedef enum {
  105. XML_OP_END=0,
  106. XML_OP_ROOT,
  107. XML_OP_ELEM,
  108. XML_OP_CHILD,
  109. XML_OP_ATTR,
  110. XML_OP_PARENT,
  111. XML_OP_ANCESTOR,
  112. XML_OP_NS,
  113. XML_OP_ALL
  114. } xmlPatOp;
  115. typedef struct _xmlStepState xmlStepState;
  116. typedef xmlStepState *xmlStepStatePtr;
  117. struct _xmlStepState {
  118. int step;
  119. xmlNodePtr node;
  120. };
  121. typedef struct _xmlStepStates xmlStepStates;
  122. typedef xmlStepStates *xmlStepStatesPtr;
  123. struct _xmlStepStates {
  124. int nbstates;
  125. int maxstates;
  126. xmlStepStatePtr states;
  127. };
  128. typedef struct _xmlStepOp xmlStepOp;
  129. typedef xmlStepOp *xmlStepOpPtr;
  130. struct _xmlStepOp {
  131. xmlPatOp op;
  132. const xmlChar *value;
  133. const xmlChar *value2; /* The namespace name */
  134. };
  135. #define PAT_FROM_ROOT (1<<8)
  136. #define PAT_FROM_CUR (1<<9)
  137. struct _xmlPattern {
  138. void *data; /* the associated template */
  139. xmlDictPtr dict; /* the optional dictionary */
  140. struct _xmlPattern *next; /* next pattern if | is used */
  141. const xmlChar *pattern; /* the pattern */
  142. int flags; /* flags */
  143. int nbStep;
  144. int maxStep;
  145. xmlStepOpPtr steps; /* ops for computation */
  146. xmlStreamCompPtr stream; /* the streaming data if any */
  147. };
  148. typedef struct _xmlPatParserContext xmlPatParserContext;
  149. typedef xmlPatParserContext *xmlPatParserContextPtr;
  150. struct _xmlPatParserContext {
  151. const xmlChar *cur; /* the current char being parsed */
  152. const xmlChar *base; /* the full expression */
  153. int error; /* error code */
  154. xmlDictPtr dict; /* the dictionary if any */
  155. xmlPatternPtr comp; /* the result */
  156. xmlNodePtr elem; /* the current node if any */
  157. const xmlChar **namespaces; /* the namespaces definitions */
  158. int nb_namespaces; /* the number of namespaces */
  159. };
  160. /************************************************************************
  161. * *
  162. * Type functions *
  163. * *
  164. ************************************************************************/
  165. /**
  166. * xmlNewPattern:
  167. *
  168. * Create a new XSLT Pattern
  169. *
  170. * Returns the newly allocated xmlPatternPtr or NULL in case of error
  171. */
  172. static xmlPatternPtr
  173. xmlNewPattern(void) {
  174. xmlPatternPtr cur;
  175. cur = (xmlPatternPtr) xmlMalloc(sizeof(xmlPattern));
  176. if (cur == NULL) {
  177. ERROR(NULL, NULL, NULL,
  178. "xmlNewPattern : malloc failed\n");
  179. return(NULL);
  180. }
  181. memset(cur, 0, sizeof(xmlPattern));
  182. cur->maxStep = 10;
  183. cur->steps = (xmlStepOpPtr) xmlMalloc(cur->maxStep * sizeof(xmlStepOp));
  184. if (cur->steps == NULL) {
  185. xmlFree(cur);
  186. ERROR(NULL, NULL, NULL,
  187. "xmlNewPattern : malloc failed\n");
  188. return(NULL);
  189. }
  190. return(cur);
  191. }
  192. /**
  193. * xmlFreePattern:
  194. * @comp: an XSLT comp
  195. *
  196. * Free up the memory allocated by @comp
  197. */
  198. void
  199. xmlFreePattern(xmlPatternPtr comp) {
  200. xmlFreePatternList(comp);
  201. }
  202. static void
  203. xmlFreePatternInternal(xmlPatternPtr comp) {
  204. xmlStepOpPtr op;
  205. int i;
  206. if (comp == NULL)
  207. return;
  208. if (comp->stream != NULL)
  209. xmlFreeStreamComp(comp->stream);
  210. if (comp->pattern != NULL)
  211. xmlFree((xmlChar *)comp->pattern);
  212. if (comp->steps != NULL) {
  213. if (comp->dict == NULL) {
  214. for (i = 0;i < comp->nbStep;i++) {
  215. op = &comp->steps[i];
  216. if (op->value != NULL)
  217. xmlFree((xmlChar *) op->value);
  218. if (op->value2 != NULL)
  219. xmlFree((xmlChar *) op->value2);
  220. }
  221. }
  222. xmlFree(comp->steps);
  223. }
  224. if (comp->dict != NULL)
  225. xmlDictFree(comp->dict);
  226. memset(comp, -1, sizeof(xmlPattern));
  227. xmlFree(comp);
  228. }
  229. /**
  230. * xmlFreePatternList:
  231. * @comp: an XSLT comp list
  232. *
  233. * Free up the memory allocated by all the elements of @comp
  234. */
  235. void
  236. xmlFreePatternList(xmlPatternPtr comp) {
  237. xmlPatternPtr cur;
  238. while (comp != NULL) {
  239. cur = comp;
  240. comp = comp->next;
  241. cur->next = NULL;
  242. xmlFreePatternInternal(cur);
  243. }
  244. }
  245. /**
  246. * xmlNewPatParserContext:
  247. * @pattern: the pattern context
  248. * @dict: the inherited dictionary or NULL
  249. * @namespaces: the prefix definitions, array of [URI, prefix] terminated
  250. * with [NULL, NULL] or NULL if no namespace is used
  251. *
  252. * Create a new XML pattern parser context
  253. *
  254. * Returns the newly allocated xmlPatParserContextPtr or NULL in case of error
  255. */
  256. static xmlPatParserContextPtr
  257. xmlNewPatParserContext(const xmlChar *pattern, xmlDictPtr dict,
  258. const xmlChar **namespaces) {
  259. xmlPatParserContextPtr cur;
  260. if (pattern == NULL)
  261. return(NULL);
  262. cur = (xmlPatParserContextPtr) xmlMalloc(sizeof(xmlPatParserContext));
  263. if (cur == NULL) {
  264. ERROR(NULL, NULL, NULL,
  265. "xmlNewPatParserContext : malloc failed\n");
  266. return(NULL);
  267. }
  268. memset(cur, 0, sizeof(xmlPatParserContext));
  269. cur->dict = dict;
  270. cur->cur = pattern;
  271. cur->base = pattern;
  272. if (namespaces != NULL) {
  273. int i;
  274. for (i = 0;namespaces[2 * i] != NULL;i++)
  275. ;
  276. cur->nb_namespaces = i;
  277. } else {
  278. cur->nb_namespaces = 0;
  279. }
  280. cur->namespaces = namespaces;
  281. return(cur);
  282. }
  283. /**
  284. * xmlFreePatParserContext:
  285. * @ctxt: an XSLT parser context
  286. *
  287. * Free up the memory allocated by @ctxt
  288. */
  289. static void
  290. xmlFreePatParserContext(xmlPatParserContextPtr ctxt) {
  291. if (ctxt == NULL)
  292. return;
  293. memset(ctxt, -1, sizeof(xmlPatParserContext));
  294. xmlFree(ctxt);
  295. }
  296. /**
  297. * xmlPatternAdd:
  298. * @comp: the compiled match expression
  299. * @op: an op
  300. * @value: the first value
  301. * @value2: the second value
  302. *
  303. * Add a step to an XSLT Compiled Match
  304. *
  305. * Returns -1 in case of failure, 0 otherwise.
  306. */
  307. static int
  308. xmlPatternAdd(xmlPatParserContextPtr ctxt ATTRIBUTE_UNUSED,
  309. xmlPatternPtr comp,
  310. xmlPatOp op, xmlChar * value, xmlChar * value2)
  311. {
  312. if (comp->nbStep >= comp->maxStep) {
  313. xmlStepOpPtr temp;
  314. temp = (xmlStepOpPtr) xmlRealloc(comp->steps, comp->maxStep * 2 *
  315. sizeof(xmlStepOp));
  316. if (temp == NULL) {
  317. ERROR(ctxt, NULL, NULL,
  318. "xmlPatternAdd: realloc failed\n");
  319. return (-1);
  320. }
  321. comp->steps = temp;
  322. comp->maxStep *= 2;
  323. }
  324. comp->steps[comp->nbStep].op = op;
  325. comp->steps[comp->nbStep].value = value;
  326. comp->steps[comp->nbStep].value2 = value2;
  327. comp->nbStep++;
  328. return (0);
  329. }
  330. #if 0
  331. /**
  332. * xsltSwapTopPattern:
  333. * @comp: the compiled match expression
  334. *
  335. * reverse the two top steps.
  336. */
  337. static void
  338. xsltSwapTopPattern(xmlPatternPtr comp) {
  339. int i;
  340. int j = comp->nbStep - 1;
  341. if (j > 0) {
  342. register const xmlChar *tmp;
  343. register xmlPatOp op;
  344. i = j - 1;
  345. tmp = comp->steps[i].value;
  346. comp->steps[i].value = comp->steps[j].value;
  347. comp->steps[j].value = tmp;
  348. tmp = comp->steps[i].value2;
  349. comp->steps[i].value2 = comp->steps[j].value2;
  350. comp->steps[j].value2 = tmp;
  351. op = comp->steps[i].op;
  352. comp->steps[i].op = comp->steps[j].op;
  353. comp->steps[j].op = op;
  354. }
  355. }
  356. #endif
  357. /**
  358. * xmlReversePattern:
  359. * @comp: the compiled match expression
  360. *
  361. * reverse all the stack of expressions
  362. *
  363. * returns 0 in case of success and -1 in case of error.
  364. */
  365. static int
  366. xmlReversePattern(xmlPatternPtr comp) {
  367. int i, j;
  368. /*
  369. * remove the leading // for //a or .//a
  370. */
  371. if ((comp->nbStep > 0) && (comp->steps[0].op == XML_OP_ANCESTOR)) {
  372. for (i = 0, j = 1;j < comp->nbStep;i++,j++) {
  373. comp->steps[i].value = comp->steps[j].value;
  374. comp->steps[i].value2 = comp->steps[j].value2;
  375. comp->steps[i].op = comp->steps[j].op;
  376. }
  377. comp->nbStep--;
  378. }
  379. if (comp->nbStep >= comp->maxStep) {
  380. xmlStepOpPtr temp;
  381. temp = (xmlStepOpPtr) xmlRealloc(comp->steps, comp->maxStep * 2 *
  382. sizeof(xmlStepOp));
  383. if (temp == NULL) {
  384. ERROR(ctxt, NULL, NULL,
  385. "xmlReversePattern: realloc failed\n");
  386. return (-1);
  387. }
  388. comp->steps = temp;
  389. comp->maxStep *= 2;
  390. }
  391. i = 0;
  392. j = comp->nbStep - 1;
  393. while (j > i) {
  394. register const xmlChar *tmp;
  395. register xmlPatOp op;
  396. tmp = comp->steps[i].value;
  397. comp->steps[i].value = comp->steps[j].value;
  398. comp->steps[j].value = tmp;
  399. tmp = comp->steps[i].value2;
  400. comp->steps[i].value2 = comp->steps[j].value2;
  401. comp->steps[j].value2 = tmp;
  402. op = comp->steps[i].op;
  403. comp->steps[i].op = comp->steps[j].op;
  404. comp->steps[j].op = op;
  405. j--;
  406. i++;
  407. }
  408. comp->steps[comp->nbStep].value = NULL;
  409. comp->steps[comp->nbStep].value2 = NULL;
  410. comp->steps[comp->nbStep++].op = XML_OP_END;
  411. return(0);
  412. }
  413. /************************************************************************
  414. * *
  415. * The interpreter for the precompiled patterns *
  416. * *
  417. ************************************************************************/
  418. static int
  419. xmlPatPushState(xmlStepStates *states, int step, xmlNodePtr node) {
  420. if ((states->states == NULL) || (states->maxstates <= 0)) {
  421. states->maxstates = 4;
  422. states->nbstates = 0;
  423. states->states = xmlMalloc(4 * sizeof(xmlStepState));
  424. }
  425. else if (states->maxstates <= states->nbstates) {
  426. xmlStepState *tmp;
  427. tmp = (xmlStepStatePtr) xmlRealloc(states->states,
  428. 2 * states->maxstates * sizeof(xmlStepState));
  429. if (tmp == NULL)
  430. return(-1);
  431. states->states = tmp;
  432. states->maxstates *= 2;
  433. }
  434. states->states[states->nbstates].step = step;
  435. states->states[states->nbstates++].node = node;
  436. #if 0
  437. fprintf(stderr, "Push: %d, %s\n", step, node->name);
  438. #endif
  439. return(0);
  440. }
  441. /**
  442. * xmlPatMatch:
  443. * @comp: the precompiled pattern
  444. * @node: a node
  445. *
  446. * Test whether the node matches the pattern
  447. *
  448. * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
  449. */
  450. static int
  451. xmlPatMatch(xmlPatternPtr comp, xmlNodePtr node) {
  452. int i;
  453. xmlStepOpPtr step;
  454. xmlStepStates states = {0, 0, NULL}; /* // may require backtrack */
  455. if ((comp == NULL) || (node == NULL)) return(-1);
  456. i = 0;
  457. restart:
  458. for (;i < comp->nbStep;i++) {
  459. step = &comp->steps[i];
  460. switch (step->op) {
  461. case XML_OP_END:
  462. goto found;
  463. case XML_OP_ROOT:
  464. if (node->type == XML_NAMESPACE_DECL)
  465. goto rollback;
  466. node = node->parent;
  467. if ((node->type == XML_DOCUMENT_NODE) ||
  468. #ifdef LIBXML_DOCB_ENABLED
  469. (node->type == XML_DOCB_DOCUMENT_NODE) ||
  470. #endif
  471. (node->type == XML_HTML_DOCUMENT_NODE))
  472. continue;
  473. goto rollback;
  474. case XML_OP_ELEM:
  475. if (node->type != XML_ELEMENT_NODE)
  476. goto rollback;
  477. if (step->value == NULL)
  478. continue;
  479. if (step->value[0] != node->name[0])
  480. goto rollback;
  481. if (!xmlStrEqual(step->value, node->name))
  482. goto rollback;
  483. /* Namespace test */
  484. if (node->ns == NULL) {
  485. if (step->value2 != NULL)
  486. goto rollback;
  487. } else if (node->ns->href != NULL) {
  488. if (step->value2 == NULL)
  489. goto rollback;
  490. if (!xmlStrEqual(step->value2, node->ns->href))
  491. goto rollback;
  492. }
  493. continue;
  494. case XML_OP_CHILD: {
  495. xmlNodePtr lst;
  496. if ((node->type != XML_ELEMENT_NODE) &&
  497. (node->type != XML_DOCUMENT_NODE) &&
  498. #ifdef LIBXML_DOCB_ENABLED
  499. (node->type != XML_DOCB_DOCUMENT_NODE) &&
  500. #endif
  501. (node->type != XML_HTML_DOCUMENT_NODE))
  502. goto rollback;
  503. lst = node->children;
  504. if (step->value != NULL) {
  505. while (lst != NULL) {
  506. if ((lst->type == XML_ELEMENT_NODE) &&
  507. (step->value[0] == lst->name[0]) &&
  508. (xmlStrEqual(step->value, lst->name)))
  509. break;
  510. lst = lst->next;
  511. }
  512. if (lst != NULL)
  513. continue;
  514. }
  515. goto rollback;
  516. }
  517. case XML_OP_ATTR:
  518. if (node->type != XML_ATTRIBUTE_NODE)
  519. goto rollback;
  520. if (step->value != NULL) {
  521. if (step->value[0] != node->name[0])
  522. goto rollback;
  523. if (!xmlStrEqual(step->value, node->name))
  524. goto rollback;
  525. }
  526. /* Namespace test */
  527. if (node->ns == NULL) {
  528. if (step->value2 != NULL)
  529. goto rollback;
  530. } else if (step->value2 != NULL) {
  531. if (!xmlStrEqual(step->value2, node->ns->href))
  532. goto rollback;
  533. }
  534. continue;
  535. case XML_OP_PARENT:
  536. if ((node->type == XML_DOCUMENT_NODE) ||
  537. (node->type == XML_HTML_DOCUMENT_NODE) ||
  538. #ifdef LIBXML_DOCB_ENABLED
  539. (node->type == XML_DOCB_DOCUMENT_NODE) ||
  540. #endif
  541. (node->type == XML_NAMESPACE_DECL))
  542. goto rollback;
  543. node = node->parent;
  544. if (node == NULL)
  545. goto rollback;
  546. if (step->value == NULL)
  547. continue;
  548. if (step->value[0] != node->name[0])
  549. goto rollback;
  550. if (!xmlStrEqual(step->value, node->name))
  551. goto rollback;
  552. /* Namespace test */
  553. if (node->ns == NULL) {
  554. if (step->value2 != NULL)
  555. goto rollback;
  556. } else if (node->ns->href != NULL) {
  557. if (step->value2 == NULL)
  558. goto rollback;
  559. if (!xmlStrEqual(step->value2, node->ns->href))
  560. goto rollback;
  561. }
  562. continue;
  563. case XML_OP_ANCESTOR:
  564. /* TODO: implement coalescing of ANCESTOR/NODE ops */
  565. if (step->value == NULL) {
  566. i++;
  567. step = &comp->steps[i];
  568. if (step->op == XML_OP_ROOT)
  569. goto found;
  570. if (step->op != XML_OP_ELEM)
  571. goto rollback;
  572. if (step->value == NULL)
  573. return(-1);
  574. }
  575. if (node == NULL)
  576. goto rollback;
  577. if ((node->type == XML_DOCUMENT_NODE) ||
  578. (node->type == XML_HTML_DOCUMENT_NODE) ||
  579. #ifdef LIBXML_DOCB_ENABLED
  580. (node->type == XML_DOCB_DOCUMENT_NODE) ||
  581. #endif
  582. (node->type == XML_NAMESPACE_DECL))
  583. goto rollback;
  584. node = node->parent;
  585. while (node != NULL) {
  586. if ((node->type == XML_ELEMENT_NODE) &&
  587. (step->value[0] == node->name[0]) &&
  588. (xmlStrEqual(step->value, node->name))) {
  589. /* Namespace test */
  590. if (node->ns == NULL) {
  591. if (step->value2 == NULL)
  592. break;
  593. } else if (node->ns->href != NULL) {
  594. if ((step->value2 != NULL) &&
  595. (xmlStrEqual(step->value2, node->ns->href)))
  596. break;
  597. }
  598. }
  599. node = node->parent;
  600. }
  601. if (node == NULL)
  602. goto rollback;
  603. /*
  604. * prepare a potential rollback from here
  605. * for ancestors of that node.
  606. */
  607. if (step->op == XML_OP_ANCESTOR)
  608. xmlPatPushState(&states, i, node);
  609. else
  610. xmlPatPushState(&states, i - 1, node);
  611. continue;
  612. case XML_OP_NS:
  613. if (node->type != XML_ELEMENT_NODE)
  614. goto rollback;
  615. if (node->ns == NULL) {
  616. if (step->value != NULL)
  617. goto rollback;
  618. } else if (node->ns->href != NULL) {
  619. if (step->value == NULL)
  620. goto rollback;
  621. if (!xmlStrEqual(step->value, node->ns->href))
  622. goto rollback;
  623. }
  624. break;
  625. case XML_OP_ALL:
  626. if (node->type != XML_ELEMENT_NODE)
  627. goto rollback;
  628. break;
  629. }
  630. }
  631. found:
  632. if (states.states != NULL) {
  633. /* Free the rollback states */
  634. xmlFree(states.states);
  635. }
  636. return(1);
  637. rollback:
  638. /* got an error try to rollback */
  639. if (states.states == NULL)
  640. return(0);
  641. if (states.nbstates <= 0) {
  642. xmlFree(states.states);
  643. return(0);
  644. }
  645. states.nbstates--;
  646. i = states.states[states.nbstates].step;
  647. node = states.states[states.nbstates].node;
  648. #if 0
  649. fprintf(stderr, "Pop: %d, %s\n", i, node->name);
  650. #endif
  651. goto restart;
  652. }
  653. /************************************************************************
  654. * *
  655. * Dedicated parser for templates *
  656. * *
  657. ************************************************************************/
  658. #define TODO \
  659. xmlGenericError(xmlGenericErrorContext, \
  660. "Unimplemented block at %s:%d\n", \
  661. __FILE__, __LINE__);
  662. #define CUR (*ctxt->cur)
  663. #define SKIP(val) ctxt->cur += (val)
  664. #define NXT(val) ctxt->cur[(val)]
  665. #define PEEKPREV(val) ctxt->cur[-(val)]
  666. #define CUR_PTR ctxt->cur
  667. #define SKIP_BLANKS \
  668. while (IS_BLANK_CH(CUR)) NEXT
  669. #define CURRENT (*ctxt->cur)
  670. #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
  671. #define PUSH(op, val, val2) \
  672. if (xmlPatternAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
  673. #define XSLT_ERROR(X) \
  674. { xsltError(ctxt, __FILE__, __LINE__, X); \
  675. ctxt->error = (X); return; }
  676. #define XSLT_ERROR0(X) \
  677. { xsltError(ctxt, __FILE__, __LINE__, X); \
  678. ctxt->error = (X); return(0); }
  679. #if 0
  680. /**
  681. * xmlPatScanLiteral:
  682. * @ctxt: the XPath Parser context
  683. *
  684. * Parse an XPath Literal:
  685. *
  686. * [29] Literal ::= '"' [^"]* '"'
  687. * | "'" [^']* "'"
  688. *
  689. * Returns the Literal parsed or NULL
  690. */
  691. static xmlChar *
  692. xmlPatScanLiteral(xmlPatParserContextPtr ctxt) {
  693. const xmlChar *q, *cur;
  694. xmlChar *ret = NULL;
  695. int val, len;
  696. SKIP_BLANKS;
  697. if (CUR == '"') {
  698. NEXT;
  699. cur = q = CUR_PTR;
  700. val = xmlStringCurrentChar(NULL, cur, &len);
  701. while ((IS_CHAR(val)) && (val != '"')) {
  702. cur += len;
  703. val = xmlStringCurrentChar(NULL, cur, &len);
  704. }
  705. if (!IS_CHAR(val)) {
  706. ctxt->error = 1;
  707. return(NULL);
  708. } else {
  709. if (ctxt->dict)
  710. ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q);
  711. else
  712. ret = xmlStrndup(q, cur - q);
  713. }
  714. cur += len;
  715. CUR_PTR = cur;
  716. } else if (CUR == '\'') {
  717. NEXT;
  718. cur = q = CUR_PTR;
  719. val = xmlStringCurrentChar(NULL, cur, &len);
  720. while ((IS_CHAR(val)) && (val != '\'')) {
  721. cur += len;
  722. val = xmlStringCurrentChar(NULL, cur, &len);
  723. }
  724. if (!IS_CHAR(val)) {
  725. ctxt->error = 1;
  726. return(NULL);
  727. } else {
  728. if (ctxt->dict)
  729. ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q);
  730. else
  731. ret = xmlStrndup(q, cur - q);
  732. }
  733. cur += len;
  734. CUR_PTR = cur;
  735. } else {
  736. /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
  737. ctxt->error = 1;
  738. return(NULL);
  739. }
  740. return(ret);
  741. }
  742. #endif
  743. /**
  744. * xmlPatScanName:
  745. * @ctxt: the XPath Parser context
  746. *
  747. * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
  748. * CombiningChar | Extender
  749. *
  750. * [5] Name ::= (Letter | '_' | ':') (NameChar)*
  751. *
  752. * [6] Names ::= Name (S Name)*
  753. *
  754. * Returns the Name parsed or NULL
  755. */
  756. static xmlChar *
  757. xmlPatScanName(xmlPatParserContextPtr ctxt) {
  758. const xmlChar *q, *cur;
  759. xmlChar *ret = NULL;
  760. int val, len;
  761. SKIP_BLANKS;
  762. cur = q = CUR_PTR;
  763. val = xmlStringCurrentChar(NULL, cur, &len);
  764. if (!IS_LETTER(val) && (val != '_') && (val != ':'))
  765. return(NULL);
  766. while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
  767. (val == '.') || (val == '-') ||
  768. (val == '_') ||
  769. (IS_COMBINING(val)) ||
  770. (IS_EXTENDER(val))) {
  771. cur += len;
  772. val = xmlStringCurrentChar(NULL, cur, &len);
  773. }
  774. if (ctxt->dict)
  775. ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q);
  776. else
  777. ret = xmlStrndup(q, cur - q);
  778. CUR_PTR = cur;
  779. return(ret);
  780. }
  781. /**
  782. * xmlPatScanNCName:
  783. * @ctxt: the XPath Parser context
  784. *
  785. * Parses a non qualified name
  786. *
  787. * Returns the Name parsed or NULL
  788. */
  789. static xmlChar *
  790. xmlPatScanNCName(xmlPatParserContextPtr ctxt) {
  791. const xmlChar *q, *cur;
  792. xmlChar *ret = NULL;
  793. int val, len;
  794. SKIP_BLANKS;
  795. cur = q = CUR_PTR;
  796. val = xmlStringCurrentChar(NULL, cur, &len);
  797. if (!IS_LETTER(val) && (val != '_'))
  798. return(NULL);
  799. while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
  800. (val == '.') || (val == '-') ||
  801. (val == '_') ||
  802. (IS_COMBINING(val)) ||
  803. (IS_EXTENDER(val))) {
  804. cur += len;
  805. val = xmlStringCurrentChar(NULL, cur, &len);
  806. }
  807. if (ctxt->dict)
  808. ret = (xmlChar *) xmlDictLookup(ctxt->dict, q, cur - q);
  809. else
  810. ret = xmlStrndup(q, cur - q);
  811. CUR_PTR = cur;
  812. return(ret);
  813. }
  814. #if 0
  815. /**
  816. * xmlPatScanQName:
  817. * @ctxt: the XPath Parser context
  818. * @prefix: the place to store the prefix
  819. *
  820. * Parse a qualified name
  821. *
  822. * Returns the Name parsed or NULL
  823. */
  824. static xmlChar *
  825. xmlPatScanQName(xmlPatParserContextPtr ctxt, xmlChar **prefix) {
  826. xmlChar *ret = NULL;
  827. *prefix = NULL;
  828. ret = xmlPatScanNCName(ctxt);
  829. if (CUR == ':') {
  830. *prefix = ret;
  831. NEXT;
  832. ret = xmlPatScanNCName(ctxt);
  833. }
  834. return(ret);
  835. }
  836. #endif
  837. /**
  838. * xmlCompileAttributeTest:
  839. * @ctxt: the compilation context
  840. *
  841. * Compile an attribute test.
  842. */
  843. static void
  844. xmlCompileAttributeTest(xmlPatParserContextPtr ctxt) {
  845. xmlChar *token = NULL;
  846. xmlChar *name = NULL;
  847. xmlChar *URL = NULL;
  848. SKIP_BLANKS;
  849. name = xmlPatScanNCName(ctxt);
  850. if (name == NULL) {
  851. if (CUR == '*') {
  852. PUSH(XML_OP_ATTR, NULL, NULL);
  853. NEXT;
  854. } else {
  855. ERROR(NULL, NULL, NULL,
  856. "xmlCompileAttributeTest : Name expected\n");
  857. ctxt->error = 1;
  858. }
  859. return;
  860. }
  861. if (CUR == ':') {
  862. int i;
  863. xmlChar *prefix = name;
  864. NEXT;
  865. if (IS_BLANK_CH(CUR)) {
  866. ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL);
  867. XML_PAT_FREE_STRING(ctxt, prefix);
  868. ctxt->error = 1;
  869. goto error;
  870. }
  871. /*
  872. * This is a namespace match
  873. */
  874. token = xmlPatScanName(ctxt);
  875. if ((prefix[0] == 'x') &&
  876. (prefix[1] == 'm') &&
  877. (prefix[2] == 'l') &&
  878. (prefix[3] == 0))
  879. {
  880. XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE);
  881. } else {
  882. for (i = 0;i < ctxt->nb_namespaces;i++) {
  883. if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) {
  884. XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i])
  885. break;
  886. }
  887. }
  888. if (i >= ctxt->nb_namespaces) {
  889. ERROR5(NULL, NULL, NULL,
  890. "xmlCompileAttributeTest : no namespace bound to prefix %s\n",
  891. prefix);
  892. XML_PAT_FREE_STRING(ctxt, prefix);
  893. ctxt->error = 1;
  894. goto error;
  895. }
  896. }
  897. XML_PAT_FREE_STRING(ctxt, prefix);
  898. if (token == NULL) {
  899. if (CUR == '*') {
  900. NEXT;
  901. PUSH(XML_OP_ATTR, NULL, URL);
  902. } else {
  903. ERROR(NULL, NULL, NULL,
  904. "xmlCompileAttributeTest : Name expected\n");
  905. ctxt->error = 1;
  906. goto error;
  907. }
  908. } else {
  909. PUSH(XML_OP_ATTR, token, URL);
  910. }
  911. } else {
  912. PUSH(XML_OP_ATTR, name, NULL);
  913. }
  914. return;
  915. error:
  916. if (URL != NULL)
  917. XML_PAT_FREE_STRING(ctxt, URL)
  918. if (token != NULL)
  919. XML_PAT_FREE_STRING(ctxt, token);
  920. }
  921. /**
  922. * xmlCompileStepPattern:
  923. * @ctxt: the compilation context
  924. *
  925. * Compile the Step Pattern and generates a precompiled
  926. * form suitable for fast matching.
  927. *
  928. * [3] Step ::= '.' | NameTest
  929. * [4] NameTest ::= QName | '*' | NCName ':' '*'
  930. */
  931. static void
  932. xmlCompileStepPattern(xmlPatParserContextPtr ctxt) {
  933. xmlChar *token = NULL;
  934. xmlChar *name = NULL;
  935. xmlChar *URL = NULL;
  936. int hasBlanks = 0;
  937. SKIP_BLANKS;
  938. if (CUR == '.') {
  939. /*
  940. * Context node.
  941. */
  942. NEXT;
  943. PUSH(XML_OP_ELEM, NULL, NULL);
  944. return;
  945. }
  946. if (CUR == '@') {
  947. /*
  948. * Attribute test.
  949. */
  950. if (XML_STREAM_XS_IDC_SEL(ctxt->comp)) {
  951. ERROR5(NULL, NULL, NULL,
  952. "Unexpected attribute axis in '%s'.\n", ctxt->base);
  953. ctxt->error = 1;
  954. return;
  955. }
  956. NEXT;
  957. xmlCompileAttributeTest(ctxt);
  958. if (ctxt->error != 0)
  959. goto error;
  960. return;
  961. }
  962. name = xmlPatScanNCName(ctxt);
  963. if (name == NULL) {
  964. if (CUR == '*') {
  965. NEXT;
  966. PUSH(XML_OP_ALL, NULL, NULL);
  967. return;
  968. } else {
  969. ERROR(NULL, NULL, NULL,
  970. "xmlCompileStepPattern : Name expected\n");
  971. ctxt->error = 1;
  972. return;
  973. }
  974. }
  975. if (IS_BLANK_CH(CUR)) {
  976. hasBlanks = 1;
  977. SKIP_BLANKS;
  978. }
  979. if (CUR == ':') {
  980. NEXT;
  981. if (CUR != ':') {
  982. xmlChar *prefix = name;
  983. int i;
  984. if (hasBlanks || IS_BLANK_CH(CUR)) {
  985. ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL);
  986. ctxt->error = 1;
  987. goto error;
  988. }
  989. /*
  990. * This is a namespace match
  991. */
  992. token = xmlPatScanName(ctxt);
  993. if ((prefix[0] == 'x') &&
  994. (prefix[1] == 'm') &&
  995. (prefix[2] == 'l') &&
  996. (prefix[3] == 0))
  997. {
  998. XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE)
  999. } else {
  1000. for (i = 0;i < ctxt->nb_namespaces;i++) {
  1001. if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) {
  1002. XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i])
  1003. break;
  1004. }
  1005. }
  1006. if (i >= ctxt->nb_namespaces) {
  1007. ERROR5(NULL, NULL, NULL,
  1008. "xmlCompileStepPattern : no namespace bound to prefix %s\n",
  1009. prefix);
  1010. ctxt->error = 1;
  1011. goto error;
  1012. }
  1013. }
  1014. XML_PAT_FREE_STRING(ctxt, prefix);
  1015. name = NULL;
  1016. if (token == NULL) {
  1017. if (CUR == '*') {
  1018. NEXT;
  1019. PUSH(XML_OP_NS, URL, NULL);
  1020. } else {
  1021. ERROR(NULL, NULL, NULL,
  1022. "xmlCompileStepPattern : Name expected\n");
  1023. ctxt->error = 1;
  1024. goto error;
  1025. }
  1026. } else {
  1027. PUSH(XML_OP_ELEM, token, URL);
  1028. }
  1029. } else {
  1030. NEXT;
  1031. if (xmlStrEqual(name, (const xmlChar *) "child")) {
  1032. XML_PAT_FREE_STRING(ctxt, name);
  1033. name = xmlPatScanName(ctxt);
  1034. if (name == NULL) {
  1035. if (CUR == '*') {
  1036. NEXT;
  1037. PUSH(XML_OP_ALL, NULL, NULL);
  1038. return;
  1039. } else {
  1040. ERROR(NULL, NULL, NULL,
  1041. "xmlCompileStepPattern : QName expected\n");
  1042. ctxt->error = 1;
  1043. goto error;
  1044. }
  1045. }
  1046. if (CUR == ':') {
  1047. xmlChar *prefix = name;
  1048. int i;
  1049. NEXT;
  1050. if (IS_BLANK_CH(CUR)) {
  1051. ERROR5(NULL, NULL, NULL, "Invalid QName.\n", NULL);
  1052. ctxt->error = 1;
  1053. goto error;
  1054. }
  1055. /*
  1056. * This is a namespace match
  1057. */
  1058. token = xmlPatScanName(ctxt);
  1059. if ((prefix[0] == 'x') &&
  1060. (prefix[1] == 'm') &&
  1061. (prefix[2] == 'l') &&
  1062. (prefix[3] == 0))
  1063. {
  1064. XML_PAT_COPY_NSNAME(ctxt, URL, XML_XML_NAMESPACE)
  1065. } else {
  1066. for (i = 0;i < ctxt->nb_namespaces;i++) {
  1067. if (xmlStrEqual(ctxt->namespaces[2 * i + 1], prefix)) {
  1068. XML_PAT_COPY_NSNAME(ctxt, URL, ctxt->namespaces[2 * i])
  1069. break;
  1070. }
  1071. }
  1072. if (i >= ctxt->nb_namespaces) {
  1073. ERROR5(NULL, NULL, NULL,
  1074. "xmlCompileStepPattern : no namespace bound "
  1075. "to prefix %s\n", prefix);
  1076. ctxt->error = 1;
  1077. goto error;
  1078. }
  1079. }
  1080. XML_PAT_FREE_STRING(ctxt, prefix);
  1081. name = NULL;
  1082. if (token == NULL) {
  1083. if (CUR == '*') {
  1084. NEXT;
  1085. PUSH(XML_OP_NS, URL, NULL);
  1086. } else {
  1087. ERROR(NULL, NULL, NULL,
  1088. "xmlCompileStepPattern : Name expected\n");
  1089. ctxt->error = 1;
  1090. goto error;
  1091. }
  1092. } else {
  1093. PUSH(XML_OP_CHILD, token, URL);
  1094. }
  1095. } else
  1096. PUSH(XML_OP_CHILD, name, NULL);
  1097. return;
  1098. } else if (xmlStrEqual(name, (const xmlChar *) "attribute")) {
  1099. XML_PAT_FREE_STRING(ctxt, name)
  1100. name = NULL;
  1101. if (XML_STREAM_XS_IDC_SEL(ctxt->comp)) {
  1102. ERROR5(NULL, NULL, NULL,
  1103. "Unexpected attribute axis in '%s'.\n", ctxt->base);
  1104. ctxt->error = 1;
  1105. goto error;
  1106. }
  1107. xmlCompileAttributeTest(ctxt);
  1108. if (ctxt->error != 0)
  1109. goto error;
  1110. return;
  1111. } else {
  1112. ERROR5(NULL, NULL, NULL,
  1113. "The 'element' or 'attribute' axis is expected.\n", NULL);
  1114. ctxt->error = 1;
  1115. goto error;
  1116. }
  1117. }
  1118. } else if (CUR == '*') {
  1119. if (name != NULL) {
  1120. ctxt->error = 1;
  1121. goto error;
  1122. }
  1123. NEXT;
  1124. PUSH(XML_OP_ALL, token, NULL);
  1125. } else {
  1126. PUSH(XML_OP_ELEM, name, NULL);
  1127. }
  1128. return;
  1129. error:
  1130. if (URL != NULL)
  1131. XML_PAT_FREE_STRING(ctxt, URL)
  1132. if (token != NULL)
  1133. XML_PAT_FREE_STRING(ctxt, token)
  1134. if (name != NULL)
  1135. XML_PAT_FREE_STRING(ctxt, name)
  1136. }
  1137. /**
  1138. * xmlCompilePathPattern:
  1139. * @ctxt: the compilation context
  1140. *
  1141. * Compile the Path Pattern and generates a precompiled
  1142. * form suitable for fast matching.
  1143. *
  1144. * [5] Path ::= ('.//')? ( Step '/' )* ( Step | '@' NameTest )
  1145. */
  1146. static void
  1147. xmlCompilePathPattern(xmlPatParserContextPtr ctxt) {
  1148. SKIP_BLANKS;
  1149. if (CUR == '/') {
  1150. ctxt->comp->flags |= PAT_FROM_ROOT;
  1151. } else if ((CUR == '.') || (ctxt->comp->flags & XML_PATTERN_NOTPATTERN)) {
  1152. ctxt->comp->flags |= PAT_FROM_CUR;
  1153. }
  1154. if ((CUR == '/') && (NXT(1) == '/')) {
  1155. PUSH(XML_OP_ANCESTOR, NULL, NULL);
  1156. NEXT;
  1157. NEXT;
  1158. } else if ((CUR == '.') && (NXT(1) == '/') && (NXT(2) == '/')) {
  1159. PUSH(XML_OP_ANCESTOR, NULL, NULL);
  1160. NEXT;
  1161. NEXT;
  1162. NEXT;
  1163. /* Check for incompleteness. */
  1164. SKIP_BLANKS;
  1165. if (CUR == 0) {
  1166. ERROR5(NULL, NULL, NULL,
  1167. "Incomplete expression '%s'.\n", ctxt->base);
  1168. ctxt->error = 1;
  1169. goto error;
  1170. }
  1171. }
  1172. if (CUR == '@') {
  1173. NEXT;
  1174. xmlCompileAttributeTest(ctxt);
  1175. SKIP_BLANKS;
  1176. /* TODO: check for incompleteness */
  1177. if (CUR != 0) {
  1178. xmlCompileStepPattern(ctxt);
  1179. if (ctxt->error != 0)
  1180. goto error;
  1181. }
  1182. } else {
  1183. if (CUR == '/') {
  1184. PUSH(XML_OP_ROOT, NULL, NULL);
  1185. NEXT;
  1186. /* Check for incompleteness. */
  1187. SKIP_BLANKS;
  1188. if (CUR == 0) {
  1189. ERROR5(NULL, NULL, NULL,
  1190. "Incomplete expression '%s'.\n", ctxt->base);
  1191. ctxt->error = 1;
  1192. goto error;
  1193. }
  1194. }
  1195. xmlCompileStepPattern(ctxt);
  1196. if (ctxt->error != 0)
  1197. goto error;
  1198. SKIP_BLANKS;
  1199. while (CUR == '/') {
  1200. if (NXT(1) == '/') {
  1201. PUSH(XML_OP_ANCESTOR, NULL, NULL);
  1202. NEXT;
  1203. NEXT;
  1204. SKIP_BLANKS;
  1205. xmlCompileStepPattern(ctxt);
  1206. if (ctxt->error != 0)
  1207. goto error;
  1208. } else {
  1209. PUSH(XML_OP_PARENT, NULL, NULL);
  1210. NEXT;
  1211. SKIP_BLANKS;
  1212. if (CUR == 0) {
  1213. ERROR5(NULL, NULL, NULL,
  1214. "Incomplete expression '%s'.\n", ctxt->base);
  1215. ctxt->error = 1;
  1216. goto error;
  1217. }
  1218. xmlCompileStepPattern(ctxt);
  1219. if (ctxt->error != 0)
  1220. goto error;
  1221. }
  1222. }
  1223. }
  1224. if (CUR != 0) {
  1225. ERROR5(NULL, NULL, NULL,
  1226. "Failed to compile pattern %s\n", ctxt->base);
  1227. ctxt->error = 1;
  1228. }
  1229. error:
  1230. return;
  1231. }
  1232. /**
  1233. * xmlCompileIDCXPathPath:
  1234. * @ctxt: the compilation context
  1235. *
  1236. * Compile the Path Pattern and generates a precompiled
  1237. * form suitable for fast matching.
  1238. *
  1239. * [5] Path ::= ('.//')? ( Step '/' )* ( Step | '@' NameTest )
  1240. */
  1241. static void
  1242. xmlCompileIDCXPathPath(xmlPatParserContextPtr ctxt) {
  1243. SKIP_BLANKS;
  1244. if (CUR == '/') {
  1245. ERROR5(NULL, NULL, NULL,
  1246. "Unexpected selection of the document root in '%s'.\n",
  1247. ctxt->base);
  1248. goto error;
  1249. }
  1250. ctxt->comp->flags |= PAT_FROM_CUR;
  1251. if (CUR == '.') {
  1252. /* "." - "self::node()" */
  1253. NEXT;
  1254. SKIP_BLANKS;
  1255. if (CUR == 0) {
  1256. /*
  1257. * Selection of the context node.
  1258. */
  1259. PUSH(XML_OP_ELEM, NULL, NULL);
  1260. return;
  1261. }
  1262. if (CUR != '/') {
  1263. /* TODO: A more meaningful error message. */
  1264. ERROR5(NULL, NULL, NULL,
  1265. "Unexpected token after '.' in '%s'.\n", ctxt->base);
  1266. goto error;
  1267. }
  1268. /* "./" - "self::node()/" */
  1269. NEXT;
  1270. SKIP_BLANKS;
  1271. if (CUR == '/') {
  1272. if (IS_BLANK_CH(PEEKPREV(1))) {
  1273. /*
  1274. * Disallow "./ /"
  1275. */
  1276. ERROR5(NULL, NULL, NULL,
  1277. "Unexpected '/' token in '%s'.\n", ctxt->base);
  1278. goto error;
  1279. }
  1280. /* ".//" - "self:node()/descendant-or-self::node()/" */
  1281. PUSH(XML_OP_ANCESTOR, NULL, NULL);
  1282. NEXT;
  1283. SKIP_BLANKS;
  1284. }
  1285. if (CUR == 0)
  1286. goto error_unfinished;
  1287. }
  1288. /*
  1289. * Process steps.
  1290. */
  1291. do {
  1292. xmlCompileStepPattern(ctxt);
  1293. if (ctxt->error != 0)
  1294. goto error;
  1295. SKIP_BLANKS;
  1296. if (CUR != '/')
  1297. break;
  1298. PUSH(XML_OP_PARENT, NULL, NULL);
  1299. NEXT;
  1300. SKIP_BLANKS;
  1301. if (CUR == '/') {
  1302. /*
  1303. * Disallow subsequent '//'.
  1304. */
  1305. ERROR5(NULL, NULL, NULL,
  1306. "Unexpected subsequent '//' in '%s'.\n",
  1307. ctxt->base);
  1308. goto error;
  1309. }
  1310. if (CUR == 0)
  1311. goto error_unfinished;
  1312. } while (CUR != 0);
  1313. if (CUR != 0) {
  1314. ERROR5(NULL, NULL, NULL,
  1315. "Failed to compile expression '%s'.\n", ctxt->base);
  1316. ctxt->error = 1;
  1317. }
  1318. return;
  1319. error:
  1320. ctxt->error = 1;
  1321. return;
  1322. error_unfinished:
  1323. ctxt->error = 1;
  1324. ERROR5(NULL, NULL, NULL,
  1325. "Unfinished expression '%s'.\n", ctxt->base);
  1326. return;
  1327. }
  1328. /************************************************************************
  1329. * *
  1330. * The streaming code *
  1331. * *
  1332. ************************************************************************/
  1333. #ifdef DEBUG_STREAMING
  1334. static void
  1335. xmlDebugStreamComp(xmlStreamCompPtr stream) {
  1336. int i;
  1337. if (stream == NULL) {
  1338. printf("Stream: NULL\n");
  1339. return;
  1340. }
  1341. printf("Stream: %d steps\n", stream->nbStep);
  1342. for (i = 0;i < stream->nbStep;i++) {
  1343. if (stream->steps[i].ns != NULL) {
  1344. printf("{%s}", stream->steps[i].ns);
  1345. }
  1346. if (stream->steps[i].name == NULL) {
  1347. printf("* ");
  1348. } else {
  1349. printf("%s ", stream->steps[i].name);
  1350. }
  1351. if (stream->steps[i].flags & XML_STREAM_STEP_ROOT)
  1352. printf("root ");
  1353. if (stream->steps[i].flags & XML_STREAM_STEP_DESC)
  1354. printf("// ");
  1355. if (stream->steps[i].flags & XML_STREAM_STEP_FINAL)
  1356. printf("final ");
  1357. printf("\n");
  1358. }
  1359. }
  1360. static void
  1361. xmlDebugStreamCtxt(xmlStreamCtxtPtr ctxt, int match) {
  1362. int i;
  1363. if (ctxt == NULL) {
  1364. printf("Stream: NULL\n");
  1365. return;
  1366. }
  1367. printf("Stream: level %d, %d states: ", ctxt->level, ctxt->nbState);
  1368. if (match)
  1369. printf("matches\n");
  1370. else
  1371. printf("\n");
  1372. for (i = 0;i < ctxt->nbState;i++) {
  1373. if (ctxt->states[2 * i] < 0)
  1374. printf(" %d: free\n", i);
  1375. else {
  1376. printf(" %d: step %d, level %d", i, ctxt->states[2 * i],
  1377. ctxt->states[(2 * i) + 1]);
  1378. if (ctxt->comp->steps[ctxt->states[2 * i]].flags &
  1379. XML_STREAM_STEP_DESC)
  1380. printf(" //\n");
  1381. else
  1382. printf("\n");
  1383. }
  1384. }
  1385. }
  1386. #endif
  1387. /**
  1388. * xmlNewStreamComp:
  1389. * @size: the number of expected steps
  1390. *
  1391. * build a new compiled pattern for streaming
  1392. *
  1393. * Returns the new structure or NULL in case of error.
  1394. */
  1395. static xmlStreamCompPtr
  1396. xmlNewStreamComp(int size) {
  1397. xmlStreamCompPtr cur;
  1398. if (size < 4)
  1399. size = 4;
  1400. cur = (xmlStreamCompPtr) xmlMalloc(sizeof(xmlStreamComp));
  1401. if (cur == NULL) {
  1402. ERROR(NULL, NULL, NULL,
  1403. "xmlNewStreamComp: malloc failed\n");
  1404. return(NULL);
  1405. }
  1406. memset(cur, 0, sizeof(xmlStreamComp));
  1407. cur->steps = (xmlStreamStepPtr) xmlMalloc(size * sizeof(xmlStreamStep));
  1408. if (cur->steps == NULL) {
  1409. xmlFree(cur);
  1410. ERROR(NULL, NULL, NULL,
  1411. "xmlNewStreamComp: malloc failed\n");
  1412. return(NULL);
  1413. }
  1414. cur->nbStep = 0;
  1415. cur->maxStep = size;
  1416. return(cur);
  1417. }
  1418. /**
  1419. * xmlFreeStreamComp:
  1420. * @comp: the compiled pattern for streaming
  1421. *
  1422. * Free the compiled pattern for streaming
  1423. */
  1424. static void
  1425. xmlFreeStreamComp(xmlStreamCompPtr comp) {
  1426. if (comp != NULL) {
  1427. if (comp->steps != NULL)
  1428. xmlFree(comp->steps);
  1429. if (comp->dict != NULL)
  1430. xmlDictFree(comp->dict);
  1431. xmlFree(comp);
  1432. }
  1433. }
  1434. /**
  1435. * xmlStreamCompAddStep:
  1436. * @comp: the compiled pattern for streaming
  1437. * @name: the first string, the name, or NULL for *
  1438. * @ns: the second step, the namespace name
  1439. * @flags: the flags for that step
  1440. *
  1441. * Add a new step to the compiled pattern
  1442. *
  1443. * Returns -1 in case of error or the step index if successful
  1444. */
  1445. static int
  1446. xmlStreamCompAddStep(xmlStreamCompPtr comp, const xmlChar *name,
  1447. const xmlChar *ns, int nodeType, int flags) {
  1448. xmlStreamStepPtr cur;
  1449. if (comp->nbStep >= comp->maxStep) {
  1450. cur = (xmlStreamStepPtr) xmlRealloc(comp->steps,
  1451. comp->maxStep * 2 * sizeof(xmlStreamStep));
  1452. if (cur == NULL) {
  1453. ERROR(NULL, NULL, NULL,
  1454. "xmlNewStreamComp: malloc failed\n");
  1455. return(-1);
  1456. }
  1457. comp->steps = cur;
  1458. comp->maxStep *= 2;
  1459. }
  1460. cur = &comp->steps[comp->nbStep++];
  1461. cur->flags = flags;
  1462. cur->name = name;
  1463. cur->ns = ns;
  1464. cur->nodeType = nodeType;
  1465. return(comp->nbStep - 1);
  1466. }
  1467. /**
  1468. * xmlStreamCompile:
  1469. * @comp: the precompiled pattern
  1470. *
  1471. * Tries to stream compile a pattern
  1472. *
  1473. * Returns -1 in case of failure and 0 in case of success.
  1474. */
  1475. static int
  1476. xmlStreamCompile(xmlPatternPtr comp) {
  1477. xmlStreamCompPtr stream;
  1478. int i, s = 0, root = 0, flags = 0, prevs = -1;
  1479. xmlStepOp step;
  1480. if ((comp == NULL) || (comp->steps == NULL))
  1481. return(-1);
  1482. /*
  1483. * special case for .
  1484. */
  1485. if ((comp->nbStep == 1) &&
  1486. (comp->steps[0].op == XML_OP_ELEM) &&
  1487. (comp->steps[0].value == NULL) &&
  1488. (comp->steps[0].value2 == NULL)) {
  1489. stream = xmlNewStreamComp(0);
  1490. if (stream == NULL)
  1491. return(-1);
  1492. /* Note that the stream will have no steps in this case. */
  1493. stream->flags |= XML_STREAM_FINAL_IS_ANY_NODE;
  1494. comp->stream = stream;
  1495. return(0);
  1496. }
  1497. stream = xmlNewStreamComp((comp->nbStep / 2) + 1);
  1498. if (stream == NULL)
  1499. return(-1);
  1500. if (comp->dict != NULL) {
  1501. stream->dict = comp->dict;
  1502. xmlDictReference(stream->dict);
  1503. }
  1504. i = 0;
  1505. if (comp->flags & PAT_FROM_ROOT)
  1506. stream->flags |= XML_STREAM_FROM_ROOT;
  1507. for (;i < comp->nbStep;i++) {
  1508. step = comp->steps[i];
  1509. switch (step.op) {
  1510. case XML_OP_END:
  1511. break;
  1512. case XML_OP_ROOT:
  1513. if (i != 0)
  1514. goto error;
  1515. root = 1;
  1516. break;
  1517. case XML_OP_NS:
  1518. s = xmlStreamCompAddStep(stream, NULL, step.value,
  1519. XML_ELEMENT_NODE, flags);
  1520. if (s < 0)
  1521. goto error;
  1522. prevs = s;
  1523. flags = 0;
  1524. break;
  1525. case XML_OP_ATTR:
  1526. flags |= XML_STREAM_STEP_ATTR;
  1527. prevs = -1;
  1528. s = xmlStreamCompAddStep(stream,
  1529. step.value, step.value2, XML_ATTRIBUTE_NODE, flags);
  1530. flags = 0;
  1531. if (s < 0)
  1532. goto error;
  1533. break;
  1534. case XML_OP_ELEM:
  1535. if ((step.value == NULL) && (step.value2 == NULL)) {
  1536. /*
  1537. * We have a "." or "self::node()" here.
  1538. * Eliminate redundant self::node() tests like in "/./."
  1539. * or "//./"
  1540. * The only case we won't eliminate is "//.", i.e. if
  1541. * self::node() is the last node test and we had
  1542. * continuation somewhere beforehand.
  1543. */
  1544. if ((comp->nbStep == i + 1) &&
  1545. (flags & XML_STREAM_STEP_DESC)) {
  1546. /*
  1547. * Mark the special case where the expression resolves
  1548. * to any type of node.
  1549. */
  1550. if (comp->nbStep == i + 1) {
  1551. stream->flags |= XML_STREAM_FINAL_IS_ANY_NODE;
  1552. }
  1553. flags |= XML_STREAM_STEP_NODE;
  1554. s = xmlStreamCompAddStep(stream, NULL, NULL,
  1555. XML_STREAM_ANY_NODE, flags);
  1556. if (s < 0)
  1557. goto error;
  1558. flags = 0;
  1559. /*
  1560. * If there was a previous step, mark it to be added to
  1561. * the result node-set; this is needed since only
  1562. * the last step will be marked as "final" and only
  1563. * "final" nodes are added to the resulting set.
  1564. */
  1565. if (prevs != -1) {
  1566. stream->steps[prevs].flags |= XML_STREAM_STEP_IN_SET;
  1567. prevs = -1;
  1568. }
  1569. break;
  1570. } else {
  1571. /* Just skip this one. */
  1572. continue;
  1573. }
  1574. }
  1575. /* An element node. */
  1576. s = xmlStreamCompAddStep(stream, step.value, step.value2,
  1577. XML_ELEMENT_NODE, flags);
  1578. if (s < 0)
  1579. goto error;
  1580. prevs = s;
  1581. flags = 0;
  1582. break;
  1583. case XML_OP_CHILD:
  1584. /* An element node child. */
  1585. s = xmlStreamCompAddStep(stream, step.value, step.value2,
  1586. XML_ELEMENT_NODE, flags);
  1587. if (s < 0)
  1588. goto error;
  1589. prevs = s;
  1590. flags = 0;
  1591. break;
  1592. case XML_OP_ALL:
  1593. s = xmlStreamCompAddStep(stream, NULL, NULL,
  1594. XML_ELEMENT_NODE, flags);
  1595. if (s < 0)
  1596. goto error;
  1597. prevs = s;
  1598. flags = 0;
  1599. break;
  1600. case XML_OP_PARENT:
  1601. break;
  1602. case XML_OP_ANCESTOR:
  1603. /* Skip redundant continuations. */
  1604. if (flags & XML_STREAM_STEP_DESC)
  1605. break;
  1606. flags |= XML_STREAM_STEP_DESC;
  1607. /*
  1608. * Mark the expression as having "//".
  1609. */
  1610. if ((stream->flags & XML_STREAM_DESC) == 0)
  1611. stream->flags |= XML_STREAM_DESC;
  1612. break;
  1613. }
  1614. }
  1615. if ((! root) && (comp->flags & XML_PATTERN_NOTPATTERN) == 0) {
  1616. /*
  1617. * If this should behave like a real pattern, we will mark
  1618. * the first step as having "//", to be reentrant on every
  1619. * tree level.
  1620. */
  1621. if ((stream->flags & XML_STREAM_DESC) == 0)
  1622. stream->flags |= XML_STREAM_DESC;
  1623. if (stream->nbStep > 0) {
  1624. if ((stream->steps[0].flags & XML_STREAM_STEP_DESC) == 0)
  1625. stream->steps[0].flags |= XML_STREAM_STEP_DESC;
  1626. }
  1627. }
  1628. if (stream->nbStep <= s)
  1629. goto error;
  1630. stream->steps[s].flags |= XML_STREAM_STEP_FINAL;
  1631. if (root)
  1632. stream->steps[0].flags |= XML_STREAM_STEP_ROOT;
  1633. #ifdef DEBUG_STREAMING
  1634. xmlDebugStreamComp(stream);
  1635. #endif
  1636. comp->stream = stream;
  1637. return(0);
  1638. error:
  1639. xmlFreeStreamComp(stream);
  1640. return(0);
  1641. }
  1642. /**
  1643. * xmlNewStreamCtxt:
  1644. * @size: the number of expected states
  1645. *
  1646. * build a new stream context
  1647. *
  1648. * Returns the new structure or NULL in case of error.
  1649. */
  1650. static xmlStreamCtxtPtr
  1651. xmlNewStreamCtxt(xmlStreamCompPtr stream) {
  1652. xmlStreamCtxtPtr cur;
  1653. cur = (xmlStreamCtxtPtr) xmlMalloc(sizeof(xmlStreamCtxt));
  1654. if (cur == NULL) {
  1655. ERROR(NULL, NULL, NULL,
  1656. "xmlNewStreamCtxt: malloc failed\n");
  1657. return(NULL);
  1658. }
  1659. memset(cur, 0, sizeof(xmlStreamCtxt));
  1660. cur->states = (int *) xmlMalloc(4 * 2 * sizeof(int));
  1661. if (cur->states == NULL) {
  1662. xmlFree(cur);
  1663. ERROR(NULL, NULL, NULL,
  1664. "xmlNewStreamCtxt: malloc failed\n");
  1665. return(NULL);
  1666. }
  1667. cur->nbState = 0;
  1668. cur->maxState = 4;
  1669. cur->level = 0;
  1670. cur->comp = stream;
  1671. cur->blockLevel = -1;
  1672. return(cur);
  1673. }
  1674. /**
  1675. * xmlFreeStreamCtxt:
  1676. * @stream: the stream context
  1677. *
  1678. * Free the stream context
  1679. */
  1680. void
  1681. xmlFreeStreamCtxt(xmlStreamCtxtPtr stream) {
  1682. xmlStreamCtxtPtr next;
  1683. while (stream != NULL) {
  1684. next = stream->next;
  1685. if (stream->states != NULL)
  1686. xmlFree(stream->states);
  1687. xmlFree(stream);
  1688. stream = next;
  1689. }
  1690. }
  1691. /**
  1692. * xmlStreamCtxtAddState:
  1693. * @comp: the stream context
  1694. * @idx: the step index for that streaming state
  1695. *
  1696. * Add a new state to the stream context
  1697. *
  1698. * Returns -1 in case of error or the state index if successful
  1699. */
  1700. static int
  1701. xmlStreamCtxtAddState(xmlStreamCtxtPtr comp, int idx, int level) {
  1702. int i;
  1703. for (i = 0;i < comp->nbState;i++) {
  1704. if (comp->states[2 * i] < 0) {
  1705. comp->states[2 * i] = idx;
  1706. comp->states[2 * i + 1] = level;
  1707. return(i);
  1708. }
  1709. }
  1710. if (comp->nbState >= comp->maxState) {
  1711. int *cur;
  1712. cur = (int *) xmlRealloc(comp->states,
  1713. comp->maxState * 4 * sizeof(int));
  1714. if (cur == NULL) {
  1715. ERROR(NULL, NULL, NULL,
  1716. "xmlNewStreamCtxt: malloc failed\n");
  1717. return(-1);
  1718. }
  1719. comp->states = cur;
  1720. comp->maxState *= 2;
  1721. }
  1722. comp->states[2 * comp->nbState] = idx;
  1723. comp->states[2 * comp->nbState++ + 1] = level;
  1724. return(comp->nbState - 1);
  1725. }
  1726. /**
  1727. * xmlStreamPushInternal:
  1728. * @stream: the stream context
  1729. * @name: the current name
  1730. * @ns: the namespace name
  1731. * @nodeType: the type of the node
  1732. *
  1733. * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
  1734. * indicated a dictionary, then strings for name and ns will be expected
  1735. * to come from the dictionary.
  1736. * Both @name and @ns being NULL means the / i.e. the root of the document.
  1737. * This can also act as a reset.
  1738. *
  1739. * Returns: -1 in case of error, 1 if the current state in the stream is a
  1740. * match and 0 otherwise.
  1741. */
  1742. static int
  1743. xmlStreamPushInternal(xmlStreamCtxtPtr stream,
  1744. const xmlChar *name, const xmlChar *ns,
  1745. int nodeType) {
  1746. int ret = 0, err = 0, final = 0, tmp, i, m, match, stepNr, desc;
  1747. xmlStreamCompPtr comp;
  1748. xmlStreamStep step;
  1749. #ifdef DEBUG_STREAMING
  1750. xmlStreamCtxtPtr orig = stream;
  1751. #endif
  1752. if ((stream == NULL) || (stream->nbState < 0))
  1753. return(-1);
  1754. while (stream != NULL) {
  1755. comp = stream->comp;
  1756. if ((nodeType == XML_ELEMENT_NODE) &&
  1757. (name == NULL) && (ns == NULL)) {
  1758. /* We have a document node here (or a reset). */
  1759. stream->nbState = 0;
  1760. stream->level = 0;
  1761. stream->blockLevel = -1;
  1762. if (comp->flags & XML_STREAM_FROM_ROOT) {
  1763. if (comp->nbStep == 0) {
  1764. /* TODO: We have a "/." here? */
  1765. ret = 1;
  1766. } else {
  1767. if ((comp->nbStep == 1) &&
  1768. (comp->steps[0].nodeType == XML_STREAM_ANY_NODE) &&
  1769. (comp->steps[0].flags & XML_STREAM_STEP_DESC))
  1770. {
  1771. /*
  1772. * In the case of "//." the document node will match
  1773. * as well.
  1774. */
  1775. ret = 1;
  1776. } else if (comp->steps[0].flags & XML_STREAM_STEP_ROOT) {
  1777. /* TODO: Do we need this ? */
  1778. tmp = xmlStreamCtxtAddState(stream, 0, 0);
  1779. if (tmp < 0)
  1780. err++;
  1781. }
  1782. }
  1783. }
  1784. stream = stream->next;
  1785. continue; /* while */
  1786. }
  1787. /*
  1788. * Fast check for ".".
  1789. */
  1790. if (comp->nbStep == 0) {
  1791. /*
  1792. * / and . are handled at the XPath node set creation
  1793. * level by checking min depth
  1794. */
  1795. if (stream->flags & XML_PATTERN_XPATH) {
  1796. stream = stream->next;
  1797. continue; /* while */
  1798. }
  1799. /*
  1800. * For non-pattern like evaluation like XML Schema IDCs
  1801. * or traditional XPath expressions, this will match if
  1802. * we are at the first level only, otherwise on every level.
  1803. */
  1804. if ((nodeType != XML_ATTRIBUTE_NODE) &&
  1805. (((stream->flags & XML_PATTERN_NOTPATTERN) == 0) ||
  1806. (stream->level == 0))) {
  1807. ret = 1;
  1808. }
  1809. stream->level++;
  1810. goto stream_next;
  1811. }
  1812. if (stream->blockLevel != -1) {
  1813. /*
  1814. * Skip blocked expressions.
  1815. */
  1816. stream->level++;
  1817. goto stream_next;
  1818. }
  1819. if ((nodeType != XML_ELEMENT_NODE) &&
  1820. (nodeType != XML_ATTRIBUTE_NODE) &&
  1821. ((comp->flags & XML_STREAM_FINAL_IS_ANY_NODE) == 0)) {
  1822. /*
  1823. * No need to process nodes of other types if we don't
  1824. * resolve to those types.
  1825. * TODO: Do we need to block the context here?
  1826. */
  1827. stream->level++;
  1828. goto stream_next;
  1829. }
  1830. /*
  1831. * Check evolution of existing states
  1832. */
  1833. i = 0;
  1834. m = stream->nbState;
  1835. while (i < m) {
  1836. if ((comp->flags & XML_STREAM_DESC) == 0) {
  1837. /*
  1838. * If there is no "//", then only the last
  1839. * added state is of interest.
  1840. */
  1841. stepNr = stream->states[2 * (stream->nbState -1)];
  1842. /*
  1843. * TODO: Security check, should not happen, remove it.
  1844. */
  1845. if (stream->states[(2 * (stream->nbState -1)) + 1] <
  1846. stream->level) {
  1847. return (-1);
  1848. }
  1849. desc = 0;
  1850. /* loop-stopper */
  1851. i = m;
  1852. } else {
  1853. /*
  1854. * If there are "//", then we need to process every "//"
  1855. * occurring in the states, plus any other state for this
  1856. * level.
  1857. */
  1858. stepNr = stream->states[2 * i];
  1859. /* TODO: should not happen anymore: dead states */
  1860. if (stepNr < 0)
  1861. goto next_state;
  1862. tmp = stream->states[(2 * i) + 1];
  1863. /* skip new states just added */
  1864. if (tmp > stream->level)
  1865. goto next_state;
  1866. /* skip states at ancestor levels, except if "//" */
  1867. desc = comp->steps[stepNr].flags & XML_STREAM_STEP_DESC;
  1868. if ((tmp < stream->level) && (!desc))
  1869. goto next_state;
  1870. }
  1871. /*
  1872. * Check for correct node-type.
  1873. */
  1874. step = comp->steps[stepNr];
  1875. if (step.nodeType != nodeType) {
  1876. if (step.nodeType == XML_ATTRIBUTE_NODE) {
  1877. /*
  1878. * Block this expression for deeper evaluation.
  1879. */
  1880. if ((comp->flags & XML_STREAM_DESC) == 0)
  1881. stream->blockLevel = stream->level +1;
  1882. goto next_state;
  1883. } else if (step.nodeType != XML_STREAM_ANY_NODE)
  1884. goto next_state;
  1885. }
  1886. /*
  1887. * Compare local/namespace-name.
  1888. */
  1889. match = 0;
  1890. if (step.nodeType == XML_STREAM_ANY_NODE) {
  1891. match = 1;
  1892. } else if (step.name == NULL) {
  1893. if (step.ns == NULL) {
  1894. /*
  1895. * This lets through all elements/attributes.
  1896. */
  1897. match = 1;
  1898. } else if (ns != NULL)
  1899. match = xmlStrEqual(step.ns, ns);
  1900. } else if (((step.ns != NULL) == (ns != NULL)) &&
  1901. (name != NULL) &&
  1902. (step.name[0] == name[0]) &&
  1903. xmlStrEqual(step.name, name) &&
  1904. ((step.ns == ns) || xmlStrEqual(step.ns, ns)))
  1905. {
  1906. match = 1;
  1907. }
  1908. #if 0
  1909. /*
  1910. * TODO: Pointer comparison won't work, since not guaranteed that the given
  1911. * values are in the same dict; especially if it's the namespace name,
  1912. * normally coming from ns->href. We need a namespace dict mechanism !
  1913. */
  1914. } else if (comp->dict) {
  1915. if (step.name == NULL) {
  1916. if (step.ns == NULL)
  1917. match = 1;
  1918. else
  1919. match = (step.ns == ns);
  1920. } else {
  1921. match = ((step.name == name) && (step.ns == ns));
  1922. }
  1923. #endif /* if 0 ------------------------------------------------------- */
  1924. if (match) {
  1925. final = step.flags & XML_STREAM_STEP_FINAL;
  1926. if (desc) {
  1927. if (final) {
  1928. ret = 1;
  1929. } else {
  1930. /* descending match create a new state */
  1931. xmlStreamCtxtAddState(stream, stepNr + 1,
  1932. stream->level + 1);
  1933. }
  1934. } else {
  1935. if (final) {
  1936. ret = 1;
  1937. } else {
  1938. xmlStreamCtxtAddState(stream, stepNr + 1,
  1939. stream->level + 1);
  1940. }
  1941. }
  1942. if ((ret != 1) && (step.flags & XML_STREAM_STEP_IN_SET)) {
  1943. /*
  1944. * Check if we have a special case like "foo/bar//.", where
  1945. * "foo" is selected as well.
  1946. */
  1947. ret = 1;
  1948. }
  1949. }
  1950. if (((comp->flags & XML_STREAM_DESC) == 0) &&
  1951. ((! match) || final)) {
  1952. /*
  1953. * Mark this expression as blocked for any evaluation at
  1954. * deeper levels. Note that this includes "/foo"
  1955. * expressions if the *pattern* behaviour is used.
  1956. */
  1957. stream->blockLevel = stream->level +1;
  1958. }
  1959. next_state:
  1960. i++;
  1961. }
  1962. stream->level++;
  1963. /*
  1964. * Re/enter the expression.
  1965. * Don't reenter if it's an absolute expression like "/foo",
  1966. * except "//foo".
  1967. */
  1968. step = comp->steps[0];
  1969. if (step.flags & XML_STREAM_STEP_ROOT)
  1970. goto stream_next;
  1971. desc = step.flags & XML_STREAM_STEP_DESC;
  1972. if (stream->flags & XML_PATTERN_NOTPATTERN) {
  1973. /*
  1974. * Re/enter the expression if it is a "descendant" one,
  1975. * or if we are at the 1st level of evaluation.
  1976. */
  1977. if (stream->level == 1) {
  1978. if (XML_STREAM_XS_IDC(stream)) {
  1979. /*
  1980. * XS-IDC: The missing "self::node()" will always
  1981. * match the first given node.
  1982. */
  1983. goto stream_next;
  1984. } else
  1985. goto compare;
  1986. }
  1987. /*
  1988. * A "//" is always reentrant.
  1989. */
  1990. if (desc)
  1991. goto compare;
  1992. /*
  1993. * XS-IDC: Process the 2nd level, since the missing
  1994. * "self::node()" is responsible for the 2nd level being
  1995. * the real start level.
  1996. */
  1997. if ((stream->level == 2) && XML_STREAM_XS_IDC(stream))
  1998. goto compare;
  1999. goto stream_next;
  2000. }
  2001. compare:
  2002. /*
  2003. * Check expected node-type.
  2004. */
  2005. if (step.nodeType != nodeType) {
  2006. if (nodeType == XML_ATTRIBUTE_NODE)
  2007. goto stream_next;
  2008. else if (step.nodeType != XML_STREAM_ANY_NODE)
  2009. goto stream_next;
  2010. }
  2011. /*
  2012. * Compare local/namespace-name.
  2013. */
  2014. match = 0;
  2015. if (step.nodeType == XML_STREAM_ANY_NODE) {
  2016. match = 1;
  2017. } else if (step.name == NULL) {
  2018. if (step.ns == NULL) {
  2019. /*
  2020. * This lets through all elements/attributes.
  2021. */
  2022. match = 1;
  2023. } else if (ns != NULL)
  2024. match = xmlStrEqual(step.ns, ns);
  2025. } else if (((step.ns != NULL) == (ns != NULL)) &&
  2026. (name != NULL) &&
  2027. (step.name[0] == name[0]) &&
  2028. xmlStrEqual(step.name, name) &&
  2029. ((step.ns == ns) || xmlStrEqual(step.ns, ns)))
  2030. {
  2031. match = 1;
  2032. }
  2033. final = step.flags & XML_STREAM_STEP_FINAL;
  2034. if (match) {
  2035. if (final)
  2036. ret = 1;
  2037. else
  2038. xmlStreamCtxtAddState(stream, 1, stream->level);
  2039. if ((ret != 1) && (step.flags & XML_STREAM_STEP_IN_SET)) {
  2040. /*
  2041. * Check if we have a special case like "foo//.", where
  2042. * "foo" is selected as well.
  2043. */
  2044. ret = 1;
  2045. }
  2046. }
  2047. if (((comp->flags & XML_STREAM_DESC) == 0) &&
  2048. ((! match) || final)) {
  2049. /*
  2050. * Mark this expression as blocked for any evaluation at
  2051. * deeper levels.
  2052. */
  2053. stream->blockLevel = stream->level;
  2054. }
  2055. stream_next:
  2056. stream = stream->next;
  2057. } /* while stream != NULL */
  2058. if (err > 0)
  2059. ret = -1;
  2060. #ifdef DEBUG_STREAMING
  2061. xmlDebugStreamCtxt(orig, ret);
  2062. #endif
  2063. return(ret);
  2064. }
  2065. /**
  2066. * xmlStreamPush:
  2067. * @stream: the stream context
  2068. * @name: the current name
  2069. * @ns: the namespace name
  2070. *
  2071. * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
  2072. * indicated a dictionary, then strings for name and ns will be expected
  2073. * to come from the dictionary.
  2074. * Both @name and @ns being NULL means the / i.e. the root of the document.
  2075. * This can also act as a reset.
  2076. * Otherwise the function will act as if it has been given an element-node.
  2077. *
  2078. * Returns: -1 in case of error, 1 if the current state in the stream is a
  2079. * match and 0 otherwise.
  2080. */
  2081. int
  2082. xmlStreamPush(xmlStreamCtxtPtr stream,
  2083. const xmlChar *name, const xmlChar *ns) {
  2084. return (xmlStreamPushInternal(stream, name, ns, (int) XML_ELEMENT_NODE));
  2085. }
  2086. /**
  2087. * xmlStreamPushNode:
  2088. * @stream: the stream context
  2089. * @name: the current name
  2090. * @ns: the namespace name
  2091. * @nodeType: the type of the node being pushed
  2092. *
  2093. * Push new data onto the stream. NOTE: if the call xmlPatterncompile()
  2094. * indicated a dictionary, then strings for name and ns will be expected
  2095. * to come from the dictionary.
  2096. * Both @name and @ns being NULL means the / i.e. the root of the document.
  2097. * This can also act as a reset.
  2098. * Different from xmlStreamPush() this function can be fed with nodes of type:
  2099. * element-, attribute-, text-, cdata-section-, comment- and
  2100. * processing-instruction-node.
  2101. *
  2102. * Returns: -1 in case of error, 1 if the current state in the stream is a
  2103. * match and 0 otherwise.
  2104. */
  2105. int
  2106. xmlStreamPushNode(xmlStreamCtxtPtr stream,
  2107. const xmlChar *name, const xmlChar *ns,
  2108. int nodeType)
  2109. {
  2110. return (xmlStreamPushInternal(stream, name, ns,
  2111. nodeType));
  2112. }
  2113. /**
  2114. * xmlStreamPushAttr:
  2115. * @stream: the stream context
  2116. * @name: the current name
  2117. * @ns: the namespace name
  2118. *
  2119. * Push new attribute data onto the stream. NOTE: if the call xmlPatterncompile()
  2120. * indicated a dictionary, then strings for name and ns will be expected
  2121. * to come from the dictionary.
  2122. * Both @name and @ns being NULL means the / i.e. the root of the document.
  2123. * This can also act as a reset.
  2124. * Otherwise the function will act as if it has been given an attribute-node.
  2125. *
  2126. * Returns: -1 in case of error, 1 if the current state in the stream is a
  2127. * match and 0 otherwise.
  2128. */
  2129. int
  2130. xmlStreamPushAttr(xmlStreamCtxtPtr stream,
  2131. const xmlChar *name, const xmlChar *ns) {
  2132. return (xmlStreamPushInternal(stream, name, ns, (int) XML_ATTRIBUTE_NODE));
  2133. }
  2134. /**
  2135. * xmlStreamPop:
  2136. * @stream: the stream context
  2137. *
  2138. * push one level from the stream.
  2139. *
  2140. * Returns: -1 in case of error, 0 otherwise.
  2141. */
  2142. int
  2143. xmlStreamPop(xmlStreamCtxtPtr stream) {
  2144. int i, lev;
  2145. if (stream == NULL)
  2146. return(-1);
  2147. while (stream != NULL) {
  2148. /*
  2149. * Reset block-level.
  2150. */
  2151. if (stream->blockLevel == stream->level)
  2152. stream->blockLevel = -1;
  2153. /*
  2154. * stream->level can be zero when XML_FINAL_IS_ANY_NODE is set
  2155. * (see the thread at
  2156. * http://mail.gnome.org/archives/xslt/2008-July/msg00027.html)
  2157. */
  2158. if (stream->level)
  2159. stream->level--;
  2160. /*
  2161. * Check evolution of existing states
  2162. */
  2163. for (i = stream->nbState -1; i >= 0; i--) {
  2164. /* discard obsoleted states */
  2165. lev = stream->states[(2 * i) + 1];
  2166. if (lev > stream->level)
  2167. stream->nbState--;
  2168. if (lev <= stream->level)
  2169. break;
  2170. }
  2171. stream = stream->next;
  2172. }
  2173. return(0);
  2174. }
  2175. /**
  2176. * xmlStreamWantsAnyNode:
  2177. * @streamCtxt: the stream context
  2178. *
  2179. * Query if the streaming pattern additionally needs to be fed with
  2180. * text-, cdata-section-, comment- and processing-instruction-nodes.
  2181. * If the result is 0 then only element-nodes and attribute-nodes
  2182. * need to be pushed.
  2183. *
  2184. * Returns: 1 in case of need of nodes of the above described types,
  2185. * 0 otherwise. -1 on API errors.
  2186. */
  2187. int
  2188. xmlStreamWantsAnyNode(xmlStreamCtxtPtr streamCtxt)
  2189. {
  2190. if (streamCtxt == NULL)
  2191. return(-1);
  2192. while (streamCtxt != NULL) {
  2193. if (streamCtxt->comp->flags & XML_STREAM_FINAL_IS_ANY_NODE)
  2194. return(1);
  2195. streamCtxt = streamCtxt->next;
  2196. }
  2197. return(0);
  2198. }
  2199. /************************************************************************
  2200. * *
  2201. * The public interfaces *
  2202. * *
  2203. ************************************************************************/
  2204. /**
  2205. * xmlPatterncompile:
  2206. * @pattern: the pattern to compile
  2207. * @dict: an optional dictionary for interned strings
  2208. * @flags: compilation flags, see xmlPatternFlags
  2209. * @namespaces: the prefix definitions, array of [URI, prefix] or NULL
  2210. *
  2211. * Compile a pattern.
  2212. *
  2213. * Returns the compiled form of the pattern or NULL in case of error
  2214. */
  2215. xmlPatternPtr
  2216. xmlPatterncompile(const xmlChar *pattern, xmlDict *dict, int flags,
  2217. const xmlChar **namespaces) {
  2218. xmlPatternPtr ret = NULL, cur;
  2219. xmlPatParserContextPtr ctxt = NULL;
  2220. const xmlChar *or, *start;
  2221. xmlChar *tmp = NULL;
  2222. int type = 0;
  2223. int streamable = 1;
  2224. if (pattern == NULL)
  2225. return(NULL);
  2226. start = pattern;
  2227. or = start;
  2228. while (*or != 0) {
  2229. tmp = NULL;
  2230. while ((*or != 0) && (*or != '|')) or++;
  2231. if (*or == 0)
  2232. ctxt = xmlNewPatParserContext(start, dict, namespaces);
  2233. else {
  2234. tmp = xmlStrndup(start, or - start);
  2235. if (tmp != NULL) {
  2236. ctxt = xmlNewPatParserContext(tmp, dict, namespaces);
  2237. }
  2238. or++;
  2239. }
  2240. if (ctxt == NULL) goto error;
  2241. cur = xmlNewPattern();
  2242. if (cur == NULL) goto error;
  2243. /*
  2244. * Assign string dict.
  2245. */
  2246. if (dict) {
  2247. cur->dict = dict;
  2248. xmlDictReference(dict);
  2249. }
  2250. if (ret == NULL)
  2251. ret = cur;
  2252. else {
  2253. cur->next = ret->next;
  2254. ret->next = cur;
  2255. }
  2256. cur->flags = flags;
  2257. ctxt->comp = cur;
  2258. if (XML_STREAM_XS_IDC(cur))
  2259. xmlCompileIDCXPathPath(ctxt);
  2260. else
  2261. xmlCompilePathPattern(ctxt);
  2262. if (ctxt->error != 0)
  2263. goto error;
  2264. xmlFreePatParserContext(ctxt);
  2265. ctxt = NULL;
  2266. if (streamable) {
  2267. if (type == 0) {
  2268. type = cur->flags & (PAT_FROM_ROOT | PAT_FROM_CUR);
  2269. } else if (type == PAT_FROM_ROOT) {
  2270. if (cur->flags & PAT_FROM_CUR)
  2271. streamable = 0;
  2272. } else if (type == PAT_FROM_CUR) {
  2273. if (cur->flags & PAT_FROM_ROOT)
  2274. streamable = 0;
  2275. }
  2276. }
  2277. if (streamable)
  2278. xmlStreamCompile(cur);
  2279. if (xmlReversePattern(cur) < 0)
  2280. goto error;
  2281. if (tmp != NULL) {
  2282. xmlFree(tmp);
  2283. tmp = NULL;
  2284. }
  2285. start = or;
  2286. }
  2287. if (streamable == 0) {
  2288. cur = ret;
  2289. while (cur != NULL) {
  2290. if (cur->stream != NULL) {
  2291. xmlFreeStreamComp(cur->stream);
  2292. cur->stream = NULL;
  2293. }
  2294. cur = cur->next;
  2295. }
  2296. }
  2297. return(ret);
  2298. error:
  2299. if (ctxt != NULL) xmlFreePatParserContext(ctxt);
  2300. if (ret != NULL) xmlFreePattern(ret);
  2301. if (tmp != NULL) xmlFree(tmp);
  2302. return(NULL);
  2303. }
  2304. /**
  2305. * xmlPatternMatch:
  2306. * @comp: the precompiled pattern
  2307. * @node: a node
  2308. *
  2309. * Test whether the node matches the pattern
  2310. *
  2311. * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
  2312. */
  2313. int
  2314. xmlPatternMatch(xmlPatternPtr comp, xmlNodePtr node)
  2315. {
  2316. int ret = 0;
  2317. if ((comp == NULL) || (node == NULL))
  2318. return(-1);
  2319. while (comp != NULL) {
  2320. ret = xmlPatMatch(comp, node);
  2321. if (ret != 0)
  2322. return(ret);
  2323. comp = comp->next;
  2324. }
  2325. return(ret);
  2326. }
  2327. /**
  2328. * xmlPatternGetStreamCtxt:
  2329. * @comp: the precompiled pattern
  2330. *
  2331. * Get a streaming context for that pattern
  2332. * Use xmlFreeStreamCtxt to free the context.
  2333. *
  2334. * Returns a pointer to the context or NULL in case of failure
  2335. */
  2336. xmlStreamCtxtPtr
  2337. xmlPatternGetStreamCtxt(xmlPatternPtr comp)
  2338. {
  2339. xmlStreamCtxtPtr ret = NULL, cur;
  2340. if ((comp == NULL) || (comp->stream == NULL))
  2341. return(NULL);
  2342. while (comp != NULL) {
  2343. if (comp->stream == NULL)
  2344. goto failed;
  2345. cur = xmlNewStreamCtxt(comp->stream);
  2346. if (cur == NULL)
  2347. goto failed;
  2348. if (ret == NULL)
  2349. ret = cur;
  2350. else {
  2351. cur->next = ret->next;
  2352. ret->next = cur;
  2353. }
  2354. cur->flags = comp->flags;
  2355. comp = comp->next;
  2356. }
  2357. return(ret);
  2358. failed:
  2359. xmlFreeStreamCtxt(ret);
  2360. return(NULL);
  2361. }
  2362. /**
  2363. * xmlPatternStreamable:
  2364. * @comp: the precompiled pattern
  2365. *
  2366. * Check if the pattern is streamable i.e. xmlPatternGetStreamCtxt()
  2367. * should work.
  2368. *
  2369. * Returns 1 if streamable, 0 if not and -1 in case of error.
  2370. */
  2371. int
  2372. xmlPatternStreamable(xmlPatternPtr comp) {
  2373. if (comp == NULL)
  2374. return(-1);
  2375. while (comp != NULL) {
  2376. if (comp->stream == NULL)
  2377. return(0);
  2378. comp = comp->next;
  2379. }
  2380. return(1);
  2381. }
  2382. /**
  2383. * xmlPatternMaxDepth:
  2384. * @comp: the precompiled pattern
  2385. *
  2386. * Check the maximum depth reachable by a pattern
  2387. *
  2388. * Returns -2 if no limit (using //), otherwise the depth,
  2389. * and -1 in case of error
  2390. */
  2391. int
  2392. xmlPatternMaxDepth(xmlPatternPtr comp) {
  2393. int ret = 0, i;
  2394. if (comp == NULL)
  2395. return(-1);
  2396. while (comp != NULL) {
  2397. if (comp->stream == NULL)
  2398. return(-1);
  2399. for (i = 0;i < comp->stream->nbStep;i++)
  2400. if (comp->stream->steps[i].flags & XML_STREAM_STEP_DESC)
  2401. return(-2);
  2402. if (comp->stream->nbStep > ret)
  2403. ret = comp->stream->nbStep;
  2404. comp = comp->next;
  2405. }
  2406. return(ret);
  2407. }
  2408. /**
  2409. * xmlPatternMinDepth:
  2410. * @comp: the precompiled pattern
  2411. *
  2412. * Check the minimum depth reachable by a pattern, 0 mean the / or . are
  2413. * part of the set.
  2414. *
  2415. * Returns -1 in case of error otherwise the depth,
  2416. *
  2417. */
  2418. int
  2419. xmlPatternMinDepth(xmlPatternPtr comp) {
  2420. int ret = 12345678;
  2421. if (comp == NULL)
  2422. return(-1);
  2423. while (comp != NULL) {
  2424. if (comp->stream == NULL)
  2425. return(-1);
  2426. if (comp->stream->nbStep < ret)
  2427. ret = comp->stream->nbStep;
  2428. if (ret == 0)
  2429. return(0);
  2430. comp = comp->next;
  2431. }
  2432. return(ret);
  2433. }
  2434. /**
  2435. * xmlPatternFromRoot:
  2436. * @comp: the precompiled pattern
  2437. *
  2438. * Check if the pattern must be looked at from the root.
  2439. *
  2440. * Returns 1 if true, 0 if false and -1 in case of error
  2441. */
  2442. int
  2443. xmlPatternFromRoot(xmlPatternPtr comp) {
  2444. if (comp == NULL)
  2445. return(-1);
  2446. while (comp != NULL) {
  2447. if (comp->stream == NULL)
  2448. return(-1);
  2449. if (comp->flags & PAT_FROM_ROOT)
  2450. return(1);
  2451. comp = comp->next;
  2452. }
  2453. return(0);
  2454. }
  2455. #define bottom_pattern
  2456. #include "elfgcchack.h"
  2457. #endif /* LIBXML_PATTERN_ENABLED */