xinclude.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492
  1. /*
  2. * xinclude.c : Code to implement XInclude processing
  3. *
  4. * World Wide Web Consortium W3C Last Call Working Draft 10 November 2003
  5. * http://www.w3.org/TR/2003/WD-xinclude-20031110
  6. *
  7. * See Copyright for the status of this software.
  8. *
  9. * daniel@veillard.com
  10. */
  11. #define IN_LIBXML
  12. #include "libxml.h"
  13. #include <string.h>
  14. #include <libxml/xmlmemory.h>
  15. #include <libxml/tree.h>
  16. #include <libxml/parser.h>
  17. #include <libxml/uri.h>
  18. #include <libxml/xpath.h>
  19. #include <libxml/xpointer.h>
  20. #include <libxml/parserInternals.h>
  21. #include <libxml/xmlerror.h>
  22. #include <libxml/encoding.h>
  23. #ifdef LIBXML_XINCLUDE_ENABLED
  24. #include <libxml/xinclude.h>
  25. #include "private/buf.h"
  26. #include "private/error.h"
  27. #include "private/tree.h"
  28. #include "private/xinclude.h"
  29. #define XINCLUDE_MAX_DEPTH 40
  30. /************************************************************************
  31. * *
  32. * XInclude context handling *
  33. * *
  34. ************************************************************************/
  35. /*
  36. * An XInclude context
  37. */
  38. typedef xmlChar *xmlURL;
  39. typedef struct _xmlXIncludeRef xmlXIncludeRef;
  40. typedef xmlXIncludeRef *xmlXIncludeRefPtr;
  41. struct _xmlXIncludeRef {
  42. xmlChar *URI; /* the fully resolved resource URL */
  43. xmlChar *fragment; /* the fragment in the URI */
  44. xmlNodePtr elem; /* the xi:include element */
  45. xmlNodePtr inc; /* the included copy */
  46. int xml; /* xml or txt */
  47. int fallback; /* fallback was loaded */
  48. int emptyFb; /* flag to show fallback empty */
  49. int expanding; /* flag to detect inclusion loops */
  50. int replace; /* should the node be replaced? */
  51. };
  52. typedef struct _xmlXIncludeDoc xmlXIncludeDoc;
  53. typedef xmlXIncludeDoc *xmlXIncludeDocPtr;
  54. struct _xmlXIncludeDoc {
  55. xmlDocPtr doc; /* the parsed document */
  56. xmlChar *url; /* the URL */
  57. int expanding; /* flag to detect inclusion loops */
  58. };
  59. typedef struct _xmlXIncludeTxt xmlXIncludeTxt;
  60. typedef xmlXIncludeTxt *xmlXIncludeTxtPtr;
  61. struct _xmlXIncludeTxt {
  62. xmlChar *text; /* text string */
  63. xmlChar *url; /* the URL */
  64. };
  65. struct _xmlXIncludeCtxt {
  66. xmlDocPtr doc; /* the source document */
  67. int incNr; /* number of includes */
  68. int incMax; /* size of includes tab */
  69. xmlXIncludeRefPtr *incTab; /* array of included references */
  70. int txtNr; /* number of unparsed documents */
  71. int txtMax; /* size of unparsed documents tab */
  72. xmlXIncludeTxt *txtTab; /* array of unparsed documents */
  73. int urlNr; /* number of documents stacked */
  74. int urlMax; /* size of document stack */
  75. xmlXIncludeDoc *urlTab; /* document stack */
  76. int nbErrors; /* the number of errors detected */
  77. int fatalErr; /* abort processing */
  78. int legacy; /* using XINCLUDE_OLD_NS */
  79. int parseFlags; /* the flags used for parsing XML documents */
  80. xmlChar * base; /* the current xml:base */
  81. void *_private; /* application data */
  82. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  83. unsigned long incTotal; /* total number of processed inclusions */
  84. #endif
  85. int depth; /* recursion depth */
  86. int isStream; /* streaming mode */
  87. };
  88. static xmlXIncludeRefPtr
  89. xmlXIncludeExpandNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node);
  90. static int
  91. xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref);
  92. static int
  93. xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlNodePtr tree);
  94. /************************************************************************
  95. * *
  96. * XInclude error handler *
  97. * *
  98. ************************************************************************/
  99. /**
  100. * xmlXIncludeErrMemory:
  101. * @extra: extra information
  102. *
  103. * Handle an out of memory condition
  104. */
  105. static void
  106. xmlXIncludeErrMemory(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node,
  107. const char *extra)
  108. {
  109. if (ctxt != NULL)
  110. ctxt->nbErrors++;
  111. __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
  112. XML_ERR_NO_MEMORY, XML_ERR_ERROR, NULL, 0,
  113. extra, NULL, NULL, 0, 0,
  114. "Memory allocation failed : %s\n", extra);
  115. }
  116. /**
  117. * xmlXIncludeErr:
  118. * @ctxt: the XInclude context
  119. * @node: the context node
  120. * @msg: the error message
  121. * @extra: extra information
  122. *
  123. * Handle an XInclude error
  124. */
  125. static void LIBXML_ATTR_FORMAT(4,0)
  126. xmlXIncludeErr(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
  127. const char *msg, const xmlChar *extra)
  128. {
  129. if (ctxt != NULL)
  130. ctxt->nbErrors++;
  131. __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
  132. error, XML_ERR_ERROR, NULL, 0,
  133. (const char *) extra, NULL, NULL, 0, 0,
  134. msg, (const char *) extra);
  135. }
  136. #if 0
  137. /**
  138. * xmlXIncludeWarn:
  139. * @ctxt: the XInclude context
  140. * @node: the context node
  141. * @msg: the error message
  142. * @extra: extra information
  143. *
  144. * Emit an XInclude warning.
  145. */
  146. static void LIBXML_ATTR_FORMAT(4,0)
  147. xmlXIncludeWarn(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node, int error,
  148. const char *msg, const xmlChar *extra)
  149. {
  150. __xmlRaiseError(NULL, NULL, NULL, ctxt, node, XML_FROM_XINCLUDE,
  151. error, XML_ERR_WARNING, NULL, 0,
  152. (const char *) extra, NULL, NULL, 0, 0,
  153. msg, (const char *) extra);
  154. }
  155. #endif
  156. /**
  157. * xmlXIncludeGetProp:
  158. * @ctxt: the XInclude context
  159. * @cur: the node
  160. * @name: the attribute name
  161. *
  162. * Get an XInclude attribute
  163. *
  164. * Returns the value (to be freed) or NULL if not found
  165. */
  166. static xmlChar *
  167. xmlXIncludeGetProp(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur,
  168. const xmlChar *name) {
  169. xmlChar *ret;
  170. ret = xmlGetNsProp(cur, XINCLUDE_NS, name);
  171. if (ret != NULL)
  172. return(ret);
  173. if (ctxt->legacy != 0) {
  174. ret = xmlGetNsProp(cur, XINCLUDE_OLD_NS, name);
  175. if (ret != NULL)
  176. return(ret);
  177. }
  178. ret = xmlGetProp(cur, name);
  179. return(ret);
  180. }
  181. /**
  182. * xmlXIncludeFreeRef:
  183. * @ref: the XInclude reference
  184. *
  185. * Free an XInclude reference
  186. */
  187. static void
  188. xmlXIncludeFreeRef(xmlXIncludeRefPtr ref) {
  189. if (ref == NULL)
  190. return;
  191. if (ref->URI != NULL)
  192. xmlFree(ref->URI);
  193. if (ref->fragment != NULL)
  194. xmlFree(ref->fragment);
  195. xmlFree(ref);
  196. }
  197. /**
  198. * xmlXIncludeNewRef:
  199. * @ctxt: the XInclude context
  200. * @URI: the resource URI
  201. * @elem: the xi:include element
  202. *
  203. * Creates a new reference within an XInclude context
  204. *
  205. * Returns the new set
  206. */
  207. static xmlXIncludeRefPtr
  208. xmlXIncludeNewRef(xmlXIncludeCtxtPtr ctxt, const xmlChar *URI,
  209. xmlNodePtr elem) {
  210. xmlXIncludeRefPtr ret;
  211. ret = (xmlXIncludeRefPtr) xmlMalloc(sizeof(xmlXIncludeRef));
  212. if (ret == NULL) {
  213. xmlXIncludeErrMemory(ctxt, elem, "growing XInclude context");
  214. return(NULL);
  215. }
  216. memset(ret, 0, sizeof(xmlXIncludeRef));
  217. if (URI == NULL)
  218. ret->URI = NULL;
  219. else
  220. ret->URI = xmlStrdup(URI);
  221. ret->fragment = NULL;
  222. ret->elem = elem;
  223. ret->xml = 0;
  224. ret->inc = NULL;
  225. if (ctxt->incNr >= ctxt->incMax) {
  226. xmlXIncludeRefPtr *tmp;
  227. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  228. size_t newSize = ctxt->incMax ? ctxt->incMax * 2 : 1;
  229. #else
  230. size_t newSize = ctxt->incMax ? ctxt->incMax * 2 : 4;
  231. #endif
  232. tmp = (xmlXIncludeRefPtr *) xmlRealloc(ctxt->incTab,
  233. newSize * sizeof(ctxt->incTab[0]));
  234. if (tmp == NULL) {
  235. xmlXIncludeErrMemory(ctxt, elem, "growing XInclude context");
  236. xmlXIncludeFreeRef(ret);
  237. return(NULL);
  238. }
  239. ctxt->incTab = tmp;
  240. ctxt->incMax = newSize;
  241. }
  242. ctxt->incTab[ctxt->incNr++] = ret;
  243. return(ret);
  244. }
  245. /**
  246. * xmlXIncludeNewContext:
  247. * @doc: an XML Document
  248. *
  249. * Creates a new XInclude context
  250. *
  251. * Returns the new set
  252. */
  253. xmlXIncludeCtxtPtr
  254. xmlXIncludeNewContext(xmlDocPtr doc) {
  255. xmlXIncludeCtxtPtr ret;
  256. if (doc == NULL)
  257. return(NULL);
  258. ret = (xmlXIncludeCtxtPtr) xmlMalloc(sizeof(xmlXIncludeCtxt));
  259. if (ret == NULL) {
  260. xmlXIncludeErrMemory(NULL, (xmlNodePtr) doc,
  261. "creating XInclude context");
  262. return(NULL);
  263. }
  264. memset(ret, 0, sizeof(xmlXIncludeCtxt));
  265. ret->doc = doc;
  266. ret->incNr = 0;
  267. ret->incMax = 0;
  268. ret->incTab = NULL;
  269. ret->nbErrors = 0;
  270. return(ret);
  271. }
  272. /**
  273. * xmlXIncludeFreeContext:
  274. * @ctxt: the XInclude context
  275. *
  276. * Free an XInclude context
  277. */
  278. void
  279. xmlXIncludeFreeContext(xmlXIncludeCtxtPtr ctxt) {
  280. int i;
  281. if (ctxt == NULL)
  282. return;
  283. if (ctxt->urlTab != NULL) {
  284. for (i = 0; i < ctxt->urlNr; i++) {
  285. xmlFreeDoc(ctxt->urlTab[i].doc);
  286. xmlFree(ctxt->urlTab[i].url);
  287. }
  288. xmlFree(ctxt->urlTab);
  289. }
  290. for (i = 0;i < ctxt->incNr;i++) {
  291. if (ctxt->incTab[i] != NULL)
  292. xmlXIncludeFreeRef(ctxt->incTab[i]);
  293. }
  294. if (ctxt->incTab != NULL)
  295. xmlFree(ctxt->incTab);
  296. if (ctxt->txtTab != NULL) {
  297. for (i = 0;i < ctxt->txtNr;i++) {
  298. xmlFree(ctxt->txtTab[i].text);
  299. xmlFree(ctxt->txtTab[i].url);
  300. }
  301. xmlFree(ctxt->txtTab);
  302. }
  303. if (ctxt->base != NULL) {
  304. xmlFree(ctxt->base);
  305. }
  306. xmlFree(ctxt);
  307. }
  308. /**
  309. * xmlXIncludeParseFile:
  310. * @ctxt: the XInclude context
  311. * @URL: the URL or file path
  312. *
  313. * parse a document for XInclude
  314. */
  315. static xmlDocPtr
  316. xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
  317. xmlDocPtr ret;
  318. xmlParserCtxtPtr pctxt;
  319. xmlParserInputPtr inputStream;
  320. xmlInitParser();
  321. pctxt = xmlNewParserCtxt();
  322. if (pctxt == NULL) {
  323. xmlXIncludeErrMemory(ctxt, NULL, "cannot allocate parser context");
  324. return(NULL);
  325. }
  326. /*
  327. * pass in the application data to the parser context.
  328. */
  329. pctxt->_private = ctxt->_private;
  330. /*
  331. * try to ensure that new documents included are actually
  332. * built with the same dictionary as the including document.
  333. */
  334. if ((ctxt->doc != NULL) && (ctxt->doc->dict != NULL)) {
  335. if (pctxt->dict != NULL)
  336. xmlDictFree(pctxt->dict);
  337. pctxt->dict = ctxt->doc->dict;
  338. xmlDictReference(pctxt->dict);
  339. }
  340. xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
  341. /* Don't read from stdin. */
  342. if ((URL != NULL) && (strcmp(URL, "-") == 0))
  343. URL = "./-";
  344. inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
  345. if (inputStream == NULL) {
  346. xmlFreeParserCtxt(pctxt);
  347. return(NULL);
  348. }
  349. inputPush(pctxt, inputStream);
  350. if (pctxt->directory == NULL)
  351. pctxt->directory = xmlParserGetDirectory(URL);
  352. pctxt->loadsubset |= XML_DETECT_IDS;
  353. xmlParseDocument(pctxt);
  354. if (pctxt->wellFormed) {
  355. ret = pctxt->myDoc;
  356. }
  357. else {
  358. ret = NULL;
  359. if (pctxt->myDoc != NULL)
  360. xmlFreeDoc(pctxt->myDoc);
  361. pctxt->myDoc = NULL;
  362. }
  363. xmlFreeParserCtxt(pctxt);
  364. return(ret);
  365. }
  366. /**
  367. * xmlXIncludeAddNode:
  368. * @ctxt: the XInclude context
  369. * @cur: the new node
  370. *
  371. * Add a new node to process to an XInclude context
  372. */
  373. static xmlXIncludeRefPtr
  374. xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr cur) {
  375. xmlXIncludeRefPtr ref;
  376. xmlURIPtr uri;
  377. xmlChar *URL;
  378. xmlChar *fragment = NULL;
  379. xmlChar *href;
  380. xmlChar *parse;
  381. xmlChar *base;
  382. xmlChar *URI;
  383. int xml = 1;
  384. int local = 0;
  385. if (ctxt == NULL)
  386. return(NULL);
  387. if (cur == NULL)
  388. return(NULL);
  389. /*
  390. * read the attributes
  391. */
  392. href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
  393. if (href == NULL) {
  394. href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
  395. if (href == NULL)
  396. return(NULL);
  397. }
  398. parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
  399. if (parse != NULL) {
  400. if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
  401. xml = 1;
  402. else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
  403. xml = 0;
  404. else {
  405. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
  406. "invalid value %s for 'parse'\n", parse);
  407. if (href != NULL)
  408. xmlFree(href);
  409. if (parse != NULL)
  410. xmlFree(parse);
  411. return(NULL);
  412. }
  413. }
  414. /*
  415. * compute the URI
  416. */
  417. base = xmlNodeGetBase(ctxt->doc, cur);
  418. if (base == NULL) {
  419. URI = xmlBuildURI(href, ctxt->doc->URL);
  420. } else {
  421. URI = xmlBuildURI(href, base);
  422. }
  423. if (URI == NULL) {
  424. xmlChar *escbase;
  425. xmlChar *eschref;
  426. /*
  427. * Some escaping may be needed
  428. */
  429. escbase = xmlURIEscape(base);
  430. eschref = xmlURIEscape(href);
  431. URI = xmlBuildURI(eschref, escbase);
  432. if (escbase != NULL)
  433. xmlFree(escbase);
  434. if (eschref != NULL)
  435. xmlFree(eschref);
  436. }
  437. if (parse != NULL)
  438. xmlFree(parse);
  439. if (href != NULL)
  440. xmlFree(href);
  441. if (base != NULL)
  442. xmlFree(base);
  443. if (URI == NULL) {
  444. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
  445. "failed build URL\n", NULL);
  446. return(NULL);
  447. }
  448. fragment = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE_XPOINTER);
  449. /*
  450. * Check the URL and remove any fragment identifier
  451. */
  452. uri = xmlParseURI((const char *)URI);
  453. if (uri == NULL) {
  454. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
  455. "invalid value URI %s\n", URI);
  456. if (fragment != NULL)
  457. xmlFree(fragment);
  458. xmlFree(URI);
  459. return(NULL);
  460. }
  461. if (uri->fragment != NULL) {
  462. if (ctxt->legacy != 0) {
  463. if (fragment == NULL) {
  464. fragment = (xmlChar *) uri->fragment;
  465. } else {
  466. xmlFree(uri->fragment);
  467. }
  468. } else {
  469. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_FRAGMENT_ID,
  470. "Invalid fragment identifier in URI %s use the xpointer attribute\n",
  471. URI);
  472. if (fragment != NULL)
  473. xmlFree(fragment);
  474. xmlFreeURI(uri);
  475. xmlFree(URI);
  476. return(NULL);
  477. }
  478. uri->fragment = NULL;
  479. }
  480. URL = xmlSaveUri(uri);
  481. xmlFreeURI(uri);
  482. if (URL == NULL) {
  483. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
  484. "invalid value URI %s\n", URI);
  485. if (fragment != NULL)
  486. xmlFree(fragment);
  487. xmlFree(URI);
  488. return(NULL);
  489. }
  490. xmlFree(URI);
  491. if (xmlStrEqual(URL, ctxt->doc->URL))
  492. local = 1;
  493. /*
  494. * If local and xml then we need a fragment
  495. */
  496. if ((local == 1) && (xml == 1) &&
  497. ((fragment == NULL) || (fragment[0] == 0))) {
  498. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_RECURSION,
  499. "detected a local recursion with no xpointer in %s\n",
  500. URL);
  501. xmlFree(URL);
  502. xmlFree(fragment);
  503. return(NULL);
  504. }
  505. ref = xmlXIncludeNewRef(ctxt, URL, cur);
  506. xmlFree(URL);
  507. if (ref == NULL) {
  508. xmlFree(fragment);
  509. return(NULL);
  510. }
  511. ref->fragment = fragment;
  512. ref->xml = xml;
  513. return(ref);
  514. }
  515. /**
  516. * xmlXIncludeRecurseDoc:
  517. * @ctxt: the XInclude context
  518. * @doc: the new document
  519. * @url: the associated URL
  520. *
  521. * The XInclude recursive nature is handled at this point.
  522. */
  523. static void
  524. xmlXIncludeRecurseDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
  525. const xmlURL url ATTRIBUTE_UNUSED) {
  526. xmlDocPtr oldDoc;
  527. xmlXIncludeRefPtr *oldIncTab;
  528. int oldIncMax, oldIncNr, oldIsStream;
  529. int i;
  530. oldDoc = ctxt->doc;
  531. oldIncMax = ctxt->incMax;
  532. oldIncNr = ctxt->incNr;
  533. oldIncTab = ctxt->incTab;
  534. oldIsStream = ctxt->isStream;
  535. ctxt->doc = doc;
  536. ctxt->incMax = 0;
  537. ctxt->incNr = 0;
  538. ctxt->incTab = NULL;
  539. ctxt->isStream = 0;
  540. xmlXIncludeDoProcess(ctxt, xmlDocGetRootElement(doc));
  541. if (ctxt->incTab != NULL) {
  542. for (i = 0; i < ctxt->incNr; i++)
  543. xmlXIncludeFreeRef(ctxt->incTab[i]);
  544. xmlFree(ctxt->incTab);
  545. }
  546. ctxt->doc = oldDoc;
  547. ctxt->incMax = oldIncMax;
  548. ctxt->incNr = oldIncNr;
  549. ctxt->incTab = oldIncTab;
  550. ctxt->isStream = oldIsStream;
  551. }
  552. /************************************************************************
  553. * *
  554. * Node copy with specific semantic *
  555. * *
  556. ************************************************************************/
  557. /**
  558. * xmlXIncludeCopyNode:
  559. * @ctxt: the XInclude context
  560. * @elem: the element
  561. * @copyChildren: copy children instead of node if true
  562. *
  563. * Make a copy of the node while expanding nested XIncludes.
  564. *
  565. * Returns a node list, not a single node.
  566. */
  567. static xmlNodePtr
  568. xmlXIncludeCopyNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr elem,
  569. int copyChildren) {
  570. xmlNodePtr result = NULL;
  571. xmlNodePtr insertParent = NULL;
  572. xmlNodePtr insertLast = NULL;
  573. xmlNodePtr cur;
  574. if (copyChildren) {
  575. cur = elem->children;
  576. if (cur == NULL)
  577. return(NULL);
  578. } else {
  579. cur = elem;
  580. }
  581. while (1) {
  582. xmlNodePtr copy = NULL;
  583. int recurse = 0;
  584. if ((cur->type == XML_DOCUMENT_NODE) ||
  585. (cur->type == XML_DTD_NODE)) {
  586. ;
  587. } else if ((cur->type == XML_ELEMENT_NODE) &&
  588. (cur->ns != NULL) &&
  589. (xmlStrEqual(cur->name, XINCLUDE_NODE)) &&
  590. ((xmlStrEqual(cur->ns->href, XINCLUDE_NS)) ||
  591. (xmlStrEqual(cur->ns->href, XINCLUDE_OLD_NS)))) {
  592. xmlXIncludeRefPtr ref = xmlXIncludeExpandNode(ctxt, cur);
  593. if (ref == NULL)
  594. goto error;
  595. /*
  596. * TODO: Insert XML_XINCLUDE_START and XML_XINCLUDE_END nodes
  597. */
  598. if (ref->inc != NULL) {
  599. copy = xmlStaticCopyNodeList(ref->inc, ctxt->doc,
  600. insertParent);
  601. if (copy == NULL)
  602. goto error;
  603. }
  604. } else {
  605. copy = xmlStaticCopyNode(cur, ctxt->doc, insertParent, 2);
  606. if (copy == NULL)
  607. goto error;
  608. recurse = (cur->type != XML_ENTITY_REF_NODE) &&
  609. (cur->children != NULL);
  610. }
  611. if (copy != NULL) {
  612. if (result == NULL)
  613. result = copy;
  614. if (insertLast != NULL) {
  615. insertLast->next = copy;
  616. copy->prev = insertLast;
  617. } else if (insertParent != NULL) {
  618. insertParent->children = copy;
  619. }
  620. insertLast = copy;
  621. while (insertLast->next != NULL) {
  622. insertLast = insertLast->next;
  623. }
  624. }
  625. if (recurse) {
  626. cur = cur->children;
  627. insertParent = insertLast;
  628. insertLast = NULL;
  629. continue;
  630. }
  631. if (cur == elem)
  632. return(result);
  633. while (cur->next == NULL) {
  634. if (insertParent != NULL)
  635. insertParent->last = insertLast;
  636. cur = cur->parent;
  637. if (cur == elem)
  638. return(result);
  639. insertLast = insertParent;
  640. insertParent = insertParent->parent;
  641. }
  642. cur = cur->next;
  643. }
  644. error:
  645. xmlFreeNodeList(result);
  646. return(NULL);
  647. }
  648. #ifdef LIBXML_XPTR_LOCS_ENABLED
  649. /**
  650. * xmlXIncludeGetNthChild:
  651. * @cur: the node
  652. * @no: the child number
  653. *
  654. * Returns the @n'th element child of @cur or NULL
  655. */
  656. static xmlNodePtr
  657. xmlXIncludeGetNthChild(xmlNodePtr cur, int no) {
  658. int i;
  659. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
  660. return(NULL);
  661. cur = cur->children;
  662. for (i = 0;i <= no;cur = cur->next) {
  663. if (cur == NULL)
  664. return(cur);
  665. if ((cur->type == XML_ELEMENT_NODE) ||
  666. (cur->type == XML_DOCUMENT_NODE) ||
  667. (cur->type == XML_HTML_DOCUMENT_NODE)) {
  668. i++;
  669. if (i == no)
  670. break;
  671. }
  672. }
  673. return(cur);
  674. }
  675. xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level); /* in xpointer.c */
  676. /**
  677. * xmlXIncludeCopyRange:
  678. * @ctxt: the XInclude context
  679. * @obj: the XPointer result from the evaluation.
  680. *
  681. * Build a node list tree copy of the XPointer result.
  682. *
  683. * Returns an xmlNodePtr list or NULL.
  684. * The caller has to free the node tree.
  685. */
  686. static xmlNodePtr
  687. xmlXIncludeCopyRange(xmlXIncludeCtxtPtr ctxt, xmlXPathObjectPtr range) {
  688. /* pointers to generated nodes */
  689. xmlNodePtr list = NULL, last = NULL, listParent = NULL;
  690. xmlNodePtr tmp, tmp2;
  691. /* pointers to traversal nodes */
  692. xmlNodePtr start, cur, end;
  693. int index1, index2;
  694. int level = 0, lastLevel = 0, endLevel = 0, endFlag = 0;
  695. if ((ctxt == NULL) || (range == NULL))
  696. return(NULL);
  697. if (range->type != XPATH_RANGE)
  698. return(NULL);
  699. start = (xmlNodePtr) range->user;
  700. if ((start == NULL) || (start->type == XML_NAMESPACE_DECL))
  701. return(NULL);
  702. end = range->user2;
  703. if (end == NULL)
  704. return(xmlDocCopyNode(start, ctxt->doc, 1));
  705. if (end->type == XML_NAMESPACE_DECL)
  706. return(NULL);
  707. cur = start;
  708. index1 = range->index;
  709. index2 = range->index2;
  710. /*
  711. * level is depth of the current node under consideration
  712. * list is the pointer to the root of the output tree
  713. * listParent is a pointer to the parent of output tree (within
  714. the included file) in case we need to add another level
  715. * last is a pointer to the last node added to the output tree
  716. * lastLevel is the depth of last (relative to the root)
  717. */
  718. while (cur != NULL) {
  719. /*
  720. * Check if our output tree needs a parent
  721. */
  722. if (level < 0) {
  723. while (level < 0) {
  724. /* copy must include namespaces and properties */
  725. tmp2 = xmlDocCopyNode(listParent, ctxt->doc, 2);
  726. xmlAddChild(tmp2, list);
  727. list = tmp2;
  728. listParent = listParent->parent;
  729. level++;
  730. }
  731. last = list;
  732. lastLevel = 0;
  733. }
  734. /*
  735. * Check whether we need to change our insertion point
  736. */
  737. while (level < lastLevel) {
  738. last = last->parent;
  739. lastLevel --;
  740. }
  741. if (cur == end) { /* Are we at the end of the range? */
  742. if (cur->type == XML_TEXT_NODE) {
  743. const xmlChar *content = cur->content;
  744. int len;
  745. if (content == NULL) {
  746. tmp = xmlNewDocTextLen(ctxt->doc, NULL, 0);
  747. } else {
  748. len = index2;
  749. if ((cur == start) && (index1 > 1)) {
  750. content += (index1 - 1);
  751. len -= (index1 - 1);
  752. } else {
  753. len = index2;
  754. }
  755. tmp = xmlNewDocTextLen(ctxt->doc, content, len);
  756. }
  757. /* single sub text node selection */
  758. if (list == NULL)
  759. return(tmp);
  760. /* prune and return full set */
  761. if (level == lastLevel)
  762. xmlAddNextSibling(last, tmp);
  763. else
  764. xmlAddChild(last, tmp);
  765. return(list);
  766. } else { /* ending node not a text node */
  767. endLevel = level; /* remember the level of the end node */
  768. endFlag = 1;
  769. /* last node - need to take care of properties + namespaces */
  770. tmp = xmlDocCopyNode(cur, ctxt->doc, 2);
  771. if (list == NULL) {
  772. list = tmp;
  773. listParent = cur->parent;
  774. last = tmp;
  775. } else {
  776. if (level == lastLevel)
  777. last = xmlAddNextSibling(last, tmp);
  778. else {
  779. last = xmlAddChild(last, tmp);
  780. lastLevel = level;
  781. }
  782. }
  783. if (index2 > 1) {
  784. end = xmlXIncludeGetNthChild(cur, index2 - 1);
  785. index2 = 0;
  786. }
  787. if ((cur == start) && (index1 > 1)) {
  788. cur = xmlXIncludeGetNthChild(cur, index1 - 1);
  789. index1 = 0;
  790. } else {
  791. cur = cur->children;
  792. }
  793. level++; /* increment level to show change */
  794. /*
  795. * Now gather the remaining nodes from cur to end
  796. */
  797. continue; /* while */
  798. }
  799. } else if (cur == start) { /* Not at the end, are we at start? */
  800. if ((cur->type == XML_TEXT_NODE) ||
  801. (cur->type == XML_CDATA_SECTION_NODE)) {
  802. const xmlChar *content = cur->content;
  803. if (content == NULL) {
  804. tmp = xmlNewDocTextLen(ctxt->doc, NULL, 0);
  805. } else {
  806. if (index1 > 1) {
  807. content += (index1 - 1);
  808. index1 = 0;
  809. }
  810. tmp = xmlNewDocText(ctxt->doc, content);
  811. }
  812. last = list = tmp;
  813. listParent = cur->parent;
  814. } else { /* Not text node */
  815. /*
  816. * start of the range - need to take care of
  817. * properties and namespaces
  818. */
  819. tmp = xmlDocCopyNode(cur, ctxt->doc, 2);
  820. list = last = tmp;
  821. listParent = cur->parent;
  822. if (index1 > 1) { /* Do we need to position? */
  823. cur = xmlXIncludeGetNthChild(cur, index1 - 1);
  824. level = lastLevel = 1;
  825. index1 = 0;
  826. /*
  827. * Now gather the remaining nodes from cur to end
  828. */
  829. continue; /* while */
  830. }
  831. }
  832. } else {
  833. tmp = NULL;
  834. switch (cur->type) {
  835. case XML_DTD_NODE:
  836. case XML_ELEMENT_DECL:
  837. case XML_ATTRIBUTE_DECL:
  838. case XML_ENTITY_NODE:
  839. /* Do not copy DTD information */
  840. break;
  841. case XML_ENTITY_DECL:
  842. /* handle crossing entities -> stack needed */
  843. break;
  844. case XML_XINCLUDE_START:
  845. case XML_XINCLUDE_END:
  846. /* don't consider it part of the tree content */
  847. break;
  848. case XML_ATTRIBUTE_NODE:
  849. /* Humm, should not happen ! */
  850. break;
  851. default:
  852. /*
  853. * Middle of the range - need to take care of
  854. * properties and namespaces
  855. */
  856. tmp = xmlDocCopyNode(cur, ctxt->doc, 2);
  857. break;
  858. }
  859. if (tmp != NULL) {
  860. if (level == lastLevel)
  861. last = xmlAddNextSibling(last, tmp);
  862. else {
  863. last = xmlAddChild(last, tmp);
  864. lastLevel = level;
  865. }
  866. }
  867. }
  868. /*
  869. * Skip to next node in document order
  870. */
  871. cur = xmlXPtrAdvanceNode(cur, &level);
  872. if (endFlag && (level >= endLevel))
  873. break;
  874. }
  875. return(list);
  876. }
  877. #endif /* LIBXML_XPTR_LOCS_ENABLED */
  878. /**
  879. * xmlXIncludeCopyXPointer:
  880. * @ctxt: the XInclude context
  881. * @obj: the XPointer result from the evaluation.
  882. *
  883. * Build a node list tree copy of the XPointer result.
  884. * This will drop Attributes and Namespace declarations.
  885. *
  886. * Returns an xmlNodePtr list or NULL.
  887. * the caller has to free the node tree.
  888. */
  889. static xmlNodePtr
  890. xmlXIncludeCopyXPointer(xmlXIncludeCtxtPtr ctxt, xmlXPathObjectPtr obj) {
  891. xmlNodePtr list = NULL, last = NULL, copy;
  892. int i;
  893. if ((ctxt == NULL) || (obj == NULL))
  894. return(NULL);
  895. switch (obj->type) {
  896. case XPATH_NODESET: {
  897. xmlNodeSetPtr set = obj->nodesetval;
  898. if (set == NULL)
  899. return(NULL);
  900. for (i = 0;i < set->nodeNr;i++) {
  901. xmlNodePtr node;
  902. if (set->nodeTab[i] == NULL)
  903. continue;
  904. switch (set->nodeTab[i]->type) {
  905. case XML_DOCUMENT_NODE:
  906. case XML_HTML_DOCUMENT_NODE:
  907. node = xmlDocGetRootElement(
  908. (xmlDocPtr) set->nodeTab[i]);
  909. if (node == NULL) {
  910. xmlXIncludeErr(ctxt, set->nodeTab[i],
  911. XML_ERR_INTERNAL_ERROR,
  912. "document without root\n", NULL);
  913. continue;
  914. }
  915. break;
  916. case XML_TEXT_NODE:
  917. case XML_CDATA_SECTION_NODE:
  918. case XML_ELEMENT_NODE:
  919. case XML_PI_NODE:
  920. case XML_COMMENT_NODE:
  921. node = set->nodeTab[i];
  922. break;
  923. default:
  924. xmlXIncludeErr(ctxt, set->nodeTab[i],
  925. XML_XINCLUDE_XPTR_RESULT,
  926. "invalid node type in XPtr result\n",
  927. NULL);
  928. continue; /* for */
  929. }
  930. /*
  931. * OPTIMIZE TODO: External documents should already be
  932. * expanded, so xmlDocCopyNode should work as well.
  933. * xmlXIncludeCopyNode is only required for the initial
  934. * document.
  935. */
  936. copy = xmlXIncludeCopyNode(ctxt, node, 0);
  937. if (copy == NULL) {
  938. xmlFreeNodeList(list);
  939. return(NULL);
  940. }
  941. if (last == NULL) {
  942. list = copy;
  943. } else {
  944. while (last->next != NULL)
  945. last = last->next;
  946. copy->prev = last;
  947. last->next = copy;
  948. }
  949. last = copy;
  950. }
  951. break;
  952. }
  953. #ifdef LIBXML_XPTR_LOCS_ENABLED
  954. case XPATH_LOCATIONSET: {
  955. xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
  956. if (set == NULL)
  957. return(NULL);
  958. for (i = 0;i < set->locNr;i++) {
  959. if (last == NULL)
  960. list = last = xmlXIncludeCopyXPointer(ctxt,
  961. set->locTab[i]);
  962. else
  963. xmlAddNextSibling(last,
  964. xmlXIncludeCopyXPointer(ctxt, set->locTab[i]));
  965. if (last != NULL) {
  966. while (last->next != NULL)
  967. last = last->next;
  968. }
  969. }
  970. break;
  971. }
  972. case XPATH_RANGE:
  973. return(xmlXIncludeCopyRange(ctxt, obj));
  974. case XPATH_POINT:
  975. /* points are ignored in XInclude */
  976. break;
  977. #endif
  978. default:
  979. break;
  980. }
  981. return(list);
  982. }
  983. /************************************************************************
  984. * *
  985. * XInclude I/O handling *
  986. * *
  987. ************************************************************************/
  988. typedef struct _xmlXIncludeMergeData xmlXIncludeMergeData;
  989. typedef xmlXIncludeMergeData *xmlXIncludeMergeDataPtr;
  990. struct _xmlXIncludeMergeData {
  991. xmlDocPtr doc;
  992. xmlXIncludeCtxtPtr ctxt;
  993. };
  994. /**
  995. * xmlXIncludeMergeOneEntity:
  996. * @ent: the entity
  997. * @doc: the including doc
  998. * @name: the entity name
  999. *
  1000. * Implements the merge of one entity
  1001. */
  1002. static void
  1003. xmlXIncludeMergeEntity(void *payload, void *vdata,
  1004. const xmlChar *name ATTRIBUTE_UNUSED) {
  1005. xmlEntityPtr ent = (xmlEntityPtr) payload;
  1006. xmlXIncludeMergeDataPtr data = (xmlXIncludeMergeDataPtr) vdata;
  1007. xmlEntityPtr ret, prev;
  1008. xmlDocPtr doc;
  1009. xmlXIncludeCtxtPtr ctxt;
  1010. if ((ent == NULL) || (data == NULL))
  1011. return;
  1012. ctxt = data->ctxt;
  1013. doc = data->doc;
  1014. if ((ctxt == NULL) || (doc == NULL))
  1015. return;
  1016. switch (ent->etype) {
  1017. case XML_INTERNAL_PARAMETER_ENTITY:
  1018. case XML_EXTERNAL_PARAMETER_ENTITY:
  1019. case XML_INTERNAL_PREDEFINED_ENTITY:
  1020. return;
  1021. case XML_INTERNAL_GENERAL_ENTITY:
  1022. case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
  1023. case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
  1024. break;
  1025. }
  1026. ret = xmlAddDocEntity(doc, ent->name, ent->etype, ent->ExternalID,
  1027. ent->SystemID, ent->content);
  1028. if (ret != NULL) {
  1029. if (ent->URI != NULL)
  1030. ret->URI = xmlStrdup(ent->URI);
  1031. } else {
  1032. prev = xmlGetDocEntity(doc, ent->name);
  1033. if (prev != NULL) {
  1034. if (ent->etype != prev->etype)
  1035. goto error;
  1036. if ((ent->SystemID != NULL) && (prev->SystemID != NULL)) {
  1037. if (!xmlStrEqual(ent->SystemID, prev->SystemID))
  1038. goto error;
  1039. } else if ((ent->ExternalID != NULL) &&
  1040. (prev->ExternalID != NULL)) {
  1041. if (!xmlStrEqual(ent->ExternalID, prev->ExternalID))
  1042. goto error;
  1043. } else if ((ent->content != NULL) && (prev->content != NULL)) {
  1044. if (!xmlStrEqual(ent->content, prev->content))
  1045. goto error;
  1046. } else {
  1047. goto error;
  1048. }
  1049. }
  1050. }
  1051. return;
  1052. error:
  1053. switch (ent->etype) {
  1054. case XML_INTERNAL_PARAMETER_ENTITY:
  1055. case XML_EXTERNAL_PARAMETER_ENTITY:
  1056. case XML_INTERNAL_PREDEFINED_ENTITY:
  1057. case XML_INTERNAL_GENERAL_ENTITY:
  1058. case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
  1059. return;
  1060. case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
  1061. break;
  1062. }
  1063. xmlXIncludeErr(ctxt, (xmlNodePtr) ent, XML_XINCLUDE_ENTITY_DEF_MISMATCH,
  1064. "mismatch in redefinition of entity %s\n",
  1065. ent->name);
  1066. }
  1067. /**
  1068. * xmlXIncludeMergeEntities:
  1069. * @ctxt: an XInclude context
  1070. * @doc: the including doc
  1071. * @from: the included doc
  1072. *
  1073. * Implements the entity merge
  1074. *
  1075. * Returns 0 if merge succeeded, -1 if some processing failed
  1076. */
  1077. static int
  1078. xmlXIncludeMergeEntities(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc,
  1079. xmlDocPtr from) {
  1080. xmlNodePtr cur;
  1081. xmlDtdPtr target, source;
  1082. if (ctxt == NULL)
  1083. return(-1);
  1084. if ((from == NULL) || (from->intSubset == NULL))
  1085. return(0);
  1086. target = doc->intSubset;
  1087. if (target == NULL) {
  1088. cur = xmlDocGetRootElement(doc);
  1089. if (cur == NULL)
  1090. return(-1);
  1091. target = xmlCreateIntSubset(doc, cur->name, NULL, NULL);
  1092. if (target == NULL)
  1093. return(-1);
  1094. }
  1095. source = from->intSubset;
  1096. if ((source != NULL) && (source->entities != NULL)) {
  1097. xmlXIncludeMergeData data;
  1098. data.ctxt = ctxt;
  1099. data.doc = doc;
  1100. xmlHashScan((xmlHashTablePtr) source->entities,
  1101. xmlXIncludeMergeEntity, &data);
  1102. }
  1103. source = from->extSubset;
  1104. if ((source != NULL) && (source->entities != NULL)) {
  1105. xmlXIncludeMergeData data;
  1106. data.ctxt = ctxt;
  1107. data.doc = doc;
  1108. /*
  1109. * don't duplicate existing stuff when external subsets are the same
  1110. */
  1111. if ((!xmlStrEqual(target->ExternalID, source->ExternalID)) &&
  1112. (!xmlStrEqual(target->SystemID, source->SystemID))) {
  1113. xmlHashScan((xmlHashTablePtr) source->entities,
  1114. xmlXIncludeMergeEntity, &data);
  1115. }
  1116. }
  1117. return(0);
  1118. }
  1119. /**
  1120. * xmlXIncludeLoadDoc:
  1121. * @ctxt: the XInclude context
  1122. * @url: the associated URL
  1123. * @ref: an XMLXincludeRefPtr
  1124. *
  1125. * Load the document, and store the result in the XInclude context
  1126. *
  1127. * Returns 0 in case of success, -1 in case of failure
  1128. */
  1129. static int
  1130. xmlXIncludeLoadDoc(xmlXIncludeCtxtPtr ctxt, const xmlChar *url,
  1131. xmlXIncludeRefPtr ref) {
  1132. xmlXIncludeDocPtr cache;
  1133. xmlDocPtr doc;
  1134. xmlURIPtr uri;
  1135. xmlChar *URL = NULL;
  1136. xmlChar *fragment = NULL;
  1137. int i = 0;
  1138. int ret = -1;
  1139. int cacheNr;
  1140. #ifdef LIBXML_XPTR_ENABLED
  1141. int saveFlags;
  1142. #endif
  1143. /*
  1144. * Check the URL and remove any fragment identifier
  1145. */
  1146. uri = xmlParseURI((const char *)url);
  1147. if (uri == NULL) {
  1148. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
  1149. "invalid value URI %s\n", url);
  1150. goto error;
  1151. }
  1152. if (uri->fragment != NULL) {
  1153. fragment = (xmlChar *) uri->fragment;
  1154. uri->fragment = NULL;
  1155. }
  1156. if (ref->fragment != NULL) {
  1157. if (fragment != NULL) xmlFree(fragment);
  1158. fragment = xmlStrdup(ref->fragment);
  1159. }
  1160. URL = xmlSaveUri(uri);
  1161. xmlFreeURI(uri);
  1162. if (URL == NULL) {
  1163. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
  1164. "invalid value URI %s\n", url);
  1165. goto error;
  1166. }
  1167. /*
  1168. * Handling of references to the local document are done
  1169. * directly through ctxt->doc.
  1170. */
  1171. if ((URL[0] == 0) || (URL[0] == '#') ||
  1172. ((ctxt->doc != NULL) && (xmlStrEqual(URL, ctxt->doc->URL)))) {
  1173. doc = ctxt->doc;
  1174. goto loaded;
  1175. }
  1176. /*
  1177. * Prevent reloading the document twice.
  1178. */
  1179. for (i = 0; i < ctxt->urlNr; i++) {
  1180. if (xmlStrEqual(URL, ctxt->urlTab[i].url)) {
  1181. if (ctxt->urlTab[i].expanding) {
  1182. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_RECURSION,
  1183. "inclusion loop detected\n", NULL);
  1184. goto error;
  1185. }
  1186. doc = ctxt->urlTab[i].doc;
  1187. if (doc == NULL)
  1188. goto error;
  1189. goto loaded;
  1190. }
  1191. }
  1192. /*
  1193. * Load it.
  1194. */
  1195. #ifdef LIBXML_XPTR_ENABLED
  1196. /*
  1197. * If this is an XPointer evaluation, we want to assure that
  1198. * all entities have been resolved prior to processing the
  1199. * referenced document
  1200. */
  1201. saveFlags = ctxt->parseFlags;
  1202. if (fragment != NULL) { /* if this is an XPointer eval */
  1203. ctxt->parseFlags |= XML_PARSE_NOENT;
  1204. }
  1205. #endif
  1206. doc = xmlXIncludeParseFile(ctxt, (const char *)URL);
  1207. #ifdef LIBXML_XPTR_ENABLED
  1208. ctxt->parseFlags = saveFlags;
  1209. #endif
  1210. /* Also cache NULL docs */
  1211. if (ctxt->urlNr >= ctxt->urlMax) {
  1212. xmlXIncludeDoc *tmp;
  1213. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  1214. size_t newSize = ctxt->urlMax ? ctxt->urlMax * 2 : 1;
  1215. #else
  1216. size_t newSize = ctxt->urlMax ? ctxt->urlMax * 2 : 8;
  1217. #endif
  1218. tmp = xmlRealloc(ctxt->urlTab, sizeof(xmlXIncludeDoc) * newSize);
  1219. if (tmp == NULL) {
  1220. xmlXIncludeErrMemory(ctxt, ref->elem,
  1221. "growing XInclude URL table");
  1222. xmlFreeDoc(doc);
  1223. goto error;
  1224. }
  1225. ctxt->urlMax = newSize;
  1226. ctxt->urlTab = tmp;
  1227. }
  1228. cacheNr = ctxt->urlNr++;
  1229. cache = &ctxt->urlTab[cacheNr];
  1230. cache->doc = doc;
  1231. cache->url = xmlStrdup(URL);
  1232. cache->expanding = 0;
  1233. if (doc == NULL)
  1234. goto error;
  1235. /*
  1236. * It's possible that the requested URL has been mapped to a
  1237. * completely different location (e.g. through a catalog entry).
  1238. * To check for this, we compare the URL with that of the doc
  1239. * and change it if they disagree (bug 146988).
  1240. */
  1241. if (!xmlStrEqual(URL, doc->URL)) {
  1242. xmlFree(URL);
  1243. URL = xmlStrdup(doc->URL);
  1244. }
  1245. /*
  1246. * Make sure we have all entities fixed up
  1247. */
  1248. xmlXIncludeMergeEntities(ctxt, ctxt->doc, doc);
  1249. /*
  1250. * We don't need the DTD anymore, free up space
  1251. if (doc->intSubset != NULL) {
  1252. xmlUnlinkNode((xmlNodePtr) doc->intSubset);
  1253. xmlFreeNode((xmlNodePtr) doc->intSubset);
  1254. doc->intSubset = NULL;
  1255. }
  1256. if (doc->extSubset != NULL) {
  1257. xmlUnlinkNode((xmlNodePtr) doc->extSubset);
  1258. xmlFreeNode((xmlNodePtr) doc->extSubset);
  1259. doc->extSubset = NULL;
  1260. }
  1261. */
  1262. cache->expanding = 1;
  1263. xmlXIncludeRecurseDoc(ctxt, doc, URL);
  1264. /* urlTab might be reallocated. */
  1265. cache = &ctxt->urlTab[cacheNr];
  1266. cache->expanding = 0;
  1267. loaded:
  1268. if (fragment == NULL) {
  1269. /*
  1270. * Add the top children list as the replacement copy.
  1271. */
  1272. ref->inc = xmlDocCopyNode(xmlDocGetRootElement(doc), ctxt->doc, 1);
  1273. }
  1274. #ifdef LIBXML_XPTR_ENABLED
  1275. else {
  1276. /*
  1277. * Computes the XPointer expression and make a copy used
  1278. * as the replacement copy.
  1279. */
  1280. xmlXPathObjectPtr xptr;
  1281. xmlXPathContextPtr xptrctxt;
  1282. xmlNodeSetPtr set;
  1283. if (ctxt->isStream && doc == ctxt->doc) {
  1284. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
  1285. "XPointer expressions not allowed in streaming"
  1286. " mode\n", NULL);
  1287. goto error;
  1288. }
  1289. xptrctxt = xmlXPtrNewContext(doc, NULL, NULL);
  1290. if (xptrctxt == NULL) {
  1291. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
  1292. "could not create XPointer context\n", NULL);
  1293. goto error;
  1294. }
  1295. xptr = xmlXPtrEval(fragment, xptrctxt);
  1296. if (xptr == NULL) {
  1297. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_FAILED,
  1298. "XPointer evaluation failed: #%s\n",
  1299. fragment);
  1300. xmlXPathFreeContext(xptrctxt);
  1301. goto error;
  1302. }
  1303. switch (xptr->type) {
  1304. case XPATH_UNDEFINED:
  1305. case XPATH_BOOLEAN:
  1306. case XPATH_NUMBER:
  1307. case XPATH_STRING:
  1308. #ifdef LIBXML_XPTR_LOCS_ENABLED
  1309. case XPATH_POINT:
  1310. #endif
  1311. case XPATH_USERS:
  1312. case XPATH_XSLT_TREE:
  1313. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_XPTR_RESULT,
  1314. "XPointer is not a range: #%s\n",
  1315. fragment);
  1316. xmlXPathFreeObject(xptr);
  1317. xmlXPathFreeContext(xptrctxt);
  1318. goto error;
  1319. case XPATH_NODESET:
  1320. if ((xptr->nodesetval == NULL) ||
  1321. (xptr->nodesetval->nodeNr <= 0)) {
  1322. xmlXPathFreeObject(xptr);
  1323. xmlXPathFreeContext(xptrctxt);
  1324. goto error;
  1325. }
  1326. #ifdef LIBXML_XPTR_LOCS_ENABLED
  1327. case XPATH_RANGE:
  1328. case XPATH_LOCATIONSET:
  1329. break;
  1330. #endif
  1331. }
  1332. set = xptr->nodesetval;
  1333. if (set != NULL) {
  1334. for (i = 0;i < set->nodeNr;i++) {
  1335. if (set->nodeTab[i] == NULL)
  1336. continue;
  1337. switch (set->nodeTab[i]->type) {
  1338. case XML_ELEMENT_NODE:
  1339. case XML_TEXT_NODE:
  1340. case XML_CDATA_SECTION_NODE:
  1341. case XML_ENTITY_REF_NODE:
  1342. case XML_ENTITY_NODE:
  1343. case XML_PI_NODE:
  1344. case XML_COMMENT_NODE:
  1345. case XML_DOCUMENT_NODE:
  1346. case XML_HTML_DOCUMENT_NODE:
  1347. continue;
  1348. case XML_ATTRIBUTE_NODE:
  1349. xmlXIncludeErr(ctxt, ref->elem,
  1350. XML_XINCLUDE_XPTR_RESULT,
  1351. "XPointer selects an attribute: #%s\n",
  1352. fragment);
  1353. set->nodeTab[i] = NULL;
  1354. continue;
  1355. case XML_NAMESPACE_DECL:
  1356. xmlXIncludeErr(ctxt, ref->elem,
  1357. XML_XINCLUDE_XPTR_RESULT,
  1358. "XPointer selects a namespace: #%s\n",
  1359. fragment);
  1360. set->nodeTab[i] = NULL;
  1361. continue;
  1362. case XML_DOCUMENT_TYPE_NODE:
  1363. case XML_DOCUMENT_FRAG_NODE:
  1364. case XML_NOTATION_NODE:
  1365. case XML_DTD_NODE:
  1366. case XML_ELEMENT_DECL:
  1367. case XML_ATTRIBUTE_DECL:
  1368. case XML_ENTITY_DECL:
  1369. case XML_XINCLUDE_START:
  1370. case XML_XINCLUDE_END:
  1371. xmlXIncludeErr(ctxt, ref->elem,
  1372. XML_XINCLUDE_XPTR_RESULT,
  1373. "XPointer selects unexpected nodes: #%s\n",
  1374. fragment);
  1375. set->nodeTab[i] = NULL;
  1376. set->nodeTab[i] = NULL;
  1377. continue; /* for */
  1378. }
  1379. }
  1380. }
  1381. ref->inc = xmlXIncludeCopyXPointer(ctxt, xptr);
  1382. xmlXPathFreeObject(xptr);
  1383. xmlXPathFreeContext(xptrctxt);
  1384. }
  1385. #endif
  1386. /*
  1387. * Do the xml:base fixup if needed
  1388. */
  1389. if ((doc != NULL) && (URL != NULL) &&
  1390. (!(ctxt->parseFlags & XML_PARSE_NOBASEFIX)) &&
  1391. (!(doc->parseFlags & XML_PARSE_NOBASEFIX))) {
  1392. xmlNodePtr node;
  1393. xmlChar *base;
  1394. xmlChar *curBase;
  1395. /*
  1396. * The base is only adjusted if "necessary", i.e. if the xinclude node
  1397. * has a base specified, or the URL is relative
  1398. */
  1399. base = xmlGetNsProp(ref->elem, BAD_CAST "base", XML_XML_NAMESPACE);
  1400. if (base == NULL) {
  1401. /*
  1402. * No xml:base on the xinclude node, so we check whether the
  1403. * URI base is different than (relative to) the context base
  1404. */
  1405. curBase = xmlBuildRelativeURI(URL, ctxt->base);
  1406. if (curBase == NULL) { /* Error return */
  1407. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
  1408. "trying to build relative URI from %s\n", URL);
  1409. } else {
  1410. /* If the URI doesn't contain a slash, it's not relative */
  1411. if (!xmlStrchr(curBase, '/'))
  1412. xmlFree(curBase);
  1413. else
  1414. base = curBase;
  1415. }
  1416. }
  1417. if (base != NULL) { /* Adjustment may be needed */
  1418. node = ref->inc;
  1419. while (node != NULL) {
  1420. /* Only work on element nodes */
  1421. if (node->type == XML_ELEMENT_NODE) {
  1422. curBase = xmlNodeGetBase(node->doc, node);
  1423. /* If no current base, set it */
  1424. if (curBase == NULL) {
  1425. xmlNodeSetBase(node, base);
  1426. } else {
  1427. /*
  1428. * If the current base is the same as the
  1429. * URL of the document, then reset it to be
  1430. * the specified xml:base or the relative URI
  1431. */
  1432. if (xmlStrEqual(curBase, node->doc->URL)) {
  1433. xmlNodeSetBase(node, base);
  1434. } else {
  1435. /*
  1436. * If the element already has an xml:base
  1437. * set, then relativise it if necessary
  1438. */
  1439. xmlChar *xmlBase;
  1440. xmlBase = xmlGetNsProp(node,
  1441. BAD_CAST "base",
  1442. XML_XML_NAMESPACE);
  1443. if (xmlBase != NULL) {
  1444. xmlChar *relBase;
  1445. relBase = xmlBuildURI(xmlBase, base);
  1446. if (relBase == NULL) { /* error */
  1447. xmlXIncludeErr(ctxt,
  1448. ref->elem,
  1449. XML_XINCLUDE_HREF_URI,
  1450. "trying to rebuild base from %s\n",
  1451. xmlBase);
  1452. } else {
  1453. xmlNodeSetBase(node, relBase);
  1454. xmlFree(relBase);
  1455. }
  1456. xmlFree(xmlBase);
  1457. }
  1458. }
  1459. xmlFree(curBase);
  1460. }
  1461. }
  1462. node = node->next;
  1463. }
  1464. xmlFree(base);
  1465. }
  1466. }
  1467. ret = 0;
  1468. error:
  1469. xmlFree(URL);
  1470. xmlFree(fragment);
  1471. return(ret);
  1472. }
  1473. /**
  1474. * xmlXIncludeLoadTxt:
  1475. * @ctxt: the XInclude context
  1476. * @url: the associated URL
  1477. * @ref: an XMLXincludeRefPtr
  1478. *
  1479. * Load the content, and store the result in the XInclude context
  1480. *
  1481. * Returns 0 in case of success, -1 in case of failure
  1482. */
  1483. static int
  1484. xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url,
  1485. xmlXIncludeRefPtr ref) {
  1486. xmlParserInputBufferPtr buf;
  1487. xmlNodePtr node = NULL;
  1488. xmlURIPtr uri = NULL;
  1489. xmlChar *URL = NULL;
  1490. int i;
  1491. int ret = -1;
  1492. xmlChar *encoding = NULL;
  1493. xmlCharEncoding enc = (xmlCharEncoding) 0;
  1494. xmlParserCtxtPtr pctxt = NULL;
  1495. xmlParserInputPtr inputStream = NULL;
  1496. int len;
  1497. const xmlChar *content;
  1498. /* Don't read from stdin. */
  1499. if (xmlStrcmp(url, BAD_CAST "-") == 0)
  1500. url = BAD_CAST "./-";
  1501. /*
  1502. * Check the URL and remove any fragment identifier
  1503. */
  1504. uri = xmlParseURI((const char *)url);
  1505. if (uri == NULL) {
  1506. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
  1507. "invalid value URI %s\n", url);
  1508. goto error;
  1509. }
  1510. if (uri->fragment != NULL) {
  1511. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_TEXT_FRAGMENT,
  1512. "fragment identifier forbidden for text: %s\n",
  1513. (const xmlChar *) uri->fragment);
  1514. goto error;
  1515. }
  1516. URL = xmlSaveUri(uri);
  1517. if (URL == NULL) {
  1518. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_HREF_URI,
  1519. "invalid value URI %s\n", url);
  1520. goto error;
  1521. }
  1522. /*
  1523. * Handling of references to the local document are done
  1524. * directly through ctxt->doc.
  1525. */
  1526. if (URL[0] == 0) {
  1527. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_TEXT_DOCUMENT,
  1528. "text serialization of document not available\n", NULL);
  1529. goto error;
  1530. }
  1531. /*
  1532. * Prevent reloading the document twice.
  1533. */
  1534. for (i = 0; i < ctxt->txtNr; i++) {
  1535. if (xmlStrEqual(URL, ctxt->txtTab[i].url)) {
  1536. node = xmlNewDocText(ctxt->doc, ctxt->txtTab[i].text);
  1537. goto loaded;
  1538. }
  1539. }
  1540. /*
  1541. * Try to get the encoding if available
  1542. */
  1543. if (ref->elem != NULL) {
  1544. encoding = xmlGetProp(ref->elem, XINCLUDE_PARSE_ENCODING);
  1545. }
  1546. if (encoding != NULL) {
  1547. /*
  1548. * TODO: we should not have to remap to the xmlCharEncoding
  1549. * predefined set, a better interface than
  1550. * xmlParserInputBufferCreateFilename should allow any
  1551. * encoding supported by iconv
  1552. */
  1553. enc = xmlParseCharEncoding((const char *) encoding);
  1554. if (enc == XML_CHAR_ENCODING_ERROR) {
  1555. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_UNKNOWN_ENCODING,
  1556. "encoding %s not supported\n", encoding);
  1557. goto error;
  1558. }
  1559. }
  1560. /*
  1561. * Load it.
  1562. */
  1563. pctxt = xmlNewParserCtxt();
  1564. inputStream = xmlLoadExternalEntity((const char*)URL, NULL, pctxt);
  1565. if(inputStream == NULL)
  1566. goto error;
  1567. buf = inputStream->buf;
  1568. if (buf == NULL)
  1569. goto error;
  1570. if (buf->encoder)
  1571. xmlCharEncCloseFunc(buf->encoder);
  1572. buf->encoder = xmlGetCharEncodingHandler(enc);
  1573. node = xmlNewDocText(ctxt->doc, NULL);
  1574. if (node == NULL) {
  1575. xmlXIncludeErrMemory(ctxt, ref->elem, NULL);
  1576. goto error;
  1577. }
  1578. /*
  1579. * Scan all chars from the resource and add the to the node
  1580. */
  1581. while (xmlParserInputBufferRead(buf, 4096) > 0)
  1582. ;
  1583. content = xmlBufContent(buf->buffer);
  1584. len = xmlBufLength(buf->buffer);
  1585. for (i = 0; i < len;) {
  1586. int cur;
  1587. int l;
  1588. l = len - i;
  1589. cur = xmlGetUTF8Char(&content[i], &l);
  1590. if ((cur < 0) || (!IS_CHAR(cur))) {
  1591. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_INVALID_CHAR,
  1592. "%s contains invalid char\n", URL);
  1593. goto error;
  1594. }
  1595. i += l;
  1596. }
  1597. xmlNodeAddContentLen(node, content, len);
  1598. if (ctxt->txtNr >= ctxt->txtMax) {
  1599. xmlXIncludeTxt *tmp;
  1600. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  1601. size_t newSize = ctxt->txtMax ? ctxt->txtMax * 2 : 1;
  1602. #else
  1603. size_t newSize = ctxt->txtMax ? ctxt->txtMax * 2 : 8;
  1604. #endif
  1605. tmp = xmlRealloc(ctxt->txtTab, sizeof(xmlXIncludeTxt) * newSize);
  1606. if (tmp == NULL) {
  1607. xmlXIncludeErrMemory(ctxt, ref->elem,
  1608. "growing XInclude text table");
  1609. goto error;
  1610. }
  1611. ctxt->txtMax = newSize;
  1612. ctxt->txtTab = tmp;
  1613. }
  1614. ctxt->txtTab[ctxt->txtNr].text = xmlStrdup(node->content);
  1615. ctxt->txtTab[ctxt->txtNr].url = xmlStrdup(URL);
  1616. ctxt->txtNr++;
  1617. loaded:
  1618. /*
  1619. * Add the element as the replacement copy.
  1620. */
  1621. ref->inc = node;
  1622. node = NULL;
  1623. ret = 0;
  1624. error:
  1625. xmlFreeNode(node);
  1626. xmlFreeInputStream(inputStream);
  1627. xmlFreeParserCtxt(pctxt);
  1628. xmlFree(encoding);
  1629. xmlFreeURI(uri);
  1630. xmlFree(URL);
  1631. return(ret);
  1632. }
  1633. /**
  1634. * xmlXIncludeLoadFallback:
  1635. * @ctxt: the XInclude context
  1636. * @fallback: the fallback node
  1637. * @ref: an XMLXincludeRefPtr
  1638. *
  1639. * Load the content of the fallback node, and store the result
  1640. * in the XInclude context
  1641. *
  1642. * Returns 0 in case of success, -1 in case of failure
  1643. */
  1644. static int
  1645. xmlXIncludeLoadFallback(xmlXIncludeCtxtPtr ctxt, xmlNodePtr fallback,
  1646. xmlXIncludeRefPtr ref) {
  1647. int ret = 0;
  1648. int oldNbErrors;
  1649. if ((fallback == NULL) || (fallback->type == XML_NAMESPACE_DECL) ||
  1650. (ctxt == NULL))
  1651. return(-1);
  1652. if (fallback->children != NULL) {
  1653. /*
  1654. * It's possible that the fallback also has 'includes'
  1655. * (Bug 129969), so we re-process the fallback just in case
  1656. */
  1657. oldNbErrors = ctxt->nbErrors;
  1658. ref->inc = xmlXIncludeCopyNode(ctxt, fallback, 1);
  1659. if (ctxt->nbErrors > oldNbErrors)
  1660. ret = -1;
  1661. else if (ref->inc == NULL)
  1662. ref->emptyFb = 1;
  1663. } else {
  1664. ref->inc = NULL;
  1665. ref->emptyFb = 1; /* flag empty callback */
  1666. }
  1667. ref->fallback = 1;
  1668. return(ret);
  1669. }
  1670. /************************************************************************
  1671. * *
  1672. * XInclude Processing *
  1673. * *
  1674. ************************************************************************/
  1675. /**
  1676. * xmlXIncludeExpandNode:
  1677. * @ctxt: an XInclude context
  1678. * @node: an XInclude node
  1679. *
  1680. * If the XInclude node wasn't processed yet, create a new RefPtr,
  1681. * add it to ctxt->incTab and load the included items.
  1682. *
  1683. * Returns the new or existing xmlXIncludeRefPtr, or NULL in case of error.
  1684. */
  1685. static xmlXIncludeRefPtr
  1686. xmlXIncludeExpandNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
  1687. xmlXIncludeRefPtr ref;
  1688. int i;
  1689. if (ctxt->fatalErr)
  1690. return(NULL);
  1691. if (ctxt->depth >= XINCLUDE_MAX_DEPTH) {
  1692. xmlXIncludeErr(ctxt, node, XML_XINCLUDE_RECURSION,
  1693. "maximum recursion depth exceeded\n", NULL);
  1694. ctxt->fatalErr = 1;
  1695. return(NULL);
  1696. }
  1697. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  1698. /*
  1699. * The XInclude engine offers no protection against exponential
  1700. * expansion attacks similar to "billion laughs". Avoid timeouts by
  1701. * limiting the total number of replacements when fuzzing.
  1702. *
  1703. * Unfortuately, a single XInclude can already result in quadratic
  1704. * behavior:
  1705. *
  1706. * <doc xmlns:xi="http://www.w3.org/2001/XInclude">
  1707. * <xi:include xpointer="xpointer(//e)"/>
  1708. * <e>
  1709. * <e>
  1710. * <e>
  1711. * <!-- more nested elements -->
  1712. * </e>
  1713. * </e>
  1714. * </e>
  1715. * </doc>
  1716. */
  1717. if (ctxt->incTotal >= 20)
  1718. return(NULL);
  1719. ctxt->incTotal++;
  1720. #endif
  1721. for (i = 0; i < ctxt->incNr; i++) {
  1722. if (ctxt->incTab[i]->elem == node) {
  1723. if (ctxt->incTab[i]->expanding) {
  1724. xmlXIncludeErr(ctxt, node, XML_XINCLUDE_RECURSION,
  1725. "inclusion loop detected\n", NULL);
  1726. return(NULL);
  1727. }
  1728. return(ctxt->incTab[i]);
  1729. }
  1730. }
  1731. ref = xmlXIncludeAddNode(ctxt, node);
  1732. if (ref == NULL)
  1733. return(NULL);
  1734. ref->expanding = 1;
  1735. ctxt->depth++;
  1736. xmlXIncludeLoadNode(ctxt, ref);
  1737. ctxt->depth--;
  1738. ref->expanding = 0;
  1739. return(ref);
  1740. }
  1741. /**
  1742. * xmlXIncludeLoadNode:
  1743. * @ctxt: an XInclude context
  1744. * @ref: an xmlXIncludeRefPtr
  1745. *
  1746. * Find and load the infoset replacement for the given node.
  1747. *
  1748. * Returns 0 if substitution succeeded, -1 if some processing failed
  1749. */
  1750. static int
  1751. xmlXIncludeLoadNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref) {
  1752. xmlNodePtr cur;
  1753. xmlChar *href;
  1754. xmlChar *parse;
  1755. xmlChar *base;
  1756. xmlChar *oldBase;
  1757. xmlChar *URI;
  1758. int xml = 1; /* default Issue 64 */
  1759. int ret;
  1760. if ((ctxt == NULL) || (ref == NULL))
  1761. return(-1);
  1762. cur = ref->elem;
  1763. if (cur == NULL)
  1764. return(-1);
  1765. /*
  1766. * read the attributes
  1767. */
  1768. href = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_HREF);
  1769. if (href == NULL) {
  1770. href = xmlStrdup(BAD_CAST ""); /* @@@@ href is now optional */
  1771. if (href == NULL)
  1772. return(-1);
  1773. }
  1774. parse = xmlXIncludeGetProp(ctxt, cur, XINCLUDE_PARSE);
  1775. if (parse != NULL) {
  1776. if (xmlStrEqual(parse, XINCLUDE_PARSE_XML))
  1777. xml = 1;
  1778. else if (xmlStrEqual(parse, XINCLUDE_PARSE_TEXT))
  1779. xml = 0;
  1780. else {
  1781. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_PARSE_VALUE,
  1782. "invalid value %s for 'parse'\n", parse);
  1783. if (href != NULL)
  1784. xmlFree(href);
  1785. if (parse != NULL)
  1786. xmlFree(parse);
  1787. return(-1);
  1788. }
  1789. }
  1790. /*
  1791. * compute the URI
  1792. */
  1793. base = xmlNodeGetBase(ctxt->doc, cur);
  1794. if (base == NULL) {
  1795. URI = xmlBuildURI(href, ctxt->doc->URL);
  1796. } else {
  1797. URI = xmlBuildURI(href, base);
  1798. }
  1799. if (URI == NULL) {
  1800. xmlChar *escbase;
  1801. xmlChar *eschref;
  1802. /*
  1803. * Some escaping may be needed
  1804. */
  1805. escbase = xmlURIEscape(base);
  1806. eschref = xmlURIEscape(href);
  1807. URI = xmlBuildURI(eschref, escbase);
  1808. if (escbase != NULL)
  1809. xmlFree(escbase);
  1810. if (eschref != NULL)
  1811. xmlFree(eschref);
  1812. }
  1813. if (URI == NULL) {
  1814. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_HREF_URI,
  1815. "failed build URL\n", NULL);
  1816. if (parse != NULL)
  1817. xmlFree(parse);
  1818. if (href != NULL)
  1819. xmlFree(href);
  1820. if (base != NULL)
  1821. xmlFree(base);
  1822. return(-1);
  1823. }
  1824. /*
  1825. * Save the base for this include (saving the current one)
  1826. */
  1827. oldBase = ctxt->base;
  1828. ctxt->base = base;
  1829. if (xml) {
  1830. ret = xmlXIncludeLoadDoc(ctxt, URI, ref);
  1831. /* xmlXIncludeGetFragment(ctxt, cur, URI); */
  1832. } else {
  1833. ret = xmlXIncludeLoadTxt(ctxt, URI, ref);
  1834. }
  1835. /*
  1836. * Restore the original base before checking for fallback
  1837. */
  1838. ctxt->base = oldBase;
  1839. if (ret < 0) {
  1840. xmlNodePtr children;
  1841. /*
  1842. * Time to try a fallback if available
  1843. */
  1844. children = cur->children;
  1845. while (children != NULL) {
  1846. if ((children->type == XML_ELEMENT_NODE) &&
  1847. (children->ns != NULL) &&
  1848. (xmlStrEqual(children->name, XINCLUDE_FALLBACK)) &&
  1849. ((xmlStrEqual(children->ns->href, XINCLUDE_NS)) ||
  1850. (xmlStrEqual(children->ns->href, XINCLUDE_OLD_NS)))) {
  1851. ret = xmlXIncludeLoadFallback(ctxt, children, ref);
  1852. break;
  1853. }
  1854. children = children->next;
  1855. }
  1856. }
  1857. if (ret < 0) {
  1858. xmlXIncludeErr(ctxt, cur, XML_XINCLUDE_NO_FALLBACK,
  1859. "could not load %s, and no fallback was found\n",
  1860. URI);
  1861. }
  1862. /*
  1863. * Cleanup
  1864. */
  1865. if (URI != NULL)
  1866. xmlFree(URI);
  1867. if (parse != NULL)
  1868. xmlFree(parse);
  1869. if (href != NULL)
  1870. xmlFree(href);
  1871. if (base != NULL)
  1872. xmlFree(base);
  1873. return(0);
  1874. }
  1875. /**
  1876. * xmlXIncludeIncludeNode:
  1877. * @ctxt: an XInclude context
  1878. * @ref: an xmlXIncludeRefPtr
  1879. *
  1880. * Implement the infoset replacement for the given node
  1881. *
  1882. * Returns 0 if substitution succeeded, -1 if some processing failed
  1883. */
  1884. static int
  1885. xmlXIncludeIncludeNode(xmlXIncludeCtxtPtr ctxt, xmlXIncludeRefPtr ref) {
  1886. xmlNodePtr cur, end, list, tmp;
  1887. if ((ctxt == NULL) || (ref == NULL))
  1888. return(-1);
  1889. cur = ref->elem;
  1890. if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
  1891. return(-1);
  1892. list = ref->inc;
  1893. ref->inc = NULL;
  1894. ref->emptyFb = 0;
  1895. /*
  1896. * Check against the risk of generating a multi-rooted document
  1897. */
  1898. if ((cur->parent != NULL) &&
  1899. (cur->parent->type != XML_ELEMENT_NODE)) {
  1900. int nb_elem = 0;
  1901. tmp = list;
  1902. while (tmp != NULL) {
  1903. if (tmp->type == XML_ELEMENT_NODE)
  1904. nb_elem++;
  1905. tmp = tmp->next;
  1906. }
  1907. if (nb_elem > 1) {
  1908. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_MULTIPLE_ROOT,
  1909. "XInclude error: would result in multiple root nodes\n",
  1910. NULL);
  1911. xmlFreeNodeList(list);
  1912. return(-1);
  1913. }
  1914. }
  1915. if (ctxt->parseFlags & XML_PARSE_NOXINCNODE) {
  1916. /*
  1917. * Add the list of nodes
  1918. */
  1919. while (list != NULL) {
  1920. end = list;
  1921. list = list->next;
  1922. xmlAddPrevSibling(cur, end);
  1923. }
  1924. /*
  1925. * FIXME: xmlUnlinkNode doesn't coalesce text nodes.
  1926. */
  1927. xmlUnlinkNode(cur);
  1928. xmlFreeNode(cur);
  1929. } else {
  1930. xmlNodePtr child, next;
  1931. /*
  1932. * Change the current node as an XInclude start one, and add an
  1933. * XInclude end one
  1934. */
  1935. if (ref->fallback)
  1936. xmlUnsetProp(cur, BAD_CAST "href");
  1937. cur->type = XML_XINCLUDE_START;
  1938. /* Remove fallback children */
  1939. for (child = cur->children; child != NULL; child = next) {
  1940. next = child->next;
  1941. xmlUnlinkNode(child);
  1942. xmlFreeNode(child);
  1943. }
  1944. end = xmlNewDocNode(cur->doc, cur->ns, cur->name, NULL);
  1945. if (end == NULL) {
  1946. xmlXIncludeErr(ctxt, ref->elem, XML_XINCLUDE_BUILD_FAILED,
  1947. "failed to build node\n", NULL);
  1948. xmlFreeNodeList(list);
  1949. return(-1);
  1950. }
  1951. end->type = XML_XINCLUDE_END;
  1952. xmlAddNextSibling(cur, end);
  1953. /*
  1954. * Add the list of nodes
  1955. */
  1956. while (list != NULL) {
  1957. cur = list;
  1958. list = list->next;
  1959. xmlAddPrevSibling(end, cur);
  1960. }
  1961. }
  1962. return(0);
  1963. }
  1964. /**
  1965. * xmlXIncludeTestNode:
  1966. * @ctxt: the XInclude processing context
  1967. * @node: an XInclude node
  1968. *
  1969. * test if the node is an XInclude node
  1970. *
  1971. * Returns 1 true, 0 otherwise
  1972. */
  1973. static int
  1974. xmlXIncludeTestNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
  1975. if (node == NULL)
  1976. return(0);
  1977. if (node->type != XML_ELEMENT_NODE)
  1978. return(0);
  1979. if (node->ns == NULL)
  1980. return(0);
  1981. if ((xmlStrEqual(node->ns->href, XINCLUDE_NS)) ||
  1982. (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS))) {
  1983. if (xmlStrEqual(node->ns->href, XINCLUDE_OLD_NS)) {
  1984. if (ctxt->legacy == 0) {
  1985. #if 0 /* wait for the XML Core Working Group to get something stable ! */
  1986. xmlXIncludeWarn(ctxt, node, XML_XINCLUDE_DEPRECATED_NS,
  1987. "Deprecated XInclude namespace found, use %s",
  1988. XINCLUDE_NS);
  1989. #endif
  1990. ctxt->legacy = 1;
  1991. }
  1992. }
  1993. if (xmlStrEqual(node->name, XINCLUDE_NODE)) {
  1994. xmlNodePtr child = node->children;
  1995. int nb_fallback = 0;
  1996. while (child != NULL) {
  1997. if ((child->type == XML_ELEMENT_NODE) &&
  1998. (child->ns != NULL) &&
  1999. ((xmlStrEqual(child->ns->href, XINCLUDE_NS)) ||
  2000. (xmlStrEqual(child->ns->href, XINCLUDE_OLD_NS)))) {
  2001. if (xmlStrEqual(child->name, XINCLUDE_NODE)) {
  2002. xmlXIncludeErr(ctxt, node,
  2003. XML_XINCLUDE_INCLUDE_IN_INCLUDE,
  2004. "%s has an 'include' child\n",
  2005. XINCLUDE_NODE);
  2006. return(0);
  2007. }
  2008. if (xmlStrEqual(child->name, XINCLUDE_FALLBACK)) {
  2009. nb_fallback++;
  2010. }
  2011. }
  2012. child = child->next;
  2013. }
  2014. if (nb_fallback > 1) {
  2015. xmlXIncludeErr(ctxt, node, XML_XINCLUDE_FALLBACKS_IN_INCLUDE,
  2016. "%s has multiple fallback children\n",
  2017. XINCLUDE_NODE);
  2018. return(0);
  2019. }
  2020. return(1);
  2021. }
  2022. if (xmlStrEqual(node->name, XINCLUDE_FALLBACK)) {
  2023. if ((node->parent == NULL) ||
  2024. (node->parent->type != XML_ELEMENT_NODE) ||
  2025. (node->parent->ns == NULL) ||
  2026. ((!xmlStrEqual(node->parent->ns->href, XINCLUDE_NS)) &&
  2027. (!xmlStrEqual(node->parent->ns->href, XINCLUDE_OLD_NS))) ||
  2028. (!xmlStrEqual(node->parent->name, XINCLUDE_NODE))) {
  2029. xmlXIncludeErr(ctxt, node,
  2030. XML_XINCLUDE_FALLBACK_NOT_IN_INCLUDE,
  2031. "%s is not the child of an 'include'\n",
  2032. XINCLUDE_FALLBACK);
  2033. }
  2034. }
  2035. }
  2036. return(0);
  2037. }
  2038. /**
  2039. * xmlXIncludeDoProcess:
  2040. * @ctxt: the XInclude processing context
  2041. * @tree: the top of the tree to process
  2042. *
  2043. * Implement the XInclude substitution on the XML document @doc
  2044. *
  2045. * Returns 0 if no substitution were done, -1 if some processing failed
  2046. * or the number of substitutions done.
  2047. */
  2048. static int
  2049. xmlXIncludeDoProcess(xmlXIncludeCtxtPtr ctxt, xmlNodePtr tree) {
  2050. xmlXIncludeRefPtr ref;
  2051. xmlNodePtr cur;
  2052. int ret = 0;
  2053. int i, start;
  2054. if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL))
  2055. return(-1);
  2056. if (ctxt == NULL)
  2057. return(-1);
  2058. /*
  2059. * First phase: lookup the elements in the document
  2060. */
  2061. start = ctxt->incNr;
  2062. cur = tree;
  2063. do {
  2064. /* TODO: need to work on entities -> stack */
  2065. if (xmlXIncludeTestNode(ctxt, cur) == 1) {
  2066. ref = xmlXIncludeExpandNode(ctxt, cur);
  2067. /*
  2068. * Mark direct includes.
  2069. */
  2070. if (ref != NULL)
  2071. ref->replace = 1;
  2072. } else if ((cur->children != NULL) &&
  2073. ((cur->type == XML_DOCUMENT_NODE) ||
  2074. (cur->type == XML_ELEMENT_NODE))) {
  2075. cur = cur->children;
  2076. continue;
  2077. }
  2078. do {
  2079. if (cur == tree)
  2080. break;
  2081. if (cur->next != NULL) {
  2082. cur = cur->next;
  2083. break;
  2084. }
  2085. cur = cur->parent;
  2086. } while (cur != NULL);
  2087. } while ((cur != NULL) && (cur != tree));
  2088. /*
  2089. * Second phase: extend the original document infoset.
  2090. */
  2091. for (i = start; i < ctxt->incNr; i++) {
  2092. if (ctxt->incTab[i]->replace != 0) {
  2093. if ((ctxt->incTab[i]->inc != NULL) ||
  2094. (ctxt->incTab[i]->emptyFb != 0)) { /* (empty fallback) */
  2095. xmlXIncludeIncludeNode(ctxt, ctxt->incTab[i]);
  2096. }
  2097. ctxt->incTab[i]->replace = 0;
  2098. } else {
  2099. /*
  2100. * Ignore includes which were added indirectly, for example
  2101. * inside xi:fallback elements.
  2102. */
  2103. if (ctxt->incTab[i]->inc != NULL) {
  2104. xmlFreeNodeList(ctxt->incTab[i]->inc);
  2105. ctxt->incTab[i]->inc = NULL;
  2106. }
  2107. }
  2108. ret++;
  2109. }
  2110. if (ctxt->isStream) {
  2111. /*
  2112. * incTab references nodes which will eventually be deleted in
  2113. * streaming mode. The table is only required for XPointer
  2114. * expressions which aren't allowed in streaming mode.
  2115. */
  2116. for (i = 0;i < ctxt->incNr;i++) {
  2117. xmlXIncludeFreeRef(ctxt->incTab[i]);
  2118. }
  2119. ctxt->incNr = 0;
  2120. }
  2121. return(ret);
  2122. }
  2123. /**
  2124. * xmlXIncludeSetFlags:
  2125. * @ctxt: an XInclude processing context
  2126. * @flags: a set of xmlParserOption used for parsing XML includes
  2127. *
  2128. * Set the flags used for further processing of XML resources.
  2129. *
  2130. * Returns 0 in case of success and -1 in case of error.
  2131. */
  2132. int
  2133. xmlXIncludeSetFlags(xmlXIncludeCtxtPtr ctxt, int flags) {
  2134. if (ctxt == NULL)
  2135. return(-1);
  2136. ctxt->parseFlags = flags;
  2137. return(0);
  2138. }
  2139. /**
  2140. * xmlXIncludeSetStreamingMode:
  2141. * @ctxt: an XInclude processing context
  2142. * @mode: whether streaming mode should be enabled
  2143. *
  2144. * In streaming mode, XPointer expressions aren't allowed.
  2145. *
  2146. * Returns 0 in case of success and -1 in case of error.
  2147. */
  2148. int
  2149. xmlXIncludeSetStreamingMode(xmlXIncludeCtxtPtr ctxt, int mode) {
  2150. if (ctxt == NULL)
  2151. return(-1);
  2152. ctxt->isStream = !!mode;
  2153. return(0);
  2154. }
  2155. /**
  2156. * xmlXIncludeProcessTreeFlagsData:
  2157. * @tree: an XML node
  2158. * @flags: a set of xmlParserOption used for parsing XML includes
  2159. * @data: application data that will be passed to the parser context
  2160. * in the _private field of the parser context(s)
  2161. *
  2162. * Implement the XInclude substitution on the XML node @tree
  2163. *
  2164. * Returns 0 if no substitution were done, -1 if some processing failed
  2165. * or the number of substitutions done.
  2166. */
  2167. int
  2168. xmlXIncludeProcessTreeFlagsData(xmlNodePtr tree, int flags, void *data) {
  2169. xmlXIncludeCtxtPtr ctxt;
  2170. int ret = 0;
  2171. if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
  2172. (tree->doc == NULL))
  2173. return(-1);
  2174. ctxt = xmlXIncludeNewContext(tree->doc);
  2175. if (ctxt == NULL)
  2176. return(-1);
  2177. ctxt->_private = data;
  2178. ctxt->base = xmlStrdup((xmlChar *)tree->doc->URL);
  2179. xmlXIncludeSetFlags(ctxt, flags);
  2180. ret = xmlXIncludeDoProcess(ctxt, tree);
  2181. if ((ret >= 0) && (ctxt->nbErrors > 0))
  2182. ret = -1;
  2183. xmlXIncludeFreeContext(ctxt);
  2184. return(ret);
  2185. }
  2186. /**
  2187. * xmlXIncludeProcessFlagsData:
  2188. * @doc: an XML document
  2189. * @flags: a set of xmlParserOption used for parsing XML includes
  2190. * @data: application data that will be passed to the parser context
  2191. * in the _private field of the parser context(s)
  2192. *
  2193. * Implement the XInclude substitution on the XML document @doc
  2194. *
  2195. * Returns 0 if no substitution were done, -1 if some processing failed
  2196. * or the number of substitutions done.
  2197. */
  2198. int
  2199. xmlXIncludeProcessFlagsData(xmlDocPtr doc, int flags, void *data) {
  2200. xmlNodePtr tree;
  2201. if (doc == NULL)
  2202. return(-1);
  2203. tree = xmlDocGetRootElement(doc);
  2204. if (tree == NULL)
  2205. return(-1);
  2206. return(xmlXIncludeProcessTreeFlagsData(tree, flags, data));
  2207. }
  2208. /**
  2209. * xmlXIncludeProcessFlags:
  2210. * @doc: an XML document
  2211. * @flags: a set of xmlParserOption used for parsing XML includes
  2212. *
  2213. * Implement the XInclude substitution on the XML document @doc
  2214. *
  2215. * Returns 0 if no substitution were done, -1 if some processing failed
  2216. * or the number of substitutions done.
  2217. */
  2218. int
  2219. xmlXIncludeProcessFlags(xmlDocPtr doc, int flags) {
  2220. return xmlXIncludeProcessFlagsData(doc, flags, NULL);
  2221. }
  2222. /**
  2223. * xmlXIncludeProcess:
  2224. * @doc: an XML document
  2225. *
  2226. * Implement the XInclude substitution on the XML document @doc
  2227. *
  2228. * Returns 0 if no substitution were done, -1 if some processing failed
  2229. * or the number of substitutions done.
  2230. */
  2231. int
  2232. xmlXIncludeProcess(xmlDocPtr doc) {
  2233. return(xmlXIncludeProcessFlags(doc, 0));
  2234. }
  2235. /**
  2236. * xmlXIncludeProcessTreeFlags:
  2237. * @tree: a node in an XML document
  2238. * @flags: a set of xmlParserOption used for parsing XML includes
  2239. *
  2240. * Implement the XInclude substitution for the given subtree
  2241. *
  2242. * Returns 0 if no substitution were done, -1 if some processing failed
  2243. * or the number of substitutions done.
  2244. */
  2245. int
  2246. xmlXIncludeProcessTreeFlags(xmlNodePtr tree, int flags) {
  2247. xmlXIncludeCtxtPtr ctxt;
  2248. int ret = 0;
  2249. if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) ||
  2250. (tree->doc == NULL))
  2251. return(-1);
  2252. ctxt = xmlXIncludeNewContext(tree->doc);
  2253. if (ctxt == NULL)
  2254. return(-1);
  2255. ctxt->base = xmlNodeGetBase(tree->doc, tree);
  2256. xmlXIncludeSetFlags(ctxt, flags);
  2257. ret = xmlXIncludeDoProcess(ctxt, tree);
  2258. if ((ret >= 0) && (ctxt->nbErrors > 0))
  2259. ret = -1;
  2260. xmlXIncludeFreeContext(ctxt);
  2261. return(ret);
  2262. }
  2263. /**
  2264. * xmlXIncludeProcessTree:
  2265. * @tree: a node in an XML document
  2266. *
  2267. * Implement the XInclude substitution for the given subtree
  2268. *
  2269. * Returns 0 if no substitution were done, -1 if some processing failed
  2270. * or the number of substitutions done.
  2271. */
  2272. int
  2273. xmlXIncludeProcessTree(xmlNodePtr tree) {
  2274. return(xmlXIncludeProcessTreeFlags(tree, 0));
  2275. }
  2276. /**
  2277. * xmlXIncludeProcessNode:
  2278. * @ctxt: an existing XInclude context
  2279. * @node: a node in an XML document
  2280. *
  2281. * Implement the XInclude substitution for the given subtree reusing
  2282. * the information and data coming from the given context.
  2283. *
  2284. * Returns 0 if no substitution were done, -1 if some processing failed
  2285. * or the number of substitutions done.
  2286. */
  2287. int
  2288. xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
  2289. int ret = 0;
  2290. if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) ||
  2291. (node->doc == NULL) || (ctxt == NULL))
  2292. return(-1);
  2293. ret = xmlXIncludeDoProcess(ctxt, node);
  2294. if ((ret >= 0) && (ctxt->nbErrors > 0))
  2295. ret = -1;
  2296. return(ret);
  2297. }
  2298. #else /* !LIBXML_XINCLUDE_ENABLED */
  2299. #endif