pattern.c 62 KB

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