xinclude.c 69 KB

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