pattern.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622
  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 */